4 * Description: Allow to embed info from friendica into another site
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix/>
8 use Friendica\Core\Addon;
9 use Friendica\Core\L10n;
10 use Friendica\Core\PConfig;
12 function widgets_install() {
13 Addon::registerHook('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
14 Addon::registerHook('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
15 logger("installed widgets");
17 function widgets_uninstall() {
18 Addon::unregisterHook('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
19 Addon::unregisterHook('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
23 function widgets_settings_post(){
26 if (isset($_POST['widgets-submit'])){
27 del_pconfig(local_user(), 'widgets', 'key');
32 function widgets_settings(&$a,&$o) {
37 $key = get_pconfig(local_user(), 'widgets', 'key' );
38 if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
41 $d = dir(dirname(__file__));
42 while(false !== ($f = $d->read())) {
43 if(substr($f,0,7)=="widget_") {
44 preg_match("|widget_([^.]+).php|", $f, $m);
48 $widgets[] = array($w, call_user_func($w."_widget_name"));
56 # $t = file_get_contents( dirname(__file__). "/settings.tpl" );
57 $t = get_markup_template("settings.tpl", "addon/widgets/");
58 $o .= replace_macros($t, [
59 '$submit' => L10n::t('Generate new key'),
60 '$baseurl' => $a->get_baseurl(),
61 '$title' => "Widgets",
62 '$label' => L10n::t('Widgets key'),
64 '$widgets_h' => L10n::t('Widgets available'),
65 '$widgets' => $widgets,
70 function widgets_module() {
74 function _abs_url($s){
76 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
79 function _randomAlphaNum($length){
80 return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
84 function widgets_content(&$a) {
86 if (!isset($_GET['k'])) {
87 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
91 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
95 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
99 $conf['uid'] = $r[0]['uid'];
100 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
104 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
105 if (file_exists($widgetfile)){
106 require_once($widgetfile);
108 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
115 //echo "<pre>"; var_dump($a->argv); die();
116 if ($a->argv[2]=="cb"){
117 /*header('Access-Control-Allow-Origin: *');*/
118 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
123 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
124 $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
125 $o .= "<h1>Preview Widget</h1>";
126 $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. L10n::t("Addon Settings") .'</a>';
128 $o .= "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
129 $o .= call_user_func($a->argv[1].'_widget_help');
130 $o .= "<br style='clear:left'/><br/>";
133 header("content-type: application/x-javascript");
138 $widget_size = call_user_func($a->argv[1].'_widget_size');
140 $script = file_get_contents(dirname(__file__)."/widgets.js");
141 $o .= replace_macros($script, array(
142 '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
143 '$key' => $conf['key'],
144 '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
145 '$loader' => $a->get_baseurl()."/images/rotator.gif",
146 '$args' => (isset($_GET['a'])?$_GET['a']:''),
147 '$width' => $widget_size[0],
148 '$height' => $widget_size[1],
149 '$type' => $a->argv[1],
153 if (isset($_GET['p'])) {
154 $wargs = call_user_func($a->argv[1].'_widget_args');
155 $jsargs = implode("</em>,<em>", $wargs);
156 if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
159 <br style='clear:left'/><br/>
160 <h4>Copy and paste this code</h4>
163 .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
165 .htmlspecialchars('"></script>')