502278a278e91ebbf1a52dbe5712e2afc1b1bbc3
[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 use Friendica\Core\PConfig;
10
11 function widgets_install() {
12         register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
13         register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
14         logger("installed widgets");
15 }
16 function widgets_uninstall() {
17         unregister_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings');
18         unregister_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
19 }
20
21
22 function widgets_settings_post(){
23         if(! local_user())
24                 return;
25         if (isset($_POST['widgets-submit'])){
26                 PConfig::delete(local_user(), 'widgets', 'key');
27
28         }
29 }
30
31 function widgets_settings(&$a,&$o) {
32     if(! local_user())
33                 return;
34
35
36         $key = PConfig::get(local_user(), 'widgets', 'key' );
37         if ($key=='') { $key = mt_rand(); PConfig::set(local_user(), 'widgets', 'key', $key); }
38
39         $widgets = [];
40         $d = dir(dirname(__file__));
41         while(false !== ($f = $d->read())) {
42                  if(substr($f,0,7)=="widget_") {
43                          preg_match("|widget_([^.]+).php|", $f, $m);
44                          $w=$m[1];
45                          if ($w!=""){
46                                 require_once($f);
47                                 $widgets[] = [$w, call_user_func($w."_widget_name")];
48                         }
49
50                  }
51         }
52
53
54
55 #       $t = file_get_contents( dirname(__file__). "/settings.tpl" );
56         $t = get_markup_template("settings.tpl", "addon/widgets/");
57         $o .= replace_macros($t, [
58                 '$submit' => t('Generate new key'),
59                 '$baseurl' => $a->get_baseurl(),
60                 '$title' => "Widgets",
61                 '$label' => t('Widgets key'),
62                 '$key' => $key,
63                 '$widgets_h' => t('Widgets available'),
64                 '$widgets' => $widgets,
65         ]);
66
67 }
68
69 function widgets_module() {
70         return;
71 }
72
73 function _abs_url($s){
74         $a = get_app();
75         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
76 }
77
78 function _randomAlphaNum($length){
79         return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
80 }
81
82
83 function widgets_content(&$a) {
84
85         if (!isset($_GET['k'])) {
86                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
87                 return;
88         }
89
90         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
91                         dbesc($_GET['k'])
92                  );
93         if (!count($r)){
94                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
95                 return;
96         }
97         $conf = [];
98         $conf['uid'] = $r[0]['uid'];
99         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
100
101         $o = "";
102
103         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
104         if (file_exists($widgetfile)){
105                 require_once($widgetfile);
106         } else {
107                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
108                 return;
109         }
110
111
112
113
114         //echo "<pre>"; var_dump($a->argv); die();
115         if ($a->argv[2]=="cb"){
116                 /*header('Access-Control-Allow-Origin: *');*/
117                 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
118
119         } else {
120
121
122                 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
123                         $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
124                         $o .= "<h1>Preview Widget</h1>";
125                         $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
126
127                         $o .=  "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
128                         $o .=  call_user_func($a->argv[1].'_widget_help');
129                         $o .= "<br style='clear:left'/><br/>";
130                         $o .= "<script>";
131                 } else {
132                         header("content-type: application/x-javascript");
133                 }
134
135
136
137                 $widget_size = call_user_func($a->argv[1].'_widget_size');
138
139                 $script = file_get_contents(dirname(__file__)."/widgets.js");
140                 $o .= replace_macros($script, [
141                         '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
142                         '$key' => $conf['key'],
143                         '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
144                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
145                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
146                         '$width' => $widget_size[0],
147                         '$height' => $widget_size[1],
148                         '$type' => $a->argv[1],
149                 ]);
150
151
152                 if (isset($_GET['p'])) {
153                         $wargs = call_user_func($a->argv[1].'_widget_args');
154                         $jsargs = implode("</em>,<em>", $wargs);
155                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
156
157                         $o .= "</script>
158                         <br style='clear:left'/><br/>
159                         <h4>Copy and paste this code</h4>
160                         <code>"
161
162                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
163                         .$jsargs
164                         .htmlspecialchars('"></script>')
165                         ."</code>";
166
167
168                         return $o;
169                 }
170
171         }
172
173         echo $o;
174         killme();
175 }
176
177
178
179
180 ?>