@@ -1,441 +1,441 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of request header to send with response |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $headers = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of request header to send with response |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $headers = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The final page content to display to user |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $_pageRender = null; |
|
| 41 | + /** |
|
| 42 | + * The final page content to display to user |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $_pageRender = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The current request URL |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $_currentUrl = null; |
|
| 47 | + /** |
|
| 48 | + * The current request URL |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $_currentUrl = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The current request URL cache key |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_currentUrlCacheKey = null; |
|
| 53 | + /** |
|
| 54 | + * The current request URL cache key |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_currentUrlCacheKey = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Whether we can compress the output using Gzip |
|
| 61 | - * @var boolean |
|
| 62 | - */ |
|
| 63 | - private static $_canCompressOutput = false; |
|
| 59 | + /** |
|
| 60 | + * Whether we can compress the output using Gzip |
|
| 61 | + * @var boolean |
|
| 62 | + */ |
|
| 63 | + private static $_canCompressOutput = false; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Construct new response instance |
|
| 67 | - */ |
|
| 68 | - public function __construct(){ |
|
| 69 | - $currentUrl = ''; |
|
| 70 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 71 | - $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 72 | - } |
|
| 73 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 74 | - $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 75 | - } |
|
| 76 | - $this->_currentUrl = $currentUrl; |
|
| 65 | + /** |
|
| 66 | + * Construct new response instance |
|
| 67 | + */ |
|
| 68 | + public function __construct(){ |
|
| 69 | + $currentUrl = ''; |
|
| 70 | + if (! empty($_SERVER['REQUEST_URI'])){ |
|
| 71 | + $currentUrl = $_SERVER['REQUEST_URI']; |
|
| 72 | + } |
|
| 73 | + if (! empty($_SERVER['QUERY_STRING'])){ |
|
| 74 | + $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
| 75 | + } |
|
| 76 | + $this->_currentUrl = $currentUrl; |
|
| 77 | 77 | |
| 78 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 78 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 79 | 79 | |
| 80 | - self::$_canCompressOutput = get_config('compress_output') |
|
| 81 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 82 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 83 | - && extension_loaded('zlib') |
|
| 84 | - && (bool) ini_get('zlib.output_compression') === false; |
|
| 85 | - } |
|
| 80 | + self::$_canCompressOutput = get_config('compress_output') |
|
| 81 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 82 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 83 | + && extension_loaded('zlib') |
|
| 84 | + && (bool) ini_get('zlib.output_compression') === false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get the logger singleton instance |
|
| 89 | - * @return Log the logger instance |
|
| 90 | - */ |
|
| 91 | - private static function getLogger(){ |
|
| 92 | - if(self::$logger == null){ |
|
| 93 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 94 | - self::$logger[0]->setLogger('Library::Response'); |
|
| 95 | - } |
|
| 96 | - return self::$logger[0]; |
|
| 97 | - } |
|
| 87 | + /** |
|
| 88 | + * Get the logger singleton instance |
|
| 89 | + * @return Log the logger instance |
|
| 90 | + */ |
|
| 91 | + private static function getLogger(){ |
|
| 92 | + if(self::$logger == null){ |
|
| 93 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 94 | + self::$logger[0]->setLogger('Library::Response'); |
|
| 95 | + } |
|
| 96 | + return self::$logger[0]; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Send the HTTP Response headers |
|
| 101 | - * @param integer $httpCode the HTTP status code |
|
| 102 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 103 | - */ |
|
| 104 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 105 | - set_http_status_header($httpCode); |
|
| 106 | - self::setHeaders($headers); |
|
| 107 | - if(! headers_sent()){ |
|
| 108 | - foreach(self::getHeaders() as $key => $value){ |
|
| 109 | - header($key .': '.$value); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 99 | + /** |
|
| 100 | + * Send the HTTP Response headers |
|
| 101 | + * @param integer $httpCode the HTTP status code |
|
| 102 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 103 | + */ |
|
| 104 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 105 | + set_http_status_header($httpCode); |
|
| 106 | + self::setHeaders($headers); |
|
| 107 | + if(! headers_sent()){ |
|
| 108 | + foreach(self::getHeaders() as $key => $value){ |
|
| 109 | + header($key .': '.$value); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Get the list of the headers |
|
| 116 | - * @return array the headers list |
|
| 117 | - */ |
|
| 118 | - public static function getHeaders(){ |
|
| 119 | - return self::$headers; |
|
| 120 | - } |
|
| 114 | + /** |
|
| 115 | + * Get the list of the headers |
|
| 116 | + * @return array the headers list |
|
| 117 | + */ |
|
| 118 | + public static function getHeaders(){ |
|
| 119 | + return self::$headers; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Get the header value for the given name |
|
| 124 | - * @param string $name the header name |
|
| 125 | - * @return string the header value |
|
| 126 | - */ |
|
| 127 | - public static function getHeader($name){ |
|
| 128 | - return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 129 | - } |
|
| 122 | + /** |
|
| 123 | + * Get the header value for the given name |
|
| 124 | + * @param string $name the header name |
|
| 125 | + * @return string the header value |
|
| 126 | + */ |
|
| 127 | + public static function getHeader($name){ |
|
| 128 | + return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Set the header value for the specified name |
|
| 134 | - * @param string $name the header name |
|
| 135 | - * @param string $value the header value to be set |
|
| 136 | - */ |
|
| 137 | - public static function setHeader($name, $value){ |
|
| 138 | - self::$headers[$name] = $value; |
|
| 139 | - } |
|
| 132 | + /** |
|
| 133 | + * Set the header value for the specified name |
|
| 134 | + * @param string $name the header name |
|
| 135 | + * @param string $value the header value to be set |
|
| 136 | + */ |
|
| 137 | + public static function setHeader($name, $value){ |
|
| 138 | + self::$headers[$name] = $value; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Set the headers using array |
|
| 143 | - * @param array $headers the list of the headers to set. |
|
| 144 | - * Note: this will merge with the existing headers |
|
| 145 | - */ |
|
| 146 | - public static function setHeaders(array $headers){ |
|
| 147 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 148 | - } |
|
| 141 | + /** |
|
| 142 | + * Set the headers using array |
|
| 143 | + * @param array $headers the list of the headers to set. |
|
| 144 | + * Note: this will merge with the existing headers |
|
| 145 | + */ |
|
| 146 | + public static function setHeaders(array $headers){ |
|
| 147 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Redirect user in the specified page |
|
| 152 | - * @param string $path the URL or URI to be redirect to |
|
| 153 | - */ |
|
| 154 | - public static function redirect($path = ''){ |
|
| 155 | - $logger = self::getLogger(); |
|
| 156 | - $url = Url::site_url($path); |
|
| 157 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 158 | - if(! headers_sent()){ |
|
| 159 | - header('Location: '.$url); |
|
| 160 | - exit; |
|
| 161 | - } |
|
| 162 | - else{ |
|
| 163 | - echo '<script> |
|
| 150 | + /** |
|
| 151 | + * Redirect user in the specified page |
|
| 152 | + * @param string $path the URL or URI to be redirect to |
|
| 153 | + */ |
|
| 154 | + public static function redirect($path = ''){ |
|
| 155 | + $logger = self::getLogger(); |
|
| 156 | + $url = Url::site_url($path); |
|
| 157 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 158 | + if(! headers_sent()){ |
|
| 159 | + header('Location: '.$url); |
|
| 160 | + exit; |
|
| 161 | + } |
|
| 162 | + else{ |
|
| 163 | + echo '<script> |
|
| 164 | 164 | location.href = "'.$url.'"; |
| 165 | 165 | </script>'; |
| 166 | - } |
|
| 167 | - } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Render the view to display later or return the content |
|
| 171 | - * @param string $view the view name or path |
|
| 172 | - * @param array|object $data the variable data to use in the view |
|
| 173 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 174 | - * @return void|string if $return is true will return the view content otherwise |
|
| 175 | - * will display the view content. |
|
| 176 | - */ |
|
| 177 | - public function render($view, $data = null, $return = false){ |
|
| 178 | - $logger = self::getLogger(); |
|
| 179 | - //convert data to an array |
|
| 180 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 181 | - $view = str_ireplace('.php', '', $view); |
|
| 182 | - $view = trim($view, '/\\'); |
|
| 183 | - $viewFile = $view . '.php'; |
|
| 184 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 169 | + /** |
|
| 170 | + * Render the view to display later or return the content |
|
| 171 | + * @param string $view the view name or path |
|
| 172 | + * @param array|object $data the variable data to use in the view |
|
| 173 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 174 | + * @return void|string if $return is true will return the view content otherwise |
|
| 175 | + * will display the view content. |
|
| 176 | + */ |
|
| 177 | + public function render($view, $data = null, $return = false){ |
|
| 178 | + $logger = self::getLogger(); |
|
| 179 | + //convert data to an array |
|
| 180 | + $data = ! is_array($data) ? (array) $data : $data; |
|
| 181 | + $view = str_ireplace('.php', '', $view); |
|
| 182 | + $view = trim($view, '/\\'); |
|
| 183 | + $viewFile = $view . '.php'; |
|
| 184 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 185 | 185 | |
| 186 | - //super instance |
|
| 187 | - $obj = & get_instance(); |
|
| 186 | + //super instance |
|
| 187 | + $obj = & get_instance(); |
|
| 188 | 188 | |
| 189 | - if(Module::hasModule()){ |
|
| 190 | - //check in module first |
|
| 191 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 192 | - $mod = null; |
|
| 193 | - //check if the request class contains module name |
|
| 194 | - if(strpos($view, '/') !== false){ |
|
| 195 | - $viewPath = explode('/', $view); |
|
| 196 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 197 | - $mod = $viewPath[0]; |
|
| 198 | - array_shift($viewPath); |
|
| 199 | - $view = implode('/', $viewPath); |
|
| 200 | - $viewFile = $view . '.php'; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 204 | - $mod = $obj->moduleName; |
|
| 205 | - } |
|
| 206 | - if($mod){ |
|
| 207 | - $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 208 | - if($moduleViewPath){ |
|
| 209 | - $path = $moduleViewPath; |
|
| 210 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | - } |
|
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - else{ |
|
| 217 | - $logger->info('The current request does not use module using the default location.'); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 221 | - $found = false; |
|
| 222 | - if(file_exists($path)){ |
|
| 223 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 224 | - if(! isset($this->{$key})){ |
|
| 225 | - $this->{$key} = & $obj->{$key}; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - ob_start(); |
|
| 229 | - extract($data); |
|
| 230 | - //need use require() instead of require_once because can load this view many time |
|
| 231 | - require $path; |
|
| 232 | - $content = ob_get_clean(); |
|
| 233 | - if($return){ |
|
| 234 | - return $content; |
|
| 235 | - } |
|
| 236 | - $this->_pageRender .= $content; |
|
| 237 | - $found = true; |
|
| 238 | - } |
|
| 239 | - if(! $found){ |
|
| 240 | - show_error('Unable to find view [' .$view . ']'); |
|
| 241 | - } |
|
| 242 | - } |
|
| 189 | + if(Module::hasModule()){ |
|
| 190 | + //check in module first |
|
| 191 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 192 | + $mod = null; |
|
| 193 | + //check if the request class contains module name |
|
| 194 | + if(strpos($view, '/') !== false){ |
|
| 195 | + $viewPath = explode('/', $view); |
|
| 196 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 197 | + $mod = $viewPath[0]; |
|
| 198 | + array_shift($viewPath); |
|
| 199 | + $view = implode('/', $viewPath); |
|
| 200 | + $viewFile = $view . '.php'; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + if(! $mod && !empty($obj->moduleName)){ |
|
| 204 | + $mod = $obj->moduleName; |
|
| 205 | + } |
|
| 206 | + if($mod){ |
|
| 207 | + $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 208 | + if($moduleViewPath){ |
|
| 209 | + $path = $moduleViewPath; |
|
| 210 | + $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 211 | + } |
|
| 212 | + else{ |
|
| 213 | + $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + else{ |
|
| 217 | + $logger->info('The current request does not use module using the default location.'); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 221 | + $found = false; |
|
| 222 | + if(file_exists($path)){ |
|
| 223 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 224 | + if(! isset($this->{$key})){ |
|
| 225 | + $this->{$key} = & $obj->{$key}; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + ob_start(); |
|
| 229 | + extract($data); |
|
| 230 | + //need use require() instead of require_once because can load this view many time |
|
| 231 | + require $path; |
|
| 232 | + $content = ob_get_clean(); |
|
| 233 | + if($return){ |
|
| 234 | + return $content; |
|
| 235 | + } |
|
| 236 | + $this->_pageRender .= $content; |
|
| 237 | + $found = true; |
|
| 238 | + } |
|
| 239 | + if(! $found){ |
|
| 240 | + show_error('Unable to find view [' .$view . ']'); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Send the final page output to user |
|
| 246 | - */ |
|
| 247 | - public function renderFinalPage(){ |
|
| 248 | - $logger = self::getLogger(); |
|
| 249 | - $obj = & get_instance(); |
|
| 250 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 251 | - $dispatcher = $obj->eventdispatcher; |
|
| 252 | - $content = $this->_pageRender; |
|
| 253 | - if(! $content){ |
|
| 254 | - $logger->warning('The final view content is empty.'); |
|
| 255 | - return; |
|
| 256 | - } |
|
| 257 | - //dispatch |
|
| 258 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 259 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 260 | - if(empty($content)){ |
|
| 261 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 262 | - } |
|
| 244 | + /** |
|
| 245 | + * Send the final page output to user |
|
| 246 | + */ |
|
| 247 | + public function renderFinalPage(){ |
|
| 248 | + $logger = self::getLogger(); |
|
| 249 | + $obj = & get_instance(); |
|
| 250 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 251 | + $dispatcher = $obj->eventdispatcher; |
|
| 252 | + $content = $this->_pageRender; |
|
| 253 | + if(! $content){ |
|
| 254 | + $logger->warning('The final view content is empty.'); |
|
| 255 | + return; |
|
| 256 | + } |
|
| 257 | + //dispatch |
|
| 258 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 259 | + $content = ! empty($event->payload) ? $event->payload : null; |
|
| 260 | + if(empty($content)){ |
|
| 261 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - //check whether need save the page into cache. |
|
| 265 | - if($cachePageStatus){ |
|
| 266 | - //current page URL |
|
| 267 | - $url = $this->_currentUrl; |
|
| 268 | - //Cache view Time to live in second |
|
| 269 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 270 | - if (!empty($obj->view_cache_ttl)){ |
|
| 271 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 272 | - } |
|
| 273 | - //the cache handler instance |
|
| 274 | - $cacheInstance = $obj->cache; |
|
| 275 | - //the current page cache key for identification |
|
| 276 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 264 | + //check whether need save the page into cache. |
|
| 265 | + if($cachePageStatus){ |
|
| 266 | + //current page URL |
|
| 267 | + $url = $this->_currentUrl; |
|
| 268 | + //Cache view Time to live in second |
|
| 269 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 270 | + if (!empty($obj->view_cache_ttl)){ |
|
| 271 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 272 | + } |
|
| 273 | + //the cache handler instance |
|
| 274 | + $cacheInstance = $obj->cache; |
|
| 275 | + //the current page cache key for identification |
|
| 276 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 277 | 277 | |
| 278 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 279 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 278 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 279 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 280 | 280 | |
| 281 | - //get the cache information to prepare header to send to browser |
|
| 282 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 283 | - if($cacheInfo){ |
|
| 284 | - $lastModified = $cacheInfo['mtime']; |
|
| 285 | - $expire = $cacheInfo['expire']; |
|
| 286 | - $maxAge = $expire - time(); |
|
| 287 | - self::setHeader('Pragma', 'public'); |
|
| 288 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 289 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 290 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 291 | - } |
|
| 292 | - } |
|
| 281 | + //get the cache information to prepare header to send to browser |
|
| 282 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 283 | + if($cacheInfo){ |
|
| 284 | + $lastModified = $cacheInfo['mtime']; |
|
| 285 | + $expire = $cacheInfo['expire']; |
|
| 286 | + $maxAge = $expire - time(); |
|
| 287 | + self::setHeader('Pragma', 'public'); |
|
| 288 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 289 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 290 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - // Parse out the elapsed time and memory usage, |
|
| 295 | - // then swap the pseudo-variables with the data |
|
| 296 | - $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 297 | - $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 298 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 294 | + // Parse out the elapsed time and memory usage, |
|
| 295 | + // then swap the pseudo-variables with the data |
|
| 296 | + $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 297 | + $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 298 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 299 | 299 | |
| 300 | - //compress the output if is available |
|
| 301 | - $type = null; |
|
| 302 | - if (self::$_canCompressOutput){ |
|
| 303 | - $type = 'ob_gzhandler'; |
|
| 304 | - } |
|
| 305 | - ob_start($type); |
|
| 306 | - self::sendHeaders(200); |
|
| 307 | - echo $content; |
|
| 308 | - ob_end_flush(); |
|
| 309 | - } |
|
| 300 | + //compress the output if is available |
|
| 301 | + $type = null; |
|
| 302 | + if (self::$_canCompressOutput){ |
|
| 303 | + $type = 'ob_gzhandler'; |
|
| 304 | + } |
|
| 305 | + ob_start($type); |
|
| 306 | + self::sendHeaders(200); |
|
| 307 | + echo $content; |
|
| 308 | + ob_end_flush(); |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Send the final page output to user if is cached |
|
| 313 | - */ |
|
| 314 | - public function renderFinalPageFromCache(&$cache){ |
|
| 315 | - $logger = self::getLogger(); |
|
| 316 | - $url = $this->_currentUrl; |
|
| 317 | - //the current page cache key for identification |
|
| 318 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 311 | + /** |
|
| 312 | + * Send the final page output to user if is cached |
|
| 313 | + */ |
|
| 314 | + public function renderFinalPageFromCache(&$cache){ |
|
| 315 | + $logger = self::getLogger(); |
|
| 316 | + $url = $this->_currentUrl; |
|
| 317 | + //the current page cache key for identification |
|
| 318 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 319 | 319 | |
| 320 | - $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 321 | - //get the cache information to prepare header to send to browser |
|
| 322 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 323 | - if($cacheInfo){ |
|
| 324 | - $lastModified = $cacheInfo['mtime']; |
|
| 325 | - $expire = $cacheInfo['expire']; |
|
| 326 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 327 | - self::setHeader('Pragma', 'public'); |
|
| 328 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 329 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 330 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 331 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 332 | - $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 333 | - self::sendHeaders(304); |
|
| 334 | - return; |
|
| 335 | - } |
|
| 336 | - $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 337 | - self::sendHeaders(200); |
|
| 338 | - //get the cache content |
|
| 339 | - $content = $cache->get($pageCacheKey); |
|
| 340 | - if($content){ |
|
| 341 | - $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 342 | - //load benchmark class |
|
| 343 | - $benchmark = & class_loader('Benchmark'); |
|
| 320 | + $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 321 | + //get the cache information to prepare header to send to browser |
|
| 322 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 323 | + if($cacheInfo){ |
|
| 324 | + $lastModified = $cacheInfo['mtime']; |
|
| 325 | + $expire = $cacheInfo['expire']; |
|
| 326 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 327 | + self::setHeader('Pragma', 'public'); |
|
| 328 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 329 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 330 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 331 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 332 | + $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 333 | + self::sendHeaders(304); |
|
| 334 | + return; |
|
| 335 | + } |
|
| 336 | + $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 337 | + self::sendHeaders(200); |
|
| 338 | + //get the cache content |
|
| 339 | + $content = $cache->get($pageCacheKey); |
|
| 340 | + if($content){ |
|
| 341 | + $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 342 | + //load benchmark class |
|
| 343 | + $benchmark = & class_loader('Benchmark'); |
|
| 344 | 344 | |
| 345 | - // Parse out the elapsed time and memory usage, |
|
| 346 | - // then swap the pseudo-variables with the data |
|
| 347 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 348 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 349 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 345 | + // Parse out the elapsed time and memory usage, |
|
| 346 | + // then swap the pseudo-variables with the data |
|
| 347 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 348 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 349 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 350 | 350 | |
| 351 | - ///display the final output |
|
| 352 | - //compress the output if is available |
|
| 353 | - $type = null; |
|
| 354 | - if (self::$_canCompressOutput){ |
|
| 355 | - $type = 'ob_gzhandler'; |
|
| 356 | - } |
|
| 357 | - ob_start($type); |
|
| 358 | - echo $content; |
|
| 359 | - ob_end_flush(); |
|
| 360 | - return; |
|
| 361 | - } |
|
| 362 | - else{ |
|
| 363 | - $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 364 | - $cache->delete($pageCacheKey); |
|
| 365 | - } |
|
| 351 | + ///display the final output |
|
| 352 | + //compress the output if is available |
|
| 353 | + $type = null; |
|
| 354 | + if (self::$_canCompressOutput){ |
|
| 355 | + $type = 'ob_gzhandler'; |
|
| 356 | + } |
|
| 357 | + ob_start($type); |
|
| 358 | + echo $content; |
|
| 359 | + ob_end_flush(); |
|
| 360 | + return; |
|
| 361 | + } |
|
| 362 | + else{ |
|
| 363 | + $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 364 | + $cache->delete($pageCacheKey); |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - } |
|
| 368 | - } |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - /** |
|
| 371 | - * Get the final page to be rendered |
|
| 372 | - * @return string |
|
| 373 | - */ |
|
| 374 | - public function getFinalPageRendered(){ |
|
| 375 | - return $this->_pageRender; |
|
| 376 | - } |
|
| 370 | + /** |
|
| 371 | + * Get the final page to be rendered |
|
| 372 | + * @return string |
|
| 373 | + */ |
|
| 374 | + public function getFinalPageRendered(){ |
|
| 375 | + return $this->_pageRender; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * Send the HTTP 404 error if can not found the |
|
| 380 | - * routing information for the current request |
|
| 381 | - */ |
|
| 382 | - public static function send404(){ |
|
| 383 | - /********* for logs **************/ |
|
| 384 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 385 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 386 | - $logger = self::getLogger(); |
|
| 387 | - $request =& class_loader('Request', 'classes'); |
|
| 388 | - $userAgent =& class_loader('Browser'); |
|
| 389 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 378 | + /** |
|
| 379 | + * Send the HTTP 404 error if can not found the |
|
| 380 | + * routing information for the current request |
|
| 381 | + */ |
|
| 382 | + public static function send404(){ |
|
| 383 | + /********* for logs **************/ |
|
| 384 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 385 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 386 | + $logger = self::getLogger(); |
|
| 387 | + $request =& class_loader('Request', 'classes'); |
|
| 388 | + $userAgent =& class_loader('Browser'); |
|
| 389 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 390 | 390 | |
| 391 | - //here can't use Loader::functions just include the helper manually |
|
| 392 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 391 | + //here can't use Loader::functions just include the helper manually |
|
| 392 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 393 | 393 | |
| 394 | - $str = '[404 page not found] : '; |
|
| 395 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 396 | - $logger->error($str); |
|
| 397 | - /***********************************/ |
|
| 398 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 399 | - if(file_exists($path)){ |
|
| 400 | - //compress the output if is available |
|
| 401 | - $type = null; |
|
| 402 | - if (self::$_canCompressOutput){ |
|
| 403 | - $type = 'ob_gzhandler'; |
|
| 404 | - } |
|
| 405 | - ob_start($type); |
|
| 406 | - require_once $path; |
|
| 407 | - $output = ob_get_clean(); |
|
| 408 | - self::sendHeaders(404); |
|
| 409 | - echo $output; |
|
| 410 | - } |
|
| 411 | - else{ |
|
| 412 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 413 | - } |
|
| 414 | - } |
|
| 394 | + $str = '[404 page not found] : '; |
|
| 395 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 396 | + $logger->error($str); |
|
| 397 | + /***********************************/ |
|
| 398 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 399 | + if(file_exists($path)){ |
|
| 400 | + //compress the output if is available |
|
| 401 | + $type = null; |
|
| 402 | + if (self::$_canCompressOutput){ |
|
| 403 | + $type = 'ob_gzhandler'; |
|
| 404 | + } |
|
| 405 | + ob_start($type); |
|
| 406 | + require_once $path; |
|
| 407 | + $output = ob_get_clean(); |
|
| 408 | + self::sendHeaders(404); |
|
| 409 | + echo $output; |
|
| 410 | + } |
|
| 411 | + else{ |
|
| 412 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Display the error to user |
|
| 418 | - * @param array $data the error information |
|
| 419 | - */ |
|
| 420 | - public static function sendError(array $data = array()){ |
|
| 421 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 422 | - if(file_exists($path)){ |
|
| 423 | - //compress the output if is available |
|
| 424 | - $type = null; |
|
| 425 | - if (self::$_canCompressOutput){ |
|
| 426 | - $type = 'ob_gzhandler'; |
|
| 427 | - } |
|
| 428 | - ob_start($type); |
|
| 429 | - extract($data); |
|
| 430 | - require_once $path; |
|
| 431 | - $output = ob_get_clean(); |
|
| 432 | - self::sendHeaders(503); |
|
| 433 | - echo $output; |
|
| 434 | - } |
|
| 435 | - else{ |
|
| 436 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 437 | - set_http_status_header(503); |
|
| 438 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - } |
|
| 416 | + /** |
|
| 417 | + * Display the error to user |
|
| 418 | + * @param array $data the error information |
|
| 419 | + */ |
|
| 420 | + public static function sendError(array $data = array()){ |
|
| 421 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 422 | + if(file_exists($path)){ |
|
| 423 | + //compress the output if is available |
|
| 424 | + $type = null; |
|
| 425 | + if (self::$_canCompressOutput){ |
|
| 426 | + $type = 'ob_gzhandler'; |
|
| 427 | + } |
|
| 428 | + ob_start($type); |
|
| 429 | + extract($data); |
|
| 430 | + require_once $path; |
|
| 431 | + $output = ob_get_clean(); |
|
| 432 | + self::sendHeaders(503); |
|
| 433 | + echo $output; |
|
| 434 | + } |
|
| 435 | + else{ |
|
| 436 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 437 | + set_http_status_header(503); |
|
| 438 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + } |
|
@@ -22,10 +22,10 @@ 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 | 27 | |
| 28 | - class FormValidation{ |
|
| 28 | + class FormValidation{ |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * The form validation status |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
| 62 | 62 | |
| 63 | - /** |
|
| 63 | + /** |
|
| 64 | 64 | * Indicated if need force the validation to be failed |
| 65 | 65 | * @var boolean |
| 66 | 66 | */ |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $this->logger =& class_loader('Log', 'classes'); |
| 101 | 101 | $this->logger->setLogger('Library::FormValidation'); |
| 102 | 102 | |
| 103 | - //Load form validation language message |
|
| 103 | + //Load form validation language message |
|
| 104 | 104 | Loader::lang('form_validation'); |
| 105 | 105 | $obj = & get_instance(); |
| 106 | 106 | $this->_errorsMessages = array( |
@@ -162,13 +162,13 @@ discard block |
||
| 162 | 162 | /** |
| 163 | 163 | * Set the form validation data |
| 164 | 164 | * @param array $data the values to be validated |
| 165 | - * |
|
| 165 | + * |
|
| 166 | 166 | * @return FormValidation Current instance of object. |
| 167 | 167 | */ |
| 168 | 168 | public function setData(array $data){ |
| 169 | 169 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
| 170 | 170 | $this->data = $data; |
| 171 | - return $this; |
|
| 171 | + return $this; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | return $this->data; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the validation function name to validate a rule |
|
| 184 | - * |
|
| 185 | - * @return string the function name |
|
| 186 | - */ |
|
| 182 | + /** |
|
| 183 | + * Get the validation function name to validate a rule |
|
| 184 | + * |
|
| 185 | + * @return string the function name |
|
| 186 | + */ |
|
| 187 | 187 | protected function _toCallCase($funcName, $prefix='_validate') { |
| 188 | 188 | $funcName = strtolower($funcName); |
| 189 | 189 | $finalFuncName = $prefix; |
@@ -253,12 +253,12 @@ discard block |
||
| 253 | 253 | $this->_forceFail = false; |
| 254 | 254 | |
| 255 | 255 | foreach ($this->getData() as $inputName => $inputVal) { |
| 256 | - if(is_array($this->data[$inputName])){ |
|
| 257 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 258 | - } |
|
| 259 | - else{ |
|
| 260 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 261 | - } |
|
| 256 | + if(is_array($this->data[$inputName])){ |
|
| 257 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 258 | + } |
|
| 259 | + else{ |
|
| 260 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | if (array_key_exists($inputName, $this->_rules)) { |
| 264 | 264 | foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @param string $inputField Name of the field or the data key to add a rule to |
| 276 | 276 | * @param string $ruleSets PIPE seperated string of rules |
| 277 | - * |
|
| 277 | + * |
|
| 278 | 278 | * @return FormValidation Current instance of object. |
| 279 | 279 | */ |
| 280 | 280 | public function setRule($inputField, $inputLabel, $ruleSets) { |
@@ -288,8 +288,8 @@ discard block |
||
| 288 | 288 | * Takes an array of rules and uses setRule() to set them, accepts an array |
| 289 | 289 | * of rule names rather than a pipe-delimited string as well. |
| 290 | 290 | * @param array $ruleSets |
| 291 | - * |
|
| 292 | - * @return FormValidation Current instance of object. |
|
| 291 | + * |
|
| 292 | + * @return FormValidation Current instance of object. |
|
| 293 | 293 | */ |
| 294 | 294 | public function setRules(array $ruleSets) { |
| 295 | 295 | foreach ($ruleSets as $ruleSet) { |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | * @param string $start Before block of errors gets displayed, HTML allowed. |
| 312 | 312 | * @param string $end After the block of errors gets displayed, HTML allowed. |
| 313 | 313 | * |
| 314 | - * @return FormValidation Current instance of object. |
|
| 314 | + * @return FormValidation Current instance of object. |
|
| 315 | 315 | */ |
| 316 | 316 | public function setErrorsDelimiter($start, $end) { |
| 317 | 317 | $this->_allErrorsDelimiter[0] = $start; |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | * @param string $start Displayed before each error. |
| 327 | 327 | * @param string $end Displayed after each error. |
| 328 | 328 | * |
| 329 | - * @return FormValidation Current instance of object. |
|
| 329 | + * @return FormValidation Current instance of object. |
|
| 330 | 330 | */ |
| 331 | 331 | public function setErrorDelimiter($start, $end) { |
| 332 | 332 | $this->_eachErrorDelimiter[0] = $start; |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | return $this; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Get the each errors delimiters |
|
| 339 | - * |
|
| 340 | - * @return array |
|
| 341 | - */ |
|
| 342 | - public function getErrorDelimiter() { |
|
| 337 | + /** |
|
| 338 | + * Get the each errors delimiters |
|
| 339 | + * |
|
| 340 | + * @return array |
|
| 341 | + */ |
|
| 342 | + public function getErrorDelimiter() { |
|
| 343 | 343 | return $this->_eachErrorDelimiter; |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | - /** |
|
| 347 | - * Get the all errors delimiters |
|
| 348 | - * |
|
| 349 | - * @return array |
|
| 350 | - */ |
|
| 351 | - public function getErrorsDelimiter() { |
|
| 346 | + /** |
|
| 347 | + * Get the all errors delimiters |
|
| 348 | + * |
|
| 349 | + * @return array |
|
| 350 | + */ |
|
| 351 | + public function getErrorsDelimiter() { |
|
| 352 | 352 | return $this->_allErrorsDelimiter; |
| 353 | 353 | } |
| 354 | 354 | |
@@ -386,7 +386,7 @@ discard block |
||
| 386 | 386 | * |
| 387 | 387 | * @param string $inputName The form input name or data key |
| 388 | 388 | * @param string $errorMessage Error to display |
| 389 | - * |
|
| 389 | + * |
|
| 390 | 390 | * @return formValidation Current instance of the object |
| 391 | 391 | */ |
| 392 | 392 | public function setCustomError($inputName, $errorMessage) { |
@@ -423,17 +423,17 @@ discard block |
||
| 423 | 423 | * |
| 424 | 424 | * @param boolean $limit number of error to display or return |
| 425 | 425 | * @param boolean $echo Whether or not the values are to be returned or displayed |
| 426 | - * |
|
| 426 | + * |
|
| 427 | 427 | * @return string Errors formatted for output |
| 428 | 428 | */ |
| 429 | 429 | public function displayErrors($limit = null, $echo = true) { |
| 430 | 430 | list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
| 431 | 431 | list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
| 432 | 432 | $errorOutput = $errorsStart; |
| 433 | - $i = 0; |
|
| 433 | + $i = 0; |
|
| 434 | 434 | if (!empty($this->_errors)) { |
| 435 | 435 | foreach ($this->_errors as $fieldName => $error) { |
| 436 | - if ($i === $limit) { |
|
| 436 | + if ($i === $limit) { |
|
| 437 | 437 | break; |
| 438 | 438 | } |
| 439 | 439 | $errorOutput .= $errorStart; |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | * Breaks up a PIPE seperated string of rules, and puts them into an array. |
| 462 | 462 | * |
| 463 | 463 | * @param string $ruleString String to be parsed. |
| 464 | - * |
|
| 464 | + * |
|
| 465 | 465 | * @return array Array of each value in original string. |
| 466 | 466 | */ |
| 467 | 467 | protected function _parseRuleString($ruleString) { |
@@ -474,10 +474,10 @@ discard block |
||
| 474 | 474 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
| 475 | 475 | preg_match($rule, $ruleString, $regexRule); |
| 476 | 476 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
| 477 | - if(!empty($regexRule[0])){ |
|
| 478 | - $ruleSets[] = $regexRule[0]; |
|
| 479 | - } |
|
| 480 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 477 | + if(!empty($regexRule[0])){ |
|
| 478 | + $ruleSets[] = $regexRule[0]; |
|
| 479 | + } |
|
| 480 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 481 | 481 | foreach ($ruleStringRegex as $rule) { |
| 482 | 482 | $rule = trim($rule); |
| 483 | 483 | if($rule){ |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | } else { |
| 494 | 494 | $ruleSets[] = $ruleString; |
| 495 | 495 | } |
| 496 | - } |
|
| 496 | + } |
|
| 497 | 497 | return $ruleSets; |
| 498 | 498 | } |
| 499 | 499 | |
@@ -501,7 +501,7 @@ discard block |
||
| 501 | 501 | * Returns whether or not a field obtains the rule "required". |
| 502 | 502 | * |
| 503 | 503 | * @param string $fieldName Field to check if required. |
| 504 | - * |
|
| 504 | + * |
|
| 505 | 505 | * @return boolean Whether or not the field is required. |
| 506 | 506 | */ |
| 507 | 507 | protected function _fieldIsRequired($fieldName) { |
@@ -536,13 +536,13 @@ discard block |
||
| 536 | 536 | return; |
| 537 | 537 | } |
| 538 | 538 | |
| 539 | - /** |
|
| 540 | - * Set error for the given field or key |
|
| 541 | - * |
|
| 542 | - * @param string $inputName the input or key name |
|
| 543 | - * @param string $ruleName the rule name |
|
| 544 | - * @param array|string $replacements |
|
| 545 | - */ |
|
| 539 | + /** |
|
| 540 | + * Set error for the given field or key |
|
| 541 | + * |
|
| 542 | + * @param string $inputName the input or key name |
|
| 543 | + * @param string $ruleName the rule name |
|
| 544 | + * @param array|string $replacements |
|
| 545 | + */ |
|
| 546 | 546 | protected function _setError($inputName, $ruleName, $replacements = array()) { |
| 547 | 547 | $rulePhraseKeyParts = explode(',', $ruleName); |
| 548 | 548 | $rulePhrase = null; |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | } |
| 560 | 560 | // Type cast to array in case it's a string |
| 561 | 561 | $replacements = (array) $replacements; |
| 562 | - $replacementCount = count($replacements); |
|
| 562 | + $replacementCount = count($replacements); |
|
| 563 | 563 | for ($i = 1; $i <= $replacementCount; $i++) { |
| 564 | 564 | $key = $i - 1; |
| 565 | 565 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
@@ -577,11 +577,11 @@ discard block |
||
| 577 | 577 | * |
| 578 | 578 | * @param type $inputArg |
| 579 | 579 | * @param string $callbackFunc |
| 580 | - * |
|
| 580 | + * |
|
| 581 | 581 | * @return mixed |
| 582 | 582 | */ |
| 583 | 583 | protected function _runCallback($inputArg, $callbackFunc) { |
| 584 | - return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
|
| 584 | + return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
@@ -590,7 +590,7 @@ discard block |
||
| 590 | 590 | * arguments. |
| 591 | 591 | * |
| 592 | 592 | * @param string $callbackFunc |
| 593 | - * |
|
| 593 | + * |
|
| 594 | 594 | * @return mixed |
| 595 | 595 | */ |
| 596 | 596 | protected function _runEmptyCallback($callbackFunc) { |
@@ -601,7 +601,7 @@ discard block |
||
| 601 | 601 | * Gets a specific label of a specific field input name. |
| 602 | 602 | * |
| 603 | 603 | * @param string $inputName |
| 604 | - * |
|
| 604 | + * |
|
| 605 | 605 | * @return string |
| 606 | 606 | */ |
| 607 | 607 | protected function _getLabel($inputName) { |
@@ -614,7 +614,7 @@ discard block |
||
| 614 | 614 | * @param string $ruleName the rule name for this validation ("required") |
| 615 | 615 | * @param array $ruleArgs the rules argument |
| 616 | 616 | */ |
| 617 | - protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
|
| 617 | + protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
|
| 618 | 618 | $inputVal = $this->post($inputName); |
| 619 | 619 | if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
| 620 | 620 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
@@ -622,8 +622,8 @@ discard block |
||
| 622 | 622 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 623 | 623 | } |
| 624 | 624 | } |
| 625 | - else if($inputVal == '') { |
|
| 626 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 625 | + else if($inputVal == '') { |
|
| 626 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 627 | 627 | } |
| 628 | 628 | } |
| 629 | 629 | |
@@ -647,10 +647,10 @@ discard block |
||
| 647 | 647 | */ |
| 648 | 648 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
| 649 | 649 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
| 650 | - $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
|
| 651 | - if(! $result){ |
|
| 652 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 653 | - } |
|
| 650 | + $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
|
| 651 | + if(! $result){ |
|
| 652 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 653 | + } |
|
| 654 | 654 | } |
| 655 | 655 | } |
| 656 | 656 | |
@@ -682,7 +682,7 @@ discard block |
||
| 682 | 682 | continue; |
| 683 | 683 | } |
| 684 | 684 | } |
| 685 | - else{ |
|
| 685 | + else{ |
|
| 686 | 686 | if ($inputVal == $doNotEqual) { |
| 687 | 687 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 688 | 688 | continue; |
@@ -774,7 +774,7 @@ discard block |
||
| 774 | 774 | * @param string $ruleName the rule name for this validation ("less_than") |
| 775 | 775 | * @param array $ruleArgs the rules argument |
| 776 | 776 | */ |
| 777 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 777 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 778 | 778 | $inputVal = $this->post($inputName); |
| 779 | 779 | if ($inputVal >= $ruleArgs[1]) { |
| 780 | 780 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -790,7 +790,7 @@ discard block |
||
| 790 | 790 | * @param string $ruleName the rule name for this validation ("greater_than") |
| 791 | 791 | * @param array $ruleArgs the rules argument |
| 792 | 792 | */ |
| 793 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 793 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 794 | 794 | $inputVal = $this->post($inputName); |
| 795 | 795 | if ($inputVal <= $ruleArgs[1]) { |
| 796 | 796 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -806,7 +806,7 @@ discard block |
||
| 806 | 806 | * @param string $ruleName the rule name for this validation ("numeric") |
| 807 | 807 | * @param array $ruleArgs the rules argument |
| 808 | 808 | */ |
| 809 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 809 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 810 | 810 | $inputVal = $this->post($inputName); |
| 811 | 811 | if (! is_numeric($inputVal)) { |
| 812 | 812 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -822,18 +822,18 @@ discard block |
||
| 822 | 822 | * @param string $ruleName the rule name for this validation ("exists") |
| 823 | 823 | * @param array $ruleArgs the rules argument |
| 824 | 824 | */ |
| 825 | - protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
|
| 825 | + protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
|
| 826 | 826 | $inputVal = $this->post($inputName); |
| 827 | - if (! is_object($this->databaseInstance)){ |
|
| 827 | + if (! is_object($this->databaseInstance)){ |
|
| 828 | 828 | $obj = & get_instance(); |
| 829 | 829 | if(isset($obj->database)){ |
| 830 | 830 | $this->databaseInstance = $obj->database; |
| 831 | 831 | } |
| 832 | 832 | } |
| 833 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 834 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 835 | - ->where($column, $inputVal); |
|
| 836 | - $this->databaseInstance->get(); |
|
| 833 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 834 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 835 | + ->where($column, $inputVal); |
|
| 836 | + $this->databaseInstance->get(); |
|
| 837 | 837 | if ($this->databaseInstance->numRows() <= 0) { |
| 838 | 838 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 839 | 839 | return; |
@@ -848,7 +848,7 @@ discard block |
||
| 848 | 848 | * @param string $ruleName the rule name for this validation ("is_unique") |
| 849 | 849 | * @param array $ruleArgs the rules argument |
| 850 | 850 | */ |
| 851 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 851 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 852 | 852 | $inputVal = $this->post($inputName); |
| 853 | 853 | if (! is_object($this->databaseInstance)){ |
| 854 | 854 | $obj = & get_instance(); |
@@ -856,11 +856,11 @@ discard block |
||
| 856 | 856 | $this->databaseInstance = $obj->database; |
| 857 | 857 | } |
| 858 | 858 | } |
| 859 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 860 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 861 | - ->where($column, $inputVal); |
|
| 862 | - $this->databaseInstance->get(); |
|
| 863 | - if ($this->databaseInstance->numRows() > 0) { |
|
| 859 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 860 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 861 | + ->where($column, $inputVal); |
|
| 862 | + $this->databaseInstance->get(); |
|
| 863 | + if ($this->databaseInstance->numRows() > 0) { |
|
| 864 | 864 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 865 | 865 | return; |
| 866 | 866 | } |
@@ -874,25 +874,25 @@ discard block |
||
| 874 | 874 | * @param string $ruleName the rule name for this validation ("is_unique_update") |
| 875 | 875 | * @param array $ruleArgs the rules argument |
| 876 | 876 | */ |
| 877 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 877 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 878 | 878 | $inputVal = $this->post($inputName); |
| 879 | - if (! is_object($this->databaseInstance)){ |
|
| 879 | + if (! is_object($this->databaseInstance)){ |
|
| 880 | 880 | $obj = & get_instance(); |
| 881 | 881 | if(isset($obj->database)){ |
| 882 | 882 | $this->databaseInstance = $obj->database; |
| 883 | 883 | } |
| 884 | 884 | } |
| 885 | - $data = explode(',', $ruleArgs[1]); |
|
| 886 | - if(count($data) < 2){ |
|
| 887 | - return; |
|
| 888 | - } |
|
| 889 | - list($table, $column) = explode('.', $data[0]); |
|
| 890 | - list($field, $val) = explode('=', $data[1]); |
|
| 891 | - $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 892 | - ->where($column, $inputVal) |
|
| 893 | - ->where($field, '!=', trim($val)); |
|
| 885 | + $data = explode(',', $ruleArgs[1]); |
|
| 886 | + if(count($data) < 2){ |
|
| 887 | + return; |
|
| 888 | + } |
|
| 889 | + list($table, $column) = explode('.', $data[0]); |
|
| 890 | + list($field, $val) = explode('=', $data[1]); |
|
| 891 | + $this->databaseInstance->getQueryBuilder()->from($table) |
|
| 892 | + ->where($column, $inputVal) |
|
| 893 | + ->where($field, '!=', trim($val)); |
|
| 894 | 894 | $this->databaseInstance->get(); |
| 895 | - if ($this->databaseInstance->numRows() > 0) { |
|
| 895 | + if ($this->databaseInstance->numRows() > 0) { |
|
| 896 | 896 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 897 | 897 | return; |
| 898 | 898 | } |
@@ -908,7 +908,7 @@ discard block |
||
| 908 | 908 | */ |
| 909 | 909 | protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
| 910 | 910 | $inputVal = $this->post($inputName); |
| 911 | - $list = explode(',', $ruleArgs[1]); |
|
| 911 | + $list = explode(',', $ruleArgs[1]); |
|
| 912 | 912 | $list = array_map('trim', $list); |
| 913 | 913 | if (! in_array($inputVal, $list)) { |
| 914 | 914 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
@@ -926,7 +926,7 @@ discard block |
||
| 926 | 926 | */ |
| 927 | 927 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
| 928 | 928 | $inputVal = $this->post($inputName); |
| 929 | - $regex = $ruleArgs[1]; |
|
| 929 | + $regex = $ruleArgs[1]; |
|
| 930 | 930 | if (! preg_match($regex, $inputVal)) { |
| 931 | 931 | if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
| 932 | 932 | return; |
@@ -1,32 +1,32 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class Pagination{ |
| 28 | 28 | |
| 29 | - /** |
|
| 29 | + /** |
|
| 30 | 30 | * The list of loaded config |
| 31 | 31 | * @var array |
| 32 | 32 | */ |
@@ -49,15 +49,15 @@ discard block |
||
| 49 | 49 | if (empty($config) || ! is_array($config)){ |
| 50 | 50 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - if (! empty($overwriteConfig)){ |
|
| 54 | - $config = array_merge($config, $overwriteConfig); |
|
| 55 | - } |
|
| 56 | - $this->config = $config; |
|
| 52 | + else{ |
|
| 53 | + if (! empty($overwriteConfig)){ |
|
| 54 | + $config = array_merge($config, $overwriteConfig); |
|
| 55 | + } |
|
| 56 | + $this->config = $config; |
|
| 57 | 57 | //put it gobally |
| 58 | - Config::setAll($config); |
|
| 59 | - unset($config); |
|
| 60 | - } |
|
| 58 | + Config::setAll($config); |
|
| 59 | + unset($config); |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | else{ |
| 63 | 63 | show_error('Unable to find the pagination configuration file'); |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | return $this->paginationQueryString; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Set the value of the pagination query string |
|
| 99 | - * @param string $paginationQueryString the new value |
|
| 100 | - * @return object |
|
| 101 | - */ |
|
| 97 | + /** |
|
| 98 | + * Set the value of the pagination query string |
|
| 99 | + * @param string $paginationQueryString the new value |
|
| 100 | + * @return object |
|
| 101 | + */ |
|
| 102 | 102 | public function setPaginationQueryString($paginationQueryString){ |
| 103 | 103 | $this->paginationQueryString = $paginationQueryString; |
| 104 | 104 | return $this; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $queryString = Url::queryString(); |
| 118 | 118 | $currentUrl = Url::current(); |
| 119 | 119 | $query = ''; |
| 120 | - if ($queryString == ''){ |
|
| 120 | + if ($queryString == ''){ |
|
| 121 | 121 | $query = '?' . $pageQueryName . '='; |
| 122 | 122 | } |
| 123 | 123 | else{ |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | public function getLink($totalRows, $currentPageNumber){ |
| 152 | 152 | $numberOfLink = $this->config['nb_link']; |
| 153 | - $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 153 | + $numberOfRowPerPage = $this->config['pagination_per_page']; |
|
| 154 | 154 | if (empty($this->paginationQueryString)){ |
| 155 | 155 | //determine the pagination query string value |
| 156 | 156 | $this->determinePaginationQueryStringValue(); |
@@ -164,8 +164,8 @@ discard block |
||
| 164 | 164 | $numberOfRowPerPage = (int) $numberOfRowPerPage; |
| 165 | 165 | |
| 166 | 166 | if ($currentPageNumber <= 0){ |
| 167 | - $currentPageNumber = 1; |
|
| 168 | - } |
|
| 167 | + $currentPageNumber = 1; |
|
| 168 | + } |
|
| 169 | 169 | if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0) { |
| 170 | 170 | return $navbar; |
| 171 | 171 | } |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $navbar .= $this->buildPaginationLinkForMiddlePage($begin, $end, $currentPageNumber); |
| 207 | 207 | } |
| 208 | 208 | else if ($currentPageNumber == $numberOfPage){ |
| 209 | - $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
|
| 209 | + $navbar .= $this->buildPaginationLinkForLastPage($begin, $end, $currentPageNumber); |
|
| 210 | 210 | } |
| 211 | 211 | $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
| 212 | 212 | return $navbar; |
@@ -1669,34 +1669,34 @@ |
||
| 1669 | 1669 | protected function checkPlatform() |
| 1670 | 1670 | { |
| 1671 | 1671 | $platformMaps = array( |
| 1672 | - 'windows' => self::PLATFORM_WINDOWS, |
|
| 1673 | - 'iPad' => self::PLATFORM_IPAD, |
|
| 1674 | - 'iPod' => self::PLATFORM_IPOD, |
|
| 1675 | - 'iPhone' => self::PLATFORM_IPHONE, |
|
| 1676 | - 'mac' => self::PLATFORM_APPLE, |
|
| 1677 | - 'android' => self::PLATFORM_ANDROID, |
|
| 1678 | - 'Silk' => self::PLATFORM_FIRE_OS, |
|
| 1679 | - 'linux_smarttv' => self::PLATFORM_LINUX .'/'.self::PLATFORM_SMART_TV, |
|
| 1680 | - 'linux' => self::PLATFORM_LINUX, |
|
| 1681 | - 'Nokia' => self::PLATFORM_NOKIA, |
|
| 1682 | - 'BlackBerry' => self::PLATFORM_BLACKBERRY, |
|
| 1683 | - 'FreeBSD' => self::PLATFORM_FREEBSD, |
|
| 1684 | - 'OpenBSD' => self::PLATFORM_OPENBSD, |
|
| 1685 | - 'NetBSD' => self::PLATFORM_NETBSD, |
|
| 1686 | - 'OpenSolaris' => self::PLATFORM_OPENSOLARIS, |
|
| 1687 | - 'SunOS' => self::PLATFORM_SUNOS, |
|
| 1688 | - 'OS\/2' => self::PLATFORM_OS2, |
|
| 1689 | - 'BeOS' => self::PLATFORM_BEOS, |
|
| 1690 | - 'win' => self::PLATFORM_WINDOWS, |
|
| 1691 | - 'Playstation' => self::PLATFORM_PLAYSTATION, |
|
| 1692 | - 'Roku' => self::PLATFORM_ROKU, |
|
| 1693 | - 'iOS' => self::PLATFORM_IPHONE . '/' . self::PLATFORM_IPAD, |
|
| 1694 | - 'tvOS' => self::PLATFORM_APPLE_TV, |
|
| 1695 | - 'curl' => self::PLATFORM_TERMINAL, |
|
| 1696 | - 'CrOS' => self::PLATFORM_CHROME_OS, |
|
| 1697 | - 'okhttp' => self::PLATFORM_JAVA_ANDROID, |
|
| 1698 | - 'PostmanRuntime' => self::PLATFORM_POSTMAN, |
|
| 1699 | - 'Iframely' => self::PLATFORM_I_FRAME |
|
| 1672 | + 'windows' => self::PLATFORM_WINDOWS, |
|
| 1673 | + 'iPad' => self::PLATFORM_IPAD, |
|
| 1674 | + 'iPod' => self::PLATFORM_IPOD, |
|
| 1675 | + 'iPhone' => self::PLATFORM_IPHONE, |
|
| 1676 | + 'mac' => self::PLATFORM_APPLE, |
|
| 1677 | + 'android' => self::PLATFORM_ANDROID, |
|
| 1678 | + 'Silk' => self::PLATFORM_FIRE_OS, |
|
| 1679 | + 'linux_smarttv' => self::PLATFORM_LINUX .'/'.self::PLATFORM_SMART_TV, |
|
| 1680 | + 'linux' => self::PLATFORM_LINUX, |
|
| 1681 | + 'Nokia' => self::PLATFORM_NOKIA, |
|
| 1682 | + 'BlackBerry' => self::PLATFORM_BLACKBERRY, |
|
| 1683 | + 'FreeBSD' => self::PLATFORM_FREEBSD, |
|
| 1684 | + 'OpenBSD' => self::PLATFORM_OPENBSD, |
|
| 1685 | + 'NetBSD' => self::PLATFORM_NETBSD, |
|
| 1686 | + 'OpenSolaris' => self::PLATFORM_OPENSOLARIS, |
|
| 1687 | + 'SunOS' => self::PLATFORM_SUNOS, |
|
| 1688 | + 'OS\/2' => self::PLATFORM_OS2, |
|
| 1689 | + 'BeOS' => self::PLATFORM_BEOS, |
|
| 1690 | + 'win' => self::PLATFORM_WINDOWS, |
|
| 1691 | + 'Playstation' => self::PLATFORM_PLAYSTATION, |
|
| 1692 | + 'Roku' => self::PLATFORM_ROKU, |
|
| 1693 | + 'iOS' => self::PLATFORM_IPHONE . '/' . self::PLATFORM_IPAD, |
|
| 1694 | + 'tvOS' => self::PLATFORM_APPLE_TV, |
|
| 1695 | + 'curl' => self::PLATFORM_TERMINAL, |
|
| 1696 | + 'CrOS' => self::PLATFORM_CHROME_OS, |
|
| 1697 | + 'okhttp' => self::PLATFORM_JAVA_ANDROID, |
|
| 1698 | + 'PostmanRuntime' => self::PLATFORM_POSTMAN, |
|
| 1699 | + 'Iframely' => self::PLATFORM_I_FRAME |
|
| 1700 | 1700 | ); |
| 1701 | 1701 | |
| 1702 | 1702 | foreach ($platformMaps as $name => $value) { |
@@ -1,71 +1,71 @@ |
||
| 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 function_string.php |
|
| 29 | - * |
|
| 30 | - * This file contains the definition of the functions relating to the processing of strings characters. |
|
| 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 | - * @since 1.0.0 |
|
| 39 | - * @filesource |
|
| 40 | - */ |
|
| 27 | + /** |
|
| 28 | + * @file function_string.php |
|
| 29 | + * |
|
| 30 | + * This file contains the definition of the functions relating to the processing of strings characters. |
|
| 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 | + * @since 1.0.0 |
|
| 39 | + * @filesource |
|
| 40 | + */ |
|
| 41 | 41 | |
| 42 | - if(! function_exists('get_random_string')){ |
|
| 43 | - /** |
|
| 44 | - * Generate a random string |
|
| 45 | - * @param string $type the type of generation. It can take the values: "alpha" for alphabetic characters, |
|
| 46 | - * "alnum" for alpha-numeric characters and "num" for numbers. |
|
| 47 | - * By default it is "alnum". |
|
| 48 | - * @param integer $length the length of the string to generate. By default it is 10. |
|
| 49 | - * @param boolean $lower if we return the generated string in lowercase (true). By default it's false. |
|
| 50 | - * @return string the generated string. |
|
| 51 | - */ |
|
| 52 | - function get_random_string($type = 'alnum', $length = 10, $lower = false){ |
|
| 53 | - $chars = array( |
|
| 54 | - 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
|
| 55 | - 'alnum' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', |
|
| 56 | - 'num' => '1234567890' |
|
| 57 | - ); |
|
| 58 | - $str = null; |
|
| 59 | - if(isset($chars[$type])){ |
|
| 60 | - $str = $chars[$type]; |
|
| 61 | - } |
|
| 62 | - $random = null; |
|
| 63 | - for($i = 0; $i < $length; $i++){ |
|
| 64 | - $random .= $str[mt_rand() % strlen($str)]; |
|
| 65 | - } |
|
| 66 | - if($lower){ |
|
| 67 | - $random = strtolower($random); |
|
| 68 | - } |
|
| 69 | - return $random; |
|
| 70 | - } |
|
| 71 | - } |
|
| 42 | + if(! function_exists('get_random_string')){ |
|
| 43 | + /** |
|
| 44 | + * Generate a random string |
|
| 45 | + * @param string $type the type of generation. It can take the values: "alpha" for alphabetic characters, |
|
| 46 | + * "alnum" for alpha-numeric characters and "num" for numbers. |
|
| 47 | + * By default it is "alnum". |
|
| 48 | + * @param integer $length the length of the string to generate. By default it is 10. |
|
| 49 | + * @param boolean $lower if we return the generated string in lowercase (true). By default it's false. |
|
| 50 | + * @return string the generated string. |
|
| 51 | + */ |
|
| 52 | + function get_random_string($type = 'alnum', $length = 10, $lower = false){ |
|
| 53 | + $chars = array( |
|
| 54 | + 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
|
| 55 | + 'alnum' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', |
|
| 56 | + 'num' => '1234567890' |
|
| 57 | + ); |
|
| 58 | + $str = null; |
|
| 59 | + if(isset($chars[$type])){ |
|
| 60 | + $str = $chars[$type]; |
|
| 61 | + } |
|
| 62 | + $random = null; |
|
| 63 | + for($i = 0; $i < $length; $i++){ |
|
| 64 | + $random .= $str[mt_rand() % strlen($str)]; |
|
| 65 | + } |
|
| 66 | + if($lower){ |
|
| 67 | + $random = strtolower($random); |
|
| 68 | + } |
|
| 69 | + return $random; |
|
| 70 | + } |
|
| 71 | + } |
|
@@ -1,18 +1,18 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - /** |
|
| 4 | - * Function to test private & protected method |
|
| 5 | - */ |
|
| 6 | - function run_private_protected_method($object, $method, array $args = array()){ |
|
| 7 | - $r = new ReflectionClass(get_class($object)); |
|
| 8 | - $m = $r->getMethod($method); |
|
| 9 | - $m->setAccessible(true); |
|
| 10 | - return $m->invokeArgs($object, $args); |
|
| 11 | - } |
|
| 3 | + /** |
|
| 4 | + * Function to test private & protected method |
|
| 5 | + */ |
|
| 6 | + function run_private_protected_method($object, $method, array $args = array()){ |
|
| 7 | + $r = new ReflectionClass(get_class($object)); |
|
| 8 | + $m = $r->getMethod($method); |
|
| 9 | + $m->setAccessible(true); |
|
| 10 | + return $m->invokeArgs($object, $args); |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * Function to return the correct database configuration |
|
| 15 | - */ |
|
| 14 | + * Function to return the correct database configuration |
|
| 15 | + */ |
|
| 16 | 16 | function get_db_config(){ |
| 17 | 17 | return array( |
| 18 | 18 | 'driver' => 'sqlite', |
@@ -1,222 +1,222 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class DBSessionHandlerTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class DBSessionHandlerTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - private $db = null; |
|
| 8 | + private $db = null; |
|
| 9 | 9 | |
| 10 | - private $model = null; |
|
| 10 | + private $model = null; |
|
| 11 | 11 | |
| 12 | - private $secret = 'bXlzZWNyZXQ'; |
|
| 12 | + private $secret = 'bXlzZWNyZXQ'; |
|
| 13 | 13 | |
| 14 | - private static $config = null; |
|
| 14 | + private static $config = null; |
|
| 15 | 15 | |
| 16 | - public function __construct(){ |
|
| 16 | + public function __construct(){ |
|
| 17 | 17 | $cfg = get_db_config(); |
| 18 | - $this->db = new Database($cfg); |
|
| 18 | + $this->db = new Database($cfg); |
|
| 19 | 19 | $qr = new DatabaseQueryRunner($this->db->getPdo()); |
| 20 | 20 | $qr->setBenchmark(new Benchmark()); |
| 21 | 21 | $qr->setDriver('sqlite'); |
| 22 | 22 | $this->db->setQueryRunner($qr); |
| 23 | - } |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public static function setUpBeforeClass() |
|
| 26 | - { |
|
| 27 | - require APPS_MODEL_PATH . 'DBSessionModel.php'; |
|
| 28 | - self::$config = new Config(); |
|
| 29 | - self::$config->init(); |
|
| 30 | - } |
|
| 25 | + public static function setUpBeforeClass() |
|
| 26 | + { |
|
| 27 | + require APPS_MODEL_PATH . 'DBSessionModel.php'; |
|
| 28 | + self::$config = new Config(); |
|
| 29 | + self::$config->init(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | 32 | |
| 33 | - public static function tearDownAfterClass() |
|
| 34 | - { |
|
| 33 | + public static function tearDownAfterClass() |
|
| 34 | + { |
|
| 35 | 35 | |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - protected function setUp() |
|
| 39 | - { |
|
| 40 | - $this->model = new DBSessionModel($this->db); |
|
| 38 | + protected function setUp() |
|
| 39 | + { |
|
| 40 | + $this->model = new DBSessionModel($this->db); |
|
| 41 | 41 | //to prevent old data conflict |
| 42 | - $this->model->truncate(); |
|
| 43 | - } |
|
| 42 | + $this->model->truncate(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - protected function tearDown() |
|
| 46 | - { |
|
| 47 | - } |
|
| 45 | + protected function tearDown() |
|
| 46 | + { |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | 50 | |
| 51 | - public function testUsingSessionConfiguration(){ |
|
| 52 | - //using value in the configuration |
|
| 53 | - self::$config->set('session_save_path', 'DBSessionModel'); |
|
| 54 | - self::$config->set('session_secret', $this->secret); |
|
| 55 | - $dbsh = new DBSessionHandler(); |
|
| 56 | - //assign Database instance manually |
|
| 57 | - $o = &get_instance(); |
|
| 58 | - $o->database = $this->db; |
|
| 59 | - |
|
| 60 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 61 | - $this->assertTrue($dbsh->close()); |
|
| 62 | - $this->assertNull($dbsh->read('foo')); |
|
| 63 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 64 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 65 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 66 | - //do update of existing data |
|
| 67 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 68 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 69 | - $this->assertTrue($dbsh->destroy('foo')); |
|
| 70 | - $this->assertNull($dbsh->read('foo')); |
|
| 71 | - $this->assertTrue($dbsh->gc(13)); |
|
| 72 | - $encoded = $dbsh->encode('foo'); |
|
| 73 | - $this->assertNotEmpty($encoded); |
|
| 74 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function testWhenDataIsExpired(){ |
|
| 78 | - $dbsh = new DBSessionHandler($this->model); |
|
| 79 | - $dbsh->setSessionSecret($this->secret); |
|
| 80 | - |
|
| 81 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 82 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 83 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 84 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 85 | - //put it in expired |
|
| 86 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
| 87 | - $this->assertNull($dbsh->read('foo')); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function testWhenDataAlreadyExistDoUpdate(){ |
|
| 91 | - $dbsh = new DBSessionHandler($this->model); |
|
| 92 | - $dbsh->setSessionSecret($this->secret); |
|
| 93 | - |
|
| 94 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 95 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 96 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 97 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 98 | - //do update of existing data |
|
| 99 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 100 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function testUsingCustomModelInstance(){ |
|
| 104 | - $dbsh = new DBSessionHandler($this->model); |
|
| 105 | - $dbsh->setSessionSecret($this->secret); |
|
| 106 | - |
|
| 107 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 108 | - $this->assertTrue($dbsh->close()); |
|
| 109 | - $this->assertNull($dbsh->read('foo')); |
|
| 110 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 111 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 112 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 113 | - //put it in expired |
|
| 114 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
| 115 | - |
|
| 116 | - $this->assertNull($dbsh->read('foo')); |
|
| 117 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 118 | - |
|
| 119 | - //do update of existing data |
|
| 120 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 121 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 122 | - $this->assertTrue($dbsh->destroy('foo')); |
|
| 123 | - $this->assertNull($dbsh->read('foo')); |
|
| 124 | - $this->assertTrue($dbsh->gc(13)); |
|
| 125 | - $encoded = $dbsh->encode('foo'); |
|
| 126 | - $this->assertNotEmpty($encoded); |
|
| 127 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - public function testUsingCustomLogInstance(){ |
|
| 132 | - $dbsh = new DBSessionHandler($this->model, new Log()); |
|
| 133 | - $dbsh->setSessionSecret($this->secret); |
|
| 134 | - |
|
| 135 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 136 | - $this->assertTrue($dbsh->close()); |
|
| 137 | - $this->assertNull($dbsh->read('foo')); |
|
| 138 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 139 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 140 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 141 | - //put it in expired |
|
| 142 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
| 143 | - |
|
| 144 | - $this->assertNull($dbsh->read('foo')); |
|
| 145 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 146 | - |
|
| 147 | - //do update of existing data |
|
| 148 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 149 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 150 | - $this->assertTrue($dbsh->destroy('foo')); |
|
| 151 | - $this->assertNull($dbsh->read('foo')); |
|
| 152 | - $this->assertTrue($dbsh->gc(13)); |
|
| 153 | - $encoded = $dbsh->encode('foo'); |
|
| 154 | - $this->assertNotEmpty($encoded); |
|
| 155 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function testUsingCustomLoaderInstance(){ |
|
| 159 | - $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
|
| 160 | - $dbsh->setSessionSecret($this->secret); |
|
| 161 | - |
|
| 162 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 163 | - $this->assertTrue($dbsh->close()); |
|
| 164 | - $this->assertNull($dbsh->read('foo')); |
|
| 165 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 166 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 167 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 168 | - //put it in expired |
|
| 169 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
| 170 | - |
|
| 171 | - $this->assertNull($dbsh->read('foo')); |
|
| 172 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 173 | - |
|
| 174 | - //do update of existing data |
|
| 175 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 176 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 177 | - $this->assertTrue($dbsh->destroy('foo')); |
|
| 178 | - $this->assertNull($dbsh->read('foo')); |
|
| 179 | - $this->assertTrue($dbsh->gc(13)); |
|
| 180 | - $encoded = $dbsh->encode('foo'); |
|
| 181 | - $this->assertNotEmpty($encoded); |
|
| 182 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - |
|
| 186 | - public function testWhenModelInsanceIsNotSet(){ |
|
| 187 | - $dbsh = new DBSessionHandler(null, null, new Loader()); |
|
| 188 | - $dbsh->setSessionSecret($this->secret); |
|
| 189 | - |
|
| 190 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 191 | - $this->assertTrue($dbsh->close()); |
|
| 192 | - $this->assertNull($dbsh->read('foo')); |
|
| 193 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 194 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
| 195 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 196 | - //put it in expired |
|
| 197 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
| 198 | - |
|
| 199 | - $this->assertNull($dbsh->read('foo')); |
|
| 200 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
| 201 | - |
|
| 202 | - //do update of existing data |
|
| 203 | - $this->assertTrue($dbsh->write('tnh', '445')); |
|
| 204 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
| 205 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 206 | - $this->assertTrue($dbsh->destroy('foo')); |
|
| 207 | - $this->assertNull($dbsh->read('foo')); |
|
| 208 | - $this->assertTrue($dbsh->gc(13)); |
|
| 209 | - $encoded = $dbsh->encode('foo'); |
|
| 210 | - $this->assertNotEmpty($encoded); |
|
| 211 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - public function testWhenModelTableColumnsIsNotSet(){ |
|
| 215 | - //session table is empty |
|
| 216 | - $this->model->setSessionTableColumns(array()); |
|
| 217 | - $dbsh = new DBSessionHandler($this->model); |
|
| 218 | - $this->assertTrue($dbsh->open(null, null)); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - } |
|
| 223 | 51 | \ No newline at end of file |
| 52 | + public function testUsingSessionConfiguration(){ |
|
| 53 | + //using value in the configuration |
|
| 54 | + self::$config->set('session_save_path', 'DBSessionModel'); |
|
| 55 | + self::$config->set('session_secret', $this->secret); |
|
| 56 | + $dbsh = new DBSessionHandler(); |
|
| 57 | + //assign Database instance manually |
|
| 58 | + $o = &get_instance(); |
|
| 59 | + $o->database = $this->db; |
|
| 60 | + |
|
| 61 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 62 | + $this->assertTrue($dbsh->close()); |
|
| 63 | + $this->assertNull($dbsh->read('foo')); |
|
| 64 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 65 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 66 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 67 | + //do update of existing data |
|
| 68 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 69 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 70 | + $this->assertTrue($dbsh->destroy('foo')); |
|
| 71 | + $this->assertNull($dbsh->read('foo')); |
|
| 72 | + $this->assertTrue($dbsh->gc(13)); |
|
| 73 | + $encoded = $dbsh->encode('foo'); |
|
| 74 | + $this->assertNotEmpty($encoded); |
|
| 75 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function testWhenDataIsExpired(){ |
|
| 79 | + $dbsh = new DBSessionHandler($this->model); |
|
| 80 | + $dbsh->setSessionSecret($this->secret); |
|
| 81 | + |
|
| 82 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 83 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 84 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 85 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 86 | + //put it in expired |
|
| 87 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
| 88 | + $this->assertNull($dbsh->read('foo')); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + public function testWhenDataAlreadyExistDoUpdate(){ |
|
| 92 | + $dbsh = new DBSessionHandler($this->model); |
|
| 93 | + $dbsh->setSessionSecret($this->secret); |
|
| 94 | + |
|
| 95 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 96 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 97 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 98 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 99 | + //do update of existing data |
|
| 100 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 101 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function testUsingCustomModelInstance(){ |
|
| 105 | + $dbsh = new DBSessionHandler($this->model); |
|
| 106 | + $dbsh->setSessionSecret($this->secret); |
|
| 107 | + |
|
| 108 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 109 | + $this->assertTrue($dbsh->close()); |
|
| 110 | + $this->assertNull($dbsh->read('foo')); |
|
| 111 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 112 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 113 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 114 | + //put it in expired |
|
| 115 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
| 116 | + |
|
| 117 | + $this->assertNull($dbsh->read('foo')); |
|
| 118 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 119 | + |
|
| 120 | + //do update of existing data |
|
| 121 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 122 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 123 | + $this->assertTrue($dbsh->destroy('foo')); |
|
| 124 | + $this->assertNull($dbsh->read('foo')); |
|
| 125 | + $this->assertTrue($dbsh->gc(13)); |
|
| 126 | + $encoded = $dbsh->encode('foo'); |
|
| 127 | + $this->assertNotEmpty($encoded); |
|
| 128 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + public function testUsingCustomLogInstance(){ |
|
| 133 | + $dbsh = new DBSessionHandler($this->model, new Log()); |
|
| 134 | + $dbsh->setSessionSecret($this->secret); |
|
| 135 | + |
|
| 136 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 137 | + $this->assertTrue($dbsh->close()); |
|
| 138 | + $this->assertNull($dbsh->read('foo')); |
|
| 139 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 140 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 141 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 142 | + //put it in expired |
|
| 143 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
| 144 | + |
|
| 145 | + $this->assertNull($dbsh->read('foo')); |
|
| 146 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 147 | + |
|
| 148 | + //do update of existing data |
|
| 149 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 150 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 151 | + $this->assertTrue($dbsh->destroy('foo')); |
|
| 152 | + $this->assertNull($dbsh->read('foo')); |
|
| 153 | + $this->assertTrue($dbsh->gc(13)); |
|
| 154 | + $encoded = $dbsh->encode('foo'); |
|
| 155 | + $this->assertNotEmpty($encoded); |
|
| 156 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function testUsingCustomLoaderInstance(){ |
|
| 160 | + $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
|
| 161 | + $dbsh->setSessionSecret($this->secret); |
|
| 162 | + |
|
| 163 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 164 | + $this->assertTrue($dbsh->close()); |
|
| 165 | + $this->assertNull($dbsh->read('foo')); |
|
| 166 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 167 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 168 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 169 | + //put it in expired |
|
| 170 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
| 171 | + |
|
| 172 | + $this->assertNull($dbsh->read('foo')); |
|
| 173 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 174 | + |
|
| 175 | + //do update of existing data |
|
| 176 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 177 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 178 | + $this->assertTrue($dbsh->destroy('foo')); |
|
| 179 | + $this->assertNull($dbsh->read('foo')); |
|
| 180 | + $this->assertTrue($dbsh->gc(13)); |
|
| 181 | + $encoded = $dbsh->encode('foo'); |
|
| 182 | + $this->assertNotEmpty($encoded); |
|
| 183 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + public function testWhenModelInsanceIsNotSet(){ |
|
| 188 | + $dbsh = new DBSessionHandler(null, null, new Loader()); |
|
| 189 | + $dbsh->setSessionSecret($this->secret); |
|
| 190 | + |
|
| 191 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 192 | + $this->assertTrue($dbsh->close()); |
|
| 193 | + $this->assertNull($dbsh->read('foo')); |
|
| 194 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 195 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
| 196 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
| 197 | + //put it in expired |
|
| 198 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
| 199 | + |
|
| 200 | + $this->assertNull($dbsh->read('foo')); |
|
| 201 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
| 202 | + |
|
| 203 | + //do update of existing data |
|
| 204 | + $this->assertTrue($dbsh->write('tnh', '445')); |
|
| 205 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
| 206 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
| 207 | + $this->assertTrue($dbsh->destroy('foo')); |
|
| 208 | + $this->assertNull($dbsh->read('foo')); |
|
| 209 | + $this->assertTrue($dbsh->gc(13)); |
|
| 210 | + $encoded = $dbsh->encode('foo'); |
|
| 211 | + $this->assertNotEmpty($encoded); |
|
| 212 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + public function testWhenModelTableColumnsIsNotSet(){ |
|
| 216 | + //session table is empty |
|
| 217 | + $this->model->setSessionTableColumns(array()); |
|
| 218 | + $dbsh = new DBSessionHandler($this->model); |
|
| 219 | + $this->assertTrue($dbsh->open(null, null)); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | 224 | \ No newline at end of file |
@@ -1,41 +1,41 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class DatabaseTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class DatabaseTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - public static function setUpBeforeClass() |
|
| 9 | - { |
|
| 8 | + public static function setUpBeforeClass() |
|
| 9 | + { |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public static function tearDownAfterClass() |
|
| 14 | - { |
|
| 13 | + public static function tearDownAfterClass() |
|
| 14 | + { |
|
| 15 | 15 | |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - protected function setUp() |
|
| 19 | - { |
|
| 20 | - } |
|
| 18 | + protected function setUp() |
|
| 19 | + { |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function tearDown() |
|
| 23 | - { |
|
| 24 | - } |
|
| 22 | + protected function tearDown() |
|
| 23 | + { |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function testConnectToDatabaseSuccessfully() |
|
| 27 | - { |
|
| 26 | + public function testConnectToDatabaseSuccessfully() |
|
| 27 | + { |
|
| 28 | 28 | $cfg = get_db_config(); |
| 29 | 29 | $db = new Database($cfg, false); |
| 30 | 30 | $isConnected = $db->connect(); |
| 31 | - $this->assertTrue($isConnected); |
|
| 32 | - } |
|
| 31 | + $this->assertTrue($isConnected); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | public function testCannotConnectToDatabase() |
| 35 | - { |
|
| 36 | - $db = new Database(array(), false); |
|
| 37 | - $isConnected = $db->connect(); |
|
| 38 | - $this->assertFalse($isConnected); |
|
| 39 | - } |
|
| 35 | + { |
|
| 36 | + $db = new Database(array(), false); |
|
| 37 | + $isConnected = $db->connect(); |
|
| 38 | + $this->assertFalse($isConnected); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - } |
|
| 42 | 41 | \ No newline at end of file |
| 42 | + } |
|
| 43 | 43 | \ No newline at end of file |
@@ -1,190 +1,190 @@ |
||
| 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 | - class Config{ |
|
| 27 | + class Config{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of loaded configuration |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $config = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of loaded configuration |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $config = 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 signleton of the logger |
|
| 43 | - * @return Object the Log instance |
|
| 44 | - */ |
|
| 45 | - private static function getLogger(){ |
|
| 46 | - if(self::$logger == null){ |
|
| 47 | - $logger = array(); |
|
| 48 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 49 | - $logger[0]->setLogger('Library::Config'); |
|
| 50 | - self::$logger = $logger[0]; |
|
| 51 | - } |
|
| 52 | - return self::$logger; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * The signleton of the logger |
|
| 43 | + * @return Object the Log instance |
|
| 44 | + */ |
|
| 45 | + private static function getLogger(){ |
|
| 46 | + if(self::$logger == null){ |
|
| 47 | + $logger = array(); |
|
| 48 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 49 | + $logger[0]->setLogger('Library::Config'); |
|
| 50 | + self::$logger = $logger[0]; |
|
| 51 | + } |
|
| 52 | + return self::$logger; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Set the log instance for future use |
|
| 57 | - * @param object $logger the log object |
|
| 58 | - * @return object the log instance |
|
| 59 | - */ |
|
| 60 | - public static function setLogger($logger){ |
|
| 61 | - self::$logger = $logger; |
|
| 62 | - return self::$logger; |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Set the log instance for future use |
|
| 57 | + * @param object $logger the log object |
|
| 58 | + * @return object the log instance |
|
| 59 | + */ |
|
| 60 | + public static function setLogger($logger){ |
|
| 61 | + self::$logger = $logger; |
|
| 62 | + return self::$logger; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Initialize the configuration by loading all the configuration from config file |
|
| 67 | - */ |
|
| 68 | - public static function init(){ |
|
| 69 | - $logger = static::getLogger(); |
|
| 70 | - $logger->debug('Initialization of the configuration'); |
|
| 71 | - self::$config = & load_configurations(); |
|
| 72 | - self::setBaseUrlUsingServerVar(); |
|
| 73 | - if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){ |
|
| 74 | - $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 75 | - } |
|
| 76 | - $logger->info('Configuration initialized successfully'); |
|
| 77 | - $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Initialize the configuration by loading all the configuration from config file |
|
| 67 | + */ |
|
| 68 | + public static function init(){ |
|
| 69 | + $logger = static::getLogger(); |
|
| 70 | + $logger->debug('Initialization of the configuration'); |
|
| 71 | + self::$config = & load_configurations(); |
|
| 72 | + self::setBaseUrlUsingServerVar(); |
|
| 73 | + if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){ |
|
| 74 | + $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 75 | + } |
|
| 76 | + $logger->info('Configuration initialized successfully'); |
|
| 77 | + $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the configuration item value |
|
| 82 | - * @param string $item the configuration item name to get |
|
| 83 | - * @param mixed $default the default value to use if can not find the config item in the list |
|
| 84 | - * @return mixed the config value if exist or the default value |
|
| 85 | - */ |
|
| 86 | - public static function get($item, $default = null){ |
|
| 87 | - $logger = static::getLogger(); |
|
| 88 | - if(array_key_exists($item, self::$config)){ |
|
| 89 | - return self::$config[$item]; |
|
| 90 | - } |
|
| 91 | - $logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']'); |
|
| 92 | - return $default; |
|
| 93 | - } |
|
| 80 | + /** |
|
| 81 | + * Get the configuration item value |
|
| 82 | + * @param string $item the configuration item name to get |
|
| 83 | + * @param mixed $default the default value to use if can not find the config item in the list |
|
| 84 | + * @return mixed the config value if exist or the default value |
|
| 85 | + */ |
|
| 86 | + public static function get($item, $default = null){ |
|
| 87 | + $logger = static::getLogger(); |
|
| 88 | + if(array_key_exists($item, self::$config)){ |
|
| 89 | + return self::$config[$item]; |
|
| 90 | + } |
|
| 91 | + $logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']'); |
|
| 92 | + return $default; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Set the configuration item value |
|
| 97 | - * @param string $item the config item name to set |
|
| 98 | - * @param mixed $value the config item value |
|
| 99 | - */ |
|
| 100 | - public static function set($item, $value){ |
|
| 101 | - self::$config[$item] = $value; |
|
| 102 | - } |
|
| 95 | + /** |
|
| 96 | + * Set the configuration item value |
|
| 97 | + * @param string $item the config item name to set |
|
| 98 | + * @param mixed $value the config item value |
|
| 99 | + */ |
|
| 100 | + public static function set($item, $value){ |
|
| 101 | + self::$config[$item] = $value; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Get all the configuration values |
|
| 106 | - * @return array the config values |
|
| 107 | - */ |
|
| 108 | - public static function getAll(){ |
|
| 109 | - return self::$config; |
|
| 110 | - } |
|
| 104 | + /** |
|
| 105 | + * Get all the configuration values |
|
| 106 | + * @return array the config values |
|
| 107 | + */ |
|
| 108 | + public static function getAll(){ |
|
| 109 | + return self::$config; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Set the configuration values bu merged with the existing configuration |
|
| 114 | - * @param array $config the config values to add in the configuration list |
|
| 115 | - */ |
|
| 116 | - public static function setAll(array $config = array()){ |
|
| 117 | - self::$config = array_merge(self::$config, $config); |
|
| 118 | - } |
|
| 112 | + /** |
|
| 113 | + * Set the configuration values bu merged with the existing configuration |
|
| 114 | + * @param array $config the config values to add in the configuration list |
|
| 115 | + */ |
|
| 116 | + public static function setAll(array $config = array()){ |
|
| 117 | + self::$config = array_merge(self::$config, $config); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Delete the configuration item in the list |
|
| 122 | - * @param string $item the config item name to be deleted |
|
| 123 | - * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 124 | - */ |
|
| 125 | - public static function delete($item){ |
|
| 126 | - $logger = static::getLogger(); |
|
| 127 | - if(array_key_exists($item, self::$config)){ |
|
| 128 | - $logger->info('Delete config item ['.$item.']'); |
|
| 129 | - unset(self::$config[$item]); |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - else{ |
|
| 133 | - $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 120 | + /** |
|
| 121 | + * Delete the configuration item in the list |
|
| 122 | + * @param string $item the config item name to be deleted |
|
| 123 | + * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 124 | + */ |
|
| 125 | + public static function delete($item){ |
|
| 126 | + $logger = static::getLogger(); |
|
| 127 | + if(array_key_exists($item, self::$config)){ |
|
| 128 | + $logger->info('Delete config item ['.$item.']'); |
|
| 129 | + unset(self::$config[$item]); |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + else{ |
|
| 133 | + $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Load the configuration file. This an alias to Loader::config() |
|
| 140 | - * @param string $config the config name to be loaded |
|
| 141 | - */ |
|
| 142 | - public static function load($config){ |
|
| 143 | - Loader::config($config); |
|
| 144 | - } |
|
| 138 | + /** |
|
| 139 | + * Load the configuration file. This an alias to Loader::config() |
|
| 140 | + * @param string $config the config name to be loaded |
|
| 141 | + */ |
|
| 142 | + public static function load($config){ |
|
| 143 | + Loader::config($config); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Set the configuration for "base_url" if is not set in the configuration |
|
| 148 | - */ |
|
| 149 | - private static function setBaseUrlUsingServerVar(){ |
|
| 150 | - $logger = static::getLogger(); |
|
| 151 | - if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){ |
|
| 152 | - if(ENVIRONMENT == 'production'){ |
|
| 153 | - $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 154 | - } |
|
| 155 | - $baseUrl = null; |
|
| 156 | - $protocol = 'http'; |
|
| 157 | - if(is_https()){ |
|
| 158 | - $protocol = 'https'; |
|
| 159 | - } |
|
| 160 | - $protocol .='://'; |
|
| 146 | + /** |
|
| 147 | + * Set the configuration for "base_url" if is not set in the configuration |
|
| 148 | + */ |
|
| 149 | + private static function setBaseUrlUsingServerVar(){ |
|
| 150 | + $logger = static::getLogger(); |
|
| 151 | + if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){ |
|
| 152 | + if(ENVIRONMENT == 'production'){ |
|
| 153 | + $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 154 | + } |
|
| 155 | + $baseUrl = null; |
|
| 156 | + $protocol = 'http'; |
|
| 157 | + if(is_https()){ |
|
| 158 | + $protocol = 'https'; |
|
| 159 | + } |
|
| 160 | + $protocol .='://'; |
|
| 161 | 161 | |
| 162 | - if (isset($_SERVER['SERVER_ADDR'])){ |
|
| 163 | - $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 164 | - //check if the server is running under IPv6 |
|
| 165 | - if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){ |
|
| 166 | - $baseUrl = '['.$_SERVER['SERVER_ADDR'].']'; |
|
| 167 | - } |
|
| 168 | - $serverPort = 80; |
|
| 169 | - if (isset($_SERVER['SERVER_PORT'])) { |
|
| 170 | - $serverPort = $_SERVER['SERVER_PORT']; |
|
| 171 | - } |
|
| 172 | - $port = ''; |
|
| 173 | - if($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))){ |
|
| 174 | - $port = ':'.$serverPort; |
|
| 175 | - } |
|
| 176 | - $baseUrl = $protocol . $baseUrl . $port . substr( |
|
| 177 | - $_SERVER['SCRIPT_NAME'], |
|
| 178 | - 0, |
|
| 179 | - strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])) |
|
| 180 | - ); |
|
| 181 | - } |
|
| 182 | - else{ |
|
| 183 | - $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 184 | - $baseUrl = 'http://localhost/'; |
|
| 185 | - } |
|
| 186 | - self::set('base_url', $baseUrl); |
|
| 187 | - } |
|
| 188 | - self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 189 | - } |
|
| 190 | - } |
|
| 162 | + if (isset($_SERVER['SERVER_ADDR'])){ |
|
| 163 | + $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 164 | + //check if the server is running under IPv6 |
|
| 165 | + if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){ |
|
| 166 | + $baseUrl = '['.$_SERVER['SERVER_ADDR'].']'; |
|
| 167 | + } |
|
| 168 | + $serverPort = 80; |
|
| 169 | + if (isset($_SERVER['SERVER_PORT'])) { |
|
| 170 | + $serverPort = $_SERVER['SERVER_PORT']; |
|
| 171 | + } |
|
| 172 | + $port = ''; |
|
| 173 | + if($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))){ |
|
| 174 | + $port = ':'.$serverPort; |
|
| 175 | + } |
|
| 176 | + $baseUrl = $protocol . $baseUrl . $port . substr( |
|
| 177 | + $_SERVER['SCRIPT_NAME'], |
|
| 178 | + 0, |
|
| 179 | + strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])) |
|
| 180 | + ); |
|
| 181 | + } |
|
| 182 | + else{ |
|
| 183 | + $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 184 | + $baseUrl = 'http://localhost/'; |
|
| 185 | + } |
|
| 186 | + self::set('base_url', $baseUrl); |
|
| 187 | + } |
|
| 188 | + self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 189 | + } |
|
| 190 | + } |
|