]> git.mxchange.org Git - friendica.git/blob - addon/widgets/widgets.php
f5f8682223b91a6226f32eaf4bdf7667ad99e7b6
[friendica.git] / addon / 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         $o .='<h3 class="settings-heading">Widgets</h3>';
38         
39         
40         $o.='
41         <div id="settings-username-wrapper">
42                 '. t('Widgets key: ') .'<strong>'.$key.'</strong>
43         </div>
44         <div id="settings-username-end"></div>
45         <div class="settings-submit-wrapper">
46                 <input type="submit" value="'.t('Generate new key').'" class="settings-submit" name="widgets-submit">
47         </div>';
48         
49         
50         $o.='<h4>Widgets:</h4>';
51         $o .= '<ul>';
52         $d = dir(dirname(__file__));
53         while(false !== ($f = $d->read())) {
54                  if(substr($f,0,7)=="widget_") {
55                          preg_match("|widget_([^.]+).php|", $f, $m);
56                          $w=$m[1];
57                          require_once($f);
58                          $o.='<li><a href="'.$a->get_baseurl().'/widgets/'.$w.'/?k='.$key.'&p=1">'. call_user_func($w."_widget_name") .'</a></li>';
59                  }
60         }
61
62         $o .= '</ul>';
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                 /*if (!local_user()){
110                         if (!isset($_GET['s']))
111                                 {header('HTTP/1.0 400 Bad Request'); killme();}
112                         
113                         if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
114                                 {header('HTTP/1.0 400 Bad Request'); killme();}
115                 } */
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         
137                 $script = file_get_contents(dirname(__file__)."/widgets.js");
138                 $o .= replace_macros($script, array(
139                         '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
140                         '$key' => $conf['key'],
141                         '$widget_id' => 'f9k_'.$a->argv[1]."_".time(),
142                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
143                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
144                 ));
145
146         
147                 if (isset($_GET['p'])) {
148                         $jsargs = implode("</em>,<em>", call_user_func($a->argv[1].'_widget_args'));
149                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
150                                 
151                         $o .= "</script>
152                         <br style='clear:left'/><br/>
153                         <h4>Copy and paste this code</h4>
154                         <code>"
155                         
156                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
157                         .$jsargs
158                         .htmlspecialchars('"></script>')
159                         ."</code>";
160                         return $o;
161                 }       
162                 
163         }       
164         
165         echo $o;
166         killme();
167 }
168
169
170
171  
172 ?>