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