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' 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`, `photo`, `site-pubkey`,
100 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
101 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d)",
102 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);
195 notice( t("Unable to resolve your name at the provided location.") . EOL);
200 if($network === 'dfrn') {
201 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
207 if(strlen($ret[0]['issued-id'])) {
208 notice( t('You have already introduced yourself here.') . EOL );
211 elseif($ret[0]['rel'] == REL_BUD) {
212 notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
216 $contact_record = $ret[0];
217 $parms = array('dfrn-request' => $ret[0]['request']);
221 $issued_id = random_string();
223 if(is_array($contact_record)) {
224 // There is a contact record but no issued-id, so this
225 // is a reciprocal introduction from a known contact
226 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
228 intval($contact_record['id'])
232 if(! validate_url($url)) {
233 notice( t('Invalid profile URL.') . EOL);
234 goaway($a->get_baseurl() . '/' . $a->cmd);
235 return; // NOTREACHED
238 if(! allowed_url($url)) {
239 notice( t('Disallowed profile URL.') . EOL);
240 goaway($a->get_baseurl() . '/' . $a->cmd);
241 return; // NOTREACHED
245 require_once('Scrape.php');
247 $parms = scrape_dfrn($url);
249 if(! count($parms)) {
250 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
251 goaway($a->get_baseurl() . '/' . $a->cmd);
255 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
256 if(! x($parms,'photo'))
257 notice( t('Warning: profile location has no profile photo.') . EOL );
258 $invalid = validate_dfrn($parms);
260 notice( $invalid . t(' required parameter')
261 . (($invalid == 1) ? t(" was ") : t("s were ") )
262 . t("not found at the given location.") . EOL ) ;
269 $parms['url'] = $url;
270 $parms['issued-id'] = $issued_id;
274 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
275 `request`, `confirm`, `notify`, `poll` )
276 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
284 $parms['dfrn-request'],
285 $parms['dfrn-confirm'],
286 $parms['dfrn-notify'],
290 // find the contact record we just created
292 $r = q("SELECT `id` FROM `contact`
293 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
299 $contact_record = $r[0];
304 notice( t('Failed to update contact record.') . EOL );
308 $hash = random_string() . (string) time(); // Generate a confirm_key
310 if(is_array($contact_record)) {
311 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
312 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
314 intval($contact_record['id']),
315 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
316 dbesc(notags(trim($_POST['dfrn-request-message']))),
318 dbesc(datetime_convert())
322 // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
325 notice( t('Your introduction has been sent.') . EOL );
327 // "Homecoming" - send the requestor back to their site to record the introduction.
329 $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
330 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
332 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url"
333 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
334 . '&confirm_key=' . $hash
335 . (($aes_allow) ? "&aes_allow=1" : "")
338 // END $network === 'dfrn'
340 elseif($network === 'stat') {
345 * Check contact existence
346 * Try and scrape together enough information to create a contact record, with us as REL_VIP
347 * Substitute our user's feed URL into $url template
348 * Send the subscriber home to subscribe
352 $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
355 // END $network === 'stat'
364 if(! function_exists('dfrn_request_content')) {
365 function dfrn_request_content(&$a) {
369 if(($a->argc != 2) || (! count($a->profile)))
372 $a->page['template'] = 'profile';
374 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
375 // to send us to the post section to record the introduction.
377 if(x($_GET,'dfrn_url')) {
380 notice( t("Please login to confirm introduction.") . EOL );
384 // Edge case, but can easily happen in the wild. This person is authenticated,
385 // but not as the person who needs to deal with this request.
387 if ($a->user['nickname'] != $a->argv[1]) {
388 notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
392 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
393 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
394 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
395 $o .= load_view_file("view/dfrn_req_confirm.tpl");
396 $o = replace_macros($o,array(
397 '$dfrn_url' => $dfrn_url,
398 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
399 '$confirm_key' => $confirm_key,
400 '$username' => $a->user['username'],
401 '$uid' => $_SESSION['uid'],
402 '$nickname' => $a->user['nickname'],
403 'dfrn_rawurl' => $_GET['dfrn_url']
408 elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
410 // we are the requestee and it is now safe to send our user their introduction,
411 // We could just unblock it, but first we have to jump through a few hoops to
412 // send an email, or even to find out if we need to send an email.
414 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
415 dbesc($_GET['confirm_key'])
420 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
421 WHERE `contact`.`id` = %d LIMIT 1",
422 intval($intro[0]['contact-id'])
425 $auto_confirm = false;
428 if($r[0]['page-flags'] != PAGE_NORMAL)
429 $auto_confirm = true;
430 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
431 $email_tpl = load_view_file('view/request_notify_eml.tpl');
432 $email = replace_macros($email_tpl, array(
433 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
434 '$url' => stripslashes($r[0]['url']),
435 '$myname' => $r[0]['username'],
436 '$siteurl' => $a->get_baseurl(),
437 '$sitename' => $a->config['sitename']
439 $res = mail($r[0]['email'],
440 t("Introduction received at ") . $a->config['sitename'],
442 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
443 // This is a redundant notification - no point throwing errors if it fails.
446 require_once('mod/dfrn_confirm.php');
448 'uid' => $r[0]['uid'],
449 'node' => $r[0]['nickname'],
450 'dfrn_id' => $r[0]['issued-id'],
451 'intro_id' => $intro[0]['id'],
452 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
454 dfrn_confirm_post($a,$handsfree);
459 if(! $auto_confirm) {
461 // If we are auto_confirming, this record will have already been nuked
462 // in dfrn_confirm_post()
464 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
465 dbesc($_GET['confirm_key'])
470 return; // NOTREACHED
474 // Normal web request. Display our user's introduction form.
476 $o = load_view_file("view/dfrn_request.tpl");
477 $o = replace_macros($o,array('$nickname' => $a->argv[1]));
481 return; // Somebody is fishing.