@@ -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; |
@@ -96,15 +100,21 @@ discard block |
||
| 96 | 100 | */ |
| 97 | 101 | public function getAPIKeyData(string $apiKey):?array { |
| 98 | 102 | // Preliminary Check. |
| 99 | - if ($this->api_key_table == null || $this->api_key_column == null) return null; |
|
| 103 | + if ($this->api_key_table == null || $this->api_key_column == null) { |
|
| 104 | + return null; |
|
| 105 | + } |
|
| 100 | 106 | // Query. |
| 101 | 107 | $this->db->select($this->api_key_column); |
| 102 | - if ($this->api_key_limit_column != null) $this->db->select($this->api_key_limit_column); |
|
| 108 | + if ($this->api_key_limit_column != null) { |
|
| 109 | + $this->db->select($this->api_key_limit_column); |
|
| 110 | + } |
|
| 103 | 111 | $this->db->from($this->api_key_table); |
| 104 | 112 | $this->db->where($this->api_key_column, $apiKey); |
| 105 | 113 | $query = $this->db->get(); |
| 106 | 114 | // Process Result. |
| 107 | - if ($query->num_rows() > 0) return $query->result_array()[0]; |
|
| 115 | + if ($query->num_rows() > 0) { |
|
| 116 | + return $query->result_array()[0]; |
|
| 117 | + } |
|
| 108 | 118 | return null; |
| 109 | 119 | } |
| 110 | 120 | /** |
@@ -123,7 +133,9 @@ discard block |
||
| 123 | 133 | public function getLimitData(string $client, string $group):?array { |
| 124 | 134 | $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 = ?'; |
| 125 | 135 | $query = $this->db->query($sql, [$client, $group]); |
| 126 | - if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0]; |
|
| 136 | + if (!is_scalar($query) && $query->num_rows() > 0) { |
|
| 137 | + return $query->result_array()[0]; |
|
| 138 | + } |
|
| 127 | 139 | return null; |
| 128 | 140 | } |
| 129 | 141 | /** |
@@ -138,16 +138,24 @@ discard block |
||
| 138 | 138 | // Match Auth Routes. |
| 139 | 139 | // The below algorithm is similar to the one Code Igniter uses in its |
| 140 | 140 | // Routing Class. |
| 141 | - if ($uri_auths == null || !is_array($uri_auths)) return; |
|
| 141 | + if ($uri_auths == null || !is_array($uri_auths)) { |
|
| 142 | + return; |
|
| 143 | + } |
|
| 142 | 144 | $auths = null; |
| 143 | 145 | foreach ($uri_auths as $uri => $auth_array) { |
| 144 | 146 | // Convert wildcards to RegEx. |
| 145 | 147 | $uri = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $uri); |
| 146 | - if (preg_match('#^'.$uri.'$#', uri_string())) $auths = $auth_array; // Assign Authentication Steps. |
|
| 148 | + if (preg_match('#^'.$uri.'$#', uri_string())) { |
|
| 149 | + $auths = $auth_array; |
|
| 150 | + } |
|
| 151 | + // Assign Authentication Steps. |
|
| 147 | 152 | break; |
| 148 | 153 | } |
| 149 | 154 | //$auths = $this->ci->config->item('rest')['uri_auth'][uri_string()] ?? null; |
| 150 | - if ($auths == null) return; // No authentication(s) to acrry out. |
|
| 155 | + if ($auths == null) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | + // No authentication(s) to acrry out. |
|
| 151 | 159 | // $this->process_auth() terminates the script if authentication fails |
| 152 | 160 | // It will call the callable in the rest.php config file under |
| 153 | 161 | // response_callbacks which matches the necesarry RESTResponse constant |
@@ -157,7 +165,9 @@ discard block |
||
| 157 | 165 | $this->process_auth($auths); |
| 158 | 166 | return; |
| 159 | 167 | } |
| 160 | - foreach ($auths as $auth) $this->process_auth($auth); |
|
| 168 | + foreach ($auths as $auth) { |
|
| 169 | + $this->process_auth($auth); |
|
| 170 | + } |
|
| 161 | 171 | } |
| 162 | 172 | /** |
| 163 | 173 | * [process_auth description] |
@@ -202,8 +212,14 @@ discard block |
||
| 202 | 212 | private function basic_auth():void { |
| 203 | 213 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
| 204 | 214 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
| 205 | - if (!$username || !$password) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
|
| 206 | - if (!$this->rest_model->basicAuth($this, $username, $password)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
|
| 215 | + if (!$username || !$password) { |
|
| 216 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
| 217 | + } |
|
| 218 | + // Exits. |
|
| 219 | + if (!$this->rest_model->basicAuth($this, $username, $password)) { |
|
| 220 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
| 221 | + } |
|
| 222 | + // Exits. |
|
| 207 | 223 | } |
| 208 | 224 | /** |
| 209 | 225 | * [api_key_auth description] |
@@ -263,7 +279,9 @@ discard block |
||
| 263 | 279 | // Trunctate Rate Limit Data. |
| 264 | 280 | $this->rest_model->truncateRatelimitData(); |
| 265 | 281 | // Check Whitelist. |
| 266 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
| 282 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
| 283 | + return; |
|
| 284 | + } |
|
| 267 | 285 | // Should we acyually Limit? |
| 268 | 286 | if ($this->ip_per_hour > 0) { |
| 269 | 287 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -339,7 +357,9 @@ discard block |
||
| 339 | 357 | if (isset($this->ci->config->item('rest')['response_callbacks'][$code])) { |
| 340 | 358 | $this->ci->config->item('rest')['response_callbacks'][$code]($auth); |
| 341 | 359 | } |
| 342 | - if (ENVIRONMENT != 'testing') exit($code); |
|
| 360 | + if (ENVIRONMENT != 'testing') { |
|
| 361 | + exit($code); |
|
| 362 | + } |
|
| 343 | 363 | throw new Exception("Error $code in $auth", $code); |
| 344 | 364 | } |
| 345 | 365 | } |
@@ -64,11 +64,17 @@ |
||
| 64 | 64 | { |
| 65 | 65 | http_response_code($this->code ?? 200); |
| 66 | 66 | |
| 67 | - if ($this->json) header('Content-Type: application/json'); |
|
| 67 | + if ($this->json) { |
|
| 68 | + header('Content-Type: application/json'); |
|
| 69 | + } |
|
| 68 | 70 | |
| 69 | - if ($data != null) echo !$this->json ? $this->data : json_encode($this->data); |
|
| 71 | + if ($data != null) { |
|
| 72 | + echo !$this->json ? $this->data : json_encode($this->data); |
|
| 73 | + } |
|
| 70 | 74 | |
| 71 | - if ($exit) exit(EXIT_SUCCESS); |
|
| 75 | + if ($exit) { |
|
| 76 | + exit(EXIT_SUCCESS); |
|
| 77 | + } |
|
| 72 | 78 | } |
| 73 | 79 | } |
| 74 | 80 | ?> |