4 * Description: Allow to embed info from friendica into another site
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix/>
10 use Friendica\Core\Addon;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\PConfig;
14 use Friendica\Database\DBA;
16 function widgets_install() {
17 Addon::registerHook('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
18 Addon::registerHook('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
19 Logger::log("installed widgets");
22 function widgets_uninstall() {
23 Addon::unregisterHook('addon_settings', 'addon/widgets/widgets.php', 'widgets_settings');
24 Addon::unregisterHook('addon_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
27 function widgets_settings_post(){
30 if (isset($_POST['widgets-submit'])){
31 PConfig::delete(local_user(), 'widgets', 'key');
36 function widgets_settings(&$a,&$o) {
41 $key = PConfig::get(local_user(), 'widgets', 'key' );
42 if ($key=='') { $key = mt_rand(); PConfig::set(local_user(), 'widgets', 'key', $key); }
45 $d = dir(dirname(__file__));
46 while(false !== ($f = $d->read())) {
47 if(substr($f,0,7)=="widget_") {
48 preg_match("|widget_([^.]+).php|", $f, $m);
52 $widgets[] = [$w, call_user_func($w."_widget_name")];
60 # $t = file_get_contents( dirname(__file__). "/settings.tpl" );
61 $t = get_markup_template("settings.tpl", "addon/widgets/");
62 $o .= replace_macros($t, [
63 '$submit' => L10n::t('Generate new key'),
64 '$baseurl' => $a->getBaseURL(),
65 '$title' => "Widgets",
66 '$label' => L10n::t('Widgets key'),
68 '$widgets_h' => L10n::t('Widgets available'),
69 '$widgets' => $widgets,
74 function widgets_module() {
78 function _abs_url($s){
80 return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->getBaseURL()."/\$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'); killme();}
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'); killme();}
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'); killme();}
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="'.$a->getBaseURL().'/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 .= replace_macros($script, [
146 '$entrypoint' => $a->getBaseURL()."/widgets/".$a->argv[1]."/cb/",
147 '$key' => $conf['key'],
148 '$widget_id' => 'f9a_'.$a->argv[1]."_"._randomAlphaNum(6),
149 '$loader' => $a->getBaseURL()."/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="'.$a->getBaseURL().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
169 .htmlspecialchars('"></script>')