Improved www.ttss.krakow.pl
Jacek Kowalski
2017-04-10 e5c5bd859034bcc838f007ce2ea75eaf15b63cd7
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     ],
e5c5bd 7     '/services/lookup/stopsByCharacter' => [
JK 8         'character' => 'ctype_alnum',
9     ],
68aeb4 10     '/services/passageInfo/stopPassages/stop' => [
e5c5bd 11         'stop' => 'ctype_digit',
896879 12         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
JK 13     ],
68aeb4 14     '/services/tripInfo/tripPassages' => [
JK 15         'tripId' => 'ctype_digit',
16         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
17         #'vehicleId' => 'ctype_digit',
18     ],
e5c5bd 19     '/geoserviceDispatcher/services/stopinfo/stopPoints' => [
JK 20         'left' => 'ctype_digit',
21         'bottom' => 'ctype_digit',
22         'right' => 'ctype_digit',
23         'top' => 'ctype_digit',
896879 24     ],
e5c5bd 25     '/geoserviceDispatcher/services/pathinfo/route' => [
JK 26         'id' => 'ctype_digit',
27         'direction' => 'ctype_digit',
28     ],
29     '/geoserviceDispatcher/services/pathinfo/vehicle' => [
30         'id' => 'ctype_digit',
31     ],
32     '/geoserviceDispatcher/services/vehicleinfo/vehicles' => [
33         // 'lastUpdate' => 'ctype_digit',
34         'positionType' => function($type) { return in_array($type, ['CORRECTED']); },
35         'colorType' => function($type) { return in_array($type, ['ROUTE_BASED']); },
36     ],
37     '/services/routeInfo/routeStops' => [
38         'routeId' => 'ctype_digit',
39     ],
40     '/services/stopInfo/stopPoint' => [
41         'stopPoint' => 'ctype_digit',
42     ],
43     '/services/passageInfo/stopPassages/stopPoint' => [
44         'stopPoint' => 'ctype_digit',
45         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
46         'startTime' => 'ctype_digit',
47         'timeFrame' => 'ctype_digit',
48     ],
49 ];
50 $rewrite = [
51     '/lookup/autocomplete/json' => '/services/lookup/autocomplete/json',
52     '/passageInfo/stopPassages/stop' => '/services/passageInfo/stopPassages/stop',
53     '/routeInfo/routeStops' => '/services/routeInfo/routeStops',
896879 54 ];
68aeb4 55 $rewrite = [
JK 56     '/lookup/autocomplete/json' => '/services/lookup/autocomplete/json',
57     '/passageInfo/stopPassages/stop' => '/services/passageInfo/stopPassages/stop',
58     '/routeInfo/routeStops' => '/services/routeInfo/routeStops',
59 ];
896879 60
JK 61 $path = $_SERVER['PATH_INFO'];
62
68aeb4 63 if(isset($rewrite[$path])) {
JK 64     $path = $rewrite[$path];
65 }
66
896879 67 if(!isset($method[$path])) {
JK 68     header('HTTP/1.1 403 Forbidden');
69     die('Forbidden');
70 }
71
72 $parameters = [];
73
74 foreach($method[$path] as $name => $filter) {
75     if(!isset($_GET[$name])) {
76         header('HTTP/1.1 403 Forbidden');
77         die('Parameter '.$name.' is required');
78     }
79     
80     if(!$filter($_GET[$name])) {
81         header('HTTP/1.1 403 Forbidden');
82         die('Parameter '.$name.' has invalid value');
83     }
84     
85     $parameters[$name] = $_GET[$name];
86 }
87
88 $result = @file_get_contents($base_proxy . $path . '?' . http_build_query($parameters));
89 if(!$result OR $http_response_header[0] != 'HTTP/1.1 200 OK') {
90     header('HTTP/1.1 503 Service Unavailable');
91     if(isset($http_response_header[0])) {
92         die($http_response_header[0]);
93     } else {
94         die('Unknown error');
95     }
96 }
97
98 header('Content-Type: application/json');
99 echo $result;