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