@@ -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,90 +1,90 @@ 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 DatabaseQueryRunner{ |
|
| 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 DatabaseQueryRunner{ |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $logger = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The last query result |
|
| 36 | - * @var object |
|
| 37 | - */ |
|
| 38 | - private $queryResult = null; |
|
| 34 | + /** |
|
| 35 | + * The last query result |
|
| 36 | + * @var object |
|
| 37 | + */ |
|
| 38 | + private $queryResult = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The benchmark instance |
|
| 42 | - * @var object |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The benchmark instance |
|
| 42 | + * @var object |
|
| 43 | + */ |
|
| 44 | 44 | private $benchmarkInstance = null; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * The SQL query statment to execute |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 47 | + * The SQL query statment to execute |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | 50 | private $query = null; |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | - * Indicate if we need return result as list (boolean) |
|
| 53 | + * Indicate if we need return result as list (boolean) |
|
| 54 | 54 | * or the data used to replace the placeholder (array) |
| 55 | - * @var array|boolean |
|
| 56 | - */ |
|
| 57 | - private $all = true; |
|
| 55 | + * @var array|boolean |
|
| 56 | + */ |
|
| 57 | + private $all = true; |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Indicate if we need return result as array or not |
|
| 62 | - * @var boolean |
|
| 63 | - */ |
|
| 64 | - private $returnAsArray = true; |
|
| 60 | + /** |
|
| 61 | + * Indicate if we need return result as array or not |
|
| 62 | + * @var boolean |
|
| 63 | + */ |
|
| 64 | + private $returnAsArray = true; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The last PDOStatment instance |
|
| 68 | - * @var object |
|
| 69 | - */ |
|
| 70 | - private $pdoStatment = null; |
|
| 66 | + /** |
|
| 67 | + * The last PDOStatment instance |
|
| 68 | + * @var object |
|
| 69 | + */ |
|
| 70 | + private $pdoStatment = null; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * The error returned for the last query |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - private $error = null; |
|
| 72 | + /** |
|
| 73 | + * The error returned for the last query |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + private $error = null; |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * The PDO instance |
| 80 | 80 | * @var object |
| 81 | - */ |
|
| 81 | + */ |
|
| 82 | 82 | private $pdo = null; |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * The database driver name used |
| 86 | 86 | * @var string |
| 87 | - */ |
|
| 87 | + */ |
|
| 88 | 88 | private $driver = null; |
| 89 | 89 | |
| 90 | 90 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false){ |
| 100 | 100 | if (is_object($pdo)){ |
| 101 | - $this->pdo = $pdo; |
|
| 101 | + $this->pdo = $pdo; |
|
| 102 | 102 | } |
| 103 | 103 | $this->query = $query; |
| 104 | 104 | $this->returnAsList = $returnAsList; |
@@ -116,16 +116,16 @@ discard block |
||
| 116 | 116 | //reset instance |
| 117 | 117 | $this->reset(); |
| 118 | 118 | |
| 119 | - //for database query execution time |
|
| 119 | + //for database query execution time |
|
| 120 | 120 | $benchmarkMarkerKey = $this->getBenchmarkKey(); |
| 121 | 121 | if (! is_object($this->benchmarkInstance)){ |
| 122 | - $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 122 | + $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $this->logger->info( |
| 126 | - 'Execute SQL query [' . $this->query . '], return type: ' |
|
| 127 | - . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') .', return as list: ' |
|
| 128 | - . ($this->returnAsList ? 'YES':'NO') |
|
| 126 | + 'Execute SQL query [' . $this->query . '], return type: ' |
|
| 127 | + . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') .', return as list: ' |
|
| 128 | + . ($this->returnAsList ? 'YES':'NO') |
|
| 129 | 129 | ); |
| 130 | 130 | |
| 131 | 131 | $this->benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
@@ -136,101 +136,101 @@ discard block |
||
| 136 | 136 | $responseTime = $this->benchmarkInstance->elapsedTime( |
| 137 | 137 | 'DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', |
| 138 | 138 | 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')' |
| 139 | - ); |
|
| 140 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 139 | + ); |
|
| 140 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 141 | 141 | if ($responseTime >= 1 ){ |
| 142 | 142 | $this->logger->warning( |
| 143 | 143 | 'High response time while processing database query [' . $this->query . ']. |
| 144 | 144 | The response time is [' .$responseTime. '] sec.' |
| 145 | - ); |
|
| 145 | + ); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | if ($this->pdoStatment !== false){ |
| 149 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 150 | - if($isSqlSELECTQuery){ |
|
| 151 | - $this->setResultForSelect(); |
|
| 152 | - } |
|
| 153 | - else{ |
|
| 154 | - $this->setResultForNonSelect(); |
|
| 155 | - } |
|
| 156 | - return $this->queryResult; |
|
| 149 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 150 | + if($isSqlSELECTQuery){ |
|
| 151 | + $this->setResultForSelect(); |
|
| 152 | + } |
|
| 153 | + else{ |
|
| 154 | + $this->setResultForNonSelect(); |
|
| 155 | + } |
|
| 156 | + return $this->queryResult; |
|
| 157 | 157 | } |
| 158 | 158 | $this->setQueryRunnerError(); |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Return the result for SELECT query |
|
| 163 | - * @see DatabaseQueryRunner::execute |
|
| 164 | - */ |
|
| 161 | + /** |
|
| 162 | + * Return the result for SELECT query |
|
| 163 | + * @see DatabaseQueryRunner::execute |
|
| 164 | + */ |
|
| 165 | 165 | protected function setResultForSelect(){ |
| 166 | - //if need return all result like list of record |
|
| 167 | - $result = null; |
|
| 168 | - $numRows = 0; |
|
| 169 | - $fetchMode = PDO::FETCH_OBJ; |
|
| 170 | - if($this->returnAsArray){ |
|
| 166 | + //if need return all result like list of record |
|
| 167 | + $result = null; |
|
| 168 | + $numRows = 0; |
|
| 169 | + $fetchMode = PDO::FETCH_OBJ; |
|
| 170 | + if($this->returnAsArray){ |
|
| 171 | 171 | $fetchMode = PDO::FETCH_ASSOC; |
| 172 | - } |
|
| 173 | - if ($this->returnAsList){ |
|
| 174 | - $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 175 | - } |
|
| 176 | - else{ |
|
| 177 | - $result = $this->pdoStatment->fetch($fetchMode); |
|
| 178 | - } |
|
| 179 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 180 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 172 | + } |
|
| 173 | + if ($this->returnAsList){ |
|
| 174 | + $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 175 | + } |
|
| 176 | + else{ |
|
| 177 | + $result = $this->pdoStatment->fetch($fetchMode); |
|
| 178 | + } |
|
| 179 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 180 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 181 | 181 | $numRows = count($result); |
| 182 | - } |
|
| 183 | - else{ |
|
| 182 | + } |
|
| 183 | + else{ |
|
| 184 | 184 | $numRows = $this->pdoStatment->rowCount(); |
| 185 | - } |
|
| 186 | - if(! is_object($this->queryResult)){ |
|
| 187 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 188 | - } |
|
| 189 | - $this->queryResult->setResult($result); |
|
| 190 | - $this->queryResult->setNumRows($numRows); |
|
| 185 | + } |
|
| 186 | + if(! is_object($this->queryResult)){ |
|
| 187 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 188 | + } |
|
| 189 | + $this->queryResult->setResult($result); |
|
| 190 | + $this->queryResult->setNumRows($numRows); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | - * Return the result for non SELECT query |
|
| 195 | - * @see DatabaseQueryRunner::execute |
|
| 196 | - */ |
|
| 194 | + * Return the result for non SELECT query |
|
| 195 | + * @see DatabaseQueryRunner::execute |
|
| 196 | + */ |
|
| 197 | 197 | protected function setResultForNonSelect(){ |
| 198 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 199 | - $result = false; |
|
| 200 | - $numRows = 0; |
|
| 201 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 198 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 199 | + $result = false; |
|
| 200 | + $numRows = 0; |
|
| 201 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 202 | 202 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
| 203 | 203 | $numRows = 1; //TODO use the correct method to get the exact affected row |
| 204 | - } |
|
| 205 | - else{ |
|
| 206 | - //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 207 | - $result = $this->pdoStatment->rowCount() >= 0; |
|
| 208 | - $numRows = $this->pdoStatment->rowCount(); |
|
| 209 | - } |
|
| 210 | - if(! is_object($this->queryResult)){ |
|
| 211 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 212 | - } |
|
| 213 | - $this->queryResult->setResult($result); |
|
| 214 | - $this->queryResult->setNumRows($numRows); |
|
| 204 | + } |
|
| 205 | + else{ |
|
| 206 | + //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 207 | + $result = $this->pdoStatment->rowCount() >= 0; |
|
| 208 | + $numRows = $this->pdoStatment->rowCount(); |
|
| 209 | + } |
|
| 210 | + if(! is_object($this->queryResult)){ |
|
| 211 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 212 | + } |
|
| 213 | + $this->queryResult->setResult($result); |
|
| 214 | + $this->queryResult->setNumRows($numRows); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | |
| 218 | - /** |
|
| 218 | + /** |
|
| 219 | 219 | * Return the benchmark instance |
| 220 | 220 | * @return Benchmark |
| 221 | 221 | */ |
| 222 | 222 | public function getBenchmark(){ |
| 223 | - return $this->benchmarkInstance; |
|
| 223 | + return $this->benchmarkInstance; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
| 227 | 227 | * Set the benchmark instance |
| 228 | 228 | * @param Benchmark $benchmark the benchmark object |
| 229 | - * @return object DatabaseQueryRunner |
|
| 229 | + * @return object DatabaseQueryRunner |
|
| 230 | 230 | */ |
| 231 | 231 | public function setBenchmark($benchmark){ |
| 232 | - $this->benchmarkInstance = $benchmark; |
|
| 233 | - return $this; |
|
| 232 | + $this->benchmarkInstance = $benchmark; |
|
| 233 | + return $this; |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
@@ -239,18 +239,18 @@ discard block |
||
| 239 | 239 | * @return object DatabaseQueryResult |
| 240 | 240 | */ |
| 241 | 241 | public function getQueryResult(){ |
| 242 | - return $this->queryResult; |
|
| 242 | + return $this->queryResult; |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
| 246 | 246 | * Set the database query result instance |
| 247 | 247 | * @param object $queryResult the query result |
| 248 | 248 | * |
| 249 | - * @return object DatabaseQueryRunner |
|
| 249 | + * @return object DatabaseQueryRunner |
|
| 250 | 250 | */ |
| 251 | 251 | public function setQueryResult(DatabaseQueryResult $queryResult){ |
| 252 | - $this->queryResult = $queryResult; |
|
| 253 | - return $this; |
|
| 252 | + $this->queryResult = $queryResult; |
|
| 253 | + return $this; |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | /** |
@@ -258,17 +258,17 @@ discard block |
||
| 258 | 258 | * @return Log |
| 259 | 259 | */ |
| 260 | 260 | public function getLogger(){ |
| 261 | - return $this->logger; |
|
| 261 | + return $this->logger; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
| 265 | 265 | * Set the log instance |
| 266 | 266 | * @param Log $logger the log object |
| 267 | - * @return object DatabaseQueryRunner |
|
| 267 | + * @return object DatabaseQueryRunner |
|
| 268 | 268 | */ |
| 269 | 269 | public function setLogger($logger){ |
| 270 | - $this->logger = $logger; |
|
| 271 | - return $this; |
|
| 270 | + $this->logger = $logger; |
|
| 271 | + return $this; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | * @return string |
| 277 | 277 | */ |
| 278 | 278 | public function getQuery(){ |
| 279 | - return $this->query; |
|
| 279 | + return $this->query; |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | /** |
@@ -285,8 +285,8 @@ discard block |
||
| 285 | 285 | * @return object DatabaseQueryRunner |
| 286 | 286 | */ |
| 287 | 287 | public function setQuery($query){ |
| 288 | - $this->query = $query; |
|
| 289 | - return $this; |
|
| 288 | + $this->query = $query; |
|
| 289 | + return $this; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
@@ -295,8 +295,8 @@ discard block |
||
| 295 | 295 | * @return object DatabaseQueryRunner |
| 296 | 296 | */ |
| 297 | 297 | public function setReturnType($returnType){ |
| 298 | - $this->returnAsList = $returnType; |
|
| 299 | - return $this; |
|
| 298 | + $this->returnAsList = $returnType; |
|
| 299 | + return $this; |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | /** |
@@ -305,8 +305,8 @@ discard block |
||
| 305 | 305 | * @return object DatabaseQueryRunner |
| 306 | 306 | */ |
| 307 | 307 | public function setReturnAsArray($status = true){ |
| 308 | - $this->returnAsArray = $status; |
|
| 309 | - return $this; |
|
| 308 | + $this->returnAsArray = $status; |
|
| 309 | + return $this; |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | 316 | public function getQueryError(){ |
| 317 | - return $this->error; |
|
| 317 | + return $this->error; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | * @return object |
| 323 | 323 | */ |
| 324 | 324 | public function getPdo(){ |
| 325 | - return $this->pdo; |
|
| 325 | + return $this->pdo; |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
@@ -331,16 +331,16 @@ discard block |
||
| 331 | 331 | * @return object DatabaseQueryRunner |
| 332 | 332 | */ |
| 333 | 333 | public function setPdo(PDO $pdo = null){ |
| 334 | - $this->pdo = $pdo; |
|
| 335 | - return $this; |
|
| 334 | + $this->pdo = $pdo; |
|
| 335 | + return $this; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - /** |
|
| 339 | - * Return the database driver |
|
| 340 | - * @return string |
|
| 341 | - */ |
|
| 338 | + /** |
|
| 339 | + * Return the database driver |
|
| 340 | + * @return string |
|
| 341 | + */ |
|
| 342 | 342 | public function getDriver(){ |
| 343 | - return $this->driver; |
|
| 343 | + return $this->driver; |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | /** |
@@ -349,8 +349,8 @@ discard block |
||
| 349 | 349 | * @return object DatabaseQueryRunner |
| 350 | 350 | */ |
| 351 | 351 | public function setDriver($driver){ |
| 352 | - $this->driver = $driver; |
|
| 353 | - return $this; |
|
| 352 | + $this->driver = $driver; |
|
| 353 | + return $this; |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
@@ -359,18 +359,18 @@ discard block |
||
| 359 | 359 | * @return string |
| 360 | 360 | */ |
| 361 | 361 | protected function getBenchmarkKey(){ |
| 362 | - return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 362 | + return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Set error for database query execution |
| 367 | 367 | */ |
| 368 | 368 | protected function setQueryRunnerError(){ |
| 369 | - $error = $this->pdo->errorInfo(); |
|
| 370 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 371 | - $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 372 | - //show error message |
|
| 373 | - show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 369 | + $error = $this->pdo->errorInfo(); |
|
| 370 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 371 | + $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 372 | + //show error message |
|
| 373 | + show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -378,19 +378,19 @@ discard block |
||
| 378 | 378 | * @param object $logger the Log instance if not null |
| 379 | 379 | */ |
| 380 | 380 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 381 | - if ($logger !== null){ |
|
| 381 | + if ($logger !== null){ |
|
| 382 | 382 | $this->logger = $logger; |
| 383 | - } |
|
| 384 | - else{ |
|
| 385 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 386 | - $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 387 | - } |
|
| 383 | + } |
|
| 384 | + else{ |
|
| 385 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 386 | + $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 387 | + } |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | |
| 391 | 391 | /** |
| 392 | - * Reset the instance before run each query |
|
| 393 | - */ |
|
| 392 | + * Reset the instance before run each query |
|
| 393 | + */ |
|
| 394 | 394 | private function reset(){ |
| 395 | 395 | $this->error = null; |
| 396 | 396 | $this->pdoStatment = null; |
@@ -1,119 +1,119 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - class Database{ |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + class Database{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The PDO instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 28 | + /** |
|
| 29 | + * The PDO instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $pdo = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The database name used for the application |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $databaseName = null; |
|
| 34 | + /** |
|
| 35 | + * The database name used for the application |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The number of rows returned by the last query |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The number of rows returned by the last query |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | 44 | private $numRows = 0; |
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | - * @var mixed |
|
| 49 | - */ |
|
| 46 | + /** |
|
| 47 | + * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | + * @var mixed |
|
| 49 | + */ |
|
| 50 | 50 | private $insertId = null; |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The full SQL query statment after build for each command |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 52 | + /** |
|
| 53 | + * The full SQL query statment after build for each command |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | 56 | private $query = null; |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * The result returned for the last query |
|
| 60 | - * @var mixed |
|
| 61 | - */ |
|
| 58 | + /** |
|
| 59 | + * The result returned for the last query |
|
| 60 | + * @var mixed |
|
| 61 | + */ |
|
| 62 | 62 | private $result = array(); |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $cacheTtl = 0; |
|
| 64 | + /** |
|
| 65 | + * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | - * @var int |
|
| 73 | - */ |
|
| 70 | + /** |
|
| 71 | + * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | + * @var int |
|
| 73 | + */ |
|
| 74 | 74 | private $temporaryCacheTtl = 0; |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * The number of executed query for the current request |
|
| 78 | - * @var int |
|
| 79 | - */ |
|
| 76 | + /** |
|
| 77 | + * The number of executed query for the current request |
|
| 78 | + * @var int |
|
| 79 | + */ |
|
| 80 | 80 | private $queryCount = 0; |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | - * @var array |
|
| 85 | - */ |
|
| 82 | + /** |
|
| 83 | + * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | + * @var array |
|
| 85 | + */ |
|
| 86 | 86 | private $data = array(); |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The database configuration |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The database configuration |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | 92 | private $config = array(); |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * The logger instance |
|
| 96 | - * @var object |
|
| 97 | - */ |
|
| 94 | + /** |
|
| 95 | + * The logger instance |
|
| 96 | + * @var object |
|
| 97 | + */ |
|
| 98 | 98 | private $logger = null; |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | - * The cache instance |
|
| 102 | - * @var object |
|
| 103 | - */ |
|
| 101 | + * The cache instance |
|
| 102 | + * @var object |
|
| 103 | + */ |
|
| 104 | 104 | private $cacheInstance = null; |
| 105 | 105 | |
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * The DatabaseQueryBuilder instance |
|
| 109 | - * @var object |
|
| 110 | - */ |
|
| 107 | + /** |
|
| 108 | + * The DatabaseQueryBuilder instance |
|
| 109 | + * @var object |
|
| 110 | + */ |
|
| 111 | 111 | private $queryBuilder = null; |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The DatabaseQueryRunner instance |
|
| 115 | - * @var object |
|
| 116 | - */ |
|
| 114 | + * The DatabaseQueryRunner instance |
|
| 115 | + * @var object |
|
| 116 | + */ |
|
| 117 | 117 | private $queryRunner = null; |
| 118 | 118 | |
| 119 | 119 | |
@@ -125,8 +125,8 @@ discard block |
||
| 125 | 125 | //Set Log instance to use |
| 126 | 126 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 127 | 127 | |
| 128 | - //Set DatabaseQueryBuilder instance to use |
|
| 129 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 128 | + //Set DatabaseQueryBuilder instance to use |
|
| 129 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 130 | 130 | |
| 131 | 131 | //Set DatabaseQueryRunner instance to use |
| 132 | 132 | $this->setQueryRunnerFromParamOrCreateNewInstance(null); |
@@ -143,22 +143,22 @@ discard block |
||
| 143 | 143 | * @return bool |
| 144 | 144 | */ |
| 145 | 145 | public function connect(){ |
| 146 | - $config = $this->getDatabaseConfiguration(); |
|
| 147 | - if (! empty($config)){ |
|
| 146 | + $config = $this->getDatabaseConfiguration(); |
|
| 147 | + if (! empty($config)){ |
|
| 148 | 148 | try{ |
| 149 | 149 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 150 | 150 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 153 | 153 | return true; |
| 154 | - } |
|
| 155 | - catch (PDOException $e){ |
|
| 154 | + } |
|
| 155 | + catch (PDOException $e){ |
|
| 156 | 156 | $this->logger->fatal($e->getMessage()); |
| 157 | 157 | show_error('Cannot connect to Database.'); |
| 158 | 158 | return false; |
| 159 | - } |
|
| 160 | - } |
|
| 161 | - return false; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + return false; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @return int |
| 168 | 168 | */ |
| 169 | 169 | public function numRows(){ |
| 170 | - return $this->numRows; |
|
| 170 | + return $this->numRows; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | * @return mixed |
| 176 | 176 | */ |
| 177 | 177 | public function insertId(){ |
| 178 | - return $this->insertId; |
|
| 178 | + return $this->insertId; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -186,13 +186,13 @@ discard block |
||
| 186 | 186 | * @return mixed the query SQL string or the record result |
| 187 | 187 | */ |
| 188 | 188 | public function get($returnSQLQueryOrResultType = false){ |
| 189 | - $this->getQueryBuilder()->limit(1); |
|
| 190 | - $query = $this->getAll(true); |
|
| 191 | - if ($returnSQLQueryOrResultType === true){ |
|
| 189 | + $this->getQueryBuilder()->limit(1); |
|
| 190 | + $query = $this->getAll(true); |
|
| 191 | + if ($returnSQLQueryOrResultType === true){ |
|
| 192 | 192 | return $query; |
| 193 | - } else { |
|
| 193 | + } else { |
|
| 194 | 194 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 195 | - } |
|
| 195 | + } |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | * @return mixed the query SQL string or the record result |
| 203 | 203 | */ |
| 204 | 204 | public function getAll($returnSQLQueryOrResultType = false){ |
| 205 | - $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | - if ($returnSQLQueryOrResultType === true){ |
|
| 207 | - return $query; |
|
| 208 | - } |
|
| 209 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 205 | + $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | + if ($returnSQLQueryOrResultType === true){ |
|
| 207 | + return $query; |
|
| 208 | + } |
|
| 209 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -216,19 +216,19 @@ discard block |
||
| 216 | 216 | * @return mixed the insert id of the new record or null |
| 217 | 217 | */ |
| 218 | 218 | public function insert($data = array(), $escape = true){ |
| 219 | - if (empty($data) && $this->getData()){ |
|
| 219 | + if (empty($data) && $this->getData()){ |
|
| 220 | 220 | //as when using $this->setData() may be the data already escaped |
| 221 | 221 | $escape = false; |
| 222 | 222 | $data = $this->getData(); |
| 223 | - } |
|
| 224 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | - $result = $this->query($query); |
|
| 226 | - if ($result){ |
|
| 223 | + } |
|
| 224 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | + $result = $this->query($query); |
|
| 226 | + if ($result){ |
|
| 227 | 227 | $this->insertId = $this->pdo->lastInsertId(); |
| 228 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 228 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 229 | 229 | return ! $this->insertId() ? true : $this->insertId(); |
| 230 | - } |
|
| 231 | - return false; |
|
| 230 | + } |
|
| 231 | + return false; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
@@ -238,13 +238,13 @@ discard block |
||
| 238 | 238 | * @return mixed the update status |
| 239 | 239 | */ |
| 240 | 240 | public function update($data = array(), $escape = true){ |
| 241 | - if (empty($data) && $this->getData()){ |
|
| 241 | + if (empty($data) && $this->getData()){ |
|
| 242 | 242 | //as when using $this->setData() may be the data already escaped |
| 243 | 243 | $escape = false; |
| 244 | 244 | $data = $this->getData(); |
| 245 | - } |
|
| 246 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | - return $this->query($query); |
|
| 245 | + } |
|
| 246 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | + return $this->query($query); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -252,8 +252,8 @@ discard block |
||
| 252 | 252 | * @return mixed the delete status |
| 253 | 253 | */ |
| 254 | 254 | public function delete(){ |
| 255 | - $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | - return $this->query($query); |
|
| 255 | + $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | + return $this->query($query); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -262,21 +262,21 @@ discard block |
||
| 262 | 262 | * @return object the current Database instance |
| 263 | 263 | */ |
| 264 | 264 | public function setCache($ttl = 0){ |
| 265 | - if ($ttl > 0){ |
|
| 265 | + if ($ttl > 0){ |
|
| 266 | 266 | $this->cacheTtl = $ttl; |
| 267 | 267 | $this->temporaryCacheTtl = $ttl; |
| 268 | - } |
|
| 269 | - return $this; |
|
| 268 | + } |
|
| 269 | + return $this; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - /** |
|
| 273 | - * Enabled cache temporary for the current query not globally |
|
| 274 | - * @param integer $ttl the cache time to live in second |
|
| 275 | - * @return object the current Database instance |
|
| 276 | - */ |
|
| 277 | - public function cached($ttl = 0){ |
|
| 272 | + /** |
|
| 273 | + * Enabled cache temporary for the current query not globally |
|
| 274 | + * @param integer $ttl the cache time to live in second |
|
| 275 | + * @return object the current Database instance |
|
| 276 | + */ |
|
| 277 | + public function cached($ttl = 0){ |
|
| 278 | 278 | if ($ttl > 0){ |
| 279 | - $this->temporaryCacheTtl = $ttl; |
|
| 279 | + $this->temporaryCacheTtl = $ttl; |
|
| 280 | 280 | } |
| 281 | 281 | return $this; |
| 282 | 282 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @return mixed the data after escaped or the same data if not |
| 289 | 289 | */ |
| 290 | 290 | public function escape($data, $escaped = true){ |
| 291 | - return $escaped ? |
|
| 291 | + return $escaped ? |
|
| 292 | 292 | $this->pdo->quote(trim($data)) |
| 293 | 293 | : $data; |
| 294 | 294 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | * @return int |
| 299 | 299 | */ |
| 300 | 300 | public function queryCount(){ |
| 301 | - return $this->queryCount; |
|
| 301 | + return $this->queryCount; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @return string |
| 307 | 307 | */ |
| 308 | 308 | public function getQuery(){ |
| 309 | - return $this->query; |
|
| 309 | + return $this->query; |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | 316 | public function getDatabaseName(){ |
| 317 | - return $this->databaseName; |
|
| 317 | + return $this->databaseName; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -322,17 +322,17 @@ discard block |
||
| 322 | 322 | * @return object |
| 323 | 323 | */ |
| 324 | 324 | public function getPdo(){ |
| 325 | - return $this->pdo; |
|
| 325 | + return $this->pdo; |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Set the PDO instance |
| 330 | 330 | * @param object $pdo the pdo object |
| 331 | - * @return object Database |
|
| 331 | + * @return object Database |
|
| 332 | 332 | */ |
| 333 | 333 | public function setPdo(PDO $pdo){ |
| 334 | - $this->pdo = $pdo; |
|
| 335 | - return $this; |
|
| 334 | + $this->pdo = $pdo; |
|
| 335 | + return $this; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | |
@@ -341,44 +341,44 @@ discard block |
||
| 341 | 341 | * @return Log |
| 342 | 342 | */ |
| 343 | 343 | public function getLogger(){ |
| 344 | - return $this->logger; |
|
| 344 | + return $this->logger; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
| 348 | 348 | * Set the log instance |
| 349 | 349 | * @param Log $logger the log object |
| 350 | - * @return object Database |
|
| 350 | + * @return object Database |
|
| 351 | 351 | */ |
| 352 | 352 | public function setLogger($logger){ |
| 353 | - $this->logger = $logger; |
|
| 354 | - return $this; |
|
| 353 | + $this->logger = $logger; |
|
| 354 | + return $this; |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - /** |
|
| 358 | - * Return the cache instance |
|
| 359 | - * @return CacheInterface |
|
| 360 | - */ |
|
| 357 | + /** |
|
| 358 | + * Return the cache instance |
|
| 359 | + * @return CacheInterface |
|
| 360 | + */ |
|
| 361 | 361 | public function getCacheInstance(){ |
| 362 | - return $this->cacheInstance; |
|
| 362 | + return $this->cacheInstance; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Set the cache instance |
| 367 | 367 | * @param CacheInterface $cache the cache object |
| 368 | - * @return object Database |
|
| 368 | + * @return object Database |
|
| 369 | 369 | */ |
| 370 | 370 | public function setCacheInstance($cache){ |
| 371 | - $this->cacheInstance = $cache; |
|
| 372 | - return $this; |
|
| 371 | + $this->cacheInstance = $cache; |
|
| 372 | + return $this; |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | |
| 376 | - /** |
|
| 377 | - * Return the DatabaseQueryBuilder instance |
|
| 378 | - * @return object DatabaseQueryBuilder |
|
| 379 | - */ |
|
| 376 | + /** |
|
| 377 | + * Return the DatabaseQueryBuilder instance |
|
| 378 | + * @return object DatabaseQueryBuilder |
|
| 379 | + */ |
|
| 380 | 380 | public function getQueryBuilder(){ |
| 381 | - return $this->queryBuilder; |
|
| 381 | + return $this->queryBuilder; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,8 +386,8 @@ discard block |
||
| 386 | 386 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 387 | 387 | */ |
| 388 | 388 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
| 389 | - $this->queryBuilder = $queryBuilder; |
|
| 390 | - return $this; |
|
| 389 | + $this->queryBuilder = $queryBuilder; |
|
| 390 | + return $this; |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * @return object DatabaseQueryRunner |
| 396 | 396 | */ |
| 397 | 397 | public function getQueryRunner(){ |
| 398 | - return $this->queryRunner; |
|
| 398 | + return $this->queryRunner; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | /** |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 404 | 404 | */ |
| 405 | 405 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
| 406 | - $this->queryRunner = $queryRunner; |
|
| 407 | - return $this; |
|
| 406 | + $this->queryRunner = $queryRunner; |
|
| 407 | + return $this; |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | * @return array |
| 413 | 413 | */ |
| 414 | 414 | public function getData(){ |
| 415 | - return $this->data; |
|
| 415 | + return $this->data; |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | /** |
@@ -423,51 +423,51 @@ discard block |
||
| 423 | 423 | * @return object the current Database instance |
| 424 | 424 | */ |
| 425 | 425 | public function setData($key, $value = null, $escape = true){ |
| 426 | - if(is_array($key)){ |
|
| 427 | - foreach($key as $k => $v){ |
|
| 428 | - $this->setData($k, $v, $escape); |
|
| 429 | - } |
|
| 430 | - } else { |
|
| 426 | + if(is_array($key)){ |
|
| 427 | + foreach($key as $k => $v){ |
|
| 428 | + $this->setData($k, $v, $escape); |
|
| 429 | + } |
|
| 430 | + } else { |
|
| 431 | 431 | $this->data[$key] = $this->escape($value, $escape); |
| 432 | - } |
|
| 433 | - return $this; |
|
| 432 | + } |
|
| 433 | + return $this; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - /** |
|
| 437 | - * Execute an SQL query |
|
| 438 | - * @param string $query the query SQL string |
|
| 439 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | - * @param boolean $returnAsArray return the result as array or not |
|
| 441 | - * @return mixed the query result |
|
| 442 | - */ |
|
| 436 | + /** |
|
| 437 | + * Execute an SQL query |
|
| 438 | + * @param string $query the query SQL string |
|
| 439 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | + * @param boolean $returnAsArray return the result as array or not |
|
| 441 | + * @return mixed the query result |
|
| 442 | + */ |
|
| 443 | 443 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
| 444 | - $this->reset(); |
|
| 445 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | - //If is the SELECT query |
|
| 447 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 444 | + $this->reset(); |
|
| 445 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | + //If is the SELECT query |
|
| 447 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 448 | 448 | |
| 449 | - //cache expire time |
|
| 450 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 449 | + //cache expire time |
|
| 450 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 451 | 451 | |
| 452 | - //return to the initial cache time |
|
| 453 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 452 | + //return to the initial cache time |
|
| 453 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 454 | 454 | |
| 455 | - //config for cache |
|
| 456 | - $cacheEnable = get_config('cache_enable'); |
|
| 455 | + //config for cache |
|
| 456 | + $cacheEnable = get_config('cache_enable'); |
|
| 457 | 457 | |
| 458 | - //the database cache content |
|
| 459 | - $cacheContent = null; |
|
| 458 | + //the database cache content |
|
| 459 | + $cacheContent = null; |
|
| 460 | 460 | |
| 461 | - //if can use cache feature for this query |
|
| 462 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 461 | + //if can use cache feature for this query |
|
| 462 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 463 | 463 | |
| 464 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | - } |
|
| 464 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - if ( !$cacheContent){ |
|
| 470 | - //count the number of query execution to server |
|
| 469 | + if ( !$cacheContent){ |
|
| 470 | + //count the number of query execution to server |
|
| 471 | 471 | $this->queryCount++; |
| 472 | 472 | |
| 473 | 473 | $this->queryRunner->setQuery($query); |
@@ -484,35 +484,35 @@ discard block |
||
| 484 | 484 | $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray), |
| 485 | 485 | $this->result, |
| 486 | 486 | $cacheExpire |
| 487 | - ); |
|
| 487 | + ); |
|
| 488 | 488 | if (! $this->result){ |
| 489 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 489 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 490 | 490 | } |
| 491 | - } |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + } else if ($isSqlSELECTQuery){ |
|
| 494 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 495 | + $this->result = $cacheContent; |
|
| 496 | + $this->numRows = count($this->result); |
|
| 492 | 497 | } |
| 493 | - } else if ($isSqlSELECTQuery){ |
|
| 494 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 495 | - $this->result = $cacheContent; |
|
| 496 | - $this->numRows = count($this->result); |
|
| 497 | - } |
|
| 498 | - return $this->result; |
|
| 498 | + return $this->result; |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | |
| 502 | - /** |
|
| 503 | - * Return the database configuration |
|
| 504 | - * @return array |
|
| 505 | - */ |
|
| 506 | - public function getDatabaseConfiguration(){ |
|
| 507 | - return $this->config; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
| 512 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 513 | - * @param boolean $useConfigFile whether to use database configuration file |
|
| 514 | - * @return object Database |
|
| 515 | - */ |
|
| 502 | + /** |
|
| 503 | + * Return the database configuration |
|
| 504 | + * @return array |
|
| 505 | + */ |
|
| 506 | + public function getDatabaseConfiguration(){ |
|
| 507 | + return $this->config; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
| 512 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 513 | + * @param boolean $useConfigFile whether to use database configuration file |
|
| 514 | + * @return object Database |
|
| 515 | + */ |
|
| 516 | 516 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
| 517 | 517 | $db = array(); |
| 518 | 518 | if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
@@ -525,50 +525,50 @@ discard block |
||
| 525 | 525 | |
| 526 | 526 | //default configuration |
| 527 | 527 | $config = array( |
| 528 | - 'driver' => 'mysql', |
|
| 529 | - 'username' => 'root', |
|
| 530 | - 'password' => '', |
|
| 531 | - 'database' => '', |
|
| 532 | - 'hostname' => 'localhost', |
|
| 533 | - 'charset' => 'utf8', |
|
| 534 | - 'collation' => 'utf8_general_ci', |
|
| 535 | - 'prefix' => '', |
|
| 536 | - 'port' => '' |
|
| 528 | + 'driver' => 'mysql', |
|
| 529 | + 'username' => 'root', |
|
| 530 | + 'password' => '', |
|
| 531 | + 'database' => '', |
|
| 532 | + 'hostname' => 'localhost', |
|
| 533 | + 'charset' => 'utf8', |
|
| 534 | + 'collation' => 'utf8_general_ci', |
|
| 535 | + 'prefix' => '', |
|
| 536 | + 'port' => '' |
|
| 537 | 537 | ); |
| 538 | 538 | |
| 539 | - $config = array_merge($config, $db); |
|
| 540 | - //determine the port using the hostname like localhost:3307 |
|
| 541 | - //hostname will be "localhost", and port "3307" |
|
| 542 | - $p = explode(':', $config['hostname']); |
|
| 543 | - if (count($p) >= 2){ |
|
| 544 | - $config['hostname'] = $p[0]; |
|
| 545 | - $config['port'] = $p[1]; |
|
| 546 | - } |
|
| 539 | + $config = array_merge($config, $db); |
|
| 540 | + //determine the port using the hostname like localhost:3307 |
|
| 541 | + //hostname will be "localhost", and port "3307" |
|
| 542 | + $p = explode(':', $config['hostname']); |
|
| 543 | + if (count($p) >= 2){ |
|
| 544 | + $config['hostname'] = $p[0]; |
|
| 545 | + $config['port'] = $p[1]; |
|
| 546 | + } |
|
| 547 | 547 | |
| 548 | - $this->databaseName = $config['database']; |
|
| 549 | - $this->config = $config; |
|
| 550 | - $this->logger->info( |
|
| 551 | - 'The database configuration are listed below: ' |
|
| 552 | - . stringfy_vars(array_merge( |
|
| 553 | - $this->config, |
|
| 554 | - array('password' => string_hidden($this->config['password'])) |
|
| 555 | - )) |
|
| 556 | - ); |
|
| 548 | + $this->databaseName = $config['database']; |
|
| 549 | + $this->config = $config; |
|
| 550 | + $this->logger->info( |
|
| 551 | + 'The database configuration are listed below: ' |
|
| 552 | + . stringfy_vars(array_merge( |
|
| 553 | + $this->config, |
|
| 554 | + array('password' => string_hidden($this->config['password'])) |
|
| 555 | + )) |
|
| 556 | + ); |
|
| 557 | 557 | |
| 558 | - //Now connect to the database |
|
| 559 | - $this->connect(); |
|
| 558 | + //Now connect to the database |
|
| 559 | + $this->connect(); |
|
| 560 | 560 | |
| 561 | - //do update of QueryRunner and Builder |
|
| 562 | - $this->updateQueryBuilderAndRunnerProperties(); |
|
| 561 | + //do update of QueryRunner and Builder |
|
| 562 | + $this->updateQueryBuilderAndRunnerProperties(); |
|
| 563 | 563 | |
| 564 | - return $this; |
|
| 564 | + return $this; |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /** |
| 568 | 568 | * Close the connexion |
| 569 | 569 | */ |
| 570 | 570 | public function close(){ |
| 571 | - $this->pdo = null; |
|
| 571 | + $this->pdo = null; |
|
| 572 | 572 | } |
| 573 | 573 | |
| 574 | 574 | /** |
@@ -576,18 +576,18 @@ discard block |
||
| 576 | 576 | * @return void |
| 577 | 577 | */ |
| 578 | 578 | protected function updateQueryBuilderAndRunnerProperties(){ |
| 579 | - //update queryBuilder with some properties needed |
|
| 580 | - if(is_object($this->queryBuilder)){ |
|
| 579 | + //update queryBuilder with some properties needed |
|
| 580 | + if(is_object($this->queryBuilder)){ |
|
| 581 | 581 | $this->queryBuilder->setDriver($this->config['driver']); |
| 582 | 582 | $this->queryBuilder->setPrefix($this->config['prefix']); |
| 583 | 583 | $this->queryBuilder->setPdo($this->pdo); |
| 584 | - } |
|
| 584 | + } |
|
| 585 | 585 | |
| 586 | - //update queryRunner with some properties needed |
|
| 587 | - if(is_object($this->queryRunner)){ |
|
| 586 | + //update queryRunner with some properties needed |
|
| 587 | + if(is_object($this->queryRunner)){ |
|
| 588 | 588 | $this->queryRunner->setDriver($this->config['driver']); |
| 589 | 589 | $this->queryRunner->setPdo($this->pdo); |
| 590 | - } |
|
| 590 | + } |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | |
@@ -596,24 +596,24 @@ discard block |
||
| 596 | 596 | * @return string the DSN string |
| 597 | 597 | */ |
| 598 | 598 | protected function getDsnFromDriver(){ |
| 599 | - $config = $this->getDatabaseConfiguration(); |
|
| 600 | - if (! empty($config)){ |
|
| 599 | + $config = $this->getDatabaseConfiguration(); |
|
| 600 | + if (! empty($config)){ |
|
| 601 | 601 | $driver = $config['driver']; |
| 602 | 602 | $driverDsnMap = array( |
| 603 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 604 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 605 | - . 'dbname=' . $config['database'], |
|
| 606 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 607 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 608 | - . 'dbname=' . $config['database'], |
|
| 609 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 610 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 603 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 604 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 605 | + . 'dbname=' . $config['database'], |
|
| 606 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 607 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 608 | + . 'dbname=' . $config['database'], |
|
| 609 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 610 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 611 | 611 | . (($config['port']) != '' ? ':' . $config['port'] : '') |
| 612 | 612 | . '/' . $config['database'] |
| 613 | - ); |
|
| 613 | + ); |
|
| 614 | 614 | return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
| 615 | - } |
|
| 616 | - return null; |
|
| 615 | + } |
|
| 616 | + return null; |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | /** |
@@ -625,11 +625,11 @@ discard block |
||
| 625 | 625 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
| 626 | 626 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 627 | 627 | if (! is_object($this->cacheInstance)){ |
| 628 | - //can not call method with reference in argument |
|
| 629 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 630 | - //use temporary variable |
|
| 631 | - $instance = & get_instance()->cache; |
|
| 632 | - $this->cacheInstance = $instance; |
|
| 628 | + //can not call method with reference in argument |
|
| 629 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 630 | + //use temporary variable |
|
| 631 | + $instance = & get_instance()->cache; |
|
| 632 | + $this->cacheInstance = $instance; |
|
| 633 | 633 | } |
| 634 | 634 | return $this->cacheInstance->get($cacheKey); |
| 635 | 635 | } |
@@ -641,87 +641,87 @@ discard block |
||
| 641 | 641 | * @param mixed $result the query result to save |
| 642 | 642 | * @param int $expire the cache TTL |
| 643 | 643 | */ |
| 644 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 644 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 645 | 645 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
| 646 | 646 | if (! is_object($this->cacheInstance)){ |
| 647 | - //can not call method with reference in argument |
|
| 648 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 649 | - //use temporary variable |
|
| 650 | - $instance = & get_instance()->cache; |
|
| 651 | - $this->cacheInstance = $instance; |
|
| 652 | - } |
|
| 647 | + //can not call method with reference in argument |
|
| 648 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 649 | + //use temporary variable |
|
| 650 | + $instance = & get_instance()->cache; |
|
| 651 | + $this->cacheInstance = $instance; |
|
| 652 | + } |
|
| 653 | 653 | $this->cacheInstance->set($key, $result, $expire); |
| 654 | - } |
|
| 654 | + } |
|
| 655 | 655 | |
| 656 | 656 | |
| 657 | - /** |
|
| 658 | - * Return the cache key for the given query |
|
| 659 | - * @see Database::query |
|
| 660 | - * |
|
| 661 | - * @return string |
|
| 662 | - */ |
|
| 657 | + /** |
|
| 658 | + * Return the cache key for the given query |
|
| 659 | + * @see Database::query |
|
| 660 | + * |
|
| 661 | + * @return string |
|
| 662 | + */ |
|
| 663 | 663 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
| 664 | - return md5($query . $returnAsList . $returnAsArray); |
|
| 664 | + return md5($query . $returnAsList . $returnAsArray); |
|
| 665 | 665 | } |
| 666 | 666 | |
| 667 | - /** |
|
| 668 | - * Set the Log instance using argument or create new instance |
|
| 669 | - * @param object $logger the Log instance if not null |
|
| 670 | - */ |
|
| 667 | + /** |
|
| 668 | + * Set the Log instance using argument or create new instance |
|
| 669 | + * @param object $logger the Log instance if not null |
|
| 670 | + */ |
|
| 671 | 671 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 672 | - if ($logger !== null){ |
|
| 672 | + if ($logger !== null){ |
|
| 673 | 673 | $this->logger = $logger; |
| 674 | - } |
|
| 675 | - else{ |
|
| 676 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 677 | - $this->logger->setLogger('Library::Database'); |
|
| 678 | - } |
|
| 674 | + } |
|
| 675 | + else{ |
|
| 676 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 677 | + $this->logger->setLogger('Library::Database'); |
|
| 678 | + } |
|
| 679 | 679 | } |
| 680 | 680 | |
| 681 | - /** |
|
| 682 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 683 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 684 | - */ |
|
| 685 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 686 | - if ($queryBuilder !== null){ |
|
| 681 | + /** |
|
| 682 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 683 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 684 | + */ |
|
| 685 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 686 | + if ($queryBuilder !== null){ |
|
| 687 | 687 | $this->queryBuilder = $queryBuilder; |
| 688 | - } |
|
| 689 | - else{ |
|
| 690 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 691 | - } |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 696 | - * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 697 | - */ |
|
| 698 | - protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 688 | + } |
|
| 689 | + else{ |
|
| 690 | + $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 691 | + } |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 696 | + * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 697 | + */ |
|
| 698 | + protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 699 | 699 | if ($queryRunner !== null){ |
| 700 | 700 | $this->queryRunner = $queryRunner; |
| 701 | 701 | } |
| 702 | 702 | else{ |
| 703 | - $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 703 | + $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 704 | + } |
|
| 704 | 705 | } |
| 705 | - } |
|
| 706 | 706 | |
| 707 | 707 | /** |
| 708 | 708 | * Reset the database class attributs to the initail values before each query. |
| 709 | 709 | */ |
| 710 | 710 | private function reset(){ |
| 711 | - //query builder reset |
|
| 712 | - $this->getQueryBuilder()->reset(); |
|
| 713 | - $this->numRows = 0; |
|
| 714 | - $this->insertId = null; |
|
| 715 | - $this->query = null; |
|
| 716 | - $this->result = array(); |
|
| 717 | - $this->data = array(); |
|
| 711 | + //query builder reset |
|
| 712 | + $this->getQueryBuilder()->reset(); |
|
| 713 | + $this->numRows = 0; |
|
| 714 | + $this->insertId = null; |
|
| 715 | + $this->query = null; |
|
| 716 | + $this->result = array(); |
|
| 717 | + $this->data = array(); |
|
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | /** |
| 721 | 721 | * The class destructor |
| 722 | 722 | */ |
| 723 | 723 | public function __destruct(){ |
| 724 | - $this->pdo = null; |
|
| 724 | + $this->pdo = null; |
|
| 725 | 725 | } |
| 726 | 726 | |
| 727 | 727 | } |
@@ -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,146 +1,146 @@ |
||
| 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 | 27 | |
| 28 | - class Url{ |
|
| 28 | + class Url{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Return the link using base_url config without front controller "index.php" |
|
| 32 | - * @param string $path the link path or full URL |
|
| 33 | - * @return string the full link URL |
|
| 34 | - */ |
|
| 35 | - public static function base_url($path = ''){ |
|
| 36 | - if(is_url($path)){ |
|
| 37 | - return $path; |
|
| 38 | - } |
|
| 39 | - return get_config('base_url') . $path; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * Return the link using base_url config without front controller "index.php" |
|
| 32 | + * @param string $path the link path or full URL |
|
| 33 | + * @return string the full link URL |
|
| 34 | + */ |
|
| 35 | + public static function base_url($path = ''){ |
|
| 36 | + if(is_url($path)){ |
|
| 37 | + return $path; |
|
| 38 | + } |
|
| 39 | + return get_config('base_url') . $path; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Return the link using base_url config with front controller "index.php" |
|
| 44 | - * @param string $path the link path or full URL |
|
| 45 | - * @return string the full link URL |
|
| 46 | - */ |
|
| 47 | - public static function site_url($path = ''){ |
|
| 48 | - if(is_url($path)){ |
|
| 49 | - return $path; |
|
| 50 | - } |
|
| 51 | - $path = rtrim($path, '/'); |
|
| 52 | - $baseUrl = get_config('base_url'); |
|
| 53 | - $frontController = get_config('front_controller'); |
|
| 54 | - $url = $baseUrl; |
|
| 55 | - if($frontController){ |
|
| 56 | - $url .= $frontController . '/'; |
|
| 57 | - } |
|
| 58 | - if(($suffix = get_config('url_suffix')) && $path){ |
|
| 59 | - if(strpos($path, '?') !== false){ |
|
| 60 | - $query = explode('?', $path); |
|
| 61 | - $query[0] = str_ireplace($suffix, '', $query[0]); |
|
| 62 | - $query[0] = rtrim($query[0], '/'); |
|
| 63 | - $query[0] .= $suffix; |
|
| 64 | - $path = implode('?', $query); |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $path .= $suffix; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - return $url . $path; |
|
| 71 | - } |
|
| 42 | + /** |
|
| 43 | + * Return the link using base_url config with front controller "index.php" |
|
| 44 | + * @param string $path the link path or full URL |
|
| 45 | + * @return string the full link URL |
|
| 46 | + */ |
|
| 47 | + public static function site_url($path = ''){ |
|
| 48 | + if(is_url($path)){ |
|
| 49 | + return $path; |
|
| 50 | + } |
|
| 51 | + $path = rtrim($path, '/'); |
|
| 52 | + $baseUrl = get_config('base_url'); |
|
| 53 | + $frontController = get_config('front_controller'); |
|
| 54 | + $url = $baseUrl; |
|
| 55 | + if($frontController){ |
|
| 56 | + $url .= $frontController . '/'; |
|
| 57 | + } |
|
| 58 | + if(($suffix = get_config('url_suffix')) && $path){ |
|
| 59 | + if(strpos($path, '?') !== false){ |
|
| 60 | + $query = explode('?', $path); |
|
| 61 | + $query[0] = str_ireplace($suffix, '', $query[0]); |
|
| 62 | + $query[0] = rtrim($query[0], '/'); |
|
| 63 | + $query[0] .= $suffix; |
|
| 64 | + $path = implode('?', $query); |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $path .= $suffix; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + return $url . $path; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Return the current site URL |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public static function current(){ |
|
| 78 | - $current = '/'; |
|
| 79 | - $requestUri = get_instance()->request->requestUri(); |
|
| 80 | - if($requestUri){ |
|
| 81 | - $current = $requestUri; |
|
| 82 | - } |
|
| 83 | - return static::domain() . $current; |
|
| 84 | - } |
|
| 73 | + /** |
|
| 74 | + * Return the current site URL |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public static function current(){ |
|
| 78 | + $current = '/'; |
|
| 79 | + $requestUri = get_instance()->request->requestUri(); |
|
| 80 | + if($requestUri){ |
|
| 81 | + $current = $requestUri; |
|
| 82 | + } |
|
| 83 | + return static::domain() . $current; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Generate a friendly text to use in link (slugs) |
|
| 88 | - * @param string $str the title or text to use to get the friendly text |
|
| 89 | - * @param string $separator the caracters separator |
|
| 90 | - * @param boolean $lowercase whether to set the final text to lowe case or not |
|
| 91 | - * @return string the friendly generated text |
|
| 92 | - */ |
|
| 93 | - public static function title($str = null, $separator = '-', $lowercase = true){ |
|
| 94 | - $str = trim($str); |
|
| 95 | - $from = array('ç','À','Á','Â','Ã','Ä','Å','à','á','â','ã','ä','å','Ò','Ó','Ô','Õ','Ö','Ø','ò','ó','ô','õ','ö','ø','È','É','Ê','Ë','è','é','ê','ë','Ç','ç','Ì','Í','Î','Ï','ì','í','î','ï','Ù','Ú','Û','Ü','ù','ú','û','ü','ÿ','Ñ','ñ'); |
|
| 96 | - $to = array('c','a','a','a','a','a','a','a','a','a','a','a','a','o','o','o','o','o','o','o','o','o','o','o','o','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','u','u','u','u','u','u','u','u','y','n','n'); |
|
| 97 | - $str = str_replace($from, $to, $str); |
|
| 98 | - $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str); |
|
| 99 | - $str = str_replace('--', $separator, $str); |
|
| 100 | - //if after process we get something like one-two-three-, need truncate the last separator "-" |
|
| 101 | - if(substr($str, -1) == $separator){ |
|
| 102 | - $str = substr($str, 0, -1); |
|
| 103 | - } |
|
| 104 | - if($lowercase){ |
|
| 105 | - $str = strtolower($str); |
|
| 106 | - } |
|
| 107 | - return $str; |
|
| 108 | - } |
|
| 86 | + /** |
|
| 87 | + * Generate a friendly text to use in link (slugs) |
|
| 88 | + * @param string $str the title or text to use to get the friendly text |
|
| 89 | + * @param string $separator the caracters separator |
|
| 90 | + * @param boolean $lowercase whether to set the final text to lowe case or not |
|
| 91 | + * @return string the friendly generated text |
|
| 92 | + */ |
|
| 93 | + public static function title($str = null, $separator = '-', $lowercase = true){ |
|
| 94 | + $str = trim($str); |
|
| 95 | + $from = array('ç','À','Á','Â','Ã','Ä','Å','à','á','â','ã','ä','å','Ò','Ó','Ô','Õ','Ö','Ø','ò','ó','ô','õ','ö','ø','È','É','Ê','Ë','è','é','ê','ë','Ç','ç','Ì','Í','Î','Ï','ì','í','î','ï','Ù','Ú','Û','Ü','ù','ú','û','ü','ÿ','Ñ','ñ'); |
|
| 96 | + $to = array('c','a','a','a','a','a','a','a','a','a','a','a','a','o','o','o','o','o','o','o','o','o','o','o','o','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','u','u','u','u','u','u','u','u','y','n','n'); |
|
| 97 | + $str = str_replace($from, $to, $str); |
|
| 98 | + $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str); |
|
| 99 | + $str = str_replace('--', $separator, $str); |
|
| 100 | + //if after process we get something like one-two-three-, need truncate the last separator "-" |
|
| 101 | + if(substr($str, -1) == $separator){ |
|
| 102 | + $str = substr($str, 0, -1); |
|
| 103 | + } |
|
| 104 | + if($lowercase){ |
|
| 105 | + $str = strtolower($str); |
|
| 106 | + } |
|
| 107 | + return $str; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get the current application domain with protocol |
|
| 112 | - * @return string the domain name |
|
| 113 | - */ |
|
| 114 | - public static function domain(){ |
|
| 115 | - $obj = & get_instance(); |
|
| 116 | - $domain = 'localhost'; |
|
| 117 | - $port = $obj->request->server('SERVER_PORT'); |
|
| 118 | - $protocol = is_https() ? 'https' : 'http'; |
|
| 110 | + /** |
|
| 111 | + * Get the current application domain with protocol |
|
| 112 | + * @return string the domain name |
|
| 113 | + */ |
|
| 114 | + public static function domain(){ |
|
| 115 | + $obj = & get_instance(); |
|
| 116 | + $domain = 'localhost'; |
|
| 117 | + $port = $obj->request->server('SERVER_PORT'); |
|
| 118 | + $protocol = is_https() ? 'https' : 'http'; |
|
| 119 | 119 | |
| 120 | - if($obj->request->server('HTTP_HOST')){ |
|
| 121 | - $domain = $obj->request->server('HTTP_HOST'); |
|
| 122 | - } |
|
| 123 | - else if($obj->request->server('SERVER_NAME')){ |
|
| 124 | - $domain = $obj->request->server('SERVER_NAME'); |
|
| 125 | - } |
|
| 126 | - else if($obj->request->server('SERVER_ADDR')){ |
|
| 127 | - $domain = $obj->request->server('SERVER_ADDR'); |
|
| 128 | - } |
|
| 129 | - if($port && (is_https() && $port != 443 || !is_https() && $port != 80)){ |
|
| 130 | - //some server use SSL but the port doesn't equal 443 sometime is 80 if is the case put the port at this end |
|
| 131 | - //of the domain like https://my.domain.com:787 |
|
| 132 | - if(is_https() && $port != 80){ |
|
| 133 | - $domain .= ':'.$port; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - return $protocol.'://'.$domain; |
|
| 137 | - } |
|
| 120 | + if($obj->request->server('HTTP_HOST')){ |
|
| 121 | + $domain = $obj->request->server('HTTP_HOST'); |
|
| 122 | + } |
|
| 123 | + else if($obj->request->server('SERVER_NAME')){ |
|
| 124 | + $domain = $obj->request->server('SERVER_NAME'); |
|
| 125 | + } |
|
| 126 | + else if($obj->request->server('SERVER_ADDR')){ |
|
| 127 | + $domain = $obj->request->server('SERVER_ADDR'); |
|
| 128 | + } |
|
| 129 | + if($port && (is_https() && $port != 443 || !is_https() && $port != 80)){ |
|
| 130 | + //some server use SSL but the port doesn't equal 443 sometime is 80 if is the case put the port at this end |
|
| 131 | + //of the domain like https://my.domain.com:787 |
|
| 132 | + if(is_https() && $port != 80){ |
|
| 133 | + $domain .= ':'.$port; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + return $protocol.'://'.$domain; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Get the current request query string |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - public static function queryString(){ |
|
| 144 | - return get_instance()->request->server('QUERY_STRING'); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | 139 | \ No newline at end of file |
| 140 | + /** |
|
| 141 | + * Get the current request query string |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public static function queryString(){ |
|
| 145 | + return get_instance()->request->server('QUERY_STRING'); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | 148 | \ No newline at end of file |
@@ -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 | + } |
|