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