4 * Description: Allow to embed info from friendica into another site
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix/>
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;
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");
23 function widgets_settings_post(){
26 if (isset($_POST['widgets-submit'])){
27 DI::pConfig()->delete(local_user(), 'widgets', 'key');
32 function widgets_settings(&$a,&$o) {
37 $key = DI::pConfig()->get(local_user(), 'widgets', 'key' );
38 if ($key=='') { $key = mt_rand(); DI::pConfig()->set(local_user(), 'widgets', 'key', $key); }
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);
48 $widgets[] = [$w, call_user_func($w."_widget_name")];
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'),
63 '$widgets_h' => DI::l10n()->t('Widgets available'),
64 '$widgets' => $widgets,
69 function widgets_module() {
73 function _abs_url($s){
74 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1" . DI::baseUrl()->get() . "/\$2", $s);
77 function _randomAlphaNum($length){
78 return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
82 function widgets_content(&$a) {
84 if (!isset($_GET['k'])) {
85 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
89 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
90 DBA::escape($_GET['k'])
93 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
97 $conf['uid'] = $r[0]['uid'];
98 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
102 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
103 if (file_exists($widgetfile)){
104 require_once($widgetfile);
106 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
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);
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>';
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/>";
131 header("content-type: application/x-javascript");
136 $widget_size = call_user_func($a->argv[1].'_widget_size');
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],
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>";
157 <br style='clear:left'/><br/>
158 <h4>Copy and paste this code</h4>
161 .htmlspecialchars('<script src="'.DI::baseUrl()->get().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
163 .htmlspecialchars('"></script>')