]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
try this
[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() && ($_SESSION['uid'] == $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
140
141         // we are operating as a remote site and an introduction was requested of us.
142         // Scrape the originating DFRN-URL for everything we need. Create a contact record
143         // and an introduction to show our user next time he/she logs in.
144         // Finally redirect back to the originator so that their site can record the request.
145         // If our user confirms the request, a record of it will need to exist on the 
146         // originator's site in order for the confirmation process to complete.. 
147
148
149         $tailname = $a->profile['nickname'];
150
151         $uid = $a->profile['uid'];
152
153         $contact_record = null;
154         $failed = false;
155         $parms = null;
156
157
158         if( x($_POST,'dfrn_url')) {
159
160                 $url = trim($_POST['dfrn_url']);
161                 if(! strlen($url)) {
162                         notice( "Invalid URL" . EOL );
163                         return;
164                 }
165
166                 if(strstr($url,'@')) {
167                         $username = substr($url,0,strpos($url,'@'));
168                         $hostname = substr($url,strpos($url,'@') + 1);
169                         require_once('Scrape.php');
170
171                 
172                         $parms = scrape_meta('https://' . $url);
173                         if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
174                                 $url = sprintf($parms['dfrn-template'],$username);
175                         }
176                         else {
177                                 $parms = scrape_meta('http://' . $url);
178                                 if((x($parms,'dfrn-template')) && strstr($parms['dfrn-template'],'%s')) {
179                                         $url = sprintf($parms['dfrn-template'],$username);
180                                 }
181                                 else {
182                                         $url = '';
183                                 }
184                         }
185
186                 }
187
188                 if(! strlen($url)) {
189                         notice("Unable to resolve your name at the provided location." . EOL);                  
190                         return;
191                 }
192
193                 $ret = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1", 
194                         intval($uid),
195                         dbesc($url)
196                 );
197
198                 if(count($ret)) {
199                         if(strlen($ret[0]['issued-id'])) {
200                                 notice( 'You have already introduced yourself here.' . EOL );
201                                 return;
202                         }
203                         else {
204                                 $contact_record = $ret[0];
205                                 $parms = array('dfrn-request' => $ret[0]['request']);
206                         }
207                 }
208                 $issued_id = random_string();
209
210                 if(is_array($contact_record)) {
211                         // There is a contact record but no issued-id, so this
212                         // is a reciprocal introduction from a known contact
213                         $r = q("UPDATE `contact` SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1",
214                                 dbesc($issued_id),
215                                 intval($contact_record['id'])
216                         );
217                 }
218                 else {
219         
220                         require_once('Scrape.php');
221
222                         $parms = scrape_dfrn($url);
223
224                         if(! count($parms)) {
225                                 notice( 'Profile location is not valid or does not contain profile information.' . EOL );
226                                 killme();
227                         }
228                         else {
229                                 if(! x($parms,'fn'))
230                                         notice( 'Warning: profile location has no identifiable owner name.' . EOL );
231                                 if(! x($parms,'photo'))
232                                         notice( 'Warning: profile location has no profile photo.' . EOL );
233                                 $invalid = validate_dfrn($parms);               
234                                 if($invalid) {
235                                         notice( $invalid . ' required parameter' 
236                                                 . (($invalid == 1) ? " was " : "s were " )
237                                                 . "not found at the given location." . EOL ) ;
238
239                                         return;
240                                 }
241                         }
242
243
244                         $parms['url'] = $url;
245                         $parms['issued-id'] = $issued_id;
246
247
248                         dbesc_array($parms);
249                         $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
250                                 `request`, `confirm`, `notify`, `poll` )
251                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
252                                 intval($uid),
253                                 datetime_convert(),
254                                 $parms['url'],
255                                 $parms['fn'],
256                                 $parms['issued-id'],
257                                 $parms['photo'],
258                                 $parms['key'],
259                                 $parms['dfrn-request'],
260                                 $parms['dfrn-confirm'],
261                                 $parms['dfrn-notify'],
262                                 $parms['dfrn-poll']
263                         );
264
265                         // find the contact record we just created
266                         if($r) {        
267                                 $r = q("SELECT `id` FROM `contact` 
268                                         WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' LIMIT 1",
269                                         intval($uid),
270                                         $parms['url'],
271                                         $parms['issued-id']
272                                 );
273                                 if(count($r)) 
274                                         $contact_record = $r[0];
275                         }
276         
277                 }
278                 if($r === false) {
279                         notice( 'Failed to update contact record.' . EOL );
280                         return;
281                 }
282
283                 $hash = random_string() . (string) time();   // Generate a confirm_key
284
285                 if(is_array($contact_record)) {
286                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
287                                 VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
288                                 intval($uid),
289                                 intval($contact_record['id']),
290                                 ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
291                                 dbesc(trim($_POST['dfrn-request-message'])),
292                                 dbesc($hash),
293                                 dbesc(datetime_convert())
294                         );
295                 }
296         
297                 // TODO: send an email notification if our user wants one
298
299                 if(! $failed) 
300                         notice( "Your introduction has been sent." . EOL );
301
302                 // "Homecoming" - send the requestor back to their site to record the introduction.
303
304                 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
305                 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
306
307                 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
308                 // NOTREACHED
309
310         }
311         return;
312 }}
313
314 if(! function_exists('dfrn_request_content')) {
315 function dfrn_request_content(&$a) {
316
317         
318
319         if(($a->argc != 2) || (! count($a->profile)))
320                 return "";
321
322         $a->page['template'] = 'profile';
323
324         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
325         // to send us to the post section to record the introduction.
326
327         if(x($_GET,'dfrn_url')) {
328
329                 if(! local_user()) {
330                         notice( "Please login to confirm introduction." . EOL );
331                         return login();
332                 }
333
334                 // Edge case, but can easily happen in the wild. This person is authenticated, 
335                 // but not as the person who needs to deal with this request.
336
337                 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
338                         notice( "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL);
339                         return login();
340                 }
341
342                 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
343                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
344                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
345                 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
346                 $o  = replace_macros($o,array(
347                         '$dfrn_url' => $dfrn_url,
348                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
349                         '$confirm_key' => $confirm_key,
350                         '$username' => $a->user['username'], 
351                         '$uid' => $_SESSION['uid'],
352                         'dfrn_rawurl' => $_GET['dfrn_url']
353                         ));
354                 return $o;
355
356         }
357         else {
358                 // we are the requestee and it is now safe to send our user their introduction
359                 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
360                         $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
361                                 dbesc($_GET['confirm_key'])
362                         );
363                         killme();
364                 }
365
366
367         // Outside request. Display our user's introduction form. 
368
369
370         $o = file_get_contents("view/dfrn_request.tpl");
371         $o = replace_macros($o,array('$uid' => $a->argv[2]));
372         return $o;
373         }
374 }}