Improved www.ttss.krakow.pl
Jacek Kowalski
2019-02-09 a4d011116c35bb9bef8589c88ed38b3043f9e072
commit | author | age
eafc1c 1 <?php
JK 2 function is_number($str) {
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
15 $base_proxy = 'http://www.ttss.krakow.pl/internetservice';
16 $method = [
17     '/services/lookup/autocomplete/json' => [
18         'query' => function() { return TRUE; },
19     ],
20     '/services/passageInfo/stopPassages/stop' => [
21         'stop' => 'ctype_alnum',
22         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
23         #'startTime' => 'ctype_digit',
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',
31     ],
32     '/services/tripInfo/tripPassages' => [
33         'tripId' => 'ctype_digit',
34         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
35         #'vehicleId' => 'ctype_digit',
36     ],
37     '/services/routeInfo/routeStops' => [
38         'routeId' => 'ctype_alnum',
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',
c38aba 68         'positionType' => function($type) { return in_array($type, ['CORRECTED', 'RAW']); },
eafc1c 69         'colorType' => function($type) { return in_array($type, ['ROUTE_BASED']); },
JK 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',
76     '/internetservice/geoserviceDispatcher/services/pathinfo/vehicle' => '/geoserviceDispatcher/services/pathinfo/vehicle',
77 ];
78
79 $path = $_SERVER['PATH_INFO'];
80
81 if(isset($rewrite[$path])) {
82     $path = $rewrite[$path];
83 }
84
85 if(!isset($method[$path])) {
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;