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