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