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_uninstall() {
24 Hook::unregister('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
25 Hook::unregister('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
28 function widgets_settings_post(){
31 if (isset($_POST['widgets-submit'])){
32 DI::pConfig()->delete(local_user(), 'widgets', 'key');
37 function widgets_settings(&$a,&$o) {
42 $key = DI::pConfig()->get(local_user(), 'widgets', 'key' );
43 if ($key=='') { $key = mt_rand(); DI::pConfig()->set(local_user(), 'widgets', 'key', $key); }
46 $d = dir(dirname(__file__));
47 while(false !== ($f = $d->read())) {
48 if(substr($f,0,7)=="widget_") {
49 preg_match("|widget_([^.]+).php|", $f, $m);
53 $widgets[] = [$w, call_user_func($w."_widget_name")];
61 # $t = file_get_contents( dirname(__file__). "/settings.tpl" );
62 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/widgets/");
63 $o .= Renderer::replaceMacros($t, [
64 '$submit' => DI::l10n()->t('Generate new key'),
65 '$title' => "Widgets",
66 '$label' => DI::l10n()->t('Widgets key'),
68 '$widgets_h' => DI::l10n()->t('Widgets available'),
69 '$widgets' => $widgets,
74 function widgets_module() {
78 function _abs_url($s){
79 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1" . DI::baseUrl()->get() . "/\$2", $s);
82 function _randomAlphaNum($length){
83 return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
87 function widgets_content(&$a) {
89 if (!isset($_GET['k'])) {
90 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
94 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
95 DBA::escape($_GET['k'])
98 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
102 $conf['uid'] = $r[0]['uid'];
103 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
107 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
108 if (file_exists($widgetfile)){
109 require_once($widgetfile);
111 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
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);
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="'.DI::baseUrl()->get().'/settings/addon">'. DI::l10n()->t("Addon Settings") .'</a>';
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/>";
136 header("content-type: application/x-javascript");
141 $widget_size = call_user_func($a->argv[1].'_widget_size');
143 $script = file_get_contents(dirname(__file__)."/widgets.js");
144 $o .= Renderer::replaceMacros($script, [
145 '$entrypoint' => DI::baseUrl()->get()."/widgets/".$a->argv[1]."/cb/",
146 '$key' => $conf['key'],
147 '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
148 '$loader' => DI::baseUrl()->get()."/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],
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>";
162 <br style='clear:left'/><br/>
163 <h4>Copy and paste this code</h4>
166 .htmlspecialchars('<script src="'.DI::baseUrl()->get().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
168 .htmlspecialchars('"></script>')