@@ -1,161 +1,161 @@ |
||
| 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{ |
|
| 27 | + class Security{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private static $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private static $logger; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the logger singleton instance |
|
| 37 | - * @return Log the logger instance |
|
| 38 | - */ |
|
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - self::$logger[0]->setLogger('Library::Security'); |
|
| 43 | - } |
|
| 44 | - return self::$logger[0]; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the logger singleton instance |
|
| 37 | + * @return Log the logger instance |
|
| 38 | + */ |
|
| 39 | + private static function getLogger(){ |
|
| 40 | + if(self::$logger == null){ |
|
| 41 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + self::$logger[0]->setLogger('Library::Security'); |
|
| 43 | + } |
|
| 44 | + return self::$logger[0]; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * This method is used to generate the CSRF token |
|
| 50 | - * @return string the generated CSRF token |
|
| 51 | - */ |
|
| 52 | - public static function generateCSRF(){ |
|
| 53 | - $logger = self::getLogger(); |
|
| 54 | - $logger->debug('Generation of CSRF ...'); |
|
| 48 | + /** |
|
| 49 | + * This method is used to generate the CSRF token |
|
| 50 | + * @return string the generated CSRF token |
|
| 51 | + */ |
|
| 52 | + public static function generateCSRF(){ |
|
| 53 | + $logger = self::getLogger(); |
|
| 54 | + $logger->debug('Generation of CSRF ...'); |
|
| 55 | 55 | |
| 56 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | - $expire = get_config('csrf_expire', 60); |
|
| 58 | - $keyExpire = 'csrf_expire'; |
|
| 59 | - $currentTime = time(); |
|
| 60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | - $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | - return Session::get($key); |
|
| 63 | - } |
|
| 64 | - else{ |
|
| 65 | - $newTime = $currentTime + $expire; |
|
| 66 | - $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | - Session::set($keyExpire, $newTime); |
|
| 69 | - Session::set($key, $token); |
|
| 70 | - return Session::get($key); |
|
| 71 | - } |
|
| 72 | - } |
|
| 56 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 57 | + $expire = get_config('csrf_expire', 60); |
|
| 58 | + $keyExpire = 'csrf_expire'; |
|
| 59 | + $currentTime = time(); |
|
| 60 | + if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 61 | + $logger->info('The CSRF token not yet expire just return it'); |
|
| 62 | + return Session::get($key); |
|
| 63 | + } |
|
| 64 | + else{ |
|
| 65 | + $newTime = $currentTime + $expire; |
|
| 66 | + $token = sha1(uniqid()) . sha1(uniqid()); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
| 68 | + Session::set($keyExpire, $newTime); |
|
| 69 | + Session::set($key, $token); |
|
| 70 | + return Session::get($key); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | - * @return boolean true if valid, false if not valid |
|
| 77 | - */ |
|
| 78 | - public static function validateCSRF(){ |
|
| 79 | - $logger = self::getLogger(); |
|
| 80 | - $logger->debug('Validation of CSRF ...'); |
|
| 74 | + /** |
|
| 75 | + * This method is used to check the CSRF if is valid, not yet expire, etc. |
|
| 76 | + * @return boolean true if valid, false if not valid |
|
| 77 | + */ |
|
| 78 | + public static function validateCSRF(){ |
|
| 79 | + $logger = self::getLogger(); |
|
| 80 | + $logger->debug('Validation of CSRF ...'); |
|
| 81 | 81 | |
| 82 | - $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | - $expire = get_config('csrf_expire', 60); |
|
| 84 | - $keyExpire = 'csrf_expire'; |
|
| 85 | - $currentTime = time(); |
|
| 86 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | - $logger->warning('The CSRF session data is not valide'); |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - else{ |
|
| 92 | - //perform form data |
|
| 93 | - //need use request->query() for best retrieve |
|
| 94 | - //super instance |
|
| 95 | - $obj = & get_instance(); |
|
| 96 | - $token = $obj->request->query($key); |
|
| 97 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 98 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - else{ |
|
| 102 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 103 | - //remove the token from session |
|
| 104 | - Session::clear($key); |
|
| 105 | - Session::clear($keyExpire); |
|
| 106 | - return true; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - } |
|
| 82 | + $key = get_config('csrf_key', 'csrf_key'); |
|
| 83 | + $expire = get_config('csrf_expire', 60); |
|
| 84 | + $keyExpire = 'csrf_expire'; |
|
| 85 | + $currentTime = time(); |
|
| 86 | + $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
| 87 | + if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 88 | + $logger->warning('The CSRF session data is not valide'); |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + else{ |
|
| 92 | + //perform form data |
|
| 93 | + //need use request->query() for best retrieve |
|
| 94 | + //super instance |
|
| 95 | + $obj = & get_instance(); |
|
| 96 | + $token = $obj->request->query($key); |
|
| 97 | + if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
| 98 | + $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + else{ |
|
| 102 | + $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 103 | + //remove the token from session |
|
| 104 | + Session::clear($key); |
|
| 105 | + Session::clear($keyExpire); |
|
| 106 | + return true; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * This method is used to check the whitelist IP address access |
|
| 113 | - */ |
|
| 114 | - public static function checkWhiteListIpAccess(){ |
|
| 115 | - $logger = self::getLogger(); |
|
| 116 | - $logger->debug('Validation of the IP address access ...'); |
|
| 117 | - $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 118 | - $isEnable = get_config('white_list_ip_enable', false); |
|
| 119 | - if($isEnable){ |
|
| 120 | - $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 121 | - $list = get_config('white_list_ip_addresses', array()); |
|
| 122 | - if(! empty($list)){ |
|
| 123 | - //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 124 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 125 | - $ip = get_ip(); |
|
| 126 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 127 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 128 | - //wildcard to access all ip address |
|
| 129 | - return; |
|
| 130 | - } |
|
| 131 | - else{ |
|
| 132 | - // go through all whitelisted ips |
|
| 133 | - foreach ($list as $ipaddr) { |
|
| 134 | - // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 135 | - $wildcardPosition = strpos($ipaddr, '*'); |
|
| 136 | - if ($wildcardPosition === false) { |
|
| 137 | - // no wild card in whitelisted ip --continue searching |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 141 | - // and add the wold card to get the same pattern |
|
| 142 | - if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 143 | - // f.e. we got |
|
| 144 | - // ip "127.0.0.1" |
|
| 145 | - // whitelisted ip "127.0.*" |
|
| 146 | - // then we compared "127.0.*" with "127.0.*" |
|
| 147 | - // return success |
|
| 148 | - $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 149 | - return; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 153 | - show_error('Access to this application is not allowed'); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - else{ |
|
| 158 | - $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - } |
|
| 111 | + /** |
|
| 112 | + * This method is used to check the whitelist IP address access |
|
| 113 | + */ |
|
| 114 | + public static function checkWhiteListIpAccess(){ |
|
| 115 | + $logger = self::getLogger(); |
|
| 116 | + $logger->debug('Validation of the IP address access ...'); |
|
| 117 | + $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
|
| 118 | + $isEnable = get_config('white_list_ip_enable', false); |
|
| 119 | + if($isEnable){ |
|
| 120 | + $logger->info('Whitelist IP access is enabled in the configuration'); |
|
| 121 | + $list = get_config('white_list_ip_addresses', array()); |
|
| 122 | + if(! empty($list)){ |
|
| 123 | + //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
|
| 124 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 125 | + $ip = get_ip(); |
|
| 126 | + if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 127 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
|
| 128 | + //wildcard to access all ip address |
|
| 129 | + return; |
|
| 130 | + } |
|
| 131 | + else{ |
|
| 132 | + // go through all whitelisted ips |
|
| 133 | + foreach ($list as $ipaddr) { |
|
| 134 | + // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
|
| 135 | + $wildcardPosition = strpos($ipaddr, '*'); |
|
| 136 | + if ($wildcardPosition === false) { |
|
| 137 | + // no wild card in whitelisted ip --continue searching |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + // cut ip at the position where we got the wild card on the whitelisted ip |
|
| 141 | + // and add the wold card to get the same pattern |
|
| 142 | + if (substr($ip, 0, $wildcardPosition) . '*' === $ipaddr) { |
|
| 143 | + // f.e. we got |
|
| 144 | + // ip "127.0.0.1" |
|
| 145 | + // whitelisted ip "127.0.*" |
|
| 146 | + // then we compared "127.0.*" with "127.0.*" |
|
| 147 | + // return success |
|
| 148 | + $logger->info('IP address ' . $ip . ' allowed using the wildcard like "x.x.x.*"'); |
|
| 149 | + return; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + $logger->warning('IP address ' . $ip . ' is not allowed to access to this application'); |
|
| 153 | + show_error('Access to this application is not allowed'); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + else{ |
|
| 158 | + $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + } |
|
@@ -1,43 +1,43 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class DatabaseQueryResult{ |
|
| 27 | + class DatabaseQueryResult{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The database query result |
|
| 31 | - * @var mixed |
|
| 32 | - */ |
|
| 33 | - private $result = null; |
|
| 29 | + /** |
|
| 30 | + * The database query result |
|
| 31 | + * @var mixed |
|
| 32 | + */ |
|
| 33 | + private $result = null; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The number of rows returned by the last query |
|
| 38 | - * @var int |
|
| 39 | - */ |
|
| 40 | - private $numRows = 0; |
|
| 36 | + /** |
|
| 37 | + * The number of rows returned by the last query |
|
| 38 | + * @var int |
|
| 39 | + */ |
|
| 40 | + private $numRows = 0; |
|
| 41 | 41 | |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -51,24 +51,24 @@ discard block |
||
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Return the query result |
|
| 56 | - * |
|
| 57 | - * @return mixed |
|
| 58 | - */ |
|
| 54 | + /** |
|
| 55 | + * Return the query result |
|
| 56 | + * |
|
| 57 | + * @return mixed |
|
| 58 | + */ |
|
| 59 | 59 | public function getResult(){ |
| 60 | - return $this->result; |
|
| 60 | + return $this->result; |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Set the query result |
| 65 | 65 | * @param mixed $result the query result |
| 66 | 66 | * |
| 67 | - * @return object DatabaseQueryResult |
|
| 67 | + * @return object DatabaseQueryResult |
|
| 68 | 68 | */ |
| 69 | 69 | public function setResult($result){ |
| 70 | - $this->result = $result; |
|
| 71 | - return $this; |
|
| 70 | + $this->result = $result; |
|
| 71 | + return $this; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -77,18 +77,18 @@ discard block |
||
| 77 | 77 | * @return int |
| 78 | 78 | */ |
| 79 | 79 | public function getNumRows(){ |
| 80 | - return $this->numRows; |
|
| 80 | + return $this->numRows; |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | 84 | * Set the number of rows returned by the query |
| 85 | 85 | * @param int $numRows the number of rows returned |
| 86 | 86 | * |
| 87 | - * @return object DatabaseQueryResult |
|
| 87 | + * @return object DatabaseQueryResult |
|
| 88 | 88 | */ |
| 89 | 89 | public function setNumRows($numRows){ |
| 90 | - $this->numRows = $numRows; |
|
| 91 | - return $this; |
|
| 90 | + $this->numRows = $numRows; |
|
| 91 | + return $this; |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | } |
@@ -1,108 +1,108 @@ 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 DatabaseQueryBuilder{ |
|
| 27 | - /** |
|
| 28 | - * The SQL SELECT statment |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - private $select = '*'; |
|
| 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 DatabaseQueryBuilder{ |
|
| 27 | + /** |
|
| 28 | + * The SQL SELECT statment |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + private $select = '*'; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * The SQL FROM statment |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $from = null; |
|
| 33 | + /** |
|
| 34 | + * The SQL FROM statment |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The SQL WHERE statment |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - private $where = null; |
|
| 39 | + /** |
|
| 40 | + * The SQL WHERE statment |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The SQL LIMIT statment |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private $limit = null; |
|
| 45 | + /** |
|
| 46 | + * The SQL LIMIT statment |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * The SQL JOIN statment |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - private $join = null; |
|
| 51 | + /** |
|
| 52 | + * The SQL JOIN statment |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * The SQL ORDER BY statment |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private $orderBy = null; |
|
| 57 | + /** |
|
| 58 | + * The SQL ORDER BY statment |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * The SQL GROUP BY statment |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private $groupBy = null; |
|
| 63 | + /** |
|
| 64 | + * The SQL GROUP BY statment |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * The SQL HAVING statment |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - private $having = null; |
|
| 69 | + /** |
|
| 70 | + * The SQL HAVING statment |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * The full SQL query statment after build for each command |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - private $query = null; |
|
| 75 | + /** |
|
| 76 | + * The full SQL query statment after build for each command |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * The list of SQL valid operators |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 81 | + /** |
|
| 82 | + * The list of SQL valid operators |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | 85 | private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
| 86 | 86 | |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The prefix used in each database table |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The prefix used in each database table |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | 92 | private $prefix = null; |
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | - * The PDO instance |
|
| 97 | - * @var object |
|
| 98 | - */ |
|
| 96 | + * The PDO instance |
|
| 97 | + * @var object |
|
| 98 | + */ |
|
| 99 | 99 | private $pdo = null; |
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * The database driver name used |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - private $driver = null; |
|
| 101 | + /** |
|
| 102 | + * The database driver name used |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + private $driver = null; |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function __construct(PDO $pdo = null){ |
| 113 | 113 | if (is_object($pdo)){ |
| 114 | - $this->setPdo($pdo); |
|
| 114 | + $this->setPdo($pdo); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | |
@@ -121,16 +121,16 @@ discard block |
||
| 121 | 121 | * @return object the current DatabaseQueryBuilder instance |
| 122 | 122 | */ |
| 123 | 123 | public function from($table){ |
| 124 | - if (is_array($table)){ |
|
| 124 | + if (is_array($table)){ |
|
| 125 | 125 | $froms = ''; |
| 126 | 126 | foreach($table as $key){ |
| 127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
| 127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | 128 | } |
| 129 | 129 | $this->from = rtrim($froms, ', '); |
| 130 | - } else { |
|
| 130 | + } else { |
|
| 131 | 131 | $this->from = $this->getPrefix() . $table; |
| 132 | - } |
|
| 133 | - return $this; |
|
| 132 | + } |
|
| 133 | + return $this; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | * @return object the current DatabaseQueryBuilder instance |
| 140 | 140 | */ |
| 141 | 141 | public function select($fields){ |
| 142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | - return $this; |
|
| 142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | + return $this; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -150,19 +150,19 @@ discard block |
||
| 150 | 150 | * @return object the current DatabaseQueryBuilder instance |
| 151 | 151 | */ |
| 152 | 152 | public function distinct($field){ |
| 153 | - $distinct = ' DISTINCT ' . $field; |
|
| 154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | - return $this; |
|
| 153 | + $distinct = ' DISTINCT ' . $field; |
|
| 154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | + return $this; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Set the SQL function COUNT in SELECT statment |
|
| 160 | - * @param string $field the field name |
|
| 161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | - * @return object the current DatabaseQueryBuilder instance |
|
| 163 | - */ |
|
| 158 | + /** |
|
| 159 | + * Set the SQL function COUNT in SELECT statment |
|
| 160 | + * @param string $field the field name |
|
| 161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | + * @return object the current DatabaseQueryBuilder instance |
|
| 163 | + */ |
|
| 164 | 164 | public function count($field = '*', $name = null){ |
| 165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @return object the current DatabaseQueryBuilder instance |
| 173 | 173 | */ |
| 174 | 174 | public function min($field, $name = null){ |
| 175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return object the current DatabaseQueryBuilder instance |
| 183 | 183 | */ |
| 184 | 184 | public function max($field, $name = null){ |
| 185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | * @return object the current DatabaseQueryBuilder instance |
| 193 | 193 | */ |
| 194 | 194 | public function sum($field, $name = null){ |
| 195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * @return object the current DatabaseQueryBuilder instance |
| 203 | 203 | */ |
| 204 | 204 | public function avg($field, $name = null){ |
| 205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | |
@@ -216,20 +216,20 @@ discard block |
||
| 216 | 216 | * @return object the current DatabaseQueryBuilder instance |
| 217 | 217 | */ |
| 218 | 218 | public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
| 219 | - $on = $field1; |
|
| 220 | - $table = $this->getPrefix() . $table; |
|
| 221 | - if (! is_null($op)){ |
|
| 219 | + $on = $field1; |
|
| 220 | + $table = $this->getPrefix() . $table; |
|
| 221 | + if (! is_null($op)){ |
|
| 222 | 222 | $on = (! in_array($op, $this->operatorList) |
| 223 | - ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | - : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | - } |
|
| 226 | - if (empty($this->join)){ |
|
| 223 | + ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | + : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | + } |
|
| 226 | + if (empty($this->join)){ |
|
| 227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 228 | - } |
|
| 229 | - else{ |
|
| 228 | + } |
|
| 229 | + else{ |
|
| 230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 231 | - } |
|
| 232 | - return $this; |
|
| 231 | + } |
|
| 232 | + return $this; |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | * @return object the current DatabaseQueryBuilder instance |
| 239 | 239 | */ |
| 240 | 240 | public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
| 241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -247,16 +247,16 @@ discard block |
||
| 247 | 247 | * @return object the current DatabaseQueryBuilder instance |
| 248 | 248 | */ |
| 249 | 249 | public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
| 250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | - } |
|
| 250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 253 | + /** |
|
| 254 | 254 | * Set the SQL RIGHT JOIN statment |
| 255 | 255 | * @see DatabaseQueryBuilder::join() |
| 256 | 256 | * @return object the current DatabaseQueryBuilder instance |
| 257 | 257 | */ |
| 258 | 258 | public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
| 259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * @return object the current DatabaseQueryBuilder instance |
| 266 | 266 | */ |
| 267 | 267 | public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * @return object the current DatabaseQueryBuilder instance |
| 275 | 275 | */ |
| 276 | 276 | public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | * @return object the current DatabaseQueryBuilder instance |
| 284 | 284 | */ |
| 285 | 285 | public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | /** |
@@ -293,14 +293,14 @@ discard block |
||
| 293 | 293 | * @return object the current DatabaseQueryBuilder instance |
| 294 | 294 | */ |
| 295 | 295 | public function whereIsNull($field, $andOr = 'AND'){ |
| 296 | - if (is_array($field)){ |
|
| 296 | + if (is_array($field)){ |
|
| 297 | 297 | foreach($field as $f){ |
| 298 | - $this->whereIsNull($f, $andOr); |
|
| 298 | + $this->whereIsNull($f, $andOr); |
|
| 299 | 299 | } |
| 300 | - } else { |
|
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 300 | + } else { |
|
| 301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | + } |
|
| 303 | + return $this; |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -310,14 +310,14 @@ discard block |
||
| 310 | 310 | * @return object the current DatabaseQueryBuilder instance |
| 311 | 311 | */ |
| 312 | 312 | public function whereIsNotNull($field, $andOr = 'AND'){ |
| 313 | - if (is_array($field)){ |
|
| 313 | + if (is_array($field)){ |
|
| 314 | 314 | foreach($field as $f){ |
| 315 | - $this->whereIsNotNull($f, $andOr); |
|
| 315 | + $this->whereIsNotNull($f, $andOr); |
|
| 316 | + } |
|
| 317 | + } else { |
|
| 318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 316 | 319 | } |
| 317 | - } else { |
|
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | - } |
|
| 320 | - return $this; |
|
| 320 | + return $this; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | /** |
@@ -331,19 +331,19 @@ discard block |
||
| 331 | 331 | * @return object the current DatabaseQueryBuilder instance |
| 332 | 332 | */ |
| 333 | 333 | public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
| 334 | - $whereStr = ''; |
|
| 335 | - if (is_array($where)){ |
|
| 334 | + $whereStr = ''; |
|
| 335 | + if (is_array($where)){ |
|
| 336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 337 | - } |
|
| 338 | - else{ |
|
| 337 | + } |
|
| 338 | + else{ |
|
| 339 | 339 | if (is_array($op)){ |
| 340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | 341 | } else { |
| 342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | + } |
|
| 343 | 344 | } |
| 344 | - } |
|
| 345 | - $this->setWhereStr($whereStr, $andOr); |
|
| 346 | - return $this; |
|
| 345 | + $this->setWhereStr($whereStr, $andOr); |
|
| 346 | + return $this; |
|
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | /** |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @return object the current DatabaseQueryBuilder instance |
| 353 | 353 | */ |
| 354 | 354 | public function orWhere($where, $op = null, $val = null, $escape = true){ |
| 355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * @return object the current DatabaseQueryBuilder instance |
| 363 | 363 | */ |
| 364 | 364 | public function notWhere($where, $op = null, $val = null, $escape = true){ |
| 365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /** |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * @return object the current DatabaseQueryBuilder instance |
| 372 | 372 | */ |
| 373 | 373 | public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
| 374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -381,16 +381,16 @@ discard block |
||
| 381 | 381 | * @return object the current DatabaseQueryBuilder instance |
| 382 | 382 | */ |
| 383 | 383 | public function groupStart($type = '', $andOr = ' AND'){ |
| 384 | - if (empty($this->where)){ |
|
| 384 | + if (empty($this->where)){ |
|
| 385 | 385 | $this->where = $type . ' ('; |
| 386 | - } else { |
|
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 386 | + } else { |
|
| 387 | + if (substr(trim($this->where), -1) == '('){ |
|
| 388 | 388 | $this->where .= $type . ' ('; |
| 389 | - } else { |
|
| 390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - return $this; |
|
| 389 | + } else { |
|
| 390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + return $this; |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | /** |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @return object the current DatabaseQueryBuilder instance |
| 400 | 400 | */ |
| 401 | 401 | public function notGroupStart(){ |
| 402 | - return $this->groupStart('NOT'); |
|
| 402 | + return $this->groupStart('NOT'); |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | /** |
@@ -408,16 +408,16 @@ discard block |
||
| 408 | 408 | * @return object the current DatabaseQueryBuilder instance |
| 409 | 409 | */ |
| 410 | 410 | public function orGroupStart(){ |
| 411 | - return $this->groupStart('', ' OR'); |
|
| 411 | + return $this->groupStart('', ' OR'); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | - * @return object the current DatabaseQueryBuilder instance |
|
| 418 | - */ |
|
| 414 | + /** |
|
| 415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | + * @return object the current DatabaseQueryBuilder instance |
|
| 418 | + */ |
|
| 419 | 419 | public function orNotGroupStart(){ |
| 420 | - return $this->groupStart('NOT', ' OR'); |
|
| 420 | + return $this->groupStart('NOT', ' OR'); |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | 423 | /** |
@@ -425,8 +425,8 @@ discard block |
||
| 425 | 425 | * @return object the current DatabaseQueryBuilder instance |
| 426 | 426 | */ |
| 427 | 427 | public function groupEnd(){ |
| 428 | - $this->where .= ')'; |
|
| 429 | - return $this; |
|
| 428 | + $this->where .= ')'; |
|
| 429 | + return $this; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | /** |
@@ -439,17 +439,17 @@ discard block |
||
| 439 | 439 | * @return object the current DatabaseQueryBuilder instance |
| 440 | 440 | */ |
| 441 | 441 | public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
| 442 | - $_keys = array(); |
|
| 443 | - foreach ($keys as $k => $v){ |
|
| 442 | + $_keys = array(); |
|
| 443 | + foreach ($keys as $k => $v){ |
|
| 444 | 444 | if (is_null($v)){ |
| 445 | - $v = ''; |
|
| 445 | + $v = ''; |
|
| 446 | 446 | } |
| 447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
| 448 | - } |
|
| 449 | - $keys = implode(', ', $_keys); |
|
| 450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | - $this->setWhereStr($whereStr, $andOr); |
|
| 452 | - return $this; |
|
| 448 | + } |
|
| 449 | + $keys = implode(', ', $_keys); |
|
| 450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | + $this->setWhereStr($whereStr, $andOr); |
|
| 452 | + return $this; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | * @return object the current DatabaseQueryBuilder instance |
| 459 | 459 | */ |
| 460 | 460 | public function notIn($field, array $keys, $escape = true){ |
| 461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | * @return object the current DatabaseQueryBuilder instance |
| 468 | 468 | */ |
| 469 | 469 | public function orIn($field, array $keys, $escape = true){ |
| 470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | /** |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | * @return object the current DatabaseQueryBuilder instance |
| 477 | 477 | */ |
| 478 | 478 | public function orNotIn($field, array $keys, $escape = true){ |
| 479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -490,15 +490,15 @@ discard block |
||
| 490 | 490 | * @return object the current DatabaseQueryBuilder instance |
| 491 | 491 | */ |
| 492 | 492 | public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
| 493 | - if (is_null($value1)){ |
|
| 493 | + if (is_null($value1)){ |
|
| 494 | 494 | $value1 = ''; |
| 495 | - } |
|
| 496 | - if (is_null($value2)){ |
|
| 495 | + } |
|
| 496 | + if (is_null($value2)){ |
|
| 497 | 497 | $value2 = ''; |
| 498 | - } |
|
| 499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | - $this->setWhereStr($whereStr, $andOr); |
|
| 501 | - return $this; |
|
| 498 | + } |
|
| 499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | + $this->setWhereStr($whereStr, $andOr); |
|
| 501 | + return $this; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | /** |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | * @return object the current DatabaseQueryBuilder instance |
| 508 | 508 | */ |
| 509 | 509 | public function notBetween($field, $value1, $value2, $escape = true){ |
| 510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | * @return object the current DatabaseQueryBuilder instance |
| 517 | 517 | */ |
| 518 | 518 | public function orBetween($field, $value1, $value2, $escape = true){ |
| 519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | /** |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | * @return object the current DatabaseQueryBuilder instance |
| 526 | 526 | */ |
| 527 | 527 | public function orNotBetween($field, $value1, $value2, $escape = true){ |
| 528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | /** |
@@ -538,11 +538,11 @@ discard block |
||
| 538 | 538 | * @return object the current DatabaseQueryBuilder instance |
| 539 | 539 | */ |
| 540 | 540 | public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
| 541 | - if (empty($data)){ |
|
| 541 | + if (empty($data)){ |
|
| 542 | 542 | $data = ''; |
| 543 | - } |
|
| 544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | - return $this; |
|
| 543 | + } |
|
| 544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | + return $this; |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | * @return object the current DatabaseQueryBuilder instance |
| 552 | 552 | */ |
| 553 | 553 | public function orLike($field, $data, $escape = true){ |
| 554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | /** |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | * @return object the current DatabaseQueryBuilder instance |
| 561 | 561 | */ |
| 562 | 562 | public function notLike($field, $data, $escape = true){ |
| 563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | /** |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | * @return object the current DatabaseQueryBuilder instance |
| 570 | 570 | */ |
| 571 | 571 | public function orNotLike($field, $data, $escape = true){ |
| 572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** |
@@ -580,16 +580,16 @@ discard block |
||
| 580 | 580 | * @return object the current DatabaseQueryBuilder instance |
| 581 | 581 | */ |
| 582 | 582 | public function limit($limit, $limitEnd = null){ |
| 583 | - if (empty($limit)){ |
|
| 583 | + if (empty($limit)){ |
|
| 584 | 584 | $limit = 0; |
| 585 | - } |
|
| 586 | - if (! is_null($limitEnd)){ |
|
| 585 | + } |
|
| 586 | + if (! is_null($limitEnd)){ |
|
| 587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
| 588 | - } |
|
| 589 | - else{ |
|
| 588 | + } |
|
| 589 | + else{ |
|
| 590 | 590 | $this->limit = $limit; |
| 591 | - } |
|
| 592 | - return $this; |
|
| 591 | + } |
|
| 592 | + return $this; |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -599,15 +599,15 @@ discard block |
||
| 599 | 599 | * @return object the current DatabaseQueryBuilder instance |
| 600 | 600 | */ |
| 601 | 601 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 604 | - } |
|
| 605 | - else{ |
|
| 604 | + } |
|
| 605 | + else{ |
|
| 606 | 606 | $this->orderBy = empty($this->orderBy) |
| 607 | - ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | - : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | - } |
|
| 610 | - return $this; |
|
| 607 | + ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | + : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | + } |
|
| 610 | + return $this; |
|
| 611 | 611 | } |
| 612 | 612 | |
| 613 | 613 | /** |
@@ -616,13 +616,13 @@ discard block |
||
| 616 | 616 | * @return object the current DatabaseQueryBuilder instance |
| 617 | 617 | */ |
| 618 | 618 | public function groupBy($field){ |
| 619 | - if (is_array($field)){ |
|
| 619 | + if (is_array($field)){ |
|
| 620 | 620 | $this->groupBy = implode(', ', $field); |
| 621 | - } |
|
| 622 | - else{ |
|
| 621 | + } |
|
| 622 | + else{ |
|
| 623 | 623 | $this->groupBy = $field; |
| 624 | - } |
|
| 625 | - return $this; |
|
| 624 | + } |
|
| 625 | + return $this; |
|
| 626 | 626 | } |
| 627 | 627 | |
| 628 | 628 | /** |
@@ -634,22 +634,22 @@ discard block |
||
| 634 | 634 | * @return object the current DatabaseQueryBuilder instance |
| 635 | 635 | */ |
| 636 | 636 | public function having($field, $op = null, $val = null, $escape = true){ |
| 637 | - if (is_array($op)){ |
|
| 637 | + if (is_array($op)){ |
|
| 638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 639 | + } |
|
| 640 | + else if (! in_array($op, $this->operatorList)){ |
|
| 641 | 641 | if (is_null($op)){ |
| 642 | - $op = ''; |
|
| 642 | + $op = ''; |
|
| 643 | 643 | } |
| 644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 645 | - } |
|
| 646 | - else{ |
|
| 645 | + } |
|
| 646 | + else{ |
|
| 647 | 647 | if (is_null($val)){ |
| 648 | - $val = ''; |
|
| 648 | + $val = ''; |
|
| 649 | 649 | } |
| 650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
| 651 | - } |
|
| 652 | - return $this; |
|
| 651 | + } |
|
| 652 | + return $this; |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | /** |
@@ -659,12 +659,12 @@ discard block |
||
| 659 | 659 | * @return object the current DatabaseQueryBuilder instance |
| 660 | 660 | */ |
| 661 | 661 | public function insert($data = array(), $escape = true){ |
| 662 | - $columns = array_keys($data); |
|
| 663 | - $column = implode(', ', $columns); |
|
| 664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 662 | + $columns = array_keys($data); |
|
| 663 | + $column = implode(', ', $columns); |
|
| 664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | 665 | |
| 666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | - return $this; |
|
| 666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | + return $this; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | /** |
@@ -674,25 +674,25 @@ discard block |
||
| 674 | 674 | * @return object the current DatabaseQueryBuilder instance |
| 675 | 675 | */ |
| 676 | 676 | public function update($data = array(), $escape = true){ |
| 677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | - $values = array(); |
|
| 679 | - foreach ($data as $column => $val){ |
|
| 677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | + $values = array(); |
|
| 679 | + foreach ($data as $column => $val){ |
|
| 680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
| 681 | - } |
|
| 682 | - $query .= implode(', ', $values); |
|
| 683 | - if (! empty($this->where)){ |
|
| 681 | + } |
|
| 682 | + $query .= implode(', ', $values); |
|
| 683 | + if (! empty($this->where)){ |
|
| 684 | 684 | $query .= ' WHERE ' . $this->where; |
| 685 | - } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - if (! empty($this->orderBy)){ |
|
| 687 | + if (! empty($this->orderBy)){ |
|
| 688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
| 689 | - } |
|
| 689 | + } |
|
| 690 | 690 | |
| 691 | - if (! empty($this->limit)){ |
|
| 691 | + if (! empty($this->limit)){ |
|
| 692 | 692 | $query .= ' LIMIT ' . $this->limit; |
| 693 | - } |
|
| 694 | - $this->query = $query; |
|
| 695 | - return $this; |
|
| 693 | + } |
|
| 694 | + $this->query = $query; |
|
| 695 | + return $this; |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -700,25 +700,25 @@ discard block |
||
| 700 | 700 | * @return object the current DatabaseQueryBuilder instance |
| 701 | 701 | */ |
| 702 | 702 | public function delete(){ |
| 703 | - $query = 'DELETE FROM ' . $this->from; |
|
| 704 | - $isTruncate = $query; |
|
| 705 | - if (! empty($this->where)){ |
|
| 706 | - $query .= ' WHERE ' . $this->where; |
|
| 707 | - } |
|
| 703 | + $query = 'DELETE FROM ' . $this->from; |
|
| 704 | + $isTruncate = $query; |
|
| 705 | + if (! empty($this->where)){ |
|
| 706 | + $query .= ' WHERE ' . $this->where; |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | - } |
|
| 709 | + if (! empty($this->orderBy)){ |
|
| 710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 714 | - $query .= ' LIMIT ' . $this->limit; |
|
| 715 | - } |
|
| 713 | + if (! empty($this->limit)){ |
|
| 714 | + $query .= ' LIMIT ' . $this->limit; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | - } |
|
| 720 | - $this->query = $query; |
|
| 721 | - return $this; |
|
| 717 | + if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | + } |
|
| 720 | + $this->query = $query; |
|
| 721 | + return $this; |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | /** |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | * @return mixed the data after escaped or the same data if not |
| 729 | 729 | */ |
| 730 | 730 | public function escape($data, $escaped = true){ |
| 731 | - return $escaped |
|
| 731 | + return $escaped |
|
| 732 | 732 | ? $this->getPdo()->quote(trim($data)) |
| 733 | 733 | : $data; |
| 734 | 734 | } |
@@ -739,126 +739,126 @@ discard block |
||
| 739 | 739 | * @return string |
| 740 | 740 | */ |
| 741 | 741 | public function getQuery(){ |
| 742 | - //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 743 | - if(empty($this->query)){ |
|
| 744 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 745 | - if (! empty($this->join)){ |
|
| 746 | - $query .= $this->join; |
|
| 747 | - } |
|
| 742 | + //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 743 | + if(empty($this->query)){ |
|
| 744 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 745 | + if (! empty($this->join)){ |
|
| 746 | + $query .= $this->join; |
|
| 747 | + } |
|
| 748 | 748 | |
| 749 | - if (! empty($this->where)){ |
|
| 750 | - $query .= ' WHERE ' . $this->where; |
|
| 751 | - } |
|
| 749 | + if (! empty($this->where)){ |
|
| 750 | + $query .= ' WHERE ' . $this->where; |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - if (! empty($this->groupBy)){ |
|
| 754 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | - } |
|
| 753 | + if (! empty($this->groupBy)){ |
|
| 754 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | + } |
|
| 756 | 756 | |
| 757 | - if (! empty($this->having)){ |
|
| 758 | - $query .= ' HAVING ' . $this->having; |
|
| 759 | - } |
|
| 757 | + if (! empty($this->having)){ |
|
| 758 | + $query .= ' HAVING ' . $this->having; |
|
| 759 | + } |
|
| 760 | 760 | |
| 761 | - if (! empty($this->orderBy)){ |
|
| 762 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 763 | - } |
|
| 761 | + if (! empty($this->orderBy)){ |
|
| 762 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 763 | + } |
|
| 764 | 764 | |
| 765 | - if (! empty($this->limit)){ |
|
| 766 | - $query .= ' LIMIT ' . $this->limit; |
|
| 767 | - } |
|
| 768 | - $this->query = $query; |
|
| 769 | - } |
|
| 770 | - return $this->query; |
|
| 765 | + if (! empty($this->limit)){ |
|
| 766 | + $query .= ' LIMIT ' . $this->limit; |
|
| 767 | + } |
|
| 768 | + $this->query = $query; |
|
| 769 | + } |
|
| 770 | + return $this->query; |
|
| 771 | 771 | } |
| 772 | 772 | |
| 773 | 773 | |
| 774 | - /** |
|
| 775 | - * Return the PDO instance |
|
| 776 | - * @return object |
|
| 777 | - */ |
|
| 774 | + /** |
|
| 775 | + * Return the PDO instance |
|
| 776 | + * @return object |
|
| 777 | + */ |
|
| 778 | 778 | public function getPdo(){ |
| 779 | - return $this->pdo; |
|
| 779 | + return $this->pdo; |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | /** |
| 783 | 783 | * Set the PDO instance |
| 784 | 784 | * @param PDO $pdo the pdo object |
| 785 | - * @return object DatabaseQueryBuilder |
|
| 785 | + * @return object DatabaseQueryBuilder |
|
| 786 | 786 | */ |
| 787 | 787 | public function setPdo(PDO $pdo = null){ |
| 788 | - $this->pdo = $pdo; |
|
| 789 | - return $this; |
|
| 788 | + $this->pdo = $pdo; |
|
| 789 | + return $this; |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | - /** |
|
| 793 | - * Return the database table prefix |
|
| 794 | - * @return string |
|
| 795 | - */ |
|
| 792 | + /** |
|
| 793 | + * Return the database table prefix |
|
| 794 | + * @return string |
|
| 795 | + */ |
|
| 796 | 796 | public function getPrefix(){ |
| 797 | - return $this->prefix; |
|
| 797 | + return $this->prefix; |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | /** |
| 801 | 801 | * Set the database table prefix |
| 802 | 802 | * @param string $prefix the new prefix |
| 803 | - * @return object DatabaseQueryBuilder |
|
| 803 | + * @return object DatabaseQueryBuilder |
|
| 804 | 804 | */ |
| 805 | 805 | public function setPrefix($prefix){ |
| 806 | - $this->prefix = $prefix; |
|
| 807 | - return $this; |
|
| 806 | + $this->prefix = $prefix; |
|
| 807 | + return $this; |
|
| 808 | 808 | } |
| 809 | 809 | |
| 810 | - /** |
|
| 811 | - * Return the database driver |
|
| 812 | - * @return string |
|
| 813 | - */ |
|
| 810 | + /** |
|
| 811 | + * Return the database driver |
|
| 812 | + * @return string |
|
| 813 | + */ |
|
| 814 | 814 | public function getDriver(){ |
| 815 | - return $this->driver; |
|
| 815 | + return $this->driver; |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | /** |
| 819 | 819 | * Set the database driver |
| 820 | 820 | * @param string $driver the new driver |
| 821 | - * @return object DatabaseQueryBuilder |
|
| 821 | + * @return object DatabaseQueryBuilder |
|
| 822 | 822 | */ |
| 823 | 823 | public function setDriver($driver){ |
| 824 | - $this->driver = $driver; |
|
| 825 | - return $this; |
|
| 824 | + $this->driver = $driver; |
|
| 825 | + return $this; |
|
| 826 | 826 | } |
| 827 | 827 | |
| 828 | - /** |
|
| 829 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | - * @return object the current DatabaseQueryBuilder instance |
|
| 831 | - */ |
|
| 828 | + /** |
|
| 829 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | + * @return object the current DatabaseQueryBuilder instance |
|
| 831 | + */ |
|
| 832 | 832 | public function reset(){ |
| 833 | - $this->select = '*'; |
|
| 834 | - $this->from = null; |
|
| 835 | - $this->where = null; |
|
| 836 | - $this->limit = null; |
|
| 837 | - $this->orderBy = null; |
|
| 838 | - $this->groupBy = null; |
|
| 839 | - $this->having = null; |
|
| 840 | - $this->join = null; |
|
| 841 | - $this->query = null; |
|
| 842 | - return $this; |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | - * @see DatabaseQueryBuilder::having |
|
| 848 | - * |
|
| 849 | - * @return string |
|
| 850 | - */ |
|
| 833 | + $this->select = '*'; |
|
| 834 | + $this->from = null; |
|
| 835 | + $this->where = null; |
|
| 836 | + $this->limit = null; |
|
| 837 | + $this->orderBy = null; |
|
| 838 | + $this->groupBy = null; |
|
| 839 | + $this->having = null; |
|
| 840 | + $this->join = null; |
|
| 841 | + $this->query = null; |
|
| 842 | + return $this; |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | + * @see DatabaseQueryBuilder::having |
|
| 848 | + * |
|
| 849 | + * @return string |
|
| 850 | + */ |
|
| 851 | 851 | protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
| 852 | 852 | $x = explode('?', $field); |
| 853 | 853 | $w = ''; |
| 854 | 854 | foreach($x as $k => $v){ |
| 855 | - if (!empty($v)){ |
|
| 855 | + if (!empty($v)){ |
|
| 856 | 856 | if (! isset($op[$k])){ |
| 857 | - $op[$k] = ''; |
|
| 857 | + $op[$k] = ''; |
|
| 858 | + } |
|
| 859 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | + } |
|
| 858 | 861 | } |
| 859 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | 862 | return $w; |
| 863 | 863 | } |
| 864 | 864 | |
@@ -870,35 +870,35 @@ discard block |
||
| 870 | 870 | * @return string |
| 871 | 871 | */ |
| 872 | 872 | protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
| 873 | - $_where = array(); |
|
| 874 | - foreach ($where as $column => $data){ |
|
| 873 | + $_where = array(); |
|
| 874 | + foreach ($where as $column => $data){ |
|
| 875 | 875 | if (is_null($data)){ |
| 876 | - $data = ''; |
|
| 876 | + $data = ''; |
|
| 877 | 877 | } |
| 878 | 878 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
| 879 | - } |
|
| 880 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | - return $where; |
|
| 879 | + } |
|
| 880 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | + return $where; |
|
| 882 | 882 | } |
| 883 | 883 | |
| 884 | - /** |
|
| 885 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | - * @see DatabaseQueryBuilder::where |
|
| 887 | - * |
|
| 888 | - * @return string |
|
| 889 | - */ |
|
| 884 | + /** |
|
| 885 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | + * @see DatabaseQueryBuilder::where |
|
| 887 | + * |
|
| 888 | + * @return string |
|
| 889 | + */ |
|
| 890 | 890 | protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
| 891 | - $x = explode('?', $where); |
|
| 892 | - $w = ''; |
|
| 893 | - foreach($x as $k => $v){ |
|
| 891 | + $x = explode('?', $where); |
|
| 892 | + $w = ''; |
|
| 893 | + foreach($x as $k => $v){ |
|
| 894 | 894 | if (! empty($v)){ |
| 895 | 895 | if (isset($op[$k]) && is_null($op[$k])){ |
| 896 | - $op[$k] = ''; |
|
| 896 | + $op[$k] = ''; |
|
| 897 | 897 | } |
| 898 | 898 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
| 899 | 899 | } |
| 900 | - } |
|
| 901 | - return $w; |
|
| 900 | + } |
|
| 901 | + return $w; |
|
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | /** |
@@ -908,53 +908,53 @@ discard block |
||
| 908 | 908 | * @return string |
| 909 | 909 | */ |
| 910 | 910 | protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
| 911 | - $w = ''; |
|
| 912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | - if (is_null($op)){ |
|
| 911 | + $w = ''; |
|
| 912 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | + if (is_null($op)){ |
|
| 914 | 914 | $op = ''; |
| 915 | - } |
|
| 916 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 915 | + } |
|
| 916 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | 917 | } else { |
| 918 | - if (is_null($val)){ |
|
| 918 | + if (is_null($val)){ |
|
| 919 | 919 | $val = ''; |
| 920 | - } |
|
| 921 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 920 | + } |
|
| 921 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | 922 | } |
| 923 | 923 | return $w; |
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Set the $this->where property |
|
| 928 | - * @param string $whereStr the WHERE clause string |
|
| 929 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | - */ |
|
| 931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Set the $this->where property |
|
| 928 | + * @param string $whereStr the WHERE clause string |
|
| 929 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | + */ |
|
| 931 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | 932 | if (empty($this->where)){ |
| 933 | - $this->where = $whereStr; |
|
| 933 | + $this->where = $whereStr; |
|
| 934 | 934 | } else { |
| 935 | - if (substr(trim($this->where), -1) == '('){ |
|
| 935 | + if (substr(trim($this->where), -1) == '('){ |
|
| 936 | 936 | $this->where = $this->where . ' ' . $whereStr; |
| 937 | - } else { |
|
| 937 | + } else { |
|
| 938 | 938 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
| 939 | - } |
|
| 939 | + } |
|
| 940 | + } |
|
| 940 | 941 | } |
| 941 | - } |
|
| 942 | 942 | |
| 943 | 943 | |
| 944 | - /** |
|
| 945 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | - * @see DatabaseQueryBuilder::min |
|
| 948 | - * @see DatabaseQueryBuilder::max |
|
| 949 | - * @see DatabaseQueryBuilder::sum |
|
| 950 | - * @see DatabaseQueryBuilder::count |
|
| 951 | - * @see DatabaseQueryBuilder::avg |
|
| 952 | - * @return object |
|
| 953 | - */ |
|
| 944 | + /** |
|
| 945 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | + * @see DatabaseQueryBuilder::min |
|
| 948 | + * @see DatabaseQueryBuilder::max |
|
| 949 | + * @see DatabaseQueryBuilder::sum |
|
| 950 | + * @see DatabaseQueryBuilder::count |
|
| 951 | + * @see DatabaseQueryBuilder::avg |
|
| 952 | + * @return object |
|
| 953 | + */ |
|
| 954 | 954 | protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
| 955 | - $clause = strtoupper($clause); |
|
| 956 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | - return $this; |
|
| 955 | + $clause = strtoupper($clause); |
|
| 956 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | + return $this; |
|
| 959 | 959 | } |
| 960 | 960 | } |
@@ -1,168 +1,168 @@ |
||
| 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{ |
|
| 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{ |
|
| 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 | - * The logger instance |
|
| 36 | - * @var Log |
|
| 37 | - */ |
|
| 38 | - private static $logger; |
|
| 34 | + /** |
|
| 35 | + * The logger instance |
|
| 36 | + * @var Log |
|
| 37 | + */ |
|
| 38 | + private static $logger; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Get the logger singleton instance |
|
| 42 | - * @return Log the logger instance |
|
| 43 | - */ |
|
| 44 | - private static function getLogger(){ |
|
| 45 | - if(self::$logger == null){ |
|
| 46 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 47 | - self::$logger[0]->setLogger('Library::Session'); |
|
| 48 | - } |
|
| 49 | - return self::$logger[0]; |
|
| 50 | - } |
|
| 40 | + /** |
|
| 41 | + * Get the logger singleton instance |
|
| 42 | + * @return Log the logger instance |
|
| 43 | + */ |
|
| 44 | + private static function getLogger(){ |
|
| 45 | + if(self::$logger == null){ |
|
| 46 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 47 | + self::$logger[0]->setLogger('Library::Session'); |
|
| 48 | + } |
|
| 49 | + return self::$logger[0]; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get the session item value |
|
| 54 | - * @param string $item the session item name to get |
|
| 55 | - * @param mixed $default the default value to use if can not find the session item in the list |
|
| 56 | - * @return mixed the session value if exist or the default value |
|
| 57 | - */ |
|
| 58 | - public static function get($item, $default = null){ |
|
| 59 | - $logger = self::getLogger(); |
|
| 60 | - $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 61 | - if(array_key_exists($item, $_SESSION)){ |
|
| 62 | - $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 63 | - return $_SESSION[$item]; |
|
| 64 | - } |
|
| 65 | - $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 66 | - return $default; |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Get the session item value |
|
| 54 | + * @param string $item the session item name to get |
|
| 55 | + * @param mixed $default the default value to use if can not find the session item in the list |
|
| 56 | + * @return mixed the session value if exist or the default value |
|
| 57 | + */ |
|
| 58 | + public static function get($item, $default = null){ |
|
| 59 | + $logger = self::getLogger(); |
|
| 60 | + $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
| 61 | + if(array_key_exists($item, $_SESSION)){ |
|
| 62 | + $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
|
| 63 | + return $_SESSION[$item]; |
|
| 64 | + } |
|
| 65 | + $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
| 66 | + return $default; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Set the session item value |
|
| 71 | - * @param string $item the session item name to set |
|
| 72 | - * @param mixed $value the session item value |
|
| 73 | - */ |
|
| 74 | - public static function set($item, $value){ |
|
| 75 | - $logger = self::getLogger(); |
|
| 76 | - $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 77 | - $_SESSION[$item] = $value; |
|
| 78 | - } |
|
| 69 | + /** |
|
| 70 | + * Set the session item value |
|
| 71 | + * @param string $item the session item name to set |
|
| 72 | + * @param mixed $value the session item value |
|
| 73 | + */ |
|
| 74 | + public static function set($item, $value){ |
|
| 75 | + $logger = self::getLogger(); |
|
| 76 | + $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
|
| 77 | + $_SESSION[$item] = $value; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the session flash item value |
|
| 82 | - * @param string $item the session flash item name to get |
|
| 83 | - * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 84 | - * @return mixed the session flash value if exist or the default value |
|
| 85 | - */ |
|
| 86 | - public static function getFlash($item, $default = null){ |
|
| 87 | - $logger = self::getLogger(); |
|
| 88 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | - $return = array_key_exists($key, $_SESSION) ? |
|
| 90 | - ($_SESSION[$key]) : $default; |
|
| 91 | - if(array_key_exists($key, $_SESSION)){ |
|
| 92 | - unset($_SESSION[$key]); |
|
| 93 | - } |
|
| 94 | - else{ |
|
| 95 | - $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 96 | - } |
|
| 97 | - return $return; |
|
| 98 | - } |
|
| 80 | + /** |
|
| 81 | + * Get the session flash item value |
|
| 82 | + * @param string $item the session flash item name to get |
|
| 83 | + * @param mixed $default the default value to use if can not find the session flash item in the list |
|
| 84 | + * @return mixed the session flash value if exist or the default value |
|
| 85 | + */ |
|
| 86 | + public static function getFlash($item, $default = null){ |
|
| 87 | + $logger = self::getLogger(); |
|
| 88 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 89 | + $return = array_key_exists($key, $_SESSION) ? |
|
| 90 | + ($_SESSION[$key]) : $default; |
|
| 91 | + if(array_key_exists($key, $_SESSION)){ |
|
| 92 | + unset($_SESSION[$key]); |
|
| 93 | + } |
|
| 94 | + else{ |
|
| 95 | + $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
| 96 | + } |
|
| 97 | + return $return; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Check whether the given session flash item exists |
|
| 102 | - * @param string $item the session flash item name |
|
| 103 | - * @return boolean |
|
| 104 | - */ |
|
| 105 | - public static function hasFlash($item){ |
|
| 106 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 107 | - return array_key_exists($key, $_SESSION); |
|
| 108 | - } |
|
| 100 | + /** |
|
| 101 | + * Check whether the given session flash item exists |
|
| 102 | + * @param string $item the session flash item name |
|
| 103 | + * @return boolean |
|
| 104 | + */ |
|
| 105 | + public static function hasFlash($item){ |
|
| 106 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 107 | + return array_key_exists($key, $_SESSION); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Set the session flash item value |
|
| 112 | - * @param string $item the session flash item name to set |
|
| 113 | - * @param mixed $value the session flash item value |
|
| 114 | - */ |
|
| 115 | - public static function setFlash($item, $value){ |
|
| 116 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 117 | - $_SESSION[$key] = $value; |
|
| 118 | - } |
|
| 110 | + /** |
|
| 111 | + * Set the session flash item value |
|
| 112 | + * @param string $item the session flash item name to set |
|
| 113 | + * @param mixed $value the session flash item value |
|
| 114 | + */ |
|
| 115 | + public static function setFlash($item, $value){ |
|
| 116 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 117 | + $_SESSION[$key] = $value; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Clear the session item in the list |
|
| 122 | - * @param string $item the session item name to be deleted |
|
| 123 | - */ |
|
| 124 | - public static function clear($item){ |
|
| 125 | - $logger = self::getLogger(); |
|
| 126 | - if(array_key_exists($item, $_SESSION)){ |
|
| 127 | - $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 128 | - unset($_SESSION[$item]); |
|
| 129 | - } |
|
| 130 | - else{ |
|
| 131 | - $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 132 | - } |
|
| 133 | - } |
|
| 120 | + /** |
|
| 121 | + * Clear the session item in the list |
|
| 122 | + * @param string $item the session item name to be deleted |
|
| 123 | + */ |
|
| 124 | + public static function clear($item){ |
|
| 125 | + $logger = self::getLogger(); |
|
| 126 | + if(array_key_exists($item, $_SESSION)){ |
|
| 127 | + $logger->info('Deleting of session for item ['.$item.' ]'); |
|
| 128 | + unset($_SESSION[$item]); |
|
| 129 | + } |
|
| 130 | + else{ |
|
| 131 | + $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Clear the session flash item in the list |
|
| 137 | - * @param string $item the session flash item name to be deleted |
|
| 138 | - */ |
|
| 139 | - public static function clearFlash($item){ |
|
| 140 | - $logger = self::getLogger(); |
|
| 141 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 142 | - if(array_key_exists($key, $_SESSION)){ |
|
| 143 | - $logger->info('Delete session flash for item ['.$item.']'); |
|
| 144 | - unset($_SESSION[$item]); |
|
| 145 | - } |
|
| 146 | - else{ |
|
| 147 | - $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 148 | - } |
|
| 149 | - } |
|
| 135 | + /** |
|
| 136 | + * Clear the session flash item in the list |
|
| 137 | + * @param string $item the session flash item name to be deleted |
|
| 138 | + */ |
|
| 139 | + public static function clearFlash($item){ |
|
| 140 | + $logger = self::getLogger(); |
|
| 141 | + $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
| 142 | + if(array_key_exists($key, $_SESSION)){ |
|
| 143 | + $logger->info('Delete session flash for item ['.$item.']'); |
|
| 144 | + unset($_SESSION[$item]); |
|
| 145 | + } |
|
| 146 | + else{ |
|
| 147 | + $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Check whether the given session item exists |
|
| 153 | - * @param string $item the session item name |
|
| 154 | - * @return boolean |
|
| 155 | - */ |
|
| 156 | - public static function exists($item){ |
|
| 157 | - return array_key_exists($item, $_SESSION); |
|
| 158 | - } |
|
| 151 | + /** |
|
| 152 | + * Check whether the given session item exists |
|
| 153 | + * @param string $item the session item name |
|
| 154 | + * @return boolean |
|
| 155 | + */ |
|
| 156 | + public static function exists($item){ |
|
| 157 | + return array_key_exists($item, $_SESSION); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Destroy all session data values |
|
| 162 | - */ |
|
| 163 | - public static function clearAll(){ |
|
| 164 | - session_unset(); |
|
| 165 | - session_destroy(); |
|
| 166 | - } |
|
| 160 | + /** |
|
| 161 | + * Destroy all session data values |
|
| 162 | + */ |
|
| 163 | + public static function clearAll(){ |
|
| 164 | + session_unset(); |
|
| 165 | + session_destroy(); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - } |
|
| 168 | + } |
|
@@ -1,330 +1,330 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | - */ |
|
| 30 | - if( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | - show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
| 27 | + /** |
|
| 28 | + * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
|
| 29 | + */ |
|
| 30 | + if( !interface_exists('SessionHandlerInterface')){ |
|
| 31 | + show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + class DBSessionHandler implements SessionHandlerInterface{ |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * The encryption method to use to encrypt session data in database |
|
| 38 | - * @const string |
|
| 39 | - */ |
|
| 40 | - const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 36 | + /** |
|
| 37 | + * The encryption method to use to encrypt session data in database |
|
| 38 | + * @const string |
|
| 39 | + */ |
|
| 40 | + const DB_SESSION_HASH_METHOD = 'AES-256-CBC'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Super global instance |
|
| 44 | - * @var object |
|
| 45 | - */ |
|
| 46 | - protected $OBJ = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Session secret to use |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - private $sessionSecret = null; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The initialisation vector to use for openssl |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $iv = null; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The model instance name to use after load model |
|
| 62 | - * @var object |
|
| 63 | - */ |
|
| 64 | - private $modelInstanceName = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The columns of the table to use to store session data |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - private $sessionTableColumns = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * The instance of the Log |
|
| 74 | - * @var Log |
|
| 75 | - */ |
|
| 76 | - private $logger; |
|
| 77 | - |
|
| 78 | - /** |
|
| 42 | + /** |
|
| 43 | + * Super global instance |
|
| 44 | + * @var object |
|
| 45 | + */ |
|
| 46 | + protected $OBJ = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Session secret to use |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + private $sessionSecret = null; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The initialisation vector to use for openssl |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $iv = null; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The model instance name to use after load model |
|
| 62 | + * @var object |
|
| 63 | + */ |
|
| 64 | + private $modelInstanceName = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The columns of the table to use to store session data |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + private $sessionTableColumns = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * The instance of the Log |
|
| 74 | + * @var Log |
|
| 75 | + */ |
|
| 76 | + private $logger; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | 79 | * Instance of the Loader class |
| 80 | 80 | * @var Loader |
| 81 | 81 | */ |
| 82 | 82 | protected $loader = null; |
| 83 | 83 | |
| 84 | - public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 85 | - /** |
|
| 86 | - * instance of the Log class |
|
| 87 | - */ |
|
| 88 | - if(is_object($logger)){ |
|
| 89 | - $this->setLogger($logger); |
|
| 90 | - } |
|
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | - $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if(is_object($loader)){ |
|
| 97 | - $this->setLoader($loader); |
|
| 98 | - } |
|
| 99 | - $this->OBJ = & get_instance(); |
|
| 84 | + public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
| 85 | + /** |
|
| 86 | + * instance of the Log class |
|
| 87 | + */ |
|
| 88 | + if(is_object($logger)){ |
|
| 89 | + $this->setLogger($logger); |
|
| 90 | + } |
|
| 91 | + else{ |
|
| 92 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | + $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if(is_object($loader)){ |
|
| 97 | + $this->setLoader($loader); |
|
| 98 | + } |
|
| 99 | + $this->OBJ = & get_instance(); |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | - if(is_object($modelInstance)){ |
|
| 103 | - $this->setModelInstance($modelInstance); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Set the session secret used to encrypt the data in database |
|
| 109 | - * @param string $secret the base64 string secret |
|
| 110 | - */ |
|
| 111 | - public function setSessionSecret($secret){ |
|
| 112 | - $this->sessionSecret = $secret; |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Return the session secret |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getSessionSecret(){ |
|
| 121 | - return $this->sessionSecret; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Set the initializer vector for openssl |
|
| 127 | - * @param string $key the session secret used |
|
| 128 | - */ |
|
| 129 | - public function setInitializerVector($key){ |
|
| 130 | - $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 131 | - $key = base64_decode($key); |
|
| 132 | - $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Return the initializer vector |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public function getInitializerVector(){ |
|
| 141 | - return $this->iv; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Open the database session handler, here nothing to do just return true |
|
| 146 | - * @param string $savePath the session save path |
|
| 147 | - * @param string $sessionName the session name |
|
| 148 | - * @return boolean |
|
| 149 | - */ |
|
| 150 | - public function open($savePath, $sessionName){ |
|
| 151 | - $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 152 | - //try to check if session secret is set before |
|
| 153 | - $secret = $this->getSessionSecret(); |
|
| 154 | - if(empty($secret)){ |
|
| 155 | - $secret = get_config('session_secret', null); |
|
| 156 | - $this->setSessionSecret($secret); |
|
| 157 | - } |
|
| 158 | - $this->logger->info('Session secret: ' . $secret); |
|
| 159 | - |
|
| 160 | - if(! $this->getModelInstance()){ |
|
| 161 | - $this->setModelInstanceFromConfig(); |
|
| 162 | - } |
|
| 163 | - $this->setInitializerVector($secret); |
|
| 164 | - |
|
| 165 | - //set session tables columns |
|
| 166 | - $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
|
| 167 | - |
|
| 168 | - if(empty($this->sessionTableColumns)){ |
|
| 169 | - show_error('The session handler is "database" but the table columns not set'); |
|
| 170 | - } |
|
| 171 | - $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 102 | + if(is_object($modelInstance)){ |
|
| 103 | + $this->setModelInstance($modelInstance); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Set the session secret used to encrypt the data in database |
|
| 109 | + * @param string $secret the base64 string secret |
|
| 110 | + */ |
|
| 111 | + public function setSessionSecret($secret){ |
|
| 112 | + $this->sessionSecret = $secret; |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Return the session secret |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getSessionSecret(){ |
|
| 121 | + return $this->sessionSecret; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Set the initializer vector for openssl |
|
| 127 | + * @param string $key the session secret used |
|
| 128 | + */ |
|
| 129 | + public function setInitializerVector($key){ |
|
| 130 | + $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
|
| 131 | + $key = base64_decode($key); |
|
| 132 | + $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Return the initializer vector |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public function getInitializerVector(){ |
|
| 141 | + return $this->iv; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Open the database session handler, here nothing to do just return true |
|
| 146 | + * @param string $savePath the session save path |
|
| 147 | + * @param string $sessionName the session name |
|
| 148 | + * @return boolean |
|
| 149 | + */ |
|
| 150 | + public function open($savePath, $sessionName){ |
|
| 151 | + $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
|
| 152 | + //try to check if session secret is set before |
|
| 153 | + $secret = $this->getSessionSecret(); |
|
| 154 | + if(empty($secret)){ |
|
| 155 | + $secret = get_config('session_secret', null); |
|
| 156 | + $this->setSessionSecret($secret); |
|
| 157 | + } |
|
| 158 | + $this->logger->info('Session secret: ' . $secret); |
|
| 159 | + |
|
| 160 | + if(! $this->getModelInstance()){ |
|
| 161 | + $this->setModelInstanceFromConfig(); |
|
| 162 | + } |
|
| 163 | + $this->setInitializerVector($secret); |
|
| 164 | + |
|
| 165 | + //set session tables columns |
|
| 166 | + $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
|
| 167 | + |
|
| 168 | + if(empty($this->sessionTableColumns)){ |
|
| 169 | + show_error('The session handler is "database" but the table columns not set'); |
|
| 170 | + } |
|
| 171 | + $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
|
| 172 | 172 | |
| 173 | - //delete the expired session |
|
| 174 | - $timeActivity = get_config('session_inactivity_time', 100); |
|
| 175 | - $this->gc($timeActivity); |
|
| 173 | + //delete the expired session |
|
| 174 | + $timeActivity = get_config('session_inactivity_time', 100); |
|
| 175 | + $this->gc($timeActivity); |
|
| 176 | 176 | |
| 177 | - return true; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Close the session |
|
| 182 | - * @return boolean |
|
| 183 | - */ |
|
| 184 | - public function close(){ |
|
| 185 | - $this->logger->debug('Closing database session handler'); |
|
| 186 | - return true; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the session value for the given session id |
|
| 191 | - * @param string $sid the session id to use |
|
| 192 | - * @return string the session data in serialiaze format |
|
| 193 | - */ |
|
| 194 | - public function read($sid){ |
|
| 195 | - $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 196 | - $instance = $this->getModelInstance(); |
|
| 197 | - $columns = $this->sessionTableColumns; |
|
| 198 | - if($this->getLoader()){ |
|
| 199 | - $this->getLoader()->functions('user_agent'); |
|
| 200 | - $this->getLoader()->library('Browser'); |
|
| 201 | - } |
|
| 202 | - else{ |
|
| 203 | - Loader::functions('user_agent'); |
|
| 204 | - Loader::library('Browser'); |
|
| 177 | + return true; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Close the session |
|
| 182 | + * @return boolean |
|
| 183 | + */ |
|
| 184 | + public function close(){ |
|
| 185 | + $this->logger->debug('Closing database session handler'); |
|
| 186 | + return true; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the session value for the given session id |
|
| 191 | + * @param string $sid the session id to use |
|
| 192 | + * @return string the session data in serialiaze format |
|
| 193 | + */ |
|
| 194 | + public function read($sid){ |
|
| 195 | + $this->logger->debug('Reading database session data for SID: ' . $sid); |
|
| 196 | + $instance = $this->getModelInstance(); |
|
| 197 | + $columns = $this->sessionTableColumns; |
|
| 198 | + if($this->getLoader()){ |
|
| 199 | + $this->getLoader()->functions('user_agent'); |
|
| 200 | + $this->getLoader()->library('Browser'); |
|
| 201 | + } |
|
| 202 | + else{ |
|
| 203 | + Loader::functions('user_agent'); |
|
| 204 | + Loader::library('Browser'); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - $ip = get_ip(); |
|
| 208 | - $host = @gethostbyaddr($ip) or null; |
|
| 209 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 207 | + $ip = get_ip(); |
|
| 208 | + $host = @gethostbyaddr($ip) or null; |
|
| 209 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 210 | 210 | |
| 211 | - $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 212 | - if($data && isset($data->{$columns['sdata']})){ |
|
| 213 | - //checking inactivity |
|
| 214 | - $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 215 | - if($data->{$columns['stime']} < $timeInactivity){ |
|
| 216 | - $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 217 | - $this->destroy($sid); |
|
| 218 | - return null; |
|
| 219 | - } |
|
| 220 | - return $this->decode($data->{$columns['sdata']}); |
|
| 221 | - } |
|
| 222 | - $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 223 | - return null; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Save the session data |
|
| 228 | - * @param string $sid the session ID |
|
| 229 | - * @param mixed $data the session data to save in serialize format |
|
| 230 | - * @return boolean |
|
| 231 | - */ |
|
| 232 | - public function write($sid, $data){ |
|
| 233 | - $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 234 | - $instance = $this->getModelInstance(); |
|
| 235 | - $columns = $this->sessionTableColumns; |
|
| 236 | - |
|
| 237 | - if($this->getLoader()){ |
|
| 238 | - $this->getLoader()->functions('user_agent'); |
|
| 239 | - $this->getLoader()->library('Browser'); |
|
| 240 | - } |
|
| 241 | - else{ |
|
| 242 | - Loader::functions('user_agent'); |
|
| 243 | - Loader::library('Browser'); |
|
| 211 | + $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
|
| 212 | + if($data && isset($data->{$columns['sdata']})){ |
|
| 213 | + //checking inactivity |
|
| 214 | + $timeInactivity = time() - get_config('session_inactivity_time', 100); |
|
| 215 | + if($data->{$columns['stime']} < $timeInactivity){ |
|
| 216 | + $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
|
| 217 | + $this->destroy($sid); |
|
| 218 | + return null; |
|
| 219 | + } |
|
| 220 | + return $this->decode($data->{$columns['sdata']}); |
|
| 221 | + } |
|
| 222 | + $this->logger->info('Database session data for SID: ' . $sid . ' is not valid return false, may be the session ID is wrong'); |
|
| 223 | + return null; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Save the session data |
|
| 228 | + * @param string $sid the session ID |
|
| 229 | + * @param mixed $data the session data to save in serialize format |
|
| 230 | + * @return boolean |
|
| 231 | + */ |
|
| 232 | + public function write($sid, $data){ |
|
| 233 | + $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
|
| 234 | + $instance = $this->getModelInstance(); |
|
| 235 | + $columns = $this->sessionTableColumns; |
|
| 236 | + |
|
| 237 | + if($this->getLoader()){ |
|
| 238 | + $this->getLoader()->functions('user_agent'); |
|
| 239 | + $this->getLoader()->library('Browser'); |
|
| 240 | + } |
|
| 241 | + else{ |
|
| 242 | + Loader::functions('user_agent'); |
|
| 243 | + Loader::library('Browser'); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $ip = get_ip(); |
|
| 247 | + $keyValue = $instance->getKeyValue(); |
|
| 248 | + $host = @gethostbyaddr($ip) or null; |
|
| 249 | + $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 250 | + $data = $this->encode($data); |
|
| 251 | + $params = array( |
|
| 252 | + $columns['sid'] => $sid, |
|
| 253 | + $columns['sdata'] => $data, |
|
| 254 | + $columns['stime'] => time(), |
|
| 255 | + $columns['shost'] => $host, |
|
| 256 | + $columns['sbrowser'] => $browser, |
|
| 257 | + $columns['sip'] => $ip, |
|
| 258 | + $columns['skey'] => $keyValue |
|
| 259 | + ); |
|
| 260 | + $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 261 | + $exists = $instance->get($sid); |
|
| 262 | + if($exists){ |
|
| 263 | + $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 264 | + //update |
|
| 265 | + unset($params[$columns['sid']]); |
|
| 266 | + $instance->update($sid, $params); |
|
| 244 | 267 | } |
| 268 | + else{ |
|
| 269 | + $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 270 | + $instance->insert($params); |
|
| 271 | + } |
|
| 272 | + return true; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Destroy the session data for the given session id |
|
| 278 | + * @param string $sid the session id value |
|
| 279 | + * @return boolean |
|
| 280 | + */ |
|
| 281 | + public function destroy($sid){ |
|
| 282 | + $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 283 | + $instance = $this->modelInstanceName; |
|
| 284 | + $instance->delete($sid); |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 245 | 287 | |
| 246 | - $ip = get_ip(); |
|
| 247 | - $keyValue = $instance->getKeyValue(); |
|
| 248 | - $host = @gethostbyaddr($ip) or null; |
|
| 249 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
| 250 | - $data = $this->encode($data); |
|
| 251 | - $params = array( |
|
| 252 | - $columns['sid'] => $sid, |
|
| 253 | - $columns['sdata'] => $data, |
|
| 254 | - $columns['stime'] => time(), |
|
| 255 | - $columns['shost'] => $host, |
|
| 256 | - $columns['sbrowser'] => $browser, |
|
| 257 | - $columns['sip'] => $ip, |
|
| 258 | - $columns['skey'] => $keyValue |
|
| 259 | - ); |
|
| 260 | - $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
|
| 261 | - $exists = $instance->get($sid); |
|
| 262 | - if($exists){ |
|
| 263 | - $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
|
| 264 | - //update |
|
| 265 | - unset($params[$columns['sid']]); |
|
| 266 | - $instance->update($sid, $params); |
|
| 267 | - } |
|
| 268 | - else{ |
|
| 269 | - $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
|
| 270 | - $instance->insert($params); |
|
| 271 | - } |
|
| 272 | - return true; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Destroy the session data for the given session id |
|
| 278 | - * @param string $sid the session id value |
|
| 279 | - * @return boolean |
|
| 280 | - */ |
|
| 281 | - public function destroy($sid){ |
|
| 282 | - $this->logger->debug('Destroy of session data for SID: ' . $sid); |
|
| 283 | - $instance = $this->modelInstanceName; |
|
| 284 | - $instance->delete($sid); |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Clean the expire session data to save espace |
|
| 290 | - * @param integer $maxLifetime the max lifetime |
|
| 291 | - * @return boolean |
|
| 292 | - */ |
|
| 293 | - public function gc($maxLifetime){ |
|
| 294 | - $instance = $this->modelInstanceName; |
|
| 295 | - $time = time() - $maxLifetime; |
|
| 296 | - $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 297 | - $instance->deleteByTime($time); |
|
| 298 | - return true; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Encode the session data using the openssl |
|
| 303 | - * @param mixed $data the session data to encode |
|
| 304 | - * @return mixed the encoded session data |
|
| 305 | - */ |
|
| 306 | - public function encode($data){ |
|
| 307 | - $key = base64_decode($this->sessionSecret); |
|
| 308 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | - $output = base64_encode($dataEncrypted); |
|
| 310 | - return $output; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Decode the session data using the openssl |
|
| 316 | - * @param mixed $data the data to decode |
|
| 317 | - * @return mixed the decoded data |
|
| 318 | - */ |
|
| 319 | - public function decode($data){ |
|
| 320 | - $key = base64_decode($this->sessionSecret); |
|
| 321 | - $data = base64_decode($data); |
|
| 322 | - $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 323 | - return $data; |
|
| 324 | - } |
|
| 288 | + /** |
|
| 289 | + * Clean the expire session data to save espace |
|
| 290 | + * @param integer $maxLifetime the max lifetime |
|
| 291 | + * @return boolean |
|
| 292 | + */ |
|
| 293 | + public function gc($maxLifetime){ |
|
| 294 | + $instance = $this->modelInstanceName; |
|
| 295 | + $time = time() - $maxLifetime; |
|
| 296 | + $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
|
| 297 | + $instance->deleteByTime($time); |
|
| 298 | + return true; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Encode the session data using the openssl |
|
| 303 | + * @param mixed $data the session data to encode |
|
| 304 | + * @return mixed the encoded session data |
|
| 305 | + */ |
|
| 306 | + public function encode($data){ |
|
| 307 | + $key = base64_decode($this->sessionSecret); |
|
| 308 | + $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 309 | + $output = base64_encode($dataEncrypted); |
|
| 310 | + return $output; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Decode the session data using the openssl |
|
| 316 | + * @param mixed $data the data to decode |
|
| 317 | + * @return mixed the decoded data |
|
| 318 | + */ |
|
| 319 | + public function decode($data){ |
|
| 320 | + $key = base64_decode($this->sessionSecret); |
|
| 321 | + $data = base64_decode($data); |
|
| 322 | + $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
| 323 | + return $data; |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | 326 | |
| 327 | - /** |
|
| 327 | + /** |
|
| 328 | 328 | * Return the loader instance |
| 329 | 329 | * @return object Loader the loader instance |
| 330 | 330 | */ |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * set the loader instance for future use |
| 337 | 337 | * @param object Loader $loader the loader object |
| 338 | 338 | */ |
| 339 | - public function setLoader($loader){ |
|
| 339 | + public function setLoader($loader){ |
|
| 340 | 340 | $this->loader = $loader; |
| 341 | 341 | return $this; |
| 342 | 342 | } |
@@ -353,47 +353,47 @@ discard block |
||
| 353 | 353 | * set the model instance for future use |
| 354 | 354 | * @param DBSessionHandlerModel $modelInstance the model object |
| 355 | 355 | */ |
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | 357 | $this->modelInstanceName = $modelInstance; |
| 358 | 358 | return $this; |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
| 362 | - * Return the Log instance |
|
| 363 | - * @return Log |
|
| 364 | - */ |
|
| 365 | - public function getLogger(){ |
|
| 366 | - return $this->logger; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the log instance |
|
| 371 | - * @param Log $logger the log object |
|
| 372 | - */ |
|
| 373 | - public function setLogger(Log $logger){ |
|
| 374 | - $this->logger = $logger; |
|
| 375 | - return $this; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Set the model instance using the configuration for session |
|
| 380 | - */ |
|
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 382 | - $modelName = get_config('session_save_path'); |
|
| 383 | - $this->logger->info('The database session model: ' . $modelName); |
|
| 384 | - if($this->getLoader()){ |
|
| 385 | - $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
|
| 386 | - } |
|
| 387 | - //@codeCoverageIgnoreStart |
|
| 388 | - else{ |
|
| 389 | - Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 362 | + * Return the Log instance |
|
| 363 | + * @return Log |
|
| 364 | + */ |
|
| 365 | + public function getLogger(){ |
|
| 366 | + return $this->logger; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the log instance |
|
| 371 | + * @param Log $logger the log object |
|
| 372 | + */ |
|
| 373 | + public function setLogger(Log $logger){ |
|
| 374 | + $this->logger = $logger; |
|
| 375 | + return $this; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Set the model instance using the configuration for session |
|
| 380 | + */ |
|
| 381 | + private function setModelInstanceFromConfig(){ |
|
| 382 | + $modelName = get_config('session_save_path'); |
|
| 383 | + $this->logger->info('The database session model: ' . $modelName); |
|
| 384 | + if($this->getLoader()){ |
|
| 385 | + $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
|
| 386 | + } |
|
| 387 | + //@codeCoverageIgnoreStart |
|
| 388 | + else{ |
|
| 389 | + Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | 390 | } |
| 391 | 391 | if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
| 392 | - show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 393 | - } |
|
| 394 | - //@codeCoverageIgnoreEnd |
|
| 392 | + show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
| 393 | + } |
|
| 394 | + //@codeCoverageIgnoreEnd |
|
| 395 | 395 | |
| 396 | - //set model instance |
|
| 397 | - $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
|
| 398 | - } |
|
| 399 | - } |
|
| 396 | + //set model instance |
|
| 397 | + $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
|
| 398 | + } |
|
| 399 | + } |
|
@@ -1,439 +1,439 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of request header to send with response |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $headers = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of request header to send with response |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $headers = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The final page content to display to user |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - private $_pageRender = null; |
|
| 41 | + /** |
|
| 42 | + * The final page content to display to user |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + private $_pageRender = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The current request URL |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $_currentUrl = null; |
|
| 47 | + /** |
|
| 48 | + * The current request URL |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $_currentUrl = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The current request URL cache key |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_currentUrlCacheKey = null; |
|
| 53 | + /** |
|
| 54 | + * The current request URL cache key |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_currentUrlCacheKey = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Whether we can compress the output using Gzip |
|
| 61 | - * @var boolean |
|
| 62 | - */ |
|
| 63 | - private static $_canCompressOutput = false; |
|
| 59 | + /** |
|
| 60 | + * Whether we can compress the output using Gzip |
|
| 61 | + * @var boolean |
|
| 62 | + */ |
|
| 63 | + private static $_canCompressOutput = false; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Construct new response instance |
|
| 67 | - */ |
|
| 68 | - public function __construct(){ |
|
| 69 | - $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
| 70 | - . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
| 65 | + /** |
|
| 66 | + * Construct new response instance |
|
| 67 | + */ |
|
| 68 | + public function __construct(){ |
|
| 69 | + $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
| 70 | + . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
| 71 | 71 | |
| 72 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 72 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
| 73 | 73 | |
| 74 | - self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | - && extension_loaded('zlib') |
|
| 78 | - && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | - } |
|
| 74 | + self::$_canCompressOutput = get_config('compress_output') |
|
| 75 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
| 76 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
| 77 | + && extension_loaded('zlib') |
|
| 78 | + && (bool) ini_get('zlib.output_compression') === false; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Get the logger singleton instance |
|
| 83 | - * @return Log the logger instance |
|
| 84 | - */ |
|
| 85 | - private static function getLogger(){ |
|
| 86 | - if(self::$logger == null){ |
|
| 87 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 88 | - self::$logger[0]->setLogger('Library::Response'); |
|
| 89 | - } |
|
| 90 | - return self::$logger[0]; |
|
| 91 | - } |
|
| 81 | + /** |
|
| 82 | + * Get the logger singleton instance |
|
| 83 | + * @return Log the logger instance |
|
| 84 | + */ |
|
| 85 | + private static function getLogger(){ |
|
| 86 | + if(self::$logger == null){ |
|
| 87 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 88 | + self::$logger[0]->setLogger('Library::Response'); |
|
| 89 | + } |
|
| 90 | + return self::$logger[0]; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Send the HTTP Response headers |
|
| 95 | - * @param integer $httpCode the HTTP status code |
|
| 96 | - * @param array $headers the additional headers to add to the existing headers list |
|
| 97 | - */ |
|
| 98 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 99 | - set_http_status_header($httpCode); |
|
| 100 | - self::setHeaders($headers); |
|
| 101 | - if(! headers_sent()){ |
|
| 102 | - foreach(self::getHeaders() as $key => $value){ |
|
| 103 | - header($key .': '.$value); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 93 | + /** |
|
| 94 | + * Send the HTTP Response headers |
|
| 95 | + * @param integer $httpCode the HTTP status code |
|
| 96 | + * @param array $headers the additional headers to add to the existing headers list |
|
| 97 | + */ |
|
| 98 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 99 | + set_http_status_header($httpCode); |
|
| 100 | + self::setHeaders($headers); |
|
| 101 | + if(! headers_sent()){ |
|
| 102 | + foreach(self::getHeaders() as $key => $value){ |
|
| 103 | + header($key .': '.$value); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the list of the headers |
|
| 110 | - * @return array the headers list |
|
| 111 | - */ |
|
| 112 | - public static function getHeaders(){ |
|
| 113 | - return self::$headers; |
|
| 114 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the list of the headers |
|
| 110 | + * @return array the headers list |
|
| 111 | + */ |
|
| 112 | + public static function getHeaders(){ |
|
| 113 | + return self::$headers; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Get the header value for the given name |
|
| 118 | - * @param string $name the header name |
|
| 119 | - * @return string the header value |
|
| 120 | - */ |
|
| 121 | - public static function getHeader($name){ |
|
| 122 | - return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 123 | - } |
|
| 116 | + /** |
|
| 117 | + * Get the header value for the given name |
|
| 118 | + * @param string $name the header name |
|
| 119 | + * @return string the header value |
|
| 120 | + */ |
|
| 121 | + public static function getHeader($name){ |
|
| 122 | + return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Set the header value for the specified name |
|
| 128 | - * @param string $name the header name |
|
| 129 | - * @param string $value the header value to be set |
|
| 130 | - */ |
|
| 131 | - public static function setHeader($name, $value){ |
|
| 132 | - self::$headers[$name] = $value; |
|
| 133 | - } |
|
| 126 | + /** |
|
| 127 | + * Set the header value for the specified name |
|
| 128 | + * @param string $name the header name |
|
| 129 | + * @param string $value the header value to be set |
|
| 130 | + */ |
|
| 131 | + public static function setHeader($name, $value){ |
|
| 132 | + self::$headers[$name] = $value; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Set the headers using array |
|
| 137 | - * @param array $headers the list of the headers to set. |
|
| 138 | - * Note: this will merge with the existing headers |
|
| 139 | - */ |
|
| 140 | - public static function setHeaders(array $headers){ |
|
| 141 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 142 | - } |
|
| 135 | + /** |
|
| 136 | + * Set the headers using array |
|
| 137 | + * @param array $headers the list of the headers to set. |
|
| 138 | + * Note: this will merge with the existing headers |
|
| 139 | + */ |
|
| 140 | + public static function setHeaders(array $headers){ |
|
| 141 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Redirect user in the specified page |
|
| 146 | - * @param string $path the URL or URI to be redirect to |
|
| 147 | - */ |
|
| 148 | - public static function redirect($path = ''){ |
|
| 149 | - $logger = self::getLogger(); |
|
| 150 | - $url = Url::site_url($path); |
|
| 151 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 152 | - if(! headers_sent()){ |
|
| 153 | - header('Location: '.$url); |
|
| 154 | - exit; |
|
| 155 | - } |
|
| 156 | - else{ |
|
| 157 | - echo '<script> |
|
| 144 | + /** |
|
| 145 | + * Redirect user in the specified page |
|
| 146 | + * @param string $path the URL or URI to be redirect to |
|
| 147 | + */ |
|
| 148 | + public static function redirect($path = ''){ |
|
| 149 | + $logger = self::getLogger(); |
|
| 150 | + $url = Url::site_url($path); |
|
| 151 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
| 152 | + if(! headers_sent()){ |
|
| 153 | + header('Location: '.$url); |
|
| 154 | + exit; |
|
| 155 | + } |
|
| 156 | + else{ |
|
| 157 | + echo '<script> |
|
| 158 | 158 | location.href = "'.$url.'"; |
| 159 | 159 | </script>'; |
| 160 | - } |
|
| 161 | - } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Render the view to display later or return the content |
|
| 165 | - * @param string $view the view name or path |
|
| 166 | - * @param array|object $data the variable data to use in the view |
|
| 167 | - * @param boolean $return whether to return the view generated content or display it directly |
|
| 168 | - * @return void|string if $return is true will return the view content otherwise |
|
| 169 | - * will display the view content. |
|
| 170 | - */ |
|
| 171 | - public function render($view, $data = null, $return = false){ |
|
| 172 | - $logger = self::getLogger(); |
|
| 173 | - //convert data to an array |
|
| 174 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 175 | - $view = str_ireplace('.php', '', $view); |
|
| 176 | - $view = trim($view, '/\\'); |
|
| 177 | - $viewFile = $view . '.php'; |
|
| 178 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
| 163 | + /** |
|
| 164 | + * Render the view to display later or return the content |
|
| 165 | + * @param string $view the view name or path |
|
| 166 | + * @param array|object $data the variable data to use in the view |
|
| 167 | + * @param boolean $return whether to return the view generated content or display it directly |
|
| 168 | + * @return void|string if $return is true will return the view content otherwise |
|
| 169 | + * will display the view content. |
|
| 170 | + */ |
|
| 171 | + public function render($view, $data = null, $return = false){ |
|
| 172 | + $logger = self::getLogger(); |
|
| 173 | + //convert data to an array |
|
| 174 | + $data = ! is_array($data) ? (array) $data : $data; |
|
| 175 | + $view = str_ireplace('.php', '', $view); |
|
| 176 | + $view = trim($view, '/\\'); |
|
| 177 | + $viewFile = $view . '.php'; |
|
| 178 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
| 179 | 179 | |
| 180 | - //super instance |
|
| 181 | - $obj = & get_instance(); |
|
| 180 | + //super instance |
|
| 181 | + $obj = & get_instance(); |
|
| 182 | 182 | |
| 183 | - if(Module::hasModule()){ |
|
| 184 | - //check in module first |
|
| 185 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 186 | - $mod = null; |
|
| 187 | - //check if the request class contains module name |
|
| 188 | - if(strpos($view, '/') !== false){ |
|
| 189 | - $viewPath = explode('/', $view); |
|
| 190 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 191 | - $mod = $viewPath[0]; |
|
| 192 | - array_shift($viewPath); |
|
| 193 | - $view = implode('/', $viewPath); |
|
| 194 | - $viewFile = $view . '.php'; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 198 | - $mod = $obj->moduleName; |
|
| 199 | - } |
|
| 200 | - if($mod){ |
|
| 201 | - $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 202 | - if($moduleViewPath){ |
|
| 203 | - $path = $moduleViewPath; |
|
| 204 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 205 | - } |
|
| 206 | - else{ |
|
| 207 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - else{ |
|
| 211 | - $logger->info('The current request does not use module using the default location.'); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 215 | - $found = false; |
|
| 216 | - if(file_exists($path)){ |
|
| 217 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 218 | - if(! isset($this->{$key})){ |
|
| 219 | - $this->{$key} = & $obj->{$key}; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - ob_start(); |
|
| 223 | - extract($data); |
|
| 224 | - //need use require() instead of require_once because can load this view many time |
|
| 225 | - require $path; |
|
| 226 | - $content = ob_get_clean(); |
|
| 227 | - if($return){ |
|
| 228 | - return $content; |
|
| 229 | - } |
|
| 230 | - $this->_pageRender .= $content; |
|
| 231 | - $found = true; |
|
| 232 | - } |
|
| 233 | - if(! $found){ |
|
| 234 | - show_error('Unable to find view [' .$view . ']'); |
|
| 235 | - } |
|
| 236 | - } |
|
| 183 | + if(Module::hasModule()){ |
|
| 184 | + //check in module first |
|
| 185 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
| 186 | + $mod = null; |
|
| 187 | + //check if the request class contains module name |
|
| 188 | + if(strpos($view, '/') !== false){ |
|
| 189 | + $viewPath = explode('/', $view); |
|
| 190 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 191 | + $mod = $viewPath[0]; |
|
| 192 | + array_shift($viewPath); |
|
| 193 | + $view = implode('/', $viewPath); |
|
| 194 | + $viewFile = $view . '.php'; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + if(! $mod && !empty($obj->moduleName)){ |
|
| 198 | + $mod = $obj->moduleName; |
|
| 199 | + } |
|
| 200 | + if($mod){ |
|
| 201 | + $moduleViewPath = Module::findViewFullPath($view, $mod); |
|
| 202 | + if($moduleViewPath){ |
|
| 203 | + $path = $moduleViewPath; |
|
| 204 | + $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 205 | + } |
|
| 206 | + else{ |
|
| 207 | + $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + else{ |
|
| 211 | + $logger->info('The current request does not use module using the default location.'); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
| 215 | + $found = false; |
|
| 216 | + if(file_exists($path)){ |
|
| 217 | + foreach(get_object_vars($obj) as $key => $value){ |
|
| 218 | + if(! isset($this->{$key})){ |
|
| 219 | + $this->{$key} = & $obj->{$key}; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + ob_start(); |
|
| 223 | + extract($data); |
|
| 224 | + //need use require() instead of require_once because can load this view many time |
|
| 225 | + require $path; |
|
| 226 | + $content = ob_get_clean(); |
|
| 227 | + if($return){ |
|
| 228 | + return $content; |
|
| 229 | + } |
|
| 230 | + $this->_pageRender .= $content; |
|
| 231 | + $found = true; |
|
| 232 | + } |
|
| 233 | + if(! $found){ |
|
| 234 | + show_error('Unable to find view [' .$view . ']'); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * Send the final page output to user |
|
| 240 | - */ |
|
| 241 | - public function renderFinalPage(){ |
|
| 242 | - $logger = self::getLogger(); |
|
| 243 | - $obj = & get_instance(); |
|
| 244 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 245 | - $dispatcher = $obj->eventdispatcher; |
|
| 246 | - $content = $this->_pageRender; |
|
| 247 | - if(! $content){ |
|
| 248 | - $logger->warning('The final view content is empty.'); |
|
| 249 | - return; |
|
| 250 | - } |
|
| 251 | - //dispatch |
|
| 252 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 253 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 254 | - if(empty($content)){ |
|
| 255 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 256 | - } |
|
| 238 | + /** |
|
| 239 | + * Send the final page output to user |
|
| 240 | + */ |
|
| 241 | + public function renderFinalPage(){ |
|
| 242 | + $logger = self::getLogger(); |
|
| 243 | + $obj = & get_instance(); |
|
| 244 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
| 245 | + $dispatcher = $obj->eventdispatcher; |
|
| 246 | + $content = $this->_pageRender; |
|
| 247 | + if(! $content){ |
|
| 248 | + $logger->warning('The final view content is empty.'); |
|
| 249 | + return; |
|
| 250 | + } |
|
| 251 | + //dispatch |
|
| 252 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
| 253 | + $content = ! empty($event->payload) ? $event->payload : null; |
|
| 254 | + if(empty($content)){ |
|
| 255 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - //check whether need save the page into cache. |
|
| 259 | - if($cachePageStatus){ |
|
| 260 | - //current page URL |
|
| 261 | - $url = $this->_currentUrl; |
|
| 262 | - //Cache view Time to live in second |
|
| 263 | - $viewCacheTtl = get_config('cache_ttl'); |
|
| 264 | - if (!empty($obj->view_cache_ttl)){ |
|
| 265 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
| 266 | - } |
|
| 267 | - //the cache handler instance |
|
| 268 | - $cacheInstance = $obj->cache; |
|
| 269 | - //the current page cache key for identification |
|
| 270 | - $cacheKey = $this->_currentUrlCacheKey; |
|
| 258 | + //check whether need save the page into cache. |
|
| 259 | + if($cachePageStatus){ |
|
| 260 | + //current page URL |
|
| 261 | + $url = $this->_currentUrl; |
|
| 262 | + //Cache view Time to live in second |
|
| 263 | + $viewCacheTtl = get_config('cache_ttl'); |
|
| 264 | + if (!empty($obj->view_cache_ttl)){ |
|
| 265 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
| 266 | + } |
|
| 267 | + //the cache handler instance |
|
| 268 | + $cacheInstance = $obj->cache; |
|
| 269 | + //the current page cache key for identification |
|
| 270 | + $cacheKey = $this->_currentUrlCacheKey; |
|
| 271 | 271 | |
| 272 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 273 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 272 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
| 273 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
| 274 | 274 | |
| 275 | - //get the cache information to prepare header to send to browser |
|
| 276 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 277 | - if($cacheInfo){ |
|
| 278 | - $lastModified = $cacheInfo['mtime']; |
|
| 279 | - $expire = $cacheInfo['expire']; |
|
| 280 | - $maxAge = $expire - time(); |
|
| 281 | - self::setHeader('Pragma', 'public'); |
|
| 282 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 283 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 284 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 285 | - } |
|
| 286 | - } |
|
| 275 | + //get the cache information to prepare header to send to browser |
|
| 276 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
| 277 | + if($cacheInfo){ |
|
| 278 | + $lastModified = $cacheInfo['mtime']; |
|
| 279 | + $expire = $cacheInfo['expire']; |
|
| 280 | + $maxAge = $expire - time(); |
|
| 281 | + self::setHeader('Pragma', 'public'); |
|
| 282 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 283 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 284 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - // Parse out the elapsed time and memory usage, |
|
| 289 | - // then swap the pseudo-variables with the data |
|
| 290 | - $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 291 | - $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 292 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 288 | + // Parse out the elapsed time and memory usage, |
|
| 289 | + // then swap the pseudo-variables with the data |
|
| 290 | + $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 291 | + $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 292 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 293 | 293 | |
| 294 | - //compress the output if is available |
|
| 295 | - $type = null; |
|
| 296 | - if (self::$_canCompressOutput){ |
|
| 297 | - $type = 'ob_gzhandler'; |
|
| 298 | - } |
|
| 299 | - ob_start($type); |
|
| 300 | - self::sendHeaders(200); |
|
| 301 | - echo $content; |
|
| 302 | - ob_end_flush(); |
|
| 303 | - } |
|
| 294 | + //compress the output if is available |
|
| 295 | + $type = null; |
|
| 296 | + if (self::$_canCompressOutput){ |
|
| 297 | + $type = 'ob_gzhandler'; |
|
| 298 | + } |
|
| 299 | + ob_start($type); |
|
| 300 | + self::sendHeaders(200); |
|
| 301 | + echo $content; |
|
| 302 | + ob_end_flush(); |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Send the final page output to user if is cached |
|
| 307 | - */ |
|
| 308 | - public function renderFinalPageFromCache(&$cache){ |
|
| 309 | - $logger = self::getLogger(); |
|
| 310 | - $url = $this->_currentUrl; |
|
| 311 | - //the current page cache key for identification |
|
| 312 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 305 | + /** |
|
| 306 | + * Send the final page output to user if is cached |
|
| 307 | + */ |
|
| 308 | + public function renderFinalPageFromCache(&$cache){ |
|
| 309 | + $logger = self::getLogger(); |
|
| 310 | + $url = $this->_currentUrl; |
|
| 311 | + //the current page cache key for identification |
|
| 312 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
| 313 | 313 | |
| 314 | - $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 315 | - //get the cache information to prepare header to send to browser |
|
| 316 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 317 | - if($cacheInfo){ |
|
| 318 | - $lastModified = $cacheInfo['mtime']; |
|
| 319 | - $expire = $cacheInfo['expire']; |
|
| 320 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 321 | - self::setHeader('Pragma', 'public'); |
|
| 322 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 323 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 324 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 325 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 326 | - $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 327 | - self::sendHeaders(304); |
|
| 328 | - return; |
|
| 329 | - } |
|
| 330 | - else{ |
|
| 331 | - $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 332 | - self::sendHeaders(200); |
|
| 333 | - //get the cache content |
|
| 334 | - $content = $cache->get($pageCacheKey); |
|
| 335 | - if($content){ |
|
| 336 | - $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 337 | - //load benchmark class |
|
| 338 | - $benchmark = & class_loader('Benchmark'); |
|
| 314 | + $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
|
| 315 | + //get the cache information to prepare header to send to browser |
|
| 316 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
| 317 | + if($cacheInfo){ |
|
| 318 | + $lastModified = $cacheInfo['mtime']; |
|
| 319 | + $expire = $cacheInfo['expire']; |
|
| 320 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
| 321 | + self::setHeader('Pragma', 'public'); |
|
| 322 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
| 323 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 324 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 325 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 326 | + $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
|
| 327 | + self::sendHeaders(304); |
|
| 328 | + return; |
|
| 329 | + } |
|
| 330 | + else{ |
|
| 331 | + $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
|
| 332 | + self::sendHeaders(200); |
|
| 333 | + //get the cache content |
|
| 334 | + $content = $cache->get($pageCacheKey); |
|
| 335 | + if($content){ |
|
| 336 | + $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
|
| 337 | + //load benchmark class |
|
| 338 | + $benchmark = & class_loader('Benchmark'); |
|
| 339 | 339 | |
| 340 | - // Parse out the elapsed time and memory usage, |
|
| 341 | - // then swap the pseudo-variables with the data |
|
| 342 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 343 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 344 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 340 | + // Parse out the elapsed time and memory usage, |
|
| 341 | + // then swap the pseudo-variables with the data |
|
| 342 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
| 343 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
| 344 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
| 345 | 345 | |
| 346 | - ///display the final output |
|
| 347 | - //compress the output if is available |
|
| 348 | - if (self::$_canCompressOutput){ |
|
| 349 | - ob_start('ob_gzhandler'); |
|
| 350 | - } |
|
| 351 | - else{ |
|
| 352 | - ob_start(); |
|
| 353 | - } |
|
| 354 | - echo $content; |
|
| 355 | - ob_end_flush(); |
|
| 356 | - return; |
|
| 357 | - } |
|
| 358 | - else{ |
|
| 359 | - $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 360 | - $cache->delete($pageCacheKey); |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - } |
|
| 346 | + ///display the final output |
|
| 347 | + //compress the output if is available |
|
| 348 | + if (self::$_canCompressOutput){ |
|
| 349 | + ob_start('ob_gzhandler'); |
|
| 350 | + } |
|
| 351 | + else{ |
|
| 352 | + ob_start(); |
|
| 353 | + } |
|
| 354 | + echo $content; |
|
| 355 | + ob_end_flush(); |
|
| 356 | + return; |
|
| 357 | + } |
|
| 358 | + else{ |
|
| 359 | + $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
|
| 360 | + $cache->delete($pageCacheKey); |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Get the final page to be rendered |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public function getFinalPageRendered(){ |
|
| 371 | - return $this->_pageRender; |
|
| 372 | - } |
|
| 366 | + /** |
|
| 367 | + * Get the final page to be rendered |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public function getFinalPageRendered(){ |
|
| 371 | + return $this->_pageRender; |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Send the HTTP 404 error if can not found the |
|
| 376 | - * routing information for the current request |
|
| 377 | - */ |
|
| 378 | - public static function send404(){ |
|
| 379 | - /********* for logs **************/ |
|
| 380 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 381 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
| 382 | - $logger = self::getLogger(); |
|
| 383 | - $request =& class_loader('Request', 'classes'); |
|
| 384 | - $userAgent =& class_loader('Browser'); |
|
| 385 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 374 | + /** |
|
| 375 | + * Send the HTTP 404 error if can not found the |
|
| 376 | + * routing information for the current request |
|
| 377 | + */ |
|
| 378 | + public static function send404(){ |
|
| 379 | + /********* for logs **************/ |
|
| 380 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
| 381 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
| 382 | + $logger = self::getLogger(); |
|
| 383 | + $request =& class_loader('Request', 'classes'); |
|
| 384 | + $userAgent =& class_loader('Browser'); |
|
| 385 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 386 | 386 | |
| 387 | - //here can't use Loader::functions just include the helper manually |
|
| 388 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 387 | + //here can't use Loader::functions just include the helper manually |
|
| 388 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 389 | 389 | |
| 390 | - $str = '[404 page not found] : '; |
|
| 391 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 392 | - $logger->error($str); |
|
| 393 | - /***********************************/ |
|
| 394 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
| 395 | - if(file_exists($path)){ |
|
| 396 | - //compress the output if is available |
|
| 397 | - if (self::$_canCompressOutput){ |
|
| 398 | - ob_start('ob_gzhandler'); |
|
| 399 | - } |
|
| 400 | - else{ |
|
| 401 | - ob_start(); |
|
| 402 | - } |
|
| 403 | - require_once $path; |
|
| 404 | - $output = ob_get_clean(); |
|
| 405 | - self::sendHeaders(404); |
|
| 406 | - echo $output; |
|
| 407 | - } |
|
| 408 | - else{ |
|
| 409 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 410 | - } |
|
| 411 | - } |
|
| 390 | + $str = '[404 page not found] : '; |
|
| 391 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
| 392 | + $logger->error($str); |
|
| 393 | + /***********************************/ |
|
| 394 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
| 395 | + if(file_exists($path)){ |
|
| 396 | + //compress the output if is available |
|
| 397 | + if (self::$_canCompressOutput){ |
|
| 398 | + ob_start('ob_gzhandler'); |
|
| 399 | + } |
|
| 400 | + else{ |
|
| 401 | + ob_start(); |
|
| 402 | + } |
|
| 403 | + require_once $path; |
|
| 404 | + $output = ob_get_clean(); |
|
| 405 | + self::sendHeaders(404); |
|
| 406 | + echo $output; |
|
| 407 | + } |
|
| 408 | + else{ |
|
| 409 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | 412 | |
| 413 | - /** |
|
| 414 | - * Display the error to user |
|
| 415 | - * @param array $data the error information |
|
| 416 | - */ |
|
| 417 | - public static function sendError(array $data = array()){ |
|
| 418 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 419 | - if(file_exists($path)){ |
|
| 420 | - //compress the output if exists |
|
| 421 | - if (self::$_canCompressOutput){ |
|
| 422 | - ob_start('ob_gzhandler'); |
|
| 423 | - } |
|
| 424 | - else{ |
|
| 425 | - ob_start(); |
|
| 426 | - } |
|
| 427 | - extract($data); |
|
| 428 | - require_once $path; |
|
| 429 | - $output = ob_get_clean(); |
|
| 430 | - self::sendHeaders(503); |
|
| 431 | - echo $output; |
|
| 432 | - } |
|
| 433 | - else{ |
|
| 434 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 435 | - set_http_status_header(503); |
|
| 436 | - echo 'The error view [' . $path . '] does not exist'; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 413 | + /** |
|
| 414 | + * Display the error to user |
|
| 415 | + * @param array $data the error information |
|
| 416 | + */ |
|
| 417 | + public static function sendError(array $data = array()){ |
|
| 418 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
| 419 | + if(file_exists($path)){ |
|
| 420 | + //compress the output if exists |
|
| 421 | + if (self::$_canCompressOutput){ |
|
| 422 | + ob_start('ob_gzhandler'); |
|
| 423 | + } |
|
| 424 | + else{ |
|
| 425 | + ob_start(); |
|
| 426 | + } |
|
| 427 | + extract($data); |
|
| 428 | + require_once $path; |
|
| 429 | + $output = ob_get_clean(); |
|
| 430 | + self::sendHeaders(503); |
|
| 431 | + echo $output; |
|
| 432 | + } |
|
| 433 | + else{ |
|
| 434 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
| 435 | + set_http_status_header(503); |
|
| 436 | + echo 'The error view [' . $path . '] does not exist'; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
@@ -1,256 +1,256 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class Request{ |
|
| 27 | + class Request{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The value for the super global $_GET |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - public $get = null; |
|
| 29 | + /** |
|
| 30 | + * The value for the super global $_GET |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + public $get = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The value for the super global $_POST |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $post = null; |
|
| 35 | + /** |
|
| 36 | + * The value for the super global $_POST |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $post = null; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The value for the super global $_SERVER |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - public $server = null; |
|
| 41 | + /** |
|
| 42 | + * The value for the super global $_SERVER |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + public $server = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * The value for the super global $_COOKIE |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - public $cookie = null; |
|
| 47 | + /** |
|
| 48 | + * The value for the super global $_COOKIE |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + public $cookie = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The value for the super global $_FILES |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - public $file = null; |
|
| 53 | + /** |
|
| 54 | + * The value for the super global $_FILES |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + public $file = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * The value for the super global $_REQUEST |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - public $query = null; |
|
| 59 | + /** |
|
| 60 | + * The value for the super global $_REQUEST |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + public $query = null; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * The session instance |
|
| 67 | - * @var Session |
|
| 68 | - */ |
|
| 69 | - public $session = null; |
|
| 65 | + /** |
|
| 66 | + * The session instance |
|
| 67 | + * @var Session |
|
| 68 | + */ |
|
| 69 | + public $session = null; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * The request headers |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - public $header = null; |
|
| 71 | + /** |
|
| 72 | + * The request headers |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + public $header = null; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | - * @var null |
|
| 80 | - */ |
|
| 81 | - private $method = null; |
|
| 77 | + /** |
|
| 78 | + * The current request method 'GET', 'POST', 'PUT', etc. |
|
| 79 | + * @var null |
|
| 80 | + */ |
|
| 81 | + private $method = null; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * The current request URI |
|
| 85 | - * @var string |
|
| 86 | - */ |
|
| 87 | - private $requestUri = null; |
|
| 83 | + /** |
|
| 84 | + * The current request URI |
|
| 85 | + * @var string |
|
| 86 | + */ |
|
| 87 | + private $requestUri = null; |
|
| 88 | 88 | |
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Construct new request instance |
|
| 92 | - */ |
|
| 93 | - public function __construct(){ |
|
| 94 | - $this->get = $_GET; |
|
| 95 | - $this->post = $_POST; |
|
| 96 | - $this->server = $_SERVER; |
|
| 97 | - $this->query = $_REQUEST; |
|
| 98 | - $this->cookie = $_COOKIE; |
|
| 99 | - $this->file = $_FILES; |
|
| 100 | - $this->session =& class_loader('Session', 'classes'); |
|
| 101 | - $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | - $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | - $this->header = array(); |
|
| 104 | - if(function_exists('apache_request_headers')){ |
|
| 105 | - $this->header = apache_request_headers(); |
|
| 106 | - } |
|
| 107 | - else if(function_exists('getallheaders')){ |
|
| 108 | - $this->header = getallheaders(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 90 | + /** |
|
| 91 | + * Construct new request instance |
|
| 92 | + */ |
|
| 93 | + public function __construct(){ |
|
| 94 | + $this->get = $_GET; |
|
| 95 | + $this->post = $_POST; |
|
| 96 | + $this->server = $_SERVER; |
|
| 97 | + $this->query = $_REQUEST; |
|
| 98 | + $this->cookie = $_COOKIE; |
|
| 99 | + $this->file = $_FILES; |
|
| 100 | + $this->session =& class_loader('Session', 'classes'); |
|
| 101 | + $this->method = $this->server('REQUEST_METHOD'); |
|
| 102 | + $this->requestUri = $this->server('REQUEST_URI'); |
|
| 103 | + $this->header = array(); |
|
| 104 | + if(function_exists('apache_request_headers')){ |
|
| 105 | + $this->header = apache_request_headers(); |
|
| 106 | + } |
|
| 107 | + else if(function_exists('getallheaders')){ |
|
| 108 | + $this->header = getallheaders(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Get the request method |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function method(){ |
|
| 117 | - return $this->method; |
|
| 118 | - } |
|
| 112 | + /** |
|
| 113 | + * Get the request method |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function method(){ |
|
| 117 | + return $this->method; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Get the request URI |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function requestUri(){ |
|
| 125 | - return $this->requestUri; |
|
| 126 | - } |
|
| 120 | + /** |
|
| 121 | + * Get the request URI |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function requestUri(){ |
|
| 125 | + return $this->requestUri; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 130 | - * @param string $key the item key to be fetched |
|
| 131 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 132 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 133 | - */ |
|
| 134 | - public function query($key = null, $xss = true){ |
|
| 135 | - if(empty($key)){ |
|
| 136 | - //return all |
|
| 137 | - return $xss ? clean_input($this->query) : $this->query; |
|
| 138 | - } |
|
| 139 | - $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 140 | - if($xss){ |
|
| 141 | - $query = clean_input($query); |
|
| 142 | - } |
|
| 143 | - return $query; |
|
| 144 | - } |
|
| 128 | + /** |
|
| 129 | + * Get the value from $_REQUEST for given key. if the key is empty will return the all values |
|
| 130 | + * @param string $key the item key to be fetched |
|
| 131 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 132 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 133 | + */ |
|
| 134 | + public function query($key = null, $xss = true){ |
|
| 135 | + if(empty($key)){ |
|
| 136 | + //return all |
|
| 137 | + return $xss ? clean_input($this->query) : $this->query; |
|
| 138 | + } |
|
| 139 | + $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
|
| 140 | + if($xss){ |
|
| 141 | + $query = clean_input($query); |
|
| 142 | + } |
|
| 143 | + return $query; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 148 | - * @param string $key the item key to be fetched |
|
| 149 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 150 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 151 | - */ |
|
| 152 | - public function get($key = null, $xss = true){ |
|
| 153 | - if(empty($key)){ |
|
| 154 | - //return all |
|
| 155 | - return $xss ? clean_input($this->get) : $this->get; |
|
| 156 | - } |
|
| 157 | - $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 158 | - if($xss){ |
|
| 159 | - $get = clean_input($get); |
|
| 160 | - } |
|
| 161 | - return $get; |
|
| 162 | - } |
|
| 146 | + /** |
|
| 147 | + * Get the value from $_GET for given key. if the key is empty will return the all values |
|
| 148 | + * @param string $key the item key to be fetched |
|
| 149 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 150 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 151 | + */ |
|
| 152 | + public function get($key = null, $xss = true){ |
|
| 153 | + if(empty($key)){ |
|
| 154 | + //return all |
|
| 155 | + return $xss ? clean_input($this->get) : $this->get; |
|
| 156 | + } |
|
| 157 | + $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
|
| 158 | + if($xss){ |
|
| 159 | + $get = clean_input($get); |
|
| 160 | + } |
|
| 161 | + return $get; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 166 | - * @param string $key the item key to be fetched |
|
| 167 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 168 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 169 | - */ |
|
| 170 | - public function post($key = null, $xss = true){ |
|
| 171 | - if(empty($key)){ |
|
| 172 | - //return all |
|
| 173 | - return $xss ? clean_input($this->post) : $this->post; |
|
| 174 | - } |
|
| 175 | - $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 176 | - if($xss){ |
|
| 177 | - $post = clean_input($post); |
|
| 178 | - } |
|
| 179 | - return $post; |
|
| 180 | - } |
|
| 164 | + /** |
|
| 165 | + * Get the value from $_POST for given key. if the key is empty will return the all values |
|
| 166 | + * @param string $key the item key to be fetched |
|
| 167 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 168 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 169 | + */ |
|
| 170 | + public function post($key = null, $xss = true){ |
|
| 171 | + if(empty($key)){ |
|
| 172 | + //return all |
|
| 173 | + return $xss ? clean_input($this->post) : $this->post; |
|
| 174 | + } |
|
| 175 | + $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
|
| 176 | + if($xss){ |
|
| 177 | + $post = clean_input($post); |
|
| 178 | + } |
|
| 179 | + return $post; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 184 | - * @param string $key the item key to be fetched |
|
| 185 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 186 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 187 | - */ |
|
| 188 | - public function server($key = null, $xss = true){ |
|
| 189 | - if(empty($key)){ |
|
| 190 | - //return all |
|
| 191 | - return $xss ? clean_input($this->server) : $this->server; |
|
| 192 | - } |
|
| 193 | - $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 194 | - if($xss){ |
|
| 195 | - $server = clean_input($server); |
|
| 196 | - } |
|
| 197 | - return $server; |
|
| 198 | - } |
|
| 182 | + /** |
|
| 183 | + * Get the value from $_SERVER for given key. if the key is empty will return the all values |
|
| 184 | + * @param string $key the item key to be fetched |
|
| 185 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 186 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 187 | + */ |
|
| 188 | + public function server($key = null, $xss = true){ |
|
| 189 | + if(empty($key)){ |
|
| 190 | + //return all |
|
| 191 | + return $xss ? clean_input($this->server) : $this->server; |
|
| 192 | + } |
|
| 193 | + $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
|
| 194 | + if($xss){ |
|
| 195 | + $server = clean_input($server); |
|
| 196 | + } |
|
| 197 | + return $server; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 202 | - * @param string $key the item key to be fetched |
|
| 203 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 204 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 205 | - */ |
|
| 206 | - public function cookie($key = null, $xss = true){ |
|
| 207 | - if(empty($key)){ |
|
| 208 | - //return all |
|
| 209 | - return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 210 | - } |
|
| 211 | - $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 212 | - if($xss){ |
|
| 213 | - $cookie = clean_input($cookie); |
|
| 214 | - } |
|
| 215 | - return $cookie; |
|
| 216 | - } |
|
| 200 | + /** |
|
| 201 | + * Get the value from $_COOKIE for given key. if the key is empty will return the all values |
|
| 202 | + * @param string $key the item key to be fetched |
|
| 203 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 204 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 205 | + */ |
|
| 206 | + public function cookie($key = null, $xss = true){ |
|
| 207 | + if(empty($key)){ |
|
| 208 | + //return all |
|
| 209 | + return $xss ? clean_input($this->cookie) : $this->cookie; |
|
| 210 | + } |
|
| 211 | + $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
|
| 212 | + if($xss){ |
|
| 213 | + $cookie = clean_input($cookie); |
|
| 214 | + } |
|
| 215 | + return $cookie; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 220 | - * @param string $key the item key to be fetched |
|
| 221 | - * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 222 | - */ |
|
| 223 | - public function file($key){ |
|
| 224 | - $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 225 | - return $file; |
|
| 226 | - } |
|
| 218 | + /** |
|
| 219 | + * Get the value from $_FILES for given key. if the key is empty will return the all values |
|
| 220 | + * @param string $key the item key to be fetched |
|
| 221 | + * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
|
| 222 | + */ |
|
| 223 | + public function file($key){ |
|
| 224 | + $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
|
| 225 | + return $file; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 230 | - * @param string $key the item key to be fetched |
|
| 231 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 232 | - * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 233 | - */ |
|
| 234 | - public function session($key, $xss = true){ |
|
| 235 | - $session = $this->session->get($key); |
|
| 236 | - if($xss){ |
|
| 237 | - $session = clean_input($session); |
|
| 238 | - } |
|
| 239 | - return $session; |
|
| 240 | - } |
|
| 228 | + /** |
|
| 229 | + * Get the value from $_SESSION for given key. if the key is empty will return the all values |
|
| 230 | + * @param string $key the item key to be fetched |
|
| 231 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 232 | + * @return array|mixed the item value if the key exists or null if the key does not exists |
|
| 233 | + */ |
|
| 234 | + public function session($key, $xss = true){ |
|
| 235 | + $session = $this->session->get($key); |
|
| 236 | + if($xss){ |
|
| 237 | + $session = clean_input($session); |
|
| 238 | + } |
|
| 239 | + return $session; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Get the value from header array for given key. |
|
| 244 | - * @param string $key the item key to be fetched |
|
| 245 | - * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 246 | - * @return mixed the item value if the key exists or null if the key does not exists |
|
| 247 | - */ |
|
| 248 | - public function header($key, $xss = true){ |
|
| 249 | - $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 250 | - if($xss){ |
|
| 251 | - $header = clean_input($header); |
|
| 252 | - } |
|
| 253 | - return $header; |
|
| 254 | - } |
|
| 242 | + /** |
|
| 243 | + * Get the value from header array for given key. |
|
| 244 | + * @param string $key the item key to be fetched |
|
| 245 | + * @param boolean $xss if need apply some XSS attack rule on the value |
|
| 246 | + * @return mixed the item value if the key exists or null if the key does not exists |
|
| 247 | + */ |
|
| 248 | + public function header($key, $xss = true){ |
|
| 249 | + $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
|
| 250 | + if($xss){ |
|
| 251 | + $header = clean_input($header); |
|
| 252 | + } |
|
| 253 | + return $header; |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - } |
|
| 256 | + } |
|
@@ -1,215 +1,215 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - class ApcCache implements CacheInterface{ |
|
| 27 | + class ApcCache implements CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The logger instance |
|
| 31 | - * @var Log |
|
| 32 | - */ |
|
| 33 | - private $logger; |
|
| 29 | + /** |
|
| 30 | + * The logger instance |
|
| 31 | + * @var Log |
|
| 32 | + */ |
|
| 33 | + private $logger; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct(Log $logger = null){ |
|
| 37 | - if(! $this->isSupported()){ |
|
| 38 | - show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | - } |
|
| 36 | + public function __construct(Log $logger = null){ |
|
| 37 | + if(! $this->isSupported()){ |
|
| 38 | + show_error('The cache for APC[u] driver is not available. Check if APC[u] extension is loaded and enabled.'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * instance of the Log class |
|
| 43 | - */ |
|
| 44 | - if(is_object($logger)){ |
|
| 45 | - $this->logger = $logger; |
|
| 46 | - } |
|
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | - } |
|
| 51 | - } |
|
| 41 | + /** |
|
| 42 | + * instance of the Log class |
|
| 43 | + */ |
|
| 44 | + if(is_object($logger)){ |
|
| 45 | + $this->logger = $logger; |
|
| 46 | + } |
|
| 47 | + else{ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * This is used to get the cache data using the key |
|
| 55 | - * @param string $key the key to identify the cache data |
|
| 56 | - * @return mixed the cache data if exists else return false |
|
| 57 | - */ |
|
| 58 | - public function get($key){ |
|
| 59 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | - $success = false; |
|
| 61 | - $data = apc_fetch($key, $success); |
|
| 62 | - if($success === false){ |
|
| 63 | - $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | - return false; |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | - $expire = time(); |
|
| 69 | - if($cacheInfo){ |
|
| 70 | - $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | - } |
|
| 72 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | - return $data; |
|
| 74 | - } |
|
| 75 | - } |
|
| 53 | + /** |
|
| 54 | + * This is used to get the cache data using the key |
|
| 55 | + * @param string $key the key to identify the cache data |
|
| 56 | + * @return mixed the cache data if exists else return false |
|
| 57 | + */ |
|
| 58 | + public function get($key){ |
|
| 59 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 60 | + $success = false; |
|
| 61 | + $data = apc_fetch($key, $success); |
|
| 62 | + if($success === false){ |
|
| 63 | + $this->logger->info('No cache found for the key ['. $key .'], return false'); |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 68 | + $expire = time(); |
|
| 69 | + if($cacheInfo){ |
|
| 70 | + $expire = $cacheInfo['creation_time'] + $cacheInfo['ttl']; |
|
| 71 | + } |
|
| 72 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 73 | + return $data; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Save data to the cache |
|
| 80 | - * @param string $key the key to identify this cache data |
|
| 81 | - * @param mixed $data the cache data to be saved |
|
| 82 | - * @param integer $ttl the cache life time |
|
| 83 | - * @return boolean true if success otherwise will return false |
|
| 84 | - */ |
|
| 85 | - public function set($key, $data, $ttl = 0){ |
|
| 86 | - $expire = time() + $ttl; |
|
| 87 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | - $result = apc_store($key, $data, $ttl); |
|
| 89 | - if($result === false){ |
|
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - } |
|
| 78 | + /** |
|
| 79 | + * Save data to the cache |
|
| 80 | + * @param string $key the key to identify this cache data |
|
| 81 | + * @param mixed $data the cache data to be saved |
|
| 82 | + * @param integer $ttl the cache life time |
|
| 83 | + * @return boolean true if success otherwise will return false |
|
| 84 | + */ |
|
| 85 | + public function set($key, $data, $ttl = 0){ |
|
| 86 | + $expire = time() + $ttl; |
|
| 87 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 88 | + $result = apc_store($key, $data, $ttl); |
|
| 89 | + if($result === false){ |
|
| 90 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + else{ |
|
| 94 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Delete the cache data for given key |
|
| 102 | - * @param string $key the key for cache to be deleted |
|
| 103 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | - * the cache or the cache with the given key not exist |
|
| 105 | - */ |
|
| 106 | - public function delete($key){ |
|
| 107 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | - $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | - if($cacheInfo === false){ |
|
| 110 | - $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - else{ |
|
| 114 | - $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | - return apc_delete($key) === true; |
|
| 116 | - } |
|
| 117 | - } |
|
| 100 | + /** |
|
| 101 | + * Delete the cache data for given key |
|
| 102 | + * @param string $key the key for cache to be deleted |
|
| 103 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 104 | + * the cache or the cache with the given key not exist |
|
| 105 | + */ |
|
| 106 | + public function delete($key){ |
|
| 107 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 108 | + $cacheInfo = $this->_getCacheInfo($key); |
|
| 109 | + if($cacheInfo === false){ |
|
| 110 | + $this->logger->info('This cache data does not exists skipping'); |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + else{ |
|
| 114 | + $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
|
| 115 | + return apc_delete($key) === true; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get the cache information for given key |
|
| 121 | - * @param string $key the key for cache to get the information for |
|
| 122 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | - * 'ttl' => the time to live of the cache in second |
|
| 126 | - */ |
|
| 127 | - public function getInfo($key){ |
|
| 128 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | - $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | - if($cacheInfos){ |
|
| 131 | - $data = array( |
|
| 132 | - 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | - 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | - 'ttl' => $cacheInfos['ttl'] |
|
| 135 | - ); |
|
| 136 | - return $data; |
|
| 137 | - } |
|
| 138 | - else{ |
|
| 139 | - $this->logger->info('This cache does not exists skipping'); |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - } |
|
| 119 | + /** |
|
| 120 | + * Get the cache information for given key |
|
| 121 | + * @param string $key the key for cache to get the information for |
|
| 122 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 123 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 124 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 125 | + * 'ttl' => the time to live of the cache in second |
|
| 126 | + */ |
|
| 127 | + public function getInfo($key){ |
|
| 128 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 129 | + $cacheInfos = $this->_getCacheInfo($key); |
|
| 130 | + if($cacheInfos){ |
|
| 131 | + $data = array( |
|
| 132 | + 'mtime' => $cacheInfos['creation_time'], |
|
| 133 | + 'expire' => $cacheInfos['creation_time'] + $cacheInfos['ttl'], |
|
| 134 | + 'ttl' => $cacheInfos['ttl'] |
|
| 135 | + ); |
|
| 136 | + return $data; |
|
| 137 | + } |
|
| 138 | + else{ |
|
| 139 | + $this->logger->info('This cache does not exists skipping'); |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Used to delete expired cache data |
|
| 147 | - */ |
|
| 148 | - public function deleteExpiredCache(){ |
|
| 149 | - //for APC[u] is done automatically |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 145 | + /** |
|
| 146 | + * Used to delete expired cache data |
|
| 147 | + */ |
|
| 148 | + public function deleteExpiredCache(){ |
|
| 149 | + //for APC[u] is done automatically |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Remove all cache data |
|
| 155 | - */ |
|
| 156 | - public function clean(){ |
|
| 157 | - $this->logger->debug('Deleting of all cache data'); |
|
| 158 | - $cacheInfos = apc_cache_info('user'); |
|
| 159 | - if(empty($cacheInfos['cache_list'])){ |
|
| 160 | - $this->logger->info('No cache data were found skipping'); |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | - return apc_clear_cache('user'); |
|
| 166 | - } |
|
| 167 | - } |
|
| 153 | + /** |
|
| 154 | + * Remove all cache data |
|
| 155 | + */ |
|
| 156 | + public function clean(){ |
|
| 157 | + $this->logger->debug('Deleting of all cache data'); |
|
| 158 | + $cacheInfos = apc_cache_info('user'); |
|
| 159 | + if(empty($cacheInfos['cache_list'])){ |
|
| 160 | + $this->logger->info('No cache data were found skipping'); |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + $this->logger->info('Found [' . count($cacheInfos) . '] cache data to remove'); |
|
| 165 | + return apc_clear_cache('user'); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check whether the cache feature for the handle is supported |
|
| 172 | - * |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function isSupported(){ |
|
| 176 | - return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | - } |
|
| 170 | + /** |
|
| 171 | + * Check whether the cache feature for the handle is supported |
|
| 172 | + * |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function isSupported(){ |
|
| 176 | + return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Return the Log instance |
|
| 181 | - * @return Log |
|
| 182 | - */ |
|
| 183 | - public function getLogger(){ |
|
| 184 | - return $this->logger; |
|
| 185 | - } |
|
| 179 | + /** |
|
| 180 | + * Return the Log instance |
|
| 181 | + * @return Log |
|
| 182 | + */ |
|
| 183 | + public function getLogger(){ |
|
| 184 | + return $this->logger; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Set the log instance |
|
| 189 | - * @param Log $logger the log object |
|
| 190 | - */ |
|
| 191 | - public function setLogger(Log $logger){ |
|
| 192 | - $this->logger = $logger; |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 187 | + /** |
|
| 188 | + * Set the log instance |
|
| 189 | + * @param Log $logger the log object |
|
| 190 | + */ |
|
| 191 | + public function setLogger(Log $logger){ |
|
| 192 | + $this->logger = $logger; |
|
| 193 | + return $this; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Return the array of cache information |
|
| 198 | - * |
|
| 199 | - * @param string $key the cache key to get the cache information |
|
| 200 | - * @return boolean|array |
|
| 201 | - */ |
|
| 202 | - private function _getCacheInfo($key){ |
|
| 203 | - $caches = apc_cache_info('user'); |
|
| 204 | - if(! empty($caches['cache_list'])){ |
|
| 205 | - $cacheLists = $caches['cache_list']; |
|
| 206 | - foreach ($cacheLists as $c){ |
|
| 207 | - if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | - return $c; |
|
| 209 | - } |
|
| 210 | - } |
|
| 196 | + /** |
|
| 197 | + * Return the array of cache information |
|
| 198 | + * |
|
| 199 | + * @param string $key the cache key to get the cache information |
|
| 200 | + * @return boolean|array |
|
| 201 | + */ |
|
| 202 | + private function _getCacheInfo($key){ |
|
| 203 | + $caches = apc_cache_info('user'); |
|
| 204 | + if(! empty($caches['cache_list'])){ |
|
| 205 | + $cacheLists = $caches['cache_list']; |
|
| 206 | + foreach ($cacheLists as $c){ |
|
| 207 | + if(isset($c['info']) && $c['info'] === $key){ |
|
| 208 | + return $c; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - } |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 212 | + } |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
@@ -1,84 +1,84 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - interface CacheInterface{ |
|
| 27 | + interface CacheInterface{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This is used to get the cache data using the key |
|
| 31 | - * @param string $key the key to identify the cache data |
|
| 32 | - * @return mixed the cache data if exists else return false |
|
| 33 | - */ |
|
| 34 | - public function get($key); |
|
| 29 | + /** |
|
| 30 | + * This is used to get the cache data using the key |
|
| 31 | + * @param string $key the key to identify the cache data |
|
| 32 | + * @return mixed the cache data if exists else return false |
|
| 33 | + */ |
|
| 34 | + public function get($key); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Save data to the cache |
|
| 39 | - * @param string $key the key to identify this cache data |
|
| 40 | - * @param mixed $data the cache data to be saved |
|
| 41 | - * @param integer $ttl the cache life time |
|
| 42 | - * @return boolean true if success otherwise will return false |
|
| 43 | - */ |
|
| 44 | - public function set($key, $data, $ttl = 0); |
|
| 37 | + /** |
|
| 38 | + * Save data to the cache |
|
| 39 | + * @param string $key the key to identify this cache data |
|
| 40 | + * @param mixed $data the cache data to be saved |
|
| 41 | + * @param integer $ttl the cache life time |
|
| 42 | + * @return boolean true if success otherwise will return false |
|
| 43 | + */ |
|
| 44 | + public function set($key, $data, $ttl = 0); |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Delete the cache data for given key |
|
| 49 | - * @param string $key the key for cache to be deleted |
|
| 50 | - * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | - * the cache or the cache with the given key not exist |
|
| 52 | - */ |
|
| 53 | - public function delete($key); |
|
| 47 | + /** |
|
| 48 | + * Delete the cache data for given key |
|
| 49 | + * @param string $key the key for cache to be deleted |
|
| 50 | + * @return boolean true if the cache is deleted, false if can't delete |
|
| 51 | + * the cache or the cache with the given key not exist |
|
| 52 | + */ |
|
| 53 | + public function delete($key); |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get the cache information for given key |
|
| 58 | - * @param string $key the key for cache to get the information for |
|
| 59 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | - * 'ttl' => the time to live of the cache in second |
|
| 63 | - */ |
|
| 64 | - public function getInfo($key); |
|
| 56 | + /** |
|
| 57 | + * Get the cache information for given key |
|
| 58 | + * @param string $key the key for cache to get the information for |
|
| 59 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
| 60 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
| 61 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
| 62 | + * 'ttl' => the time to live of the cache in second |
|
| 63 | + */ |
|
| 64 | + public function getInfo($key); |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Used to delete expired cache data |
|
| 69 | - */ |
|
| 70 | - public function deleteExpiredCache(); |
|
| 67 | + /** |
|
| 68 | + * Used to delete expired cache data |
|
| 69 | + */ |
|
| 70 | + public function deleteExpiredCache(); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Remove all cache data |
|
| 74 | - */ |
|
| 75 | - public function clean(); |
|
| 72 | + /** |
|
| 73 | + * Remove all cache data |
|
| 74 | + */ |
|
| 75 | + public function clean(); |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Check whether the cache feature for the handle is supported |
|
| 80 | - * |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - public function isSupported(); |
|
| 84 | - } |
|
| 78 | + /** |
|
| 79 | + * Check whether the cache feature for the handle is supported |
|
| 80 | + * |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + public function isSupported(); |
|
| 84 | + } |
|