]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
track whether contact is a community page or not
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('library/simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
6
7
8 function dfrn_notify_post(&$a) {
9
10         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? notags(trim($_POST['dfrn_id']))   : '');
11         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version']    : 2.0);
12         $challenge    = ((x($_POST,'challenge'))    ? notags(trim($_POST['challenge'])) : '');
13         $data         = ((x($_POST,'data'))         ? $_POST['data']                    : '');
14         $key          = ((x($_POST,'key'))          ? $_POST['key']                     : '');
15         $dissolve     = ((x($_POST,'dissolve'))     ? intval($_POST['dissolve'])        :  0);
16         $perm         = ((x($_POST,'perm'))         ? notags(trim($_POST['perm']))      : 'r');
17         $ssl_policy   = ((x($_POST,'ssl_policy'))   ? notags(trim($_POST['ssl_policy'])): 'none');
18         $page         = ((x($_POST,'page'))         ? intval($_POST['page'])            :  0);
19
20         $writable = (-1);
21         if($dfrn_version >= 2.21) {
22                 $writable = (($perm === 'rw') ? 1 : 0);
23         }
24
25         $direction = (-1);
26         if(strpos($dfrn_id,':') == 1) {
27                 $direction = intval(substr($dfrn_id,0,1));
28                 $dfrn_id = substr($dfrn_id,2);
29         }
30
31         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
32                 dbesc($dfrn_id),
33                 dbesc($challenge)
34         );
35         if(! count($r)) {
36                 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
37                 xml_status(3);
38         }
39
40         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
41                 dbesc($dfrn_id),
42                 dbesc($challenge)
43         );
44
45         // find the local user who owns this relationship.
46
47         $sql_extra = '';
48         switch($direction) {
49                 case (-1):
50                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
51                         break;
52                 case 0:
53                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
54                         break;
55                 case 1:
56                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
57                         break;
58                 default:
59                         xml_status(3);
60                         break; // NOTREACHED
61         }
62                  
63         // be careful - $importer will contain both the contact information for the contact
64         // sending us the post, and also the user information for the person receiving it.
65         // since they are mixed together, it is easy to get them confused.
66
67         $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
68                                         `contact`.`pubkey` AS `cpubkey`, 
69                                         `contact`.`prvkey` AS `cprvkey`, 
70                                         `contact`.`thumb` AS `thumb`, 
71                                         `contact`.`url` as `url`,
72                                         `contact`.`name` as `senderName`,
73                                         `user`.* 
74                         FROM `contact` 
75                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
76                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
77                                 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
78                 dbesc($a->argv[1])
79         );
80
81         if(! count($r)) {
82                 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
83                 xml_status(3);
84                 //NOTREACHED
85         }
86
87         // $importer in this case contains the contact record for the remote contact joined with the user record of our user. 
88
89         $importer = $r[0];
90
91         if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $page)) {
92                 q("UPDATE `contact` SET `writable` = %d, forum = %d WHERE `id` = %d LIMIT 1",
93                         intval(($writable == (-1)) ? $importer['writable'] : $writable),
94                         intval($page),
95                         intval($importer['id'])
96                 );
97                 if($writable != (-1))
98                         $importer['writable'] = $writable;
99                 $importer['forum'] = $page;
100         }
101
102         // if contact's ssl policy changed, update our links
103
104         $ssl_changed = false;
105
106         if($ssl_policy == 'self' && strstr($importer['url'],'https:')) {
107                 $ssl_changed = true;
108                 $importer['url']     =  str_replace('https:','http:',$importer['url']);
109                 $importer['nurl']    = normalise_link($importer['url']);
110                 $importer['photo']   =  str_replace('https:','http:',$importer['photo']);
111                 $importer['thumb']   =  str_replace('https:','http:',$importer['thumb']);
112                 $importer['micro']   =  str_replace('https:','http:',$importer['micro']);
113                 $importer['request'] =  str_replace('https:','http:',$importer['request']);
114                 $importer['notify']  =  str_replace('https:','http:',$importer['notify']);
115                 $importer['poll']    =  str_replace('https:','http:',$importer['poll']);
116                 $importer['confirm'] =  str_replace('https:','http:',$importer['confirm']);
117                 $importer['poco']    =  str_replace('https:','http:',$importer['poco']);
118         }
119
120         if($ssl_policy == 'full' && strstr($importer['url'],'http:')) {
121                 $ssl_changed = true;
122                 $importer['url']     =  str_replace('http:','https:',$importer['url']);
123                 $importer['nurl']    = normalise_link($importer['url']);
124                 $importer['photo']   =  str_replace('http:','https:',$importer['photo']);
125                 $importer['thumb']   =  str_replace('http:','https:',$importer['thumb']);
126                 $importer['micro']   =  str_replace('http:','https:',$importer['micro']);
127                 $importer['request'] =  str_replace('http:','https:',$importer['request']);
128                 $importer['notify']  =  str_replace('http:','https:',$importer['notify']);
129                 $importer['poll']    =  str_replace('http:','https:',$importer['poll']);
130                 $importer['confirm'] =  str_replace('http:','https:',$importer['confirm']);
131                 $importer['poco']    =  str_replace('http:','https:',$importer['poco']);
132         }
133
134         if($ssl_changed) {
135                 q("update contact set 
136                         url = '%s', 
137                         nurl = '%s',
138                         photo = '%s',
139                         thumb = '%s',
140                         micro = '%s',
141                         request = '%s',
142                         notify = '%s',
143                         poll = '%s',
144                         confirm = '%s',
145                         poco = '%s'
146                         where id = %d limit 1",
147                         dbesc($importer['url']),
148                         dbesc($importer['nurl']),
149                         dbesc($importer['photo']),
150                         dbesc($importer['thumb']),
151                         dbesc($importer['micro']),
152                         dbesc($importer['request']),
153                         dbesc($importer['notify']),
154                         dbesc($importer['poll']),
155                         dbesc($importer['confirm']),
156                         dbesc($importer['poco']),
157                         intval($importer['id'])
158                 );
159         }
160                         
161         logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
162         logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
163
164         if($dissolve == 1) {
165
166                 /**
167                  * Relationship is dissolved permanently
168                  */
169
170                 require_once('include/Contact.php'); 
171                 contact_remove($importer['id']);
172                 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
173                 xml_status(0);
174
175         }
176
177         if(strlen($key)) {
178                 $rawkey = hex2bin(trim($key));
179                 logger('rino: md5 raw key: ' . md5($rawkey));
180                 $final_key = '';
181
182                 if($dfrn_version >= 2.1) {
183                         if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
184                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
185                         }
186                         else {
187                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
188                         }
189                 }
190                 else {
191                         if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
192                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
193                         }
194                         else {
195                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
196                         }
197                 }
198
199                 logger('rino: received key : ' . $final_key);
200                 $data = aes_decrypt(hex2bin($data),$final_key);
201                 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
202         }
203
204
205         $ret = local_delivery($importer,$data);
206         xml_status($ret);
207
208         // NOTREACHED
209 }
210
211
212 function dfrn_notify_content(&$a) {
213
214         if(x($_GET,'dfrn_id')) {
215
216                 // initial communication from external contact, $direction is their direction.
217                 // If this is a duplex communication, ours will be the opposite.
218
219                 $dfrn_id = notags(trim($_GET['dfrn_id']));
220                 $dfrn_version = (float) $_GET['dfrn_version'];
221
222                 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
223
224                 $direction = (-1);
225                 if(strpos($dfrn_id,':') == 1) {
226                         $direction = intval(substr($dfrn_id,0,1));
227                         $dfrn_id = substr($dfrn_id,2);
228                 }
229
230                 $hash = random_string();
231
232                 $status = 0;
233
234                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
235
236                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
237                         VALUES( '%s', '%s', %d ) ",
238                         dbesc($hash),
239                         dbesc($dfrn_id),
240                         intval(time() + 90 )
241                 );
242
243                 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
244
245                 $sql_extra = '';
246                 switch($direction) {
247                         case (-1):
248                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
249                                 $my_id = $dfrn_id;
250                                 break;
251                         case 0:
252                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
253                                 $my_id = '1:' . $dfrn_id;
254                                 break;
255                         case 1:
256                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
257                                 $my_id = '0:' . $dfrn_id;
258                                 break;
259                         default:
260                                 $status = 1;
261                                 break; // NOTREACHED
262                 }
263
264                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
265                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' 
266                                 AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
267                                 dbesc($a->argv[1])
268                 );
269
270                 if(! count($r))
271                         $status = 1;
272
273                 $challenge = '';
274                 $encrypted_id = '';
275                 $id_str = $my_id . '.' . mt_rand(1000,9999);
276
277                 $prv_key = trim($r[0]['prvkey']);
278                 $pub_key = trim($r[0]['pubkey']);
279                 $dplx = intval($r[0]['duplex']);
280
281                 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
282                         openssl_private_encrypt($hash,$challenge,$prv_key);
283                         openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
284                 }
285                 elseif(strlen($pub_key)) {
286                         openssl_public_encrypt($hash,$challenge,$pub_key);
287                         openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
288                 }
289                 else
290                         $status = 1;
291
292                 $challenge    = bin2hex($challenge);
293                 $encrypted_id = bin2hex($encrypted_id);
294
295                 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
296
297                 $rino_enable = get_config('system','rino_encrypt');
298
299                 if(! $rino_enable)
300                         $rino = 0;
301
302
303                 header("Content-type: text/xml");
304
305                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
306                         . '<dfrn_notify>' . "\r\n"
307                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
308                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
309                         . "\t" . '<rino>' . $rino . '</rino>' . "\r\n" 
310                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
311                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
312                         . '</dfrn_notify>' . "\r\n" ;
313
314                 killme();
315         }
316
317 }