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