@@ -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 | + } |
|
@@ -1,439 +1,439 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of request header to send with response |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $headers = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of request header to send with response |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $headers = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The final page content to display to user |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $_pageRender = null; |
|
| 41 | + /** |
|
| 42 | + * The final page content to display to user |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $_pageRender = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The current request URL |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $_currentUrl = null; |
|
| 47 | + /** |
|
| 48 | + * The current request URL |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $_currentUrl = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The current request URL cache key |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_currentUrlCacheKey = null; |
|
| 53 | + /** |
|
| 54 | + * The current request URL cache key |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_currentUrlCacheKey = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Whether we can compress the output using Gzip |
|
| 61 | - * @var boolean |
|
| 62 | - */ |
|
| 63 | - private static $_canCompressOutput = false; |
|
| 59 | + /** |
|
| 60 | + * Whether we can compress the output using Gzip |
|
| 61 | + * @var boolean |
|
| 62 | + */ |
|
| 63 | + private static $_canCompressOutput = false; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Construct new response instance |
|
| 67 | - */ |
|
| 68 | - public function __construct(){ |
|
| 69 | - $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
| 70 | - . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
| 65 | + /** |
|
| 66 | + * Construct new response instance |
|
| 67 | + */ |
|
| 68 | + public function __construct(){ |
|
| 69 | + $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
| 70 | + . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
| 71 | 71 | |
| 72 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 72 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 73 | 73 | |
| 74 | - self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | - && extension_loaded('zlib') |
|
| 78 | - && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | - } |
|
| 74 | + self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | + && extension_loaded('zlib') |
|
| 78 | + && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Get the logger singleton instance |
|
| 83 | - * @return Log the logger instance |
|
| 84 | - */ |
|
| 85 | - private static function getLogger(){ |
|
| 86 | - if(self::$logger == null){ |
|
| 87 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 88 | - self::$logger[0]->setLogger('Library::Response'); |
|
| 89 | - } |
|
| 90 | - return self::$logger[0]; |
|
| 91 | - } |
|
| 81 | + /** |
|
| 82 | + * Get the logger singleton instance |
|
| 83 | + * @return Log the logger instance |
|
| 84 | + */ |
|
| 85 | + private static function getLogger(){ |
|
| 86 | + if(self::$logger == null){ |
|
| 87 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 88 | + self::$logger[0]->setLogger('Library::Response'); |
|
| 89 | + } |
|
| 90 | + return self::$logger[0]; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Send the HTTP Response headers |
|
| 95 | - * @param integer $httpCode the HTTP status code |
|
| 96 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 97 | - */ |
|
| 98 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 99 | - set_http_status_header($httpCode); |
|
| 100 | - self::setHeaders($headers); |
|
| 101 | - if(! headers_sent()){ |
|
| 102 | - foreach(self::getHeaders() as $key => $value){ |
|
| 103 | - header($key .': '.$value); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 93 | + /** |
|
| 94 | + * Send the HTTP Response headers |
|
| 95 | + * @param integer $httpCode the HTTP status code |
|
| 96 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 97 | + */ |
|
| 98 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 99 | + set_http_status_header($httpCode); |
|
| 100 | + self::setHeaders($headers); |
|
| 101 | + if(! headers_sent()){ |
|
| 102 | + foreach(self::getHeaders() as $key => $value){ |
|
| 103 | + header($key .': '.$value); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the list of the headers |
|
| 110 | - * @return array the headers list |
|
| 111 | - */ |
|
| 112 | - public static function getHeaders(){ |
|
| 113 | - return self::$headers; |
|
| 114 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the list of the headers |
|
| 110 | + * @return array the headers list |
|
| 111 | + */ |
|
| 112 | + public static function getHeaders(){ |
|
| 113 | + return self::$headers; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Get the header value for the given name |
|
| 118 | - * @param string $name the header name |
|
| 119 | - * @return string the header value |
|
| 120 | - */ |
|
| 121 | - public static function getHeader($name){ |
|
| 122 | - return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 123 | - } |
|
| 116 | + /** |
|
| 117 | + * Get the header value for the given name |
|
| 118 | + * @param string $name the header name |
|
| 119 | + * @return string the header value |
|
| 120 | + */ |
|
| 121 | + public static function getHeader($name){ |
|
| 122 | + return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Set the header value for the specified name |
|
| 128 | - * @param string $name the header name |
|
| 129 | - * @param string $value the header value to be set |
|
| 130 | - */ |
|
| 131 | - public static function setHeader($name, $value){ |
|
| 132 | - self::$headers[$name] = $value; |
|
| 133 | - } |
|
| 126 | + /** |
|
| 127 | + * Set the header value for the specified name |
|
| 128 | + * @param string $name the header name |
|
| 129 | + * @param string $value the header value to be set |
|
| 130 | + */ |
|
| 131 | + public static function setHeader($name, $value){ |
|
| 132 | + self::$headers[$name] = $value; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Set the headers using array |
|
| 137 | - * @param array $headers the list of the headers to set. |
|
| 138 | - * Note: this will merge with the existing headers |
|
| 139 | - */ |
|
| 140 | - public static function setHeaders(array $headers){ |
|
| 141 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 142 | - } |
|
| 135 | + /** |
|
| 136 | + * Set the headers using array |
|
| 137 | + * @param array $headers the list of the headers to set. |
|
| 138 | + * Note: this will merge with the existing headers |
|
| 139 | + */ |
|
| 140 | + public static function setHeaders(array $headers){ |
|
| 141 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Redirect user in the specified page |
|
| 146 | - * @param string $path the URL or URI to be redirect to |
|
| 147 | - */ |
|
| 148 | - public static function redirect($path = ''){ |
|
| 149 | - $logger = self::getLogger(); |
|
| 150 | - $url = Url::site_url($path); |
|
| 151 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 152 | - if(! headers_sent()){ |
|
| 153 | - header('Location: '.$url); |
|
| 154 | - exit; |
|
| 155 | - } |
|
| 156 | - else{ |
|
| 157 | - echo '<script> |
|
| 144 | + /** |
|
| 145 | + * Redirect user in the specified page |
|
| 146 | + * @param string $path the URL or URI to be redirect to |
|
| 147 | + */ |
|
| 148 | + public static function redirect($path = ''){ |
|
| 149 | + $logger = self::getLogger(); |
|
| 150 | + $url = Url::site_url($path); |
|
| 151 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 152 | + if(! headers_sent()){ |
|
| 153 | + header('Location: '.$url); |
|
| 154 | + exit; |
|
| 155 | + } |
|
| 156 | + else{ |
|
| 157 | + echo '<script> |
|
| 158 | 158 | location.href = "'.$url.'"; |
| 159 | 159 | </script>'; |
| 160 | - } |
|
| 161 | - } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Render the view to display later or return the content |
|
| 165 | - * @param string $view the view name or path |
|
| 166 | - * @param array|object $data the variable data to use in the view |
|
| 167 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 168 | - * @return void|string if $return is true will return the view content otherwise |
|
| 169 | - * will display the view content. |
|
| 170 | - */ |
|
| 171 | - public function render($view, $data = null, $return = false){ |
|
| 172 | - $logger = self::getLogger(); |
|
| 173 | - //convert data to an array |
|
| 174 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 175 | - $view = str_ireplace('.php', '', $view); |
|
| 176 | - $view = trim($view, '/\\'); |
|
| 177 | - $viewFile = $view . '.php'; |
|
| 178 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 163 | + /** |
|
| 164 | + * Render the view to display later or return the content |
|
| 165 | + * @param string $view the view name or path |
|
| 166 | + * @param array|object $data the variable data to use in the view |
|
| 167 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 168 | + * @return void|string if $return is true will return the view content otherwise |
|
| 169 | + * will display the view content. |
|
| 170 | + */ |
|
| 171 | + public function render($view, $data = null, $return = false){ |
|
| 172 | + $logger = self::getLogger(); |
|
| 173 | + //convert data to an array |
|
| 174 | + $data = ! is_array($data) ? (array) $data : $data; |
|
| 175 | + $view = str_ireplace('.php', '', $view); |
|
| 176 | + $view = trim($view, '/\\'); |
|
| 177 | + $viewFile = $view . '.php'; |
|
| 178 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 179 | 179 | |
| 180 | - //super instance |
|
| 181 | - $obj = & get_instance(); |
|
| 180 | + //super instance |
|
| 181 | + $obj = & get_instance(); |
|
| 182 | 182 | |
| 183 | - if(Module::hasModule()){ |
|
| 184 | - //check in module first |
|
| 185 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 186 | - $mod = null; |
|
| 187 | - //check if the request class contains module name |
|
| 188 | - if(strpos($view, '/') !== false){ |
|
| 189 | - $viewPath = explode('/', $view); |
|
| 190 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 191 | - $mod = $viewPath[0]; |
|
| 192 | - array_shift($viewPath); |
|
| 193 | - $view = implode('/', $viewPath); |
|
| 194 | - $viewFile = $view . '.php'; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 198 | - $mod = $obj->moduleName; |
|
| 199 | - } |
|
| 200 | - if($mod){ |
|
| 201 | - $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 202 | - if($moduleViewPath){ |
|
| 203 | - $path = $moduleViewPath; |
|
| 204 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 205 | - } |
|
| 206 | - else{ |
|
| 207 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - else{ |
|
| 211 | - $logger->info('The current request does not use module using the default location.'); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 215 | - $found = false; |
|
| 216 | - if(file_exists($path)){ |
|
| 217 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 218 | - if(! isset($this->{$key})){ |
|
| 219 | - $this->{$key} = & $obj->{$key}; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - ob_start(); |
|
| 223 | - extract($data); |
|
| 224 | - //need use require() instead of require_once because can load this view many time |
|
| 225 | - require $path; |
|
| 226 | - $content = ob_get_clean(); |
|
| 227 | - if($return){ |
|
| 228 | - return $content; |
|
| 229 | - } |
|
| 230 | - $this->_pageRender .= $content; |
|
| 231 | - $found = true; |
|
| 232 | - } |
|
| 233 | - if(! $found){ |
|
| 234 | - show_error('Unable to find view [' .$view . ']'); |
|
| 235 | - } |
|
| 236 | - } |
|
| 183 | + if(Module::hasModule()){ |
|
| 184 | + //check in module first |
|
| 185 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 186 | + $mod = null; |
|
| 187 | + //check if the request class contains module name |
|
| 188 | + if(strpos($view, '/') !== false){ |
|
| 189 | + $viewPath = explode('/', $view); |
|
| 190 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 191 | + $mod = $viewPath[0]; |
|
| 192 | + array_shift($viewPath); |
|
| 193 | + $view = implode('/', $viewPath); |
|
| 194 | + $viewFile = $view . '.php'; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + if(! $mod && !empty($obj->moduleName)){ |
|
| 198 | + $mod = $obj->moduleName; |
|
| 199 | + } |
|
| 200 | + if($mod){ |
|
| 201 | + $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 202 | + if($moduleViewPath){ |
|
| 203 | + $path = $moduleViewPath; |
|
| 204 | + $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 205 | + } |
|
| 206 | + else{ |
|
| 207 | + $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + else{ |
|
| 211 | + $logger->info('The current request does not use module using the default location.'); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 215 | + $found = false; |
|
| 216 | + if(file_exists($path)){ |
|
| 217 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 218 | + if(! isset($this->{$key})){ |
|
| 219 | + $this->{$key} = & $obj->{$key}; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + ob_start(); |
|
| 223 | + extract($data); |
|
| 224 | + //need use require() instead of require_once because can load this view many time |
|
| 225 | + require $path; |
|
| 226 | + $content = ob_get_clean(); |
|
| 227 | + if($return){ |
|
| 228 | + return $content; |
|
| 229 | + } |
|
| 230 | + $this->_pageRender .= $content; |
|
| 231 | + $found = true; |
|
| 232 | + } |
|
| 233 | + if(! $found){ |
|
| 234 | + show_error('Unable to find view [' .$view . ']'); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * Send the final page output to user |
|
| 240 | - */ |
|
| 241 | - public function renderFinalPage(){ |
|
| 242 | - $logger = self::getLogger(); |
|
| 243 | - $obj = & get_instance(); |
|
| 244 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 245 | - $dispatcher = $obj->eventdispatcher; |
|
| 246 | - $content = $this->_pageRender; |
|
| 247 | - if(! $content){ |
|
| 248 | - $logger->warning('The final view content is empty.'); |
|
| 249 | - return; |
|
| 250 | - } |
|
| 251 | - //dispatch |
|
| 252 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 253 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 254 | - if(empty($content)){ |
|
| 255 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 256 | - } |
|
| 238 | + /** |
|
| 239 | + * Send the final page output to user |
|
| 240 | + */ |
|
| 241 | + public function renderFinalPage(){ |
|
| 242 | + $logger = self::getLogger(); |
|
| 243 | + $obj = & get_instance(); |
|
| 244 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 245 | + $dispatcher = $obj->eventdispatcher; |
|
| 246 | + $content = $this->_pageRender; |
|
| 247 | + if(! $content){ |
|
| 248 | + $logger->warning('The final view content is empty.'); |
|
| 249 | + return; |
|
| 250 | + } |
|
| 251 | + //dispatch |
|
| 252 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 253 | + $content = ! empty($event->payload) ? $event->payload : null; |
|
| 254 | + if(empty($content)){ |
|
| 255 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - //check whether need save the page into cache. |
|
| 259 | - if($cachePageStatus){ |
|
| 260 | - //current page URL |
|
| 261 | - $url = $this->_currentUrl; |
|
| 262 | - //Cache view Time to live in second |
|
| 263 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 264 | - if (!empty($obj->view_cache_ttl)){ |
|
| 265 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 266 | - } |
|
| 267 | - //the cache handler instance |
|
| 268 | - $cacheInstance = $obj->cache; |
|
| 269 | - //the current page cache key for identification |
|
| 270 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 258 | + //check whether need save the page into cache. |
|
| 259 | + if($cachePageStatus){ |
|
| 260 | + //current page URL |
|
| 261 | + $url = $this->_currentUrl; |
|
| 262 | + //Cache view Time to live in second |
|
| 263 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 264 | + if (!empty($obj->view_cache_ttl)){ |
|
| 265 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 266 | + } |
|
| 267 | + //the cache handler instance |
|
| 268 | + $cacheInstance = $obj->cache; |
|
| 269 | + //the current page cache key for identification |
|
| 270 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 271 | 271 | |
| 272 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 273 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 272 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 273 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 274 | 274 | |
| 275 | - //get the cache information to prepare header to send to browser |
|
| 276 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 277 | - if($cacheInfo){ |
|
| 278 | - $lastModified = $cacheInfo['mtime']; |
|
| 279 | - $expire = $cacheInfo['expire']; |
|
| 280 | - $maxAge = $expire - time(); |
|
| 281 | - self::setHeader('Pragma', 'public'); |
|
| 282 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 283 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 284 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 285 | - } |
|
| 286 | - } |
|
| 275 | + //get the cache information to prepare header to send to browser |
|
| 276 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 277 | + if($cacheInfo){ |
|
| 278 | + $lastModified = $cacheInfo['mtime']; |
|
| 279 | + $expire = $cacheInfo['expire']; |
|
| 280 | + $maxAge = $expire - time(); |
|
| 281 | + self::setHeader('Pragma', 'public'); |
|
| 282 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 283 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 284 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - // Parse out the elapsed time and memory usage, |
|
| 289 | - // then swap the pseudo-variables with the data |
|
| 290 | - $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 291 | - $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 292 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 288 | + // Parse out the elapsed time and memory usage, |
|
| 289 | + // then swap the pseudo-variables with the data |
|
| 290 | + $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 291 | + $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 292 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 293 | 293 | |
| 294 | - //compress the output if is available |
|
| 295 | - $type = null; |
|
| 296 | - if (self::$_canCompressOutput){ |
|
| 297 | - $type = 'ob_gzhandler'; |
|
| 298 | - } |
|
| 299 | - ob_start($type); |
|
| 300 | - self::sendHeaders(200); |
|
| 301 | - echo $content; |
|
| 302 | - ob_end_flush(); |
|
| 303 | - } |
|
| 294 | + //compress the output if is available |
|
| 295 | + $type = null; |
|
| 296 | + if (self::$_canCompressOutput){ |
|
| 297 | + $type = 'ob_gzhandler'; |
|
| 298 | + } |
|
| 299 | + ob_start($type); |
|
| 300 | + self::sendHeaders(200); |
|
| 301 | + echo $content; |
|
| 302 | + ob_end_flush(); |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Send the final page output to user if is cached |
|
| 307 | - */ |
|
| 308 | - public function renderFinalPageFromCache(&$cache){ |
|
| 309 | - $logger = self::getLogger(); |
|
| 310 | - $url = $this->_currentUrl; |
|
| 311 | - //the current page cache key for identification |
|
| 312 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 305 | + /** |
|
| 306 | + * Send the final page output to user if is cached |
|
| 307 | + */ |
|
| 308 | + public function renderFinalPageFromCache(&$cache){ |
|
| 309 | + $logger = self::getLogger(); |
|
| 310 | + $url = $this->_currentUrl; |
|
| 311 | + //the current page cache key for identification |
|
| 312 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 313 | 313 | |
| 314 | - $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 315 | - //get the cache information to prepare header to send to browser |
|
| 316 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 317 | - if($cacheInfo){ |
|
| 318 | - $lastModified = $cacheInfo['mtime']; |
|
| 319 | - $expire = $cacheInfo['expire']; |
|
| 320 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 321 | - self::setHeader('Pragma', 'public'); |
|
| 322 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 323 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 324 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 325 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 326 | - $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 327 | - self::sendHeaders(304); |
|
| 328 | - return; |
|
| 329 | - } |
|
| 330 | - else{ |
|
| 331 | - $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 332 | - self::sendHeaders(200); |
|
| 333 | - //get the cache content |
|
| 334 | - $content = $cache->get($pageCacheKey); |
|
| 335 | - if($content){ |
|
| 336 | - $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 337 | - //load benchmark class |
|
| 338 | - $benchmark = & class_loader('Benchmark'); |
|
| 314 | + $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 315 | + //get the cache information to prepare header to send to browser |
|
| 316 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 317 | + if($cacheInfo){ |
|
| 318 | + $lastModified = $cacheInfo['mtime']; |
|
| 319 | + $expire = $cacheInfo['expire']; |
|
| 320 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 321 | + self::setHeader('Pragma', 'public'); |
|
| 322 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 323 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 324 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 325 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 326 | + $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 327 | + self::sendHeaders(304); |
|
| 328 | + return; |
|
| 329 | + } |
|
| 330 | + else{ |
|
| 331 | + $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 332 | + self::sendHeaders(200); |
|
| 333 | + //get the cache content |
|
| 334 | + $content = $cache->get($pageCacheKey); |
|
| 335 | + if($content){ |
|
| 336 | + $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 337 | + //load benchmark class |
|
| 338 | + $benchmark = & class_loader('Benchmark'); |
|
| 339 | 339 | |
| 340 | - // Parse out the elapsed time and memory usage, |
|
| 341 | - // then swap the pseudo-variables with the data |
|
| 342 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 343 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 344 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 340 | + // Parse out the elapsed time and memory usage, |
|
| 341 | + // then swap the pseudo-variables with the data |
|
| 342 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 343 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 344 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 345 | 345 | |
| 346 | - ///display the final output |
|
| 347 | - //compress the output if is available |
|
| 348 | - if (self::$_canCompressOutput){ |
|
| 349 | - ob_start('ob_gzhandler'); |
|
| 350 | - } |
|
| 351 | - else{ |
|
| 352 | - ob_start(); |
|
| 353 | - } |
|
| 354 | - echo $content; |
|
| 355 | - ob_end_flush(); |
|
| 356 | - return; |
|
| 357 | - } |
|
| 358 | - else{ |
|
| 359 | - $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 360 | - $cache->delete($pageCacheKey); |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - } |
|
| 346 | + ///display the final output |
|
| 347 | + //compress the output if is available |
|
| 348 | + if (self::$_canCompressOutput){ |
|
| 349 | + ob_start('ob_gzhandler'); |
|
| 350 | + } |
|
| 351 | + else{ |
|
| 352 | + ob_start(); |
|
| 353 | + } |
|
| 354 | + echo $content; |
|
| 355 | + ob_end_flush(); |
|
| 356 | + return; |
|
| 357 | + } |
|
| 358 | + else{ |
|
| 359 | + $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 360 | + $cache->delete($pageCacheKey); |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Get the final page to be rendered |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public function getFinalPageRendered(){ |
|
| 371 | - return $this->_pageRender; |
|
| 372 | - } |
|
| 366 | + /** |
|
| 367 | + * Get the final page to be rendered |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public function getFinalPageRendered(){ |
|
| 371 | + return $this->_pageRender; |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Send the HTTP 404 error if can not found the |
|
| 376 | - * routing information for the current request |
|
| 377 | - */ |
|
| 378 | - public static function send404(){ |
|
| 379 | - /********* for logs **************/ |
|
| 380 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 381 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 382 | - $logger = self::getLogger(); |
|
| 383 | - $request =& class_loader('Request', 'classes'); |
|
| 384 | - $userAgent =& class_loader('Browser'); |
|
| 385 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 374 | + /** |
|
| 375 | + * Send the HTTP 404 error if can not found the |
|
| 376 | + * routing information for the current request |
|
| 377 | + */ |
|
| 378 | + public static function send404(){ |
|
| 379 | + /********* for logs **************/ |
|
| 380 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 381 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 382 | + $logger = self::getLogger(); |
|
| 383 | + $request =& class_loader('Request', 'classes'); |
|
| 384 | + $userAgent =& class_loader('Browser'); |
|
| 385 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 386 | 386 | |
| 387 | - //here can't use Loader::functions just include the helper manually |
|
| 388 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 387 | + //here can't use Loader::functions just include the helper manually |
|
| 388 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 389 | 389 | |
| 390 | - $str = '[404 page not found] : '; |
|
| 391 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 392 | - $logger->error($str); |
|
| 393 | - /***********************************/ |
|
| 394 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 395 | - if(file_exists($path)){ |
|
| 396 | - //compress the output if is available |
|
| 397 | - if (self::$_canCompressOutput){ |
|
| 398 | - ob_start('ob_gzhandler'); |
|
| 399 | - } |
|
| 400 | - else{ |
|
| 401 | - ob_start(); |
|
| 402 | - } |
|
| 403 | - require_once $path; |
|
| 404 | - $output = ob_get_clean(); |
|
| 405 | - self::sendHeaders(404); |
|
| 406 | - echo $output; |
|
| 407 | - } |
|
| 408 | - else{ |
|
| 409 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 410 | - } |
|
| 411 | - } |
|
| 390 | + $str = '[404 page not found] : '; |
|
| 391 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 392 | + $logger->error($str); |
|
| 393 | + /***********************************/ |
|
| 394 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 395 | + if(file_exists($path)){ |
|
| 396 | + //compress the output if is available |
|
| 397 | + if (self::$_canCompressOutput){ |
|
| 398 | + ob_start('ob_gzhandler'); |
|
| 399 | + } |
|
| 400 | + else{ |
|
| 401 | + ob_start(); |
|
| 402 | + } |
|
| 403 | + require_once $path; |
|
| 404 | + $output = ob_get_clean(); |
|
| 405 | + self::sendHeaders(404); |
|
| 406 | + echo $output; |
|
| 407 | + } |
|
| 408 | + else{ |
|
| 409 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | 412 | |
| 413 | - /** |
|
| 414 | - * Display the error to user |
|
| 415 | - * @param array $data the error information |
|
| 416 | - */ |
|
| 417 | - public static function sendError(array $data = array()){ |
|
| 418 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 419 | - if(file_exists($path)){ |
|
| 420 | - //compress the output if exists |
|
| 421 | - if (self::$_canCompressOutput){ |
|
| 422 | - ob_start('ob_gzhandler'); |
|
| 423 | - } |
|
| 424 | - else{ |
|
| 425 | - ob_start(); |
|
| 426 | - } |
|
| 427 | - extract($data); |
|
| 428 | - require_once $path; |
|
| 429 | - $output = ob_get_clean(); |
|
| 430 | - self::sendHeaders(503); |
|
| 431 | - echo $output; |
|
| 432 | - } |
|
| 433 | - else{ |
|
| 434 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 435 | - set_http_status_header(503); |
|
| 436 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 413 | + /** |
|
| 414 | + * Display the error to user |
|
| 415 | + * @param array $data the error information |
|
| 416 | + */ |
|
| 417 | + public static function sendError(array $data = array()){ |
|
| 418 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 419 | + if(file_exists($path)){ |
|
| 420 | + //compress the output if exists |
|
| 421 | + if (self::$_canCompressOutput){ |
|
| 422 | + ob_start('ob_gzhandler'); |
|
| 423 | + } |
|
| 424 | + else{ |
|
| 425 | + ob_start(); |
|
| 426 | + } |
|
| 427 | + extract($data); |
|
| 428 | + require_once $path; |
|
| 429 | + $output = ob_get_clean(); |
|
| 430 | + self::sendHeaders(503); |
|
| 431 | + echo $output; |
|
| 432 | + } |
|
| 433 | + else{ |
|
| 434 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 435 | + set_http_status_header(503); |
|
| 436 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
@@ -1,256 +1,256 @@ |
||
| 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 Request{ |
|
| 27 | + class Request{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The value for the super global $_GET |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - public $get = null; |
|
| 29 | + /** |
|
| 30 | + * The value for the super global $_GET |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + public $get = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The value for the super global $_POST |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $post = null; |
|
| 35 | + /** |
|
| 36 | + * The value for the super global $_POST |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $post = null; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The value for the super global $_SERVER |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - public $server = null; |
|
| 41 | + /** |
|
| 42 | + * The value for the super global $_SERVER |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + public $server = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The value for the super global $_COOKIE |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - public $cookie = null; |
|
| 47 | + /** |
|
| 48 | + * The value for the super global $_COOKIE |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + public $cookie = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The value for the super global $_FILES |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - public $file = null; |
|
| 53 | + /** |
|
| 54 | + * The value for the super global $_FILES |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + public $file = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * The value for the super global $_REQUEST |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - public $query = null; |
|
| 59 | + /** |
|
| 60 | + * The value for the super global $_REQUEST |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + public $query = null; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * The session instance |
|
| 67 | - * @var Session |
|
| 68 | - */ |
|
| 69 | - public $session = null; |
|
| 65 | + /** |
|
| 66 | + * The session instance |
|
| 67 | + * @var Session |
|
| 68 | + */ |
|
| 69 | + public $session = null; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * The request headers |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - public $header = null; |
|
| 71 | + /** |
|
| 72 | + * The request headers |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + public $header = null; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | - * @var null |
|
| 80 | - */ |
|
| 81 | - private $method = null; |
|
| 77 | + /** |
|
| 78 | + * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | + * @var null |
|
| 80 | + */ |
|
| 81 | + private $method = null; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * The current request URI |
|
| 85 | - * @var string |
|
| 86 | - */ |
|
| 87 | - private $requestUri = null; |
|
| 83 | + /** |
|
| 84 | + * The current request URI |
|
| 85 | + * @var string |
|
| 86 | + */ |
|
| 87 | + private $requestUri = null; |
|
| 88 | 88 | |
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Construct new request instance |
|
| 92 | - */ |
|
| 93 | - public function __construct(){ |
|
| 94 | - $this->get = $_GET; |
|
| 95 | - $this->post = $_POST; |
|
| 96 | - $this->server = $_SERVER; |
|
| 97 | - $this->query = $_REQUEST; |
|
| 98 | - $this->cookie = $_COOKIE; |
|
| 99 | - $this->file = $_FILES; |
|
| 100 | - $this->session =& class_loader('Session', 'classes'); |
|
| 101 | - $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | - $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | - $this->header = array(); |
|
| 104 | - if(function_exists('apache_request_headers')){ |
|
| 105 | - $this->header = apache_request_headers(); |
|
| 106 | - } |
|
| 107 | - else if(function_exists('getallheaders')){ |
|
| 108 | - $this->header = getallheaders(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 90 | + /** |
|
| 91 | + * Construct new request instance |
|
| 92 | + */ |
|
| 93 | + public function __construct(){ |
|
| 94 | + $this->get = $_GET; |
|
| 95 | + $this->post = $_POST; |
|
| 96 | + $this->server = $_SERVER; |
|
| 97 | + $this->query = $_REQUEST; |
|
| 98 | + $this->cookie = $_COOKIE; |
|
| 99 | + $this->file = $_FILES; |
|
| 100 | + $this->session =& class_loader('Session', 'classes'); |
|
| 101 | + $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | + $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | + $this->header = array(); |
|
| 104 | + if(function_exists('apache_request_headers')){ |
|
| 105 | + $this->header = apache_request_headers(); |
|
| 106 | + } |
|
| 107 | + else if(function_exists('getallheaders')){ |
|
| 108 | + $this->header = getallheaders(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Get the request method |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function method(){ |
|
| 117 | - return $this->method; |
|
| 118 | - } |
|
| 112 | + /** |
|
| 113 | + * Get the request method |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function method(){ |
|
| 117 | + return $this->method; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Get the request URI |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function requestUri(){ |
|
| 125 | - return $this->requestUri; |
|
| 126 | - } |
|
| 120 | + /** |
|
| 121 | + * Get the request URI |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function requestUri(){ |
|
| 125 | + return $this->requestUri; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 130 | - * @param string $key the item key to be fetched |
|
| 131 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 132 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 133 | - */ |
|
| 134 | - public function query($key = null, $xss = true){ |
|
| 135 | - if(empty($key)){ |
|
| 136 | - //return all |
|
| 137 | - return $xss ? clean_input($this->query) : $this->query; |
|
| 138 | - } |
|
| 139 | - $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 140 | - if($xss){ |
|
| 141 | - $query = clean_input($query); |
|
| 142 | - } |
|
| 143 | - return $query; |
|
| 144 | - } |
|
| 128 | + /** |
|
| 129 | + * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 130 | + * @param string $key the item key to be fetched |
|
| 131 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 132 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 133 | + */ |
|
| 134 | + public function query($key = null, $xss = true){ |
|
| 135 | + if(empty($key)){ |
|
| 136 | + //return all |
|
| 137 | + return $xss ? clean_input($this->query) : $this->query; |
|
| 138 | + } |
|
| 139 | + $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 140 | + if($xss){ |
|
| 141 | + $query = clean_input($query); |
|
| 142 | + } |
|
| 143 | + return $query; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 148 | - * @param string $key the item key to be fetched |
|
| 149 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 150 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 151 | - */ |
|
| 152 | - public function get($key = null, $xss = true){ |
|
| 153 | - if(empty($key)){ |
|
| 154 | - //return all |
|
| 155 | - return $xss ? clean_input($this->get) : $this->get; |
|
| 156 | - } |
|
| 157 | - $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 158 | - if($xss){ |
|
| 159 | - $get = clean_input($get); |
|
| 160 | - } |
|
| 161 | - return $get; |
|
| 162 | - } |
|
| 146 | + /** |
|
| 147 | + * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 148 | + * @param string $key the item key to be fetched |
|
| 149 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 150 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 151 | + */ |
|
| 152 | + public function get($key = null, $xss = true){ |
|
| 153 | + if(empty($key)){ |
|
| 154 | + //return all |
|
| 155 | + return $xss ? clean_input($this->get) : $this->get; |
|
| 156 | + } |
|
| 157 | + $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 158 | + if($xss){ |
|
| 159 | + $get = clean_input($get); |
|
| 160 | + } |
|
| 161 | + return $get; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 166 | - * @param string $key the item key to be fetched |
|
| 167 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 168 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 169 | - */ |
|
| 170 | - public function post($key = null, $xss = true){ |
|
| 171 | - if(empty($key)){ |
|
| 172 | - //return all |
|
| 173 | - return $xss ? clean_input($this->post) : $this->post; |
|
| 174 | - } |
|
| 175 | - $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 176 | - if($xss){ |
|
| 177 | - $post = clean_input($post); |
|
| 178 | - } |
|
| 179 | - return $post; |
|
| 180 | - } |
|
| 164 | + /** |
|
| 165 | + * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 166 | + * @param string $key the item key to be fetched |
|
| 167 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 168 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 169 | + */ |
|
| 170 | + public function post($key = null, $xss = true){ |
|
| 171 | + if(empty($key)){ |
|
| 172 | + //return all |
|
| 173 | + return $xss ? clean_input($this->post) : $this->post; |
|
| 174 | + } |
|
| 175 | + $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 176 | + if($xss){ |
|
| 177 | + $post = clean_input($post); |
|
| 178 | + } |
|
| 179 | + return $post; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 184 | - * @param string $key the item key to be fetched |
|
| 185 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 186 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 187 | - */ |
|
| 188 | - public function server($key = null, $xss = true){ |
|
| 189 | - if(empty($key)){ |
|
| 190 | - //return all |
|
| 191 | - return $xss ? clean_input($this->server) : $this->server; |
|
| 192 | - } |
|
| 193 | - $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 194 | - if($xss){ |
|
| 195 | - $server = clean_input($server); |
|
| 196 | - } |
|
| 197 | - return $server; |
|
| 198 | - } |
|
| 182 | + /** |
|
| 183 | + * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 184 | + * @param string $key the item key to be fetched |
|
| 185 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 186 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 187 | + */ |
|
| 188 | + public function server($key = null, $xss = true){ |
|
| 189 | + if(empty($key)){ |
|
| 190 | + //return all |
|
| 191 | + return $xss ? clean_input($this->server) : $this->server; |
|
| 192 | + } |
|
| 193 | + $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 194 | + if($xss){ |
|
| 195 | + $server = clean_input($server); |
|
| 196 | + } |
|
| 197 | + return $server; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 202 | - * @param string $key the item key to be fetched |
|
| 203 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 204 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 205 | - */ |
|
| 206 | - public function cookie($key = null, $xss = true){ |
|
| 207 | - if(empty($key)){ |
|
| 208 | - //return all |
|
| 209 | - return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 210 | - } |
|
| 211 | - $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 212 | - if($xss){ |
|
| 213 | - $cookie = clean_input($cookie); |
|
| 214 | - } |
|
| 215 | - return $cookie; |
|
| 216 | - } |
|
| 200 | + /** |
|
| 201 | + * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 202 | + * @param string $key the item key to be fetched |
|
| 203 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 204 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 205 | + */ |
|
| 206 | + public function cookie($key = null, $xss = true){ |
|
| 207 | + if(empty($key)){ |
|
| 208 | + //return all |
|
| 209 | + return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 210 | + } |
|
| 211 | + $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 212 | + if($xss){ |
|
| 213 | + $cookie = clean_input($cookie); |
|
| 214 | + } |
|
| 215 | + return $cookie; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 220 | - * @param string $key the item key to be fetched |
|
| 221 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 222 | - */ |
|
| 223 | - public function file($key){ |
|
| 224 | - $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 225 | - return $file; |
|
| 226 | - } |
|
| 218 | + /** |
|
| 219 | + * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 220 | + * @param string $key the item key to be fetched |
|
| 221 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 222 | + */ |
|
| 223 | + public function file($key){ |
|
| 224 | + $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 225 | + return $file; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 230 | - * @param string $key the item key to be fetched |
|
| 231 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 232 | - * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 233 | - */ |
|
| 234 | - public function session($key, $xss = true){ |
|
| 235 | - $session = $this->session->get($key); |
|
| 236 | - if($xss){ |
|
| 237 | - $session = clean_input($session); |
|
| 238 | - } |
|
| 239 | - return $session; |
|
| 240 | - } |
|
| 228 | + /** |
|
| 229 | + * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 230 | + * @param string $key the item key to be fetched |
|
| 231 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 232 | + * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 233 | + */ |
|
| 234 | + public function session($key, $xss = true){ |
|
| 235 | + $session = $this->session->get($key); |
|
| 236 | + if($xss){ |
|
| 237 | + $session = clean_input($session); |
|
| 238 | + } |
|
| 239 | + return $session; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Get the value from header array for given key. |
|
| 244 | - * @param string $key the item key to be fetched |
|
| 245 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 246 | - * @return mixed the item value if the key exists or null if the key does not exists |
|
| 247 | - */ |
|
| 248 | - public function header($key, $xss = true){ |
|
| 249 | - $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 250 | - if($xss){ |
|
| 251 | - $header = clean_input($header); |
|
| 252 | - } |
|
| 253 | - return $header; |
|
| 254 | - } |
|
| 242 | + /** |
|
| 243 | + * Get the value from header array for given key. |
|
| 244 | + * @param string $key the item key to be fetched |
|
| 245 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 246 | + * @return mixed the item value if the key exists or null if the key does not exists |
|
| 247 | + */ |
|
| 248 | + public function header($key, $xss = true){ |
|
| 249 | + $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 250 | + if($xss){ |
|
| 251 | + $header = clean_input($header); |
|
| 252 | + } |
|
| 253 | + return $header; |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - } |
|
| 256 | + } |
|
@@ -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 | + } |
|
@@ -1,84 +1,84 @@ |
||
| 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 | - interface CacheInterface{ |
|
| 27 | + interface CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This is used to get the cache data using the key |
|
| 31 | - * @param string $key the key to identify the cache data |
|
| 32 | - * @return mixed the cache data if exists else return false |
|
| 33 | - */ |
|
| 34 | - public function get($key); |
|
| 29 | + /** |
|
| 30 | + * This is used to get the cache data using the key |
|
| 31 | + * @param string $key the key to identify the cache data |
|
| 32 | + * @return mixed the cache data if exists else return false |
|
| 33 | + */ |
|
| 34 | + public function get($key); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Save data to the cache |
|
| 39 | - * @param string $key the key to identify this cache data |
|
| 40 | - * @param mixed $data the cache data to be saved |
|
| 41 | - * @param integer $ttl the cache life time |
|
| 42 | - * @return boolean true if success otherwise will return false |
|
| 43 | - */ |
|
| 44 | - public function set($key, $data, $ttl = 0); |
|
| 37 | + /** |
|
| 38 | + * Save data to the cache |
|
| 39 | + * @param string $key the key to identify this cache data |
|
| 40 | + * @param mixed $data the cache data to be saved |
|
| 41 | + * @param integer $ttl the cache life time |
|
| 42 | + * @return boolean true if success otherwise will return false |
|
| 43 | + */ |
|
| 44 | + public function set($key, $data, $ttl = 0); |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Delete the cache data for given key |
|
| 49 | - * @param string $key the key for cache to be deleted |
|
| 50 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | - * the cache or the cache with the given key not exist |
|
| 52 | - */ |
|
| 53 | - public function delete($key); |
|
| 47 | + /** |
|
| 48 | + * Delete the cache data for given key |
|
| 49 | + * @param string $key the key for cache to be deleted |
|
| 50 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | + * the cache or the cache with the given key not exist |
|
| 52 | + */ |
|
| 53 | + public function delete($key); |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get the cache information for given key |
|
| 58 | - * @param string $key the key for cache to get the information for |
|
| 59 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | - * 'ttl' => the time to live of the cache in second |
|
| 63 | - */ |
|
| 64 | - public function getInfo($key); |
|
| 56 | + /** |
|
| 57 | + * Get the cache information for given key |
|
| 58 | + * @param string $key the key for cache to get the information for |
|
| 59 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | + * 'ttl' => the time to live of the cache in second |
|
| 63 | + */ |
|
| 64 | + public function getInfo($key); |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Used to delete expired cache data |
|
| 69 | - */ |
|
| 70 | - public function deleteExpiredCache(); |
|
| 67 | + /** |
|
| 68 | + * Used to delete expired cache data |
|
| 69 | + */ |
|
| 70 | + public function deleteExpiredCache(); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Remove all cache data |
|
| 74 | - */ |
|
| 75 | - public function clean(); |
|
| 72 | + /** |
|
| 73 | + * Remove all cache data |
|
| 74 | + */ |
|
| 75 | + public function clean(); |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Check whether the cache feature for the handle is supported |
|
| 80 | - * |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - public function isSupported(); |
|
| 84 | - } |
|
| 78 | + /** |
|
| 79 | + * Check whether the cache feature for the handle is supported |
|
| 80 | + * |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + public function isSupported(); |
|
| 84 | + } |
|
@@ -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 | + } |
|
@@ -1,182 +1,182 @@ |
||
| 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 | - * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | - * also to dispatch the event |
|
| 30 | - */ |
|
| 27 | + /** |
|
| 28 | + * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | + * also to dispatch the event |
|
| 30 | + */ |
|
| 31 | 31 | |
| 32 | - class EventDispatcher{ |
|
| 32 | + class EventDispatcher{ |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The list of the registered listeners |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $listeners = array(); |
|
| 34 | + /** |
|
| 35 | + * The list of the registered listeners |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $listeners = array(); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The logger instance |
|
| 43 | - * @var Log |
|
| 44 | - */ |
|
| 45 | - private $logger; |
|
| 41 | + /** |
|
| 42 | + * The logger instance |
|
| 43 | + * @var Log |
|
| 44 | + */ |
|
| 45 | + private $logger; |
|
| 46 | 46 | |
| 47 | - public function __construct(){ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | - } |
|
| 47 | + public function __construct(){ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Register new listener |
|
| 54 | - * @param string $eventName the name of the event to register for |
|
| 55 | - * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | - */ |
|
| 57 | - public function addListener($eventName, callable $listener){ |
|
| 58 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | - if(! isset($this->listeners[$eventName])){ |
|
| 60 | - $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | - $this->listeners[$eventName] = array(); |
|
| 62 | - } |
|
| 63 | - else{ |
|
| 64 | - $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | - } |
|
| 66 | - $this->listeners[$eventName][] = $listener; |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Register new listener |
|
| 54 | + * @param string $eventName the name of the event to register for |
|
| 55 | + * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | + */ |
|
| 57 | + public function addListener($eventName, callable $listener){ |
|
| 58 | + $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | + if(! isset($this->listeners[$eventName])){ |
|
| 60 | + $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | + $this->listeners[$eventName] = array(); |
|
| 62 | + } |
|
| 63 | + else{ |
|
| 64 | + $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | + } |
|
| 66 | + $this->listeners[$eventName][] = $listener; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Remove the event listener from list |
|
| 71 | - * @param string $eventName the event name |
|
| 72 | - * @param callable $listener the listener callback |
|
| 73 | - */ |
|
| 74 | - public function removeListener($eventName, callable $listener){ |
|
| 75 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | - if(isset($this->listeners[$eventName])){ |
|
| 77 | - $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | - unset($this->listeners[$eventName][$index]); |
|
| 81 | - } |
|
| 82 | - else{ |
|
| 83 | - $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - else{ |
|
| 87 | - $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * Remove the event listener from list |
|
| 71 | + * @param string $eventName the event name |
|
| 72 | + * @param callable $listener the listener callback |
|
| 73 | + */ |
|
| 74 | + public function removeListener($eventName, callable $listener){ |
|
| 75 | + $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | + if(isset($this->listeners[$eventName])){ |
|
| 77 | + $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | + if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | + $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | + unset($this->listeners[$eventName][$index]); |
|
| 81 | + } |
|
| 82 | + else{ |
|
| 83 | + $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + else{ |
|
| 87 | + $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | - * remove all listeners for this event |
|
| 94 | - * @param string $eventName the event name |
|
| 95 | - */ |
|
| 96 | - public function removeAllListener($eventName = null){ |
|
| 97 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | - $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | - unset($this->listeners[$eventName]); |
|
| 101 | - } |
|
| 102 | - else{ |
|
| 103 | - $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | - $this->listeners = array(); |
|
| 105 | - } |
|
| 106 | - } |
|
| 91 | + /** |
|
| 92 | + * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | + * remove all listeners for this event |
|
| 94 | + * @param string $eventName the event name |
|
| 95 | + */ |
|
| 96 | + public function removeAllListener($eventName = null){ |
|
| 97 | + $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | + if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | + $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | + unset($this->listeners[$eventName]); |
|
| 101 | + } |
|
| 102 | + else{ |
|
| 103 | + $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | + $this->listeners = array(); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the list of listener for this event |
|
| 110 | - * @param string $eventName the event name |
|
| 111 | - * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | - */ |
|
| 113 | - public function getListeners($eventName){ |
|
| 114 | - return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the list of listener for this event |
|
| 110 | + * @param string $eventName the event name |
|
| 111 | + * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | + */ |
|
| 113 | + public function getListeners($eventName){ |
|
| 114 | + return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Dispatch the event to the registered listeners. |
|
| 119 | - * @param mixed|object $event the event information |
|
| 120 | - * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | - */ |
|
| 122 | - public function dispatch($event){ |
|
| 123 | - if(! $event || !$event instanceof EventInfo){ |
|
| 124 | - $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | - $event = new EventInfo((string) $event); |
|
| 126 | - } |
|
| 127 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | - if(isset($event->stop) && $event->stop){ |
|
| 129 | - $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | - return; |
|
| 131 | - } |
|
| 132 | - if($event->returnBack){ |
|
| 133 | - $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | - return $this->dispatchToListerners($event); |
|
| 135 | - } |
|
| 136 | - else{ |
|
| 137 | - $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | - $this->dispatchToListerners($event); |
|
| 139 | - } |
|
| 140 | - } |
|
| 117 | + /** |
|
| 118 | + * Dispatch the event to the registered listeners. |
|
| 119 | + * @param mixed|object $event the event information |
|
| 120 | + * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | + */ |
|
| 122 | + public function dispatch($event){ |
|
| 123 | + if(! $event || !$event instanceof EventInfo){ |
|
| 124 | + $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | + $event = new EventInfo((string) $event); |
|
| 126 | + } |
|
| 127 | + $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | + if(isset($event->stop) && $event->stop){ |
|
| 129 | + $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | + return; |
|
| 131 | + } |
|
| 132 | + if($event->returnBack){ |
|
| 133 | + $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | + return $this->dispatchToListerners($event); |
|
| 135 | + } |
|
| 136 | + else{ |
|
| 137 | + $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | + $this->dispatchToListerners($event); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Dispatch the event to the registered listeners. |
|
| 144 | - * @param object EventInfo $event the event information |
|
| 145 | - * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | - */ |
|
| 147 | - private function dispatchToListerners(EventInfo $event){ |
|
| 148 | - $eBackup = $event; |
|
| 149 | - $list = $this->getListeners($event->name); |
|
| 150 | - if(empty($list)){ |
|
| 151 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | - if($event->returnBack){ |
|
| 153 | - return $event; |
|
| 154 | - } |
|
| 155 | - return; |
|
| 156 | - } |
|
| 157 | - else{ |
|
| 158 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | - } |
|
| 160 | - foreach($list as $listener){ |
|
| 161 | - if($eBackup->returnBack){ |
|
| 162 | - $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | - if($returnedEvent instanceof EventInfo){ |
|
| 164 | - $event = $returnedEvent; |
|
| 165 | - } |
|
| 166 | - else{ |
|
| 167 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - else{ |
|
| 171 | - call_user_func_array($listener, array($event)); |
|
| 172 | - } |
|
| 173 | - if($event->stop){ |
|
| 174 | - break; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | - if($eBackup->returnBack){ |
|
| 179 | - return $event; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 142 | + /** |
|
| 143 | + * Dispatch the event to the registered listeners. |
|
| 144 | + * @param object EventInfo $event the event information |
|
| 145 | + * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | + */ |
|
| 147 | + private function dispatchToListerners(EventInfo $event){ |
|
| 148 | + $eBackup = $event; |
|
| 149 | + $list = $this->getListeners($event->name); |
|
| 150 | + if(empty($list)){ |
|
| 151 | + $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | + if($event->returnBack){ |
|
| 153 | + return $event; |
|
| 154 | + } |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + else{ |
|
| 158 | + $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | + } |
|
| 160 | + foreach($list as $listener){ |
|
| 161 | + if($eBackup->returnBack){ |
|
| 162 | + $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | + if($returnedEvent instanceof EventInfo){ |
|
| 164 | + $event = $returnedEvent; |
|
| 165 | + } |
|
| 166 | + else{ |
|
| 167 | + show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + else{ |
|
| 171 | + call_user_func_array($listener, array($event)); |
|
| 172 | + } |
|
| 173 | + if($event->stop){ |
|
| 174 | + break; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | + if($eBackup->returnBack){ |
|
| 179 | + return $event; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
@@ -1,265 +1,265 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Log{ |
|
| 27 | + class Log{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The defined constante for Log level |
|
| 31 | - */ |
|
| 32 | - const NONE = 99999999; |
|
| 33 | - const FATAL = 500; |
|
| 34 | - const ERROR = 400; |
|
| 35 | - const WARNING = 300; |
|
| 36 | - const INFO = 200; |
|
| 37 | - const DEBUG = 100; |
|
| 38 | - const ALL = -99999999; |
|
| 29 | + /** |
|
| 30 | + * The defined constante for Log level |
|
| 31 | + */ |
|
| 32 | + const NONE = 99999999; |
|
| 33 | + const FATAL = 500; |
|
| 34 | + const ERROR = 400; |
|
| 35 | + const WARNING = 300; |
|
| 36 | + const INFO = 200; |
|
| 37 | + const DEBUG = 100; |
|
| 38 | + const ALL = -99999999; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The logger name |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $logger = 'ROOT_LOGGER'; |
|
| 40 | + /** |
|
| 41 | + * The logger name |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $logger = 'ROOT_LOGGER'; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * List of valid log level to be checked for the configuration |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 46 | + /** |
|
| 47 | + * List of valid log level to be checked for the configuration |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Create new Log instance |
|
| 54 | - */ |
|
| 55 | - public function __construct(){ |
|
| 56 | - } |
|
| 52 | + /** |
|
| 53 | + * Create new Log instance |
|
| 54 | + */ |
|
| 55 | + public function __construct(){ |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Set the logger to identify each message in the log |
|
| 60 | - * @param string $logger the logger name |
|
| 61 | - */ |
|
| 62 | - public function setLogger($logger){ |
|
| 63 | - $this->logger = $logger; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Set the logger to identify each message in the log |
|
| 60 | + * @param string $logger the logger name |
|
| 61 | + */ |
|
| 62 | + public function setLogger($logger){ |
|
| 63 | + $this->logger = $logger; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Save the fatal message in the log |
|
| 68 | - * @see Log::writeLog for more detail |
|
| 69 | - * @param string $message the log message to save |
|
| 70 | - */ |
|
| 71 | - public function fatal($message){ |
|
| 72 | - $this->writeLog($message, self::FATAL); |
|
| 73 | - } |
|
| 66 | + /** |
|
| 67 | + * Save the fatal message in the log |
|
| 68 | + * @see Log::writeLog for more detail |
|
| 69 | + * @param string $message the log message to save |
|
| 70 | + */ |
|
| 71 | + public function fatal($message){ |
|
| 72 | + $this->writeLog($message, self::FATAL); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Save the error message in the log |
|
| 77 | - * @see Log::writeLog for more detail |
|
| 78 | - * @param string $message the log message to save |
|
| 79 | - */ |
|
| 80 | - public function error($message){ |
|
| 81 | - $this->writeLog($message, self::ERROR); |
|
| 82 | - } |
|
| 75 | + /** |
|
| 76 | + * Save the error message in the log |
|
| 77 | + * @see Log::writeLog for more detail |
|
| 78 | + * @param string $message the log message to save |
|
| 79 | + */ |
|
| 80 | + public function error($message){ |
|
| 81 | + $this->writeLog($message, self::ERROR); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Save the warning message in the log |
|
| 86 | - * @see Log::writeLog for more detail |
|
| 87 | - * @param string $message the log message to save |
|
| 88 | - */ |
|
| 89 | - public function warning($message){ |
|
| 90 | - $this->writeLog($message, self::WARNING); |
|
| 91 | - } |
|
| 84 | + /** |
|
| 85 | + * Save the warning message in the log |
|
| 86 | + * @see Log::writeLog for more detail |
|
| 87 | + * @param string $message the log message to save |
|
| 88 | + */ |
|
| 89 | + public function warning($message){ |
|
| 90 | + $this->writeLog($message, self::WARNING); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Save the info message in the log |
|
| 95 | - * @see Log::writeLog for more detail |
|
| 96 | - * @param string $message the log message to save |
|
| 97 | - */ |
|
| 98 | - public function info($message){ |
|
| 99 | - $this->writeLog($message, self::INFO); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Save the info message in the log |
|
| 95 | + * @see Log::writeLog for more detail |
|
| 96 | + * @param string $message the log message to save |
|
| 97 | + */ |
|
| 98 | + public function info($message){ |
|
| 99 | + $this->writeLog($message, self::INFO); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Save the debug message in the log |
|
| 104 | - * @see Log::writeLog for more detail |
|
| 105 | - * @param string $message the log message to save |
|
| 106 | - */ |
|
| 107 | - public function debug($message){ |
|
| 108 | - $this->writeLog($message, self::DEBUG); |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Save the debug message in the log |
|
| 104 | + * @see Log::writeLog for more detail |
|
| 105 | + * @param string $message the log message to save |
|
| 106 | + */ |
|
| 107 | + public function debug($message){ |
|
| 108 | + $this->writeLog($message, self::DEBUG); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Save the log message |
|
| 114 | - * @param string $message the log message to be saved |
|
| 115 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | - * to allow check the log level threshold. |
|
| 117 | - */ |
|
| 118 | - public function writeLog($message, $level = self::INFO){ |
|
| 119 | - $configLogLevel = get_config('log_level'); |
|
| 120 | - if(! $configLogLevel){ |
|
| 121 | - //so means no need log just stop here |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - //check config log level |
|
| 125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | - show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | - } |
|
| 112 | + /** |
|
| 113 | + * Save the log message |
|
| 114 | + * @param string $message the log message to be saved |
|
| 115 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | + * to allow check the log level threshold. |
|
| 117 | + */ |
|
| 118 | + public function writeLog($message, $level = self::INFO){ |
|
| 119 | + $configLogLevel = get_config('log_level'); |
|
| 120 | + if(! $configLogLevel){ |
|
| 121 | + //so means no need log just stop here |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + //check config log level |
|
| 125 | + if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | + show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - //check if config log_logger_name is set |
|
| 131 | - if($this->logger){ |
|
| 132 | - $configLoggersName = get_config('log_logger_name', array()); |
|
| 133 | - if (!empty($configLoggersName)) { |
|
| 134 | - //for best comparaison put all string to lowercase |
|
| 135 | - $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 136 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 137 | - return; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - } |
|
| 130 | + //check if config log_logger_name is set |
|
| 131 | + if($this->logger){ |
|
| 132 | + $configLoggersName = get_config('log_logger_name', array()); |
|
| 133 | + if (!empty($configLoggersName)) { |
|
| 134 | + //for best comparaison put all string to lowercase |
|
| 135 | + $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 136 | + if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 137 | + return; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - //if $level is not an integer |
|
| 143 | - if(! is_numeric($level)){ |
|
| 144 | - $level = self::getLevelValue($level); |
|
| 145 | - } |
|
| 142 | + //if $level is not an integer |
|
| 143 | + if(! is_numeric($level)){ |
|
| 144 | + $level = self::getLevelValue($level); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - //check if can logging regarding the log level config |
|
| 148 | - $configLevel = self::getLevelValue($configLogLevel); |
|
| 149 | - if($configLevel > $level){ |
|
| 150 | - //can't log |
|
| 151 | - return; |
|
| 152 | - } |
|
| 147 | + //check if can logging regarding the log level config |
|
| 148 | + $configLevel = self::getLevelValue($configLogLevel); |
|
| 149 | + if($configLevel > $level){ |
|
| 150 | + //can't log |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $logSavePath = get_config('log_save_path'); |
|
| 155 | - if(! $logSavePath){ |
|
| 156 | - $logSavePath = LOGS_PATH; |
|
| 157 | - } |
|
| 154 | + $logSavePath = get_config('log_save_path'); |
|
| 155 | + if(! $logSavePath){ |
|
| 156 | + $logSavePath = LOGS_PATH; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 160 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 161 | - show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 162 | - } |
|
| 159 | + if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 160 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 161 | + show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 165 | - if(! file_exists($path)){ |
|
| 166 | - touch($path); |
|
| 167 | - } |
|
| 168 | - //may be at this time helper user_agent not yet included |
|
| 169 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 164 | + $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 165 | + if(! file_exists($path)){ |
|
| 166 | + touch($path); |
|
| 167 | + } |
|
| 168 | + //may be at this time helper user_agent not yet included |
|
| 169 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 170 | 170 | |
| 171 | - ///////////////////// date ////////////// |
|
| 172 | - $timestampWithMicro = microtime(true); |
|
| 173 | - $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 174 | - $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 175 | - $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 176 | - //ip |
|
| 177 | - $ip = get_ip(); |
|
| 178 | - //level name |
|
| 179 | - $levelName = self::getLevelName($level); |
|
| 171 | + ///////////////////// date ////////////// |
|
| 172 | + $timestampWithMicro = microtime(true); |
|
| 173 | + $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 174 | + $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 175 | + $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 176 | + //ip |
|
| 177 | + $ip = get_ip(); |
|
| 178 | + //level name |
|
| 179 | + $levelName = self::getLevelName($level); |
|
| 180 | 180 | |
| 181 | - //debug info |
|
| 182 | - $dtrace = debug_backtrace(); |
|
| 183 | - $fileInfo = $dtrace[0]['file'] == __FILE__ ? $dtrace[1] : $dtrace[0]; |
|
| 181 | + //debug info |
|
| 182 | + $dtrace = debug_backtrace(); |
|
| 183 | + $fileInfo = $dtrace[0]['file'] == __FILE__ ? $dtrace[1] : $dtrace[0]; |
|
| 184 | 184 | |
| 185 | - $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 186 | - $fp = fopen($path, 'a+'); |
|
| 187 | - if(is_resource($fp)){ |
|
| 188 | - flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 189 | - fwrite($fp, $str); |
|
| 190 | - fclose($fp); |
|
| 191 | - } |
|
| 192 | - } |
|
| 185 | + $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 186 | + $fp = fopen($path, 'a+'); |
|
| 187 | + if(is_resource($fp)){ |
|
| 188 | + flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 189 | + fwrite($fp, $str); |
|
| 190 | + fclose($fp); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Check if the given log level is valid |
|
| 196 | - * |
|
| 197 | - * @param string $level the log level |
|
| 198 | - * |
|
| 199 | - * @return boolean true if the given log level is valid, false if not |
|
| 200 | - */ |
|
| 201 | - private static function isValidConfigLevel($level){ |
|
| 202 | - $level = strtolower($level); |
|
| 203 | - return in_array($level, self::$validConfigLevel); |
|
| 204 | - } |
|
| 194 | + /** |
|
| 195 | + * Check if the given log level is valid |
|
| 196 | + * |
|
| 197 | + * @param string $level the log level |
|
| 198 | + * |
|
| 199 | + * @return boolean true if the given log level is valid, false if not |
|
| 200 | + */ |
|
| 201 | + private static function isValidConfigLevel($level){ |
|
| 202 | + $level = strtolower($level); |
|
| 203 | + return in_array($level, self::$validConfigLevel); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Get the log level number for the given level string |
|
| 208 | - * @param string $level the log level in string format |
|
| 209 | - * @return int the log level in integer format using the predefinied constants |
|
| 210 | - */ |
|
| 211 | - private static function getLevelValue($level){ |
|
| 212 | - $level = strtolower($level); |
|
| 213 | - $value = self::NONE; |
|
| 206 | + /** |
|
| 207 | + * Get the log level number for the given level string |
|
| 208 | + * @param string $level the log level in string format |
|
| 209 | + * @return int the log level in integer format using the predefinied constants |
|
| 210 | + */ |
|
| 211 | + private static function getLevelValue($level){ |
|
| 212 | + $level = strtolower($level); |
|
| 213 | + $value = self::NONE; |
|
| 214 | 214 | |
| 215 | - //the default value is NONE, so means no need test for NONE |
|
| 216 | - if($level == 'fatal'){ |
|
| 217 | - $value = self::FATAL; |
|
| 218 | - } |
|
| 219 | - else if($level == 'error'){ |
|
| 220 | - $value = self::ERROR; |
|
| 221 | - } |
|
| 222 | - else if($level == 'warning' || $level == 'warn'){ |
|
| 223 | - $value = self::WARNING; |
|
| 224 | - } |
|
| 225 | - else if($level == 'info'){ |
|
| 226 | - $value = self::INFO; |
|
| 227 | - } |
|
| 228 | - else if($level == 'debug'){ |
|
| 229 | - $value = self::DEBUG; |
|
| 230 | - } |
|
| 231 | - else if($level == 'all'){ |
|
| 232 | - $value = self::ALL; |
|
| 233 | - } |
|
| 234 | - return $value; |
|
| 235 | - } |
|
| 215 | + //the default value is NONE, so means no need test for NONE |
|
| 216 | + if($level == 'fatal'){ |
|
| 217 | + $value = self::FATAL; |
|
| 218 | + } |
|
| 219 | + else if($level == 'error'){ |
|
| 220 | + $value = self::ERROR; |
|
| 221 | + } |
|
| 222 | + else if($level == 'warning' || $level == 'warn'){ |
|
| 223 | + $value = self::WARNING; |
|
| 224 | + } |
|
| 225 | + else if($level == 'info'){ |
|
| 226 | + $value = self::INFO; |
|
| 227 | + } |
|
| 228 | + else if($level == 'debug'){ |
|
| 229 | + $value = self::DEBUG; |
|
| 230 | + } |
|
| 231 | + else if($level == 'all'){ |
|
| 232 | + $value = self::ALL; |
|
| 233 | + } |
|
| 234 | + return $value; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Get the log level string for the given log level integer |
|
| 239 | - * @param integer $level the log level in integer format |
|
| 240 | - * @return string the log level in string format |
|
| 241 | - */ |
|
| 242 | - private static function getLevelName($level){ |
|
| 243 | - $value = ''; |
|
| 237 | + /** |
|
| 238 | + * Get the log level string for the given log level integer |
|
| 239 | + * @param integer $level the log level in integer format |
|
| 240 | + * @return string the log level in string format |
|
| 241 | + */ |
|
| 242 | + private static function getLevelName($level){ |
|
| 243 | + $value = ''; |
|
| 244 | 244 | |
| 245 | - //the default value is NONE, so means no need test for NONE |
|
| 246 | - if($level == self::FATAL){ |
|
| 247 | - $value = 'FATAL'; |
|
| 248 | - } |
|
| 249 | - else if($level == self::ERROR){ |
|
| 250 | - $value = 'ERROR'; |
|
| 251 | - } |
|
| 252 | - else if($level == self::WARNING){ |
|
| 253 | - $value = 'WARNING'; |
|
| 254 | - } |
|
| 255 | - else if($level == self::INFO){ |
|
| 256 | - $value = 'INFO'; |
|
| 257 | - } |
|
| 258 | - else if($level == self::DEBUG){ |
|
| 259 | - $value = 'DEBUG'; |
|
| 260 | - } |
|
| 261 | - //no need for ALL |
|
| 262 | - return $value; |
|
| 263 | - } |
|
| 245 | + //the default value is NONE, so means no need test for NONE |
|
| 246 | + if($level == self::FATAL){ |
|
| 247 | + $value = 'FATAL'; |
|
| 248 | + } |
|
| 249 | + else if($level == self::ERROR){ |
|
| 250 | + $value = 'ERROR'; |
|
| 251 | + } |
|
| 252 | + else if($level == self::WARNING){ |
|
| 253 | + $value = 'WARNING'; |
|
| 254 | + } |
|
| 255 | + else if($level == self::INFO){ |
|
| 256 | + $value = 'INFO'; |
|
| 257 | + } |
|
| 258 | + else if($level == self::DEBUG){ |
|
| 259 | + $value = 'DEBUG'; |
|
| 260 | + } |
|
| 261 | + //no need for ALL |
|
| 262 | + return $value; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - } |
|
| 265 | + } |
|
@@ -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 | + } |
|