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