Passed
Push — master ( 33dcd4...7a8481 )
by Francis
06:54
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);
@@ -362,7 +362,7 @@  discard block
 block discarded – undo
362 362
       }
363 363
       // Should we acyually Limit?
364 364
       if ($this->per_hour > 0) {
365
-        $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey->{$this->api_key_column});
365
+        $client = hash('md5', $this->ci->input->ip_address()."%".$apiKey->{$this->api_key_column});
366 366
         $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user');
367 367
         if ($limitData == null) {
368 368
           $limitData = [];
@@ -448,7 +448,7 @@  discard block
 block discarded – undo
448 448
    * [get_authorization_header description]
449 449
    * @return [type] [description]
450 450
    */
451
-  private function get_authorization_header():?string
451
+  private function get_authorization_header(): ?string
452 452
   {
453 453
     if (isset($_SERVER['Authorization'])) {
454 454
       return trim($_SERVER["Authorization"]);
@@ -471,7 +471,7 @@  discard block
 block discarded – undo
471 471
    * [handle_response description]
472 472
    * @param int $code [description]
473 473
    */
474
-  private function handle_response(int $code, $auth=null, ?string $errorReason=null):void
474
+  private function handle_response(int $code, $auth = null, ?string $errorReason = null):void
475 475
   {
476 476
     http_response_code($code);
477 477
     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 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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.