]> git.mxchange.org Git - friendica.git/blob - addon/widgets/widgets.php
305423cb954d76be96f027750621e270599a3244
[friendica.git] / addon / widgets / widgets.php
1 <?php
2         /**
3          * widgets from friendika
4          * 
5          * allow to embed info from friendika into another site
6          */
7          
8          
9 function widgets_install() {
10         //  we need some hooks, for the configuration and for sending tweets
11         register_hook('plugin_settings', 'addon/widgets/widgets.php', 'widgets_settings'); 
12         register_hook('plugin_settings_post', 'addon/widgets/widgets.php', 'widgets_settings_post');
13
14         logger("installed widgets");
15 }
16
17 function widgets_settings_post(){
18         
19         if (isset($_POST['widgets-submit'])){
20                 set_pconfig(local_user(), 'widgets', 'site', $_POST['widgets-site']);
21                 set_pconfig(local_user(), 'widgets', 'key', $_POST['widgets-key']);
22         }
23 }
24
25 function widgets_settings(&$a,&$o) {
26     if(! local_user())
27                 return;         
28         
29         $key    = get_pconfig(local_user(), 'widgets', 'key' );
30         $site    = get_pconfig(local_user(), 'widgets', 'site' );
31
32         if ($key=='') $key = mt_rand(); 
33
34         $o .='
35         <h3 class="settings-heading">Widgets</h3>
36         <div id="settings-username-wrapper">
37                 <label for="widgets-site" id="settings-username-label">'.t('Remote site: ').'</label>
38                 <input type="text" value="'.$site.'" id="settings-username" name="widgets-site">
39         </div>
40         <div id="settings-username-end"></div>
41         <div id="settings-username-wrapper">
42                 <label for="widgets-key" id="settings-username-label">'.t('Widgets key: ').'</label>
43                 <input type="hidden" value="'.$key.'" id="settings-username" name="widgets-key">
44                 <strong>'.$key.'</strong>
45         </div>
46         <div id="settings-username-end"></div>
47         
48         
49                 
50         <div class="settings-submit-wrapper">
51                 <input type="submit" value="'.t('Submit').'" class="settings-submit" name="widgets-submit">
52         </div>  
53         ';
54         
55         if ($key!='' and $site!='') {
56                 $o.='<h4>Widgets:</h4>
57                 <ul>
58                         <li><a href="'.$a->get_baseurl().'/widgets/friends/?p=1&k='.$key.'">Friend list</a></li>
59                         <li><a href="'.$a->get_baseurl().'/widgets/like/?p=1&k='.$key.'">Likes count</a></li>
60                 </ul>
61                 ';
62         }
63
64 }
65
66 function widgets_module() {
67         return;
68 }
69
70 function _abs_url($s){
71         $a = get_app();
72         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
73 }
74
75
76 function widgets_content(&$a) {
77
78         if (!isset($_GET['k'])) {
79                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
80                 return;
81         }
82
83         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
84                         dbesc($_GET['k'])
85                  );
86         if (!count($r)){
87                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
88                 return;
89         }    
90         $conf = array();
91         $conf['uid'] = $r[0]['uid'];
92         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
93
94         $o = "";        
95
96         $widgetfile =dirname(__file__)."/widget_".$a->argv[1].".php";
97         if (file_exists($widgetfile)){
98                 require_once($widgetfile);
99         } else {
100                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
101                 return;
102         }               
103         
104
105
106
107         //echo "<pre>"; var_dump($a->argv); die();
108         if ($a->argv[2]=="cb"){
109                 if (!local_user()){
110                         if (!isset($_GET['s']))
111                                 {header('HTTP/1.0 400 Bad Request'); killme();}
112                         
113                         if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
114                                 {header('HTTP/1.0 400 Bad Request'); killme();}
115                 } 
116                 widget_content($a, $o, $conf);
117                 
118         } else {
119
120                 
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="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
125                         $o .= "<br style='clear:left'/><br/>";                  
126                         widget_help($a, $o, $conf);
127                         $o .= "<br style='clear:left'/><br/>";
128                         $o .= "<script>";
129                 } else {
130                         header("content-type: application/x-javascript");
131                 }
132         
133         
134
135         
136                 $script = file_get_contents(dirname(__file__)."/widgets.js");
137                 $o .= replace_macros($script, array(
138                         '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
139                         '$key' => $conf['key'],
140                         '$widget_id' => 'f9k_'.$a->argv[1]."_".time(),
141                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
142                 ));
143
144         
145                 if (isset($_GET['p'])) {
146                         $jsargs = implode("</em>,<em>", widget_args());
147                         if ($jsargs!='') $jsargs = "&a=<em>".$jsargs."</em>";
148                                 
149                         $o .= "</script>
150                         <br style='clear:left'/><br/>
151                         <h4>Copy and paste this code</h4>
152                         <code>"
153                         
154                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'])
155                         .$jsargs
156                         .htmlspecialchars('"></script>')
157                         ."</code>";
158                         return $o;
159                 }       
160                 
161         }       
162         
163         echo $o;
164         killme();
165 }
166
167
168
169  
170 ?>