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