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