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