3 * widgets from friendika
5 * allow to embed info from friendika into another site
9 function widgets_install() {
10 // we need some hooks, for the configuration and for sending tweets
11 register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
12 register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
14 logger("installed widgets");
17 function widgets_settings_post(){
19 if (isset($_POST['widgets-submit'])){
20 del_pconfig(local_user(), 'widgets', 'key');
25 function widgets_settings(&$a,&$o) {
30 $key = get_pconfig(local_user(), 'widgets', 'key' );
31 if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
33 $o .='<h3 class="settings-heading">Widgets</h3>';
37 <div id="settings-username-wrapper">
38 '. t('Widgets key: ') .'<strong>'.$key.'</strong>
40 <div id="settings-username-end"></div>
41 <div class="settings-submit-wrapper">
42 <input type="submit" value="'.t('Generate new key').'" class="settings-submit" name="widgets-submit">
46 $o.='<h4>Widgets:</h4>';
48 $d = dir(dirname(__file__));
49 while(false !== ($f = $d->read())) {
50 if(substr($f,0,7)=="widget_") {
51 preg_match("|widget_([^.]+).php|", $f, $m);
54 $o.='<li><a href="'.$a->get_baseurl().'/widgets/'.$w.'/?k='.$key.'&p=1">'. call_user_func($w."_widget_name") .'</a></li>';
62 function widgets_module() {
66 function _abs_url($s){
68 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
72 function widgets_content(&$a) {
74 if (!isset($_GET['k'])) {
75 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
79 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
83 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
87 $conf['uid'] = $r[0]['uid'];
88 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
92 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
93 if (file_exists($widgetfile)){
94 require_once($widgetfile);
96 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
103 //echo "<pre>"; var_dump($a->argv); die();
104 if ($a->argv[2]=="cb"){
105 /*if (!local_user()){
106 if (!isset($_GET['s']))
107 {header('HTTP/1.0 400 Bad Request'); killme();}
109 if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
110 {header('HTTP/1.0 400 Bad Request'); killme();}
112 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
117 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
118 $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
119 $o .= "<h1>Preview Widget</h1>";
120 $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
122 $o .= "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
123 $o .= call_user_func($a->argv[1].'_widget_help');
124 $o .= "<br style='clear:left'/><br/>";
127 header("content-type: application/x-javascript");
133 $script = file_get_contents(dirname(__file__)."/widgets.js");
134 $o .= replace_macros($script, array(
135 '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
136 '$key' => $conf['key'],
137 '$widget_id' => 'f9k_'.$a->argv[1]."_".time(),
138 '$loader' => $a->get_baseurl()."/images/rotator.gif",
139 '$args' => (isset($_GET['a'])?$_GET['a']:''),
143 if (isset($_GET['p'])) {
144 $jsargs = implode("</em>,<em>", call_user_func($a->argv[1].'_widget_args'));
145 if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
148 <br style='clear:left'/><br/>
149 <h4>Copy and paste this code</h4>
152 .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
154 .htmlspecialchars('"></script>')