@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Api\Server; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | use Myth\Auth\AuthTrait; |
34 | 34 | use Myth\Controllers\BaseController; |
@@ -141,8 +141,8 @@ discard block |
||
141 | 141 | * @var array |
142 | 142 | */ |
143 | 143 | protected $codes = array( |
144 | - 'created' => 201, |
|
145 | - 'deleted' => 200, |
|
144 | + 'created' => 201, |
|
145 | + 'deleted' => 200, |
|
146 | 146 | 'invalid_request' => 400, |
147 | 147 | 'unsupported_response_type' => 400, |
148 | 148 | 'invalid_scope' => 400, |
@@ -166,32 +166,32 @@ discard block |
||
166 | 166 | 'not_implemented' => 501 |
167 | 167 | ); |
168 | 168 | |
169 | - /** |
|
170 | - * Convert common browser-sent langauge |
|
171 | - * strings to a folder name in the languages folder |
|
172 | - * that we want to use. |
|
173 | - * |
|
174 | - * Primarily used for converting to english when |
|
175 | - * viewing the API in a browser. |
|
176 | - * |
|
177 | - * @var array |
|
178 | - */ |
|
179 | - protected $lang_map = [ |
|
180 | - 'en-us' => 'english', |
|
181 | - 'en' => 'english', |
|
182 | - 'eng' => 'english', |
|
183 | - 'en-au' => 'english', |
|
184 | - 'en-nz' => 'english', |
|
185 | - 'en-za' => 'english', |
|
186 | - 'en-tt' => 'english', |
|
187 | - 'en-gb' => 'english', |
|
188 | - 'en-ca' => 'english', |
|
189 | - 'en-ie' => 'english', |
|
190 | - 'en-jm' => 'english', |
|
191 | - 'en-bz' => 'english', |
|
192 | - ]; |
|
193 | - |
|
194 | - /** |
|
169 | + /** |
|
170 | + * Convert common browser-sent langauge |
|
171 | + * strings to a folder name in the languages folder |
|
172 | + * that we want to use. |
|
173 | + * |
|
174 | + * Primarily used for converting to english when |
|
175 | + * viewing the API in a browser. |
|
176 | + * |
|
177 | + * @var array |
|
178 | + */ |
|
179 | + protected $lang_map = [ |
|
180 | + 'en-us' => 'english', |
|
181 | + 'en' => 'english', |
|
182 | + 'eng' => 'english', |
|
183 | + 'en-au' => 'english', |
|
184 | + 'en-nz' => 'english', |
|
185 | + 'en-za' => 'english', |
|
186 | + 'en-tt' => 'english', |
|
187 | + 'en-gb' => 'english', |
|
188 | + 'en-ca' => 'english', |
|
189 | + 'en-ie' => 'english', |
|
190 | + 'en-jm' => 'english', |
|
191 | + 'en-bz' => 'english', |
|
192 | + ]; |
|
193 | + |
|
194 | + /** |
|
195 | 195 | * If you wish to override the default authentication |
196 | 196 | * library used for authentication, set this to the |
197 | 197 | * fully namespaced class name. |
@@ -544,7 +544,7 @@ discard block |
||
544 | 544 | */ |
545 | 545 | public function grabVar($name) |
546 | 546 | { |
547 | - return array_key_exists($name, $this->vars) ? $this->vars[$name] : false; |
|
547 | + return array_key_exists($name, $this->vars) ? $this->vars[$name] : false; |
|
548 | 548 | } |
549 | 549 | |
550 | 550 | //-------------------------------------------------------------------- |
@@ -757,12 +757,12 @@ discard block |
||
757 | 757 | // Remove weight and strip space |
758 | 758 | list($lang) = explode(';', $lang); |
759 | 759 | |
760 | - $lang = strtolower(trim($lang)); |
|
760 | + $lang = strtolower(trim($lang)); |
|
761 | 761 | |
762 | - if (array_key_exists($lang, $this->lang_map)) |
|
763 | - { |
|
764 | - $lang = $this->lang_map[$lang]; |
|
765 | - } |
|
762 | + if (array_key_exists($lang, $this->lang_map)) |
|
763 | + { |
|
764 | + $lang = $this->lang_map[$lang]; |
|
765 | + } |
|
766 | 766 | |
767 | 767 | $return_langs[] = $lang; |
768 | 768 | } |
@@ -782,10 +782,10 @@ discard block |
||
782 | 782 | */ |
783 | 783 | public function detectFields() |
784 | 784 | { |
785 | - if (! array_key_exists('fields', $_GET)) |
|
786 | - { |
|
787 | - return; |
|
788 | - } |
|
785 | + if (! array_key_exists('fields', $_GET)) |
|
786 | + { |
|
787 | + return; |
|
788 | + } |
|
789 | 789 | |
790 | 790 | $fields = explode(',', $_GET['fields']); |
791 | 791 | |
@@ -814,7 +814,7 @@ discard block |
||
814 | 814 | return; |
815 | 815 | } |
816 | 816 | |
817 | - $model = new LogModel(); |
|
817 | + $model = new LogModel(); |
|
818 | 818 | |
819 | 819 | $data = [ |
820 | 820 | 'duration' => microtime(true) - $this->start_time, |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | $this->request->lang = $this->detectLanguage(); |
223 | 223 | |
224 | 224 | // Load our language, requested. |
225 | - if (! empty($this->request->lang)) |
|
225 | + if ( ! empty($this->request->lang)) |
|
226 | 226 | { |
227 | 227 | $file = ! empty($this->language_file) ? $this->language_file : 'application'; |
228 | 228 | |
@@ -241,20 +241,20 @@ discard block |
||
241 | 241 | $this->config->load('api'); |
242 | 242 | |
243 | 243 | // Gather config defaults when a value isn't set for this controller |
244 | - if ( empty($this->enable_logging) ) $this->enable_logging = config_item('api.enable_logging'); |
|
245 | - if ( empty($this->enable_rate_limits) ) $this->enable_rate_limits = config_item('api.enable_rate_limits'); |
|
246 | - if ( empty($this->rate_limits) ) $this->rate_limits = config_item('api.rate_limits'); |
|
244 | + if (empty($this->enable_logging)) $this->enable_logging = config_item('api.enable_logging'); |
|
245 | + if (empty($this->enable_rate_limits)) $this->enable_rate_limits = config_item('api.enable_rate_limits'); |
|
246 | + if (empty($this->rate_limits)) $this->rate_limits = config_item('api.rate_limits'); |
|
247 | 247 | |
248 | 248 | // Should we restrict to SSL requests? |
249 | 249 | if (config_item('api.require_ssl') === true && ! $this->request->ssl) |
250 | 250 | { |
251 | - $this->failForbidden( lang('api.ssl_required') ); |
|
251 | + $this->failForbidden(lang('api.ssl_required')); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | // Should we restrict to only allow AJAX requests? |
255 | - if (config_item('api.ajax_only') === true && ! $this->input->is_ajax_request() ) |
|
255 | + if (config_item('api.ajax_only') === true && ! $this->input->is_ajax_request()) |
|
256 | 256 | { |
257 | - $this->failForbidden( lang('api.ajax_required') ); |
|
257 | + $this->failForbidden(lang('api.ajax_required')); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | $this->detectPage(); |
@@ -266,17 +266,17 @@ discard block |
||
266 | 266 | // in a single codebase. |
267 | 267 | get_instance()->config->set_item('api.authenticate_lib', $this->authenticate_class); |
268 | 268 | |
269 | - if (! $this->restrict(null, true) ) |
|
269 | + if ( ! $this->restrict(null, true)) |
|
270 | 270 | { |
271 | 271 | $this->logTime(); |
272 | - $this->failUnauthorized( lang('api.unauthorized') ); |
|
272 | + $this->failUnauthorized(lang('api.unauthorized')); |
|
273 | 273 | } |
274 | 274 | } |
275 | 275 | |
276 | 276 | // Has the user hit rate limits for this hour? |
277 | 277 | if ($this->enable_rate_limits && ! $this->isWithinLimits()) |
278 | 278 | { |
279 | - $this->failTooManyRequests( sprintf( lang('api.too_many_requests'), $this->rate_limits) ); |
|
279 | + $this->failTooManyRequests(sprintf(lang('api.too_many_requests'), $this->rate_limits)); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | // NEVER allow profiling via API. |
@@ -310,7 +310,7 @@ discard block |
||
310 | 310 | } |
311 | 311 | else |
312 | 312 | { |
313 | - return $this->fail( lang('api.unknown_endpoint'), 'not_implemented'); |
|
313 | + return $this->fail(lang('api.unknown_endpoint'), 'not_implemented'); |
|
314 | 314 | } |
315 | 315 | } |
316 | 316 | |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | * |
332 | 332 | * @return mixed |
333 | 333 | */ |
334 | - public function respond ($data = null, $status_code = 200) |
|
334 | + public function respond($data = null, $status_code = 200) |
|
335 | 335 | { |
336 | 336 | // If data is null and not code provide, error and bail |
337 | 337 | if ($data === null && $status_code === null) |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | |
358 | 358 | set_status_header($status_code); |
359 | 359 | |
360 | - header('Content-Length: ' . strlen($output)); |
|
360 | + header('Content-Length: '.strlen($output)); |
|
361 | 361 | |
362 | 362 | exit($output); |
363 | 363 | } |
@@ -374,7 +374,7 @@ discard block |
||
374 | 374 | * |
375 | 375 | * @return mixed |
376 | 376 | */ |
377 | - protected function fail ($description, $status_code, $error_code = 'invalid_request') |
|
377 | + protected function fail($description, $status_code, $error_code = 'invalid_request') |
|
378 | 378 | { |
379 | 379 | if (is_string($status_code)) |
380 | 380 | { |
@@ -585,26 +585,26 @@ discard block |
||
585 | 585 | |
586 | 586 | $params['page'] = ($this->page > 1 ? $this->page + 1 : 2); |
587 | 587 | |
588 | - if (! $clean_get) |
|
588 | + if ( ! $clean_get) |
|
589 | 589 | { |
590 | - if ( ! isset($_GET) || ! is_array($_GET) ) |
|
590 | + if ( ! isset($_GET) || ! is_array($_GET)) |
|
591 | 591 | { |
592 | 592 | $_GET = []; |
593 | 593 | } |
594 | 594 | |
595 | - foreach ( $_GET as $key => $value ) |
|
595 | + foreach ($_GET as $key => $value) |
|
596 | 596 | { |
597 | - $params[ $key ] = $value; |
|
597 | + $params[$key] = $value; |
|
598 | 598 | } |
599 | 599 | |
600 | 600 | // Ensure we get a correct per_page value |
601 | - if (! array_key_exists('per_page', $params)) |
|
601 | + if ( ! array_key_exists('per_page', $params)) |
|
602 | 602 | { |
603 | 603 | $params['per_page'] = $this->per_page; |
604 | 604 | } |
605 | 605 | } |
606 | 606 | |
607 | - return site_url($path) .'?'. http_build_query($params); |
|
607 | + return site_url($path).'?'.http_build_query($params); |
|
608 | 608 | } |
609 | 609 | |
610 | 610 | //-------------------------------------------------------------------- |
@@ -625,7 +625,7 @@ discard block |
||
625 | 625 | * |
626 | 626 | * @return string |
627 | 627 | */ |
628 | - public function prevURL ($path, $clean_get = false) |
|
628 | + public function prevURL($path, $clean_get = false) |
|
629 | 629 | { |
630 | 630 | // If paging is turned off, get out of here |
631 | 631 | if ($this->per_page == 0) |
@@ -637,26 +637,26 @@ discard block |
||
637 | 637 | |
638 | 638 | $params['page'] = ($this->page > 1 ? $this->page - 1 : 1); |
639 | 639 | |
640 | - if (! $clean_get) |
|
640 | + if ( ! $clean_get) |
|
641 | 641 | { |
642 | - if ( ! isset($_GET) || ! is_array($_GET) ) |
|
642 | + if ( ! isset($_GET) || ! is_array($_GET)) |
|
643 | 643 | { |
644 | 644 | $_GET = []; |
645 | 645 | } |
646 | 646 | |
647 | - foreach ( $_GET as $key => $value ) |
|
647 | + foreach ($_GET as $key => $value) |
|
648 | 648 | { |
649 | 649 | $params[$key] = $value; |
650 | 650 | } |
651 | 651 | |
652 | 652 | // Ensure we get a correct per_page value |
653 | - if (! array_key_exists('per_page', $params)) |
|
653 | + if ( ! array_key_exists('per_page', $params)) |
|
654 | 654 | { |
655 | 655 | $params['per_page'] = $this->per_page; |
656 | 656 | } |
657 | 657 | } |
658 | 658 | |
659 | - return site_url($path) .'?'. http_build_query($params); |
|
659 | + return site_url($path).'?'.http_build_query($params); |
|
660 | 660 | } |
661 | 661 | |
662 | 662 | //-------------------------------------------------------------------- |
@@ -675,11 +675,11 @@ discard block |
||
675 | 675 | protected function detectPage( ) |
676 | 676 | { |
677 | 677 | // Is a per-page limit being set? |
678 | - $this->per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : $this->per_page; |
|
678 | + $this->per_page = isset($_GET['per_page']) ? (int) $_GET['per_page'] : $this->per_page; |
|
679 | 679 | |
680 | - $page = (int)$this->input->get('page'); |
|
680 | + $page = (int) $this->input->get('page'); |
|
681 | 681 | |
682 | - if (! $page || $page == 1) |
|
682 | + if ( ! $page || $page == 1) |
|
683 | 683 | { |
684 | 684 | $offset = 0; |
685 | 685 | } |
@@ -693,7 +693,7 @@ discard block |
||
693 | 693 | |
694 | 694 | // If they've specifically passed in page=0, then we need |
695 | 695 | // to ignore paging... |
696 | - if ((int)$this->input->get('page') === 0 && ! is_null($this->input->get('page')) ) |
|
696 | + if ((int) $this->input->get('page') === 0 && ! is_null($this->input->get('page'))) |
|
697 | 697 | { |
698 | 698 | $this->per_page = 0; |
699 | 699 | } |
@@ -715,7 +715,7 @@ discard block |
||
715 | 715 | $method = strtolower($this->input->server('REQUEST_METHOD')); |
716 | 716 | |
717 | 717 | // If it's not an allowed method, let's default to a GET |
718 | - if (! in_array($method, $this->allowed_http_methods)) |
|
718 | + if ( ! in_array($method, $this->allowed_http_methods)) |
|
719 | 719 | { |
720 | 720 | $method = 'get'; |
721 | 721 | } |
@@ -775,7 +775,7 @@ discard block |
||
775 | 775 | // We must check to see if the folder exists |
776 | 776 | // here since CI's lang->load will throw |
777 | 777 | // an error if the language doesn't exist. |
778 | - if (is_dir(APPPATH .'language/'. $lang)) |
|
778 | + if (is_dir(APPPATH.'language/'.$lang)) |
|
779 | 779 | { |
780 | 780 | $return_langs[] = $lang; |
781 | 781 | } |
@@ -783,7 +783,7 @@ discard block |
||
783 | 783 | |
784 | 784 | // If no languages were added, let the script |
785 | 785 | // send the default language back instead. |
786 | - if (! empty($return_langs)) |
|
786 | + if ( ! empty($return_langs)) |
|
787 | 787 | { |
788 | 788 | return $return_langs; |
789 | 789 | } |
@@ -801,14 +801,14 @@ discard block |
||
801 | 801 | */ |
802 | 802 | public function detectFields() |
803 | 803 | { |
804 | - if (! array_key_exists('fields', $_GET)) |
|
804 | + if ( ! array_key_exists('fields', $_GET)) |
|
805 | 805 | { |
806 | 806 | return; |
807 | 807 | } |
808 | 808 | |
809 | 809 | $fields = explode(',', $_GET['fields']); |
810 | 810 | |
811 | - if (! is_array($fields)) |
|
811 | + if ( ! is_array($fields)) |
|
812 | 812 | { |
813 | 813 | return; |
814 | 814 | } |
@@ -828,7 +828,7 @@ discard block |
||
828 | 828 | */ |
829 | 829 | public function logTime() |
830 | 830 | { |
831 | - if (! $this->enable_logging) |
|
831 | + if ( ! $this->enable_logging) |
|
832 | 832 | { |
833 | 833 | return; |
834 | 834 | } |
@@ -839,7 +839,7 @@ discard block |
||
839 | 839 | 'duration' => microtime(true) - $this->start_time, |
840 | 840 | 'user_id' => $this->authenticate->id(), |
841 | 841 | 'ip_address' => $this->input->ip_address(), |
842 | - 'request' => $this->uri->uri_string() .'?'. $_SERVER['QUERY_STRING'], |
|
842 | + 'request' => $this->uri->uri_string().'?'.$_SERVER['QUERY_STRING'], |
|
843 | 843 | 'method' => $this->request->method |
844 | 844 | ]; |
845 | 845 | |
@@ -860,7 +860,7 @@ discard block |
||
860 | 860 | { |
861 | 861 | $model = new LogModel(); |
862 | 862 | |
863 | - if ($model->requestsThisHourForUser( $this->authenticate->id() ) > $this->rate_limits) |
|
863 | + if ($model->requestsThisHourForUser($this->authenticate->id()) > $this->rate_limits) |
|
864 | 864 | { |
865 | 865 | return false; |
866 | 866 | } |