]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
"notes" app
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3
4
5 require_once('include/items.php');
6 require_once('include/auth.php');
7
8
9 function dfrn_poll_init(&$a) {
10
11
12         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
13         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
14         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
15         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
16         $challenge       = ((x($_GET,'challenge'))       ? $_GET['challenge']            : '');
17         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
18         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
19         $perm            = ((x($_GET,'perm'))            ? $_GET['perm']                 : 'r');
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                 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
31                         killme();
32                 }
33
34                 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] );
35                 header("Content-type: application/atom+xml");
36                 $o = get_feed_for($a, '', $a->argv[1],$last_update);
37                 echo $o;
38                 killme();
39         }
40
41         if(($type === 'profile') && (! strlen($sec))) {
42
43                 $sql_extra = '';
44                 switch($direction) {
45                         case (-1):
46                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
47                                 $my_id = $dfrn_id;
48                                 break;
49                         case 0:
50                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
51                                 $my_id = '1:' . $dfrn_id;
52                                 break;
53                         case 1:
54                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
55                                 $my_id = '0:' . $dfrn_id;
56                                 break;
57                         default:
58                                 goaway($a->get_baseurl());
59                                 break; // NOTREACHED
60                 }
61
62                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
63                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
64                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
65                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
66                         dbesc($a->argv[1])
67                 );
68                 
69                 if(count($r)) {
70
71                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
72
73                         logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
74
75                         if(strlen($s)) {
76
77                                 $xml = parse_xml_string($s);
78
79                                 if((int) $xml->status == 1) {
80                                         $_SESSION['authenticated'] = 1;
81                                         $_SESSION['visitor_id'] = $r[0]['id'];
82                                         $_SESSION['visitor_home'] = $r[0]['url'];
83                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
84                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
85                                         // Visitors get 1 day session.
86                                         $session_id = session_id();
87                                         $expire = time() + 86400;
88                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
89                                                 dbesc($expire),
90                                                 dbesc($session_id)
91                                         ); 
92                                 }
93                         }
94                         $profile = $r[0]['nickname'];
95                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
96                 }
97                 goaway($a->get_baseurl());
98
99         }
100
101         if($type === 'profile-check' && $dfrn_version < 2.2 ) {
102
103                 if((strlen($challenge)) && (strlen($sec))) {
104
105                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
106                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
107                                 dbesc($sec)
108                         );
109                         if(! count($r)) {
110                                 xml_status(3, 'No ticket');
111                                 // NOTREACHED
112                         }
113                         $orig_id = $r[0]['dfrn_id'];
114                         if(strpos($orig_id, ':'))
115                                 $orig_id = substr($orig_id,2);
116
117                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
118                                 intval($r[0]['cid'])
119                         );
120                         if(! count($c)) {
121                                 xml_status(3, 'No profile');
122                         }
123                         $contact = $c[0];
124
125                         $sent_dfrn_id = hex2bin($dfrn_id);
126                         $challenge    = hex2bin($challenge);
127
128                         $final_dfrn_id = '';
129
130                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
131                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
132                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
133                         }
134                         else {
135                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
136                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
137                         }
138
139                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
140
141                         if(strpos($final_dfrn_id,':') == 1)
142                                 $final_dfrn_id = substr($final_dfrn_id,2);
143
144                         if($final_dfrn_id != $orig_id) {
145                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
146                                 // did not decode properly - cannot trust this site 
147                                 xml_status(3, 'Bad decryption');
148                         }
149
150                         header("Content-type: text/xml");
151                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
152                         killme();
153                         // NOTREACHED
154                 }
155                 else {
156                                 // old protocol
157
158                         switch($direction) {
159                                 case 1:
160                                         $dfrn_id = '0:' . $dfrn_id;
161                                         break;
162                                 case 0:
163                                         $dfrn_id = '1:' . $dfrn_id;
164                                         break;
165                                 default:
166                                         break;
167                         }
168
169
170                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
171                         $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
172                                 dbesc($dfrn_id));
173                         if(count($r)) {
174                                 xml_status(1);
175                                 return; // NOTREACHED
176                         }
177                         xml_status(0);
178                         return; // NOTREACHED
179                 }
180         }
181
182 }
183
184
185
186 function dfrn_poll_post(&$a) {
187
188         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? $_POST['dfrn_id']              : '');
189         $challenge    = ((x($_POST,'challenge'))    ? $_POST['challenge']            : '');
190         $url          = ((x($_POST,'url'))          ? $_POST['url']                  : '');
191         $sec          = ((x($_POST,'sec'))          ? $_POST['sec']                  : '');
192         $ptype        = ((x($_POST,'type'))         ? $_POST['type']                 : '');
193         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
194         $perm         = ((x($_POST,'perm'))         ? $_POST['perm']                 : 'r');
195
196         if($ptype === 'profile-check') {
197
198                 if((strlen($challenge)) && (strlen($sec))) {
199
200                         logger('dfrn_poll: POST: profile-check');
201  
202                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
203                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
204                                 dbesc($sec)
205                         );
206                         if(! count($r)) {
207                                 xml_status(3, 'No ticket');
208                                 // NOTREACHED
209                         }
210                         $orig_id = $r[0]['dfrn_id'];
211                         if(strpos($orig_id, ':'))
212                                 $orig_id = substr($orig_id,2);
213
214                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
215                                 intval($r[0]['cid'])
216                         );
217                         if(! count($c)) {
218                                 xml_status(3, 'No profile');
219                         }
220                         $contact = $c[0];
221
222                         $sent_dfrn_id = hex2bin($dfrn_id);
223                         $challenge    = hex2bin($challenge);
224
225                         $final_dfrn_id = '';
226
227                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
228                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
229                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
230                         }
231                         else {
232                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
233                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
234                         }
235
236                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
237
238                         if(strpos($final_dfrn_id,':') == 1)
239                                 $final_dfrn_id = substr($final_dfrn_id,2);
240
241                         if($final_dfrn_id != $orig_id) {
242                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
243                                 // did not decode properly - cannot trust this site 
244                                 xml_status(3, 'Bad decryption');
245                         }
246
247                         header("Content-type: text/xml");
248                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
249                         killme();
250                         // NOTREACHED
251                 }
252
253         }
254
255         $direction    = (-1);
256         if(strpos($dfrn_id,':') == 1) {
257                 $direction = intval(substr($dfrn_id,0,1));
258                 $dfrn_id   = substr($dfrn_id,2);
259         }
260
261
262         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
263                 dbesc($dfrn_id),
264                 dbesc($challenge)
265         );
266
267         if(! count($r))
268                 killme();
269
270         $type = $r[0]['type'];
271         $last_update = $r[0]['last_update'];
272
273         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
274                 dbesc($dfrn_id),
275                 dbesc($challenge)
276         );
277
278
279         $sql_extra = '';
280         switch($direction) {
281                 case (-1):
282                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
283                         $my_id = $dfrn_id;
284                         break;
285                 case 0:
286                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
287                         $my_id = '1:' . $dfrn_id;
288                         break;
289                 case 1:
290                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
291                         $my_id = '0:' . $dfrn_id;
292                         break;
293                 default:
294                         goaway($a->get_baseurl());
295                         break; // NOTREACHED
296         }
297
298
299         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
300
301
302         if(! count($r))
303                 killme();
304
305         $contact = $r[0];
306         $owner_uid = $r[0]['uid'];
307         $contact_id = $r[0]['id']; 
308
309
310         if($type === 'reputation' && strlen($url)) {
311                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
312                         dbesc($url),
313                         intval($owner_uid)
314                 );
315                 $reputation = 0;
316                 $text = '';
317
318                 if(count($r)) {
319                         $reputation = $r[0]['rating'];
320                         $text = $r[0]['reason'];
321
322                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
323                                 $reputation = 0;
324                                 $text = '';
325                         }
326                 }
327
328                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
329                 <reputation>
330                         <url>$url</url>
331                         <rating>$reputation</rating>
332                         <description>$text</description>
333                 </reputation>
334                 ";
335                 killme();
336                 // NOTREACHED
337         }
338         else {
339
340                 // Update the writable flag if it changed               
341                 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
342                 if($dfrn_version >= 2.21) {
343                         if($perm === 'rw')
344                                 $writable = 1;
345                         else
346                                 $writable = 0;
347
348                         if($writable !=  $contact['writable']) {
349                                 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
350                                         intval($writable),
351                                         intval($contact_id)
352                                 );
353                         }
354                 }                               
355
356                 header("Content-type: application/atom+xml");
357                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
358                 echo $o;
359                 killme();
360
361         }
362 }
363
364 function dfrn_poll_content(&$a) {
365
366         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
367         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
368         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
369         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
370         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
371         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
372         $perm            = ((x($_GET,'perm'))            ? $_GET['perm']                 : 'r');
373
374         $direction = (-1);
375         if(strpos($dfrn_id,':') == 1) {
376                 $direction = intval(substr($dfrn_id,0,1));
377                 $dfrn_id = substr($dfrn_id,2);
378         }
379
380
381         if($dfrn_id != '') {
382                 // initial communication from external contact
383                 $hash = random_string();
384
385                 $status = 0;
386
387                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
388
389                 if($type !== 'profile') {
390                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
391                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
392                                 dbesc($hash),
393                                 dbesc($dfrn_id),
394                                 intval(time() + 60 ),
395                                 dbesc($type),
396                                 dbesc($last_update)
397                         );
398                 }
399                 $sql_extra = '';
400                 switch($direction) {
401                         case (-1):
402                                 if($type === 'profile')
403                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
404                                 else
405                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
406                                 $my_id = $dfrn_id;
407                                 break;
408                         case 0:
409                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
410                                 $my_id = '1:' . $dfrn_id;
411                                 break;
412                         case 1:
413                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
414                                 $my_id = '0:' . $dfrn_id;
415                                 break;
416                         default:
417                                 goaway($a->get_baseurl());
418                                 break; // NOTREACHED
419                 }
420
421                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
422                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
423                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
424                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
425                         dbesc($a->argv[1])
426                 );
427
428                 if(count($r)) {
429
430                         $challenge = '';
431                         $encrypted_id = '';
432                         $id_str = $my_id . '.' . mt_rand(1000,9999);
433
434                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
435                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
436                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
437                         }
438                         else {
439                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
440                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
441                         }
442
443                         $challenge = bin2hex($challenge);
444                         $encrypted_id = bin2hex($encrypted_id);
445                 }
446                 else {
447                         $status = 1;
448                         $challenge = '';
449                         $encrypted_id = '';
450                 }
451
452                 if(($type === 'profile') && (strlen($sec))) {
453
454                         // URL reply
455
456                         if($dfrn_version < 2.2) {
457                                 $s = fetch_url($r[0]['poll'] 
458                                         . '?dfrn_id=' . $encrypted_id 
459                                         . '&type=profile-check'
460                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
461                                         . '&challenge=' . $challenge
462                                         . '&sec=' . $sec
463                                 );
464                         }
465                         else {
466                                 $s = post_url($r[0]['poll'], array(
467                                         'dfrn_id' => $encrypted_id,
468                                         'type' => 'profile-check',
469                                         'dfrn_version' => DFRN_PROTOCOL_VERSION,
470                                         'challenge' => $challenge,
471                                         'sec' => $sec
472                                 ));
473                         }
474                         
475                         $profile = $r[0]['nickname'];
476
477                         switch($destination_url) {
478                                 case 'profile':
479                                         $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
480                                         break;
481                                 case 'photos':
482                                         $dest = $a->get_baseurl() . '/photos/' . $profile;
483                                         break;
484                                 case 'status':
485                                 case '':
486                                         $dest = $a->get_baseurl() . '/profile/' . $profile;
487                                         break;          
488                                 default:
489                                         $dest = $destination_url;
490                                         break;
491                         }
492
493                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
494
495                         if(strlen($s) && strstr($s,'<?xml')) {
496
497                                 $xml = parse_xml_string($s);
498
499                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
500
501                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
502                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
503  
504                                 
505                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
506                                         $_SESSION['authenticated'] = 1;
507                                         $_SESSION['visitor_id'] = $r[0]['id'];
508                                         $_SESSION['visitor_home'] = $r[0]['url'];
509                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
510                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
511                                         // Visitors get 1 day session.
512                                         $session_id = session_id();
513                                         $expire = time() + 86400;
514                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
515                                                 dbesc($expire),
516                                                 dbesc($session_id)
517                                         ); 
518                                 }
519                         
520                                 goaway($dest);
521                         }
522                         goaway($dest);
523                         // NOTREACHED
524
525                 }
526                 else {
527                         // XML reply
528                         header("Content-type: text/xml");
529                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
530                                 . '<dfrn_poll>' . "\r\n"
531                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
532                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
533                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
534                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
535                                 . '</dfrn_poll>' . "\r\n" ;
536                         killme();
537                         // NOTREACHED
538                 }
539         }
540 }
541
542