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