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