]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
pull some template strings
[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' AND `self` = 0 LIMIT 1",
48                                         intval(local_user()),
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`, `nick`, `photo`, `site-pubkey`,
100                                                 `request`, `confirm`, `notify`, `poll`, `aes_allow`) 
101                                                 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
102                                                 intval(local_user()),
103                                                 datetime_convert(),
104                                                 dbesc($dfrn_url),
105                                                 $parms['fn'],
106                                                 $parms['nick'],
107                                                 $parms['photo'],
108                                                 $parms['key'],
109                                                 $parms['dfrn-request'],
110                                                 $parms['dfrn-confirm'],
111                                                 $parms['dfrn-notify'],
112                                                 $parms['dfrn-poll'],
113                                                 intval($aes_allow)
114                                         );
115                                 }
116
117                                 if($r) {
118                                         notice( t("Introduction complete.") . EOL);
119                                 }
120
121                                 // Allow the blocked remote notification to complete
122
123                                 if(is_array($contact_record))
124                                         $dfrn_request = $contact_record['request'];
125
126                                 if(strlen($dfrn_request) && strlen($confirm_key))
127                                         $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
128                                         // ignore reply
129                                 goaway($dfrn_url);
130                                 return; // NOTREACHED
131
132                         }
133
134                 }
135
136                 // invalid/bogus request
137
138                 notice( t('Unrecoverable protocol error.') . EOL );
139                 goaway($a->get_baseurl());
140                 return; // NOTREACHED
141         }
142
143         // Otherwise:
144
145         // We are the requestee. A person from a remote cell has made an introduction 
146         // on our profile web page and clicked submit. We will use their DFRN-URL to 
147         // figure out how to contact their cell.  
148
149         // Scrape the originating DFRN-URL for everything we need. Create a contact record
150         // and an introduction to show our user next time he/she logs in.
151         // Finally redirect back to the requestor so that their site can record the request.
152         // If our user (the requestee) later confirms this request, a record of it will need 
153         // to exist on the requestor's cell in order for the confirmation process to complete.. 
154
155         // It's possible that neither the requestor or the requestee are logged in at the moment,
156         // and the requestor does not yet have any credentials to the requestee profile.
157
158         // Who is the requestee? We've already loaded their profile which means their nickname should be
159         // in $a->argv[1] and we should have their complete info in $a->profile.
160
161         if(! (is_array($a->profile) && count($a->profile))) {
162                 notice( t('Profile unavailable.') . EOL);
163                 return;
164         }
165
166         $nickname = $a->profile['nickname'];
167         $notify_flags = $a->profile['notify-flags'];
168         $uid = $a->profile['uid'];
169
170         $contact_record = null;
171         $failed = false;
172         $parms = null;
173
174
175         if( x($_POST,'dfrn_url')) {
176
177                 $url = trim($_POST['dfrn_url']);
178                 if(! strlen($url)) {
179                         notice( t("Invalid locator") . EOL );
180                         return;
181                 }
182
183                 // Canonicalise email-style profile locator
184
185                 $url = webfinger_dfrn($url);
186
187                 if(substr($url,0,5) === 'stat:') {
188                         $network = 'stat';
189                         $url = substr($url,5);
190                 }
191                 else {
192                         $network = 'dfrn';
193                 }
194
195                 logger('dfrn_request: url: ' . $url);
196
197                 if(! strlen($url)) {
198                         notice( t("Unable to resolve your name at the provided location.") . EOL);                      
199                         return;
200                 }
201
202
203                 if($network === 'dfrn') {
204                         $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `self` = 0 LIMIT 1", 
205                                 intval($uid),
206                                 dbesc($url)
207                         );
208
209                         if(count($ret)) {
210                                 if(strlen($ret[0]['issued-id'])) {
211                                         notice( t('You have already introduced yourself here.') . EOL );
212                                         return;
213                                 }
214                                 elseif($ret[0]['rel'] == REL_BUD) {
215                                         notice( t('Apparently you are already friends with .') . $a->profile['name'] . EOL);
216                                         return;
217                                 }
218                                 else {
219                                         $contact_record = $ret[0];
220                                         $parms = array('dfrn-request' => $ret[0]['request']);
221                                 }
222                         }
223
224                         $issued_id = random_string();
225
226                         if(is_array($contact_record)) {
227                                 // There is a contact record but no issued-id, so this
228                                 // is a reciprocal introduction from a known contact
229                                 $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
230                                         dbesc($issued_id),
231                                         intval($contact_record['id'])
232                                 );
233                         }
234                         else {
235                                 if(! validate_url($url)) {
236                                         notice( t('Invalid profile URL.') . EOL);
237                                         goaway($a->get_baseurl() . '/' . $a->cmd);
238                                         return; // NOTREACHED
239                                 }
240
241                                 if(! allowed_url($url)) {
242                                         notice( t('Disallowed profile URL.') . EOL);
243                                         goaway($a->get_baseurl() . '/' . $a->cmd);
244                                         return; // NOTREACHED
245                                 }
246                         
247
248                                 require_once('Scrape.php');
249
250                                 $parms = scrape_dfrn($url);
251
252                                 if(! count($parms)) {
253                                         notice( t('Profile location is not valid or does not contain profile information.') . EOL );
254                                         goaway($a->get_baseurl() . '/' . $a->cmd);
255                                 }
256                                 else {
257                                         if(! x($parms,'fn'))
258                                                 notice( t('Warning: profile location has no identifiable owner name.') . EOL );
259                                         if(! x($parms,'photo'))
260                                                 notice( t('Warning: profile location has no profile photo.') . EOL );
261                                         $invalid = validate_dfrn($parms);               
262                                         if($invalid) {
263                                                 notice( $invalid . t(' required parameter') 
264                                                         . (($invalid == 1) ? t(" was ") : t("s were ") )
265                                                         . t("not found at the given location.") . EOL ) ;
266         
267                                                 return;
268                                         }
269                                 }
270
271
272                                 $parms['url'] = $url;
273                                 $parms['issued-id'] = $issued_id;
274
275
276                                 dbesc_array($parms);
277                                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `issued-id`, `photo`, `site-pubkey`,
278                                         `request`, `confirm`, `notify`, `poll` )
279                                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
280                                         intval($uid),
281                                         datetime_convert(),
282                                         $parms['url'],
283                                         $parms['fn'],
284                                         $parms['nick'],
285                                         $parms['issued-id'],
286                                         $parms['photo'],
287                                         $parms['key'],
288                                         $parms['dfrn-request'],
289                                         $parms['dfrn-confirm'],
290                                         $parms['dfrn-notify'],
291                                         $parms['dfrn-poll']
292                                 );
293
294                                 // find the contact record we just created
295                                 if($r) {        
296                                         $r = q("SELECT `id` FROM `contact` 
297                                                 WHERE `uid` = %d AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
298                                                 intval($uid),
299                                                 $parms['url'],
300                                                 $parms['issued-id']
301                                         );
302                                         if(count($r)) 
303                                                 $contact_record = $r[0];
304                                 }
305         
306                         }
307                         if($r === false) {
308                                 notice( t('Failed to update contact record.') . EOL );
309                                 return;
310                         }
311
312                         $hash = random_string() . (string) time();   // Generate a confirm_key
313         
314                         if(is_array($contact_record)) {
315                                 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
316                                         VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
317                                         intval($uid),
318                                         intval($contact_record['id']),
319                                         ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
320                                         dbesc(notags(trim($_POST['dfrn-request-message']))),
321                                         dbesc($hash),
322                                         dbesc(datetime_convert())
323                                 );
324                         }
325         
326                         // This notice will only be seen by the requestor if the requestor and requestee are on the same server.
327
328                         if(! $failed) 
329                                 notice( t('Your introduction has been sent.') . EOL );
330
331                         // "Homecoming" - send the requestor back to their site to record the introduction.
332
333                         $dfrn_url = bin2hex($a->get_baseurl() . '/profile/' . $nickname);
334                         $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
335
336                         goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" 
337                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
338                                 . '&confirm_key='  . $hash 
339                                 . (($aes_allow) ? "&aes_allow=1" : "")
340                         );
341                         // NOTREACHED
342                         // END $network === 'dfrn'
343                 }
344                 elseif($network === 'stat') {
345                         
346                         /**
347                         *
348                         * OStatus network
349                         * Check contact existence
350                         * Try and scrape together enough information to create a contact record, with us as REL_VIP
351                         * Substitute our user's feed URL into $url template
352                         * Send the subscriber home to subscribe
353                         *
354                         **/
355
356                         $url = str_replace('{uri}', $a->get_baseurl() . '/dfrn_poll/' . $nickname, $url);
357                         goaway($url);
358                         // NOTREACHED
359                         // END $network === 'stat'
360                 }
361
362         }       return;
363 }}
364
365
366
367
368 if(! function_exists('dfrn_request_content')) {
369 function dfrn_request_content(&$a) {
370
371         
372
373         if(($a->argc != 2) || (! count($a->profile)))
374                 return "";
375
376         $a->page['template'] = 'profile';
377
378         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
379         // to send us to the post section to record the introduction.
380
381         if(x($_GET,'dfrn_url')) {
382
383                 if(! local_user()) {
384                         notice( t("Please login to confirm introduction.") . EOL );
385                         return login();
386                 }
387
388                 // Edge case, but can easily happen in the wild. This person is authenticated, 
389                 // but not as the person who needs to deal with this request.
390
391                 if ($a->user['nickname'] != $a->argv[1]) {
392                         notice( t("Incorrect identity currently logged in. Please login to <strong>this</strong> profile.") . EOL);
393                         return login();
394                 }
395
396                 $dfrn_url = notags(trim(hex2bin($_GET['dfrn_url'])));
397                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
398                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
399                 $o .= load_view_file("view/dfrn_req_confirm.tpl");
400                 $o  = replace_macros($o,array(
401                         '$dfrn_url' => $dfrn_url,
402                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
403                         '$confirm_key' => $confirm_key,
404                         '$username' => $a->user['username'], 
405                         '$uid' => $_SESSION['uid'],
406                         '$nickname' => $a->user['nickname'],
407                         'dfrn_rawurl' => $_GET['dfrn_url']
408                         ));
409                 return $o;
410
411         }
412         elseif((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) { 
413
414                 // we are the requestee and it is now safe to send our user their introduction,
415                 // We could just unblock it, but first we have to jump through a few hoops to 
416                 // send an email, or even to find out if we need to send an email. 
417
418                 $intro = q("SELECT * FROM `intro` WHERE `hash` = '%s' LIMIT 1",
419                         dbesc($_GET['confirm_key'])
420                 );
421
422                 if(count($intro)) {
423
424                         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
425                                 WHERE `contact`.`id` = %d LIMIT 1",
426                                 intval($intro[0]['contact-id'])
427                         );
428
429                         $auto_confirm = false;
430
431                         if(count($r)) {
432                                 if($r[0]['page-flags'] != PAGE_NORMAL)
433                                         $auto_confirm = true;                           
434                                 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && (! $auto_confirm)) {
435                                         $email_tpl = load_view_file('view/request_notify_eml.tpl');
436                                         $email = replace_macros($email_tpl, array(
437                                                 '$requestor' => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
438                                                 '$url' => stripslashes($r[0]['url']),
439                                                 '$myname' => $r[0]['username'],
440                                                 '$siteurl' => $a->get_baseurl(),
441                                                 '$sitename' => $a->config['sitename']
442                                         ));
443                                         $res = mail($r[0]['email'], 
444                                                 t("Introduction received at ") . $a->config['sitename'],
445                                                 $email,
446                                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
447                                         // This is a redundant notification - no point throwing errors if it fails.
448                                 }
449                                 if($auto_confirm) {
450                                         require_once('mod/dfrn_confirm.php');
451                                         $handsfree = array(
452                                                 'uid' => $r[0]['uid'],
453                                                 'node' => $r[0]['nickname'],
454                                                 'dfrn_id' => $r[0]['issued-id'],
455                                                 'intro_id' => $intro[0]['id'],
456                                                 'duplex' => (($r[0]['page-flags'] == PAGE_FREELOVE) ? 1 : 0)
457                                         );
458                                         dfrn_confirm_post($a,$handsfree);
459                                 }
460
461                         }
462
463                         if(! $auto_confirm) {
464
465                                 // If we are auto_confirming, this record will have already been nuked
466                                 // in dfrn_confirm_post()
467
468                                 $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
469                                         dbesc($_GET['confirm_key'])
470                                 );
471                         }
472                 }
473                 killme();
474                 return; // NOTREACHED
475         }
476         else {
477                 $myaddr = ((x($_GET,'address')) ? urldecode($_GET['address']) : '');
478                 // Normal web request. Display our user's introduction form. 
479                 if($a->profile['page-flags'] == PAGE_NORMAL)
480                         $tpl = load_view_file('view/dfrn_request.tpl');
481                 else
482                         $tpl = load_view_file('view/auto_request.tpl');
483                 $o .= replace_macros($tpl,array(
484                         '$nickname' => $a->argv[1],
485                         '$name' => $a->profile['name'],
486                         '$myaddr' => $myaddr
487                 ));
488                 return $o;
489         }
490
491         return; // Somebody is fishing.
492 }}