]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
Merge branch 'master' of git://github.com/friendika/friendika
[friendica.git] / mod / dfrn_request.php
1 <?php
2
3 if(! function_exists('dfrn_request_init')) {
4 function dfrn_request_init(&$a) {
5
6         if($a->argc > 1)
7                 $which = $a->argv[1];
8
9         profile_load($a,$which);
10
11         return;
12 }}
13
14
15 if(! function_exists('dfrn_request_post')) {
16 function dfrn_request_post(&$a) {
17
18         if(($a->argc != 2) || (! count($a->profile)))
19                 return;
20
21
22         if($_POST['cancel']) {
23                 goaway($a->get_baseurl());
24         } 
25
26
27         // We've introduced ourself to another cell, then have been returned to our own cell
28         // to confirm the request, and then we've clicked submit (perhaps after logging in). 
29         // That brings us here:
30
31         if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
32
33                 // Ensure this is a valid request
34  
35                 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
36
37
38                         $dfrn_url = notags(trim($_POST['dfrn_url']));
39                         $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
40                         $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
41
42                         $contact_record = null;
43         
44                         if(x($dfrn_url)) {
45         
46                                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
47                                         intval(local_user()),
48                                         dbesc($dfrn_url)
49                                 );
50         
51                                 if(count($r)) {
52                                         if(strlen($r[0]['dfrn-id'])) {
53                                                 notice( t("This introduction has already been accepted.") . EOL );
54                                                 return;
55                                         }
56                                         else
57                                                 $contact_record = $r[0];
58                                 }
59         
60                                 if(is_array($contact_record)) {
61                                         $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
62                                                 intval($aes_allow),
63                                                 intval($contact_record['id'])
64                                         );
65                                 }
66                                 else {
67         
68                                         require_once('Scrape.php');
69         
70         
71                                         $parms = scrape_dfrn($dfrn_url);
72         
73                                         if(! count($parms)) {
74                                                 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
75                                                 return;
76                                         }
77                                         else {
78                                                 if(! x($parms,'fn'))
79                                                         notice( t('Warning: profile location has no identifiable owner name.') . EOL );
80                                                 if(! x($parms,'photo'))
81                                                         notice( t('Warning: profile location has no profile photo.') . EOL );
82                                                 $invalid = validate_dfrn($parms);               
83                                                 if($invalid) {
84                                                         notice( $invalid . t(' required parameter') 
85                                                                 . (($invalid == 1) ? t(" was ") : t("s were ") )
86                                                                 . t("not found at the given location.") . EOL ) ;
87                                                         return;
88                                                 }
89                                         }
90
91
92
93                                         $dfrn_request = $parms['dfrn-request'];
94
95                                         dbesc_array($parms);
96
97
98                                         $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `nick`, `photo`, `site-pubkey`,
99                                                 `request`, `confirm`, `notify`, `poll`, `aes_allow`) 
100                                                 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
101                                                 intval(local_user()),
102                                                 datetime_convert(),
103                                                 dbesc($dfrn_url),
104                                                 $parms['fn'],
105                                                 $parms['nick'],
106                                                 $parms['photo'],
107                                                 $parms['key'],
108                                                 $parms['dfrn-request'],
109                                                 $parms['dfrn-confirm'],
110                                                 $parms['dfrn-notify'],
111                                                 $parms['dfrn-poll'],
112                                                 intval($aes_allow)
113                                         );
114                                 }
115
116                                 if($r) {
117                                         notice( t("Introduction complete.") . EOL);
118                                 }
119
120                                 // Allow the blocked remote notification to complete
121
122                                 if(is_array($contact_record))
123                                         $dfrn_request = $contact_record['request'];
124
125                                 if(strlen($dfrn_request) && strlen($confirm_key))
126                                         $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
127                                         // ignore reply
128                                 goaway($dfrn_url);
129                                 return; // NOTREACHED
130
131                         }
132
133                 }
134
135                 // invalid/bogus request
136
137                 notice( t('Unrecoverable protocol error.') . EOL );
138                 goaway($a->get_baseurl());
139                 return; // NOTREACHED
140         }
141
142         // Otherwise:
143
144         // We are the requestee. A person from a remote cell has made an introduction 
145         // on our profile web page and clicked submit. We will use their DFRN-URL to 
146         // figure out how to contact their cell.  
147
148         // Scrape the originating DFRN-URL for everything we need. Create a contact record
149         // and an introduction to show our user next time he/she logs in.
150         // Finally redirect back to the requestor so that their site can record the request.
151         // If our user (the requestee) later confirms this request, a record of it will need 
152         // to exist on the requestor's cell in order for the confirmation process to complete.. 
153
154         // It's possible that neither the requestor or the requestee are logged in at the moment,
155         // and the requestor does not yet have any credentials to the requestee profile.
156
157         // Who is the requestee? We've already loaded their profile which means their nickname should be
158         // in $a->argv[1] and we should have their complete info in $a->profile.
159
160         if(! (is_array($a->profile) && count($a->profile))) {
161                 notice( t('Profile unavailable.') . EOL);
162                 return;
163         }
164
165         $nickname = $a->profile['nickname'];
166         $notify_flags = $a->profile['notify-flags'];
167         $uid = $a->profile['uid'];
168
169         $contact_record = null;
170         $failed = false;
171         $parms = null;
172
173
174         if( x($_POST,'dfrn_url')) {
175
176                 $url = trim($_POST['dfrn_url']);
177                 if(! strlen($url)) {
178                         notice( t("Invalid locator") . EOL );
179                         return;
180                 }
181
182                 // Canonicalise email-style profile locator
183
184                 $url = webfinger_dfrn($url);
185
186                 if(substr($url,0,5) === 'stat:') {
187                         $network = 'stat';
188                         $url = substr($url,5);
189                 }
190                 else {
191                         $network = 'dfrn';
192                 }
193
194                 logger('dfrn_request: url: ' . $url);
195
196                 if(! strlen($url)) {
197                         notice( t("Unable to resolve your name at the provided location.") . EOL);                      
198                         return;
199                 }
200
201
202                 if($network === 'dfrn') {
203                         $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1", 
204                                 intval($uid),
205                                 dbesc($url)
206                         );
207
208                         if(count($ret)) {
209                                 if(strlen($ret[0]['issued-id'])) {
210                                         notice( t('You have already introduced yourself here.') . EOL );
211                                         return;
212                                 }
213                                 elseif($ret[0]['rel'] == REL_BUD) {
214                                         notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
215                                         return;
216                                 }
217                                 else {
218                                         $contact_record = $ret[0];
219                                         $parms = array('dfrn-request' => $ret[0]['request']);
220                                 }
221                         }
222
223                         $issued_id = random_string();
224
225                         if(is_array($contact_record)) {
226                                 // There is a contact record but no issued-id, so this
227                                 // is a reciprocal introduction from a known contact
228                                 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
229                                         dbesc($issued_id),
230                                         intval($contact_record['id'])
231                                 );
232                         }
233                         else {
234                                 if(! validate_url($url)) {
235                                         notice( t('Invalid profile URL.') . EOL);
236                                         goaway($a->get_baseurl() . '/' . $a->cmd);
237                                         return; // NOTREACHED
238                                 }
239
240                                 if(! allowed_url($url)) {
241                                         notice( t('Disallowed profile URL.') . EOL);
242                                         goaway($a->get_baseurl() . '/' . $a->cmd);
243                                         return; // NOTREACHED
244                                 }
245                         
246
247                                 require_once('Scrape.php');
248
249                                 $parms = scrape_dfrn($url);
250
251                                 if(! count($parms)) {
252                                         notice( t('Profile location is not valid or does not contain profile information.') . EOL );
253                                         goaway($a->get_baseurl() . '/' . $a->cmd);
254                                 }
255                                 else {
256                                         if(! x($parms,'fn'))
257                                                 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
258                                         if(! x($parms,'photo'))
259                                                 notice( t('Warning: profile location has no profile photo.') . EOL );
260                                         $invalid = validate_dfrn($parms);               
261                                         if($invalid) {
262                                                 notice( $invalid . t(' required parameter') 
263                                                         . (($invalid == 1) ? t(" was ") : t("s were ") )
264                                                         . t("not found at the given location.") . EOL ) ;
265         
266                                                 return;
267                                         }
268                                 }
269
270
271                                 $parms['url'] = $url;
272                                 $parms['issued-id'] = $issued_id;
273
274
275                                 dbesc_array($parms);
276                                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
277                                         `request`, `confirm`, `notify`, `poll` )
278                                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
279                                         intval($uid),
280                                         datetime_convert(),
281                                         $parms['url'],
282                                         $parms['fn'],
283                                         $parms['nick'],
284                                         $parms['issued-id'],
285                                         $parms['photo'],
286                                         $parms['key'],
287                                         $parms['dfrn-request'],
288                                         $parms['dfrn-confirm'],
289                                         $parms['dfrn-notify'],
290                                         $parms['dfrn-poll']
291                                 );
292
293                                 // find the contact record we just created
294                                 if($r) {        
295                                         $r = q("SELECT `id` FROM `contact` 
296                                                 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
297                                                 intval($uid),
298                                                 $parms['url'],
299                                                 $parms['issued-id']
300                                         );
301                                         if(count($r)) 
302                                                 $contact_record = $r[0];
303                                 }
304         
305                         }
306                         if($r === false) {
307                                 notice( t('Failed to update contact record.') . EOL );
308                                 return;
309                         }
310
311                         $hash = random_string() . (string) time();   // Generate a confirm_key
312         
313                         if(is_array($contact_record)) {
314                                 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
315                                         VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
316                                         intval($uid),
317                                         intval($contact_record['id']),
318                                         ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
319                                         dbesc(notags(trim($_POST['dfrn-request-message']))),
320                                         dbesc($hash),
321                                         dbesc(datetime_convert())
322                                 );
323                         }
324         
325                         // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
326
327                         if(! $failed) 
328                                 notice( t('Your introduction has been sent.') . EOL );
329
330                         // "Homecoming" - send the requestor back to their site to record the introduction.
331
332                         $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
333                         $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
334
335                         goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" 
336                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
337                                 . '&confirm_key='  . $hash 
338                                 . (($aes_allow) ? "&aes_allow=1" : "")
339                         );
340                         // NOTREACHED
341                         // END $network === 'dfrn'
342                 }
343                 elseif($network === 'stat') {
344                         
345                         /**
346                         *
347                         * OStatus network
348                         * Check contact existence
349                         * Try and scrape together enough information to create a contact record, with us as REL_VIP
350                         * Substitute our user's feed URL into $url template
351                         * Send the subscriber home to subscribe
352                         *
353                         **/
354
355                         $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
356                         goaway($url);
357                         // NOTREACHED
358                         // END $network === 'stat'
359                 }
360
361         }       return;
362 }}
363
364
365
366
367 if(! function_exists('dfrn_request_content')) {
368 function dfrn_request_content(&$a) {
369
370         
371
372         if(($a->argc != 2) || (! count($a->profile)))
373                 return "";
374
375         $a->page['template'] = 'profile';
376
377         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
378         // to send us to the post section to record the introduction.
379
380         if(x($_GET,'dfrn_url')) {
381
382                 if(! local_user()) {
383                         notice( t("Please login to confirm introduction.") . EOL );
384                         return login();
385                 }
386
387                 // Edge case, but can easily happen in the wild. This person is authenticated, 
388                 // but not as the person who needs to deal with this request.
389
390                 if ($a->user['nickname'] != $a->argv[1]) {
391                         notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
392                         return login();
393                 }
394
395                 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
396                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
397                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
398                 $o .= load_view_file("view/dfrn_req_confirm.tpl");
399                 $o  = replace_macros($o,array(
400                         '$dfrn_url' => $dfrn_url,
401                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
402                         '$confirm_key' => $confirm_key,
403                         '$username' => $a->user['username'], 
404                         '$uid' => $_SESSION['uid'],
405                         '$nickname' => $a->user['nickname'],
406                         'dfrn_rawurl' => $_GET['dfrn_url']
407                         ));
408                 return $o;
409
410         }
411         elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) { 
412
413                 // we are the requestee and it is now safe to send our user their introduction,
414                 // We could just unblock it, but first we have to jump through a few hoops to 
415                 // send an email, or even to find out if we need to send an email. 
416
417                 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
418                         dbesc($_GET['confirm_key'])
419                 );
420
421                 if(count($intro)) {
422
423                         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
424                                 WHERE `contact`.`id` = %d LIMIT 1",
425                                 intval($intro[0]['contact-id'])
426                         );
427
428                         $auto_confirm = false;
429
430                         if(count($r)) {
431                                 if($r[0]['page-flags'] != PAGE_NORMAL)
432                                         $auto_confirm = true;                           
433                                 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
434                                         $email_tpl = load_view_file('view/request_notify_eml.tpl');
435                                         $email = replace_macros($email_tpl, array(
436                                                 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
437                                                 '$url' => stripslashes($r[0]['url']),
438                                                 '$myname' => $r[0]['username'],
439                                                 '$siteurl' => $a->get_baseurl(),
440                                                 '$sitename' => $a->config['sitename']
441                                         ));
442                                         $res = mail($r[0]['email'], 
443                                                 t("Introduction received at ") . $a->config['sitename'],
444                                                 $email,
445                                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
446                                         // This is a redundant notification - no point throwing errors if it fails.
447                                 }
448                                 if($auto_confirm) {
449                                         require_once('mod/dfrn_confirm.php');
450                                         $handsfree = array(
451                                                 'uid' => $r[0]['uid'],
452                                                 'node' => $r[0]['nickname'],
453                                                 'dfrn_id' => $r[0]['issued-id'],
454                                                 'intro_id' => $intro[0]['id'],
455                                                 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
456                                         );
457                                         dfrn_confirm_post($a,$handsfree);
458                                 }
459
460                         }
461
462                         if(! $auto_confirm) {
463
464                                 // If we are auto_confirming, this record will have already been nuked
465                                 // in dfrn_confirm_post()
466
467                                 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
468                                         dbesc($_GET['confirm_key'])
469                                 );
470                         }
471                 }
472                 killme();
473                 return; // NOTREACHED
474         }
475         else {
476                 $myaddr = ((x($_GET,'address')) ? urldecode($_GET['address']) : '');
477                 // Normal web request. Display our user's introduction form. 
478                 if($a->profile['page-flags'] == PAGE_NORMAL)
479                         $tpl = load_view_file('view/dfrn_request.tpl');
480                 else
481                         $tpl = load_view_file('view/auto_request.tpl');
482                 $o .= replace_macros($tpl,array(
483                         '$nickname' => $a->argv[1],
484                         '$name' => $a->profile['name'],
485                         '$myaddr' => $myaddr
486                 ));
487                 return $o;
488         }
489
490         return; // Somebody is fishing.
491 }}