]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
show creative commons coverage on registration page
[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
20         $direction = (-1);
21
22
23         if(strpos($dfrn_id,':') == 1) {
24                 $direction = intval(substr($dfrn_id,0,1));
25                 $dfrn_id   = substr($dfrn_id,2);
26         }
27
28         if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
29                 header("Content-type: application/atom+xml");
30                 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
31                 echo $o;
32                 killme();
33         }
34
35         if(($type === 'profile') && (! strlen($sec))) {
36
37                 $sql_extra = '';
38                 switch($direction) {
39                         case (-1):
40                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),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`.`username`, `user`.`nickname` 
57                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
58                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
59                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
60                         dbesc($a->argv[1])
61                 );
62                 
63                 if(count($r)) {
64
65                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
66
67                         logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
68
69                         if(strlen($s)) {
70
71                                 $xml = simplexml_load_string($s);
72
73                                 if((int) $xml->status == 1) {
74                                         $_SESSION['authenticated'] = 1;
75                                         $_SESSION['visitor_id'] = $r[0]['id'];
76                                         notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
77                                         // Visitors get 1 day session.
78                                         $session_id = session_id();
79                                         $expire = time() + 86400;
80                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
81                                                 dbesc($expire),
82                                                 dbesc($session_id)
83                                         ); 
84                                 }
85                         }
86                         $profile = $r[0]['nickname'];
87                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
88                 }
89                 goaway($a->get_baseurl());
90
91         }
92
93         if($type === 'profile-check') {
94
95                 if((strlen($challenge)) && (strlen($sec))) {
96
97                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
98                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
99                                 dbesc($sec)
100                         );
101                         if(! count($r)) {
102                                 xml_status(3);
103                                 // NOTREACHED
104                         }
105                         $orig_id = $r[0]['dfrn_id'];
106                         if(strpos(':',$orig_id))
107                                 $orig_id = substr($orig_id,2);
108
109                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
110                                 intval($r[0]['cid'])
111                         );
112                         if(! count($c)) {
113                                 xml_status(3);
114                         }
115                         $contact = $c[0];
116
117                         $sent_dfrn_id = hex2bin($dfrn_id);
118                         $challenge    = hex2bin($challenge);
119
120                         $final_dfrn_id = '';
121
122                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
123                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
124                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
125                         }
126                         else {
127                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
128                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
129                         }
130
131                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
132
133                         if(strpos($final_dfrn_id,':') == 1)
134                                 $final_dfrn_id = substr($final_dfrn_id,2);
135
136                         if($final_dfrn_id != $orig_id) {
137
138                                 // did not decode properly - cannot trust this site 
139                                 xml_status(3);
140                         }
141
142                         header("Content-type: text/xml");
143                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
144                         killme();
145                         // NOTREACHED
146                 }
147                 else {
148                                 // old protocol
149
150                         switch($direction) {
151                                 case 1:
152                                         $dfrn_id = '0:' . $dfrn_id;
153                                         break;
154                                 case 0:
155                                         $dfrn_id = '1:' . $dfrn_id;
156                                         break;
157                                 default:
158                                         break;
159                         }
160
161
162                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
163                         $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
164                                 dbesc($dfrn_id));
165                         if(count($r)) {
166                                 xml_status(1);
167                                 return; // NOTREACHED
168                         }
169                         xml_status(0);
170                         return; // NOTREACHED
171                 }
172         }
173
174 }
175
176
177
178 function dfrn_poll_post(&$a) {
179
180         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? $_POST['dfrn_id']              : '');
181         $challenge    = ((x($_POST,'challenge'))    ? $_POST['challenge']            : '');
182         $url          = ((x($_POST,'url'))          ? $_POST['url']                  : '');
183         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
184
185         $direction    = (-1);
186         if(strpos($dfrn_id,':') == 1) {
187                 $direction = intval(substr($dfrn_id,0,1));
188                 $dfrn_id   = substr($dfrn_id,2);
189         }
190
191
192         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
193                 dbesc($dfrn_id),
194                 dbesc($challenge)
195         );
196
197         if(! count($r))
198                 killme();
199
200         $type = $r[0]['type'];
201         $last_update = $r[0]['last_update'];
202
203         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
204                 dbesc($dfrn_id),
205                 dbesc($challenge)
206         );
207
208
209         $sql_extra = '';
210         switch($direction) {
211                 case (-1):
212                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
213                         $my_id = $dfrn_id;
214                         break;
215                 case 0:
216                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
217                         $my_id = '1:' . $dfrn_id;
218                         break;
219                 case 1:
220                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
221                         $my_id = '0:' . $dfrn_id;
222                         break;
223                 default:
224                         goaway($a->get_baseurl());
225                         break; // NOTREACHED
226         }
227
228
229         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
230
231
232         if(! count($r))
233                 killme();
234
235         $owner_uid = $r[0]['uid'];
236         $contact_id = $r[0]['id']; 
237
238
239         if($type === 'reputation' && strlen($url)) {
240                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
241                         dbesc($url),
242                         intval($owner_uid)
243                 );
244                 $reputation = 0;
245                 $text = '';
246
247                 if(count($r)) {
248                         $reputation = $r[0]['rating'];
249                         $text = $r[0]['reason'];
250
251                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
252                                 $reputation = 0;
253                                 $text = '';
254                         }
255                 }
256
257                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
258                 <reputation>
259                         <url>$url</url>
260                         <rating>$reputation</rating>
261                         <description>$text</description>
262                 </reputation>
263                 ";
264                 killme();
265                 // NOTREACHED
266         }
267         else {
268                 header("Content-type: application/atom+xml");
269                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
270                 echo $o;
271                 killme();
272
273         }
274 }
275
276 function dfrn_poll_content(&$a) {
277
278         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
279         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
280         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
281         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
282         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
283         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
284
285         $direction = (-1);
286         if(strpos($dfrn_id,':') == 1) {
287                 $direction = intval(substr($dfrn_id,0,1));
288                 $dfrn_id = substr($dfrn_id,2);
289         }
290
291
292         if($dfrn_id != '') {
293                 // initial communication from external contact
294                 $hash = random_string();
295
296                 $status = 0;
297
298                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
299
300                 if($type !== 'profile') {
301                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
302                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
303                                 dbesc($hash),
304                                 dbesc($dfrn_id),
305                                 intval(time() + 60 ),
306                                 dbesc($type),
307                                 dbesc($last_update)
308                         );
309                 }
310                 $sql_extra = '';
311                 switch($direction) {
312                         case (-1):
313                                 if($type === 'profile')
314                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
315                                 else
316                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
317                                 $my_id = $dfrn_id;
318                                 break;
319                         case 0:
320                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
321                                 $my_id = '1:' . $dfrn_id;
322                                 break;
323                         case 1:
324                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
325                                 $my_id = '0:' . $dfrn_id;
326                                 break;
327                         default:
328                                 goaway($a->get_baseurl());
329                                 break; // NOTREACHED
330                 }
331
332                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
333                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
334                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
335                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
336                         dbesc($a->argv[1])
337                 );
338
339                 if(count($r)) {
340
341                         $challenge = '';
342                         $encrypted_id = '';
343                         $id_str = $my_id . '.' . mt_rand(1000,9999);
344
345                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
346                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
347                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
348                         }
349                         else {
350                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
351                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
352                         }
353
354                         $challenge = bin2hex($challenge);
355                         $encrypted_id = bin2hex($encrypted_id);
356                 }
357                 else {
358                         $status = 1;
359                         $challenge = '';
360                         $encrypted_id = '';
361                 }
362
363                 if(($type === 'profile') && (strlen($sec))) {
364                         // URL reply
365
366                         $s = fetch_url($r[0]['poll'] 
367                                 . '?dfrn_id=' . $encrypted_id 
368                                 . '&type=profile-check'
369                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
370                                 . '&challenge=' . $challenge
371                                 . '&sec=' . $sec
372                         );
373
374                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
375
376                         if(strlen($s) && strstr($s,'<?xml')) {
377
378                                 $xml = simplexml_load_string($s);
379
380                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
381
382                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
383                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
384  
385                                 
386                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
387                                         $_SESSION['authenticated'] = 1;
388                                         $_SESSION['visitor_id'] = $r[0]['id'];
389                                         notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
390                                         // Visitors get 1 day session.
391                                         $session_id = session_id();
392                                         $expire = time() + 86400;
393                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
394                                                 dbesc($expire),
395                                                 dbesc($session_id)
396                                         ); 
397                                 }
398                                 $profile = $r[0]['nickname'];
399                                 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
400                         }
401                         goaway($a->get_baseurl());
402                         // NOTREACHED
403
404                 }
405                 else {
406                         // XML reply
407                         header("Content-type: text/xml");
408                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
409                                 . '<dfrn_poll>' . "\r\n"
410                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
411                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
412                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
413                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
414                                 . '</dfrn_poll>' . "\r\n" ;
415                         killme();
416                         // NOTREACHED
417                 }
418         }
419 }
420
421