@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | * @param $file |
23 | 23 | * @param $limitPerMinute |
24 | 24 | */ |
25 | - public function __construct($file, $limitPerMinute){ |
|
25 | + public function __construct($file, $limitPerMinute) { |
|
26 | 26 | $this->db = new \SQLite3($file); |
27 | 27 | |
28 | 28 | $result = $this->db->query('SELECT name FROM sqlite_master'); |
29 | - if(!($row = $result->fetchArray()) || $row['name'] != 'throttle'){ |
|
29 | + if (!($row = $result->fetchArray()) || $row['name'] != 'throttle') { |
|
30 | 30 | $this->db->exec('CREATE TABLE throttle (key STRING, count NUMBER, lastMinute NUMBER)'); |
31 | 31 | } |
32 | 32 | $this->limit = $limitPerMinute; |
@@ -42,11 +42,11 @@ discard block |
||
42 | 42 | * @param $key |
43 | 43 | * @return bool |
44 | 44 | */ |
45 | - protected function genericThrottle($key){ |
|
45 | + protected function genericThrottle($key) { |
|
46 | 46 | $minute = ceil(time() / 60); |
47 | - if($result = $this->db->querySingle('SELECT key, count, lastMinute FROM throttle WHERE key = ' . \SQLite3::escapeString($key), true)) { |
|
48 | - if($result['lastMinute'] == $minute){ |
|
49 | - if($result['count'] + 1 <= $this->limit){ |
|
47 | + if ($result = $this->db->querySingle('SELECT key, count, lastMinute FROM throttle WHERE key = ' . \SQLite3::escapeString($key), true)) { |
|
48 | + if ($result['lastMinute'] == $minute) { |
|
49 | + if ($result['count'] + 1 <= $this->limit) { |
|
50 | 50 | $this->db->query('UPDATE throttle SET count = ' . ($result['count'] + 1) . ' WHERE key = ' . \SQLite3::escapeString($key)); |
51 | 51 | } else { |
52 | 52 | return true; |
@@ -7,7 +7,7 @@ |
||
7 | 7 | class phonenumbers extends Endpoint { |
8 | 8 | |
9 | 9 | public function get(){ |
10 | - $geoPhone = new GeoPhone('data.csv'); |
|
11 | - return $geoPhone->lookupNumber($this->request->getInstance()); |
|
12 | - } |
|
10 | + $geoPhone = new GeoPhone('data.csv'); |
|
11 | + return $geoPhone->lookupNumber($this->request->getInstance()); |
|
12 | + } |
|
13 | 13 | } |
14 | 14 | \ No newline at end of file |
@@ -6,7 +6,7 @@ |
||
6 | 6 | |
7 | 7 | class phonenumbers extends Endpoint { |
8 | 8 | |
9 | - public function get(){ |
|
9 | + public function get() { |
|
10 | 10 | $geoPhone = new GeoPhone('data.csv'); |
11 | 11 | return $geoPhone->lookupNumber($this->request->getInstance()); |
12 | 12 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | /** |
15 | 15 | * @param string $filename |
16 | 16 | */ |
17 | - public function __construct($filename = 'data.csv'){ |
|
17 | + public function __construct($filename = 'data.csv') { |
|
18 | 18 | $this->fileHandle = fopen($filename, 'r'); |
19 | 19 | } |
20 | 20 | |
@@ -23,13 +23,13 @@ discard block |
||
23 | 23 | * @return array an array with one value keyed with "location". The value is an array with two keys "city" and "state". |
24 | 24 | * City and state are empty when not found. |
25 | 25 | */ |
26 | - public function lookupNumber($phoneNumber){ |
|
26 | + public function lookupNumber($phoneNumber) { |
|
27 | 27 | $phoneNumber = preg_replace('/[^0-9]/s', '', $phoneNumber); |
28 | 28 | $areaCode = substr($phoneNumber, 0, 3); |
29 | 29 | $nextThree = substr($phoneNumber, 3, 3); |
30 | 30 | |
31 | 31 | while (($data = fgetcsv($this->fileHandle)) !== FALSE) { |
32 | - if($areaCode == $data[0] && $nextThree == $data[1]){ |
|
32 | + if ($areaCode == $data[0] && $nextThree == $data[1]) { |
|
33 | 33 | return ["location" => |
34 | 34 | [ |
35 | 35 | 'city' => $data[2], |
@@ -5,6 +5,6 @@ |
||
5 | 5 | |
6 | 6 | class helloworld extends Endpoint { |
7 | 7 | public function getAll(){ |
8 | - return ["helloworld" => "hello world"]; |
|
9 | - } |
|
8 | + return ["helloworld" => "hello world"]; |
|
9 | + } |
|
10 | 10 | } |
11 | 11 | \ No newline at end of file |
@@ -4,7 +4,7 @@ |
||
4 | 4 | use LunixREST\Endpoints\Endpoint; |
5 | 5 | |
6 | 6 | class helloworld extends Endpoint { |
7 | - public function getAll(){ |
|
7 | + public function getAll() { |
|
8 | 8 | return ["helloworld" => "hello world"]; |
9 | 9 | } |
10 | 10 | } |
11 | 11 | \ No newline at end of file |
@@ -21,7 +21,7 @@ |
||
21 | 21 | header('404 Not Found', 404); |
22 | 22 | } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
23 | 23 | header('403 Access Denied', 403); |
24 | - } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
24 | + } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
25 | 25 | header('429 Too Many Requests', 429); |
26 | 26 | } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
27 | 27 | header('500 Internal Server Error', 500); |
@@ -9,27 +9,27 @@ |
||
9 | 9 | $router = new \LunixREST\Router\Router($accessControl, $throttle, $formatsConfig, "HelloWorld"); |
10 | 10 | |
11 | 11 | try { |
12 | - $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1.0/public/helloworld.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
12 | + $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1.0/public/helloworld.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
13 | 13 | |
14 | - try { |
|
15 | - echo $router->handle($request); |
|
16 | - } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
17 | - header('400 Bad Request', true, 400); |
|
18 | - } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
19 | - header('404 Not Found', true, 404); |
|
20 | - } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
21 | - header('404 Not Found', true, 404); |
|
22 | - } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
23 | - header('403 Access Denied', true, 403); |
|
24 | - } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
25 | - header('429 Too Many Requests', true, 429); |
|
26 | - } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
27 | - header('500 Internal Server Error', true, 500); |
|
28 | - } catch(Exception $e){ |
|
29 | - header('500 Internal Server Error', true, 500); |
|
30 | - } |
|
14 | + try { |
|
15 | + echo $router->handle($request); |
|
16 | + } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
17 | + header('400 Bad Request', true, 400); |
|
18 | + } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
19 | + header('404 Not Found', true, 404); |
|
20 | + } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
21 | + header('404 Not Found', true, 404); |
|
22 | + } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
23 | + header('403 Access Denied', true, 403); |
|
24 | + } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
25 | + header('429 Too Many Requests', true, 429); |
|
26 | + } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
27 | + header('500 Internal Server Error', true, 500); |
|
28 | + } catch(Exception $e){ |
|
29 | + header('500 Internal Server Error', true, 500); |
|
30 | + } |
|
31 | 31 | } catch(\LunixREST\Exceptions\InvalidRequestFormatException $e){ |
32 | - header('400 Bad Request', true, 400); |
|
32 | + header('400 Bad Request', true, 400); |
|
33 | 33 | } catch(Exception $e){ |
34 | - header('500 Internal Server Error', true, 500); |
|
34 | + header('500 Internal Server Error', true, 500); |
|
35 | 35 | } |
@@ -9,27 +9,27 @@ |
||
9 | 9 | $router = new \LunixREST\Router\Router($accessControl, $throttle, $formatsConfig, "HelloWorld"); |
10 | 10 | |
11 | 11 | try { |
12 | - $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1.0/public/helloworld.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
12 | + $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1.0/public/helloworld.json"); // \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
13 | 13 | |
14 | 14 | try { |
15 | 15 | echo $router->handle($request); |
16 | - } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
16 | + } catch (\LunixREST\Exceptions\InvalidAPIKeyException $e) { |
|
17 | 17 | header('400 Bad Request', true, 400); |
18 | - } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
18 | + } catch (\LunixREST\Exceptions\UnknownEndpointException $e) { |
|
19 | 19 | header('404 Not Found', true, 404); |
20 | - } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
20 | + } catch (\LunixREST\Exceptions\UnknownResponseFormatException $e) { |
|
21 | 21 | header('404 Not Found', true, 404); |
22 | - } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
22 | + } catch (\LunixREST\Exceptions\AccessDeniedException $e) { |
|
23 | 23 | header('403 Access Denied', true, 403); |
24 | - } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
24 | + } catch (\LunixREST\Exceptions\ThrottleLimitExceededException $e) { |
|
25 | 25 | header('429 Too Many Requests', true, 429); |
26 | - } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
26 | + } catch (\LunixREST\Exceptions\InvalidResponseFormatException $e) { |
|
27 | 27 | header('500 Internal Server Error', true, 500); |
28 | - } catch(Exception $e){ |
|
28 | + } catch (Exception $e) { |
|
29 | 29 | header('500 Internal Server Error', true, 500); |
30 | 30 | } |
31 | -} catch(\LunixREST\Exceptions\InvalidRequestFormatException $e){ |
|
31 | +} catch (\LunixREST\Exceptions\InvalidRequestFormatException $e) { |
|
32 | 32 | header('400 Bad Request', true, 400); |
33 | -} catch(Exception $e){ |
|
33 | +} catch (Exception $e) { |
|
34 | 34 | header('500 Internal Server Error', true, 500); |
35 | 35 | } |
@@ -17,56 +17,56 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @param Request $request |
19 | 19 | */ |
20 | - public function __construct(Request $request){ |
|
20 | + public function __construct(Request $request) { |
|
21 | 21 | $this->request = $request; |
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @return array |
26 | 26 | */ |
27 | - public function get(){ |
|
27 | + public function get() { |
|
28 | 28 | $this->unsupportedMethod(); |
29 | 29 | } |
30 | 30 | |
31 | 31 | /** |
32 | 32 | * @return array |
33 | 33 | */ |
34 | - public function getAll(){ |
|
34 | + public function getAll() { |
|
35 | 35 | $this->unsupportedMethod(); |
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
39 | 39 | * @return array |
40 | 40 | */ |
41 | - public function post(){ |
|
41 | + public function post() { |
|
42 | 42 | $this->unsupportedMethod(); |
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @return array |
47 | 47 | */ |
48 | - public function postAll(){ |
|
48 | + public function postAll() { |
|
49 | 49 | $this->unsupportedMethod(); |
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * @return array |
54 | 54 | */ |
55 | - public function put(){ |
|
55 | + public function put() { |
|
56 | 56 | $this->unsupportedMethod(); |
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
60 | 60 | * @return array |
61 | 61 | */ |
62 | - public function putAll(){ |
|
62 | + public function putAll() { |
|
63 | 63 | $this->unsupportedMethod(); |
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
67 | 67 | * @return array |
68 | 68 | */ |
69 | - public function options(){ |
|
69 | + public function options() { |
|
70 | 70 | $this->unsupportedMethod(); |
71 | 71 | } |
72 | 72 | |
@@ -74,25 +74,25 @@ discard block |
||
74 | 74 | /** |
75 | 75 | * @return array |
76 | 76 | */ |
77 | - public function optionsAll(){ |
|
77 | + public function optionsAll() { |
|
78 | 78 | $this->unsupportedMethod(); |
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
82 | 82 | * @return array |
83 | 83 | */ |
84 | - public function delete(){ |
|
84 | + public function delete() { |
|
85 | 85 | $this->unsupportedMethod(); |
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
89 | 89 | * @return array |
90 | 90 | */ |
91 | - public function deleteAll(){ |
|
91 | + public function deleteAll() { |
|
92 | 92 | $this->unsupportedMethod(); |
93 | 93 | } |
94 | 94 | |
95 | - private function unsupportedMethod(){ |
|
95 | + private function unsupportedMethod() { |
|
96 | 96 | throw new UnknownEndpointException('Method not supported'); |
97 | 97 | } |
98 | 98 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param string $extension |
58 | 58 | * @param string $instance |
59 | 59 | */ |
60 | - public function __construct($method, array $headers, array $data, $ip, $version, $apiKey, $endpoint, $extension, $instance = null){ |
|
60 | + public function __construct($method, array $headers, array $data, $ip, $version, $apiKey, $endpoint, $extension, $instance = null) { |
|
61 | 61 | $this->method = strtolower($method); |
62 | 62 | $this->headers = $headers; |
63 | 63 | $this->data = $data; |
@@ -150,9 +150,9 @@ discard block |
||
150 | 150 | * @return Request |
151 | 151 | * @throws InvalidRequestFormatException |
152 | 152 | */ |
153 | - public static function createFromURL($method, array $headers, array $data, $ip, $url){ |
|
153 | + public static function createFromURL($method, array $headers, array $data, $ip, $url) { |
|
154 | 154 | $splitURL = explode('/', trim($url, '/')); |
155 | - if(count($splitURL) < 3){ |
|
155 | + if (count($splitURL) < 3) { |
|
156 | 156 | throw new InvalidRequestFormatException(); |
157 | 157 | } |
158 | 158 | //Find endpoint |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | |
166 | 166 | $instance = null; |
167 | 167 | |
168 | - if(count($splitURL) == 4){ |
|
168 | + if (count($splitURL) == 4) { |
|
169 | 169 | $instance = implode('.', $splitExtension); |
170 | 170 | } else { |
171 | 171 | $endpoint = implode('.', $splitExtension); |
@@ -10,29 +10,29 @@ |
||
10 | 10 | $router = new \LunixREST\Router\Router($accessControl, $throttle, $formatsConfig, "GeoPhone"); |
11 | 11 | |
12 | 12 | try { |
13 | - $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1/123456/phonenumbers/6517855237.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
13 | + $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1/123456/phonenumbers/6517855237.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
14 | 14 | |
15 | - try { |
|
16 | - echo $router->handle($request); |
|
17 | - } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
18 | - header('400 Bad Request', true, 400); |
|
19 | - } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
20 | - header('404 Not Found', true, 404); |
|
21 | - } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
22 | - header('404 Not Found', true, 404); |
|
23 | - } catch(\LunixREST\Exceptions\InstanceNotFoundException $e){ |
|
24 | - header('404 Not Found', true, 404); |
|
25 | - } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
26 | - header('403 Access Denied', true, 403); |
|
27 | - } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
28 | - header('429 Too Many Requests', true, 429); |
|
29 | - } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
30 | - header('500 Internal Server Error', true, 500); |
|
31 | - } catch(Exception $e){ |
|
32 | - header('500 Internal Server Error', true, 500); |
|
33 | - } |
|
15 | + try { |
|
16 | + echo $router->handle($request); |
|
17 | + } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
18 | + header('400 Bad Request', true, 400); |
|
19 | + } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
20 | + header('404 Not Found', true, 404); |
|
21 | + } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
22 | + header('404 Not Found', true, 404); |
|
23 | + } catch(\LunixREST\Exceptions\InstanceNotFoundException $e){ |
|
24 | + header('404 Not Found', true, 404); |
|
25 | + } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
26 | + header('403 Access Denied', true, 403); |
|
27 | + } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
28 | + header('429 Too Many Requests', true, 429); |
|
29 | + } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
30 | + header('500 Internal Server Error', true, 500); |
|
31 | + } catch(Exception $e){ |
|
32 | + header('500 Internal Server Error', true, 500); |
|
33 | + } |
|
34 | 34 | } catch(\LunixREST\Exceptions\InvalidRequestFormatException $e){ |
35 | - header('400 Bad Request', 400); |
|
35 | + header('400 Bad Request', 400); |
|
36 | 36 | } catch(Exception $e){ |
37 | - header('500 Internal Server Error', true, 500); |
|
37 | + header('500 Internal Server Error', true, 500); |
|
38 | 38 | } |
@@ -10,29 +10,29 @@ |
||
10 | 10 | $router = new \LunixREST\Router\Router($accessControl, $throttle, $formatsConfig, "GeoPhone"); |
11 | 11 | |
12 | 12 | try { |
13 | - $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1/123456/phonenumbers/6517855237.json");// \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
13 | + $request = \LunixREST\Request\Request::createFromURL("GET", [], [], '127.0.0.1', "/1/123456/phonenumbers/6517855237.json"); // \LunixREST\Request\Request::createFromURL($_SERVER['REQUEST_METHOD'], getallheaders(), $_REQUEST, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']); |
|
14 | 14 | |
15 | 15 | try { |
16 | 16 | echo $router->handle($request); |
17 | - } catch(\LunixREST\Exceptions\InvalidAPIKeyException $e){ |
|
17 | + } catch (\LunixREST\Exceptions\InvalidAPIKeyException $e) { |
|
18 | 18 | header('400 Bad Request', true, 400); |
19 | - } catch(\LunixREST\Exceptions\UnknownEndpointException $e){ |
|
19 | + } catch (\LunixREST\Exceptions\UnknownEndpointException $e) { |
|
20 | 20 | header('404 Not Found', true, 404); |
21 | - } catch(\LunixREST\Exceptions\UnknownResponseFormatException $e){ |
|
21 | + } catch (\LunixREST\Exceptions\UnknownResponseFormatException $e) { |
|
22 | 22 | header('404 Not Found', true, 404); |
23 | - } catch(\LunixREST\Exceptions\InstanceNotFoundException $e){ |
|
23 | + } catch (\LunixREST\Exceptions\InstanceNotFoundException $e) { |
|
24 | 24 | header('404 Not Found', true, 404); |
25 | - } catch(\LunixREST\Exceptions\AccessDeniedException $e){ |
|
25 | + } catch (\LunixREST\Exceptions\AccessDeniedException $e) { |
|
26 | 26 | header('403 Access Denied', true, 403); |
27 | - } catch(\LunixREST\Exceptions\ThrottleLimitExceededException $e){ |
|
27 | + } catch (\LunixREST\Exceptions\ThrottleLimitExceededException $e) { |
|
28 | 28 | header('429 Too Many Requests', true, 429); |
29 | - } catch(\LunixREST\Exceptions\InvalidResponseFormatException $e){ |
|
29 | + } catch (\LunixREST\Exceptions\InvalidResponseFormatException $e) { |
|
30 | 30 | header('500 Internal Server Error', true, 500); |
31 | - } catch(Exception $e){ |
|
31 | + } catch (Exception $e) { |
|
32 | 32 | header('500 Internal Server Error', true, 500); |
33 | 33 | } |
34 | -} catch(\LunixREST\Exceptions\InvalidRequestFormatException $e){ |
|
34 | +} catch (\LunixREST\Exceptions\InvalidRequestFormatException $e) { |
|
35 | 35 | header('400 Bad Request', 400); |
36 | -} catch(Exception $e){ |
|
36 | +} catch (Exception $e) { |
|
37 | 37 | header('500 Internal Server Error', true, 500); |
38 | 38 | } |