@@ -1,113 +1,113 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Database extends BaseClass{ |
|
| 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 Database extends BaseClass{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The PDO instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 28 | + /** |
|
| 29 | + * The PDO instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $pdo = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The database name used for the application |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $databaseName = null; |
|
| 34 | + /** |
|
| 35 | + * The database name used for the application |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The number of rows returned by the last query |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The number of rows returned by the last query |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | 44 | private $numRows = 0; |
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | - * @var mixed |
|
| 49 | - */ |
|
| 46 | + /** |
|
| 47 | + * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | + * @var mixed |
|
| 49 | + */ |
|
| 50 | 50 | private $insertId = null; |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The full SQL query statment after build for each command |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 52 | + /** |
|
| 53 | + * The full SQL query statment after build for each command |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | 56 | private $query = null; |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * The result returned for the last query |
|
| 60 | - * @var mixed |
|
| 61 | - */ |
|
| 58 | + /** |
|
| 59 | + * The result returned for the last query |
|
| 60 | + * @var mixed |
|
| 61 | + */ |
|
| 62 | 62 | private $result = array(); |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $cacheTtl = 0; |
|
| 64 | + /** |
|
| 65 | + * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | - * @var int |
|
| 73 | - */ |
|
| 70 | + /** |
|
| 71 | + * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | + * @var int |
|
| 73 | + */ |
|
| 74 | 74 | private $temporaryCacheTtl = 0; |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * The number of executed query for the current request |
|
| 78 | - * @var int |
|
| 79 | - */ |
|
| 76 | + /** |
|
| 77 | + * The number of executed query for the current request |
|
| 78 | + * @var int |
|
| 79 | + */ |
|
| 80 | 80 | private $queryCount = 0; |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | - * @var array |
|
| 85 | - */ |
|
| 82 | + /** |
|
| 83 | + * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | + * @var array |
|
| 85 | + */ |
|
| 86 | 86 | private $data = array(); |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The database configuration |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The database configuration |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | 92 | private $config = array(); |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | - * The cache instance |
|
| 96 | - * @var object |
|
| 97 | - */ |
|
| 95 | + * The cache instance |
|
| 96 | + * @var object |
|
| 97 | + */ |
|
| 98 | 98 | private $cacheInstance = null; |
| 99 | 99 | |
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * The DatabaseQueryBuilder instance |
|
| 103 | - * @var object |
|
| 104 | - */ |
|
| 101 | + /** |
|
| 102 | + * The DatabaseQueryBuilder instance |
|
| 103 | + * @var object |
|
| 104 | + */ |
|
| 105 | 105 | protected $queryBuilder = null; |
| 106 | 106 | |
| 107 | 107 | /** |
| 108 | - * The DatabaseQueryRunner instance |
|
| 109 | - * @var object |
|
| 110 | - */ |
|
| 108 | + * The DatabaseQueryRunner instance |
|
| 109 | + * @var object |
|
| 110 | + */ |
|
| 111 | 111 | protected $queryRunner = null; |
| 112 | 112 | |
| 113 | 113 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | public function __construct($overwriteConfig = array(), $autoConnect = true){ |
| 120 | 120 | parent::__construct(); |
| 121 | 121 | |
| 122 | - //Set DatabaseQueryBuilder instance to use |
|
| 122 | + //Set DatabaseQueryBuilder instance to use |
|
| 123 | 123 | $this->setDependencyInstanceFromParamOrCreate('queryBuilder', null, 'DatabaseQueryBuilder', 'classes/database'); |
| 124 | 124 | |
| 125 | 125 | //Set DatabaseQueryRunner instance to use |
@@ -137,8 +137,8 @@ discard block |
||
| 137 | 137 | * @return bool |
| 138 | 138 | */ |
| 139 | 139 | public function connect(){ |
| 140 | - $config = $this->getDatabaseConfiguration(); |
|
| 141 | - if (! empty($config)){ |
|
| 140 | + $config = $this->getDatabaseConfiguration(); |
|
| 141 | + if (! empty($config)){ |
|
| 142 | 142 | try{ |
| 143 | 143 | $this->pdo = new PDO($this->getDsnValueFromConfig(), $config['username'], $config['password']); |
| 144 | 144 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
@@ -149,14 +149,14 @@ discard block |
||
| 149 | 149 | $this->updateQueryBuilderAndRunnerProperties(); |
| 150 | 150 | |
| 151 | 151 | return is_object($this->pdo); |
| 152 | - } |
|
| 153 | - catch (PDOException $e){ |
|
| 152 | + } |
|
| 153 | + catch (PDOException $e){ |
|
| 154 | 154 | $this->logger->fatal($e->getMessage()); |
| 155 | 155 | show_error('Cannot connect to Database.'); |
| 156 | 156 | return false; |
| 157 | - } |
|
| 158 | - } |
|
| 159 | - return false; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + return false; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | * @return int |
| 166 | 166 | */ |
| 167 | 167 | public function numRows(){ |
| 168 | - return $this->numRows; |
|
| 168 | + return $this->numRows; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | * @return mixed |
| 174 | 174 | */ |
| 175 | 175 | public function insertId(){ |
| 176 | - return $this->insertId; |
|
| 176 | + return $this->insertId; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | |
@@ -184,13 +184,13 @@ discard block |
||
| 184 | 184 | * @return mixed the query SQL string or the record result |
| 185 | 185 | */ |
| 186 | 186 | public function get($returnSQLQueryOrResultType = false){ |
| 187 | - $this->queryBuilder->limit(1); |
|
| 188 | - $query = $this->getAll(true); |
|
| 189 | - if ($returnSQLQueryOrResultType === true){ |
|
| 187 | + $this->queryBuilder->limit(1); |
|
| 188 | + $query = $this->getAll(true); |
|
| 189 | + if ($returnSQLQueryOrResultType === true){ |
|
| 190 | 190 | return $query; |
| 191 | - } else { |
|
| 191 | + } else { |
|
| 192 | 192 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 193 | - } |
|
| 193 | + } |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
@@ -200,11 +200,11 @@ discard block |
||
| 200 | 200 | * @return mixed the query SQL string or the record result |
| 201 | 201 | */ |
| 202 | 202 | public function getAll($returnSQLQueryOrResultType = false){ |
| 203 | - $query = $this->queryBuilder->getQuery(); |
|
| 204 | - if ($returnSQLQueryOrResultType === true){ |
|
| 205 | - return $query; |
|
| 206 | - } |
|
| 207 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 203 | + $query = $this->queryBuilder->getQuery(); |
|
| 204 | + if ($returnSQLQueryOrResultType === true){ |
|
| 205 | + return $query; |
|
| 206 | + } |
|
| 207 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -214,19 +214,19 @@ discard block |
||
| 214 | 214 | * @return mixed the insert id of the new record or null |
| 215 | 215 | */ |
| 216 | 216 | public function insert($data = array(), $escape = true){ |
| 217 | - if (empty($data) && ! empty($this->data)){ |
|
| 217 | + if (empty($data) && ! empty($this->data)){ |
|
| 218 | 218 | //as when using $this->setData() may be the data already escaped |
| 219 | 219 | $escape = false; |
| 220 | 220 | $data = $this->data; |
| 221 | - } |
|
| 222 | - $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
|
| 223 | - $result = $this->query($query); |
|
| 224 | - if ($result){ |
|
| 221 | + } |
|
| 222 | + $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
|
| 223 | + $result = $this->query($query); |
|
| 224 | + if ($result){ |
|
| 225 | 225 | $this->insertId = $this->pdo->lastInsertId(); |
| 226 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 226 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 227 | 227 | return ! ($this->insertId) ? true : $this->insertId; |
| 228 | - } |
|
| 229 | - return false; |
|
| 228 | + } |
|
| 229 | + return false; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -236,13 +236,13 @@ discard block |
||
| 236 | 236 | * @return mixed the update status |
| 237 | 237 | */ |
| 238 | 238 | public function update($data = array(), $escape = true){ |
| 239 | - if (empty($data) && ! empty($this->data)){ |
|
| 239 | + if (empty($data) && ! empty($this->data)){ |
|
| 240 | 240 | //as when using $this->setData() may be the data already escaped |
| 241 | 241 | $escape = false; |
| 242 | 242 | $data = $this->data; |
| 243 | - } |
|
| 244 | - $query = $this->queryBuilder->update($data, $escape)->getQuery(); |
|
| 245 | - return $this->query($query); |
|
| 243 | + } |
|
| 244 | + $query = $this->queryBuilder->update($data, $escape)->getQuery(); |
|
| 245 | + return $this->query($query); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
@@ -250,8 +250,8 @@ discard block |
||
| 250 | 250 | * @return mixed the delete status |
| 251 | 251 | */ |
| 252 | 252 | public function delete(){ |
| 253 | - $query = $this->queryBuilder->delete()->getQuery(); |
|
| 254 | - return $this->query($query); |
|
| 253 | + $query = $this->queryBuilder->delete()->getQuery(); |
|
| 254 | + return $this->query($query); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** |
@@ -260,17 +260,17 @@ discard block |
||
| 260 | 260 | * @return object the current Database instance |
| 261 | 261 | */ |
| 262 | 262 | public function setCache($ttl = 0){ |
| 263 | - $this->cacheTtl = $ttl; |
|
| 264 | - $this->temporaryCacheTtl = $ttl; |
|
| 265 | - return $this; |
|
| 263 | + $this->cacheTtl = $ttl; |
|
| 264 | + $this->temporaryCacheTtl = $ttl; |
|
| 265 | + return $this; |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Enabled cache temporary for the current query not globally |
|
| 270 | - * @param integer $ttl the cache time to live in second |
|
| 271 | - * @return object the current Database instance |
|
| 272 | - */ |
|
| 273 | - public function cached($ttl = 0){ |
|
| 268 | + /** |
|
| 269 | + * Enabled cache temporary for the current query not globally |
|
| 270 | + * @param integer $ttl the cache time to live in second |
|
| 271 | + * @return object the current Database instance |
|
| 272 | + */ |
|
| 273 | + public function cached($ttl = 0){ |
|
| 274 | 274 | $this->temporaryCacheTtl = $ttl; |
| 275 | 275 | return $this; |
| 276 | 276 | } |
@@ -283,11 +283,11 @@ discard block |
||
| 283 | 283 | * need escaped |
| 284 | 284 | */ |
| 285 | 285 | public function escape($data, $escaped = true){ |
| 286 | - $data = trim($data); |
|
| 287 | - if($escaped){ |
|
| 286 | + $data = trim($data); |
|
| 287 | + if($escaped){ |
|
| 288 | 288 | return $this->pdo->quote($data); |
| 289 | - } |
|
| 290 | - return $data; |
|
| 289 | + } |
|
| 290 | + return $data; |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | /** |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | * @return int |
| 296 | 296 | */ |
| 297 | 297 | public function queryCount(){ |
| 298 | - return $this->queryCount; |
|
| 298 | + return $this->queryCount; |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | /** |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | * @return string |
| 304 | 304 | */ |
| 305 | 305 | public function getQuery(){ |
| 306 | - return $this->query; |
|
| 306 | + return $this->query; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /** |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | * @return string |
| 312 | 312 | */ |
| 313 | 313 | public function getDatabaseName(){ |
| 314 | - return $this->databaseName; |
|
| 314 | + return $this->databaseName; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | /** |
@@ -319,44 +319,44 @@ discard block |
||
| 319 | 319 | * @return object |
| 320 | 320 | */ |
| 321 | 321 | public function getPdo(){ |
| 322 | - return $this->pdo; |
|
| 322 | + return $this->pdo; |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | /** |
| 326 | 326 | * Set the PDO instance |
| 327 | 327 | * @param object $pdo the pdo object |
| 328 | - * @return object Database |
|
| 328 | + * @return object Database |
|
| 329 | 329 | */ |
| 330 | 330 | public function setPdo(PDO $pdo){ |
| 331 | - $this->pdo = $pdo; |
|
| 332 | - return $this; |
|
| 331 | + $this->pdo = $pdo; |
|
| 332 | + return $this; |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Return the cache instance |
|
| 337 | - * @return CacheInterface |
|
| 338 | - */ |
|
| 335 | + /** |
|
| 336 | + * Return the cache instance |
|
| 337 | + * @return CacheInterface |
|
| 338 | + */ |
|
| 339 | 339 | public function getCacheInstance(){ |
| 340 | - return $this->cacheInstance; |
|
| 340 | + return $this->cacheInstance; |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | /** |
| 344 | 344 | * Set the cache instance |
| 345 | 345 | * @param CacheInterface $cache the cache object |
| 346 | - * @return object Database |
|
| 346 | + * @return object Database |
|
| 347 | 347 | */ |
| 348 | 348 | public function setCacheInstance($cache){ |
| 349 | - $this->cacheInstance = $cache; |
|
| 350 | - return $this; |
|
| 349 | + $this->cacheInstance = $cache; |
|
| 350 | + return $this; |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | |
| 354 | - /** |
|
| 355 | - * Return the DatabaseQueryBuilder instance |
|
| 356 | - * @return object DatabaseQueryBuilder |
|
| 357 | - */ |
|
| 354 | + /** |
|
| 355 | + * Return the DatabaseQueryBuilder instance |
|
| 356 | + * @return object DatabaseQueryBuilder |
|
| 357 | + */ |
|
| 358 | 358 | public function getQueryBuilder(){ |
| 359 | - return $this->queryBuilder; |
|
| 359 | + return $this->queryBuilder; |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | /** |
@@ -364,8 +364,8 @@ discard block |
||
| 364 | 364 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 365 | 365 | */ |
| 366 | 366 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
| 367 | - $this->queryBuilder = $queryBuilder; |
|
| 368 | - return $this; |
|
| 367 | + $this->queryBuilder = $queryBuilder; |
|
| 368 | + return $this; |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | /** |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | * @return object DatabaseQueryRunner |
| 374 | 374 | */ |
| 375 | 375 | public function getQueryRunner(){ |
| 376 | - return $this->queryRunner; |
|
| 376 | + return $this->queryRunner; |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | /** |
@@ -381,8 +381,8 @@ discard block |
||
| 381 | 381 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 382 | 382 | */ |
| 383 | 383 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
| 384 | - $this->queryRunner = $queryRunner; |
|
| 385 | - return $this; |
|
| 384 | + $this->queryRunner = $queryRunner; |
|
| 385 | + return $this; |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | /** |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | * @return array |
| 391 | 391 | */ |
| 392 | 392 | public function getData(){ |
| 393 | - return $this->data; |
|
| 393 | + return $this->data; |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | /** |
@@ -401,62 +401,62 @@ discard block |
||
| 401 | 401 | * @return object the current Database instance |
| 402 | 402 | */ |
| 403 | 403 | public function setData($key, $value = null, $escape = true){ |
| 404 | - if (is_array($key)){ |
|
| 405 | - foreach($key as $k => $v){ |
|
| 406 | - $this->setData($k, $v, $escape); |
|
| 407 | - } |
|
| 408 | - } else { |
|
| 404 | + if (is_array($key)){ |
|
| 405 | + foreach($key as $k => $v){ |
|
| 406 | + $this->setData($k, $v, $escape); |
|
| 407 | + } |
|
| 408 | + } else { |
|
| 409 | 409 | $this->data[$key] = $this->escape($value, $escape); |
| 410 | - } |
|
| 411 | - return $this; |
|
| 410 | + } |
|
| 411 | + return $this; |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Execute an SQL query |
|
| 416 | - * @param string $query the query SQL string |
|
| 417 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 418 | - * @param boolean $returnAsArray return the result as array or not |
|
| 419 | - * @return mixed the query result |
|
| 420 | - */ |
|
| 414 | + /** |
|
| 415 | + * Execute an SQL query |
|
| 416 | + * @param string $query the query SQL string |
|
| 417 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 418 | + * @param boolean $returnAsArray return the result as array or not |
|
| 419 | + * @return mixed the query result |
|
| 420 | + */ |
|
| 421 | 421 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
| 422 | - $this->reset(); |
|
| 423 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 424 | - //If is the SELECT query |
|
| 425 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 422 | + $this->reset(); |
|
| 423 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 424 | + //If is the SELECT query |
|
| 425 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 426 | 426 | |
| 427 | - //cache expire time |
|
| 428 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 427 | + //cache expire time |
|
| 428 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 429 | 429 | |
| 430 | - //return to the initial cache time |
|
| 431 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 430 | + //return to the initial cache time |
|
| 431 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 432 | 432 | |
| 433 | - //config for cache |
|
| 434 | - $cacheEnable = get_config('cache_enable'); |
|
| 433 | + //config for cache |
|
| 434 | + $cacheEnable = get_config('cache_enable'); |
|
| 435 | 435 | |
| 436 | - //the database cache content |
|
| 437 | - $cacheContent = null; |
|
| 436 | + //the database cache content |
|
| 437 | + $cacheContent = null; |
|
| 438 | 438 | |
| 439 | - //if can use cache feature for this query |
|
| 440 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 439 | + //if can use cache feature for this query |
|
| 440 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 441 | 441 | |
| 442 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 443 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 444 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 445 | - } |
|
| 442 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 443 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 444 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - if (!$cacheContent){ |
|
| 448 | - //count the number of query execution to server |
|
| 447 | + if (!$cacheContent){ |
|
| 448 | + //count the number of query execution to server |
|
| 449 | 449 | $this->queryCount++; |
| 450 | 450 | |
| 451 | 451 | $queryResult = $this->queryRunner->setQuery($query) |
| 452 | - ->setReturnType($returnAsList) |
|
| 453 | - ->setReturnAsArray($returnAsArray) |
|
| 454 | - ->execute(); |
|
| 452 | + ->setReturnType($returnAsList) |
|
| 453 | + ->setReturnAsArray($returnAsArray) |
|
| 454 | + ->execute(); |
|
| 455 | 455 | |
| 456 | 456 | if (!is_object($queryResult)){ |
| 457 | - $this->result = false; |
|
| 458 | - $this->numRows = 0; |
|
| 459 | - return $this->result; |
|
| 457 | + $this->result = false; |
|
| 458 | + $this->numRows = 0; |
|
| 459 | + return $this->result; |
|
| 460 | 460 | } |
| 461 | 461 | $this->result = $queryResult->getResult(); |
| 462 | 462 | $this->numRows = $queryResult->getNumRows(); |
@@ -464,72 +464,72 @@ discard block |
||
| 464 | 464 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 465 | 465 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 466 | 466 | } |
| 467 | - } else if ($isSqlSELECTQuery){ |
|
| 468 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 469 | - $this->result = $cacheContent; |
|
| 470 | - $this->numRows = count($this->result); |
|
| 471 | - } |
|
| 472 | - return $this->result; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
| 477 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 478 | - * @param boolean $useConfigFile whether to use database configuration file |
|
| 479 | - * @param boolean $autoConnect whether to connect to database after set the configuration |
|
| 480 | - * @return object Database |
|
| 481 | - */ |
|
| 467 | + } else if ($isSqlSELECTQuery){ |
|
| 468 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 469 | + $this->result = $cacheContent; |
|
| 470 | + $this->numRows = count($this->result); |
|
| 471 | + } |
|
| 472 | + return $this->result; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
| 477 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 478 | + * @param boolean $useConfigFile whether to use database configuration file |
|
| 479 | + * @param boolean $autoConnect whether to connect to database after set the configuration |
|
| 480 | + * @return object Database |
|
| 481 | + */ |
|
| 482 | 482 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false){ |
| 483 | - $db = array(); |
|
| 484 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 485 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 486 | - require CONFIG_PATH . 'database.php'; |
|
| 487 | - } |
|
| 483 | + $db = array(); |
|
| 484 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 485 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 486 | + require CONFIG_PATH . 'database.php'; |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - //merge with the parameter |
|
| 490 | - $db = array_merge($db, $overwriteConfig); |
|
| 489 | + //merge with the parameter |
|
| 490 | + $db = array_merge($db, $overwriteConfig); |
|
| 491 | 491 | |
| 492 | - //get the default configuration |
|
| 493 | - $config = $this->getDatabaseDefaultConfiguration(); |
|
| 492 | + //get the default configuration |
|
| 493 | + $config = $this->getDatabaseDefaultConfiguration(); |
|
| 494 | 494 | |
| 495 | - $config = array_merge($config, $db); |
|
| 496 | - //determine the port using the hostname like localhost:3307 |
|
| 497 | - //hostname will be "localhost", and port "3307" |
|
| 498 | - $p = explode(':', $config['hostname']); |
|
| 499 | - if (count($p) >= 2){ |
|
| 500 | - $config['hostname'] = $p[0]; |
|
| 501 | - $config['port'] = $p[1]; |
|
| 502 | - } |
|
| 495 | + $config = array_merge($config, $db); |
|
| 496 | + //determine the port using the hostname like localhost:3307 |
|
| 497 | + //hostname will be "localhost", and port "3307" |
|
| 498 | + $p = explode(':', $config['hostname']); |
|
| 499 | + if (count($p) >= 2){ |
|
| 500 | + $config['hostname'] = $p[0]; |
|
| 501 | + $config['port'] = $p[1]; |
|
| 502 | + } |
|
| 503 | 503 | |
| 504 | - $this->databaseName = $config['database']; |
|
| 505 | - $this->config = $config; |
|
| 506 | - $this->logger->info( |
|
| 507 | - 'The database configuration are listed below: ' |
|
| 508 | - . stringfy_vars(array_merge( |
|
| 509 | - $this->config, |
|
| 510 | - array('password' => string_hidden($this->config['password'])) |
|
| 511 | - )) |
|
| 512 | - ); |
|
| 513 | - if($autoConnect){ |
|
| 514 | - //Now connect to the database |
|
| 515 | - $this->connect(); |
|
| 516 | - } |
|
| 517 | - return $this; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Return the database configuration |
|
| 522 | - * @return array |
|
| 523 | - */ |
|
| 504 | + $this->databaseName = $config['database']; |
|
| 505 | + $this->config = $config; |
|
| 506 | + $this->logger->info( |
|
| 507 | + 'The database configuration are listed below: ' |
|
| 508 | + . stringfy_vars(array_merge( |
|
| 509 | + $this->config, |
|
| 510 | + array('password' => string_hidden($this->config['password'])) |
|
| 511 | + )) |
|
| 512 | + ); |
|
| 513 | + if($autoConnect){ |
|
| 514 | + //Now connect to the database |
|
| 515 | + $this->connect(); |
|
| 516 | + } |
|
| 517 | + return $this; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Return the database configuration |
|
| 522 | + * @return array |
|
| 523 | + */ |
|
| 524 | 524 | public function getDatabaseConfiguration(){ |
| 525 | - return $this->config; |
|
| 525 | + return $this->config; |
|
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | /** |
| 529 | 529 | * Close the connexion |
| 530 | 530 | */ |
| 531 | 531 | public function close(){ |
| 532 | - $this->pdo = null; |
|
| 532 | + $this->pdo = null; |
|
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | /** |
@@ -537,16 +537,16 @@ discard block |
||
| 537 | 537 | * @return array |
| 538 | 538 | */ |
| 539 | 539 | protected function getDatabaseDefaultConfiguration(){ |
| 540 | - return array( |
|
| 541 | - 'driver' => '', |
|
| 542 | - 'username' => '', |
|
| 543 | - 'password' => '', |
|
| 544 | - 'database' => '', |
|
| 545 | - 'hostname' => 'localhost', |
|
| 546 | - 'charset' => 'utf8', |
|
| 547 | - 'collation' => 'utf8_general_ci', |
|
| 548 | - 'prefix' => '', |
|
| 549 | - 'port' => '' |
|
| 540 | + return array( |
|
| 541 | + 'driver' => '', |
|
| 542 | + 'username' => '', |
|
| 543 | + 'password' => '', |
|
| 544 | + 'database' => '', |
|
| 545 | + 'hostname' => 'localhost', |
|
| 546 | + 'charset' => 'utf8', |
|
| 547 | + 'collation' => 'utf8_general_ci', |
|
| 548 | + 'prefix' => '', |
|
| 549 | + 'port' => '' |
|
| 550 | 550 | ); |
| 551 | 551 | } |
| 552 | 552 | |
@@ -555,18 +555,18 @@ discard block |
||
| 555 | 555 | * @return void |
| 556 | 556 | */ |
| 557 | 557 | protected function updateQueryBuilderAndRunnerProperties(){ |
| 558 | - //update queryBuilder with some properties needed |
|
| 559 | - if (is_object($this->queryBuilder)){ |
|
| 558 | + //update queryBuilder with some properties needed |
|
| 559 | + if (is_object($this->queryBuilder)){ |
|
| 560 | 560 | $this->queryBuilder->setDriver($this->config['driver']) |
| 561 | - ->setPrefix($this->config['prefix']) |
|
| 562 | - ->setPdo($this->pdo); |
|
| 563 | - } |
|
| 561 | + ->setPrefix($this->config['prefix']) |
|
| 562 | + ->setPdo($this->pdo); |
|
| 563 | + } |
|
| 564 | 564 | |
| 565 | - //update queryRunner with some properties needed |
|
| 566 | - if (is_object($this->queryRunner)){ |
|
| 565 | + //update queryRunner with some properties needed |
|
| 566 | + if (is_object($this->queryRunner)){ |
|
| 567 | 567 | $this->queryRunner->setDriver($this->config['driver']) |
| 568 | - ->setPdo($this->pdo); |
|
| 569 | - } |
|
| 568 | + ->setPdo($this->pdo); |
|
| 569 | + } |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | |
@@ -575,21 +575,21 @@ discard block |
||
| 575 | 575 | * @return string|null the DSN string or null if can not find it |
| 576 | 576 | */ |
| 577 | 577 | protected function getDsnValueFromConfig(){ |
| 578 | - $dsn = null; |
|
| 579 | - $config = $this->getDatabaseConfiguration(); |
|
| 580 | - if (! empty($config)){ |
|
| 578 | + $dsn = null; |
|
| 579 | + $config = $this->getDatabaseConfiguration(); |
|
| 580 | + if (! empty($config)){ |
|
| 581 | 581 | $driver = $config['driver']; |
| 582 | 582 | $driverDsnMap = array( |
| 583 | - 'mysql' => $this->getDsnValueForDriver('mysql'), |
|
| 584 | - 'pgsql' => $this->getDsnValueForDriver('pgsql'), |
|
| 585 | - 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
|
| 586 | - 'oracle' => $this->getDsnValueForDriver('oracle') |
|
| 587 | - ); |
|
| 583 | + 'mysql' => $this->getDsnValueForDriver('mysql'), |
|
| 584 | + 'pgsql' => $this->getDsnValueForDriver('pgsql'), |
|
| 585 | + 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
|
| 586 | + 'oracle' => $this->getDsnValueForDriver('oracle') |
|
| 587 | + ); |
|
| 588 | 588 | if (isset($driverDsnMap[$driver])){ |
| 589 | - $dsn = $driverDsnMap[$driver]; |
|
| 589 | + $dsn = $driverDsnMap[$driver]; |
|
| 590 | 590 | } |
| 591 | - } |
|
| 592 | - return $dsn; |
|
| 591 | + } |
|
| 592 | + return $dsn; |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -598,32 +598,32 @@ discard block |
||
| 598 | 598 | * @return string|null the dsn name |
| 599 | 599 | */ |
| 600 | 600 | protected function getDsnValueForDriver($driver){ |
| 601 | - $dsn = ''; |
|
| 602 | - $config = $this->getDatabaseConfiguration(); |
|
| 603 | - if (empty($config)){ |
|
| 601 | + $dsn = ''; |
|
| 602 | + $config = $this->getDatabaseConfiguration(); |
|
| 603 | + if (empty($config)){ |
|
| 604 | 604 | return null; |
| 605 | - } |
|
| 606 | - switch ($driver) { |
|
| 605 | + } |
|
| 606 | + switch ($driver) { |
|
| 607 | 607 | case 'mysql': |
| 608 | 608 | case 'pgsql': |
| 609 | 609 | $port = ''; |
| 610 | - if (! empty($config['port'])) { |
|
| 610 | + if (! empty($config['port'])) { |
|
| 611 | 611 | $port = 'port=' . $config['port'] . ';'; |
| 612 | - } |
|
| 613 | - $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
|
| 614 | - break; |
|
| 612 | + } |
|
| 613 | + $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
|
| 614 | + break; |
|
| 615 | 615 | case 'sqlite': |
| 616 | 616 | $dsn = 'sqlite:' . $config['database']; |
| 617 | - break; |
|
| 618 | - case 'oracle': |
|
| 617 | + break; |
|
| 618 | + case 'oracle': |
|
| 619 | 619 | $port = ''; |
| 620 | - if (! empty($config['port'])) { |
|
| 620 | + if (! empty($config['port'])) { |
|
| 621 | 621 | $port = ':' . $config['port']; |
| 622 | - } |
|
| 623 | - $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 624 | - break; |
|
| 625 | - } |
|
| 626 | - return $dsn; |
|
| 622 | + } |
|
| 623 | + $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 624 | + break; |
|
| 625 | + } |
|
| 626 | + return $dsn; |
|
| 627 | 627 | } |
| 628 | 628 | |
| 629 | 629 | /** |
@@ -635,11 +635,11 @@ discard block |
||
| 635 | 635 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
| 636 | 636 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 637 | 637 | if (! is_object($this->cacheInstance)){ |
| 638 | - //can not call method with reference in argument |
|
| 639 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 640 | - //use temporary variable |
|
| 641 | - $instance = & get_instance()->cache; |
|
| 642 | - $this->cacheInstance = $instance; |
|
| 638 | + //can not call method with reference in argument |
|
| 639 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 640 | + //use temporary variable |
|
| 641 | + $instance = & get_instance()->cache; |
|
| 642 | + $this->cacheInstance = $instance; |
|
| 643 | 643 | } |
| 644 | 644 | return $this->cacheInstance->get($cacheKey); |
| 645 | 645 | } |
@@ -651,27 +651,27 @@ discard block |
||
| 651 | 651 | * @param mixed $result the query result to save |
| 652 | 652 | * @param int $expire the cache TTL |
| 653 | 653 | */ |
| 654 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 654 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 655 | 655 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
| 656 | 656 | if (! is_object($this->cacheInstance)){ |
| 657 | - //can not call method with reference in argument |
|
| 658 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 659 | - //use temporary variable |
|
| 660 | - $instance = & get_instance()->cache; |
|
| 661 | - $this->cacheInstance = $instance; |
|
| 662 | - } |
|
| 657 | + //can not call method with reference in argument |
|
| 658 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 659 | + //use temporary variable |
|
| 660 | + $instance = & get_instance()->cache; |
|
| 661 | + $this->cacheInstance = $instance; |
|
| 662 | + } |
|
| 663 | 663 | $this->cacheInstance->set($key, $result, $expire); |
| 664 | - } |
|
| 664 | + } |
|
| 665 | 665 | |
| 666 | 666 | |
| 667 | - /** |
|
| 668 | - * Return the cache key for the given query |
|
| 669 | - * @see Database::query |
|
| 670 | - * |
|
| 671 | - * @return string |
|
| 672 | - */ |
|
| 667 | + /** |
|
| 668 | + * Return the cache key for the given query |
|
| 669 | + * @see Database::query |
|
| 670 | + * |
|
| 671 | + * @return string |
|
| 672 | + */ |
|
| 673 | 673 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
| 674 | - return md5($query . $returnAsList . $returnAsArray); |
|
| 674 | + return md5($query . $returnAsList . $returnAsArray); |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | |
@@ -679,20 +679,20 @@ discard block |
||
| 679 | 679 | * Reset the database class attributs to the initail values before each query. |
| 680 | 680 | */ |
| 681 | 681 | private function reset(){ |
| 682 | - //query builder reset |
|
| 683 | - $this->queryBuilder->reset(); |
|
| 684 | - $this->numRows = 0; |
|
| 685 | - $this->insertId = null; |
|
| 686 | - $this->query = null; |
|
| 687 | - $this->result = array(); |
|
| 688 | - $this->data = array(); |
|
| 682 | + //query builder reset |
|
| 683 | + $this->queryBuilder->reset(); |
|
| 684 | + $this->numRows = 0; |
|
| 685 | + $this->insertId = null; |
|
| 686 | + $this->query = null; |
|
| 687 | + $this->result = array(); |
|
| 688 | + $this->data = array(); |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | /** |
| 692 | 692 | * The class destructor |
| 693 | 693 | */ |
| 694 | 694 | public function __destruct(){ |
| 695 | - $this->pdo = null; |
|
| 695 | + $this->pdo = null; |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | } |
@@ -23,92 +23,92 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Database extends BaseClass{ |
|
| 26 | + class Database extends BaseClass { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | 31 | */ |
| 32 | - private $pdo = null; |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - private $databaseName = null; |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The number of rows returned by the last query |
| 42 | 42 | * @var int |
| 43 | 43 | */ |
| 44 | - private $numRows = 0; |
|
| 44 | + private $numRows = 0; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The last insert id for the primary key column that have auto increment or sequence |
| 48 | 48 | * @var mixed |
| 49 | 49 | */ |
| 50 | - private $insertId = null; |
|
| 50 | + private $insertId = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The full SQL query statment after build for each command |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $query = null; |
|
| 56 | + private $query = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The result returned for the last query |
| 60 | 60 | * @var mixed |
| 61 | 61 | */ |
| 62 | - private $result = array(); |
|
| 62 | + private $result = array(); |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 66 | 66 | * @var int |
| 67 | 67 | */ |
| 68 | - private $cacheTtl = 0; |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The cache current time to live. 0 means no need to use the cache feature |
| 72 | 72 | * @var int |
| 73 | 73 | */ |
| 74 | - private $temporaryCacheTtl = 0; |
|
| 74 | + private $temporaryCacheTtl = 0; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The number of executed query for the current request |
| 78 | 78 | * @var int |
| 79 | 79 | */ |
| 80 | - private $queryCount = 0; |
|
| 80 | + private $queryCount = 0; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The default data to be used in the statments query INSERT, UPDATE |
| 84 | 84 | * @var array |
| 85 | 85 | */ |
| 86 | - private $data = array(); |
|
| 86 | + private $data = array(); |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The database configuration |
| 90 | 90 | * @var array |
| 91 | 91 | */ |
| 92 | - private $config = array(); |
|
| 92 | + private $config = array(); |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The cache instance |
| 96 | 96 | * @var object |
| 97 | 97 | */ |
| 98 | - private $cacheInstance = null; |
|
| 98 | + private $cacheInstance = null; |
|
| 99 | 99 | |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * The DatabaseQueryBuilder instance |
| 103 | 103 | * @var object |
| 104 | 104 | */ |
| 105 | - protected $queryBuilder = null; |
|
| 105 | + protected $queryBuilder = null; |
|
| 106 | 106 | |
| 107 | 107 | /** |
| 108 | 108 | * The DatabaseQueryRunner instance |
| 109 | 109 | * @var object |
| 110 | 110 | */ |
| 111 | - protected $queryRunner = null; |
|
| 111 | + protected $queryRunner = null; |
|
| 112 | 112 | |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 117 | 117 | * @param boolean $autoConnect whether to connect to database automatically |
| 118 | 118 | */ |
| 119 | - public function __construct($overwriteConfig = array(), $autoConnect = true){ |
|
| 119 | + public function __construct($overwriteConfig = array(), $autoConnect = true) { |
|
| 120 | 120 | parent::__construct(); |
| 121 | 121 | |
| 122 | 122 | //Set DatabaseQueryBuilder instance to use |
@@ -136,10 +136,10 @@ discard block |
||
| 136 | 136 | * This is used to connect to database |
| 137 | 137 | * @return bool |
| 138 | 138 | */ |
| 139 | - public function connect(){ |
|
| 139 | + public function connect() { |
|
| 140 | 140 | $config = $this->getDatabaseConfiguration(); |
| 141 | - if (! empty($config)){ |
|
| 142 | - try{ |
|
| 141 | + if (!empty($config)) { |
|
| 142 | + try { |
|
| 143 | 143 | $this->pdo = new PDO($this->getDsnValueFromConfig(), $config['username'], $config['password']); |
| 144 | 144 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 145 | 145 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | return is_object($this->pdo); |
| 152 | 152 | } |
| 153 | - catch (PDOException $e){ |
|
| 153 | + catch (PDOException $e) { |
|
| 154 | 154 | $this->logger->fatal($e->getMessage()); |
| 155 | 155 | show_error('Cannot connect to Database.'); |
| 156 | 156 | return false; |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | * Return the number of rows returned by the current query |
| 165 | 165 | * @return int |
| 166 | 166 | */ |
| 167 | - public function numRows(){ |
|
| 167 | + public function numRows() { |
|
| 168 | 168 | return $this->numRows; |
| 169 | 169 | } |
| 170 | 170 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * Return the last insert id value |
| 173 | 173 | * @return mixed |
| 174 | 174 | */ |
| 175 | - public function insertId(){ |
|
| 175 | + public function insertId() { |
|
| 176 | 176 | return $this->insertId; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -183,10 +183,10 @@ discard block |
||
| 183 | 183 | * If is string will determine the result type "array" or "object" |
| 184 | 184 | * @return mixed the query SQL string or the record result |
| 185 | 185 | */ |
| 186 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 186 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 187 | 187 | $this->queryBuilder->limit(1); |
| 188 | 188 | $query = $this->getAll(true); |
| 189 | - if ($returnSQLQueryOrResultType === true){ |
|
| 189 | + if ($returnSQLQueryOrResultType === true) { |
|
| 190 | 190 | return $query; |
| 191 | 191 | } else { |
| 192 | 192 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | * If is string will determine the result type "array" or "object" |
| 200 | 200 | * @return mixed the query SQL string or the record result |
| 201 | 201 | */ |
| 202 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 202 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 203 | 203 | $query = $this->queryBuilder->getQuery(); |
| 204 | - if ($returnSQLQueryOrResultType === true){ |
|
| 204 | + if ($returnSQLQueryOrResultType === true) { |
|
| 205 | 205 | return $query; |
| 206 | 206 | } |
| 207 | 207 | return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
@@ -213,18 +213,18 @@ discard block |
||
| 213 | 213 | * @param boolean $escape whether to escape or not the values |
| 214 | 214 | * @return mixed the insert id of the new record or null |
| 215 | 215 | */ |
| 216 | - public function insert($data = array(), $escape = true){ |
|
| 217 | - if (empty($data) && ! empty($this->data)){ |
|
| 216 | + public function insert($data = array(), $escape = true) { |
|
| 217 | + if (empty($data) && !empty($this->data)) { |
|
| 218 | 218 | //as when using $this->setData() may be the data already escaped |
| 219 | 219 | $escape = false; |
| 220 | 220 | $data = $this->data; |
| 221 | 221 | } |
| 222 | 222 | $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
| 223 | 223 | $result = $this->query($query); |
| 224 | - if ($result){ |
|
| 224 | + if ($result) { |
|
| 225 | 225 | $this->insertId = $this->pdo->lastInsertId(); |
| 226 | 226 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 227 | - return ! ($this->insertId) ? true : $this->insertId; |
|
| 227 | + return !($this->insertId) ? true : $this->insertId; |
|
| 228 | 228 | } |
| 229 | 229 | return false; |
| 230 | 230 | } |
@@ -235,8 +235,8 @@ discard block |
||
| 235 | 235 | * @param boolean $escape whether to escape or not the values |
| 236 | 236 | * @return mixed the update status |
| 237 | 237 | */ |
| 238 | - public function update($data = array(), $escape = true){ |
|
| 239 | - if (empty($data) && ! empty($this->data)){ |
|
| 238 | + public function update($data = array(), $escape = true) { |
|
| 239 | + if (empty($data) && !empty($this->data)) { |
|
| 240 | 240 | //as when using $this->setData() may be the data already escaped |
| 241 | 241 | $escape = false; |
| 242 | 242 | $data = $this->data; |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | * Delete the record in database |
| 250 | 250 | * @return mixed the delete status |
| 251 | 251 | */ |
| 252 | - public function delete(){ |
|
| 252 | + public function delete() { |
|
| 253 | 253 | $query = $this->queryBuilder->delete()->getQuery(); |
| 254 | 254 | return $this->query($query); |
| 255 | 255 | } |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * @param integer $ttl the cache time to live in second |
| 260 | 260 | * @return object the current Database instance |
| 261 | 261 | */ |
| 262 | - public function setCache($ttl = 0){ |
|
| 262 | + public function setCache($ttl = 0) { |
|
| 263 | 263 | $this->cacheTtl = $ttl; |
| 264 | 264 | $this->temporaryCacheTtl = $ttl; |
| 265 | 265 | return $this; |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | * @param integer $ttl the cache time to live in second |
| 271 | 271 | * @return object the current Database instance |
| 272 | 272 | */ |
| 273 | - public function cached($ttl = 0){ |
|
| 273 | + public function cached($ttl = 0) { |
|
| 274 | 274 | $this->temporaryCacheTtl = $ttl; |
| 275 | 275 | return $this; |
| 276 | 276 | } |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | * @return mixed the data after escaped or the same data if no |
| 283 | 283 | * need escaped |
| 284 | 284 | */ |
| 285 | - public function escape($data, $escaped = true){ |
|
| 285 | + public function escape($data, $escaped = true) { |
|
| 286 | 286 | $data = trim($data); |
| 287 | - if($escaped){ |
|
| 287 | + if ($escaped) { |
|
| 288 | 288 | return $this->pdo->quote($data); |
| 289 | 289 | } |
| 290 | 290 | return $data; |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | * Return the number query executed count for the current request |
| 295 | 295 | * @return int |
| 296 | 296 | */ |
| 297 | - public function queryCount(){ |
|
| 297 | + public function queryCount() { |
|
| 298 | 298 | return $this->queryCount; |
| 299 | 299 | } |
| 300 | 300 | |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | * Return the current query SQL string |
| 303 | 303 | * @return string |
| 304 | 304 | */ |
| 305 | - public function getQuery(){ |
|
| 305 | + public function getQuery() { |
|
| 306 | 306 | return $this->query; |
| 307 | 307 | } |
| 308 | 308 | |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * Return the application database name |
| 311 | 311 | * @return string |
| 312 | 312 | */ |
| 313 | - public function getDatabaseName(){ |
|
| 313 | + public function getDatabaseName() { |
|
| 314 | 314 | return $this->databaseName; |
| 315 | 315 | } |
| 316 | 316 | |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * Return the PDO instance |
| 319 | 319 | * @return object |
| 320 | 320 | */ |
| 321 | - public function getPdo(){ |
|
| 321 | + public function getPdo() { |
|
| 322 | 322 | return $this->pdo; |
| 323 | 323 | } |
| 324 | 324 | |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | * @param object $pdo the pdo object |
| 328 | 328 | * @return object Database |
| 329 | 329 | */ |
| 330 | - public function setPdo(PDO $pdo){ |
|
| 330 | + public function setPdo(PDO $pdo) { |
|
| 331 | 331 | $this->pdo = $pdo; |
| 332 | 332 | return $this; |
| 333 | 333 | } |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * Return the cache instance |
| 337 | 337 | * @return CacheInterface |
| 338 | 338 | */ |
| 339 | - public function getCacheInstance(){ |
|
| 339 | + public function getCacheInstance() { |
|
| 340 | 340 | return $this->cacheInstance; |
| 341 | 341 | } |
| 342 | 342 | |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | * @param CacheInterface $cache the cache object |
| 346 | 346 | * @return object Database |
| 347 | 347 | */ |
| 348 | - public function setCacheInstance($cache){ |
|
| 348 | + public function setCacheInstance($cache) { |
|
| 349 | 349 | $this->cacheInstance = $cache; |
| 350 | 350 | return $this; |
| 351 | 351 | } |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | * Return the DatabaseQueryBuilder instance |
| 356 | 356 | * @return object DatabaseQueryBuilder |
| 357 | 357 | */ |
| 358 | - public function getQueryBuilder(){ |
|
| 358 | + public function getQueryBuilder() { |
|
| 359 | 359 | return $this->queryBuilder; |
| 360 | 360 | } |
| 361 | 361 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * Set the DatabaseQueryBuilder instance |
| 364 | 364 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 365 | 365 | */ |
| 366 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 366 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder) { |
|
| 367 | 367 | $this->queryBuilder = $queryBuilder; |
| 368 | 368 | return $this; |
| 369 | 369 | } |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | * Return the DatabaseQueryRunner instance |
| 373 | 373 | * @return object DatabaseQueryRunner |
| 374 | 374 | */ |
| 375 | - public function getQueryRunner(){ |
|
| 375 | + public function getQueryRunner() { |
|
| 376 | 376 | return $this->queryRunner; |
| 377 | 377 | } |
| 378 | 378 | |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | * Set the DatabaseQueryRunner instance |
| 381 | 381 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 382 | 382 | */ |
| 383 | - public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
|
| 383 | + public function setQueryRunner(DatabaseQueryRunner $queryRunner) { |
|
| 384 | 384 | $this->queryRunner = $queryRunner; |
| 385 | 385 | return $this; |
| 386 | 386 | } |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | * Return the data to be used for insert, update, etc. |
| 390 | 390 | * @return array |
| 391 | 391 | */ |
| 392 | - public function getData(){ |
|
| 392 | + public function getData() { |
|
| 393 | 393 | return $this->data; |
| 394 | 394 | } |
| 395 | 395 | |
@@ -400,9 +400,9 @@ discard block |
||
| 400 | 400 | * @param boolean $escape whether to escape or not the $value |
| 401 | 401 | * @return object the current Database instance |
| 402 | 402 | */ |
| 403 | - public function setData($key, $value = null, $escape = true){ |
|
| 404 | - if (is_array($key)){ |
|
| 405 | - foreach($key as $k => $v){ |
|
| 403 | + public function setData($key, $value = null, $escape = true) { |
|
| 404 | + if (is_array($key)) { |
|
| 405 | + foreach ($key as $k => $v) { |
|
| 406 | 406 | $this->setData($k, $v, $escape); |
| 407 | 407 | } |
| 408 | 408 | } else { |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | * @param boolean $returnAsArray return the result as array or not |
| 419 | 419 | * @return mixed the query result |
| 420 | 420 | */ |
| 421 | - public function query($query, $returnAsList = true, $returnAsArray = false){ |
|
| 421 | + public function query($query, $returnAsList = true, $returnAsArray = false) { |
|
| 422 | 422 | $this->reset(); |
| 423 | 423 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 424 | 424 | //If is the SELECT query |
@@ -439,12 +439,12 @@ discard block |
||
| 439 | 439 | //if can use cache feature for this query |
| 440 | 440 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 441 | 441 | |
| 442 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 442 | + if ($dbCacheStatus && $isSqlSELECTQuery) { |
|
| 443 | 443 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 444 | 444 | $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | - if (!$cacheContent){ |
|
| 447 | + if (!$cacheContent) { |
|
| 448 | 448 | //count the number of query execution to server |
| 449 | 449 | $this->queryCount++; |
| 450 | 450 | |
@@ -453,19 +453,19 @@ discard block |
||
| 453 | 453 | ->setReturnAsArray($returnAsArray) |
| 454 | 454 | ->execute(); |
| 455 | 455 | |
| 456 | - if (!is_object($queryResult)){ |
|
| 456 | + if (!is_object($queryResult)) { |
|
| 457 | 457 | $this->result = false; |
| 458 | 458 | $this->numRows = 0; |
| 459 | 459 | return $this->result; |
| 460 | 460 | } |
| 461 | 461 | $this->result = $queryResult->getResult(); |
| 462 | 462 | $this->numRows = $queryResult->getNumRows(); |
| 463 | - if ($isSqlSELECTQuery && $dbCacheStatus){ |
|
| 463 | + if ($isSqlSELECTQuery && $dbCacheStatus) { |
|
| 464 | 464 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 465 | 465 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 466 | 466 | } |
| 467 | - } else if ($isSqlSELECTQuery){ |
|
| 468 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 467 | + } else if ($isSqlSELECTQuery) { |
|
| 468 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 469 | 469 | $this->result = $cacheContent; |
| 470 | 470 | $this->numRows = count($this->result); |
| 471 | 471 | } |
@@ -479,9 +479,9 @@ discard block |
||
| 479 | 479 | * @param boolean $autoConnect whether to connect to database after set the configuration |
| 480 | 480 | * @return object Database |
| 481 | 481 | */ |
| 482 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false){ |
|
| 482 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false) { |
|
| 483 | 483 | $db = array(); |
| 484 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 484 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')) { |
|
| 485 | 485 | //here don't use require_once because somewhere user can create database instance directly |
| 486 | 486 | require CONFIG_PATH . 'database.php'; |
| 487 | 487 | } |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | //determine the port using the hostname like localhost:3307 |
| 497 | 497 | //hostname will be "localhost", and port "3307" |
| 498 | 498 | $p = explode(':', $config['hostname']); |
| 499 | - if (count($p) >= 2){ |
|
| 499 | + if (count($p) >= 2) { |
|
| 500 | 500 | $config['hostname'] = $p[0]; |
| 501 | 501 | $config['port'] = $p[1]; |
| 502 | 502 | } |
@@ -510,7 +510,7 @@ discard block |
||
| 510 | 510 | array('password' => string_hidden($this->config['password'])) |
| 511 | 511 | )) |
| 512 | 512 | ); |
| 513 | - if($autoConnect){ |
|
| 513 | + if ($autoConnect) { |
|
| 514 | 514 | //Now connect to the database |
| 515 | 515 | $this->connect(); |
| 516 | 516 | } |
@@ -521,14 +521,14 @@ discard block |
||
| 521 | 521 | * Return the database configuration |
| 522 | 522 | * @return array |
| 523 | 523 | */ |
| 524 | - public function getDatabaseConfiguration(){ |
|
| 524 | + public function getDatabaseConfiguration() { |
|
| 525 | 525 | return $this->config; |
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | /** |
| 529 | 529 | * Close the connexion |
| 530 | 530 | */ |
| 531 | - public function close(){ |
|
| 531 | + public function close() { |
|
| 532 | 532 | $this->pdo = null; |
| 533 | 533 | } |
| 534 | 534 | |
@@ -536,7 +536,7 @@ discard block |
||
| 536 | 536 | * Return the database default configuration |
| 537 | 537 | * @return array |
| 538 | 538 | */ |
| 539 | - protected function getDatabaseDefaultConfiguration(){ |
|
| 539 | + protected function getDatabaseDefaultConfiguration() { |
|
| 540 | 540 | return array( |
| 541 | 541 | 'driver' => '', |
| 542 | 542 | 'username' => '', |
@@ -554,16 +554,16 @@ discard block |
||
| 554 | 554 | * Update the DatabaseQueryBuilder and DatabaseQueryRunner properties |
| 555 | 555 | * @return void |
| 556 | 556 | */ |
| 557 | - protected function updateQueryBuilderAndRunnerProperties(){ |
|
| 557 | + protected function updateQueryBuilderAndRunnerProperties() { |
|
| 558 | 558 | //update queryBuilder with some properties needed |
| 559 | - if (is_object($this->queryBuilder)){ |
|
| 559 | + if (is_object($this->queryBuilder)) { |
|
| 560 | 560 | $this->queryBuilder->setDriver($this->config['driver']) |
| 561 | 561 | ->setPrefix($this->config['prefix']) |
| 562 | 562 | ->setPdo($this->pdo); |
| 563 | 563 | } |
| 564 | 564 | |
| 565 | 565 | //update queryRunner with some properties needed |
| 566 | - if (is_object($this->queryRunner)){ |
|
| 566 | + if (is_object($this->queryRunner)) { |
|
| 567 | 567 | $this->queryRunner->setDriver($this->config['driver']) |
| 568 | 568 | ->setPdo($this->pdo); |
| 569 | 569 | } |
@@ -574,10 +574,10 @@ discard block |
||
| 574 | 574 | * This method is used to get the PDO DSN string using the configured driver |
| 575 | 575 | * @return string|null the DSN string or null if can not find it |
| 576 | 576 | */ |
| 577 | - protected function getDsnValueFromConfig(){ |
|
| 577 | + protected function getDsnValueFromConfig() { |
|
| 578 | 578 | $dsn = null; |
| 579 | 579 | $config = $this->getDatabaseConfiguration(); |
| 580 | - if (! empty($config)){ |
|
| 580 | + if (!empty($config)) { |
|
| 581 | 581 | $driver = $config['driver']; |
| 582 | 582 | $driverDsnMap = array( |
| 583 | 583 | 'mysql' => $this->getDsnValueForDriver('mysql'), |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
| 586 | 586 | 'oracle' => $this->getDsnValueForDriver('oracle') |
| 587 | 587 | ); |
| 588 | - if (isset($driverDsnMap[$driver])){ |
|
| 588 | + if (isset($driverDsnMap[$driver])) { |
|
| 589 | 589 | $dsn = $driverDsnMap[$driver]; |
| 590 | 590 | } |
| 591 | 591 | } |
@@ -597,17 +597,17 @@ discard block |
||
| 597 | 597 | * @param string $driver the driver name |
| 598 | 598 | * @return string|null the dsn name |
| 599 | 599 | */ |
| 600 | - protected function getDsnValueForDriver($driver){ |
|
| 600 | + protected function getDsnValueForDriver($driver) { |
|
| 601 | 601 | $dsn = ''; |
| 602 | 602 | $config = $this->getDatabaseConfiguration(); |
| 603 | - if (empty($config)){ |
|
| 603 | + if (empty($config)) { |
|
| 604 | 604 | return null; |
| 605 | 605 | } |
| 606 | 606 | switch ($driver) { |
| 607 | 607 | case 'mysql': |
| 608 | 608 | case 'pgsql': |
| 609 | 609 | $port = ''; |
| 610 | - if (! empty($config['port'])) { |
|
| 610 | + if (!empty($config['port'])) { |
|
| 611 | 611 | $port = 'port=' . $config['port'] . ';'; |
| 612 | 612 | } |
| 613 | 613 | $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
@@ -617,10 +617,10 @@ discard block |
||
| 617 | 617 | break; |
| 618 | 618 | case 'oracle': |
| 619 | 619 | $port = ''; |
| 620 | - if (! empty($config['port'])) { |
|
| 620 | + if (!empty($config['port'])) { |
|
| 621 | 621 | $port = ':' . $config['port']; |
| 622 | 622 | } |
| 623 | - $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 623 | + $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 624 | 624 | break; |
| 625 | 625 | } |
| 626 | 626 | return $dsn; |
@@ -632,9 +632,9 @@ discard block |
||
| 632 | 632 | * |
| 633 | 633 | * @return mixed |
| 634 | 634 | */ |
| 635 | - protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
|
| 635 | + protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray) { |
|
| 636 | 636 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 637 | - if (! is_object($this->cacheInstance)){ |
|
| 637 | + if (!is_object($this->cacheInstance)) { |
|
| 638 | 638 | //can not call method with reference in argument |
| 639 | 639 | //like $this->setCacheInstance(& get_instance()->cache); |
| 640 | 640 | //use temporary variable |
@@ -651,9 +651,9 @@ discard block |
||
| 651 | 651 | * @param mixed $result the query result to save |
| 652 | 652 | * @param int $expire the cache TTL |
| 653 | 653 | */ |
| 654 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 655 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 656 | - if (! is_object($this->cacheInstance)){ |
|
| 654 | + protected function setCacheContentForQuery($query, $key, $result, $expire) { |
|
| 655 | + $this->logger->info('Save the result for query [' . $query . '] into cache for future use'); |
|
| 656 | + if (!is_object($this->cacheInstance)) { |
|
| 657 | 657 | //can not call method with reference in argument |
| 658 | 658 | //like $this->setCacheInstance(& get_instance()->cache); |
| 659 | 659 | //use temporary variable |
@@ -670,7 +670,7 @@ discard block |
||
| 670 | 670 | * |
| 671 | 671 | * @return string |
| 672 | 672 | */ |
| 673 | - protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
|
| 673 | + protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray) { |
|
| 674 | 674 | return md5($query . $returnAsList . $returnAsArray); |
| 675 | 675 | } |
| 676 | 676 | |
@@ -678,7 +678,7 @@ discard block |
||
| 678 | 678 | /** |
| 679 | 679 | * Reset the database class attributs to the initail values before each query. |
| 680 | 680 | */ |
| 681 | - private function reset(){ |
|
| 681 | + private function reset() { |
|
| 682 | 682 | //query builder reset |
| 683 | 683 | $this->queryBuilder->reset(); |
| 684 | 684 | $this->numRows = 0; |
@@ -691,7 +691,7 @@ discard block |
||
| 691 | 691 | /** |
| 692 | 692 | * The class destructor |
| 693 | 693 | */ |
| 694 | - public function __destruct(){ |
|
| 694 | + public function __destruct() { |
|
| 695 | 695 | $this->pdo = null; |
| 696 | 696 | } |
| 697 | 697 | |
@@ -1,75 +1,75 @@ 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 extends BaseClass 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 extends BaseClass implements SessionHandlerInterface{ |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The encryption method to use to encrypt session data in database |
|
| 38 | - * @const string |
|
| 39 | - */ |
|
| 40 | - const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 36 | + /** |
|
| 37 | + * The encryption method to use to encrypt session data in database |
|
| 38 | + * @const string |
|
| 39 | + */ |
|
| 40 | + const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Super global instance |
|
| 44 | - * @var object |
|
| 45 | - */ |
|
| 46 | - protected $OBJ = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Session secret to use |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - private $sessionSecret = null; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The initialisation vector to use for openssl |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $iv = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The model instance to use |
|
| 62 | - * @var object |
|
| 63 | - */ |
|
| 64 | - private $modelInstance = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The columns of the table to use to store session data |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $sessionTableColumns = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 42 | + /** |
|
| 43 | + * Super global instance |
|
| 44 | + * @var object |
|
| 45 | + */ |
|
| 46 | + protected $OBJ = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Session secret to use |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + private $sessionSecret = null; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The initialisation vector to use for openssl |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $iv = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The model instance to use |
|
| 62 | + * @var object |
|
| 63 | + */ |
|
| 64 | + private $modelInstance = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The columns of the table to use to store session data |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $sessionTableColumns = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | 73 | * Instance of the Loader class |
| 74 | 74 | * @var Loader |
| 75 | 75 | */ |
@@ -79,225 +79,225 @@ discard block |
||
| 79 | 79 | * Create new instance of Database session handler |
| 80 | 80 | * @param object $modelInstance the model instance |
| 81 | 81 | */ |
| 82 | - public function __construct(DBSessionHandlerModel $modelInstance = null){ |
|
| 83 | - parent::__construct(); |
|
| 82 | + public function __construct(DBSessionHandlerModel $modelInstance = null){ |
|
| 83 | + parent::__construct(); |
|
| 84 | 84 | |
| 85 | - //Set Loader instance to use |
|
| 86 | - $this->setDependencyInstanceFromParamOrCreate('loader', null, 'Loader', 'classes'); |
|
| 85 | + //Set Loader instance to use |
|
| 86 | + $this->setDependencyInstanceFromParamOrCreate('loader', null, 'Loader', 'classes'); |
|
| 87 | 87 | |
| 88 | - $this->OBJ = & get_instance(); |
|
| 88 | + $this->OBJ = & get_instance(); |
|
| 89 | 89 | |
| 90 | - if (is_object($modelInstance)){ |
|
| 91 | - $this->setModelInstance($modelInstance); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Set the session secret used to encrypt the data in database |
|
| 97 | - * @param string $secret the base64 string secret |
|
| 98 | - */ |
|
| 99 | - public function setSessionSecret($secret){ |
|
| 100 | - $this->sessionSecret = $secret; |
|
| 101 | - return $this; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Return the session secret |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function getSessionSecret(){ |
|
| 109 | - return $this->sessionSecret; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Set the initializer vector for openssl |
|
| 115 | - * @param string $key the session secret used in base64 format |
|
| 116 | - */ |
|
| 117 | - public function setInitializerVector($key){ |
|
| 118 | - $ivLength = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 119 | - $key = base64_decode($key); |
|
| 120 | - $this->iv = substr(hash('sha256', $key), 0, $ivLength); |
|
| 121 | - return $this; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Return the initializer vector |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - public function getInitializerVector(){ |
|
| 129 | - return $this->iv; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Open the database session handler, here nothing to do just return true |
|
| 134 | - * @param string $savePath the session save path |
|
| 135 | - * @param string $sessionName the session name |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - public function open($savePath, $sessionName){ |
|
| 139 | - $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 140 | - //try to check if session secret is set before |
|
| 141 | - $secret = $this->getSessionSecret(); |
|
| 142 | - if (empty($secret)){ |
|
| 143 | - $secret = get_config('session_secret', null); |
|
| 144 | - $this->setSessionSecret($secret); |
|
| 145 | - } |
|
| 146 | - $this->logger->info('Session secret: ' . $secret); |
|
| 147 | - |
|
| 148 | - if (! is_object($this->modelInstance)){ |
|
| 149 | - $this->setModelInstanceFromSessionConfig(); |
|
| 150 | - } |
|
| 151 | - $this->setInitializerVector($secret); |
|
| 152 | - |
|
| 153 | - //set session tables columns |
|
| 154 | - $this->sessionTableColumns = $this->modelInstance->getSessionTableColumns(); |
|
| 155 | - |
|
| 156 | - if (empty($this->sessionTableColumns)){ |
|
| 157 | - show_error('The session handler is "database" but the table columns not set'); |
|
| 158 | - } |
|
| 159 | - $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 90 | + if (is_object($modelInstance)){ |
|
| 91 | + $this->setModelInstance($modelInstance); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Set the session secret used to encrypt the data in database |
|
| 97 | + * @param string $secret the base64 string secret |
|
| 98 | + */ |
|
| 99 | + public function setSessionSecret($secret){ |
|
| 100 | + $this->sessionSecret = $secret; |
|
| 101 | + return $this; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Return the session secret |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function getSessionSecret(){ |
|
| 109 | + return $this->sessionSecret; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Set the initializer vector for openssl |
|
| 115 | + * @param string $key the session secret used in base64 format |
|
| 116 | + */ |
|
| 117 | + public function setInitializerVector($key){ |
|
| 118 | + $ivLength = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 119 | + $key = base64_decode($key); |
|
| 120 | + $this->iv = substr(hash('sha256', $key), 0, $ivLength); |
|
| 121 | + return $this; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Return the initializer vector |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + public function getInitializerVector(){ |
|
| 129 | + return $this->iv; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Open the database session handler, here nothing to do just return true |
|
| 134 | + * @param string $savePath the session save path |
|
| 135 | + * @param string $sessionName the session name |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + public function open($savePath, $sessionName){ |
|
| 139 | + $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 140 | + //try to check if session secret is set before |
|
| 141 | + $secret = $this->getSessionSecret(); |
|
| 142 | + if (empty($secret)){ |
|
| 143 | + $secret = get_config('session_secret', null); |
|
| 144 | + $this->setSessionSecret($secret); |
|
| 145 | + } |
|
| 146 | + $this->logger->info('Session secret: ' . $secret); |
|
| 147 | + |
|
| 148 | + if (! is_object($this->modelInstance)){ |
|
| 149 | + $this->setModelInstanceFromSessionConfig(); |
|
| 150 | + } |
|
| 151 | + $this->setInitializerVector($secret); |
|
| 152 | + |
|
| 153 | + //set session tables columns |
|
| 154 | + $this->sessionTableColumns = $this->modelInstance->getSessionTableColumns(); |
|
| 155 | + |
|
| 156 | + if (empty($this->sessionTableColumns)){ |
|
| 157 | + show_error('The session handler is "database" but the table columns not set'); |
|
| 158 | + } |
|
| 159 | + $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 160 | 160 | |
| 161 | - //delete the expired session |
|
| 162 | - $timeActivity = get_config('session_inactivity_time', 100); |
|
| 163 | - $this->gc($timeActivity); |
|
| 161 | + //delete the expired session |
|
| 162 | + $timeActivity = get_config('session_inactivity_time', 100); |
|
| 163 | + $this->gc($timeActivity); |
|
| 164 | 164 | |
| 165 | - return true; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Close the session |
|
| 170 | - * @return boolean |
|
| 171 | - */ |
|
| 172 | - public function close(){ |
|
| 173 | - $this->logger->debug('Closing database session handler'); |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Get the session value for the given session id |
|
| 179 | - * @param string $sid the session id to use |
|
| 180 | - * @return string the session data in serialiaze format |
|
| 181 | - */ |
|
| 182 | - public function read($sid){ |
|
| 183 | - $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 184 | - $instance = $this->getModelInstance(); |
|
| 185 | - $columns = $this->sessionTableColumns; |
|
| 186 | - $this->loader->functions('user_agent'); |
|
| 187 | - $this->loader->library('Browser'); |
|
| 165 | + return true; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Close the session |
|
| 170 | + * @return boolean |
|
| 171 | + */ |
|
| 172 | + public function close(){ |
|
| 173 | + $this->logger->debug('Closing database session handler'); |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Get the session value for the given session id |
|
| 179 | + * @param string $sid the session id to use |
|
| 180 | + * @return string the session data in serialiaze format |
|
| 181 | + */ |
|
| 182 | + public function read($sid){ |
|
| 183 | + $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 184 | + $instance = $this->getModelInstance(); |
|
| 185 | + $columns = $this->sessionTableColumns; |
|
| 186 | + $this->loader->functions('user_agent'); |
|
| 187 | + $this->loader->library('Browser'); |
|
| 188 | 188 | |
| 189 | - $ip = get_ip(); |
|
| 190 | - $host = @gethostbyaddr($ip) or null; |
|
| 191 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 189 | + $ip = get_ip(); |
|
| 190 | + $host = @gethostbyaddr($ip) or null; |
|
| 191 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 192 | 192 | |
| 193 | - $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 194 | - if ($data && isset($data->{$columns['sdata']})){ |
|
| 195 | - //checking inactivity |
|
| 196 | - $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 197 | - if ($data->{$columns['stime']} < $timeInactivity){ |
|
| 198 | - $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 199 | - $this->destroy($sid); |
|
| 200 | - return null; |
|
| 201 | - } |
|
| 202 | - return $this->decode($data->{$columns['sdata']}); |
|
| 203 | - } |
|
| 204 | - $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 205 | - return null; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Save the session data |
|
| 210 | - * @param string $sid the session ID |
|
| 211 | - * @param mixed $data the session data to save in serialize format |
|
| 212 | - * @return boolean |
|
| 213 | - */ |
|
| 214 | - public function write($sid, $data){ |
|
| 215 | - $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 216 | - $instance = $this->getModelInstance(); |
|
| 217 | - $columns = $this->sessionTableColumns; |
|
| 218 | - |
|
| 219 | - $this->loader->functions('user_agent'); |
|
| 220 | - $this->loader->library('Browser'); |
|
| 221 | - |
|
| 222 | - $ip = get_ip(); |
|
| 223 | - $keyValue = $instance->getKeyValue(); |
|
| 224 | - $host = @gethostbyaddr($ip) or null; |
|
| 225 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 226 | - $data = $this->encode($data); |
|
| 227 | - $params = array( |
|
| 228 | - $columns['sid'] => $sid, |
|
| 229 | - $columns['sdata'] => $data, |
|
| 230 | - $columns['stime'] => time(), |
|
| 231 | - $columns['shost'] => $host, |
|
| 232 | - $columns['sbrowser'] => $browser, |
|
| 233 | - $columns['sip'] => $ip, |
|
| 234 | - $columns['skey'] => $keyValue |
|
| 235 | - ); |
|
| 236 | - $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 237 | - $exists = $instance->get($sid); |
|
| 238 | - if ($exists){ |
|
| 239 | - $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 240 | - //update |
|
| 241 | - unset($params[$columns['sid']]); |
|
| 242 | - return $instance->update($sid, $params); |
|
| 243 | - } |
|
| 244 | - $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 245 | - return $instance->insert($params); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Destroy the session data for the given session id |
|
| 251 | - * @param string $sid the session id value |
|
| 252 | - * @return boolean |
|
| 253 | - */ |
|
| 254 | - public function destroy($sid){ |
|
| 255 | - $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 256 | - $instance = $this->modelInstance; |
|
| 257 | - $instance->delete($sid); |
|
| 258 | - return true; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Clean the expire session data to save espace |
|
| 263 | - * @param integer $maxLifetime the max lifetime |
|
| 264 | - * @return boolean |
|
| 265 | - */ |
|
| 266 | - public function gc($maxLifetime){ |
|
| 267 | - $instance = $this->modelInstance; |
|
| 268 | - $time = time() - $maxLifetime; |
|
| 269 | - $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 270 | - $instance->deleteByTime($time); |
|
| 271 | - return true; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Encode the session data using the openssl |
|
| 276 | - * @param mixed $data the session data to encode |
|
| 277 | - * @return mixed the encoded session data |
|
| 278 | - */ |
|
| 279 | - public function encode($data){ |
|
| 280 | - $key = base64_decode($this->sessionSecret); |
|
| 281 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 282 | - $output = base64_encode($dataEncrypted); |
|
| 283 | - return $output; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Decode the session data using the openssl |
|
| 289 | - * @param mixed $data the data to decode |
|
| 290 | - * @return mixed the decoded data |
|
| 291 | - */ |
|
| 292 | - public function decode($data){ |
|
| 293 | - $key = base64_decode($this->sessionSecret); |
|
| 294 | - $data = base64_decode($data); |
|
| 295 | - $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 296 | - return $data; |
|
| 297 | - } |
|
| 193 | + $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 194 | + if ($data && isset($data->{$columns['sdata']})){ |
|
| 195 | + //checking inactivity |
|
| 196 | + $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 197 | + if ($data->{$columns['stime']} < $timeInactivity){ |
|
| 198 | + $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 199 | + $this->destroy($sid); |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | + return $this->decode($data->{$columns['sdata']}); |
|
| 203 | + } |
|
| 204 | + $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 205 | + return null; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Save the session data |
|
| 210 | + * @param string $sid the session ID |
|
| 211 | + * @param mixed $data the session data to save in serialize format |
|
| 212 | + * @return boolean |
|
| 213 | + */ |
|
| 214 | + public function write($sid, $data){ |
|
| 215 | + $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 216 | + $instance = $this->getModelInstance(); |
|
| 217 | + $columns = $this->sessionTableColumns; |
|
| 218 | + |
|
| 219 | + $this->loader->functions('user_agent'); |
|
| 220 | + $this->loader->library('Browser'); |
|
| 221 | + |
|
| 222 | + $ip = get_ip(); |
|
| 223 | + $keyValue = $instance->getKeyValue(); |
|
| 224 | + $host = @gethostbyaddr($ip) or null; |
|
| 225 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 226 | + $data = $this->encode($data); |
|
| 227 | + $params = array( |
|
| 228 | + $columns['sid'] => $sid, |
|
| 229 | + $columns['sdata'] => $data, |
|
| 230 | + $columns['stime'] => time(), |
|
| 231 | + $columns['shost'] => $host, |
|
| 232 | + $columns['sbrowser'] => $browser, |
|
| 233 | + $columns['sip'] => $ip, |
|
| 234 | + $columns['skey'] => $keyValue |
|
| 235 | + ); |
|
| 236 | + $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 237 | + $exists = $instance->get($sid); |
|
| 238 | + if ($exists){ |
|
| 239 | + $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 240 | + //update |
|
| 241 | + unset($params[$columns['sid']]); |
|
| 242 | + return $instance->update($sid, $params); |
|
| 243 | + } |
|
| 244 | + $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 245 | + return $instance->insert($params); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Destroy the session data for the given session id |
|
| 251 | + * @param string $sid the session id value |
|
| 252 | + * @return boolean |
|
| 253 | + */ |
|
| 254 | + public function destroy($sid){ |
|
| 255 | + $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 256 | + $instance = $this->modelInstance; |
|
| 257 | + $instance->delete($sid); |
|
| 258 | + return true; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Clean the expire session data to save espace |
|
| 263 | + * @param integer $maxLifetime the max lifetime |
|
| 264 | + * @return boolean |
|
| 265 | + */ |
|
| 266 | + public function gc($maxLifetime){ |
|
| 267 | + $instance = $this->modelInstance; |
|
| 268 | + $time = time() - $maxLifetime; |
|
| 269 | + $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 270 | + $instance->deleteByTime($time); |
|
| 271 | + return true; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Encode the session data using the openssl |
|
| 276 | + * @param mixed $data the session data to encode |
|
| 277 | + * @return mixed the encoded session data |
|
| 278 | + */ |
|
| 279 | + public function encode($data){ |
|
| 280 | + $key = base64_decode($this->sessionSecret); |
|
| 281 | + $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 282 | + $output = base64_encode($dataEncrypted); |
|
| 283 | + return $output; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Decode the session data using the openssl |
|
| 289 | + * @param mixed $data the data to decode |
|
| 290 | + * @return mixed the decoded data |
|
| 291 | + */ |
|
| 292 | + public function decode($data){ |
|
| 293 | + $key = base64_decode($this->sessionSecret); |
|
| 294 | + $data = base64_decode($data); |
|
| 295 | + $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 296 | + return $data; |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | 299 | |
| 300 | - /** |
|
| 300 | + /** |
|
| 301 | 301 | * Return the loader instance |
| 302 | 302 | * @return object Loader the loader instance |
| 303 | 303 | */ |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | * set the loader instance for future use |
| 310 | 310 | * @param object Loader $loader the loader object |
| 311 | 311 | */ |
| 312 | - public function setLoader($loader){ |
|
| 312 | + public function setLoader($loader){ |
|
| 313 | 313 | $this->loader = $loader; |
| 314 | 314 | return $this; |
| 315 | 315 | } |
@@ -326,27 +326,27 @@ discard block |
||
| 326 | 326 | * set the model instance for future use |
| 327 | 327 | * @param DBSessionHandlerModel $modelInstance the model object |
| 328 | 328 | */ |
| 329 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 329 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 330 | 330 | $this->modelInstance = $modelInstance; |
| 331 | 331 | return $this; |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - /** |
|
| 335 | - * Set the model instance using the configuration for session |
|
| 336 | - */ |
|
| 337 | - protected function setModelInstanceFromSessionConfig(){ |
|
| 338 | - $modelName = get_config('session_save_path'); |
|
| 339 | - $this->logger->info('The database session model: ' . $modelName); |
|
| 340 | - $this->loader->model($modelName, 'dbsessionhandlerinstance'); |
|
| 341 | - //@codeCoverageIgnoreStart |
|
| 334 | + /** |
|
| 335 | + * Set the model instance using the configuration for session |
|
| 336 | + */ |
|
| 337 | + protected function setModelInstanceFromSessionConfig(){ |
|
| 338 | + $modelName = get_config('session_save_path'); |
|
| 339 | + $this->logger->info('The database session model: ' . $modelName); |
|
| 340 | + $this->loader->model($modelName, 'dbsessionhandlerinstance'); |
|
| 341 | + //@codeCoverageIgnoreStart |
|
| 342 | 342 | if (isset($this->OBJ->dbsessionhandlerinstance) |
| 343 | - && ! ($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 343 | + && ! ($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 344 | 344 | ) { |
| 345 | - show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 346 | - } |
|
| 347 | - //@codeCoverageIgnoreEnd |
|
| 345 | + show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
| 346 | + } |
|
| 347 | + //@codeCoverageIgnoreEnd |
|
| 348 | 348 | |
| 349 | - //set model instance |
|
| 350 | - $this->modelInstance = $this->OBJ->dbsessionhandlerinstance; |
|
| 351 | - } |
|
| 352 | - } |
|
| 349 | + //set model instance |
|
| 350 | + $this->modelInstance = $this->OBJ->dbsessionhandlerinstance; |
|
| 351 | + } |
|
| 352 | + } |
|
@@ -27,11 +27,11 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
| 29 | 29 | */ |
| 30 | - if ( !interface_exists('SessionHandlerInterface')){ |
|
| 30 | + if (!interface_exists('SessionHandlerInterface')) { |
|
| 31 | 31 | show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - class DBSessionHandler extends BaseClass implements SessionHandlerInterface{ |
|
| 34 | + class DBSessionHandler extends BaseClass implements SessionHandlerInterface { |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The encryption method to use to encrypt session data in database |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | * Create new instance of Database session handler |
| 80 | 80 | * @param object $modelInstance the model instance |
| 81 | 81 | */ |
| 82 | - public function __construct(DBSessionHandlerModel $modelInstance = null){ |
|
| 82 | + public function __construct(DBSessionHandlerModel $modelInstance = null) { |
|
| 83 | 83 | parent::__construct(); |
| 84 | 84 | |
| 85 | 85 | //Set Loader instance to use |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | $this->OBJ = & get_instance(); |
| 89 | 89 | |
| 90 | - if (is_object($modelInstance)){ |
|
| 90 | + if (is_object($modelInstance)) { |
|
| 91 | 91 | $this->setModelInstance($modelInstance); |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * Set the session secret used to encrypt the data in database |
| 97 | 97 | * @param string $secret the base64 string secret |
| 98 | 98 | */ |
| 99 | - public function setSessionSecret($secret){ |
|
| 99 | + public function setSessionSecret($secret) { |
|
| 100 | 100 | $this->sessionSecret = $secret; |
| 101 | 101 | return $this; |
| 102 | 102 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * Return the session secret |
| 106 | 106 | * @return string |
| 107 | 107 | */ |
| 108 | - public function getSessionSecret(){ |
|
| 108 | + public function getSessionSecret() { |
|
| 109 | 109 | return $this->sessionSecret; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | * Set the initializer vector for openssl |
| 115 | 115 | * @param string $key the session secret used in base64 format |
| 116 | 116 | */ |
| 117 | - public function setInitializerVector($key){ |
|
| 117 | + public function setInitializerVector($key) { |
|
| 118 | 118 | $ivLength = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
| 119 | 119 | $key = base64_decode($key); |
| 120 | 120 | $this->iv = substr(hash('sha256', $key), 0, $ivLength); |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | * Return the initializer vector |
| 126 | 126 | * @return string |
| 127 | 127 | */ |
| 128 | - public function getInitializerVector(){ |
|
| 128 | + public function getInitializerVector() { |
|
| 129 | 129 | return $this->iv; |
| 130 | 130 | } |
| 131 | 131 | |
@@ -135,17 +135,17 @@ discard block |
||
| 135 | 135 | * @param string $sessionName the session name |
| 136 | 136 | * @return boolean |
| 137 | 137 | */ |
| 138 | - public function open($savePath, $sessionName){ |
|
| 138 | + public function open($savePath, $sessionName) { |
|
| 139 | 139 | $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
| 140 | 140 | //try to check if session secret is set before |
| 141 | 141 | $secret = $this->getSessionSecret(); |
| 142 | - if (empty($secret)){ |
|
| 142 | + if (empty($secret)) { |
|
| 143 | 143 | $secret = get_config('session_secret', null); |
| 144 | 144 | $this->setSessionSecret($secret); |
| 145 | 145 | } |
| 146 | 146 | $this->logger->info('Session secret: ' . $secret); |
| 147 | 147 | |
| 148 | - if (! is_object($this->modelInstance)){ |
|
| 148 | + if (!is_object($this->modelInstance)) { |
|
| 149 | 149 | $this->setModelInstanceFromSessionConfig(); |
| 150 | 150 | } |
| 151 | 151 | $this->setInitializerVector($secret); |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | //set session tables columns |
| 154 | 154 | $this->sessionTableColumns = $this->modelInstance->getSessionTableColumns(); |
| 155 | 155 | |
| 156 | - if (empty($this->sessionTableColumns)){ |
|
| 156 | + if (empty($this->sessionTableColumns)) { |
|
| 157 | 157 | show_error('The session handler is "database" but the table columns not set'); |
| 158 | 158 | } |
| 159 | 159 | $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * Close the session |
| 170 | 170 | * @return boolean |
| 171 | 171 | */ |
| 172 | - public function close(){ |
|
| 172 | + public function close() { |
|
| 173 | 173 | $this->logger->debug('Closing database session handler'); |
| 174 | 174 | return true; |
| 175 | 175 | } |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | * @param string $sid the session id to use |
| 180 | 180 | * @return string the session data in serialiaze format |
| 181 | 181 | */ |
| 182 | - public function read($sid){ |
|
| 182 | + public function read($sid) { |
|
| 183 | 183 | $this->logger->debug('Reading database session data for SID: ' . $sid); |
| 184 | 184 | $instance = $this->getModelInstance(); |
| 185 | 185 | $columns = $this->sessionTableColumns; |
@@ -188,13 +188,13 @@ discard block |
||
| 188 | 188 | |
| 189 | 189 | $ip = get_ip(); |
| 190 | 190 | $host = @gethostbyaddr($ip) or null; |
| 191 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 191 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 192 | 192 | |
| 193 | 193 | $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
| 194 | - if ($data && isset($data->{$columns['sdata']})){ |
|
| 194 | + if ($data && isset($data->{$columns['sdata']})) { |
|
| 195 | 195 | //checking inactivity |
| 196 | 196 | $timeInactivity = time() - get_config('session_inactivity_time', 100); |
| 197 | - if ($data->{$columns['stime']} < $timeInactivity){ |
|
| 197 | + if ($data->{$columns['stime']} < $timeInactivity) { |
|
| 198 | 198 | $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
| 199 | 199 | $this->destroy($sid); |
| 200 | 200 | return null; |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | * @param mixed $data the session data to save in serialize format |
| 212 | 212 | * @return boolean |
| 213 | 213 | */ |
| 214 | - public function write($sid, $data){ |
|
| 214 | + public function write($sid, $data) { |
|
| 215 | 215 | $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
| 216 | 216 | $instance = $this->getModelInstance(); |
| 217 | 217 | $columns = $this->sessionTableColumns; |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | $ip = get_ip(); |
| 223 | 223 | $keyValue = $instance->getKeyValue(); |
| 224 | 224 | $host = @gethostbyaddr($ip) or null; |
| 225 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 225 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
| 226 | 226 | $data = $this->encode($data); |
| 227 | 227 | $params = array( |
| 228 | 228 | $columns['sid'] => $sid, |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | ); |
| 236 | 236 | $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
| 237 | 237 | $exists = $instance->get($sid); |
| 238 | - if ($exists){ |
|
| 238 | + if ($exists) { |
|
| 239 | 239 | $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
| 240 | 240 | //update |
| 241 | 241 | unset($params[$columns['sid']]); |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | * @param string $sid the session id value |
| 252 | 252 | * @return boolean |
| 253 | 253 | */ |
| 254 | - public function destroy($sid){ |
|
| 254 | + public function destroy($sid) { |
|
| 255 | 255 | $this->logger->debug('Destroy of session data for SID: ' . $sid); |
| 256 | 256 | $instance = $this->modelInstance; |
| 257 | 257 | $instance->delete($sid); |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | * @param integer $maxLifetime the max lifetime |
| 264 | 264 | * @return boolean |
| 265 | 265 | */ |
| 266 | - public function gc($maxLifetime){ |
|
| 266 | + public function gc($maxLifetime) { |
|
| 267 | 267 | $instance = $this->modelInstance; |
| 268 | 268 | $time = time() - $maxLifetime; |
| 269 | 269 | $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
@@ -276,9 +276,9 @@ discard block |
||
| 276 | 276 | * @param mixed $data the session data to encode |
| 277 | 277 | * @return mixed the encoded session data |
| 278 | 278 | */ |
| 279 | - public function encode($data){ |
|
| 279 | + public function encode($data) { |
|
| 280 | 280 | $key = base64_decode($this->sessionSecret); |
| 281 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 281 | + $dataEncrypted = openssl_encrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 282 | 282 | $output = base64_encode($dataEncrypted); |
| 283 | 283 | return $output; |
| 284 | 284 | } |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | * @param mixed $data the data to decode |
| 290 | 290 | * @return mixed the decoded data |
| 291 | 291 | */ |
| 292 | - public function decode($data){ |
|
| 292 | + public function decode($data) { |
|
| 293 | 293 | $key = base64_decode($this->sessionSecret); |
| 294 | 294 | $data = base64_decode($data); |
| 295 | 295 | $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * Return the loader instance |
| 302 | 302 | * @return object Loader the loader instance |
| 303 | 303 | */ |
| 304 | - public function getLoader(){ |
|
| 304 | + public function getLoader() { |
|
| 305 | 305 | return $this->loader; |
| 306 | 306 | } |
| 307 | 307 | |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | * set the loader instance for future use |
| 310 | 310 | * @param object Loader $loader the loader object |
| 311 | 311 | */ |
| 312 | - public function setLoader($loader){ |
|
| 312 | + public function setLoader($loader) { |
|
| 313 | 313 | $this->loader = $loader; |
| 314 | 314 | return $this; |
| 315 | 315 | } |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * Return the model instance |
| 319 | 319 | * @return object DBSessionHandlerModel the model instance |
| 320 | 320 | */ |
| 321 | - public function getModelInstance(){ |
|
| 321 | + public function getModelInstance() { |
|
| 322 | 322 | return $this->modelInstance; |
| 323 | 323 | } |
| 324 | 324 | |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | * set the model instance for future use |
| 327 | 327 | * @param DBSessionHandlerModel $modelInstance the model object |
| 328 | 328 | */ |
| 329 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 329 | + public function setModelInstance(DBSessionHandlerModel $modelInstance) { |
|
| 330 | 330 | $this->modelInstance = $modelInstance; |
| 331 | 331 | return $this; |
| 332 | 332 | } |
@@ -334,13 +334,13 @@ discard block |
||
| 334 | 334 | /** |
| 335 | 335 | * Set the model instance using the configuration for session |
| 336 | 336 | */ |
| 337 | - protected function setModelInstanceFromSessionConfig(){ |
|
| 337 | + protected function setModelInstanceFromSessionConfig() { |
|
| 338 | 338 | $modelName = get_config('session_save_path'); |
| 339 | 339 | $this->logger->info('The database session model: ' . $modelName); |
| 340 | 340 | $this->loader->model($modelName, 'dbsessionhandlerinstance'); |
| 341 | 341 | //@codeCoverageIgnoreStart |
| 342 | 342 | if (isset($this->OBJ->dbsessionhandlerinstance) |
| 343 | - && ! ($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 343 | + && !($this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) |
|
| 344 | 344 | ) { |
| 345 | 345 | show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
| 346 | 346 | } |
@@ -1,201 +1,201 @@ |
||
| 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 | - |
|
| 27 | - /** |
|
| 28 | - * For application languages management |
|
| 29 | - */ |
|
| 30 | - class Lang extends BaseClass{ |
|
| 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 | + |
|
| 27 | + /** |
|
| 28 | + * For application languages management |
|
| 29 | + */ |
|
| 30 | + class Lang extends BaseClass{ |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The supported available language for this application. |
|
| 34 | - * @example "en" => "english" |
|
| 35 | - * @see Lang::addLang() |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $availables = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The all messages language |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $languages = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The default language to use if can not |
|
| 48 | - * determine the client language |
|
| 49 | - * |
|
| 50 | - * @example $default = 'en' |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $default = null; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The current client language |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $current = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Construct new Lang instance |
|
| 63 | - */ |
|
| 64 | - public function __construct(){ |
|
| 65 | - parent::__construct(); |
|
| 66 | - |
|
| 67 | - $this->default = get_config('default_language', 'en'); |
|
| 68 | - $this->logger->debug('Setting the supported languages'); |
|
| 32 | + /** |
|
| 33 | + * The supported available language for this application. |
|
| 34 | + * @example "en" => "english" |
|
| 35 | + * @see Lang::addLang() |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $availables = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The all messages language |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $languages = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The default language to use if can not |
|
| 48 | + * determine the client language |
|
| 49 | + * |
|
| 50 | + * @example $default = 'en' |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $default = null; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The current client language |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $current = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Construct new Lang instance |
|
| 63 | + */ |
|
| 64 | + public function __construct(){ |
|
| 65 | + parent::__construct(); |
|
| 66 | + |
|
| 67 | + $this->default = get_config('default_language', 'en'); |
|
| 68 | + $this->logger->debug('Setting the supported languages'); |
|
| 69 | 69 | |
| 70 | - //add the supported languages ('key', 'display name') |
|
| 71 | - $languages = get_config('languages', null); |
|
| 72 | - if(! empty($languages)){ |
|
| 73 | - foreach($languages as $key => $displayName){ |
|
| 74 | - $this->addLang($key, $displayName); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - unset($languages); |
|
| 78 | - |
|
| 79 | - //if the language exists in cookie use it |
|
| 80 | - $cfgKey = get_config('language_cookie_name'); |
|
| 81 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | - $objCookie = & class_loader('Cookie'); |
|
| 83 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | - $this->current = $cookieLang; |
|
| 86 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | - } |
|
| 88 | - else{ |
|
| 89 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 90 | - $this->current = $this->getDefault(); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Get the all languages messages |
|
| 96 | - * |
|
| 97 | - * @return array the language message list |
|
| 98 | - */ |
|
| 99 | - public function getAll(){ |
|
| 100 | - return $this->languages; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Set the language message |
|
| 105 | - * |
|
| 106 | - * @param string $key the language key to identify |
|
| 107 | - * @param string $value the language message value |
|
| 108 | - */ |
|
| 109 | - public function set($key, $value){ |
|
| 110 | - $this->languages[$key] = $value; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the language message for the given key. If can't find return the default value |
|
| 115 | - * |
|
| 116 | - * @param string $key the message language key |
|
| 117 | - * @param string $default the default value to return if can not found the language message key |
|
| 118 | - * |
|
| 119 | - * @return string the language message value |
|
| 120 | - */ |
|
| 121 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 122 | - if(isset($this->languages[$key])){ |
|
| 123 | - return $this->languages[$key]; |
|
| 124 | - } |
|
| 125 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 126 | - return $default; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Check whether the language file for given name exists |
|
| 131 | - * |
|
| 132 | - * @param string $language the language name like "fr", "en", etc. |
|
| 133 | - * |
|
| 134 | - * @return boolean true if the language directory exists, false or not |
|
| 135 | - */ |
|
| 136 | - public function isValid($language){ |
|
| 137 | - $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 138 | - foreach($searchDir as $dir){ |
|
| 139 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 140 | - return true; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the default language value like "en" , "fr", etc. |
|
| 148 | - * |
|
| 149 | - * @return string the default language |
|
| 150 | - */ |
|
| 151 | - public function getDefault(){ |
|
| 152 | - return $this->default; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Get the current language defined by cookie or the default value |
|
| 157 | - * |
|
| 158 | - * @return string the current language |
|
| 159 | - */ |
|
| 160 | - public function getCurrent(){ |
|
| 161 | - return $this->current; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Add new supported or available language |
|
| 166 | - * |
|
| 167 | - * @param string $name the short language name like "en", "fr". |
|
| 168 | - * @param string $description the human readable description of this language |
|
| 169 | - */ |
|
| 170 | - public function addLang($name, $description){ |
|
| 171 | - if(isset($this->availables[$name])){ |
|
| 172 | - return; //already added cost in performance |
|
| 173 | - } |
|
| 174 | - if($this->isValid($name)){ |
|
| 175 | - $this->availables[$name] = $description; |
|
| 176 | - } |
|
| 177 | - else{ |
|
| 178 | - show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the list of the application supported language |
|
| 184 | - * |
|
| 185 | - * @return array the list of the application language |
|
| 186 | - */ |
|
| 187 | - public function getSupported(){ |
|
| 188 | - return $this->availables; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Add new language messages |
|
| 193 | - * |
|
| 194 | - * @param array $langs the languages array of the messages to be added |
|
| 195 | - */ |
|
| 196 | - public function addLangMessages(array $langs){ |
|
| 197 | - foreach ($langs as $key => $value) { |
|
| 198 | - $this->set($key, $value); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 70 | + //add the supported languages ('key', 'display name') |
|
| 71 | + $languages = get_config('languages', null); |
|
| 72 | + if(! empty($languages)){ |
|
| 73 | + foreach($languages as $key => $displayName){ |
|
| 74 | + $this->addLang($key, $displayName); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + unset($languages); |
|
| 78 | + |
|
| 79 | + //if the language exists in cookie use it |
|
| 80 | + $cfgKey = get_config('language_cookie_name'); |
|
| 81 | + $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 82 | + $objCookie = & class_loader('Cookie'); |
|
| 83 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 84 | + if($cookieLang && $this->isValid($cookieLang)){ |
|
| 85 | + $this->current = $cookieLang; |
|
| 86 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 87 | + } |
|
| 88 | + else{ |
|
| 89 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 90 | + $this->current = $this->getDefault(); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Get the all languages messages |
|
| 96 | + * |
|
| 97 | + * @return array the language message list |
|
| 98 | + */ |
|
| 99 | + public function getAll(){ |
|
| 100 | + return $this->languages; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Set the language message |
|
| 105 | + * |
|
| 106 | + * @param string $key the language key to identify |
|
| 107 | + * @param string $value the language message value |
|
| 108 | + */ |
|
| 109 | + public function set($key, $value){ |
|
| 110 | + $this->languages[$key] = $value; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the language message for the given key. If can't find return the default value |
|
| 115 | + * |
|
| 116 | + * @param string $key the message language key |
|
| 117 | + * @param string $default the default value to return if can not found the language message key |
|
| 118 | + * |
|
| 119 | + * @return string the language message value |
|
| 120 | + */ |
|
| 121 | + public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 122 | + if(isset($this->languages[$key])){ |
|
| 123 | + return $this->languages[$key]; |
|
| 124 | + } |
|
| 125 | + $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 126 | + return $default; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Check whether the language file for given name exists |
|
| 131 | + * |
|
| 132 | + * @param string $language the language name like "fr", "en", etc. |
|
| 133 | + * |
|
| 134 | + * @return boolean true if the language directory exists, false or not |
|
| 135 | + */ |
|
| 136 | + public function isValid($language){ |
|
| 137 | + $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 138 | + foreach($searchDir as $dir){ |
|
| 139 | + if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 140 | + return true; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the default language value like "en" , "fr", etc. |
|
| 148 | + * |
|
| 149 | + * @return string the default language |
|
| 150 | + */ |
|
| 151 | + public function getDefault(){ |
|
| 152 | + return $this->default; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Get the current language defined by cookie or the default value |
|
| 157 | + * |
|
| 158 | + * @return string the current language |
|
| 159 | + */ |
|
| 160 | + public function getCurrent(){ |
|
| 161 | + return $this->current; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Add new supported or available language |
|
| 166 | + * |
|
| 167 | + * @param string $name the short language name like "en", "fr". |
|
| 168 | + * @param string $description the human readable description of this language |
|
| 169 | + */ |
|
| 170 | + public function addLang($name, $description){ |
|
| 171 | + if(isset($this->availables[$name])){ |
|
| 172 | + return; //already added cost in performance |
|
| 173 | + } |
|
| 174 | + if($this->isValid($name)){ |
|
| 175 | + $this->availables[$name] = $description; |
|
| 176 | + } |
|
| 177 | + else{ |
|
| 178 | + show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the list of the application supported language |
|
| 184 | + * |
|
| 185 | + * @return array the list of the application language |
|
| 186 | + */ |
|
| 187 | + public function getSupported(){ |
|
| 188 | + return $this->availables; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Add new language messages |
|
| 193 | + * |
|
| 194 | + * @param array $langs the languages array of the messages to be added |
|
| 195 | + */ |
|
| 196 | + public function addLangMessages(array $langs){ |
|
| 197 | + foreach ($langs as $key => $value) { |
|
| 198 | + $this->set($key, $value); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * For application languages management |
| 29 | 29 | */ |
| 30 | - class Lang extends BaseClass{ |
|
| 30 | + class Lang extends BaseClass { |
|
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * The supported available language for this application. |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | /** |
| 62 | 62 | * Construct new Lang instance |
| 63 | 63 | */ |
| 64 | - public function __construct(){ |
|
| 64 | + public function __construct() { |
|
| 65 | 65 | parent::__construct(); |
| 66 | 66 | |
| 67 | 67 | $this->default = get_config('default_language', 'en'); |
@@ -69,8 +69,8 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | //add the supported languages ('key', 'display name') |
| 71 | 71 | $languages = get_config('languages', null); |
| 72 | - if(! empty($languages)){ |
|
| 73 | - foreach($languages as $key => $displayName){ |
|
| 72 | + if (!empty($languages)) { |
|
| 73 | + foreach ($languages as $key => $displayName) { |
|
| 74 | 74 | $this->addLang($key, $displayName); |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -78,15 +78,15 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | //if the language exists in cookie use it |
| 80 | 80 | $cfgKey = get_config('language_cookie_name'); |
| 81 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 81 | + $this->logger->debug('Getting current language from cookie [' . $cfgKey . ']'); |
|
| 82 | 82 | $objCookie = & class_loader('Cookie'); |
| 83 | 83 | $cookieLang = $objCookie->get($cfgKey); |
| 84 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 84 | + if ($cookieLang && $this->isValid($cookieLang)) { |
|
| 85 | 85 | $this->current = $cookieLang; |
| 86 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 86 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is valid so we will set the language using the cookie value [' . $cookieLang . ']'); |
|
| 87 | 87 | } |
| 88 | - else{ |
|
| 89 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 88 | + else { |
|
| 89 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is not set, use the default value [' . $this->getDefault() . ']'); |
|
| 90 | 90 | $this->current = $this->getDefault(); |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * |
| 97 | 97 | * @return array the language message list |
| 98 | 98 | */ |
| 99 | - public function getAll(){ |
|
| 99 | + public function getAll() { |
|
| 100 | 100 | return $this->languages; |
| 101 | 101 | } |
| 102 | 102 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * @param string $key the language key to identify |
| 107 | 107 | * @param string $value the language message value |
| 108 | 108 | */ |
| 109 | - public function set($key, $value){ |
|
| 109 | + public function set($key, $value) { |
|
| 110 | 110 | $this->languages[$key] = $value; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -118,11 +118,11 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @return string the language message value |
| 120 | 120 | */ |
| 121 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 122 | - if(isset($this->languages[$key])){ |
|
| 121 | + public function get($key, $default = 'LANGUAGE_ERROR') { |
|
| 122 | + if (isset($this->languages[$key])) { |
|
| 123 | 123 | return $this->languages[$key]; |
| 124 | 124 | } |
| 125 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 125 | + $this->logger->warning('Language key [' . $key . '] does not exist use the default value [' . $default . ']'); |
|
| 126 | 126 | return $default; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return boolean true if the language directory exists, false or not |
| 135 | 135 | */ |
| 136 | - public function isValid($language){ |
|
| 136 | + public function isValid($language) { |
|
| 137 | 137 | $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
| 138 | - foreach($searchDir as $dir){ |
|
| 139 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 138 | + foreach ($searchDir as $dir) { |
|
| 139 | + if (file_exists($dir . $language) && is_dir($dir . $language)) { |
|
| 140 | 140 | return true; |
| 141 | 141 | } |
| 142 | 142 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return string the default language |
| 150 | 150 | */ |
| 151 | - public function getDefault(){ |
|
| 151 | + public function getDefault() { |
|
| 152 | 152 | return $this->default; |
| 153 | 153 | } |
| 154 | 154 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * @return string the current language |
| 159 | 159 | */ |
| 160 | - public function getCurrent(){ |
|
| 160 | + public function getCurrent() { |
|
| 161 | 161 | return $this->current; |
| 162 | 162 | } |
| 163 | 163 | |
@@ -167,14 +167,14 @@ discard block |
||
| 167 | 167 | * @param string $name the short language name like "en", "fr". |
| 168 | 168 | * @param string $description the human readable description of this language |
| 169 | 169 | */ |
| 170 | - public function addLang($name, $description){ |
|
| 171 | - if(isset($this->availables[$name])){ |
|
| 170 | + public function addLang($name, $description) { |
|
| 171 | + if (isset($this->availables[$name])) { |
|
| 172 | 172 | return; //already added cost in performance |
| 173 | 173 | } |
| 174 | - if($this->isValid($name)){ |
|
| 174 | + if ($this->isValid($name)) { |
|
| 175 | 175 | $this->availables[$name] = $description; |
| 176 | 176 | } |
| 177 | - else{ |
|
| 177 | + else { |
|
| 178 | 178 | show_error('The language [' . $name . '] is not valid or does not exists.'); |
| 179 | 179 | } |
| 180 | 180 | } |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | * |
| 185 | 185 | * @return array the list of the application language |
| 186 | 186 | */ |
| 187 | - public function getSupported(){ |
|
| 187 | + public function getSupported() { |
|
| 188 | 188 | return $this->availables; |
| 189 | 189 | } |
| 190 | 190 | |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | * |
| 194 | 194 | * @param array $langs the languages array of the messages to be added |
| 195 | 195 | */ |
| 196 | - public function addLangMessages(array $langs){ |
|
| 196 | + public function addLangMessages(array $langs) { |
|
| 197 | 197 | foreach ($langs as $key => $value) { |
| 198 | 198 | $this->set($key, $value); |
| 199 | 199 | } |
@@ -1,586 +1,586 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - class Router extends BaseClass{ |
|
| 28 | - /** |
|
| 29 | - * @var array $pattern: The list of URIs to validate against |
|
| 30 | - */ |
|
| 31 | - private $pattern = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $callback: The list of callback to call |
|
| 35 | - */ |
|
| 36 | - private $callback = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | - */ |
|
| 41 | - protected $uriTrim = '/\^$'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $uri: The route URI to use |
|
| 45 | - */ |
|
| 46 | - protected $uri = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The module name of the current request |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $module = null; |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + class Router extends BaseClass{ |
|
| 28 | + /** |
|
| 29 | + * @var array $pattern: The list of URIs to validate against |
|
| 30 | + */ |
|
| 31 | + private $pattern = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $callback: The list of callback to call |
|
| 35 | + */ |
|
| 36 | + private $callback = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string $uriTrim: The char to remove from the URIs |
|
| 40 | + */ |
|
| 41 | + protected $uriTrim = '/\^$'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $uri: The route URI to use |
|
| 45 | + */ |
|
| 46 | + protected $uri = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The module name of the current request |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $module = null; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * The controller name of the current request |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - protected $controller = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The controller path |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - protected $controllerPath = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The method name. The default value is "index" |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - protected $method = 'index'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * List of argument to pass to the method |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $args = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * List of routes configurations |
|
| 80 | - * @var array |
|
| 81 | - */ |
|
| 82 | - protected $routes = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The segments array for the current request |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected $segments = array(); |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Construct the new Router instance |
|
| 92 | - */ |
|
| 93 | - public function __construct(){ |
|
| 94 | - parent::__construct(); |
|
| 54 | + /** |
|
| 55 | + * The controller name of the current request |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + protected $controller = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The controller path |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + protected $controllerPath = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The method name. The default value is "index" |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + protected $method = 'index'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * List of argument to pass to the method |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $args = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * List of routes configurations |
|
| 80 | + * @var array |
|
| 81 | + */ |
|
| 82 | + protected $routes = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The segments array for the current request |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected $segments = array(); |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Construct the new Router instance |
|
| 92 | + */ |
|
| 93 | + public function __construct(){ |
|
| 94 | + parent::__construct(); |
|
| 95 | 95 | |
| 96 | - //loading routes for module |
|
| 97 | - $moduleRouteList = array(); |
|
| 98 | - $modulesRoutes = Module::getModulesRoutesConfig(); |
|
| 99 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 100 | - $moduleRouteList = $modulesRoutes; |
|
| 101 | - unset($modulesRoutes); |
|
| 102 | - } |
|
| 103 | - $this->setRouteConfiguration($moduleRouteList); |
|
| 104 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 105 | - |
|
| 106 | - //Set route informations |
|
| 107 | - $this->setRouteConfigurationInfos(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Get the route patterns |
|
| 112 | - * @return array |
|
| 113 | - */ |
|
| 114 | - public function getPattern(){ |
|
| 115 | - return $this->pattern; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get the route callbacks |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function getCallback(){ |
|
| 123 | - return $this->callback; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get the module name |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getModule(){ |
|
| 131 | - return $this->module; |
|
| 132 | - } |
|
| 96 | + //loading routes for module |
|
| 97 | + $moduleRouteList = array(); |
|
| 98 | + $modulesRoutes = Module::getModulesRoutesConfig(); |
|
| 99 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 100 | + $moduleRouteList = $modulesRoutes; |
|
| 101 | + unset($modulesRoutes); |
|
| 102 | + } |
|
| 103 | + $this->setRouteConfiguration($moduleRouteList); |
|
| 104 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 105 | + |
|
| 106 | + //Set route informations |
|
| 107 | + $this->setRouteConfigurationInfos(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Get the route patterns |
|
| 112 | + * @return array |
|
| 113 | + */ |
|
| 114 | + public function getPattern(){ |
|
| 115 | + return $this->pattern; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get the route callbacks |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function getCallback(){ |
|
| 123 | + return $this->callback; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get the module name |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getModule(){ |
|
| 131 | + return $this->module; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the controller name |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getController(){ |
|
| 139 | - return $this->controller; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get the controller file path |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - public function getControllerPath(){ |
|
| 147 | - return $this->controllerPath; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get the controller method |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getMethod(){ |
|
| 155 | - return $this->method; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get the request arguments |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function getArgs(){ |
|
| 163 | - return $this->args; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the URL segments array |
|
| 168 | - * @return array |
|
| 169 | - */ |
|
| 170 | - public function getSegments(){ |
|
| 171 | - return $this->segments; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get the route URI |
|
| 176 | - * @return string |
|
| 177 | - */ |
|
| 178 | - public function getRouteUri(){ |
|
| 179 | - return $this->uri; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Add the URI and callback to the list of URIs to validate |
|
| 184 | - * |
|
| 185 | - * @param string $uri the request URI |
|
| 186 | - * @param string $callback the callback function |
|
| 187 | - * |
|
| 188 | - * @return object the current instance |
|
| 189 | - */ |
|
| 190 | - public function add($uri, $callback) { |
|
| 191 | - $uri = trim($uri, $this->uriTrim); |
|
| 192 | - if(in_array($uri, $this->pattern)){ |
|
| 193 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 194 | - } |
|
| 195 | - $this->pattern[] = $uri; |
|
| 196 | - $this->callback[] = $callback; |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Remove the route configuration |
|
| 202 | - * |
|
| 203 | - * @param string $uri the URI |
|
| 204 | - * |
|
| 205 | - * @return object the current instance |
|
| 206 | - */ |
|
| 207 | - public function removeRoute($uri) { |
|
| 208 | - $index = array_search($uri, $this->pattern, true); |
|
| 209 | - if($index !== false){ |
|
| 210 | - $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 211 | - unset($this->pattern[$index]); |
|
| 212 | - unset($this->callback[$index]); |
|
| 213 | - } |
|
| 214 | - return $this; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Remove all the routes from the configuration |
|
| 220 | - * |
|
| 221 | - * @return object the current instance |
|
| 222 | - */ |
|
| 223 | - public function removeAllRoute() { |
|
| 224 | - $this->logger->info('Remove all routes from the configuration'); |
|
| 225 | - $this->pattern = array(); |
|
| 226 | - $this->callback = array(); |
|
| 227 | - $this->routes = array(); |
|
| 228 | - return $this; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Set the route URI to use later |
|
| 234 | - * @param string $uri the route URI, if is empty will determine automatically |
|
| 235 | - * @return object |
|
| 236 | - */ |
|
| 237 | - public function setRouteUri($uri = ''){ |
|
| 238 | - $routeUri = ''; |
|
| 239 | - if(! empty($uri)){ |
|
| 240 | - $routeUri = $uri; |
|
| 241 | - } |
|
| 242 | - //if the application is running in CLI mode use the first argument |
|
| 243 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 244 | - $routeUri = $_SERVER['argv'][1]; |
|
| 245 | - } |
|
| 246 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 247 | - $routeUri = $_SERVER['REQUEST_URI']; |
|
| 248 | - } |
|
| 249 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 250 | - //remove url suffix from the request URI |
|
| 251 | - $suffix = get_config('url_suffix'); |
|
| 252 | - if ($suffix) { |
|
| 253 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 254 | - $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 255 | - } |
|
| 256 | - if (strpos($routeUri, '?') !== false){ |
|
| 257 | - $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 258 | - } |
|
| 259 | - $this->uri = trim($routeUri, $this->uriTrim); |
|
| 260 | - return $this; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set the route segments informations |
|
| 265 | - * @param array $segements the route segments information |
|
| 266 | - * |
|
| 267 | - * @return object |
|
| 268 | - */ |
|
| 269 | - public function setRouteSegments(array $segments = array()){ |
|
| 270 | - if(! empty($segments)){ |
|
| 271 | - $this->segments = $segments; |
|
| 272 | - } else if (!empty($this->uri)) { |
|
| 273 | - $this->segments = explode('/', $this->uri); |
|
| 274 | - } |
|
| 275 | - $segment = $this->segments; |
|
| 276 | - $baseUrl = get_config('base_url'); |
|
| 277 | - //check if the app is not in DOCUMENT_ROOT |
|
| 278 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 279 | - array_shift($segment); |
|
| 280 | - $this->segments = $segment; |
|
| 281 | - } |
|
| 282 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 283 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 284 | - $this->logger->info('The request URI contains the front controller'); |
|
| 285 | - array_shift($segment); |
|
| 286 | - $this->segments = $segment; |
|
| 287 | - } |
|
| 288 | - return $this; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Setting the route parameters like module, controller, method, argument |
|
| 293 | - * @return object the current instance |
|
| 294 | - */ |
|
| 295 | - public function determineRouteParamsInformation() { |
|
| 296 | - $this->logger->debug('Routing process start ...'); |
|
| 134 | + /** |
|
| 135 | + * Get the controller name |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getController(){ |
|
| 139 | + return $this->controller; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get the controller file path |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + public function getControllerPath(){ |
|
| 147 | + return $this->controllerPath; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get the controller method |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getMethod(){ |
|
| 155 | + return $this->method; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get the request arguments |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function getArgs(){ |
|
| 163 | + return $this->args; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the URL segments array |
|
| 168 | + * @return array |
|
| 169 | + */ |
|
| 170 | + public function getSegments(){ |
|
| 171 | + return $this->segments; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get the route URI |
|
| 176 | + * @return string |
|
| 177 | + */ |
|
| 178 | + public function getRouteUri(){ |
|
| 179 | + return $this->uri; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Add the URI and callback to the list of URIs to validate |
|
| 184 | + * |
|
| 185 | + * @param string $uri the request URI |
|
| 186 | + * @param string $callback the callback function |
|
| 187 | + * |
|
| 188 | + * @return object the current instance |
|
| 189 | + */ |
|
| 190 | + public function add($uri, $callback) { |
|
| 191 | + $uri = trim($uri, $this->uriTrim); |
|
| 192 | + if(in_array($uri, $this->pattern)){ |
|
| 193 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 194 | + } |
|
| 195 | + $this->pattern[] = $uri; |
|
| 196 | + $this->callback[] = $callback; |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Remove the route configuration |
|
| 202 | + * |
|
| 203 | + * @param string $uri the URI |
|
| 204 | + * |
|
| 205 | + * @return object the current instance |
|
| 206 | + */ |
|
| 207 | + public function removeRoute($uri) { |
|
| 208 | + $index = array_search($uri, $this->pattern, true); |
|
| 209 | + if($index !== false){ |
|
| 210 | + $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
|
| 211 | + unset($this->pattern[$index]); |
|
| 212 | + unset($this->callback[$index]); |
|
| 213 | + } |
|
| 214 | + return $this; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Remove all the routes from the configuration |
|
| 220 | + * |
|
| 221 | + * @return object the current instance |
|
| 222 | + */ |
|
| 223 | + public function removeAllRoute() { |
|
| 224 | + $this->logger->info('Remove all routes from the configuration'); |
|
| 225 | + $this->pattern = array(); |
|
| 226 | + $this->callback = array(); |
|
| 227 | + $this->routes = array(); |
|
| 228 | + return $this; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Set the route URI to use later |
|
| 234 | + * @param string $uri the route URI, if is empty will determine automatically |
|
| 235 | + * @return object |
|
| 236 | + */ |
|
| 237 | + public function setRouteUri($uri = ''){ |
|
| 238 | + $routeUri = ''; |
|
| 239 | + if(! empty($uri)){ |
|
| 240 | + $routeUri = $uri; |
|
| 241 | + } |
|
| 242 | + //if the application is running in CLI mode use the first argument |
|
| 243 | + else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 244 | + $routeUri = $_SERVER['argv'][1]; |
|
| 245 | + } |
|
| 246 | + else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 247 | + $routeUri = $_SERVER['REQUEST_URI']; |
|
| 248 | + } |
|
| 249 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 250 | + //remove url suffix from the request URI |
|
| 251 | + $suffix = get_config('url_suffix'); |
|
| 252 | + if ($suffix) { |
|
| 253 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 254 | + $routeUri = str_ireplace($suffix, '', $routeUri); |
|
| 255 | + } |
|
| 256 | + if (strpos($routeUri, '?') !== false){ |
|
| 257 | + $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
|
| 258 | + } |
|
| 259 | + $this->uri = trim($routeUri, $this->uriTrim); |
|
| 260 | + return $this; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set the route segments informations |
|
| 265 | + * @param array $segements the route segments information |
|
| 266 | + * |
|
| 267 | + * @return object |
|
| 268 | + */ |
|
| 269 | + public function setRouteSegments(array $segments = array()){ |
|
| 270 | + if(! empty($segments)){ |
|
| 271 | + $this->segments = $segments; |
|
| 272 | + } else if (!empty($this->uri)) { |
|
| 273 | + $this->segments = explode('/', $this->uri); |
|
| 274 | + } |
|
| 275 | + $segment = $this->segments; |
|
| 276 | + $baseUrl = get_config('base_url'); |
|
| 277 | + //check if the app is not in DOCUMENT_ROOT |
|
| 278 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 279 | + array_shift($segment); |
|
| 280 | + $this->segments = $segment; |
|
| 281 | + } |
|
| 282 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 283 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 284 | + $this->logger->info('The request URI contains the front controller'); |
|
| 285 | + array_shift($segment); |
|
| 286 | + $this->segments = $segment; |
|
| 287 | + } |
|
| 288 | + return $this; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Setting the route parameters like module, controller, method, argument |
|
| 293 | + * @return object the current instance |
|
| 294 | + */ |
|
| 295 | + public function determineRouteParamsInformation() { |
|
| 296 | + $this->logger->debug('Routing process start ...'); |
|
| 297 | 297 | |
| 298 | - //determine route parameters using the config |
|
| 299 | - $this->determineRouteParamsFromConfig(); |
|
| 298 | + //determine route parameters using the config |
|
| 299 | + $this->determineRouteParamsFromConfig(); |
|
| 300 | 300 | |
| 301 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 302 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 303 | - if(! $this->controller){ |
|
| 304 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 305 | - //determine route parameters using the REQUEST_URI param |
|
| 306 | - $this->determineRouteParamsFromRequestUri(); |
|
| 307 | - } |
|
| 308 | - //Set the controller file path if not yet set |
|
| 309 | - $this->setControllerFilePath(); |
|
| 310 | - $this->logger->debug('Routing process end.'); |
|
| 311 | - |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 301 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 302 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 303 | + if(! $this->controller){ |
|
| 304 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 305 | + //determine route parameters using the REQUEST_URI param |
|
| 306 | + $this->determineRouteParamsFromRequestUri(); |
|
| 307 | + } |
|
| 308 | + //Set the controller file path if not yet set |
|
| 309 | + $this->setControllerFilePath(); |
|
| 310 | + $this->logger->debug('Routing process end.'); |
|
| 311 | + |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Routing the request to the correspondant module/controller/method if exists |
|
| 317 | - * otherwise send 404 error. |
|
| 318 | - */ |
|
| 319 | - public function processRequest(){ |
|
| 320 | - //Setting the route URI |
|
| 321 | - $this->setRouteUri(); |
|
| 322 | - |
|
| 323 | - //setting route segments |
|
| 324 | - $this->setRouteSegments(); |
|
| 325 | - |
|
| 326 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 327 | - |
|
| 328 | - //determine the route parameters information |
|
| 329 | - $this->determineRouteParamsInformation(); |
|
| 330 | - |
|
| 331 | - $e404 = false; |
|
| 332 | - $classFilePath = $this->controllerPath; |
|
| 333 | - $controller = ucfirst($this->controller); |
|
| 334 | - $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 335 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 315 | + /** |
|
| 316 | + * Routing the request to the correspondant module/controller/method if exists |
|
| 317 | + * otherwise send 404 error. |
|
| 318 | + */ |
|
| 319 | + public function processRequest(){ |
|
| 320 | + //Setting the route URI |
|
| 321 | + $this->setRouteUri(); |
|
| 322 | + |
|
| 323 | + //setting route segments |
|
| 324 | + $this->setRouteSegments(); |
|
| 325 | + |
|
| 326 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 327 | + |
|
| 328 | + //determine the route parameters information |
|
| 329 | + $this->determineRouteParamsInformation(); |
|
| 330 | + |
|
| 331 | + $e404 = false; |
|
| 332 | + $classFilePath = $this->controllerPath; |
|
| 333 | + $controller = ucfirst($this->controller); |
|
| 334 | + $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 335 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 336 | 336 | |
| 337 | - if(file_exists($classFilePath)){ |
|
| 338 | - require_once $classFilePath; |
|
| 339 | - if(! class_exists($controller, false)){ |
|
| 340 | - $e404 = true; |
|
| 341 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 342 | - } |
|
| 343 | - else{ |
|
| 344 | - $controllerInstance = new $controller(); |
|
| 345 | - $controllerMethod = $this->getMethod(); |
|
| 346 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 347 | - $e404 = true; |
|
| 348 | - $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 349 | - } |
|
| 350 | - else{ |
|
| 351 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
| 352 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 353 | - //render the final page to user |
|
| 354 | - $this->logger->info('Render the final output to the browser'); |
|
| 355 | - get_instance()->response->renderFinalPage(); |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - else{ |
|
| 360 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 361 | - $e404 = true; |
|
| 362 | - } |
|
| 363 | - if($e404){ |
|
| 364 | - if(IS_CLI){ |
|
| 365 | - set_http_status_header(404); |
|
| 366 | - echo 'Error 404: page not found.'; |
|
| 367 | - } else { |
|
| 368 | - $response =& class_loader('Response', 'classes'); |
|
| 369 | - $response->send404(); |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
| 377 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 378 | - * @param boolean $useConfigFile whether to use route configuration file |
|
| 379 | - * @return object |
|
| 380 | - */ |
|
| 381 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 382 | - $route = array(); |
|
| 383 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 384 | - require_once CONFIG_PATH . 'routes.php'; |
|
| 385 | - } |
|
| 386 | - $route = array_merge($route, $overwriteConfig); |
|
| 387 | - $this->routes = $route; |
|
| 388 | - //if route is empty remove all configuration |
|
| 389 | - if(empty($route)){ |
|
| 390 | - $this->removeAllRoute(); |
|
| 391 | - } |
|
| 392 | - return $this; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Get the route configuration |
|
| 397 | - * @return array |
|
| 398 | - */ |
|
| 399 | - public function getRouteConfiguration(){ |
|
| 400 | - return $this->routes; |
|
| 401 | - } |
|
| 337 | + if(file_exists($classFilePath)){ |
|
| 338 | + require_once $classFilePath; |
|
| 339 | + if(! class_exists($controller, false)){ |
|
| 340 | + $e404 = true; |
|
| 341 | + $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 342 | + } |
|
| 343 | + else{ |
|
| 344 | + $controllerInstance = new $controller(); |
|
| 345 | + $controllerMethod = $this->getMethod(); |
|
| 346 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 347 | + $e404 = true; |
|
| 348 | + $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 349 | + } |
|
| 350 | + else{ |
|
| 351 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
| 352 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
|
| 353 | + //render the final page to user |
|
| 354 | + $this->logger->info('Render the final output to the browser'); |
|
| 355 | + get_instance()->response->renderFinalPage(); |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + else{ |
|
| 360 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 361 | + $e404 = true; |
|
| 362 | + } |
|
| 363 | + if($e404){ |
|
| 364 | + if(IS_CLI){ |
|
| 365 | + set_http_status_header(404); |
|
| 366 | + echo 'Error 404: page not found.'; |
|
| 367 | + } else { |
|
| 368 | + $response =& class_loader('Response', 'classes'); |
|
| 369 | + $response->send404(); |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
| 377 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 378 | + * @param boolean $useConfigFile whether to use route configuration file |
|
| 379 | + * @return object |
|
| 380 | + */ |
|
| 381 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 382 | + $route = array(); |
|
| 383 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 384 | + require_once CONFIG_PATH . 'routes.php'; |
|
| 385 | + } |
|
| 386 | + $route = array_merge($route, $overwriteConfig); |
|
| 387 | + $this->routes = $route; |
|
| 388 | + //if route is empty remove all configuration |
|
| 389 | + if(empty($route)){ |
|
| 390 | + $this->removeAllRoute(); |
|
| 391 | + } |
|
| 392 | + return $this; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Get the route configuration |
|
| 397 | + * @return array |
|
| 398 | + */ |
|
| 399 | + public function getRouteConfiguration(){ |
|
| 400 | + return $this->routes; |
|
| 401 | + } |
|
| 402 | 402 | |
| 403 | 403 | |
| 404 | - /** |
|
| 405 | - * Set the controller file path if is not set |
|
| 406 | - * @param string $path the file path if is null will using the route |
|
| 407 | - * information |
|
| 408 | - * |
|
| 409 | - * @return object the current instance |
|
| 410 | - */ |
|
| 411 | - public function setControllerFilePath($path = null){ |
|
| 412 | - if($path !== null){ |
|
| 413 | - $this->controllerPath = $path; |
|
| 414 | - return $this; |
|
| 415 | - } |
|
| 416 | - //did we set the controller, so set the controller path |
|
| 417 | - if($this->controller && ! $this->controllerPath){ |
|
| 418 | - $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 419 | - $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 420 | - //if the controller is in module |
|
| 421 | - if($this->module){ |
|
| 422 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 423 | - if($path !== false){ |
|
| 424 | - $controllerPath = $path; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - $this->controllerPath = $controllerPath; |
|
| 428 | - } |
|
| 429 | - return $this; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Determine the route parameters from route configuration |
|
| 434 | - * @return void |
|
| 435 | - */ |
|
| 436 | - protected function determineRouteParamsFromConfig(){ |
|
| 437 | - $uri = implode('/', $this->segments); |
|
| 438 | - /* |
|
| 404 | + /** |
|
| 405 | + * Set the controller file path if is not set |
|
| 406 | + * @param string $path the file path if is null will using the route |
|
| 407 | + * information |
|
| 408 | + * |
|
| 409 | + * @return object the current instance |
|
| 410 | + */ |
|
| 411 | + public function setControllerFilePath($path = null){ |
|
| 412 | + if($path !== null){ |
|
| 413 | + $this->controllerPath = $path; |
|
| 414 | + return $this; |
|
| 415 | + } |
|
| 416 | + //did we set the controller, so set the controller path |
|
| 417 | + if($this->controller && ! $this->controllerPath){ |
|
| 418 | + $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
|
| 419 | + $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
|
| 420 | + //if the controller is in module |
|
| 421 | + if($this->module){ |
|
| 422 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 423 | + if($path !== false){ |
|
| 424 | + $controllerPath = $path; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + $this->controllerPath = $controllerPath; |
|
| 428 | + } |
|
| 429 | + return $this; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Determine the route parameters from route configuration |
|
| 434 | + * @return void |
|
| 435 | + */ |
|
| 436 | + protected function determineRouteParamsFromConfig(){ |
|
| 437 | + $uri = implode('/', $this->segments); |
|
| 438 | + /* |
|
| 439 | 439 | * Generics routes patterns |
| 440 | 440 | */ |
| 441 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 442 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 443 | - |
|
| 444 | - $this->logger->debug( |
|
| 445 | - 'Begin to loop in the predefined routes configuration ' |
|
| 446 | - . 'to check if the current request match' |
|
| 447 | - ); |
|
| 448 | - |
|
| 449 | - // Cycle through the URIs stored in the array |
|
| 450 | - foreach ($this->pattern as $index => $uriList) { |
|
| 451 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 452 | - // Check for an existant matching URI |
|
| 453 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 454 | - $this->logger->info( |
|
| 455 | - 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 456 | - . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 457 | - ); |
|
| 458 | - array_shift($args); |
|
| 459 | - //check if this contains an module |
|
| 460 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 461 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 462 | - $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 463 | - $this->module = $moduleControllerMethod[0]; |
|
| 464 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 465 | - } |
|
| 466 | - else{ |
|
| 467 | - $this->logger->info('The current request does not use the module'); |
|
| 468 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 469 | - } |
|
| 470 | - if(is_array($moduleControllerMethod)){ |
|
| 471 | - if(isset($moduleControllerMethod[0])){ |
|
| 472 | - $this->controller = $moduleControllerMethod[0]; |
|
| 473 | - } |
|
| 474 | - if(isset($moduleControllerMethod[1])){ |
|
| 475 | - $this->method = $moduleControllerMethod[1]; |
|
| 476 | - } |
|
| 477 | - $this->args = $args; |
|
| 478 | - } |
|
| 479 | - // stop here |
|
| 480 | - break; |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - //first if the controller is not set and the module is set use the module name as the controller |
|
| 485 | - if(! $this->controller && $this->module){ |
|
| 486 | - $this->logger->info( |
|
| 487 | - 'After loop in predefined routes configuration, |
|
| 441 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 442 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 443 | + |
|
| 444 | + $this->logger->debug( |
|
| 445 | + 'Begin to loop in the predefined routes configuration ' |
|
| 446 | + . 'to check if the current request match' |
|
| 447 | + ); |
|
| 448 | + |
|
| 449 | + // Cycle through the URIs stored in the array |
|
| 450 | + foreach ($this->pattern as $index => $uriList) { |
|
| 451 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 452 | + // Check for an existant matching URI |
|
| 453 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 454 | + $this->logger->info( |
|
| 455 | + 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
|
| 456 | + . ' [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']' |
|
| 457 | + ); |
|
| 458 | + array_shift($args); |
|
| 459 | + //check if this contains an module |
|
| 460 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 461 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 462 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
| 463 | + $this->module = $moduleControllerMethod[0]; |
|
| 464 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 465 | + } |
|
| 466 | + else{ |
|
| 467 | + $this->logger->info('The current request does not use the module'); |
|
| 468 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 469 | + } |
|
| 470 | + if(is_array($moduleControllerMethod)){ |
|
| 471 | + if(isset($moduleControllerMethod[0])){ |
|
| 472 | + $this->controller = $moduleControllerMethod[0]; |
|
| 473 | + } |
|
| 474 | + if(isset($moduleControllerMethod[1])){ |
|
| 475 | + $this->method = $moduleControllerMethod[1]; |
|
| 476 | + } |
|
| 477 | + $this->args = $args; |
|
| 478 | + } |
|
| 479 | + // stop here |
|
| 480 | + break; |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + //first if the controller is not set and the module is set use the module name as the controller |
|
| 485 | + if(! $this->controller && $this->module){ |
|
| 486 | + $this->logger->info( |
|
| 487 | + 'After loop in predefined routes configuration, |
|
| 488 | 488 | the module name is set but the controller is not set, |
| 489 | 489 | so we will use module as the controller' |
| 490 | - ); |
|
| 491 | - $this->controller = $this->module; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 497 | - * @return void |
|
| 498 | - */ |
|
| 499 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 500 | - $segment = $this->segments; |
|
| 501 | - $nbSegment = count($segment); |
|
| 502 | - //if segment is null so means no need to perform |
|
| 503 | - if($nbSegment > 0){ |
|
| 504 | - //get the module list |
|
| 505 | - $modules = Module::getModuleList(); |
|
| 506 | - //first check if no module |
|
| 507 | - if(empty($modules)){ |
|
| 508 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
| 509 | - //the application don't use module |
|
| 510 | - //controller |
|
| 511 | - if(isset($segment[0])){ |
|
| 512 | - $this->controller = $segment[0]; |
|
| 513 | - array_shift($segment); |
|
| 514 | - } |
|
| 515 | - //method |
|
| 516 | - if(isset($segment[0])){ |
|
| 517 | - $this->method = $segment[0]; |
|
| 518 | - array_shift($segment); |
|
| 519 | - } |
|
| 520 | - //args |
|
| 521 | - $this->args = $segment; |
|
| 522 | - } |
|
| 523 | - else{ |
|
| 524 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 525 | - if(in_array($segment[0], $modules)){ |
|
| 526 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 527 | - $this->module = $segment[0]; |
|
| 528 | - array_shift($segment); |
|
| 529 | - //check if the second arg is the controller from module |
|
| 530 | - if(isset($segment[0])){ |
|
| 531 | - $this->controller = $segment[0]; |
|
| 532 | - //check if the request use the same module name and controller |
|
| 533 | - $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 534 | - if(! $path){ |
|
| 535 | - $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 536 | - $this->controller = $this->module; |
|
| 537 | - } |
|
| 538 | - else{ |
|
| 539 | - $this->controllerPath = $path; |
|
| 540 | - array_shift($segment); |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - //check for method |
|
| 544 | - if(isset($segment[0])){ |
|
| 545 | - $this->method = $segment[0]; |
|
| 546 | - array_shift($segment); |
|
| 547 | - } |
|
| 548 | - //the remaining is for args |
|
| 549 | - $this->args = $segment; |
|
| 550 | - } |
|
| 551 | - else{ |
|
| 552 | - $this->logger->info('The current request information is not found in the module list'); |
|
| 553 | - //controller |
|
| 554 | - if(isset($segment[0])){ |
|
| 555 | - $this->controller = $segment[0]; |
|
| 556 | - array_shift($segment); |
|
| 557 | - } |
|
| 558 | - //method |
|
| 559 | - if(isset($segment[0])){ |
|
| 560 | - $this->method = $segment[0]; |
|
| 561 | - array_shift($segment); |
|
| 562 | - } |
|
| 563 | - //args |
|
| 564 | - $this->args = $segment; |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - if(! $this->controller && $this->module){ |
|
| 568 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 569 | - $this->controller = $this->module; |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Set the route informations using the configuration |
|
| 576 | - * |
|
| 577 | - * @return object the current instance |
|
| 578 | - */ |
|
| 579 | - protected function setRouteConfigurationInfos(){ |
|
| 580 | - //adding route |
|
| 581 | - foreach($this->routes as $pattern => $callback){ |
|
| 582 | - $this->add($pattern, $callback); |
|
| 583 | - } |
|
| 584 | - return $this; |
|
| 585 | - } |
|
| 586 | - } |
|
| 490 | + ); |
|
| 491 | + $this->controller = $this->module; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Determine the route parameters using the server variable "REQUEST_URI" |
|
| 497 | + * @return void |
|
| 498 | + */ |
|
| 499 | + protected function determineRouteParamsFromRequestUri(){ |
|
| 500 | + $segment = $this->segments; |
|
| 501 | + $nbSegment = count($segment); |
|
| 502 | + //if segment is null so means no need to perform |
|
| 503 | + if($nbSegment > 0){ |
|
| 504 | + //get the module list |
|
| 505 | + $modules = Module::getModuleList(); |
|
| 506 | + //first check if no module |
|
| 507 | + if(empty($modules)){ |
|
| 508 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
| 509 | + //the application don't use module |
|
| 510 | + //controller |
|
| 511 | + if(isset($segment[0])){ |
|
| 512 | + $this->controller = $segment[0]; |
|
| 513 | + array_shift($segment); |
|
| 514 | + } |
|
| 515 | + //method |
|
| 516 | + if(isset($segment[0])){ |
|
| 517 | + $this->method = $segment[0]; |
|
| 518 | + array_shift($segment); |
|
| 519 | + } |
|
| 520 | + //args |
|
| 521 | + $this->args = $segment; |
|
| 522 | + } |
|
| 523 | + else{ |
|
| 524 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 525 | + if(in_array($segment[0], $modules)){ |
|
| 526 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 527 | + $this->module = $segment[0]; |
|
| 528 | + array_shift($segment); |
|
| 529 | + //check if the second arg is the controller from module |
|
| 530 | + if(isset($segment[0])){ |
|
| 531 | + $this->controller = $segment[0]; |
|
| 532 | + //check if the request use the same module name and controller |
|
| 533 | + $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
|
| 534 | + if(! $path){ |
|
| 535 | + $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
|
| 536 | + $this->controller = $this->module; |
|
| 537 | + } |
|
| 538 | + else{ |
|
| 539 | + $this->controllerPath = $path; |
|
| 540 | + array_shift($segment); |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + //check for method |
|
| 544 | + if(isset($segment[0])){ |
|
| 545 | + $this->method = $segment[0]; |
|
| 546 | + array_shift($segment); |
|
| 547 | + } |
|
| 548 | + //the remaining is for args |
|
| 549 | + $this->args = $segment; |
|
| 550 | + } |
|
| 551 | + else{ |
|
| 552 | + $this->logger->info('The current request information is not found in the module list'); |
|
| 553 | + //controller |
|
| 554 | + if(isset($segment[0])){ |
|
| 555 | + $this->controller = $segment[0]; |
|
| 556 | + array_shift($segment); |
|
| 557 | + } |
|
| 558 | + //method |
|
| 559 | + if(isset($segment[0])){ |
|
| 560 | + $this->method = $segment[0]; |
|
| 561 | + array_shift($segment); |
|
| 562 | + } |
|
| 563 | + //args |
|
| 564 | + $this->args = $segment; |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + if(! $this->controller && $this->module){ |
|
| 568 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 569 | + $this->controller = $this->module; |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Set the route informations using the configuration |
|
| 576 | + * |
|
| 577 | + * @return object the current instance |
|
| 578 | + */ |
|
| 579 | + protected function setRouteConfigurationInfos(){ |
|
| 580 | + //adding route |
|
| 581 | + foreach($this->routes as $pattern => $callback){ |
|
| 582 | + $this->add($pattern, $callback); |
|
| 583 | + } |
|
| 584 | + return $this; |
|
| 585 | + } |
|
| 586 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Router extends BaseClass{ |
|
| 27 | + class Router extends BaseClass { |
|
| 28 | 28 | /** |
| 29 | 29 | * @var array $pattern: The list of URIs to validate against |
| 30 | 30 | */ |
@@ -90,13 +90,13 @@ discard block |
||
| 90 | 90 | /** |
| 91 | 91 | * Construct the new Router instance |
| 92 | 92 | */ |
| 93 | - public function __construct(){ |
|
| 93 | + public function __construct() { |
|
| 94 | 94 | parent::__construct(); |
| 95 | 95 | |
| 96 | 96 | //loading routes for module |
| 97 | 97 | $moduleRouteList = array(); |
| 98 | 98 | $modulesRoutes = Module::getModulesRoutesConfig(); |
| 99 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 99 | + if ($modulesRoutes && is_array($modulesRoutes)) { |
|
| 100 | 100 | $moduleRouteList = $modulesRoutes; |
| 101 | 101 | unset($modulesRoutes); |
| 102 | 102 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * Get the route patterns |
| 112 | 112 | * @return array |
| 113 | 113 | */ |
| 114 | - public function getPattern(){ |
|
| 114 | + public function getPattern() { |
|
| 115 | 115 | return $this->pattern; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * Get the route callbacks |
| 120 | 120 | * @return array |
| 121 | 121 | */ |
| 122 | - public function getCallback(){ |
|
| 122 | + public function getCallback() { |
|
| 123 | 123 | return $this->callback; |
| 124 | 124 | } |
| 125 | 125 | |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | * Get the module name |
| 128 | 128 | * @return string |
| 129 | 129 | */ |
| 130 | - public function getModule(){ |
|
| 130 | + public function getModule() { |
|
| 131 | 131 | return $this->module; |
| 132 | 132 | } |
| 133 | 133 | |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | * Get the controller name |
| 136 | 136 | * @return string |
| 137 | 137 | */ |
| 138 | - public function getController(){ |
|
| 138 | + public function getController() { |
|
| 139 | 139 | return $this->controller; |
| 140 | 140 | } |
| 141 | 141 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * Get the controller file path |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - public function getControllerPath(){ |
|
| 146 | + public function getControllerPath() { |
|
| 147 | 147 | return $this->controllerPath; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * Get the controller method |
| 152 | 152 | * @return string |
| 153 | 153 | */ |
| 154 | - public function getMethod(){ |
|
| 154 | + public function getMethod() { |
|
| 155 | 155 | return $this->method; |
| 156 | 156 | } |
| 157 | 157 | |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * Get the request arguments |
| 160 | 160 | * @return array |
| 161 | 161 | */ |
| 162 | - public function getArgs(){ |
|
| 162 | + public function getArgs() { |
|
| 163 | 163 | return $this->args; |
| 164 | 164 | } |
| 165 | 165 | |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * Get the URL segments array |
| 168 | 168 | * @return array |
| 169 | 169 | */ |
| 170 | - public function getSegments(){ |
|
| 170 | + public function getSegments() { |
|
| 171 | 171 | return $this->segments; |
| 172 | 172 | } |
| 173 | 173 | |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | * Get the route URI |
| 176 | 176 | * @return string |
| 177 | 177 | */ |
| 178 | - public function getRouteUri(){ |
|
| 178 | + public function getRouteUri() { |
|
| 179 | 179 | return $this->uri; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | public function add($uri, $callback) { |
| 191 | 191 | $uri = trim($uri, $this->uriTrim); |
| 192 | - if(in_array($uri, $this->pattern)){ |
|
| 192 | + if (in_array($uri, $this->pattern)) { |
|
| 193 | 193 | $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
| 194 | 194 | } |
| 195 | 195 | $this->pattern[] = $uri; |
@@ -205,8 +205,8 @@ discard block |
||
| 205 | 205 | * @return object the current instance |
| 206 | 206 | */ |
| 207 | 207 | public function removeRoute($uri) { |
| 208 | - $index = array_search($uri, $this->pattern, true); |
|
| 209 | - if($index !== false){ |
|
| 208 | + $index = array_search($uri, $this->pattern, true); |
|
| 209 | + if ($index !== false) { |
|
| 210 | 210 | $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
| 211 | 211 | unset($this->pattern[$index]); |
| 212 | 212 | unset($this->callback[$index]); |
@@ -234,26 +234,26 @@ discard block |
||
| 234 | 234 | * @param string $uri the route URI, if is empty will determine automatically |
| 235 | 235 | * @return object |
| 236 | 236 | */ |
| 237 | - public function setRouteUri($uri = ''){ |
|
| 237 | + public function setRouteUri($uri = '') { |
|
| 238 | 238 | $routeUri = ''; |
| 239 | - if(! empty($uri)){ |
|
| 239 | + if (!empty($uri)) { |
|
| 240 | 240 | $routeUri = $uri; |
| 241 | 241 | } |
| 242 | 242 | //if the application is running in CLI mode use the first argument |
| 243 | - else if(IS_CLI && isset($_SERVER['argv'][1])){ |
|
| 243 | + else if (IS_CLI && isset($_SERVER['argv'][1])) { |
|
| 244 | 244 | $routeUri = $_SERVER['argv'][1]; |
| 245 | 245 | } |
| 246 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 246 | + else if (isset($_SERVER['REQUEST_URI'])) { |
|
| 247 | 247 | $routeUri = $_SERVER['REQUEST_URI']; |
| 248 | 248 | } |
| 249 | 249 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
| 250 | 250 | //remove url suffix from the request URI |
| 251 | 251 | $suffix = get_config('url_suffix'); |
| 252 | 252 | if ($suffix) { |
| 253 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 253 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']'); |
|
| 254 | 254 | $routeUri = str_ireplace($suffix, '', $routeUri); |
| 255 | 255 | } |
| 256 | - if (strpos($routeUri, '?') !== false){ |
|
| 256 | + if (strpos($routeUri, '?') !== false) { |
|
| 257 | 257 | $routeUri = substr($routeUri, 0, strpos($routeUri, '?')); |
| 258 | 258 | } |
| 259 | 259 | $this->uri = trim($routeUri, $this->uriTrim); |
@@ -266,8 +266,8 @@ discard block |
||
| 266 | 266 | * |
| 267 | 267 | * @return object |
| 268 | 268 | */ |
| 269 | - public function setRouteSegments(array $segments = array()){ |
|
| 270 | - if(! empty($segments)){ |
|
| 269 | + public function setRouteSegments(array $segments = array()) { |
|
| 270 | + if (!empty($segments)) { |
|
| 271 | 271 | $this->segments = $segments; |
| 272 | 272 | } else if (!empty($this->uri)) { |
| 273 | 273 | $this->segments = explode('/', $this->uri); |
@@ -275,12 +275,12 @@ discard block |
||
| 275 | 275 | $segment = $this->segments; |
| 276 | 276 | $baseUrl = get_config('base_url'); |
| 277 | 277 | //check if the app is not in DOCUMENT_ROOT |
| 278 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false){ |
|
| 278 | + if (isset($segment[0]) && stripos($baseUrl, $segment[0]) !== false) { |
|
| 279 | 279 | array_shift($segment); |
| 280 | 280 | $this->segments = $segment; |
| 281 | 281 | } |
| 282 | 282 | $this->logger->debug('Check if the request URI contains the front controller'); |
| 283 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 283 | + if (isset($segment[0]) && $segment[0] == SELF) { |
|
| 284 | 284 | $this->logger->info('The request URI contains the front controller'); |
| 285 | 285 | array_shift($segment); |
| 286 | 286 | $this->segments = $segment; |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | |
| 301 | 301 | //if can not determine the module/controller/method via the defined routes configuration we will use |
| 302 | 302 | //the URL like http://domain.com/module/controller/method/arg1/arg2 |
| 303 | - if(! $this->controller){ |
|
| 303 | + if (!$this->controller) { |
|
| 304 | 304 | $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
| 305 | 305 | //determine route parameters using the REQUEST_URI param |
| 306 | 306 | $this->determineRouteParamsFromRequestUri(); |
@@ -316,14 +316,14 @@ discard block |
||
| 316 | 316 | * Routing the request to the correspondant module/controller/method if exists |
| 317 | 317 | * otherwise send 404 error. |
| 318 | 318 | */ |
| 319 | - public function processRequest(){ |
|
| 319 | + public function processRequest() { |
|
| 320 | 320 | //Setting the route URI |
| 321 | 321 | $this->setRouteUri(); |
| 322 | 322 | |
| 323 | 323 | //setting route segments |
| 324 | 324 | $this->setRouteSegments(); |
| 325 | 325 | |
| 326 | - $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']' ); |
|
| 326 | + $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']'); |
|
| 327 | 327 | |
| 328 | 328 | //determine the route parameters information |
| 329 | 329 | $this->determineRouteParamsInformation(); |
@@ -334,20 +334,20 @@ discard block |
||
| 334 | 334 | $this->logger->info('The routing information are: module [' . $this->module . '], controller [' . $controller . '], method [' . $this->method . '], args [' . stringfy_vars($this->args) . ']'); |
| 335 | 335 | $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
| 336 | 336 | |
| 337 | - if(file_exists($classFilePath)){ |
|
| 337 | + if (file_exists($classFilePath)) { |
|
| 338 | 338 | require_once $classFilePath; |
| 339 | - if(! class_exists($controller, false)){ |
|
| 339 | + if (!class_exists($controller, false)) { |
|
| 340 | 340 | $e404 = true; |
| 341 | - $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 341 | + $this->logger->warning('The controller file [' . $classFilePath . '] exists but does not contain the class [' . $controller . ']'); |
|
| 342 | 342 | } |
| 343 | - else{ |
|
| 343 | + else { |
|
| 344 | 344 | $controllerInstance = new $controller(); |
| 345 | 345 | $controllerMethod = $this->getMethod(); |
| 346 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 346 | + if (!method_exists($controllerInstance, $controllerMethod)) { |
|
| 347 | 347 | $e404 = true; |
| 348 | 348 | $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
| 349 | 349 | } |
| 350 | - else{ |
|
| 350 | + else { |
|
| 351 | 351 | $this->logger->info('Routing data is set correctly now GO!'); |
| 352 | 352 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
| 353 | 353 | //render the final page to user |
@@ -356,16 +356,16 @@ discard block |
||
| 356 | 356 | } |
| 357 | 357 | } |
| 358 | 358 | } |
| 359 | - else{ |
|
| 359 | + else { |
|
| 360 | 360 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
| 361 | 361 | $e404 = true; |
| 362 | 362 | } |
| 363 | - if($e404){ |
|
| 364 | - if(IS_CLI){ |
|
| 363 | + if ($e404) { |
|
| 364 | + if (IS_CLI) { |
|
| 365 | 365 | set_http_status_header(404); |
| 366 | 366 | echo 'Error 404: page not found.'; |
| 367 | 367 | } else { |
| 368 | - $response =& class_loader('Response', 'classes'); |
|
| 368 | + $response = & class_loader('Response', 'classes'); |
|
| 369 | 369 | $response->send404(); |
| 370 | 370 | } |
| 371 | 371 | } |
@@ -378,15 +378,15 @@ discard block |
||
| 378 | 378 | * @param boolean $useConfigFile whether to use route configuration file |
| 379 | 379 | * @return object |
| 380 | 380 | */ |
| 381 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 381 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
|
| 382 | 382 | $route = array(); |
| 383 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
| 383 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')) { |
|
| 384 | 384 | require_once CONFIG_PATH . 'routes.php'; |
| 385 | 385 | } |
| 386 | 386 | $route = array_merge($route, $overwriteConfig); |
| 387 | 387 | $this->routes = $route; |
| 388 | 388 | //if route is empty remove all configuration |
| 389 | - if(empty($route)){ |
|
| 389 | + if (empty($route)) { |
|
| 390 | 390 | $this->removeAllRoute(); |
| 391 | 391 | } |
| 392 | 392 | return $this; |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | * Get the route configuration |
| 397 | 397 | * @return array |
| 398 | 398 | */ |
| 399 | - public function getRouteConfiguration(){ |
|
| 399 | + public function getRouteConfiguration() { |
|
| 400 | 400 | return $this->routes; |
| 401 | 401 | } |
| 402 | 402 | |
@@ -408,19 +408,19 @@ discard block |
||
| 408 | 408 | * |
| 409 | 409 | * @return object the current instance |
| 410 | 410 | */ |
| 411 | - public function setControllerFilePath($path = null){ |
|
| 412 | - if($path !== null){ |
|
| 411 | + public function setControllerFilePath($path = null) { |
|
| 412 | + if ($path !== null) { |
|
| 413 | 413 | $this->controllerPath = $path; |
| 414 | 414 | return $this; |
| 415 | 415 | } |
| 416 | 416 | //did we set the controller, so set the controller path |
| 417 | - if($this->controller && ! $this->controllerPath){ |
|
| 417 | + if ($this->controller && !$this->controllerPath) { |
|
| 418 | 418 | $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
| 419 | 419 | $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
| 420 | 420 | //if the controller is in module |
| 421 | - if($this->module){ |
|
| 421 | + if ($this->module) { |
|
| 422 | 422 | $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
| 423 | - if($path !== false){ |
|
| 423 | + if ($path !== false) { |
|
| 424 | 424 | $controllerPath = $path; |
| 425 | 425 | } |
| 426 | 426 | } |
@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | * Determine the route parameters from route configuration |
| 434 | 434 | * @return void |
| 435 | 435 | */ |
| 436 | - protected function determineRouteParamsFromConfig(){ |
|
| 436 | + protected function determineRouteParamsFromConfig() { |
|
| 437 | 437 | $uri = implode('/', $this->segments); |
| 438 | 438 | /* |
| 439 | 439 | * Generics routes patterns |
@@ -458,20 +458,20 @@ discard block |
||
| 458 | 458 | array_shift($args); |
| 459 | 459 | //check if this contains an module |
| 460 | 460 | $moduleControllerMethod = explode('#', $this->callback[$index]); |
| 461 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 461 | + if (is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2) { |
|
| 462 | 462 | $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
| 463 | 463 | $this->module = $moduleControllerMethod[0]; |
| 464 | 464 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
| 465 | 465 | } |
| 466 | - else{ |
|
| 466 | + else { |
|
| 467 | 467 | $this->logger->info('The current request does not use the module'); |
| 468 | 468 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
| 469 | 469 | } |
| 470 | - if(is_array($moduleControllerMethod)){ |
|
| 471 | - if(isset($moduleControllerMethod[0])){ |
|
| 470 | + if (is_array($moduleControllerMethod)) { |
|
| 471 | + if (isset($moduleControllerMethod[0])) { |
|
| 472 | 472 | $this->controller = $moduleControllerMethod[0]; |
| 473 | 473 | } |
| 474 | - if(isset($moduleControllerMethod[1])){ |
|
| 474 | + if (isset($moduleControllerMethod[1])) { |
|
| 475 | 475 | $this->method = $moduleControllerMethod[1]; |
| 476 | 476 | } |
| 477 | 477 | $this->args = $args; |
@@ -482,7 +482,7 @@ discard block |
||
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | //first if the controller is not set and the module is set use the module name as the controller |
| 485 | - if(! $this->controller && $this->module){ |
|
| 485 | + if (!$this->controller && $this->module) { |
|
| 486 | 486 | $this->logger->info( |
| 487 | 487 | 'After loop in predefined routes configuration, |
| 488 | 488 | the module name is set but the controller is not set, |
@@ -496,67 +496,67 @@ discard block |
||
| 496 | 496 | * Determine the route parameters using the server variable "REQUEST_URI" |
| 497 | 497 | * @return void |
| 498 | 498 | */ |
| 499 | - protected function determineRouteParamsFromRequestUri(){ |
|
| 499 | + protected function determineRouteParamsFromRequestUri() { |
|
| 500 | 500 | $segment = $this->segments; |
| 501 | 501 | $nbSegment = count($segment); |
| 502 | 502 | //if segment is null so means no need to perform |
| 503 | - if($nbSegment > 0){ |
|
| 503 | + if ($nbSegment > 0) { |
|
| 504 | 504 | //get the module list |
| 505 | 505 | $modules = Module::getModuleList(); |
| 506 | 506 | //first check if no module |
| 507 | - if(empty($modules)){ |
|
| 507 | + if (empty($modules)) { |
|
| 508 | 508 | $this->logger->info('No module was loaded will skip the module checking'); |
| 509 | 509 | //the application don't use module |
| 510 | 510 | //controller |
| 511 | - if(isset($segment[0])){ |
|
| 511 | + if (isset($segment[0])) { |
|
| 512 | 512 | $this->controller = $segment[0]; |
| 513 | 513 | array_shift($segment); |
| 514 | 514 | } |
| 515 | 515 | //method |
| 516 | - if(isset($segment[0])){ |
|
| 516 | + if (isset($segment[0])) { |
|
| 517 | 517 | $this->method = $segment[0]; |
| 518 | 518 | array_shift($segment); |
| 519 | 519 | } |
| 520 | 520 | //args |
| 521 | 521 | $this->args = $segment; |
| 522 | 522 | } |
| 523 | - else{ |
|
| 523 | + else { |
|
| 524 | 524 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
| 525 | - if(in_array($segment[0], $modules)){ |
|
| 525 | + if (in_array($segment[0], $modules)) { |
|
| 526 | 526 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
| 527 | 527 | $this->module = $segment[0]; |
| 528 | 528 | array_shift($segment); |
| 529 | 529 | //check if the second arg is the controller from module |
| 530 | - if(isset($segment[0])){ |
|
| 530 | + if (isset($segment[0])) { |
|
| 531 | 531 | $this->controller = $segment[0]; |
| 532 | 532 | //check if the request use the same module name and controller |
| 533 | 533 | $path = Module::findControllerFullPath(ucfirst($this->controller), $this->module); |
| 534 | - if(! $path){ |
|
| 534 | + if (!$path) { |
|
| 535 | 535 | $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
| 536 | 536 | $this->controller = $this->module; |
| 537 | 537 | } |
| 538 | - else{ |
|
| 538 | + else { |
|
| 539 | 539 | $this->controllerPath = $path; |
| 540 | 540 | array_shift($segment); |
| 541 | 541 | } |
| 542 | 542 | } |
| 543 | 543 | //check for method |
| 544 | - if(isset($segment[0])){ |
|
| 544 | + if (isset($segment[0])) { |
|
| 545 | 545 | $this->method = $segment[0]; |
| 546 | 546 | array_shift($segment); |
| 547 | 547 | } |
| 548 | 548 | //the remaining is for args |
| 549 | 549 | $this->args = $segment; |
| 550 | 550 | } |
| 551 | - else{ |
|
| 551 | + else { |
|
| 552 | 552 | $this->logger->info('The current request information is not found in the module list'); |
| 553 | 553 | //controller |
| 554 | - if(isset($segment[0])){ |
|
| 554 | + if (isset($segment[0])) { |
|
| 555 | 555 | $this->controller = $segment[0]; |
| 556 | 556 | array_shift($segment); |
| 557 | 557 | } |
| 558 | 558 | //method |
| 559 | - if(isset($segment[0])){ |
|
| 559 | + if (isset($segment[0])) { |
|
| 560 | 560 | $this->method = $segment[0]; |
| 561 | 561 | array_shift($segment); |
| 562 | 562 | } |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | $this->args = $segment; |
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | - if(! $this->controller && $this->module){ |
|
| 567 | + if (!$this->controller && $this->module) { |
|
| 568 | 568 | $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
| 569 | 569 | $this->controller = $this->module; |
| 570 | 570 | } |
@@ -576,9 +576,9 @@ discard block |
||
| 576 | 576 | * |
| 577 | 577 | * @return object the current instance |
| 578 | 578 | */ |
| 579 | - protected function setRouteConfigurationInfos(){ |
|
| 579 | + protected function setRouteConfigurationInfos() { |
|
| 580 | 580 | //adding route |
| 581 | - foreach($this->routes as $pattern => $callback){ |
|
| 581 | + foreach ($this->routes as $pattern => $callback) { |
|
| 582 | 582 | $this->add($pattern, $callback); |
| 583 | 583 | } |
| 584 | 584 | return $this; |
@@ -242,8 +242,7 @@ discard block |
||
| 242 | 242 | //if the application is running in CLI mode use the first argument |
| 243 | 243 | else if(IS_CLI && isset($_SERVER['argv'][1])){ |
| 244 | 244 | $routeUri = $_SERVER['argv'][1]; |
| 245 | - } |
|
| 246 | - else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 245 | + } else if(isset($_SERVER['REQUEST_URI'])){ |
|
| 247 | 246 | $routeUri = $_SERVER['REQUEST_URI']; |
| 248 | 247 | } |
| 249 | 248 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
@@ -339,15 +338,13 @@ discard block |
||
| 339 | 338 | if(! class_exists($controller, false)){ |
| 340 | 339 | $e404 = true; |
| 341 | 340 | $this->logger->warning('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
| 342 | - } |
|
| 343 | - else{ |
|
| 341 | + } else{ |
|
| 344 | 342 | $controllerInstance = new $controller(); |
| 345 | 343 | $controllerMethod = $this->getMethod(); |
| 346 | 344 | if(! method_exists($controllerInstance, $controllerMethod)){ |
| 347 | 345 | $e404 = true; |
| 348 | 346 | $this->logger->warning('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
| 349 | - } |
|
| 350 | - else{ |
|
| 347 | + } else{ |
|
| 351 | 348 | $this->logger->info('Routing data is set correctly now GO!'); |
| 352 | 349 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->args); |
| 353 | 350 | //render the final page to user |
@@ -355,8 +352,7 @@ discard block |
||
| 355 | 352 | get_instance()->response->renderFinalPage(); |
| 356 | 353 | } |
| 357 | 354 | } |
| 358 | - } |
|
| 359 | - else{ |
|
| 355 | + } else{ |
|
| 360 | 356 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
| 361 | 357 | $e404 = true; |
| 362 | 358 | } |
@@ -462,8 +458,7 @@ discard block |
||
| 462 | 458 | $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
| 463 | 459 | $this->module = $moduleControllerMethod[0]; |
| 464 | 460 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
| 465 | - } |
|
| 466 | - else{ |
|
| 461 | + } else{ |
|
| 467 | 462 | $this->logger->info('The current request does not use the module'); |
| 468 | 463 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
| 469 | 464 | } |
@@ -519,8 +514,7 @@ discard block |
||
| 519 | 514 | } |
| 520 | 515 | //args |
| 521 | 516 | $this->args = $segment; |
| 522 | - } |
|
| 523 | - else{ |
|
| 517 | + } else{ |
|
| 524 | 518 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
| 525 | 519 | if(in_array($segment[0], $modules)){ |
| 526 | 520 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
@@ -534,8 +528,7 @@ discard block |
||
| 534 | 528 | if(! $path){ |
| 535 | 529 | $this->logger->info('The controller [' . $this->controller . '] not found in the module, may be will use the module [' . $this->module . '] as controller'); |
| 536 | 530 | $this->controller = $this->module; |
| 537 | - } |
|
| 538 | - else{ |
|
| 531 | + } else{ |
|
| 539 | 532 | $this->controllerPath = $path; |
| 540 | 533 | array_shift($segment); |
| 541 | 534 | } |
@@ -547,8 +540,7 @@ discard block |
||
| 547 | 540 | } |
| 548 | 541 | //the remaining is for args |
| 549 | 542 | $this->args = $segment; |
| 550 | - } |
|
| 551 | - else{ |
|
| 543 | + } else{ |
|
| 552 | 544 | $this->logger->info('The current request information is not found in the module list'); |
| 553 | 545 | //controller |
| 554 | 546 | if(isset($segment[0])){ |
@@ -1,150 +1,150 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Session extends BaseStaticClass{ |
|
| 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 | + class Session extends BaseStaticClass{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The session flash key to use |
|
| 30 | - * @const |
|
| 31 | - */ |
|
| 32 | - const SESSION_FLASH_KEY = 'session_flash'; |
|
| 28 | + /** |
|
| 29 | + * The session flash key to use |
|
| 30 | + * @const |
|
| 31 | + */ |
|
| 32 | + const SESSION_FLASH_KEY = 'session_flash'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the session item value |
|
| 36 | - * @param string $item the session item name to get |
|
| 37 | - * @param mixed $default the default value to use if can not find the session item in the list |
|
| 38 | - * @return mixed the session value if exist or the default value |
|
| 39 | - */ |
|
| 40 | - public static function get($item, $default = null){ |
|
| 41 | - $logger = self::getLogger(); |
|
| 42 | - $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 43 | - if(array_key_exists($item, $_SESSION)){ |
|
| 44 | - $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 45 | - return $_SESSION[$item]; |
|
| 46 | - } |
|
| 47 | - $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 48 | - return $default; |
|
| 49 | - } |
|
| 34 | + /** |
|
| 35 | + * Get the session item value |
|
| 36 | + * @param string $item the session item name to get |
|
| 37 | + * @param mixed $default the default value to use if can not find the session item in the list |
|
| 38 | + * @return mixed the session value if exist or the default value |
|
| 39 | + */ |
|
| 40 | + public static function get($item, $default = null){ |
|
| 41 | + $logger = self::getLogger(); |
|
| 42 | + $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 43 | + if(array_key_exists($item, $_SESSION)){ |
|
| 44 | + $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 45 | + return $_SESSION[$item]; |
|
| 46 | + } |
|
| 47 | + $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 48 | + return $default; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Set the session item value |
|
| 53 | - * @param string $item the session item name to set |
|
| 54 | - * @param mixed $value the session item value |
|
| 55 | - */ |
|
| 56 | - public static function set($item, $value){ |
|
| 57 | - $logger = self::getLogger(); |
|
| 58 | - $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 59 | - $_SESSION[$item] = $value; |
|
| 60 | - } |
|
| 51 | + /** |
|
| 52 | + * Set the session item value |
|
| 53 | + * @param string $item the session item name to set |
|
| 54 | + * @param mixed $value the session item value |
|
| 55 | + */ |
|
| 56 | + public static function set($item, $value){ |
|
| 57 | + $logger = self::getLogger(); |
|
| 58 | + $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 59 | + $_SESSION[$item] = $value; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get the session flash item value |
|
| 64 | - * @param string $item the session flash item name to get |
|
| 65 | - * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 66 | - * @return mixed the session flash value if exist or the default value |
|
| 67 | - */ |
|
| 68 | - public static function getFlash($item, $default = null){ |
|
| 69 | - $logger = self::getLogger(); |
|
| 70 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 71 | - $return = array_key_exists($key, $_SESSION) ? |
|
| 72 | - ($_SESSION[$key]) : $default; |
|
| 73 | - if(array_key_exists($key, $_SESSION)){ |
|
| 74 | - unset($_SESSION[$key]); |
|
| 75 | - } |
|
| 76 | - else{ |
|
| 77 | - $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 78 | - } |
|
| 79 | - return $return; |
|
| 80 | - } |
|
| 62 | + /** |
|
| 63 | + * Get the session flash item value |
|
| 64 | + * @param string $item the session flash item name to get |
|
| 65 | + * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 66 | + * @return mixed the session flash value if exist or the default value |
|
| 67 | + */ |
|
| 68 | + public static function getFlash($item, $default = null){ |
|
| 69 | + $logger = self::getLogger(); |
|
| 70 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 71 | + $return = array_key_exists($key, $_SESSION) ? |
|
| 72 | + ($_SESSION[$key]) : $default; |
|
| 73 | + if(array_key_exists($key, $_SESSION)){ |
|
| 74 | + unset($_SESSION[$key]); |
|
| 75 | + } |
|
| 76 | + else{ |
|
| 77 | + $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 78 | + } |
|
| 79 | + return $return; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Check whether the given session flash item exists |
|
| 84 | - * @param string $item the session flash item name |
|
| 85 | - * @return boolean |
|
| 86 | - */ |
|
| 87 | - public static function hasFlash($item){ |
|
| 88 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | - return array_key_exists($key, $_SESSION); |
|
| 90 | - } |
|
| 82 | + /** |
|
| 83 | + * Check whether the given session flash item exists |
|
| 84 | + * @param string $item the session flash item name |
|
| 85 | + * @return boolean |
|
| 86 | + */ |
|
| 87 | + public static function hasFlash($item){ |
|
| 88 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | + return array_key_exists($key, $_SESSION); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Set the session flash item value |
|
| 94 | - * @param string $item the session flash item name to set |
|
| 95 | - * @param mixed $value the session flash item value |
|
| 96 | - */ |
|
| 97 | - public static function setFlash($item, $value){ |
|
| 98 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 99 | - $_SESSION[$key] = $value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * Set the session flash item value |
|
| 94 | + * @param string $item the session flash item name to set |
|
| 95 | + * @param mixed $value the session flash item value |
|
| 96 | + */ |
|
| 97 | + public static function setFlash($item, $value){ |
|
| 98 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 99 | + $_SESSION[$key] = $value; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Clear the session item in the list |
|
| 104 | - * @param string $item the session item name to be deleted |
|
| 105 | - */ |
|
| 106 | - public static function clear($item){ |
|
| 107 | - $logger = self::getLogger(); |
|
| 108 | - if(array_key_exists($item, $_SESSION)){ |
|
| 109 | - $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 110 | - unset($_SESSION[$item]); |
|
| 111 | - } |
|
| 112 | - else{ |
|
| 113 | - $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 114 | - } |
|
| 115 | - } |
|
| 102 | + /** |
|
| 103 | + * Clear the session item in the list |
|
| 104 | + * @param string $item the session item name to be deleted |
|
| 105 | + */ |
|
| 106 | + public static function clear($item){ |
|
| 107 | + $logger = self::getLogger(); |
|
| 108 | + if(array_key_exists($item, $_SESSION)){ |
|
| 109 | + $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 110 | + unset($_SESSION[$item]); |
|
| 111 | + } |
|
| 112 | + else{ |
|
| 113 | + $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Clear the session flash item in the list |
|
| 119 | - * @param string $item the session flash item name to be deleted |
|
| 120 | - */ |
|
| 121 | - public static function clearFlash($item){ |
|
| 122 | - $logger = self::getLogger(); |
|
| 123 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 124 | - if(array_key_exists($key, $_SESSION)){ |
|
| 125 | - $logger->info('Delete session flash for item ['.$item.']'); |
|
| 126 | - unset($_SESSION[$item]); |
|
| 127 | - } |
|
| 128 | - else{ |
|
| 129 | - $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 130 | - } |
|
| 131 | - } |
|
| 117 | + /** |
|
| 118 | + * Clear the session flash item in the list |
|
| 119 | + * @param string $item the session flash item name to be deleted |
|
| 120 | + */ |
|
| 121 | + public static function clearFlash($item){ |
|
| 122 | + $logger = self::getLogger(); |
|
| 123 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 124 | + if(array_key_exists($key, $_SESSION)){ |
|
| 125 | + $logger->info('Delete session flash for item ['.$item.']'); |
|
| 126 | + unset($_SESSION[$item]); |
|
| 127 | + } |
|
| 128 | + else{ |
|
| 129 | + $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Check whether the given session item exists |
|
| 135 | - * @param string $item the session item name |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - public static function exists($item){ |
|
| 139 | - return array_key_exists($item, $_SESSION); |
|
| 140 | - } |
|
| 133 | + /** |
|
| 134 | + * Check whether the given session item exists |
|
| 135 | + * @param string $item the session item name |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + public static function exists($item){ |
|
| 139 | + return array_key_exists($item, $_SESSION); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Destroy all session data values |
|
| 144 | - */ |
|
| 145 | - public static function clearAll(){ |
|
| 146 | - session_unset(); |
|
| 147 | - session_destroy(); |
|
| 148 | - } |
|
| 142 | + /** |
|
| 143 | + * Destroy all session data values |
|
| 144 | + */ |
|
| 145 | + public static function clearAll(){ |
|
| 146 | + session_unset(); |
|
| 147 | + session_destroy(); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - } |
|
| 150 | + } |
|
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Session extends BaseStaticClass{ |
|
| 26 | + class Session extends BaseStaticClass { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The session flash key to use |
@@ -37,14 +37,14 @@ discard block |
||
| 37 | 37 | * @param mixed $default the default value to use if can not find the session item in the list |
| 38 | 38 | * @return mixed the session value if exist or the default value |
| 39 | 39 | */ |
| 40 | - public static function get($item, $default = null){ |
|
| 40 | + public static function get($item, $default = null) { |
|
| 41 | 41 | $logger = self::getLogger(); |
| 42 | - $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 43 | - if(array_key_exists($item, $_SESSION)){ |
|
| 42 | + $logger->debug('Getting session data for item [' . $item . '] ...'); |
|
| 43 | + if (array_key_exists($item, $_SESSION)) { |
|
| 44 | 44 | $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
| 45 | 45 | return $_SESSION[$item]; |
| 46 | 46 | } |
| 47 | - $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 47 | + $logger->warning('Cannot find session item [' . $item . '] using the default value [' . $default . ']'); |
|
| 48 | 48 | return $default; |
| 49 | 49 | } |
| 50 | 50 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @param string $item the session item name to set |
| 54 | 54 | * @param mixed $value the session item value |
| 55 | 55 | */ |
| 56 | - public static function set($item, $value){ |
|
| 56 | + public static function set($item, $value) { |
|
| 57 | 57 | $logger = self::getLogger(); |
| 58 | 58 | $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
| 59 | 59 | $_SESSION[$item] = $value; |
@@ -65,16 +65,16 @@ discard block |
||
| 65 | 65 | * @param mixed $default the default value to use if can not find the session flash item in the list |
| 66 | 66 | * @return mixed the session flash value if exist or the default value |
| 67 | 67 | */ |
| 68 | - public static function getFlash($item, $default = null){ |
|
| 68 | + public static function getFlash($item, $default = null) { |
|
| 69 | 69 | $logger = self::getLogger(); |
| 70 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 70 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
| 71 | 71 | $return = array_key_exists($key, $_SESSION) ? |
| 72 | 72 | ($_SESSION[$key]) : $default; |
| 73 | - if(array_key_exists($key, $_SESSION)){ |
|
| 73 | + if (array_key_exists($key, $_SESSION)) { |
|
| 74 | 74 | unset($_SESSION[$key]); |
| 75 | 75 | } |
| 76 | - else{ |
|
| 77 | - $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 76 | + else { |
|
| 77 | + $logger->warning('Cannot find session flash item [' . $key . '] using the default value [' . $default . ']'); |
|
| 78 | 78 | } |
| 79 | 79 | return $return; |
| 80 | 80 | } |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | * @param string $item the session flash item name |
| 85 | 85 | * @return boolean |
| 86 | 86 | */ |
| 87 | - public static function hasFlash($item){ |
|
| 88 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 87 | + public static function hasFlash($item) { |
|
| 88 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
| 89 | 89 | return array_key_exists($key, $_SESSION); |
| 90 | 90 | } |
| 91 | 91 | |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | * @param string $item the session flash item name to set |
| 95 | 95 | * @param mixed $value the session flash item value |
| 96 | 96 | */ |
| 97 | - public static function setFlash($item, $value){ |
|
| 98 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 97 | + public static function setFlash($item, $value) { |
|
| 98 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
| 99 | 99 | $_SESSION[$key] = $value; |
| 100 | 100 | } |
| 101 | 101 | |
@@ -103,14 +103,14 @@ discard block |
||
| 103 | 103 | * Clear the session item in the list |
| 104 | 104 | * @param string $item the session item name to be deleted |
| 105 | 105 | */ |
| 106 | - public static function clear($item){ |
|
| 106 | + public static function clear($item) { |
|
| 107 | 107 | $logger = self::getLogger(); |
| 108 | - if(array_key_exists($item, $_SESSION)){ |
|
| 109 | - $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 108 | + if (array_key_exists($item, $_SESSION)) { |
|
| 109 | + $logger->info('Deleting of session for item [' . $item . ' ]'); |
|
| 110 | 110 | unset($_SESSION[$item]); |
| 111 | 111 | } |
| 112 | - else{ |
|
| 113 | - $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 112 | + else { |
|
| 113 | + $logger->warning('Session item [' . $item . '] to be deleted does not exists'); |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | |
@@ -118,15 +118,15 @@ discard block |
||
| 118 | 118 | * Clear the session flash item in the list |
| 119 | 119 | * @param string $item the session flash item name to be deleted |
| 120 | 120 | */ |
| 121 | - public static function clearFlash($item){ |
|
| 121 | + public static function clearFlash($item) { |
|
| 122 | 122 | $logger = self::getLogger(); |
| 123 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 124 | - if(array_key_exists($key, $_SESSION)){ |
|
| 125 | - $logger->info('Delete session flash for item ['.$item.']'); |
|
| 123 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
| 124 | + if (array_key_exists($key, $_SESSION)) { |
|
| 125 | + $logger->info('Delete session flash for item [' . $item . ']'); |
|
| 126 | 126 | unset($_SESSION[$item]); |
| 127 | 127 | } |
| 128 | - else{ |
|
| 129 | - $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 128 | + else { |
|
| 129 | + $logger->warning('Dession flash item [' . $item . '] to be deleted does not exists'); |
|
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
@@ -135,14 +135,14 @@ discard block |
||
| 135 | 135 | * @param string $item the session item name |
| 136 | 136 | * @return boolean |
| 137 | 137 | */ |
| 138 | - public static function exists($item){ |
|
| 138 | + public static function exists($item) { |
|
| 139 | 139 | return array_key_exists($item, $_SESSION); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * Destroy all session data values |
| 144 | 144 | */ |
| 145 | - public static function clearAll(){ |
|
| 145 | + public static function clearAll() { |
|
| 146 | 146 | session_unset(); |
| 147 | 147 | session_destroy(); |
| 148 | 148 | } |
@@ -1,183 +1,183 @@ |
||
| 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 extends BaseClass implements CacheInterface{ |
|
| 27 | + class ApcCache extends BaseClass implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function __construct(){ |
|
| 32 | - parent::__construct(); |
|
| 33 | - if(! $this->isSupported()){ |
|
| 34 | - show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 35 | - } |
|
| 36 | - } |
|
| 31 | + public function __construct(){ |
|
| 32 | + parent::__construct(); |
|
| 33 | + if(! $this->isSupported()){ |
|
| 34 | + show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * This is used to get the cache data using the key |
|
| 40 | - * @param string $key the key to identify the cache data |
|
| 41 | - * @return mixed the cache data if exists else return false |
|
| 42 | - */ |
|
| 43 | - public function get($key){ |
|
| 44 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 45 | - $success = false; |
|
| 46 | - $data = apc_fetch($key, $success); |
|
| 47 | - if($success === false){ |
|
| 48 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - else{ |
|
| 52 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 53 | - $expire = time(); |
|
| 54 | - if($cacheInfo){ |
|
| 55 | - $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 56 | - } |
|
| 57 | - $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) . ']'); |
|
| 58 | - return $data; |
|
| 59 | - } |
|
| 60 | - } |
|
| 38 | + /** |
|
| 39 | + * This is used to get the cache data using the key |
|
| 40 | + * @param string $key the key to identify the cache data |
|
| 41 | + * @return mixed the cache data if exists else return false |
|
| 42 | + */ |
|
| 43 | + public function get($key){ |
|
| 44 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 45 | + $success = false; |
|
| 46 | + $data = apc_fetch($key, $success); |
|
| 47 | + if($success === false){ |
|
| 48 | + $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + else{ |
|
| 52 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 53 | + $expire = time(); |
|
| 54 | + if($cacheInfo){ |
|
| 55 | + $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 56 | + } |
|
| 57 | + $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) . ']'); |
|
| 58 | + return $data; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Save data to the cache |
|
| 65 | - * @param string $key the key to identify this cache data |
|
| 66 | - * @param mixed $data the cache data to be saved |
|
| 67 | - * @param integer $ttl the cache life time |
|
| 68 | - * @return boolean true if success otherwise will return false |
|
| 69 | - */ |
|
| 70 | - public function set($key, $data, $ttl = 0){ |
|
| 71 | - $expire = time() + $ttl; |
|
| 72 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | - $result = apc_store($key, $data, $ttl); |
|
| 74 | - if($result === false){ |
|
| 75 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - else{ |
|
| 79 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 80 | - return true; |
|
| 81 | - } |
|
| 82 | - } |
|
| 63 | + /** |
|
| 64 | + * Save data to the cache |
|
| 65 | + * @param string $key the key to identify this cache data |
|
| 66 | + * @param mixed $data the cache data to be saved |
|
| 67 | + * @param integer $ttl the cache life time |
|
| 68 | + * @return boolean true if success otherwise will return false |
|
| 69 | + */ |
|
| 70 | + public function set($key, $data, $ttl = 0){ |
|
| 71 | + $expire = time() + $ttl; |
|
| 72 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | + $result = apc_store($key, $data, $ttl); |
|
| 74 | + if($result === false){ |
|
| 75 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + else{ |
|
| 79 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 80 | + return true; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Delete the cache data for given key |
|
| 87 | - * @param string $key the key for cache to be deleted |
|
| 88 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 89 | - * the cache or the cache with the given key not exist |
|
| 90 | - */ |
|
| 91 | - public function delete($key){ |
|
| 92 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 93 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 94 | - if($cacheInfo === false){ |
|
| 95 | - $this->logger->info('This cache data does not exists skipping'); |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - else{ |
|
| 99 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 100 | - return apc_delete($key) === true; |
|
| 101 | - } |
|
| 102 | - } |
|
| 85 | + /** |
|
| 86 | + * Delete the cache data for given key |
|
| 87 | + * @param string $key the key for cache to be deleted |
|
| 88 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 89 | + * the cache or the cache with the given key not exist |
|
| 90 | + */ |
|
| 91 | + public function delete($key){ |
|
| 92 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 93 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 94 | + if($cacheInfo === false){ |
|
| 95 | + $this->logger->info('This cache data does not exists skipping'); |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + else{ |
|
| 99 | + $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 100 | + return apc_delete($key) === true; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Get the cache information for given key |
|
| 106 | - * @param string $key the key for cache to get the information for |
|
| 107 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 108 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 109 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 110 | - * 'ttl' => the time to live of the cache in second |
|
| 111 | - */ |
|
| 112 | - public function getInfo($key){ |
|
| 113 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 114 | - $cacheInfos = $this->_getCacheInfo($key); |
|
| 115 | - if($cacheInfos){ |
|
| 116 | - $data = array( |
|
| 117 | - 'mtime' => $cacheInfos['creation_time'], |
|
| 118 | - 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 119 | - 'ttl' => $cacheInfos['ttl'] |
|
| 120 | - ); |
|
| 121 | - return $data; |
|
| 122 | - } |
|
| 123 | - else{ |
|
| 124 | - $this->logger->info('This cache does not exists skipping'); |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - } |
|
| 104 | + /** |
|
| 105 | + * Get the cache information for given key |
|
| 106 | + * @param string $key the key for cache to get the information for |
|
| 107 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 108 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 109 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 110 | + * 'ttl' => the time to live of the cache in second |
|
| 111 | + */ |
|
| 112 | + public function getInfo($key){ |
|
| 113 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 114 | + $cacheInfos = $this->_getCacheInfo($key); |
|
| 115 | + if($cacheInfos){ |
|
| 116 | + $data = array( |
|
| 117 | + 'mtime' => $cacheInfos['creation_time'], |
|
| 118 | + 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 119 | + 'ttl' => $cacheInfos['ttl'] |
|
| 120 | + ); |
|
| 121 | + return $data; |
|
| 122 | + } |
|
| 123 | + else{ |
|
| 124 | + $this->logger->info('This cache does not exists skipping'); |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Used to delete expired cache data |
|
| 132 | - */ |
|
| 133 | - public function deleteExpiredCache(){ |
|
| 134 | - //for APC[u] is done automatically |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 130 | + /** |
|
| 131 | + * Used to delete expired cache data |
|
| 132 | + */ |
|
| 133 | + public function deleteExpiredCache(){ |
|
| 134 | + //for APC[u] is done automatically |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Remove all cache data |
|
| 140 | - */ |
|
| 141 | - public function clean(){ |
|
| 142 | - $this->logger->debug('Deleting of all cache data'); |
|
| 143 | - $cacheInfos = apc_cache_info('user'); |
|
| 144 | - if(empty($cacheInfos['cache_list'])){ |
|
| 145 | - $this->logger->info('No cache data were found skipping'); |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - else{ |
|
| 149 | - $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 150 | - return apc_clear_cache('user'); |
|
| 151 | - } |
|
| 152 | - } |
|
| 138 | + /** |
|
| 139 | + * Remove all cache data |
|
| 140 | + */ |
|
| 141 | + public function clean(){ |
|
| 142 | + $this->logger->debug('Deleting of all cache data'); |
|
| 143 | + $cacheInfos = apc_cache_info('user'); |
|
| 144 | + if(empty($cacheInfos['cache_list'])){ |
|
| 145 | + $this->logger->info('No cache data were found skipping'); |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + else{ |
|
| 149 | + $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 150 | + return apc_clear_cache('user'); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Check whether the cache feature for the handle is supported |
|
| 157 | - * |
|
| 158 | - * @return bool |
|
| 159 | - */ |
|
| 160 | - public function isSupported(){ |
|
| 161 | - return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 162 | - } |
|
| 155 | + /** |
|
| 156 | + * Check whether the cache feature for the handle is supported |
|
| 157 | + * |
|
| 158 | + * @return bool |
|
| 159 | + */ |
|
| 160 | + public function isSupported(){ |
|
| 161 | + return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Return the array of cache information |
|
| 166 | - * |
|
| 167 | - * @param string $key the cache key to get the cache information |
|
| 168 | - * @return boolean|array |
|
| 169 | - */ |
|
| 170 | - private function _getCacheInfo($key){ |
|
| 171 | - $caches = apc_cache_info('user'); |
|
| 172 | - if(! empty($caches['cache_list'])){ |
|
| 173 | - $cacheLists = $caches['cache_list']; |
|
| 174 | - foreach ($cacheLists as $c){ |
|
| 175 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 176 | - return $c; |
|
| 177 | - } |
|
| 178 | - } |
|
| 164 | + /** |
|
| 165 | + * Return the array of cache information |
|
| 166 | + * |
|
| 167 | + * @param string $key the cache key to get the cache information |
|
| 168 | + * @return boolean|array |
|
| 169 | + */ |
|
| 170 | + private function _getCacheInfo($key){ |
|
| 171 | + $caches = apc_cache_info('user'); |
|
| 172 | + if(! empty($caches['cache_list'])){ |
|
| 173 | + $cacheLists = $caches['cache_list']; |
|
| 174 | + foreach ($cacheLists as $c){ |
|
| 175 | + if(isset($c['info']) && $c['info'] === $key){ |
|
| 176 | + return $c; |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - } |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - } |
|
| 180 | + } |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + } |
|
@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class ApcCache extends BaseClass implements CacheInterface{ |
|
| 27 | + class ApcCache extends BaseClass implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function __construct(){ |
|
| 31 | + public function __construct() { |
|
| 32 | 32 | parent::__construct(); |
| 33 | - if(! $this->isSupported()){ |
|
| 33 | + if (!$this->isSupported()) { |
|
| 34 | 34 | show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
| 35 | 35 | } |
| 36 | 36 | } |
@@ -40,21 +40,21 @@ discard block |
||
| 40 | 40 | * @param string $key the key to identify the cache data |
| 41 | 41 | * @return mixed the cache data if exists else return false |
| 42 | 42 | */ |
| 43 | - public function get($key){ |
|
| 44 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 43 | + public function get($key) { |
|
| 44 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 45 | 45 | $success = false; |
| 46 | 46 | $data = apc_fetch($key, $success); |
| 47 | - if($success === false){ |
|
| 48 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 47 | + if ($success === false) { |
|
| 48 | + $this->logger->info('No cache found for the key [' . $key . '], return false'); |
|
| 49 | 49 | return false; |
| 50 | 50 | } |
| 51 | - else{ |
|
| 51 | + else { |
|
| 52 | 52 | $cacheInfo = $this->_getCacheInfo($key); |
| 53 | 53 | $expire = time(); |
| 54 | - if($cacheInfo){ |
|
| 54 | + if ($cacheInfo) { |
|
| 55 | 55 | $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
| 56 | 56 | } |
| 57 | - $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) . ']'); |
|
| 57 | + $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) . ']'); |
|
| 58 | 58 | return $data; |
| 59 | 59 | } |
| 60 | 60 | } |
@@ -67,16 +67,16 @@ discard block |
||
| 67 | 67 | * @param integer $ttl the cache life time |
| 68 | 68 | * @return boolean true if success otherwise will return false |
| 69 | 69 | */ |
| 70 | - public function set($key, $data, $ttl = 0){ |
|
| 70 | + public function set($key, $data, $ttl = 0) { |
|
| 71 | 71 | $expire = time() + $ttl; |
| 72 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 72 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | 73 | $result = apc_store($key, $data, $ttl); |
| 74 | - if($result === false){ |
|
| 75 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 74 | + if ($result === false) { |
|
| 75 | + $this->logger->error('Can not write cache data for the key [' . $key . '], return false'); |
|
| 76 | 76 | return false; |
| 77 | 77 | } |
| 78 | - else{ |
|
| 79 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 78 | + else { |
|
| 79 | + $this->logger->info('Cache data saved for the key [' . $key . ']'); |
|
| 80 | 80 | return true; |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -88,15 +88,15 @@ discard block |
||
| 88 | 88 | * @return boolean true if the cache is deleted, false if can't delete |
| 89 | 89 | * the cache or the cache with the given key not exist |
| 90 | 90 | */ |
| 91 | - public function delete($key){ |
|
| 92 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 91 | + public function delete($key) { |
|
| 92 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 93 | 93 | $cacheInfo = $this->_getCacheInfo($key); |
| 94 | - if($cacheInfo === false){ |
|
| 94 | + if ($cacheInfo === false) { |
|
| 95 | 95 | $this->logger->info('This cache data does not exists skipping'); |
| 96 | 96 | return false; |
| 97 | 97 | } |
| 98 | - else{ |
|
| 99 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 98 | + else { |
|
| 99 | + $this->logger->info('Found cache data for the key [' . $key . '] remove it'); |
|
| 100 | 100 | return apc_delete($key) === true; |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -109,10 +109,10 @@ discard block |
||
| 109 | 109 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 110 | 110 | * 'ttl' => the time to live of the cache in second |
| 111 | 111 | */ |
| 112 | - public function getInfo($key){ |
|
| 113 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 112 | + public function getInfo($key) { |
|
| 113 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 114 | 114 | $cacheInfos = $this->_getCacheInfo($key); |
| 115 | - if($cacheInfos){ |
|
| 115 | + if ($cacheInfos) { |
|
| 116 | 116 | $data = array( |
| 117 | 117 | 'mtime' => $cacheInfos['creation_time'], |
| 118 | 118 | 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | ); |
| 121 | 121 | return $data; |
| 122 | 122 | } |
| 123 | - else{ |
|
| 123 | + else { |
|
| 124 | 124 | $this->logger->info('This cache does not exists skipping'); |
| 125 | 125 | return false; |
| 126 | 126 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | /** |
| 131 | 131 | * Used to delete expired cache data |
| 132 | 132 | */ |
| 133 | - public function deleteExpiredCache(){ |
|
| 133 | + public function deleteExpiredCache() { |
|
| 134 | 134 | //for APC[u] is done automatically |
| 135 | 135 | return true; |
| 136 | 136 | } |
@@ -138,14 +138,14 @@ discard block |
||
| 138 | 138 | /** |
| 139 | 139 | * Remove all cache data |
| 140 | 140 | */ |
| 141 | - public function clean(){ |
|
| 141 | + public function clean() { |
|
| 142 | 142 | $this->logger->debug('Deleting of all cache data'); |
| 143 | 143 | $cacheInfos = apc_cache_info('user'); |
| 144 | - if(empty($cacheInfos['cache_list'])){ |
|
| 144 | + if (empty($cacheInfos['cache_list'])) { |
|
| 145 | 145 | $this->logger->info('No cache data were found skipping'); |
| 146 | 146 | return false; |
| 147 | 147 | } |
| 148 | - else{ |
|
| 148 | + else { |
|
| 149 | 149 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 150 | 150 | return apc_clear_cache('user'); |
| 151 | 151 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * @return bool |
| 159 | 159 | */ |
| 160 | - public function isSupported(){ |
|
| 160 | + public function isSupported() { |
|
| 161 | 161 | return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
| 162 | 162 | } |
| 163 | 163 | |
@@ -167,12 +167,12 @@ discard block |
||
| 167 | 167 | * @param string $key the cache key to get the cache information |
| 168 | 168 | * @return boolean|array |
| 169 | 169 | */ |
| 170 | - private function _getCacheInfo($key){ |
|
| 170 | + private function _getCacheInfo($key) { |
|
| 171 | 171 | $caches = apc_cache_info('user'); |
| 172 | - if(! empty($caches['cache_list'])){ |
|
| 172 | + if (!empty($caches['cache_list'])) { |
|
| 173 | 173 | $cacheLists = $caches['cache_list']; |
| 174 | - foreach ($cacheLists as $c){ |
|
| 175 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 174 | + foreach ($cacheLists as $c) { |
|
| 175 | + if (isset($c['info']) && $c['info'] === $key) { |
|
| 176 | 176 | return $c; |
| 177 | 177 | } |
| 178 | 178 | } |
@@ -47,8 +47,7 @@ discard block |
||
| 47 | 47 | if($success === false){ |
| 48 | 48 | $this->logger->info('No cache found for the key ['. $key .'], return false'); |
| 49 | 49 | return false; |
| 50 | - } |
|
| 51 | - else{ |
|
| 50 | + } else{ |
|
| 52 | 51 | $cacheInfo = $this->_getCacheInfo($key); |
| 53 | 52 | $expire = time(); |
| 54 | 53 | if($cacheInfo){ |
@@ -74,8 +73,7 @@ discard block |
||
| 74 | 73 | if($result === false){ |
| 75 | 74 | $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
| 76 | 75 | return false; |
| 77 | - } |
|
| 78 | - else{ |
|
| 76 | + } else{ |
|
| 79 | 77 | $this->logger->info('Cache data saved for the key ['. $key .']'); |
| 80 | 78 | return true; |
| 81 | 79 | } |
@@ -94,8 +92,7 @@ discard block |
||
| 94 | 92 | if($cacheInfo === false){ |
| 95 | 93 | $this->logger->info('This cache data does not exists skipping'); |
| 96 | 94 | return false; |
| 97 | - } |
|
| 98 | - else{ |
|
| 95 | + } else{ |
|
| 99 | 96 | $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
| 100 | 97 | return apc_delete($key) === true; |
| 101 | 98 | } |
@@ -119,8 +116,7 @@ discard block |
||
| 119 | 116 | 'ttl' => $cacheInfos['ttl'] |
| 120 | 117 | ); |
| 121 | 118 | return $data; |
| 122 | - } |
|
| 123 | - else{ |
|
| 119 | + } else{ |
|
| 124 | 120 | $this->logger->info('This cache does not exists skipping'); |
| 125 | 121 | return false; |
| 126 | 122 | } |
@@ -144,8 +140,7 @@ discard block |
||
| 144 | 140 | if(empty($cacheInfos['cache_list'])){ |
| 145 | 141 | $this->logger->info('No cache data were found skipping'); |
| 146 | 142 | return false; |
| 147 | - } |
|
| 148 | - else{ |
|
| 143 | + } else{ |
|
| 149 | 144 | $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
| 150 | 145 | return apc_clear_cache('user'); |
| 151 | 146 | } |
@@ -1,283 +1,283 @@ |
||
| 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 extends BaseClass implements CacheInterface{ |
|
| 27 | + class FileCache extends BaseClass 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 | - * Class constructor |
|
| 37 | - */ |
|
| 38 | - public function __construct(){ |
|
| 39 | - parent::__construct(); |
|
| 40 | - if(! $this->isSupported()){ |
|
| 41 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * Class constructor |
|
| 37 | + */ |
|
| 38 | + public function __construct(){ |
|
| 39 | + parent::__construct(); |
|
| 40 | + if(! $this->isSupported()){ |
|
| 41 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 45 | - if(! extension_loaded('zlib')){ |
|
| 46 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 47 | - $this->compressCacheData = false; |
|
| 48 | - } |
|
| 49 | - } |
|
| 44 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 45 | + if(! extension_loaded('zlib')){ |
|
| 46 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 47 | + $this->compressCacheData = false; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * This is used to get the cache data using the key |
|
| 53 | - * @param string $key the key to identify the cache data |
|
| 54 | - * @return mixed the cache data if exists else return false |
|
| 55 | - */ |
|
| 56 | - public function get($key){ |
|
| 57 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | - $filePath = $this->getFilePath($key); |
|
| 59 | - if(! file_exists($filePath)){ |
|
| 60 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 64 | - $handle = fopen($filePath,'r'); |
|
| 65 | - if(! is_resource($handle)){ |
|
| 66 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - // Getting a shared lock |
|
| 70 | - flock($handle, LOCK_SH); |
|
| 71 | - $data = file_get_contents($filePath); |
|
| 72 | - fclose($handle); |
|
| 73 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 74 | - if (! $data) { |
|
| 75 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 76 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 77 | - unlink($filePath); |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - if (time() > $data['expire']) { |
|
| 81 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 82 | - // Unlinking when the file was expired |
|
| 83 | - unlink($filePath); |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - else{ |
|
| 87 | - $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']) . ']'); |
|
| 88 | - return $data['data']; |
|
| 89 | - } |
|
| 90 | - } |
|
| 51 | + /** |
|
| 52 | + * This is used to get the cache data using the key |
|
| 53 | + * @param string $key the key to identify the cache data |
|
| 54 | + * @return mixed the cache data if exists else return false |
|
| 55 | + */ |
|
| 56 | + public function get($key){ |
|
| 57 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 58 | + $filePath = $this->getFilePath($key); |
|
| 59 | + if(! file_exists($filePath)){ |
|
| 60 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 64 | + $handle = fopen($filePath,'r'); |
|
| 65 | + if(! is_resource($handle)){ |
|
| 66 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + // Getting a shared lock |
|
| 70 | + flock($handle, LOCK_SH); |
|
| 71 | + $data = file_get_contents($filePath); |
|
| 72 | + fclose($handle); |
|
| 73 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 74 | + if (! $data) { |
|
| 75 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 76 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 77 | + unlink($filePath); |
|
| 78 | + return false; |
|
| 79 | + } |
|
| 80 | + if (time() > $data['expire']) { |
|
| 81 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 82 | + // Unlinking when the file was expired |
|
| 83 | + unlink($filePath); |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + else{ |
|
| 87 | + $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']) . ']'); |
|
| 88 | + return $data['data']; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Save data to the cache |
|
| 95 | - * @param string $key the key to identify this cache data |
|
| 96 | - * @param mixed $data the cache data |
|
| 97 | - * @param integer $ttl the cache life time |
|
| 98 | - * @return boolean true if success otherwise will return false |
|
| 99 | - */ |
|
| 100 | - public function set($key, $data, $ttl = 0){ |
|
| 101 | - $expire = time() + $ttl; |
|
| 102 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 103 | - $filePath = $this->getFilePath($key); |
|
| 104 | - $handle = fopen($filePath,'w'); |
|
| 105 | - if(! is_resource($handle)){ |
|
| 106 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 110 | - //Serializing along with the TTL |
|
| 111 | - $cacheData = serialize(array( |
|
| 112 | - 'mtime' => time(), |
|
| 113 | - 'expire' => $expire, |
|
| 114 | - 'data' => $data, |
|
| 115 | - 'ttl' => $ttl |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 119 | - if(! $result){ |
|
| 120 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 121 | - fclose($handle); |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - else{ |
|
| 125 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 126 | - fclose($handle); |
|
| 127 | - chmod($filePath, 0640); |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - } |
|
| 93 | + /** |
|
| 94 | + * Save data to the cache |
|
| 95 | + * @param string $key the key to identify this cache data |
|
| 96 | + * @param mixed $data the cache data |
|
| 97 | + * @param integer $ttl the cache life time |
|
| 98 | + * @return boolean true if success otherwise will return false |
|
| 99 | + */ |
|
| 100 | + public function set($key, $data, $ttl = 0){ |
|
| 101 | + $expire = time() + $ttl; |
|
| 102 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 103 | + $filePath = $this->getFilePath($key); |
|
| 104 | + $handle = fopen($filePath,'w'); |
|
| 105 | + if(! is_resource($handle)){ |
|
| 106 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 110 | + //Serializing along with the TTL |
|
| 111 | + $cacheData = serialize(array( |
|
| 112 | + 'mtime' => time(), |
|
| 113 | + 'expire' => $expire, |
|
| 114 | + 'data' => $data, |
|
| 115 | + 'ttl' => $ttl |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 119 | + if(! $result){ |
|
| 120 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 121 | + fclose($handle); |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + else{ |
|
| 125 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 126 | + fclose($handle); |
|
| 127 | + chmod($filePath, 0640); |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Delete the cache data for given key |
|
| 135 | - * @param string $key the key for cache to be deleted |
|
| 136 | - * @return boolean true if the cache is delete, false if can't delete |
|
| 137 | - * the cache or the cache with the given key not exist |
|
| 138 | - */ |
|
| 139 | - public function delete($key){ |
|
| 140 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 141 | - $filePath = $this->getFilePath($key); |
|
| 142 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 143 | - if(! file_exists($filePath)){ |
|
| 144 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 149 | - unlink($filePath); |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 152 | - } |
|
| 133 | + /** |
|
| 134 | + * Delete the cache data for given key |
|
| 135 | + * @param string $key the key for cache to be deleted |
|
| 136 | + * @return boolean true if the cache is delete, false if can't delete |
|
| 137 | + * the cache or the cache with the given key not exist |
|
| 138 | + */ |
|
| 139 | + public function delete($key){ |
|
| 140 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 141 | + $filePath = $this->getFilePath($key); |
|
| 142 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 143 | + if(! file_exists($filePath)){ |
|
| 144 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 149 | + unlink($filePath); |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Get the cache information for given key |
|
| 156 | - * @param string $key the key for cache to get the information for |
|
| 157 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 158 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 159 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 160 | - * 'ttl' => the time to live of the cache in second |
|
| 161 | - */ |
|
| 162 | - public function getInfo($key){ |
|
| 163 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 164 | - $filePath = $this->getFilePath($key); |
|
| 165 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 166 | - if(! file_exists($filePath)){ |
|
| 167 | - $this->logger->info('This cache file does not exists skipping'); |
|
| 168 | - return false; |
|
| 169 | - } |
|
| 170 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 171 | - $data = file_get_contents($filePath); |
|
| 172 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 173 | - if(! $data){ |
|
| 174 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - $this->logger->info('This cache data is OK check for expire'); |
|
| 178 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 179 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
| 180 | - $info = array( |
|
| 181 | - 'mtime' => $data['mtime'], |
|
| 182 | - 'expire' => $data['expire'], |
|
| 183 | - 'ttl' => $data['ttl'] |
|
| 184 | - ); |
|
| 185 | - return $info; |
|
| 186 | - } |
|
| 187 | - $this->logger->info('This cache already expired return false'); |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 154 | + /** |
|
| 155 | + * Get the cache information for given key |
|
| 156 | + * @param string $key the key for cache to get the information for |
|
| 157 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 158 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 159 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 160 | + * 'ttl' => the time to live of the cache in second |
|
| 161 | + */ |
|
| 162 | + public function getInfo($key){ |
|
| 163 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 164 | + $filePath = $this->getFilePath($key); |
|
| 165 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 166 | + if(! file_exists($filePath)){ |
|
| 167 | + $this->logger->info('This cache file does not exists skipping'); |
|
| 168 | + return false; |
|
| 169 | + } |
|
| 170 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 171 | + $data = file_get_contents($filePath); |
|
| 172 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 173 | + if(! $data){ |
|
| 174 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + $this->logger->info('This cache data is OK check for expire'); |
|
| 178 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 179 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
| 180 | + $info = array( |
|
| 181 | + 'mtime' => $data['mtime'], |
|
| 182 | + 'expire' => $data['expire'], |
|
| 183 | + 'ttl' => $data['ttl'] |
|
| 184 | + ); |
|
| 185 | + return $info; |
|
| 186 | + } |
|
| 187 | + $this->logger->info('This cache already expired return false'); |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Used to delete expired cache data |
|
| 194 | - */ |
|
| 195 | - public function deleteExpiredCache(){ |
|
| 196 | - $this->logger->debug('Deleting of expired cache files'); |
|
| 197 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 198 | - if(! $list){ |
|
| 199 | - $this->logger->info('No cache files were found skipping'); |
|
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 203 | - foreach ($list as $file) { |
|
| 204 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 205 | - $data = file_get_contents($file); |
|
| 206 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 207 | - if(! $data){ |
|
| 208 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 209 | - } |
|
| 210 | - else if(time() > $data['expire']){ |
|
| 211 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 212 | - unlink($file); |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - } |
|
| 192 | + /** |
|
| 193 | + * Used to delete expired cache data |
|
| 194 | + */ |
|
| 195 | + public function deleteExpiredCache(){ |
|
| 196 | + $this->logger->debug('Deleting of expired cache files'); |
|
| 197 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 198 | + if(! $list){ |
|
| 199 | + $this->logger->info('No cache files were found skipping'); |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
| 203 | + foreach ($list as $file) { |
|
| 204 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 205 | + $data = file_get_contents($file); |
|
| 206 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 207 | + if(! $data){ |
|
| 208 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 209 | + } |
|
| 210 | + else if(time() > $data['expire']){ |
|
| 211 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 212 | + unlink($file); |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Remove all file from cache folder |
|
| 223 | - */ |
|
| 224 | - public function clean(){ |
|
| 225 | - $this->logger->debug('Deleting of all cache files'); |
|
| 226 | - $list = glob(CACHE_PATH . '*.cache'); |
|
| 227 | - if(! $list){ |
|
| 228 | - $this->logger->info('No cache files were found skipping'); |
|
| 229 | - } |
|
| 230 | - else{ |
|
| 231 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 232 | - foreach ($list as $file) { |
|
| 233 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 234 | - unlink($file); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 221 | + /** |
|
| 222 | + * Remove all file from cache folder |
|
| 223 | + */ |
|
| 224 | + public function clean(){ |
|
| 225 | + $this->logger->debug('Deleting of all cache files'); |
|
| 226 | + $list = glob(CACHE_PATH . '*.cache'); |
|
| 227 | + if(! $list){ |
|
| 228 | + $this->logger->info('No cache files were found skipping'); |
|
| 229 | + } |
|
| 230 | + else{ |
|
| 231 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
| 232 | + foreach ($list as $file) { |
|
| 233 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
| 234 | + unlink($file); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * @return boolean |
|
| 241 | - */ |
|
| 242 | - public function isCompressCacheData(){ |
|
| 243 | - return $this->compressCacheData; |
|
| 244 | - } |
|
| 239 | + /** |
|
| 240 | + * @return boolean |
|
| 241 | + */ |
|
| 242 | + public function isCompressCacheData(){ |
|
| 243 | + return $this->compressCacheData; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @param boolean $compressCacheData |
|
| 248 | - * |
|
| 249 | - * @return object |
|
| 250 | - */ |
|
| 251 | - public function setCompressCacheData($status = true){ |
|
| 252 | - //if Zlib extension is not loaded set compressCacheData to false |
|
| 253 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 246 | + /** |
|
| 247 | + * @param boolean $compressCacheData |
|
| 248 | + * |
|
| 249 | + * @return object |
|
| 250 | + */ |
|
| 251 | + public function setCompressCacheData($status = true){ |
|
| 252 | + //if Zlib extension is not loaded set compressCacheData to false |
|
| 253 | + if($status === true && ! extension_loaded('zlib')){ |
|
| 254 | 254 | |
| 255 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 256 | - $this->compressCacheData = false; |
|
| 257 | - } |
|
| 258 | - else{ |
|
| 259 | - $this->compressCacheData = $status; |
|
| 260 | - } |
|
| 261 | - return $this; |
|
| 262 | - } |
|
| 255 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
| 256 | + $this->compressCacheData = false; |
|
| 257 | + } |
|
| 258 | + else{ |
|
| 259 | + $this->compressCacheData = $status; |
|
| 260 | + } |
|
| 261 | + return $this; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - /** |
|
| 265 | - * Check whether the cache feature for the handle is supported |
|
| 266 | - * |
|
| 267 | - * @return bool |
|
| 268 | - */ |
|
| 269 | - public function isSupported(){ |
|
| 270 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 271 | - } |
|
| 264 | + /** |
|
| 265 | + * Check whether the cache feature for the handle is supported |
|
| 266 | + * |
|
| 267 | + * @return bool |
|
| 268 | + */ |
|
| 269 | + public function isSupported(){ |
|
| 270 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Get the cache file full path for the given key |
|
| 276 | - * |
|
| 277 | - * @param string $key the cache item key |
|
| 278 | - * @return string the full cache file path for this key |
|
| 279 | - */ |
|
| 280 | - private function getFilePath($key){ |
|
| 281 | - return CACHE_PATH . md5($key) . '.cache'; |
|
| 282 | - } |
|
| 283 | - } |
|
| 274 | + /** |
|
| 275 | + * Get the cache file full path for the given key |
|
| 276 | + * |
|
| 277 | + * @param string $key the cache item key |
|
| 278 | + * @return string the full cache file path for this key |
|
| 279 | + */ |
|
| 280 | + private function getFilePath($key){ |
|
| 281 | + return CACHE_PATH . md5($key) . '.cache'; |
|
| 282 | + } |
|
| 283 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class FileCache extends BaseClass implements CacheInterface{ |
|
| 27 | + class FileCache extends BaseClass implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -35,14 +35,14 @@ discard block |
||
| 35 | 35 | /** |
| 36 | 36 | * Class constructor |
| 37 | 37 | */ |
| 38 | - public function __construct(){ |
|
| 38 | + public function __construct() { |
|
| 39 | 39 | parent::__construct(); |
| 40 | - if(! $this->isSupported()){ |
|
| 40 | + if (!$this->isSupported()) { |
|
| 41 | 41 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | //if Zlib extension is not loaded set compressCacheData to false |
| 45 | - if(! extension_loaded('zlib')){ |
|
| 45 | + if (!extension_loaded('zlib')) { |
|
| 46 | 46 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 47 | 47 | $this->compressCacheData = false; |
| 48 | 48 | } |
@@ -53,17 +53,17 @@ discard block |
||
| 53 | 53 | * @param string $key the key to identify the cache data |
| 54 | 54 | * @return mixed the cache data if exists else return false |
| 55 | 55 | */ |
| 56 | - public function get($key){ |
|
| 57 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 56 | + public function get($key) { |
|
| 57 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 58 | 58 | $filePath = $this->getFilePath($key); |
| 59 | - if(! file_exists($filePath)){ |
|
| 60 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 59 | + if (!file_exists($filePath)) { |
|
| 60 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 61 | 61 | return false; |
| 62 | 62 | } |
| 63 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 64 | - $handle = fopen($filePath,'r'); |
|
| 65 | - if(! is_resource($handle)){ |
|
| 66 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 63 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 64 | + $handle = fopen($filePath, 'r'); |
|
| 65 | + if (!is_resource($handle)) { |
|
| 66 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 67 | 67 | return false; |
| 68 | 68 | } |
| 69 | 69 | // Getting a shared lock |
@@ -71,20 +71,20 @@ discard block |
||
| 71 | 71 | $data = file_get_contents($filePath); |
| 72 | 72 | fclose($handle); |
| 73 | 73 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 74 | - if (! $data) { |
|
| 75 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 74 | + if (!$data) { |
|
| 75 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 76 | 76 | // If unserializing somehow didn't work out, we'll delete the file |
| 77 | 77 | unlink($filePath); |
| 78 | 78 | return false; |
| 79 | 79 | } |
| 80 | 80 | if (time() > $data['expire']) { |
| 81 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 81 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 82 | 82 | // Unlinking when the file was expired |
| 83 | 83 | unlink($filePath); |
| 84 | 84 | return false; |
| 85 | 85 | } |
| 86 | - else{ |
|
| 87 | - $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']) . ']'); |
|
| 86 | + else { |
|
| 87 | + $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']) . ']'); |
|
| 88 | 88 | return $data['data']; |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -97,13 +97,13 @@ discard block |
||
| 97 | 97 | * @param integer $ttl the cache life time |
| 98 | 98 | * @return boolean true if success otherwise will return false |
| 99 | 99 | */ |
| 100 | - public function set($key, $data, $ttl = 0){ |
|
| 100 | + public function set($key, $data, $ttl = 0) { |
|
| 101 | 101 | $expire = time() + $ttl; |
| 102 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 102 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 103 | 103 | $filePath = $this->getFilePath($key); |
| 104 | - $handle = fopen($filePath,'w'); |
|
| 105 | - if(! is_resource($handle)){ |
|
| 106 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 104 | + $handle = fopen($filePath, 'w'); |
|
| 105 | + if (!is_resource($handle)) { |
|
| 106 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 107 | 107 | return false; |
| 108 | 108 | } |
| 109 | 109 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -116,13 +116,13 @@ discard block |
||
| 116 | 116 | ) |
| 117 | 117 | ); |
| 118 | 118 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 119 | - if(! $result){ |
|
| 120 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 119 | + if (!$result) { |
|
| 120 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 121 | 121 | fclose($handle); |
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | - else{ |
|
| 125 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 124 | + else { |
|
| 125 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 126 | 126 | fclose($handle); |
| 127 | 127 | chmod($filePath, 0640); |
| 128 | 128 | return true; |
@@ -136,16 +136,16 @@ discard block |
||
| 136 | 136 | * @return boolean true if the cache is delete, false if can't delete |
| 137 | 137 | * the cache or the cache with the given key not exist |
| 138 | 138 | */ |
| 139 | - public function delete($key){ |
|
| 140 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 139 | + public function delete($key) { |
|
| 140 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 141 | 141 | $filePath = $this->getFilePath($key); |
| 142 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 143 | - if(! file_exists($filePath)){ |
|
| 142 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 143 | + if (!file_exists($filePath)) { |
|
| 144 | 144 | $this->logger->info('This cache file does not exists skipping'); |
| 145 | 145 | return false; |
| 146 | 146 | } |
| 147 | - else{ |
|
| 148 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 147 | + else { |
|
| 148 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 149 | 149 | unlink($filePath); |
| 150 | 150 | return true; |
| 151 | 151 | } |
@@ -159,23 +159,23 @@ discard block |
||
| 159 | 159 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 160 | 160 | * 'ttl' => the time to live of the cache in second |
| 161 | 161 | */ |
| 162 | - public function getInfo($key){ |
|
| 163 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 162 | + public function getInfo($key) { |
|
| 163 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 164 | 164 | $filePath = $this->getFilePath($key); |
| 165 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 166 | - if(! file_exists($filePath)){ |
|
| 165 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 166 | + if (!file_exists($filePath)) { |
|
| 167 | 167 | $this->logger->info('This cache file does not exists skipping'); |
| 168 | 168 | return false; |
| 169 | 169 | } |
| 170 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 170 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 171 | 171 | $data = file_get_contents($filePath); |
| 172 | 172 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 173 | - if(! $data){ |
|
| 173 | + if (!$data) { |
|
| 174 | 174 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 175 | 175 | return false; |
| 176 | 176 | } |
| 177 | 177 | $this->logger->info('This cache data is OK check for expire'); |
| 178 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 178 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 179 | 179 | $this->logger->info('This cache not yet expired return cache informations'); |
| 180 | 180 | $info = array( |
| 181 | 181 | 'mtime' => $data['mtime'], |
@@ -192,26 +192,26 @@ discard block |
||
| 192 | 192 | /** |
| 193 | 193 | * Used to delete expired cache data |
| 194 | 194 | */ |
| 195 | - public function deleteExpiredCache(){ |
|
| 195 | + public function deleteExpiredCache() { |
|
| 196 | 196 | $this->logger->debug('Deleting of expired cache files'); |
| 197 | 197 | $list = glob(CACHE_PATH . '*.cache'); |
| 198 | - if(! $list){ |
|
| 198 | + if (!$list) { |
|
| 199 | 199 | $this->logger->info('No cache files were found skipping'); |
| 200 | 200 | } |
| 201 | - else{ |
|
| 201 | + else { |
|
| 202 | 202 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 203 | 203 | foreach ($list as $file) { |
| 204 | 204 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 205 | 205 | $data = file_get_contents($file); |
| 206 | 206 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 207 | - if(! $data){ |
|
| 207 | + if (!$data) { |
|
| 208 | 208 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 209 | 209 | } |
| 210 | - else if(time() > $data['expire']){ |
|
| 210 | + else if (time() > $data['expire']) { |
|
| 211 | 211 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 212 | 212 | unlink($file); |
| 213 | 213 | } |
| 214 | - else{ |
|
| 214 | + else { |
|
| 215 | 215 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 216 | 216 | } |
| 217 | 217 | } |
@@ -221,13 +221,13 @@ discard block |
||
| 221 | 221 | /** |
| 222 | 222 | * Remove all file from cache folder |
| 223 | 223 | */ |
| 224 | - public function clean(){ |
|
| 224 | + public function clean() { |
|
| 225 | 225 | $this->logger->debug('Deleting of all cache files'); |
| 226 | 226 | $list = glob(CACHE_PATH . '*.cache'); |
| 227 | - if(! $list){ |
|
| 227 | + if (!$list) { |
|
| 228 | 228 | $this->logger->info('No cache files were found skipping'); |
| 229 | 229 | } |
| 230 | - else{ |
|
| 230 | + else { |
|
| 231 | 231 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 232 | 232 | foreach ($list as $file) { |
| 233 | 233 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | /** |
| 240 | 240 | * @return boolean |
| 241 | 241 | */ |
| 242 | - public function isCompressCacheData(){ |
|
| 242 | + public function isCompressCacheData() { |
|
| 243 | 243 | return $this->compressCacheData; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -248,14 +248,14 @@ discard block |
||
| 248 | 248 | * |
| 249 | 249 | * @return object |
| 250 | 250 | */ |
| 251 | - public function setCompressCacheData($status = true){ |
|
| 251 | + public function setCompressCacheData($status = true) { |
|
| 252 | 252 | //if Zlib extension is not loaded set compressCacheData to false |
| 253 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 253 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 254 | 254 | |
| 255 | 255 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 256 | 256 | $this->compressCacheData = false; |
| 257 | 257 | } |
| 258 | - else{ |
|
| 258 | + else { |
|
| 259 | 259 | $this->compressCacheData = $status; |
| 260 | 260 | } |
| 261 | 261 | return $this; |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | * |
| 267 | 267 | * @return bool |
| 268 | 268 | */ |
| 269 | - public function isSupported(){ |
|
| 269 | + public function isSupported() { |
|
| 270 | 270 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 271 | 271 | } |
| 272 | 272 | |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | * @param string $key the cache item key |
| 278 | 278 | * @return string the full cache file path for this key |
| 279 | 279 | */ |
| 280 | - private function getFilePath($key){ |
|
| 280 | + private function getFilePath($key) { |
|
| 281 | 281 | return CACHE_PATH . md5($key) . '.cache'; |
| 282 | 282 | } |
| 283 | 283 | } |
@@ -82,8 +82,7 @@ discard block |
||
| 82 | 82 | // Unlinking when the file was expired |
| 83 | 83 | unlink($filePath); |
| 84 | 84 | return false; |
| 85 | - } |
|
| 86 | - else{ |
|
| 85 | + } else{ |
|
| 87 | 86 | $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']) . ']'); |
| 88 | 87 | return $data['data']; |
| 89 | 88 | } |
@@ -120,8 +119,7 @@ discard block |
||
| 120 | 119 | $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
| 121 | 120 | fclose($handle); |
| 122 | 121 | return false; |
| 123 | - } |
|
| 124 | - else{ |
|
| 122 | + } else{ |
|
| 125 | 123 | $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
| 126 | 124 | fclose($handle); |
| 127 | 125 | chmod($filePath, 0640); |
@@ -143,8 +141,7 @@ discard block |
||
| 143 | 141 | if(! file_exists($filePath)){ |
| 144 | 142 | $this->logger->info('This cache file does not exists skipping'); |
| 145 | 143 | return false; |
| 146 | - } |
|
| 147 | - else{ |
|
| 144 | + } else{ |
|
| 148 | 145 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 149 | 146 | unlink($filePath); |
| 150 | 147 | return true; |
@@ -197,8 +194,7 @@ discard block |
||
| 197 | 194 | $list = glob(CACHE_PATH . '*.cache'); |
| 198 | 195 | if(! $list){ |
| 199 | 196 | $this->logger->info('No cache files were found skipping'); |
| 200 | - } |
|
| 201 | - else{ |
|
| 197 | + } else{ |
|
| 202 | 198 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 203 | 199 | foreach ($list as $file) { |
| 204 | 200 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -206,12 +202,10 @@ discard block |
||
| 206 | 202 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 207 | 203 | if(! $data){ |
| 208 | 204 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 209 | - } |
|
| 210 | - else if(time() > $data['expire']){ |
|
| 205 | + } else if(time() > $data['expire']){ |
|
| 211 | 206 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 212 | 207 | unlink($file); |
| 213 | - } |
|
| 214 | - else{ |
|
| 208 | + } else{ |
|
| 215 | 209 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 216 | 210 | } |
| 217 | 211 | } |
@@ -226,8 +220,7 @@ discard block |
||
| 226 | 220 | $list = glob(CACHE_PATH . '*.cache'); |
| 227 | 221 | if(! $list){ |
| 228 | 222 | $this->logger->info('No cache files were found skipping'); |
| 229 | - } |
|
| 230 | - else{ |
|
| 223 | + } else{ |
|
| 231 | 224 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 232 | 225 | foreach ($list as $file) { |
| 233 | 226 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -254,8 +247,7 @@ discard block |
||
| 254 | 247 | |
| 255 | 248 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 256 | 249 | $this->compressCacheData = false; |
| 257 | - } |
|
| 258 | - else{ |
|
| 250 | + } else{ |
|
| 259 | 251 | $this->compressCacheData = $status; |
| 260 | 252 | } |
| 261 | 253 | return $this; |
@@ -1,126 +1,126 @@ |
||
| 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 Controller extends BaseClass{ |
|
| 27 | + class Controller extends BaseClass{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The name of the module if this controller belong to an module |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $moduleName = null; |
|
| 29 | + /** |
|
| 30 | + * The name of the module if this controller belong to an module |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $moduleName = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The singleton of the super object |
|
| 37 | - * @var Controller |
|
| 38 | - */ |
|
| 39 | - private static $instance; |
|
| 35 | + /** |
|
| 36 | + * The singleton of the super object |
|
| 37 | + * @var Controller |
|
| 38 | + */ |
|
| 39 | + private static $instance; |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Class constructor |
|
| 44 | - */ |
|
| 45 | - public function __construct(){ |
|
| 46 | - parent::__construct(); |
|
| 42 | + /** |
|
| 43 | + * Class constructor |
|
| 44 | + */ |
|
| 45 | + public function __construct(){ |
|
| 46 | + parent::__construct(); |
|
| 47 | 47 | |
| 48 | - //instance of the super object |
|
| 49 | - self::$instance = & $this; |
|
| 48 | + //instance of the super object |
|
| 49 | + self::$instance = & $this; |
|
| 50 | 50 | |
| 51 | - //Load the resources loaded during the application bootstrap |
|
| 52 | - $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 53 | - foreach (class_loaded() as $var => $class){ |
|
| 54 | - $this->$var =& class_loader($class); |
|
| 55 | - } |
|
| 51 | + //Load the resources loaded during the application bootstrap |
|
| 52 | + $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 53 | + foreach (class_loaded() as $var => $class){ |
|
| 54 | + $this->$var =& class_loader($class); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - //set module using the router |
|
| 58 | - $this->setModuleNameFromRouter(); |
|
| 57 | + //set module using the router |
|
| 58 | + $this->setModuleNameFromRouter(); |
|
| 59 | 59 | |
| 60 | - //load the required resources |
|
| 61 | - $this->loadRequiredResources(); |
|
| 60 | + //load the required resources |
|
| 61 | + $this->loadRequiredResources(); |
|
| 62 | 62 | |
| 63 | - //set the cache using the configuration |
|
| 64 | - $this->setCacheFromParamOrConfig(null); |
|
| 63 | + //set the cache using the configuration |
|
| 64 | + $this->setCacheFromParamOrConfig(null); |
|
| 65 | 65 | |
| 66 | - //set application session configuration |
|
| 67 | - $this->logger->debug('Setting PHP application session handler'); |
|
| 68 | - set_session_config(); |
|
| 66 | + //set application session configuration |
|
| 67 | + $this->logger->debug('Setting PHP application session handler'); |
|
| 68 | + set_session_config(); |
|
| 69 | 69 | |
| 70 | - //dispatch the loaded instance of super controller event |
|
| 71 | - $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 72 | - } |
|
| 70 | + //dispatch the loaded instance of super controller event |
|
| 71 | + $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * This is a very useful method it's used to get the super object instance |
|
| 77 | - * @return Controller the super object instance |
|
| 78 | - */ |
|
| 79 | - public static function &get_instance(){ |
|
| 80 | - return self::$instance; |
|
| 81 | - } |
|
| 75 | + /** |
|
| 76 | + * This is a very useful method it's used to get the super object instance |
|
| 77 | + * @return Controller the super object instance |
|
| 78 | + */ |
|
| 79 | + public static function &get_instance(){ |
|
| 80 | + return self::$instance; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * This method is used to set the module name |
|
| 85 | - */ |
|
| 86 | - protected function setModuleNameFromRouter(){ |
|
| 87 | - //set the module using the router instance |
|
| 88 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 89 | - $this->moduleName = $this->router->getModule(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * This method is used to set the module name |
|
| 85 | + */ |
|
| 86 | + protected function setModuleNameFromRouter(){ |
|
| 87 | + //set the module using the router instance |
|
| 88 | + if(isset($this->router) && $this->router->getModule()){ |
|
| 89 | + $this->moduleName = $this->router->getModule(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Set the cache using the argument otherwise will use the configuration |
|
| 95 | - * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 96 | - */ |
|
| 97 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 98 | - $this->logger->debug('Setting the cache handler instance'); |
|
| 99 | - //set cache handler instance |
|
| 100 | - if(get_config('cache_enable', false)){ |
|
| 101 | - if ($cache !== null){ |
|
| 102 | - $this->cache = $cache; |
|
| 103 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 104 | - $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 105 | - unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 93 | + /** |
|
| 94 | + * Set the cache using the argument otherwise will use the configuration |
|
| 95 | + * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 96 | + */ |
|
| 97 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 98 | + $this->logger->debug('Setting the cache handler instance'); |
|
| 99 | + //set cache handler instance |
|
| 100 | + if(get_config('cache_enable', false)){ |
|
| 101 | + if ($cache !== null){ |
|
| 102 | + $this->cache = $cache; |
|
| 103 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 104 | + $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 105 | + unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * This method is used to load the required resources for framework to work |
|
| 113 | - * @return void |
|
| 114 | - */ |
|
| 115 | - private function loadRequiredResources(){ |
|
| 116 | - $this->logger->debug('Loading the required classes into super instance'); |
|
| 117 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 118 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 119 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 120 | - $this->request =& class_loader('Request', 'classes'); |
|
| 121 | - //dispatch the request instance created event |
|
| 122 | - $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 123 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 124 | - } |
|
| 111 | + /** |
|
| 112 | + * This method is used to load the required resources for framework to work |
|
| 113 | + * @return void |
|
| 114 | + */ |
|
| 115 | + private function loadRequiredResources(){ |
|
| 116 | + $this->logger->debug('Loading the required classes into super instance'); |
|
| 117 | + $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 118 | + $this->loader =& class_loader('Loader', 'classes'); |
|
| 119 | + $this->lang =& class_loader('Lang', 'classes'); |
|
| 120 | + $this->request =& class_loader('Request', 'classes'); |
|
| 121 | + //dispatch the request instance created event |
|
| 122 | + $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 123 | + $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - } |
|
| 126 | + } |
|
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Controller extends BaseClass{ |
|
| 27 | + class Controller extends BaseClass { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The name of the module if this controller belong to an module |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | /** |
| 43 | 43 | * Class constructor |
| 44 | 44 | */ |
| 45 | - public function __construct(){ |
|
| 45 | + public function __construct() { |
|
| 46 | 46 | parent::__construct(); |
| 47 | 47 | |
| 48 | 48 | //instance of the super object |
@@ -50,8 +50,8 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | //Load the resources loaded during the application bootstrap |
| 52 | 52 | $this->logger->debug('Adding the loaded classes to the super instance'); |
| 53 | - foreach (class_loaded() as $var => $class){ |
|
| 54 | - $this->$var =& class_loader($class); |
|
| 53 | + foreach (class_loaded() as $var => $class) { |
|
| 54 | + $this->$var = & class_loader($class); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //set module using the router |
@@ -83,9 +83,9 @@ discard block |
||
| 83 | 83 | /** |
| 84 | 84 | * This method is used to set the module name |
| 85 | 85 | */ |
| 86 | - protected function setModuleNameFromRouter(){ |
|
| 86 | + protected function setModuleNameFromRouter() { |
|
| 87 | 87 | //set the module using the router instance |
| 88 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 88 | + if (isset($this->router) && $this->router->getModule()) { |
|
| 89 | 89 | $this->moduleName = $this->router->getModule(); |
| 90 | 90 | } |
| 91 | 91 | } |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | * Set the cache using the argument otherwise will use the configuration |
| 95 | 95 | * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
| 96 | 96 | */ |
| 97 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 97 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null) { |
|
| 98 | 98 | $this->logger->debug('Setting the cache handler instance'); |
| 99 | 99 | //set cache handler instance |
| 100 | - if(get_config('cache_enable', false)){ |
|
| 101 | - if ($cache !== null){ |
|
| 100 | + if (get_config('cache_enable', false)) { |
|
| 101 | + if ($cache !== null) { |
|
| 102 | 102 | $this->cache = $cache; |
| 103 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 103 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})) { |
|
| 104 | 104 | $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
| 105 | 105 | unset($this->{strtolower(get_config('cache_handler'))}); |
| 106 | 106 | } |
@@ -112,15 +112,15 @@ discard block |
||
| 112 | 112 | * This method is used to load the required resources for framework to work |
| 113 | 113 | * @return void |
| 114 | 114 | */ |
| 115 | - private function loadRequiredResources(){ |
|
| 115 | + private function loadRequiredResources() { |
|
| 116 | 116 | $this->logger->debug('Loading the required classes into super instance'); |
| 117 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 118 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 119 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 120 | - $this->request =& class_loader('Request', 'classes'); |
|
| 117 | + $this->eventdispatcher = & class_loader('EventDispatcher', 'classes'); |
|
| 118 | + $this->loader = & class_loader('Loader', 'classes'); |
|
| 119 | + $this->lang = & class_loader('Lang', 'classes'); |
|
| 120 | + $this->request = & class_loader('Request', 'classes'); |
|
| 121 | 121 | //dispatch the request instance created event |
| 122 | 122 | $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
| 123 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 123 | + $this->response = & class_loader('Response', 'classes', 'classes'); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | } |
@@ -1,138 +1,138 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Security extends BaseStaticClass{ |
|
| 27 | + class Security extends BaseStaticClass{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This method is used to generate the CSRF token |
|
| 31 | - * @return string the generated CSRF token |
|
| 32 | - */ |
|
| 33 | - public static function generateCSRF(){ |
|
| 34 | - $logger = self::getLogger(); |
|
| 35 | - $logger->debug('Generation of CSRF ...'); |
|
| 29 | + /** |
|
| 30 | + * This method is used to generate the CSRF token |
|
| 31 | + * @return string the generated CSRF token |
|
| 32 | + */ |
|
| 33 | + public static function generateCSRF(){ |
|
| 34 | + $logger = self::getLogger(); |
|
| 35 | + $logger->debug('Generation of CSRF ...'); |
|
| 36 | 36 | |
| 37 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 38 | - $expire = get_config('csrf_expire', 60); |
|
| 39 | - $keyExpire = 'csrf_expire'; |
|
| 40 | - $currentTime = time(); |
|
| 41 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 42 | - $logger->info('The CSRF token not yet expire just return it'); |
|
| 43 | - return Session::get($key); |
|
| 44 | - } |
|
| 45 | - else{ |
|
| 46 | - $newTime = $currentTime + $expire; |
|
| 47 | - $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 48 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 49 | - Session::set($keyExpire, $newTime); |
|
| 50 | - Session::set($key, $token); |
|
| 51 | - return Session::get($key); |
|
| 52 | - } |
|
| 53 | - } |
|
| 37 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 38 | + $expire = get_config('csrf_expire', 60); |
|
| 39 | + $keyExpire = 'csrf_expire'; |
|
| 40 | + $currentTime = time(); |
|
| 41 | + if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 42 | + $logger->info('The CSRF token not yet expire just return it'); |
|
| 43 | + return Session::get($key); |
|
| 44 | + } |
|
| 45 | + else{ |
|
| 46 | + $newTime = $currentTime + $expire; |
|
| 47 | + $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 48 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 49 | + Session::set($keyExpire, $newTime); |
|
| 50 | + Session::set($key, $token); |
|
| 51 | + return Session::get($key); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 57 | - * @return boolean true if valid, false if not valid |
|
| 58 | - */ |
|
| 59 | - public static function validateCSRF(){ |
|
| 60 | - $logger = self::getLogger(); |
|
| 61 | - $logger->debug('Validation of CSRF ...'); |
|
| 55 | + /** |
|
| 56 | + * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 57 | + * @return boolean true if valid, false if not valid |
|
| 58 | + */ |
|
| 59 | + public static function validateCSRF(){ |
|
| 60 | + $logger = self::getLogger(); |
|
| 61 | + $logger->debug('Validation of CSRF ...'); |
|
| 62 | 62 | |
| 63 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 64 | - $expire = get_config('csrf_expire', 60); |
|
| 65 | - $keyExpire = 'csrf_expire'; |
|
| 66 | - $currentTime = time(); |
|
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 68 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 69 | - $logger->warning('The CSRF session data is not valide'); |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - //perform form data |
|
| 73 | - //need use request->query() for best retrieve |
|
| 74 | - //super instance |
|
| 75 | - $obj = & get_instance(); |
|
| 76 | - $token = $obj->request->query($key); |
|
| 77 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 78 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 82 | - //remove the token from session |
|
| 83 | - Session::clear($key); |
|
| 84 | - Session::clear($keyExpire); |
|
| 85 | - return true; |
|
| 86 | - } |
|
| 63 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 64 | + $expire = get_config('csrf_expire', 60); |
|
| 65 | + $keyExpire = 'csrf_expire'; |
|
| 66 | + $currentTime = time(); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 68 | + if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 69 | + $logger->warning('The CSRF session data is not valide'); |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + //perform form data |
|
| 73 | + //need use request->query() for best retrieve |
|
| 74 | + //super instance |
|
| 75 | + $obj = & get_instance(); |
|
| 76 | + $token = $obj->request->query($key); |
|
| 77 | + if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 78 | + $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 82 | + //remove the token from session |
|
| 83 | + Session::clear($key); |
|
| 84 | + Session::clear($keyExpire); |
|
| 85 | + return true; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * This method is used to check the whitelist IP address access |
|
| 90 | - */ |
|
| 91 | - public static function checkWhiteListIpAccess(){ |
|
| 92 | - $logger = self::getLogger(); |
|
| 93 | - $logger->debug('Validation of the IP address access ...'); |
|
| 94 | - $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 95 | - $isEnable = get_config('white_list_ip_enable', false); |
|
| 96 | - if($isEnable){ |
|
| 97 | - $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 98 | - $list = get_config('white_list_ip_addresses', array()); |
|
| 99 | - if(! empty($list)){ |
|
| 100 | - //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 101 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 102 | - $ip = get_ip(); |
|
| 103 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 104 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 105 | - //wildcard to access all ip address |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - else{ |
|
| 109 | - // go through all whitelisted ips |
|
| 110 | - foreach ($list as $ipaddr) { |
|
| 111 | - // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 112 | - $wildcardPosition = strpos($ipaddr, '*'); |
|
| 113 | - if ($wildcardPosition === false) { |
|
| 114 | - // no wild card in whitelisted ip --continue searching |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 118 | - // and add the wold card to get the same pattern |
|
| 119 | - if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 120 | - // f.e. we got |
|
| 121 | - // ip "127.0.0.1" |
|
| 122 | - // whitelisted ip "127.0.*" |
|
| 123 | - // then we compared "127.0.*" with "127.0.*" |
|
| 124 | - // return success |
|
| 125 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 126 | - return; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 130 | - show_error('Access to this application is not allowed'); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - else{ |
|
| 135 | - $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 88 | + /** |
|
| 89 | + * This method is used to check the whitelist IP address access |
|
| 90 | + */ |
|
| 91 | + public static function checkWhiteListIpAccess(){ |
|
| 92 | + $logger = self::getLogger(); |
|
| 93 | + $logger->debug('Validation of the IP address access ...'); |
|
| 94 | + $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 95 | + $isEnable = get_config('white_list_ip_enable', false); |
|
| 96 | + if($isEnable){ |
|
| 97 | + $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 98 | + $list = get_config('white_list_ip_addresses', array()); |
|
| 99 | + if(! empty($list)){ |
|
| 100 | + //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 101 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 102 | + $ip = get_ip(); |
|
| 103 | + if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 104 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 105 | + //wildcard to access all ip address |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + else{ |
|
| 109 | + // go through all whitelisted ips |
|
| 110 | + foreach ($list as $ipaddr) { |
|
| 111 | + // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 112 | + $wildcardPosition = strpos($ipaddr, '*'); |
|
| 113 | + if ($wildcardPosition === false) { |
|
| 114 | + // no wild card in whitelisted ip --continue searching |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 118 | + // and add the wold card to get the same pattern |
|
| 119 | + if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 120 | + // f.e. we got |
|
| 121 | + // ip "127.0.0.1" |
|
| 122 | + // whitelisted ip "127.0.*" |
|
| 123 | + // then we compared "127.0.*" with "127.0.*" |
|
| 124 | + // return success |
|
| 125 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 126 | + return; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 130 | + show_error('Access to this application is not allowed'); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + else{ |
|
| 135 | + $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Security extends BaseStaticClass{ |
|
| 27 | + class Security extends BaseStaticClass { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * This method is used to generate the CSRF token |
| 31 | 31 | * @return string the generated CSRF token |
| 32 | 32 | */ |
| 33 | - public static function generateCSRF(){ |
|
| 33 | + public static function generateCSRF() { |
|
| 34 | 34 | $logger = self::getLogger(); |
| 35 | 35 | $logger->debug('Generation of CSRF ...'); |
| 36 | 36 | |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | $expire = get_config('csrf_expire', 60); |
| 39 | 39 | $keyExpire = 'csrf_expire'; |
| 40 | 40 | $currentTime = time(); |
| 41 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 41 | + if (Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime) { |
|
| 42 | 42 | $logger->info('The CSRF token not yet expire just return it'); |
| 43 | 43 | return Session::get($key); |
| 44 | 44 | } |
| 45 | - else{ |
|
| 45 | + else { |
|
| 46 | 46 | $newTime = $currentTime + $expire; |
| 47 | 47 | $token = sha1(uniqid()) . sha1(uniqid()); |
| 48 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 48 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . '], token [' . $token . ']'); |
|
| 49 | 49 | Session::set($keyExpire, $newTime); |
| 50 | 50 | Session::set($key, $token); |
| 51 | 51 | return Session::get($key); |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | * This method is used to check the CSRF if is valid, not yet expire, etc. |
| 57 | 57 | * @return boolean true if valid, false if not valid |
| 58 | 58 | */ |
| 59 | - public static function validateCSRF(){ |
|
| 59 | + public static function validateCSRF() { |
|
| 60 | 60 | $logger = self::getLogger(); |
| 61 | 61 | $logger->debug('Validation of CSRF ...'); |
| 62 | 62 | |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | $expire = get_config('csrf_expire', 60); |
| 65 | 65 | $keyExpire = 'csrf_expire'; |
| 66 | 66 | $currentTime = time(); |
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 68 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . ']'); |
|
| 68 | + if (!Session::exists($key) || Session::get($keyExpire) <= $currentTime) { |
|
| 69 | 69 | $logger->warning('The CSRF session data is not valide'); |
| 70 | 70 | return false; |
| 71 | 71 | } |
@@ -74,11 +74,11 @@ discard block |
||
| 74 | 74 | //super instance |
| 75 | 75 | $obj = & get_instance(); |
| 76 | 76 | $token = $obj->request->query($key); |
| 77 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 78 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 77 | + if (!$token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime) { |
|
| 78 | + $logger->warning('The CSRF data [' . $token . '] is not valide may be attacker do his job'); |
|
| 79 | 79 | return false; |
| 80 | 80 | } |
| 81 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 81 | + $logger->info('The CSRF data [' . $token . '] is valide the form data is safe continue'); |
|
| 82 | 82 | //remove the token from session |
| 83 | 83 | Session::clear($key); |
| 84 | 84 | Session::clear($keyExpire); |
@@ -88,24 +88,24 @@ discard block |
||
| 88 | 88 | /** |
| 89 | 89 | * This method is used to check the whitelist IP address access |
| 90 | 90 | */ |
| 91 | - public static function checkWhiteListIpAccess(){ |
|
| 91 | + public static function checkWhiteListIpAccess() { |
|
| 92 | 92 | $logger = self::getLogger(); |
| 93 | 93 | $logger->debug('Validation of the IP address access ...'); |
| 94 | 94 | $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
| 95 | 95 | $isEnable = get_config('white_list_ip_enable', false); |
| 96 | - if($isEnable){ |
|
| 96 | + if ($isEnable) { |
|
| 97 | 97 | $logger->info('Whitelist IP access is enabled in the configuration'); |
| 98 | 98 | $list = get_config('white_list_ip_addresses', array()); |
| 99 | - if(! empty($list)){ |
|
| 99 | + if (!empty($list)) { |
|
| 100 | 100 | //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
| 101 | 101 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
| 102 | 102 | $ip = get_ip(); |
| 103 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 103 | + if ((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)) { |
|
| 104 | 104 | $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
| 105 | 105 | //wildcard to access all ip address |
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | - else{ |
|
| 108 | + else { |
|
| 109 | 109 | // go through all whitelisted ips |
| 110 | 110 | foreach ($list as $ipaddr) { |
| 111 | 111 | // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | - else{ |
|
| 134 | + else { |
|
| 135 | 135 | $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
| 136 | 136 | } |
| 137 | 137 | } |