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