4 * Description: Allow to embed info from friendica into another site
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix/>
10 function widgets_install() {
11 register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
12 register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
13 logger("installed widgets");
15 function widgets_uninstall() {
16 unregister_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
17 unregister_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
21 function widgets_settings_post(){
23 if (isset($_POST['widgets-submit'])){
24 del_pconfig(local_user(), 'widgets', 'key');
29 function widgets_settings(&$a,&$o) {
34 $key = get_pconfig(local_user(), 'widgets', 'key' );
35 if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
38 $d = dir(dirname(__file__));
39 while(false !== ($f = $d->read())) {
40 if(substr($f,0,7)=="widget_") {
41 preg_match("|widget_([^.]+).php|", $f, $m);
45 $widgets[] = array($w, call_user_func($w."_widget_name"));
53 $t = file_get_contents( dirname(__file__). "/settings.tpl" );
54 $o .= replace_macros($t, array(
55 '$submit' => t('Generate new key'),
56 '$baseurl' => $a->get_baseurl(),
57 '$title' => "Widgets",
58 '$label' => t('Widgets key'),
60 '$widgets_h' => t('Widgets available'),
61 '$widgets' => $widgets,
66 function widgets_module() {
70 function _abs_url($s){
72 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
75 function _randomAlphaNum($length){
76 return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
80 function widgets_content(&$a) {
82 if (!isset($_GET['k'])) {
83 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
87 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
91 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
95 $conf['uid'] = $r[0]['uid'];
96 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
100 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
101 if (file_exists($widgetfile)){
102 require_once($widgetfile);
104 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
111 //echo "<pre>"; var_dump($a->argv); die();
112 if ($a->argv[2]=="cb"){
113 /*header('Access-Control-Allow-Origin: *');*/
114 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
119 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
120 $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
121 $o .= "<h1>Preview Widget</h1>";
122 $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
124 $o .= "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
125 $o .= call_user_func($a->argv[1].'_widget_help');
126 $o .= "<br style='clear:left'/><br/>";
129 header("content-type: application/x-javascript");
134 $widget_size = call_user_func($a->argv[1].'_widget_size');
136 $script = file_get_contents(dirname(__file__)."/widgets.js");
137 $o .= replace_macros($script, array(
138 '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
139 '$key' => $conf['key'],
140 '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
141 '$loader' => $a->get_baseurl()."/images/rotator.gif",
142 '$args' => (isset($_GET['a'])?$_GET['a']:''),
143 '$width' => $widget_size[0],
144 '$height' => $widget_size[1],
145 '$type' => $a->argv[1],
149 if (isset($_GET['p'])) {
150 $wargs = call_user_func($a->argv[1].'_widget_args');
151 $jsargs = implode("</em>,<em>", $wargs);
152 if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
155 <br style='clear:left'/><br/>
156 <h4>Copy and paste this code</h4>
159 .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
161 .htmlspecialchars('"></script>')