Passed
Push — master ( 7a8481...6a477e )
by Francis
01:21
created
libraries/REST.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -137,16 +137,16 @@  discard block
 block discarded – undo
137 137
    * @param array|null $params Initialization parameters from the Slint system.
138 138
    *                           There's no use for this arg yet.
139 139
    */
140
-  function __construct(?array $params=null)
140
+  function __construct(?array $params = null)
141 141
   {
142
-    $this->ci =& get_instance();
142
+    $this->ci = & get_instance();
143 143
 
144 144
     if ($this->ci->input->is_cli_request()) return;
145 145
 
146 146
     // Load Config If Exists.
147 147
     //$this->ci->config->load('rest', true, true);
148
-    if (is_file(APPPATH . 'config/rest.php')) {
149
-      include APPPATH . 'config/rest.php';
148
+    if (is_file(APPPATH.'config/rest.php')) {
149
+      include APPPATH.'config/rest.php';
150 150
     }
151 151
 
152 152
     $this->config = $config;
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 
163 163
     // Load Model.
164 164
     $this->ci->load->splint(self::PACKAGE, '*RESTModel', 'rest_model');
165
-    $this->rest_model =& $this->ci->rest_model;
165
+    $this->rest_model = & $this->ci->rest_model;
166 166
 
167 167
     $this->rest_model->init([
168 168
       'users_table'           => $config['basic_auth']['users_table'] ?? null,
@@ -262,14 +262,14 @@  discard block
 block discarded – undo
262 262
    */
263 263
   private function auth_proceed(bool $success, int $flags):bool
264 264
   {
265
-    if ($flags & self::AUTH_GRAVITY) return $success;
265
+    if ($flags&self::AUTH_GRAVITY) return $success;
266 266
     if ($success) {
267
-      if ($flags & self::AUTH_FINAL) {
267
+      if ($flags&self::AUTH_FINAL) {
268 268
         $this->authPreempted = true;
269 269
         return true;
270 270
       }
271 271
     } else {
272
-      return $flags & self::AUTH_PASSIVE ? true : false;
272
+      return $flags&self::AUTH_PASSIVE ? true : false;
273 273
     }
274 274
   }
275 275
 
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
   /**
330 330
    * [api_key_auth description]
331 331
    */
332
-  private function api_key_auth(int $flags=self::AUTH_GRAVITY):void
332
+  private function api_key_auth(int $flags = self::AUTH_GRAVITY):void
333 333
   {
334 334
     if (uri_string() == '')  return;
335 335
     $shouldProceed = $this->auth_proceed(false, $flags);
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
     }
377 377
     // Should we acyually Limit?
378 378
     if ($this->per_hour > 0) {
379
-      $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey);
379
+      $client = hash('md5', $this->ci->input->ip_address()."%".$apiKey);
380 380
       $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user');
381 381
       if ($limitData == null) {
382 382
         $limitData = [];
@@ -460,7 +460,7 @@  discard block
 block discarded – undo
460 460
    * [get_authorization_header description]
461 461
    * @return [type] [description]
462 462
    */
463
-  private function get_authorization_header():?string
463
+  private function get_authorization_header(): ?string
464 464
   {
465 465
     if (isset($_SERVER['Authorization'])) {
466 466
       return trim($_SERVER["Authorization"]);
@@ -483,7 +483,7 @@  discard block
 block discarded – undo
483 483
    * [handle_response description]
484 484
    * @param int $code [description]
485 485
    */
486
-  private function handle_response(int $code, $auth=null, ?string $errorReason=null):void
486
+  private function handle_response(int $code, $auth = null, ?string $errorReason = null):void
487 487
   {
488 488
     http_response_code($code);
489 489
     header("Content-Type: application/json");
Please login to merge, or discard this patch.
Braces   +36 added lines, -11 removed lines patch added patch discarded remove patch
@@ -141,7 +141,9 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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 ==== //
355 376
 
@@ -410,7 +431,9 @@  discard block
 block discarded – undo
410 431
     // Trunctate Rate Limit Data.
411 432
     $this->rest_model->truncateRatelimitData();
412 433
     // Check Whitelist.
413
-    if (in_array($this->ci->input->ip_address(), $this->whitelist)) return;
434
+    if (in_array($this->ci->input->ip_address(), $this->whitelist)) {
435
+     return;
436
+    }
414 437
     // Should we acyually Limit?
415 438
     if ($this->ip_per_hour > 0) {
416 439
       $client = hash('md5', $this->ci->input->ip_address());
@@ -490,7 +513,9 @@  discard block
 block discarded – undo
490 513
     if (isset($this->config['response_callbacks'][$code])) {
491 514
       $this->config['response_callbacks'][$code]($auth, $errorReason);
492 515
     }
493
-    if (ENVIRONMENT != 'testing') exit($code);
516
+    if (ENVIRONMENT != 'testing') {
517
+     exit($code);
518
+    }
494 519
     throw new Exception("Error $code in $auth", $code);
495 520
   }
496 521
 }
Please login to merge, or discard this patch.