@@ -65,11 +65,17 @@ |
||
| 65 | 65 | { |
| 66 | 66 | http_response_code($this->code ?? 200); |
| 67 | 67 | |
| 68 | - if ($this->json) header('Content-Type: application/json'); |
|
| 68 | + if ($this->json) { |
|
| 69 | + header('Content-Type: application/json'); |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | - if ($this->data !== null) echo !$this->json ? $this->data : json_encode($this->data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); |
|
| 72 | + if ($this->data !== null) { |
|
| 73 | + echo !$this->json ? $this->data : json_encode($this->data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); |
|
| 74 | + } |
|
| 71 | 75 | |
| 72 | - if ($exit) exit(EXIT_SUCCESS); |
|
| 76 | + if ($exit) { |
|
| 77 | + exit(EXIT_SUCCESS); |
|
| 78 | + } |
|
| 73 | 79 | } |
| 74 | 80 | } |
| 75 | 81 | ?> |
@@ -115,7 +115,9 @@ discard block |
||
| 115 | 115 | { |
| 116 | 116 | $this->ci =& get_instance(); |
| 117 | 117 | |
| 118 | - if ($this->ci->input->is_cli_request()) return; |
|
| 118 | + if ($this->ci->input->is_cli_request()) { |
|
| 119 | + return; |
|
| 120 | + } |
|
| 119 | 121 | |
| 120 | 122 | // Load Config If Exists. |
| 121 | 123 | $this->ci->config->load('rest', true, true); |
@@ -217,7 +219,10 @@ discard block |
||
| 217 | 219 | } |
| 218 | 220 | |
| 219 | 221 | //$auths = $this->ci->config->item('rest')['uri_auth'][uri_string()] ?? null; |
| 220 | - if ($auths == null) return; // No authentication(s) to carry out. |
|
| 222 | + if ($auths == null) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + // No authentication(s) to carry out. |
|
| 221 | 226 | |
| 222 | 227 | // $this->process_auth() terminates the script if authentication fails |
| 223 | 228 | // It will call the callable in the rest.php config file under |
@@ -229,7 +234,9 @@ discard block |
||
| 229 | 234 | //return; |
| 230 | 235 | //} |
| 231 | 236 | |
| 232 | - foreach ($auths as $auth) $this->process_auth($auth); |
|
| 237 | + foreach ($auths as $auth) { |
|
| 238 | + $this->process_auth($auth); |
|
| 239 | + } |
|
| 233 | 240 | } |
| 234 | 241 | /** |
| 235 | 242 | * [process_auth description] |
@@ -283,15 +290,23 @@ discard block |
||
| 283 | 290 | private function basic_auth():void { |
| 284 | 291 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
| 285 | 292 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
| 286 | - if (!$username || !$password) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
|
| 287 | - if (!$this->rest_model->basicAuth($this, $username, $password)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
|
| 293 | + if (!$username || !$password) { |
|
| 294 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
| 295 | + } |
|
| 296 | + // Exits. |
|
| 297 | + if (!$this->rest_model->basicAuth($this, $username, $password)) { |
|
| 298 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
| 299 | + } |
|
| 300 | + // Exits. |
|
| 288 | 301 | } |
| 289 | 302 | /** |
| 290 | 303 | * [api_key_auth description] |
| 291 | 304 | */ |
| 292 | 305 | private function api_key_auth():void |
| 293 | 306 | { |
| 294 | - if (uri_string() == '') return; |
|
| 307 | + if (uri_string() == '') { |
|
| 308 | + return; |
|
| 309 | + } |
|
| 295 | 310 | |
| 296 | 311 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true)) { |
| 297 | 312 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
@@ -351,7 +366,9 @@ discard block |
||
| 351 | 366 | // Trunctate Rate Limit Data. |
| 352 | 367 | $this->rest_model->truncateRatelimitData(); |
| 353 | 368 | // Check Whitelist. |
| 354 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
| 369 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
| 370 | + return; |
|
| 371 | + } |
|
| 355 | 372 | // Should we acyually Limit? |
| 356 | 373 | if ($this->ip_per_hour > 0) { |
| 357 | 374 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -431,7 +448,9 @@ discard block |
||
| 431 | 448 | if (isset($this->ci->config->item('rest')['response_callbacks'][$code])) { |
| 432 | 449 | $this->ci->config->item('rest')['response_callbacks'][$code]($auth); |
| 433 | 450 | } |
| 434 | - if (ENVIRONMENT != 'testing') exit($code); |
|
| 451 | + if (ENVIRONMENT != 'testing') { |
|
| 452 | + exit($code); |
|
| 453 | + } |
|
| 435 | 454 | throw new Exception("Error $code in $auth", $code); |
| 436 | 455 | } |
| 437 | 456 | } |
@@ -81,10 +81,14 @@ discard block |
||
| 81 | 81 | $this->db->or_where($this->users_username_column, $username); |
| 82 | 82 | } |
| 83 | 83 | $query = $this->db->get(); |
| 84 | - if ($query->num_rows() == 0) return false; |
|
| 84 | + if ($query->num_rows() == 0) { |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 85 | 87 | // Authenticate. |
| 86 | 88 | if (password_verify($password, $query->result()[0]->{$this->users_password_column})) { |
| 87 | - if ($this->users_id_column != null) $context->userId = $query->result()[0]->{$this->users_id_column}; |
|
| 89 | + if ($this->users_id_column != null) { |
|
| 90 | + $context->userId = $query->result()[0]->{$this->users_id_column}; |
|
| 91 | + } |
|
| 88 | 92 | return true; |
| 89 | 93 | } |
| 90 | 94 | return false; |
@@ -97,7 +101,9 @@ discard block |
||
| 97 | 101 | */ |
| 98 | 102 | public function getAPIKeyData(string $apiKey):?object { |
| 99 | 103 | // Preliminary Check. |
| 100 | - if ($this->api_key_table == null || $this->api_key_column == null) return null; |
|
| 104 | + if ($this->api_key_table == null || $this->api_key_column == null) { |
|
| 105 | + return null; |
|
| 106 | + } |
|
| 101 | 107 | // Query. |
| 102 | 108 | $this->db->from($this->api_key_table); |
| 103 | 109 | $this->db->where($this->api_key_column, $apiKey); |
@@ -122,7 +128,9 @@ discard block |
||
| 122 | 128 | public function getLimitData(string $client, string $group):?array { |
| 123 | 129 | $sql = 'SELECT count, start, (`start` + INTERVAL (1 - TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP(), NOW())) HOUR) AS reset_epoch FROM rest_api_rate_limit WHERE client = ? AND _group = ?'; |
| 124 | 130 | $query = $this->db->query($sql, [$client, $group]); |
| 125 | - if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0]; |
|
| 131 | + if (!is_scalar($query) && $query->num_rows() > 0) { |
|
| 132 | + return $query->result_array()[0]; |
|
| 133 | + } |
|
| 126 | 134 | return null; |
| 127 | 135 | } |
| 128 | 136 | /** |