]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
84c249a58ddfc42a72441de009bc18829475ced6
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 require_once('include/items.php');
4 require_once('include/auth.php');
5
6
7 function dfrn_poll_init(&$a) {
8
9         $dfrn_id = '';
10
11         if(x($_GET,'dfrn_id'))
12                 $dfrn_id = $_GET['dfrn_id'];
13         if(x($_GET,'type'))
14                 $type = $_GET['type'];
15         if(x($_GET,'last_update'))
16                 $last_update = $_GET['last_update'];
17         $dfrn_version    = (float) $_GET['dfrn_version'] ;
18         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
19
20
21         $direction = (-1);
22
23
24         if(strpos($dfrn_id,':') == 1) {
25                 $direction = intval(substr($dfrn_id,0,1));
26                 $dfrn_id = substr($dfrn_id,2);
27         }
28
29         if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
30                 header("Content-type: application/atom+xml");
31                 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
32                 echo $o;
33                 killme();
34         }
35
36         if((x($type)) && ($type === 'profile')) {
37
38                 $sql_extra = '';
39                 switch($direction) {
40                         case (-1):
41                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
42                                 $my_id = $dfrn_id;
43                                 break;
44                         case 0:
45                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46                                 $my_id = '1:' . $dfrn_id;
47                                 break;
48                         case 1:
49                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50                                 $my_id = '0:' . $dfrn_id;
51                                 break;
52                         default:
53                                 goaway($a->get_baseurl());
54                                 break; // NOTREACHED
55                 }
56
57                 $r = q("SELECT `contact`.*, `user`.`nickname` 
58                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
60                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
61                         dbesc($a->argv[1])
62                 );
63                 
64                 if(count($r)) {
65
66                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
67
68                         if(strlen($s)) {
69
70                                 $xml = simplexml_load_string($s);
71
72                                 if((int) $xml->status == 1) {
73                                         $_SESSION['authenticated'] = 1;
74                                         $_SESSION['visitor_id'] = $r[0]['id'];
75                                         notice( t('Hi ') . $r[0]['name'] . EOL);
76                                         // Visitors get 1 day session.
77                                         $session_id = session_id();
78                                         $expire = time() + 86400;
79                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
80                                                 dbesc($expire),
81                                                 dbesc($session_id)
82                                         ); 
83                                 }
84                         }
85                         $profile = $r[0]['nickname'];
86                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
87                 }
88                 goaway($a->get_baseurl());
89
90         }
91
92         if((x($type)) && ($type === 'profile-check')) {
93
94                 switch($direction) {
95                         case 1:
96                                 $dfrn_id = '0:' . $dfrn_id;
97                                 break;
98                         case 0:
99                                 $dfrn_id = '1:' . $dfrn_id;
100                                 break;
101                         default:
102                                 break;
103                 }
104                 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
105                 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
106                         dbesc($dfrn_id));
107                 if(count($r)) {
108                         xml_status(1);
109                         return; // NOTREACHED
110                 }
111                 xml_status(0);
112                 return; // NOTREACHED
113         }
114
115
116 }
117
118
119
120 function dfrn_poll_post(&$a) {
121
122         $dfrn_id      = $_POST['dfrn_id'];
123         $challenge    = $_POST['challenge'];
124         $url          = $_POST['url'];
125         $dfrn_version = (float) $_POST['dfrn_version'];
126
127         $direction    = (-1);
128         if(strpos($dfrn_id,':') == 1) {
129                 $direction = intval(substr($dfrn_id,0,1));
130                 $dfrn_id = substr($dfrn_id,2);
131         }
132
133
134         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
135                 dbesc($dfrn_id),
136                 dbesc($challenge)
137         );
138
139         if(! count($r))
140                 killme();
141
142         $type = $r[0]['type'];
143         $last_update = $r[0]['last_update'];
144
145         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
146                 dbesc($dfrn_id),
147                 dbesc($challenge)
148         );
149
150
151         $sql_extra = '';
152         switch($direction) {
153                 case (-1):
154                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
155                         $my_id = $dfrn_id;
156                         break;
157                 case 0:
158                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
159                         $my_id = '1:' . $dfrn_id;
160                         break;
161                 case 1:
162                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
163                         $my_id = '0:' . $dfrn_id;
164                         break;
165                 default:
166                         goaway($a->get_baseurl());
167                         break; // NOTREACHED
168         }
169
170
171         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
172
173
174         if(! count($r))
175                 killme();
176
177         $owner_uid = $r[0]['uid'];
178         $contact_id = $r[0]['id']; 
179
180
181         if($type === 'reputation' && strlen($url)) {
182                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
183                         dbesc($url),
184                         intval($owner_uid)
185                 );
186                 $reputation = 0;
187                 $text = '';
188
189                 if(count($r)) {
190                         $reputation = $r[0]['rating'];
191                         $text = $r[0]['reason'];
192
193                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
194                                 $reputation = 0;
195                                 $text = '';
196                         }
197                 }
198
199                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
200                 <reputation>
201                         <url>$url</url>
202                         <rating>$reputation</rating>
203                         <description>$text</description>
204                 </reputation>
205                 ";
206                 killme();
207                 // NOTREACHED
208         }
209         else {
210                 header("Content-type: application/atom+xml");
211                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
212                 echo $o;
213                 killme();
214
215         }
216 }
217
218 function dfrn_poll_content(&$a) {
219
220
221         $dfrn_id = '';
222         $type = 'data';
223
224         if(x($_GET,'dfrn_id'))
225                 $dfrn_id = $_GET['dfrn_id'];
226         if(x($_GET,'type'))
227                 $type = $_GET['type'];
228         if(x($_GET,'last_update'))
229                 $last_update = $_GET['last_update'];
230
231         $dfrn_version = (float) $_GET['dfrn_version'];
232
233         $direction = (-1);
234         if(strpos($dfrn_id,':') == 1) {
235                 $direction = intval(substr($dfrn_id,0,1));
236                 $dfrn_id = substr($dfrn_id,2);
237         }
238
239
240         if($dfrn_id != '') {
241                 // initial communication from external contact
242                 $hash = random_string();
243
244                 $status = 0;
245
246                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
247
248                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
249                         VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
250                         dbesc($hash),
251                         dbesc($dfrn_id),
252                         intval(time() + 60 ),
253                         dbesc($type),
254                         dbesc($last_update)
255                 );
256
257
258                 $sql_extra = '';
259                 switch($direction) {
260                         case (-1):
261                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
262                                 $my_id = $dfrn_id;
263                                 break;
264                         case 0:
265                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
266                                 $my_id = '1:' . $dfrn_id;
267                                 break;
268                         case 1:
269                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
270                                 $my_id = '0:' . $dfrn_id;
271                                 break;
272                         default:
273                                 goaway($a->get_baseurl());
274                                 break; // NOTREACHED
275                 }
276
277
278
279
280                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
281
282                 if(count($r)) {
283
284                         $challenge = '';
285                         $encrypted_id = '';
286                         $id_str = $my_id . '.' . mt_rand(1000,9999);
287
288
289                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
290                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
291                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
292                         }
293                         else {
294                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
295                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
296                         }
297
298                         $challenge = bin2hex($challenge);
299                         $encrypted_id = bin2hex($encrypted_id);
300                 }
301                 else {
302                         $status = 1;
303                 }
304                 header("Content-type: text/xml");
305                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
306                         . '<dfrn_poll>' . "\r\n"
307                         . "\t" . '<status>' .$status . '</status>' . "\r\n"
308                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
309                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
310                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
311                         . '</dfrn_poll>' . "\r\n" ;
312                 killme();
313         }
314 }
315
316