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