@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Security{ |
|
| 27 | + class Security { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The logger instance |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | * Get the logger singleton instance |
| 37 | 37 | * @return Log the logger instance |
| 38 | 38 | */ |
| 39 | - private static function getLogger(){ |
|
| 40 | - if(self::$logger == null){ |
|
| 41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 39 | + private static function getLogger() { |
|
| 40 | + if (self::$logger == null) { |
|
| 41 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 42 | 42 | self::$logger[0]->setLogger('Library::Security'); |
| 43 | 43 | } |
| 44 | 44 | return self::$logger[0]; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * This method is used to generate the CSRF token |
| 50 | 50 | * @return string the generated CSRF token |
| 51 | 51 | */ |
| 52 | - public static function generateCSRF(){ |
|
| 52 | + public static function generateCSRF() { |
|
| 53 | 53 | $logger = self::getLogger(); |
| 54 | 54 | $logger->debug('Generation of CSRF ...'); |
| 55 | 55 | |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | $expire = get_config('csrf_expire', 60); |
| 58 | 58 | $keyExpire = 'csrf_expire'; |
| 59 | 59 | $currentTime = time(); |
| 60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
| 60 | + if (Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime) { |
|
| 61 | 61 | $logger->info('The CSRF token not yet expire just return it'); |
| 62 | 62 | return Session::get($key); |
| 63 | 63 | } |
| 64 | - else{ |
|
| 64 | + else { |
|
| 65 | 65 | $newTime = $currentTime + $expire; |
| 66 | 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. ']'); |
|
| 67 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . '], token [' . $token . ']'); |
|
| 68 | 68 | Session::set($keyExpire, $newTime); |
| 69 | 69 | Session::set($key, $token); |
| 70 | 70 | return Session::get($key); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | * This method is used to check the CSRF if is valid, not yet expire, etc. |
| 76 | 76 | * @return boolean true if valid, false if not valid |
| 77 | 77 | */ |
| 78 | - public static function validateCSRF(){ |
|
| 78 | + public static function validateCSRF() { |
|
| 79 | 79 | $logger = self::getLogger(); |
| 80 | 80 | $logger->debug('Validation of CSRF ...'); |
| 81 | 81 | |
@@ -83,23 +83,23 @@ discard block |
||
| 83 | 83 | $expire = get_config('csrf_expire', 60); |
| 84 | 84 | $keyExpire = 'csrf_expire'; |
| 85 | 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){ |
|
| 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 | 88 | $logger->warning('The CSRF session data is not valide'); |
| 89 | 89 | return false; |
| 90 | 90 | } |
| 91 | - else{ |
|
| 91 | + else { |
|
| 92 | 92 | //perform form data |
| 93 | 93 | //need use request->query() for best retrieve |
| 94 | 94 | //super instance |
| 95 | 95 | $obj = & get_instance(); |
| 96 | 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'); |
|
| 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 | 99 | return false; |
| 100 | 100 | } |
| 101 | - else{ |
|
| 102 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
| 101 | + else { |
|
| 102 | + $logger->info('The CSRF data [' . $token . '] is valide the form data is safe continue'); |
|
| 103 | 103 | //remove the token from session |
| 104 | 104 | Session::clear($key); |
| 105 | 105 | Session::clear($keyExpire); |
@@ -111,24 +111,24 @@ discard block |
||
| 111 | 111 | /** |
| 112 | 112 | * This method is used to check the whitelist IP address access |
| 113 | 113 | */ |
| 114 | - public static function checkWhiteListIpAccess(){ |
|
| 114 | + public static function checkWhiteListIpAccess() { |
|
| 115 | 115 | $logger = self::getLogger(); |
| 116 | 116 | $logger->debug('Validation of the IP address access ...'); |
| 117 | 117 | $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
| 118 | 118 | $isEnable = get_config('white_list_ip_enable', false); |
| 119 | - if($isEnable){ |
|
| 119 | + if ($isEnable) { |
|
| 120 | 120 | $logger->info('Whitelist IP access is enabled in the configuration'); |
| 121 | 121 | $list = get_config('white_list_ip_addresses', array()); |
| 122 | - if(! empty($list)){ |
|
| 122 | + if (!empty($list)) { |
|
| 123 | 123 | //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
| 124 | 124 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
| 125 | 125 | $ip = get_ip(); |
| 126 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
| 126 | + if ((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)) { |
|
| 127 | 127 | $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
| 128 | 128 | //wildcard to access all ip address |
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | - else{ |
|
| 131 | + else { |
|
| 132 | 132 | // go through all whitelisted ips |
| 133 | 133 | foreach ($list as $ipaddr) { |
| 134 | 134 | // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | - else{ |
|
| 157 | + else { |
|
| 158 | 158 | $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
| 159 | 159 | } |
| 160 | 160 | } |