@@ -178,15 +178,13 @@ |
||
| 178 | 178 | function clean_input($str){ |
| 179 | 179 | if(is_array($str)){ |
| 180 | 180 | $str = array_map('clean_input', $str); |
| 181 | - } |
|
| 182 | - else if(is_object($str)){ |
|
| 181 | + } else if(is_object($str)){ |
|
| 183 | 182 | $obj = $str; |
| 184 | 183 | foreach ($str as $var => $value) { |
| 185 | 184 | $obj->$var = clean_input($value); |
| 186 | 185 | } |
| 187 | 186 | $str = $obj; |
| 188 | - } |
|
| 189 | - else{ |
|
| 187 | + } else{ |
|
| 190 | 188 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
| 191 | 189 | } |
| 192 | 190 | return $str; |
@@ -29,14 +29,14 @@ discard block |
||
| 29 | 29 | //put the first letter of class to upper case |
| 30 | 30 | $class = ucfirst($class); |
| 31 | 31 | static $classes = array(); |
| 32 | - if(isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 32 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log') { |
|
| 33 | 33 | return $classes[$class]; |
| 34 | 34 | } |
| 35 | 35 | $found = false; |
| 36 | 36 | foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
| 37 | 37 | $file = $path . $dir . '/' . $class . '.php'; |
| 38 | - if(file_exists($file)){ |
|
| 39 | - if(class_exists($class, false) === false){ |
|
| 38 | + if (file_exists($file)) { |
|
| 39 | + if (class_exists($class, false) === false) { |
|
| 40 | 40 | require_once $file; |
| 41 | 41 | } |
| 42 | 42 | //already found |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | break; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | - if(! $found){ |
|
| 47 | + if (!$found) { |
|
| 48 | 48 | //can't use show_error() at this time because some dependencies not yet loaded |
| 49 | 49 | set_http_status_header(503); |
| 50 | 50 | echo 'Cannot find the class [' . $class . ']'; |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | /* |
| 55 | 55 | TODO use the best method to get the Log instance |
| 56 | 56 | */ |
| 57 | - if($class == 'Log'){ |
|
| 57 | + if ($class == 'Log') { |
|
| 58 | 58 | //can't use the instruction like "return new Log()" |
| 59 | 59 | //because we need return the reference instance of the loaded class. |
| 60 | 60 | $log = new Log(); |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | function & class_loaded($class = null){ |
| 74 | 74 | static $list = array(); |
| 75 | - if($class != null){ |
|
| 75 | + if ($class != null) { |
|
| 76 | 76 | $list[strtolower($class)] = $class; |
| 77 | 77 | } |
| 78 | 78 | return $list; |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | function & load_configurations(array $overwrite_values = array()){ |
| 82 | 82 | static $config; |
| 83 | - if(empty($config)){ |
|
| 83 | + if (empty($config)) { |
|
| 84 | 84 | $file = CONFIG_PATH . 'config.php'; |
| 85 | 85 | require_once $file; |
| 86 | 86 | |
@@ -94,53 +94,53 @@ discard block |
||
| 94 | 94 | /** |
| 95 | 95 | * @test |
| 96 | 96 | */ |
| 97 | - function get_config($key, $default = null){ |
|
| 97 | + function get_config($key, $default = null) { |
|
| 98 | 98 | static $cfg; |
| 99 | - if(empty($cfg)){ |
|
| 99 | + if (empty($cfg)) { |
|
| 100 | 100 | $cfg[0] = & load_configurations(); |
| 101 | 101 | } |
| 102 | 102 | return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - function save_to_log($level, $message, $logger = null){ |
|
| 106 | - echo 'save_to_log('.$level . ',' . $message . ',' . $logger . ")\n"; |
|
| 105 | + function save_to_log($level, $message, $logger = null) { |
|
| 106 | + echo 'save_to_log(' . $level . ',' . $message . ',' . $logger . ")\n"; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | |
| 110 | - function set_http_status_header($code = 200, $text = null){ |
|
| 110 | + function set_http_status_header($code = 200, $text = null) { |
|
| 111 | 111 | echo 'header(' . $code . ', ' . $text . ')'; |
| 112 | 112 | return true; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | |
| 116 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 116 | + function show_error($msg, $title = 'error', $logging = true) { |
|
| 117 | 117 | //show only and continue to help track of some error occured |
| 118 | 118 | echo 'show_error(' . $msg . ', ' . $title . ', ' . ($logging ? 'Y' : 'N') . ")\n"; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - function is_https(){ |
|
| 121 | + function is_https() { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * @test |
| 127 | 127 | */ |
| 128 | - function is_url($url){ |
|
| 128 | + function is_url($url) { |
|
| 129 | 129 | return preg_match('/^(http|https|ftp):\/\/(.*)/', $url); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - function php_exception_handler($ex){ |
|
| 132 | + function php_exception_handler($ex) { |
|
| 133 | 133 | //show only and continue to help track of some error occured |
| 134 | - echo 'php_exception_handler('.$ex->getMessage().', '.$ex->getFile().', '.$ex->getLine() . ")\n"; |
|
| 134 | + echo 'php_exception_handler(' . $ex->getMessage() . ', ' . $ex->getFile() . ', ' . $ex->getLine() . ")\n"; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
| 138 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 138 | + function php_error_handler($errno, $errstr, $errfile, $errline) { |
|
| 139 | 139 | //show only and continue to help track of some error occured |
| 140 | - echo 'php_error_handler('.$errno .', ' . $errstr.', ' . $errfile.', '.$errline . ")\n"; |
|
| 140 | + echo 'php_error_handler(' . $errno . ', ' . $errstr . ', ' . $errfile . ', ' . $errline . ")\n"; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - function php_shudown_handler(){ |
|
| 143 | + function php_shudown_handler() { |
|
| 144 | 144 | return true; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -148,11 +148,11 @@ discard block |
||
| 148 | 148 | /** |
| 149 | 149 | * @test |
| 150 | 150 | */ |
| 151 | - function attributes_to_string(array $attributes){ |
|
| 151 | + function attributes_to_string(array $attributes) { |
|
| 152 | 152 | $str = ' '; |
| 153 | 153 | //we check that the array passed as an argument is not empty. |
| 154 | - if(! empty($attributes)){ |
|
| 155 | - foreach($attributes as $key => $value){ |
|
| 154 | + if (!empty($attributes)) { |
|
| 155 | + foreach ($attributes as $key => $value) { |
|
| 156 | 156 | $key = trim(htmlspecialchars($key)); |
| 157 | 157 | $value = trim(htmlspecialchars($value)); |
| 158 | 158 | /* |
@@ -162,35 +162,35 @@ discard block |
||
| 162 | 162 | * $attr = array('placeholder' => 'I am a "puple"') |
| 163 | 163 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 164 | 164 | */ |
| 165 | - if($value && strpos('"', $value) !== false){ |
|
| 165 | + if ($value && strpos('"', $value) !== false) { |
|
| 166 | 166 | $value = addslashes($value); |
| 167 | 167 | } |
| 168 | - $str .= $key.' = "'.$value.'" '; |
|
| 168 | + $str .= $key . ' = "' . $value . '" '; |
|
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | //remove the space after using rtrim() |
| 172 | 172 | return rtrim($str); |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - function stringfy_vars($var){ |
|
| 175 | + function stringfy_vars($var) { |
|
| 176 | 176 | return print_r($var, true); |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | 180 | * @test |
| 181 | 181 | */ |
| 182 | - function clean_input($str){ |
|
| 183 | - if(is_array($str)){ |
|
| 182 | + function clean_input($str) { |
|
| 183 | + if (is_array($str)) { |
|
| 184 | 184 | $str = array_map('clean_input', $str); |
| 185 | 185 | } |
| 186 | - else if(is_object($str)){ |
|
| 186 | + else if (is_object($str)) { |
|
| 187 | 187 | $obj = $str; |
| 188 | 188 | foreach ($str as $var => $value) { |
| 189 | 189 | $obj->$var = clean_input($value); |
| 190 | 190 | } |
| 191 | 191 | $str = $obj; |
| 192 | 192 | } |
| 193 | - else{ |
|
| 193 | + else { |
|
| 194 | 194 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
| 195 | 195 | } |
| 196 | 196 | return $str; |
@@ -199,11 +199,11 @@ discard block |
||
| 199 | 199 | /** |
| 200 | 200 | * @test |
| 201 | 201 | */ |
| 202 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 202 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*') { |
|
| 203 | 203 | //get the string length |
| 204 | 204 | $len = strlen($str); |
| 205 | 205 | //if str is empty |
| 206 | - if($len <= 0){ |
|
| 206 | + if ($len <= 0) { |
|
| 207 | 207 | return str_repeat($hiddenChar, 6); |
| 208 | 208 | } |
| 209 | 209 | //if the length is less than startCount and endCount |
@@ -211,14 +211,14 @@ discard block |
||
| 211 | 211 | //or startCount is negative or endCount is negative |
| 212 | 212 | //return the full string hidden |
| 213 | 213 | |
| 214 | - if((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 214 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)) { |
|
| 215 | 215 | return str_repeat($hiddenChar, $len); |
| 216 | 216 | } |
| 217 | 217 | //the start non hidden string |
| 218 | 218 | $startNonHiddenStr = substr($str, 0, $startCount); |
| 219 | 219 | //the end non hidden string |
| 220 | 220 | $endNonHiddenStr = null; |
| 221 | - if($endCount > 0){ |
|
| 221 | + if ($endCount > 0) { |
|
| 222 | 222 | $endNonHiddenStr = substr($str, - $endCount); |
| 223 | 223 | } |
| 224 | 224 | //the hidden string |
@@ -227,12 +227,12 @@ discard block |
||
| 227 | 227 | return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - function set_session_config(){ |
|
| 230 | + function set_session_config() { |
|
| 231 | 231 | return true; |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | function & get_instance(){ |
| 235 | - if(! Controller::get_instance()){ |
|
| 235 | + if (!Controller::get_instance()) { |
|
| 236 | 236 | $c = new Controller(); |
| 237 | 237 | return $c; |
| 238 | 238 | } |
@@ -1,240 +1,240 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 29 | - //put the first letter of class to upper case |
|
| 30 | - $class = ucfirst($class); |
|
| 31 | - static $classes = array(); |
|
| 32 | - if(isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 33 | - return $classes[$class]; |
|
| 34 | - } |
|
| 35 | - $found = false; |
|
| 36 | - foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 37 | - $file = $path . $dir . '/' . $class . '.php'; |
|
| 38 | - if(file_exists($file)){ |
|
| 39 | - if(class_exists($class, false) === false){ |
|
| 40 | - require_once $file; |
|
| 41 | - } |
|
| 42 | - //already found |
|
| 43 | - $found = true; |
|
| 44 | - break; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - if(! $found){ |
|
| 48 | - //can't use show_error() at this time because some dependencies not yet loaded |
|
| 49 | - set_http_status_header(503); |
|
| 50 | - echo 'Cannot find the class [' . $class . ']'; |
|
| 51 | - die(); |
|
| 52 | - } |
|
| 28 | + function & class_loader($class, $dir = 'libraries', $params = null){ |
|
| 29 | + //put the first letter of class to upper case |
|
| 30 | + $class = ucfirst($class); |
|
| 31 | + static $classes = array(); |
|
| 32 | + if(isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 33 | + return $classes[$class]; |
|
| 34 | + } |
|
| 35 | + $found = false; |
|
| 36 | + foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
|
| 37 | + $file = $path . $dir . '/' . $class . '.php'; |
|
| 38 | + if(file_exists($file)){ |
|
| 39 | + if(class_exists($class, false) === false){ |
|
| 40 | + require_once $file; |
|
| 41 | + } |
|
| 42 | + //already found |
|
| 43 | + $found = true; |
|
| 44 | + break; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + if(! $found){ |
|
| 48 | + //can't use show_error() at this time because some dependencies not yet loaded |
|
| 49 | + set_http_status_header(503); |
|
| 50 | + echo 'Cannot find the class [' . $class . ']'; |
|
| 51 | + die(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /* |
|
| 54 | + /* |
|
| 55 | 55 | TODO use the best method to get the Log instance |
| 56 | 56 | */ |
| 57 | - if($class == 'Log'){ |
|
| 58 | - //can't use the instruction like "return new Log()" |
|
| 59 | - //because we need return the reference instance of the loaded class. |
|
| 60 | - $log = new Log(); |
|
| 61 | - return $log; |
|
| 62 | - } |
|
| 63 | - //track of loaded classes |
|
| 64 | - class_loaded($class); |
|
| 57 | + if($class == 'Log'){ |
|
| 58 | + //can't use the instruction like "return new Log()" |
|
| 59 | + //because we need return the reference instance of the loaded class. |
|
| 60 | + $log = new Log(); |
|
| 61 | + return $log; |
|
| 62 | + } |
|
| 63 | + //track of loaded classes |
|
| 64 | + class_loaded($class); |
|
| 65 | 65 | |
| 66 | - //record the class instance |
|
| 67 | - $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 66 | + //record the class instance |
|
| 67 | + $classes[$class] = isset($params) ? new $class($params) : new $class(); |
|
| 68 | 68 | |
| 69 | - return $classes[$class]; |
|
| 70 | - } |
|
| 69 | + return $classes[$class]; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | - function & class_loaded($class = null){ |
|
| 74 | - static $list = array(); |
|
| 75 | - if($class != null){ |
|
| 76 | - $list[strtolower($class)] = $class; |
|
| 77 | - } |
|
| 78 | - return $list; |
|
| 79 | - } |
|
| 73 | + function & class_loaded($class = null){ |
|
| 74 | + static $list = array(); |
|
| 75 | + if($class != null){ |
|
| 76 | + $list[strtolower($class)] = $class; |
|
| 77 | + } |
|
| 78 | + return $list; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - function & load_configurations(array $overwrite_values = array()){ |
|
| 82 | - static $config; |
|
| 83 | - if(empty($config)){ |
|
| 84 | - $file = CONFIG_PATH . 'config.php'; |
|
| 85 | - require_once $file; |
|
| 81 | + function & load_configurations(array $overwrite_values = array()){ |
|
| 82 | + static $config; |
|
| 83 | + if(empty($config)){ |
|
| 84 | + $file = CONFIG_PATH . 'config.php'; |
|
| 85 | + require_once $file; |
|
| 86 | 86 | |
| 87 | - foreach ($overwrite_values as $key => $value) { |
|
| 88 | - $config[$key] = $value; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - return $config; |
|
| 92 | - } |
|
| 87 | + foreach ($overwrite_values as $key => $value) { |
|
| 88 | + $config[$key] = $value; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + return $config; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @test |
|
| 96 | - */ |
|
| 97 | - function get_config($key, $default = null){ |
|
| 98 | - static $cfg; |
|
| 99 | - if(empty($cfg)){ |
|
| 100 | - $cfg[0] = & load_configurations(); |
|
| 101 | - } |
|
| 102 | - return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 103 | - } |
|
| 94 | + /** |
|
| 95 | + * @test |
|
| 96 | + */ |
|
| 97 | + function get_config($key, $default = null){ |
|
| 98 | + static $cfg; |
|
| 99 | + if(empty($cfg)){ |
|
| 100 | + $cfg[0] = & load_configurations(); |
|
| 101 | + } |
|
| 102 | + return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - function save_to_log($level, $message, $logger = null){ |
|
| 106 | - echo 'save_to_log('.$level . ',' . $message . ',' . $logger . ")\n"; |
|
| 107 | - } |
|
| 105 | + function save_to_log($level, $message, $logger = null){ |
|
| 106 | + echo 'save_to_log('.$level . ',' . $message . ',' . $logger . ")\n"; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | 109 | |
| 110 | - function set_http_status_header($code = 200, $text = null){ |
|
| 111 | - echo 'header(' . $code . ', ' . $text . ')'; |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 110 | + function set_http_status_header($code = 200, $text = null){ |
|
| 111 | + echo 'header(' . $code . ', ' . $text . ')'; |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 117 | - //show only and continue to help track of some error occured |
|
| 118 | - echo 'show_error(' . $msg . ', ' . $title . ', ' . ($logging ? 'Y' : 'N') . ")\n"; |
|
| 119 | - } |
|
| 116 | + function show_error($msg, $title = 'error', $logging = true){ |
|
| 117 | + //show only and continue to help track of some error occured |
|
| 118 | + echo 'show_error(' . $msg . ', ' . $title . ', ' . ($logging ? 'Y' : 'N') . ")\n"; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - function is_https(){ |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 121 | + function is_https(){ |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @test |
|
| 127 | - */ |
|
| 128 | - function is_url($url){ |
|
| 129 | - return preg_match('/^(http|https|ftp):\/\/(.*)/', $url); |
|
| 130 | - } |
|
| 125 | + /** |
|
| 126 | + * @test |
|
| 127 | + */ |
|
| 128 | + function is_url($url){ |
|
| 129 | + return preg_match('/^(http|https|ftp):\/\/(.*)/', $url); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - function php_exception_handler($ex){ |
|
| 133 | - //show only and continue to help track of some error occured |
|
| 134 | - echo 'php_exception_handler('.$ex->getMessage().', '.$ex->getFile().', '.$ex->getLine() . ")\n"; |
|
| 135 | - } |
|
| 132 | + function php_exception_handler($ex){ |
|
| 133 | + //show only and continue to help track of some error occured |
|
| 134 | + echo 'php_exception_handler('.$ex->getMessage().', '.$ex->getFile().', '.$ex->getLine() . ")\n"; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | |
| 138 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 139 | - //show only and continue to help track of some error occured |
|
| 140 | - echo 'php_error_handler('.$errno .', ' . $errstr.', ' . $errfile.', '.$errline . ")\n"; |
|
| 141 | - } |
|
| 138 | + function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 139 | + //show only and continue to help track of some error occured |
|
| 140 | + echo 'php_error_handler('.$errno .', ' . $errstr.', ' . $errfile.', '.$errline . ")\n"; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - function php_shudown_handler(){ |
|
| 144 | - return true; |
|
| 145 | - } |
|
| 143 | + function php_shudown_handler(){ |
|
| 144 | + return true; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * @test |
|
| 150 | - */ |
|
| 151 | - function attributes_to_string(array $attributes){ |
|
| 152 | - $str = ' '; |
|
| 153 | - //we check that the array passed as an argument is not empty. |
|
| 154 | - if(! empty($attributes)){ |
|
| 155 | - foreach($attributes as $key => $value){ |
|
| 156 | - $key = trim(htmlspecialchars($key)); |
|
| 157 | - $value = trim(htmlspecialchars($value)); |
|
| 158 | - /* |
|
| 148 | + /** |
|
| 149 | + * @test |
|
| 150 | + */ |
|
| 151 | + function attributes_to_string(array $attributes){ |
|
| 152 | + $str = ' '; |
|
| 153 | + //we check that the array passed as an argument is not empty. |
|
| 154 | + if(! empty($attributes)){ |
|
| 155 | + foreach($attributes as $key => $value){ |
|
| 156 | + $key = trim(htmlspecialchars($key)); |
|
| 157 | + $value = trim(htmlspecialchars($value)); |
|
| 158 | + /* |
|
| 159 | 159 | * To predict the case where the string to convert contains the character " |
| 160 | 160 | * we check if this is the case we add a slash to solve this problem. |
| 161 | 161 | * For example: |
| 162 | 162 | * $attr = array('placeholder' => 'I am a "puple"') |
| 163 | 163 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 164 | 164 | */ |
| 165 | - if($value && strpos('"', $value) !== false){ |
|
| 166 | - $value = addslashes($value); |
|
| 167 | - } |
|
| 168 | - $str .= $key.' = "'.$value.'" '; |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - //remove the space after using rtrim() |
|
| 172 | - return rtrim($str); |
|
| 173 | - } |
|
| 165 | + if($value && strpos('"', $value) !== false){ |
|
| 166 | + $value = addslashes($value); |
|
| 167 | + } |
|
| 168 | + $str .= $key.' = "'.$value.'" '; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + //remove the space after using rtrim() |
|
| 172 | + return rtrim($str); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - function stringfy_vars($var){ |
|
| 176 | - return print_r($var, true); |
|
| 177 | - } |
|
| 175 | + function stringfy_vars($var){ |
|
| 176 | + return print_r($var, true); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * @test |
|
| 181 | - */ |
|
| 182 | - function clean_input($str){ |
|
| 183 | - if(is_array($str)){ |
|
| 184 | - $str = array_map('clean_input', $str); |
|
| 185 | - } |
|
| 186 | - else if(is_object($str)){ |
|
| 187 | - $obj = $str; |
|
| 188 | - foreach ($str as $var => $value) { |
|
| 189 | - $obj->$var = clean_input($value); |
|
| 190 | - } |
|
| 191 | - $str = $obj; |
|
| 192 | - } |
|
| 193 | - else{ |
|
| 194 | - $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 195 | - } |
|
| 196 | - return $str; |
|
| 197 | - } |
|
| 179 | + /** |
|
| 180 | + * @test |
|
| 181 | + */ |
|
| 182 | + function clean_input($str){ |
|
| 183 | + if(is_array($str)){ |
|
| 184 | + $str = array_map('clean_input', $str); |
|
| 185 | + } |
|
| 186 | + else if(is_object($str)){ |
|
| 187 | + $obj = $str; |
|
| 188 | + foreach ($str as $var => $value) { |
|
| 189 | + $obj->$var = clean_input($value); |
|
| 190 | + } |
|
| 191 | + $str = $obj; |
|
| 192 | + } |
|
| 193 | + else{ |
|
| 194 | + $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
|
| 195 | + } |
|
| 196 | + return $str; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * @test |
|
| 201 | - */ |
|
| 202 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 203 | - //get the string length |
|
| 204 | - $len = strlen($str); |
|
| 205 | - //if str is empty |
|
| 206 | - if($len <= 0){ |
|
| 207 | - return str_repeat($hiddenChar, 6); |
|
| 208 | - } |
|
| 209 | - //if the length is less than startCount and endCount |
|
| 210 | - //or the startCount and endCount length is 0 |
|
| 211 | - //or startCount is negative or endCount is negative |
|
| 212 | - //return the full string hidden |
|
| 199 | + /** |
|
| 200 | + * @test |
|
| 201 | + */ |
|
| 202 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 203 | + //get the string length |
|
| 204 | + $len = strlen($str); |
|
| 205 | + //if str is empty |
|
| 206 | + if($len <= 0){ |
|
| 207 | + return str_repeat($hiddenChar, 6); |
|
| 208 | + } |
|
| 209 | + //if the length is less than startCount and endCount |
|
| 210 | + //or the startCount and endCount length is 0 |
|
| 211 | + //or startCount is negative or endCount is negative |
|
| 212 | + //return the full string hidden |
|
| 213 | 213 | |
| 214 | - if((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 215 | - return str_repeat($hiddenChar, $len); |
|
| 216 | - } |
|
| 217 | - //the start non hidden string |
|
| 218 | - $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 219 | - //the end non hidden string |
|
| 220 | - $endNonHiddenStr = null; |
|
| 221 | - if($endCount > 0){ |
|
| 222 | - $endNonHiddenStr = substr($str, - $endCount); |
|
| 223 | - } |
|
| 224 | - //the hidden string |
|
| 225 | - $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 214 | + if((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 215 | + return str_repeat($hiddenChar, $len); |
|
| 216 | + } |
|
| 217 | + //the start non hidden string |
|
| 218 | + $startNonHiddenStr = substr($str, 0, $startCount); |
|
| 219 | + //the end non hidden string |
|
| 220 | + $endNonHiddenStr = null; |
|
| 221 | + if($endCount > 0){ |
|
| 222 | + $endNonHiddenStr = substr($str, - $endCount); |
|
| 223 | + } |
|
| 224 | + //the hidden string |
|
| 225 | + $hiddenStr = str_repeat($hiddenChar, $len - ($startCount + $endCount)); |
|
| 226 | 226 | |
| 227 | - return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 228 | - } |
|
| 227 | + return $startNonHiddenStr . $hiddenStr . $endNonHiddenStr; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - function set_session_config(){ |
|
| 231 | - return true; |
|
| 232 | - } |
|
| 230 | + function set_session_config(){ |
|
| 231 | + return true; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - function & get_instance(){ |
|
| 235 | - if(! Controller::get_instance()){ |
|
| 236 | - $c = new Controller(); |
|
| 237 | - return $c; |
|
| 238 | - } |
|
| 239 | - return Controller::get_instance(); |
|
| 240 | - } |
|
| 234 | + function & get_instance(){ |
|
| 235 | + if(! Controller::get_instance()){ |
|
| 236 | + $c = new Controller(); |
|
| 237 | + return $c; |
|
| 238 | + } |
|
| 239 | + return Controller::get_instance(); |
|
| 240 | + } |
|
@@ -3,7 +3,7 @@ |
||
| 3 | 3 | /** |
| 4 | 4 | * Function to test private & protected method |
| 5 | 5 | */ |
| 6 | - function runPrivateOrProtectedMethod($object, $method, array $args = array()){ |
|
| 6 | + function runPrivateOrProtectedMethod($object, $method, array $args = array()) { |
|
| 7 | 7 | $r = new ReflectionClass(get_class($object)); |
| 8 | 8 | $m = $r->getMethod($method); |
| 9 | 9 | $m->setAccessible(true); |
@@ -1,11 +1,11 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - /** |
|
| 4 | - * Function to test private & protected method |
|
| 5 | - */ |
|
| 6 | - function runPrivateOrProtectedMethod($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 | 3 | \ No newline at end of file |
| 4 | + /** |
|
| 5 | + * Function to test private & protected method |
|
| 6 | + */ |
|
| 7 | + function runPrivateOrProtectedMethod($object, $method, array $args = array()){ |
|
| 8 | + $r = new ReflectionClass(get_class($object)); |
|
| 9 | + $m = $r->getMethod($method); |
|
| 10 | + $m->setAccessible(true); |
|
| 11 | + return $m->invokeArgs($object, $args); |
|
| 12 | + } |
|
| 13 | 13 | \ No newline at end of file |
@@ -87,8 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | if(is_object($logger)){ |
| 89 | 89 | $this->setLogger($logger); |
| 90 | - } |
|
| 91 | - else{ |
|
| 90 | + } else{ |
|
| 92 | 91 | $this->logger =& class_loader('Log', 'classes'); |
| 93 | 92 | $this->logger->setLogger('Library::DBSessionHandler'); |
| 94 | 93 | } |
@@ -197,8 +196,7 @@ discard block |
||
| 197 | 196 | if($this->getLoader()){ |
| 198 | 197 | $this->getLoader()->functions('user_agent'); |
| 199 | 198 | $this->getLoader()->library('Browser'); |
| 200 | - } |
|
| 201 | - else{ |
|
| 199 | + } else{ |
|
| 202 | 200 | Loader::functions('user_agent'); |
| 203 | 201 | Loader::library('Browser'); |
| 204 | 202 | } |
@@ -237,8 +235,7 @@ discard block |
||
| 237 | 235 | if($this->getLoader()){ |
| 238 | 236 | $this->getLoader()->functions('user_agent'); |
| 239 | 237 | $this->getLoader()->library('Browser'); |
| 240 | - } |
|
| 241 | - else{ |
|
| 238 | + } else{ |
|
| 242 | 239 | Loader::functions('user_agent'); |
| 243 | 240 | Loader::library('Browser'); |
| 244 | 241 | } |
@@ -264,8 +261,7 @@ discard block |
||
| 264 | 261 | //update |
| 265 | 262 | unset($params[$columns['sid']]); |
| 266 | 263 | $instance->update($sid, $params); |
| 267 | - } |
|
| 268 | - else{ |
|
| 264 | + } else{ |
|
| 269 | 265 | $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
| 270 | 266 | $instance->insert($params); |
| 271 | 267 | } |
@@ -27,11 +27,11 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
| 29 | 29 | */ |
| 30 | - if( !interface_exists('SessionHandlerInterface')){ |
|
| 30 | + if (!interface_exists('SessionHandlerInterface')) { |
|
| 31 | 31 | show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
| 34 | + class DBSessionHandler implements SessionHandlerInterface { |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The encryption method to use to encrypt session data in database |
@@ -81,25 +81,25 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | protected $loader = null; |
| 83 | 83 | |
| 84 | - public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 84 | + public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null) { |
|
| 85 | 85 | /** |
| 86 | 86 | * instance of the Log class |
| 87 | 87 | */ |
| 88 | - if(is_object($logger)){ |
|
| 88 | + if (is_object($logger)) { |
|
| 89 | 89 | $this->setLogger($logger); |
| 90 | 90 | } |
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 91 | + else { |
|
| 92 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 93 | 93 | $this->logger->setLogger('Library::DBSessionHandler'); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - if(is_object($loader)){ |
|
| 96 | + if (is_object($loader)) { |
|
| 97 | 97 | $this->setLoader($loader); |
| 98 | 98 | } |
| 99 | 99 | $this->OBJ = & get_instance(); |
| 100 | 100 | |
| 101 | 101 | |
| 102 | - if(is_object($modelInstance)){ |
|
| 102 | + if (is_object($modelInstance)) { |
|
| 103 | 103 | $this->setModelInstance($modelInstance); |
| 104 | 104 | } |
| 105 | 105 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | * Set the session secret used to encrypt the data in database |
| 109 | 109 | * @param string $secret the base64 string secret |
| 110 | 110 | */ |
| 111 | - public function setSessionSecret($secret){ |
|
| 111 | + public function setSessionSecret($secret) { |
|
| 112 | 112 | $this->sessionSecret = $secret; |
| 113 | 113 | return $this; |
| 114 | 114 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * Return the session secret |
| 118 | 118 | * @return string |
| 119 | 119 | */ |
| 120 | - public function getSessionSecret(){ |
|
| 120 | + public function getSessionSecret() { |
|
| 121 | 121 | return $this->sessionSecret; |
| 122 | 122 | } |
| 123 | 123 | |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * Set the initializer vector for openssl |
| 127 | 127 | * @param string $key the session secret used |
| 128 | 128 | */ |
| 129 | - public function setInitializerVector($key){ |
|
| 129 | + public function setInitializerVector($key) { |
|
| 130 | 130 | $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
| 131 | 131 | $key = base64_decode($key); |
| 132 | 132 | $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * Return the initializer vector |
| 138 | 138 | * @return string |
| 139 | 139 | */ |
| 140 | - public function getInitializerVector(){ |
|
| 140 | + public function getInitializerVector() { |
|
| 141 | 141 | return $this->iv; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -147,17 +147,17 @@ discard block |
||
| 147 | 147 | * @param string $sessionName the session name |
| 148 | 148 | * @return boolean |
| 149 | 149 | */ |
| 150 | - public function open($savePath, $sessionName){ |
|
| 150 | + public function open($savePath, $sessionName) { |
|
| 151 | 151 | $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
| 152 | 152 | //try to check if session secret is set before |
| 153 | 153 | $secret = $this->getSessionSecret(); |
| 154 | - if(empty($secret)){ |
|
| 154 | + if (empty($secret)) { |
|
| 155 | 155 | $secret = get_config('session_secret', null); |
| 156 | 156 | $this->setSessionSecret($secret); |
| 157 | 157 | } |
| 158 | 158 | $this->logger->info('Session secret: ' . $secret); |
| 159 | 159 | |
| 160 | - if(! $this->getModelInstance()){ |
|
| 160 | + if (!$this->getModelInstance()) { |
|
| 161 | 161 | $this->setModelInstanceFromConfig(); |
| 162 | 162 | } |
| 163 | 163 | $this->setInitializerVector($secret); |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | //set session tables columns |
| 166 | 166 | $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
| 167 | 167 | |
| 168 | - if(empty($this->sessionTableColumns)){ |
|
| 168 | + if (empty($this->sessionTableColumns)) { |
|
| 169 | 169 | show_error('The session handler is "database" but the table columns not set'); |
| 170 | 170 | } |
| 171 | 171 | $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | * Close the session |
| 182 | 182 | * @return boolean |
| 183 | 183 | */ |
| 184 | - public function close(){ |
|
| 184 | + public function close() { |
|
| 185 | 185 | $this->logger->debug('Closing database session handler'); |
| 186 | 186 | return true; |
| 187 | 187 | } |
@@ -191,28 +191,28 @@ discard block |
||
| 191 | 191 | * @param string $sid the session id to use |
| 192 | 192 | * @return string the session data in serialiaze format |
| 193 | 193 | */ |
| 194 | - public function read($sid){ |
|
| 194 | + public function read($sid) { |
|
| 195 | 195 | $this->logger->debug('Reading database session data for SID: ' . $sid); |
| 196 | 196 | $instance = $this->getModelInstance(); |
| 197 | 197 | $columns = $this->sessionTableColumns; |
| 198 | - if($this->getLoader()){ |
|
| 198 | + if ($this->getLoader()) { |
|
| 199 | 199 | $this->getLoader()->functions('user_agent'); |
| 200 | 200 | $this->getLoader()->library('Browser'); |
| 201 | 201 | } |
| 202 | - else{ |
|
| 202 | + else { |
|
| 203 | 203 | Loader::functions('user_agent'); |
| 204 | 204 | Loader::library('Browser'); |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | $ip = get_ip(); |
| 208 | 208 | $host = @gethostbyaddr($ip) or null; |
| 209 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 209 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 210 | 210 | |
| 211 | 211 | $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
| 212 | - if($data && isset($data->{$columns['sdata']})){ |
|
| 212 | + if ($data && isset($data->{$columns['sdata']})) { |
|
| 213 | 213 | //checking inactivity |
| 214 | 214 | $timeInactivity = time() - get_config('session_inactivity_time', 100); |
| 215 | - if($data->{$columns['stime']} < $timeInactivity){ |
|
| 215 | + if ($data->{$columns['stime']} < $timeInactivity) { |
|
| 216 | 216 | $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
| 217 | 217 | $this->destroy($sid); |
| 218 | 218 | return null; |
@@ -229,16 +229,16 @@ discard block |
||
| 229 | 229 | * @param mixed $data the session data to save in serialize format |
| 230 | 230 | * @return boolean |
| 231 | 231 | */ |
| 232 | - public function write($sid, $data){ |
|
| 232 | + public function write($sid, $data) { |
|
| 233 | 233 | $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
| 234 | 234 | $instance = $this->getModelInstance(); |
| 235 | 235 | $columns = $this->sessionTableColumns; |
| 236 | 236 | |
| 237 | - if($this->getLoader()){ |
|
| 237 | + if ($this->getLoader()) { |
|
| 238 | 238 | $this->getLoader()->functions('user_agent'); |
| 239 | 239 | $this->getLoader()->library('Browser'); |
| 240 | 240 | } |
| 241 | - else{ |
|
| 241 | + else { |
|
| 242 | 242 | Loader::functions('user_agent'); |
| 243 | 243 | Loader::library('Browser'); |
| 244 | 244 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $ip = get_ip(); |
| 247 | 247 | $keyValue = $instance->getKeyValue(); |
| 248 | 248 | $host = @gethostbyaddr($ip) or null; |
| 249 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 249 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 250 | 250 | $data = $this->encode($data); |
| 251 | 251 | $params = array( |
| 252 | 252 | $columns['sid'] => $sid, |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | ); |
| 260 | 260 | $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
| 261 | 261 | $exists = $instance->get($sid); |
| 262 | - if($exists){ |
|
| 262 | + if ($exists) { |
|
| 263 | 263 | $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
| 264 | 264 | //update |
| 265 | 265 | unset($params[$columns['sid']]); |
| 266 | 266 | $instance->update($sid, $params); |
| 267 | 267 | } |
| 268 | - else{ |
|
| 268 | + else { |
|
| 269 | 269 | $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
| 270 | 270 | $instance->insert($params); |
| 271 | 271 | } |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * @param string $sid the session id value |
| 279 | 279 | * @return boolean |
| 280 | 280 | */ |
| 281 | - public function destroy($sid){ |
|
| 281 | + public function destroy($sid) { |
|
| 282 | 282 | $this->logger->debug('Destroy of session data for SID: ' . $sid); |
| 283 | 283 | $instance = $this->modelInstanceName; |
| 284 | 284 | $instance->delete($sid); |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | * @param integer $maxLifetime the max lifetime |
| 291 | 291 | * @return boolean |
| 292 | 292 | */ |
| 293 | - public function gc($maxLifetime){ |
|
| 293 | + public function gc($maxLifetime) { |
|
| 294 | 294 | $instance = $this->modelInstanceName; |
| 295 | 295 | $time = time() - $maxLifetime; |
| 296 | 296 | $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | * @param mixed $data the session data to encode |
| 304 | 304 | * @return mixed the encoded session data |
| 305 | 305 | */ |
| 306 | - public function encode($data){ |
|
| 306 | + public function encode($data) { |
|
| 307 | 307 | $key = base64_decode($this->sessionSecret); |
| 308 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 308 | + $dataEncrypted = openssl_encrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | 309 | $output = base64_encode($dataEncrypted); |
| 310 | 310 | return $output; |
| 311 | 311 | } |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | * @param mixed $data the data to decode |
| 317 | 317 | * @return mixed the decoded data |
| 318 | 318 | */ |
| 319 | - public function decode($data){ |
|
| 319 | + public function decode($data) { |
|
| 320 | 320 | $key = base64_decode($this->sessionSecret); |
| 321 | 321 | $data = base64_decode($data); |
| 322 | 322 | $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | * Return the loader instance |
| 329 | 329 | * @return object Loader the loader instance |
| 330 | 330 | */ |
| 331 | - public function getLoader(){ |
|
| 331 | + public function getLoader() { |
|
| 332 | 332 | return $this->loader; |
| 333 | 333 | } |
| 334 | 334 | |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * set the loader instance for future use |
| 337 | 337 | * @param object Loader $loader the loader object |
| 338 | 338 | */ |
| 339 | - public function setLoader($loader){ |
|
| 339 | + public function setLoader($loader) { |
|
| 340 | 340 | $this->loader = $loader; |
| 341 | 341 | return $this; |
| 342 | 342 | } |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | * Return the model instance |
| 346 | 346 | * @return object DBSessionHandlerModel the model instance |
| 347 | 347 | */ |
| 348 | - public function getModelInstance(){ |
|
| 348 | + public function getModelInstance() { |
|
| 349 | 349 | return $this->modelInstanceName; |
| 350 | 350 | } |
| 351 | 351 | |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | * set the model instance for future use |
| 354 | 354 | * @param DBSessionHandlerModel $modelInstance the model object |
| 355 | 355 | */ |
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance) { |
|
| 357 | 357 | $this->modelInstanceName = $modelInstance; |
| 358 | 358 | return $this; |
| 359 | 359 | } |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * Return the Log instance |
| 363 | 363 | * @return Log |
| 364 | 364 | */ |
| 365 | - public function getLogger(){ |
|
| 365 | + public function getLogger() { |
|
| 366 | 366 | return $this->logger; |
| 367 | 367 | } |
| 368 | 368 | |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | * Set the log instance |
| 371 | 371 | * @param Log $logger the log object |
| 372 | 372 | */ |
| 373 | - public function setLogger(Log $logger){ |
|
| 373 | + public function setLogger(Log $logger) { |
|
| 374 | 374 | $this->logger = $logger; |
| 375 | 375 | return $this; |
| 376 | 376 | } |
@@ -378,18 +378,18 @@ discard block |
||
| 378 | 378 | /** |
| 379 | 379 | * Set the model instance using the configuration for session |
| 380 | 380 | */ |
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 381 | + private function setModelInstanceFromConfig() { |
|
| 382 | 382 | $modelName = get_config('session_save_path'); |
| 383 | 383 | $this->logger->info('The database session model: ' . $modelName); |
| 384 | - if($this->getLoader()){ |
|
| 384 | + if ($this->getLoader()) { |
|
| 385 | 385 | $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
| 386 | 386 | } |
| 387 | 387 | //@codeCoverageIgnoreStart |
| 388 | - else{ |
|
| 388 | + else { |
|
| 389 | 389 | Loader::model($modelName, 'dbsessionhandlerinstance'); |
| 390 | 390 | } |
| 391 | - if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 392 | - show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 391 | + if (isset($this->OBJ->dbsessionhandlerinstance) && !$this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) { |
|
| 392 | + show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 393 | 393 | } |
| 394 | 394 | //@codeCoverageIgnoreEnd |
| 395 | 395 | |
@@ -1,330 +1,330 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | - */ |
|
| 30 | - if( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | - show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
| 27 | + /** |
|
| 28 | + * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | + */ |
|
| 30 | + if( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | + show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + class DBSessionHandler implements SessionHandlerInterface{ |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The encryption method to use to encrypt session data in database |
|
| 38 | - * @const string |
|
| 39 | - */ |
|
| 40 | - const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 36 | + /** |
|
| 37 | + * The encryption method to use to encrypt session data in database |
|
| 38 | + * @const string |
|
| 39 | + */ |
|
| 40 | + const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Super global instance |
|
| 44 | - * @var object |
|
| 45 | - */ |
|
| 46 | - protected $OBJ = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Session secret to use |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - private $sessionSecret = null; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The initialisation vector to use for openssl |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $iv = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The model instance name to use after load model |
|
| 62 | - * @var object |
|
| 63 | - */ |
|
| 64 | - private $modelInstanceName = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The columns of the table to use to store session data |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $sessionTableColumns = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * The instance of the Log |
|
| 74 | - * @var Log |
|
| 75 | - */ |
|
| 76 | - private $logger; |
|
| 77 | - |
|
| 78 | - /** |
|
| 42 | + /** |
|
| 43 | + * Super global instance |
|
| 44 | + * @var object |
|
| 45 | + */ |
|
| 46 | + protected $OBJ = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Session secret to use |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + private $sessionSecret = null; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The initialisation vector to use for openssl |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $iv = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The model instance name to use after load model |
|
| 62 | + * @var object |
|
| 63 | + */ |
|
| 64 | + private $modelInstanceName = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The columns of the table to use to store session data |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $sessionTableColumns = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * The instance of the Log |
|
| 74 | + * @var Log |
|
| 75 | + */ |
|
| 76 | + private $logger; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | 79 | * Instance of the Loader class |
| 80 | 80 | * @var Loader |
| 81 | 81 | */ |
| 82 | 82 | protected $loader = null; |
| 83 | 83 | |
| 84 | - public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 85 | - /** |
|
| 86 | - * instance of the Log class |
|
| 87 | - */ |
|
| 88 | - if(is_object($logger)){ |
|
| 89 | - $this->setLogger($logger); |
|
| 90 | - } |
|
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | - $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if(is_object($loader)){ |
|
| 97 | - $this->setLoader($loader); |
|
| 98 | - } |
|
| 99 | - $this->OBJ = & get_instance(); |
|
| 84 | + public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 85 | + /** |
|
| 86 | + * instance of the Log class |
|
| 87 | + */ |
|
| 88 | + if(is_object($logger)){ |
|
| 89 | + $this->setLogger($logger); |
|
| 90 | + } |
|
| 91 | + else{ |
|
| 92 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | + $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if(is_object($loader)){ |
|
| 97 | + $this->setLoader($loader); |
|
| 98 | + } |
|
| 99 | + $this->OBJ = & get_instance(); |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | - if(is_object($modelInstance)){ |
|
| 103 | - $this->setModelInstance($modelInstance); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Set the session secret used to encrypt the data in database |
|
| 109 | - * @param string $secret the base64 string secret |
|
| 110 | - */ |
|
| 111 | - public function setSessionSecret($secret){ |
|
| 112 | - $this->sessionSecret = $secret; |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Return the session secret |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getSessionSecret(){ |
|
| 121 | - return $this->sessionSecret; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Set the initializer vector for openssl |
|
| 127 | - * @param string $key the session secret used |
|
| 128 | - */ |
|
| 129 | - public function setInitializerVector($key){ |
|
| 130 | - $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 131 | - $key = base64_decode($key); |
|
| 132 | - $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Return the initializer vector |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public function getInitializerVector(){ |
|
| 141 | - return $this->iv; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Open the database session handler, here nothing to do just return true |
|
| 146 | - * @param string $savePath the session save path |
|
| 147 | - * @param string $sessionName the session name |
|
| 148 | - * @return boolean |
|
| 149 | - */ |
|
| 150 | - public function open($savePath, $sessionName){ |
|
| 151 | - $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 152 | - //try to check if session secret is set before |
|
| 153 | - $secret = $this->getSessionSecret(); |
|
| 154 | - if(empty($secret)){ |
|
| 155 | - $secret = get_config('session_secret', null); |
|
| 156 | - $this->setSessionSecret($secret); |
|
| 157 | - } |
|
| 158 | - $this->logger->info('Session secret: ' . $secret); |
|
| 159 | - |
|
| 160 | - if(! $this->getModelInstance()){ |
|
| 161 | - $this->setModelInstanceFromConfig(); |
|
| 162 | - } |
|
| 163 | - $this->setInitializerVector($secret); |
|
| 164 | - |
|
| 165 | - //set session tables columns |
|
| 166 | - $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
|
| 167 | - |
|
| 168 | - if(empty($this->sessionTableColumns)){ |
|
| 169 | - show_error('The session handler is "database" but the table columns not set'); |
|
| 170 | - } |
|
| 171 | - $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 102 | + if(is_object($modelInstance)){ |
|
| 103 | + $this->setModelInstance($modelInstance); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Set the session secret used to encrypt the data in database |
|
| 109 | + * @param string $secret the base64 string secret |
|
| 110 | + */ |
|
| 111 | + public function setSessionSecret($secret){ |
|
| 112 | + $this->sessionSecret = $secret; |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Return the session secret |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getSessionSecret(){ |
|
| 121 | + return $this->sessionSecret; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Set the initializer vector for openssl |
|
| 127 | + * @param string $key the session secret used |
|
| 128 | + */ |
|
| 129 | + public function setInitializerVector($key){ |
|
| 130 | + $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 131 | + $key = base64_decode($key); |
|
| 132 | + $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Return the initializer vector |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public function getInitializerVector(){ |
|
| 141 | + return $this->iv; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Open the database session handler, here nothing to do just return true |
|
| 146 | + * @param string $savePath the session save path |
|
| 147 | + * @param string $sessionName the session name |
|
| 148 | + * @return boolean |
|
| 149 | + */ |
|
| 150 | + public function open($savePath, $sessionName){ |
|
| 151 | + $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 152 | + //try to check if session secret is set before |
|
| 153 | + $secret = $this->getSessionSecret(); |
|
| 154 | + if(empty($secret)){ |
|
| 155 | + $secret = get_config('session_secret', null); |
|
| 156 | + $this->setSessionSecret($secret); |
|
| 157 | + } |
|
| 158 | + $this->logger->info('Session secret: ' . $secret); |
|
| 159 | + |
|
| 160 | + if(! $this->getModelInstance()){ |
|
| 161 | + $this->setModelInstanceFromConfig(); |
|
| 162 | + } |
|
| 163 | + $this->setInitializerVector($secret); |
|
| 164 | + |
|
| 165 | + //set session tables columns |
|
| 166 | + $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
|
| 167 | + |
|
| 168 | + if(empty($this->sessionTableColumns)){ |
|
| 169 | + show_error('The session handler is "database" but the table columns not set'); |
|
| 170 | + } |
|
| 171 | + $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 172 | 172 | |
| 173 | - //delete the expired session |
|
| 174 | - $timeActivity = get_config('session_inactivity_time', 100); |
|
| 175 | - $this->gc($timeActivity); |
|
| 173 | + //delete the expired session |
|
| 174 | + $timeActivity = get_config('session_inactivity_time', 100); |
|
| 175 | + $this->gc($timeActivity); |
|
| 176 | 176 | |
| 177 | - return true; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Close the session |
|
| 182 | - * @return boolean |
|
| 183 | - */ |
|
| 184 | - public function close(){ |
|
| 185 | - $this->logger->debug('Closing database session handler'); |
|
| 186 | - return true; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the session value for the given session id |
|
| 191 | - * @param string $sid the session id to use |
|
| 192 | - * @return string the session data in serialiaze format |
|
| 193 | - */ |
|
| 194 | - public function read($sid){ |
|
| 195 | - $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 196 | - $instance = $this->getModelInstance(); |
|
| 197 | - $columns = $this->sessionTableColumns; |
|
| 198 | - if($this->getLoader()){ |
|
| 199 | - $this->getLoader()->functions('user_agent'); |
|
| 200 | - $this->getLoader()->library('Browser'); |
|
| 201 | - } |
|
| 202 | - else{ |
|
| 203 | - Loader::functions('user_agent'); |
|
| 204 | - Loader::library('Browser'); |
|
| 177 | + return true; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Close the session |
|
| 182 | + * @return boolean |
|
| 183 | + */ |
|
| 184 | + public function close(){ |
|
| 185 | + $this->logger->debug('Closing database session handler'); |
|
| 186 | + return true; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the session value for the given session id |
|
| 191 | + * @param string $sid the session id to use |
|
| 192 | + * @return string the session data in serialiaze format |
|
| 193 | + */ |
|
| 194 | + public function read($sid){ |
|
| 195 | + $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 196 | + $instance = $this->getModelInstance(); |
|
| 197 | + $columns = $this->sessionTableColumns; |
|
| 198 | + if($this->getLoader()){ |
|
| 199 | + $this->getLoader()->functions('user_agent'); |
|
| 200 | + $this->getLoader()->library('Browser'); |
|
| 201 | + } |
|
| 202 | + else{ |
|
| 203 | + Loader::functions('user_agent'); |
|
| 204 | + Loader::library('Browser'); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - $ip = get_ip(); |
|
| 208 | - $host = @gethostbyaddr($ip) or null; |
|
| 209 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 207 | + $ip = get_ip(); |
|
| 208 | + $host = @gethostbyaddr($ip) or null; |
|
| 209 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 210 | 210 | |
| 211 | - $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 212 | - if($data && isset($data->{$columns['sdata']})){ |
|
| 213 | - //checking inactivity |
|
| 214 | - $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 215 | - if($data->{$columns['stime']} < $timeInactivity){ |
|
| 216 | - $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 217 | - $this->destroy($sid); |
|
| 218 | - return null; |
|
| 219 | - } |
|
| 220 | - return $this->decode($data->{$columns['sdata']}); |
|
| 221 | - } |
|
| 222 | - $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 223 | - return null; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Save the session data |
|
| 228 | - * @param string $sid the session ID |
|
| 229 | - * @param mixed $data the session data to save in serialize format |
|
| 230 | - * @return boolean |
|
| 231 | - */ |
|
| 232 | - public function write($sid, $data){ |
|
| 233 | - $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 234 | - $instance = $this->getModelInstance(); |
|
| 235 | - $columns = $this->sessionTableColumns; |
|
| 236 | - |
|
| 237 | - if($this->getLoader()){ |
|
| 238 | - $this->getLoader()->functions('user_agent'); |
|
| 239 | - $this->getLoader()->library('Browser'); |
|
| 240 | - } |
|
| 241 | - else{ |
|
| 242 | - Loader::functions('user_agent'); |
|
| 243 | - Loader::library('Browser'); |
|
| 211 | + $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 212 | + if($data && isset($data->{$columns['sdata']})){ |
|
| 213 | + //checking inactivity |
|
| 214 | + $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 215 | + if($data->{$columns['stime']} < $timeInactivity){ |
|
| 216 | + $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 217 | + $this->destroy($sid); |
|
| 218 | + return null; |
|
| 219 | + } |
|
| 220 | + return $this->decode($data->{$columns['sdata']}); |
|
| 221 | + } |
|
| 222 | + $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 223 | + return null; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Save the session data |
|
| 228 | + * @param string $sid the session ID |
|
| 229 | + * @param mixed $data the session data to save in serialize format |
|
| 230 | + * @return boolean |
|
| 231 | + */ |
|
| 232 | + public function write($sid, $data){ |
|
| 233 | + $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 234 | + $instance = $this->getModelInstance(); |
|
| 235 | + $columns = $this->sessionTableColumns; |
|
| 236 | + |
|
| 237 | + if($this->getLoader()){ |
|
| 238 | + $this->getLoader()->functions('user_agent'); |
|
| 239 | + $this->getLoader()->library('Browser'); |
|
| 240 | + } |
|
| 241 | + else{ |
|
| 242 | + Loader::functions('user_agent'); |
|
| 243 | + Loader::library('Browser'); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $ip = get_ip(); |
|
| 247 | + $keyValue = $instance->getKeyValue(); |
|
| 248 | + $host = @gethostbyaddr($ip) or null; |
|
| 249 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 250 | + $data = $this->encode($data); |
|
| 251 | + $params = array( |
|
| 252 | + $columns['sid'] => $sid, |
|
| 253 | + $columns['sdata'] => $data, |
|
| 254 | + $columns['stime'] => time(), |
|
| 255 | + $columns['shost'] => $host, |
|
| 256 | + $columns['sbrowser'] => $browser, |
|
| 257 | + $columns['sip'] => $ip, |
|
| 258 | + $columns['skey'] => $keyValue |
|
| 259 | + ); |
|
| 260 | + $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 261 | + $exists = $instance->get($sid); |
|
| 262 | + if($exists){ |
|
| 263 | + $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 264 | + //update |
|
| 265 | + unset($params[$columns['sid']]); |
|
| 266 | + $instance->update($sid, $params); |
|
| 244 | 267 | } |
| 268 | + else{ |
|
| 269 | + $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 270 | + $instance->insert($params); |
|
| 271 | + } |
|
| 272 | + return true; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Destroy the session data for the given session id |
|
| 278 | + * @param string $sid the session id value |
|
| 279 | + * @return boolean |
|
| 280 | + */ |
|
| 281 | + public function destroy($sid){ |
|
| 282 | + $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 283 | + $instance = $this->modelInstanceName; |
|
| 284 | + $instance->delete($sid); |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 245 | 287 | |
| 246 | - $ip = get_ip(); |
|
| 247 | - $keyValue = $instance->getKeyValue(); |
|
| 248 | - $host = @gethostbyaddr($ip) or null; |
|
| 249 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 250 | - $data = $this->encode($data); |
|
| 251 | - $params = array( |
|
| 252 | - $columns['sid'] => $sid, |
|
| 253 | - $columns['sdata'] => $data, |
|
| 254 | - $columns['stime'] => time(), |
|
| 255 | - $columns['shost'] => $host, |
|
| 256 | - $columns['sbrowser'] => $browser, |
|
| 257 | - $columns['sip'] => $ip, |
|
| 258 | - $columns['skey'] => $keyValue |
|
| 259 | - ); |
|
| 260 | - $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 261 | - $exists = $instance->get($sid); |
|
| 262 | - if($exists){ |
|
| 263 | - $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 264 | - //update |
|
| 265 | - unset($params[$columns['sid']]); |
|
| 266 | - $instance->update($sid, $params); |
|
| 267 | - } |
|
| 268 | - else{ |
|
| 269 | - $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 270 | - $instance->insert($params); |
|
| 271 | - } |
|
| 272 | - return true; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Destroy the session data for the given session id |
|
| 278 | - * @param string $sid the session id value |
|
| 279 | - * @return boolean |
|
| 280 | - */ |
|
| 281 | - public function destroy($sid){ |
|
| 282 | - $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 283 | - $instance = $this->modelInstanceName; |
|
| 284 | - $instance->delete($sid); |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Clean the expire session data to save espace |
|
| 290 | - * @param integer $maxLifetime the max lifetime |
|
| 291 | - * @return boolean |
|
| 292 | - */ |
|
| 293 | - public function gc($maxLifetime){ |
|
| 294 | - $instance = $this->modelInstanceName; |
|
| 295 | - $time = time() - $maxLifetime; |
|
| 296 | - $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 297 | - $instance->deleteByTime($time); |
|
| 298 | - return true; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Encode the session data using the openssl |
|
| 303 | - * @param mixed $data the session data to encode |
|
| 304 | - * @return mixed the encoded session data |
|
| 305 | - */ |
|
| 306 | - public function encode($data){ |
|
| 307 | - $key = base64_decode($this->sessionSecret); |
|
| 308 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | - $output = base64_encode($dataEncrypted); |
|
| 310 | - return $output; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Decode the session data using the openssl |
|
| 316 | - * @param mixed $data the data to decode |
|
| 317 | - * @return mixed the decoded data |
|
| 318 | - */ |
|
| 319 | - public function decode($data){ |
|
| 320 | - $key = base64_decode($this->sessionSecret); |
|
| 321 | - $data = base64_decode($data); |
|
| 322 | - $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 323 | - return $data; |
|
| 324 | - } |
|
| 288 | + /** |
|
| 289 | + * Clean the expire session data to save espace |
|
| 290 | + * @param integer $maxLifetime the max lifetime |
|
| 291 | + * @return boolean |
|
| 292 | + */ |
|
| 293 | + public function gc($maxLifetime){ |
|
| 294 | + $instance = $this->modelInstanceName; |
|
| 295 | + $time = time() - $maxLifetime; |
|
| 296 | + $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 297 | + $instance->deleteByTime($time); |
|
| 298 | + return true; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Encode the session data using the openssl |
|
| 303 | + * @param mixed $data the session data to encode |
|
| 304 | + * @return mixed the encoded session data |
|
| 305 | + */ |
|
| 306 | + public function encode($data){ |
|
| 307 | + $key = base64_decode($this->sessionSecret); |
|
| 308 | + $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | + $output = base64_encode($dataEncrypted); |
|
| 310 | + return $output; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Decode the session data using the openssl |
|
| 316 | + * @param mixed $data the data to decode |
|
| 317 | + * @return mixed the decoded data |
|
| 318 | + */ |
|
| 319 | + public function decode($data){ |
|
| 320 | + $key = base64_decode($this->sessionSecret); |
|
| 321 | + $data = base64_decode($data); |
|
| 322 | + $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 323 | + return $data; |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | 326 | |
| 327 | - /** |
|
| 327 | + /** |
|
| 328 | 328 | * Return the loader instance |
| 329 | 329 | * @return object Loader the loader instance |
| 330 | 330 | */ |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * set the loader instance for future use |
| 337 | 337 | * @param object Loader $loader the loader object |
| 338 | 338 | */ |
| 339 | - public function setLoader($loader){ |
|
| 339 | + public function setLoader($loader){ |
|
| 340 | 340 | $this->loader = $loader; |
| 341 | 341 | return $this; |
| 342 | 342 | } |
@@ -353,47 +353,47 @@ discard block |
||
| 353 | 353 | * set the model instance for future use |
| 354 | 354 | * @param DBSessionHandlerModel $modelInstance the model object |
| 355 | 355 | */ |
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | 357 | $this->modelInstanceName = $modelInstance; |
| 358 | 358 | return $this; |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
| 362 | - * Return the Log instance |
|
| 363 | - * @return Log |
|
| 364 | - */ |
|
| 365 | - public function getLogger(){ |
|
| 366 | - return $this->logger; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the log instance |
|
| 371 | - * @param Log $logger the log object |
|
| 372 | - */ |
|
| 373 | - public function setLogger(Log $logger){ |
|
| 374 | - $this->logger = $logger; |
|
| 375 | - return $this; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Set the model instance using the configuration for session |
|
| 380 | - */ |
|
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 382 | - $modelName = get_config('session_save_path'); |
|
| 383 | - $this->logger->info('The database session model: ' . $modelName); |
|
| 384 | - if($this->getLoader()){ |
|
| 385 | - $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
|
| 386 | - } |
|
| 387 | - //@codeCoverageIgnoreStart |
|
| 388 | - else{ |
|
| 389 | - Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 362 | + * Return the Log instance |
|
| 363 | + * @return Log |
|
| 364 | + */ |
|
| 365 | + public function getLogger(){ |
|
| 366 | + return $this->logger; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the log instance |
|
| 371 | + * @param Log $logger the log object |
|
| 372 | + */ |
|
| 373 | + public function setLogger(Log $logger){ |
|
| 374 | + $this->logger = $logger; |
|
| 375 | + return $this; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Set the model instance using the configuration for session |
|
| 380 | + */ |
|
| 381 | + private function setModelInstanceFromConfig(){ |
|
| 382 | + $modelName = get_config('session_save_path'); |
|
| 383 | + $this->logger->info('The database session model: ' . $modelName); |
|
| 384 | + if($this->getLoader()){ |
|
| 385 | + $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
|
| 386 | + } |
|
| 387 | + //@codeCoverageIgnoreStart |
|
| 388 | + else{ |
|
| 389 | + Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | 390 | } |
| 391 | 391 | if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
| 392 | - show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 393 | - } |
|
| 394 | - //@codeCoverageIgnoreEnd |
|
| 392 | + show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 393 | + } |
|
| 394 | + //@codeCoverageIgnoreEnd |
|
| 395 | 395 | |
| 396 | - //set model instance |
|
| 397 | - $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
|
| 398 | - } |
|
| 399 | - } |
|
| 396 | + //set model instance |
|
| 397 | + $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
|
| 398 | + } |
|
| 399 | + } |
|
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | protected $sessionTableColumns = array(); |
| 47 | 47 | |
| 48 | - public function __construct(Database $db = null){ |
|
| 48 | + public function __construct(Database $db = null) { |
|
| 49 | 49 | parent::__construct($db); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * Return the session database table columns |
| 54 | 54 | * @return array |
| 55 | 55 | */ |
| 56 | - public function getSessionTableColumns(){ |
|
| 56 | + public function getSessionTableColumns() { |
|
| 57 | 57 | return $this->sessionTableColumns; |
| 58 | 58 | } |
| 59 | 59 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * Set the session database table columns |
| 62 | 62 | * @param array $columns the columns definition |
| 63 | 63 | */ |
| 64 | - public function setSessionTableColumns(array $columns){ |
|
| 64 | + public function setSessionTableColumns(array $columns) { |
|
| 65 | 65 | $this->sessionTableColumns = $columns; |
| 66 | 66 | return $this; |
| 67 | 67 | } |
@@ -1,39 +1,39 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * DB session handler class |
|
| 29 | - */ |
|
| 30 | - abstract class DBSessionHandlerModel extends Model { |
|
| 27 | + /** |
|
| 28 | + * DB session handler class |
|
| 29 | + */ |
|
| 30 | + abstract class DBSessionHandlerModel extends Model { |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The session table columns to use |
|
| 34 | - * @var array |
|
| 35 | - * @example |
|
| 36 | - * array( |
|
| 32 | + /** |
|
| 33 | + * The session table columns to use |
|
| 34 | + * @var array |
|
| 35 | + * @example |
|
| 36 | + * array( |
|
| 37 | 37 | 'sid' => '', //VARCHAR(255) Note: this a primary key |
| 38 | 38 | 'sdata' => '', //TEXT |
| 39 | 39 | 'stime' => '', //unix timestamp (INT|BIGINT) |
@@ -42,41 +42,41 @@ discard block |
||
| 42 | 42 | 'sbrowser' => '', //VARCHAR(255) |
| 43 | 43 | 'skey' => '' //VARCHAR(255) |
| 44 | 44 | ); |
| 45 | - */ |
|
| 46 | - protected $sessionTableColumns = array(); |
|
| 45 | + */ |
|
| 46 | + protected $sessionTableColumns = array(); |
|
| 47 | 47 | |
| 48 | - public function __construct(Database $db = null){ |
|
| 49 | - parent::__construct($db); |
|
| 50 | - } |
|
| 48 | + public function __construct(Database $db = null){ |
|
| 49 | + parent::__construct($db); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Return the session database table columns |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function getSessionTableColumns(){ |
|
| 57 | - return $this->sessionTableColumns; |
|
| 58 | - } |
|
| 52 | + /** |
|
| 53 | + * Return the session database table columns |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function getSessionTableColumns(){ |
|
| 57 | + return $this->sessionTableColumns; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Set the session database table columns |
|
| 62 | - * @param array $columns the columns definition |
|
| 63 | - */ |
|
| 64 | - public function setSessionTableColumns(array $columns){ |
|
| 65 | - $this->sessionTableColumns = $columns; |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * Set the session database table columns |
|
| 62 | + * @param array $columns the columns definition |
|
| 63 | + */ |
|
| 64 | + public function setSessionTableColumns(array $columns){ |
|
| 65 | + $this->sessionTableColumns = $columns; |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Delete the expire session |
|
| 71 | - * @param int $time the unix timestamp |
|
| 72 | - * @return int affected rows |
|
| 73 | - */ |
|
| 74 | - abstract public function deleteByTime($time); |
|
| 69 | + /** |
|
| 70 | + * Delete the expire session |
|
| 71 | + * @param int $time the unix timestamp |
|
| 72 | + * @return int affected rows |
|
| 73 | + */ |
|
| 74 | + abstract public function deleteByTime($time); |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * How to get the value of the table column key. Generally is the session key |
|
| 79 | - * @return mixed the key value like used to identify the data |
|
| 80 | - */ |
|
| 81 | - abstract public function getKeyValue(); |
|
| 82 | - } |
|
| 77 | + /** |
|
| 78 | + * How to get the value of the table column key. Generally is the session key |
|
| 79 | + * @return mixed the key value like used to identify the data |
|
| 80 | + */ |
|
| 81 | + abstract public function getKeyValue(); |
|
| 82 | + } |
|
@@ -24,20 +24,20 @@ |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
| 27 | - public function testFunctionGetConfigKeyNotExist(){ |
|
| 27 | + public function testFunctionGetConfigKeyNotExist() { |
|
| 28 | 28 | $key = 'foo'; |
| 29 | 29 | $cfg = get_config($key); |
| 30 | 30 | $this->assertNull($cfg); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function testFunctionGetConfigKeyNotExistUsingDefaultValue(){ |
|
| 33 | + public function testFunctionGetConfigKeyNotExistUsingDefaultValue() { |
|
| 34 | 34 | $key = 'foo'; |
| 35 | 35 | $expected = 'bar'; |
| 36 | 36 | $cfg = get_config($key, $expected); |
| 37 | 37 | $this->assertEquals($cfg, $expected); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - public function testFunctionGetConfigAfterSet(){ |
|
| 40 | + public function testFunctionGetConfigAfterSet() { |
|
| 41 | 41 | $key = 'foo'; |
| 42 | 42 | $expected = 'bar'; |
| 43 | 43 | $c = new Config(); |
@@ -1,50 +1,50 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class CommonTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class CommonTest 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 | 26 | |
| 27 | - public function testFunctionGetConfigKeyNotExist(){ |
|
| 28 | - $key = 'foo'; |
|
| 29 | - $cfg = get_config($key); |
|
| 30 | - $this->assertNull($cfg); |
|
| 31 | - } |
|
| 27 | + public function testFunctionGetConfigKeyNotExist(){ |
|
| 28 | + $key = 'foo'; |
|
| 29 | + $cfg = get_config($key); |
|
| 30 | + $this->assertNull($cfg); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function testFunctionGetConfigKeyNotExistUsingDefaultValue(){ |
|
| 34 | - $key = 'foo'; |
|
| 35 | - $expected = 'bar'; |
|
| 36 | - $cfg = get_config($key, $expected); |
|
| 37 | - $this->assertEquals($cfg, $expected); |
|
| 38 | - } |
|
| 33 | + public function testFunctionGetConfigKeyNotExistUsingDefaultValue(){ |
|
| 34 | + $key = 'foo'; |
|
| 35 | + $expected = 'bar'; |
|
| 36 | + $cfg = get_config($key, $expected); |
|
| 37 | + $this->assertEquals($cfg, $expected); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function testFunctionGetConfigAfterSet(){ |
|
| 41 | - $key = 'foo'; |
|
| 42 | - $expected = 'bar'; |
|
| 43 | - $c = new Config(); |
|
| 44 | - $c->init(); |
|
| 45 | - $c->set($key, $expected); |
|
| 46 | - $cfg = get_config($key); |
|
| 47 | - $this->assertEquals($cfg, $expected); |
|
| 48 | - } |
|
| 40 | + public function testFunctionGetConfigAfterSet(){ |
|
| 41 | + $key = 'foo'; |
|
| 42 | + $expected = 'bar'; |
|
| 43 | + $c = new Config(); |
|
| 44 | + $c->init(); |
|
| 45 | + $c->set($key, $expected); |
|
| 46 | + $cfg = get_config($key); |
|
| 47 | + $this->assertEquals($cfg, $expected); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - } |
|
| 51 | 50 | \ No newline at end of file |
| 51 | + } |
|
| 52 | 52 | \ No newline at end of file |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class ApcCache implements CacheInterface{ |
|
| 27 | + class ApcCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The logger instance |
@@ -33,19 +33,19 @@ discard block |
||
| 33 | 33 | private $logger; |
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct(Log $logger = null){ |
|
| 37 | - if(! $this->isSupported()){ |
|
| 36 | + public function __construct(Log $logger = null) { |
|
| 37 | + if (!$this->isSupported()) { |
|
| 38 | 38 | show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * instance of the Log class |
| 43 | 43 | */ |
| 44 | - if(is_object($logger)){ |
|
| 44 | + if (is_object($logger)) { |
|
| 45 | 45 | $this->logger = $logger; |
| 46 | 46 | } |
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 47 | + else { |
|
| 48 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 49 | 49 | $this->logger->setLogger('Library::ApcCache'); |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -55,21 +55,21 @@ discard block |
||
| 55 | 55 | * @param string $key the key to identify the cache data |
| 56 | 56 | * @return mixed the cache data if exists else return false |
| 57 | 57 | */ |
| 58 | - public function get($key){ |
|
| 59 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | + public function get($key) { |
|
| 59 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 60 | 60 | $success = false; |
| 61 | 61 | $data = apc_fetch($key, $success); |
| 62 | - if($success === false){ |
|
| 63 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 62 | + if ($success === false) { |
|
| 63 | + $this->logger->info('No cache found for the key [' . $key . '], return false'); |
|
| 64 | 64 | return false; |
| 65 | 65 | } |
| 66 | - else{ |
|
| 66 | + else { |
|
| 67 | 67 | $cacheInfo = $this->_getCacheInfo($key); |
| 68 | 68 | $expire = time(); |
| 69 | - if($cacheInfo){ |
|
| 69 | + if ($cacheInfo) { |
|
| 70 | 70 | $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
| 71 | 71 | } |
| 72 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 72 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | 73 | return $data; |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -82,16 +82,16 @@ discard block |
||
| 82 | 82 | * @param integer $ttl the cache life time |
| 83 | 83 | * @return boolean true if success otherwise will return false |
| 84 | 84 | */ |
| 85 | - public function set($key, $data, $ttl = 0){ |
|
| 85 | + public function set($key, $data, $ttl = 0) { |
|
| 86 | 86 | $expire = time() + $ttl; |
| 87 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 87 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | 88 | $result = apc_store($key, $data, $ttl); |
| 89 | - if($result === false){ |
|
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 89 | + if ($result === false) { |
|
| 90 | + $this->logger->error('Can not write cache data for the key [' . $key . '], return false'); |
|
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 93 | + else { |
|
| 94 | + $this->logger->info('Cache data saved for the key [' . $key . ']'); |
|
| 95 | 95 | return true; |
| 96 | 96 | } |
| 97 | 97 | } |
@@ -103,15 +103,15 @@ discard block |
||
| 103 | 103 | * @return boolean true if the cache is deleted, false if can't delete |
| 104 | 104 | * the cache or the cache with the given key not exist |
| 105 | 105 | */ |
| 106 | - public function delete($key){ |
|
| 107 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 106 | + public function delete($key) { |
|
| 107 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 108 | 108 | $cacheInfo = $this->_getCacheInfo($key); |
| 109 | - if($cacheInfo === false){ |
|
| 109 | + if ($cacheInfo === false) { |
|
| 110 | 110 | $this->logger->info('This cache data does not exists skipping'); |
| 111 | 111 | return false; |
| 112 | 112 | } |
| 113 | - else{ |
|
| 114 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 113 | + else { |
|
| 114 | + $this->logger->info('Found cache data for the key [' . $key . '] remove it'); |
|
| 115 | 115 | return apc_delete($key) === true; |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -124,10 +124,10 @@ discard block |
||
| 124 | 124 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 125 | 125 | * 'ttl' => the time to live of the cache in second |
| 126 | 126 | */ |
| 127 | - public function getInfo($key){ |
|
| 128 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 127 | + public function getInfo($key) { |
|
| 128 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 129 | 129 | $cacheInfos = $this->_getCacheInfo($key); |
| 130 | - if($cacheInfos){ |
|
| 130 | + if ($cacheInfos) { |
|
| 131 | 131 | $data = array( |
| 132 | 132 | 'mtime' => $cacheInfos['creation_time'], |
| 133 | 133 | 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | ); |
| 136 | 136 | return $data; |
| 137 | 137 | } |
| 138 | - else{ |
|
| 138 | + else { |
|
| 139 | 139 | $this->logger->info('This cache does not exists skipping'); |
| 140 | 140 | return false; |
| 141 | 141 | } |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | /** |
| 146 | 146 | * Used to delete expired cache data |
| 147 | 147 | */ |
| 148 | - public function deleteExpiredCache(){ |
|
| 148 | + public function deleteExpiredCache() { |
|
| 149 | 149 | //for APC[u] is done automatically |
| 150 | 150 | return true; |
| 151 | 151 | } |
@@ -153,14 +153,14 @@ discard block |
||
| 153 | 153 | /** |
| 154 | 154 | * Remove all cache data |
| 155 | 155 | */ |
| 156 | - public function clean(){ |
|
| 156 | + public function clean() { |
|
| 157 | 157 | $this->logger->debug('Deleting of all cache data'); |
| 158 | 158 | $cacheInfos = apc_cache_info('user'); |
| 159 | - if(empty($cacheInfos['cache_list'])){ |
|
| 159 | + if (empty($cacheInfos['cache_list'])) { |
|
| 160 | 160 | $this->logger->info('No cache data were found skipping'); |
| 161 | 161 | return false; |
| 162 | 162 | } |
| 163 | - else{ |
|
| 163 | + else { |
|
| 164 | 164 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 165 | 165 | return apc_clear_cache('user'); |
| 166 | 166 | } |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @return bool |
| 174 | 174 | */ |
| 175 | - public function isSupported(){ |
|
| 175 | + public function isSupported() { |
|
| 176 | 176 | return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
| 177 | 177 | } |
| 178 | 178 | |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | * Return the Log instance |
| 181 | 181 | * @return Log |
| 182 | 182 | */ |
| 183 | - public function getLogger(){ |
|
| 183 | + public function getLogger() { |
|
| 184 | 184 | return $this->logger; |
| 185 | 185 | } |
| 186 | 186 | |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | * Set the log instance |
| 189 | 189 | * @param Log $logger the log object |
| 190 | 190 | */ |
| 191 | - public function setLogger(Log $logger){ |
|
| 191 | + public function setLogger(Log $logger) { |
|
| 192 | 192 | $this->logger = $logger; |
| 193 | 193 | return $this; |
| 194 | 194 | } |
@@ -199,12 +199,12 @@ discard block |
||
| 199 | 199 | * @param string $key the cache key to get the cache information |
| 200 | 200 | * @return boolean|array |
| 201 | 201 | */ |
| 202 | - private function _getCacheInfo($key){ |
|
| 202 | + private function _getCacheInfo($key) { |
|
| 203 | 203 | $caches = apc_cache_info('user'); |
| 204 | - if(! empty($caches['cache_list'])){ |
|
| 204 | + if (!empty($caches['cache_list'])) { |
|
| 205 | 205 | $cacheLists = $caches['cache_list']; |
| 206 | - foreach ($cacheLists as $c){ |
|
| 207 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 206 | + foreach ($cacheLists as $c) { |
|
| 207 | + if (isset($c['info']) && $c['info'] === $key) { |
|
| 208 | 208 | return $c; |
| 209 | 209 | } |
| 210 | 210 | } |
@@ -43,8 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | if(is_object($logger)){ |
| 45 | 45 | $this->logger = $logger; |
| 46 | - } |
|
| 47 | - else{ |
|
| 46 | + } else{ |
|
| 48 | 47 | $this->logger =& class_loader('Log', 'classes'); |
| 49 | 48 | $this->logger->setLogger('Library::ApcCache'); |
| 50 | 49 | } |
@@ -62,8 +61,7 @@ discard block |
||
| 62 | 61 | if($success === false){ |
| 63 | 62 | $this->logger->info('No cache found for the key ['. $key .'], return false'); |
| 64 | 63 | return false; |
| 65 | - } |
|
| 66 | - else{ |
|
| 64 | + } else{ |
|
| 67 | 65 | $cacheInfo = $this->_getCacheInfo($key); |
| 68 | 66 | $expire = time(); |
| 69 | 67 | if($cacheInfo){ |
@@ -89,8 +87,7 @@ discard block |
||
| 89 | 87 | if($result === false){ |
| 90 | 88 | $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
| 91 | 89 | return false; |
| 92 | - } |
|
| 93 | - else{ |
|
| 90 | + } else{ |
|
| 94 | 91 | $this->logger->info('Cache data saved for the key ['. $key .']'); |
| 95 | 92 | return true; |
| 96 | 93 | } |
@@ -109,8 +106,7 @@ discard block |
||
| 109 | 106 | if($cacheInfo === false){ |
| 110 | 107 | $this->logger->info('This cache data does not exists skipping'); |
| 111 | 108 | return false; |
| 112 | - } |
|
| 113 | - else{ |
|
| 109 | + } else{ |
|
| 114 | 110 | $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
| 115 | 111 | return apc_delete($key) === true; |
| 116 | 112 | } |
@@ -134,8 +130,7 @@ discard block |
||
| 134 | 130 | 'ttl' => $cacheInfos['ttl'] |
| 135 | 131 | ); |
| 136 | 132 | return $data; |
| 137 | - } |
|
| 138 | - else{ |
|
| 133 | + } else{ |
|
| 139 | 134 | $this->logger->info('This cache does not exists skipping'); |
| 140 | 135 | return false; |
| 141 | 136 | } |
@@ -159,8 +154,7 @@ discard block |
||
| 159 | 154 | if(empty($cacheInfos['cache_list'])){ |
| 160 | 155 | $this->logger->info('No cache data were found skipping'); |
| 161 | 156 | return false; |
| 162 | - } |
|
| 163 | - else{ |
|
| 157 | + } else{ |
|
| 164 | 158 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 165 | 159 | return apc_clear_cache('user'); |
| 166 | 160 | } |
@@ -1,215 +1,215 @@ |
||
| 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 ApcCache implements CacheInterface{ |
|
| 27 | + class ApcCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private $logger; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct(Log $logger = null){ |
|
| 37 | - if(! $this->isSupported()){ |
|
| 38 | - show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | - } |
|
| 36 | + public function __construct(Log $logger = null){ |
|
| 37 | + if(! $this->isSupported()){ |
|
| 38 | + show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * instance of the Log class |
|
| 43 | - */ |
|
| 44 | - if(is_object($logger)){ |
|
| 45 | - $this->logger = $logger; |
|
| 46 | - } |
|
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | - } |
|
| 51 | - } |
|
| 41 | + /** |
|
| 42 | + * instance of the Log class |
|
| 43 | + */ |
|
| 44 | + if(is_object($logger)){ |
|
| 45 | + $this->logger = $logger; |
|
| 46 | + } |
|
| 47 | + else{ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * This is used to get the cache data using the key |
|
| 55 | - * @param string $key the key to identify the cache data |
|
| 56 | - * @return mixed the cache data if exists else return false |
|
| 57 | - */ |
|
| 58 | - public function get($key){ |
|
| 59 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | - $success = false; |
|
| 61 | - $data = apc_fetch($key, $success); |
|
| 62 | - if($success === false){ |
|
| 63 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | - return false; |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | - $expire = time(); |
|
| 69 | - if($cacheInfo){ |
|
| 70 | - $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | - } |
|
| 72 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | - return $data; |
|
| 74 | - } |
|
| 75 | - } |
|
| 53 | + /** |
|
| 54 | + * This is used to get the cache data using the key |
|
| 55 | + * @param string $key the key to identify the cache data |
|
| 56 | + * @return mixed the cache data if exists else return false |
|
| 57 | + */ |
|
| 58 | + public function get($key){ |
|
| 59 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | + $success = false; |
|
| 61 | + $data = apc_fetch($key, $success); |
|
| 62 | + if($success === false){ |
|
| 63 | + $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | + $expire = time(); |
|
| 69 | + if($cacheInfo){ |
|
| 70 | + $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | + } |
|
| 72 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | + return $data; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Save data to the cache |
|
| 80 | - * @param string $key the key to identify this cache data |
|
| 81 | - * @param mixed $data the cache data to be saved |
|
| 82 | - * @param integer $ttl the cache life time |
|
| 83 | - * @return boolean true if success otherwise will return false |
|
| 84 | - */ |
|
| 85 | - public function set($key, $data, $ttl = 0){ |
|
| 86 | - $expire = time() + $ttl; |
|
| 87 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | - $result = apc_store($key, $data, $ttl); |
|
| 89 | - if($result === false){ |
|
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - } |
|
| 78 | + /** |
|
| 79 | + * Save data to the cache |
|
| 80 | + * @param string $key the key to identify this cache data |
|
| 81 | + * @param mixed $data the cache data to be saved |
|
| 82 | + * @param integer $ttl the cache life time |
|
| 83 | + * @return boolean true if success otherwise will return false |
|
| 84 | + */ |
|
| 85 | + public function set($key, $data, $ttl = 0){ |
|
| 86 | + $expire = time() + $ttl; |
|
| 87 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | + $result = apc_store($key, $data, $ttl); |
|
| 89 | + if($result === false){ |
|
| 90 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + else{ |
|
| 94 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Delete the cache data for given key |
|
| 102 | - * @param string $key the key for cache to be deleted |
|
| 103 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | - * the cache or the cache with the given key not exist |
|
| 105 | - */ |
|
| 106 | - public function delete($key){ |
|
| 107 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | - if($cacheInfo === false){ |
|
| 110 | - $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - else{ |
|
| 114 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | - return apc_delete($key) === true; |
|
| 116 | - } |
|
| 117 | - } |
|
| 100 | + /** |
|
| 101 | + * Delete the cache data for given key |
|
| 102 | + * @param string $key the key for cache to be deleted |
|
| 103 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | + * the cache or the cache with the given key not exist |
|
| 105 | + */ |
|
| 106 | + public function delete($key){ |
|
| 107 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | + if($cacheInfo === false){ |
|
| 110 | + $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + else{ |
|
| 114 | + $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | + return apc_delete($key) === true; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get the cache information for given key |
|
| 121 | - * @param string $key the key for cache to get the information for |
|
| 122 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | - * 'ttl' => the time to live of the cache in second |
|
| 126 | - */ |
|
| 127 | - public function getInfo($key){ |
|
| 128 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | - $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | - if($cacheInfos){ |
|
| 131 | - $data = array( |
|
| 132 | - 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | - 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | - 'ttl' => $cacheInfos['ttl'] |
|
| 135 | - ); |
|
| 136 | - return $data; |
|
| 137 | - } |
|
| 138 | - else{ |
|
| 139 | - $this->logger->info('This cache does not exists skipping'); |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - } |
|
| 119 | + /** |
|
| 120 | + * Get the cache information for given key |
|
| 121 | + * @param string $key the key for cache to get the information for |
|
| 122 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | + * 'ttl' => the time to live of the cache in second |
|
| 126 | + */ |
|
| 127 | + public function getInfo($key){ |
|
| 128 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | + $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | + if($cacheInfos){ |
|
| 131 | + $data = array( |
|
| 132 | + 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | + 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | + 'ttl' => $cacheInfos['ttl'] |
|
| 135 | + ); |
|
| 136 | + return $data; |
|
| 137 | + } |
|
| 138 | + else{ |
|
| 139 | + $this->logger->info('This cache does not exists skipping'); |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Used to delete expired cache data |
|
| 147 | - */ |
|
| 148 | - public function deleteExpiredCache(){ |
|
| 149 | - //for APC[u] is done automatically |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 145 | + /** |
|
| 146 | + * Used to delete expired cache data |
|
| 147 | + */ |
|
| 148 | + public function deleteExpiredCache(){ |
|
| 149 | + //for APC[u] is done automatically |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Remove all cache data |
|
| 155 | - */ |
|
| 156 | - public function clean(){ |
|
| 157 | - $this->logger->debug('Deleting of all cache data'); |
|
| 158 | - $cacheInfos = apc_cache_info('user'); |
|
| 159 | - if(empty($cacheInfos['cache_list'])){ |
|
| 160 | - $this->logger->info('No cache data were found skipping'); |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | - return apc_clear_cache('user'); |
|
| 166 | - } |
|
| 167 | - } |
|
| 153 | + /** |
|
| 154 | + * Remove all cache data |
|
| 155 | + */ |
|
| 156 | + public function clean(){ |
|
| 157 | + $this->logger->debug('Deleting of all cache data'); |
|
| 158 | + $cacheInfos = apc_cache_info('user'); |
|
| 159 | + if(empty($cacheInfos['cache_list'])){ |
|
| 160 | + $this->logger->info('No cache data were found skipping'); |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | + return apc_clear_cache('user'); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check whether the cache feature for the handle is supported |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function isSupported(){ |
|
| 176 | - return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | - } |
|
| 170 | + /** |
|
| 171 | + * Check whether the cache feature for the handle is supported |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function isSupported(){ |
|
| 176 | + return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Return the Log instance |
|
| 181 | - * @return Log |
|
| 182 | - */ |
|
| 183 | - public function getLogger(){ |
|
| 184 | - return $this->logger; |
|
| 185 | - } |
|
| 179 | + /** |
|
| 180 | + * Return the Log instance |
|
| 181 | + * @return Log |
|
| 182 | + */ |
|
| 183 | + public function getLogger(){ |
|
| 184 | + return $this->logger; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Set the log instance |
|
| 189 | - * @param Log $logger the log object |
|
| 190 | - */ |
|
| 191 | - public function setLogger(Log $logger){ |
|
| 192 | - $this->logger = $logger; |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 187 | + /** |
|
| 188 | + * Set the log instance |
|
| 189 | + * @param Log $logger the log object |
|
| 190 | + */ |
|
| 191 | + public function setLogger(Log $logger){ |
|
| 192 | + $this->logger = $logger; |
|
| 193 | + return $this; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Return the array of cache information |
|
| 198 | - * |
|
| 199 | - * @param string $key the cache key to get the cache information |
|
| 200 | - * @return boolean|array |
|
| 201 | - */ |
|
| 202 | - private function _getCacheInfo($key){ |
|
| 203 | - $caches = apc_cache_info('user'); |
|
| 204 | - if(! empty($caches['cache_list'])){ |
|
| 205 | - $cacheLists = $caches['cache_list']; |
|
| 206 | - foreach ($cacheLists as $c){ |
|
| 207 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | - return $c; |
|
| 209 | - } |
|
| 210 | - } |
|
| 196 | + /** |
|
| 197 | + * Return the array of cache information |
|
| 198 | + * |
|
| 199 | + * @param string $key the cache key to get the cache information |
|
| 200 | + * @return boolean|array |
|
| 201 | + */ |
|
| 202 | + private function _getCacheInfo($key){ |
|
| 203 | + $caches = apc_cache_info('user'); |
|
| 204 | + if(! empty($caches['cache_list'])){ |
|
| 205 | + $cacheLists = $caches['cache_list']; |
|
| 206 | + foreach ($cacheLists as $c){ |
|
| 207 | + if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | + return $c; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - } |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 212 | + } |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | if(is_object($logger)){ |
| 50 | 50 | $this->logger = $logger; |
| 51 | - } |
|
| 52 | - else{ |
|
| 51 | + } else{ |
|
| 53 | 52 | $this->logger =& class_loader('Log', 'classes'); |
| 54 | 53 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 54 | } |
@@ -95,8 +94,7 @@ discard block |
||
| 95 | 94 | // Unlinking when the file was expired |
| 96 | 95 | unlink($filePath); |
| 97 | 96 | return false; |
| 98 | - } |
|
| 99 | - else{ |
|
| 97 | + } else{ |
|
| 100 | 98 | $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
| 101 | 99 | return $data['data']; |
| 102 | 100 | } |
@@ -133,8 +131,7 @@ discard block |
||
| 133 | 131 | $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
| 134 | 132 | fclose($handle); |
| 135 | 133 | return false; |
| 136 | - } |
|
| 137 | - else{ |
|
| 134 | + } else{ |
|
| 138 | 135 | $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
| 139 | 136 | fclose($handle); |
| 140 | 137 | chmod($filePath, 0640); |
@@ -156,8 +153,7 @@ discard block |
||
| 156 | 153 | if(! file_exists($filePath)){ |
| 157 | 154 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 155 | return false; |
| 159 | - } |
|
| 160 | - else{ |
|
| 156 | + } else{ |
|
| 161 | 157 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 162 | 158 | unlink($filePath); |
| 163 | 159 | return true; |
@@ -179,16 +175,14 @@ discard block |
||
| 179 | 175 | if(! file_exists($filePath)){ |
| 180 | 176 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 177 | return false; |
| 182 | - } |
|
| 183 | - else{ |
|
| 178 | + } else{ |
|
| 184 | 179 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 185 | 180 | $data = file_get_contents($filePath); |
| 186 | 181 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | 182 | if(! $data){ |
| 188 | 183 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 189 | 184 | return false; |
| 190 | - } |
|
| 191 | - else{ |
|
| 185 | + } else{ |
|
| 192 | 186 | $this->logger->info('This cache data is OK check for expire'); |
| 193 | 187 | if(isset($data['expire']) && $data['expire'] > time()){ |
| 194 | 188 | $this->logger->info('This cache not yet expired return cache informations'); |
@@ -198,8 +192,7 @@ discard block |
||
| 198 | 192 | 'ttl' => $data['ttl'] |
| 199 | 193 | ); |
| 200 | 194 | return $info; |
| 201 | - } |
|
| 202 | - else{ |
|
| 195 | + } else{ |
|
| 203 | 196 | $this->logger->info('This cache already expired return false'); |
| 204 | 197 | return false; |
| 205 | 198 | } |
@@ -216,8 +209,7 @@ discard block |
||
| 216 | 209 | $list = glob(CACHE_PATH . '*.cache'); |
| 217 | 210 | if(! $list){ |
| 218 | 211 | $this->logger->info('No cache files were found skipping'); |
| 219 | - } |
|
| 220 | - else{ |
|
| 212 | + } else{ |
|
| 221 | 213 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 222 | 214 | foreach ($list as $file) { |
| 223 | 215 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -225,12 +217,10 @@ discard block |
||
| 225 | 217 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 226 | 218 | if(! $data){ |
| 227 | 219 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 228 | - } |
|
| 229 | - else if(time() > $data['expire']){ |
|
| 220 | + } else if(time() > $data['expire']){ |
|
| 230 | 221 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 231 | 222 | unlink($file); |
| 232 | - } |
|
| 233 | - else{ |
|
| 223 | + } else{ |
|
| 234 | 224 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 235 | 225 | } |
| 236 | 226 | } |
@@ -245,8 +235,7 @@ discard block |
||
| 245 | 235 | $list = glob(CACHE_PATH . '*.cache'); |
| 246 | 236 | if(! $list){ |
| 247 | 237 | $this->logger->info('No cache files were found skipping'); |
| 248 | - } |
|
| 249 | - else{ |
|
| 238 | + } else{ |
|
| 250 | 239 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 251 | 240 | foreach ($list as $file) { |
| 252 | 241 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -273,8 +262,7 @@ discard block |
||
| 273 | 262 | |
| 274 | 263 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 275 | 264 | $this->compressCacheData = false; |
| 276 | - } |
|
| 277 | - else{ |
|
| 265 | + } else{ |
|
| 278 | 266 | $this->compressCacheData = $status; |
| 279 | 267 | } |
| 280 | 268 | return $this; |
@@ -1,318 +1,318 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Whether to enable compression of the cache data file. |
|
| 31 | - * @var boolean |
|
| 32 | - */ |
|
| 33 | - private $compressCacheData = true; |
|
| 29 | + /** |
|
| 30 | + * Whether to enable compression of the cache data file. |
|
| 31 | + * @var boolean |
|
| 32 | + */ |
|
| 33 | + private $compressCacheData = true; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private $logger; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 44 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | - } |
|
| 46 | - /** |
|
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 42 | + public function __construct(Log $logger = null){ |
|
| 43 | + if(! $this->isSupported()){ |
|
| 44 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 45 | + } |
|
| 46 | + /** |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | - if(! extension_loaded('zlib')){ |
|
| 59 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | - $this->compressCacheData = false; |
|
| 61 | - } |
|
| 62 | - } |
|
| 57 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 58 | + if(! extension_loaded('zlib')){ |
|
| 59 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 60 | + $this->compressCacheData = false; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * This is used to get the cache data using the key |
|
| 66 | - * @param string $key the key to identify the cache data |
|
| 67 | - * @return mixed the cache data if exists else return false |
|
| 68 | - */ |
|
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | - $filePath = $this->getFilePath($key); |
|
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - // Getting a shared lock |
|
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 103 | - } |
|
| 64 | + /** |
|
| 65 | + * This is used to get the cache data using the key |
|
| 66 | + * @param string $key the key to identify the cache data |
|
| 67 | + * @return mixed the cache data if exists else return false |
|
| 68 | + */ |
|
| 69 | + public function get($key){ |
|
| 70 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 71 | + $filePath = $this->getFilePath($key); |
|
| 72 | + if(! file_exists($filePath)){ |
|
| 73 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath,'r'); |
|
| 78 | + if(! is_resource($handle)){ |
|
| 79 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + // Getting a shared lock |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Save data to the cache |
|
| 108 | - * @param string $key the key to identify this cache data |
|
| 109 | - * @param mixed $data the cache data |
|
| 110 | - * @param integer $ttl the cache life time |
|
| 111 | - * @return boolean true if success otherwise will return false |
|
| 112 | - */ |
|
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 114 | - $expire = time() + $ttl; |
|
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | - $filePath = $this->getFilePath($key); |
|
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | - //Serializing along with the TTL |
|
| 124 | - $cacheData = serialize(array( |
|
| 125 | - 'mtime' => time(), |
|
| 126 | - 'expire' => $expire, |
|
| 127 | - 'data' => $data, |
|
| 128 | - 'ttl' => $ttl |
|
| 129 | - ) |
|
| 130 | - ); |
|
| 131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | - fclose($handle); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | - fclose($handle); |
|
| 140 | - chmod($filePath, 0640); |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 106 | + /** |
|
| 107 | + * Save data to the cache |
|
| 108 | + * @param string $key the key to identify this cache data |
|
| 109 | + * @param mixed $data the cache data |
|
| 110 | + * @param integer $ttl the cache life time |
|
| 111 | + * @return boolean true if success otherwise will return false |
|
| 112 | + */ |
|
| 113 | + public function set($key, $data, $ttl = 0){ |
|
| 114 | + $expire = time() + $ttl; |
|
| 115 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | + $filePath = $this->getFilePath($key); |
|
| 117 | + $handle = fopen($filePath,'w'); |
|
| 118 | + if(! is_resource($handle)){ |
|
| 119 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 123 | + //Serializing along with the TTL |
|
| 124 | + $cacheData = serialize(array( |
|
| 125 | + 'mtime' => time(), |
|
| 126 | + 'expire' => $expire, |
|
| 127 | + 'data' => $data, |
|
| 128 | + 'ttl' => $ttl |
|
| 129 | + ) |
|
| 130 | + ); |
|
| 131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | + if(! $result){ |
|
| 133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | + fclose($handle); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + else{ |
|
| 138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | + fclose($handle); |
|
| 140 | + chmod($filePath, 0640); |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Delete the cache data for given key |
|
| 148 | - * @param string $key the key for cache to be deleted |
|
| 149 | - * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | - * the cache or the cache with the given key not exist |
|
| 151 | - */ |
|
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | - $filePath = $this->getFilePath($key); |
|
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 157 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | - unlink($filePath); |
|
| 163 | - return true; |
|
| 164 | - } |
|
| 165 | - } |
|
| 146 | + /** |
|
| 147 | + * Delete the cache data for given key |
|
| 148 | + * @param string $key the key for cache to be deleted |
|
| 149 | + * @return boolean true if the cache is delete, false if can't delete |
|
| 150 | + * the cache or the cache with the given key not exist |
|
| 151 | + */ |
|
| 152 | + public function delete($key){ |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 154 | + $filePath = $this->getFilePath($key); |
|
| 155 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | + if(! file_exists($filePath)){ |
|
| 157 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + else{ |
|
| 161 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 162 | + unlink($filePath); |
|
| 163 | + return true; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Get the cache information for given key |
|
| 169 | - * @param string $key the key for cache to get the information for |
|
| 170 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | - * 'ttl' => the time to live of the cache in second |
|
| 174 | - */ |
|
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | - $filePath = $this->getFilePath($key); |
|
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 180 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - else{ |
|
| 184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 185 | - $data = file_get_contents($filePath); |
|
| 186 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 187 | - if(! $data){ |
|
| 188 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - else{ |
|
| 192 | - $this->logger->info('This cache data is OK check for expire'); |
|
| 193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 194 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
| 195 | - $info = array( |
|
| 196 | - 'mtime' => $data['mtime'], |
|
| 197 | - 'expire' => $data['expire'], |
|
| 198 | - 'ttl' => $data['ttl'] |
|
| 199 | - ); |
|
| 200 | - return $info; |
|
| 201 | - } |
|
| 202 | - else{ |
|
| 203 | - $this->logger->info('This cache already expired return false'); |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 167 | + /** |
|
| 168 | + * Get the cache information for given key |
|
| 169 | + * @param string $key the key for cache to get the information for |
|
| 170 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 171 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 172 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 173 | + * 'ttl' => the time to live of the cache in second |
|
| 174 | + */ |
|
| 175 | + public function getInfo($key){ |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 177 | + $filePath = $this->getFilePath($key); |
|
| 178 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | + if(! file_exists($filePath)){ |
|
| 180 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + else{ |
|
| 184 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 185 | + $data = file_get_contents($filePath); |
|
| 186 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 187 | + if(! $data){ |
|
| 188 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | + else{ |
|
| 192 | + $this->logger->info('This cache data is OK check for expire'); |
|
| 193 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 194 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
| 195 | + $info = array( |
|
| 196 | + 'mtime' => $data['mtime'], |
|
| 197 | + 'expire' => $data['expire'], |
|
| 198 | + 'ttl' => $data['ttl'] |
|
| 199 | + ); |
|
| 200 | + return $info; |
|
| 201 | + } |
|
| 202 | + else{ |
|
| 203 | + $this->logger->info('This cache already expired return false'); |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Used to delete expired cache data |
|
| 213 | - */ |
|
| 214 | - public function deleteExpiredCache(){ |
|
| 215 | - $this->logger->debug('Deleting of expired cache files'); |
|
| 216 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 217 | - if(! $list){ |
|
| 218 | - $this->logger->info('No cache files were found skipping'); |
|
| 219 | - } |
|
| 220 | - else{ |
|
| 221 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 222 | - foreach ($list as $file) { |
|
| 223 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 224 | - $data = file_get_contents($file); |
|
| 225 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | - if(! $data){ |
|
| 227 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | - } |
|
| 229 | - else if(time() > $data['expire']){ |
|
| 230 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | - unlink($file); |
|
| 232 | - } |
|
| 233 | - else{ |
|
| 234 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - } |
|
| 211 | + /** |
|
| 212 | + * Used to delete expired cache data |
|
| 213 | + */ |
|
| 214 | + public function deleteExpiredCache(){ |
|
| 215 | + $this->logger->debug('Deleting of expired cache files'); |
|
| 216 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 217 | + if(! $list){ |
|
| 218 | + $this->logger->info('No cache files were found skipping'); |
|
| 219 | + } |
|
| 220 | + else{ |
|
| 221 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 222 | + foreach ($list as $file) { |
|
| 223 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 224 | + $data = file_get_contents($file); |
|
| 225 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | + if(! $data){ |
|
| 227 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | + } |
|
| 229 | + else if(time() > $data['expire']){ |
|
| 230 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | + unlink($file); |
|
| 232 | + } |
|
| 233 | + else{ |
|
| 234 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Remove all file from cache folder |
|
| 242 | - */ |
|
| 243 | - public function clean(){ |
|
| 244 | - $this->logger->debug('Deleting of all cache files'); |
|
| 245 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 246 | - if(! $list){ |
|
| 247 | - $this->logger->info('No cache files were found skipping'); |
|
| 248 | - } |
|
| 249 | - else{ |
|
| 250 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 251 | - foreach ($list as $file) { |
|
| 252 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 253 | - unlink($file); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 240 | + /** |
|
| 241 | + * Remove all file from cache folder |
|
| 242 | + */ |
|
| 243 | + public function clean(){ |
|
| 244 | + $this->logger->debug('Deleting of all cache files'); |
|
| 245 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 246 | + if(! $list){ |
|
| 247 | + $this->logger->info('No cache files were found skipping'); |
|
| 248 | + } |
|
| 249 | + else{ |
|
| 250 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 251 | + foreach ($list as $file) { |
|
| 252 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 253 | + unlink($file); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * @return boolean |
|
| 260 | - */ |
|
| 261 | - public function isCompressCacheData(){ |
|
| 262 | - return $this->compressCacheData; |
|
| 263 | - } |
|
| 258 | + /** |
|
| 259 | + * @return boolean |
|
| 260 | + */ |
|
| 261 | + public function isCompressCacheData(){ |
|
| 262 | + return $this->compressCacheData; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * @param boolean $compressCacheData |
|
| 267 | - * |
|
| 268 | - * @return object |
|
| 269 | - */ |
|
| 270 | - public function setCompressCacheData($status = true){ |
|
| 271 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 272 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 265 | + /** |
|
| 266 | + * @param boolean $compressCacheData |
|
| 267 | + * |
|
| 268 | + * @return object |
|
| 269 | + */ |
|
| 270 | + public function setCompressCacheData($status = true){ |
|
| 271 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 272 | + if($status === true && ! extension_loaded('zlib')){ |
|
| 273 | 273 | |
| 274 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 275 | - $this->compressCacheData = false; |
|
| 276 | - } |
|
| 277 | - else{ |
|
| 278 | - $this->compressCacheData = $status; |
|
| 279 | - } |
|
| 280 | - return $this; |
|
| 281 | - } |
|
| 274 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 275 | + $this->compressCacheData = false; |
|
| 276 | + } |
|
| 277 | + else{ |
|
| 278 | + $this->compressCacheData = $status; |
|
| 279 | + } |
|
| 280 | + return $this; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * Check whether the cache feature for the handle is supported |
|
| 285 | - * |
|
| 286 | - * @return bool |
|
| 287 | - */ |
|
| 288 | - public function isSupported(){ |
|
| 289 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 290 | - } |
|
| 283 | + /** |
|
| 284 | + * Check whether the cache feature for the handle is supported |
|
| 285 | + * |
|
| 286 | + * @return bool |
|
| 287 | + */ |
|
| 288 | + public function isSupported(){ |
|
| 289 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * Return the Log instance |
|
| 294 | - * @return object |
|
| 295 | - */ |
|
| 296 | - public function getLogger(){ |
|
| 297 | - return $this->logger; |
|
| 298 | - } |
|
| 292 | + /** |
|
| 293 | + * Return the Log instance |
|
| 294 | + * @return object |
|
| 295 | + */ |
|
| 296 | + public function getLogger(){ |
|
| 297 | + return $this->logger; |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * Set the log instance |
|
| 302 | - * @param Log $logger the log object |
|
| 303 | - */ |
|
| 304 | - public function setLogger(Log $logger){ |
|
| 305 | - $this->logger = $logger; |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 300 | + /** |
|
| 301 | + * Set the log instance |
|
| 302 | + * @param Log $logger the log object |
|
| 303 | + */ |
|
| 304 | + public function setLogger(Log $logger){ |
|
| 305 | + $this->logger = $logger; |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | - /** |
|
| 310 | - * Get the cache file full path for the given key |
|
| 311 | - * |
|
| 312 | - * @param string $key the cache item key |
|
| 313 | - * @return string the full cache file path for this key |
|
| 314 | - */ |
|
| 315 | - private function getFilePath($key){ |
|
| 316 | - return CACHE_PATH . md5($key) . '.cache'; |
|
| 317 | - } |
|
| 318 | - } |
|
| 309 | + /** |
|
| 310 | + * Get the cache file full path for the given key |
|
| 311 | + * |
|
| 312 | + * @param string $key the cache item key |
|
| 313 | + * @return string the full cache file path for this key |
|
| 314 | + */ |
|
| 315 | + private function getFilePath($key){ |
|
| 316 | + return CACHE_PATH . md5($key) . '.cache'; |
|
| 317 | + } |
|
| 318 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | private $logger; |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 42 | + public function __construct(Log $logger = null) { |
|
| 43 | + if (!$this->isSupported()) { |
|
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | 47 | * instance of the Log class |
| 48 | 48 | */ |
| 49 | - if(is_object($logger)){ |
|
| 49 | + if (is_object($logger)) { |
|
| 50 | 50 | $this->logger = $logger; |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 52 | + else { |
|
| 53 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 54 | 54 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | - if(! extension_loaded('zlib')){ |
|
| 58 | + if (!extension_loaded('zlib')) { |
|
| 59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 60 | 60 | $this->compressCacheData = false; |
| 61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | * @param string $key the key to identify the cache data |
| 67 | 67 | * @return mixed the cache data if exists else return false |
| 68 | 68 | */ |
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 69 | + public function get($key) { |
|
| 70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 71 | 71 | $filePath = $this->getFilePath($key); |
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 72 | + if (!file_exists($filePath)) { |
|
| 73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath, 'r'); |
|
| 78 | + if (!is_resource($handle)) { |
|
| 79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | $data = file_get_contents($filePath); |
| 85 | 85 | fclose($handle); |
| 86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 87 | + if (!$data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
| 90 | 90 | unlink($filePath); |
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | 93 | if (time() > $data['expire']) { |
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 95 | 95 | // Unlinking when the file was expired |
| 96 | 96 | unlink($filePath); |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 99 | + else { |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | 101 | return $data['data']; |
| 102 | 102 | } |
| 103 | 103 | } |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | * @param integer $ttl the cache life time |
| 111 | 111 | * @return boolean true if success otherwise will return false |
| 112 | 112 | */ |
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 113 | + public function set($key, $data, $ttl = 0) { |
|
| 114 | 114 | $expire = time() + $ttl; |
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 115 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | 116 | $filePath = $this->getFilePath($key); |
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 117 | + $handle = fopen($filePath, 'w'); |
|
| 118 | + if (!is_resource($handle)) { |
|
| 119 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | 131 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 132 | + if (!$result) { |
|
| 133 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 134 | 134 | fclose($handle); |
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 137 | + else { |
|
| 138 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 139 | 139 | fclose($handle); |
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
@@ -149,16 +149,16 @@ discard block |
||
| 149 | 149 | * @return boolean true if the cache is delete, false if can't delete |
| 150 | 150 | * the cache or the cache with the given key not exist |
| 151 | 151 | */ |
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 152 | + public function delete($key) { |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 154 | 154 | $filePath = $this->getFilePath($key); |
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 155 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 156 | + if (!file_exists($filePath)) { |
|
| 157 | 157 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 160 | + else { |
|
| 161 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 162 | 162 | unlink($filePath); |
| 163 | 163 | return true; |
| 164 | 164 | } |
@@ -172,25 +172,25 @@ discard block |
||
| 172 | 172 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 173 | 173 | * 'ttl' => the time to live of the cache in second |
| 174 | 174 | */ |
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 175 | + public function getInfo($key) { |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 177 | 177 | $filePath = $this->getFilePath($key); |
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 178 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 179 | + if (!file_exists($filePath)) { |
|
| 180 | 180 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 181 | return false; |
| 182 | 182 | } |
| 183 | - else{ |
|
| 184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 183 | + else { |
|
| 184 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 185 | 185 | $data = file_get_contents($filePath); |
| 186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | - if(! $data){ |
|
| 187 | + if (!$data) { |
|
| 188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 189 | 189 | return false; |
| 190 | 190 | } |
| 191 | - else{ |
|
| 191 | + else { |
|
| 192 | 192 | $this->logger->info('This cache data is OK check for expire'); |
| 193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 193 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 194 | 194 | $this->logger->info('This cache not yet expired return cache informations'); |
| 195 | 195 | $info = array( |
| 196 | 196 | 'mtime' => $data['mtime'], |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | ); |
| 200 | 200 | return $info; |
| 201 | 201 | } |
| 202 | - else{ |
|
| 202 | + else { |
|
| 203 | 203 | $this->logger->info('This cache already expired return false'); |
| 204 | 204 | return false; |
| 205 | 205 | } |
@@ -211,26 +211,26 @@ discard block |
||
| 211 | 211 | /** |
| 212 | 212 | * Used to delete expired cache data |
| 213 | 213 | */ |
| 214 | - public function deleteExpiredCache(){ |
|
| 214 | + public function deleteExpiredCache() { |
|
| 215 | 215 | $this->logger->debug('Deleting of expired cache files'); |
| 216 | 216 | $list = glob(CACHE_PATH . '*.cache'); |
| 217 | - if(! $list){ |
|
| 217 | + if (!$list) { |
|
| 218 | 218 | $this->logger->info('No cache files were found skipping'); |
| 219 | 219 | } |
| 220 | - else{ |
|
| 220 | + else { |
|
| 221 | 221 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 222 | 222 | foreach ($list as $file) { |
| 223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 224 | 224 | $data = file_get_contents($file); |
| 225 | 225 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 226 | - if(! $data){ |
|
| 226 | + if (!$data) { |
|
| 227 | 227 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 228 | 228 | } |
| 229 | - else if(time() > $data['expire']){ |
|
| 229 | + else if (time() > $data['expire']) { |
|
| 230 | 230 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 231 | 231 | unlink($file); |
| 232 | 232 | } |
| 233 | - else{ |
|
| 233 | + else { |
|
| 234 | 234 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | /** |
| 241 | 241 | * Remove all file from cache folder |
| 242 | 242 | */ |
| 243 | - public function clean(){ |
|
| 243 | + public function clean() { |
|
| 244 | 244 | $this->logger->debug('Deleting of all cache files'); |
| 245 | 245 | $list = glob(CACHE_PATH . '*.cache'); |
| 246 | - if(! $list){ |
|
| 246 | + if (!$list) { |
|
| 247 | 247 | $this->logger->info('No cache files were found skipping'); |
| 248 | 248 | } |
| 249 | - else{ |
|
| 249 | + else { |
|
| 250 | 250 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 251 | 251 | foreach ($list as $file) { |
| 252 | 252 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | /** |
| 259 | 259 | * @return boolean |
| 260 | 260 | */ |
| 261 | - public function isCompressCacheData(){ |
|
| 261 | + public function isCompressCacheData() { |
|
| 262 | 262 | return $this->compressCacheData; |
| 263 | 263 | } |
| 264 | 264 | |
@@ -267,14 +267,14 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return object |
| 269 | 269 | */ |
| 270 | - public function setCompressCacheData($status = true){ |
|
| 270 | + public function setCompressCacheData($status = true) { |
|
| 271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
| 272 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 272 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 273 | 273 | |
| 274 | 274 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 275 | 275 | $this->compressCacheData = false; |
| 276 | 276 | } |
| 277 | - else{ |
|
| 277 | + else { |
|
| 278 | 278 | $this->compressCacheData = $status; |
| 279 | 279 | } |
| 280 | 280 | return $this; |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @return bool |
| 287 | 287 | */ |
| 288 | - public function isSupported(){ |
|
| 288 | + public function isSupported() { |
|
| 289 | 289 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 290 | 290 | } |
| 291 | 291 | |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * Return the Log instance |
| 294 | 294 | * @return object |
| 295 | 295 | */ |
| 296 | - public function getLogger(){ |
|
| 296 | + public function getLogger() { |
|
| 297 | 297 | return $this->logger; |
| 298 | 298 | } |
| 299 | 299 | |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * Set the log instance |
| 302 | 302 | * @param Log $logger the log object |
| 303 | 303 | */ |
| 304 | - public function setLogger(Log $logger){ |
|
| 304 | + public function setLogger(Log $logger) { |
|
| 305 | 305 | $this->logger = $logger; |
| 306 | 306 | return $this; |
| 307 | 307 | } |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | * @param string $key the cache item key |
| 313 | 313 | * @return string the full cache file path for this key |
| 314 | 314 | */ |
| 315 | - private function getFilePath($key){ |
|
| 315 | + private function getFilePath($key) { |
|
| 316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
| 317 | 317 | } |
| 318 | 318 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * Class for Benchmark |
| 29 | 29 | */ |
| 30 | - class Benchmark{ |
|
| 30 | + class Benchmark { |
|
| 31 | 31 | /** |
| 32 | 32 | * The markers for excution time |
| 33 | 33 | * @var array |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * This method is used to mark one point for benchmark (execution time and memory usage) |
| 45 | 45 | * @param string $name the marker name |
| 46 | 46 | */ |
| 47 | - public function mark($name){ |
|
| 47 | + public function mark($name) { |
|
| 48 | 48 | //Marker for execution time |
| 49 | 49 | $this->markersTime[$name] = microtime(true); |
| 50 | 50 | //Marker for memory usage |
@@ -58,12 +58,12 @@ discard block |
||
| 58 | 58 | * @param integer $decimalCount the number of decimal |
| 59 | 59 | * @return string the total execution time |
| 60 | 60 | */ |
| 61 | - public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 62 | - if(! $startMarkerName || !isset($this->markersTime[$startMarkerName])){ |
|
| 61 | + public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6) { |
|
| 62 | + if (!$startMarkerName || !isset($this->markersTime[$startMarkerName])) { |
|
| 63 | 63 | return 0; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if(! isset($this->markersTime[$endMarkerName])){ |
|
| 66 | + if (!isset($this->markersTime[$endMarkerName])) { |
|
| 67 | 67 | $this->markersTime[$endMarkerName] = microtime(true); |
| 68 | 68 | } |
| 69 | 69 | return number_format($this->markersTime[$endMarkerName] - $this->markersTime[$startMarkerName], $decimalCount); |
@@ -76,12 +76,12 @@ discard block |
||
| 76 | 76 | * @param integer $decimalCount the number of decimal |
| 77 | 77 | * @return string the total memory usage |
| 78 | 78 | */ |
| 79 | - public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 80 | - if(! $startMarkerName || !isset($this->markersMemory[$startMarkerName])){ |
|
| 79 | + public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6) { |
|
| 80 | + if (!$startMarkerName || !isset($this->markersMemory[$startMarkerName])) { |
|
| 81 | 81 | return 0; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if(! isset($this->markersMemory[$endMarkerName])){ |
|
| 84 | + if (!isset($this->markersMemory[$endMarkerName])) { |
|
| 85 | 85 | $this->markersMemory[$endMarkerName] = microtime(true); |
| 86 | 86 | } |
| 87 | 87 | return number_format($this->markersMemory[$endMarkerName] - $this->markersMemory[$startMarkerName], $decimalCount); |
@@ -1,89 +1,89 @@ |
||
| 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 | - /** |
|
| 28 | - * Class for Benchmark |
|
| 29 | - */ |
|
| 30 | - class Benchmark{ |
|
| 31 | - /** |
|
| 32 | - * The markers for excution time |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - private $markersTime = array(); |
|
| 27 | + /** |
|
| 28 | + * Class for Benchmark |
|
| 29 | + */ |
|
| 30 | + class Benchmark{ |
|
| 31 | + /** |
|
| 32 | + * The markers for excution time |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + private $markersTime = array(); |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * The markers for memory usage |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - private $markersMemory = array(); |
|
| 37 | + /** |
|
| 38 | + * The markers for memory usage |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + private $markersMemory = array(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * This method is used to mark one point for benchmark (execution time and memory usage) |
|
| 45 | - * @param string $name the marker name |
|
| 46 | - */ |
|
| 47 | - public function mark($name){ |
|
| 48 | - //Marker for execution time |
|
| 49 | - $this->markersTime[$name] = microtime(true); |
|
| 50 | - //Marker for memory usage |
|
| 51 | - $this->markersMemory[$name] = memory_get_usage(true); |
|
| 52 | - } |
|
| 43 | + /** |
|
| 44 | + * This method is used to mark one point for benchmark (execution time and memory usage) |
|
| 45 | + * @param string $name the marker name |
|
| 46 | + */ |
|
| 47 | + public function mark($name){ |
|
| 48 | + //Marker for execution time |
|
| 49 | + $this->markersTime[$name] = microtime(true); |
|
| 50 | + //Marker for memory usage |
|
| 51 | + $this->markersMemory[$name] = memory_get_usage(true); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * This method is used to get the total excution time in second between two markers |
|
| 56 | - * @param string $startMarkerName the marker for start point |
|
| 57 | - * @param string $endMarkerName the marker for end point |
|
| 58 | - * @param integer $decimalCount the number of decimal |
|
| 59 | - * @return string the total execution time |
|
| 60 | - */ |
|
| 61 | - public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 62 | - if(! $startMarkerName || !isset($this->markersTime[$startMarkerName])){ |
|
| 63 | - return 0; |
|
| 64 | - } |
|
| 54 | + /** |
|
| 55 | + * This method is used to get the total excution time in second between two markers |
|
| 56 | + * @param string $startMarkerName the marker for start point |
|
| 57 | + * @param string $endMarkerName the marker for end point |
|
| 58 | + * @param integer $decimalCount the number of decimal |
|
| 59 | + * @return string the total execution time |
|
| 60 | + */ |
|
| 61 | + public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 62 | + if(! $startMarkerName || !isset($this->markersTime[$startMarkerName])){ |
|
| 63 | + return 0; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - if(! isset($this->markersTime[$endMarkerName])){ |
|
| 67 | - $this->markersTime[$endMarkerName] = microtime(true); |
|
| 68 | - } |
|
| 69 | - return number_format($this->markersTime[$endMarkerName] - $this->markersTime[$startMarkerName], $decimalCount); |
|
| 70 | - } |
|
| 66 | + if(! isset($this->markersTime[$endMarkerName])){ |
|
| 67 | + $this->markersTime[$endMarkerName] = microtime(true); |
|
| 68 | + } |
|
| 69 | + return number_format($this->markersTime[$endMarkerName] - $this->markersTime[$startMarkerName], $decimalCount); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * This method is used to get the total memory usage in byte between two markers |
|
| 74 | - * @param string $startMarkerName the marker for start point |
|
| 75 | - * @param string $endMarkerName the marker for end point |
|
| 76 | - * @param integer $decimalCount the number of decimal |
|
| 77 | - * @return string the total memory usage |
|
| 78 | - */ |
|
| 79 | - public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 80 | - if(! $startMarkerName || !isset($this->markersMemory[$startMarkerName])){ |
|
| 81 | - return 0; |
|
| 82 | - } |
|
| 72 | + /** |
|
| 73 | + * This method is used to get the total memory usage in byte between two markers |
|
| 74 | + * @param string $startMarkerName the marker for start point |
|
| 75 | + * @param string $endMarkerName the marker for end point |
|
| 76 | + * @param integer $decimalCount the number of decimal |
|
| 77 | + * @return string the total memory usage |
|
| 78 | + */ |
|
| 79 | + public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
| 80 | + if(! $startMarkerName || !isset($this->markersMemory[$startMarkerName])){ |
|
| 81 | + return 0; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - if(! isset($this->markersMemory[$endMarkerName])){ |
|
| 85 | - $this->markersMemory[$endMarkerName] = microtime(true); |
|
| 86 | - } |
|
| 87 | - return number_format($this->markersMemory[$endMarkerName] - $this->markersMemory[$startMarkerName], $decimalCount); |
|
| 88 | - } |
|
| 89 | - } |
|
| 84 | + if(! isset($this->markersMemory[$endMarkerName])){ |
|
| 85 | + $this->markersMemory[$endMarkerName] = microtime(true); |
|
| 86 | + } |
|
| 87 | + return number_format($this->markersMemory[$endMarkerName] - $this->markersMemory[$startMarkerName], $decimalCount); |
|
| 88 | + } |
|
| 89 | + } |
|
@@ -148,8 +148,7 @@ discard block |
||
| 148 | 148 | if(isset($path[1])){ |
| 149 | 149 | $instance = strtolower($path[1]); |
| 150 | 150 | } |
| 151 | - } |
|
| 152 | - else{ |
|
| 151 | + } else{ |
|
| 153 | 152 | $instance = strtolower($class); |
| 154 | 153 | } |
| 155 | 154 | } |
@@ -169,8 +168,7 @@ discard block |
||
| 169 | 168 | $searchModuleName = $path[0]; |
| 170 | 169 | $class = ucfirst($path[1]); |
| 171 | 170 | } |
| 172 | - } |
|
| 173 | - else{ |
|
| 171 | + } else{ |
|
| 174 | 172 | $class = ucfirst($class); |
| 175 | 173 | } |
| 176 | 174 | |
@@ -181,8 +179,7 @@ discard block |
||
| 181 | 179 | if($moduleModelFilePath){ |
| 182 | 180 | $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
| 183 | 181 | $classFilePath = $moduleModelFilePath; |
| 184 | - } |
|
| 185 | - else{ |
|
| 182 | + } else{ |
|
| 186 | 183 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
| 187 | 184 | } |
| 188 | 185 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
@@ -194,12 +191,10 @@ discard block |
||
| 194 | 191 | $obj->{$instance} = $c; |
| 195 | 192 | static::$loaded[$instance] = $class; |
| 196 | 193 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
| 197 | - } |
|
| 198 | - else{ |
|
| 194 | + } else{ |
|
| 199 | 195 | show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
| 200 | 196 | } |
| 201 | - } |
|
| 202 | - else{ |
|
| 197 | + } else{ |
|
| 203 | 198 | show_error('Unable to find the model [' . $class . ']'); |
| 204 | 199 | } |
| 205 | 200 | } |
@@ -227,8 +222,7 @@ discard block |
||
| 227 | 222 | if(isset($path[1])){ |
| 228 | 223 | $instance = strtolower($path[1]); |
| 229 | 224 | } |
| 230 | - } |
|
| 231 | - else{ |
|
| 225 | + } else{ |
|
| 232 | 226 | $instance = strtolower($class); |
| 233 | 227 | } |
| 234 | 228 | } |
@@ -252,8 +246,7 @@ discard block |
||
| 252 | 246 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
| 253 | 247 | $class = ucfirst($class); |
| 254 | 248 | $logger->info('This library is a system library'); |
| 255 | - } |
|
| 256 | - else{ |
|
| 249 | + } else{ |
|
| 257 | 250 | $logger->info('This library is not a system library'); |
| 258 | 251 | //first check if this library is in the module |
| 259 | 252 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
@@ -265,8 +258,7 @@ discard block |
||
| 265 | 258 | $searchModuleName = $path[0]; |
| 266 | 259 | $class = ucfirst($path[1]); |
| 267 | 260 | } |
| 268 | - } |
|
| 269 | - else{ |
|
| 261 | + } else{ |
|
| 270 | 262 | $class = ucfirst($class); |
| 271 | 263 | } |
| 272 | 264 | if(! $searchModuleName && !empty($obj->moduleName)){ |
@@ -276,8 +268,7 @@ discard block |
||
| 276 | 268 | if($moduleLibraryPath){ |
| 277 | 269 | $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
| 278 | 270 | $libraryFilePath = $moduleLibraryPath; |
| 279 | - } |
|
| 280 | - else{ |
|
| 271 | + } else{ |
|
| 281 | 272 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
| 282 | 273 | } |
| 283 | 274 | } |
@@ -301,12 +292,10 @@ discard block |
||
| 301 | 292 | $obj->{$instance} = $c; |
| 302 | 293 | static::$loaded[$instance] = $class; |
| 303 | 294 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
| 304 | - } |
|
| 305 | - else{ |
|
| 295 | + } else{ |
|
| 306 | 296 | show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
| 307 | 297 | } |
| 308 | - } |
|
| 309 | - else{ |
|
| 298 | + } else{ |
|
| 310 | 299 | show_error('Unable to find library class [' . $class . ']'); |
| 311 | 300 | } |
| 312 | 301 | } |
@@ -350,8 +339,7 @@ discard block |
||
| 350 | 339 | if($moduleFunctionPath){ |
| 351 | 340 | $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
| 352 | 341 | $functionFilePath = $moduleFunctionPath; |
| 353 | - } |
|
| 354 | - else{ |
|
| 342 | + } else{ |
|
| 355 | 343 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
| 356 | 344 | } |
| 357 | 345 | if(! $functionFilePath){ |
@@ -370,8 +358,7 @@ discard block |
||
| 370 | 358 | require_once $functionFilePath; |
| 371 | 359 | static::$loaded['function_' . $function] = $functionFilePath; |
| 372 | 360 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
| 373 | - } |
|
| 374 | - else{ |
|
| 361 | + } else{ |
|
| 375 | 362 | show_error('Unable to find helper file [' . $file . ']'); |
| 376 | 363 | } |
| 377 | 364 | } |
@@ -414,8 +401,7 @@ discard block |
||
| 414 | 401 | if($moduleConfigPath){ |
| 415 | 402 | $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
| 416 | 403 | $configFilePath = $moduleConfigPath; |
| 417 | - } |
|
| 418 | - else{ |
|
| 404 | + } else{ |
|
| 419 | 405 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
| 420 | 406 | } |
| 421 | 407 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
@@ -424,8 +410,7 @@ discard block |
||
| 424 | 410 | if(! empty($config) && is_array($config)){ |
| 425 | 411 | Config::setAll($config); |
| 426 | 412 | } |
| 427 | - } |
|
| 428 | - else{ |
|
| 413 | + } else{ |
|
| 429 | 414 | show_error('Unable to find config file ['. $configFilePath . ']'); |
| 430 | 415 | } |
| 431 | 416 | static::$loaded['config_' . $filename] = $configFilePath; |
@@ -483,8 +468,7 @@ discard block |
||
| 483 | 468 | if($moduleLanguagePath){ |
| 484 | 469 | $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
| 485 | 470 | $languageFilePath = $moduleLanguagePath; |
| 486 | - } |
|
| 487 | - else{ |
|
| 471 | + } else{ |
|
| 488 | 472 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
| 489 | 473 | } |
| 490 | 474 | if(! $languageFilePath){ |
@@ -511,8 +495,7 @@ discard block |
||
| 511 | 495 | } |
| 512 | 496 | static::$loaded['lang_' . $language] = $languageFilePath; |
| 513 | 497 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
| 514 | - } |
|
| 515 | - else{ |
|
| 498 | + } else{ |
|
| 516 | 499 | show_error('Unable to find language file [' . $file . ']'); |
| 517 | 500 | } |
| 518 | 501 | } |
@@ -1,521 +1,521 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Loader{ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + class Loader{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * List of loaded resources |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - public static $loaded = array(); |
|
| 28 | + /** |
|
| 29 | + * List of loaded resources |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + public static $loaded = array(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The logger instance |
|
| 36 | - * @var Log |
|
| 37 | - */ |
|
| 38 | - private static $logger; |
|
| 34 | + /** |
|
| 35 | + * The logger instance |
|
| 36 | + * @var Log |
|
| 37 | + */ |
|
| 38 | + private static $logger; |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 42 | - //add the resources already loaded during application bootstrap |
|
| 43 | - //in the list to prevent duplicate or loading the resources again. |
|
| 44 | - static::$loaded = class_loaded(); |
|
| 41 | + public function __construct(){ |
|
| 42 | + //add the resources already loaded during application bootstrap |
|
| 43 | + //in the list to prevent duplicate or loading the resources again. |
|
| 44 | + static::$loaded = class_loaded(); |
|
| 45 | 45 | |
| 46 | - //Load resources from autoload configuration |
|
| 47 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | - } |
|
| 46 | + //Load resources from autoload configuration |
|
| 47 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Get the logger singleton instance |
|
| 52 | - * @return Log the logger instance |
|
| 53 | - */ |
|
| 54 | - private static function getLogger(){ |
|
| 55 | - if(self::$logger == null){ |
|
| 56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 57 | - self::$logger[0]->setLogger('Library::Loader'); |
|
| 58 | - } |
|
| 59 | - return self::$logger[0]; |
|
| 60 | - } |
|
| 50 | + /** |
|
| 51 | + * Get the logger singleton instance |
|
| 52 | + * @return Log the logger instance |
|
| 53 | + */ |
|
| 54 | + private static function getLogger(){ |
|
| 55 | + if(self::$logger == null){ |
|
| 56 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 57 | + self::$logger[0]->setLogger('Library::Loader'); |
|
| 58 | + } |
|
| 59 | + return self::$logger[0]; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Load the model class |
|
| 64 | - * |
|
| 65 | - * @param string $class the class name to be loaded |
|
| 66 | - * @param string $instance the name of the instance to use in super object |
|
| 67 | - * |
|
| 68 | - * @return void |
|
| 69 | - */ |
|
| 70 | - public static function model($class, $instance = null){ |
|
| 71 | - $logger = static::getLogger(); |
|
| 72 | - $class = str_ireplace('.php', '', $class); |
|
| 73 | - $class = trim($class, '/\\'); |
|
| 74 | - $file = ucfirst($class).'.php'; |
|
| 75 | - $logger->debug('Loading model [' . $class . '] ...'); |
|
| 76 | - if(! $instance){ |
|
| 77 | - //for module |
|
| 78 | - if(strpos($class, '/') !== false){ |
|
| 79 | - $path = explode('/', $class); |
|
| 80 | - if(isset($path[1])){ |
|
| 81 | - $instance = strtolower($path[1]); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - else{ |
|
| 85 | - $instance = strtolower($class); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - if(isset(static::$loaded[$instance])){ |
|
| 89 | - $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 90 | - return; |
|
| 91 | - } |
|
| 92 | - $classFilePath = APPS_MODEL_PATH . $file; |
|
| 93 | - //first check if this model is in the module |
|
| 94 | - $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 95 | - $searchModuleName = null; |
|
| 96 | - $obj = & get_instance(); |
|
| 97 | - //check if the request class contains module name |
|
| 98 | - if(strpos($class, '/') !== false){ |
|
| 99 | - $path = explode('/', $class); |
|
| 100 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 101 | - $searchModuleName = $path[0]; |
|
| 102 | - $class = ucfirst($path[1]); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - else{ |
|
| 106 | - $class = ucfirst($class); |
|
| 107 | - } |
|
| 62 | + /** |
|
| 63 | + * Load the model class |
|
| 64 | + * |
|
| 65 | + * @param string $class the class name to be loaded |
|
| 66 | + * @param string $instance the name of the instance to use in super object |
|
| 67 | + * |
|
| 68 | + * @return void |
|
| 69 | + */ |
|
| 70 | + public static function model($class, $instance = null){ |
|
| 71 | + $logger = static::getLogger(); |
|
| 72 | + $class = str_ireplace('.php', '', $class); |
|
| 73 | + $class = trim($class, '/\\'); |
|
| 74 | + $file = ucfirst($class).'.php'; |
|
| 75 | + $logger->debug('Loading model [' . $class . '] ...'); |
|
| 76 | + if(! $instance){ |
|
| 77 | + //for module |
|
| 78 | + if(strpos($class, '/') !== false){ |
|
| 79 | + $path = explode('/', $class); |
|
| 80 | + if(isset($path[1])){ |
|
| 81 | + $instance = strtolower($path[1]); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + else{ |
|
| 85 | + $instance = strtolower($class); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + if(isset(static::$loaded[$instance])){ |
|
| 89 | + $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 90 | + return; |
|
| 91 | + } |
|
| 92 | + $classFilePath = APPS_MODEL_PATH . $file; |
|
| 93 | + //first check if this model is in the module |
|
| 94 | + $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 95 | + $searchModuleName = null; |
|
| 96 | + $obj = & get_instance(); |
|
| 97 | + //check if the request class contains module name |
|
| 98 | + if(strpos($class, '/') !== false){ |
|
| 99 | + $path = explode('/', $class); |
|
| 100 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 101 | + $searchModuleName = $path[0]; |
|
| 102 | + $class = ucfirst($path[1]); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + else{ |
|
| 106 | + $class = ucfirst($class); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 110 | - $searchModuleName = $obj->moduleName; |
|
| 111 | - } |
|
| 112 | - $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
|
| 113 | - if($moduleModelFilePath){ |
|
| 114 | - $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 115 | - $classFilePath = $moduleModelFilePath; |
|
| 116 | - } |
|
| 117 | - else{ |
|
| 118 | - $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 119 | - } |
|
| 120 | - $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 121 | - if(file_exists($classFilePath)){ |
|
| 122 | - require_once $classFilePath; |
|
| 123 | - if(class_exists($class)){ |
|
| 124 | - $c = new $class(); |
|
| 125 | - $obj = & get_instance(); |
|
| 126 | - $obj->{$instance} = $c; |
|
| 127 | - static::$loaded[$instance] = $class; |
|
| 128 | - $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 129 | - } |
|
| 130 | - else{ |
|
| 131 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - else{ |
|
| 135 | - show_error('Unable to find the model [' . $class . ']'); |
|
| 136 | - } |
|
| 137 | - } |
|
| 109 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 110 | + $searchModuleName = $obj->moduleName; |
|
| 111 | + } |
|
| 112 | + $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
|
| 113 | + if($moduleModelFilePath){ |
|
| 114 | + $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 115 | + $classFilePath = $moduleModelFilePath; |
|
| 116 | + } |
|
| 117 | + else{ |
|
| 118 | + $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 119 | + } |
|
| 120 | + $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 121 | + if(file_exists($classFilePath)){ |
|
| 122 | + require_once $classFilePath; |
|
| 123 | + if(class_exists($class)){ |
|
| 124 | + $c = new $class(); |
|
| 125 | + $obj = & get_instance(); |
|
| 126 | + $obj->{$instance} = $c; |
|
| 127 | + static::$loaded[$instance] = $class; |
|
| 128 | + $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 129 | + } |
|
| 130 | + else{ |
|
| 131 | + show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + else{ |
|
| 135 | + show_error('Unable to find the model [' . $class . ']'); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Load the library class |
|
| 142 | - * |
|
| 143 | - * @param string $class the library class name to be loaded |
|
| 144 | - * @param string $instance the instance name to use in super object |
|
| 145 | - * @param mixed $params the arguments to pass to the constructor |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 150 | - $logger = static::getLogger(); |
|
| 151 | - $class = str_ireplace('.php', '', $class); |
|
| 152 | - $class = trim($class, '/\\'); |
|
| 153 | - $file = ucfirst($class) .'.php'; |
|
| 154 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 155 | - if(! $instance){ |
|
| 156 | - //for module |
|
| 157 | - if(strpos($class, '/') !== false){ |
|
| 158 | - $path = explode('/', $class); |
|
| 159 | - if(isset($path[1])){ |
|
| 160 | - $instance = strtolower($path[1]); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - $instance = strtolower($class); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - if(isset(static::$loaded[$instance])){ |
|
| 168 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 169 | - return; |
|
| 170 | - } |
|
| 171 | - $obj = & get_instance(); |
|
| 172 | - //TODO for Database library |
|
| 173 | - if(strtolower($class) == 'database'){ |
|
| 174 | - $logger->info('This is the Database library ...'); |
|
| 175 | - $dbInstance = & class_loader('Database', 'classes/database', $params); |
|
| 176 | - $obj->{$instance} = $dbInstance; |
|
| 177 | - static::$loaded[$instance] = $class; |
|
| 178 | - $logger->info('Library Database loaded successfully.'); |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - $libraryFilePath = null; |
|
| 182 | - $logger->debug('Check if this is a system library ...'); |
|
| 183 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 184 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 185 | - $class = ucfirst($class); |
|
| 186 | - $logger->info('This library is a system library'); |
|
| 187 | - } |
|
| 188 | - else{ |
|
| 189 | - $logger->info('This library is not a system library'); |
|
| 190 | - //first check if this library is in the module |
|
| 191 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 192 | - $searchModuleName = null; |
|
| 193 | - //check if the request class contains module name |
|
| 194 | - if(strpos($class, '/') !== false){ |
|
| 195 | - $path = explode('/', $class); |
|
| 196 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 197 | - $searchModuleName = $path[0]; |
|
| 198 | - $class = ucfirst($path[1]); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - $class = ucfirst($class); |
|
| 203 | - } |
|
| 204 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 205 | - $searchModuleName = $obj->moduleName; |
|
| 206 | - } |
|
| 207 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
|
| 208 | - if($moduleLibraryPath){ |
|
| 209 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 210 | - $libraryFilePath = $moduleLibraryPath; |
|
| 211 | - } |
|
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - if(! $libraryFilePath){ |
|
| 217 | - $searchDir = array(LIBRARY_PATH); |
|
| 218 | - foreach($searchDir as $dir){ |
|
| 219 | - $filePath = $dir . $file; |
|
| 220 | - if(file_exists($filePath)){ |
|
| 221 | - $libraryFilePath = $filePath; |
|
| 222 | - //is already found not to continue |
|
| 223 | - break; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 228 | - if($libraryFilePath){ |
|
| 229 | - require_once $libraryFilePath; |
|
| 230 | - if(class_exists($class)){ |
|
| 231 | - $c = $params ? new $class($params) : new $class(); |
|
| 232 | - $obj = & get_instance(); |
|
| 233 | - $obj->{$instance} = $c; |
|
| 234 | - static::$loaded[$instance] = $class; |
|
| 235 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 236 | - } |
|
| 237 | - else{ |
|
| 238 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - else{ |
|
| 242 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 243 | - } |
|
| 244 | - } |
|
| 140 | + /** |
|
| 141 | + * Load the library class |
|
| 142 | + * |
|
| 143 | + * @param string $class the library class name to be loaded |
|
| 144 | + * @param string $instance the instance name to use in super object |
|
| 145 | + * @param mixed $params the arguments to pass to the constructor |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public static function library($class, $instance = null, array $params = array()){ |
|
| 150 | + $logger = static::getLogger(); |
|
| 151 | + $class = str_ireplace('.php', '', $class); |
|
| 152 | + $class = trim($class, '/\\'); |
|
| 153 | + $file = ucfirst($class) .'.php'; |
|
| 154 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 155 | + if(! $instance){ |
|
| 156 | + //for module |
|
| 157 | + if(strpos($class, '/') !== false){ |
|
| 158 | + $path = explode('/', $class); |
|
| 159 | + if(isset($path[1])){ |
|
| 160 | + $instance = strtolower($path[1]); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + $instance = strtolower($class); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + if(isset(static::$loaded[$instance])){ |
|
| 168 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 169 | + return; |
|
| 170 | + } |
|
| 171 | + $obj = & get_instance(); |
|
| 172 | + //TODO for Database library |
|
| 173 | + if(strtolower($class) == 'database'){ |
|
| 174 | + $logger->info('This is the Database library ...'); |
|
| 175 | + $dbInstance = & class_loader('Database', 'classes/database', $params); |
|
| 176 | + $obj->{$instance} = $dbInstance; |
|
| 177 | + static::$loaded[$instance] = $class; |
|
| 178 | + $logger->info('Library Database loaded successfully.'); |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + $libraryFilePath = null; |
|
| 182 | + $logger->debug('Check if this is a system library ...'); |
|
| 183 | + if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 184 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 185 | + $class = ucfirst($class); |
|
| 186 | + $logger->info('This library is a system library'); |
|
| 187 | + } |
|
| 188 | + else{ |
|
| 189 | + $logger->info('This library is not a system library'); |
|
| 190 | + //first check if this library is in the module |
|
| 191 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 192 | + $searchModuleName = null; |
|
| 193 | + //check if the request class contains module name |
|
| 194 | + if(strpos($class, '/') !== false){ |
|
| 195 | + $path = explode('/', $class); |
|
| 196 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 197 | + $searchModuleName = $path[0]; |
|
| 198 | + $class = ucfirst($path[1]); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + $class = ucfirst($class); |
|
| 203 | + } |
|
| 204 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 205 | + $searchModuleName = $obj->moduleName; |
|
| 206 | + } |
|
| 207 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
|
| 208 | + if($moduleLibraryPath){ |
|
| 209 | + $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 210 | + $libraryFilePath = $moduleLibraryPath; |
|
| 211 | + } |
|
| 212 | + else{ |
|
| 213 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + if(! $libraryFilePath){ |
|
| 217 | + $searchDir = array(LIBRARY_PATH); |
|
| 218 | + foreach($searchDir as $dir){ |
|
| 219 | + $filePath = $dir . $file; |
|
| 220 | + if(file_exists($filePath)){ |
|
| 221 | + $libraryFilePath = $filePath; |
|
| 222 | + //is already found not to continue |
|
| 223 | + break; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 228 | + if($libraryFilePath){ |
|
| 229 | + require_once $libraryFilePath; |
|
| 230 | + if(class_exists($class)){ |
|
| 231 | + $c = $params ? new $class($params) : new $class(); |
|
| 232 | + $obj = & get_instance(); |
|
| 233 | + $obj->{$instance} = $c; |
|
| 234 | + static::$loaded[$instance] = $class; |
|
| 235 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 236 | + } |
|
| 237 | + else{ |
|
| 238 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + else{ |
|
| 242 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Load the helper |
|
| 248 | - * |
|
| 249 | - * @param string $function the helper name to be loaded |
|
| 250 | - * |
|
| 251 | - * @return void |
|
| 252 | - */ |
|
| 253 | - public static function functions($function){ |
|
| 254 | - $logger = static::getLogger(); |
|
| 255 | - $function = str_ireplace('.php', '', $function); |
|
| 256 | - $function = trim($function, '/\\'); |
|
| 257 | - $function = str_ireplace('function_', '', $function); |
|
| 258 | - $file = 'function_'.$function.'.php'; |
|
| 259 | - $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 260 | - if(isset(static::$loaded['function_' . $function])){ |
|
| 261 | - $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 262 | - return; |
|
| 263 | - } |
|
| 264 | - $functionFilePath = null; |
|
| 265 | - //first check if this helper is in the module |
|
| 266 | - $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 267 | - $searchModuleName = null; |
|
| 268 | - $obj = & get_instance(); |
|
| 269 | - //check if the request class contains module name |
|
| 270 | - if(strpos($function, '/') !== false){ |
|
| 271 | - $path = explode('/', $function); |
|
| 272 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 273 | - $searchModuleName = $path[0]; |
|
| 274 | - $function = 'function_' . $path[1] . '.php'; |
|
| 275 | - $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 279 | - $searchModuleName = $obj->moduleName; |
|
| 280 | - } |
|
| 281 | - $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
|
| 282 | - if($moduleFunctionPath){ |
|
| 283 | - $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 284 | - $functionFilePath = $moduleFunctionPath; |
|
| 285 | - } |
|
| 286 | - else{ |
|
| 287 | - $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 288 | - } |
|
| 289 | - if(! $functionFilePath){ |
|
| 290 | - $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 291 | - foreach($searchDir as $dir){ |
|
| 292 | - $filePath = $dir . $file; |
|
| 293 | - if(file_exists($filePath)){ |
|
| 294 | - $functionFilePath = $filePath; |
|
| 295 | - //is already found not to continue |
|
| 296 | - break; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 301 | - if($functionFilePath){ |
|
| 302 | - require_once $functionFilePath; |
|
| 303 | - static::$loaded['function_' . $function] = $functionFilePath; |
|
| 304 | - $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 305 | - } |
|
| 306 | - else{ |
|
| 307 | - show_error('Unable to find helper file [' . $file . ']'); |
|
| 308 | - } |
|
| 309 | - } |
|
| 246 | + /** |
|
| 247 | + * Load the helper |
|
| 248 | + * |
|
| 249 | + * @param string $function the helper name to be loaded |
|
| 250 | + * |
|
| 251 | + * @return void |
|
| 252 | + */ |
|
| 253 | + public static function functions($function){ |
|
| 254 | + $logger = static::getLogger(); |
|
| 255 | + $function = str_ireplace('.php', '', $function); |
|
| 256 | + $function = trim($function, '/\\'); |
|
| 257 | + $function = str_ireplace('function_', '', $function); |
|
| 258 | + $file = 'function_'.$function.'.php'; |
|
| 259 | + $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 260 | + if(isset(static::$loaded['function_' . $function])){ |
|
| 261 | + $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 262 | + return; |
|
| 263 | + } |
|
| 264 | + $functionFilePath = null; |
|
| 265 | + //first check if this helper is in the module |
|
| 266 | + $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 267 | + $searchModuleName = null; |
|
| 268 | + $obj = & get_instance(); |
|
| 269 | + //check if the request class contains module name |
|
| 270 | + if(strpos($function, '/') !== false){ |
|
| 271 | + $path = explode('/', $function); |
|
| 272 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 273 | + $searchModuleName = $path[0]; |
|
| 274 | + $function = 'function_' . $path[1] . '.php'; |
|
| 275 | + $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 279 | + $searchModuleName = $obj->moduleName; |
|
| 280 | + } |
|
| 281 | + $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
|
| 282 | + if($moduleFunctionPath){ |
|
| 283 | + $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 284 | + $functionFilePath = $moduleFunctionPath; |
|
| 285 | + } |
|
| 286 | + else{ |
|
| 287 | + $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 288 | + } |
|
| 289 | + if(! $functionFilePath){ |
|
| 290 | + $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 291 | + foreach($searchDir as $dir){ |
|
| 292 | + $filePath = $dir . $file; |
|
| 293 | + if(file_exists($filePath)){ |
|
| 294 | + $functionFilePath = $filePath; |
|
| 295 | + //is already found not to continue |
|
| 296 | + break; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 301 | + if($functionFilePath){ |
|
| 302 | + require_once $functionFilePath; |
|
| 303 | + static::$loaded['function_' . $function] = $functionFilePath; |
|
| 304 | + $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 305 | + } |
|
| 306 | + else{ |
|
| 307 | + show_error('Unable to find helper file [' . $file . ']'); |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Load the configuration file |
|
| 313 | - * |
|
| 314 | - * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 315 | - * |
|
| 316 | - * @return void |
|
| 317 | - */ |
|
| 318 | - public static function config($filename){ |
|
| 319 | - $logger = static::getLogger(); |
|
| 320 | - $filename = str_ireplace('.php', '', $filename); |
|
| 321 | - $filename = trim($filename, '/\\'); |
|
| 322 | - $filename = str_ireplace('config_', '', $filename); |
|
| 323 | - $file = 'config_'.$filename.'.php'; |
|
| 324 | - $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 325 | - if(isset(static::$loaded['config_' . $filename])){ |
|
| 326 | - $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 327 | - return; |
|
| 328 | - } |
|
| 329 | - $configFilePath = CONFIG_PATH . $file; |
|
| 330 | - //first check if this config is in the module |
|
| 331 | - $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 332 | - $searchModuleName = null; |
|
| 333 | - $obj = & get_instance(); |
|
| 334 | - //check if the request class contains module name |
|
| 335 | - if(strpos($filename, '/') !== false){ |
|
| 336 | - $path = explode('/', $filename); |
|
| 337 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 338 | - $searchModuleName = $path[0]; |
|
| 339 | - $filename = $path[1] . '.php'; |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 343 | - $searchModuleName = $obj->moduleName; |
|
| 344 | - } |
|
| 345 | - $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
|
| 346 | - if($moduleConfigPath){ |
|
| 347 | - $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 348 | - $configFilePath = $moduleConfigPath; |
|
| 349 | - } |
|
| 350 | - else{ |
|
| 351 | - $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 352 | - } |
|
| 353 | - $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 354 | - $config = array(); |
|
| 355 | - if(file_exists($configFilePath)){ |
|
| 356 | - require_once $configFilePath; |
|
| 357 | - if(! empty($config) && is_array($config)){ |
|
| 358 | - Config::setAll($config); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - else{ |
|
| 362 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 363 | - } |
|
| 364 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 365 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 366 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 367 | - unset($config); |
|
| 368 | - } |
|
| 311 | + /** |
|
| 312 | + * Load the configuration file |
|
| 313 | + * |
|
| 314 | + * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 315 | + * |
|
| 316 | + * @return void |
|
| 317 | + */ |
|
| 318 | + public static function config($filename){ |
|
| 319 | + $logger = static::getLogger(); |
|
| 320 | + $filename = str_ireplace('.php', '', $filename); |
|
| 321 | + $filename = trim($filename, '/\\'); |
|
| 322 | + $filename = str_ireplace('config_', '', $filename); |
|
| 323 | + $file = 'config_'.$filename.'.php'; |
|
| 324 | + $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 325 | + if(isset(static::$loaded['config_' . $filename])){ |
|
| 326 | + $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 327 | + return; |
|
| 328 | + } |
|
| 329 | + $configFilePath = CONFIG_PATH . $file; |
|
| 330 | + //first check if this config is in the module |
|
| 331 | + $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 332 | + $searchModuleName = null; |
|
| 333 | + $obj = & get_instance(); |
|
| 334 | + //check if the request class contains module name |
|
| 335 | + if(strpos($filename, '/') !== false){ |
|
| 336 | + $path = explode('/', $filename); |
|
| 337 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 338 | + $searchModuleName = $path[0]; |
|
| 339 | + $filename = $path[1] . '.php'; |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 343 | + $searchModuleName = $obj->moduleName; |
|
| 344 | + } |
|
| 345 | + $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
|
| 346 | + if($moduleConfigPath){ |
|
| 347 | + $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 348 | + $configFilePath = $moduleConfigPath; |
|
| 349 | + } |
|
| 350 | + else{ |
|
| 351 | + $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 352 | + } |
|
| 353 | + $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 354 | + $config = array(); |
|
| 355 | + if(file_exists($configFilePath)){ |
|
| 356 | + require_once $configFilePath; |
|
| 357 | + if(! empty($config) && is_array($config)){ |
|
| 358 | + Config::setAll($config); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + else{ |
|
| 362 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 363 | + } |
|
| 364 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 365 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 366 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 367 | + unset($config); |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | 370 | |
| 371 | - /** |
|
| 372 | - * Load the language |
|
| 373 | - * |
|
| 374 | - * @param string $language the language name to be loaded |
|
| 375 | - * |
|
| 376 | - * @return void |
|
| 377 | - */ |
|
| 378 | - public static function lang($language){ |
|
| 379 | - $logger = static::getLogger(); |
|
| 380 | - $language = str_ireplace('.php', '', $language); |
|
| 381 | - $language = trim($language, '/\\'); |
|
| 382 | - $language = str_ireplace('lang_', '', $language); |
|
| 383 | - $file = 'lang_'.$language.'.php'; |
|
| 384 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 385 | - if(isset(static::$loaded['lang_' . $language])){ |
|
| 386 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 387 | - return; |
|
| 388 | - } |
|
| 389 | - //determine the current language |
|
| 390 | - $appLang = get_config('default_language'); |
|
| 391 | - //if the language exists in the cookie use it |
|
| 392 | - $cfgKey = get_config('language_cookie_name'); |
|
| 393 | - $objCookie = & class_loader('Cookie'); |
|
| 394 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 395 | - if($cookieLang){ |
|
| 396 | - $appLang = $cookieLang; |
|
| 397 | - } |
|
| 398 | - $languageFilePath = null; |
|
| 399 | - //first check if this language is in the module |
|
| 400 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 401 | - $searchModuleName = null; |
|
| 402 | - $obj = & get_instance(); |
|
| 403 | - //check if the request class contains module name |
|
| 404 | - if(strpos($language, '/') !== false){ |
|
| 405 | - $path = explode('/', $language); |
|
| 406 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 407 | - $searchModuleName = $path[0]; |
|
| 408 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 409 | - $file = $path[0] . DS .$language; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 413 | - $searchModuleName = $obj->moduleName; |
|
| 414 | - } |
|
| 415 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
|
| 416 | - if($moduleLanguagePath){ |
|
| 417 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 418 | - $languageFilePath = $moduleLanguagePath; |
|
| 419 | - } |
|
| 420 | - else{ |
|
| 421 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 422 | - } |
|
| 423 | - if(! $languageFilePath){ |
|
| 424 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 425 | - foreach($searchDir as $dir){ |
|
| 426 | - $filePath = $dir . $appLang . DS . $file; |
|
| 427 | - if(file_exists($filePath)){ |
|
| 428 | - $languageFilePath = $filePath; |
|
| 429 | - //is already found not to continue |
|
| 430 | - break; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 435 | - if($languageFilePath){ |
|
| 436 | - $lang = array(); |
|
| 437 | - require_once $languageFilePath; |
|
| 438 | - if(! empty($lang) && is_array($lang)){ |
|
| 439 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 440 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 441 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 442 | - $langObj->addLangMessages($lang); |
|
| 443 | - //free the memory |
|
| 444 | - unset($lang); |
|
| 445 | - } |
|
| 446 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 447 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 448 | - } |
|
| 449 | - else{ |
|
| 450 | - show_error('Unable to find language file [' . $file . ']'); |
|
| 451 | - } |
|
| 452 | - } |
|
| 371 | + /** |
|
| 372 | + * Load the language |
|
| 373 | + * |
|
| 374 | + * @param string $language the language name to be loaded |
|
| 375 | + * |
|
| 376 | + * @return void |
|
| 377 | + */ |
|
| 378 | + public static function lang($language){ |
|
| 379 | + $logger = static::getLogger(); |
|
| 380 | + $language = str_ireplace('.php', '', $language); |
|
| 381 | + $language = trim($language, '/\\'); |
|
| 382 | + $language = str_ireplace('lang_', '', $language); |
|
| 383 | + $file = 'lang_'.$language.'.php'; |
|
| 384 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 385 | + if(isset(static::$loaded['lang_' . $language])){ |
|
| 386 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 387 | + return; |
|
| 388 | + } |
|
| 389 | + //determine the current language |
|
| 390 | + $appLang = get_config('default_language'); |
|
| 391 | + //if the language exists in the cookie use it |
|
| 392 | + $cfgKey = get_config('language_cookie_name'); |
|
| 393 | + $objCookie = & class_loader('Cookie'); |
|
| 394 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 395 | + if($cookieLang){ |
|
| 396 | + $appLang = $cookieLang; |
|
| 397 | + } |
|
| 398 | + $languageFilePath = null; |
|
| 399 | + //first check if this language is in the module |
|
| 400 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 401 | + $searchModuleName = null; |
|
| 402 | + $obj = & get_instance(); |
|
| 403 | + //check if the request class contains module name |
|
| 404 | + if(strpos($language, '/') !== false){ |
|
| 405 | + $path = explode('/', $language); |
|
| 406 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 407 | + $searchModuleName = $path[0]; |
|
| 408 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 409 | + $file = $path[0] . DS .$language; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 413 | + $searchModuleName = $obj->moduleName; |
|
| 414 | + } |
|
| 415 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
|
| 416 | + if($moduleLanguagePath){ |
|
| 417 | + $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 418 | + $languageFilePath = $moduleLanguagePath; |
|
| 419 | + } |
|
| 420 | + else{ |
|
| 421 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 422 | + } |
|
| 423 | + if(! $languageFilePath){ |
|
| 424 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 425 | + foreach($searchDir as $dir){ |
|
| 426 | + $filePath = $dir . $appLang . DS . $file; |
|
| 427 | + if(file_exists($filePath)){ |
|
| 428 | + $languageFilePath = $filePath; |
|
| 429 | + //is already found not to continue |
|
| 430 | + break; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 435 | + if($languageFilePath){ |
|
| 436 | + $lang = array(); |
|
| 437 | + require_once $languageFilePath; |
|
| 438 | + if(! empty($lang) && is_array($lang)){ |
|
| 439 | + $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 440 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 441 | + $langObj =& class_loader('Lang', 'classes'); |
|
| 442 | + $langObj->addLangMessages($lang); |
|
| 443 | + //free the memory |
|
| 444 | + unset($lang); |
|
| 445 | + } |
|
| 446 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 447 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 448 | + } |
|
| 449 | + else{ |
|
| 450 | + show_error('Unable to find language file [' . $file . ']'); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | 453 | |
| 454 | 454 | |
| 455 | - private function getResourcesFromAutoloadConfig(){ |
|
| 456 | - $autoloads = array(); |
|
| 457 | - $autoloads['config'] = array(); |
|
| 458 | - $autoloads['languages'] = array(); |
|
| 459 | - $autoloads['libraries'] = array(); |
|
| 460 | - $autoloads['models'] = array(); |
|
| 461 | - $autoloads['functions'] = array(); |
|
| 462 | - //loading of the resources in autoload.php configuration file |
|
| 463 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 464 | - $autoload = array(); |
|
| 465 | - require_once CONFIG_PATH . 'autoload.php'; |
|
| 466 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 467 | - $autoloads = array_merge($autoloads, $autoload); |
|
| 468 | - unset($autoload); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - //loading autoload configuration for modules |
|
| 472 | - $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 473 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 474 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 475 | - } |
|
| 476 | - return $autoloads; |
|
| 477 | - } |
|
| 455 | + private function getResourcesFromAutoloadConfig(){ |
|
| 456 | + $autoloads = array(); |
|
| 457 | + $autoloads['config'] = array(); |
|
| 458 | + $autoloads['languages'] = array(); |
|
| 459 | + $autoloads['libraries'] = array(); |
|
| 460 | + $autoloads['models'] = array(); |
|
| 461 | + $autoloads['functions'] = array(); |
|
| 462 | + //loading of the resources in autoload.php configuration file |
|
| 463 | + if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 464 | + $autoload = array(); |
|
| 465 | + require_once CONFIG_PATH . 'autoload.php'; |
|
| 466 | + if(! empty($autoload) && is_array($autoload)){ |
|
| 467 | + $autoloads = array_merge($autoloads, $autoload); |
|
| 468 | + unset($autoload); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + //loading autoload configuration for modules |
|
| 472 | + $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 473 | + if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 474 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 475 | + } |
|
| 476 | + return $autoloads; |
|
| 477 | + } |
|
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * Load the autoload configuration |
|
| 481 | - * @return void |
|
| 482 | - */ |
|
| 483 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 484 | - $autoloads = array(); |
|
| 485 | - $autoloads['config'] = array(); |
|
| 486 | - $autoloads['languages'] = array(); |
|
| 487 | - $autoloads['libraries'] = array(); |
|
| 488 | - $autoloads['models'] = array(); |
|
| 489 | - $autoloads['functions'] = array(); |
|
| 479 | + /** |
|
| 480 | + * Load the autoload configuration |
|
| 481 | + * @return void |
|
| 482 | + */ |
|
| 483 | + private function loadResourcesFromAutoloadConfig(){ |
|
| 484 | + $autoloads = array(); |
|
| 485 | + $autoloads['config'] = array(); |
|
| 486 | + $autoloads['languages'] = array(); |
|
| 487 | + $autoloads['libraries'] = array(); |
|
| 488 | + $autoloads['models'] = array(); |
|
| 489 | + $autoloads['functions'] = array(); |
|
| 490 | 490 | |
| 491 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 492 | - $autoloads = array_merge($autoloads, $list); |
|
| 491 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 492 | + $autoloads = array_merge($autoloads, $list); |
|
| 493 | 493 | |
| 494 | - //config autoload |
|
| 495 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 494 | + //config autoload |
|
| 495 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 496 | 496 | |
| 497 | - //languages autoload |
|
| 498 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 497 | + //languages autoload |
|
| 498 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 499 | 499 | |
| 500 | - //libraries autoload |
|
| 501 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 500 | + //libraries autoload |
|
| 501 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 502 | 502 | |
| 503 | - //models autoload |
|
| 504 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 503 | + //models autoload |
|
| 504 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 505 | 505 | |
| 506 | - //functions autoload |
|
| 507 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 508 | - } |
|
| 506 | + //functions autoload |
|
| 507 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Load the resources autoload array |
|
| 512 | - * @param string $method this object method name to call |
|
| 513 | - * @param array $resources the resource to load |
|
| 514 | - * @return void |
|
| 515 | - */ |
|
| 516 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 517 | - foreach ($resources as $name) { |
|
| 518 | - $this->{$method}($name); |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 510 | + /** |
|
| 511 | + * Load the resources autoload array |
|
| 512 | + * @param string $method this object method name to call |
|
| 513 | + * @param array $resources the resource to load |
|
| 514 | + * @return void |
|
| 515 | + */ |
|
| 516 | + private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 517 | + foreach ($resources as $name) { |
|
| 518 | + $this->{$method}($name); |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
@@ -23,7 +23,7 @@ discard block |
||
| 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 | - class Loader{ |
|
| 26 | + class Loader { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * List of loaded resources |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | private static $logger; |
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 41 | + public function __construct() { |
|
| 42 | 42 | //add the resources already loaded during application bootstrap |
| 43 | 43 | //in the list to prevent duplicate or loading the resources again. |
| 44 | 44 | static::$loaded = class_loaded(); |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * Get the logger singleton instance |
| 52 | 52 | * @return Log the logger instance |
| 53 | 53 | */ |
| 54 | - private static function getLogger(){ |
|
| 55 | - if(self::$logger == null){ |
|
| 56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 54 | + private static function getLogger() { |
|
| 55 | + if (self::$logger == null) { |
|
| 56 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 57 | 57 | self::$logger[0]->setLogger('Library::Loader'); |
| 58 | 58 | } |
| 59 | 59 | return self::$logger[0]; |
@@ -67,25 +67,25 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return void |
| 69 | 69 | */ |
| 70 | - public static function model($class, $instance = null){ |
|
| 70 | + public static function model($class, $instance = null) { |
|
| 71 | 71 | $logger = static::getLogger(); |
| 72 | 72 | $class = str_ireplace('.php', '', $class); |
| 73 | 73 | $class = trim($class, '/\\'); |
| 74 | - $file = ucfirst($class).'.php'; |
|
| 74 | + $file = ucfirst($class) . '.php'; |
|
| 75 | 75 | $logger->debug('Loading model [' . $class . '] ...'); |
| 76 | - if(! $instance){ |
|
| 76 | + if (!$instance) { |
|
| 77 | 77 | //for module |
| 78 | - if(strpos($class, '/') !== false){ |
|
| 78 | + if (strpos($class, '/') !== false) { |
|
| 79 | 79 | $path = explode('/', $class); |
| 80 | - if(isset($path[1])){ |
|
| 80 | + if (isset($path[1])) { |
|
| 81 | 81 | $instance = strtolower($path[1]); |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | - else{ |
|
| 84 | + else { |
|
| 85 | 85 | $instance = strtolower($class); |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | - if(isset(static::$loaded[$instance])){ |
|
| 88 | + if (isset(static::$loaded[$instance])) { |
|
| 89 | 89 | $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 90 | 90 | return; |
| 91 | 91 | } |
@@ -95,43 +95,43 @@ discard block |
||
| 95 | 95 | $searchModuleName = null; |
| 96 | 96 | $obj = & get_instance(); |
| 97 | 97 | //check if the request class contains module name |
| 98 | - if(strpos($class, '/') !== false){ |
|
| 98 | + if (strpos($class, '/') !== false) { |
|
| 99 | 99 | $path = explode('/', $class); |
| 100 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 100 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 101 | 101 | $searchModuleName = $path[0]; |
| 102 | 102 | $class = ucfirst($path[1]); |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | - else{ |
|
| 105 | + else { |
|
| 106 | 106 | $class = ucfirst($class); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 109 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 110 | 110 | $searchModuleName = $obj->moduleName; |
| 111 | 111 | } |
| 112 | 112 | $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
| 113 | - if($moduleModelFilePath){ |
|
| 114 | - $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 113 | + if ($moduleModelFilePath) { |
|
| 114 | + $logger->info('Found model [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleModelFilePath . '] we will used it'); |
|
| 115 | 115 | $classFilePath = $moduleModelFilePath; |
| 116 | 116 | } |
| 117 | - else{ |
|
| 117 | + else { |
|
| 118 | 118 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
| 119 | 119 | } |
| 120 | 120 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
| 121 | - if(file_exists($classFilePath)){ |
|
| 121 | + if (file_exists($classFilePath)) { |
|
| 122 | 122 | require_once $classFilePath; |
| 123 | - if(class_exists($class)){ |
|
| 123 | + if (class_exists($class)) { |
|
| 124 | 124 | $c = new $class(); |
| 125 | 125 | $obj = & get_instance(); |
| 126 | 126 | $obj->{$instance} = $c; |
| 127 | 127 | static::$loaded[$instance] = $class; |
| 128 | 128 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
| 129 | 129 | } |
| 130 | - else{ |
|
| 131 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 130 | + else { |
|
| 131 | + show_error('The file ' . $classFilePath . ' exists but does not contain the class [' . $class . ']'); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | - else{ |
|
| 134 | + else { |
|
| 135 | 135 | show_error('Unable to find the model [' . $class . ']'); |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -146,31 +146,31 @@ discard block |
||
| 146 | 146 | * |
| 147 | 147 | * @return void |
| 148 | 148 | */ |
| 149 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 149 | + public static function library($class, $instance = null, array $params = array()) { |
|
| 150 | 150 | $logger = static::getLogger(); |
| 151 | 151 | $class = str_ireplace('.php', '', $class); |
| 152 | 152 | $class = trim($class, '/\\'); |
| 153 | - $file = ucfirst($class) .'.php'; |
|
| 153 | + $file = ucfirst($class) . '.php'; |
|
| 154 | 154 | $logger->debug('Loading library [' . $class . '] ...'); |
| 155 | - if(! $instance){ |
|
| 155 | + if (!$instance) { |
|
| 156 | 156 | //for module |
| 157 | - if(strpos($class, '/') !== false){ |
|
| 157 | + if (strpos($class, '/') !== false) { |
|
| 158 | 158 | $path = explode('/', $class); |
| 159 | - if(isset($path[1])){ |
|
| 159 | + if (isset($path[1])) { |
|
| 160 | 160 | $instance = strtolower($path[1]); |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | - else{ |
|
| 163 | + else { |
|
| 164 | 164 | $instance = strtolower($class); |
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | - if(isset(static::$loaded[$instance])){ |
|
| 167 | + if (isset(static::$loaded[$instance])) { |
|
| 168 | 168 | $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 169 | 169 | return; |
| 170 | 170 | } |
| 171 | 171 | $obj = & get_instance(); |
| 172 | 172 | //TODO for Database library |
| 173 | - if(strtolower($class) == 'database'){ |
|
| 173 | + if (strtolower($class) == 'database') { |
|
| 174 | 174 | $logger->info('This is the Database library ...'); |
| 175 | 175 | $dbInstance = & class_loader('Database', 'classes/database', $params); |
| 176 | 176 | $obj->{$instance} = $dbInstance; |
@@ -180,44 +180,44 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | $libraryFilePath = null; |
| 182 | 182 | $logger->debug('Check if this is a system library ...'); |
| 183 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 183 | + if (file_exists(CORE_LIBRARY_PATH . $file)) { |
|
| 184 | 184 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
| 185 | 185 | $class = ucfirst($class); |
| 186 | 186 | $logger->info('This library is a system library'); |
| 187 | 187 | } |
| 188 | - else{ |
|
| 188 | + else { |
|
| 189 | 189 | $logger->info('This library is not a system library'); |
| 190 | 190 | //first check if this library is in the module |
| 191 | 191 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
| 192 | 192 | $searchModuleName = null; |
| 193 | 193 | //check if the request class contains module name |
| 194 | - if(strpos($class, '/') !== false){ |
|
| 194 | + if (strpos($class, '/') !== false) { |
|
| 195 | 195 | $path = explode('/', $class); |
| 196 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 196 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 197 | 197 | $searchModuleName = $path[0]; |
| 198 | 198 | $class = ucfirst($path[1]); |
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | - else{ |
|
| 201 | + else { |
|
| 202 | 202 | $class = ucfirst($class); |
| 203 | 203 | } |
| 204 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 204 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 205 | 205 | $searchModuleName = $obj->moduleName; |
| 206 | 206 | } |
| 207 | 207 | $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
| 208 | - if($moduleLibraryPath){ |
|
| 209 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 208 | + if ($moduleLibraryPath) { |
|
| 209 | + $logger->info('Found library [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLibraryPath . '] we will used it'); |
|
| 210 | 210 | $libraryFilePath = $moduleLibraryPath; |
| 211 | 211 | } |
| 212 | - else{ |
|
| 212 | + else { |
|
| 213 | 213 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | - if(! $libraryFilePath){ |
|
| 216 | + if (!$libraryFilePath) { |
|
| 217 | 217 | $searchDir = array(LIBRARY_PATH); |
| 218 | - foreach($searchDir as $dir){ |
|
| 218 | + foreach ($searchDir as $dir) { |
|
| 219 | 219 | $filePath = $dir . $file; |
| 220 | - if(file_exists($filePath)){ |
|
| 220 | + if (file_exists($filePath)) { |
|
| 221 | 221 | $libraryFilePath = $filePath; |
| 222 | 222 | //is already found not to continue |
| 223 | 223 | break; |
@@ -225,20 +225,20 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
| 228 | - if($libraryFilePath){ |
|
| 228 | + if ($libraryFilePath) { |
|
| 229 | 229 | require_once $libraryFilePath; |
| 230 | - if(class_exists($class)){ |
|
| 230 | + if (class_exists($class)) { |
|
| 231 | 231 | $c = $params ? new $class($params) : new $class(); |
| 232 | 232 | $obj = & get_instance(); |
| 233 | 233 | $obj->{$instance} = $c; |
| 234 | 234 | static::$loaded[$instance] = $class; |
| 235 | 235 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
| 236 | 236 | } |
| 237 | - else{ |
|
| 238 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 237 | + else { |
|
| 238 | + show_error('The file ' . $libraryFilePath . ' exists but does not contain the class ' . $class); |
|
| 239 | 239 | } |
| 240 | 240 | } |
| 241 | - else{ |
|
| 241 | + else { |
|
| 242 | 242 | show_error('Unable to find library class [' . $class . ']'); |
| 243 | 243 | } |
| 244 | 244 | } |
@@ -250,14 +250,14 @@ discard block |
||
| 250 | 250 | * |
| 251 | 251 | * @return void |
| 252 | 252 | */ |
| 253 | - public static function functions($function){ |
|
| 253 | + public static function functions($function) { |
|
| 254 | 254 | $logger = static::getLogger(); |
| 255 | 255 | $function = str_ireplace('.php', '', $function); |
| 256 | 256 | $function = trim($function, '/\\'); |
| 257 | 257 | $function = str_ireplace('function_', '', $function); |
| 258 | - $file = 'function_'.$function.'.php'; |
|
| 258 | + $file = 'function_' . $function . '.php'; |
|
| 259 | 259 | $logger->debug('Loading helper [' . $function . '] ...'); |
| 260 | - if(isset(static::$loaded['function_' . $function])){ |
|
| 260 | + if (isset(static::$loaded['function_' . $function])) { |
|
| 261 | 261 | $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
| 262 | 262 | return; |
| 263 | 263 | } |
@@ -267,30 +267,30 @@ discard block |
||
| 267 | 267 | $searchModuleName = null; |
| 268 | 268 | $obj = & get_instance(); |
| 269 | 269 | //check if the request class contains module name |
| 270 | - if(strpos($function, '/') !== false){ |
|
| 270 | + if (strpos($function, '/') !== false) { |
|
| 271 | 271 | $path = explode('/', $function); |
| 272 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 272 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 273 | 273 | $searchModuleName = $path[0]; |
| 274 | 274 | $function = 'function_' . $path[1] . '.php'; |
| 275 | - $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
| 275 | + $file = $path[0] . DS . 'function_' . $function . '.php'; |
|
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 278 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 279 | 279 | $searchModuleName = $obj->moduleName; |
| 280 | 280 | } |
| 281 | 281 | $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
| 282 | - if($moduleFunctionPath){ |
|
| 283 | - $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 282 | + if ($moduleFunctionPath) { |
|
| 283 | + $logger->info('Found helper [' . $function . '] from module [' . $searchModuleName . '], the file path is [' . $moduleFunctionPath . '] we will used it'); |
|
| 284 | 284 | $functionFilePath = $moduleFunctionPath; |
| 285 | 285 | } |
| 286 | - else{ |
|
| 286 | + else { |
|
| 287 | 287 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
| 288 | 288 | } |
| 289 | - if(! $functionFilePath){ |
|
| 289 | + if (!$functionFilePath) { |
|
| 290 | 290 | $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
| 291 | - foreach($searchDir as $dir){ |
|
| 291 | + foreach ($searchDir as $dir) { |
|
| 292 | 292 | $filePath = $dir . $file; |
| 293 | - if(file_exists($filePath)){ |
|
| 293 | + if (file_exists($filePath)) { |
|
| 294 | 294 | $functionFilePath = $filePath; |
| 295 | 295 | //is already found not to continue |
| 296 | 296 | break; |
@@ -298,12 +298,12 @@ discard block |
||
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
| 301 | - if($functionFilePath){ |
|
| 301 | + if ($functionFilePath) { |
|
| 302 | 302 | require_once $functionFilePath; |
| 303 | 303 | static::$loaded['function_' . $function] = $functionFilePath; |
| 304 | 304 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
| 305 | 305 | } |
| 306 | - else{ |
|
| 306 | + else { |
|
| 307 | 307 | show_error('Unable to find helper file [' . $file . ']'); |
| 308 | 308 | } |
| 309 | 309 | } |
@@ -315,14 +315,14 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | * @return void |
| 317 | 317 | */ |
| 318 | - public static function config($filename){ |
|
| 318 | + public static function config($filename) { |
|
| 319 | 319 | $logger = static::getLogger(); |
| 320 | 320 | $filename = str_ireplace('.php', '', $filename); |
| 321 | 321 | $filename = trim($filename, '/\\'); |
| 322 | 322 | $filename = str_ireplace('config_', '', $filename); |
| 323 | - $file = 'config_'.$filename.'.php'; |
|
| 323 | + $file = 'config_' . $filename . '.php'; |
|
| 324 | 324 | $logger->debug('Loading configuration [' . $filename . '] ...'); |
| 325 | - if(isset(static::$loaded['config_' . $filename])){ |
|
| 325 | + if (isset(static::$loaded['config_' . $filename])) { |
|
| 326 | 326 | $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
| 327 | 327 | return; |
| 328 | 328 | } |
@@ -332,34 +332,34 @@ discard block |
||
| 332 | 332 | $searchModuleName = null; |
| 333 | 333 | $obj = & get_instance(); |
| 334 | 334 | //check if the request class contains module name |
| 335 | - if(strpos($filename, '/') !== false){ |
|
| 335 | + if (strpos($filename, '/') !== false) { |
|
| 336 | 336 | $path = explode('/', $filename); |
| 337 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 337 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 338 | 338 | $searchModuleName = $path[0]; |
| 339 | 339 | $filename = $path[1] . '.php'; |
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 342 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 343 | 343 | $searchModuleName = $obj->moduleName; |
| 344 | 344 | } |
| 345 | 345 | $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
| 346 | - if($moduleConfigPath){ |
|
| 347 | - $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 346 | + if ($moduleConfigPath) { |
|
| 347 | + $logger->info('Found config [' . $filename . '] from module [' . $searchModuleName . '], the file path is [' . $moduleConfigPath . '] we will used it'); |
|
| 348 | 348 | $configFilePath = $moduleConfigPath; |
| 349 | 349 | } |
| 350 | - else{ |
|
| 350 | + else { |
|
| 351 | 351 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
| 352 | 352 | } |
| 353 | 353 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
| 354 | 354 | $config = array(); |
| 355 | - if(file_exists($configFilePath)){ |
|
| 355 | + if (file_exists($configFilePath)) { |
|
| 356 | 356 | require_once $configFilePath; |
| 357 | - if(! empty($config) && is_array($config)){ |
|
| 357 | + if (!empty($config) && is_array($config)) { |
|
| 358 | 358 | Config::setAll($config); |
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | - else{ |
|
| 362 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 361 | + else { |
|
| 362 | + show_error('Unable to find config file [' . $configFilePath . ']'); |
|
| 363 | 363 | } |
| 364 | 364 | static::$loaded['config_' . $filename] = $configFilePath; |
| 365 | 365 | $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
@@ -375,14 +375,14 @@ discard block |
||
| 375 | 375 | * |
| 376 | 376 | * @return void |
| 377 | 377 | */ |
| 378 | - public static function lang($language){ |
|
| 378 | + public static function lang($language) { |
|
| 379 | 379 | $logger = static::getLogger(); |
| 380 | 380 | $language = str_ireplace('.php', '', $language); |
| 381 | 381 | $language = trim($language, '/\\'); |
| 382 | 382 | $language = str_ireplace('lang_', '', $language); |
| 383 | - $file = 'lang_'.$language.'.php'; |
|
| 383 | + $file = 'lang_' . $language . '.php'; |
|
| 384 | 384 | $logger->debug('Loading language [' . $language . '] ...'); |
| 385 | - if(isset(static::$loaded['lang_' . $language])){ |
|
| 385 | + if (isset(static::$loaded['lang_' . $language])) { |
|
| 386 | 386 | $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
| 387 | 387 | return; |
| 388 | 388 | } |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | $cfgKey = get_config('language_cookie_name'); |
| 393 | 393 | $objCookie = & class_loader('Cookie'); |
| 394 | 394 | $cookieLang = $objCookie->get($cfgKey); |
| 395 | - if($cookieLang){ |
|
| 395 | + if ($cookieLang) { |
|
| 396 | 396 | $appLang = $cookieLang; |
| 397 | 397 | } |
| 398 | 398 | $languageFilePath = null; |
@@ -401,30 +401,30 @@ discard block |
||
| 401 | 401 | $searchModuleName = null; |
| 402 | 402 | $obj = & get_instance(); |
| 403 | 403 | //check if the request class contains module name |
| 404 | - if(strpos($language, '/') !== false){ |
|
| 404 | + if (strpos($language, '/') !== false) { |
|
| 405 | 405 | $path = explode('/', $language); |
| 406 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 406 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 407 | 407 | $searchModuleName = $path[0]; |
| 408 | 408 | $language = 'lang_' . $path[1] . '.php'; |
| 409 | - $file = $path[0] . DS .$language; |
|
| 409 | + $file = $path[0] . DS . $language; |
|
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 412 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 413 | 413 | $searchModuleName = $obj->moduleName; |
| 414 | 414 | } |
| 415 | 415 | $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
| 416 | - if($moduleLanguagePath){ |
|
| 417 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 416 | + if ($moduleLanguagePath) { |
|
| 417 | + $logger->info('Found language [' . $language . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLanguagePath . '] we will used it'); |
|
| 418 | 418 | $languageFilePath = $moduleLanguagePath; |
| 419 | 419 | } |
| 420 | - else{ |
|
| 420 | + else { |
|
| 421 | 421 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
| 422 | 422 | } |
| 423 | - if(! $languageFilePath){ |
|
| 423 | + if (!$languageFilePath) { |
|
| 424 | 424 | $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
| 425 | - foreach($searchDir as $dir){ |
|
| 425 | + foreach ($searchDir as $dir) { |
|
| 426 | 426 | $filePath = $dir . $appLang . DS . $file; |
| 427 | - if(file_exists($filePath)){ |
|
| 427 | + if (file_exists($filePath)) { |
|
| 428 | 428 | $languageFilePath = $filePath; |
| 429 | 429 | //is already found not to continue |
| 430 | 430 | break; |
@@ -432,13 +432,13 @@ discard block |
||
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | 434 | $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
| 435 | - if($languageFilePath){ |
|
| 435 | + if ($languageFilePath) { |
|
| 436 | 436 | $lang = array(); |
| 437 | 437 | require_once $languageFilePath; |
| 438 | - if(! empty($lang) && is_array($lang)){ |
|
| 439 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 438 | + if (!empty($lang) && is_array($lang)) { |
|
| 439 | + $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
| 440 | 440 | //Note: may be here the class 'Lang' not yet loaded |
| 441 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 441 | + $langObj = & class_loader('Lang', 'classes'); |
|
| 442 | 442 | $langObj->addLangMessages($lang); |
| 443 | 443 | //free the memory |
| 444 | 444 | unset($lang); |
@@ -446,13 +446,13 @@ discard block |
||
| 446 | 446 | static::$loaded['lang_' . $language] = $languageFilePath; |
| 447 | 447 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
| 448 | 448 | } |
| 449 | - else{ |
|
| 449 | + else { |
|
| 450 | 450 | show_error('Unable to find language file [' . $file . ']'); |
| 451 | 451 | } |
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | |
| 455 | - private function getResourcesFromAutoloadConfig(){ |
|
| 455 | + private function getResourcesFromAutoloadConfig() { |
|
| 456 | 456 | $autoloads = array(); |
| 457 | 457 | $autoloads['config'] = array(); |
| 458 | 458 | $autoloads['languages'] = array(); |
@@ -460,17 +460,17 @@ discard block |
||
| 460 | 460 | $autoloads['models'] = array(); |
| 461 | 461 | $autoloads['functions'] = array(); |
| 462 | 462 | //loading of the resources in autoload.php configuration file |
| 463 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 463 | + if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
| 464 | 464 | $autoload = array(); |
| 465 | 465 | require_once CONFIG_PATH . 'autoload.php'; |
| 466 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 466 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 467 | 467 | $autoloads = array_merge($autoloads, $autoload); |
| 468 | 468 | unset($autoload); |
| 469 | 469 | } |
| 470 | 470 | } |
| 471 | 471 | //loading autoload configuration for modules |
| 472 | 472 | $modulesAutoloads = Module::getModulesAutoloadConfig(); |
| 473 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 473 | + if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
| 474 | 474 | $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
| 475 | 475 | } |
| 476 | 476 | return $autoloads; |
@@ -480,7 +480,7 @@ discard block |
||
| 480 | 480 | * Load the autoload configuration |
| 481 | 481 | * @return void |
| 482 | 482 | */ |
| 483 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 483 | + private function loadResourcesFromAutoloadConfig() { |
|
| 484 | 484 | $autoloads = array(); |
| 485 | 485 | $autoloads['config'] = array(); |
| 486 | 486 | $autoloads['languages'] = array(); |
@@ -513,7 +513,7 @@ discard block |
||
| 513 | 513 | * @param array $resources the resource to load |
| 514 | 514 | * @return void |
| 515 | 515 | */ |
| 516 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 516 | + private function loadAutoloadResourcesArray($method, array $resources) { |
|
| 517 | 517 | foreach ($resources as $name) { |
| 518 | 518 | $this->{$method}($name); |
| 519 | 519 | } |