]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
preliminary network abstraction, configurable debugging.
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 require_once('include/items.php');
4 require_once('include/auth.php');
5
6
7 function dfrn_poll_init(&$a) {
8
9         $dfrn_id = '';
10
11         if(x($_GET,'dfrn_id'))
12                 $dfrn_id = $_GET['dfrn_id'];
13         if(x($_GET,'type'))
14                 $type = $_GET['type'];
15         if(x($_GET,'last_update'))
16                 $last_update = $_GET['last_update'];
17         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? $_GET['dfrn_version']    : '1.0');
18         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
19
20
21         $direction = (-1);
22
23
24         if(strpos($dfrn_id,':') == 1) {
25                 $direction = intval(substr($dfrn_id,0,1));
26                 $dfrn_id = substr($dfrn_id,2);
27         }
28
29         if(($dfrn_id == '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
30                 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
31                 echo $o;
32                 killme();
33         }
34
35         if((x($type)) && ($type == 'profile')) {
36
37                 $sql_extra = '';
38                 switch($direction) {
39                         case (-1):
40                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($dfrn_id));
41                                 $my_id = $dfrn_id;
42                                 break;
43                         case 0:
44                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45                                 $my_id = '1:' . $dfrn_id;
46                                 break;
47                         case 1:
48                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
49                                 $my_id = '0:' . $dfrn_id;
50                                 break;
51                         default:
52                                 goaway($a->get_baseurl());
53                                 break; // NOTREACHED
54                 }
55
56                 $r = q("SELECT `contact`.*, `user`.`nickname` 
57                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
58                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 $sql_extra LIMIT 1");
59                 
60                 if(count($r)) {
61
62                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
63
64                         if(strlen($s)) {
65
66                                 $xml = simplexml_load_string($s);
67
68                                 if((int) $xml->status == 1) {
69                                         $_SESSION['authenticated'] = 1;
70                                         $_SESSION['visitor_id'] = $r[0]['id'];
71                                         notice( t('Hi ') . $r[0]['name'] . EOL);
72                                         // Visitors get 1 day session.
73                                         $session_id = session_id();
74                                         $expire = time() + 86400;
75                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
76                                                 dbesc($expire),
77                                                 dbesc($session_id)
78                                         ); 
79                                 }
80                         }
81                         $profile = $r[0]['nickname'];
82                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
83                 }
84                 goaway($a->get_baseurl());
85
86         }
87
88         if((x($type)) && ($type == 'profile-check')) {
89
90                 switch($direction) {
91                         case 1:
92                                 $dfrn_id = '0:' . $dfrn_id;
93                                 break;
94                         case 0:
95                                 $dfrn_id = '1:' . $dfrn_id;
96                                 break;
97                         default:
98                                 break;
99                 }
100                 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
101                 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
102                         dbesc($dfrn_id));
103                 if(count($r)) {
104                         xml_status(1);
105                         return; // NOTREACHED
106                 }
107                 xml_status(0);
108                 return; // NOTREACHED
109         }
110
111
112 }
113
114
115
116 function dfrn_poll_post(&$a) {
117
118         $dfrn_id = $_POST['dfrn_id'];
119         $challenge = $_POST['challenge'];
120         $url = $_POST['url'];
121
122         $direction = (-1);
123         if(strpos($dfrn_id,':') == 1) {
124                 $direction = intval(substr($dfrn_id,0,1));
125                 $dfrn_id = substr($dfrn_id,2);
126         }
127
128
129         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
130                 dbesc($dfrn_id),
131                 dbesc($challenge)
132         );
133
134         if(! count($r))
135                 killme();
136
137         $type = $r[0]['type'];
138         $last_update = $r[0]['last_update'];
139
140         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
141                 dbesc($dfrn_id),
142                 dbesc($challenge)
143         );
144
145
146         $sql_extra = '';
147         switch($direction) {
148                 case (-1):
149                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
150                         $my_id = $dfrn_id;
151                         break;
152                 case 0:
153                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
154                         $my_id = '1:' . $dfrn_id;
155                         break;
156                 case 1:
157                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
158                         $my_id = '0:' . $dfrn_id;
159                         break;
160                 default:
161                         goaway($a->get_baseurl());
162                         break; // NOTREACHED
163         }
164
165
166         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
167
168
169         if(! count($r))
170                 killme();
171
172         $owner_uid = $r[0]['uid'];
173         $contact_id = $r[0]['id']; 
174
175
176         if($type == 'reputation' && strlen($url)) {
177                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
178                         dbesc($url),
179                         intval($owner_uid)
180                 );
181                 $reputation = 0;
182                 $text = '';
183
184                 if(count($r)) {
185                         $reputation = $r[0]['rating'];
186                         $text = $r[0]['reason'];
187
188                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
189                                 $reputation = 0;
190                                 $text = '';
191                         }
192                 }
193
194                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
195                 <reputation>
196                         <url>$url</url>
197                         <rating>$reputation</rating>
198                         <description>$text</description>
199                 </reputation>
200                 ";
201                 killme();
202                 return; // NOTREACHED
203         }
204         else {
205                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
206                 echo $o;
207                 killme();
208
209         }
210 }
211
212 function dfrn_poll_content(&$a) {
213
214
215         $dfrn_id = '';
216         $type = 'data';
217
218         if(x($_GET,'dfrn_id'))
219                 $dfrn_id = $_GET['dfrn_id'];
220         if(x($_GET,'type'))
221                 $type = $_GET['type'];
222         if(x($_GET,'last_update'))
223                 $last_update = $_GET['last_update'];
224
225         $direction = (-1);
226         if(strpos($dfrn_id,':') == 1) {
227                 $direction = intval(substr($dfrn_id,0,1));
228                 $dfrn_id = substr($dfrn_id,2);
229         }
230
231
232         if($dfrn_id != '') {
233                 // initial communication from external contact
234                 $hash = random_string();
235
236                 $status = 0;
237
238                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
239
240                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
241                         VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
242                         dbesc($hash),
243                         dbesc($dfrn_id),
244                         intval(time() + 60 ),
245                         dbesc($type),
246                         dbesc($last_update)
247                 );
248
249
250                 $sql_extra = '';
251                 switch($direction) {
252                         case (-1):
253                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
254                                 $my_id = $dfrn_id;
255                                 break;
256                         case 0:
257                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
258                                 $my_id = '1:' . $dfrn_id;
259                                 break;
260                         case 1:
261                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
262                                 $my_id = '0:' . $dfrn_id;
263                                 break;
264                         default:
265                                 goaway($a->get_baseurl());
266                                 break; // NOTREACHED
267                 }
268
269
270
271
272                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
273
274                 if(count($r)) {
275
276                         $challenge = '';
277                         $encrypted_id = '';
278                         $id_str = $my_id . '.' . mt_rand(1000,9999);
279
280
281                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
282                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
283                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
284                         }
285                         else {
286                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
287                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
288                         }
289
290                         $challenge = bin2hex($challenge);
291                         $encrypted_id = bin2hex($encrypted_id);
292                 }
293                 else {
294                         $status = 1;
295                 }
296
297                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_poll><status>' .$status . '</status><dfrn_version>2.0</dfrn_version><dfrn_id>' . $encrypted_id . '</dfrn_id>'
298                         . '<challenge>' . $challenge . '</challenge></dfrn_poll>' . "\r\n" ;
299                 session_write_close();
300                 exit;           
301         }
302 }
303
304