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\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
18 function widgets_install() {
19 Hook::register('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
20 Hook::register('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
21 Logger::log("installed widgets");
24 function widgets_uninstall() {
25 Hook::unregister('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
26 Hook::unregister('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
29 function widgets_settings_post(){
32 if (isset($_POST['widgets-submit'])){
33 DI::pConfig()->delete(local_user(), 'widgets', 'key');
38 function widgets_settings(&$a,&$o) {
43 $key = DI::pConfig()->get(local_user(), 'widgets', 'key' );
44 if ($key=='') { $key = mt_rand(); DI::pConfig()->set(local_user(), 'widgets', 'key', $key); }
47 $d = dir(dirname(__file__));
48 while(false !== ($f = $d->read())) {
49 if(substr($f,0,7)=="widget_") {
50 preg_match("|widget_([^.]+).php|", $f, $m);
54 $widgets[] = [$w, call_user_func($w."_widget_name")];
62 # $t = file_get_contents( dirname(__file__). "/settings.tpl" );
63 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/widgets/");
64 $o .= Renderer::replaceMacros($t, [
65 '$submit' => L10n::t('Generate new key'),
66 '$title' => "Widgets",
67 '$label' => L10n::t('Widgets key'),
69 '$widgets_h' => L10n::t('Widgets available'),
70 '$widgets' => $widgets,
75 function widgets_module() {
79 function _abs_url($s){
80 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1" . DI::baseUrl()->get() . "/\$2", $s);
83 function _randomAlphaNum($length){
84 return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$length)),0,$length);
88 function widgets_content(&$a) {
90 if (!isset($_GET['k'])) {
91 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
95 $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig WHERE v='%s')AND cat='widgets'",
96 DBA::escape($_GET['k'])
99 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
103 $conf['uid'] = $r[0]['uid'];
104 foreach($r as $e) { $conf[$e['k']]=$e['v']; }
108 $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
109 if (file_exists($widgetfile)){
110 require_once($widgetfile);
112 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); exit();}
119 //echo "<pre>"; var_dump($a->argv); die();
120 if ($a->argv[2]=="cb"){
121 /*header('Access-Control-Allow-Origin: *');*/
122 $o .= call_user_func($a->argv[1].'_widget_content',$a, $conf);
127 if (isset($_GET['p']) && local_user()==$conf['uid'] ) {
128 $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
129 $o .= "<h1>Preview Widget</h1>";
130 $o .= '<a href="'.DI::baseUrl()->get().'/settings/addon">'. L10n::t("Addon Settings") .'</a>';
132 $o .= "<h4>".call_user_func($a->argv[1].'_widget_name')."</h4>";
133 $o .= call_user_func($a->argv[1].'_widget_help');
134 $o .= "<br style='clear:left'/><br/>";
137 header("content-type: application/x-javascript");
142 $widget_size = call_user_func($a->argv[1].'_widget_size');
144 $script = file_get_contents(dirname(__file__)."/widgets.js");
145 $o .= Renderer::replaceMacros($script, [
146 '$entrypoint' => DI::baseUrl()->get()."/widgets/".$a->argv[1]."/cb/",
147 '$key' => $conf['key'],
148 '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
149 '$loader' => DI::baseUrl()->get()."/images/rotator.gif",
150 '$args' => (isset($_GET['a'])?$_GET['a']:''),
151 '$width' => $widget_size[0],
152 '$height' => $widget_size[1],
153 '$type' => $a->argv[1],
157 if (isset($_GET['p'])) {
158 $wargs = call_user_func($a->argv[1].'_widget_args');
159 $jsargs = implode("</em>,<em>", $wargs);
160 if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
163 <br style='clear:left'/><br/>
164 <h4>Copy and paste this code</h4>
167 .htmlspecialchars('<script src="'.DI::baseUrl()->get().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
169 .htmlspecialchars('"></script>')