3 if(! function_exists('dfrn_request_init')) {
4 function dfrn_request_init(&$a) {
6 if($_SESSION['authenticated']) {
7 // choose which page to show (could be remote auth)
14 require_once('mod/profile.php');
15 profile_init($a,$which);
21 if(! function_exists('dfrn_request_post')) {
22 function dfrn_request_post(&$a) {
24 if(($a->argc != 2) || (! count($a->profile)))
28 if($_POST['cancel']) {
29 goaway($a->get_baseurl());
33 // callback to local site after remote request and local confirm
35 if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)
36 && local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
38 // We are the requestor, and we've been sent back to our own site
39 // to confirm the request. We've done so and clicked submit,
40 // which brings us here.
42 $dfrn_url = notags(trim($_POST['dfrn_url']));
43 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
44 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
46 $contact_record = null;
50 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
51 intval($_SESSION['uid']),
56 if(strlen($r[0]['dfrn-id'])) {
57 notice("This introduction has already been accepted." . EOL );
61 $contact_record = $r[0];
64 if(is_array($contact_record)) {
65 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
67 intval($contact_record['id'])
72 require_once('Scrape.php');
75 $parms = scrape_dfrn($dfrn_url);
78 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
83 notice( 'Warning: profile location has no identifiable owner name.' . EOL );
84 if(! x($parms,'photo'))
85 notice( 'Warning: profile location has no profile photo.' . EOL );
86 $invalid = validate_dfrn($parms);
88 notice( $invalid . ' required parameter'
89 . (($invalid == 1) ? " was " : "s were " )
90 . "not found at the given location." . EOL ) ;
97 $dfrn_request = $parms['dfrn-request'];
102 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `photo`, `site-pubkey`,
103 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
104 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d)",
105 intval($_SESSION['uid']),
111 $parms['dfrn-request'],
112 $parms['dfrn-confirm'],
113 $parms['dfrn-notify'],
120 notice( "Introduction complete." . EOL);
123 // Allow the blocked remote notification to complete
125 if(is_array($contact_record))
126 $dfrn_request = $contact_record['request'];
128 if(strlen($dfrn_request) && strlen($confirm_key))
129 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
136 notice( "Unrecoverable protocol error." . EOL );
137 goaway($a->get_baseurl());
140 if($_POST['localconfirm'])
144 // we are operating as a remote site and an introduction was requested of us.
145 // Scrape the originating DFRN-URL for everything we need. Create a contact record
146 // and an introduction to show our user next time he/she logs in.
147 // Finally redirect back to the originator so that their site can record the request.
148 // If our user confirms the request, a record of it will need to exist on the
149 // originator's site in order for the confirmation process to complete..
152 $tailname = $a->profile['nickname'];
154 $uid = $a->profile['uid'];
156 $contact_record = null;
161 if( x($_POST,'dfrn_url')) {
163 $url = trim($_POST['dfrn_url']);
165 notice( "Invalid URL" . EOL );
169 if(strstr($url,'@')) {
170 $username = substr($url,0,strpos($url,'@'));
171 $hostname = substr($url,strpos($url,'@') + 1);
172 require_once('Scrape.php');
175 $parms = scrape_meta('https://' . $url);
176 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
177 $url = sprintf($parms['dfrn-template'],$username);
180 $parms = scrape_meta('http://' . $url);
181 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
182 $url = sprintf($parms['dfrn-template'],$username);
192 notice("Unable to resolve your name at the provided location." . EOL);
196 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
202 if(strlen($ret[0]['issued-id'])) {
203 notice( 'You have already introduced yourself here.' . EOL );
207 $contact_record = $ret[0];
208 $parms = array('dfrn-request' => $ret[0]['request']);
211 $issued_id = random_string();
213 if(is_array($contact_record)) {
214 // There is a contact record but no issued-id, so this
215 // is a reciprocal introduction from a known contact
216 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
218 intval($contact_record['id'])
223 require_once('Scrape.php');
225 $parms = scrape_dfrn($url);
227 if(! count($parms)) {
228 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
233 notice( 'Warning: profile location has no identifiable owner name.' . EOL );
234 if(! x($parms,'photo'))
235 notice( 'Warning: profile location has no profile photo.' . EOL );
236 $invalid = validate_dfrn($parms);
238 notice( $invalid . ' required parameter'
239 . (($invalid == 1) ? " was " : "s were " )
240 . "not found at the given location." . EOL ) ;
247 $parms['url'] = $url;
248 $parms['issued-id'] = $issued_id;
252 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
253 `request`, `confirm`, `notify`, `poll` )
254 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
262 $parms['dfrn-request'],
263 $parms['dfrn-confirm'],
264 $parms['dfrn-notify'],
268 // find the contact record we just created
270 $r = q("SELECT `id` FROM `contact`
271 WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
277 $contact_record = $r[0];
282 notice( 'Failed to update contact record.' . EOL );
286 $hash = random_string() . (string) time(); // Generate a confirm_key
288 if(is_array($contact_record)) {
289 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
290 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
292 intval($contact_record['id']),
293 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
294 dbesc(trim($_POST['dfrn-request-message'])),
296 dbesc(datetime_convert())
300 // TODO: send an email notification if our user wants one
303 notice( "Your introduction has been sent." . EOL );
305 // "Homecoming" - send the requestor back to their site to record the introduction.
307 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
308 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
310 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
317 if(! function_exists('dfrn_request_content')) {
318 function dfrn_request_content(&$a) {
322 if(($a->argc != 2) || (! count($a->profile)))
325 $a->page['template'] = 'profile';
327 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
328 // to send us to the post section to record the introduction.
330 if(x($_GET,'dfrn_url')) {
333 notice( "Please login to confirm introduction." . EOL );
337 // Edge case, but can easily happen in the wild. This person is authenticated,
338 // but not as the person who needs to deal with this request.
340 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
341 notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
345 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
346 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
347 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
348 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
349 $o = replace_macros($o,array(
350 '$dfrn_url' => $dfrn_url,
351 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
352 '$confirm_key' => $confirm_key,
353 '$username' => $a->user['username'],
354 '$uid' => $_SESSION['uid'],
355 '$nickname' => $a->user['nickname'],
356 'dfrn_rawurl' => $_GET['dfrn_url']
362 // we are the requestee and it is now safe to send our user their introduction
363 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
364 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
365 dbesc($_GET['confirm_key'])
371 // Outside request. Display our user's introduction form.
374 $o = file_get_contents("view/dfrn_request.tpl");
375 $o = replace_macros($o,array('$uid' => $a->argv[1]));