3 if(! function_exists('dfrn_request_init')) {
4 function dfrn_request_init(&$a) {
9 require_once('mod/profile.php');
10 profile_init($a,$which);
16 if(! function_exists('dfrn_request_post')) {
17 function dfrn_request_post(&$a) {
19 if(($a->argc != 2) || (! count($a->profile)))
23 if($_POST['cancel']) {
24 goaway($a->get_baseurl());
28 // We've introduced ourself to another cell, then have been returned to our own cell
29 // to confirm the request, and then we've clicked submit (perhaps after logging in).
30 // That brings us here:
32 if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
34 // Ensure this is a valid request
36 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
39 $dfrn_url = notags(trim($_POST['dfrn_url']));
40 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
41 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
43 $contact_record = null;
47 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
53 if(strlen($r[0]['dfrn-id'])) {
54 notice( t("This introduction has already been accepted.") . EOL );
58 $contact_record = $r[0];
61 if(is_array($contact_record)) {
62 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
64 intval($contact_record['id'])
69 require_once('Scrape.php');
72 $parms = scrape_dfrn($dfrn_url);
75 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
80 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
81 if(! x($parms,'photo'))
82 notice( t('Warning: profile location has no profile photo.') . EOL );
83 $invalid = validate_dfrn($parms);
85 notice( $invalid . t(' required parameter')
86 . (($invalid == 1) ? t(" was ") : t("s were ") )
87 . t("not found at the given location.") . EOL ) ;
94 $dfrn_request = $parms['dfrn-request'];
99 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `nick`, `photo`, `site-pubkey`,
100 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
101 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
102 intval(local_user()),
109 $parms['dfrn-request'],
110 $parms['dfrn-confirm'],
111 $parms['dfrn-notify'],
118 notice( t("Introduction complete.") . EOL);
121 // Allow the blocked remote notification to complete
123 if(is_array($contact_record))
124 $dfrn_request = $contact_record['request'];
126 if(strlen($dfrn_request) && strlen($confirm_key))
127 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
130 return; // NOTREACHED
136 // invalid/bogus request
138 notice( t('Unrecoverable protocol error.') . EOL );
139 goaway($a->get_baseurl());
140 return; // NOTREACHED
145 // We are the requestee. A person from a remote cell has made an introduction
146 // on our profile web page and clicked submit. We will use their DFRN-URL to
147 // figure out how to contact their cell.
149 // Scrape the originating DFRN-URL for everything we need. Create a contact record
150 // and an introduction to show our user next time he/she logs in.
151 // Finally redirect back to the requestor so that their site can record the request.
152 // If our user (the requestee) later confirms this request, a record of it will need
153 // to exist on the requestor's cell in order for the confirmation process to complete..
155 // It's possible that neither the requestor or the requestee are logged in at the moment,
156 // and the requestor does not yet have any credentials to the requestee profile.
158 // Who is the requestee? We've already loaded their profile which means their nickname should be
159 // in $a->argv[1] and we should have their complete info in $a->profile.
161 if(! (is_array($a->profile) && count($a->profile))) {
162 notice( t('Profile unavailable.') . EOL);
166 $nickname = $a->profile['nickname'];
167 $notify_flags = $a->profile['notify-flags'];
168 $uid = $a->profile['uid'];
170 $contact_record = null;
175 if( x($_POST,'dfrn_url')) {
177 $url = trim($_POST['dfrn_url']);
179 notice( t("Invalid locator") . EOL );
183 // Canonicalise email-style profile locator
185 $url = webfinger_dfrn($url);
187 if(substr($url,0,5) === 'stat:') {
189 $url = substr($url,5);
196 notice( t("Unable to resolve your name at the provided location.") . EOL);
201 if($network === 'dfrn') {
202 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1",
208 if(strlen($ret[0]['issued-id'])) {
209 notice( t('You have already introduced yourself here.') . EOL );
212 elseif($ret[0]['rel'] == REL_BUD) {
213 notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
217 $contact_record = $ret[0];
218 $parms = array('dfrn-request' => $ret[0]['request']);
222 $issued_id = random_string();
224 if(is_array($contact_record)) {
225 // There is a contact record but no issued-id, so this
226 // is a reciprocal introduction from a known contact
227 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
229 intval($contact_record['id'])
233 if(! validate_url($url)) {
234 notice( t('Invalid profile URL.') . EOL);
235 goaway($a->get_baseurl() . '/' . $a->cmd);
236 return; // NOTREACHED
239 if(! allowed_url($url)) {
240 notice( t('Disallowed profile URL.') . EOL);
241 goaway($a->get_baseurl() . '/' . $a->cmd);
242 return; // NOTREACHED
246 require_once('Scrape.php');
248 $parms = scrape_dfrn($url);
250 if(! count($parms)) {
251 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
252 goaway($a->get_baseurl() . '/' . $a->cmd);
256 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
257 if(! x($parms,'photo'))
258 notice( t('Warning: profile location has no profile photo.') . EOL );
259 $invalid = validate_dfrn($parms);
261 notice( $invalid . t(' required parameter')
262 . (($invalid == 1) ? t(" was ") : t("s were ") )
263 . t("not found at the given location.") . EOL ) ;
270 $parms['url'] = $url;
271 $parms['issued-id'] = $issued_id;
275 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
276 `request`, `confirm`, `notify`, `poll` )
277 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
286 $parms['dfrn-request'],
287 $parms['dfrn-confirm'],
288 $parms['dfrn-notify'],
292 // find the contact record we just created
294 $r = q("SELECT `id` FROM `contact`
295 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
301 $contact_record = $r[0];
306 notice( t('Failed to update contact record.') . EOL );
310 $hash = random_string() . (string) time(); // Generate a confirm_key
312 if(is_array($contact_record)) {
313 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
314 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
316 intval($contact_record['id']),
317 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
318 dbesc(notags(trim($_POST['dfrn-request-message']))),
320 dbesc(datetime_convert())
324 // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
327 notice( t('Your introduction has been sent.') . EOL );
329 // "Homecoming" - send the requestor back to their site to record the introduction.
331 $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
332 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
334 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
335 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
336 . '&confirm_key=' . $hash
337 . (($aes_allow) ? "&aes_allow=1" : "")
340 // END $network === 'dfrn'
342 elseif($network === 'stat') {
347 * Check contact existence
348 * Try and scrape together enough information to create a contact record, with us as REL_VIP
349 * Substitute our user's feed URL into $url template
350 * Send the subscriber home to subscribe
354 $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
357 // END $network === 'stat'
366 if(! function_exists('dfrn_request_content')) {
367 function dfrn_request_content(&$a) {
371 if(($a->argc != 2) || (! count($a->profile)))
374 $a->page['template'] = 'profile';
376 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
377 // to send us to the post section to record the introduction.
379 if(x($_GET,'dfrn_url')) {
382 notice( t("Please login to confirm introduction.") . EOL );
386 // Edge case, but can easily happen in the wild. This person is authenticated,
387 // but not as the person who needs to deal with this request.
389 if ($a->user['nickname'] != $a->argv[1]) {
390 notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
394 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
395 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
396 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
397 $o .= load_view_file("view/dfrn_req_confirm.tpl");
398 $o = replace_macros($o,array(
399 '$dfrn_url' => $dfrn_url,
400 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
401 '$confirm_key' => $confirm_key,
402 '$username' => $a->user['username'],
403 '$uid' => $_SESSION['uid'],
404 '$nickname' => $a->user['nickname'],
405 'dfrn_rawurl' => $_GET['dfrn_url']
410 elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
412 // we are the requestee and it is now safe to send our user their introduction,
413 // We could just unblock it, but first we have to jump through a few hoops to
414 // send an email, or even to find out if we need to send an email.
416 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
417 dbesc($_GET['confirm_key'])
422 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
423 WHERE `contact`.`id` = %d LIMIT 1",
424 intval($intro[0]['contact-id'])
427 $auto_confirm = false;
430 if($r[0]['page-flags'] != PAGE_NORMAL)
431 $auto_confirm = true;
432 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
433 $email_tpl = load_view_file('view/request_notify_eml.tpl');
434 $email = replace_macros($email_tpl, array(
435 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
436 '$url' => stripslashes($r[0]['url']),
437 '$myname' => $r[0]['username'],
438 '$siteurl' => $a->get_baseurl(),
439 '$sitename' => $a->config['sitename']
441 $res = mail($r[0]['email'],
442 t("Introduction received at ") . $a->config['sitename'],
444 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
445 // This is a redundant notification - no point throwing errors if it fails.
448 require_once('mod/dfrn_confirm.php');
450 'uid' => $r[0]['uid'],
451 'node' => $r[0]['nickname'],
452 'dfrn_id' => $r[0]['issued-id'],
453 'intro_id' => $intro[0]['id'],
454 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
456 dfrn_confirm_post($a,$handsfree);
461 if(! $auto_confirm) {
463 // If we are auto_confirming, this record will have already been nuked
464 // in dfrn_confirm_post()
466 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
467 dbesc($_GET['confirm_key'])
472 return; // NOTREACHED
475 $myaddr = ((x($_GET,'address')) ? urldecode($_GET['address']) : '');
476 // Normal web request. Display our user's introduction form.
477 if($a->profile['page-flags'] == PAGE_NORMAL)
478 $tpl = load_view_file('view/dfrn_request.tpl');
480 $tpl = load_view_file('view/auto_request.tpl');
481 $o .= replace_macros($tpl,array(
482 '$nickname' => $a->argv[1],
483 '$name' => $a->profile['name'],
489 return; // Somebody is fishing.