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