7 * Purpose: Handles communication associated with the issuance of
12 if(! function_exists('dfrn_request_init')) {
13 function dfrn_request_init(&$a) {
18 profile_load($a,$which);
24 * Function: dfrn_request_post
27 * Handles multiple scenarios.
30 * Clicking 'submit' on a friend request page.
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.
39 if(! function_exists('dfrn_request_post')) {
40 function dfrn_request_post(&$a) {
42 if(($a->argc != 2) || (! count($a->profile)))
46 if($_POST['cancel']) {
47 goaway($a->get_baseurl());
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:
59 if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
62 * Ensure this is a valid request
65 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
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'] : "");
72 $contact_record = null;
77 * Lookup the contact based on their URL (which is the only unique thing we have at the moment)
80 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
86 if(strlen($r[0]['dfrn-id'])) {
89 * We don't need to be here. It has already happened.
92 notice( t("This introduction has already been accepted.") . EOL );
96 $contact_record = $r[0];
99 if(is_array($contact_record)) {
100 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
102 intval($contact_record['id'])
108 * Scrape the other site's profile page to pick up the dfrn links, key, fn, and photo
111 require_once('Scrape.php');
113 $parms = scrape_dfrn($dfrn_url);
115 if(! count($parms)) {
116 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
121 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
122 if(! x($parms,'photo'))
123 notice( t('Warning: profile location has no profile photo.') . EOL );
124 $invalid = validate_dfrn($parms);
126 notice( $invalid . t(' required parameter')
127 . (($invalid == 1) ? t(" was ") : t("s were ") )
128 . t("not found at the given location.") . EOL ) ;
133 $dfrn_request = $parms['dfrn-request'];
135 /********* Escape the entire array ********/
139 /******************************************/
142 * Create a contact record on our site for the other person
145 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `nick`, `photo`, `site-pubkey`,
146 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
147 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
148 intval(local_user()),
155 $parms['dfrn-request'],
156 $parms['dfrn-confirm'],
157 $parms['dfrn-notify'],
164 notice( t("Introduction complete.") . EOL);
168 * Allow the blocked remote notification to complete
171 if(is_array($contact_record))
172 $dfrn_request = $contact_record['request'];
174 if(strlen($dfrn_request) && strlen($confirm_key))
175 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
177 // (ignore reply, nothing we can do it failed)
180 return; // NOTREACHED
186 // invalid/bogus request
188 notice( t('Unrecoverable protocol error.') . EOL );
189 goaway($a->get_baseurl());
190 return; // NOTREACHED
197 * We are the requestee. A person from a remote cell has made an introduction
198 * on our profile web page and clicked submit. We will use their DFRN-URL to
199 * figure out how to contact their cell.
201 * Scrape the originating DFRN-URL for everything we need. Create a contact record
202 * and an introduction to show our user next time he/she logs in.
203 * Finally redirect back to the requestor so that their site can record the request.
204 * If our user (the requestee) later confirms this request, a record of it will need
205 * to exist on the requestor's cell in order for the confirmation process to complete..
207 * It's possible that neither the requestor or the requestee are logged in at the moment,
208 * and the requestor does not yet have any credentials to the requestee profile.
210 * Who is the requestee? We've already loaded their profile which means their nickname should be
211 * in $a->argv[1] and we should have their complete info in $a->profile.
215 if(! (is_array($a->profile) && count($a->profile))) {
216 notice( t('Profile unavailable.') . EOL);
220 $nickname = $a->profile['nickname'];
221 $notify_flags = $a->profile['notify-flags'];
222 $uid = $a->profile['uid'];
223 $maxreq = intval($a->profile['maxreq']);
224 $contact_record = null;
229 if( x($_POST,'dfrn_url')) {
232 * Block friend request spam
236 $r = q("SELECT * FROM `intro` WHERE `datetime` > '%s' AND `uid` = %d",
237 dbesc(datetime_convert('UTC','UTC','now - 24 hours')),
240 if(count($r) > $maxreq) {
241 notice( $a->profile['name'] . t(' has received too many connection requests today.') . EOL);
242 notice( t('Spam protection measures have been invoked.') . EOL);
243 notice( t('Friends are advised to please try again in 24 hours.') . EOL);
248 $url = trim($_POST['dfrn_url']);
250 notice( t("Invalid locator") . EOL );
254 // Canonicalise email-style profile locator
256 $url = webfinger_dfrn($url);
258 if(substr($url,0,5) === 'stat:') {
260 $url = substr($url,5);
266 logger('dfrn_request: url: ' . $url);
269 notice( t("Unable to resolve your name at the provided location.") . EOL);
274 if($network === 'dfrn') {
275 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
281 if(strlen($ret[0]['issued-id'])) {
282 notice( t('You have already introduced yourself here.') . EOL );
285 elseif($ret[0]['rel'] == REL_BUD) {
286 notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
290 $contact_record = $ret[0];
291 $parms = array('dfrn-request' => $ret[0]['request']);
295 $issued_id = random_string();
297 if(is_array($contact_record)) {
298 // There is a contact record but no issued-id, so this
299 // is a reciprocal introduction from a known contact
300 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
302 intval($contact_record['id'])
306 if(! validate_url($url)) {
307 notice( t('Invalid profile URL.') . EOL);
308 goaway($a->get_baseurl() . '/' . $a->cmd);
309 return; // NOTREACHED
312 if(! allowed_url($url)) {
313 notice( t('Disallowed profile URL.') . EOL);
314 goaway($a->get_baseurl() . '/' . $a->cmd);
315 return; // NOTREACHED
319 require_once('Scrape.php');
321 $parms = scrape_dfrn($url);
323 if(! count($parms)) {
324 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
325 goaway($a->get_baseurl() . '/' . $a->cmd);
329 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
330 if(! x($parms,'photo'))
331 notice( t('Warning: profile location has no profile photo.') . EOL );
332 $invalid = validate_dfrn($parms);
334 notice( $invalid . t(' required parameter')
335 . (($invalid == 1) ? t(" was ") : t("s were ") )
336 . t("not found at the given location.") . EOL ) ;
343 $parms['url'] = $url;
344 $parms['issued-id'] = $issued_id;
348 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
349 `request`, `confirm`, `notify`, `poll` )
350 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
359 $parms['dfrn-request'],
360 $parms['dfrn-confirm'],
361 $parms['dfrn-notify'],
365 // find the contact record we just created
367 $r = q("SELECT `id` FROM `contact`
368 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
374 $contact_record = $r[0];
379 notice( t('Failed to update contact record.') . EOL );
383 $hash = random_string() . (string) time(); // Generate a confirm_key
385 if(is_array($contact_record)) {
386 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
387 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
389 intval($contact_record['id']),
390 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
391 dbesc(notags(trim($_POST['dfrn-request-message']))),
393 dbesc(datetime_convert())
397 // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
400 notice( t('Your introduction has been sent.') . EOL );
402 // "Homecoming" - send the requestor back to their site to record the introduction.
404 $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
405 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
407 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
408 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
409 . '&confirm_key=' . $hash
410 . (($aes_allow) ? "&aes_allow=1" : "")
413 // END $network === 'dfrn'
415 elseif($network === 'stat') {
420 * Check contact existence
421 * Try and scrape together enough information to create a contact record, with us as REL_VIP
422 * Substitute our user's feed URL into $url template
423 * Send the subscriber home to subscribe
427 $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
430 // END $network === 'stat'
439 if(! function_exists('dfrn_request_content')) {
440 function dfrn_request_content(&$a) {
444 if(($a->argc != 2) || (! count($a->profile)))
448 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
449 // to send us to the post section to record the introduction.
451 if(x($_GET,'dfrn_url')) {
454 notice( t("Please login to confirm introduction.") . EOL );
458 // Edge case, but can easily happen in the wild. This person is authenticated,
459 // but not as the person who needs to deal with this request.
461 if ($a->user['nickname'] != $a->argv[1]) {
462 notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
466 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
467 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
468 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
469 $o .= load_view_file("view/dfrn_req_confirm.tpl");
470 $o = replace_macros($o,array(
471 '$dfrn_url' => $dfrn_url,
472 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
473 '$confirm_key' => $confirm_key,
474 '$username' => $a->user['username'],
475 '$uid' => $_SESSION['uid'],
476 '$nickname' => $a->user['nickname'],
477 'dfrn_rawurl' => $_GET['dfrn_url']
482 elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
484 // we are the requestee and it is now safe to send our user their introduction,
485 // We could just unblock it, but first we have to jump through a few hoops to
486 // send an email, or even to find out if we need to send an email.
488 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
489 dbesc($_GET['confirm_key'])
494 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
495 WHERE `contact`.`id` = %d LIMIT 1",
496 intval($intro[0]['contact-id'])
499 $auto_confirm = false;
502 if($r[0]['page-flags'] != PAGE_NORMAL)
503 $auto_confirm = true;
504 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
505 $email_tpl = load_view_file('view/request_notify_eml.tpl');
506 $email = replace_macros($email_tpl, array(
507 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
508 '$url' => stripslashes($r[0]['url']),
509 '$myname' => $r[0]['username'],
510 '$siteurl' => $a->get_baseurl(),
511 '$sitename' => $a->config['sitename']
513 $res = mail($r[0]['email'],
514 t("Introduction received at ") . $a->config['sitename'],
516 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
517 // This is a redundant notification - no point throwing errors if it fails.
520 require_once('mod/dfrn_confirm.php');
522 'uid' => $r[0]['uid'],
523 'node' => $r[0]['nickname'],
524 'dfrn_id' => $r[0]['issued-id'],
525 'intro_id' => $intro[0]['id'],
526 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
528 dfrn_confirm_post($a,$handsfree);
533 if(! $auto_confirm) {
535 // If we are auto_confirming, this record will have already been nuked
536 // in dfrn_confirm_post()
538 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
539 dbesc($_GET['confirm_key'])
544 return; // NOTREACHED
547 $myaddr = ((x($_GET,'address')) ? urldecode($_GET['address']) : '');
548 // Normal web request. Display our user's introduction form.
549 if($a->profile['page-flags'] == PAGE_NORMAL)
550 $tpl = load_view_file('view/dfrn_request.tpl');
552 $tpl = load_view_file('view/auto_request.tpl');
553 $o .= replace_macros($tpl,array(
554 '$header' => t('Friend/Connection Request'),
555 '$pls_answer' => t('Please answer the following:'),
556 '$does_know' => t('Does $name know you?'),
559 '$add_note' => t('Add a personal note:'),
560 '$page_desc' => t('Please enter your profile address from one of the following supported social networks:'),
561 '$friendika' => t('Friendika'),
562 '$statusnet' => t('StatusNet/Federated Social Web'),
563 '$private_net' => t("Private \x28secure\x29 network"),
564 '$public_net' => t("Public \x28insecure\x29 network"),
565 '$your_address' => t('Your profile address:'),
566 '$submit' => t('Submit Request'),
567 '$cancel' => t('Cancel'),
568 '$nickname' => $a->argv[1],
569 '$name' => $a->profile['name'],
575 return; // Somebody is fishing.