Official Addons for the Friendica Communications Platform. (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.4 KiB

  1. <?php
  2. /**
  3. * Name: Widgets
  4. * Description: Allow to embed info from friendika into another site
  5. * Version: 1.0
  6. * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix/>
  7. */
  8. function widgets_install() {
  9. register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
  10. register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
  11. logger("installed widgets");
  12. }
  13. function widgets_uninstall() {
  14. unregister_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
  15. unregister_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
  16. }
  17. function widgets_settings_post(){
  18. if (isset($_POST['widgets-submit'])){
  19. del_pconfig(local_user(), 'widgets', 'key');
  20. }
  21. }
  22. function widgets_settings(&$a,&$o) {
  23. if(! local_user())
  24. return;
  25. $key = get_pconfig(local_user(), 'widgets', 'key' );
  26. if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
  27. $widgets = array();
  28. $d = dir(dirname(__file__));
  29. while(false !== ($f = $d->read())) {
  30. if(substr($f,0,7)=="widget_") {
  31. preg_match("|widget_([^.]+).php|", $f, $m);
  32. $w=$m[1];
  33. if ($w!=""){
  34. require_once($f);
  35. $widgets[] = array($w, call_user_func($w."_widget_name"));
  36. }
  37. }
  38. }
  39. $t = file_get_contents( dirname(__file__). "/settings.tpl" );
  40. $o .= replace_macros($t, array(
  41. '$submit' => t('Generate new key'),
  42. '$baseurl' => $a->get_baseurl(),
  43. '$title' => "Widgets",
  44. '$label' => t('Widgets key'),
  45. '$key' => $key,
  46. '$widgets_h' => t('Widgets available'),
  47. '$widgets' => $widgets,
  48. ));
  49. }
  50. function widgets_module() {
  51. return;
  52. }
  53. function _abs_url($s){
  54. $a = get_app();
  55. return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
  56. }
  57. function _randomAlphaNum($length){
  58. return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
  59. }
  60. function widgets_content(&$a) {
  61. if (!isset($_GET['k'])) {
  62. if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
  63. return;
  64. }
  65. $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
  66. dbesc($_GET['k'])
  67. );
  68. if (!count($r)){
  69. if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
  70. return;
  71. }
  72. $conf = array();
  73. $conf['uid'] = $r[0]['uid'];
  74. foreach($r as $e) { $conf[$e['k']]=$e['v']; }
  75. $o = "";
  76. $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
  77. if (file_exists($widgetfile)){
  78. require_once($widgetfile);
  79. } else {
  80. if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
  81. return;
  82. }
  83. //echo "<pre>"; var_dump($a->argv); die();
  84. if ($a->argv[2]=="cb"){
  85. /*header('Access-Control-Allow-Origin: *');*/
  86. $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
  87. } else {
  88. if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
  89. $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
  90. $o .= "<h1>Preview Widget</h1>";
  91. $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
  92. $o .= "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
  93. $o .= call_user_func($a->argv[1].'_widget_help');
  94. $o .= "<br style='clear:left'/><br/>";
  95. $o .= "<script>";
  96. } else {
  97. header("content-type: application/x-javascript");
  98. }
  99. $widget_size = call_user_func($a->argv[1].'_widget_size');
  100. $script = file_get_contents(dirname(__file__)."/widgets.js");
  101. $o .= replace_macros($script, array(
  102. '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
  103. '$key' => $conf['key'],
  104. '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
  105. '$loader' => $a->get_baseurl()."/images/rotator.gif",
  106. '$args' => (isset($_GET['a'])?$_GET['a']:''),
  107. '$width' => $widget_size[0],
  108. '$height' => $widget_size[1],
  109. '$type' => $a->argv[1],
  110. ));
  111. if (isset($_GET['p'])) {
  112. $wargs = call_user_func($a->argv[1].'_widget_args');
  113. $jsargs = implode("</em>,<em>", $wargs);
  114. if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
  115. $o .= "</script>
  116. <br style='clear:left'/><br/>
  117. <h4>Copy and paste this code</h4>
  118. <code>"
  119. .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
  120. .$jsargs
  121. .htmlspecialchars('"></script>')
  122. ."</code>";
  123. return $o;
  124. }
  125. }
  126. echo $o;
  127. killme();
  128. }
  129. ?>