Improved www.ttss.krakow.pl
Jacek Kowalski
2019-06-21 2096bf0d3782a3a11e280e175afd5cfb873599d8
commit | author | age
896879 1 <?php
769858 2 if(!isset($base_proxy)) {
JK 3     echo 'This script is for inclusion only.';
4     die();
5 }
6
57b8d3 7 function is_number($str) {
JK 8     $str = (string)$str;
9
10     return
11         ctype_digit($str)
12         OR
13         (
14             substr($str, 0, 1) == '-'
15             AND
16             ctype_digit(substr($str, 1))
17         );
18 }
19
896879 20 $method = [
68aeb4 21     '/services/lookup/autocomplete/json' => [
896879 22         'query' => function() { return TRUE; },
JK 23     ],
68aeb4 24     '/services/passageInfo/stopPassages/stop' => [
896879 25         'stop' => 'ctype_alnum',
JK 26         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
57b8d3 27         #'startTime' => 'ctype_digit',
JK 28         #'timeFrame' => 'ctype_digit',
29     ],
30     '/services/passageInfo/stopPassages/stopPoint' => [
31         'stopPoint' => 'is_number',
32         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
33         #'startTime' => 'ctype_digit',
34         #'timeFrame' => 'ctype_digit',
896879 35     ],
68aeb4 36     '/services/tripInfo/tripPassages' => [
JK 37         'tripId' => 'ctype_digit',
38         'mode' => function($mode) { return in_array($mode, ['arrival', 'departure']); },
39         #'vehicleId' => 'ctype_digit',
40     ],
41     '/services/routeInfo/routeStops' => [
57b8d3 42         'routeId' => 'ctype_alnum',
JK 43     ],
44     '/services/stopInfo/stop' => [
45         'stop' => 'is_number',
46     ],
47     '/services/stopInfo/stopPoint' => [
48         'stopPoint' => 'is_number',
49     ],
50     
51     '/geoserviceDispatcher/services/stopinfo/stops' => [
52         'left' => 'is_number',
53         'bottom' => 'is_number',
54         'right' => 'is_number',
55         'top' => 'is_number',
56     ],
57     '/geoserviceDispatcher/services/stopinfo/stopPoints' => [
58         'left' => 'is_number',
59         'bottom' => 'is_number',
60         'right' => 'is_number',
61         'top' => 'is_number',
62     ],
63     '/geoserviceDispatcher/services/pathinfo/route' => [
64         'id' => 'is_number',
65         'direction' => 'is_number',
66     ],
67     '/geoserviceDispatcher/services/pathinfo/vehicle' => [
68         'id' => 'is_number',
69     ],
70     '/geoserviceDispatcher/services/vehicleinfo/vehicles' => [
71         'lastUpdate' => 'ctype_digit',
769858 72         'positionType' => function($type) { return in_array($type, ['CORRECTED', 'RAW']); },
57b8d3 73         'colorType' => function($type) { return in_array($type, ['ROUTE_BASED']); },
896879 74     ],
JK 75 ];
68aeb4 76 $rewrite = [
JK 77     '/lookup/autocomplete/json' => '/services/lookup/autocomplete/json',
78     '/passageInfo/stopPassages/stop' => '/services/passageInfo/stopPassages/stop',
79     '/routeInfo/routeStops' => '/services/routeInfo/routeStops',
9dd3d1 80     '/internetservice/geoserviceDispatcher/services/pathinfo/vehicle' => '/geoserviceDispatcher/services/pathinfo/vehicle',
68aeb4 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;