@@ -1,578 +1,578 @@ |
||
| 1 | 1 | <?php |
| 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 | - */ |
|
| 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 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @file common.php |
|
| 29 | - * |
|
| 30 | - * Contains most of the commons functions used by the system |
|
| 31 | - * |
|
| 32 | - * @package core |
|
| 33 | - * @author Tony NGUEREZA |
|
| 34 | - * @copyright Copyright (c) 2017 |
|
| 35 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | - * @link http://www.iacademy.cf |
|
| 37 | - * @version 1.0.0 |
|
| 38 | - * @filesource |
|
| 39 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file common.php |
|
| 29 | + * |
|
| 30 | + * Contains most of the commons functions used by the system |
|
| 31 | + * |
|
| 32 | + * @package core |
|
| 33 | + * @author Tony NGUEREZA |
|
| 34 | + * @copyright Copyright (c) 2017 |
|
| 35 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | + * @link http://www.iacademy.cf |
|
| 37 | + * @version 1.0.0 |
|
| 38 | + * @filesource |
|
| 39 | + */ |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This function is the class loader helper is used if the library "Loader" not yet loaded |
|
| 44 | - * he load the class once |
|
| 45 | - * @param string $class the class name to be loaded |
|
| 46 | - * @param string $dir the directory where to find the class |
|
| 47 | - * @param mixed $params the parameter to pass as argument to the constructor of the class |
|
| 48 | - * @codeCoverageIgnore |
|
| 49 | - * |
|
| 50 | - * @return object the instance of the loaded class |
|
| 51 | - */ |
|
| 52 | - function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 53 | - //put the first letter of class to upper case |
|
| 54 | - $class = ucfirst($class); |
|
| 55 | - static $classes = array(); |
|
| 56 | - if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 57 | - return $classes[$class]; |
|
| 58 | - } |
|
| 59 | - $found = false; |
|
| 60 | - foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 61 | - $file = $path . $dir . '/' . $class . '.php'; |
|
| 62 | - if (file_exists($file)){ |
|
| 63 | - if (class_exists($class, false) === false){ |
|
| 64 | - require_once $file; |
|
| 65 | - } |
|
| 66 | - //already found |
|
| 67 | - $found = true; |
|
| 68 | - break; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - if (! $found){ |
|
| 72 | - //can't use show_error() at this time because some dependencies not yet loaded |
|
| 73 | - set_http_status_header(503); |
|
| 74 | - echo 'Cannot find the class [' . $class . ']'; |
|
| 75 | - die(); |
|
| 76 | - } |
|
| 42 | + /** |
|
| 43 | + * This function is the class loader helper is used if the library "Loader" not yet loaded |
|
| 44 | + * he load the class once |
|
| 45 | + * @param string $class the class name to be loaded |
|
| 46 | + * @param string $dir the directory where to find the class |
|
| 47 | + * @param mixed $params the parameter to pass as argument to the constructor of the class |
|
| 48 | + * @codeCoverageIgnore |
|
| 49 | + * |
|
| 50 | + * @return object the instance of the loaded class |
|
| 51 | + */ |
|
| 52 | + function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 53 | + //put the first letter of class to upper case |
|
| 54 | + $class = ucfirst($class); |
|
| 55 | + static $classes = array(); |
|
| 56 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 57 | + return $classes[$class]; |
|
| 58 | + } |
|
| 59 | + $found = false; |
|
| 60 | + foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 61 | + $file = $path . $dir . '/' . $class . '.php'; |
|
| 62 | + if (file_exists($file)){ |
|
| 63 | + if (class_exists($class, false) === false){ |
|
| 64 | + require_once $file; |
|
| 65 | + } |
|
| 66 | + //already found |
|
| 67 | + $found = true; |
|
| 68 | + break; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + if (! $found){ |
|
| 72 | + //can't use show_error() at this time because some dependencies not yet loaded |
|
| 73 | + set_http_status_header(503); |
|
| 74 | + echo 'Cannot find the class [' . $class . ']'; |
|
| 75 | + die(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /* |
|
| 78 | + /* |
|
| 79 | 79 | TODO use the best method to get the Log instance |
| 80 | 80 | */ |
| 81 | - if ($class == 'Log'){ |
|
| 82 | - //can't use the instruction like "return new Log()" |
|
| 83 | - //because we need return the reference instance of the loaded class. |
|
| 84 | - $log = new Log(); |
|
| 85 | - return $log; |
|
| 86 | - } |
|
| 87 | - //track of loaded classes |
|
| 88 | - class_loaded($class); |
|
| 81 | + if ($class == 'Log'){ |
|
| 82 | + //can't use the instruction like "return new Log()" |
|
| 83 | + //because we need return the reference instance of the loaded class. |
|
| 84 | + $log = new Log(); |
|
| 85 | + return $log; |
|
| 86 | + } |
|
| 87 | + //track of loaded classes |
|
| 88 | + class_loaded($class); |
|
| 89 | 89 | |
| 90 | - //record the class instance |
|
| 91 | - $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 90 | + //record the class instance |
|
| 91 | + $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 92 | 92 | |
| 93 | - return $classes[$class]; |
|
| 94 | - } |
|
| 93 | + return $classes[$class]; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * This function is the helper to record the loaded classes |
|
| 98 | - * @param string $class the loaded class name |
|
| 99 | - * @codeCoverageIgnore |
|
| 100 | - * |
|
| 101 | - * @return array the list of the loaded classes |
|
| 102 | - */ |
|
| 103 | - function & class_loaded($class = null){ |
|
| 104 | - static $list = array(); |
|
| 105 | - if ($class !== null){ |
|
| 106 | - $list[strtolower($class)] = $class; |
|
| 107 | - } |
|
| 108 | - return $list; |
|
| 109 | - } |
|
| 96 | + /** |
|
| 97 | + * This function is the helper to record the loaded classes |
|
| 98 | + * @param string $class the loaded class name |
|
| 99 | + * @codeCoverageIgnore |
|
| 100 | + * |
|
| 101 | + * @return array the list of the loaded classes |
|
| 102 | + */ |
|
| 103 | + function & class_loaded($class = null){ |
|
| 104 | + static $list = array(); |
|
| 105 | + if ($class !== null){ |
|
| 106 | + $list[strtolower($class)] = $class; |
|
| 107 | + } |
|
| 108 | + return $list; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * This function is used to load the configurations in the case the "Config" library not yet loaded |
|
| 113 | - * @param array $overwrite_values if need overwrite the existing configuration |
|
| 114 | - * @codeCoverageIgnore |
|
| 115 | - * |
|
| 116 | - * @return array the configurations values |
|
| 117 | - */ |
|
| 118 | - function & load_configurations(array $overwrite_values = array()){ |
|
| 119 | - static $config; |
|
| 120 | - if (empty($config)){ |
|
| 121 | - $file = CONFIG_PATH . 'config.php'; |
|
| 122 | - $found = false; |
|
| 123 | - if (file_exists($file)){ |
|
| 124 | - require_once $file; |
|
| 125 | - $found = true; |
|
| 126 | - } |
|
| 127 | - if (! $found){ |
|
| 128 | - set_http_status_header(503); |
|
| 129 | - echo 'Unable to find the configuration file [' . $file . ']'; |
|
| 130 | - die(); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - foreach ($overwrite_values as $key => $value) { |
|
| 134 | - $config[$key] = $value; |
|
| 135 | - } |
|
| 136 | - return $config; |
|
| 137 | - } |
|
| 111 | + /** |
|
| 112 | + * This function is used to load the configurations in the case the "Config" library not yet loaded |
|
| 113 | + * @param array $overwrite_values if need overwrite the existing configuration |
|
| 114 | + * @codeCoverageIgnore |
|
| 115 | + * |
|
| 116 | + * @return array the configurations values |
|
| 117 | + */ |
|
| 118 | + function & load_configurations(array $overwrite_values = array()){ |
|
| 119 | + static $config; |
|
| 120 | + if (empty($config)){ |
|
| 121 | + $file = CONFIG_PATH . 'config.php'; |
|
| 122 | + $found = false; |
|
| 123 | + if (file_exists($file)){ |
|
| 124 | + require_once $file; |
|
| 125 | + $found = true; |
|
| 126 | + } |
|
| 127 | + if (! $found){ |
|
| 128 | + set_http_status_header(503); |
|
| 129 | + echo 'Unable to find the configuration file [' . $file . ']'; |
|
| 130 | + die(); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + foreach ($overwrite_values as $key => $value) { |
|
| 134 | + $config[$key] = $value; |
|
| 135 | + } |
|
| 136 | + return $config; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * This function is the helper to get the config value in case the "Config" library not yet loaded |
|
| 141 | - * @param string $key the config item to get the vale |
|
| 142 | - * @param mixed $default the default value to return if can't find the config item in the configuration |
|
| 143 | - * @test |
|
| 144 | - * |
|
| 145 | - * @return mixed the config value |
|
| 146 | - */ |
|
| 147 | - function get_config($key, $default = null){ |
|
| 148 | - static $cfg; |
|
| 149 | - if (empty($cfg)){ |
|
| 150 | - $cfg[0] = & load_configurations(); |
|
| 151 | - } |
|
| 152 | - return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 153 | - } |
|
| 139 | + /** |
|
| 140 | + * This function is the helper to get the config value in case the "Config" library not yet loaded |
|
| 141 | + * @param string $key the config item to get the vale |
|
| 142 | + * @param mixed $default the default value to return if can't find the config item in the configuration |
|
| 143 | + * @test |
|
| 144 | + * |
|
| 145 | + * @return mixed the config value |
|
| 146 | + */ |
|
| 147 | + function get_config($key, $default = null){ |
|
| 148 | + static $cfg; |
|
| 149 | + if (empty($cfg)){ |
|
| 150 | + $cfg[0] = & load_configurations(); |
|
| 151 | + } |
|
| 152 | + return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * This function is a helper to logging message |
|
| 157 | - * @param string $level the log level "ERROR", "DEBUG", "INFO", etc. |
|
| 158 | - * @param string $message the log message to be saved |
|
| 159 | - * @param string $logger the logger to use if is set |
|
| 160 | - * |
|
| 161 | - * @codeCoverageIgnore |
|
| 162 | - */ |
|
| 163 | - function save_to_log($level, $message, $logger = null){ |
|
| 164 | - $log =& class_loader('Log', 'classes'); |
|
| 165 | - if ($logger){ |
|
| 166 | - $log->setLogger($logger); |
|
| 167 | - } |
|
| 168 | - $log->writeLog($message, $level); |
|
| 169 | - } |
|
| 155 | + /** |
|
| 156 | + * This function is a helper to logging message |
|
| 157 | + * @param string $level the log level "ERROR", "DEBUG", "INFO", etc. |
|
| 158 | + * @param string $message the log message to be saved |
|
| 159 | + * @param string $logger the logger to use if is set |
|
| 160 | + * |
|
| 161 | + * @codeCoverageIgnore |
|
| 162 | + */ |
|
| 163 | + function save_to_log($level, $message, $logger = null){ |
|
| 164 | + $log =& class_loader('Log', 'classes'); |
|
| 165 | + if ($logger){ |
|
| 166 | + $log->setLogger($logger); |
|
| 167 | + } |
|
| 168 | + $log->writeLog($message, $level); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Set the HTTP status header |
|
| 173 | - * @param integer $code the HTTP status code |
|
| 174 | - * @param string $text the HTTP status text |
|
| 175 | - * |
|
| 176 | - * @codeCoverageIgnore |
|
| 177 | - */ |
|
| 178 | - function set_http_status_header($code = 200, $text = null){ |
|
| 179 | - if (empty($text)){ |
|
| 180 | - $http_status = array( |
|
| 181 | - 100 => 'Continue', |
|
| 182 | - 101 => 'Switching Protocols', |
|
| 183 | - 200 => 'OK', |
|
| 184 | - 201 => 'Created', |
|
| 185 | - 202 => 'Accepted', |
|
| 186 | - 203 => 'Non-Authoritative Information', |
|
| 187 | - 204 => 'No Content', |
|
| 188 | - 205 => 'Reset Content', |
|
| 189 | - 206 => 'Partial Content', |
|
| 190 | - 300 => 'Multiple Choices', |
|
| 191 | - 301 => 'Moved Permanently', |
|
| 192 | - 302 => 'Found', |
|
| 193 | - 303 => 'See Other', |
|
| 194 | - 304 => 'Not Modified', |
|
| 195 | - 305 => 'Use Proxy', |
|
| 196 | - 307 => 'Temporary Redirect', |
|
| 197 | - 400 => 'Bad Request', |
|
| 198 | - 401 => 'Unauthorized', |
|
| 199 | - 402 => 'Payment Required', |
|
| 200 | - 403 => 'Forbidden', |
|
| 201 | - 404 => 'Not Found', |
|
| 202 | - 405 => 'Method Not Allowed', |
|
| 203 | - 406 => 'Not Acceptable', |
|
| 204 | - 407 => 'Proxy Authentication Required', |
|
| 205 | - 408 => 'Request Timeout', |
|
| 206 | - 409 => 'Conflict', |
|
| 207 | - 410 => 'Gone', |
|
| 208 | - 411 => 'Length Required', |
|
| 209 | - 412 => 'Precondition Failed', |
|
| 210 | - 413 => 'Request Entity Too Large', |
|
| 211 | - 414 => 'Request-URI Too Long', |
|
| 212 | - 415 => 'Unsupported Media Type', |
|
| 213 | - 416 => 'Requested Range Not Satisfiable', |
|
| 214 | - 417 => 'Expectation Failed', |
|
| 215 | - 418 => 'I\'m a teapot', |
|
| 216 | - 500 => 'Internal Server Error', |
|
| 217 | - 501 => 'Not Implemented', |
|
| 218 | - 502 => 'Bad Gateway', |
|
| 219 | - 503 => 'Service Unavailable', |
|
| 220 | - 504 => 'Gateway Timeout', |
|
| 221 | - 505 => 'HTTP Version Not Supported', |
|
| 222 | - ); |
|
| 223 | - if (isset($http_status[$code])){ |
|
| 224 | - $text = $http_status[$code]; |
|
| 225 | - } |
|
| 226 | - else{ |
|
| 227 | - show_error('No HTTP status text found for your code please check it.'); |
|
| 228 | - } |
|
| 229 | - } |
|
| 171 | + /** |
|
| 172 | + * Set the HTTP status header |
|
| 173 | + * @param integer $code the HTTP status code |
|
| 174 | + * @param string $text the HTTP status text |
|
| 175 | + * |
|
| 176 | + * @codeCoverageIgnore |
|
| 177 | + */ |
|
| 178 | + function set_http_status_header($code = 200, $text = null){ |
|
| 179 | + if (empty($text)){ |
|
| 180 | + $http_status = array( |
|
| 181 | + 100 => 'Continue', |
|
| 182 | + 101 => 'Switching Protocols', |
|
| 183 | + 200 => 'OK', |
|
| 184 | + 201 => 'Created', |
|
| 185 | + 202 => 'Accepted', |
|
| 186 | + 203 => 'Non-Authoritative Information', |
|
| 187 | + 204 => 'No Content', |
|
| 188 | + 205 => 'Reset Content', |
|
| 189 | + 206 => 'Partial Content', |
|
| 190 | + 300 => 'Multiple Choices', |
|
| 191 | + 301 => 'Moved Permanently', |
|
| 192 | + 302 => 'Found', |
|
| 193 | + 303 => 'See Other', |
|
| 194 | + 304 => 'Not Modified', |
|
| 195 | + 305 => 'Use Proxy', |
|
| 196 | + 307 => 'Temporary Redirect', |
|
| 197 | + 400 => 'Bad Request', |
|
| 198 | + 401 => 'Unauthorized', |
|
| 199 | + 402 => 'Payment Required', |
|
| 200 | + 403 => 'Forbidden', |
|
| 201 | + 404 => 'Not Found', |
|
| 202 | + 405 => 'Method Not Allowed', |
|
| 203 | + 406 => 'Not Acceptable', |
|
| 204 | + 407 => 'Proxy Authentication Required', |
|
| 205 | + 408 => 'Request Timeout', |
|
| 206 | + 409 => 'Conflict', |
|
| 207 | + 410 => 'Gone', |
|
| 208 | + 411 => 'Length Required', |
|
| 209 | + 412 => 'Precondition Failed', |
|
| 210 | + 413 => 'Request Entity Too Large', |
|
| 211 | + 414 => 'Request-URI Too Long', |
|
| 212 | + 415 => 'Unsupported Media Type', |
|
| 213 | + 416 => 'Requested Range Not Satisfiable', |
|
| 214 | + 417 => 'Expectation Failed', |
|
| 215 | + 418 => 'I\'m a teapot', |
|
| 216 | + 500 => 'Internal Server Error', |
|
| 217 | + 501 => 'Not Implemented', |
|
| 218 | + 502 => 'Bad Gateway', |
|
| 219 | + 503 => 'Service Unavailable', |
|
| 220 | + 504 => 'Gateway Timeout', |
|
| 221 | + 505 => 'HTTP Version Not Supported', |
|
| 222 | + ); |
|
| 223 | + if (isset($http_status[$code])){ |
|
| 224 | + $text = $http_status[$code]; |
|
| 225 | + } |
|
| 226 | + else{ |
|
| 227 | + show_error('No HTTP status text found for your code please check it.'); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 232 | - header('Status: ' . $code . ' ' . $text, TRUE); |
|
| 233 | - } |
|
| 234 | - else{ |
|
| 235 | - $proto = 'HTTP/1.1'; |
|
| 236 | - if(isset($_SERVER['SERVER_PROTOCOL'])){ |
|
| 237 | - $proto = $_SERVER['SERVER_PROTOCOL']; |
|
| 238 | - } |
|
| 239 | - header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
|
| 240 | - } |
|
| 241 | - } |
|
| 231 | + if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 232 | + header('Status: ' . $code . ' ' . $text, TRUE); |
|
| 233 | + } |
|
| 234 | + else{ |
|
| 235 | + $proto = 'HTTP/1.1'; |
|
| 236 | + if(isset($_SERVER['SERVER_PROTOCOL'])){ |
|
| 237 | + $proto = $_SERVER['SERVER_PROTOCOL']; |
|
| 238 | + } |
|
| 239 | + header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * This function displays an error message to the user and ends the execution of the script. |
|
| 245 | - * |
|
| 246 | - * @param string $msg the message to display |
|
| 247 | - * @param string $title the message title: "error", "info", "warning", etc. |
|
| 248 | - * @param boolean $logging either to save error in log |
|
| 249 | - * |
|
| 250 | - * @codeCoverageIgnore |
|
| 251 | - */ |
|
| 252 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 253 | - $title = strtoupper($title); |
|
| 254 | - $data = array(); |
|
| 255 | - $data['error'] = $msg; |
|
| 256 | - $data['title'] = $title; |
|
| 257 | - if ($logging){ |
|
| 258 | - save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 259 | - } |
|
| 260 | - $response = & class_loader('Response', 'classes'); |
|
| 261 | - $response->sendError($data); |
|
| 262 | - die(); |
|
| 263 | - } |
|
| 243 | + /** |
|
| 244 | + * This function displays an error message to the user and ends the execution of the script. |
|
| 245 | + * |
|
| 246 | + * @param string $msg the message to display |
|
| 247 | + * @param string $title the message title: "error", "info", "warning", etc. |
|
| 248 | + * @param boolean $logging either to save error in log |
|
| 249 | + * |
|
| 250 | + * @codeCoverageIgnore |
|
| 251 | + */ |
|
| 252 | + function show_error($msg, $title = 'error', $logging = true){ |
|
| 253 | + $title = strtoupper($title); |
|
| 254 | + $data = array(); |
|
| 255 | + $data['error'] = $msg; |
|
| 256 | + $data['title'] = $title; |
|
| 257 | + if ($logging){ |
|
| 258 | + save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 259 | + } |
|
| 260 | + $response = & class_loader('Response', 'classes'); |
|
| 261 | + $response->sendError($data); |
|
| 262 | + die(); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Check whether the protocol used is "https" or not |
|
| 267 | - * That is, the web server is configured to use a secure connection. |
|
| 268 | - * @codeCoverageIgnore |
|
| 269 | - * |
|
| 270 | - * @return boolean true if the web server uses the https protocol, false if not. |
|
| 271 | - */ |
|
| 272 | - function is_https(){ |
|
| 273 | - /* |
|
| 265 | + /** |
|
| 266 | + * Check whether the protocol used is "https" or not |
|
| 267 | + * That is, the web server is configured to use a secure connection. |
|
| 268 | + * @codeCoverageIgnore |
|
| 269 | + * |
|
| 270 | + * @return boolean true if the web server uses the https protocol, false if not. |
|
| 271 | + */ |
|
| 272 | + function is_https(){ |
|
| 273 | + /* |
|
| 274 | 274 | * some servers pass the "HTTPS" parameter in the server variable, |
| 275 | 275 | * if is the case, check if the value is "on", "true", "1". |
| 276 | 276 | */ |
| 277 | - if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 278 | - return true; |
|
| 279 | - } |
|
| 280 | - if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 281 | - return true; |
|
| 282 | - } |
|
| 283 | - if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 284 | - return true; |
|
| 285 | - } |
|
| 286 | - return false; |
|
| 287 | - } |
|
| 277 | + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 278 | + return true; |
|
| 279 | + } |
|
| 280 | + if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 281 | + return true; |
|
| 282 | + } |
|
| 283 | + if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 284 | + return true; |
|
| 285 | + } |
|
| 286 | + return false; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * This function is used to check the URL format of the given string argument. |
|
| 291 | - * The address is valid if the protocol is http, https, ftp, etc. |
|
| 292 | - * |
|
| 293 | - * @param string $url the URL address to check |
|
| 294 | - * @test |
|
| 295 | - * |
|
| 296 | - * @return boolean true if is a valid URL address or false. |
|
| 297 | - */ |
|
| 298 | - function is_url($url){ |
|
| 299 | - return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
|
| 300 | - } |
|
| 289 | + /** |
|
| 290 | + * This function is used to check the URL format of the given string argument. |
|
| 291 | + * The address is valid if the protocol is http, https, ftp, etc. |
|
| 292 | + * |
|
| 293 | + * @param string $url the URL address to check |
|
| 294 | + * @test |
|
| 295 | + * |
|
| 296 | + * @return boolean true if is a valid URL address or false. |
|
| 297 | + */ |
|
| 298 | + function is_url($url){ |
|
| 299 | + return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - /** |
|
| 303 | - * Function defined to load controller |
|
| 304 | - * |
|
| 305 | - * @param string $controllerClass the controller class name to be loaded |
|
| 306 | - * @codeCoverageIgnore |
|
| 307 | - */ |
|
| 308 | - function autoload_controller($controllerClass){ |
|
| 309 | - if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 310 | - require_once $path; |
|
| 311 | - } |
|
| 312 | - } |
|
| 302 | + /** |
|
| 303 | + * Function defined to load controller |
|
| 304 | + * |
|
| 305 | + * @param string $controllerClass the controller class name to be loaded |
|
| 306 | + * @codeCoverageIgnore |
|
| 307 | + */ |
|
| 308 | + function autoload_controller($controllerClass){ |
|
| 309 | + if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 310 | + require_once $path; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * Function defined for handling PHP exception error message, |
|
| 316 | - * it displays an error message using the function "show_error" |
|
| 317 | - * |
|
| 318 | - * @param object $ex instance of the "Exception" class or a derived class |
|
| 319 | - * @codeCoverageIgnore |
|
| 320 | - * |
|
| 321 | - * @return boolean |
|
| 322 | - */ |
|
| 323 | - function php_exception_handler($ex){ |
|
| 324 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 325 | - show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 326 | - } |
|
| 327 | - else{ |
|
| 328 | - save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
|
| 329 | - } |
|
| 330 | - return true; |
|
| 331 | - } |
|
| 314 | + /** |
|
| 315 | + * Function defined for handling PHP exception error message, |
|
| 316 | + * it displays an error message using the function "show_error" |
|
| 317 | + * |
|
| 318 | + * @param object $ex instance of the "Exception" class or a derived class |
|
| 319 | + * @codeCoverageIgnore |
|
| 320 | + * |
|
| 321 | + * @return boolean |
|
| 322 | + */ |
|
| 323 | + function php_exception_handler($ex){ |
|
| 324 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 325 | + show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 326 | + } |
|
| 327 | + else{ |
|
| 328 | + save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
|
| 329 | + } |
|
| 330 | + return true; |
|
| 331 | + } |
|
| 332 | 332 | |
| 333 | - /** |
|
| 334 | - * Function defined for PHP error message handling |
|
| 335 | - * |
|
| 336 | - * @param int $errno the type of error for example: E_USER_ERROR, E_USER_WARNING, etc. |
|
| 337 | - * @param string $errstr the error message |
|
| 338 | - * @param string $errfile the file where the error occurred |
|
| 339 | - * @param int $errline the line number where the error occurred |
|
| 340 | - * @codeCoverageIgnore |
|
| 341 | - * |
|
| 342 | - * @return boolean |
|
| 343 | - */ |
|
| 344 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 345 | - $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
|
| 346 | - if ($isError){ |
|
| 347 | - set_http_status_header(500); |
|
| 348 | - } |
|
| 349 | - if (! (error_reporting() & $errno)) { |
|
| 350 | - save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
|
| 351 | - return; |
|
| 352 | - } |
|
| 353 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 354 | - $errorType = 'error'; |
|
| 355 | - switch ($errno) { |
|
| 356 | - case E_USER_WARNING: |
|
| 357 | - $errorType = 'warning'; |
|
| 358 | - break; |
|
| 359 | - case E_USER_NOTICE: |
|
| 360 | - $errorType = 'notice'; |
|
| 361 | - break; |
|
| 362 | - } |
|
| 363 | - show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 364 | - } |
|
| 365 | - if ($isError){ |
|
| 366 | - die(); |
|
| 367 | - } |
|
| 368 | - return true; |
|
| 369 | - } |
|
| 333 | + /** |
|
| 334 | + * Function defined for PHP error message handling |
|
| 335 | + * |
|
| 336 | + * @param int $errno the type of error for example: E_USER_ERROR, E_USER_WARNING, etc. |
|
| 337 | + * @param string $errstr the error message |
|
| 338 | + * @param string $errfile the file where the error occurred |
|
| 339 | + * @param int $errline the line number where the error occurred |
|
| 340 | + * @codeCoverageIgnore |
|
| 341 | + * |
|
| 342 | + * @return boolean |
|
| 343 | + */ |
|
| 344 | + function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 345 | + $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
|
| 346 | + if ($isError){ |
|
| 347 | + set_http_status_header(500); |
|
| 348 | + } |
|
| 349 | + if (! (error_reporting() & $errno)) { |
|
| 350 | + save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
|
| 351 | + return; |
|
| 352 | + } |
|
| 353 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 354 | + $errorType = 'error'; |
|
| 355 | + switch ($errno) { |
|
| 356 | + case E_USER_WARNING: |
|
| 357 | + $errorType = 'warning'; |
|
| 358 | + break; |
|
| 359 | + case E_USER_NOTICE: |
|
| 360 | + $errorType = 'notice'; |
|
| 361 | + break; |
|
| 362 | + } |
|
| 363 | + show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 364 | + } |
|
| 365 | + if ($isError){ |
|
| 366 | + die(); |
|
| 367 | + } |
|
| 368 | + return true; |
|
| 369 | + } |
|
| 370 | 370 | |
| 371 | - /** |
|
| 372 | - * This function is used to run in shutdown situation of the script |
|
| 373 | - * @codeCoverageIgnore |
|
| 374 | - */ |
|
| 375 | - function php_shudown_handler(){ |
|
| 376 | - $lastError = error_get_last(); |
|
| 377 | - if (isset($lastError) && |
|
| 378 | - ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 379 | - php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
|
| 380 | - } |
|
| 381 | - } |
|
| 371 | + /** |
|
| 372 | + * This function is used to run in shutdown situation of the script |
|
| 373 | + * @codeCoverageIgnore |
|
| 374 | + */ |
|
| 375 | + function php_shudown_handler(){ |
|
| 376 | + $lastError = error_get_last(); |
|
| 377 | + if (isset($lastError) && |
|
| 378 | + ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 379 | + php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | 383 | |
| 384 | - /** |
|
| 385 | - * Convert array attributes to string |
|
| 386 | - * |
|
| 387 | - * This function converts an associative array into HTML attributes. |
|
| 388 | - * For example : |
|
| 389 | - * $a = array('name' => 'Foo', 'type' => 'text'); => produces the following string: |
|
| 390 | - * name = "Foo" type = "text" |
|
| 391 | - * |
|
| 392 | - * @param array $attributes associative array to convert to a string attribute. |
|
| 393 | - * |
|
| 394 | - * @return string string of the HTML attribute. |
|
| 395 | - */ |
|
| 396 | - function attributes_to_string(array $attributes){ |
|
| 397 | - $str = ' '; |
|
| 398 | - //we check that the array passed as an argument is not empty. |
|
| 399 | - if (! empty($attributes)){ |
|
| 400 | - foreach($attributes as $key => $value){ |
|
| 401 | - $key = trim(htmlspecialchars($key)); |
|
| 402 | - $value = trim(htmlspecialchars($value)); |
|
| 403 | - /* |
|
| 384 | + /** |
|
| 385 | + * Convert array attributes to string |
|
| 386 | + * |
|
| 387 | + * This function converts an associative array into HTML attributes. |
|
| 388 | + * For example : |
|
| 389 | + * $a = array('name' => 'Foo', 'type' => 'text'); => produces the following string: |
|
| 390 | + * name = "Foo" type = "text" |
|
| 391 | + * |
|
| 392 | + * @param array $attributes associative array to convert to a string attribute. |
|
| 393 | + * |
|
| 394 | + * @return string string of the HTML attribute. |
|
| 395 | + */ |
|
| 396 | + function attributes_to_string(array $attributes){ |
|
| 397 | + $str = ' '; |
|
| 398 | + //we check that the array passed as an argument is not empty. |
|
| 399 | + if (! empty($attributes)){ |
|
| 400 | + foreach($attributes as $key => $value){ |
|
| 401 | + $key = trim(htmlspecialchars($key)); |
|
| 402 | + $value = trim(htmlspecialchars($value)); |
|
| 403 | + /* |
|
| 404 | 404 | * To predict the case where the string to convert contains the character " |
| 405 | 405 | * we check if this is the case we add a slash to solve this problem. |
| 406 | 406 | * For example: |
| 407 | 407 | * $attr = array('placeholder' => 'I am a "puple"') |
| 408 | 408 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 409 | 409 | */ |
| 410 | - if ($value && strpos('"', $value) !== false){ |
|
| 411 | - $value = addslashes($value); |
|
| 412 | - } |
|
| 413 | - $str .= $key.' = "'.$value.'" '; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - //remove the space after using rtrim() |
|
| 417 | - return rtrim($str); |
|
| 418 | - } |
|
| 410 | + if ($value && strpos('"', $value) !== false){ |
|
| 411 | + $value = addslashes($value); |
|
| 412 | + } |
|
| 413 | + $str .= $key.' = "'.$value.'" '; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + //remove the space after using rtrim() |
|
| 417 | + return rtrim($str); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | 420 | |
| 421 | - /** |
|
| 422 | - * Function to stringfy PHP variable, useful in debug situation |
|
| 423 | - * |
|
| 424 | - * @param mixed $var the variable to stringfy |
|
| 425 | - * @codeCoverageIgnore |
|
| 426 | - * |
|
| 427 | - * @return string the stringfy value |
|
| 428 | - */ |
|
| 429 | - function stringfy_vars($var){ |
|
| 430 | - return print_r($var, true); |
|
| 431 | - } |
|
| 421 | + /** |
|
| 422 | + * Function to stringfy PHP variable, useful in debug situation |
|
| 423 | + * |
|
| 424 | + * @param mixed $var the variable to stringfy |
|
| 425 | + * @codeCoverageIgnore |
|
| 426 | + * |
|
| 427 | + * @return string the stringfy value |
|
| 428 | + */ |
|
| 429 | + function stringfy_vars($var){ |
|
| 430 | + return print_r($var, true); |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | - /** |
|
| 434 | - * Clean the user input |
|
| 435 | - * @param mixed $str the value to clean |
|
| 436 | - * @test |
|
| 437 | - * |
|
| 438 | - * @return mixed the sanitize value |
|
| 439 | - */ |
|
| 440 | - function clean_input($str){ |
|
| 441 | - if (is_array($str)){ |
|
| 442 | - $str = array_map('clean_input', $str); |
|
| 443 | - } |
|
| 444 | - else if (is_object($str)){ |
|
| 445 | - $obj = $str; |
|
| 446 | - foreach ($str as $var => $value) { |
|
| 447 | - $obj->$var = clean_input($value); |
|
| 448 | - } |
|
| 449 | - $str = $obj; |
|
| 450 | - } |
|
| 451 | - else{ |
|
| 452 | - $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 453 | - } |
|
| 454 | - return $str; |
|
| 455 | - } |
|
| 433 | + /** |
|
| 434 | + * Clean the user input |
|
| 435 | + * @param mixed $str the value to clean |
|
| 436 | + * @test |
|
| 437 | + * |
|
| 438 | + * @return mixed the sanitize value |
|
| 439 | + */ |
|
| 440 | + function clean_input($str){ |
|
| 441 | + if (is_array($str)){ |
|
| 442 | + $str = array_map('clean_input', $str); |
|
| 443 | + } |
|
| 444 | + else if (is_object($str)){ |
|
| 445 | + $obj = $str; |
|
| 446 | + foreach ($str as $var => $value) { |
|
| 447 | + $obj->$var = clean_input($value); |
|
| 448 | + } |
|
| 449 | + $str = $obj; |
|
| 450 | + } |
|
| 451 | + else{ |
|
| 452 | + $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 453 | + } |
|
| 454 | + return $str; |
|
| 455 | + } |
|
| 456 | 456 | |
| 457 | - /** |
|
| 458 | - * This function is used to hidden some part of the given string. Helpful if you need hide some confidential |
|
| 459 | - * Information like credit card number, password, etc. |
|
| 460 | - * |
|
| 461 | - * @param string $str the string you want to hide some part |
|
| 462 | - * @param int $startCount the length of non hidden for the beginning char |
|
| 463 | - * @param int $endCount the length of non hidden for the ending char |
|
| 464 | - * @param string $hiddenChar the char used to hide the given string |
|
| 465 | - * @test |
|
| 466 | - * |
|
| 467 | - * @return string the string with the hidden part. |
|
| 468 | - */ |
|
| 469 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 470 | - //get the string length |
|
| 471 | - $len = strlen($str); |
|
| 472 | - //if str is empty |
|
| 473 | - if ($len <= 0){ |
|
| 474 | - return str_repeat($hiddenChar, 6); |
|
| 475 | - } |
|
| 476 | - //if the length is less than startCount and endCount |
|
| 477 | - //or the startCount and endCount length is 0 |
|
| 478 | - //or startCount is negative or endCount is negative |
|
| 479 | - //return the full string hidden |
|
| 457 | + /** |
|
| 458 | + * This function is used to hidden some part of the given string. Helpful if you need hide some confidential |
|
| 459 | + * Information like credit card number, password, etc. |
|
| 460 | + * |
|
| 461 | + * @param string $str the string you want to hide some part |
|
| 462 | + * @param int $startCount the length of non hidden for the beginning char |
|
| 463 | + * @param int $endCount the length of non hidden for the ending char |
|
| 464 | + * @param string $hiddenChar the char used to hide the given string |
|
| 465 | + * @test |
|
| 466 | + * |
|
| 467 | + * @return string the string with the hidden part. |
|
| 468 | + */ |
|
| 469 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 470 | + //get the string length |
|
| 471 | + $len = strlen($str); |
|
| 472 | + //if str is empty |
|
| 473 | + if ($len <= 0){ |
|
| 474 | + return str_repeat($hiddenChar, 6); |
|
| 475 | + } |
|
| 476 | + //if the length is less than startCount and endCount |
|
| 477 | + //or the startCount and endCount length is 0 |
|
| 478 | + //or startCount is negative or endCount is negative |
|
| 479 | + //return the full string hidden |
|
| 480 | 480 | |
| 481 | - if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 482 | - return str_repeat($hiddenChar, $len); |
|
| 483 | - } |
|
| 484 | - //the start non hidden string |
|
| 485 | - $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 486 | - //the end non hidden string |
|
| 487 | - $endNonHiddenStr = null; |
|
| 488 | - if ($endCount > 0){ |
|
| 489 | - $endNonHiddenStr = substr($str, - $endCount); |
|
| 490 | - } |
|
| 491 | - //the hidden string |
|
| 492 | - $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 481 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 482 | + return str_repeat($hiddenChar, $len); |
|
| 483 | + } |
|
| 484 | + //the start non hidden string |
|
| 485 | + $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 486 | + //the end non hidden string |
|
| 487 | + $endNonHiddenStr = null; |
|
| 488 | + if ($endCount > 0){ |
|
| 489 | + $endNonHiddenStr = substr($str, - $endCount); |
|
| 490 | + } |
|
| 491 | + //the hidden string |
|
| 492 | + $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 493 | 493 | |
| 494 | - return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 495 | - } |
|
| 494 | + return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 495 | + } |
|
| 496 | 496 | |
| 497 | - /** |
|
| 498 | - * This function is used to set the initial session config regarding the configuration |
|
| 499 | - * @codeCoverageIgnore |
|
| 500 | - */ |
|
| 501 | - function set_session_config(){ |
|
| 502 | - //$_SESSION is not available on cli mode |
|
| 503 | - if (! IS_CLI){ |
|
| 504 | - $logger =& class_loader('Log', 'classes'); |
|
| 505 | - $logger->setLogger('PHPSession'); |
|
| 506 | - //set session params |
|
| 507 | - $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
|
| 508 | - $sessionName = get_config('session_name'); |
|
| 509 | - if ($sessionName){ |
|
| 510 | - session_name($sessionName); |
|
| 511 | - } |
|
| 512 | - $logger->info('Session handler: ' . $sessionHandler); |
|
| 513 | - $logger->info('Session name: ' . $sessionName); |
|
| 497 | + /** |
|
| 498 | + * This function is used to set the initial session config regarding the configuration |
|
| 499 | + * @codeCoverageIgnore |
|
| 500 | + */ |
|
| 501 | + function set_session_config(){ |
|
| 502 | + //$_SESSION is not available on cli mode |
|
| 503 | + if (! IS_CLI){ |
|
| 504 | + $logger =& class_loader('Log', 'classes'); |
|
| 505 | + $logger->setLogger('PHPSession'); |
|
| 506 | + //set session params |
|
| 507 | + $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
|
| 508 | + $sessionName = get_config('session_name'); |
|
| 509 | + if ($sessionName){ |
|
| 510 | + session_name($sessionName); |
|
| 511 | + } |
|
| 512 | + $logger->info('Session handler: ' . $sessionHandler); |
|
| 513 | + $logger->info('Session name: ' . $sessionName); |
|
| 514 | 514 | |
| 515 | - if ($sessionHandler == 'files'){ |
|
| 516 | - $sessionSavePath = get_config('session_save_path'); |
|
| 517 | - if ($sessionSavePath){ |
|
| 518 | - if (! is_dir($sessionSavePath)){ |
|
| 519 | - mkdir($sessionSavePath, 1773); |
|
| 520 | - } |
|
| 521 | - session_save_path($sessionSavePath); |
|
| 522 | - $logger->info('Session save path: ' . $sessionSavePath); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - else if ($sessionHandler == 'database'){ |
|
| 526 | - //load database session handle library |
|
| 527 | - //Database Session handler Model |
|
| 528 | - require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
|
| 515 | + if ($sessionHandler == 'files'){ |
|
| 516 | + $sessionSavePath = get_config('session_save_path'); |
|
| 517 | + if ($sessionSavePath){ |
|
| 518 | + if (! is_dir($sessionSavePath)){ |
|
| 519 | + mkdir($sessionSavePath, 1773); |
|
| 520 | + } |
|
| 521 | + session_save_path($sessionSavePath); |
|
| 522 | + $logger->info('Session save path: ' . $sessionSavePath); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + else if ($sessionHandler == 'database'){ |
|
| 526 | + //load database session handle library |
|
| 527 | + //Database Session handler Model |
|
| 528 | + require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
|
| 529 | 529 | |
| 530 | - $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 531 | - session_set_save_handler($DBS, true); |
|
| 532 | - $logger->info('session save path: ' . get_config('session_save_path')); |
|
| 533 | - } |
|
| 534 | - else{ |
|
| 535 | - show_error('Invalid session handler configuration'); |
|
| 536 | - } |
|
| 537 | - $lifetime = get_config('session_cookie_lifetime', 0); |
|
| 538 | - $path = get_config('session_cookie_path', '/'); |
|
| 539 | - $domain = get_config('session_cookie_domain', ''); |
|
| 540 | - $secure = get_config('session_cookie_secure', false); |
|
| 541 | - session_set_cookie_params( |
|
| 542 | - $lifetime, |
|
| 543 | - $path, |
|
| 544 | - $domain, |
|
| 545 | - $secure, |
|
| 546 | - $httponly = true /*for security for access to cookie via javascript or XSS attack*/ |
|
| 547 | - ); |
|
| 548 | - //to prevent attack of Session Fixation |
|
| 549 | - //thank to https://www.phparch.com/2018/01/php-sessions-in-depth/ |
|
| 550 | - ini_set('session.use_strict_mode ', 1); |
|
| 551 | - ini_set('session.use_only_cookies', 1); |
|
| 552 | - ini_set('session.use_trans_sid ', 0); |
|
| 530 | + $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 531 | + session_set_save_handler($DBS, true); |
|
| 532 | + $logger->info('session save path: ' . get_config('session_save_path')); |
|
| 533 | + } |
|
| 534 | + else{ |
|
| 535 | + show_error('Invalid session handler configuration'); |
|
| 536 | + } |
|
| 537 | + $lifetime = get_config('session_cookie_lifetime', 0); |
|
| 538 | + $path = get_config('session_cookie_path', '/'); |
|
| 539 | + $domain = get_config('session_cookie_domain', ''); |
|
| 540 | + $secure = get_config('session_cookie_secure', false); |
|
| 541 | + session_set_cookie_params( |
|
| 542 | + $lifetime, |
|
| 543 | + $path, |
|
| 544 | + $domain, |
|
| 545 | + $secure, |
|
| 546 | + $httponly = true /*for security for access to cookie via javascript or XSS attack*/ |
|
| 547 | + ); |
|
| 548 | + //to prevent attack of Session Fixation |
|
| 549 | + //thank to https://www.phparch.com/2018/01/php-sessions-in-depth/ |
|
| 550 | + ini_set('session.use_strict_mode ', 1); |
|
| 551 | + ini_set('session.use_only_cookies', 1); |
|
| 552 | + ini_set('session.use_trans_sid ', 0); |
|
| 553 | 553 | |
| 554 | - $logger->info('Session lifetime: ' . $lifetime); |
|
| 555 | - $logger->info('Session cookie path: ' . $path); |
|
| 556 | - $logger->info('Session domain: ' . $domain); |
|
| 557 | - $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 554 | + $logger->info('Session lifetime: ' . $lifetime); |
|
| 555 | + $logger->info('Session cookie path: ' . $path); |
|
| 556 | + $logger->info('Session domain: ' . $domain); |
|
| 557 | + $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 558 | 558 | |
| 559 | - if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 560 | - $logger->info('Session not yet start, start it now'); |
|
| 561 | - session_start(); |
|
| 562 | - } |
|
| 563 | - } |
|
| 564 | - } |
|
| 559 | + if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 560 | + $logger->info('Session not yet start, start it now'); |
|
| 561 | + session_start(); |
|
| 562 | + } |
|
| 563 | + } |
|
| 564 | + } |
|
| 565 | 565 | |
| 566 | - /** |
|
| 567 | - * This function is very useful, it allows to recover the instance of the global controller. |
|
| 568 | - * Note this function always returns the address of the super instance. |
|
| 569 | - * For example : |
|
| 570 | - * $obj = & get_instance(); |
|
| 571 | - * |
|
| 572 | - * @codeCoverageIgnore |
|
| 573 | - * |
|
| 574 | - * @return object the instance of the "Controller" class |
|
| 575 | - */ |
|
| 576 | - function & get_instance(){ |
|
| 577 | - return Controller::get_instance(); |
|
| 578 | - } |
|
| 566 | + /** |
|
| 567 | + * This function is very useful, it allows to recover the instance of the global controller. |
|
| 568 | + * Note this function always returns the address of the super instance. |
|
| 569 | + * For example : |
|
| 570 | + * $obj = & get_instance(); |
|
| 571 | + * |
|
| 572 | + * @codeCoverageIgnore |
|
| 573 | + * |
|
| 574 | + * @return object the instance of the "Controller" class |
|
| 575 | + */ |
|
| 576 | + function & get_instance(){ |
|
| 577 | + return Controller::get_instance(); |
|
| 578 | + } |
|
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
| 6 | 6 | * A simple PHP framework using HMVC architecture |
@@ -22,283 +22,283 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Module{ |
|
| 27 | + class Module{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * list of loaded module |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $list = array(); |
|
| 29 | + /** |
|
| 30 | + * list of loaded module |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $list = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * logger instance |
|
| 37 | - * @var object |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * logger instance |
|
| 37 | + * @var object |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Initialise the module list by scanning the directory MODULE_PATH |
|
| 43 | - */ |
|
| 44 | - public function init(){ |
|
| 45 | - $logger = self::getLogger(); |
|
| 46 | - $logger->debug('Check if the application contains the modules ...'); |
|
| 47 | - $moduleDir = opendir(MODULE_PATH); |
|
| 48 | - if (is_resource($moduleDir)){ |
|
| 49 | - while(($module = readdir($moduleDir)) !== false){ |
|
| 50 | - if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
|
| 51 | - self::$list[] = $module; |
|
| 52 | - } |
|
| 53 | - else{ |
|
| 54 | - $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - closedir($moduleDir); |
|
| 58 | - } |
|
| 59 | - ksort(self::$list); |
|
| 41 | + /** |
|
| 42 | + * Initialise the module list by scanning the directory MODULE_PATH |
|
| 43 | + */ |
|
| 44 | + public function init(){ |
|
| 45 | + $logger = self::getLogger(); |
|
| 46 | + $logger->debug('Check if the application contains the modules ...'); |
|
| 47 | + $moduleDir = opendir(MODULE_PATH); |
|
| 48 | + if (is_resource($moduleDir)){ |
|
| 49 | + while(($module = readdir($moduleDir)) !== false){ |
|
| 50 | + if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
|
| 51 | + self::$list[] = $module; |
|
| 52 | + } |
|
| 53 | + else{ |
|
| 54 | + $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + closedir($moduleDir); |
|
| 58 | + } |
|
| 59 | + ksort(self::$list); |
|
| 60 | 60 | |
| 61 | - if (! empty(self::$list)){ |
|
| 62 | - $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
|
| 63 | - } |
|
| 64 | - } |
|
| 61 | + if (! empty(self::$list)){ |
|
| 62 | + $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The signleton of the logger |
|
| 68 | - * @return Object the Log instance |
|
| 69 | - */ |
|
| 70 | - public static function getLogger(){ |
|
| 71 | - if (self::$logger == null){ |
|
| 72 | - $logger = array(); |
|
| 73 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 74 | - $logger[0]->setLogger('Library::Module'); |
|
| 75 | - self::$logger = $logger[0]; |
|
| 76 | - } |
|
| 77 | - return self::$logger; |
|
| 78 | - } |
|
| 66 | + /** |
|
| 67 | + * The signleton of the logger |
|
| 68 | + * @return Object the Log instance |
|
| 69 | + */ |
|
| 70 | + public static function getLogger(){ |
|
| 71 | + if (self::$logger == null){ |
|
| 72 | + $logger = array(); |
|
| 73 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 74 | + $logger[0]->setLogger('Library::Module'); |
|
| 75 | + self::$logger = $logger[0]; |
|
| 76 | + } |
|
| 77 | + return self::$logger; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Set the log instance for future use |
|
| 82 | - * @param object $logger the log object |
|
| 83 | - * @return object the log instance |
|
| 84 | - */ |
|
| 85 | - public static function setLogger($logger){ |
|
| 86 | - self::$logger = $logger; |
|
| 87 | - return self::$logger; |
|
| 88 | - } |
|
| 80 | + /** |
|
| 81 | + * Set the log instance for future use |
|
| 82 | + * @param object $logger the log object |
|
| 83 | + * @return object the log instance |
|
| 84 | + */ |
|
| 85 | + public static function setLogger($logger){ |
|
| 86 | + self::$logger = $logger; |
|
| 87 | + return self::$logger; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | |
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Add new module in the list |
|
| 94 | - * @param string $name the name of the module |
|
| 95 | - * |
|
| 96 | - * @return object the current instance |
|
| 97 | - */ |
|
| 98 | - public function add($name){ |
|
| 99 | - self::$list[] = $name; |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 92 | + /** |
|
| 93 | + * Add new module in the list |
|
| 94 | + * @param string $name the name of the module |
|
| 95 | + * |
|
| 96 | + * @return object the current instance |
|
| 97 | + */ |
|
| 98 | + public function add($name){ |
|
| 99 | + self::$list[] = $name; |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get the list of the custom autoload configuration from module if exists |
|
| 105 | - * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
|
| 106 | - */ |
|
| 107 | - public static function getModulesAutoloadConfig(){ |
|
| 108 | - $logger = self::getLogger(); |
|
| 109 | - if (empty(self::$list)){ |
|
| 110 | - $logger->info('No module was loaded skipping.'); |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - $autoloads = array(); |
|
| 114 | - $autoloads['libraries'] = array(); |
|
| 115 | - $autoloads['config'] = array(); |
|
| 116 | - $autoloads['models'] = array(); |
|
| 117 | - $autoloads['functions'] = array(); |
|
| 118 | - $autoloads['languages'] = array(); |
|
| 103 | + /** |
|
| 104 | + * Get the list of the custom autoload configuration from module if exists |
|
| 105 | + * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
|
| 106 | + */ |
|
| 107 | + public static function getModulesAutoloadConfig(){ |
|
| 108 | + $logger = self::getLogger(); |
|
| 109 | + if (empty(self::$list)){ |
|
| 110 | + $logger->info('No module was loaded skipping.'); |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + $autoloads = array(); |
|
| 114 | + $autoloads['libraries'] = array(); |
|
| 115 | + $autoloads['config'] = array(); |
|
| 116 | + $autoloads['models'] = array(); |
|
| 117 | + $autoloads['functions'] = array(); |
|
| 118 | + $autoloads['languages'] = array(); |
|
| 119 | 119 | |
| 120 | - foreach (self::$list as $module) { |
|
| 121 | - $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
|
| 122 | - if (file_exists($file)){ |
|
| 123 | - $autoload = array(); |
|
| 124 | - require_once $file; |
|
| 125 | - if (! empty($autoload) && is_array($autoload)){ |
|
| 126 | - $autoloads = array_merge_recursive($autoloads, $autoload); |
|
| 127 | - unset($autoload); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - return $autoloads; |
|
| 132 | - } |
|
| 120 | + foreach (self::$list as $module) { |
|
| 121 | + $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
|
| 122 | + if (file_exists($file)){ |
|
| 123 | + $autoload = array(); |
|
| 124 | + require_once $file; |
|
| 125 | + if (! empty($autoload) && is_array($autoload)){ |
|
| 126 | + $autoloads = array_merge_recursive($autoloads, $autoload); |
|
| 127 | + unset($autoload); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + return $autoloads; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the list of the custom routes configuration from module if exists |
|
| 136 | - * @return array|boolean the routes list or false if no module contains the routes configuration |
|
| 137 | - */ |
|
| 138 | - public static function getModulesRoutesConfig(){ |
|
| 139 | - $logger = self::getLogger(); |
|
| 140 | - if (empty(self::$list)){ |
|
| 141 | - $logger->info('No module was loaded skipping.'); |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - $routes = array(); |
|
| 145 | - foreach (self::$list as $module) { |
|
| 146 | - $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
|
| 147 | - if (file_exists($file)){ |
|
| 148 | - $route = array(); |
|
| 149 | - require_once $file; |
|
| 150 | - if (! empty($route) && is_array($route)){ |
|
| 151 | - $routes = array_merge($routes, $route); |
|
| 152 | - unset($route); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return $routes; |
|
| 157 | - } |
|
| 134 | + /** |
|
| 135 | + * Get the list of the custom routes configuration from module if exists |
|
| 136 | + * @return array|boolean the routes list or false if no module contains the routes configuration |
|
| 137 | + */ |
|
| 138 | + public static function getModulesRoutesConfig(){ |
|
| 139 | + $logger = self::getLogger(); |
|
| 140 | + if (empty(self::$list)){ |
|
| 141 | + $logger->info('No module was loaded skipping.'); |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + $routes = array(); |
|
| 145 | + foreach (self::$list as $module) { |
|
| 146 | + $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
|
| 147 | + if (file_exists($file)){ |
|
| 148 | + $route = array(); |
|
| 149 | + require_once $file; |
|
| 150 | + if (! empty($route) && is_array($route)){ |
|
| 151 | + $routes = array_merge($routes, $route); |
|
| 152 | + unset($route); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return $routes; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Check if in module list can have this controller |
|
| 162 | - * @see Module::findClassInModuleFullFilePath |
|
| 163 | - * @return boolean|string false or null if no module have this controller, path the full path of the controller |
|
| 164 | - */ |
|
| 165 | - public static function findControllerFullPath($class, $module = null){ |
|
| 166 | - return self::findClassInModuleFullFilePath($class, $module, 'controllers'); |
|
| 167 | - } |
|
| 160 | + /** |
|
| 161 | + * Check if in module list can have this controller |
|
| 162 | + * @see Module::findClassInModuleFullFilePath |
|
| 163 | + * @return boolean|string false or null if no module have this controller, path the full path of the controller |
|
| 164 | + */ |
|
| 165 | + public static function findControllerFullPath($class, $module = null){ |
|
| 166 | + return self::findClassInModuleFullFilePath($class, $module, 'controllers'); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Check if in module list can have this model |
|
| 171 | - * @see Module::findClassInModuleFullFilePath |
|
| 172 | - * @return boolean|string false or null if no module have this model, return the full path of this model |
|
| 173 | - */ |
|
| 174 | - public static function findModelFullPath($class, $module = null){ |
|
| 175 | - return self::findClassInModuleFullFilePath($class, $module, 'models'); |
|
| 176 | - } |
|
| 169 | + /** |
|
| 170 | + * Check if in module list can have this model |
|
| 171 | + * @see Module::findClassInModuleFullFilePath |
|
| 172 | + * @return boolean|string false or null if no module have this model, return the full path of this model |
|
| 173 | + */ |
|
| 174 | + public static function findModelFullPath($class, $module = null){ |
|
| 175 | + return self::findClassInModuleFullFilePath($class, $module, 'models'); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Check if in module list can have this library |
|
| 180 | - * @see Module::findClassInModuleFullFilePath |
|
| 181 | - * @return boolean|string false or null if no module have this library, return the full path of this library |
|
| 182 | - */ |
|
| 183 | - public static function findLibraryFullPath($class, $module = null){ |
|
| 184 | - return self::findClassInModuleFullFilePath($class, $module, 'libraries'); |
|
| 185 | - } |
|
| 178 | + /** |
|
| 179 | + * Check if in module list can have this library |
|
| 180 | + * @see Module::findClassInModuleFullFilePath |
|
| 181 | + * @return boolean|string false or null if no module have this library, return the full path of this library |
|
| 182 | + */ |
|
| 183 | + public static function findLibraryFullPath($class, $module = null){ |
|
| 184 | + return self::findClassInModuleFullFilePath($class, $module, 'libraries'); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Check if in module list can have this config |
|
| 190 | - * @see Module::findNonClassInModuleFullFilePath |
|
| 191 | - * @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
|
| 192 | - */ |
|
| 193 | - public static function findConfigFullPath($configuration, $module = null){ |
|
| 194 | - return self::findNonClassInModuleFullFilePath($configuration, $module, 'config'); |
|
| 195 | - } |
|
| 188 | + /** |
|
| 189 | + * Check if in module list can have this config |
|
| 190 | + * @see Module::findNonClassInModuleFullFilePath |
|
| 191 | + * @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
|
| 192 | + */ |
|
| 193 | + public static function findConfigFullPath($configuration, $module = null){ |
|
| 194 | + return self::findNonClassInModuleFullFilePath($configuration, $module, 'config'); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Check if in module list can have this helper |
|
| 199 | - * @see Module::findNonClassInModuleFullFilePath |
|
| 200 | - * @return boolean|string false or null if no module have this helper, return the full path of this helper |
|
| 201 | - */ |
|
| 202 | - public static function findFunctionFullPath($helper, $module = null){ |
|
| 203 | - return self::findNonClassInModuleFullFilePath($helper, $module, 'functions'); |
|
| 204 | - } |
|
| 197 | + /** |
|
| 198 | + * Check if in module list can have this helper |
|
| 199 | + * @see Module::findNonClassInModuleFullFilePath |
|
| 200 | + * @return boolean|string false or null if no module have this helper, return the full path of this helper |
|
| 201 | + */ |
|
| 202 | + public static function findFunctionFullPath($helper, $module = null){ |
|
| 203 | + return self::findNonClassInModuleFullFilePath($helper, $module, 'functions'); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Check if in module list can have this view |
|
| 208 | - * @see Module::findNonClassInModuleFullFilePath |
|
| 209 | - * @return boolean|string false or null if no module have this view, path the full path of the view |
|
| 210 | - */ |
|
| 211 | - public static function findViewFullPath($view, $module = null){ |
|
| 212 | - return self::findNonClassInModuleFullFilePath($view, $module, 'views'); |
|
| 213 | - } |
|
| 206 | + /** |
|
| 207 | + * Check if in module list can have this view |
|
| 208 | + * @see Module::findNonClassInModuleFullFilePath |
|
| 209 | + * @return boolean|string false or null if no module have this view, path the full path of the view |
|
| 210 | + */ |
|
| 211 | + public static function findViewFullPath($view, $module = null){ |
|
| 212 | + return self::findNonClassInModuleFullFilePath($view, $module, 'views'); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Check if in module list can have this language |
|
| 217 | - * @see Module::findNonClassInModuleFullFilePath |
|
| 218 | - * @return boolean|string false or null if no module have this language, return the full path of this language |
|
| 219 | - */ |
|
| 220 | - public static function findLanguageFullPath($language, $appLang, $module = null){ |
|
| 221 | - return self::findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang); |
|
| 222 | - } |
|
| 215 | + /** |
|
| 216 | + * Check if in module list can have this language |
|
| 217 | + * @see Module::findNonClassInModuleFullFilePath |
|
| 218 | + * @return boolean|string false or null if no module have this language, return the full path of this language |
|
| 219 | + */ |
|
| 220 | + public static function findLanguageFullPath($language, $appLang, $module = null){ |
|
| 221 | + return self::findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang); |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - /** |
|
| 225 | - * Get the list of module loaded |
|
| 226 | - * @return array the module list |
|
| 227 | - */ |
|
| 228 | - public static function getModuleList(){ |
|
| 229 | - return self::$list; |
|
| 230 | - } |
|
| 224 | + /** |
|
| 225 | + * Get the list of module loaded |
|
| 226 | + * @return array the module list |
|
| 227 | + */ |
|
| 228 | + public static function getModuleList(){ |
|
| 229 | + return self::$list; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Check if the application has an module |
|
| 234 | - * @return boolean |
|
| 235 | - */ |
|
| 236 | - public static function hasModule(){ |
|
| 237 | - return !empty(self::$list); |
|
| 238 | - } |
|
| 232 | + /** |
|
| 233 | + * Check if the application has an module |
|
| 234 | + * @return boolean |
|
| 235 | + */ |
|
| 236 | + public static function hasModule(){ |
|
| 237 | + return !empty(self::$list); |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Check if in module list can have the model, controller, library |
|
| 242 | - * @param string $class the class name of library, model, controller |
|
| 243 | - * @param string $module the module name |
|
| 244 | - * @param string $type the name of the type "controllers", "libraries", "models" |
|
| 245 | - * @return boolean|string false or null if no module |
|
| 246 | - * have this class, return the full path of the class |
|
| 247 | - */ |
|
| 248 | - protected static function findClassInModuleFullFilePath($class, $module, $type){ |
|
| 249 | - $logger = self::getLogger(); |
|
| 250 | - $class = str_ireplace('.php', '', $class); |
|
| 251 | - $class = ucfirst($class); |
|
| 252 | - $classFile = $class.'.php'; |
|
| 253 | - $logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...'); |
|
| 254 | - $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile; |
|
| 255 | - if (file_exists($filePath)){ |
|
| 256 | - $logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' .$filePath. ']'); |
|
| 257 | - return $filePath; |
|
| 258 | - } |
|
| 259 | - $logger->info('Class [' . $class . '] does not exist in the module [' .$module. '] for [' . $type . ']'); |
|
| 260 | - return false; |
|
| 261 | - } |
|
| 240 | + /** |
|
| 241 | + * Check if in module list can have the model, controller, library |
|
| 242 | + * @param string $class the class name of library, model, controller |
|
| 243 | + * @param string $module the module name |
|
| 244 | + * @param string $type the name of the type "controllers", "libraries", "models" |
|
| 245 | + * @return boolean|string false or null if no module |
|
| 246 | + * have this class, return the full path of the class |
|
| 247 | + */ |
|
| 248 | + protected static function findClassInModuleFullFilePath($class, $module, $type){ |
|
| 249 | + $logger = self::getLogger(); |
|
| 250 | + $class = str_ireplace('.php', '', $class); |
|
| 251 | + $class = ucfirst($class); |
|
| 252 | + $classFile = $class.'.php'; |
|
| 253 | + $logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...'); |
|
| 254 | + $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile; |
|
| 255 | + if (file_exists($filePath)){ |
|
| 256 | + $logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' .$filePath. ']'); |
|
| 257 | + return $filePath; |
|
| 258 | + } |
|
| 259 | + $logger->info('Class [' . $class . '] does not exist in the module [' .$module. '] for [' . $type . ']'); |
|
| 260 | + return false; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Check if in module list can have the config, view, helper, language |
|
| 265 | - * @param string $name the name of config, view, helper, language |
|
| 266 | - * @param string $module the module name |
|
| 267 | - * @param string $type the name of the type "config", "functions", "views", "lang" |
|
| 268 | - * @param string|null $appLang the application language. This is use only when $type = "lang" |
|
| 269 | - * @return boolean|string false or null if no module |
|
| 270 | - * have this resource, return the full path of the resource |
|
| 271 | - */ |
|
| 272 | - protected static function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null){ |
|
| 273 | - $logger = self::getLogger(); |
|
| 274 | - $name = str_ireplace('.php', '', $name); |
|
| 275 | - $file = $name.'.php'; |
|
| 276 | - $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 277 | - switch($type){ |
|
| 278 | - case 'functions': |
|
| 279 | - $name = str_ireplace('function_', '', $name); |
|
| 280 | - $file = 'function_'.$name.'.php'; |
|
| 281 | - $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 282 | - break; |
|
| 283 | - case 'views': |
|
| 284 | - $name = trim($name, '/\\'); |
|
| 285 | - $name = str_ireplace('/', DS, $name); |
|
| 286 | - $file = $name . '.php'; |
|
| 287 | - $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 288 | - break; |
|
| 289 | - case 'lang': |
|
| 290 | - $name = str_ireplace('lang_', '', $name); |
|
| 291 | - $file = 'lang_'.$name.'.php'; |
|
| 292 | - $filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file; |
|
| 293 | - break; |
|
| 294 | - } |
|
| 295 | - $logger->debug('Checking resource [' . $name . '] in module [' .$module. '] for [' . $type . '] ...'); |
|
| 296 | - if (file_exists($filePath)){ |
|
| 297 | - $logger->info('Found resource [' . $name . '] in module [' .$module. '] for [' . $type . '] the file path is [' .$filePath. ']'); |
|
| 298 | - return $filePath; |
|
| 299 | - } |
|
| 300 | - $logger->info('Resource [' . $name . '] does not exist in the module [' .$module. '] for [' . $type . ']'); |
|
| 301 | - return false; |
|
| 302 | - } |
|
| 263 | + /** |
|
| 264 | + * Check if in module list can have the config, view, helper, language |
|
| 265 | + * @param string $name the name of config, view, helper, language |
|
| 266 | + * @param string $module the module name |
|
| 267 | + * @param string $type the name of the type "config", "functions", "views", "lang" |
|
| 268 | + * @param string|null $appLang the application language. This is use only when $type = "lang" |
|
| 269 | + * @return boolean|string false or null if no module |
|
| 270 | + * have this resource, return the full path of the resource |
|
| 271 | + */ |
|
| 272 | + protected static function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null){ |
|
| 273 | + $logger = self::getLogger(); |
|
| 274 | + $name = str_ireplace('.php', '', $name); |
|
| 275 | + $file = $name.'.php'; |
|
| 276 | + $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 277 | + switch($type){ |
|
| 278 | + case 'functions': |
|
| 279 | + $name = str_ireplace('function_', '', $name); |
|
| 280 | + $file = 'function_'.$name.'.php'; |
|
| 281 | + $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 282 | + break; |
|
| 283 | + case 'views': |
|
| 284 | + $name = trim($name, '/\\'); |
|
| 285 | + $name = str_ireplace('/', DS, $name); |
|
| 286 | + $file = $name . '.php'; |
|
| 287 | + $filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
| 288 | + break; |
|
| 289 | + case 'lang': |
|
| 290 | + $name = str_ireplace('lang_', '', $name); |
|
| 291 | + $file = 'lang_'.$name.'.php'; |
|
| 292 | + $filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file; |
|
| 293 | + break; |
|
| 294 | + } |
|
| 295 | + $logger->debug('Checking resource [' . $name . '] in module [' .$module. '] for [' . $type . '] ...'); |
|
| 296 | + if (file_exists($filePath)){ |
|
| 297 | + $logger->info('Found resource [' . $name . '] in module [' .$module. '] for [' . $type . '] the file path is [' .$filePath. ']'); |
|
| 298 | + return $filePath; |
|
| 299 | + } |
|
| 300 | + $logger->info('Resource [' . $name . '] does not exist in the module [' .$module. '] for [' . $type . ']'); |
|
| 301 | + return false; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - } |
|
| 304 | + } |
|
@@ -1,542 +1,542 @@ |
||
| 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 object |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var object |
|
| 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 | 87 | |
| 88 | - /** |
|
| 89 | - * The signleton of the logger |
|
| 90 | - * @return Object the Log instance |
|
| 91 | - */ |
|
| 92 | - public static function getLogger(){ |
|
| 93 | - if(self::$logger == null){ |
|
| 94 | - $logger = array(); |
|
| 95 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 96 | - $logger[0]->setLogger('Library::Response'); |
|
| 97 | - self::$logger = $logger[0]; |
|
| 98 | - } |
|
| 99 | - return self::$logger; |
|
| 100 | - } |
|
| 88 | + /** |
|
| 89 | + * The signleton of the logger |
|
| 90 | + * @return Object the Log instance |
|
| 91 | + */ |
|
| 92 | + public static function getLogger(){ |
|
| 93 | + if(self::$logger == null){ |
|
| 94 | + $logger = array(); |
|
| 95 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 96 | + $logger[0]->setLogger('Library::Response'); |
|
| 97 | + self::$logger = $logger[0]; |
|
| 98 | + } |
|
| 99 | + return self::$logger; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Set the log instance for future use |
|
| 104 | - * @param object $logger the log object |
|
| 105 | - * @return object the log instance |
|
| 106 | - */ |
|
| 107 | - public static function setLogger($logger){ |
|
| 108 | - self::$logger = $logger; |
|
| 109 | - return self::$logger; |
|
| 110 | - } |
|
| 102 | + /** |
|
| 103 | + * Set the log instance for future use |
|
| 104 | + * @param object $logger the log object |
|
| 105 | + * @return object the log instance |
|
| 106 | + */ |
|
| 107 | + public static function setLogger($logger){ |
|
| 108 | + self::$logger = $logger; |
|
| 109 | + return self::$logger; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Send the HTTP Response headers |
|
| 115 | - * @param integer $httpCode the HTTP status code |
|
| 116 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 117 | - */ |
|
| 118 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 119 | - set_http_status_header($httpCode); |
|
| 120 | - self::setHeaders($headers); |
|
| 121 | - if(! headers_sent()){ |
|
| 122 | - foreach(self::getHeaders() as $key => $value){ |
|
| 123 | - header($key .': '.$value); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - } |
|
| 113 | + /** |
|
| 114 | + * Send the HTTP Response headers |
|
| 115 | + * @param integer $httpCode the HTTP status code |
|
| 116 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 117 | + */ |
|
| 118 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 119 | + set_http_status_header($httpCode); |
|
| 120 | + self::setHeaders($headers); |
|
| 121 | + if(! headers_sent()){ |
|
| 122 | + foreach(self::getHeaders() as $key => $value){ |
|
| 123 | + header($key .': '.$value); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get the list of the headers |
|
| 130 | - * @return array the headers list |
|
| 131 | - */ |
|
| 132 | - public static function getHeaders(){ |
|
| 133 | - return self::$headers; |
|
| 134 | - } |
|
| 128 | + /** |
|
| 129 | + * Get the list of the headers |
|
| 130 | + * @return array the headers list |
|
| 131 | + */ |
|
| 132 | + public static function getHeaders(){ |
|
| 133 | + return self::$headers; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Get the header value for the given name |
|
| 138 | - * @param string $name the header name |
|
| 139 | - * @return string|null the header value |
|
| 140 | - */ |
|
| 141 | - public static function getHeader($name){ |
|
| 142 | - if(array_key_exists($name, self::$headers)){ |
|
| 143 | - return self::$headers[$name]; |
|
| 144 | - } |
|
| 145 | - return null; |
|
| 146 | - } |
|
| 136 | + /** |
|
| 137 | + * Get the header value for the given name |
|
| 138 | + * @param string $name the header name |
|
| 139 | + * @return string|null the header value |
|
| 140 | + */ |
|
| 141 | + public static function getHeader($name){ |
|
| 142 | + if(array_key_exists($name, self::$headers)){ |
|
| 143 | + return self::$headers[$name]; |
|
| 144 | + } |
|
| 145 | + return null; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Set the header value for the specified name |
|
| 151 | - * @param string $name the header name |
|
| 152 | - * @param string $value the header value to be set |
|
| 153 | - */ |
|
| 154 | - public static function setHeader($name, $value){ |
|
| 155 | - self::$headers[$name] = $value; |
|
| 156 | - } |
|
| 149 | + /** |
|
| 150 | + * Set the header value for the specified name |
|
| 151 | + * @param string $name the header name |
|
| 152 | + * @param string $value the header value to be set |
|
| 153 | + */ |
|
| 154 | + public static function setHeader($name, $value){ |
|
| 155 | + self::$headers[$name] = $value; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Set the headers using array |
|
| 160 | - * @param array $headers the list of the headers to set. |
|
| 161 | - * Note: this will merge with the existing headers |
|
| 162 | - */ |
|
| 163 | - public static function setHeaders(array $headers){ |
|
| 164 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 165 | - } |
|
| 158 | + /** |
|
| 159 | + * Set the headers using array |
|
| 160 | + * @param array $headers the list of the headers to set. |
|
| 161 | + * Note: this will merge with the existing headers |
|
| 162 | + */ |
|
| 163 | + public static function setHeaders(array $headers){ |
|
| 164 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Redirect user to the specified page |
|
| 169 | - * @param string $path the URL or URI to be redirect to |
|
| 170 | - */ |
|
| 171 | - public static function redirect($path = ''){ |
|
| 172 | - $logger = self::getLogger(); |
|
| 173 | - $url = Url::site_url($path); |
|
| 174 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 175 | - if(! headers_sent()){ |
|
| 176 | - header('Location: '.$url); |
|
| 177 | - exit; |
|
| 178 | - } |
|
| 179 | - echo '<script> |
|
| 167 | + /** |
|
| 168 | + * Redirect user to the specified page |
|
| 169 | + * @param string $path the URL or URI to be redirect to |
|
| 170 | + */ |
|
| 171 | + public static function redirect($path = ''){ |
|
| 172 | + $logger = self::getLogger(); |
|
| 173 | + $url = Url::site_url($path); |
|
| 174 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 175 | + if(! headers_sent()){ |
|
| 176 | + header('Location: '.$url); |
|
| 177 | + exit; |
|
| 178 | + } |
|
| 179 | + echo '<script> |
|
| 180 | 180 | location.href = "'.$url.'"; |
| 181 | 181 | </script>'; |
| 182 | - } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Render the view to display later or return the content |
|
| 186 | - * @param string $view the view name or path |
|
| 187 | - * @param array|object $data the variable data to use in the view |
|
| 188 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 189 | - * @return void|string if $return is true will return the view content otherwise |
|
| 190 | - * will display the view content. |
|
| 191 | - */ |
|
| 192 | - public function render($view, $data = null, $return = false){ |
|
| 193 | - $logger = self::getLogger(); |
|
| 194 | - //convert data to an array |
|
| 195 | - $data = (array) $data; |
|
| 196 | - $view = str_ireplace('.php', '', $view); |
|
| 197 | - $view = trim($view, '/\\'); |
|
| 198 | - $viewFile = $view . '.php'; |
|
| 199 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 184 | + /** |
|
| 185 | + * Render the view to display later or return the content |
|
| 186 | + * @param string $view the view name or path |
|
| 187 | + * @param array|object $data the variable data to use in the view |
|
| 188 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 189 | + * @return void|string if $return is true will return the view content otherwise |
|
| 190 | + * will display the view content. |
|
| 191 | + */ |
|
| 192 | + public function render($view, $data = null, $return = false){ |
|
| 193 | + $logger = self::getLogger(); |
|
| 194 | + //convert data to an array |
|
| 195 | + $data = (array) $data; |
|
| 196 | + $view = str_ireplace('.php', '', $view); |
|
| 197 | + $view = trim($view, '/\\'); |
|
| 198 | + $viewFile = $view . '.php'; |
|
| 199 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 200 | 200 | |
| 201 | - //check in module first |
|
| 202 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 203 | - $moduleInfo = $this->getModuleInfoForView($view); |
|
| 204 | - $module = $moduleInfo['module']; |
|
| 205 | - $view = $moduleInfo['view']; |
|
| 201 | + //check in module first |
|
| 202 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 203 | + $moduleInfo = $this->getModuleInfoForView($view); |
|
| 204 | + $module = $moduleInfo['module']; |
|
| 205 | + $view = $moduleInfo['view']; |
|
| 206 | 206 | |
| 207 | - $moduleViewPath = Module::findViewFullPath($view, $module); |
|
| 208 | - if($moduleViewPath){ |
|
| 209 | - $path = $moduleViewPath; |
|
| 210 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | - } |
|
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
| 214 | - } |
|
| 207 | + $moduleViewPath = Module::findViewFullPath($view, $module); |
|
| 208 | + if($moduleViewPath){ |
|
| 209 | + $path = $moduleViewPath; |
|
| 210 | + $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | + } |
|
| 212 | + else{ |
|
| 213 | + $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 216 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 217 | 217 | |
| 218 | - ///////// |
|
| 219 | - if($return){ |
|
| 220 | - return $this->loadView($path, $data, true); |
|
| 221 | - } |
|
| 222 | - $this->loadView($path, $data, false); |
|
| 223 | - } |
|
| 218 | + ///////// |
|
| 219 | + if($return){ |
|
| 220 | + return $this->loadView($path, $data, true); |
|
| 221 | + } |
|
| 222 | + $this->loadView($path, $data, false); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Send the final page output to user |
|
| 228 | - */ |
|
| 229 | - public function renderFinalPage(){ |
|
| 230 | - $logger = self::getLogger(); |
|
| 231 | - $obj = & get_instance(); |
|
| 232 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 233 | - $dispatcher = $obj->eventdispatcher; |
|
| 234 | - $content = $this->_pageRender; |
|
| 235 | - if(! $content){ |
|
| 236 | - $logger->warning('The final view content is empty.'); |
|
| 237 | - return; |
|
| 238 | - } |
|
| 239 | - //dispatch |
|
| 240 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 241 | - $content = null; |
|
| 242 | - if(! empty($event->payload)){ |
|
| 243 | - $content = $event->payload; |
|
| 244 | - } |
|
| 245 | - if(empty($content)){ |
|
| 246 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 247 | - } |
|
| 248 | - //remove unsed space in the content |
|
| 249 | - $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 250 | - //check whether need save the page into cache. |
|
| 251 | - if($cachePageStatus){ |
|
| 252 | - $this->savePageContentIntoCache($content); |
|
| 253 | - } |
|
| 254 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 226 | + /** |
|
| 227 | + * Send the final page output to user |
|
| 228 | + */ |
|
| 229 | + public function renderFinalPage(){ |
|
| 230 | + $logger = self::getLogger(); |
|
| 231 | + $obj = & get_instance(); |
|
| 232 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 233 | + $dispatcher = $obj->eventdispatcher; |
|
| 234 | + $content = $this->_pageRender; |
|
| 235 | + if(! $content){ |
|
| 236 | + $logger->warning('The final view content is empty.'); |
|
| 237 | + return; |
|
| 238 | + } |
|
| 239 | + //dispatch |
|
| 240 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 241 | + $content = null; |
|
| 242 | + if(! empty($event->payload)){ |
|
| 243 | + $content = $event->payload; |
|
| 244 | + } |
|
| 245 | + if(empty($content)){ |
|
| 246 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 247 | + } |
|
| 248 | + //remove unsed space in the content |
|
| 249 | + $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 250 | + //check whether need save the page into cache. |
|
| 251 | + if($cachePageStatus){ |
|
| 252 | + $this->savePageContentIntoCache($content); |
|
| 253 | + } |
|
| 254 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 255 | 255 | |
| 256 | - //compress the output if is available |
|
| 257 | - $type = null; |
|
| 258 | - if (self::$_canCompressOutput){ |
|
| 259 | - $type = 'ob_gzhandler'; |
|
| 260 | - } |
|
| 261 | - ob_start($type); |
|
| 262 | - self::sendHeaders(200); |
|
| 263 | - echo $content; |
|
| 264 | - ob_end_flush(); |
|
| 265 | - } |
|
| 256 | + //compress the output if is available |
|
| 257 | + $type = null; |
|
| 258 | + if (self::$_canCompressOutput){ |
|
| 259 | + $type = 'ob_gzhandler'; |
|
| 260 | + } |
|
| 261 | + ob_start($type); |
|
| 262 | + self::sendHeaders(200); |
|
| 263 | + echo $content; |
|
| 264 | + ob_end_flush(); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Send the final page output to user if is cached |
|
| 270 | - * @param object $cache the cache instance |
|
| 271 | - * |
|
| 272 | - * @return boolean whether the page content if available or not |
|
| 273 | - */ |
|
| 274 | - public function renderFinalPageFromCache(&$cache){ |
|
| 275 | - $logger = self::getLogger(); |
|
| 276 | - //the current page cache key for identification |
|
| 277 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 268 | + /** |
|
| 269 | + * Send the final page output to user if is cached |
|
| 270 | + * @param object $cache the cache instance |
|
| 271 | + * |
|
| 272 | + * @return boolean whether the page content if available or not |
|
| 273 | + */ |
|
| 274 | + public function renderFinalPageFromCache(&$cache){ |
|
| 275 | + $logger = self::getLogger(); |
|
| 276 | + //the current page cache key for identification |
|
| 277 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 278 | 278 | |
| 279 | - $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
| 280 | - //get the cache information to prepare header to send to browser |
|
| 281 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 282 | - if($cacheInfo){ |
|
| 283 | - $status = $this->sendCacheNotYetExpireInfoToBrowser($cacheInfo); |
|
| 284 | - if($status === false){ |
|
| 285 | - return $this->sendCachePageContentToBrowser($cache); |
|
| 286 | - } |
|
| 287 | - return true; |
|
| 288 | - } |
|
| 289 | - return false; |
|
| 290 | - } |
|
| 279 | + $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
| 280 | + //get the cache information to prepare header to send to browser |
|
| 281 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 282 | + if($cacheInfo){ |
|
| 283 | + $status = $this->sendCacheNotYetExpireInfoToBrowser($cacheInfo); |
|
| 284 | + if($status === false){ |
|
| 285 | + return $this->sendCachePageContentToBrowser($cache); |
|
| 286 | + } |
|
| 287 | + return true; |
|
| 288 | + } |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * Get the final page to be rendered |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - public function getFinalPageRendered(){ |
|
| 298 | - return $this->_pageRender; |
|
| 299 | - } |
|
| 293 | + /** |
|
| 294 | + * Get the final page to be rendered |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + public function getFinalPageRendered(){ |
|
| 298 | + return $this->_pageRender; |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * Send the HTTP 404 error if can not found the |
|
| 303 | - * routing information for the current request |
|
| 304 | - */ |
|
| 305 | - public static function send404(){ |
|
| 306 | - /********* for logs **************/ |
|
| 307 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 308 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 309 | - $logger = self::getLogger(); |
|
| 310 | - $request =& class_loader('Request', 'classes'); |
|
| 311 | - $userAgent =& class_loader('Browser'); |
|
| 312 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 301 | + /** |
|
| 302 | + * Send the HTTP 404 error if can not found the |
|
| 303 | + * routing information for the current request |
|
| 304 | + */ |
|
| 305 | + public static function send404(){ |
|
| 306 | + /********* for logs **************/ |
|
| 307 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 308 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 309 | + $logger = self::getLogger(); |
|
| 310 | + $request =& class_loader('Request', 'classes'); |
|
| 311 | + $userAgent =& class_loader('Browser'); |
|
| 312 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 313 | 313 | |
| 314 | - //here can't use Loader::functions just include the helper manually |
|
| 315 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 314 | + //here can't use Loader::functions just include the helper manually |
|
| 315 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 316 | 316 | |
| 317 | - $str = '[404 page not found] : '; |
|
| 318 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 319 | - $logger->error($str); |
|
| 320 | - /***********************************/ |
|
| 321 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 322 | - if(file_exists($path)){ |
|
| 323 | - //compress the output if is available |
|
| 324 | - $type = null; |
|
| 325 | - if (self::$_canCompressOutput){ |
|
| 326 | - $type = 'ob_gzhandler'; |
|
| 327 | - } |
|
| 328 | - ob_start($type); |
|
| 329 | - require_once $path; |
|
| 330 | - $output = ob_get_clean(); |
|
| 331 | - self::sendHeaders(404); |
|
| 332 | - echo $output; |
|
| 333 | - } |
|
| 334 | - else{ |
|
| 335 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 336 | - } |
|
| 337 | - } |
|
| 317 | + $str = '[404 page not found] : '; |
|
| 318 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 319 | + $logger->error($str); |
|
| 320 | + /***********************************/ |
|
| 321 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 322 | + if(file_exists($path)){ |
|
| 323 | + //compress the output if is available |
|
| 324 | + $type = null; |
|
| 325 | + if (self::$_canCompressOutput){ |
|
| 326 | + $type = 'ob_gzhandler'; |
|
| 327 | + } |
|
| 328 | + ob_start($type); |
|
| 329 | + require_once $path; |
|
| 330 | + $output = ob_get_clean(); |
|
| 331 | + self::sendHeaders(404); |
|
| 332 | + echo $output; |
|
| 333 | + } |
|
| 334 | + else{ |
|
| 335 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Display the error to user |
|
| 341 | - * @param array $data the error information |
|
| 342 | - */ |
|
| 343 | - public static function sendError(array $data = array()){ |
|
| 344 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 345 | - if(file_exists($path)){ |
|
| 346 | - //compress the output if is available |
|
| 347 | - $type = null; |
|
| 348 | - if (self::$_canCompressOutput){ |
|
| 349 | - $type = 'ob_gzhandler'; |
|
| 350 | - } |
|
| 351 | - ob_start($type); |
|
| 352 | - extract($data); |
|
| 353 | - require_once $path; |
|
| 354 | - $output = ob_get_clean(); |
|
| 355 | - self::sendHeaders(503); |
|
| 356 | - echo $output; |
|
| 357 | - } |
|
| 358 | - else{ |
|
| 359 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 360 | - set_http_status_header(503); |
|
| 361 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 362 | - } |
|
| 363 | - } |
|
| 339 | + /** |
|
| 340 | + * Display the error to user |
|
| 341 | + * @param array $data the error information |
|
| 342 | + */ |
|
| 343 | + public static function sendError(array $data = array()){ |
|
| 344 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 345 | + if(file_exists($path)){ |
|
| 346 | + //compress the output if is available |
|
| 347 | + $type = null; |
|
| 348 | + if (self::$_canCompressOutput){ |
|
| 349 | + $type = 'ob_gzhandler'; |
|
| 350 | + } |
|
| 351 | + ob_start($type); |
|
| 352 | + extract($data); |
|
| 353 | + require_once $path; |
|
| 354 | + $output = ob_get_clean(); |
|
| 355 | + self::sendHeaders(503); |
|
| 356 | + echo $output; |
|
| 357 | + } |
|
| 358 | + else{ |
|
| 359 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 360 | + set_http_status_header(503); |
|
| 361 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | - /** |
|
| 366 | - * Send the cache not yet expire to browser |
|
| 367 | - * @param array $cacheInfo the cache information |
|
| 368 | - * @return boolean true if the information is sent otherwise false |
|
| 369 | - */ |
|
| 370 | - protected function sendCacheNotYetExpireInfoToBrowser($cacheInfo){ |
|
| 371 | - if(! empty($cacheInfo)){ |
|
| 372 | - $logger = self::getLogger(); |
|
| 373 | - $lastModified = $cacheInfo['mtime']; |
|
| 374 | - $expire = $cacheInfo['expire']; |
|
| 375 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 376 | - self::setHeader('Pragma', 'public'); |
|
| 377 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 378 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 379 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 380 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 381 | - $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
| 382 | - self::sendHeaders(304); |
|
| 383 | - return true; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - return false; |
|
| 387 | - } |
|
| 365 | + /** |
|
| 366 | + * Send the cache not yet expire to browser |
|
| 367 | + * @param array $cacheInfo the cache information |
|
| 368 | + * @return boolean true if the information is sent otherwise false |
|
| 369 | + */ |
|
| 370 | + protected function sendCacheNotYetExpireInfoToBrowser($cacheInfo){ |
|
| 371 | + if(! empty($cacheInfo)){ |
|
| 372 | + $logger = self::getLogger(); |
|
| 373 | + $lastModified = $cacheInfo['mtime']; |
|
| 374 | + $expire = $cacheInfo['expire']; |
|
| 375 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 376 | + self::setHeader('Pragma', 'public'); |
|
| 377 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 378 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 379 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 380 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 381 | + $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
| 382 | + self::sendHeaders(304); |
|
| 383 | + return true; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + return false; |
|
| 387 | + } |
|
| 388 | 388 | |
| 389 | - /** |
|
| 390 | - * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
| 391 | - * @param string $content the page content |
|
| 392 | - * @return string the page content after replace |
|
| 393 | - * '{elapsed_time}', '{memory_usage}' |
|
| 394 | - */ |
|
| 395 | - protected function replaceElapseTimeAndMemoryUsage($content){ |
|
| 396 | - //load benchmark class |
|
| 397 | - $benchmark = & class_loader('Benchmark'); |
|
| 389 | + /** |
|
| 390 | + * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
| 391 | + * @param string $content the page content |
|
| 392 | + * @return string the page content after replace |
|
| 393 | + * '{elapsed_time}', '{memory_usage}' |
|
| 394 | + */ |
|
| 395 | + protected function replaceElapseTimeAndMemoryUsage($content){ |
|
| 396 | + //load benchmark class |
|
| 397 | + $benchmark = & class_loader('Benchmark'); |
|
| 398 | 398 | |
| 399 | - // Parse out the elapsed time and memory usage, |
|
| 400 | - // then swap the pseudo-variables with the data |
|
| 401 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 402 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 403 | - return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 404 | - } |
|
| 399 | + // Parse out the elapsed time and memory usage, |
|
| 400 | + // then swap the pseudo-variables with the data |
|
| 401 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 402 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 403 | + return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - /** |
|
| 407 | - * Send the page content from cache to browser |
|
| 408 | - * @param object $cache the cache instance |
|
| 409 | - * @return boolean the status of the operation |
|
| 410 | - */ |
|
| 411 | - protected function sendCachePageContentToBrowser(&$cache){ |
|
| 412 | - $logger = self::getLogger(); |
|
| 413 | - $logger->info('The cache page content is expired or the browser does not send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
| 414 | - self::sendHeaders(200); |
|
| 415 | - //current page cache key |
|
| 416 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 417 | - //get the cache content |
|
| 418 | - $content = $cache->get($pageCacheKey); |
|
| 419 | - if($content){ |
|
| 420 | - $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
| 421 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 422 | - ///display the final output |
|
| 423 | - //compress the output if is available |
|
| 424 | - $type = null; |
|
| 425 | - if (self::$_canCompressOutput){ |
|
| 426 | - $type = 'ob_gzhandler'; |
|
| 427 | - } |
|
| 428 | - ob_start($type); |
|
| 429 | - echo $content; |
|
| 430 | - ob_end_flush(); |
|
| 431 | - return true; |
|
| 432 | - } |
|
| 433 | - $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
| 434 | - $cache->delete($pageCacheKey); |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 406 | + /** |
|
| 407 | + * Send the page content from cache to browser |
|
| 408 | + * @param object $cache the cache instance |
|
| 409 | + * @return boolean the status of the operation |
|
| 410 | + */ |
|
| 411 | + protected function sendCachePageContentToBrowser(&$cache){ |
|
| 412 | + $logger = self::getLogger(); |
|
| 413 | + $logger->info('The cache page content is expired or the browser does not send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
| 414 | + self::sendHeaders(200); |
|
| 415 | + //current page cache key |
|
| 416 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 417 | + //get the cache content |
|
| 418 | + $content = $cache->get($pageCacheKey); |
|
| 419 | + if($content){ |
|
| 420 | + $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
| 421 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
| 422 | + ///display the final output |
|
| 423 | + //compress the output if is available |
|
| 424 | + $type = null; |
|
| 425 | + if (self::$_canCompressOutput){ |
|
| 426 | + $type = 'ob_gzhandler'; |
|
| 427 | + } |
|
| 428 | + ob_start($type); |
|
| 429 | + echo $content; |
|
| 430 | + ob_end_flush(); |
|
| 431 | + return true; |
|
| 432 | + } |
|
| 433 | + $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
| 434 | + $cache->delete($pageCacheKey); |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | 437 | |
| 438 | - /** |
|
| 439 | - * Save the content of page into cache |
|
| 440 | - * @param string $content the page content to be saved |
|
| 441 | - * @return void |
|
| 442 | - */ |
|
| 443 | - protected function savePageContentIntoCache($content){ |
|
| 444 | - $obj = & get_instance(); |
|
| 445 | - $logger = self::getLogger(); |
|
| 438 | + /** |
|
| 439 | + * Save the content of page into cache |
|
| 440 | + * @param string $content the page content to be saved |
|
| 441 | + * @return void |
|
| 442 | + */ |
|
| 443 | + protected function savePageContentIntoCache($content){ |
|
| 444 | + $obj = & get_instance(); |
|
| 445 | + $logger = self::getLogger(); |
|
| 446 | 446 | |
| 447 | - //current page URL |
|
| 448 | - $url = $this->_currentUrl; |
|
| 449 | - //Cache view Time to live in second |
|
| 450 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 451 | - if (!empty($obj->view_cache_ttl)){ |
|
| 452 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 453 | - } |
|
| 454 | - //the cache handler instance |
|
| 455 | - $cacheInstance = $obj->cache; |
|
| 456 | - //the current page cache key for identification |
|
| 457 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 458 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 459 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 447 | + //current page URL |
|
| 448 | + $url = $this->_currentUrl; |
|
| 449 | + //Cache view Time to live in second |
|
| 450 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 451 | + if (!empty($obj->view_cache_ttl)){ |
|
| 452 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 453 | + } |
|
| 454 | + //the cache handler instance |
|
| 455 | + $cacheInstance = $obj->cache; |
|
| 456 | + //the current page cache key for identification |
|
| 457 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 458 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 459 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 460 | 460 | |
| 461 | - //get the cache information to prepare header to send to browser |
|
| 462 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 463 | - if($cacheInfo){ |
|
| 464 | - $lastModified = $cacheInfo['mtime']; |
|
| 465 | - $expire = $cacheInfo['expire']; |
|
| 466 | - $maxAge = $expire - time(); |
|
| 467 | - self::setHeader('Pragma', 'public'); |
|
| 468 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 469 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 470 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 471 | - } |
|
| 472 | - } |
|
| 461 | + //get the cache information to prepare header to send to browser |
|
| 462 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 463 | + if($cacheInfo){ |
|
| 464 | + $lastModified = $cacheInfo['mtime']; |
|
| 465 | + $expire = $cacheInfo['expire']; |
|
| 466 | + $maxAge = $expire - time(); |
|
| 467 | + self::setHeader('Pragma', 'public'); |
|
| 468 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 469 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 470 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | 473 | |
| 474 | 474 | |
| 475 | - /** |
|
| 476 | - * Get the module information for the view to load |
|
| 477 | - * @param string $view the view name like moduleName/viewName, viewName |
|
| 478 | - * |
|
| 479 | - * @return array the module information |
|
| 480 | - * array( |
|
| 481 | - * 'module'=> 'module_name' |
|
| 482 | - * 'view' => 'view_name' |
|
| 483 | - * 'viewFile' => 'view_file' |
|
| 484 | - * ) |
|
| 485 | - */ |
|
| 486 | - protected function getModuleInfoForView($view){ |
|
| 487 | - $module = null; |
|
| 488 | - $viewFile = null; |
|
| 489 | - $obj = & get_instance(); |
|
| 490 | - //check if the request class contains module name |
|
| 491 | - if(strpos($view, '/') !== false){ |
|
| 492 | - $viewPath = explode('/', $view); |
|
| 493 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 494 | - $module = $viewPath[0]; |
|
| 495 | - array_shift($viewPath); |
|
| 496 | - $view = implode('/', $viewPath); |
|
| 497 | - $viewFile = $view . '.php'; |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - if(! $module && !empty($obj->moduleName)){ |
|
| 501 | - $module = $obj->moduleName; |
|
| 502 | - } |
|
| 503 | - return array( |
|
| 504 | - 'view' => $view, |
|
| 505 | - 'module' => $module, |
|
| 506 | - 'viewFile' => $viewFile |
|
| 507 | - ); |
|
| 508 | - } |
|
| 475 | + /** |
|
| 476 | + * Get the module information for the view to load |
|
| 477 | + * @param string $view the view name like moduleName/viewName, viewName |
|
| 478 | + * |
|
| 479 | + * @return array the module information |
|
| 480 | + * array( |
|
| 481 | + * 'module'=> 'module_name' |
|
| 482 | + * 'view' => 'view_name' |
|
| 483 | + * 'viewFile' => 'view_file' |
|
| 484 | + * ) |
|
| 485 | + */ |
|
| 486 | + protected function getModuleInfoForView($view){ |
|
| 487 | + $module = null; |
|
| 488 | + $viewFile = null; |
|
| 489 | + $obj = & get_instance(); |
|
| 490 | + //check if the request class contains module name |
|
| 491 | + if(strpos($view, '/') !== false){ |
|
| 492 | + $viewPath = explode('/', $view); |
|
| 493 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 494 | + $module = $viewPath[0]; |
|
| 495 | + array_shift($viewPath); |
|
| 496 | + $view = implode('/', $viewPath); |
|
| 497 | + $viewFile = $view . '.php'; |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + if(! $module && !empty($obj->moduleName)){ |
|
| 501 | + $module = $obj->moduleName; |
|
| 502 | + } |
|
| 503 | + return array( |
|
| 504 | + 'view' => $view, |
|
| 505 | + 'module' => $module, |
|
| 506 | + 'viewFile' => $viewFile |
|
| 507 | + ); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Render the view page |
|
| 512 | - * @see Response::render |
|
| 513 | - * @return void|string |
|
| 514 | - */ |
|
| 515 | - protected function loadView($path, array $data = array(), $return = false){ |
|
| 516 | - $found = false; |
|
| 517 | - if(file_exists($path)){ |
|
| 518 | - //super instance |
|
| 519 | - $obj = & get_instance(); |
|
| 520 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 521 | - if(! isset($this->{$key})){ |
|
| 522 | - $this->{$key} = & $obj->{$key}; |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - ob_start(); |
|
| 526 | - extract($data); |
|
| 527 | - //need use require() instead of require_once because can load this view many time |
|
| 528 | - require $path; |
|
| 529 | - $content = ob_get_clean(); |
|
| 530 | - if($return){ |
|
| 531 | - //remove unused html space |
|
| 532 | - return preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 533 | - } |
|
| 534 | - $this->_pageRender .= $content; |
|
| 535 | - $found = true; |
|
| 536 | - } |
|
| 537 | - if(! $found){ |
|
| 538 | - show_error('Unable to find view [' .$path . ']'); |
|
| 539 | - } |
|
| 540 | - } |
|
| 510 | + /** |
|
| 511 | + * Render the view page |
|
| 512 | + * @see Response::render |
|
| 513 | + * @return void|string |
|
| 514 | + */ |
|
| 515 | + protected function loadView($path, array $data = array(), $return = false){ |
|
| 516 | + $found = false; |
|
| 517 | + if(file_exists($path)){ |
|
| 518 | + //super instance |
|
| 519 | + $obj = & get_instance(); |
|
| 520 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 521 | + if(! isset($this->{$key})){ |
|
| 522 | + $this->{$key} = & $obj->{$key}; |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + ob_start(); |
|
| 526 | + extract($data); |
|
| 527 | + //need use require() instead of require_once because can load this view many time |
|
| 528 | + require $path; |
|
| 529 | + $content = ob_get_clean(); |
|
| 530 | + if($return){ |
|
| 531 | + //remove unused html space |
|
| 532 | + return preg_replace('~>\s*\n\s*<~', '><', $content); |
|
| 533 | + } |
|
| 534 | + $this->_pageRender .= $content; |
|
| 535 | + $found = true; |
|
| 536 | + } |
|
| 537 | + if(! $found){ |
|
| 538 | + show_error('Unable to find view [' .$path . ']'); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | 541 | |
| 542 | - } |
|
| 542 | + } |
|
@@ -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::getModulesRoutesConfig(); |
|
| 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::getModulesRoutesConfig(); |
|
| 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 | + } |
|
@@ -1,81 +1,81 @@ discard block |
||
| 1 | 1 | <?php |
| 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 | - */ |
|
| 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 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | - */ |
|
| 30 | - if ( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | - show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
| 27 | + /** |
|
| 28 | + * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | + */ |
|
| 30 | + if ( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | + show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + class DBSessionHandler implements SessionHandlerInterface{ |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The encryption method to use to encrypt session data in database |
|
| 38 | - * @const string |
|
| 39 | - */ |
|
| 40 | - const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 36 | + /** |
|
| 37 | + * The encryption method to use to encrypt session data in database |
|
| 38 | + * @const string |
|
| 39 | + */ |
|
| 40 | + const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Super global instance |
|
| 44 | - * @var object |
|
| 45 | - */ |
|
| 46 | - protected $OBJ = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Session secret to use |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - private $sessionSecret = null; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The initialisation vector to use for openssl |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $iv = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The model instance to use |
|
| 62 | - * @var object |
|
| 63 | - */ |
|
| 64 | - private $modelInstance = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The columns of the table to use to store session data |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $sessionTableColumns = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * The instance of the Log |
|
| 74 | - * @var Log |
|
| 75 | - */ |
|
| 76 | - private $logger; |
|
| 77 | - |
|
| 78 | - /** |
|
| 42 | + /** |
|
| 43 | + * Super global instance |
|
| 44 | + * @var object |
|
| 45 | + */ |
|
| 46 | + protected $OBJ = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Session secret to use |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + private $sessionSecret = null; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The initialisation vector to use for openssl |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $iv = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The model instance to use |
|
| 62 | + * @var object |
|
| 63 | + */ |
|
| 64 | + private $modelInstance = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The columns of the table to use to store session data |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $sessionTableColumns = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * The instance of the Log |
|
| 74 | + * @var Log |
|
| 75 | + */ |
|
| 76 | + private $logger; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | 79 | * Instance of the Loader class |
| 80 | 80 | * @var Loader |
| 81 | 81 | */ |
@@ -85,227 +85,227 @@ discard block |
||
| 85 | 85 | * Create new instance of Database session handler |
| 86 | 86 | * @param object $modelInstance the model instance |
| 87 | 87 | */ |
| 88 | - public function __construct(DBSessionHandlerModel $modelInstance = null){ |
|
| 89 | - //Set Log instance to use |
|
| 90 | - $this->setLoggerFromParamOrCreate(null); |
|
| 88 | + public function __construct(DBSessionHandlerModel $modelInstance = null){ |
|
| 89 | + //Set Log instance to use |
|
| 90 | + $this->setLoggerFromParamOrCreate(null); |
|
| 91 | 91 | |
| 92 | - //Set Loader instance to use |
|
| 93 | - $this->setDependencyInstanceFromParamOrCreate('loader', null, 'Loader', 'classes'); |
|
| 92 | + //Set Loader instance to use |
|
| 93 | + $this->setDependencyInstanceFromParamOrCreate('loader', null, 'Loader', 'classes'); |
|
| 94 | 94 | |
| 95 | - $this->OBJ = & get_instance(); |
|
| 95 | + $this->OBJ = & get_instance(); |
|
| 96 | 96 | |
| 97 | - if (is_object($modelInstance)){ |
|
| 98 | - $this->setModelInstance($modelInstance); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Set the session secret used to encrypt the data in database |
|
| 104 | - * @param string $secret the base64 string secret |
|
| 105 | - */ |
|
| 106 | - public function setSessionSecret($secret){ |
|
| 107 | - $this->sessionSecret = $secret; |
|
| 108 | - return $this; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Return the session secret |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - public function getSessionSecret(){ |
|
| 116 | - return $this->sessionSecret; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Set the initializer vector for openssl |
|
| 122 | - * @param string $key the session secret used in base64 format |
|
| 123 | - */ |
|
| 124 | - public function setInitializerVector($key){ |
|
| 125 | - $ivLength = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 126 | - $key = base64_decode($key); |
|
| 127 | - $this->iv = substr(hash('sha256', $key), 0, $ivLength); |
|
| 128 | - return $this; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Return the initializer vector |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function getInitializerVector(){ |
|
| 136 | - return $this->iv; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Open the database session handler, here nothing to do just return true |
|
| 141 | - * @param string $savePath the session save path |
|
| 142 | - * @param string $sessionName the session name |
|
| 143 | - * @return boolean |
|
| 144 | - */ |
|
| 145 | - public function open($savePath, $sessionName){ |
|
| 146 | - $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 147 | - //try to check if session secret is set before |
|
| 148 | - $secret = $this->getSessionSecret(); |
|
| 149 | - if (empty($secret)){ |
|
| 150 | - $secret = get_config('session_secret', null); |
|
| 151 | - $this->setSessionSecret($secret); |
|
| 152 | - } |
|
| 153 | - $this->logger->info('Session secret: ' . $secret); |
|
| 154 | - |
|
| 155 | - if (! is_object($this->modelInstance)){ |
|
| 156 | - $this->setModelInstanceFromConfig(); |
|
| 157 | - } |
|
| 158 | - $this->setInitializerVector($secret); |
|
| 159 | - |
|
| 160 | - //set session tables columns |
|
| 161 | - $this->sessionTableColumns = $this->modelInstance->getSessionTableColumns(); |
|
| 162 | - |
|
| 163 | - if (empty($this->sessionTableColumns)){ |
|
| 164 | - show_error('The session handler is "database" but the table columns not set'); |
|
| 165 | - } |
|
| 166 | - $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 97 | + if (is_object($modelInstance)){ |
|
| 98 | + $this->setModelInstance($modelInstance); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Set the session secret used to encrypt the data in database |
|
| 104 | + * @param string $secret the base64 string secret |
|
| 105 | + */ |
|
| 106 | + public function setSessionSecret($secret){ |
|
| 107 | + $this->sessionSecret = $secret; |
|
| 108 | + return $this; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Return the session secret |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + public function getSessionSecret(){ |
|
| 116 | + return $this->sessionSecret; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Set the initializer vector for openssl |
|
| 122 | + * @param string $key the session secret used in base64 format |
|
| 123 | + */ |
|
| 124 | + public function setInitializerVector($key){ |
|
| 125 | + $ivLength = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 126 | + $key = base64_decode($key); |
|
| 127 | + $this->iv = substr(hash('sha256', $key), 0, $ivLength); |
|
| 128 | + return $this; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Return the initializer vector |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function getInitializerVector(){ |
|
| 136 | + return $this->iv; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Open the database session handler, here nothing to do just return true |
|
| 141 | + * @param string $savePath the session save path |
|
| 142 | + * @param string $sessionName the session name |
|
| 143 | + * @return boolean |
|
| 144 | + */ |
|
| 145 | + public function open($savePath, $sessionName){ |
|
| 146 | + $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 147 | + //try to check if session secret is set before |
|
| 148 | + $secret = $this->getSessionSecret(); |
|
| 149 | + if (empty($secret)){ |
|
| 150 | + $secret = get_config('session_secret', null); |
|
| 151 | + $this->setSessionSecret($secret); |
|
| 152 | + } |
|
| 153 | + $this->logger->info('Session secret: ' . $secret); |
|
| 154 | + |
|
| 155 | + if (! is_object($this->modelInstance)){ |
|
| 156 | + $this->setModelInstanceFromConfig(); |
|
| 157 | + } |
|
| 158 | + $this->setInitializerVector($secret); |
|
| 159 | + |
|
| 160 | + //set session tables columns |
|
| 161 | + $this->sessionTableColumns = $this->modelInstance->getSessionTableColumns(); |
|
| 162 | + |
|
| 163 | + if (empty($this->sessionTableColumns)){ |
|
| 164 | + show_error('The session handler is "database" but the table columns not set'); |
|
| 165 | + } |
|
| 166 | + $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 167 | 167 | |
| 168 | - //delete the expired session |
|
| 169 | - $timeActivity = get_config('session_inactivity_time', 100); |
|
| 170 | - $this->gc($timeActivity); |
|
| 168 | + //delete the expired session |
|
| 169 | + $timeActivity = get_config('session_inactivity_time', 100); |
|
| 170 | + $this->gc($timeActivity); |
|
| 171 | 171 | |
| 172 | - return true; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Close the session |
|
| 177 | - * @return boolean |
|
| 178 | - */ |
|
| 179 | - public function close(){ |
|
| 180 | - $this->logger->debug('Closing database session handler'); |
|
| 181 | - return true; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Get the session value for the given session id |
|
| 186 | - * @param string $sid the session id to use |
|
| 187 | - * @return string the session data in serialiaze format |
|
| 188 | - */ |
|
| 189 | - public function read($sid){ |
|
| 190 | - $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 191 | - $instance = $this->getModelInstance(); |
|
| 192 | - $columns = $this->sessionTableColumns; |
|
| 193 | - $this->loader->functions('user_agent'); |
|
| 194 | - $this->loader->library('Browser'); |
|
| 172 | + return true; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Close the session |
|
| 177 | + * @return boolean |
|
| 178 | + */ |
|
| 179 | + public function close(){ |
|
| 180 | + $this->logger->debug('Closing database session handler'); |
|
| 181 | + return true; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Get the session value for the given session id |
|
| 186 | + * @param string $sid the session id to use |
|
| 187 | + * @return string the session data in serialiaze format |
|
| 188 | + */ |
|
| 189 | + public function read($sid){ |
|
| 190 | + $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 191 | + $instance = $this->getModelInstance(); |
|
| 192 | + $columns = $this->sessionTableColumns; |
|
| 193 | + $this->loader->functions('user_agent'); |
|
| 194 | + $this->loader->library('Browser'); |
|
| 195 | 195 | |
| 196 | - $ip = get_ip(); |
|
| 197 | - $host = @gethostbyaddr($ip) or null; |
|
| 198 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 196 | + $ip = get_ip(); |
|
| 197 | + $host = @gethostbyaddr($ip) or null; |
|
| 198 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 199 | 199 | |
| 200 | - $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 201 | - if ($data && isset($data->{$columns['sdata']})){ |
|
| 202 | - //checking inactivity |
|
| 203 | - $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 204 | - if ($data->{$columns['stime']} < $timeInactivity){ |
|
| 205 | - $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 206 | - $this->destroy($sid); |
|
| 207 | - return null; |
|
| 208 | - } |
|
| 209 | - return $this->decode($data->{$columns['sdata']}); |
|
| 210 | - } |
|
| 211 | - $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 212 | - return null; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Save the session data |
|
| 217 | - * @param string $sid the session ID |
|
| 218 | - * @param mixed $data the session data to save in serialize format |
|
| 219 | - * @return boolean |
|
| 220 | - */ |
|
| 221 | - public function write($sid, $data){ |
|
| 222 | - $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 223 | - $instance = $this->getModelInstance(); |
|
| 224 | - $columns = $this->sessionTableColumns; |
|
| 225 | - |
|
| 226 | - $this->loader->functions('user_agent'); |
|
| 227 | - $this->loader->library('Browser'); |
|
| 228 | - |
|
| 229 | - $ip = get_ip(); |
|
| 230 | - $keyValue = $instance->getKeyValue(); |
|
| 231 | - $host = @gethostbyaddr($ip) or null; |
|
| 232 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 233 | - $data = $this->encode($data); |
|
| 234 | - $params = array( |
|
| 235 | - $columns['sid'] => $sid, |
|
| 236 | - $columns['sdata'] => $data, |
|
| 237 | - $columns['stime'] => time(), |
|
| 238 | - $columns['shost'] => $host, |
|
| 239 | - $columns['sbrowser'] => $browser, |
|
| 240 | - $columns['sip'] => $ip, |
|
| 241 | - $columns['skey'] => $keyValue |
|
| 242 | - ); |
|
| 243 | - $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 244 | - $exists = $instance->get($sid); |
|
| 245 | - if ($exists){ |
|
| 246 | - $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 247 | - //update |
|
| 248 | - unset($params[$columns['sid']]); |
|
| 249 | - return $instance->update($sid, $params); |
|
| 250 | - } |
|
| 251 | - $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 252 | - return $instance->insert($params); |
|
| 253 | - return true; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Destroy the session data for the given session id |
|
| 259 | - * @param string $sid the session id value |
|
| 260 | - * @return boolean |
|
| 261 | - */ |
|
| 262 | - public function destroy($sid){ |
|
| 263 | - $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 264 | - $instance = $this->modelInstance; |
|
| 265 | - $instance->delete($sid); |
|
| 266 | - return true; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Clean the expire session data to save espace |
|
| 271 | - * @param integer $maxLifetime the max lifetime |
|
| 272 | - * @return boolean |
|
| 273 | - */ |
|
| 274 | - public function gc($maxLifetime){ |
|
| 275 | - $instance = $this->modelInstance; |
|
| 276 | - $time = time() - $maxLifetime; |
|
| 277 | - $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 278 | - $instance->deleteByTime($time); |
|
| 279 | - return true; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Encode the session data using the openssl |
|
| 284 | - * @param mixed $data the session data to encode |
|
| 285 | - * @return mixed the encoded session data |
|
| 286 | - */ |
|
| 287 | - public function encode($data){ |
|
| 288 | - $key = base64_decode($this->sessionSecret); |
|
| 289 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 290 | - $output = base64_encode($dataEncrypted); |
|
| 291 | - return $output; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Decode the session data using the openssl |
|
| 297 | - * @param mixed $data the data to decode |
|
| 298 | - * @return mixed the decoded data |
|
| 299 | - */ |
|
| 300 | - public function decode($data){ |
|
| 301 | - $key = base64_decode($this->sessionSecret); |
|
| 302 | - $data = base64_decode($data); |
|
| 303 | - $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 304 | - return $data; |
|
| 305 | - } |
|
| 200 | + $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 201 | + if ($data && isset($data->{$columns['sdata']})){ |
|
| 202 | + //checking inactivity |
|
| 203 | + $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 204 | + if ($data->{$columns['stime']} < $timeInactivity){ |
|
| 205 | + $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 206 | + $this->destroy($sid); |
|
| 207 | + return null; |
|
| 208 | + } |
|
| 209 | + return $this->decode($data->{$columns['sdata']}); |
|
| 210 | + } |
|
| 211 | + $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 212 | + return null; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Save the session data |
|
| 217 | + * @param string $sid the session ID |
|
| 218 | + * @param mixed $data the session data to save in serialize format |
|
| 219 | + * @return boolean |
|
| 220 | + */ |
|
| 221 | + public function write($sid, $data){ |
|
| 222 | + $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 223 | + $instance = $this->getModelInstance(); |
|
| 224 | + $columns = $this->sessionTableColumns; |
|
| 225 | + |
|
| 226 | + $this->loader->functions('user_agent'); |
|
| 227 | + $this->loader->library('Browser'); |
|
| 228 | + |
|
| 229 | + $ip = get_ip(); |
|
| 230 | + $keyValue = $instance->getKeyValue(); |
|
| 231 | + $host = @gethostbyaddr($ip) or null; |
|
| 232 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 233 | + $data = $this->encode($data); |
|
| 234 | + $params = array( |
|
| 235 | + $columns['sid'] => $sid, |
|
| 236 | + $columns['sdata'] => $data, |
|
| 237 | + $columns['stime'] => time(), |
|
| 238 | + $columns['shost'] => $host, |
|
| 239 | + $columns['sbrowser'] => $browser, |
|
| 240 | + $columns['sip'] => $ip, |
|
| 241 | + $columns['skey'] => $keyValue |
|
| 242 | + ); |
|
| 243 | + $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 244 | + $exists = $instance->get($sid); |
|
| 245 | + if ($exists){ |
|
| 246 | + $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 247 | + //update |
|
| 248 | + unset($params[$columns['sid']]); |
|
| 249 | + return $instance->update($sid, $params); |
|
| 250 | + } |
|
| 251 | + $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 252 | + return $instance->insert($params); |
|
| 253 | + return true; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Destroy the session data for the given session id |
|
| 259 | + * @param string $sid the session id value |
|
| 260 | + * @return boolean |
|
| 261 | + */ |
|
| 262 | + public function destroy($sid){ |
|
| 263 | + $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 264 | + $instance = $this->modelInstance; |
|
| 265 | + $instance->delete($sid); |
|
| 266 | + return true; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Clean the expire session data to save espace |
|
| 271 | + * @param integer $maxLifetime the max lifetime |
|
| 272 | + * @return boolean |
|
| 273 | + */ |
|
| 274 | + public function gc($maxLifetime){ |
|
| 275 | + $instance = $this->modelInstance; |
|
| 276 | + $time = time() - $maxLifetime; |
|
| 277 | + $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 278 | + $instance->deleteByTime($time); |
|
| 279 | + return true; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Encode the session data using the openssl |
|
| 284 | + * @param mixed $data the session data to encode |
|
| 285 | + * @return mixed the encoded session data |
|
| 286 | + */ |
|
| 287 | + public function encode($data){ |
|
| 288 | + $key = base64_decode($this->sessionSecret); |
|
| 289 | + $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 290 | + $output = base64_encode($dataEncrypted); |
|
| 291 | + return $output; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Decode the session data using the openssl |
|
| 297 | + * @param mixed $data the data to decode |
|
| 298 | + * @return mixed the decoded data |
|
| 299 | + */ |
|
| 300 | + public function decode($data){ |
|
| 301 | + $key = base64_decode($this->sessionSecret); |
|
| 302 | + $data = base64_decode($data); |
|
| 303 | + $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 304 | + return $data; |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | 307 | |
| 308 | - /** |
|
| 308 | + /** |
|
| 309 | 309 | * Return the loader instance |
| 310 | 310 | * @return object Loader the loader instance |
| 311 | 311 | */ |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | * set the loader instance for future use |
| 318 | 318 | * @param object Loader $loader the loader object |
| 319 | 319 | */ |
| 320 | - public function setLoader($loader){ |
|
| 320 | + public function setLoader($loader){ |
|
| 321 | 321 | $this->loader = $loader; |
| 322 | 322 | return $this; |
| 323 | 323 | } |
@@ -334,77 +334,77 @@ discard block |
||
| 334 | 334 | * set the model instance for future use |
| 335 | 335 | * @param DBSessionHandlerModel $modelInstance the model object |
| 336 | 336 | */ |
| 337 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 337 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 338 | 338 | $this->modelInstance = $modelInstance; |
| 339 | 339 | return $this; |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
| 343 | - * Return the Log instance |
|
| 344 | - * @return Log |
|
| 345 | - */ |
|
| 346 | - public function getLogger(){ |
|
| 347 | - return $this->logger; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Set the log instance |
|
| 352 | - * @param Log $logger the log object |
|
| 353 | - */ |
|
| 354 | - public function setLogger(Log $logger){ |
|
| 355 | - $this->logger = $logger; |
|
| 356 | - return $this; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Set the dependencies instance using argument or create new instance if is null |
|
| 361 | - * @param string $name this class property name. |
|
| 362 | - * @param object $instance the instance. If is not null will use it |
|
| 363 | - * otherwise will create new instance. |
|
| 364 | - * @param string $loadClassName the name of class to load using class_loader function. |
|
| 365 | - * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 366 | - * |
|
| 367 | - * @return object this current instance |
|
| 368 | - */ |
|
| 369 | - protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
|
| 370 | - if ($instance !== null){ |
|
| 371 | - $this->{$name} = $instance; |
|
| 372 | - return $this; |
|
| 373 | - } |
|
| 374 | - $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 375 | - return $this; |
|
| 376 | - } |
|
| 343 | + * Return the Log instance |
|
| 344 | + * @return Log |
|
| 345 | + */ |
|
| 346 | + public function getLogger(){ |
|
| 347 | + return $this->logger; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Set the log instance |
|
| 352 | + * @param Log $logger the log object |
|
| 353 | + */ |
|
| 354 | + public function setLogger(Log $logger){ |
|
| 355 | + $this->logger = $logger; |
|
| 356 | + return $this; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Set the dependencies instance using argument or create new instance if is null |
|
| 361 | + * @param string $name this class property name. |
|
| 362 | + * @param object $instance the instance. If is not null will use it |
|
| 363 | + * otherwise will create new instance. |
|
| 364 | + * @param string $loadClassName the name of class to load using class_loader function. |
|
| 365 | + * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 366 | + * |
|
| 367 | + * @return object this current instance |
|
| 368 | + */ |
|
| 369 | + protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
|
| 370 | + if ($instance !== null){ |
|
| 371 | + $this->{$name} = $instance; |
|
| 372 | + return $this; |
|
| 373 | + } |
|
| 374 | + $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 375 | + return $this; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * Set the Log instance using argument or create new instance |
|
| 380 | - * @param object $logger the Log instance if not null |
|
| 381 | - * |
|
| 382 | - * @return object this current instance |
|
| 383 | - */ |
|
| 384 | - protected function setLoggerFromParamOrCreate(Log $logger = null){ |
|
| 385 | - $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 386 | - if ($logger === null){ |
|
| 387 | - $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 388 | - } |
|
| 389 | - return $this; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Set the model instance using the configuration for session |
|
| 394 | - */ |
|
| 395 | - protected function setModelInstanceFromConfig(){ |
|
| 396 | - $modelName = get_config('session_save_path'); |
|
| 397 | - $this->logger->info('The database session model: ' . $modelName); |
|
| 398 | - $this->loader->model($modelName, 'dbsessionhandlerinstance'); |
|
| 399 | - //@codeCoverageIgnoreStart |
|
| 378 | + /** |
|
| 379 | + * Set the Log instance using argument or create new instance |
|
| 380 | + * @param object $logger the Log instance if not null |
|
| 381 | + * |
|
| 382 | + * @return object this current instance |
|
| 383 | + */ |
|
| 384 | + protected function setLoggerFromParamOrCreate(Log $logger = null){ |
|
| 385 | + $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 386 | + if ($logger === null){ |
|
| 387 | + $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 388 | + } |
|
| 389 | + return $this; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Set the model instance using the configuration for session |
|
| 394 | + */ |
|
| 395 | + protected function setModelInstanceFromConfig(){ |
|
| 396 | + $modelName = get_config('session_save_path'); |
|
| 397 | + $this->logger->info('The database session model: ' . $modelName); |
|
| 398 | + $this->loader->model($modelName, 'dbsessionhandlerinstance'); |
|
| 399 | + //@codeCoverageIgnoreStart |
|
| 400 | 400 | if (isset($this->OBJ->dbsessionhandlerinstance) |
| 401 | - && ! ($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 401 | + && ! ($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 402 | 402 | ) { |
| 403 | - show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 404 | - } |
|
| 405 | - //@codeCoverageIgnoreEnd |
|
| 403 | + show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 404 | + } |
|
| 405 | + //@codeCoverageIgnoreEnd |
|
| 406 | 406 | |
| 407 | - //set model instance |
|
| 408 | - $this->modelInstance = $this->OBJ->dbsessionhandlerinstance; |
|
| 409 | - } |
|
| 410 | - } |
|
| 407 | + //set model instance |
|
| 408 | + $this->modelInstance = $this->OBJ->dbsessionhandlerinstance; |
|
| 409 | + } |
|
| 410 | + } |
|
@@ -1,657 +1,657 @@ |
||
| 1 | 1 | <?php |
| 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 | - */ |
|
| 26 | - class Loader{ |
|
| 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 | + */ |
|
| 26 | + class Loader{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * List of loaded resources |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - public static $loaded = array(); |
|
| 28 | + /** |
|
| 29 | + * List of loaded resources |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + public static $loaded = array(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The logger instance |
|
| 36 | - * @var object |
|
| 37 | - */ |
|
| 38 | - private static $logger; |
|
| 34 | + /** |
|
| 35 | + * The logger instance |
|
| 36 | + * @var object |
|
| 37 | + */ |
|
| 38 | + private static $logger; |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 42 | - //add the resources already loaded during application bootstrap |
|
| 43 | - //in the list to prevent duplicate or loading the resources again. |
|
| 44 | - static::$loaded = class_loaded(); |
|
| 41 | + public function __construct(){ |
|
| 42 | + //add the resources already loaded during application bootstrap |
|
| 43 | + //in the list to prevent duplicate or loading the resources again. |
|
| 44 | + static::$loaded = class_loaded(); |
|
| 45 | 45 | |
| 46 | - //Load resources from autoload configuration |
|
| 47 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | - } |
|
| 46 | + //Load resources from autoload configuration |
|
| 47 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * The signleton of the logger |
|
| 52 | - * @return object the Log instance |
|
| 53 | - */ |
|
| 54 | - public static function getLogger(){ |
|
| 55 | - if(self::$logger == null){ |
|
| 56 | - $logger = array(); |
|
| 57 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 58 | - $logger[0]->setLogger('Library::Loader'); |
|
| 59 | - self::$logger = $logger[0]; |
|
| 60 | - } |
|
| 61 | - return self::$logger; |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * The signleton of the logger |
|
| 52 | + * @return object the Log instance |
|
| 53 | + */ |
|
| 54 | + public static function getLogger(){ |
|
| 55 | + if(self::$logger == null){ |
|
| 56 | + $logger = array(); |
|
| 57 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 58 | + $logger[0]->setLogger('Library::Loader'); |
|
| 59 | + self::$logger = $logger[0]; |
|
| 60 | + } |
|
| 61 | + return self::$logger; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Set the log instance for future use |
|
| 66 | - * @param object $logger the log object |
|
| 67 | - * @return object the log instance |
|
| 68 | - */ |
|
| 69 | - public static function setLogger($logger){ |
|
| 70 | - self::$logger = $logger; |
|
| 71 | - return self::$logger; |
|
| 72 | - } |
|
| 64 | + /** |
|
| 65 | + * Set the log instance for future use |
|
| 66 | + * @param object $logger the log object |
|
| 67 | + * @return object the log instance |
|
| 68 | + */ |
|
| 69 | + public static function setLogger($logger){ |
|
| 70 | + self::$logger = $logger; |
|
| 71 | + return self::$logger; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Load the model class |
|
| 77 | - * |
|
| 78 | - * @param string $class the class name to be loaded |
|
| 79 | - * @param string $instance the name of the instance to use in super object |
|
| 80 | - * |
|
| 81 | - * @return void |
|
| 82 | - */ |
|
| 83 | - public static function model($class, $instance = null){ |
|
| 84 | - $logger = static::getLogger(); |
|
| 85 | - $class = str_ireplace('.php', '', $class); |
|
| 86 | - $class = trim($class, '/\\'); |
|
| 87 | - $file = ucfirst($class).'.php'; |
|
| 88 | - $logger->debug('Loading model [' . $class . '] ...'); |
|
| 89 | - //************ |
|
| 90 | - if (! $instance){ |
|
| 91 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 92 | - } |
|
| 93 | - //**************** |
|
| 94 | - if (isset(static::$loaded[$instance])){ |
|
| 95 | - $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - $classFilePath = APPS_MODEL_PATH . $file; |
|
| 99 | - //first check if this model is in the module |
|
| 100 | - $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 101 | - //check if the request class contains module name |
|
| 102 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 103 | - $module = $moduleInfo['module']; |
|
| 104 | - $class = $moduleInfo['class']; |
|
| 75 | + /** |
|
| 76 | + * Load the model class |
|
| 77 | + * |
|
| 78 | + * @param string $class the class name to be loaded |
|
| 79 | + * @param string $instance the name of the instance to use in super object |
|
| 80 | + * |
|
| 81 | + * @return void |
|
| 82 | + */ |
|
| 83 | + public static function model($class, $instance = null){ |
|
| 84 | + $logger = static::getLogger(); |
|
| 85 | + $class = str_ireplace('.php', '', $class); |
|
| 86 | + $class = trim($class, '/\\'); |
|
| 87 | + $file = ucfirst($class).'.php'; |
|
| 88 | + $logger->debug('Loading model [' . $class . '] ...'); |
|
| 89 | + //************ |
|
| 90 | + if (! $instance){ |
|
| 91 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 92 | + } |
|
| 93 | + //**************** |
|
| 94 | + if (isset(static::$loaded[$instance])){ |
|
| 95 | + $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + $classFilePath = APPS_MODEL_PATH . $file; |
|
| 99 | + //first check if this model is in the module |
|
| 100 | + $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 101 | + //check if the request class contains module name |
|
| 102 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 103 | + $module = $moduleInfo['module']; |
|
| 104 | + $class = $moduleInfo['class']; |
|
| 105 | 105 | |
| 106 | - $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 107 | - if ($moduleModelFilePath){ |
|
| 108 | - $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 109 | - $classFilePath = $moduleModelFilePath; |
|
| 110 | - } |
|
| 111 | - else{ |
|
| 112 | - $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 113 | - } |
|
| 114 | - $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 115 | - if (file_exists($classFilePath)){ |
|
| 116 | - require_once $classFilePath; |
|
| 117 | - if (class_exists($class)){ |
|
| 118 | - $c = new $class(); |
|
| 119 | - $obj = & get_instance(); |
|
| 120 | - $obj->{$instance} = $c; |
|
| 121 | - static::$loaded[$instance] = $class; |
|
| 122 | - $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 123 | - } |
|
| 124 | - else{ |
|
| 125 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - else{ |
|
| 129 | - show_error('Unable to find the model [' . $class . ']'); |
|
| 130 | - } |
|
| 131 | - } |
|
| 106 | + $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 107 | + if ($moduleModelFilePath){ |
|
| 108 | + $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 109 | + $classFilePath = $moduleModelFilePath; |
|
| 110 | + } |
|
| 111 | + else{ |
|
| 112 | + $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 113 | + } |
|
| 114 | + $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 115 | + if (file_exists($classFilePath)){ |
|
| 116 | + require_once $classFilePath; |
|
| 117 | + if (class_exists($class)){ |
|
| 118 | + $c = new $class(); |
|
| 119 | + $obj = & get_instance(); |
|
| 120 | + $obj->{$instance} = $c; |
|
| 121 | + static::$loaded[$instance] = $class; |
|
| 122 | + $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 123 | + } |
|
| 124 | + else{ |
|
| 125 | + show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + else{ |
|
| 129 | + show_error('Unable to find the model [' . $class . ']'); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Load the library class |
|
| 136 | - * |
|
| 137 | - * @param string $class the library class name to be loaded |
|
| 138 | - * @param string $instance the instance name to use in super object |
|
| 139 | - * @param mixed $params the arguments to pass to the constructor |
|
| 140 | - * |
|
| 141 | - * @return void |
|
| 142 | - */ |
|
| 143 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 144 | - $logger = static::getLogger(); |
|
| 145 | - $class = str_ireplace('.php', '', $class); |
|
| 146 | - $class = trim($class, '/\\'); |
|
| 147 | - $file = ucfirst($class) .'.php'; |
|
| 148 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 149 | - if (! $instance){ |
|
| 150 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 151 | - } |
|
| 152 | - if (isset(static::$loaded[$instance])){ |
|
| 153 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - $obj = & get_instance(); |
|
| 157 | - //Check and load Database library |
|
| 158 | - if (strtolower($class) == 'database'){ |
|
| 159 | - $logger->info('This is the Database library ...'); |
|
| 160 | - $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 161 | - static::$loaded[$instance] = $class; |
|
| 162 | - $logger->info('Library Database loaded successfully.'); |
|
| 163 | - return; |
|
| 164 | - } |
|
| 165 | - $libraryFilePath = null; |
|
| 166 | - $logger->debug('Check if this is a system library ...'); |
|
| 167 | - if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 168 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 169 | - $class = ucfirst($class); |
|
| 170 | - $logger->info('This library is a system library'); |
|
| 171 | - } |
|
| 172 | - else{ |
|
| 173 | - $logger->info('This library is not a system library'); |
|
| 174 | - //first check if this library is in the module |
|
| 175 | - $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 176 | - //*************** |
|
| 177 | - } |
|
| 178 | - if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 179 | - $libraryFilePath = LIBRARY_PATH . $file; |
|
| 180 | - } |
|
| 181 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 182 | - //************************* |
|
| 183 | - self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 184 | - } |
|
| 134 | + /** |
|
| 135 | + * Load the library class |
|
| 136 | + * |
|
| 137 | + * @param string $class the library class name to be loaded |
|
| 138 | + * @param string $instance the instance name to use in super object |
|
| 139 | + * @param mixed $params the arguments to pass to the constructor |
|
| 140 | + * |
|
| 141 | + * @return void |
|
| 142 | + */ |
|
| 143 | + public static function library($class, $instance = null, array $params = array()){ |
|
| 144 | + $logger = static::getLogger(); |
|
| 145 | + $class = str_ireplace('.php', '', $class); |
|
| 146 | + $class = trim($class, '/\\'); |
|
| 147 | + $file = ucfirst($class) .'.php'; |
|
| 148 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 149 | + if (! $instance){ |
|
| 150 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 151 | + } |
|
| 152 | + if (isset(static::$loaded[$instance])){ |
|
| 153 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + $obj = & get_instance(); |
|
| 157 | + //Check and load Database library |
|
| 158 | + if (strtolower($class) == 'database'){ |
|
| 159 | + $logger->info('This is the Database library ...'); |
|
| 160 | + $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 161 | + static::$loaded[$instance] = $class; |
|
| 162 | + $logger->info('Library Database loaded successfully.'); |
|
| 163 | + return; |
|
| 164 | + } |
|
| 165 | + $libraryFilePath = null; |
|
| 166 | + $logger->debug('Check if this is a system library ...'); |
|
| 167 | + if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 168 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 169 | + $class = ucfirst($class); |
|
| 170 | + $logger->info('This library is a system library'); |
|
| 171 | + } |
|
| 172 | + else{ |
|
| 173 | + $logger->info('This library is not a system library'); |
|
| 174 | + //first check if this library is in the module |
|
| 175 | + $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 176 | + //*************** |
|
| 177 | + } |
|
| 178 | + if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 179 | + $libraryFilePath = LIBRARY_PATH . $file; |
|
| 180 | + } |
|
| 181 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 182 | + //************************* |
|
| 183 | + self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Load the helper |
|
| 188 | - * |
|
| 189 | - * @param string $function the helper name to be loaded |
|
| 190 | - * |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - public static function functions($function){ |
|
| 194 | - $logger = static::getLogger(); |
|
| 195 | - $function = str_ireplace('.php', '', $function); |
|
| 196 | - $function = trim($function, '/\\'); |
|
| 197 | - $function = str_ireplace('function_', '', $function); |
|
| 198 | - $file = 'function_'.$function.'.php'; |
|
| 199 | - $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 200 | - if (isset(static::$loaded['function_' . $function])){ |
|
| 201 | - $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 202 | - return; |
|
| 203 | - } |
|
| 204 | - $functionFilePath = null; |
|
| 205 | - //first check if this helper is in the module |
|
| 206 | - $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 207 | - $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 208 | - $module = $moduleInfo['module']; |
|
| 209 | - $function = $moduleInfo['function']; |
|
| 210 | - if(! empty($moduleInfo['file'])){ |
|
| 211 | - $file = $moduleInfo['file']; |
|
| 212 | - } |
|
| 213 | - $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 214 | - if ($moduleFunctionPath){ |
|
| 215 | - $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 216 | - $functionFilePath = $moduleFunctionPath; |
|
| 217 | - } |
|
| 218 | - else{ |
|
| 219 | - $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 220 | - } |
|
| 221 | - if (! $functionFilePath){ |
|
| 222 | - $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 223 | - foreach($searchDir as $dir){ |
|
| 224 | - $filePath = $dir . $file; |
|
| 225 | - if (file_exists($filePath)){ |
|
| 226 | - $functionFilePath = $filePath; |
|
| 227 | - //is already found not to continue |
|
| 228 | - break; |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 233 | - if ($functionFilePath){ |
|
| 234 | - require_once $functionFilePath; |
|
| 235 | - static::$loaded['function_' . $function] = $functionFilePath; |
|
| 236 | - $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 237 | - } |
|
| 238 | - else{ |
|
| 239 | - show_error('Unable to find helper file [' . $file . ']'); |
|
| 240 | - } |
|
| 241 | - } |
|
| 186 | + /** |
|
| 187 | + * Load the helper |
|
| 188 | + * |
|
| 189 | + * @param string $function the helper name to be loaded |
|
| 190 | + * |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + public static function functions($function){ |
|
| 194 | + $logger = static::getLogger(); |
|
| 195 | + $function = str_ireplace('.php', '', $function); |
|
| 196 | + $function = trim($function, '/\\'); |
|
| 197 | + $function = str_ireplace('function_', '', $function); |
|
| 198 | + $file = 'function_'.$function.'.php'; |
|
| 199 | + $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 200 | + if (isset(static::$loaded['function_' . $function])){ |
|
| 201 | + $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | + $functionFilePath = null; |
|
| 205 | + //first check if this helper is in the module |
|
| 206 | + $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 207 | + $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 208 | + $module = $moduleInfo['module']; |
|
| 209 | + $function = $moduleInfo['function']; |
|
| 210 | + if(! empty($moduleInfo['file'])){ |
|
| 211 | + $file = $moduleInfo['file']; |
|
| 212 | + } |
|
| 213 | + $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 214 | + if ($moduleFunctionPath){ |
|
| 215 | + $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 216 | + $functionFilePath = $moduleFunctionPath; |
|
| 217 | + } |
|
| 218 | + else{ |
|
| 219 | + $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 220 | + } |
|
| 221 | + if (! $functionFilePath){ |
|
| 222 | + $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 223 | + foreach($searchDir as $dir){ |
|
| 224 | + $filePath = $dir . $file; |
|
| 225 | + if (file_exists($filePath)){ |
|
| 226 | + $functionFilePath = $filePath; |
|
| 227 | + //is already found not to continue |
|
| 228 | + break; |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 233 | + if ($functionFilePath){ |
|
| 234 | + require_once $functionFilePath; |
|
| 235 | + static::$loaded['function_' . $function] = $functionFilePath; |
|
| 236 | + $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 237 | + } |
|
| 238 | + else{ |
|
| 239 | + show_error('Unable to find helper file [' . $file . ']'); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Load the configuration file |
|
| 245 | - * |
|
| 246 | - * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 247 | - * |
|
| 248 | - * @return void |
|
| 249 | - */ |
|
| 250 | - public static function config($filename){ |
|
| 251 | - $logger = static::getLogger(); |
|
| 252 | - $filename = str_ireplace('.php', '', $filename); |
|
| 253 | - $filename = trim($filename, '/\\'); |
|
| 254 | - $filename = str_ireplace('config_', '', $filename); |
|
| 255 | - $file = 'config_'.$filename.'.php'; |
|
| 256 | - $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 257 | - if (isset(static::$loaded['config_' . $filename])){ |
|
| 258 | - $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 259 | - return; |
|
| 260 | - } |
|
| 261 | - $configFilePath = CONFIG_PATH . $file; |
|
| 262 | - //first check if this config is in the module |
|
| 263 | - $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 264 | - $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 265 | - $module = $moduleInfo['module']; |
|
| 266 | - $filename = $moduleInfo['filename']; |
|
| 267 | - $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 268 | - if ($moduleConfigPath){ |
|
| 269 | - $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 270 | - $configFilePath = $moduleConfigPath; |
|
| 271 | - } |
|
| 272 | - else{ |
|
| 273 | - $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 274 | - } |
|
| 275 | - $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 276 | - $config = array(); |
|
| 277 | - if (file_exists($configFilePath)){ |
|
| 278 | - require_once $configFilePath; |
|
| 279 | - if (! empty($config) && is_array($config)){ |
|
| 280 | - Config::setAll($config); |
|
| 281 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 282 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 283 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 284 | - unset($config); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - else{ |
|
| 288 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 289 | - } |
|
| 290 | - } |
|
| 243 | + /** |
|
| 244 | + * Load the configuration file |
|
| 245 | + * |
|
| 246 | + * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 247 | + * |
|
| 248 | + * @return void |
|
| 249 | + */ |
|
| 250 | + public static function config($filename){ |
|
| 251 | + $logger = static::getLogger(); |
|
| 252 | + $filename = str_ireplace('.php', '', $filename); |
|
| 253 | + $filename = trim($filename, '/\\'); |
|
| 254 | + $filename = str_ireplace('config_', '', $filename); |
|
| 255 | + $file = 'config_'.$filename.'.php'; |
|
| 256 | + $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 257 | + if (isset(static::$loaded['config_' . $filename])){ |
|
| 258 | + $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 259 | + return; |
|
| 260 | + } |
|
| 261 | + $configFilePath = CONFIG_PATH . $file; |
|
| 262 | + //first check if this config is in the module |
|
| 263 | + $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 264 | + $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 265 | + $module = $moduleInfo['module']; |
|
| 266 | + $filename = $moduleInfo['filename']; |
|
| 267 | + $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 268 | + if ($moduleConfigPath){ |
|
| 269 | + $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 270 | + $configFilePath = $moduleConfigPath; |
|
| 271 | + } |
|
| 272 | + else{ |
|
| 273 | + $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 274 | + } |
|
| 275 | + $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 276 | + $config = array(); |
|
| 277 | + if (file_exists($configFilePath)){ |
|
| 278 | + require_once $configFilePath; |
|
| 279 | + if (! empty($config) && is_array($config)){ |
|
| 280 | + Config::setAll($config); |
|
| 281 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 282 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 283 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 284 | + unset($config); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + else{ |
|
| 288 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * Load the language |
|
| 295 | - * |
|
| 296 | - * @param string $language the language name to be loaded |
|
| 297 | - * |
|
| 298 | - * @return void |
|
| 299 | - */ |
|
| 300 | - public static function lang($language){ |
|
| 301 | - $logger = static::getLogger(); |
|
| 302 | - $language = str_ireplace('.php', '', $language); |
|
| 303 | - $language = trim($language, '/\\'); |
|
| 304 | - $language = str_ireplace('lang_', '', $language); |
|
| 305 | - $file = 'lang_'.$language.'.php'; |
|
| 306 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 307 | - if (isset(static::$loaded['lang_' . $language])){ |
|
| 308 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 309 | - return; |
|
| 310 | - } |
|
| 311 | - //get the current language |
|
| 312 | - $appLang = self::getAppLang(); |
|
| 313 | - $languageFilePath = null; |
|
| 314 | - //first check if this language is in the module |
|
| 315 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 316 | - $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 317 | - $module = $moduleInfo['module']; |
|
| 318 | - $language = $moduleInfo['language']; |
|
| 319 | - if(! empty($moduleInfo['file'])){ |
|
| 320 | - $file = $moduleInfo['file']; |
|
| 321 | - } |
|
| 322 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 323 | - if ($moduleLanguagePath){ |
|
| 324 | - $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 325 | - $languageFilePath = $moduleLanguagePath; |
|
| 326 | - } |
|
| 327 | - else{ |
|
| 328 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 329 | - } |
|
| 330 | - if (! $languageFilePath){ |
|
| 331 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 332 | - foreach($searchDir as $dir){ |
|
| 333 | - $filePath = $dir . $appLang . DS . $file; |
|
| 334 | - if (file_exists($filePath)){ |
|
| 335 | - $languageFilePath = $filePath; |
|
| 336 | - //already found no need continue |
|
| 337 | - break; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 342 | - self::loadLanguage($languageFilePath, $language); |
|
| 343 | - } |
|
| 293 | + /** |
|
| 294 | + * Load the language |
|
| 295 | + * |
|
| 296 | + * @param string $language the language name to be loaded |
|
| 297 | + * |
|
| 298 | + * @return void |
|
| 299 | + */ |
|
| 300 | + public static function lang($language){ |
|
| 301 | + $logger = static::getLogger(); |
|
| 302 | + $language = str_ireplace('.php', '', $language); |
|
| 303 | + $language = trim($language, '/\\'); |
|
| 304 | + $language = str_ireplace('lang_', '', $language); |
|
| 305 | + $file = 'lang_'.$language.'.php'; |
|
| 306 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 307 | + if (isset(static::$loaded['lang_' . $language])){ |
|
| 308 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 309 | + return; |
|
| 310 | + } |
|
| 311 | + //get the current language |
|
| 312 | + $appLang = self::getAppLang(); |
|
| 313 | + $languageFilePath = null; |
|
| 314 | + //first check if this language is in the module |
|
| 315 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 316 | + $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 317 | + $module = $moduleInfo['module']; |
|
| 318 | + $language = $moduleInfo['language']; |
|
| 319 | + if(! empty($moduleInfo['file'])){ |
|
| 320 | + $file = $moduleInfo['file']; |
|
| 321 | + } |
|
| 322 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 323 | + if ($moduleLanguagePath){ |
|
| 324 | + $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 325 | + $languageFilePath = $moduleLanguagePath; |
|
| 326 | + } |
|
| 327 | + else{ |
|
| 328 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 329 | + } |
|
| 330 | + if (! $languageFilePath){ |
|
| 331 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 332 | + foreach($searchDir as $dir){ |
|
| 333 | + $filePath = $dir . $appLang . DS . $file; |
|
| 334 | + if (file_exists($filePath)){ |
|
| 335 | + $languageFilePath = $filePath; |
|
| 336 | + //already found no need continue |
|
| 337 | + break; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 342 | + self::loadLanguage($languageFilePath, $language); |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * Return the current app language by default will use the value from cookie |
|
| 347 | - * if can not found will use the default value from configuration |
|
| 348 | - * @return string the app language like "en", "fr" |
|
| 349 | - */ |
|
| 350 | - protected static function getAppLang(){ |
|
| 351 | - //determine the current language |
|
| 352 | - $appLang = get_config('default_language'); |
|
| 353 | - //if the language exists in the cookie use it |
|
| 354 | - $cfgKey = get_config('language_cookie_name'); |
|
| 355 | - $objCookie = & class_loader('Cookie'); |
|
| 356 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 357 | - if ($cookieLang){ |
|
| 358 | - $appLang = $cookieLang; |
|
| 359 | - } |
|
| 360 | - return $appLang; |
|
| 361 | - } |
|
| 362 | - /** |
|
| 363 | - * Get the module information for the model and library to load |
|
| 364 | - * @param string $class the full class name like moduleName/className, className, |
|
| 365 | - * @return array the module information |
|
| 366 | - * array( |
|
| 367 | - * 'module'=> 'module_name' |
|
| 368 | - * 'class' => 'class_name' |
|
| 369 | - * ) |
|
| 370 | - */ |
|
| 371 | - protected static function getModuleInfoForModelLibrary($class){ |
|
| 372 | - $module = null; |
|
| 373 | - $obj = & get_instance(); |
|
| 374 | - if (strpos($class, '/') !== false){ |
|
| 375 | - $path = explode('/', $class); |
|
| 376 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 377 | - $module = $path[0]; |
|
| 378 | - $class = ucfirst($path[1]); |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - else{ |
|
| 382 | - $class = ucfirst($class); |
|
| 383 | - } |
|
| 384 | - if (! $module && !empty($obj->moduleName)){ |
|
| 385 | - $module = $obj->moduleName; |
|
| 386 | - } |
|
| 387 | - return array( |
|
| 388 | - 'class' => $class, |
|
| 389 | - 'module' => $module |
|
| 390 | - ); |
|
| 391 | - } |
|
| 345 | + /** |
|
| 346 | + * Return the current app language by default will use the value from cookie |
|
| 347 | + * if can not found will use the default value from configuration |
|
| 348 | + * @return string the app language like "en", "fr" |
|
| 349 | + */ |
|
| 350 | + protected static function getAppLang(){ |
|
| 351 | + //determine the current language |
|
| 352 | + $appLang = get_config('default_language'); |
|
| 353 | + //if the language exists in the cookie use it |
|
| 354 | + $cfgKey = get_config('language_cookie_name'); |
|
| 355 | + $objCookie = & class_loader('Cookie'); |
|
| 356 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 357 | + if ($cookieLang){ |
|
| 358 | + $appLang = $cookieLang; |
|
| 359 | + } |
|
| 360 | + return $appLang; |
|
| 361 | + } |
|
| 362 | + /** |
|
| 363 | + * Get the module information for the model and library to load |
|
| 364 | + * @param string $class the full class name like moduleName/className, className, |
|
| 365 | + * @return array the module information |
|
| 366 | + * array( |
|
| 367 | + * 'module'=> 'module_name' |
|
| 368 | + * 'class' => 'class_name' |
|
| 369 | + * ) |
|
| 370 | + */ |
|
| 371 | + protected static function getModuleInfoForModelLibrary($class){ |
|
| 372 | + $module = null; |
|
| 373 | + $obj = & get_instance(); |
|
| 374 | + if (strpos($class, '/') !== false){ |
|
| 375 | + $path = explode('/', $class); |
|
| 376 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 377 | + $module = $path[0]; |
|
| 378 | + $class = ucfirst($path[1]); |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + else{ |
|
| 382 | + $class = ucfirst($class); |
|
| 383 | + } |
|
| 384 | + if (! $module && !empty($obj->moduleName)){ |
|
| 385 | + $module = $obj->moduleName; |
|
| 386 | + } |
|
| 387 | + return array( |
|
| 388 | + 'class' => $class, |
|
| 389 | + 'module' => $module |
|
| 390 | + ); |
|
| 391 | + } |
|
| 392 | 392 | |
| 393 | - /** |
|
| 394 | - * Get the module information for the function to load |
|
| 395 | - * @param string $function the function name like moduleName/functionName, functionName, |
|
| 396 | - * @return array the module information |
|
| 397 | - * array( |
|
| 398 | - * 'module'=> 'module_name' |
|
| 399 | - * 'function' => 'function' |
|
| 400 | - * 'file' => 'file' |
|
| 401 | - * ) |
|
| 402 | - */ |
|
| 403 | - protected static function getModuleInfoForFunction($function){ |
|
| 404 | - $module = null; |
|
| 405 | - $file = null; |
|
| 406 | - $obj = & get_instance(); |
|
| 407 | - //check if the request class contains module name |
|
| 408 | - if (strpos($function, '/') !== false){ |
|
| 409 | - $path = explode('/', $function); |
|
| 410 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 411 | - $module = $path[0]; |
|
| 412 | - $function = 'function_' . $path[1]; |
|
| 413 | - $file = $path[0] . DS . $function.'.php'; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - if (! $module && !empty($obj->moduleName)){ |
|
| 417 | - $module = $obj->moduleName; |
|
| 418 | - } |
|
| 419 | - return array( |
|
| 420 | - 'function' => $function, |
|
| 421 | - 'module' => $module, |
|
| 422 | - 'file' => $file |
|
| 423 | - ); |
|
| 424 | - } |
|
| 393 | + /** |
|
| 394 | + * Get the module information for the function to load |
|
| 395 | + * @param string $function the function name like moduleName/functionName, functionName, |
|
| 396 | + * @return array the module information |
|
| 397 | + * array( |
|
| 398 | + * 'module'=> 'module_name' |
|
| 399 | + * 'function' => 'function' |
|
| 400 | + * 'file' => 'file' |
|
| 401 | + * ) |
|
| 402 | + */ |
|
| 403 | + protected static function getModuleInfoForFunction($function){ |
|
| 404 | + $module = null; |
|
| 405 | + $file = null; |
|
| 406 | + $obj = & get_instance(); |
|
| 407 | + //check if the request class contains module name |
|
| 408 | + if (strpos($function, '/') !== false){ |
|
| 409 | + $path = explode('/', $function); |
|
| 410 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 411 | + $module = $path[0]; |
|
| 412 | + $function = 'function_' . $path[1]; |
|
| 413 | + $file = $path[0] . DS . $function.'.php'; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + if (! $module && !empty($obj->moduleName)){ |
|
| 417 | + $module = $obj->moduleName; |
|
| 418 | + } |
|
| 419 | + return array( |
|
| 420 | + 'function' => $function, |
|
| 421 | + 'module' => $module, |
|
| 422 | + 'file' => $file |
|
| 423 | + ); |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - /** |
|
| 427 | - * Get the module information for the language to load |
|
| 428 | - * @param string $language the language name like moduleName/languageName, languageName, |
|
| 429 | - * @return array the module information |
|
| 430 | - * array( |
|
| 431 | - * 'module'=> 'module_name' |
|
| 432 | - * 'language' => 'language' |
|
| 433 | - * 'file' => 'file' |
|
| 434 | - * ) |
|
| 435 | - */ |
|
| 436 | - protected static function getModuleInfoForLanguage($language){ |
|
| 437 | - $module = null; |
|
| 438 | - $file = null; |
|
| 439 | - $obj = & get_instance(); |
|
| 440 | - //check if the request class contains module name |
|
| 441 | - if (strpos($language, '/') !== false){ |
|
| 442 | - $path = explode('/', $language); |
|
| 443 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 444 | - $module = $path[0]; |
|
| 445 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 446 | - $file = $path[0] . DS .$language; |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - if (! $module && !empty($obj->moduleName)){ |
|
| 450 | - $module = $obj->moduleName; |
|
| 451 | - } |
|
| 452 | - return array( |
|
| 453 | - 'language' => $language, |
|
| 454 | - 'module' => $module, |
|
| 455 | - 'file' => $file |
|
| 456 | - ); |
|
| 457 | - } |
|
| 426 | + /** |
|
| 427 | + * Get the module information for the language to load |
|
| 428 | + * @param string $language the language name like moduleName/languageName, languageName, |
|
| 429 | + * @return array the module information |
|
| 430 | + * array( |
|
| 431 | + * 'module'=> 'module_name' |
|
| 432 | + * 'language' => 'language' |
|
| 433 | + * 'file' => 'file' |
|
| 434 | + * ) |
|
| 435 | + */ |
|
| 436 | + protected static function getModuleInfoForLanguage($language){ |
|
| 437 | + $module = null; |
|
| 438 | + $file = null; |
|
| 439 | + $obj = & get_instance(); |
|
| 440 | + //check if the request class contains module name |
|
| 441 | + if (strpos($language, '/') !== false){ |
|
| 442 | + $path = explode('/', $language); |
|
| 443 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 444 | + $module = $path[0]; |
|
| 445 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 446 | + $file = $path[0] . DS .$language; |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + if (! $module && !empty($obj->moduleName)){ |
|
| 450 | + $module = $obj->moduleName; |
|
| 451 | + } |
|
| 452 | + return array( |
|
| 453 | + 'language' => $language, |
|
| 454 | + 'module' => $module, |
|
| 455 | + 'file' => $file |
|
| 456 | + ); |
|
| 457 | + } |
|
| 458 | 458 | |
| 459 | 459 | |
| 460 | - /** |
|
| 461 | - * Get the module information for the config to load |
|
| 462 | - * @param string $filename the filename of the configuration file, |
|
| 463 | - * @return array the module information |
|
| 464 | - * array( |
|
| 465 | - * 'module'=> 'module_name' |
|
| 466 | - * 'filename' => 'filename' |
|
| 467 | - * ) |
|
| 468 | - */ |
|
| 469 | - protected static function getModuleInfoForConfig($filename){ |
|
| 470 | - $module = null; |
|
| 471 | - $obj = & get_instance(); |
|
| 472 | - //check if the request class contains module name |
|
| 473 | - if (strpos($filename, '/') !== false){ |
|
| 474 | - $path = explode('/', $filename); |
|
| 475 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 476 | - $module = $path[0]; |
|
| 477 | - $filename = $path[1] . '.php'; |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - if (! $module && !empty($obj->moduleName)){ |
|
| 481 | - $module = $obj->moduleName; |
|
| 482 | - } |
|
| 483 | - return array( |
|
| 484 | - 'filename' => $filename, |
|
| 485 | - 'module' => $module |
|
| 486 | - ); |
|
| 487 | - } |
|
| 460 | + /** |
|
| 461 | + * Get the module information for the config to load |
|
| 462 | + * @param string $filename the filename of the configuration file, |
|
| 463 | + * @return array the module information |
|
| 464 | + * array( |
|
| 465 | + * 'module'=> 'module_name' |
|
| 466 | + * 'filename' => 'filename' |
|
| 467 | + * ) |
|
| 468 | + */ |
|
| 469 | + protected static function getModuleInfoForConfig($filename){ |
|
| 470 | + $module = null; |
|
| 471 | + $obj = & get_instance(); |
|
| 472 | + //check if the request class contains module name |
|
| 473 | + if (strpos($filename, '/') !== false){ |
|
| 474 | + $path = explode('/', $filename); |
|
| 475 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 476 | + $module = $path[0]; |
|
| 477 | + $filename = $path[1] . '.php'; |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + if (! $module && !empty($obj->moduleName)){ |
|
| 481 | + $module = $obj->moduleName; |
|
| 482 | + } |
|
| 483 | + return array( |
|
| 484 | + 'filename' => $filename, |
|
| 485 | + 'module' => $module |
|
| 486 | + ); |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - /** |
|
| 490 | - * Get the name of model or library instance if is null |
|
| 491 | - * @param string $class the class name to determine the instance |
|
| 492 | - * @return string the instance name |
|
| 493 | - */ |
|
| 494 | - protected static function getModelLibraryInstanceName($class){ |
|
| 495 | - //for module |
|
| 496 | - $instance = null; |
|
| 497 | - if (strpos($class, '/') !== false){ |
|
| 498 | - $path = explode('/', $class); |
|
| 499 | - if (isset($path[1])){ |
|
| 500 | - $instance = strtolower($path[1]); |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - else{ |
|
| 504 | - $instance = strtolower($class); |
|
| 505 | - } |
|
| 506 | - return $instance; |
|
| 507 | - } |
|
| 489 | + /** |
|
| 490 | + * Get the name of model or library instance if is null |
|
| 491 | + * @param string $class the class name to determine the instance |
|
| 492 | + * @return string the instance name |
|
| 493 | + */ |
|
| 494 | + protected static function getModelLibraryInstanceName($class){ |
|
| 495 | + //for module |
|
| 496 | + $instance = null; |
|
| 497 | + if (strpos($class, '/') !== false){ |
|
| 498 | + $path = explode('/', $class); |
|
| 499 | + if (isset($path[1])){ |
|
| 500 | + $instance = strtolower($path[1]); |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + else{ |
|
| 504 | + $instance = strtolower($class); |
|
| 505 | + } |
|
| 506 | + return $instance; |
|
| 507 | + } |
|
| 508 | 508 | |
| 509 | - /** |
|
| 510 | - * Get the library file path using the module information |
|
| 511 | - * @param string $class the class name |
|
| 512 | - * @return string|null the library file path otherwise null will be returned |
|
| 513 | - */ |
|
| 514 | - protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 515 | - $logger = static::getLogger(); |
|
| 516 | - $libraryFilePath = null; |
|
| 517 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 518 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 519 | - $module = $moduleInfo['module']; |
|
| 520 | - $class = $moduleInfo['class']; |
|
| 521 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 522 | - if ($moduleLibraryPath){ |
|
| 523 | - $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 524 | - $libraryFilePath = $moduleLibraryPath; |
|
| 525 | - } |
|
| 526 | - else{ |
|
| 527 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 528 | - } |
|
| 529 | - return $libraryFilePath; |
|
| 530 | - } |
|
| 509 | + /** |
|
| 510 | + * Get the library file path using the module information |
|
| 511 | + * @param string $class the class name |
|
| 512 | + * @return string|null the library file path otherwise null will be returned |
|
| 513 | + */ |
|
| 514 | + protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 515 | + $logger = static::getLogger(); |
|
| 516 | + $libraryFilePath = null; |
|
| 517 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 518 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 519 | + $module = $moduleInfo['module']; |
|
| 520 | + $class = $moduleInfo['class']; |
|
| 521 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 522 | + if ($moduleLibraryPath){ |
|
| 523 | + $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 524 | + $libraryFilePath = $moduleLibraryPath; |
|
| 525 | + } |
|
| 526 | + else{ |
|
| 527 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 528 | + } |
|
| 529 | + return $libraryFilePath; |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | - /** |
|
| 533 | - * Load the library |
|
| 534 | - * @param string $libraryFilePath the file path of the library to load |
|
| 535 | - * @param string $class the class name |
|
| 536 | - * @param string $instance the instance |
|
| 537 | - * @param array $params the parameter to use |
|
| 538 | - * @return void |
|
| 539 | - */ |
|
| 540 | - protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 541 | - if ($libraryFilePath){ |
|
| 542 | - $logger = static::getLogger(); |
|
| 543 | - require_once $libraryFilePath; |
|
| 544 | - if (class_exists($class)){ |
|
| 545 | - $c = $params ? new $class($params) : new $class(); |
|
| 546 | - $obj = & get_instance(); |
|
| 547 | - $obj->{$instance} = $c; |
|
| 548 | - static::$loaded[$instance] = $class; |
|
| 549 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 550 | - } |
|
| 551 | - else{ |
|
| 552 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 553 | - } |
|
| 554 | - } |
|
| 555 | - else{ |
|
| 556 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 557 | - } |
|
| 558 | - } |
|
| 532 | + /** |
|
| 533 | + * Load the library |
|
| 534 | + * @param string $libraryFilePath the file path of the library to load |
|
| 535 | + * @param string $class the class name |
|
| 536 | + * @param string $instance the instance |
|
| 537 | + * @param array $params the parameter to use |
|
| 538 | + * @return void |
|
| 539 | + */ |
|
| 540 | + protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 541 | + if ($libraryFilePath){ |
|
| 542 | + $logger = static::getLogger(); |
|
| 543 | + require_once $libraryFilePath; |
|
| 544 | + if (class_exists($class)){ |
|
| 545 | + $c = $params ? new $class($params) : new $class(); |
|
| 546 | + $obj = & get_instance(); |
|
| 547 | + $obj->{$instance} = $c; |
|
| 548 | + static::$loaded[$instance] = $class; |
|
| 549 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 550 | + } |
|
| 551 | + else{ |
|
| 552 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 553 | + } |
|
| 554 | + } |
|
| 555 | + else{ |
|
| 556 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 557 | + } |
|
| 558 | + } |
|
| 559 | 559 | |
| 560 | - /** |
|
| 561 | - * Load the language |
|
| 562 | - * @param string $languageFilePath the file path of the language to load |
|
| 563 | - * @param string $language the language name |
|
| 564 | - * @return void |
|
| 565 | - */ |
|
| 566 | - protected static function loadLanguage($languageFilePath, $language){ |
|
| 567 | - if ($languageFilePath){ |
|
| 568 | - $logger = static::getLogger(); |
|
| 569 | - $lang = array(); |
|
| 570 | - require_once $languageFilePath; |
|
| 571 | - if (! empty($lang) && is_array($lang)){ |
|
| 572 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 573 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 574 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 575 | - $langObj->addLangMessages($lang); |
|
| 576 | - //free the memory |
|
| 577 | - unset($lang); |
|
| 578 | - } |
|
| 579 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 580 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 581 | - } |
|
| 582 | - else{ |
|
| 583 | - show_error('Unable to find language [' . $language . ']'); |
|
| 584 | - } |
|
| 585 | - } |
|
| 560 | + /** |
|
| 561 | + * Load the language |
|
| 562 | + * @param string $languageFilePath the file path of the language to load |
|
| 563 | + * @param string $language the language name |
|
| 564 | + * @return void |
|
| 565 | + */ |
|
| 566 | + protected static function loadLanguage($languageFilePath, $language){ |
|
| 567 | + if ($languageFilePath){ |
|
| 568 | + $logger = static::getLogger(); |
|
| 569 | + $lang = array(); |
|
| 570 | + require_once $languageFilePath; |
|
| 571 | + if (! empty($lang) && is_array($lang)){ |
|
| 572 | + $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 573 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 574 | + $langObj =& class_loader('Lang', 'classes'); |
|
| 575 | + $langObj->addLangMessages($lang); |
|
| 576 | + //free the memory |
|
| 577 | + unset($lang); |
|
| 578 | + } |
|
| 579 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 580 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 581 | + } |
|
| 582 | + else{ |
|
| 583 | + show_error('Unable to find language [' . $language . ']'); |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | 586 | |
| 587 | - /** |
|
| 588 | - * Get all the autoload using the configuration file |
|
| 589 | - * @return array |
|
| 590 | - */ |
|
| 591 | - private function getResourcesFromAutoloadConfig(){ |
|
| 592 | - $autoloads = array(); |
|
| 593 | - $autoloads['config'] = array(); |
|
| 594 | - $autoloads['languages'] = array(); |
|
| 595 | - $autoloads['libraries'] = array(); |
|
| 596 | - $autoloads['models'] = array(); |
|
| 597 | - $autoloads['functions'] = array(); |
|
| 598 | - //loading of the resources from autoload configuration file |
|
| 599 | - if (file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 600 | - $autoload = array(); |
|
| 601 | - require_once CONFIG_PATH . 'autoload.php'; |
|
| 602 | - if (! empty($autoload) && is_array($autoload)){ |
|
| 603 | - $autoloads = array_merge($autoloads, $autoload); |
|
| 604 | - unset($autoload); |
|
| 605 | - } |
|
| 606 | - } |
|
| 607 | - //loading autoload configuration for modules |
|
| 608 | - $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 609 | - if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 610 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 611 | - } |
|
| 612 | - return $autoloads; |
|
| 613 | - } |
|
| 587 | + /** |
|
| 588 | + * Get all the autoload using the configuration file |
|
| 589 | + * @return array |
|
| 590 | + */ |
|
| 591 | + private function getResourcesFromAutoloadConfig(){ |
|
| 592 | + $autoloads = array(); |
|
| 593 | + $autoloads['config'] = array(); |
|
| 594 | + $autoloads['languages'] = array(); |
|
| 595 | + $autoloads['libraries'] = array(); |
|
| 596 | + $autoloads['models'] = array(); |
|
| 597 | + $autoloads['functions'] = array(); |
|
| 598 | + //loading of the resources from autoload configuration file |
|
| 599 | + if (file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 600 | + $autoload = array(); |
|
| 601 | + require_once CONFIG_PATH . 'autoload.php'; |
|
| 602 | + if (! empty($autoload) && is_array($autoload)){ |
|
| 603 | + $autoloads = array_merge($autoloads, $autoload); |
|
| 604 | + unset($autoload); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | + //loading autoload configuration for modules |
|
| 608 | + $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 609 | + if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 610 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 611 | + } |
|
| 612 | + return $autoloads; |
|
| 613 | + } |
|
| 614 | 614 | |
| 615 | - /** |
|
| 616 | - * Load the autoload configuration |
|
| 617 | - * @return void |
|
| 618 | - */ |
|
| 619 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 620 | - $autoloads = array(); |
|
| 621 | - $autoloads['config'] = array(); |
|
| 622 | - $autoloads['languages'] = array(); |
|
| 623 | - $autoloads['libraries'] = array(); |
|
| 624 | - $autoloads['models'] = array(); |
|
| 625 | - $autoloads['functions'] = array(); |
|
| 615 | + /** |
|
| 616 | + * Load the autoload configuration |
|
| 617 | + * @return void |
|
| 618 | + */ |
|
| 619 | + private function loadResourcesFromAutoloadConfig(){ |
|
| 620 | + $autoloads = array(); |
|
| 621 | + $autoloads['config'] = array(); |
|
| 622 | + $autoloads['languages'] = array(); |
|
| 623 | + $autoloads['libraries'] = array(); |
|
| 624 | + $autoloads['models'] = array(); |
|
| 625 | + $autoloads['functions'] = array(); |
|
| 626 | 626 | |
| 627 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 628 | - $autoloads = array_merge($autoloads, $list); |
|
| 627 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 628 | + $autoloads = array_merge($autoloads, $list); |
|
| 629 | 629 | |
| 630 | - //config autoload |
|
| 631 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 630 | + //config autoload |
|
| 631 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 632 | 632 | |
| 633 | - //languages autoload |
|
| 634 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 633 | + //languages autoload |
|
| 634 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 635 | 635 | |
| 636 | - //libraries autoload |
|
| 637 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 636 | + //libraries autoload |
|
| 637 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 638 | 638 | |
| 639 | - //models autoload |
|
| 640 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 639 | + //models autoload |
|
| 640 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 641 | 641 | |
| 642 | - //functions autoload |
|
| 643 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 644 | - } |
|
| 642 | + //functions autoload |
|
| 643 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 644 | + } |
|
| 645 | 645 | |
| 646 | - /** |
|
| 647 | - * Load the resources autoload array |
|
| 648 | - * @param string $method this object method name to call |
|
| 649 | - * @param array $resources the resource to load |
|
| 650 | - * @return void |
|
| 651 | - */ |
|
| 652 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 653 | - foreach ($resources as $name) { |
|
| 654 | - $this->{$method}($name); |
|
| 655 | - } |
|
| 656 | - } |
|
| 657 | - } |
|
| 646 | + /** |
|
| 647 | + * Load the resources autoload array |
|
| 648 | + * @param string $method this object method name to call |
|
| 649 | + * @param array $resources the resource to load |
|
| 650 | + * @return void |
|
| 651 | + */ |
|
| 652 | + private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 653 | + foreach ($resources as $name) { |
|
| 654 | + $this->{$method}($name); |
|
| 655 | + } |
|
| 656 | + } |
|
| 657 | + } |
|
@@ -1,322 +1,322 @@ |
||
| 1 | 1 | <?php |
| 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 | - */ |
|
| 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 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class Form{ |
|
| 28 | + class Form{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Generate the form opened tag |
|
| 32 | - * @param string $path the form action path |
|
| 33 | - * @param array $attributes the additional form attributes |
|
| 34 | - * @param string $method the form method like 'GET', 'POST' |
|
| 35 | - * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | - * @return string the generated form html |
|
| 37 | - */ |
|
| 38 | - public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | - if($path){ |
|
| 40 | - $path = Url::site_url($path); |
|
| 41 | - } |
|
| 42 | - $method = strtoupper($method); |
|
| 43 | - $str = null; |
|
| 44 | - $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | - if(! empty($enctype)){ |
|
| 46 | - $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | - } |
|
| 48 | - if(! isset($attributes['accept-charset'])){ |
|
| 49 | - $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | - } |
|
| 51 | - $str .= attributes_to_string($attributes); |
|
| 52 | - $str .= '>'; |
|
| 53 | - //if CSRF is enabled in the configuration |
|
| 54 | - if(get_config('csrf_enable', false) && $method == 'POST'){ |
|
| 55 | - $csrfValue = Security::generateCSRF(); |
|
| 56 | - $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 57 | - $str .= static::hidden($csrfName, $csrfValue); |
|
| 58 | - } |
|
| 59 | - return $str; |
|
| 60 | - } |
|
| 30 | + /** |
|
| 31 | + * Generate the form opened tag |
|
| 32 | + * @param string $path the form action path |
|
| 33 | + * @param array $attributes the additional form attributes |
|
| 34 | + * @param string $method the form method like 'GET', 'POST' |
|
| 35 | + * @param string $enctype the form enctype like "multipart/form-data" |
|
| 36 | + * @return string the generated form html |
|
| 37 | + */ |
|
| 38 | + public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
|
| 39 | + if($path){ |
|
| 40 | + $path = Url::site_url($path); |
|
| 41 | + } |
|
| 42 | + $method = strtoupper($method); |
|
| 43 | + $str = null; |
|
| 44 | + $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
|
| 45 | + if(! empty($enctype)){ |
|
| 46 | + $str .= ' enctype = "'.$enctype.'" '; |
|
| 47 | + } |
|
| 48 | + if(! isset($attributes['accept-charset'])){ |
|
| 49 | + $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
|
| 50 | + } |
|
| 51 | + $str .= attributes_to_string($attributes); |
|
| 52 | + $str .= '>'; |
|
| 53 | + //if CSRF is enabled in the configuration |
|
| 54 | + if(get_config('csrf_enable', false) && $method == 'POST'){ |
|
| 55 | + $csrfValue = Security::generateCSRF(); |
|
| 56 | + $csrfName = get_config('csrf_key', 'csrf_key'); |
|
| 57 | + $str .= static::hidden($csrfName, $csrfValue); |
|
| 58 | + } |
|
| 59 | + return $str; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Generate the form opened tag for multipart like to send a file |
|
| 64 | - * @see Form::open() for more details |
|
| 65 | - * @return string the generated multipart form html |
|
| 66 | - */ |
|
| 67 | - public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 68 | - return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 69 | - } |
|
| 62 | + /** |
|
| 63 | + * Generate the form opened tag for multipart like to send a file |
|
| 64 | + * @see Form::open() for more details |
|
| 65 | + * @return string the generated multipart form html |
|
| 66 | + */ |
|
| 67 | + public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
|
| 68 | + return self::open($path, $attributes, $method, 'multipart/form-data'); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Generate the form close |
|
| 73 | - * @return string the form close html |
|
| 74 | - */ |
|
| 75 | - public static function close(){ |
|
| 76 | - return '</form>'; |
|
| 77 | - } |
|
| 71 | + /** |
|
| 72 | + * Generate the form close |
|
| 73 | + * @return string the form close html |
|
| 74 | + */ |
|
| 75 | + public static function close(){ |
|
| 76 | + return '</form>'; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Generate the form fieldset & legend |
|
| 81 | - * @param string $legend the legend tag value |
|
| 82 | - * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 83 | - * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 84 | - * @return string the generated fieldset value |
|
| 85 | - */ |
|
| 86 | - public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 87 | - $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 88 | - if($legend){ |
|
| 89 | - $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 90 | - } |
|
| 91 | - return $str; |
|
| 92 | - } |
|
| 79 | + /** |
|
| 80 | + * Generate the form fieldset & legend |
|
| 81 | + * @param string $legend the legend tag value |
|
| 82 | + * @param array $fieldsetAttributes the fieldset additional HTML attributes |
|
| 83 | + * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
|
| 84 | + * @return string the generated fieldset value |
|
| 85 | + */ |
|
| 86 | + public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()){ |
|
| 87 | + $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
|
| 88 | + if($legend){ |
|
| 89 | + $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
|
| 90 | + } |
|
| 91 | + return $str; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Generate the fieldset close tag |
|
| 96 | - * @return string the generated html for fieldset close |
|
| 97 | - */ |
|
| 98 | - public static function fieldsetClose(){ |
|
| 99 | - return '</fieldset>'; |
|
| 100 | - } |
|
| 94 | + /** |
|
| 95 | + * Generate the fieldset close tag |
|
| 96 | + * @return string the generated html for fieldset close |
|
| 97 | + */ |
|
| 98 | + public static function fieldsetClose(){ |
|
| 99 | + return '</fieldset>'; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Get the error message for the given form field name. |
|
| 104 | - * This use the form validation information to get the error information. |
|
| 105 | - * @param string $name the form field name |
|
| 106 | - * @return string the error message if exists and null if not |
|
| 107 | - */ |
|
| 108 | - public static function error($name){ |
|
| 109 | - $return = null; |
|
| 110 | - $obj = & get_instance(); |
|
| 111 | - if(isset($obj->formvalidation)){ |
|
| 112 | - $errors = $obj->formvalidation->returnErrors(); |
|
| 113 | - if(isset($errors[$name])){ |
|
| 114 | - list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 115 | - $return = $errorStart . $errors[$name] . $errorEnd; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - return $return; |
|
| 119 | - } |
|
| 102 | + /** |
|
| 103 | + * Get the error message for the given form field name. |
|
| 104 | + * This use the form validation information to get the error information. |
|
| 105 | + * @param string $name the form field name |
|
| 106 | + * @return string the error message if exists and null if not |
|
| 107 | + */ |
|
| 108 | + public static function error($name){ |
|
| 109 | + $return = null; |
|
| 110 | + $obj = & get_instance(); |
|
| 111 | + if(isset($obj->formvalidation)){ |
|
| 112 | + $errors = $obj->formvalidation->returnErrors(); |
|
| 113 | + if(isset($errors[$name])){ |
|
| 114 | + list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
|
| 115 | + $return = $errorStart . $errors[$name] . $errorEnd; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + return $return; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Get the form field value |
|
| 123 | - * @param string $name the form field name |
|
| 124 | - * @param mixed $default the default value if can not found the given form field name |
|
| 125 | - * @return mixed the form field value if is set, otherwise return the default value. |
|
| 126 | - */ |
|
| 127 | - public static function value($name, $default = null){ |
|
| 128 | - $value = get_instance()->request->query($name); |
|
| 129 | - if(strlen($value) > 0){ |
|
| 130 | - return $value; |
|
| 131 | - } |
|
| 132 | - return $default; |
|
| 133 | - } |
|
| 121 | + /** |
|
| 122 | + * Get the form field value |
|
| 123 | + * @param string $name the form field name |
|
| 124 | + * @param mixed $default the default value if can not found the given form field name |
|
| 125 | + * @return mixed the form field value if is set, otherwise return the default value. |
|
| 126 | + */ |
|
| 127 | + public static function value($name, $default = null){ |
|
| 128 | + $value = get_instance()->request->query($name); |
|
| 129 | + if(strlen($value) > 0){ |
|
| 130 | + return $value; |
|
| 131 | + } |
|
| 132 | + return $default; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Generate the form label html content |
|
| 137 | - * @param string $label the title of the label |
|
| 138 | - * @param string $for the value of the label "for" attribute |
|
| 139 | - * @param array $attributes the additional attributes to be added |
|
| 140 | - * @return string the generated label html content |
|
| 141 | - */ |
|
| 142 | - public static function label($label, $for = '', array $attributes = array()){ |
|
| 143 | - $str = '<label'; |
|
| 144 | - if($for){ |
|
| 145 | - $str .= ' for = "'.$for.'"'; |
|
| 146 | - } |
|
| 147 | - $str .= attributes_to_string($attributes); |
|
| 148 | - $str .= '>'; |
|
| 149 | - $str .= $label.'</label>'; |
|
| 150 | - return $str; |
|
| 151 | - } |
|
| 135 | + /** |
|
| 136 | + * Generate the form label html content |
|
| 137 | + * @param string $label the title of the label |
|
| 138 | + * @param string $for the value of the label "for" attribute |
|
| 139 | + * @param array $attributes the additional attributes to be added |
|
| 140 | + * @return string the generated label html content |
|
| 141 | + */ |
|
| 142 | + public static function label($label, $for = '', array $attributes = array()){ |
|
| 143 | + $str = '<label'; |
|
| 144 | + if($for){ |
|
| 145 | + $str .= ' for = "'.$for.'"'; |
|
| 146 | + } |
|
| 147 | + $str .= attributes_to_string($attributes); |
|
| 148 | + $str .= '>'; |
|
| 149 | + $str .= $label.'</label>'; |
|
| 150 | + return $str; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Generate the form field for input like "text", "email", "password", etc. |
|
| 155 | - * @param string $name the form field name |
|
| 156 | - * @param mixed $value the form field value to be set |
|
| 157 | - * @param array $attributes the additional attributes to be added in the form input |
|
| 158 | - * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 159 | - * @return string the generated form field html content for the input |
|
| 160 | - */ |
|
| 161 | - public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 162 | - $str = null; |
|
| 163 | - $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 164 | - $str .= attributes_to_string($attributes); |
|
| 165 | - $str .= '/>'; |
|
| 166 | - return $str; |
|
| 167 | - } |
|
| 153 | + /** |
|
| 154 | + * Generate the form field for input like "text", "email", "password", etc. |
|
| 155 | + * @param string $name the form field name |
|
| 156 | + * @param mixed $value the form field value to be set |
|
| 157 | + * @param array $attributes the additional attributes to be added in the form input |
|
| 158 | + * @param string $type the type of the form field (password, text, submit, button, etc.) |
|
| 159 | + * @return string the generated form field html content for the input |
|
| 160 | + */ |
|
| 161 | + public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
|
| 162 | + $str = null; |
|
| 163 | + $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
|
| 164 | + $str .= attributes_to_string($attributes); |
|
| 165 | + $str .= '/>'; |
|
| 166 | + return $str; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Generate the form field for "text" |
|
| 171 | - * @see Form::input() for more details |
|
| 172 | - */ |
|
| 173 | - public static function text($name, $value = null, array $attributes = array()){ |
|
| 174 | - return self::input($name, $value, $attributes, 'text'); |
|
| 175 | - } |
|
| 169 | + /** |
|
| 170 | + * Generate the form field for "text" |
|
| 171 | + * @see Form::input() for more details |
|
| 172 | + */ |
|
| 173 | + public static function text($name, $value = null, array $attributes = array()){ |
|
| 174 | + return self::input($name, $value, $attributes, 'text'); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Generate the form field for "password" |
|
| 179 | - * @see Form::input() for more details |
|
| 180 | - */ |
|
| 181 | - public static function password($name, $value = null, array $attributes = array()){ |
|
| 182 | - return self::input($name, $value, $attributes, 'password'); |
|
| 183 | - } |
|
| 177 | + /** |
|
| 178 | + * Generate the form field for "password" |
|
| 179 | + * @see Form::input() for more details |
|
| 180 | + */ |
|
| 181 | + public static function password($name, $value = null, array $attributes = array()){ |
|
| 182 | + return self::input($name, $value, $attributes, 'password'); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Generate the form field for "radio" |
|
| 187 | - * @see Form::input() for more details |
|
| 188 | - */ |
|
| 189 | - public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 190 | - if($checked){ |
|
| 191 | - $attributes['checked'] = true; |
|
| 192 | - } |
|
| 193 | - return self::input($name, $value, $attributes, 'radio'); |
|
| 194 | - } |
|
| 185 | + /** |
|
| 186 | + * Generate the form field for "radio" |
|
| 187 | + * @see Form::input() for more details |
|
| 188 | + */ |
|
| 189 | + public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 190 | + if($checked){ |
|
| 191 | + $attributes['checked'] = true; |
|
| 192 | + } |
|
| 193 | + return self::input($name, $value, $attributes, 'radio'); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Generate the form field for "checkbox" |
|
| 198 | - * @see Form::input() for more details |
|
| 199 | - */ |
|
| 200 | - public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 201 | - if($checked){ |
|
| 202 | - $attributes['checked'] = true; |
|
| 203 | - } |
|
| 204 | - return self::input($name, $value, $attributes, 'checkbox'); |
|
| 205 | - } |
|
| 196 | + /** |
|
| 197 | + * Generate the form field for "checkbox" |
|
| 198 | + * @see Form::input() for more details |
|
| 199 | + */ |
|
| 200 | + public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
|
| 201 | + if($checked){ |
|
| 202 | + $attributes['checked'] = true; |
|
| 203 | + } |
|
| 204 | + return self::input($name, $value, $attributes, 'checkbox'); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * Generate the form field for "number" |
|
| 209 | - * @see Form::input() for more details |
|
| 210 | - */ |
|
| 211 | - public static function number($name, $value = null, array $attributes = array()){ |
|
| 212 | - return self::input($name, $value, $attributes, 'number'); |
|
| 213 | - } |
|
| 207 | + /** |
|
| 208 | + * Generate the form field for "number" |
|
| 209 | + * @see Form::input() for more details |
|
| 210 | + */ |
|
| 211 | + public static function number($name, $value = null, array $attributes = array()){ |
|
| 212 | + return self::input($name, $value, $attributes, 'number'); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Generate the form field for "phone" |
|
| 217 | - * @see Form::input() for more details |
|
| 218 | - */ |
|
| 219 | - public static function phone($name, $value = null, array $attributes = array()){ |
|
| 220 | - return self::input($name, $value, $attributes, 'phone'); |
|
| 221 | - } |
|
| 215 | + /** |
|
| 216 | + * Generate the form field for "phone" |
|
| 217 | + * @see Form::input() for more details |
|
| 218 | + */ |
|
| 219 | + public static function phone($name, $value = null, array $attributes = array()){ |
|
| 220 | + return self::input($name, $value, $attributes, 'phone'); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Generate the form field for "email" |
|
| 225 | - * @see Form::input() for more details |
|
| 226 | - */ |
|
| 227 | - public static function email($name, $value = null, array $attributes = array()){ |
|
| 228 | - return self::input($name, $value, $attributes, 'email'); |
|
| 229 | - } |
|
| 223 | + /** |
|
| 224 | + * Generate the form field for "email" |
|
| 225 | + * @see Form::input() for more details |
|
| 226 | + */ |
|
| 227 | + public static function email($name, $value = null, array $attributes = array()){ |
|
| 228 | + return self::input($name, $value, $attributes, 'email'); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Generate the form field for "search" |
|
| 233 | - * @see Form::input() for more details |
|
| 234 | - */ |
|
| 235 | - public static function search($name, $value = null, array $attributes = array()){ |
|
| 236 | - return self::input($name, $value, $attributes, 'search'); |
|
| 237 | - } |
|
| 231 | + /** |
|
| 232 | + * Generate the form field for "search" |
|
| 233 | + * @see Form::input() for more details |
|
| 234 | + */ |
|
| 235 | + public static function search($name, $value = null, array $attributes = array()){ |
|
| 236 | + return self::input($name, $value, $attributes, 'search'); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Generate the form field for "hidden" |
|
| 241 | - * @see Form::input() for more details |
|
| 242 | - */ |
|
| 243 | - public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 244 | - return self::input($name, $value, $attributes, 'hidden'); |
|
| 245 | - } |
|
| 239 | + /** |
|
| 240 | + * Generate the form field for "hidden" |
|
| 241 | + * @see Form::input() for more details |
|
| 242 | + */ |
|
| 243 | + public static function hidden($name, $value = null, array $attributes = array()){ |
|
| 244 | + return self::input($name, $value, $attributes, 'hidden'); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Generate the form field for "file" |
|
| 249 | - * @see Form::input() for more details |
|
| 250 | - */ |
|
| 251 | - public static function file($name, array $attributes = array()){ |
|
| 252 | - return self::input($name, null, $attributes, 'file'); |
|
| 253 | - } |
|
| 247 | + /** |
|
| 248 | + * Generate the form field for "file" |
|
| 249 | + * @see Form::input() for more details |
|
| 250 | + */ |
|
| 251 | + public static function file($name, array $attributes = array()){ |
|
| 252 | + return self::input($name, null, $attributes, 'file'); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Generate the form field for "button" |
|
| 257 | - * @see Form::input() for more details |
|
| 258 | - */ |
|
| 259 | - public static function button($name, $value = null, array $attributes = array()){ |
|
| 260 | - return self::input($name, $value, $attributes, 'button'); |
|
| 261 | - } |
|
| 255 | + /** |
|
| 256 | + * Generate the form field for "button" |
|
| 257 | + * @see Form::input() for more details |
|
| 258 | + */ |
|
| 259 | + public static function button($name, $value = null, array $attributes = array()){ |
|
| 260 | + return self::input($name, $value, $attributes, 'button'); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Generate the form field for "reset" |
|
| 265 | - * @see Form::input() for more details |
|
| 266 | - */ |
|
| 267 | - public static function reset($name, $value = null, array $attributes = array()){ |
|
| 268 | - return self::input($name, $value, $attributes, 'reset'); |
|
| 269 | - } |
|
| 263 | + /** |
|
| 264 | + * Generate the form field for "reset" |
|
| 265 | + * @see Form::input() for more details |
|
| 266 | + */ |
|
| 267 | + public static function reset($name, $value = null, array $attributes = array()){ |
|
| 268 | + return self::input($name, $value, $attributes, 'reset'); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Generate the form field for "submit" |
|
| 273 | - * @see Form::input() for more details |
|
| 274 | - */ |
|
| 275 | - public static function submit($name, $value = null, array $attributes = array()){ |
|
| 276 | - return self::input($name, $value, $attributes, 'submit'); |
|
| 277 | - } |
|
| 271 | + /** |
|
| 272 | + * Generate the form field for "submit" |
|
| 273 | + * @see Form::input() for more details |
|
| 274 | + */ |
|
| 275 | + public static function submit($name, $value = null, array $attributes = array()){ |
|
| 276 | + return self::input($name, $value, $attributes, 'submit'); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Generate the form field for textarea |
|
| 281 | - * @param string $name the name of the textarea field |
|
| 282 | - * @param string $value the textarea field value |
|
| 283 | - * @param array $attributes the additional attributes to be added |
|
| 284 | - * @return string the generated textarea form html content |
|
| 285 | - */ |
|
| 286 | - public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 287 | - $str = null; |
|
| 288 | - $str .= '<textarea name = "'.$name.'"'; |
|
| 289 | - $str .= attributes_to_string($attributes); |
|
| 290 | - $str .= '>'; |
|
| 291 | - $str .= $value.'</textarea>'; |
|
| 292 | - return $str; |
|
| 293 | - } |
|
| 279 | + /** |
|
| 280 | + * Generate the form field for textarea |
|
| 281 | + * @param string $name the name of the textarea field |
|
| 282 | + * @param string $value the textarea field value |
|
| 283 | + * @param array $attributes the additional attributes to be added |
|
| 284 | + * @return string the generated textarea form html content |
|
| 285 | + */ |
|
| 286 | + public static function textarea($name, $value = '', array $attributes = array()){ |
|
| 287 | + $str = null; |
|
| 288 | + $str .= '<textarea name = "'.$name.'"'; |
|
| 289 | + $str .= attributes_to_string($attributes); |
|
| 290 | + $str .= '>'; |
|
| 291 | + $str .= $value.'</textarea>'; |
|
| 292 | + return $str; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Generate the form field for select |
|
| 297 | - * @param string $name the name of the form field |
|
| 298 | - * @param mixed|array $values the values used to populate the "option" tags |
|
| 299 | - * @param mixed $selected the selected value in the option list |
|
| 300 | - * @param array $attributes the additional attribute to be added |
|
| 301 | - * @return string the generated form field html content for select |
|
| 302 | - */ |
|
| 303 | - public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 304 | - if(! is_array($values)){ |
|
| 305 | - $values = array('' => $values); |
|
| 306 | - } |
|
| 307 | - $str = null; |
|
| 308 | - $str .= '<select name = "'.$name.'"'; |
|
| 309 | - $str .= attributes_to_string($attributes); |
|
| 310 | - $str .= '>'; |
|
| 311 | - foreach($values as $key => $val){ |
|
| 312 | - $select = ''; |
|
| 313 | - if($key == $selected){ |
|
| 314 | - $select = 'selected'; |
|
| 315 | - } |
|
| 316 | - $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 317 | - } |
|
| 318 | - $str .= '</select>'; |
|
| 319 | - return $str; |
|
| 320 | - } |
|
| 295 | + /** |
|
| 296 | + * Generate the form field for select |
|
| 297 | + * @param string $name the name of the form field |
|
| 298 | + * @param mixed|array $values the values used to populate the "option" tags |
|
| 299 | + * @param mixed $selected the selected value in the option list |
|
| 300 | + * @param array $attributes the additional attribute to be added |
|
| 301 | + * @return string the generated form field html content for select |
|
| 302 | + */ |
|
| 303 | + public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
|
| 304 | + if(! is_array($values)){ |
|
| 305 | + $values = array('' => $values); |
|
| 306 | + } |
|
| 307 | + $str = null; |
|
| 308 | + $str .= '<select name = "'.$name.'"'; |
|
| 309 | + $str .= attributes_to_string($attributes); |
|
| 310 | + $str .= '>'; |
|
| 311 | + foreach($values as $key => $val){ |
|
| 312 | + $select = ''; |
|
| 313 | + if($key == $selected){ |
|
| 314 | + $select = 'selected'; |
|
| 315 | + } |
|
| 316 | + $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>'; |
|
| 317 | + } |
|
| 318 | + $str .= '</select>'; |
|
| 319 | + return $str; |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - } |
|
| 322 | + } |
|