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