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