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());
32 // callback to local site after remote request and local confirm
34 if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)
35 && (x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))
36 && ($_SESSION['uid'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
38 $dfrn_url = notags(trim($_POST['dfrn_url']));
39 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
40 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
43 require_once('Scrape.php');
47 $parms = scrape_dfrn($dfrn_url);
50 $_SESSION['sysmsg'] .= 'URL is not valid or does not contain profile information.' . EOL ;
55 $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no identifiable owner name.' . EOL ;
56 if(! x($parms,'photo'))
57 $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no profile photo.' . EOL ;
58 $invalid = validate_dfrn($parms);
60 echo $invalid . ' required DFRN parameter'
61 . (($invalid == 1) ? " was " : "s were " )
62 . "not found at the given URL" . '<br />';
70 $dfrn_request = $parms['dfrn-request'];
71 /////////////////////////
73 ////////////////////////
75 $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `photo`, `site-pubkey`,
76 `request`, `confirm`, `notify`, `poll`, `aes_allow`)
77 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d)",
78 intval($_SESSION['uid']),
84 $parms['dfrn-request'],
85 $parms['dfrn-confirm'],
86 $parms['dfrn-notify'],
91 $_SESSION['sysmsg'] .= "Failed to create contact." . EOL;
93 $_SESSION['sysmsg'] .= "Introduction complete.";
95 // Allow the blocked remote notification to complete
97 if(strlen($dfrn_request) && strlen($confirm_key))
98 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
105 // we are operating as a remote site and an introduction was requested of us.
106 // Scrape the originating DFRN-URL for everything we need. Create a contact record
107 // and an introduction to show our user next time he/she logs in.
108 // Finally redirect back to the originator so that their site can record the request.
109 // If our user confirms the request, a record of it will need to exist on the
110 // originator's site in order for the confirmation process to complete..
112 if($a->profile['nickname'])
113 $tailname = $a->profile['nickname'];
115 $tailname = $a->profile['uid'];
117 $uid = $a->profile['uid'];
121 require_once('Scrape.php');
123 if( x($_POST,'dfrn_url')) {
125 $url = trim($_POST['dfrn_url']);
127 $parms = scrape_dfrn($url);
129 if(! count($parms)) {
130 $_SESSION['sysmsg'] .= 'URL is not valid or does not contain profile information.' . EOL ;
135 $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no identifiable owner name.' . EOL ;
136 if(! x($parms,'photo'))
137 $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no profile photo.' . EOL ;
138 $invalid = validate_dfrn($parms);
140 echo $invalid . ' required DFRN parameter'
141 . (($invalid == 1) ? " was " : "s were " )
142 . "not found at the given URL" . '<br />';
149 $ret = q("SELECT `url` FROM `contact` WHERE `url` = '%s'", dbesc($url));
150 if($ret !== false && count($ret)) {
151 $_SESSION['sysmsg'] .= 'You have already introduced yourself here.' . EOL;
158 $parms['url'] = $url;
159 $parms['issued-id'] = random_string();
161 /////////////////////////
163 ////////////////////////
165 $ret = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
166 `request`, `confirm`, `notify`, `poll`, `visible` )
167 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
175 $parms['dfrn-request'],
176 $parms['dfrn-confirm'],
177 $parms['dfrn-notify'],
179 ((x($_POST,'visible')) ? 1 : 0 )
184 $_SESSION['sysmsg'] .= 'Failed to create contact record.' . EOL;
188 $ret = q("SELECT `id` FROM `contact`
189 WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s'
196 if(($ret !== NULL) && (count($ret)))
197 $contact_id = $ret[0]['id'];
199 $hash = random_string() . (string) time(); // Generate a confirm_key
202 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
203 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
206 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
207 dbesc(trim($_POST['dfrn-request-message'])),
209 dbesc(datetime_convert())
214 // TODO: send an email notification if our user wants one
217 $_SESSION['sysmsg'] .= "Your introduction has been sent." . EOL;
219 // "Homecoming" - send the requestor back to their site to record the introduction.
221 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
222 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
224 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
230 if(! function_exists('dfrn_request_content')) {
231 function dfrn_request_content(&$a) {
235 if(($a->argc != 2) || (! count($a->profile)))
238 $a->page['template'] = 'profile';
240 // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
241 // to send us to the post section to record the introduction.
243 if(x($_GET,'dfrn_url')) {
245 if(! x($_SESSION,'authenticated')) {
246 $_SESSION['sysmsg'] .= "Please login to confirm introduction." . EOL;
250 // Edge case, but can easily happen in the wild. This person is authenticated,
251 // but not as the person who needs to deal with this request.
253 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
254 $_SESSION['sysmsg'] .= "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL;
258 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
259 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
260 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
261 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
262 $o = replace_macros($o,array(
263 '$dfrn_url' => $dfrn_url,
264 '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
265 '$confirm_key' => $confirm_key,
266 '$username' => $a->user['username'],
267 '$uid' => $_SESSION['uid'],
268 'dfrn_rawurl' => $_GET['dfrn_url']
274 // safe to send our user their introduction
275 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
276 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
277 dbesc($_GET['confirm_key'])
283 // Outside request. Display our user's introduction form.
286 $o = file_get_contents("view/dfrn_request.tpl");
287 $o = replace_macros($o,array('$uid' => $a->profile['uid']));