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.
43 $dfrn_url = notags(trim($_POST['dfrn_url']));
44 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
45 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
47 $contact_record = null;
51 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `dfrn-url` = '%s' LIMIT 1",
52 intval($_SESSION['uid']),
57 if(strlen($r[0]['dfrn-id'])) {
58 notice("This introduction has already been accepted." . EOL );
62 $contact_record = $r[0];
65 if(is_array($contact_record)) {
66 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
68 intval($contact_record['id'])
73 require_once('Scrape.php');
76 $parms = scrape_dfrn($dfrn_url);
79 notice( 'URL is not valid or does not contain profile information.' . EOL );
84 notice( 'Warning: DFRN profile has no identifiable owner name.' . EOL );
85 if(! x($parms,'photo'))
86 notice( 'Warning: DFRN profile has no profile photo.' . EOL );
87 $invalid = validate_dfrn($parms);
89 notice( $invalid . ' required DFRN parameter'
90 . (($invalid == 1) ? " was " : "s were " )
91 . "not found at the given URL" . EOL );
98 $dfrn_request = $parms['dfrn-request'];
103 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `photo`, `site-pubkey`,
104 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
105 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d)",
106 intval($_SESSION['uid']),
112 $parms['dfrn-request'],
113 $parms['dfrn-confirm'],
114 $parms['dfrn-notify'],
121 notice( "Introduction complete." . EOL);
124 // Allow the blocked remote notification to complete
126 if(is_array($contact_record))
127 $dfrn_request = $contact_record['request'];
129 if(strlen($dfrn_request) && strlen($confirm_key))
130 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
137 notice( "Unrecoverable protocol error." . EOL );
138 goaway($a->get_baseurl());
142 // we are operating as a remote site and an introduction was requested of us.
143 // Scrape the originating DFRN-URL for everything we need. Create a contact record
144 // and an introduction to show our user next time he/she logs in.
145 // Finally redirect back to the originator so that their site can record the request.
146 // If our user confirms the request, a record of it will need to exist on the
147 // originator's site in order for the confirmation process to complete..
149 if($a->profile['nickname'])
150 $tailname = $a->profile['nickname'];
152 $tailname = $a->profile['uid'];
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');
174 $parms = scrape_meta('http://' . $url);
175 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s'))
176 $url = sprintf($parms['dfrn-template'],$username);
179 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
185 if(strlen($ret[0]['issued-id'])) {
186 notice( 'You have already introduced yourself here.' . EOL );
190 $contact_record = $ret[0];
191 $parms = array('dfrn-request' => $ret[0]['request']);
194 $issued_id = random_string();
196 if(is_array($contact_record)) {
197 // There is a contact record but no issued-id, so this
198 // is a reciprocal introduction from a known contact
199 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
201 intval($contact_record['id'])
206 require_once('Scrape.php');
208 $parms = scrape_dfrn($url);
210 if(! count($parms)) {
211 notice( 'URL is not valid or does not contain profile information.' . EOL );
216 notice( 'Warning: DFRN profile has no identifiable owner name.' . EOL );
217 if(! x($parms,'photo'))
218 notice( 'Warning: DFRN profile has no profile photo.' . EOL );
219 $invalid = validate_dfrn($parms);
221 notice( $invalid . ' required DFRN parameter'
222 . (($invalid == 1) ? " was " : "s were " )
223 . "not found at the given URL" . EOL) ;
230 $parms['url'] = $url;
231 $parms['issued-id'] = $issued_id;
235 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
236 `request`, `confirm`, `notify`, `poll` )
237 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
245 $parms['dfrn-request'],
246 $parms['dfrn-confirm'],
247 $parms['dfrn-notify'],
251 // find the contact record we just created
253 $r = q("SELECT `id` FROM `contact`
254 WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
260 $contact_record = $r[0];
265 notice( 'Failed to update contact record.' . EOL );
269 $hash = random_string() . (string) time(); // Generate a confirm_key
271 if(is_array($contact_record)) {
272 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
273 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
275 intval($contact_record['id']),
276 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
277 dbesc(trim($_POST['dfrn-request-message'])),
279 dbesc(datetime_convert())
283 // TODO: send an email notification if our user wants one
286 notice( "Your introduction has been sent." . EOL );
288 // "Homecoming" - send the requestor back to their site to record the introduction.
290 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
291 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
293 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
300 if(! function_exists('dfrn_request_content')) {
301 function dfrn_request_content(&$a) {
305 if(($a->argc != 2) || (! count($a->profile)))
308 $a->page['template'] = 'profile';
310 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
311 // to send us to the post section to record the introduction.
313 if(x($_GET,'dfrn_url')) {
316 notice( "Please login to confirm introduction." . EOL );
320 // Edge case, but can easily happen in the wild. This person is authenticated,
321 // but not as the person who needs to deal with this request.
323 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
324 notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
328 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
329 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
330 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
331 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
332 $o = replace_macros($o,array(
333 '$dfrn_url' => $dfrn_url,
334 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
335 '$confirm_key' => $confirm_key,
336 '$username' => $a->user['username'],
337 '$uid' => $_SESSION['uid'],
338 'dfrn_rawurl' => $_GET['dfrn_url']
344 // we are the requestee and it is now safe to send our user their introduction
345 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
346 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
347 dbesc($_GET['confirm_key'])
353 // Outside request. Display our user's introduction form.
356 $o = file_get_contents("view/dfrn_request.tpl");
357 $o = replace_macros($o,array('$uid' => $a->profile['uid']));