]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
add mail module
[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($_SESSION['authenticated']) {
7                 // choose which page to show (could be remote auth)
8
9         }
10
11         if($a->argc > 1)
12                 $which = $a->argv[1];
13
14         require_once('mod/profile.php');
15         profile_init($a,$which);
16
17         return;
18 }}
19
20
21 if(! function_exists('dfrn_request_post')) {
22 function dfrn_request_post(&$a) {
23
24         if(($a->argc != 2) || (! count($a->profile)))
25                 return;
26
27
28         if($_POST['cancel']) {
29                 goaway($a->get_baseurl());
30         } 
31
32
33         // callback to local site after remote request and local confirm
34
35         if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1) 
36                 && local_user() && ($a->user['nickname'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
37
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.
41
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'] : "");
45
46                 $contact_record = null;
47         
48                 if(x($dfrn_url)) {
49
50                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
51                                 intval($_SESSION['uid']),
52                                 dbesc($dfrn_url)
53                         );
54         
55                         if(count($r)) {
56                                 if(strlen($r[0]['dfrn-id'])) {
57                                         notice("This introduction has already been accepted." . EOL );
58                                         return;
59                                 }
60                                 else
61                                         $contact_record = $r[0];
62                         }
63         
64                         if(is_array($contact_record)) {
65                                 $r = q("UPDATE `contact` SET `ret-aes` = %d WHERE `id` = %d LIMIT 1",
66                                         intval($aes_allow),
67                                         intval($contact_record['id'])
68                                 );
69                         }
70                         else {
71
72                                 require_once('Scrape.php');
73
74
75                                 $parms = scrape_dfrn($dfrn_url);
76
77                                 if(! count($parms)) {
78                                         notice( 'Profile location is not valid or does not contain profile information.' . EOL );
79                                         return;
80                                 }
81                                 else {
82                                         if(! x($parms,'fn'))
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);               
87                                         if($invalid) {
88                                                 notice( $invalid . ' required parameter' 
89                                                         . (($invalid == 1) ? " was " : "s were " )
90                                                         . "not found at the given location." . EOL ) ;
91                                                 return;
92                                         }
93                                 }
94
95
96
97                                 $dfrn_request = $parms['dfrn-request'];
98
99                                 dbesc_array($parms);
100
101
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']),
106                                         datetime_convert(),
107                                         dbesc($dfrn_url),
108                                         $parms['fn'],
109                                         $parms['photo'],
110                                         $parms['key'],
111                                         $parms['dfrn-request'],
112                                         $parms['dfrn-confirm'],
113                                         $parms['dfrn-notify'],
114                                         $parms['dfrn-poll'],
115                                         intval($aes_allow)
116                                 );
117                         }
118
119                         if($r) {
120                                 notice( "Introduction complete." . EOL);
121                         }
122
123                         // Allow the blocked remote notification to complete
124
125                         if(is_array($contact_record))
126                                 $dfrn_request = $contact_record['request'];
127
128                         if(strlen($dfrn_request) && strlen($confirm_key))
129                                 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
130                                 // ignore reply
131                         goaway($dfrn_url);
132                         // NOTREACHED
133
134                 }
135                 // invalid DFRN-url
136                 notice( "Unrecoverable protocol error." . EOL );
137                 goaway($a->get_baseurl());
138         }
139         // extra safety
140         if($_POST['localconfirm'])
141                 return;
142
143
144         // we are operating as a remote site and an introduction was requested of us.
145         // Scrape the originating DFRN-URL for everything we need. Create a contact record
146         // and an introduction to show our user next time he/she logs in.
147         // Finally redirect back to the originator so that their site can record the request.
148         // If our user confirms the request, a record of it will need to exist on the 
149         // originator's site in order for the confirmation process to complete.. 
150
151
152         $tailname = $a->profile['nickname'];
153
154         $uid = $a->profile['uid'];
155
156         $contact_record = null;
157         $failed = false;
158         $parms = null;
159
160
161         if( x($_POST,'dfrn_url')) {
162
163                 $url = trim($_POST['dfrn_url']);
164                 if(! strlen($url)) {
165                         notice( "Invalid URL" . EOL );
166                         return;
167                 }
168
169                 if(strstr($url,'@')) {
170                         $username = substr($url,0,strpos($url,'@'));
171                         $hostname = substr($url,strpos($url,'@') + 1);
172                         require_once('Scrape.php');
173
174                 
175                         $parms = scrape_meta('https://' . $url);
176                         if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
177                                 $url = sprintf($parms['dfrn-template'],$username);
178                         }
179                         else {
180                                 $parms = scrape_meta('http://' . $url);
181                                 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
182                                         $url = sprintf($parms['dfrn-template'],$username);
183                                 }
184                                 else {
185                                         $url = '';
186                                 }
187                         }
188
189                 }
190
191                 if(! strlen($url)) {
192                         notice("Unable to resolve your name at the provided location." . EOL);                  
193                         return;
194                 }
195
196                 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1", 
197                         intval($uid),
198                         dbesc($url)
199                 );
200
201                 if(count($ret)) {
202                         if(strlen($ret[0]['issued-id'])) {
203                                 notice( 'You have already introduced yourself here.' . EOL );
204                                 return;
205                         }
206                         else {
207                                 $contact_record = $ret[0];
208                                 $parms = array('dfrn-request' => $ret[0]['request']);
209                         }
210                 }
211                 $issued_id = random_string();
212
213                 if(is_array($contact_record)) {
214                         // There is a contact record but no issued-id, so this
215                         // is a reciprocal introduction from a known contact
216                         $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
217                                 dbesc($issued_id),
218                                 intval($contact_record['id'])
219                         );
220                 }
221                 else {
222         
223                         require_once('Scrape.php');
224
225                         $parms = scrape_dfrn($url);
226
227                         if(! count($parms)) {
228                                 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
229                                 killme();
230                         }
231                         else {
232                                 if(! x($parms,'fn'))
233                                         notice( 'Warning: profile location has no identifiable owner name.' . EOL );
234                                 if(! x($parms,'photo'))
235                                         notice( 'Warning: profile location has no profile photo.' . EOL );
236                                 $invalid = validate_dfrn($parms);               
237                                 if($invalid) {
238                                         notice( $invalid . ' required parameter' 
239                                                 . (($invalid == 1) ? " was " : "s were " )
240                                                 . "not found at the given location." . EOL ) ;
241
242                                         return;
243                                 }
244                         }
245
246
247                         $parms['url'] = $url;
248                         $parms['issued-id'] = $issued_id;
249
250
251                         dbesc_array($parms);
252                         $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
253                                 `request`, `confirm`, `notify`, `poll` )
254                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
255                                 intval($uid),
256                                 datetime_convert(),
257                                 $parms['url'],
258                                 $parms['fn'],
259                                 $parms['issued-id'],
260                                 $parms['photo'],
261                                 $parms['key'],
262                                 $parms['dfrn-request'],
263                                 $parms['dfrn-confirm'],
264                                 $parms['dfrn-notify'],
265                                 $parms['dfrn-poll']
266                         );
267
268                         // find the contact record we just created
269                         if($r) {        
270                                 $r = q("SELECT `id` FROM `contact` 
271                                         WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
272                                         intval($uid),
273                                         $parms['url'],
274                                         $parms['issued-id']
275                                 );
276                                 if(count($r)) 
277                                         $contact_record = $r[0];
278                         }
279         
280                 }
281                 if($r === false) {
282                         notice( 'Failed to update contact record.' . EOL );
283                         return;
284                 }
285
286                 $hash = random_string() . (string) time();   // Generate a confirm_key
287
288                 if(is_array($contact_record)) {
289                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
290                                 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
291                                 intval($uid),
292                                 intval($contact_record['id']),
293                                 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
294                                 dbesc(trim($_POST['dfrn-request-message'])),
295                                 dbesc($hash),
296                                 dbesc(datetime_convert())
297                         );
298                 }
299         
300                 // TODO: send an email notification if our user wants one
301
302                 if(! $failed) 
303                         notice( "Your introduction has been sent." . EOL );
304
305                 // "Homecoming" - send the requestor back to their site to record the introduction.
306
307                 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
308                 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
309
310                 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
311                 // NOTREACHED
312
313         }
314         return;
315 }}
316
317 if(! function_exists('dfrn_request_content')) {
318 function dfrn_request_content(&$a) {
319
320         
321
322         if(($a->argc != 2) || (! count($a->profile)))
323                 return "";
324
325         $a->page['template'] = 'profile';
326
327         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
328         // to send us to the post section to record the introduction.
329
330         if(x($_GET,'dfrn_url')) {
331
332                 if(! local_user()) {
333                         notice( "Please login to confirm introduction." . EOL );
334                         return login();
335                 }
336
337                 // Edge case, but can easily happen in the wild. This person is authenticated, 
338                 // but not as the person who needs to deal with this request.
339
340                 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
341                         notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
342                         return login();
343                 }
344
345                 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
346                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
347                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
348                 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
349                 $o  = replace_macros($o,array(
350                         '$dfrn_url' => $dfrn_url,
351                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
352                         '$confirm_key' => $confirm_key,
353                         '$username' => $a->user['username'], 
354                         '$uid' => $_SESSION['uid'],
355                         '$nickname' => $a->user['nickname'],
356                         'dfrn_rawurl' => $_GET['dfrn_url']
357                         ));
358                 return $o;
359
360         }
361         else {
362                 // we are the requestee and it is now safe to send our user their introduction
363                 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
364                         $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
365                                 dbesc($_GET['confirm_key'])
366                         );
367                         killme();
368                 }
369
370
371         // Outside request. Display our user's introduction form. 
372
373
374         $o = file_get_contents("view/dfrn_request.tpl");
375         $o = replace_macros($o,array('$uid' => $a->argv[1]));
376         return $o;
377         }
378 }}