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",
48 intval($_SESSION['uid']),
53 if(strlen($r[0]['dfrn-id'])) {
54 notice("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( 'Profile location is not valid or does not contain profile information.' . EOL );
80 notice( 'Warning: profile location has no identifiable owner name.' . EOL );
81 if(! x($parms,'photo'))
82 notice( 'Warning: profile location has no profile photo.' . EOL );
83 $invalid = validate_dfrn($parms);
85 notice( $invalid . ' required parameter'
86 . (($invalid == 1) ? " was " : "s were " )
87 . "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($_SESSION['uid']),
108 $parms['dfrn-request'],
109 $parms['dfrn-confirm'],
110 $parms['dfrn-notify'],
117 notice( "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( "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("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( "Invalid URL" . EOL );
182 // Is this an email-style DFRN locator?
184 if(strstr($url,'@')) {
185 $username = substr($url,0,strpos($url,'@'));
186 $hostname = substr($url,strpos($url,'@') + 1);
187 require_once('Scrape.php');
190 $parms = scrape_meta('https://' . $url);
191 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
192 $url = sprintf($parms['dfrn-template'],$username);
195 $parms = scrape_meta('http://' . $url);
196 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
197 $url = sprintf($parms['dfrn-template'],$username);
207 notice("Unable to resolve your name at the provided location." . EOL);
211 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
217 if(strlen($ret[0]['issued-id'])) {
218 notice( 'You have already introduced yourself here.' . EOL );
222 $contact_record = $ret[0];
223 $parms = array('dfrn-request' => $ret[0]['request']);
226 $issued_id = random_string();
228 if(is_array($contact_record)) {
229 // There is a contact record but no issued-id, so this
230 // is a reciprocal introduction from a known contact
231 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
233 intval($contact_record['id'])
238 require_once('Scrape.php');
240 $parms = scrape_dfrn($url);
242 if(! count($parms)) {
243 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
248 notice( 'Warning: profile location has no identifiable owner name.' . EOL );
249 if(! x($parms,'photo'))
250 notice( 'Warning: profile location has no profile photo.' . EOL );
251 $invalid = validate_dfrn($parms);
253 notice( $invalid . ' required parameter'
254 . (($invalid == 1) ? " was " : "s were " )
255 . "not found at the given location." . EOL ) ;
262 $parms['url'] = $url;
263 $parms['issued-id'] = $issued_id;
267 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
268 `request`, `confirm`, `notify`, `poll` )
269 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
277 $parms['dfrn-request'],
278 $parms['dfrn-confirm'],
279 $parms['dfrn-notify'],
283 // find the contact record we just created
285 $r = q("SELECT `id` FROM `contact`
286 WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
292 $contact_record = $r[0];
297 notice( 'Failed to update contact record.' . EOL );
301 $hash = random_string() . (string) time(); // Generate a confirm_key
303 if(is_array($contact_record)) {
304 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
305 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
307 intval($contact_record['id']),
308 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
309 dbesc(trim($_POST['dfrn-request-message'])),
311 dbesc(datetime_convert())
316 // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
319 notice( "Your introduction has been sent." . EOL );
321 // "Homecoming" - send the requestor back to their site to record the introduction.
323 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$nickname");
324 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
326 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
327 return; // NOTREACHED
336 if(! function_exists('dfrn_request_content')) {
337 function dfrn_request_content(&$a) {
341 if(($a->argc != 2) || (! count($a->profile)))
344 $a->page['template'] = 'profile';
346 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
347 // to send us to the post section to record the introduction.
349 if(x($_GET,'dfrn_url')) {
352 notice( "Please login to confirm introduction." . EOL );
356 // Edge case, but can easily happen in the wild. This person is authenticated,
357 // but not as the person who needs to deal with this request.
359 if ($a->user['nickname'] != $a->argv[1]) {
360 notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
364 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
365 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
366 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
367 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
368 $o = replace_macros($o,array(
369 '$dfrn_url' => $dfrn_url,
370 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
371 '$confirm_key' => $confirm_key,
372 '$username' => $a->user['username'],
373 '$uid' => $_SESSION['uid'],
374 '$nickname' => $a->user['nickname'],
375 'dfrn_rawurl' => $_GET['dfrn_url']
380 elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
382 // we are the requestee and it is now safe to send our user their introduction,
383 // We could just unblock it, but first we have to jump through a few hoops to
384 // send an email, or even to find out if we need to send an email.
386 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
387 dbesc($_GET['confirm_key'])
392 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
393 WHERE `contact`.`id` = %d LIMIT 1",
394 intval($intro[0]['contact-id'])
398 if($r[0]['notify-flags'] & NOTIFY_INTRO) {
399 $email_tpl = file_get_contents('view/request_notify_eml.tpl');
400 $email = replace_macros($email_tpl, array(
401 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : '[Name Withheld]'),
402 '$url' => stripslashes($r[0]['url']),
403 '$myname' => $r[0]['username'],
404 '$siteurl' => $a->get_baseurl(),
405 '$sitename' => $a->config['sitename']
407 $res = mail($r[0]['email'],"Introduction received at {$a->config['sitename']}",$email,"From: Administrator@{$_SERVER[SERVER_NAME]}");
408 // This is a redundant notification - no point throwing errors if it fails.
412 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
413 dbesc($_GET['confirm_key'])
418 return; // NOTREACHED
422 // Normal web request. Display our user's introduction form.
424 $o = file_get_contents("view/dfrn_request.tpl");
425 $o = replace_macros($o,array('$nickname' => $a->argv[1]));
428 return; // Somebody is fishing.