]> git.mxchange.org Git - friendica.git/blob - addon/widgets/widgets.php
add some style to the widget
[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                 </ul>
60                 ';
61         }
62
63 }
64
65 function widgets_module() {
66         return;
67 }
68
69 function _abs_url($s){
70         $a = get_app();
71         return preg_replace("|href=(['\"])([^h][^t][^t][^p])|", "href=\$1".$a->get_baseurl()."/\$2", $s);
72 }
73
74
75 function widgets_content(&$a) {
76
77         if (!isset($_GET['k'])) {
78                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
79                 return;
80         }
81
82         $r = q("SELECT * FROM pconfig WHERE uid IN (SELECT uid FROM pconfig  WHERE v='%s')AND  cat='widgets'",
83                         dbesc($_GET['k'])
84                  );
85         if (!count($r)){
86                 if($a->argv[2]=="cb"){header('HTTP/1.0 400 Bad Request'); killme();}
87                 return;
88         }    
89         $conf = array();
90         $conf['uid'] = $r[0]['uid'];
91         foreach($r as $e) { $conf[$e['k']]=$e['v']; }
92
93         $o = "";        
94
95 //      echo "<pre>"; var_dump($a->argv); die();
96         if ($a->argv[2]=="cb"){
97                 switch($a->argv[1]) {
98                         case 'friends': 
99                                 widget_friends_content($a, $o, $conf);
100                                 break;
101                 }
102                 
103         } else {
104
105                 
106                 if (isset($_GET['p'])) {
107                         $o .= "<style>.f9k_widget { float: left;border:1px solid black; }</style>";
108                         $o .= "<h1>Preview Widget</h1>";
109                         $o .= '<a href="'.$a->get_baseurl().'/settings/addon">'. t("Plugin Settings") .'</a>';
110                         $o .= "<br style='clear:left'/><br/>";
111                         $o .= "<script>";
112                 } else {
113                         header("content-type: application/x-javascript");
114                 }
115         
116         
117
118         
119                 $script = file_get_contents(dirname(__file__)."/widgets.js");
120                 $o .= replace_macros($script, array(
121                         '$entrypoint' => $a->get_baseurl()."/widgets/".$a->argv[1]."/cb/",
122                         '$key' => $conf['key'],
123                         '$widget_id' => 'f9k_'.$a->argv[1]."_".time(),
124                         '$loader' => $a->get_baseurl()."/images/rotator.gif",
125                 ));
126
127         
128                 if (isset($_GET['p'])) {
129                         $o .= "</script>
130                         <br style='clear:left'/><br/>
131                         <h4>Copy and paste this code</h4>
132                         <code>"
133                         
134                         .htmlspecialchars('<script src="'.$a->get_baseurl().'/widgets/'.$a->argv[1].'?k='.$conf['key'].'"></script>')
135                         ."</code>";
136                         return $o;
137                 }       
138                 
139         }       
140         
141         echo $o;
142         killme();
143 }
144
145
146 function widget_friends_content(&$a, &$o, $conf){
147         if (!local_user()){
148                 if (!isset($_GET['s']))
149                         header('HTTP/1.0 400 Bad Request');
150                 
151                 if (substr($_GET['s'],0,strlen($conf['site'])) !== $conf['site'])
152                         header('HTTP/1.0 400 Bad Request');
153         } 
154         $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` 
155                         LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
156                         WHERE `user`.`uid` = %s AND `profile`.`is-default` = 1 LIMIT 1",
157                         intval($conf['uid'])
158         );
159         
160         if(!count($r)) return;
161         $a->profile = $r[0];
162
163         $o .= "<style>
164                 .f9k_widget .contact-block-div { display: block !important; float: left!important; width: 50px!important; height: 50px!important; margin: 2px!important;}
165                 .f9k_widget #contact-block-end { clear: left; }
166         </style>";
167         $o .= _abs_url(contact_block());
168         $o .= "<a href='".$a->get_baseurl().'/profile/'.$a->profile['nickname']."'>". t('Connect on Friendika!') ."</a>";
169         
170
171 }
172  
173 ?>