]> git.mxchange.org Git - friendica-addons.git/blob - widgets/widgets.php
Merge pull request #4 from fabrixxm/master
[friendica-addons.git] / widgets / widgets.php
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  
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         
23         if (isset($_POST['widgets-submit'])){
24                 del_pconfig(local_user(), 'widgets', 'key');
25                 
26         }
27 }
28
29 function widgets_settings(&$a,&$o) {
30     if(! local_user())
31                 return;         
32         
33         
34         $key = get_pconfig(local_user(), 'widgets', 'key' );
35         if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
36
37         $widgets = array();
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);
42                          $w=$m[1];
43                          if ($w!=""){
44                                 require_once($f);
45                                 $widgets[] = array($w, call_user_func($w."_widget_name"));
46                         }
47
48                  }
49         }
50
51         
52         
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'),
59                 '$key' => $key,
60                 '$widgets_h' => t('Widgets available'),
61                 '$widgets' => $widgets,
62         ));
63         
64 }
65
66 function widgets_module() {
67         return;
68 }
69
70 function _abs_url($s){
71         $a = get_app();
72         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
73 }
74
75
76 function widgets_content(&$a) {
77
78         if (!isset($_GET['k'])) {
79                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
80                 return;
81         }
82
83         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
84                         dbesc($_GET['k'])
85                  );
86         if (!count($r)){
87                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
88                 return;
89         }    
90         $conf = array();
91         $conf['uid'] = $r[0]['uid'];
92         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
93         
94         $o = "";        
95
96         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
97         if (file_exists($widgetfile)){
98                 require_once($widgetfile);
99         } else {
100                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
101                 return;
102         }               
103         
104
105
106
107         //echo "<pre>"; var_dump($a->argv); die();
108         if ($a->argv[2]=="cb"){
109                 header('Access-Control-Allow-Origin: *');
110                 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
111                 
112         } else {
113
114                 
115                 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
116                         $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
117                         $o .= "<h1>Preview Widget</h1>";
118                         $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
119
120                         $o .=  "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
121                         $o .=  call_user_func($a->argv[1].'_widget_help');
122                         $o .= "<br style='clear:left'/><br/>";
123                         $o .= "<script>";
124                 } else {
125                         header("content-type: application/x-javascript");
126                 }
127         
128         
129
130         
131                 $script = file_get_contents(dirname(__file__)."/widgets.js");
132                 $o .= replace_macros($script, array(
133                         '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
134                         '$key' => $conf['key'],
135                         '$widget_id' => 'f9a_'.$a->argv[1]."_". ceil(microtime(true)*100),
136                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
137                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
138                 ));
139
140         
141                 if (isset($_GET['p'])) {
142                         $wargs = call_user_func($a->argv[1].'_widget_args');
143                         $jsargs = implode("</em>,<em>", $wargs);
144                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
145                                 
146                         $o .= "</script>
147                         <br style='clear:left'/><br/>
148                         <h4>Copy and paste this code</h4>
149                         <code>"
150                         
151                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
152                         .$jsargs
153                         .htmlspecialchars('"></script>')
154                         ."</code>";
155
156                         
157                         return $o;
158                 }       
159                 
160         }       
161         
162         echo $o;
163         killme();
164 }
165
166
167
168  
169 ?>