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() && ($_SESSION['uid'] == $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());
141 // we are operating as a remote site and an introduction was requested of us.
142 // Scrape the originating DFRN-URL for everything we need. Create a contact record
143 // and an introduction to show our user next time he/she logs in.
144 // Finally redirect back to the originator so that their site can record the request.
145 // If our user confirms the request, a record of it will need to exist on the
146 // originator's site in order for the confirmation process to complete..
149 $tailname = $a->profile['nickname'];
151 $uid = $a->profile['uid'];
153 $contact_record = null;
158 if( x($_POST,'dfrn_url')) {
160 $url = trim($_POST['dfrn_url']);
162 notice( "Invalid URL" . EOL );
166 if(strstr($url,'@')) {
167 $username = substr($url,0,strpos($url,'@'));
168 $hostname = substr($url,strpos($url,'@') + 1);
169 require_once('Scrape.php');
172 $parms = scrape_meta('https://' . $url);
173 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
174 $url = sprintf($parms['dfrn-template'],$username);
177 $parms = scrape_meta('http://' . $url);
178 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
179 $url = sprintf($parms['dfrn-template'],$username);
189 notice("Unable to resolve your name at the provided location." . EOL);
193 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
199 if(strlen($ret[0]['issued-id'])) {
200 notice( 'You have already introduced yourself here.' . EOL );
204 $contact_record = $ret[0];
205 $parms = array('dfrn-request' => $ret[0]['request']);
208 $issued_id = random_string();
210 if(is_array($contact_record)) {
211 // There is a contact record but no issued-id, so this
212 // is a reciprocal introduction from a known contact
213 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
215 intval($contact_record['id'])
220 require_once('Scrape.php');
222 $parms = scrape_dfrn($url);
224 if(! count($parms)) {
225 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
230 notice( 'Warning: profile location has no identifiable owner name.' . EOL );
231 if(! x($parms,'photo'))
232 notice( 'Warning: profile location has no profile photo.' . EOL );
233 $invalid = validate_dfrn($parms);
235 notice( $invalid . ' required parameter'
236 . (($invalid == 1) ? " was " : "s were " )
237 . "not found at the given location." . EOL ) ;
244 $parms['url'] = $url;
245 $parms['issued-id'] = $issued_id;
249 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
250 `request`, `confirm`, `notify`, `poll` )
251 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
259 $parms['dfrn-request'],
260 $parms['dfrn-confirm'],
261 $parms['dfrn-notify'],
265 // find the contact record we just created
267 $r = q("SELECT `id` FROM `contact`
268 WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
274 $contact_record = $r[0];
279 notice( 'Failed to update contact record.' . EOL );
283 $hash = random_string() . (string) time(); // Generate a confirm_key
285 if(is_array($contact_record)) {
286 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
287 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
289 intval($contact_record['id']),
290 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
291 dbesc(trim($_POST['dfrn-request-message'])),
293 dbesc(datetime_convert())
297 // TODO: send an email notification if our user wants one
300 notice( "Your introduction has been sent." . EOL );
302 // "Homecoming" - send the requestor back to their site to record the introduction.
304 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
305 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
307 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
314 if(! function_exists('dfrn_request_content')) {
315 function dfrn_request_content(&$a) {
319 if(($a->argc != 2) || (! count($a->profile)))
322 $a->page['template'] = 'profile';
324 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
325 // to send us to the post section to record the introduction.
327 if(x($_GET,'dfrn_url')) {
330 notice( "Please login to confirm introduction." . EOL );
334 // Edge case, but can easily happen in the wild. This person is authenticated,
335 // but not as the person who needs to deal with this request.
337 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
338 notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
342 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
343 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
344 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
345 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
346 $o = replace_macros($o,array(
347 '$dfrn_url' => $dfrn_url,
348 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
349 '$confirm_key' => $confirm_key,
350 '$username' => $a->user['username'],
351 '$uid' => $_SESSION['uid'],
352 'dfrn_rawurl' => $_GET['dfrn_url']
358 // we are the requestee and it is now safe to send our user their introduction
359 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
360 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
361 dbesc($_GET['confirm_key'])
367 // Outside request. Display our user's introduction form.
370 $o = file_get_contents("view/dfrn_request.tpl");
371 $o = replace_macros($o,array('$uid' => $a->argv[2]));