]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_request.php
ef3c7274bc21dbd505076b1e938cd9d02fa1864a
[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         // callback to local site after remote request and local confirm
33
34         if((x($_POST,'localconfirm')) && ($_POST['localconfirm'] == 1) 
35                 && (x($_SESSION,'authenticated')) && (x($_SESSION,'uid'))
36                 && ($_SESSION['uid'] == $a->argv[1]) && (x($_POST,'dfrn_url'))) {
37
38                 $dfrn_url = notags(trim($_POST['dfrn_url']));
39                 $aes_allow = (((x($_POST,'aes_allow')) && ($_POST['aes_allow'] == 1)) ? 1 : 0);
40                 $confirm_key = ((x($_POST,'confirm_key')) ? $_POST['confirm_key'] : "");
41                 $failed = false;
42
43                 require_once('Scrape.php');
44
45                 if(x($dfrn_url)) {
46
47                         $parms = scrape_dfrn($dfrn_url);
48
49                         if(! count($parms)) {
50                                 $_SESSION['sysmsg'] .= 'URL is not valid or does not contain profile information.' . EOL ;
51                                 $failed = true;
52                         }
53                         else {
54                                 if(! x($parms,'fn'))
55                                         $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no identifiable owner name.' . EOL ;
56                                 if(! x($parms,'photo'))
57                                         $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no profile photo.' . EOL ;
58                                 $invalid = validate_dfrn($parms);               
59                                 if($invalid) {
60                                         echo $invalid . ' required DFRN parameter' 
61                                                 . (($invalid == 1) ? " was " : "s were " )
62                                                 . "not found at the given URL" . '<br />';
63
64                                         $failed = true;
65                                 }
66                         }
67                 }
68                 if(! $failed) {
69
70                         $dfrn_request = $parms['dfrn-request'];
71                         /////////////////////////
72                         dbesc_array($parms);
73                         ////////////////////////
74
75                         $r = q("INSERT INTO `contact` ( `uid`, `created`,`url`, `name`, `photo`, `site-pubkey`,
76                                 `request`, `confirm`, `notify`, `poll`, `aes_allow`) 
77                                 VALUES ( %d, '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d)",
78                                 intval($_SESSION['uid']),
79                                 datetime_convert(),
80                                 dbesc($dfrn_url),
81                                 $parms['fn'],
82                                 $parms['photo'],
83                                 $parms['key'],
84                                 $parms['dfrn-request'],
85                                 $parms['dfrn-confirm'],
86                                 $parms['dfrn-notify'],
87                                 $parms['dfrn-poll'],
88                                 intval($aes_allow)
89                         );
90                         if($r === false)
91                                 $_SESSION['sysmsg'] .= "Failed to create contact." . EOL;
92                         else
93                                 $_SESSION['sysmsg'] .= "Introduction complete.";
94
95                         // Allow the blocked remote notification to complete
96
97                         if(strlen($dfrn_request) && strlen($confirm_key))
98                                 $s = fetch_url($dfrn_request . '?confirm_key=' . $confirm_key);
99
100                         goaway($dfrn_url);
101                 }
102         }
103
104
105         // we are operating as a remote site and an introduction was requested of us.
106         // Scrape the originating DFRN-URL for everything we need. Create a contact record
107         // and an introduction to show our user next time he/she logs in.
108         // Finally redirect back to the originator so that their site can record the request.
109         // If our user confirms the request, a record of it will need to exist on the 
110         // originator's site in order for the confirmation process to complete.. 
111
112         if($a->profile['nickname'])
113                 $tailname = $a->profile['nickname'];
114         else
115                 $tailname = $a->profile['uid'];
116
117         $uid = $a->profile['uid'];
118
119         $failed = false;
120
121         require_once('Scrape.php');
122
123         if( x($_POST,'dfrn_url')) {
124
125                 $url = trim($_POST['dfrn_url']);
126                 if(x($url)) {
127                         $parms = scrape_dfrn($url);
128
129                         if(! count($parms)) {
130                                 $_SESSION['sysmsg'] .= 'URL is not valid or does not contain profile information.' . EOL ;
131                                 $failed = true;
132                         }
133                         else {
134                                 if(! x($parms,'fn'))
135                                         $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no identifiable owner name.' . EOL ;
136                                 if(! x($parms,'photo'))
137                                         $_SESSION['sysmsg'] .= 'Warning: DFRN profile has no profile photo.' . EOL ;
138                                 $invalid = validate_dfrn($parms);               
139                                 if($invalid) {
140                                         echo $invalid . ' required DFRN parameter' 
141                                                 . (($invalid == 1) ? " was " : "s were " )
142                                                 . "not found at the given URL" . '<br />';
143
144                                         $failed = true;
145                                 }
146                         }
147                 }
148
149                 $ret = q("SELECT `url` FROM `contact` WHERE `url` = '%s'", dbesc($url));
150                 if($ret !== false && count($ret)) {
151                         $_SESSION['sysmsg'] .= 'You have already introduced yourself here.' . EOL;
152                         $failed = true;
153                 }
154
155
156                 if(! $failed) {
157
158                         $parms['url'] = $url;
159                         $parms['issued-id'] = random_string();
160
161                         /////////////////////////
162                         dbesc_array($parms);
163                         ////////////////////////
164
165                 $ret = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `issued-id`, `photo`, `site-pubkey`,
166                         `request`, `confirm`, `notify`, `poll`, `visible` )
167                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
168                         intval($uid),
169                         datetime_convert(),
170                         $parms['url'],
171                         $parms['fn'],
172                         $parms['issued-id'],
173                         $parms['photo'],
174                         $parms['key'],
175                         $parms['dfrn-request'],
176                         $parms['dfrn-confirm'],
177                         $parms['dfrn-notify'],
178                         $parms['dfrn-poll'],
179                         ((x($_POST,'visible')) ? 1 : 0 )
180                         );
181         
182                 }
183                 if($ret === false) {
184                         $_SESSION['sysmsg'] .= 'Failed to create contact record.' . EOL;
185                         return;
186                 }
187
188                 $ret = q("SELECT `id` FROM `contact` 
189                         WHERE `uid` = '%s' AND `url` = '%s' AND `issued-id` = '%s' 
190                         LIMIT 1",
191                         intval($uid),
192                         $parms['url'],
193                         $parms['issued-id']
194                         );
195
196                 if(($ret !== NULL)  && (count($ret)))
197                         $contact_id = $ret[0]['id'];
198
199                 $hash = random_string() . (string) time();   // Generate a confirm_key
200
201                 if($contact_id) {
202                         $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
203                         VALUES ( %d, %d, 1, %d, '%s', '%s', '%s' )",
204                         intval($uid),
205                         intval($contact_id),
206                         ((x($_POST,'knowyou') && ($_POST['knowyou'] == 1)) ? 1 : 0),
207                         dbesc(trim($_POST['dfrn-request-message'])),
208                         dbesc($hash),
209                         dbesc(datetime_convert())
210                         );
211                 }
212                 
213         
214                 // TODO: send an email notification if our user wants one
215
216                 if(! $failed) 
217                         $_SESSION['sysmsg'] .= "Your introduction has been sent." . EOL;
218
219                 // "Homecoming" - send the requestor back to their site to record the introduction.
220
221                 $dfrn_url = bin2hex($a->get_baseurl() . "/profile/$tailname");
222                 $aes_allow = ((function_exists('openssl_encrypt')) ? 1 : 0);
223
224                 goaway($parms['dfrn-request'] . "?dfrn_url=$dfrn_url" . '&confirm_key=' . $hash . (($aes_allow) ? "&aes_allow=1" : ""));
225
226         }
227
228 }}
229
230 if(! function_exists('dfrn_request_content')) {
231 function dfrn_request_content(&$a) {
232
233         
234
235         if(($a->argc != 2) || (! count($a->profile)))
236                 return "";
237
238         $a->page['template'] = 'profile';
239
240         // "Homecoming". Make sure we're logged in to this site as the correct user. Then offer a confirm button
241         // to send us to the post section to record the introduction.
242
243         if(x($_GET,'dfrn_url')) {
244
245                 if(! x($_SESSION,'authenticated')) {
246                         $_SESSION['sysmsg'] .= "Please login to confirm introduction." . EOL;
247                         return login();
248                 }
249
250                 // Edge case, but can easily happen in the wild. This person is authenticated, 
251                 // but not as the person who needs to deal with this request.
252
253                 if (($_SESSION['uid'] != $a->argv[1]) && ($a->user['nickname'] != $a->argv[1])) {
254                         $_SESSION['sysmsg'] .= "Incorrect identity currently logged in. Please login to <strong>this</strong> profile." . EOL;
255                         return login();
256                 }
257
258                 $dfrn_url = notags(trim(pack("H*" , $_GET['dfrn_url'])));
259                 $aes_allow = (((x($_GET,'aes_allow')) && ($_GET['aes_allow'] == 1)) ? 1 : 0);
260                 $confirm_key = (x($_GET,'confirm_key') ? $_GET['confirm_key'] : "");
261                 $o .= file_get_contents("view/dfrn_req_confirm.tpl");
262                 $o  = replace_macros($o,array(
263                         '$dfrn_url' => $dfrn_url,
264                         '$aes_allow' => (($aes_allow) ? '<input type="hidden" name="aes_allow" value="1" />' : "" ),
265                         '$confirm_key' => $confirm_key,
266                         '$username' => $a->user['username'], 
267                         '$uid' => $_SESSION['uid'],
268                         'dfrn_rawurl' => $_GET['dfrn_url']
269                         ));
270                 return $o;
271
272         }
273         else {
274                 // safe to send our user their introduction
275                 if((x($_GET,'confirm_key')) && strlen($_GET['confirm_key'])) {
276                         $r = q("UPDATE `intro` SET `blocked` = 0 WHERE `hash` = '%s' LIMIT 1",
277                                 dbesc($_GET['confirm_key'])
278                         );
279                         return;
280                 }
281
282
283         // Outside request. Display our user's introduction form. 
284
285
286         $o = file_get_contents("view/dfrn_request.tpl");
287         $o = replace_macros($o,array('$uid' => $a->profile['uid']));
288         return $o;
289         }
290 }}