]> git.mxchange.org Git - friendica-addons.git/blob - widgets/widgets.php
Merge commit 'upstream/master'
[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         $o .= replace_macros($t, array(
56                 '$submit' => t('Generate new key'),
57                 '$baseurl' => $a->get_baseurl(),
58                 '$title' => "Widgets",
59                 '$label' => t('Widgets key'),
60                 '$key' => $key,
61                 '$widgets_h' => t('Widgets available'),
62                 '$widgets' => $widgets,
63         ));
64         
65 }
66
67 function widgets_module() {
68         return;
69 }
70
71 function _abs_url($s){
72         $a = get_app();
73         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
74 }
75
76 function _randomAlphaNum($length){
77         return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
78
79
80
81 function widgets_content(&$a) {
82
83         if (!isset($_GET['k'])) {
84                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
85                 return;
86         }
87
88         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
89                         dbesc($_GET['k'])
90                  );
91         if (!count($r)){
92                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
93                 return;
94         }    
95         $conf = array();
96         $conf['uid'] = $r[0]['uid'];
97         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
98         
99         $o = "";        
100
101         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
102         if (file_exists($widgetfile)){
103                 require_once($widgetfile);
104         } else {
105                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
106                 return;
107         }               
108         
109
110
111
112         //echo "<pre>"; var_dump($a->argv); die();
113         if ($a->argv[2]=="cb"){
114                 /*header('Access-Control-Allow-Origin: *');*/
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                 $widget_size = call_user_func($a->argv[1].'_widget_size');
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' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
142                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
143                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
144                         '$width' => $widget_size[0],
145                         '$height' => $widget_size[1],
146                         '$type' => $a->argv[1],
147                 ));
148
149         
150                 if (isset($_GET['p'])) {
151                         $wargs = call_user_func($a->argv[1].'_widget_args');
152                         $jsargs = implode("</em>,<em>", $wargs);
153                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
154                                 
155                         $o .= "</script>
156                         <br style='clear:left'/><br/>
157                         <h4>Copy and paste this code</h4>
158                         <code>"
159                         
160                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
161                         .$jsargs
162                         .htmlspecialchars('"></script>')
163                         ."</code>";
164
165                         
166                         return $o;
167                 }       
168                 
169         }       
170         
171         echo $o;
172         killme();
173 }
174
175
176
177  
178 ?>