]> git.mxchange.org Git - friendica.git/blob - addon/widgets/widgets.php
af17d9e9aff8910c9c927eb44b874b9268017dd4
[friendica.git] / addon / widgets / widgets.php
1 <?php
2         /**
3          * widgets from friendika
4          * 
5          * allow to embed info from friendika into another site
6          */
7          
8          
9 function widgets_install() {
10         register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings'); 
11         register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
12         logger("installed widgets");
13 }
14 function widgets_uninstall() {
15         unregister_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings'); 
16         unregister_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
17 }
18
19
20 function widgets_settings_post(){
21         
22         if (isset($_POST['widgets-submit'])){
23                 del_pconfig(local_user(), 'widgets', 'key');
24                 
25         }
26 }
27
28 function widgets_settings(&$a,&$o) {
29     if(! local_user())
30                 return;         
31         
32         
33         $key = get_pconfig(local_user(), 'widgets', 'key' );
34         if ($key=='') { $key = mt_rand(); set_pconfig(local_user(), 'widgets', 'key', $key); }
35         
36         $o .='<h3 class="settings-heading">Widgets</h3>';
37         
38         
39         $o.='
40         <div id="settings-username-wrapper">
41                 '. t('Widgets key: ') .'<strong>'.$key.'</strong>
42         </div>
43         <div id="settings-username-end"></div>
44         <div class="settings-submit-wrapper">
45                 <input type="submit" value="'.t('Generate new key').'" class="settings-submit" name="widgets-submit">
46         </div>';
47         
48         
49         $o.='<h4>Widgets:</h4>';
50         $o .= '<ul>';
51         $d = dir(dirname(__file__));
52         while(false !== ($f = $d->read())) {
53                  if(substr($f,0,7)=="widget_") {
54                          preg_match("|widget_([^.]+).php|", $f, $m);
55                          $w=$m[1];
56                          require_once($f);
57                          $o.='<li><a href="'.$a->get_baseurl().'/widgets/'.$w.'/?k='.$key.'&p=1">'. call_user_func($w."_widget_name") .'</a></li>';
58                  }
59         }
60
61         $o .= '</ul>';
62
63 }
64
65 function widgets_module() {
66         return;
67 }
68
69 function _abs_url($s){
70         $a = get_app();
71         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
72 }
73
74
75 function widgets_content(&$a) {
76
77         if (!isset($_GET['k'])) {
78                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
79                 return;
80         }
81
82         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
83                         dbesc($_GET['k'])
84                  );
85         if (!count($r)){
86                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
87                 return;
88         }    
89         $conf = array();
90         $conf['uid'] = $r[0]['uid'];
91         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
92         
93         $o = "";        
94
95         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
96         if (file_exists($widgetfile)){
97                 require_once($widgetfile);
98         } else {
99                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
100                 return;
101         }               
102         
103
104
105
106         //echo "<pre>"; var_dump($a->argv); die();
107         if ($a->argv[2]=="cb"){
108                 /*if (!local_user()){
109                         if (!isset($_GET['s']))
110                                 {header('HTTP/1.0 400 Bad Request'); killme();}
111                         
112                         if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
113                                 {header('HTTP/1.0 400 Bad Request'); killme();}
114                 } */
115                 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
116                 
117         } else {
118
119                 
120                 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
121                         $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
122                         $o .= "<h1>Preview Widget</h1>";
123                         $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
124
125                         $o .=  "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
126                         $o .=  call_user_func($a->argv[1].'_widget_help');
127                         $o .= "<br style='clear:left'/><br/>";
128                         $o .= "<script>";
129                 } else {
130                         header("content-type: application/x-javascript");
131                 }
132         
133         
134
135         
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' => 'f9k_'.$a->argv[1]."_".time(),
141                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
142                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
143                 ));
144
145         
146                 if (isset($_GET['p'])) {
147                         $jsargs = implode("</em>,<em>", call_user_func($a->argv[1].'_widget_args'));
148                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
149                                 
150                         $o .= "</script>
151                         <br style='clear:left'/><br/>
152                         <h4>Copy and paste this code</h4>
153                         <code>"
154                         
155                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
156                         .$jsargs
157                         .htmlspecialchars('"></script>')
158                         ."</code>";
159                         return $o;
160                 }       
161                 
162         }       
163         
164         echo $o;
165         killme();
166 }
167
168
169
170  
171 ?>