@@ -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 | ?> |
@@ -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 | /** |
@@ -141,7 +141,9 @@ discard block |
||
| 141 | 141 | { |
| 142 | 142 | $this->ci =& get_instance(); |
| 143 | 143 | |
| 144 | - if ($this->ci->input->is_cli_request()) return; |
|
| 144 | + if ($this->ci->input->is_cli_request()) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 145 | 147 | |
| 146 | 148 | // Load Config If Exists. |
| 147 | 149 | //$this->ci->config->load('rest', true, true); |
@@ -212,9 +214,14 @@ discard block |
||
| 212 | 214 | { |
| 213 | 215 | $auths = null; |
| 214 | 216 | $auths = $this->config['auth'] ?? null; |
| 215 | - if ($auths) $auths = is_array($auths) ? $auths : [$auths]; |
|
| 217 | + if ($auths) { |
|
| 218 | + $auths = is_array($auths) ? $auths : [$auths]; |
|
| 219 | + } |
|
| 216 | 220 | |
| 217 | - if (!$auths) return; // No authentication(s) to carry out. |
|
| 221 | + if (!$auths) { |
|
| 222 | + return; |
|
| 223 | + } |
|
| 224 | + // No authentication(s) to carry out. |
|
| 218 | 225 | |
| 219 | 226 | /** |
| 220 | 227 | * $this->process_auth() terminates the script if authentication fails |
@@ -225,7 +232,9 @@ discard block |
||
| 225 | 232 | */ |
| 226 | 233 | |
| 227 | 234 | foreach ($auths as $key => $auth) { |
| 228 | - if ($this->authPreempted) break; |
|
| 235 | + if ($this->authPreempted) { |
|
| 236 | + break; |
|
| 237 | + } |
|
| 229 | 238 | if (is_numeric($key)) { |
| 230 | 239 | $this->process_auth($auth, self::AUTH_GRAVITY); |
| 231 | 240 | } else { |
@@ -262,7 +271,9 @@ discard block |
||
| 262 | 271 | */ |
| 263 | 272 | private function auth_proceed(bool $success, int $flags):bool |
| 264 | 273 | { |
| 265 | - if ($flags & self::AUTH_GRAVITY) return $success; |
|
| 274 | + if ($flags & self::AUTH_GRAVITY) { |
|
| 275 | + return $success; |
|
| 276 | + } |
|
| 266 | 277 | if ($success) { |
| 267 | 278 | if ($flags & self::AUTH_FINAL) { |
| 268 | 279 | $this->authPreempted = true; |
@@ -323,15 +334,23 @@ discard block |
||
| 323 | 334 | { |
| 324 | 335 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
| 325 | 336 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
| 326 | - if (!$this->auth_proceed(!$username || !$password, $flags)) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
|
| 327 | - if (!$this->auth_proceed($this->rest_model->basicAuth($this, $username, $password), $flags)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
|
| 337 | + if (!$this->auth_proceed(!$username || !$password, $flags)) { |
|
| 338 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
| 339 | + } |
|
| 340 | + // Exits. |
|
| 341 | + if (!$this->auth_proceed($this->rest_model->basicAuth($this, $username, $password), $flags)) { |
|
| 342 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
| 343 | + } |
|
| 344 | + // Exits. |
|
| 328 | 345 | } |
| 329 | 346 | /** |
| 330 | 347 | * [api_key_auth description] |
| 331 | 348 | */ |
| 332 | 349 | private function api_key_auth(int $flags=self::AUTH_GRAVITY):void |
| 333 | 350 | { |
| 334 | - if (uri_string() == '') return; |
|
| 351 | + if (uri_string() == '') { |
|
| 352 | + return; |
|
| 353 | + } |
|
| 335 | 354 | $shouldProceed = $this->auth_proceed(false, $flags); |
| 336 | 355 | |
| 337 | 356 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true) && !$shouldProceed) { |
@@ -349,7 +368,9 @@ discard block |
||
| 349 | 368 | |
| 350 | 369 | $this->apiKey = $apiKey; |
| 351 | 370 | |
| 352 | - if (!$this->auth_proceed(true, $flags)) return; |
|
| 371 | + if (!$this->auth_proceed(true, $flags)) { |
|
| 372 | + return; |
|
| 373 | + } |
|
| 353 | 374 | |
| 354 | 375 | // API KEY Auth Passed Above. |
| 355 | 376 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey->{$this->api_key_limit_column} == 1) { |
@@ -398,7 +419,9 @@ discard block |
||
| 398 | 419 | // Trunctate Rate Limit Data. |
| 399 | 420 | $this->rest_model->truncateRatelimitData(); |
| 400 | 421 | // Check Whitelist. |
| 401 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
| 422 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
| 423 | + return; |
|
| 424 | + } |
|
| 402 | 425 | // Should we acyually Limit? |
| 403 | 426 | if ($this->ip_per_hour > 0) { |
| 404 | 427 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -478,7 +501,9 @@ discard block |
||
| 478 | 501 | if (isset($this->config['response_callbacks'][$code])) { |
| 479 | 502 | $this->config['response_callbacks'][$code]($auth, $errorReason); |
| 480 | 503 | } |
| 481 | - if (ENVIRONMENT != 'testing') exit($code); |
|
| 504 | + if (ENVIRONMENT != 'testing') { |
|
| 505 | + exit($code); |
|
| 506 | + } |
|
| 482 | 507 | throw new Exception("Error $code in $auth", $code); |
| 483 | 508 | } |
| 484 | 509 | } |