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