]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
Merge pull request #2224 from rabuzarus/2812_js_scrollbars
[friendica.git] / mod / dfrn_request.php
1 <?php
2
3 /**
4  *
5  * Module: dfrn_request
6  *
7  * Purpose: Handles communication associated with the issuance of
8  * friend requests.
9  *
10  */
11
12 require_once('include/enotify.php');
13
14 if(! function_exists('dfrn_request_init')) {
15 function dfrn_request_init(&$a) {
16
17         if($a->argc > 1)
18                 $which = $a->argv[1];
19
20         profile_load($a,$which);
21         return;
22 }}
23
24
25 /**
26  * Function: dfrn_request_post
27  *
28  * Purpose:
29  * Handles multiple scenarios.
30  *
31  * Scenario 1:
32  * Clicking 'submit' on a friend request page.
33  *
34  * Scenario 2:
35  * Following Scenario 1, we are brought back to our home site
36  * in order to link our friend request with our own server cell.
37  * After logging in, we click 'submit' to approve the linkage.
38  *
39  */
40
41 if(! function_exists('dfrn_request_post')) {
42 function dfrn_request_post(&$a) {
43
44         if(($a->argc != 2) || (! count($a->profile)))
45                 return;
46
47
48         if(x($_POST, 'cancel')) {
49                 goaway(z_root());
50         }
51
52
53         /**
54          *
55          * Scenario 2: We've introduced ourself to another cell, then have been returned to our own cell
56          * to confirm the request, and then we've clicked submit (perhaps after logging in).
57          * That brings us here:
58          *
59          */
60
61         if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
62
63                 /**
64                  * Ensure this is a valid request
65                  */
66
67                 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
68
69
70                         $dfrn_url    = notags(trim($_POST['dfrn_url']));
71                         $aes_allow   = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
72                         $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
73                         $hidden = ((x($_POST,'hidden-contact')) ? intval($_POST['hidden-contact']) : 0);
74                         $contact_record = null;
75
76                         if(x($dfrn_url)) {
77
78                                 /**
79                                  * Lookup the contact based on their URL (which is the only unique thing we have at the moment)
80                                  */
81
82                                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND (`url` = '%s' OR `nurl` = '%s') AND `self` = 0 LIMIT 1",
83                                         intval(local_user()),
84                                         dbesc($dfrn_url),
85                                         dbesc(normalise_link($dfrn_url))
86                                 );
87
88                                 if(count($r)) {
89                                         if(strlen($r[0]['dfrn-id'])) {
90
91                                                 /**
92                                                  * We don't need to be here. It has already happened.
93                                                  */
94
95                                                 notice( t("This introduction has already been accepted.") . EOL );
96                                                 return;
97                                         }
98                                         else
99                                                 $contact_record = $r[0];
100                                 }
101
102                                 if(is_array($contact_record)) {
103                                         $r = q("UPDATE `contact` SET `ret-aes` = %d, hidden = %d WHERE `id` = %d",
104                                                 intval($aes_allow),
105                                                 intval($hidden),
106                                                 intval($contact_record['id'])
107                                         );
108                                 }
109                                 else {
110
111                                         /**
112                                          * Scrape the other site's profile page to pick up the dfrn links, key, fn, and photo
113                                          */
114
115                                         require_once('include/Scrape.php');
116
117                                         $parms = scrape_dfrn($dfrn_url);
118
119                                         if(! count($parms)) {
120                                                 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
121                                                 return;
122                                         }
123                                         else {
124                                                 if(! x($parms,'fn'))
125                                                         notice( t('Warning: profile location has no identifiable owner name.') . EOL );
126                                                 if(! x($parms,'photo'))
127                                                         notice( t('Warning: profile location has no profile photo.') . EOL );
128                                                 $invalid = validate_dfrn($parms);
129                                                 if($invalid) {
130                                                         notice( sprintf( tt("%d required parameter was not found at the given location",
131                                                                                                 "%d required parameters were not found at the given location",
132                                                                                                 $invalid), $invalid) . EOL );
133                                                         return;
134                                                 }
135                                         }
136
137                                         $dfrn_request = $parms['dfrn-request'];
138
139                                         /********* Escape the entire array ********/
140
141                                         dbesc_array($parms);
142
143                                         /******************************************/
144
145                                         /**
146                                          * Create a contact record on our site for the other person
147                                          */
148
149                                         $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `nurl`, `addr`, `name`, `nick`, `photo`, `site-pubkey`,
150                                                 `request`, `confirm`, `notify`, `poll`, `poco`, `network`, `aes_allow`, `hidden`)
151                                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
152                                                 intval(local_user()),
153                                                 datetime_convert(),
154                                                 dbesc($dfrn_url),
155                                                 dbesc(normalise_link($dfrn_url)),
156                                                 $parms['addr'],
157                                                 $parms['fn'],
158                                                 $parms['nick'],
159                                                 $parms['photo'],
160                                                 $parms['key'],
161                                                 $parms['dfrn-request'],
162                                                 $parms['dfrn-confirm'],
163                                                 $parms['dfrn-notify'],
164                                                 $parms['dfrn-poll'],
165                                                 $parms['dfrn-poco'],
166                                                 dbesc(NETWORK_DFRN),
167                                                 intval($aes_allow),
168                                                 intval($hidden)
169                                         );
170                                 }
171
172                                 if($r) {
173                                         info( t("Introduction complete.") . EOL);
174                                 }
175
176                                 $r = q("select id from contact where uid = %d and url = '%s' and `site-pubkey` = '%s' limit 1",
177                                         intval(local_user()),
178                                         dbesc($dfrn_url),
179                                         $parms['key'] // this was already escaped
180                                 );
181                                 if(count($r)) {
182                                         $g = q("select def_gid from user where uid = %d limit 1",
183                                                 intval(local_user())
184                                         );
185                                         if($g && intval($g[0]['def_gid'])) {
186                                                 require_once('include/group.php');
187                                                 group_add_member(local_user(),'',$r[0]['id'],$g[0]['def_gid']);
188                                         }
189                                         $forwardurl = $a->get_baseurl()."/contacts/".$r[0]['id'];
190                                 } else
191                                         $forwardurl = $a->get_baseurl()."/contacts";
192
193                                 /**
194                                  * Allow the blocked remote notification to complete
195                                  */
196
197                                 if(is_array($contact_record))
198                                         $dfrn_request = $contact_record['request'];
199
200                                 if(strlen($dfrn_request) && strlen($confirm_key))
201                                         $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
202
203                                 // (ignore reply, nothing we can do it failed)
204
205                                 // Old: goaway(zrl($dfrn_url));
206                                 goaway($forwardurl);
207                                 return; // NOTREACHED
208
209                         }
210
211                 }
212
213                 // invalid/bogus request
214
215                 notice( t('Unrecoverable protocol error.') . EOL );
216                 goaway(z_root());
217                 return; // NOTREACHED
218         }
219
220         /**
221          * Otherwise:
222          *
223          * Scenario 1:
224          * We are the requestee. A person from a remote cell has made an introduction
225          * on our profile web page and clicked submit. We will use their DFRN-URL to
226          * figure out how to contact their cell.
227          *
228          * Scrape the originating DFRN-URL for everything we need. Create a contact record
229          * and an introduction to show our user next time he/she logs in.
230          * Finally redirect back to the requestor so that their site can record the request.
231          * If our user (the requestee) later confirms this request, a record of it will need
232          * to exist on the requestor's cell in order for the confirmation process to complete..
233          *
234          * It's possible that neither the requestor or the requestee are logged in at the moment,
235          * and the requestor does not yet have any credentials to the requestee profile.
236          *
237          * Who is the requestee? We've already loaded their profile which means their nickname should be
238          * in $a->argv[1] and we should have their complete info in $a->profile.
239          *
240          */
241
242         if(! (is_array($a->profile) && count($a->profile))) {
243                 notice( t('Profile unavailable.') . EOL);
244                 return;
245         }
246
247         $nickname       = $a->profile['nickname'];
248         $notify_flags   = $a->profile['notify-flags'];
249         $uid            = $a->profile['uid'];
250         $maxreq         = intval($a->profile['maxreq']);
251         $contact_record = null;
252         $failed         = false;
253         $parms          = null;
254
255
256         if( x($_POST,'dfrn_url')) {
257
258                 /**
259                  * Block friend request spam
260                  */
261
262                 if($maxreq) {
263                         $r = q("SELECT * FROM `intro` WHERE `datetime` > '%s' AND `uid` = %d",
264                                 dbesc(datetime_convert('UTC','UTC','now - 24 hours')),
265                                 intval($uid)
266                         );
267                         if(count($r) > $maxreq) {
268                                 notice( sprintf( t('%s has received too many connection requests today.'),  $a->profile['name']) . EOL);
269                                 notice( t('Spam protection measures have been invoked.') . EOL);
270                                 notice( t('Friends are advised to please try again in 24 hours.') . EOL);
271                                 return;
272                         }
273                 }
274
275                 /**
276                  *
277                  * Cleanup old introductions that remain blocked.
278                  * Also remove the contact record, but only if there is no existing relationship
279                  * Do not remove email contacts as these may be awaiting email verification
280                  */
281
282                 $r = q("SELECT `intro`.*, `intro`.`id` AS `iid`, `contact`.`id` AS `cid`, `contact`.`rel`
283                         FROM `intro` LEFT JOIN `contact` on `intro`.`contact-id` = `contact`.`id`
284                         WHERE `intro`.`blocked` = 1 AND `contact`.`self` = 0
285                         AND `contact`.`network` != '%s'
286                         AND `intro`.`datetime` < UTC_TIMESTAMP() - INTERVAL 30 MINUTE ",
287                         dbesc(NETWORK_MAIL2)
288                 );
289                 if(count($r)) {
290                         foreach($r as $rr) {
291                                 if(! $rr['rel']) {
292                                         q("DELETE FROM `contact` WHERE `id` = %d",
293                                                 intval($rr['cid'])
294                                         );
295                                 }
296                                 q("DELETE FROM `intro` WHERE `id` = %d",
297                                         intval($rr['iid'])
298                                 );
299                         }
300                 }
301
302                 /**
303                  *
304                  * Cleanup any old email intros - which will have a greater lifetime
305                  */
306
307                 $r = q("SELECT `intro`.*, `intro`.`id` AS `iid`, `contact`.`id` AS `cid`, `contact`.`rel`
308                         FROM `intro` LEFT JOIN `contact` on `intro`.`contact-id` = `contact`.`id`
309                         WHERE `intro`.`blocked` = 1 AND `contact`.`self` = 0
310                         AND `contact`.`network` = '%s'
311                         AND `intro`.`datetime` < UTC_TIMESTAMP() - INTERVAL 3 DAY ",
312                         dbesc(NETWORK_MAIL2)
313                 );
314                 if(count($r)) {
315                         foreach($r as $rr) {
316                                 if(! $rr['rel']) {
317                                         q("DELETE FROM `contact` WHERE `id` = %d",
318                                                 intval($rr['cid'])
319                                         );
320                                 }
321                                 q("DELETE FROM `intro` WHERE `id` = %d",
322                                         intval($rr['iid'])
323                                 );
324                         }
325                 }
326
327                 $email_follow = (x($_POST,'email_follow') ? intval($_POST['email_follow']) : 0);
328                 $real_name = (x($_POST,'realname') ? notags(trim($_POST['realname'])) : '');
329
330                 $url = trim($_POST['dfrn_url']);
331                 if(! strlen($url)) {
332                         notice( t("Invalid locator") . EOL );
333                         return;
334                 }
335
336                 $hcard = '';
337
338                 if($email_follow) {
339
340                         if(! validate_email($url)) {
341                                 notice( t('Invalid email address.') . EOL);
342                                 return;
343                         }
344
345                         $addr    = $url;
346                         $name    = ($realname) ? $realname : $addr;
347                         $nick    = substr($addr,0,strpos($addr,'@'));
348                         $url     = 'http://' . substr($addr,strpos($addr,'@') + 1);
349                         $nurl    = normalise_url($host);
350                         $poll    = 'email ' . random_string();
351                         $notify  = 'smtp ' . random_string();
352                         $blocked = 1;
353                         $pending = 1;
354                         $network = NETWORK_MAIL2;
355                         $rel     = CONTACT_IS_FOLLOWER;
356
357                         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
358                         if(get_config('system','dfrn_only'))
359                                 $mail_disabled = 1;
360
361                         if(! $mail_disabled) {
362                                 $failed = false;
363                                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
364                                         intval($uid)
365                                 );
366                                 if(! count($r)) {
367
368                                         notice( t('This account has not been configured for email. Request failed.') . EOL);
369                                         return;
370                                 }
371                         }
372
373                         $r = q("insert into contact ( uid, created, addr, name, nick, url, nurl, poll, notify, blocked, pending, network, rel )
374                                 values( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d ) ",
375                                 intval($uid),
376                                 dbesc(datetime_convert()),
377                                 dbesc($addr),
378                                 dbesc($name),
379                                 dbesc($nick),
380                                 dbesc($url),
381                                 dbesc($nurl),
382                                 dbesc($poll),
383                                 dbesc($notify),
384                                 intval($blocked),
385                                 intval($pending),
386                                 dbesc($network),
387                                 intval($rel)
388                         );
389
390                         $r = q("select id from contact where poll = '%s' and uid = %d limit 1",
391                                 dbesc($poll),
392                                 intval($uid)
393                         );
394                         if(count($r)) {
395                                 $contact_id = $r[0]['id'];
396
397                                 $g = q("select def_gid from user where uid = %d limit 1",
398                                         intval($uid)
399                                 );
400                                 if($g && intval($g[0]['def_gid'])) {
401                                         require_once('include/group.php');
402                                         group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
403                                 }
404
405                                 $photo = avatar_img($addr);
406
407                                 $r = q("UPDATE `contact` SET
408                                         `photo` = '%s',
409                                         `thumb` = '%s',
410                                         `micro` = '%s',
411                                         `name-date` = '%s',
412                                         `uri-date` = '%s',
413                                         `avatar-date` = '%s',
414                                         `hidden` = 0,
415                                         WHERE `id` = %d
416                                 ",
417                                         dbesc($photos[0]),
418                                         dbesc($photos[1]),
419                                         dbesc($photos[2]),
420                                         dbesc(datetime_convert()),
421                                         dbesc(datetime_convert()),
422                                         dbesc(datetime_convert()),
423                                         intval($contact_id)
424                                 );
425                         }
426
427                         // contact is created. Now create an introduction
428
429                         $hash = random_string();
430
431                         $r = q("insert into intro ( uid, `contact-id`, knowyou, note, hash, datetime, blocked )
432                                 values( %d , %d, %d, '%s', '%s', '%s', %d ) ",
433                                 intval($uid),
434                                 intval($contact_id),
435                                 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
436                                 dbesc(notags(trim($_POST['dfrn-request-message']))),
437                                 dbesc($hash),
438                                 dbesc(datetime_convert()),
439                                 1
440                         );
441
442                         // Next send an email verify form to the requestor.
443
444                 } else {
445                         // Detect the network
446                         $data = probe_url($url);
447                         $network = $data["network"];
448
449                         // Canonicalise email-style profile locator
450                         $url = webfinger_dfrn($url,$hcard);
451
452                         if (substr($url,0,5) === 'stat:') {
453
454                                 // Every time we detect the remote subscription we define this as OStatus.
455                                 // We do this even if it is not OStatus.
456                                 // we only need to pass this through another section of the code.
457                                 if ($network != NETWORK_DIASPORA)
458                                         $network = NETWORK_OSTATUS;
459
460                                 $url = substr($url,5);
461                         } else
462                                 $network = NETWORK_DFRN;
463                 }
464
465                 logger('dfrn_request: url: ' . $url);
466
467                 if($network === NETWORK_DFRN) {
468                         $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
469                                 intval($uid),
470                                 dbesc($url)
471                         );
472
473                         if(count($ret)) {
474                                 if(strlen($ret[0]['issued-id'])) {
475                                         notice( t('You have already introduced yourself here.') . EOL );
476                                         return;
477                                 }
478                                 elseif($ret[0]['rel'] == CONTACT_IS_FRIEND) {
479                                         notice( sprintf( t('Apparently you are already friends with %s.'), $a->profile['name']) . EOL);
480                                         return;
481                                 }
482                                 else {
483                                         $contact_record = $ret[0];
484                                         $parms = array('dfrn-request' => $ret[0]['request']);
485                                 }
486                         }
487
488                         $issued_id = random_string();
489
490                         if(is_array($contact_record)) {
491                                 // There is a contact record but no issued-id, so this
492                                 // is a reciprocal introduction from a known contact
493                                 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d",
494                                         dbesc($issued_id),
495                                         intval($contact_record['id'])
496                                 );
497                         }
498                         else {
499                                 if(! validate_url($url)) {
500                                         notice( t('Invalid profile URL.') . EOL);
501                                         goaway($a->get_baseurl() . '/' . $a->cmd);
502                                         return; // NOTREACHED
503                                 }
504
505                                 if(! allowed_url($url)) {
506                                         notice( t('Disallowed profile URL.') . EOL);
507                                         goaway($a->get_baseurl() . '/' . $a->cmd);
508                                         return; // NOTREACHED
509                                 }
510
511
512                                 require_once('include/Scrape.php');
513
514                                 $parms = scrape_dfrn(($hcard) ? $hcard : $url);
515
516                                 if(! count($parms)) {
517                                         notice( t('Profile location is not valid or does not contain profile information.') . EOL );
518                                         goaway($a->get_baseurl() . '/' . $a->cmd);
519                                 }
520                                 else {
521                                         if(! x($parms,'fn'))
522                                                 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
523                                         if(! x($parms,'photo'))
524                                                 notice( t('Warning: profile location has no profile photo.') . EOL );
525                                         $invalid = validate_dfrn($parms);
526                                         if($invalid) {
527                                                 notice( sprintf( tt("%d required parameter was not found at the given location",
528                                                                                         "%d required parameters were not found at the given location",
529                                                                                         $invalid), $invalid) . EOL );
530
531                                                 return;
532                                         }
533                                 }
534
535
536                                 $parms['url'] = $url;
537                                 $parms['issued-id'] = $issued_id;
538
539
540                                 dbesc_array($parms);
541                                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
542                                         `request`, `confirm`, `notify`, `poll`, `poco`, `network` )
543                                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
544                                         intval($uid),
545                                         dbesc(datetime_convert()),
546                                         $parms['url'],
547                                         dbesc(normalise_link($parms['url'])),
548                                         $parms['addr'],
549                                         $parms['fn'],
550                                         $parms['nick'],
551                                         $parms['issued-id'],
552                                         $parms['photo'],
553                                         $parms['key'],
554                                         $parms['dfrn-request'],
555                                         $parms['dfrn-confirm'],
556                                         $parms['dfrn-notify'],
557                                         $parms['dfrn-poll'],
558                                         $parms['dfrn-poco'],
559                                         dbesc(NETWORK_DFRN)
560                                 );
561
562                                 // find the contact record we just created
563                                 if($r) {
564                                         $r = q("SELECT `id` FROM `contact`
565                                                 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
566                                                 intval($uid),
567                                                 $parms['url'],
568                                                 $parms['issued-id']
569                                         );
570                                         if(count($r))
571                                                 $contact_record = $r[0];
572                                 }
573
574                         }
575                         if($r === false) {
576                                 notice( t('Failed to update contact record.') . EOL );
577                                 return;
578                         }
579
580                         $hash = random_string() . (string) time();   // Generate a confirm_key
581
582                         if(is_array($contact_record)) {
583                                 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
584                                         VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
585                                         intval($uid),
586                                         intval($contact_record['id']),
587                                         ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
588                                         dbesc(notags(trim($_POST['dfrn-request-message']))),
589                                         dbesc($hash),
590                                         dbesc(datetime_convert())
591                                 );
592                         }
593
594                         // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
595
596                         if(! $failed)
597                                 info( t('Your introduction has been sent.') . EOL );
598
599                         // "Homecoming" - send the requestor back to their site to record the introduction.
600
601                         $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
602                         $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
603
604                         goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
605                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
606                                 . '&confirm_key='  . $hash
607                                 . (($aes_allow) ? "&aes_allow=1" : "")
608                         );
609                         // NOTREACHED
610                         // END $network === NETWORK_DFRN
611                 } elseif (($network != NETWORK_PHANTOM) AND ($url != "")) {
612
613                         /**
614                          *
615                          * Substitute our user's feed URL into $url template
616                          * Send the subscriber home to subscribe
617                          *
618                          */
619
620                         // Diaspora needs the uri in the format user@domain.tld
621                         // Diaspora will support the remote subscription in a future version
622                         if ($network == NETWORK_DIASPORA) {
623                                 $uri = $nickname.'@'.$a->get_hostname();
624
625                                 if ($a->get_path())
626                                         $uri .= '/'.$a->get_path();
627
628                                 $uri = urlencode($uri);
629                         } else
630                                 $uri = $a->get_baseurl().'/profile/'.$nickname;
631
632                         $url = str_replace('{uri}', $uri, $url);
633                         goaway($url);
634                         // NOTREACHED
635                         // END $network != NETWORK_PHANTOM
636                 } else {
637                         notice(t("Remote subscription can't be done for your network. Please subscribe directly on your system.").EOL);
638                         return;
639                 }
640
641         }       return;
642 }}
643
644
645
646
647 if(! function_exists('dfrn_request_content')) {
648 function dfrn_request_content(&$a) {
649
650         if(($a->argc != 2) || (! count($a->profile)))
651                 return "";
652
653
654         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
655         // to send us to the post section to record the introduction.
656
657         if(x($_GET,'dfrn_url')) {
658
659                 if(! local_user()) {
660                         info( t("Please login to confirm introduction.") . EOL );
661                         /* setup the return URL to come back to this page if they use openid */
662                         $_SESSION['return_url'] = $a->query_string;
663                         return login();
664                 }
665
666                 // Edge case, but can easily happen in the wild. This person is authenticated,
667                 // but not as the person who needs to deal with this request.
668
669                 if ($a->user['nickname'] != $a->argv[1]) {
670                         notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
671                         return login();
672                 }
673
674                 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
675                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
676                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
677
678                 // Checking fastlane for validity
679                 if (x($_SESSION, "fastlane") AND (normalise_link($_SESSION["fastlane"]) == normalise_link($dfrn_url))) {
680                         $_POST["dfrn_url"] = $dfrn_url;
681                         $_POST["confirm_key"] = $confirm_key;
682                         $_POST["localconfirm"] = 1;
683                         $_POST["hidden-contact"] = 0;
684                         $_POST["submit"] = t('Confirm');
685
686                         dfrn_request_post($a);
687
688                         killme();
689                         return; // NOTREACHED
690                 }
691
692                 $tpl = get_markup_template("dfrn_req_confirm.tpl");
693                 $o  = replace_macros($tpl,array(
694                         '$dfrn_url' => $dfrn_url,
695                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
696                         '$hidethem' => t('Hide this contact'),
697                         '$hidechecked' => '',
698                         '$confirm_key' => $confirm_key,
699                         '$welcome' => sprintf( t('Welcome home %s.'), $a->user['username']),
700                         '$please' => sprintf( t('Please confirm your introduction/connection request to %s.'), $dfrn_url),
701                         '$submit' => t('Confirm'),
702                         '$uid' => $_SESSION['uid'],
703                         '$nickname' => $a->user['nickname'],
704                         'dfrn_rawurl' => $_GET['dfrn_url']
705                         ));
706                 return $o;
707
708         }
709         elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
710
711                 // we are the requestee and it is now safe to send our user their introduction,
712                 // We could just unblock it, but first we have to jump through a few hoops to
713                 // send an email, or even to find out if we need to send an email.
714
715                 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
716                         dbesc($_GET['confirm_key'])
717                 );
718
719                 if(count($intro)) {
720
721                         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
722                                 WHERE `contact`.`id` = %d LIMIT 1",
723                                 intval($intro[0]['contact-id'])
724                         );
725
726                         $auto_confirm = false;
727
728                         if(count($r)) {
729                                 if(($r[0]['page-flags'] != PAGE_NORMAL) && ($r[0]['page-flags'] != PAGE_PRVGROUP))
730                                         $auto_confirm = true;
731
732                                 if(! $auto_confirm) {
733
734                                         notification(array(
735                                                 'type'         => NOTIFY_INTRO,
736                                                 'notify_flags' => $r[0]['notify-flags'],
737                                                 'language'     => $r[0]['language'],
738                                                 'to_name'      => $r[0]['username'],
739                                                 'to_email'     => $r[0]['email'],
740                                                 'uid'          => $r[0]['uid'],
741                                                 'link'             => $a->get_baseurl() . '/notifications/intros',
742                                                 'source_name'  => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
743                                                 'source_link'  => $r[0]['url'],
744                                                 'source_photo' => $r[0]['photo'],
745                                                 'verb'         => ACTIVITY_REQ_FRIEND,
746                                                 'otype'        => 'intro'
747                                         ));
748                                 }
749
750                                 if($auto_confirm) {
751                                         require_once('mod/dfrn_confirm.php');
752                                         $handsfree = array(
753                                                 'uid' => $r[0]['uid'],
754                                                 'node' => $r[0]['nickname'],
755                                                 'dfrn_id' => $r[0]['issued-id'],
756                                                 'intro_id' => $intro[0]['id'],
757                                                 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0),
758                                                 'activity' => intval(get_pconfig($r[0]['uid'],'system','post_newfriend'))
759                                         );
760                                         dfrn_confirm_post($a,$handsfree);
761                                 }
762
763                         }
764
765                         if(! $auto_confirm) {
766
767                                 // If we are auto_confirming, this record will have already been nuked
768                                 // in dfrn_confirm_post()
769
770                                 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s'",
771                                         dbesc($_GET['confirm_key'])
772                                 );
773                         }
774                 }
775
776                 killme();
777                 return; // NOTREACHED
778         }
779         else {
780
781                 /**
782                  * Normal web request. Display our user's introduction form.
783                  */
784
785                 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
786                         if(! get_config('system','local_block')) {
787                                 notice( t('Public access denied.') . EOL);
788                                 return;
789                         }
790                 }
791
792
793                 /**
794                  * Try to auto-fill the profile address
795                  */
796
797                 // At first look if an address was provided
798                 // Otherwise take the local address
799                 if (x($_GET,'addr') AND ($_GET['addr'] != ""))
800                         $myaddr = hex2bin($_GET['addr']);
801                 elseif (x($_GET,'address') AND ($_GET['address'] != ""))
802                         $myaddr = $_GET['address'];
803                 elseif(local_user()) {
804                         if(strlen($a->path)) {
805                                 $myaddr = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
806                         }
807                         else {
808                                 $myaddr = $a->user['nickname'] . '@' . substr(z_root(), strpos(z_root(),'://') + 3 );
809                         }
810                 } else  // last, try a zrl
811                         $myaddr = get_my_url();
812
813                 $target_addr = $a->profile['nickname'] . '@' . substr(z_root(), strpos(z_root(),'://') + 3 );
814
815
816                 /**
817                  *
818                  * The auto_request form only has the profile address
819                  * because nobody is going to read the comments and
820                  * it doesn't matter if they know you or not.
821                  *
822                  */
823
824                 if($a->profile['page-flags'] == PAGE_NORMAL)
825                         $tpl = get_markup_template('dfrn_request.tpl');
826                 else
827                         $tpl = get_markup_template('auto_request.tpl');
828
829                 $page_desc .= t("Please enter your 'Identity Address' from one of the following supported communications networks:");
830
831                 // see if we are allowed to have NETWORK_MAIL2 contacts
832
833                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
834                 if(get_config('system','dfrn_only'))
835                         $mail_disabled = 1;
836
837                 if(! $mail_disabled) {
838                         $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
839                                 intval($a->profile['uid'])
840                         );
841                         if(! count($r))
842                                 $mail_disabled = 1;
843                 }
844
845                 // "coming soon" is disabled for now
846                 //$emailnet = (($mail_disabled) ? '' : t("<strike>Connect as an email follower</strike> \x28Coming soon\x29"));
847                 $emailnet = "";
848
849                 $invite_desc = sprintf(
850                         t('If you are not yet a member of the free social web, <a href="%s/siteinfo">follow this link to find a public Friendica site and join us today</a>.'),
851                         get_server()
852                 );
853
854                 $o .= replace_macros($tpl,array(
855                         '$header' => t('Friend/Connection Request'),
856                         '$desc' => t('Examples: jojo@demo.friendica.com, http://demo.friendica.com/profile/jojo, testuser@identi.ca'),
857                         '$pls_answer' => t('Please answer the following:'),
858                         '$does_know_you' => array('knowyou', sprintf(t('Does %s know you?'),$a->profile['name']), false, '', array(t('No'),t('Yes'))),
859                         /*'$does_know' => sprintf( t('Does %s know you?'),$a->profile['name']),
860                         '$yes' => t('Yes'),
861                         '$no' => t('No'), */
862                         '$add_note' => t('Add a personal note:'),
863                         '$page_desc' => $page_desc,
864                         '$friendica' => t('Friendica'),
865                         '$statusnet' => t('StatusNet/Federated Social Web'),
866                         '$diaspora' => t('Diaspora'),
867                         '$diasnote' => sprintf (t(' - please do not use this form.  Instead, enter %s into your Diaspora search bar.'),$target_addr),
868                         '$your_address' => t('Your Identity Address:'),
869                         '$invite_desc' => $invite_desc,
870                         '$emailnet' => $emailnet,
871                         '$submit' => t('Submit Request'),
872                         '$cancel' => t('Cancel'),
873                         '$nickname' => $a->argv[1],
874                         '$name' => $a->profile['name'],
875                         '$myaddr' => $myaddr
876                 ));
877                 return $o;
878         }
879
880         return; // Somebody is fishing.
881 }}