]> git.mxchange.org Git - friendica-addons.git/blob - widgets/widgets.php
[mahjongg] Remove mahjongg addon because Flash is no longer supported by the browsers
[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  * Status: Unsupported
8  */
9
10 use Friendica\Core\Hook;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
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_settings_post(){
24         if(! local_user())
25                 return;
26         if (isset($_POST['widgets-submit'])){
27                 DI::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 = DI::pConfig()->get(local_user(), 'widgets', 'key' );
38         if ($key=='') { $key = mt_rand(); DI::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 = Renderer::getMarkupTemplate("settings.tpl", "addon/widgets/");
58         $o .= Renderer::replaceMacros($t, [
59                 '$submit' => DI::l10n()->t('Generate new key'),
60                 '$title' => "Widgets",
61                 '$label' => DI::l10n()->t('Widgets key'),
62                 '$key' => $key,
63                 '$widgets_h' => DI::l10n()->t('Widgets available'),
64                 '$widgets' => $widgets,
65         ]);
66
67 }
68
69 function widgets_module() {
70         return;
71 }
72
73 function _abs_url($s){
74         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1" . DI::baseUrl()->get() . "/\$2", $s);
75 }
76
77 function _randomAlphaNum($length){
78         return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
79 }
80
81
82 function widgets_content(&$a) {
83
84         if (!isset($_GET['k'])) {
85                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
86                 return;
87         }
88
89         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
90                         DBA::escape($_GET['k'])
91                  );
92         if (!count($r)){
93                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
94                 return;
95         }
96         $conf = [];
97         $conf['uid'] = $r[0]['uid'];
98         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
99
100         $o = "";
101
102         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
103         if (file_exists($widgetfile)){
104                 require_once($widgetfile);
105         } else {
106                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
107                 return;
108         }
109
110
111
112
113         //echo "<pre>"; var_dump($a->argv); die();
114         if ($a->argv[2]=="cb"){
115                 /*header('Access-Control-Allow-Origin: *');*/
116                 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
117
118         } else {
119
120
121                 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
122                         $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
123                         $o .= "<h1>Preview Widget</h1>";
124                         $o .= '<a href="'.DI::baseUrl()->get().'/settings/addon">'. DI::l10n()->t("Addon Settings") .'</a>';
125
126                         $o .=  "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
127                         $o .=  call_user_func($a->argv[1].'_widget_help');
128                         $o .= "<br style='clear:left'/><br/>";
129                         $o .= "<script>";
130                 } else {
131                         header("content-type: application/x-javascript");
132                 }
133
134
135
136                 $widget_size = call_user_func($a->argv[1].'_widget_size');
137
138                 $script = file_get_contents(dirname(__file__)."/widgets.js");
139                 $o .= Renderer::replaceMacros($script, [
140                         '$entrypoint' => DI::baseUrl()->get()."/widgets/".$a->argv[1]."/cb/",
141                         '$key' => $conf['key'],
142                         '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
143                         '$loader' => DI::baseUrl()->get()."/images/rotator.gif",
144                         '$args' => (isset($_GET['a'])?$_GET['a']:''),
145                         '$width' => $widget_size[0],
146                         '$height' => $widget_size[1],
147                         '$type' => $a->argv[1],
148                 ]);
149
150
151                 if (isset($_GET['p'])) {
152                         $wargs = call_user_func($a->argv[1].'_widget_args');
153                         $jsargs = implode("</em>,<em>", $wargs);
154                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
155
156                         $o .= "</script>
157                         <br style='clear:left'/><br/>
158                         <h4>Copy and paste this code</h4>
159                         <code>"
160
161                         .htmlspecialchars('<script src="'.DI::baseUrl()->get().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
162                         .$jsargs
163                         .htmlspecialchars('"></script>')
164                         ."</code>";
165
166
167                         return $o;
168                 }
169         }
170
171         echo $o;
172         exit();
173 }