@@ -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 | } |
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|