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