]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
1458ba8e467dadfb5eb235d5590570fed93cb254
[friendica.git] / mod / dfrn_request.php
1 <?php
2
3 if(! function_exists('dfrn_request_init')) {
4 function dfrn_request_init(&$a) {
5
6         if($a->argc > 1)
7                 $which = $a->argv[1];
8
9         require_once('mod/profile.php');
10         profile_init($a,$which);
11
12         return;
13 }}
14
15
16 if(! function_exists('dfrn_request_post')) {
17 function dfrn_request_post(&$a) {
18
19         if(($a->argc != 2) || (! count($a->profile)))
20                 return;
21
22
23         if($_POST['cancel']) {
24                 goaway($a->get_baseurl());
25         } 
26
27
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:
31
32         if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1)) {
33
34                 // Ensure this is a valid request
35  
36                 if(local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
37
38
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'] : "");
42
43                         $contact_record = null;
44         
45                         if(x($dfrn_url)) {
46         
47                                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
48                                         intval(get_uid()),
49                                         dbesc($dfrn_url)
50                                 );
51         
52                                 if(count($r)) {
53                                         if(strlen($r[0]['dfrn-id'])) {
54                                                 notice( t("This introduction has already been accepted.") . EOL );
55                                                 return;
56                                         }
57                                         else
58                                                 $contact_record = $r[0];
59                                 }
60         
61                                 if(is_array($contact_record)) {
62                                         $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
63                                                 intval($aes_allow),
64                                                 intval($contact_record['id'])
65                                         );
66                                 }
67                                 else {
68         
69                                         require_once('Scrape.php');
70         
71         
72                                         $parms = scrape_dfrn($dfrn_url);
73         
74                                         if(! count($parms)) {
75                                                 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
76                                                 return;
77                                         }
78                                         else {
79                                                 if(! x($parms,'fn'))
80                                                         notice( t('Warning: profile location has no identifiable owner name.') . EOL );
81                                                 if(! x($parms,'photo'))
82                                                         notice( t('Warning: profile location has no profile photo.') . EOL );
83                                                 $invalid = validate_dfrn($parms);               
84                                                 if($invalid) {
85                                                         notice( $invalid . t(' required parameter') 
86                                                                 . (($invalid == 1) ? t(" was ") : t("s were ") )
87                                                                 . t("not found at the given location.") . EOL ) ;
88                                                         return;
89                                                 }
90                                         }
91
92
93
94                                         $dfrn_request = $parms['dfrn-request'];
95
96                                         dbesc_array($parms);
97
98
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(get_uid()),
103                                                 datetime_convert(),
104                                                 dbesc($dfrn_url),
105                                                 $parms['fn'],
106                                                 $parms['photo'],
107                                                 $parms['key'],
108                                                 $parms['dfrn-request'],
109                                                 $parms['dfrn-confirm'],
110                                                 $parms['dfrn-notify'],
111                                                 $parms['dfrn-poll'],
112                                                 intval($aes_allow)
113                                         );
114                                 }
115
116                                 if($r) {
117                                         notice( t("Introduction complete.") . EOL);
118                                 }
119
120                                 // Allow the blocked remote notification to complete
121
122                                 if(is_array($contact_record))
123                                         $dfrn_request = $contact_record['request'];
124
125                                 if(strlen($dfrn_request) && strlen($confirm_key))
126                                         $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
127                                         // ignore reply
128                                 goaway($dfrn_url);
129                                 return; // NOTREACHED
130
131                         }
132
133                 }
134
135                 // invalid/bogus request
136
137                 notice( t('Unrecoverable protocol error.') . EOL );
138                 goaway($a->get_baseurl());
139                 return; // NOTREACHED
140         }
141
142         // Otherwise:
143
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.  
147
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.. 
153
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.
156
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.
159
160         if(! (is_array($a->profile) && count($a->profile))) {
161                 notice( t('Profile unavailable.') . EOL);
162                 return;
163         }
164
165         $nickname = $a->profile['nickname'];
166         $notify_flags = $a->profile['notify-flags'];
167         $uid = $a->profile['uid'];
168
169         $contact_record = null;
170         $failed = false;
171         $parms = null;
172
173
174         if( x($_POST,'dfrn_url')) {
175
176                 $url = trim($_POST['dfrn_url']);
177                 if(! strlen($url)) {
178                         notice( t("Invalid locator") . EOL );
179                         return;
180                 }
181
182                 // Canonicalise email-style profile locator
183
184                 $url = webfinger($url);
185
186                 if(! strlen($url)) {
187                         notice( t("Unable to resolve your name at the provided location.") . EOL);                      
188                         return;
189                 }
190
191                 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1", 
192                         intval($uid),
193                         dbesc($url)
194                 );
195
196                 if(count($ret)) {
197                         if(strlen($ret[0]['issued-id'])) {
198                                 notice( t('You have already introduced yourself here.') . EOL );
199                                 return;
200                         }
201                         else {
202                                 $contact_record = $ret[0];
203                                 $parms = array('dfrn-request' => $ret[0]['request']);
204                         }
205                 }
206                 $issued_id = random_string();
207
208                 if(is_array($contact_record)) {
209                         // There is a contact record but no issued-id, so this
210                         // is a reciprocal introduction from a known contact
211                         $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
212                                 dbesc($issued_id),
213                                 intval($contact_record['id'])
214                         );
215                 }
216                 else {
217                         if(! validate_url($url)) {
218                                 notice( t('Invalid profile URL.') . EOL);
219                                 goaway($a->get_baseurl() . '/' . $a->cmd);
220                                 return; // NOTREACHED
221                         }
222
223                         if(! allowed_url($url)) {
224                                 notice( t('Disallowed profile URL.') . EOL);
225                                 goaway($a->get_baseurl() . '/' . $a->cmd);
226                                 return; // NOTREACHED
227                         }
228                         
229
230                         require_once('Scrape.php');
231
232                         $parms = scrape_dfrn($url);
233
234                         if(! count($parms)) {
235                                 notice( t('Profile location is not valid or does not contain profile information.') . EOL );
236                                 goaway($a->get_baseurl() . '/' . $a->cmd);
237                         }
238                         else {
239                                 if(! x($parms,'fn'))
240                                         notice( t('Warning: profile location has no identifiable owner name.') . EOL );
241                                 if(! x($parms,'photo'))
242                                         notice( t('Warning: profile location has no profile photo.') . EOL );
243                                 $invalid = validate_dfrn($parms);               
244                                 if($invalid) {
245                                         notice( $invalid . t(' required parameter') 
246                                                 . (($invalid == 1) ? t(" was ") : t("s were ") )
247                                                 . t("not found at the given location.") . EOL ) ;
248
249                                         return;
250                                 }
251                         }
252
253
254                         $parms['url'] = $url;
255                         $parms['issued-id'] = $issued_id;
256
257
258                         dbesc_array($parms);
259                         $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
260                                 `request`, `confirm`, `notify`, `poll` )
261                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
262                                 intval($uid),
263                                 datetime_convert(),
264                                 $parms['url'],
265                                 $parms['fn'],
266                                 $parms['issued-id'],
267                                 $parms['photo'],
268                                 $parms['key'],
269                                 $parms['dfrn-request'],
270                                 $parms['dfrn-confirm'],
271                                 $parms['dfrn-notify'],
272                                 $parms['dfrn-poll']
273                         );
274
275                         // find the contact record we just created
276                         if($r) {        
277                                 $r = q("SELECT `id` FROM `contact` 
278                                         WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
279                                         intval($uid),
280                                         $parms['url'],
281                                         $parms['issued-id']
282                                 );
283                                 if(count($r)) 
284                                         $contact_record = $r[0];
285                         }
286         
287                 }
288                 if($r === false) {
289                         notice( t('Failed to update contact record.') . EOL );
290                         return;
291                 }
292
293                 $hash = random_string() . (string) time();   // Generate a confirm_key
294
295                 if(is_array($contact_record)) {
296                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
297                                 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
298                                 intval($uid),
299                                 intval($contact_record['id']),
300                                 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
301                                 dbesc(notags(trim($_POST['dfrn-request-message']))),
302                                 dbesc($hash),
303                                 dbesc(datetime_convert())
304                         );
305                 }
306         
307
308                 // This notice will only be seen by the requestor if  the requestor and requestee are on the same server.
309
310                 if(! $failed) 
311                         notice( t('Your introduction has been sent.') . EOL );
312
313                 // "Homecoming" - send the requestor back to their site to record the introduction.
314
315                 $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
316                 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
317
318                 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
319                 return; // NOTREACHED
320
321         }
322         return;
323 }}
324
325
326
327
328 if(! function_exists('dfrn_request_content')) {
329 function dfrn_request_content(&$a) {
330
331         
332
333         if(($a->argc != 2) || (! count($a->profile)))
334                 return "";
335
336         $a->page['template'] = 'profile';
337
338         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
339         // to send us to the post section to record the introduction.
340
341         if(x($_GET,'dfrn_url')) {
342
343                 if(! local_user()) {
344                         notice( t("Please login to confirm introduction.") . EOL );
345                         return login();
346                 }
347
348                 // Edge case, but can easily happen in the wild. This person is authenticated, 
349                 // but not as the person who needs to deal with this request.
350
351                 if ($a->user['nickname'] != $a->argv[1]) {
352                         notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
353                         return login();
354                 }
355
356                 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
357                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
358                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
359                 $o .= load_view_file("view/dfrn_req_confirm.tpl");
360                 $o  = replace_macros($o,array(
361                         '$dfrn_url' => $dfrn_url,
362                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
363                         '$confirm_key' => $confirm_key,
364                         '$username' => $a->user['username'], 
365                         '$uid' => $_SESSION['uid'],
366                         '$nickname' => $a->user['nickname'],
367                         'dfrn_rawurl' => $_GET['dfrn_url']
368                         ));
369                 return $o;
370
371         }
372         elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) { 
373
374                 // we are the requestee and it is now safe to send our user their introduction,
375                 // We could just unblock it, but first we have to jump through a few hoops to 
376                 // send an email, or even to find out if we need to send an email. 
377
378                 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
379                         dbesc($_GET['confirm_key'])
380                 );
381
382                 if(count($intro)) {
383
384                         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
385                                 WHERE `contact`.`id` = %d LIMIT 1",
386                                 intval($intro[0]['contact-id'])
387                         );
388                         if(count($r)) {
389
390                                 if($r[0]['notify-flags'] & NOTIFY_INTRO) {
391                                         $email_tpl = load_view_file('view/request_notify_eml.tpl');
392                                         $email = replace_macros($email_tpl, array(
393                                                 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
394                                                 '$url' => stripslashes($r[0]['url']),
395                                                 '$myname' => $r[0]['username'],
396                                                 '$siteurl' => $a->get_baseurl(),
397                                                 '$sitename' => $a->config['sitename']
398                                         ));
399                                         $res = mail($r[0]['email'], 
400                                                 t("Introduction received at ") . $a->config['sitename'],
401                                                 $email,
402                                                 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
403                                         // This is a redundant notification - no point throwing errors if it fails.
404                                 }
405                         }
406
407                         $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
408                                 dbesc($_GET['confirm_key'])
409                         );
410
411                 }
412                 killme();
413                 return; // NOTREACHED
414         }
415         else {
416
417                 // Normal web request. Display our user's introduction form. 
418
419                 $o = load_view_file("view/dfrn_request.tpl");
420                 $o = replace_macros($o,array('$nickname' => $a->argv[1]));
421                 return $o;
422         }
423
424         return; // Somebody is fishing.
425 }}