Improved www.ttss.krakow.pl
Jacek Kowalski
2017-05-14 434151b5b3368920dc7add2a28d8da172cb70060
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',
9dd3d1 76     '/internetservice/geoserviceDispatcher/services/pathinfo/vehicle' => '/geoserviceDispatcher/services/pathinfo/vehicle',
68aeb4 77 ];
896879 78
JK 79 $path = $_SERVER['PATH_INFO'];
80
68aeb4 81 if(isset($rewrite[$path])) {
JK 82     $path = $rewrite[$path];
83 }
84
896879 85 if(!isset($method[$path])) {
JK 86     header('HTTP/1.1 403 Forbidden');
87     die('Forbidden');
88 }
89
90 $parameters = [];
91
92 foreach($method[$path] as $name => $filter) {
93     if(!isset($_GET[$name])) {
94         header('HTTP/1.1 403 Forbidden');
95         die('Parameter '.$name.' is required');
96     }
97     
98     if(!$filter($_GET[$name])) {
99         header('HTTP/1.1 403 Forbidden');
100         die('Parameter '.$name.' has invalid value');
101     }
102     
103     $parameters[$name] = $_GET[$name];
104 }
105
106 $result = @file_get_contents($base_proxy . $path . '?' . http_build_query($parameters));
107 if(!$result OR $http_response_header[0] != 'HTTP/1.1 200 OK') {
108     header('HTTP/1.1 503 Service Unavailable');
109     if(isset($http_response_header[0])) {
110         die($http_response_header[0]);
111     } else {
112         die('Unknown error');
113     }
114 }
115
116 header('Content-Type: application/json');
117 echo $result;