@@ -1,31 +1,31 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class DatabaseQueryResultTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class DatabaseQueryResultTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - public static function setUpBeforeClass() |
|
| 9 | - { |
|
| 8 | + public static function setUpBeforeClass() |
|
| 9 | + { |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public static function tearDownAfterClass() |
|
| 14 | - { |
|
| 13 | + public static function tearDownAfterClass() |
|
| 14 | + { |
|
| 15 | 15 | |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - protected function setUp() |
|
| 19 | - { |
|
| 20 | - } |
|
| 18 | + protected function setUp() |
|
| 19 | + { |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function tearDown() |
|
| 23 | - { |
|
| 24 | - } |
|
| 22 | + protected function tearDown() |
|
| 23 | + { |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function testNotYet() |
|
| 27 | - { |
|
| 28 | - $this->markTestSkipped(); |
|
| 29 | - } |
|
| 26 | + public function testNotYet() |
|
| 27 | + { |
|
| 28 | + $this->markTestSkipped(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - } |
|
| 32 | 31 | \ No newline at end of file |
| 32 | + } |
|
| 33 | 33 | \ No newline at end of file |
@@ -1,31 +1,31 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class DatabaseQueryRunnerTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class DatabaseQueryRunnerTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - public static function setUpBeforeClass() |
|
| 9 | - { |
|
| 8 | + public static function setUpBeforeClass() |
|
| 9 | + { |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public static function tearDownAfterClass() |
|
| 14 | - { |
|
| 13 | + public static function tearDownAfterClass() |
|
| 14 | + { |
|
| 15 | 15 | |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - protected function setUp() |
|
| 19 | - { |
|
| 20 | - } |
|
| 18 | + protected function setUp() |
|
| 19 | + { |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function tearDown() |
|
| 23 | - { |
|
| 24 | - } |
|
| 22 | + protected function tearDown() |
|
| 23 | + { |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function testNotYet() |
|
| 27 | - { |
|
| 28 | - $this->markTestSkipped(); |
|
| 29 | - } |
|
| 26 | + public function testNotYet() |
|
| 27 | + { |
|
| 28 | + $this->markTestSkipped(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - } |
|
| 32 | 31 | \ No newline at end of file |
| 32 | + } |
|
| 33 | 33 | \ No newline at end of file |
@@ -1,69 +1,69 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class FunctionUserAgentTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class FunctionUserAgentTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - public static function setUpBeforeClass() |
|
| 9 | - { |
|
| 8 | + public static function setUpBeforeClass() |
|
| 9 | + { |
|
| 10 | 10 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public function testLoopBack() |
|
| 14 | - { |
|
| 15 | - $this->assertEquals('127.0.0.1', get_ip()); |
|
| 16 | - } |
|
| 13 | + public function testLoopBack() |
|
| 14 | + { |
|
| 15 | + $this->assertEquals('127.0.0.1', get_ip()); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | 18 | public function testServerRemoteAddr() |
| 19 | - { |
|
| 19 | + { |
|
| 20 | 20 | $ip = '182.23.24.56'; |
| 21 | 21 | $_SERVER['REMOTE_ADDR'] = $ip; |
| 22 | - $this->assertEquals($ip, get_ip()); |
|
| 23 | - } |
|
| 22 | + $this->assertEquals($ip, get_ip()); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | 25 | public function testServerHttpClientIp() |
| 26 | - { |
|
| 26 | + { |
|
| 27 | 27 | $ip = '192.168.24.1'; |
| 28 | 28 | $_SERVER['HTTP_CLIENT_IP'] = $ip; |
| 29 | - $this->assertEquals($ip, get_ip()); |
|
| 30 | - } |
|
| 29 | + $this->assertEquals($ip, get_ip()); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | 32 | public function testServerHttpXForwardedFor() |
| 33 | - { |
|
| 33 | + { |
|
| 34 | 34 | $ip = '172.18.2.1'; |
| 35 | 35 | $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip; |
| 36 | - $this->assertEquals($ip, get_ip()); |
|
| 37 | - } |
|
| 36 | + $this->assertEquals($ip, get_ip()); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | public function testServerHttpXForwarded() |
| 40 | - { |
|
| 40 | + { |
|
| 41 | 41 | |
| 42 | 42 | $ip = '12.18.2.1'; |
| 43 | 43 | $_SERVER['HTTP_X_FORWARDED'] = $ip; |
| 44 | - $this->assertEquals($ip, get_ip()); |
|
| 45 | - } |
|
| 44 | + $this->assertEquals($ip, get_ip()); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | public function testServerHttpForwardedFor() |
| 48 | - { |
|
| 48 | + { |
|
| 49 | 49 | $ip = '198.180.2.1'; |
| 50 | 50 | $_SERVER['HTTP_FORWARDED_FOR'] = $ip; |
| 51 | - $this->assertEquals($ip, get_ip()); |
|
| 52 | - } |
|
| 51 | + $this->assertEquals($ip, get_ip()); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | 54 | public function testServerHttpForwarded() |
| 55 | - { |
|
| 55 | + { |
|
| 56 | 56 | $ip = '220.200.2.1'; |
| 57 | 57 | $_SERVER['HTTP_FORWARDED'] = $ip; |
| 58 | - $this->assertEquals($ip, get_ip()); |
|
| 59 | - } |
|
| 58 | + $this->assertEquals($ip, get_ip()); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function testManyIp() |
|
| 62 | - { |
|
| 61 | + public function testManyIp() |
|
| 62 | + { |
|
| 63 | 63 | $ips = '20.200.2.1, 192.168.34.4'; |
| 64 | 64 | $ip = '20.200.2.1'; |
| 65 | 65 | $_SERVER['REMOTE_ADDR'] = $ips; |
| 66 | - $this->assertEquals($ip, get_ip()); |
|
| 67 | - } |
|
| 66 | + $this->assertEquals($ip, get_ip()); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 70 | 69 | \ No newline at end of file |
| 70 | + } |
|
| 71 | 71 | \ No newline at end of file |
@@ -1,287 +1,287 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * TNH Framework |
|
| 4 | - * |
|
| 5 | - * A simple PHP framework using HMVC architecture |
|
| 6 | - * |
|
| 7 | - * This content is released under the GNU GPL License (GPL) |
|
| 8 | - * |
|
| 9 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 10 | - * |
|
| 11 | - * This program is free software; you can redistribute it and/or |
|
| 12 | - * modify it under the terms of the GNU General Public License |
|
| 13 | - * as published by the Free Software Foundation; either version 3 |
|
| 14 | - * of the License, or (at your option) any later version. |
|
| 15 | - * |
|
| 16 | - * This program is distributed in the hope that it will be useful, |
|
| 17 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 | - * GNU General Public License for more details. |
|
| 20 | - * |
|
| 21 | - * You should have received a copy of the GNU General Public License |
|
| 22 | - * along with this program; if not, write to the Free Software |
|
| 23 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 24 | - */ |
|
| 2 | + /** |
|
| 3 | + * TNH Framework |
|
| 4 | + * |
|
| 5 | + * A simple PHP framework using HMVC architecture |
|
| 6 | + * |
|
| 7 | + * This content is released under the GNU GPL License (GPL) |
|
| 8 | + * |
|
| 9 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 10 | + * |
|
| 11 | + * This program is free software; you can redistribute it and/or |
|
| 12 | + * modify it under the terms of the GNU General Public License |
|
| 13 | + * as published by the Free Software Foundation; either version 3 |
|
| 14 | + * of the License, or (at your option) any later version. |
|
| 15 | + * |
|
| 16 | + * This program is distributed in the hope that it will be useful, |
|
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 | + * GNU General Public License for more details. |
|
| 20 | + * |
|
| 21 | + * You should have received a copy of the GNU General Public License |
|
| 22 | + * along with this program; if not, write to the Free Software |
|
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 24 | + */ |
|
| 25 | 25 | |
| 26 | - error_reporting(E_ALL | E_STRICT); |
|
| 27 | - ini_set('display_errors', 1); |
|
| 26 | + error_reporting(E_ALL | E_STRICT); |
|
| 27 | + ini_set('display_errors', 1); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | - */ |
|
| 32 | - define('DS', DIRECTORY_SEPARATOR); |
|
| 29 | + /** |
|
| 30 | + * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | + */ |
|
| 32 | + define('DS', DIRECTORY_SEPARATOR); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The root directory of the application. |
|
| 36 | - * |
|
| 37 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | - */ |
|
| 39 | - define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
|
| 34 | + /** |
|
| 35 | + * The root directory of the application. |
|
| 36 | + * |
|
| 37 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | + */ |
|
| 39 | + define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
|
| 40 | 40 | |
| 41 | - //tests dir path |
|
| 42 | - define('TESTS_PATH', dirname(realpath(__FILE__)) . DS); |
|
| 41 | + //tests dir path |
|
| 42 | + define('TESTS_PATH', dirname(realpath(__FILE__)) . DS); |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * The path to the directory. |
|
| 46 | - * |
|
| 47 | - * That contains your static files (javascript, css, images, etc.) |
|
| 48 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 49 | - */ |
|
| 50 | - define('ASSETS_PATH', 'assets/'); |
|
| 44 | + /** |
|
| 45 | + * The path to the directory. |
|
| 46 | + * |
|
| 47 | + * That contains your static files (javascript, css, images, etc.) |
|
| 48 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 49 | + */ |
|
| 50 | + define('ASSETS_PATH', 'assets/'); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The path to the directory of your cache files. |
|
| 54 | - * |
|
| 55 | - * This feature is available currently for database and views. |
|
| 56 | - */ |
|
| 57 | - define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
| 52 | + /** |
|
| 53 | + * The path to the directory of your cache files. |
|
| 54 | + * |
|
| 55 | + * This feature is available currently for database and views. |
|
| 56 | + */ |
|
| 57 | + define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Custom application path for tests |
|
| 61 | - */ |
|
| 62 | - define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
| 59 | + /** |
|
| 60 | + * Custom application path for tests |
|
| 61 | + */ |
|
| 62 | + define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The path to the controller directory of your application. |
|
| 66 | - * |
|
| 67 | - * If you already know the MVC architecture you know what a controller means; |
|
| 68 | - * it is he who makes the business logic of your application in general. |
|
| 69 | - */ |
|
| 70 | - define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
| 64 | + /** |
|
| 65 | + * The path to the controller directory of your application. |
|
| 66 | + * |
|
| 67 | + * If you already know the MVC architecture you know what a controller means; |
|
| 68 | + * it is he who makes the business logic of your application in general. |
|
| 69 | + */ |
|
| 70 | + define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * The path to the directory of your model classes of your application. |
|
| 74 | - * |
|
| 75 | - * If you already know the MVC architecture you know what a model means; |
|
| 76 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 77 | - */ |
|
| 78 | - define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
| 72 | + /** |
|
| 73 | + * The path to the directory of your model classes of your application. |
|
| 74 | + * |
|
| 75 | + * If you already know the MVC architecture you know what a model means; |
|
| 76 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 77 | + */ |
|
| 78 | + define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * The path to the directory of your views. |
|
| 82 | - * |
|
| 83 | - * If you already know the MVC architecture you know what a view means, |
|
| 84 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
| 85 | - * everything displayed in the browser interface, etc. |
|
| 86 | - */ |
|
| 87 | - define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
| 80 | + /** |
|
| 81 | + * The path to the directory of your views. |
|
| 82 | + * |
|
| 83 | + * If you already know the MVC architecture you know what a view means, |
|
| 84 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
| 85 | + * everything displayed in the browser interface, etc. |
|
| 86 | + */ |
|
| 87 | + define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * The path to the configuration directory. |
|
| 91 | - * |
|
| 92 | - * That contains most of the configuration files for your |
|
| 93 | - * application (database, class loading file, functions, etc.) |
|
| 94 | - */ |
|
| 95 | - define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
| 89 | + /** |
|
| 90 | + * The path to the configuration directory. |
|
| 91 | + * |
|
| 92 | + * That contains most of the configuration files for your |
|
| 93 | + * application (database, class loading file, functions, etc.) |
|
| 94 | + */ |
|
| 95 | + define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * The core directory |
|
| 99 | - * |
|
| 100 | - * It is recommended to put this folder out of the web directory of your server and |
|
| 101 | - * you should not change its content because in case of update you could lose the modified files. |
|
| 102 | - */ |
|
| 103 | - define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
| 97 | + /** |
|
| 98 | + * The core directory |
|
| 99 | + * |
|
| 100 | + * It is recommended to put this folder out of the web directory of your server and |
|
| 101 | + * you should not change its content because in case of update you could lose the modified files. |
|
| 102 | + */ |
|
| 103 | + define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * The path to the directory of core classes that used by the system. |
|
| 107 | - * |
|
| 108 | - * It contains PHP classes that are used by the framework internally. |
|
| 109 | - */ |
|
| 110 | - define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
| 105 | + /** |
|
| 106 | + * The path to the directory of core classes that used by the system. |
|
| 107 | + * |
|
| 108 | + * It contains PHP classes that are used by the framework internally. |
|
| 109 | + */ |
|
| 110 | + define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * The path to the directory of core classes for the cache used by the system. |
|
| 114 | - * |
|
| 115 | - * It contains PHP classes for the cache drivers. |
|
| 116 | - */ |
|
| 117 | - define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
| 112 | + /** |
|
| 113 | + * The path to the directory of core classes for the cache used by the system. |
|
| 114 | + * |
|
| 115 | + * It contains PHP classes for the cache drivers. |
|
| 116 | + */ |
|
| 117 | + define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | - * The path to the directory of core classes for the database used by the system. |
|
| 121 | - * |
|
| 122 | - * It contains PHP classes for the database library, drivers, etc. |
|
| 123 | - */ |
|
| 124 | - define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
| 120 | + * The path to the directory of core classes for the database used by the system. |
|
| 121 | + * |
|
| 122 | + * It contains PHP classes for the database library, drivers, etc. |
|
| 123 | + */ |
|
| 124 | + define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * The path to the directory of core classes for the model used by the system. |
|
| 128 | - * |
|
| 129 | - * It contains PHP classes for the models. |
|
| 130 | - */ |
|
| 131 | - define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
| 126 | + /** |
|
| 127 | + * The path to the directory of core classes for the model used by the system. |
|
| 128 | + * |
|
| 129 | + * It contains PHP classes for the models. |
|
| 130 | + */ |
|
| 131 | + define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * The path to the directory of functions or helper systems. |
|
| 135 | - * |
|
| 136 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 137 | - */ |
|
| 138 | - define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
| 133 | + /** |
|
| 134 | + * The path to the directory of functions or helper systems. |
|
| 135 | + * |
|
| 136 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 137 | + */ |
|
| 138 | + define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * The path to the core directory of languages files. |
|
| 142 | - * |
|
| 143 | - */ |
|
| 144 | - define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
| 140 | + /** |
|
| 141 | + * The path to the core directory of languages files. |
|
| 142 | + * |
|
| 143 | + */ |
|
| 144 | + define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * The path to the system library directory. |
|
| 148 | - * |
|
| 149 | - * Which contains the libraries most often used in your web application, as for the |
|
| 150 | - * core directory it is advisable to put it out of the root directory of your application. |
|
| 151 | - */ |
|
| 152 | - define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
| 146 | + /** |
|
| 147 | + * The path to the system library directory. |
|
| 148 | + * |
|
| 149 | + * Which contains the libraries most often used in your web application, as for the |
|
| 150 | + * core directory it is advisable to put it out of the root directory of your application. |
|
| 151 | + */ |
|
| 152 | + define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * The path to the system view directory. |
|
| 156 | - * |
|
| 157 | - * That contains the views used for the system, such as error messages, and so on. |
|
| 158 | - */ |
|
| 159 | - define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
| 154 | + /** |
|
| 155 | + * The path to the system view directory. |
|
| 156 | + * |
|
| 157 | + * That contains the views used for the system, such as error messages, and so on. |
|
| 158 | + */ |
|
| 159 | + define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * The path to the directory of your PHP personal functions or helper. |
|
| 163 | - * |
|
| 164 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 165 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 166 | - * because if you update the system you may lose them. |
|
| 167 | - */ |
|
| 168 | - define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
| 161 | + /** |
|
| 162 | + * The path to the directory of your PHP personal functions or helper. |
|
| 163 | + * |
|
| 164 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 165 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 166 | + * because if you update the system you may lose them. |
|
| 167 | + */ |
|
| 168 | + define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * The path to the app directory of personal language. |
|
| 172 | - * |
|
| 173 | - * This feature is not yet available. |
|
| 174 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
| 175 | - */ |
|
| 176 | - define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
| 170 | + /** |
|
| 171 | + * The path to the app directory of personal language. |
|
| 172 | + * |
|
| 173 | + * This feature is not yet available. |
|
| 174 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
| 175 | + */ |
|
| 176 | + define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * The path to the directory of your personal libraries |
|
| 180 | - * |
|
| 181 | - * It contains your PHP classes, package, etc. |
|
| 182 | - * Note: you should not put your personal libraries in the system library directory, |
|
| 183 | - * because it is recalled in case of updating the system you might have surprises. |
|
| 184 | - */ |
|
| 185 | - define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
| 178 | + /** |
|
| 179 | + * The path to the directory of your personal libraries |
|
| 180 | + * |
|
| 181 | + * It contains your PHP classes, package, etc. |
|
| 182 | + * Note: you should not put your personal libraries in the system library directory, |
|
| 183 | + * because it is recalled in case of updating the system you might have surprises. |
|
| 184 | + */ |
|
| 185 | + define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * The path to the directory that contains the log files. |
|
| 189 | - * |
|
| 190 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 191 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 192 | - * details see the documentation of your web server. |
|
| 193 | - * Example for Unix or linux with apache web server: |
|
| 194 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 195 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 196 | - */ |
|
| 197 | - define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
| 187 | + /** |
|
| 188 | + * The path to the directory that contains the log files. |
|
| 189 | + * |
|
| 190 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 191 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 192 | + * details see the documentation of your web server. |
|
| 193 | + * Example for Unix or linux with apache web server: |
|
| 194 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 195 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 196 | + */ |
|
| 197 | + define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * The path to the modules directory. |
|
| 201 | - * |
|
| 202 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 203 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
| 204 | - */ |
|
| 205 | - define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
| 199 | + /** |
|
| 200 | + * The path to the modules directory. |
|
| 201 | + * |
|
| 202 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 203 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
| 204 | + */ |
|
| 205 | + define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * The path to the directory of sources external to your application. |
|
| 209 | - * |
|
| 210 | - * If you have already used "composer" you know what that means. |
|
| 211 | - */ |
|
| 212 | - define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
| 207 | + /** |
|
| 208 | + * The path to the directory of sources external to your application. |
|
| 209 | + * |
|
| 210 | + * If you have already used "composer" you know what that means. |
|
| 211 | + */ |
|
| 212 | + define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * The front controller of your application. |
|
| 216 | - * |
|
| 217 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 218 | - * your application by using the rewrite module URL of your web server . |
|
| 219 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 220 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 221 | - */ |
|
| 222 | - define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
| 214 | + /** |
|
| 215 | + * The front controller of your application. |
|
| 216 | + * |
|
| 217 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 218 | + * your application by using the rewrite module URL of your web server . |
|
| 219 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 220 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 221 | + */ |
|
| 222 | + define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
| 223 | 223 | |
| 224 | - /** |
|
| 225 | - * Check if user run the application under CLI |
|
| 226 | - */ |
|
| 227 | - define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
| 224 | + /** |
|
| 225 | + * Check if user run the application under CLI |
|
| 226 | + */ |
|
| 227 | + define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * The environment of your application (production, test, development). |
|
| 231 | - * |
|
| 232 | - * if your application is still in development you use the value "development" |
|
| 233 | - * so you will have the display of the error messages, etc. |
|
| 234 | - * Once you finish the development of your application that is to put it online |
|
| 235 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 236 | - * the loading of the system, will be fast. |
|
| 237 | - */ |
|
| 238 | - define('ENVIRONMENT', 'testing'); |
|
| 229 | + /** |
|
| 230 | + * The environment of your application (production, test, development). |
|
| 231 | + * |
|
| 232 | + * if your application is still in development you use the value "development" |
|
| 233 | + * so you will have the display of the error messages, etc. |
|
| 234 | + * Once you finish the development of your application that is to put it online |
|
| 235 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 236 | + * the loading of the system, will be fast. |
|
| 237 | + */ |
|
| 238 | + define('ENVIRONMENT', 'testing'); |
|
| 239 | 239 | |
| 240 | 240 | |
| 241 | - //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
|
| 242 | - $_SESSION = array(); |
|
| 241 | + //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
|
| 242 | + $_SESSION = array(); |
|
| 243 | 243 | |
| 244 | - //check for composer autoload file if exists include it |
|
| 245 | - if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 246 | - require_once VENDOR_PATH . 'autoload.php'; |
|
| 244 | + //check for composer autoload file if exists include it |
|
| 245 | + if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 246 | + require_once VENDOR_PATH . 'autoload.php'; |
|
| 247 | 247 | |
| 248 | - //define the class alias for vstream |
|
| 249 | - class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); |
|
| 250 | - class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); |
|
| 251 | - class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); |
|
| 248 | + //define the class alias for vstream |
|
| 249 | + class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); |
|
| 250 | + class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); |
|
| 251 | + class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); |
|
| 252 | 252 | |
| 253 | - } |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - //require autoloader for test |
|
| 256 | - require_once 'include/autoloader.php'; |
|
| 255 | + //require autoloader for test |
|
| 256 | + require_once 'include/autoloader.php'; |
|
| 257 | 257 | |
| 258 | 258 | |
| 259 | 259 | |
| 260 | - //grap from core/common.php functions and mock some functions for tests |
|
| 261 | - require_once 'include/common.php'; |
|
| 260 | + //grap from core/common.php functions and mock some functions for tests |
|
| 261 | + require_once 'include/common.php'; |
|
| 262 | 262 | |
| 263 | - //Some utilities functions, classes, etc. |
|
| 264 | - require_once 'include/testsUtil.php'; |
|
| 263 | + //Some utilities functions, classes, etc. |
|
| 264 | + require_once 'include/testsUtil.php'; |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * Setting of the PHP error message handling function |
|
| 268 | - */ |
|
| 269 | - set_error_handler('php_error_handler'); |
|
| 266 | + /** |
|
| 267 | + * Setting of the PHP error message handling function |
|
| 268 | + */ |
|
| 269 | + set_error_handler('php_error_handler'); |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Setting of the PHP error exception handling function |
|
| 273 | - */ |
|
| 274 | - set_exception_handler('php_exception_handler'); |
|
| 271 | + /** |
|
| 272 | + * Setting of the PHP error exception handling function |
|
| 273 | + */ |
|
| 274 | + set_exception_handler('php_exception_handler'); |
|
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * Setting of the PHP shutdown handling function |
|
| 278 | - */ |
|
| 279 | - register_shutdown_function('php_shudown_handler'); |
|
| 276 | + /** |
|
| 277 | + * Setting of the PHP shutdown handling function |
|
| 278 | + */ |
|
| 279 | + register_shutdown_function('php_shudown_handler'); |
|
| 280 | 280 | |
| 281 | - /** |
|
| 282 | - * Register the tests autoload |
|
| 283 | - */ |
|
| 284 | - spl_autoload_register('tests_autoload'); |
|
| 281 | + /** |
|
| 282 | + * Register the tests autoload |
|
| 283 | + */ |
|
| 284 | + spl_autoload_register('tests_autoload'); |
|
| 285 | 285 | |
| 286 | 286 | |
| 287 | - |
|
| 288 | 287 | \ No newline at end of file |
| 288 | + |
|
| 289 | 289 | \ No newline at end of file |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | /** |
| 60 | 60 | * Custom application path for tests |
| 61 | 61 | */ |
| 62 | - define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
| 62 | + define('APPS_PATH', TESTS_PATH . 'hmvc' . DS); |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The path to the controller directory of your application. |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
| 203 | 203 | * in HMVC architecture (hierichical, controllers, models, views). |
| 204 | 204 | */ |
| 205 | - define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
| 205 | + define('MODULE_PATH', dirname(realpath(__FILE__)) . DS . 'hmvc' . DS . 'modules' . DS); |
|
| 206 | 206 | |
| 207 | 207 | /** |
| 208 | 208 | * The path to the directory of sources external to your application. |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | $_SESSION = array(); |
| 243 | 243 | |
| 244 | 244 | //check for composer autoload file if exists include it |
| 245 | - if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 245 | + if (file_exists(VENDOR_PATH . 'autoload.php')) { |
|
| 246 | 246 | require_once VENDOR_PATH . 'autoload.php'; |
| 247 | 247 | |
| 248 | 248 | //define the class alias for vstream |
@@ -1,312 +1,312 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Whether to enable compression of the cache data file. |
|
| 31 | - * @var boolean |
|
| 32 | - */ |
|
| 33 | - private $compressCacheData = true; |
|
| 29 | + /** |
|
| 30 | + * Whether to enable compression of the cache data file. |
|
| 31 | + * @var boolean |
|
| 32 | + */ |
|
| 33 | + private $compressCacheData = true; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private $logger; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 44 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | - } |
|
| 46 | - /** |
|
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 42 | + public function __construct(Log $logger = null){ |
|
| 43 | + if(! $this->isSupported()){ |
|
| 44 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | + } |
|
| 46 | + /** |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | - if(! extension_loaded('zlib')){ |
|
| 59 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | - $this->compressCacheData = false; |
|
| 61 | - } |
|
| 62 | - } |
|
| 57 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | + if(! extension_loaded('zlib')){ |
|
| 59 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | + $this->compressCacheData = false; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * This is used to get the cache data using the key |
|
| 66 | - * @param string $key the key to identify the cache data |
|
| 67 | - * @return mixed the cache data if exists else return false |
|
| 68 | - */ |
|
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | - $filePath = $this->getFilePath($key); |
|
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - // Getting a shared lock |
|
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 103 | - } |
|
| 64 | + /** |
|
| 65 | + * This is used to get the cache data using the key |
|
| 66 | + * @param string $key the key to identify the cache data |
|
| 67 | + * @return mixed the cache data if exists else return false |
|
| 68 | + */ |
|
| 69 | + public function get($key){ |
|
| 70 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | + $filePath = $this->getFilePath($key); |
|
| 72 | + if(! file_exists($filePath)){ |
|
| 73 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath,'r'); |
|
| 78 | + if(! is_resource($handle)){ |
|
| 79 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + // Getting a shared lock |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Save data to the cache |
|
| 108 | - * @param string $key the key to identify this cache data |
|
| 109 | - * @param mixed $data the cache data |
|
| 110 | - * @param integer $ttl the cache life time |
|
| 111 | - * @return boolean true if success otherwise will return false |
|
| 112 | - */ |
|
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 114 | - $expire = time() + $ttl; |
|
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | - $filePath = $this->getFilePath($key); |
|
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | - //Serializing along with the TTL |
|
| 124 | - $cacheData = serialize(array( |
|
| 125 | - 'mtime' => time(), |
|
| 126 | - 'expire' => $expire, |
|
| 127 | - 'data' => $data, |
|
| 128 | - 'ttl' => $ttl |
|
| 129 | - ) |
|
| 130 | - ); |
|
| 131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | - fclose($handle); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | - fclose($handle); |
|
| 140 | - chmod($filePath, 0640); |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 106 | + /** |
|
| 107 | + * Save data to the cache |
|
| 108 | + * @param string $key the key to identify this cache data |
|
| 109 | + * @param mixed $data the cache data |
|
| 110 | + * @param integer $ttl the cache life time |
|
| 111 | + * @return boolean true if success otherwise will return false |
|
| 112 | + */ |
|
| 113 | + public function set($key, $data, $ttl = 0){ |
|
| 114 | + $expire = time() + $ttl; |
|
| 115 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | + $filePath = $this->getFilePath($key); |
|
| 117 | + $handle = fopen($filePath,'w'); |
|
| 118 | + if(! is_resource($handle)){ |
|
| 119 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | + //Serializing along with the TTL |
|
| 124 | + $cacheData = serialize(array( |
|
| 125 | + 'mtime' => time(), |
|
| 126 | + 'expire' => $expire, |
|
| 127 | + 'data' => $data, |
|
| 128 | + 'ttl' => $ttl |
|
| 129 | + ) |
|
| 130 | + ); |
|
| 131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | + if(! $result){ |
|
| 133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | + fclose($handle); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + else{ |
|
| 138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | + fclose($handle); |
|
| 140 | + chmod($filePath, 0640); |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Delete the cache data for given key |
|
| 148 | - * @param string $key the key for cache to be deleted |
|
| 149 | - * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | - * the cache or the cache with the given key not exist |
|
| 151 | - */ |
|
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | - $filePath = $this->getFilePath($key); |
|
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 157 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | - unlink($filePath); |
|
| 163 | - return true; |
|
| 164 | - } |
|
| 165 | - } |
|
| 146 | + /** |
|
| 147 | + * Delete the cache data for given key |
|
| 148 | + * @param string $key the key for cache to be deleted |
|
| 149 | + * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | + * the cache or the cache with the given key not exist |
|
| 151 | + */ |
|
| 152 | + public function delete($key){ |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | + $filePath = $this->getFilePath($key); |
|
| 155 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | + if(! file_exists($filePath)){ |
|
| 157 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + else{ |
|
| 161 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | + unlink($filePath); |
|
| 163 | + return true; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Get the cache information for given key |
|
| 169 | - * @param string $key the key for cache to get the information for |
|
| 170 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | - * 'ttl' => the time to live of the cache in second |
|
| 174 | - */ |
|
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | - $filePath = $this->getFilePath($key); |
|
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 180 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 184 | - $data = file_get_contents($filePath); |
|
| 185 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 186 | - if(! $data){ |
|
| 187 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - $this->logger->info('This cache data is OK check for expire'); |
|
| 191 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 192 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
| 193 | - $info = array( |
|
| 194 | - 'mtime' => $data['mtime'], |
|
| 195 | - 'expire' => $data['expire'], |
|
| 196 | - 'ttl' => $data['ttl'] |
|
| 197 | - ); |
|
| 198 | - return $info; |
|
| 199 | - } |
|
| 200 | - $this->logger->info('This cache already expired return false'); |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 167 | + /** |
|
| 168 | + * Get the cache information for given key |
|
| 169 | + * @param string $key the key for cache to get the information for |
|
| 170 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | + * 'ttl' => the time to live of the cache in second |
|
| 174 | + */ |
|
| 175 | + public function getInfo($key){ |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | + $filePath = $this->getFilePath($key); |
|
| 178 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | + if(! file_exists($filePath)){ |
|
| 180 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 184 | + $data = file_get_contents($filePath); |
|
| 185 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 186 | + if(! $data){ |
|
| 187 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + $this->logger->info('This cache data is OK check for expire'); |
|
| 191 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 192 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
| 193 | + $info = array( |
|
| 194 | + 'mtime' => $data['mtime'], |
|
| 195 | + 'expire' => $data['expire'], |
|
| 196 | + 'ttl' => $data['ttl'] |
|
| 197 | + ); |
|
| 198 | + return $info; |
|
| 199 | + } |
|
| 200 | + $this->logger->info('This cache already expired return false'); |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Used to delete expired cache data |
|
| 207 | - */ |
|
| 208 | - public function deleteExpiredCache(){ |
|
| 209 | - $this->logger->debug('Deleting of expired cache files'); |
|
| 210 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 211 | - if(! $list){ |
|
| 212 | - $this->logger->info('No cache files were found skipping'); |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 216 | - foreach ($list as $file) { |
|
| 217 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 218 | - $data = file_get_contents($file); |
|
| 219 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 220 | - if(! $data){ |
|
| 221 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 222 | - } |
|
| 223 | - else if(time() > $data['expire']){ |
|
| 224 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 225 | - unlink($file); |
|
| 226 | - } |
|
| 227 | - else{ |
|
| 228 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 205 | + /** |
|
| 206 | + * Used to delete expired cache data |
|
| 207 | + */ |
|
| 208 | + public function deleteExpiredCache(){ |
|
| 209 | + $this->logger->debug('Deleting of expired cache files'); |
|
| 210 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 211 | + if(! $list){ |
|
| 212 | + $this->logger->info('No cache files were found skipping'); |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 216 | + foreach ($list as $file) { |
|
| 217 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 218 | + $data = file_get_contents($file); |
|
| 219 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 220 | + if(! $data){ |
|
| 221 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 222 | + } |
|
| 223 | + else if(time() > $data['expire']){ |
|
| 224 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 225 | + unlink($file); |
|
| 226 | + } |
|
| 227 | + else{ |
|
| 228 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Remove all file from cache folder |
|
| 236 | - */ |
|
| 237 | - public function clean(){ |
|
| 238 | - $this->logger->debug('Deleting of all cache files'); |
|
| 239 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 240 | - if(! $list){ |
|
| 241 | - $this->logger->info('No cache files were found skipping'); |
|
| 242 | - } |
|
| 243 | - else{ |
|
| 244 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 245 | - foreach ($list as $file) { |
|
| 246 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 247 | - unlink($file); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } |
|
| 234 | + /** |
|
| 235 | + * Remove all file from cache folder |
|
| 236 | + */ |
|
| 237 | + public function clean(){ |
|
| 238 | + $this->logger->debug('Deleting of all cache files'); |
|
| 239 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 240 | + if(! $list){ |
|
| 241 | + $this->logger->info('No cache files were found skipping'); |
|
| 242 | + } |
|
| 243 | + else{ |
|
| 244 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 245 | + foreach ($list as $file) { |
|
| 246 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 247 | + unlink($file); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * @return boolean |
|
| 254 | - */ |
|
| 255 | - public function isCompressCacheData(){ |
|
| 256 | - return $this->compressCacheData; |
|
| 257 | - } |
|
| 252 | + /** |
|
| 253 | + * @return boolean |
|
| 254 | + */ |
|
| 255 | + public function isCompressCacheData(){ |
|
| 256 | + return $this->compressCacheData; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * @param boolean $compressCacheData |
|
| 261 | - * |
|
| 262 | - * @return object |
|
| 263 | - */ |
|
| 264 | - public function setCompressCacheData($status = true){ |
|
| 265 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 266 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 259 | + /** |
|
| 260 | + * @param boolean $compressCacheData |
|
| 261 | + * |
|
| 262 | + * @return object |
|
| 263 | + */ |
|
| 264 | + public function setCompressCacheData($status = true){ |
|
| 265 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 266 | + if($status === true && ! extension_loaded('zlib')){ |
|
| 267 | 267 | |
| 268 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 269 | - $this->compressCacheData = false; |
|
| 270 | - } |
|
| 271 | - else{ |
|
| 272 | - $this->compressCacheData = $status; |
|
| 273 | - } |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 268 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 269 | + $this->compressCacheData = false; |
|
| 270 | + } |
|
| 271 | + else{ |
|
| 272 | + $this->compressCacheData = $status; |
|
| 273 | + } |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * Check whether the cache feature for the handle is supported |
|
| 279 | - * |
|
| 280 | - * @return bool |
|
| 281 | - */ |
|
| 282 | - public function isSupported(){ |
|
| 283 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 284 | - } |
|
| 277 | + /** |
|
| 278 | + * Check whether the cache feature for the handle is supported |
|
| 279 | + * |
|
| 280 | + * @return bool |
|
| 281 | + */ |
|
| 282 | + public function isSupported(){ |
|
| 283 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Return the Log instance |
|
| 288 | - * @return object |
|
| 289 | - */ |
|
| 290 | - public function getLogger(){ |
|
| 291 | - return $this->logger; |
|
| 292 | - } |
|
| 286 | + /** |
|
| 287 | + * Return the Log instance |
|
| 288 | + * @return object |
|
| 289 | + */ |
|
| 290 | + public function getLogger(){ |
|
| 291 | + return $this->logger; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Set the log instance |
|
| 296 | - * @param Log $logger the log object |
|
| 297 | - */ |
|
| 298 | - public function setLogger(Log $logger){ |
|
| 299 | - $this->logger = $logger; |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 294 | + /** |
|
| 295 | + * Set the log instance |
|
| 296 | + * @param Log $logger the log object |
|
| 297 | + */ |
|
| 298 | + public function setLogger(Log $logger){ |
|
| 299 | + $this->logger = $logger; |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Get the cache file full path for the given key |
|
| 305 | - * |
|
| 306 | - * @param string $key the cache item key |
|
| 307 | - * @return string the full cache file path for this key |
|
| 308 | - */ |
|
| 309 | - private function getFilePath($key){ |
|
| 310 | - return CACHE_PATH . md5($key) . '.cache'; |
|
| 311 | - } |
|
| 312 | - } |
|
| 303 | + /** |
|
| 304 | + * Get the cache file full path for the given key |
|
| 305 | + * |
|
| 306 | + * @param string $key the cache item key |
|
| 307 | + * @return string the full cache file path for this key |
|
| 308 | + */ |
|
| 309 | + private function getFilePath($key){ |
|
| 310 | + return CACHE_PATH . md5($key) . '.cache'; |
|
| 311 | + } |
|
| 312 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | private $logger; |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 42 | + public function __construct(Log $logger = null) { |
|
| 43 | + if (!$this->isSupported()) { |
|
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | 47 | * instance of the Log class |
| 48 | 48 | */ |
| 49 | - if(is_object($logger)){ |
|
| 49 | + if (is_object($logger)) { |
|
| 50 | 50 | $this->logger = $logger; |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 52 | + else { |
|
| 53 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 54 | 54 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | - if(! extension_loaded('zlib')){ |
|
| 58 | + if (!extension_loaded('zlib')) { |
|
| 59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 60 | 60 | $this->compressCacheData = false; |
| 61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | * @param string $key the key to identify the cache data |
| 67 | 67 | * @return mixed the cache data if exists else return false |
| 68 | 68 | */ |
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 69 | + public function get($key) { |
|
| 70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 71 | 71 | $filePath = $this->getFilePath($key); |
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 72 | + if (!file_exists($filePath)) { |
|
| 73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath, 'r'); |
|
| 78 | + if (!is_resource($handle)) { |
|
| 79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | $data = file_get_contents($filePath); |
| 85 | 85 | fclose($handle); |
| 86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 87 | + if (!$data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
| 90 | 90 | unlink($filePath); |
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | 93 | if (time() > $data['expire']) { |
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 95 | 95 | // Unlinking when the file was expired |
| 96 | 96 | unlink($filePath); |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 99 | + else { |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | 101 | return $data['data']; |
| 102 | 102 | } |
| 103 | 103 | } |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | * @param integer $ttl the cache life time |
| 111 | 111 | * @return boolean true if success otherwise will return false |
| 112 | 112 | */ |
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 113 | + public function set($key, $data, $ttl = 0) { |
|
| 114 | 114 | $expire = time() + $ttl; |
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 115 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | 116 | $filePath = $this->getFilePath($key); |
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 117 | + $handle = fopen($filePath, 'w'); |
|
| 118 | + if (!is_resource($handle)) { |
|
| 119 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | 131 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 132 | + if (!$result) { |
|
| 133 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 134 | 134 | fclose($handle); |
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 137 | + else { |
|
| 138 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 139 | 139 | fclose($handle); |
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
@@ -149,16 +149,16 @@ discard block |
||
| 149 | 149 | * @return boolean true if the cache is delete, false if can't delete |
| 150 | 150 | * the cache or the cache with the given key not exist |
| 151 | 151 | */ |
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 152 | + public function delete($key) { |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 154 | 154 | $filePath = $this->getFilePath($key); |
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 155 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 156 | + if (!file_exists($filePath)) { |
|
| 157 | 157 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 160 | + else { |
|
| 161 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 162 | 162 | unlink($filePath); |
| 163 | 163 | return true; |
| 164 | 164 | } |
@@ -172,23 +172,23 @@ discard block |
||
| 172 | 172 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 173 | 173 | * 'ttl' => the time to live of the cache in second |
| 174 | 174 | */ |
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 175 | + public function getInfo($key) { |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 177 | 177 | $filePath = $this->getFilePath($key); |
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 178 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 179 | + if (!file_exists($filePath)) { |
|
| 180 | 180 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 181 | return false; |
| 182 | 182 | } |
| 183 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 183 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 184 | 184 | $data = file_get_contents($filePath); |
| 185 | 185 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 186 | - if(! $data){ |
|
| 186 | + if (!$data) { |
|
| 187 | 187 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 188 | 188 | return false; |
| 189 | 189 | } |
| 190 | 190 | $this->logger->info('This cache data is OK check for expire'); |
| 191 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 191 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 192 | 192 | $this->logger->info('This cache not yet expired return cache informations'); |
| 193 | 193 | $info = array( |
| 194 | 194 | 'mtime' => $data['mtime'], |
@@ -205,26 +205,26 @@ discard block |
||
| 205 | 205 | /** |
| 206 | 206 | * Used to delete expired cache data |
| 207 | 207 | */ |
| 208 | - public function deleteExpiredCache(){ |
|
| 208 | + public function deleteExpiredCache() { |
|
| 209 | 209 | $this->logger->debug('Deleting of expired cache files'); |
| 210 | 210 | $list = glob(CACHE_PATH . '*.cache'); |
| 211 | - if(! $list){ |
|
| 211 | + if (!$list) { |
|
| 212 | 212 | $this->logger->info('No cache files were found skipping'); |
| 213 | 213 | } |
| 214 | - else{ |
|
| 214 | + else { |
|
| 215 | 215 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 216 | 216 | foreach ($list as $file) { |
| 217 | 217 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 218 | 218 | $data = file_get_contents($file); |
| 219 | 219 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 220 | - if(! $data){ |
|
| 220 | + if (!$data) { |
|
| 221 | 221 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 222 | 222 | } |
| 223 | - else if(time() > $data['expire']){ |
|
| 223 | + else if (time() > $data['expire']) { |
|
| 224 | 224 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 225 | 225 | unlink($file); |
| 226 | 226 | } |
| 227 | - else{ |
|
| 227 | + else { |
|
| 228 | 228 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 229 | 229 | } |
| 230 | 230 | } |
@@ -234,13 +234,13 @@ discard block |
||
| 234 | 234 | /** |
| 235 | 235 | * Remove all file from cache folder |
| 236 | 236 | */ |
| 237 | - public function clean(){ |
|
| 237 | + public function clean() { |
|
| 238 | 238 | $this->logger->debug('Deleting of all cache files'); |
| 239 | 239 | $list = glob(CACHE_PATH . '*.cache'); |
| 240 | - if(! $list){ |
|
| 240 | + if (!$list) { |
|
| 241 | 241 | $this->logger->info('No cache files were found skipping'); |
| 242 | 242 | } |
| 243 | - else{ |
|
| 243 | + else { |
|
| 244 | 244 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 245 | 245 | foreach ($list as $file) { |
| 246 | 246 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | /** |
| 253 | 253 | * @return boolean |
| 254 | 254 | */ |
| 255 | - public function isCompressCacheData(){ |
|
| 255 | + public function isCompressCacheData() { |
|
| 256 | 256 | return $this->compressCacheData; |
| 257 | 257 | } |
| 258 | 258 | |
@@ -261,14 +261,14 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return object |
| 263 | 263 | */ |
| 264 | - public function setCompressCacheData($status = true){ |
|
| 264 | + public function setCompressCacheData($status = true) { |
|
| 265 | 265 | //if Zlib extension is not loaded set compressCacheData to false |
| 266 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 266 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 267 | 267 | |
| 268 | 268 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 269 | 269 | $this->compressCacheData = false; |
| 270 | 270 | } |
| 271 | - else{ |
|
| 271 | + else { |
|
| 272 | 272 | $this->compressCacheData = $status; |
| 273 | 273 | } |
| 274 | 274 | return $this; |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | * |
| 280 | 280 | * @return bool |
| 281 | 281 | */ |
| 282 | - public function isSupported(){ |
|
| 282 | + public function isSupported() { |
|
| 283 | 283 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 284 | 284 | } |
| 285 | 285 | |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | * Return the Log instance |
| 288 | 288 | * @return object |
| 289 | 289 | */ |
| 290 | - public function getLogger(){ |
|
| 290 | + public function getLogger() { |
|
| 291 | 291 | return $this->logger; |
| 292 | 292 | } |
| 293 | 293 | |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | * Set the log instance |
| 296 | 296 | * @param Log $logger the log object |
| 297 | 297 | */ |
| 298 | - public function setLogger(Log $logger){ |
|
| 298 | + public function setLogger(Log $logger) { |
|
| 299 | 299 | $this->logger = $logger; |
| 300 | 300 | return $this; |
| 301 | 301 | } |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @param string $key the cache item key |
| 307 | 307 | * @return string the full cache file path for this key |
| 308 | 308 | */ |
| 309 | - private function getFilePath($key){ |
|
| 309 | + private function getFilePath($key) { |
|
| 310 | 310 | return CACHE_PATH . md5($key) . '.cache'; |
| 311 | 311 | } |
| 312 | 312 | } |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | if(is_object($logger)){ |
| 50 | 50 | $this->logger = $logger; |
| 51 | - } |
|
| 52 | - else{ |
|
| 51 | + } else{ |
|
| 53 | 52 | $this->logger =& class_loader('Log', 'classes'); |
| 54 | 53 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 54 | } |
@@ -95,8 +94,7 @@ discard block |
||
| 95 | 94 | // Unlinking when the file was expired |
| 96 | 95 | unlink($filePath); |
| 97 | 96 | return false; |
| 98 | - } |
|
| 99 | - else{ |
|
| 97 | + } else{ |
|
| 100 | 98 | $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
| 101 | 99 | return $data['data']; |
| 102 | 100 | } |
@@ -133,8 +131,7 @@ discard block |
||
| 133 | 131 | $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
| 134 | 132 | fclose($handle); |
| 135 | 133 | return false; |
| 136 | - } |
|
| 137 | - else{ |
|
| 134 | + } else{ |
|
| 138 | 135 | $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
| 139 | 136 | fclose($handle); |
| 140 | 137 | chmod($filePath, 0640); |
@@ -156,8 +153,7 @@ discard block |
||
| 156 | 153 | if(! file_exists($filePath)){ |
| 157 | 154 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 155 | return false; |
| 159 | - } |
|
| 160 | - else{ |
|
| 156 | + } else{ |
|
| 161 | 157 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 162 | 158 | unlink($filePath); |
| 163 | 159 | return true; |
@@ -210,8 +206,7 @@ discard block |
||
| 210 | 206 | $list = glob(CACHE_PATH . '*.cache'); |
| 211 | 207 | if(! $list){ |
| 212 | 208 | $this->logger->info('No cache files were found skipping'); |
| 213 | - } |
|
| 214 | - else{ |
|
| 209 | + } else{ |
|
| 215 | 210 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 216 | 211 | foreach ($list as $file) { |
| 217 | 212 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -219,12 +214,10 @@ discard block |
||
| 219 | 214 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 220 | 215 | if(! $data){ |
| 221 | 216 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 222 | - } |
|
| 223 | - else if(time() > $data['expire']){ |
|
| 217 | + } else if(time() > $data['expire']){ |
|
| 224 | 218 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 225 | 219 | unlink($file); |
| 226 | - } |
|
| 227 | - else{ |
|
| 220 | + } else{ |
|
| 228 | 221 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 229 | 222 | } |
| 230 | 223 | } |
@@ -239,8 +232,7 @@ discard block |
||
| 239 | 232 | $list = glob(CACHE_PATH . '*.cache'); |
| 240 | 233 | if(! $list){ |
| 241 | 234 | $this->logger->info('No cache files were found skipping'); |
| 242 | - } |
|
| 243 | - else{ |
|
| 235 | + } else{ |
|
| 244 | 236 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 245 | 237 | foreach ($list as $file) { |
| 246 | 238 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -267,8 +259,7 @@ discard block |
||
| 267 | 259 | |
| 268 | 260 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 269 | 261 | $this->compressCacheData = false; |
| 270 | - } |
|
| 271 | - else{ |
|
| 262 | + } else{ |
|
| 272 | 263 | $this->compressCacheData = $status; |
| 273 | 264 | } |
| 274 | 265 | return $this; |
@@ -1,624 +1,624 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - class Router { |
|
| 28 | - /** |
|
| 29 | - * @var array $pattern: The list of URIs to validate against |
|
| 30 | - */ |
|
| 31 | - private $pattern = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $callback: The list of callback to call |
|
| 35 | - */ |
|
| 36 | - private $callback = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | - */ |
|
| 41 | - protected $uriTrim = '/\^$'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $uri: The route URI to use |
|
| 45 | - */ |
|
| 46 | - protected $uri = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The module name of the current request |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $module = null; |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + class Router { |
|
| 28 | + /** |
|
| 29 | + * @var array $pattern: The list of URIs to validate against |
|
| 30 | + */ |
|
| 31 | + private $pattern = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $callback: The list of callback to call |
|
| 35 | + */ |
|
| 36 | + private $callback = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | + */ |
|
| 41 | + protected $uriTrim = '/\^$'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $uri: The route URI to use |
|
| 45 | + */ |
|
| 46 | + protected $uri = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The module name of the current request |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $module = null; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * The controller name of the current request |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - protected $controller = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The controller path |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - protected $controllerPath = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The method name. The default value is "index" |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - protected $method = 'index'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * List of argument to pass to the method |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $args = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * List of routes configurations |
|
| 80 | - * @var array |
|
| 81 | - */ |
|
| 82 | - protected $routes = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The segments array for the current request |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected $segments = array(); |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * The logger instance |
|
| 92 | - * @var Log |
|
| 93 | - */ |
|
| 94 | - private $logger; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Construct the new Router instance |
|
| 98 | - */ |
|
| 99 | - public function __construct(){ |
|
| 100 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 54 | + /** |
|
| 55 | + * The controller name of the current request |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + protected $controller = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The controller path |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + protected $controllerPath = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The method name. The default value is "index" |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + protected $method = 'index'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * List of argument to pass to the method |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $args = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * List of routes configurations |
|
| 80 | + * @var array |
|
| 81 | + */ |
|
| 82 | + protected $routes = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The segments array for the current request |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected $segments = array(); |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * The logger instance |
|
| 92 | + * @var Log |
|
| 93 | + */ |
|
| 94 | + private $logger; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Construct the new Router instance |
|
| 98 | + */ |
|
| 99 | + public function __construct(){ |
|
| 100 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 101 | 101 | |
| 102 | - //loading routes for module |
|
| 103 | - $moduleRouteList = array(); |
|
| 104 | - $modulesRoutes = Module::getModulesRoutes(); |
|
| 105 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 106 | - $moduleRouteList = $modulesRoutes; |
|
| 107 | - unset($modulesRoutes); |
|
| 108 | - } |
|
| 109 | - $this->setRouteConfiguration($moduleRouteList); |
|
| 110 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 111 | - |
|
| 112 | - //Set route informations |
|
| 113 | - $this->setRouteConfigurationInfos(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Get the route patterns |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - public function getPattern(){ |
|
| 121 | - return $this->pattern; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get the route callbacks |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function getCallback(){ |
|
| 129 | - return $this->callback; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Get the module name |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function getModule(){ |
|
| 137 | - return $this->module; |
|
| 138 | - } |
|
| 102 | + //loading routes for module |
|
| 103 | + $moduleRouteList = array(); |
|
| 104 | + $modulesRoutes = Module::getModulesRoutes(); |
|
| 105 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 106 | + $moduleRouteList = $modulesRoutes; |
|
| 107 | + unset($modulesRoutes); |
|
| 108 | + } |
|
| 109 | + $this->setRouteConfiguration($moduleRouteList); |
|
| 110 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 111 | + |
|
| 112 | + //Set route informations |
|
| 113 | + $this->setRouteConfigurationInfos(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Get the route patterns |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + public function getPattern(){ |
|
| 121 | + return $this->pattern; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get the route callbacks |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function getCallback(){ |
|
| 129 | + return $this->callback; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Get the module name |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function getModule(){ |
|
| 137 | + return $this->module; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Get the controller name |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getController(){ |
|
| 145 | - return $this->controller; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get the controller file path |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function getControllerPath(){ |
|
| 153 | - return $this->controllerPath; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get the controller method |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - public function getMethod(){ |
|
| 161 | - return $this->method; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Get the request arguments |
|
| 166 | - * @return array |
|
| 167 | - */ |
|
| 168 | - public function getArgs(){ |
|
| 169 | - return $this->args; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Get the URL segments array |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function getSegments(){ |
|
| 177 | - return $this->segments; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Return the Log instance |
|
| 182 | - * @return Log |
|
| 183 | - */ |
|
| 184 | - public function getLogger(){ |
|
| 185 | - return $this->logger; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the log instance |
|
| 190 | - * @param Log $logger the log object |
|
| 191 | - * @return object |
|
| 192 | - */ |
|
| 193 | - public function setLogger($logger){ |
|
| 194 | - $this->logger = $logger; |
|
| 195 | - return $this; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get the route URI |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function getRouteUri(){ |
|
| 203 | - return $this->uri; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add the URI and callback to the list of URIs to validate |
|
| 208 | - * |
|
| 209 | - * @param string $uri the request URI |
|
| 210 | - * @param string $callback the callback function |
|
| 211 | - * |
|
| 212 | - * @return object the current instance |
|
| 213 | - */ |
|
| 214 | - public function add($uri, $callback) { |
|
| 215 | - $uri = trim($uri, $this->uriTrim); |
|
| 216 | - if(in_array($uri, $this->pattern)){ |
|
| 217 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 218 | - } |
|
| 219 | - $this->pattern[] = $uri; |
|
| 220 | - $this->callback[] = $callback; |
|
| 221 | - return $this; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Remove the route configuration |
|
| 226 | - * |
|
| 227 | - * @param string $uri the URI |
|
| 228 | - * |
|
| 229 | - * @return object the current instance |
|
| 230 | - */ |
|
| 231 | - public function removeRoute($uri) { |
|
| 232 | - $index = array_search($uri, $this->pattern, true); |
|
| 233 | - if($index !== false){ |
|
| 234 | - $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 235 | - unset($this->pattern[$index]); |
|
| 236 | - unset($this->callback[$index]); |
|
| 237 | - } |
|
| 238 | - return $this; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Remove all the routes from the configuration |
|
| 244 | - * |
|
| 245 | - * @return object the current instance |
|
| 246 | - */ |
|
| 247 | - public function removeAllRoute() { |
|
| 248 | - $this->logger->info('Remove all routes from the configuration'); |
|
| 249 | - $this->pattern = array(); |
|
| 250 | - $this->callback = array(); |
|
| 251 | - $this->routes = array(); |
|
| 252 | - return $this; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Set the route URI to use later |
|
| 258 | - * @param string $uri the route URI, if is empty will determine automatically |
|
| 259 | - * @return object |
|
| 260 | - */ |
|
| 261 | - public function setRouteUri($uri = ''){ |
|
| 262 | - $routeUri = ''; |
|
| 263 | - if(! empty($uri)){ |
|
| 264 | - $routeUri = $uri; |
|
| 265 | - } |
|
| 266 | - //if the application is running in CLI mode use the first argument |
|
| 267 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 268 | - $routeUri = $_SERVER['argv'][1]; |
|
| 269 | - } |
|
| 270 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 271 | - $routeUri = $_SERVER['REQUEST_URI']; |
|
| 272 | - } |
|
| 273 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 274 | - //remove url suffix from the request URI |
|
| 275 | - $suffix = get_config('url_suffix'); |
|
| 276 | - if ($suffix) { |
|
| 277 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 278 | - $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 279 | - } |
|
| 280 | - if (strpos($routeUri, '?') !== false){ |
|
| 281 | - $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 282 | - } |
|
| 283 | - $this->uri = trim($routeUri, $this->uriTrim); |
|
| 284 | - return $this; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Set the route segments informations |
|
| 289 | - * @param array $segements the route segments information |
|
| 290 | - * |
|
| 291 | - * @return object |
|
| 292 | - */ |
|
| 293 | - public function setRouteSegments(array $segments = array()){ |
|
| 294 | - if(! empty($segments)){ |
|
| 295 | - $this->segments = $segments; |
|
| 296 | - } else if (!empty($this->uri)) { |
|
| 297 | - $this->segments = explode('/', $this->uri); |
|
| 298 | - } |
|
| 299 | - $segment = $this->segments; |
|
| 300 | - $baseUrl = get_config('base_url'); |
|
| 301 | - //check if the app is not in DOCUMENT_ROOT |
|
| 302 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 303 | - array_shift($segment); |
|
| 304 | - $this->segments = $segment; |
|
| 305 | - } |
|
| 306 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 307 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 308 | - $this->logger->info('The request URI contains the front controller'); |
|
| 309 | - array_shift($segment); |
|
| 310 | - $this->segments = $segment; |
|
| 311 | - } |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Setting the route parameters like module, controller, method, argument |
|
| 317 | - * @return object the current instance |
|
| 318 | - */ |
|
| 319 | - public function determineRouteParamsInformation() { |
|
| 320 | - $this->logger->debug('Routing process start ...'); |
|
| 140 | + /** |
|
| 141 | + * Get the controller name |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getController(){ |
|
| 145 | + return $this->controller; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get the controller file path |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function getControllerPath(){ |
|
| 153 | + return $this->controllerPath; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get the controller method |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + public function getMethod(){ |
|
| 161 | + return $this->method; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Get the request arguments |
|
| 166 | + * @return array |
|
| 167 | + */ |
|
| 168 | + public function getArgs(){ |
|
| 169 | + return $this->args; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Get the URL segments array |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function getSegments(){ |
|
| 177 | + return $this->segments; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Return the Log instance |
|
| 182 | + * @return Log |
|
| 183 | + */ |
|
| 184 | + public function getLogger(){ |
|
| 185 | + return $this->logger; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the log instance |
|
| 190 | + * @param Log $logger the log object |
|
| 191 | + * @return object |
|
| 192 | + */ |
|
| 193 | + public function setLogger($logger){ |
|
| 194 | + $this->logger = $logger; |
|
| 195 | + return $this; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get the route URI |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function getRouteUri(){ |
|
| 203 | + return $this->uri; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add the URI and callback to the list of URIs to validate |
|
| 208 | + * |
|
| 209 | + * @param string $uri the request URI |
|
| 210 | + * @param string $callback the callback function |
|
| 211 | + * |
|
| 212 | + * @return object the current instance |
|
| 213 | + */ |
|
| 214 | + public function add($uri, $callback) { |
|
| 215 | + $uri = trim($uri, $this->uriTrim); |
|
| 216 | + if(in_array($uri, $this->pattern)){ |
|
| 217 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 218 | + } |
|
| 219 | + $this->pattern[] = $uri; |
|
| 220 | + $this->callback[] = $callback; |
|
| 221 | + return $this; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Remove the route configuration |
|
| 226 | + * |
|
| 227 | + * @param string $uri the URI |
|
| 228 | + * |
|
| 229 | + * @return object the current instance |
|
| 230 | + */ |
|
| 231 | + public function removeRoute($uri) { |
|
| 232 | + $index = array_search($uri, $this->pattern, true); |
|
| 233 | + if($index !== false){ |
|
| 234 | + $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 235 | + unset($this->pattern[$index]); |
|
| 236 | + unset($this->callback[$index]); |
|
| 237 | + } |
|
| 238 | + return $this; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Remove all the routes from the configuration |
|
| 244 | + * |
|
| 245 | + * @return object the current instance |
|
| 246 | + */ |
|
| 247 | + public function removeAllRoute() { |
|
| 248 | + $this->logger->info('Remove all routes from the configuration'); |
|
| 249 | + $this->pattern = array(); |
|
| 250 | + $this->callback = array(); |
|
| 251 | + $this->routes = array(); |
|
| 252 | + return $this; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Set the route URI to use later |
|
| 258 | + * @param string $uri the route URI, if is empty will determine automatically |
|
| 259 | + * @return object |
|
| 260 | + */ |
|
| 261 | + public function setRouteUri($uri = ''){ |
|
| 262 | + $routeUri = ''; |
|
| 263 | + if(! empty($uri)){ |
|
| 264 | + $routeUri = $uri; |
|
| 265 | + } |
|
| 266 | + //if the application is running in CLI mode use the first argument |
|
| 267 | + else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 268 | + $routeUri = $_SERVER['argv'][1]; |
|
| 269 | + } |
|
| 270 | + else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 271 | + $routeUri = $_SERVER['REQUEST_URI']; |
|
| 272 | + } |
|
| 273 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 274 | + //remove url suffix from the request URI |
|
| 275 | + $suffix = get_config('url_suffix'); |
|
| 276 | + if ($suffix) { |
|
| 277 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 278 | + $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 279 | + } |
|
| 280 | + if (strpos($routeUri, '?') !== false){ |
|
| 281 | + $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 282 | + } |
|
| 283 | + $this->uri = trim($routeUri, $this->uriTrim); |
|
| 284 | + return $this; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Set the route segments informations |
|
| 289 | + * @param array $segements the route segments information |
|
| 290 | + * |
|
| 291 | + * @return object |
|
| 292 | + */ |
|
| 293 | + public function setRouteSegments(array $segments = array()){ |
|
| 294 | + if(! empty($segments)){ |
|
| 295 | + $this->segments = $segments; |
|
| 296 | + } else if (!empty($this->uri)) { |
|
| 297 | + $this->segments = explode('/', $this->uri); |
|
| 298 | + } |
|
| 299 | + $segment = $this->segments; |
|
| 300 | + $baseUrl = get_config('base_url'); |
|
| 301 | + //check if the app is not in DOCUMENT_ROOT |
|
| 302 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 303 | + array_shift($segment); |
|
| 304 | + $this->segments = $segment; |
|
| 305 | + } |
|
| 306 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 307 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 308 | + $this->logger->info('The request URI contains the front controller'); |
|
| 309 | + array_shift($segment); |
|
| 310 | + $this->segments = $segment; |
|
| 311 | + } |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Setting the route parameters like module, controller, method, argument |
|
| 317 | + * @return object the current instance |
|
| 318 | + */ |
|
| 319 | + public function determineRouteParamsInformation() { |
|
| 320 | + $this->logger->debug('Routing process start ...'); |
|
| 321 | 321 | |
| 322 | - //determine route parameters using the config |
|
| 323 | - $this->determineRouteParamsFromConfig(); |
|
| 322 | + //determine route parameters using the config |
|
| 323 | + $this->determineRouteParamsFromConfig(); |
|
| 324 | 324 | |
| 325 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 326 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 327 | - if(! $this->controller){ |
|
| 328 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 329 | - //determine route parameters using the REQUEST_URI param |
|
| 330 | - $this->determineRouteParamsFromRequestUri(); |
|
| 331 | - } |
|
| 332 | - //Set the controller file path if not yet set |
|
| 333 | - $this->setControllerFilePath(); |
|
| 334 | - $this->logger->debug('Routing process end.'); |
|
| 335 | - |
|
| 336 | - return $this; |
|
| 337 | - } |
|
| 325 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 326 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 327 | + if(! $this->controller){ |
|
| 328 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 329 | + //determine route parameters using the REQUEST_URI param |
|
| 330 | + $this->determineRouteParamsFromRequestUri(); |
|
| 331 | + } |
|
| 332 | + //Set the controller file path if not yet set |
|
| 333 | + $this->setControllerFilePath(); |
|
| 334 | + $this->logger->debug('Routing process end.'); |
|
| 335 | + |
|
| 336 | + return $this; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Routing the request to the correspondant module/controller/method if exists |
|
| 341 | - * otherwise send 404 error. |
|
| 342 | - */ |
|
| 343 | - public function processRequest(){ |
|
| 344 | - //Setting the route URI |
|
| 345 | - $this->setRouteUri(); |
|
| 346 | - |
|
| 347 | - //setting route segments |
|
| 348 | - $this->setRouteSegments(); |
|
| 349 | - |
|
| 350 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 351 | - |
|
| 352 | - //determine the route parameters information |
|
| 353 | - $this->determineRouteParamsInformation(); |
|
| 354 | - |
|
| 355 | - $e404 = false; |
|
| 356 | - $classFilePath = $this->controllerPath; |
|
| 357 | - $controller = ucfirst($this->controller); |
|
| 358 | - $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 359 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 339 | + /** |
|
| 340 | + * Routing the request to the correspondant module/controller/method if exists |
|
| 341 | + * otherwise send 404 error. |
|
| 342 | + */ |
|
| 343 | + public function processRequest(){ |
|
| 344 | + //Setting the route URI |
|
| 345 | + $this->setRouteUri(); |
|
| 346 | + |
|
| 347 | + //setting route segments |
|
| 348 | + $this->setRouteSegments(); |
|
| 349 | + |
|
| 350 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 351 | + |
|
| 352 | + //determine the route parameters information |
|
| 353 | + $this->determineRouteParamsInformation(); |
|
| 354 | + |
|
| 355 | + $e404 = false; |
|
| 356 | + $classFilePath = $this->controllerPath; |
|
| 357 | + $controller = ucfirst($this->controller); |
|
| 358 | + $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 359 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 360 | 360 | |
| 361 | - if(file_exists($classFilePath)){ |
|
| 362 | - require_once $classFilePath; |
|
| 363 | - if(! class_exists($controller, false)){ |
|
| 364 | - $e404 = true; |
|
| 365 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 366 | - } |
|
| 367 | - else{ |
|
| 368 | - $controllerInstance = new $controller(); |
|
| 369 | - $controllerMethod = $this->getMethod(); |
|
| 370 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 371 | - $e404 = true; |
|
| 372 | - $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 373 | - } |
|
| 374 | - else{ |
|
| 375 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
| 376 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 377 | - //render the final page to user |
|
| 378 | - $this->logger->info('Render the final output to the browser'); |
|
| 379 | - get_instance()->response->renderFinalPage(); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - else{ |
|
| 384 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 385 | - $e404 = true; |
|
| 386 | - } |
|
| 387 | - if($e404){ |
|
| 388 | - if(IS_CLI){ |
|
| 389 | - set_http_status_header(404); |
|
| 390 | - echo 'Error 404: page not found.'; |
|
| 391 | - } else { |
|
| 392 | - $response =& class_loader('Response', 'classes'); |
|
| 393 | - $response->send404(); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
| 401 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 402 | - * @param boolean $useConfigFile whether to use route configuration file |
|
| 403 | - * @return object |
|
| 404 | - */ |
|
| 405 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 406 | - $route = array(); |
|
| 407 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 408 | - require_once CONFIG_PATH . 'routes.php'; |
|
| 409 | - } |
|
| 410 | - $route = array_merge($route, $overwriteConfig); |
|
| 411 | - $this->routes = $route; |
|
| 412 | - //if route is empty remove all configuration |
|
| 413 | - if(empty($route)){ |
|
| 414 | - $this->removeAllRoute(); |
|
| 415 | - } |
|
| 416 | - return $this; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Get the route configuration |
|
| 421 | - * @return array |
|
| 422 | - */ |
|
| 423 | - public function getRouteConfiguration(){ |
|
| 424 | - return $this->routes; |
|
| 425 | - } |
|
| 361 | + if(file_exists($classFilePath)){ |
|
| 362 | + require_once $classFilePath; |
|
| 363 | + if(! class_exists($controller, false)){ |
|
| 364 | + $e404 = true; |
|
| 365 | + $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 366 | + } |
|
| 367 | + else{ |
|
| 368 | + $controllerInstance = new $controller(); |
|
| 369 | + $controllerMethod = $this->getMethod(); |
|
| 370 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 371 | + $e404 = true; |
|
| 372 | + $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 373 | + } |
|
| 374 | + else{ |
|
| 375 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
| 376 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 377 | + //render the final page to user |
|
| 378 | + $this->logger->info('Render the final output to the browser'); |
|
| 379 | + get_instance()->response->renderFinalPage(); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + else{ |
|
| 384 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 385 | + $e404 = true; |
|
| 386 | + } |
|
| 387 | + if($e404){ |
|
| 388 | + if(IS_CLI){ |
|
| 389 | + set_http_status_header(404); |
|
| 390 | + echo 'Error 404: page not found.'; |
|
| 391 | + } else { |
|
| 392 | + $response =& class_loader('Response', 'classes'); |
|
| 393 | + $response->send404(); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
| 401 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 402 | + * @param boolean $useConfigFile whether to use route configuration file |
|
| 403 | + * @return object |
|
| 404 | + */ |
|
| 405 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 406 | + $route = array(); |
|
| 407 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 408 | + require_once CONFIG_PATH . 'routes.php'; |
|
| 409 | + } |
|
| 410 | + $route = array_merge($route, $overwriteConfig); |
|
| 411 | + $this->routes = $route; |
|
| 412 | + //if route is empty remove all configuration |
|
| 413 | + if(empty($route)){ |
|
| 414 | + $this->removeAllRoute(); |
|
| 415 | + } |
|
| 416 | + return $this; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Get the route configuration |
|
| 421 | + * @return array |
|
| 422 | + */ |
|
| 423 | + public function getRouteConfiguration(){ |
|
| 424 | + return $this->routes; |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | 427 | |
| 428 | - /** |
|
| 429 | - * Set the controller file path if is not set |
|
| 430 | - * @param string $path the file path if is null will using the route |
|
| 431 | - * information |
|
| 432 | - * |
|
| 433 | - * @return object the current instance |
|
| 434 | - */ |
|
| 435 | - public function setControllerFilePath($path = null){ |
|
| 436 | - if($path !== null){ |
|
| 437 | - $this->controllerPath = $path; |
|
| 438 | - return $this; |
|
| 439 | - } |
|
| 440 | - //did we set the controller, so set the controller path |
|
| 441 | - if($this->controller && ! $this->controllerPath){ |
|
| 442 | - $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 443 | - $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 444 | - //if the controller is in module |
|
| 445 | - if($this->module){ |
|
| 446 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 447 | - if($path !== false){ |
|
| 448 | - $controllerPath = $path; |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - $this->controllerPath = $controllerPath; |
|
| 452 | - } |
|
| 453 | - return $this; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Determine the route parameters from route configuration |
|
| 458 | - * @return void |
|
| 459 | - */ |
|
| 460 | - protected function determineRouteParamsFromConfig(){ |
|
| 461 | - $uri = implode('/', $this->segments); |
|
| 462 | - /* |
|
| 428 | + /** |
|
| 429 | + * Set the controller file path if is not set |
|
| 430 | + * @param string $path the file path if is null will using the route |
|
| 431 | + * information |
|
| 432 | + * |
|
| 433 | + * @return object the current instance |
|
| 434 | + */ |
|
| 435 | + public function setControllerFilePath($path = null){ |
|
| 436 | + if($path !== null){ |
|
| 437 | + $this->controllerPath = $path; |
|
| 438 | + return $this; |
|
| 439 | + } |
|
| 440 | + //did we set the controller, so set the controller path |
|
| 441 | + if($this->controller && ! $this->controllerPath){ |
|
| 442 | + $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 443 | + $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 444 | + //if the controller is in module |
|
| 445 | + if($this->module){ |
|
| 446 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 447 | + if($path !== false){ |
|
| 448 | + $controllerPath = $path; |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + $this->controllerPath = $controllerPath; |
|
| 452 | + } |
|
| 453 | + return $this; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Determine the route parameters from route configuration |
|
| 458 | + * @return void |
|
| 459 | + */ |
|
| 460 | + protected function determineRouteParamsFromConfig(){ |
|
| 461 | + $uri = implode('/', $this->segments); |
|
| 462 | + /* |
|
| 463 | 463 | * Generics routes patterns |
| 464 | 464 | */ |
| 465 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 466 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 467 | - |
|
| 468 | - $this->logger->debug( |
|
| 469 | - 'Begin to loop in the predefined routes configuration ' |
|
| 470 | - . 'to check if the current request match' |
|
| 471 | - ); |
|
| 472 | - |
|
| 473 | - // Cycle through the URIs stored in the array |
|
| 474 | - foreach ($this->pattern as $index => $uriList) { |
|
| 475 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 476 | - // Check for an existant matching URI |
|
| 477 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 478 | - $this->logger->info( |
|
| 479 | - 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 480 | - . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 481 | - ); |
|
| 482 | - array_shift($args); |
|
| 483 | - //check if this contains an module |
|
| 484 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 485 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 486 | - $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 487 | - $this->module = $moduleControllerMethod[0]; |
|
| 488 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 489 | - } |
|
| 490 | - else{ |
|
| 491 | - $this->logger->info('The current request does not use the module'); |
|
| 492 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 493 | - } |
|
| 494 | - if(is_array($moduleControllerMethod)){ |
|
| 495 | - if(isset($moduleControllerMethod[0])){ |
|
| 496 | - $this->controller = $moduleControllerMethod[0]; |
|
| 497 | - } |
|
| 498 | - if(isset($moduleControllerMethod[1])){ |
|
| 499 | - $this->method = $moduleControllerMethod[1]; |
|
| 500 | - } |
|
| 501 | - $this->args = $args; |
|
| 502 | - } |
|
| 503 | - // stop here |
|
| 504 | - break; |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - //first if the controller is not set and the module is set use the module name as the controller |
|
| 509 | - if(! $this->controller && $this->module){ |
|
| 510 | - $this->logger->info( |
|
| 511 | - 'After loop in predefined routes configuration, |
|
| 465 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 466 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 467 | + |
|
| 468 | + $this->logger->debug( |
|
| 469 | + 'Begin to loop in the predefined routes configuration ' |
|
| 470 | + . 'to check if the current request match' |
|
| 471 | + ); |
|
| 472 | + |
|
| 473 | + // Cycle through the URIs stored in the array |
|
| 474 | + foreach ($this->pattern as $index => $uriList) { |
|
| 475 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 476 | + // Check for an existant matching URI |
|
| 477 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 478 | + $this->logger->info( |
|
| 479 | + 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 480 | + . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 481 | + ); |
|
| 482 | + array_shift($args); |
|
| 483 | + //check if this contains an module |
|
| 484 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 485 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 486 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 487 | + $this->module = $moduleControllerMethod[0]; |
|
| 488 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 489 | + } |
|
| 490 | + else{ |
|
| 491 | + $this->logger->info('The current request does not use the module'); |
|
| 492 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 493 | + } |
|
| 494 | + if(is_array($moduleControllerMethod)){ |
|
| 495 | + if(isset($moduleControllerMethod[0])){ |
|
| 496 | + $this->controller = $moduleControllerMethod[0]; |
|
| 497 | + } |
|
| 498 | + if(isset($moduleControllerMethod[1])){ |
|
| 499 | + $this->method = $moduleControllerMethod[1]; |
|
| 500 | + } |
|
| 501 | + $this->args = $args; |
|
| 502 | + } |
|
| 503 | + // stop here |
|
| 504 | + break; |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + //first if the controller is not set and the module is set use the module name as the controller |
|
| 509 | + if(! $this->controller && $this->module){ |
|
| 510 | + $this->logger->info( |
|
| 511 | + 'After loop in predefined routes configuration, |
|
| 512 | 512 | the module name is set but the controller is not set, |
| 513 | 513 | so we will use module as the controller' |
| 514 | - ); |
|
| 515 | - $this->controller = $this->module; |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 521 | - * @return void |
|
| 522 | - */ |
|
| 523 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 524 | - $segment = $this->segments; |
|
| 525 | - $nbSegment = count($segment); |
|
| 526 | - //if segment is null so means no need to perform |
|
| 527 | - if($nbSegment > 0){ |
|
| 528 | - //get the module list |
|
| 529 | - $modules = Module::getModuleList(); |
|
| 530 | - //first check if no module |
|
| 531 | - if(empty($modules)){ |
|
| 532 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
| 533 | - //the application don't use module |
|
| 534 | - //controller |
|
| 535 | - if(isset($segment[0])){ |
|
| 536 | - $this->controller = $segment[0]; |
|
| 537 | - array_shift($segment); |
|
| 538 | - } |
|
| 539 | - //method |
|
| 540 | - if(isset($segment[0])){ |
|
| 541 | - $this->method = $segment[0]; |
|
| 542 | - array_shift($segment); |
|
| 543 | - } |
|
| 544 | - //args |
|
| 545 | - $this->args = $segment; |
|
| 546 | - } |
|
| 547 | - else{ |
|
| 548 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 549 | - if(in_array($segment[0], $modules)){ |
|
| 550 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 551 | - $this->module = $segment[0]; |
|
| 552 | - array_shift($segment); |
|
| 553 | - //check if the second arg is the controller from module |
|
| 554 | - if(isset($segment[0])){ |
|
| 555 | - $this->controller = $segment[0]; |
|
| 556 | - //check if the request use the same module name and controller |
|
| 557 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 558 | - if(! $path){ |
|
| 559 | - $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 560 | - $this->controller = $this->module; |
|
| 561 | - } |
|
| 562 | - else{ |
|
| 563 | - $this->controllerPath = $path; |
|
| 564 | - array_shift($segment); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - //check for method |
|
| 568 | - if(isset($segment[0])){ |
|
| 569 | - $this->method = $segment[0]; |
|
| 570 | - array_shift($segment); |
|
| 571 | - } |
|
| 572 | - //the remaining is for args |
|
| 573 | - $this->args = $segment; |
|
| 574 | - } |
|
| 575 | - else{ |
|
| 576 | - $this->logger->info('The current request information is not found in the module list'); |
|
| 577 | - //controller |
|
| 578 | - if(isset($segment[0])){ |
|
| 579 | - $this->controller = $segment[0]; |
|
| 580 | - array_shift($segment); |
|
| 581 | - } |
|
| 582 | - //method |
|
| 583 | - if(isset($segment[0])){ |
|
| 584 | - $this->method = $segment[0]; |
|
| 585 | - array_shift($segment); |
|
| 586 | - } |
|
| 587 | - //args |
|
| 588 | - $this->args = $segment; |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - if(! $this->controller && $this->module){ |
|
| 592 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 593 | - $this->controller = $this->module; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Set the route informations using the configuration |
|
| 600 | - * |
|
| 601 | - * @return object the current instance |
|
| 602 | - */ |
|
| 603 | - protected function setRouteConfigurationInfos(){ |
|
| 604 | - //adding route |
|
| 605 | - foreach($this->routes as $pattern => $callback){ |
|
| 606 | - $this->add($pattern, $callback); |
|
| 607 | - } |
|
| 608 | - return $this; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Set the Log instance using argument or create new instance |
|
| 613 | - * @param object $logger the Log instance if not null |
|
| 614 | - */ |
|
| 615 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 616 | - if ($logger !== null){ |
|
| 617 | - $this->logger = $logger; |
|
| 618 | - } |
|
| 619 | - else{ |
|
| 620 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 621 | - $this->logger->setLogger('Library::Router'); |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - } |
|
| 514 | + ); |
|
| 515 | + $this->controller = $this->module; |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 521 | + * @return void |
|
| 522 | + */ |
|
| 523 | + protected function determineRouteParamsFromRequestUri(){ |
|
| 524 | + $segment = $this->segments; |
|
| 525 | + $nbSegment = count($segment); |
|
| 526 | + //if segment is null so means no need to perform |
|
| 527 | + if($nbSegment > 0){ |
|
| 528 | + //get the module list |
|
| 529 | + $modules = Module::getModuleList(); |
|
| 530 | + //first check if no module |
|
| 531 | + if(empty($modules)){ |
|
| 532 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
| 533 | + //the application don't use module |
|
| 534 | + //controller |
|
| 535 | + if(isset($segment[0])){ |
|
| 536 | + $this->controller = $segment[0]; |
|
| 537 | + array_shift($segment); |
|
| 538 | + } |
|
| 539 | + //method |
|
| 540 | + if(isset($segment[0])){ |
|
| 541 | + $this->method = $segment[0]; |
|
| 542 | + array_shift($segment); |
|
| 543 | + } |
|
| 544 | + //args |
|
| 545 | + $this->args = $segment; |
|
| 546 | + } |
|
| 547 | + else{ |
|
| 548 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 549 | + if(in_array($segment[0], $modules)){ |
|
| 550 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 551 | + $this->module = $segment[0]; |
|
| 552 | + array_shift($segment); |
|
| 553 | + //check if the second arg is the controller from module |
|
| 554 | + if(isset($segment[0])){ |
|
| 555 | + $this->controller = $segment[0]; |
|
| 556 | + //check if the request use the same module name and controller |
|
| 557 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 558 | + if(! $path){ |
|
| 559 | + $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 560 | + $this->controller = $this->module; |
|
| 561 | + } |
|
| 562 | + else{ |
|
| 563 | + $this->controllerPath = $path; |
|
| 564 | + array_shift($segment); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + //check for method |
|
| 568 | + if(isset($segment[0])){ |
|
| 569 | + $this->method = $segment[0]; |
|
| 570 | + array_shift($segment); |
|
| 571 | + } |
|
| 572 | + //the remaining is for args |
|
| 573 | + $this->args = $segment; |
|
| 574 | + } |
|
| 575 | + else{ |
|
| 576 | + $this->logger->info('The current request information is not found in the module list'); |
|
| 577 | + //controller |
|
| 578 | + if(isset($segment[0])){ |
|
| 579 | + $this->controller = $segment[0]; |
|
| 580 | + array_shift($segment); |
|
| 581 | + } |
|
| 582 | + //method |
|
| 583 | + if(isset($segment[0])){ |
|
| 584 | + $this->method = $segment[0]; |
|
| 585 | + array_shift($segment); |
|
| 586 | + } |
|
| 587 | + //args |
|
| 588 | + $this->args = $segment; |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + if(! $this->controller && $this->module){ |
|
| 592 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 593 | + $this->controller = $this->module; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Set the route informations using the configuration |
|
| 600 | + * |
|
| 601 | + * @return object the current instance |
|
| 602 | + */ |
|
| 603 | + protected function setRouteConfigurationInfos(){ |
|
| 604 | + //adding route |
|
| 605 | + foreach($this->routes as $pattern => $callback){ |
|
| 606 | + $this->add($pattern, $callback); |
|
| 607 | + } |
|
| 608 | + return $this; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Set the Log instance using argument or create new instance |
|
| 613 | + * @param object $logger the Log instance if not null |
|
| 614 | + */ |
|
| 615 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 616 | + if ($logger !== null){ |
|
| 617 | + $this->logger = $logger; |
|
| 618 | + } |
|
| 619 | + else{ |
|
| 620 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 621 | + $this->logger->setLogger('Library::Router'); |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + } |
|
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | /** |
| 97 | 97 | * Construct the new Router instance |
| 98 | 98 | */ |
| 99 | - public function __construct(){ |
|
| 99 | + public function __construct() { |
|
| 100 | 100 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 101 | 101 | |
| 102 | 102 | //loading routes for module |
| 103 | 103 | $moduleRouteList = array(); |
| 104 | 104 | $modulesRoutes = Module::getModulesRoutes(); |
| 105 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 105 | + if ($modulesRoutes && is_array($modulesRoutes)) { |
|
| 106 | 106 | $moduleRouteList = $modulesRoutes; |
| 107 | 107 | unset($modulesRoutes); |
| 108 | 108 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * Get the route patterns |
| 118 | 118 | * @return array |
| 119 | 119 | */ |
| 120 | - public function getPattern(){ |
|
| 120 | + public function getPattern() { |
|
| 121 | 121 | return $this->pattern; |
| 122 | 122 | } |
| 123 | 123 | |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | * Get the route callbacks |
| 126 | 126 | * @return array |
| 127 | 127 | */ |
| 128 | - public function getCallback(){ |
|
| 128 | + public function getCallback() { |
|
| 129 | 129 | return $this->callback; |
| 130 | 130 | } |
| 131 | 131 | |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | * Get the module name |
| 134 | 134 | * @return string |
| 135 | 135 | */ |
| 136 | - public function getModule(){ |
|
| 136 | + public function getModule() { |
|
| 137 | 137 | return $this->module; |
| 138 | 138 | } |
| 139 | 139 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * Get the controller name |
| 142 | 142 | * @return string |
| 143 | 143 | */ |
| 144 | - public function getController(){ |
|
| 144 | + public function getController() { |
|
| 145 | 145 | return $this->controller; |
| 146 | 146 | } |
| 147 | 147 | |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * Get the controller file path |
| 150 | 150 | * @return string |
| 151 | 151 | */ |
| 152 | - public function getControllerPath(){ |
|
| 152 | + public function getControllerPath() { |
|
| 153 | 153 | return $this->controllerPath; |
| 154 | 154 | } |
| 155 | 155 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * Get the controller method |
| 158 | 158 | * @return string |
| 159 | 159 | */ |
| 160 | - public function getMethod(){ |
|
| 160 | + public function getMethod() { |
|
| 161 | 161 | return $this->method; |
| 162 | 162 | } |
| 163 | 163 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | * Get the request arguments |
| 166 | 166 | * @return array |
| 167 | 167 | */ |
| 168 | - public function getArgs(){ |
|
| 168 | + public function getArgs() { |
|
| 169 | 169 | return $this->args; |
| 170 | 170 | } |
| 171 | 171 | |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | * Get the URL segments array |
| 174 | 174 | * @return array |
| 175 | 175 | */ |
| 176 | - public function getSegments(){ |
|
| 176 | + public function getSegments() { |
|
| 177 | 177 | return $this->segments; |
| 178 | 178 | } |
| 179 | 179 | |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | * Return the Log instance |
| 182 | 182 | * @return Log |
| 183 | 183 | */ |
| 184 | - public function getLogger(){ |
|
| 184 | + public function getLogger() { |
|
| 185 | 185 | return $this->logger; |
| 186 | 186 | } |
| 187 | 187 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * @param Log $logger the log object |
| 191 | 191 | * @return object |
| 192 | 192 | */ |
| 193 | - public function setLogger($logger){ |
|
| 193 | + public function setLogger($logger) { |
|
| 194 | 194 | $this->logger = $logger; |
| 195 | 195 | return $this; |
| 196 | 196 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * Get the route URI |
| 200 | 200 | * @return string |
| 201 | 201 | */ |
| 202 | - public function getRouteUri(){ |
|
| 202 | + public function getRouteUri() { |
|
| 203 | 203 | return $this->uri; |
| 204 | 204 | } |
| 205 | 205 | |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | */ |
| 214 | 214 | public function add($uri, $callback) { |
| 215 | 215 | $uri = trim($uri, $this->uriTrim); |
| 216 | - if(in_array($uri, $this->pattern)){ |
|
| 216 | + if (in_array($uri, $this->pattern)) { |
|
| 217 | 217 | $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
| 218 | 218 | } |
| 219 | 219 | $this->pattern[] = $uri; |
@@ -229,8 +229,8 @@ discard block |
||
| 229 | 229 | * @return object the current instance |
| 230 | 230 | */ |
| 231 | 231 | public function removeRoute($uri) { |
| 232 | - $index = array_search($uri, $this->pattern, true); |
|
| 233 | - if($index !== false){ |
|
| 232 | + $index = array_search($uri, $this->pattern, true); |
|
| 233 | + if ($index !== false) { |
|
| 234 | 234 | $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
| 235 | 235 | unset($this->pattern[$index]); |
| 236 | 236 | unset($this->callback[$index]); |
@@ -258,26 +258,26 @@ discard block |
||
| 258 | 258 | * @param string $uri the route URI, if is empty will determine automatically |
| 259 | 259 | * @return object |
| 260 | 260 | */ |
| 261 | - public function setRouteUri($uri = ''){ |
|
| 261 | + public function setRouteUri($uri = '') { |
|
| 262 | 262 | $routeUri = ''; |
| 263 | - if(! empty($uri)){ |
|
| 263 | + if (!empty($uri)) { |
|
| 264 | 264 | $routeUri = $uri; |
| 265 | 265 | } |
| 266 | 266 | //if the application is running in CLI mode use the first argument |
| 267 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 267 | + else if (IS_CLI && isset($_SERVER['argv'][1])) { |
|
| 268 | 268 | $routeUri = $_SERVER['argv'][1]; |
| 269 | 269 | } |
| 270 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 270 | + else if (isset($_SERVER['REQUEST_URI'])) { |
|
| 271 | 271 | $routeUri = $_SERVER['REQUEST_URI']; |
| 272 | 272 | } |
| 273 | 273 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
| 274 | 274 | //remove url suffix from the request URI |
| 275 | 275 | $suffix = get_config('url_suffix'); |
| 276 | 276 | if ($suffix) { |
| 277 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 277 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']'); |
|
| 278 | 278 | $routeUri = str_ireplace($suffix, '', $routeUri); |
| 279 | 279 | } |
| 280 | - if (strpos($routeUri, '?') !== false){ |
|
| 280 | + if (strpos($routeUri, '?') !== false) { |
|
| 281 | 281 | $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
| 282 | 282 | } |
| 283 | 283 | $this->uri = trim($routeUri, $this->uriTrim); |
@@ -290,8 +290,8 @@ discard block |
||
| 290 | 290 | * |
| 291 | 291 | * @return object |
| 292 | 292 | */ |
| 293 | - public function setRouteSegments(array $segments = array()){ |
|
| 294 | - if(! empty($segments)){ |
|
| 293 | + public function setRouteSegments(array $segments = array()) { |
|
| 294 | + if (!empty($segments)) { |
|
| 295 | 295 | $this->segments = $segments; |
| 296 | 296 | } else if (!empty($this->uri)) { |
| 297 | 297 | $this->segments = explode('/', $this->uri); |
@@ -299,12 +299,12 @@ discard block |
||
| 299 | 299 | $segment = $this->segments; |
| 300 | 300 | $baseUrl = get_config('base_url'); |
| 301 | 301 | //check if the app is not in DOCUMENT_ROOT |
| 302 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 302 | + if (isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false) { |
|
| 303 | 303 | array_shift($segment); |
| 304 | 304 | $this->segments = $segment; |
| 305 | 305 | } |
| 306 | 306 | $this->logger->debug('Check if the request URI contains the front controller'); |
| 307 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 307 | + if (isset($segment[0]) && $segment[0] == SELF) { |
|
| 308 | 308 | $this->logger->info('The request URI contains the front controller'); |
| 309 | 309 | array_shift($segment); |
| 310 | 310 | $this->segments = $segment; |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | |
| 325 | 325 | //if can not determine the module/controller/method via the defined routes configuration we will use |
| 326 | 326 | //the URL like http://domain.com/module/controller/method/arg1/arg2 |
| 327 | - if(! $this->controller){ |
|
| 327 | + if (!$this->controller) { |
|
| 328 | 328 | $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
| 329 | 329 | //determine route parameters using the REQUEST_URI param |
| 330 | 330 | $this->determineRouteParamsFromRequestUri(); |
@@ -340,14 +340,14 @@ discard block |
||
| 340 | 340 | * Routing the request to the correspondant module/controller/method if exists |
| 341 | 341 | * otherwise send 404 error. |
| 342 | 342 | */ |
| 343 | - public function processRequest(){ |
|
| 343 | + public function processRequest() { |
|
| 344 | 344 | //Setting the route URI |
| 345 | 345 | $this->setRouteUri(); |
| 346 | 346 | |
| 347 | 347 | //setting route segments |
| 348 | 348 | $this->setRouteSegments(); |
| 349 | 349 | |
| 350 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 350 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']'); |
|
| 351 | 351 | |
| 352 | 352 | //determine the route parameters information |
| 353 | 353 | $this->determineRouteParamsInformation(); |
@@ -358,20 +358,20 @@ discard block |
||
| 358 | 358 | $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
| 359 | 359 | $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
| 360 | 360 | |
| 361 | - if(file_exists($classFilePath)){ |
|
| 361 | + if (file_exists($classFilePath)) { |
|
| 362 | 362 | require_once $classFilePath; |
| 363 | - if(! class_exists($controller, false)){ |
|
| 363 | + if (!class_exists($controller, false)) { |
|
| 364 | 364 | $e404 = true; |
| 365 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 365 | + $this->logger->warning('The controller file [' . $classFilePath . '] exists but does not contain the class [' . $controller . ']'); |
|
| 366 | 366 | } |
| 367 | - else{ |
|
| 367 | + else { |
|
| 368 | 368 | $controllerInstance = new $controller(); |
| 369 | 369 | $controllerMethod = $this->getMethod(); |
| 370 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 370 | + if (!method_exists($controllerInstance, $controllerMethod)) { |
|
| 371 | 371 | $e404 = true; |
| 372 | 372 | $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
| 373 | 373 | } |
| 374 | - else{ |
|
| 374 | + else { |
|
| 375 | 375 | $this->logger->info('Routing data is set correctly now GO!'); |
| 376 | 376 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
| 377 | 377 | //render the final page to user |
@@ -380,16 +380,16 @@ discard block |
||
| 380 | 380 | } |
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | - else{ |
|
| 383 | + else { |
|
| 384 | 384 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
| 385 | 385 | $e404 = true; |
| 386 | 386 | } |
| 387 | - if($e404){ |
|
| 388 | - if(IS_CLI){ |
|
| 387 | + if ($e404) { |
|
| 388 | + if (IS_CLI) { |
|
| 389 | 389 | set_http_status_header(404); |
| 390 | 390 | echo 'Error 404: page not found.'; |
| 391 | 391 | } else { |
| 392 | - $response =& class_loader('Response', 'classes'); |
|
| 392 | + $response = & class_loader('Response', 'classes'); |
|
| 393 | 393 | $response->send404(); |
| 394 | 394 | } |
| 395 | 395 | } |
@@ -402,15 +402,15 @@ discard block |
||
| 402 | 402 | * @param boolean $useConfigFile whether to use route configuration file |
| 403 | 403 | * @return object |
| 404 | 404 | */ |
| 405 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 405 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
|
| 406 | 406 | $route = array(); |
| 407 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 407 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')) { |
|
| 408 | 408 | require_once CONFIG_PATH . 'routes.php'; |
| 409 | 409 | } |
| 410 | 410 | $route = array_merge($route, $overwriteConfig); |
| 411 | 411 | $this->routes = $route; |
| 412 | 412 | //if route is empty remove all configuration |
| 413 | - if(empty($route)){ |
|
| 413 | + if (empty($route)) { |
|
| 414 | 414 | $this->removeAllRoute(); |
| 415 | 415 | } |
| 416 | 416 | return $this; |
@@ -420,7 +420,7 @@ discard block |
||
| 420 | 420 | * Get the route configuration |
| 421 | 421 | * @return array |
| 422 | 422 | */ |
| 423 | - public function getRouteConfiguration(){ |
|
| 423 | + public function getRouteConfiguration() { |
|
| 424 | 424 | return $this->routes; |
| 425 | 425 | } |
| 426 | 426 | |
@@ -432,19 +432,19 @@ discard block |
||
| 432 | 432 | * |
| 433 | 433 | * @return object the current instance |
| 434 | 434 | */ |
| 435 | - public function setControllerFilePath($path = null){ |
|
| 436 | - if($path !== null){ |
|
| 435 | + public function setControllerFilePath($path = null) { |
|
| 436 | + if ($path !== null) { |
|
| 437 | 437 | $this->controllerPath = $path; |
| 438 | 438 | return $this; |
| 439 | 439 | } |
| 440 | 440 | //did we set the controller, so set the controller path |
| 441 | - if($this->controller && ! $this->controllerPath){ |
|
| 441 | + if ($this->controller && !$this->controllerPath) { |
|
| 442 | 442 | $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
| 443 | 443 | $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
| 444 | 444 | //if the controller is in module |
| 445 | - if($this->module){ |
|
| 445 | + if ($this->module) { |
|
| 446 | 446 | $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
| 447 | - if($path !== false){ |
|
| 447 | + if ($path !== false) { |
|
| 448 | 448 | $controllerPath = $path; |
| 449 | 449 | } |
| 450 | 450 | } |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | * Determine the route parameters from route configuration |
| 458 | 458 | * @return void |
| 459 | 459 | */ |
| 460 | - protected function determineRouteParamsFromConfig(){ |
|
| 460 | + protected function determineRouteParamsFromConfig() { |
|
| 461 | 461 | $uri = implode('/', $this->segments); |
| 462 | 462 | /* |
| 463 | 463 | * Generics routes patterns |
@@ -482,20 +482,20 @@ discard block |
||
| 482 | 482 | array_shift($args); |
| 483 | 483 | //check if this contains an module |
| 484 | 484 | $moduleControllerMethod = explode('#', $this->callback[$index]); |
| 485 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 485 | + if (is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2) { |
|
| 486 | 486 | $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
| 487 | 487 | $this->module = $moduleControllerMethod[0]; |
| 488 | 488 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
| 489 | 489 | } |
| 490 | - else{ |
|
| 490 | + else { |
|
| 491 | 491 | $this->logger->info('The current request does not use the module'); |
| 492 | 492 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
| 493 | 493 | } |
| 494 | - if(is_array($moduleControllerMethod)){ |
|
| 495 | - if(isset($moduleControllerMethod[0])){ |
|
| 494 | + if (is_array($moduleControllerMethod)) { |
|
| 495 | + if (isset($moduleControllerMethod[0])) { |
|
| 496 | 496 | $this->controller = $moduleControllerMethod[0]; |
| 497 | 497 | } |
| 498 | - if(isset($moduleControllerMethod[1])){ |
|
| 498 | + if (isset($moduleControllerMethod[1])) { |
|
| 499 | 499 | $this->method = $moduleControllerMethod[1]; |
| 500 | 500 | } |
| 501 | 501 | $this->args = $args; |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | //first if the controller is not set and the module is set use the module name as the controller |
| 509 | - if(! $this->controller && $this->module){ |
|
| 509 | + if (!$this->controller && $this->module) { |
|
| 510 | 510 | $this->logger->info( |
| 511 | 511 | 'After loop in predefined routes configuration, |
| 512 | 512 | the module name is set but the controller is not set, |
@@ -520,67 +520,67 @@ discard block |
||
| 520 | 520 | * Determine the route parameters using the server variable "REQUEST_URI" |
| 521 | 521 | * @return void |
| 522 | 522 | */ |
| 523 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 523 | + protected function determineRouteParamsFromRequestUri() { |
|
| 524 | 524 | $segment = $this->segments; |
| 525 | 525 | $nbSegment = count($segment); |
| 526 | 526 | //if segment is null so means no need to perform |
| 527 | - if($nbSegment > 0){ |
|
| 527 | + if ($nbSegment > 0) { |
|
| 528 | 528 | //get the module list |
| 529 | 529 | $modules = Module::getModuleList(); |
| 530 | 530 | //first check if no module |
| 531 | - if(empty($modules)){ |
|
| 531 | + if (empty($modules)) { |
|
| 532 | 532 | $this->logger->info('No module was loaded will skip the module checking'); |
| 533 | 533 | //the application don't use module |
| 534 | 534 | //controller |
| 535 | - if(isset($segment[0])){ |
|
| 535 | + if (isset($segment[0])) { |
|
| 536 | 536 | $this->controller = $segment[0]; |
| 537 | 537 | array_shift($segment); |
| 538 | 538 | } |
| 539 | 539 | //method |
| 540 | - if(isset($segment[0])){ |
|
| 540 | + if (isset($segment[0])) { |
|
| 541 | 541 | $this->method = $segment[0]; |
| 542 | 542 | array_shift($segment); |
| 543 | 543 | } |
| 544 | 544 | //args |
| 545 | 545 | $this->args = $segment; |
| 546 | 546 | } |
| 547 | - else{ |
|
| 547 | + else { |
|
| 548 | 548 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
| 549 | - if(in_array($segment[0], $modules)){ |
|
| 549 | + if (in_array($segment[0], $modules)) { |
|
| 550 | 550 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
| 551 | 551 | $this->module = $segment[0]; |
| 552 | 552 | array_shift($segment); |
| 553 | 553 | //check if the second arg is the controller from module |
| 554 | - if(isset($segment[0])){ |
|
| 554 | + if (isset($segment[0])) { |
|
| 555 | 555 | $this->controller = $segment[0]; |
| 556 | 556 | //check if the request use the same module name and controller |
| 557 | 557 | $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
| 558 | - if(! $path){ |
|
| 558 | + if (!$path) { |
|
| 559 | 559 | $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
| 560 | 560 | $this->controller = $this->module; |
| 561 | 561 | } |
| 562 | - else{ |
|
| 562 | + else { |
|
| 563 | 563 | $this->controllerPath = $path; |
| 564 | 564 | array_shift($segment); |
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | 567 | //check for method |
| 568 | - if(isset($segment[0])){ |
|
| 568 | + if (isset($segment[0])) { |
|
| 569 | 569 | $this->method = $segment[0]; |
| 570 | 570 | array_shift($segment); |
| 571 | 571 | } |
| 572 | 572 | //the remaining is for args |
| 573 | 573 | $this->args = $segment; |
| 574 | 574 | } |
| 575 | - else{ |
|
| 575 | + else { |
|
| 576 | 576 | $this->logger->info('The current request information is not found in the module list'); |
| 577 | 577 | //controller |
| 578 | - if(isset($segment[0])){ |
|
| 578 | + if (isset($segment[0])) { |
|
| 579 | 579 | $this->controller = $segment[0]; |
| 580 | 580 | array_shift($segment); |
| 581 | 581 | } |
| 582 | 582 | //method |
| 583 | - if(isset($segment[0])){ |
|
| 583 | + if (isset($segment[0])) { |
|
| 584 | 584 | $this->method = $segment[0]; |
| 585 | 585 | array_shift($segment); |
| 586 | 586 | } |
@@ -588,7 +588,7 @@ discard block |
||
| 588 | 588 | $this->args = $segment; |
| 589 | 589 | } |
| 590 | 590 | } |
| 591 | - if(! $this->controller && $this->module){ |
|
| 591 | + if (!$this->controller && $this->module) { |
|
| 592 | 592 | $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
| 593 | 593 | $this->controller = $this->module; |
| 594 | 594 | } |
@@ -600,9 +600,9 @@ discard block |
||
| 600 | 600 | * |
| 601 | 601 | * @return object the current instance |
| 602 | 602 | */ |
| 603 | - protected function setRouteConfigurationInfos(){ |
|
| 603 | + protected function setRouteConfigurationInfos() { |
|
| 604 | 604 | //adding route |
| 605 | - foreach($this->routes as $pattern => $callback){ |
|
| 605 | + foreach ($this->routes as $pattern => $callback) { |
|
| 606 | 606 | $this->add($pattern, $callback); |
| 607 | 607 | } |
| 608 | 608 | return $this; |
@@ -612,12 +612,12 @@ discard block |
||
| 612 | 612 | * Set the Log instance using argument or create new instance |
| 613 | 613 | * @param object $logger the Log instance if not null |
| 614 | 614 | */ |
| 615 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 616 | - if ($logger !== null){ |
|
| 615 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
| 616 | + if ($logger !== null) { |
|
| 617 | 617 | $this->logger = $logger; |
| 618 | 618 | } |
| 619 | - else{ |
|
| 620 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 619 | + else { |
|
| 620 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 621 | 621 | $this->logger->setLogger('Library::Router'); |
| 622 | 622 | } |
| 623 | 623 | } |
@@ -266,8 +266,7 @@ discard block |
||
| 266 | 266 | //if the application is running in CLI mode use the first argument |
| 267 | 267 | else if(IS_CLI && isset($_SERVER['argv'][1])){ |
| 268 | 268 | $routeUri = $_SERVER['argv'][1]; |
| 269 | - } |
|
| 270 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 269 | + } else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 271 | 270 | $routeUri = $_SERVER['REQUEST_URI']; |
| 272 | 271 | } |
| 273 | 272 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
@@ -363,15 +362,13 @@ discard block |
||
| 363 | 362 | if(! class_exists($controller, false)){ |
| 364 | 363 | $e404 = true; |
| 365 | 364 | $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
| 366 | - } |
|
| 367 | - else{ |
|
| 365 | + } else{ |
|
| 368 | 366 | $controllerInstance = new $controller(); |
| 369 | 367 | $controllerMethod = $this->getMethod(); |
| 370 | 368 | if(! method_exists($controllerInstance, $controllerMethod)){ |
| 371 | 369 | $e404 = true; |
| 372 | 370 | $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
| 373 | - } |
|
| 374 | - else{ |
|
| 371 | + } else{ |
|
| 375 | 372 | $this->logger->info('Routing data is set correctly now GO!'); |
| 376 | 373 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
| 377 | 374 | //render the final page to user |
@@ -379,8 +376,7 @@ discard block |
||
| 379 | 376 | get_instance()->response->renderFinalPage(); |
| 380 | 377 | } |
| 381 | 378 | } |
| 382 | - } |
|
| 383 | - else{ |
|
| 379 | + } else{ |
|
| 384 | 380 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
| 385 | 381 | $e404 = true; |
| 386 | 382 | } |
@@ -486,8 +482,7 @@ discard block |
||
| 486 | 482 | $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
| 487 | 483 | $this->module = $moduleControllerMethod[0]; |
| 488 | 484 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
| 489 | - } |
|
| 490 | - else{ |
|
| 485 | + } else{ |
|
| 491 | 486 | $this->logger->info('The current request does not use the module'); |
| 492 | 487 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
| 493 | 488 | } |
@@ -543,8 +538,7 @@ discard block |
||
| 543 | 538 | } |
| 544 | 539 | //args |
| 545 | 540 | $this->args = $segment; |
| 546 | - } |
|
| 547 | - else{ |
|
| 541 | + } else{ |
|
| 548 | 542 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
| 549 | 543 | if(in_array($segment[0], $modules)){ |
| 550 | 544 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
@@ -558,8 +552,7 @@ discard block |
||
| 558 | 552 | if(! $path){ |
| 559 | 553 | $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
| 560 | 554 | $this->controller = $this->module; |
| 561 | - } |
|
| 562 | - else{ |
|
| 555 | + } else{ |
|
| 563 | 556 | $this->controllerPath = $path; |
| 564 | 557 | array_shift($segment); |
| 565 | 558 | } |
@@ -571,8 +564,7 @@ discard block |
||
| 571 | 564 | } |
| 572 | 565 | //the remaining is for args |
| 573 | 566 | $this->args = $segment; |
| 574 | - } |
|
| 575 | - else{ |
|
| 567 | + } else{ |
|
| 576 | 568 | $this->logger->info('The current request information is not found in the module list'); |
| 577 | 569 | //controller |
| 578 | 570 | if(isset($segment[0])){ |
@@ -615,8 +607,7 @@ discard block |
||
| 615 | 607 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 616 | 608 | if ($logger !== null){ |
| 617 | 609 | $this->logger = $logger; |
| 618 | - } |
|
| 619 | - else{ |
|
| 610 | + } else{ |
|
| 620 | 611 | $this->logger =& class_loader('Log', 'classes'); |
| 621 | 612 | $this->logger->setLogger('Library::Router'); |
| 622 | 613 | } |
@@ -1,157 +1,157 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Security{ |
|
| 27 | + class Security{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private static $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private static $logger; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the logger singleton instance |
|
| 37 | - * @return Log the logger instance |
|
| 38 | - */ |
|
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - self::$logger[0]->setLogger('Library::Security'); |
|
| 43 | - } |
|
| 44 | - return self::$logger[0]; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the logger singleton instance |
|
| 37 | + * @return Log the logger instance |
|
| 38 | + */ |
|
| 39 | + private static function getLogger(){ |
|
| 40 | + if(self::$logger == null){ |
|
| 41 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + self::$logger[0]->setLogger('Library::Security'); |
|
| 43 | + } |
|
| 44 | + return self::$logger[0]; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * This method is used to generate the CSRF token |
|
| 50 | - * @return string the generated CSRF token |
|
| 51 | - */ |
|
| 52 | - public static function generateCSRF(){ |
|
| 53 | - $logger = self::getLogger(); |
|
| 54 | - $logger->debug('Generation of CSRF ...'); |
|
| 48 | + /** |
|
| 49 | + * This method is used to generate the CSRF token |
|
| 50 | + * @return string the generated CSRF token |
|
| 51 | + */ |
|
| 52 | + public static function generateCSRF(){ |
|
| 53 | + $logger = self::getLogger(); |
|
| 54 | + $logger->debug('Generation of CSRF ...'); |
|
| 55 | 55 | |
| 56 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | - $expire = get_config('csrf_expire', 60); |
|
| 58 | - $keyExpire = 'csrf_expire'; |
|
| 59 | - $currentTime = time(); |
|
| 60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | - $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | - return Session::get($key); |
|
| 63 | - } |
|
| 64 | - else{ |
|
| 65 | - $newTime = $currentTime + $expire; |
|
| 66 | - $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | - Session::set($keyExpire, $newTime); |
|
| 69 | - Session::set($key, $token); |
|
| 70 | - return Session::get($key); |
|
| 71 | - } |
|
| 72 | - } |
|
| 56 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | + $expire = get_config('csrf_expire', 60); |
|
| 58 | + $keyExpire = 'csrf_expire'; |
|
| 59 | + $currentTime = time(); |
|
| 60 | + if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | + $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | + return Session::get($key); |
|
| 63 | + } |
|
| 64 | + else{ |
|
| 65 | + $newTime = $currentTime + $expire; |
|
| 66 | + $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | + Session::set($keyExpire, $newTime); |
|
| 69 | + Session::set($key, $token); |
|
| 70 | + return Session::get($key); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | - * @return boolean true if valid, false if not valid |
|
| 77 | - */ |
|
| 78 | - public static function validateCSRF(){ |
|
| 79 | - $logger = self::getLogger(); |
|
| 80 | - $logger->debug('Validation of CSRF ...'); |
|
| 74 | + /** |
|
| 75 | + * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | + * @return boolean true if valid, false if not valid |
|
| 77 | + */ |
|
| 78 | + public static function validateCSRF(){ |
|
| 79 | + $logger = self::getLogger(); |
|
| 80 | + $logger->debug('Validation of CSRF ...'); |
|
| 81 | 81 | |
| 82 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | - $expire = get_config('csrf_expire', 60); |
|
| 84 | - $keyExpire = 'csrf_expire'; |
|
| 85 | - $currentTime = time(); |
|
| 86 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | - $logger->warning('The CSRF session data is not valide'); |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - //perform form data |
|
| 92 | - //need use request->query() for best retrieve |
|
| 93 | - //super instance |
|
| 94 | - $obj = & get_instance(); |
|
| 95 | - $token = $obj->request->query($key); |
|
| 96 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 97 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 101 | - //remove the token from session |
|
| 102 | - Session::clear($key); |
|
| 103 | - Session::clear($keyExpire); |
|
| 104 | - return true; |
|
| 105 | - } |
|
| 82 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | + $expire = get_config('csrf_expire', 60); |
|
| 84 | + $keyExpire = 'csrf_expire'; |
|
| 85 | + $currentTime = time(); |
|
| 86 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | + if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | + $logger->warning('The CSRF session data is not valide'); |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + //perform form data |
|
| 92 | + //need use request->query() for best retrieve |
|
| 93 | + //super instance |
|
| 94 | + $obj = & get_instance(); |
|
| 95 | + $token = $obj->request->query($key); |
|
| 96 | + if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 97 | + $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 101 | + //remove the token from session |
|
| 102 | + Session::clear($key); |
|
| 103 | + Session::clear($keyExpire); |
|
| 104 | + return true; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * This method is used to check the whitelist IP address access |
|
| 109 | - */ |
|
| 110 | - public static function checkWhiteListIpAccess(){ |
|
| 111 | - $logger = self::getLogger(); |
|
| 112 | - $logger->debug('Validation of the IP address access ...'); |
|
| 113 | - $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 114 | - $isEnable = get_config('white_list_ip_enable', false); |
|
| 115 | - if($isEnable){ |
|
| 116 | - $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 117 | - $list = get_config('white_list_ip_addresses', array()); |
|
| 118 | - if(! empty($list)){ |
|
| 119 | - //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 120 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 121 | - $ip = get_ip(); |
|
| 122 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 123 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 124 | - //wildcard to access all ip address |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - else{ |
|
| 128 | - // go through all whitelisted ips |
|
| 129 | - foreach ($list as $ipaddr) { |
|
| 130 | - // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 131 | - $wildcardPosition = strpos($ipaddr, '*'); |
|
| 132 | - if ($wildcardPosition === false) { |
|
| 133 | - // no wild card in whitelisted ip --continue searching |
|
| 134 | - continue; |
|
| 135 | - } |
|
| 136 | - // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 137 | - // and add the wold card to get the same pattern |
|
| 138 | - if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 139 | - // f.e. we got |
|
| 140 | - // ip "127.0.0.1" |
|
| 141 | - // whitelisted ip "127.0.*" |
|
| 142 | - // then we compared "127.0.*" with "127.0.*" |
|
| 143 | - // return success |
|
| 144 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 149 | - show_error('Access to this application is not allowed'); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - else{ |
|
| 154 | - $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 107 | + /** |
|
| 108 | + * This method is used to check the whitelist IP address access |
|
| 109 | + */ |
|
| 110 | + public static function checkWhiteListIpAccess(){ |
|
| 111 | + $logger = self::getLogger(); |
|
| 112 | + $logger->debug('Validation of the IP address access ...'); |
|
| 113 | + $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 114 | + $isEnable = get_config('white_list_ip_enable', false); |
|
| 115 | + if($isEnable){ |
|
| 116 | + $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 117 | + $list = get_config('white_list_ip_addresses', array()); |
|
| 118 | + if(! empty($list)){ |
|
| 119 | + //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 120 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 121 | + $ip = get_ip(); |
|
| 122 | + if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 123 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 124 | + //wildcard to access all ip address |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + else{ |
|
| 128 | + // go through all whitelisted ips |
|
| 129 | + foreach ($list as $ipaddr) { |
|
| 130 | + // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 131 | + $wildcardPosition = strpos($ipaddr, '*'); |
|
| 132 | + if ($wildcardPosition === false) { |
|
| 133 | + // no wild card in whitelisted ip --continue searching |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 136 | + // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 137 | + // and add the wold card to get the same pattern |
|
| 138 | + if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 139 | + // f.e. we got |
|
| 140 | + // ip "127.0.0.1" |
|
| 141 | + // whitelisted ip "127.0.*" |
|
| 142 | + // then we compared "127.0.*" with "127.0.*" |
|
| 143 | + // return success |
|
| 144 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 149 | + show_error('Access to this application is not allowed'); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + else{ |
|
| 154 | + $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Security{ |
|
| 27 | + class Security { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The logger instance |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | * Get the logger singleton instance |
| 37 | 37 | * @return Log the logger instance |
| 38 | 38 | */ |
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 39 | + private static function getLogger() { |
|
| 40 | + if (self::$logger == null) { |
|
| 41 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 42 | 42 | self::$logger[0]->setLogger('Library::Security'); |
| 43 | 43 | } |
| 44 | 44 | return self::$logger[0]; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * This method is used to generate the CSRF token |
| 50 | 50 | * @return string the generated CSRF token |
| 51 | 51 | */ |
| 52 | - public static function generateCSRF(){ |
|
| 52 | + public static function generateCSRF() { |
|
| 53 | 53 | $logger = self::getLogger(); |
| 54 | 54 | $logger->debug('Generation of CSRF ...'); |
| 55 | 55 | |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | $expire = get_config('csrf_expire', 60); |
| 58 | 58 | $keyExpire = 'csrf_expire'; |
| 59 | 59 | $currentTime = time(); |
| 60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 60 | + if (Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime) { |
|
| 61 | 61 | $logger->info('The CSRF token not yet expire just return it'); |
| 62 | 62 | return Session::get($key); |
| 63 | 63 | } |
| 64 | - else{ |
|
| 64 | + else { |
|
| 65 | 65 | $newTime = $currentTime + $expire; |
| 66 | 66 | $token = sha1(uniqid()) . sha1(uniqid()); |
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . '], token [' . $token . ']'); |
|
| 68 | 68 | Session::set($keyExpire, $newTime); |
| 69 | 69 | Session::set($key, $token); |
| 70 | 70 | return Session::get($key); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | * This method is used to check the CSRF if is valid, not yet expire, etc. |
| 76 | 76 | * @return boolean true if valid, false if not valid |
| 77 | 77 | */ |
| 78 | - public static function validateCSRF(){ |
|
| 78 | + public static function validateCSRF() { |
|
| 79 | 79 | $logger = self::getLogger(); |
| 80 | 80 | $logger->debug('Validation of CSRF ...'); |
| 81 | 81 | |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | $expire = get_config('csrf_expire', 60); |
| 84 | 84 | $keyExpire = 'csrf_expire'; |
| 85 | 85 | $currentTime = time(); |
| 86 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 86 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . ']'); |
|
| 87 | + if (!Session::exists($key) || Session::get($keyExpire) <= $currentTime) { |
|
| 88 | 88 | $logger->warning('The CSRF session data is not valide'); |
| 89 | 89 | return false; |
| 90 | 90 | } |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | //super instance |
| 94 | 94 | $obj = & get_instance(); |
| 95 | 95 | $token = $obj->request->query($key); |
| 96 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 97 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 96 | + if (!$token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime) { |
|
| 97 | + $logger->warning('The CSRF data [' . $token . '] is not valide may be attacker do his job'); |
|
| 98 | 98 | return false; |
| 99 | 99 | } |
| 100 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 100 | + $logger->info('The CSRF data [' . $token . '] is valide the form data is safe continue'); |
|
| 101 | 101 | //remove the token from session |
| 102 | 102 | Session::clear($key); |
| 103 | 103 | Session::clear($keyExpire); |
@@ -107,24 +107,24 @@ discard block |
||
| 107 | 107 | /** |
| 108 | 108 | * This method is used to check the whitelist IP address access |
| 109 | 109 | */ |
| 110 | - public static function checkWhiteListIpAccess(){ |
|
| 110 | + public static function checkWhiteListIpAccess() { |
|
| 111 | 111 | $logger = self::getLogger(); |
| 112 | 112 | $logger->debug('Validation of the IP address access ...'); |
| 113 | 113 | $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
| 114 | 114 | $isEnable = get_config('white_list_ip_enable', false); |
| 115 | - if($isEnable){ |
|
| 115 | + if ($isEnable) { |
|
| 116 | 116 | $logger->info('Whitelist IP access is enabled in the configuration'); |
| 117 | 117 | $list = get_config('white_list_ip_addresses', array()); |
| 118 | - if(! empty($list)){ |
|
| 118 | + if (!empty($list)) { |
|
| 119 | 119 | //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
| 120 | 120 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
| 121 | 121 | $ip = get_ip(); |
| 122 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 122 | + if ((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)) { |
|
| 123 | 123 | $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
| 124 | 124 | //wildcard to access all ip address |
| 125 | 125 | return; |
| 126 | 126 | } |
| 127 | - else{ |
|
| 127 | + else { |
|
| 128 | 128 | // go through all whitelisted ips |
| 129 | 129 | foreach ($list as $ipaddr) { |
| 130 | 130 | // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | - else{ |
|
| 153 | + else { |
|
| 154 | 154 | $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
| 155 | 155 | } |
| 156 | 156 | } |
@@ -60,8 +60,7 @@ discard block |
||
| 60 | 60 | if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
| 61 | 61 | $logger->info('The CSRF token not yet expire just return it'); |
| 62 | 62 | return Session::get($key); |
| 63 | - } |
|
| 64 | - else{ |
|
| 63 | + } else{ |
|
| 65 | 64 | $newTime = $currentTime + $expire; |
| 66 | 65 | $token = sha1(uniqid()) . sha1(uniqid()); |
| 67 | 66 | $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
@@ -123,8 +122,7 @@ discard block |
||
| 123 | 122 | $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
| 124 | 123 | //wildcard to access all ip address |
| 125 | 124 | return; |
| 126 | - } |
|
| 127 | - else{ |
|
| 125 | + } else{ |
|
| 128 | 126 | // go through all whitelisted ips |
| 129 | 127 | foreach ($list as $ipaddr) { |
| 130 | 128 | // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
@@ -149,8 +147,7 @@ discard block |
||
| 149 | 147 | show_error('Access to this application is not allowed'); |
| 150 | 148 | } |
| 151 | 149 | } |
| 152 | - } |
|
| 153 | - else{ |
|
| 150 | + } else{ |
|
| 154 | 151 | $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
| 155 | 152 | } |
| 156 | 153 | } |
@@ -1,441 +1,441 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of request header to send with response |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $headers = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of request header to send with response |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $headers = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The final page content to display to user |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $_pageRender = null; |
|
| 41 | + /** |
|
| 42 | + * The final page content to display to user |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $_pageRender = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The current request URL |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $_currentUrl = null; |
|
| 47 | + /** |
|
| 48 | + * The current request URL |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $_currentUrl = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The current request URL cache key |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_currentUrlCacheKey = null; |
|
| 53 | + /** |
|
| 54 | + * The current request URL cache key |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_currentUrlCacheKey = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Whether we can compress the output using Gzip |
|
| 61 | - * @var boolean |
|
| 62 | - */ |
|
| 63 | - private static $_canCompressOutput = false; |
|
| 59 | + /** |
|
| 60 | + * Whether we can compress the output using Gzip |
|
| 61 | + * @var boolean |
|
| 62 | + */ |
|
| 63 | + private static $_canCompressOutput = false; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Construct new response instance |
|
| 67 | - */ |
|
| 68 | - public function __construct(){ |
|
| 69 | - $currentUrl = ''; |
|
| 70 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 71 | - $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 72 | - } |
|
| 73 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 74 | - $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 75 | - } |
|
| 76 | - $this->_currentUrl = $currentUrl; |
|
| 65 | + /** |
|
| 66 | + * Construct new response instance |
|
| 67 | + */ |
|
| 68 | + public function __construct(){ |
|
| 69 | + $currentUrl = ''; |
|
| 70 | + if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 71 | + $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 72 | + } |
|
| 73 | + if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 74 | + $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 75 | + } |
|
| 76 | + $this->_currentUrl = $currentUrl; |
|
| 77 | 77 | |
| 78 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 78 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 79 | 79 | |
| 80 | - self::$_canCompressOutput = get_config('compress_output') |
|
| 81 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 82 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 83 | - && extension_loaded('zlib') |
|
| 84 | - && (bool) ini_get('zlib.output_compression') === false; |
|
| 85 | - } |
|
| 80 | + self::$_canCompressOutput = get_config('compress_output') |
|
| 81 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 82 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 83 | + && extension_loaded('zlib') |
|
| 84 | + && (bool) ini_get('zlib.output_compression') === false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get the logger singleton instance |
|
| 89 | - * @return Log the logger instance |
|
| 90 | - */ |
|
| 91 | - private static function getLogger(){ |
|
| 92 | - if(self::$logger == null){ |
|
| 93 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 94 | - self::$logger[0]->setLogger('Library::Response'); |
|
| 95 | - } |
|
| 96 | - return self::$logger[0]; |
|
| 97 | - } |
|
| 87 | + /** |
|
| 88 | + * Get the logger singleton instance |
|
| 89 | + * @return Log the logger instance |
|
| 90 | + */ |
|
| 91 | + private static function getLogger(){ |
|
| 92 | + if(self::$logger == null){ |
|
| 93 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 94 | + self::$logger[0]->setLogger('Library::Response'); |
|
| 95 | + } |
|
| 96 | + return self::$logger[0]; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Send the HTTP Response headers |
|
| 101 | - * @param integer $httpCode the HTTP status code |
|
| 102 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 103 | - */ |
|
| 104 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 105 | - set_http_status_header($httpCode); |
|
| 106 | - self::setHeaders($headers); |
|
| 107 | - if(! headers_sent()){ |
|
| 108 | - foreach(self::getHeaders() as $key => $value){ |
|
| 109 | - header($key .': '.$value); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 99 | + /** |
|
| 100 | + * Send the HTTP Response headers |
|
| 101 | + * @param integer $httpCode the HTTP status code |
|
| 102 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 103 | + */ |
|
| 104 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 105 | + set_http_status_header($httpCode); |
|
| 106 | + self::setHeaders($headers); |
|
| 107 | + if(! headers_sent()){ |
|
| 108 | + foreach(self::getHeaders() as $key => $value){ |
|
| 109 | + header($key .': '.$value); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Get the list of the headers |
|
| 116 | - * @return array the headers list |
|
| 117 | - */ |
|
| 118 | - public static function getHeaders(){ |
|
| 119 | - return self::$headers; |
|
| 120 | - } |
|
| 114 | + /** |
|
| 115 | + * Get the list of the headers |
|
| 116 | + * @return array the headers list |
|
| 117 | + */ |
|
| 118 | + public static function getHeaders(){ |
|
| 119 | + return self::$headers; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Get the header value for the given name |
|
| 124 | - * @param string $name the header name |
|
| 125 | - * @return string the header value |
|
| 126 | - */ |
|
| 127 | - public static function getHeader($name){ |
|
| 128 | - return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 129 | - } |
|
| 122 | + /** |
|
| 123 | + * Get the header value for the given name |
|
| 124 | + * @param string $name the header name |
|
| 125 | + * @return string the header value |
|
| 126 | + */ |
|
| 127 | + public static function getHeader($name){ |
|
| 128 | + return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Set the header value for the specified name |
|
| 134 | - * @param string $name the header name |
|
| 135 | - * @param string $value the header value to be set |
|
| 136 | - */ |
|
| 137 | - public static function setHeader($name, $value){ |
|
| 138 | - self::$headers[$name] = $value; |
|
| 139 | - } |
|
| 132 | + /** |
|
| 133 | + * Set the header value for the specified name |
|
| 134 | + * @param string $name the header name |
|
| 135 | + * @param string $value the header value to be set |
|
| 136 | + */ |
|
| 137 | + public static function setHeader($name, $value){ |
|
| 138 | + self::$headers[$name] = $value; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Set the headers using array |
|
| 143 | - * @param array $headers the list of the headers to set. |
|
| 144 | - * Note: this will merge with the existing headers |
|
| 145 | - */ |
|
| 146 | - public static function setHeaders(array $headers){ |
|
| 147 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 148 | - } |
|
| 141 | + /** |
|
| 142 | + * Set the headers using array |
|
| 143 | + * @param array $headers the list of the headers to set. |
|
| 144 | + * Note: this will merge with the existing headers |
|
| 145 | + */ |
|
| 146 | + public static function setHeaders(array $headers){ |
|
| 147 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Redirect user in the specified page |
|
| 152 | - * @param string $path the URL or URI to be redirect to |
|
| 153 | - */ |
|
| 154 | - public static function redirect($path = ''){ |
|
| 155 | - $logger = self::getLogger(); |
|
| 156 | - $url = Url::site_url($path); |
|
| 157 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 158 | - if(! headers_sent()){ |
|
| 159 | - header('Location: '.$url); |
|
| 160 | - exit; |
|
| 161 | - } |
|
| 162 | - else{ |
|
| 163 | - echo '<script> |
|
| 150 | + /** |
|
| 151 | + * Redirect user in the specified page |
|
| 152 | + * @param string $path the URL or URI to be redirect to |
|
| 153 | + */ |
|
| 154 | + public static function redirect($path = ''){ |
|
| 155 | + $logger = self::getLogger(); |
|
| 156 | + $url = Url::site_url($path); |
|
| 157 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 158 | + if(! headers_sent()){ |
|
| 159 | + header('Location: '.$url); |
|
| 160 | + exit; |
|
| 161 | + } |
|
| 162 | + else{ |
|
| 163 | + echo '<script> |
|
| 164 | 164 | location.href = "'.$url.'"; |
| 165 | 165 | </script>'; |
| 166 | - } |
|
| 167 | - } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Render the view to display later or return the content |
|
| 171 | - * @param string $view the view name or path |
|
| 172 | - * @param array|object $data the variable data to use in the view |
|
| 173 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 174 | - * @return void|string if $return is true will return the view content otherwise |
|
| 175 | - * will display the view content. |
|
| 176 | - */ |
|
| 177 | - public function render($view, $data = null, $return = false){ |
|
| 178 | - $logger = self::getLogger(); |
|
| 179 | - //convert data to an array |
|
| 180 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 181 | - $view = str_ireplace('.php', '', $view); |
|
| 182 | - $view = trim($view, '/\\'); |
|
| 183 | - $viewFile = $view . '.php'; |
|
| 184 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 169 | + /** |
|
| 170 | + * Render the view to display later or return the content |
|
| 171 | + * @param string $view the view name or path |
|
| 172 | + * @param array|object $data the variable data to use in the view |
|
| 173 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 174 | + * @return void|string if $return is true will return the view content otherwise |
|
| 175 | + * will display the view content. |
|
| 176 | + */ |
|
| 177 | + public function render($view, $data = null, $return = false){ |
|
| 178 | + $logger = self::getLogger(); |
|
| 179 | + //convert data to an array |
|
| 180 | + $data = ! is_array($data) ? (array) $data : $data; |
|
| 181 | + $view = str_ireplace('.php', '', $view); |
|
| 182 | + $view = trim($view, '/\\'); |
|
| 183 | + $viewFile = $view . '.php'; |
|
| 184 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 185 | 185 | |
| 186 | - //super instance |
|
| 187 | - $obj = & get_instance(); |
|
| 186 | + //super instance |
|
| 187 | + $obj = & get_instance(); |
|
| 188 | 188 | |
| 189 | - if(Module::hasModule()){ |
|
| 190 | - //check in module first |
|
| 191 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 192 | - $mod = null; |
|
| 193 | - //check if the request class contains module name |
|
| 194 | - if(strpos($view, '/') !== false){ |
|
| 195 | - $viewPath = explode('/', $view); |
|
| 196 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 197 | - $mod = $viewPath[0]; |
|
| 198 | - array_shift($viewPath); |
|
| 199 | - $view = implode('/', $viewPath); |
|
| 200 | - $viewFile = $view . '.php'; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 204 | - $mod = $obj->moduleName; |
|
| 205 | - } |
|
| 206 | - if($mod){ |
|
| 207 | - $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 208 | - if($moduleViewPath){ |
|
| 209 | - $path = $moduleViewPath; |
|
| 210 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | - } |
|
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - else{ |
|
| 217 | - $logger->info('The current request does not use module using the default location.'); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 221 | - $found = false; |
|
| 222 | - if(file_exists($path)){ |
|
| 223 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 224 | - if(! isset($this->{$key})){ |
|
| 225 | - $this->{$key} = & $obj->{$key}; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - ob_start(); |
|
| 229 | - extract($data); |
|
| 230 | - //need use require() instead of require_once because can load this view many time |
|
| 231 | - require $path; |
|
| 232 | - $content = ob_get_clean(); |
|
| 233 | - if($return){ |
|
| 234 | - return $content; |
|
| 235 | - } |
|
| 236 | - $this->_pageRender .= $content; |
|
| 237 | - $found = true; |
|
| 238 | - } |
|
| 239 | - if(! $found){ |
|
| 240 | - show_error('Unable to find view [' .$view . ']'); |
|
| 241 | - } |
|
| 242 | - } |
|
| 189 | + if(Module::hasModule()){ |
|
| 190 | + //check in module first |
|
| 191 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 192 | + $mod = null; |
|
| 193 | + //check if the request class contains module name |
|
| 194 | + if(strpos($view, '/') !== false){ |
|
| 195 | + $viewPath = explode('/', $view); |
|
| 196 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 197 | + $mod = $viewPath[0]; |
|
| 198 | + array_shift($viewPath); |
|
| 199 | + $view = implode('/', $viewPath); |
|
| 200 | + $viewFile = $view . '.php'; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + if(! $mod && !empty($obj->moduleName)){ |
|
| 204 | + $mod = $obj->moduleName; |
|
| 205 | + } |
|
| 206 | + if($mod){ |
|
| 207 | + $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 208 | + if($moduleViewPath){ |
|
| 209 | + $path = $moduleViewPath; |
|
| 210 | + $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | + } |
|
| 212 | + else{ |
|
| 213 | + $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + else{ |
|
| 217 | + $logger->info('The current request does not use module using the default location.'); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 221 | + $found = false; |
|
| 222 | + if(file_exists($path)){ |
|
| 223 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 224 | + if(! isset($this->{$key})){ |
|
| 225 | + $this->{$key} = & $obj->{$key}; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + ob_start(); |
|
| 229 | + extract($data); |
|
| 230 | + //need use require() instead of require_once because can load this view many time |
|
| 231 | + require $path; |
|
| 232 | + $content = ob_get_clean(); |
|
| 233 | + if($return){ |
|
| 234 | + return $content; |
|
| 235 | + } |
|
| 236 | + $this->_pageRender .= $content; |
|
| 237 | + $found = true; |
|
| 238 | + } |
|
| 239 | + if(! $found){ |
|
| 240 | + show_error('Unable to find view [' .$view . ']'); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Send the final page output to user |
|
| 246 | - */ |
|
| 247 | - public function renderFinalPage(){ |
|
| 248 | - $logger = self::getLogger(); |
|
| 249 | - $obj = & get_instance(); |
|
| 250 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 251 | - $dispatcher = $obj->eventdispatcher; |
|
| 252 | - $content = $this->_pageRender; |
|
| 253 | - if(! $content){ |
|
| 254 | - $logger->warning('The final view content is empty.'); |
|
| 255 | - return; |
|
| 256 | - } |
|
| 257 | - //dispatch |
|
| 258 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 259 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 260 | - if(empty($content)){ |
|
| 261 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 262 | - } |
|
| 244 | + /** |
|
| 245 | + * Send the final page output to user |
|
| 246 | + */ |
|
| 247 | + public function renderFinalPage(){ |
|
| 248 | + $logger = self::getLogger(); |
|
| 249 | + $obj = & get_instance(); |
|
| 250 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 251 | + $dispatcher = $obj->eventdispatcher; |
|
| 252 | + $content = $this->_pageRender; |
|
| 253 | + if(! $content){ |
|
| 254 | + $logger->warning('The final view content is empty.'); |
|
| 255 | + return; |
|
| 256 | + } |
|
| 257 | + //dispatch |
|
| 258 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 259 | + $content = ! empty($event->payload) ? $event->payload : null; |
|
| 260 | + if(empty($content)){ |
|
| 261 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - //check whether need save the page into cache. |
|
| 265 | - if($cachePageStatus){ |
|
| 266 | - //current page URL |
|
| 267 | - $url = $this->_currentUrl; |
|
| 268 | - //Cache view Time to live in second |
|
| 269 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 270 | - if (!empty($obj->view_cache_ttl)){ |
|
| 271 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 272 | - } |
|
| 273 | - //the cache handler instance |
|
| 274 | - $cacheInstance = $obj->cache; |
|
| 275 | - //the current page cache key for identification |
|
| 276 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 264 | + //check whether need save the page into cache. |
|
| 265 | + if($cachePageStatus){ |
|
| 266 | + //current page URL |
|
| 267 | + $url = $this->_currentUrl; |
|
| 268 | + //Cache view Time to live in second |
|
| 269 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 270 | + if (!empty($obj->view_cache_ttl)){ |
|
| 271 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 272 | + } |
|
| 273 | + //the cache handler instance |
|
| 274 | + $cacheInstance = $obj->cache; |
|
| 275 | + //the current page cache key for identification |
|
| 276 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 277 | 277 | |
| 278 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 279 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 278 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 279 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 280 | 280 | |
| 281 | - //get the cache information to prepare header to send to browser |
|
| 282 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 283 | - if($cacheInfo){ |
|
| 284 | - $lastModified = $cacheInfo['mtime']; |
|
| 285 | - $expire = $cacheInfo['expire']; |
|
| 286 | - $maxAge = $expire - time(); |
|
| 287 | - self::setHeader('Pragma', 'public'); |
|
| 288 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 289 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 290 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 291 | - } |
|
| 292 | - } |
|
| 281 | + //get the cache information to prepare header to send to browser |
|
| 282 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 283 | + if($cacheInfo){ |
|
| 284 | + $lastModified = $cacheInfo['mtime']; |
|
| 285 | + $expire = $cacheInfo['expire']; |
|
| 286 | + $maxAge = $expire - time(); |
|
| 287 | + self::setHeader('Pragma', 'public'); |
|
| 288 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 289 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 290 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - // Parse out the elapsed time and memory usage, |
|
| 295 | - // then swap the pseudo-variables with the data |
|
| 296 | - $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 297 | - $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 298 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 294 | + // Parse out the elapsed time and memory usage, |
|
| 295 | + // then swap the pseudo-variables with the data |
|
| 296 | + $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 297 | + $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 298 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 299 | 299 | |
| 300 | - //compress the output if is available |
|
| 301 | - $type = null; |
|
| 302 | - if (self::$_canCompressOutput){ |
|
| 303 | - $type = 'ob_gzhandler'; |
|
| 304 | - } |
|
| 305 | - ob_start($type); |
|
| 306 | - self::sendHeaders(200); |
|
| 307 | - echo $content; |
|
| 308 | - ob_end_flush(); |
|
| 309 | - } |
|
| 300 | + //compress the output if is available |
|
| 301 | + $type = null; |
|
| 302 | + if (self::$_canCompressOutput){ |
|
| 303 | + $type = 'ob_gzhandler'; |
|
| 304 | + } |
|
| 305 | + ob_start($type); |
|
| 306 | + self::sendHeaders(200); |
|
| 307 | + echo $content; |
|
| 308 | + ob_end_flush(); |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Send the final page output to user if is cached |
|
| 313 | - */ |
|
| 314 | - public function renderFinalPageFromCache(&$cache){ |
|
| 315 | - $logger = self::getLogger(); |
|
| 316 | - $url = $this->_currentUrl; |
|
| 317 | - //the current page cache key for identification |
|
| 318 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 311 | + /** |
|
| 312 | + * Send the final page output to user if is cached |
|
| 313 | + */ |
|
| 314 | + public function renderFinalPageFromCache(&$cache){ |
|
| 315 | + $logger = self::getLogger(); |
|
| 316 | + $url = $this->_currentUrl; |
|
| 317 | + //the current page cache key for identification |
|
| 318 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 319 | 319 | |
| 320 | - $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 321 | - //get the cache information to prepare header to send to browser |
|
| 322 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 323 | - if($cacheInfo){ |
|
| 324 | - $lastModified = $cacheInfo['mtime']; |
|
| 325 | - $expire = $cacheInfo['expire']; |
|
| 326 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 327 | - self::setHeader('Pragma', 'public'); |
|
| 328 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 329 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 330 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 331 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 332 | - $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 333 | - self::sendHeaders(304); |
|
| 334 | - return; |
|
| 335 | - } |
|
| 336 | - $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 337 | - self::sendHeaders(200); |
|
| 338 | - //get the cache content |
|
| 339 | - $content = $cache->get($pageCacheKey); |
|
| 340 | - if($content){ |
|
| 341 | - $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 342 | - //load benchmark class |
|
| 343 | - $benchmark = & class_loader('Benchmark'); |
|
| 320 | + $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 321 | + //get the cache information to prepare header to send to browser |
|
| 322 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 323 | + if($cacheInfo){ |
|
| 324 | + $lastModified = $cacheInfo['mtime']; |
|
| 325 | + $expire = $cacheInfo['expire']; |
|
| 326 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 327 | + self::setHeader('Pragma', 'public'); |
|
| 328 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 329 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 330 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 331 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 332 | + $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 333 | + self::sendHeaders(304); |
|
| 334 | + return; |
|
| 335 | + } |
|
| 336 | + $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 337 | + self::sendHeaders(200); |
|
| 338 | + //get the cache content |
|
| 339 | + $content = $cache->get($pageCacheKey); |
|
| 340 | + if($content){ |
|
| 341 | + $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 342 | + //load benchmark class |
|
| 343 | + $benchmark = & class_loader('Benchmark'); |
|
| 344 | 344 | |
| 345 | - // Parse out the elapsed time and memory usage, |
|
| 346 | - // then swap the pseudo-variables with the data |
|
| 347 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 348 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 349 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 345 | + // Parse out the elapsed time and memory usage, |
|
| 346 | + // then swap the pseudo-variables with the data |
|
| 347 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 348 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 349 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 350 | 350 | |
| 351 | - ///display the final output |
|
| 352 | - //compress the output if is available |
|
| 353 | - $type = null; |
|
| 354 | - if (self::$_canCompressOutput){ |
|
| 355 | - $type = 'ob_gzhandler'; |
|
| 356 | - } |
|
| 357 | - ob_start($type); |
|
| 358 | - echo $content; |
|
| 359 | - ob_end_flush(); |
|
| 360 | - return; |
|
| 361 | - } |
|
| 362 | - else{ |
|
| 363 | - $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 364 | - $cache->delete($pageCacheKey); |
|
| 365 | - } |
|
| 351 | + ///display the final output |
|
| 352 | + //compress the output if is available |
|
| 353 | + $type = null; |
|
| 354 | + if (self::$_canCompressOutput){ |
|
| 355 | + $type = 'ob_gzhandler'; |
|
| 356 | + } |
|
| 357 | + ob_start($type); |
|
| 358 | + echo $content; |
|
| 359 | + ob_end_flush(); |
|
| 360 | + return; |
|
| 361 | + } |
|
| 362 | + else{ |
|
| 363 | + $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 364 | + $cache->delete($pageCacheKey); |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - } |
|
| 368 | - } |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - /** |
|
| 371 | - * Get the final page to be rendered |
|
| 372 | - * @return string |
|
| 373 | - */ |
|
| 374 | - public function getFinalPageRendered(){ |
|
| 375 | - return $this->_pageRender; |
|
| 376 | - } |
|
| 370 | + /** |
|
| 371 | + * Get the final page to be rendered |
|
| 372 | + * @return string |
|
| 373 | + */ |
|
| 374 | + public function getFinalPageRendered(){ |
|
| 375 | + return $this->_pageRender; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * Send the HTTP 404 error if can not found the |
|
| 380 | - * routing information for the current request |
|
| 381 | - */ |
|
| 382 | - public static function send404(){ |
|
| 383 | - /********* for logs **************/ |
|
| 384 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 385 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 386 | - $logger = self::getLogger(); |
|
| 387 | - $request =& class_loader('Request', 'classes'); |
|
| 388 | - $userAgent =& class_loader('Browser'); |
|
| 389 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 378 | + /** |
|
| 379 | + * Send the HTTP 404 error if can not found the |
|
| 380 | + * routing information for the current request |
|
| 381 | + */ |
|
| 382 | + public static function send404(){ |
|
| 383 | + /********* for logs **************/ |
|
| 384 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 385 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 386 | + $logger = self::getLogger(); |
|
| 387 | + $request =& class_loader('Request', 'classes'); |
|
| 388 | + $userAgent =& class_loader('Browser'); |
|
| 389 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 390 | 390 | |
| 391 | - //here can't use Loader::functions just include the helper manually |
|
| 392 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 391 | + //here can't use Loader::functions just include the helper manually |
|
| 392 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 393 | 393 | |
| 394 | - $str = '[404 page not found] : '; |
|
| 395 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 396 | - $logger->error($str); |
|
| 397 | - /***********************************/ |
|
| 398 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 399 | - if(file_exists($path)){ |
|
| 400 | - //compress the output if is available |
|
| 401 | - $type = null; |
|
| 402 | - if (self::$_canCompressOutput){ |
|
| 403 | - $type = 'ob_gzhandler'; |
|
| 404 | - } |
|
| 405 | - ob_start($type); |
|
| 406 | - require_once $path; |
|
| 407 | - $output = ob_get_clean(); |
|
| 408 | - self::sendHeaders(404); |
|
| 409 | - echo $output; |
|
| 410 | - } |
|
| 411 | - else{ |
|
| 412 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 413 | - } |
|
| 414 | - } |
|
| 394 | + $str = '[404 page not found] : '; |
|
| 395 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 396 | + $logger->error($str); |
|
| 397 | + /***********************************/ |
|
| 398 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 399 | + if(file_exists($path)){ |
|
| 400 | + //compress the output if is available |
|
| 401 | + $type = null; |
|
| 402 | + if (self::$_canCompressOutput){ |
|
| 403 | + $type = 'ob_gzhandler'; |
|
| 404 | + } |
|
| 405 | + ob_start($type); |
|
| 406 | + require_once $path; |
|
| 407 | + $output = ob_get_clean(); |
|
| 408 | + self::sendHeaders(404); |
|
| 409 | + echo $output; |
|
| 410 | + } |
|
| 411 | + else{ |
|
| 412 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Display the error to user |
|
| 418 | - * @param array $data the error information |
|
| 419 | - */ |
|
| 420 | - public static function sendError(array $data = array()){ |
|
| 421 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 422 | - if(file_exists($path)){ |
|
| 423 | - //compress the output if is available |
|
| 424 | - $type = null; |
|
| 425 | - if (self::$_canCompressOutput){ |
|
| 426 | - $type = 'ob_gzhandler'; |
|
| 427 | - } |
|
| 428 | - ob_start($type); |
|
| 429 | - extract($data); |
|
| 430 | - require_once $path; |
|
| 431 | - $output = ob_get_clean(); |
|
| 432 | - self::sendHeaders(503); |
|
| 433 | - echo $output; |
|
| 434 | - } |
|
| 435 | - else{ |
|
| 436 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 437 | - set_http_status_header(503); |
|
| 438 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - } |
|
| 416 | + /** |
|
| 417 | + * Display the error to user |
|
| 418 | + * @param array $data the error information |
|
| 419 | + */ |
|
| 420 | + public static function sendError(array $data = array()){ |
|
| 421 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 422 | + if(file_exists($path)){ |
|
| 423 | + //compress the output if is available |
|
| 424 | + $type = null; |
|
| 425 | + if (self::$_canCompressOutput){ |
|
| 426 | + $type = 'ob_gzhandler'; |
|
| 427 | + } |
|
| 428 | + ob_start($type); |
|
| 429 | + extract($data); |
|
| 430 | + require_once $path; |
|
| 431 | + $output = ob_get_clean(); |
|
| 432 | + self::sendHeaders(503); |
|
| 433 | + echo $output; |
|
| 434 | + } |
|
| 435 | + else{ |
|
| 436 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 437 | + set_http_status_header(503); |
|
| 438 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The list of request header to send with response |
@@ -65,15 +65,15 @@ discard block |
||
| 65 | 65 | /** |
| 66 | 66 | * Construct new response instance |
| 67 | 67 | */ |
| 68 | - public function __construct(){ |
|
| 68 | + public function __construct() { |
|
| 69 | 69 | $currentUrl = ''; |
| 70 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 70 | + if (!empty($_SERVER['REQUEST_URI'])) { |
|
| 71 | 71 | $currentUrl = $_SERVER['REQUEST_URI']; |
| 72 | 72 | } |
| 73 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 73 | + if (!empty($_SERVER['QUERY_STRING'])) { |
|
| 74 | 74 | $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
| 75 | 75 | } |
| 76 | - $this->_currentUrl = $currentUrl; |
|
| 76 | + $this->_currentUrl = $currentUrl; |
|
| 77 | 77 | |
| 78 | 78 | $this->_currentUrlCacheKey = md5($this->_currentUrl); |
| 79 | 79 | |
@@ -88,9 +88,9 @@ discard block |
||
| 88 | 88 | * Get the logger singleton instance |
| 89 | 89 | * @return Log the logger instance |
| 90 | 90 | */ |
| 91 | - private static function getLogger(){ |
|
| 92 | - if(self::$logger == null){ |
|
| 93 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 91 | + private static function getLogger() { |
|
| 92 | + if (self::$logger == null) { |
|
| 93 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 94 | 94 | self::$logger[0]->setLogger('Library::Response'); |
| 95 | 95 | } |
| 96 | 96 | return self::$logger[0]; |
@@ -101,12 +101,12 @@ discard block |
||
| 101 | 101 | * @param integer $httpCode the HTTP status code |
| 102 | 102 | * @param array $headers the additional headers to add to the existing headers list |
| 103 | 103 | */ |
| 104 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 104 | + public static function sendHeaders($httpCode = 200, array $headers = array()) { |
|
| 105 | 105 | set_http_status_header($httpCode); |
| 106 | 106 | self::setHeaders($headers); |
| 107 | - if(! headers_sent()){ |
|
| 108 | - foreach(self::getHeaders() as $key => $value){ |
|
| 109 | - header($key .': '.$value); |
|
| 107 | + if (!headers_sent()) { |
|
| 108 | + foreach (self::getHeaders() as $key => $value) { |
|
| 109 | + header($key . ': ' . $value); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | * Get the list of the headers |
| 116 | 116 | * @return array the headers list |
| 117 | 117 | */ |
| 118 | - public static function getHeaders(){ |
|
| 118 | + public static function getHeaders() { |
|
| 119 | 119 | return self::$headers; |
| 120 | 120 | } |
| 121 | 121 | |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | * @param string $name the header name |
| 125 | 125 | * @return string the header value |
| 126 | 126 | */ |
| 127 | - public static function getHeader($name){ |
|
| 127 | + public static function getHeader($name) { |
|
| 128 | 128 | return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
| 129 | 129 | } |
| 130 | 130 | |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @param string $name the header name |
| 135 | 135 | * @param string $value the header value to be set |
| 136 | 136 | */ |
| 137 | - public static function setHeader($name, $value){ |
|
| 137 | + public static function setHeader($name, $value) { |
|
| 138 | 138 | self::$headers[$name] = $value; |
| 139 | 139 | } |
| 140 | 140 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * @param array $headers the list of the headers to set. |
| 144 | 144 | * Note: this will merge with the existing headers |
| 145 | 145 | */ |
| 146 | - public static function setHeaders(array $headers){ |
|
| 146 | + public static function setHeaders(array $headers) { |
|
| 147 | 147 | self::$headers = array_merge(self::getHeaders(), $headers); |
| 148 | 148 | } |
| 149 | 149 | |
@@ -151,17 +151,17 @@ discard block |
||
| 151 | 151 | * Redirect user in the specified page |
| 152 | 152 | * @param string $path the URL or URI to be redirect to |
| 153 | 153 | */ |
| 154 | - public static function redirect($path = ''){ |
|
| 154 | + public static function redirect($path = '') { |
|
| 155 | 155 | $logger = self::getLogger(); |
| 156 | 156 | $url = Url::site_url($path); |
| 157 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 158 | - if(! headers_sent()){ |
|
| 159 | - header('Location: '.$url); |
|
| 157 | + $logger->info('Redirect to URL [' . $url . ']'); |
|
| 158 | + if (!headers_sent()) { |
|
| 159 | + header('Location: ' . $url); |
|
| 160 | 160 | exit; |
| 161 | 161 | } |
| 162 | - else{ |
|
| 162 | + else { |
|
| 163 | 163 | echo '<script> |
| 164 | - location.href = "'.$url.'"; |
|
| 164 | + location.href = "'.$url . '"; |
|
| 165 | 165 | </script>'; |
| 166 | 166 | } |
| 167 | 167 | } |
@@ -174,10 +174,10 @@ discard block |
||
| 174 | 174 | * @return void|string if $return is true will return the view content otherwise |
| 175 | 175 | * will display the view content. |
| 176 | 176 | */ |
| 177 | - public function render($view, $data = null, $return = false){ |
|
| 177 | + public function render($view, $data = null, $return = false) { |
|
| 178 | 178 | $logger = self::getLogger(); |
| 179 | 179 | //convert data to an array |
| 180 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 180 | + $data = !is_array($data) ? (array) $data : $data; |
|
| 181 | 181 | $view = str_ireplace('.php', '', $view); |
| 182 | 182 | $view = trim($view, '/\\'); |
| 183 | 183 | $viewFile = $view . '.php'; |
@@ -186,42 +186,42 @@ discard block |
||
| 186 | 186 | //super instance |
| 187 | 187 | $obj = & get_instance(); |
| 188 | 188 | |
| 189 | - if(Module::hasModule()){ |
|
| 189 | + if (Module::hasModule()) { |
|
| 190 | 190 | //check in module first |
| 191 | 191 | $logger->debug('Checking the view [' . $view . '] from module list ...'); |
| 192 | 192 | $mod = null; |
| 193 | 193 | //check if the request class contains module name |
| 194 | - if(strpos($view, '/') !== false){ |
|
| 194 | + if (strpos($view, '/') !== false) { |
|
| 195 | 195 | $viewPath = explode('/', $view); |
| 196 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 196 | + if (isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())) { |
|
| 197 | 197 | $mod = $viewPath[0]; |
| 198 | 198 | array_shift($viewPath); |
| 199 | 199 | $view = implode('/', $viewPath); |
| 200 | 200 | $viewFile = $view . '.php'; |
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 203 | + if (!$mod && !empty($obj->moduleName)) { |
|
| 204 | 204 | $mod = $obj->moduleName; |
| 205 | 205 | } |
| 206 | - if($mod){ |
|
| 206 | + if ($mod) { |
|
| 207 | 207 | $moduleViewPath = Module::findViewFullPath($view, $mod); |
| 208 | - if($moduleViewPath){ |
|
| 208 | + if ($moduleViewPath) { |
|
| 209 | 209 | $path = $moduleViewPath; |
| 210 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 210 | + $logger->info('Found view [' . $view . '] in module [' . $mod . '], the file path is [' . $moduleViewPath . '] we will used it'); |
|
| 211 | 211 | } |
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 212 | + else { |
|
| 213 | + $logger->info('Cannot find view [' . $view . '] in module [' . $mod . '] using the default location'); |
|
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | - else{ |
|
| 216 | + else { |
|
| 217 | 217 | $logger->info('The current request does not use module using the default location.'); |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | $logger->info('The view file path to be loaded is [' . $path . ']'); |
| 221 | 221 | $found = false; |
| 222 | - if(file_exists($path)){ |
|
| 223 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 224 | - if(! isset($this->{$key})){ |
|
| 222 | + if (file_exists($path)) { |
|
| 223 | + foreach (get_object_vars($obj) as $key => $value) { |
|
| 224 | + if (!isset($this->{$key})) { |
|
| 225 | 225 | $this->{$key} = & $obj->{$key}; |
| 226 | 226 | } |
| 227 | 227 | } |
@@ -230,44 +230,44 @@ discard block |
||
| 230 | 230 | //need use require() instead of require_once because can load this view many time |
| 231 | 231 | require $path; |
| 232 | 232 | $content = ob_get_clean(); |
| 233 | - if($return){ |
|
| 233 | + if ($return) { |
|
| 234 | 234 | return $content; |
| 235 | 235 | } |
| 236 | 236 | $this->_pageRender .= $content; |
| 237 | 237 | $found = true; |
| 238 | 238 | } |
| 239 | - if(! $found){ |
|
| 240 | - show_error('Unable to find view [' .$view . ']'); |
|
| 239 | + if (!$found) { |
|
| 240 | + show_error('Unable to find view [' . $view . ']'); |
|
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
| 245 | 245 | * Send the final page output to user |
| 246 | 246 | */ |
| 247 | - public function renderFinalPage(){ |
|
| 247 | + public function renderFinalPage() { |
|
| 248 | 248 | $logger = self::getLogger(); |
| 249 | 249 | $obj = & get_instance(); |
| 250 | 250 | $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
| 251 | 251 | $dispatcher = $obj->eventdispatcher; |
| 252 | 252 | $content = $this->_pageRender; |
| 253 | - if(! $content){ |
|
| 253 | + if (!$content) { |
|
| 254 | 254 | $logger->warning('The final view content is empty.'); |
| 255 | 255 | return; |
| 256 | 256 | } |
| 257 | 257 | //dispatch |
| 258 | 258 | $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
| 259 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 260 | - if(empty($content)){ |
|
| 259 | + $content = !empty($event->payload) ? $event->payload : null; |
|
| 260 | + if (empty($content)) { |
|
| 261 | 261 | $logger->warning('The view content is empty after dispatch to event listeners.'); |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | //check whether need save the page into cache. |
| 265 | - if($cachePageStatus){ |
|
| 265 | + if ($cachePageStatus) { |
|
| 266 | 266 | //current page URL |
| 267 | 267 | $url = $this->_currentUrl; |
| 268 | 268 | //Cache view Time to live in second |
| 269 | 269 | $viewCacheTtl = get_config('cache_ttl'); |
| 270 | - if (!empty($obj->view_cache_ttl)){ |
|
| 270 | + if (!empty($obj->view_cache_ttl)) { |
|
| 271 | 271 | $viewCacheTtl = $obj->view_cache_ttl; |
| 272 | 272 | } |
| 273 | 273 | //the cache handler instance |
@@ -280,14 +280,14 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | //get the cache information to prepare header to send to browser |
| 282 | 282 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
| 283 | - if($cacheInfo){ |
|
| 283 | + if ($cacheInfo) { |
|
| 284 | 284 | $lastModified = $cacheInfo['mtime']; |
| 285 | 285 | $expire = $cacheInfo['expire']; |
| 286 | 286 | $maxAge = $expire - time(); |
| 287 | 287 | self::setHeader('Pragma', 'public'); |
| 288 | 288 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
| 289 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 290 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 289 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
| 290 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
| 291 | 291 | } |
| 292 | 292 | } |
| 293 | 293 | |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | |
| 300 | 300 | //compress the output if is available |
| 301 | 301 | $type = null; |
| 302 | - if (self::$_canCompressOutput){ |
|
| 302 | + if (self::$_canCompressOutput) { |
|
| 303 | 303 | $type = 'ob_gzhandler'; |
| 304 | 304 | } |
| 305 | 305 | ob_start($type); |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | /** |
| 312 | 312 | * Send the final page output to user if is cached |
| 313 | 313 | */ |
| 314 | - public function renderFinalPageFromCache(&$cache){ |
|
| 314 | + public function renderFinalPageFromCache(&$cache) { |
|
| 315 | 315 | $logger = self::getLogger(); |
| 316 | 316 | $url = $this->_currentUrl; |
| 317 | 317 | //the current page cache key for identification |
@@ -320,15 +320,15 @@ discard block |
||
| 320 | 320 | $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
| 321 | 321 | //get the cache information to prepare header to send to browser |
| 322 | 322 | $cacheInfo = $cache->getInfo($pageCacheKey); |
| 323 | - if($cacheInfo){ |
|
| 323 | + if ($cacheInfo) { |
|
| 324 | 324 | $lastModified = $cacheInfo['mtime']; |
| 325 | 325 | $expire = $cacheInfo['expire']; |
| 326 | 326 | $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
| 327 | 327 | self::setHeader('Pragma', 'public'); |
| 328 | 328 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
| 329 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 330 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 331 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 329 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
| 330 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
| 331 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
|
| 332 | 332 | $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
| 333 | 333 | self::sendHeaders(304); |
| 334 | 334 | return; |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | self::sendHeaders(200); |
| 338 | 338 | //get the cache content |
| 339 | 339 | $content = $cache->get($pageCacheKey); |
| 340 | - if($content){ |
|
| 340 | + if ($content) { |
|
| 341 | 341 | $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
| 342 | 342 | //load benchmark class |
| 343 | 343 | $benchmark = & class_loader('Benchmark'); |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | ///display the final output |
| 352 | 352 | //compress the output if is available |
| 353 | 353 | $type = null; |
| 354 | - if (self::$_canCompressOutput){ |
|
| 354 | + if (self::$_canCompressOutput) { |
|
| 355 | 355 | $type = 'ob_gzhandler'; |
| 356 | 356 | } |
| 357 | 357 | ob_start($type); |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | ob_end_flush(); |
| 360 | 360 | return; |
| 361 | 361 | } |
| 362 | - else{ |
|
| 362 | + else { |
|
| 363 | 363 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
| 364 | 364 | $cache->delete($pageCacheKey); |
| 365 | 365 | } |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * Get the final page to be rendered |
| 372 | 372 | * @return string |
| 373 | 373 | */ |
| 374 | - public function getFinalPageRendered(){ |
|
| 374 | + public function getFinalPageRendered() { |
|
| 375 | 375 | return $this->_pageRender; |
| 376 | 376 | } |
| 377 | 377 | |
@@ -379,14 +379,14 @@ discard block |
||
| 379 | 379 | * Send the HTTP 404 error if can not found the |
| 380 | 380 | * routing information for the current request |
| 381 | 381 | */ |
| 382 | - public static function send404(){ |
|
| 382 | + public static function send404() { |
|
| 383 | 383 | /********* for logs **************/ |
| 384 | 384 | //can't use $obj = & get_instance() here because the global super object will be available until |
| 385 | 385 | //the main controller is loaded even for Loader::library('xxxx'); |
| 386 | 386 | $logger = self::getLogger(); |
| 387 | - $request =& class_loader('Request', 'classes'); |
|
| 388 | - $userAgent =& class_loader('Browser'); |
|
| 389 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 387 | + $request = & class_loader('Request', 'classes'); |
|
| 388 | + $userAgent = & class_loader('Browser'); |
|
| 389 | + $browser = $userAgent->getPlatform() . ', ' . $userAgent->getBrowser() . ' ' . $userAgent->getVersion(); |
|
| 390 | 390 | |
| 391 | 391 | //here can't use Loader::functions just include the helper manually |
| 392 | 392 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
@@ -396,10 +396,10 @@ discard block |
||
| 396 | 396 | $logger->error($str); |
| 397 | 397 | /***********************************/ |
| 398 | 398 | $path = CORE_VIEWS_PATH . '404.php'; |
| 399 | - if(file_exists($path)){ |
|
| 399 | + if (file_exists($path)) { |
|
| 400 | 400 | //compress the output if is available |
| 401 | 401 | $type = null; |
| 402 | - if (self::$_canCompressOutput){ |
|
| 402 | + if (self::$_canCompressOutput) { |
|
| 403 | 403 | $type = 'ob_gzhandler'; |
| 404 | 404 | } |
| 405 | 405 | ob_start($type); |
@@ -408,8 +408,8 @@ discard block |
||
| 408 | 408 | self::sendHeaders(404); |
| 409 | 409 | echo $output; |
| 410 | 410 | } |
| 411 | - else{ |
|
| 412 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 411 | + else { |
|
| 412 | + show_error('The 404 view [' . $path . '] does not exist'); |
|
| 413 | 413 | } |
| 414 | 414 | } |
| 415 | 415 | |
@@ -417,12 +417,12 @@ discard block |
||
| 417 | 417 | * Display the error to user |
| 418 | 418 | * @param array $data the error information |
| 419 | 419 | */ |
| 420 | - public static function sendError(array $data = array()){ |
|
| 420 | + public static function sendError(array $data = array()) { |
|
| 421 | 421 | $path = CORE_VIEWS_PATH . 'errors.php'; |
| 422 | - if(file_exists($path)){ |
|
| 422 | + if (file_exists($path)) { |
|
| 423 | 423 | //compress the output if is available |
| 424 | 424 | $type = null; |
| 425 | - if (self::$_canCompressOutput){ |
|
| 425 | + if (self::$_canCompressOutput) { |
|
| 426 | 426 | $type = 'ob_gzhandler'; |
| 427 | 427 | } |
| 428 | 428 | ob_start($type); |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | self::sendHeaders(503); |
| 433 | 433 | echo $output; |
| 434 | 434 | } |
| 435 | - else{ |
|
| 435 | + else { |
|
| 436 | 436 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
| 437 | 437 | set_http_status_header(503); |
| 438 | 438 | echo 'The error view [' . $path . '] does not exist'; |
@@ -158,8 +158,7 @@ discard block |
||
| 158 | 158 | if(! headers_sent()){ |
| 159 | 159 | header('Location: '.$url); |
| 160 | 160 | exit; |
| 161 | - } |
|
| 162 | - else{ |
|
| 161 | + } else{ |
|
| 163 | 162 | echo '<script> |
| 164 | 163 | location.href = "'.$url.'"; |
| 165 | 164 | </script>'; |
@@ -208,12 +207,10 @@ discard block |
||
| 208 | 207 | if($moduleViewPath){ |
| 209 | 208 | $path = $moduleViewPath; |
| 210 | 209 | $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
| 211 | - } |
|
| 212 | - else{ |
|
| 210 | + } else{ |
|
| 213 | 211 | $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
| 214 | 212 | } |
| 215 | - } |
|
| 216 | - else{ |
|
| 213 | + } else{ |
|
| 217 | 214 | $logger->info('The current request does not use module using the default location.'); |
| 218 | 215 | } |
| 219 | 216 | } |
@@ -358,8 +355,7 @@ discard block |
||
| 358 | 355 | echo $content; |
| 359 | 356 | ob_end_flush(); |
| 360 | 357 | return; |
| 361 | - } |
|
| 362 | - else{ |
|
| 358 | + } else{ |
|
| 363 | 359 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
| 364 | 360 | $cache->delete($pageCacheKey); |
| 365 | 361 | } |
@@ -407,8 +403,7 @@ discard block |
||
| 407 | 403 | $output = ob_get_clean(); |
| 408 | 404 | self::sendHeaders(404); |
| 409 | 405 | echo $output; |
| 410 | - } |
|
| 411 | - else{ |
|
| 406 | + } else{ |
|
| 412 | 407 | show_error('The 404 view [' .$path. '] does not exist'); |
| 413 | 408 | } |
| 414 | 409 | } |
@@ -431,8 +426,7 @@ discard block |
||
| 431 | 426 | $output = ob_get_clean(); |
| 432 | 427 | self::sendHeaders(503); |
| 433 | 428 | echo $output; |
| 434 | - } |
|
| 435 | - else{ |
|
| 429 | + } else{ |
|
| 436 | 430 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
| 437 | 431 | set_http_status_header(503); |
| 438 | 432 | echo 'The error view [' . $path . '] does not exist'; |
@@ -1,32 +1,32 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class Pagination{ |
| 28 | 28 | |
| 29 | - /** |
|
| 29 | + /** |
|
| 30 | 30 | * The list of loaded config |
| 31 | 31 | * @var array |
| 32 | 32 | */ |
@@ -49,15 +49,15 @@ discard block |
||
| 49 | 49 | if (empty($config) || ! is_array($config)){ |
| 50 | 50 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - if (! empty($overwriteConfig)){ |
|
| 54 | - $config = array_merge($config, $overwriteConfig); |
|
| 55 | - } |
|
| 56 | - $this->config = $config; |
|
| 52 | + else{ |
|
| 53 | + if (! empty($overwriteConfig)){ |
|
| 54 | + $config = array_merge($config, $overwriteConfig); |
|
| 55 | + } |
|
| 56 | + $this->config = $config; |
|
| 57 | 57 | //put it gobally |
| 58 | - Config::setAll($config); |
|
| 59 | - unset($config); |
|
| 60 | - } |
|
| 58 | + Config::setAll($config); |
|
| 59 | + unset($config); |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | else{ |
| 63 | 63 | show_error('Unable to find the pagination configuration file'); |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | return $this->paginationQueryString; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Set the value of the pagination query string |
|
| 99 | - * @param string $paginationQueryString the new value |
|
| 100 | - * @return object |
|
| 101 | - */ |
|
| 97 | + /** |
|
| 98 | + * Set the value of the pagination query string |
|
| 99 | + * @param string $paginationQueryString the new value |
|
| 100 | + * @return object |
|
| 101 | + */ |
|
| 102 | 102 | public function setPaginationQueryString($paginationQueryString){ |
| 103 | 103 | $this->paginationQueryString = $paginationQueryString; |
| 104 | 104 | return $this; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $queryString = Url::queryString(); |
| 118 | 118 | $currentUrl = Url::current(); |
| 119 | 119 | $query = ''; |
| 120 | - if ($queryString == ''){ |
|
| 120 | + if ($queryString == ''){ |
|
| 121 | 121 | $query = '?' . $pageQueryName . '='; |
| 122 | 122 | } |
| 123 | 123 | else{ |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | public function getLink($totalRows, $currentPageNumber){ |
| 152 | 152 | $numberOfLink = $this->config['nb_link']; |
| 153 | - $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 153 | + $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 154 | 154 | if (empty($this->paginationQueryString)){ |
| 155 | 155 | //determine the pagination query string value |
| 156 | 156 | $this->determinePaginationQueryStringValue(); |
@@ -164,8 +164,8 @@ discard block |
||
| 164 | 164 | $numberOfRowPerPage = (int) $numberOfRowPerPage; |
| 165 | 165 | |
| 166 | 166 | if ($currentPageNumber <= 0){ |
| 167 | - $currentPageNumber = 1; |
|
| 168 | - } |
|
| 167 | + $currentPageNumber = 1; |
|
| 168 | + } |
|
| 169 | 169 | if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0) { |
| 170 | 170 | return $navbar; |
| 171 | 171 | } |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $navbar .= $this->buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber); |
| 207 | 207 | } |
| 208 | 208 | else if ($currentPageNumber == $numberOfPage){ |
| 209 | - $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
|
| 209 | + $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
|
| 210 | 210 | } |
| 211 | 211 | $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
| 212 | 212 | return $navbar; |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | require_once CONFIG_PATH . 'config_pagination.php'; |
| 49 | 49 | if (empty($config) || ! is_array($config)){ |
| 50 | 50 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 51 | - } |
|
| 52 | - else{ |
|
| 51 | + } else{ |
|
| 53 | 52 | if (! empty($overwriteConfig)){ |
| 54 | 53 | $config = array_merge($config, $overwriteConfig); |
| 55 | 54 | } |
@@ -58,8 +57,7 @@ discard block |
||
| 58 | 57 | Config::setAll($config); |
| 59 | 58 | unset($config); |
| 60 | 59 | } |
| 61 | - } |
|
| 62 | - else{ |
|
| 60 | + } else{ |
|
| 63 | 61 | show_error('Unable to find the pagination configuration file'); |
| 64 | 62 | } |
| 65 | 63 | } |
@@ -119,18 +117,15 @@ discard block |
||
| 119 | 117 | $query = ''; |
| 120 | 118 | if ($queryString == ''){ |
| 121 | 119 | $query = '?' . $pageQueryName . '='; |
| 122 | - } |
|
| 123 | - else{ |
|
| 120 | + } else{ |
|
| 124 | 121 | $tab = explode($pageQueryName . '=', $queryString); |
| 125 | 122 | $nb = count($tab); |
| 126 | 123 | if ($nb == 1){ |
| 127 | 124 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
| 128 | - } |
|
| 129 | - else{ |
|
| 125 | + } else{ |
|
| 130 | 126 | if ($tab[0] == ''){ |
| 131 | 127 | $query = '?' . $pageQueryName . '='; |
| 132 | - } |
|
| 133 | - else{ |
|
| 128 | + } else{ |
|
| 134 | 129 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
| 135 | 130 | } |
| 136 | 131 | } |
@@ -175,20 +170,17 @@ discard block |
||
| 175 | 170 | if ($numberOfLink % 2 == 0){ |
| 176 | 171 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
| 177 | 172 | $end = $currentPageNumber + ($numberOfLink / 2); |
| 178 | - } |
|
| 179 | - else{ |
|
| 173 | + } else{ |
|
| 180 | 174 | $start = $currentPageNumber - floor($numberOfLink / 2); |
| 181 | 175 | $end = $currentPageNumber + floor($numberOfLink / 2); |
| 182 | 176 | } |
| 183 | 177 | if ($start <= 1){ |
| 184 | 178 | $begin = 1; |
| 185 | 179 | $end = $numberOfLink; |
| 186 | - } |
|
| 187 | - else if ($start > 1 && $end < $numberOfPage){ |
|
| 180 | + } else if ($start > 1 && $end < $numberOfPage){ |
|
| 188 | 181 | $begin = $start; |
| 189 | 182 | $end = $end; |
| 190 | - } |
|
| 191 | - else{ |
|
| 183 | + } else{ |
|
| 192 | 184 | $begin = ($numberOfPage - $numberOfLink) + 1; |
| 193 | 185 | $end = $numberOfPage; |
| 194 | 186 | } |
@@ -204,8 +196,7 @@ discard block |
||
| 204 | 196 | /////////// |
| 205 | 197 | else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
| 206 | 198 | $navbar .= $this->buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber); |
| 207 | - } |
|
| 208 | - else if ($currentPageNumber == $numberOfPage){ |
|
| 199 | + } else if ($currentPageNumber == $numberOfPage){ |
|
| 209 | 200 | $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
| 210 | 201 | } |
| 211 | 202 | $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
@@ -225,8 +216,7 @@ discard block |
||
| 225 | 216 | for($i = $begin; $i <= $end; $i++){ |
| 226 | 217 | if ($i == $currentPageNumber){ |
| 227 | 218 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 228 | - } |
|
| 229 | - else{ |
|
| 219 | + } else{ |
|
| 230 | 220 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 231 | 221 | } |
| 232 | 222 | } |
@@ -248,8 +238,7 @@ discard block |
||
| 248 | 238 | for($i = $begin; $i <= $end; $i++){ |
| 249 | 239 | if ($i == $currentPageNumber){ |
| 250 | 240 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 251 | - } |
|
| 252 | - else{ |
|
| 241 | + } else{ |
|
| 253 | 242 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
| 254 | 243 | } |
| 255 | 244 | } |
@@ -271,8 +260,7 @@ discard block |
||
| 271 | 260 | for($i = $begin; $i <= $end; $i++){ |
| 272 | 261 | if ($i == $currentPageNumber){ |
| 273 | 262 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 274 | - } |
|
| 275 | - else{ |
|
| 263 | + } else{ |
|
| 276 | 264 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 277 | 265 | } |
| 278 | 266 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Pagination{ |
|
| 27 | + class Pagination { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The list of loaded config |
@@ -42,15 +42,15 @@ discard block |
||
| 42 | 42 | * Create an instance of pagination |
| 43 | 43 | * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
| 44 | 44 | */ |
| 45 | - public function __construct($overwriteConfig = array()){ |
|
| 46 | - if (file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
| 45 | + public function __construct($overwriteConfig = array()) { |
|
| 46 | + if (file_exists(CONFIG_PATH . 'config_pagination.php')) { |
|
| 47 | 47 | $config = array(); |
| 48 | 48 | require_once CONFIG_PATH . 'config_pagination.php'; |
| 49 | - if (empty($config) || ! is_array($config)){ |
|
| 49 | + if (empty($config) || !is_array($config)) { |
|
| 50 | 50 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - if (! empty($overwriteConfig)){ |
|
| 52 | + else { |
|
| 53 | + if (!empty($overwriteConfig)) { |
|
| 54 | 54 | $config = array_merge($config, $overwriteConfig); |
| 55 | 55 | } |
| 56 | 56 | $this->config = $config; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | unset($config); |
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | - else{ |
|
| 62 | + else { |
|
| 63 | 63 | show_error('Unable to find the pagination configuration file'); |
| 64 | 64 | } |
| 65 | 65 | } |
@@ -70,8 +70,8 @@ discard block |
||
| 70 | 70 | * config_pagination.php |
| 71 | 71 | * @param array $config the configuration to overwrite |
| 72 | 72 | */ |
| 73 | - public function setConfig(array $config = array()){ |
|
| 74 | - if (! empty($config)){ |
|
| 73 | + public function setConfig(array $config = array()) { |
|
| 74 | + if (!empty($config)) { |
|
| 75 | 75 | $this->config = array_merge($this->config, $config); |
| 76 | 76 | Config::setAll($config); |
| 77 | 77 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return array |
| 84 | 84 | */ |
| 85 | - public function getConfig(){ |
|
| 85 | + public function getConfig() { |
|
| 86 | 86 | return $this->config; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * Return the value of the pagination query string |
| 91 | 91 | * @return string |
| 92 | 92 | */ |
| 93 | - public function getPaginationQueryString(){ |
|
| 93 | + public function getPaginationQueryString() { |
|
| 94 | 94 | return $this->paginationQueryString; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | * @param string $paginationQueryString the new value |
| 100 | 100 | * @return object |
| 101 | 101 | */ |
| 102 | - public function setPaginationQueryString($paginationQueryString){ |
|
| 102 | + public function setPaginationQueryString($paginationQueryString) { |
|
| 103 | 103 | $this->paginationQueryString = $paginationQueryString; |
| 104 | 104 | return $this; |
| 105 | 105 | } |
@@ -112,25 +112,25 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return object |
| 114 | 114 | */ |
| 115 | - public function determinePaginationQueryStringValue(){ |
|
| 115 | + public function determinePaginationQueryStringValue() { |
|
| 116 | 116 | $pageQueryName = $this->config['page_query_string_name']; |
| 117 | 117 | $queryString = Url::queryString(); |
| 118 | 118 | $currentUrl = Url::current(); |
| 119 | 119 | $query = ''; |
| 120 | - if ($queryString == ''){ |
|
| 120 | + if ($queryString == '') { |
|
| 121 | 121 | $query = '?' . $pageQueryName . '='; |
| 122 | 122 | } |
| 123 | - else{ |
|
| 123 | + else { |
|
| 124 | 124 | $tab = explode($pageQueryName . '=', $queryString); |
| 125 | 125 | $nb = count($tab); |
| 126 | - if ($nb == 1){ |
|
| 126 | + if ($nb == 1) { |
|
| 127 | 127 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
| 128 | 128 | } |
| 129 | - else{ |
|
| 130 | - if ($tab[0] == ''){ |
|
| 129 | + else { |
|
| 130 | + if ($tab[0] == '') { |
|
| 131 | 131 | $query = '?' . $pageQueryName . '='; |
| 132 | 132 | } |
| 133 | - else{ |
|
| 133 | + else { |
|
| 134 | 134 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -148,10 +148,10 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return string the pagination link |
| 150 | 150 | */ |
| 151 | - public function getLink($totalRows, $currentPageNumber){ |
|
| 151 | + public function getLink($totalRows, $currentPageNumber) { |
|
| 152 | 152 | $numberOfLink = $this->config['nb_link']; |
| 153 | 153 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
| 154 | - if (empty($this->paginationQueryString)){ |
|
| 154 | + if (empty($this->paginationQueryString)) { |
|
| 155 | 155 | //determine the pagination query string value |
| 156 | 156 | $this->determinePaginationQueryStringValue(); |
| 157 | 157 | } |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | $numberOfLink = (int) $numberOfLink; |
| 163 | 163 | $numberOfRowPerPage = (int) $numberOfRowPerPage; |
| 164 | 164 | |
| 165 | - if ($currentPageNumber <= 0){ |
|
| 165 | + if ($currentPageNumber <= 0) { |
|
| 166 | 166 | $currentPageNumber = 1; |
| 167 | 167 | } |
| 168 | 168 | if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0) { |
@@ -171,27 +171,27 @@ discard block |
||
| 171 | 171 | $start = null; |
| 172 | 172 | $end = null; |
| 173 | 173 | $begin = null; |
| 174 | - if ($numberOfLink % 2 == 0){ |
|
| 174 | + if ($numberOfLink % 2 == 0) { |
|
| 175 | 175 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
| 176 | 176 | $end = $currentPageNumber + ($numberOfLink / 2); |
| 177 | 177 | } |
| 178 | - else{ |
|
| 178 | + else { |
|
| 179 | 179 | $start = $currentPageNumber - floor($numberOfLink / 2); |
| 180 | 180 | $end = $currentPageNumber + floor($numberOfLink / 2); |
| 181 | 181 | } |
| 182 | - if ($start <= 1){ |
|
| 182 | + if ($start <= 1) { |
|
| 183 | 183 | $begin = 1; |
| 184 | 184 | $end = $numberOfLink; |
| 185 | 185 | } |
| 186 | - else if ($start > 1 && $end < $numberOfPage){ |
|
| 186 | + else if ($start > 1 && $end < $numberOfPage) { |
|
| 187 | 187 | $begin = $start; |
| 188 | 188 | $end = $end; |
| 189 | 189 | } |
| 190 | - else{ |
|
| 190 | + else { |
|
| 191 | 191 | $begin = ($numberOfPage - $numberOfLink) + 1; |
| 192 | 192 | $end = $numberOfPage; |
| 193 | 193 | } |
| 194 | - if ($numberOfPage <= $numberOfLink){ |
|
| 194 | + if ($numberOfPage <= $numberOfLink) { |
|
| 195 | 195 | $begin = 1; |
| 196 | 196 | $end = $numberOfPage; |
| 197 | 197 | } |
@@ -199,14 +199,14 @@ discard block |
||
| 199 | 199 | $end = (int) $end; |
| 200 | 200 | |
| 201 | 201 | //** |
| 202 | - if ($currentPageNumber == 1){ |
|
| 202 | + if ($currentPageNumber == 1) { |
|
| 203 | 203 | $navbar .= $this->buildPaginationLinkForFirstPage($begin, $end, $currentPageNumber); |
| 204 | 204 | } |
| 205 | 205 | /////////// |
| 206 | - else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
| 206 | + else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage) { |
|
| 207 | 207 | $navbar .= $this->buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber); |
| 208 | 208 | } |
| 209 | - else if ($currentPageNumber == $numberOfPage){ |
|
| 209 | + else if ($currentPageNumber == $numberOfPage) { |
|
| 210 | 210 | $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
| 211 | 211 | } |
| 212 | 212 | $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
@@ -220,14 +220,14 @@ discard block |
||
| 220 | 220 | * @param int $currentPageNumber the pagination current page number |
| 221 | 221 | * @return string |
| 222 | 222 | */ |
| 223 | - protected function buildPaginationLinkForFirstPage($begin, $end, $currentPageNumber){ |
|
| 223 | + protected function buildPaginationLinkForFirstPage($begin, $end, $currentPageNumber) { |
|
| 224 | 224 | $navbar = null; |
| 225 | 225 | $query = $this->paginationQueryString; |
| 226 | - for($i = $begin; $i <= $end; $i++){ |
|
| 227 | - if ($i == $currentPageNumber){ |
|
| 226 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 227 | + if ($i == $currentPageNumber) { |
|
| 228 | 228 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 229 | 229 | } |
| 230 | - else{ |
|
| 230 | + else { |
|
| 231 | 231 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 232 | 232 | } |
| 233 | 233 | } |
@@ -242,19 +242,19 @@ discard block |
||
| 242 | 242 | * @param int $currentPageNumber the pagination current page number |
| 243 | 243 | * @return string |
| 244 | 244 | */ |
| 245 | - protected function buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber){ |
|
| 245 | + protected function buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber) { |
|
| 246 | 246 | $navbar = null; |
| 247 | 247 | $query = $this->paginationQueryString; |
| 248 | 248 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 249 | - for($i = $begin; $i <= $end; $i++){ |
|
| 250 | - if ($i == $currentPageNumber){ |
|
| 249 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 250 | + if ($i == $currentPageNumber) { |
|
| 251 | 251 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 252 | 252 | } |
| 253 | - else{ |
|
| 254 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
| 253 | + else { |
|
| 254 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
| 257 | + $navbar .= $this->config['next_open'] . "<a href='$query" . ($currentPageNumber + 1) . "'>" . $this->config['next_text'] . "</a>" . $this->config['next_close']; |
|
| 258 | 258 | return $navbar; |
| 259 | 259 | } |
| 260 | 260 | |
@@ -265,15 +265,15 @@ discard block |
||
| 265 | 265 | * @param int $currentPageNumber the pagination current page number |
| 266 | 266 | * @return string |
| 267 | 267 | */ |
| 268 | - protected function buildPaginationLinkForLastPage($begin, $end, $currentPageNumber){ |
|
| 268 | + protected function buildPaginationLinkForLastPage($begin, $end, $currentPageNumber) { |
|
| 269 | 269 | $navbar = null; |
| 270 | 270 | $query = $this->paginationQueryString; |
| 271 | 271 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 272 | - for($i = $begin; $i <= $end; $i++){ |
|
| 273 | - if ($i == $currentPageNumber){ |
|
| 272 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 273 | + if ($i == $currentPageNumber) { |
|
| 274 | 274 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 275 | 275 | } |
| 276 | - else{ |
|
| 276 | + else { |
|
| 277 | 277 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 278 | 278 | } |
| 279 | 279 | } |