3 if(! function_exists('dfrn_request_init')) {
4 function dfrn_request_init(&$a) {
9 profile_load($a,$which);
15 if(! function_exists('dfrn_request_post')) {
16 function dfrn_request_post(&$a) {
18 if(($a->argc != 2) || (! count($a->profile)))
22 if($_POST['cancel']) {
23 goaway($a->get_baseurl());
27 // We've introduced ourself to another cell, then have been returned to our own cell
28 // to confirm the request, and then we've clicked submit (perhaps after logging in).
29 // That brings us here:
31 if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
33 // Ensure this is a valid request
35 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
38 $dfrn_url = notags(trim($_POST['dfrn_url']));
39 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
40 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
42 $contact_record = null;
46 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
52 if(strlen($r[0]['dfrn-id'])) {
53 notice( t("This introduction has already been accepted.") . EOL );
57 $contact_record = $r[0];
60 if(is_array($contact_record)) {
61 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
63 intval($contact_record['id'])
68 require_once('Scrape.php');
71 $parms = scrape_dfrn($dfrn_url);
74 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
79 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
80 if(! x($parms,'photo'))
81 notice( t('Warning: profile location has no profile photo.') . EOL );
82 $invalid = validate_dfrn($parms);
84 notice( $invalid . t(' required parameter')
85 . (($invalid == 1) ? t(" was ") : t("s were ") )
86 . t("not found at the given location.") . EOL ) ;
93 $dfrn_request = $parms['dfrn-request'];
98 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `nick`, `photo`, `site-pubkey`,
99 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
100 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
101 intval(local_user()),
108 $parms['dfrn-request'],
109 $parms['dfrn-confirm'],
110 $parms['dfrn-notify'],
117 notice( t("Introduction complete.") . EOL);
120 // Allow the blocked remote notification to complete
122 if(is_array($contact_record))
123 $dfrn_request = $contact_record['request'];
125 if(strlen($dfrn_request) && strlen($confirm_key))
126 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
129 return; // NOTREACHED
135 // invalid/bogus request
137 notice( t('Unrecoverable protocol error.') . EOL );
138 goaway($a->get_baseurl());
139 return; // NOTREACHED
144 // We are the requestee. A person from a remote cell has made an introduction
145 // on our profile web page and clicked submit. We will use their DFRN-URL to
146 // figure out how to contact their cell.
148 // Scrape the originating DFRN-URL for everything we need. Create a contact record
149 // and an introduction to show our user next time he/she logs in.
150 // Finally redirect back to the requestor so that their site can record the request.
151 // If our user (the requestee) later confirms this request, a record of it will need
152 // to exist on the requestor's cell in order for the confirmation process to complete..
154 // It's possible that neither the requestor or the requestee are logged in at the moment,
155 // and the requestor does not yet have any credentials to the requestee profile.
157 // Who is the requestee? We've already loaded their profile which means their nickname should be
158 // in $a->argv[1] and we should have their complete info in $a->profile.
160 if(! (is_array($a->profile) && count($a->profile))) {
161 notice( t('Profile unavailable.') . EOL);
165 $nickname = $a->profile['nickname'];
166 $notify_flags = $a->profile['notify-flags'];
167 $uid = $a->profile['uid'];
169 $contact_record = null;
174 if( x($_POST,'dfrn_url')) {
176 $url = trim($_POST['dfrn_url']);
178 notice( t("Invalid locator") . EOL );
182 // Canonicalise email-style profile locator
184 $url = webfinger_dfrn($url);
186 if(substr($url,0,5) === 'stat:') {
188 $url = substr($url,5);
194 logger('dfrn_request: url: ' . $url);
197 notice( t("Unable to resolve your name at the provided location.") . EOL);
202 if($network === 'dfrn') {
203 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
209 if(strlen($ret[0]['issued-id'])) {
210 notice( t('You have already introduced yourself here.') . EOL );
213 elseif($ret[0]['rel'] == REL_BUD) {
214 notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
218 $contact_record = $ret[0];
219 $parms = array('dfrn-request' => $ret[0]['request']);
223 $issued_id = random_string();
225 if(is_array($contact_record)) {
226 // There is a contact record but no issued-id, so this
227 // is a reciprocal introduction from a known contact
228 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
230 intval($contact_record['id'])
234 if(! validate_url($url)) {
235 notice( t('Invalid profile URL.') . EOL);
236 goaway($a->get_baseurl() . '/' . $a->cmd);
237 return; // NOTREACHED
240 if(! allowed_url($url)) {
241 notice( t('Disallowed profile URL.') . EOL);
242 goaway($a->get_baseurl() . '/' . $a->cmd);
243 return; // NOTREACHED
247 require_once('Scrape.php');
249 $parms = scrape_dfrn($url);
251 if(! count($parms)) {
252 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
253 goaway($a->get_baseurl() . '/' . $a->cmd);
257 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
258 if(! x($parms,'photo'))
259 notice( t('Warning: profile location has no profile photo.') . EOL );
260 $invalid = validate_dfrn($parms);
262 notice( $invalid . t(' required parameter')
263 . (($invalid == 1) ? t(" was ") : t("s were ") )
264 . t("not found at the given location.") . EOL ) ;
271 $parms['url'] = $url;
272 $parms['issued-id'] = $issued_id;
276 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
277 `request`, `confirm`, `notify`, `poll` )
278 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
287 $parms['dfrn-request'],
288 $parms['dfrn-confirm'],
289 $parms['dfrn-notify'],
293 // find the contact record we just created
295 $r = q("SELECT `id` FROM `contact`
296 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
302 $contact_record = $r[0];
307 notice( t('Failed to update contact record.') . EOL );
311 $hash = random_string() . (string) time(); // Generate a confirm_key
313 if(is_array($contact_record)) {
314 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
315 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
317 intval($contact_record['id']),
318 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
319 dbesc(notags(trim($_POST['dfrn-request-message']))),
321 dbesc(datetime_convert())
325 // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
328 notice( t('Your introduction has been sent.') . EOL );
330 // "Homecoming" - send the requestor back to their site to record the introduction.
332 $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
333 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
335 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
336 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
337 . '&confirm_key=' . $hash
338 . (($aes_allow) ? "&aes_allow=1" : "")
341 // END $network === 'dfrn'
343 elseif($network === 'stat') {
348 * Check contact existence
349 * Try and scrape together enough information to create a contact record, with us as REL_VIP
350 * Substitute our user's feed URL into $url template
351 * Send the subscriber home to subscribe
355 $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
358 // END $network === 'stat'
367 if(! function_exists('dfrn_request_content')) {
368 function dfrn_request_content(&$a) {
372 if(($a->argc != 2) || (! count($a->profile)))
375 $a->page['template'] = 'profile';
377 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
378 // to send us to the post section to record the introduction.
380 if(x($_GET,'dfrn_url')) {
383 notice( t("Please login to confirm introduction.") . EOL );
387 // Edge case, but can easily happen in the wild. This person is authenticated,
388 // but not as the person who needs to deal with this request.
390 if ($a->user['nickname'] != $a->argv[1]) {
391 notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
395 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
396 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
397 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
398 $o .= load_view_file("view/dfrn_req_confirm.tpl");
399 $o = replace_macros($o,array(
400 '$dfrn_url' => $dfrn_url,
401 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
402 '$confirm_key' => $confirm_key,
403 '$username' => $a->user['username'],
404 '$uid' => $_SESSION['uid'],
405 '$nickname' => $a->user['nickname'],
406 'dfrn_rawurl' => $_GET['dfrn_url']
411 elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
413 // we are the requestee and it is now safe to send our user their introduction,
414 // We could just unblock it, but first we have to jump through a few hoops to
415 // send an email, or even to find out if we need to send an email.
417 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
418 dbesc($_GET['confirm_key'])
423 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
424 WHERE `contact`.`id` = %d LIMIT 1",
425 intval($intro[0]['contact-id'])
428 $auto_confirm = false;
431 if($r[0]['page-flags'] != PAGE_NORMAL)
432 $auto_confirm = true;
433 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
434 $email_tpl = load_view_file('view/request_notify_eml.tpl');
435 $email = replace_macros($email_tpl, array(
436 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
437 '$url' => stripslashes($r[0]['url']),
438 '$myname' => $r[0]['username'],
439 '$siteurl' => $a->get_baseurl(),
440 '$sitename' => $a->config['sitename']
442 $res = mail($r[0]['email'],
443 t("Introduction received at ") . $a->config['sitename'],
445 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
446 // This is a redundant notification - no point throwing errors if it fails.
449 require_once('mod/dfrn_confirm.php');
451 'uid' => $r[0]['uid'],
452 'node' => $r[0]['nickname'],
453 'dfrn_id' => $r[0]['issued-id'],
454 'intro_id' => $intro[0]['id'],
455 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
457 dfrn_confirm_post($a,$handsfree);
462 if(! $auto_confirm) {
464 // If we are auto_confirming, this record will have already been nuked
465 // in dfrn_confirm_post()
467 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
468 dbesc($_GET['confirm_key'])
473 return; // NOTREACHED
476 $myaddr = ((x($_GET,'address')) ? urldecode($_GET['address']) : '');
477 // Normal web request. Display our user's introduction form.
478 if($a->profile['page-flags'] == PAGE_NORMAL)
479 $tpl = load_view_file('view/dfrn_request.tpl');
481 $tpl = load_view_file('view/auto_request.tpl');
482 $o .= replace_macros($tpl,array(
483 '$nickname' => $a->argv[1],
484 '$name' => $a->profile['name'],
490 return; // Somebody is fishing.