Improved www.ttss.krakow.pl
Jacek Kowalski
2017-04-12 73da941644c4ee6cd03437815690c2cfaae4d958
commit | author | age
896879 1 <?php
68aeb4 2 $base_proxy = 'http://www.ttss.krakow.pl/internetservice';
896879 3 $method = [
68aeb4 4     '/services/lookup/autocomplete/json' => [
896879 5         'query' => function() { return TRUE; },
JK 6     ],
68aeb4 7     '/services/passageInfo/stopPassages/stop' => [
896879 8         'stop' => 'ctype_alnum',
JK 9         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
10     ],
68aeb4 11     '/services/tripInfo/tripPassages' => [
JK 12         'tripId' => 'ctype_digit',
13         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
14         #'vehicleId' => 'ctype_digit',
15     ],
16     '/services/routeInfo/routeStops' => [
896879 17         'routeId' => 'ctype_alnum'
JK 18     ],
19 ];
68aeb4 20 $rewrite = [
JK 21     '/lookup/autocomplete/json' => '/services/lookup/autocomplete/json',
22     '/passageInfo/stopPassages/stop' => '/services/passageInfo/stopPassages/stop',
23     '/routeInfo/routeStops' => '/services/routeInfo/routeStops',
24 ];
896879 25
JK 26 $path = $_SERVER['PATH_INFO'];
27
68aeb4 28 if(isset($rewrite[$path])) {
JK 29     $path = $rewrite[$path];
30 }
31
896879 32 if(!isset($method[$path])) {
JK 33     header('HTTP/1.1 403 Forbidden');
34     die('Forbidden');
35 }
36
37 $parameters = [];
38
39 foreach($method[$path] as $name => $filter) {
40     if(!isset($_GET[$name])) {
41         header('HTTP/1.1 403 Forbidden');
42         die('Parameter '.$name.' is required');
43     }
44     
45     if(!$filter($_GET[$name])) {
46         header('HTTP/1.1 403 Forbidden');
47         die('Parameter '.$name.' has invalid value');
48     }
49     
50     $parameters[$name] = $_GET[$name];
51 }
52
53 $result = @file_get_contents($base_proxy . $path . '?' . http_build_query($parameters));
54 if(!$result OR $http_response_header[0] != 'HTTP/1.1 200 OK') {
55     header('HTTP/1.1 503 Service Unavailable');
56     if(isset($http_response_header[0])) {
57         die($http_response_header[0]);
58     } else {
59         die('Unknown error');
60     }
61 }
62
63 header('Content-Type: application/json');
64 echo $result;