@@ -53,14 +53,14 @@ discard block |
||
| 53 | 53 | //put the first letter of class to upper case |
| 54 | 54 | $class = ucfirst($class); |
| 55 | 55 | static $classes = array(); |
| 56 | - if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 56 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log') { |
|
| 57 | 57 | return $classes[$class]; |
| 58 | 58 | } |
| 59 | 59 | $found = false; |
| 60 | 60 | foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
| 61 | 61 | $file = $path . $dir . '/' . $class . '.php'; |
| 62 | - if (file_exists($file)){ |
|
| 63 | - if (class_exists($class, false) === false){ |
|
| 62 | + if (file_exists($file)) { |
|
| 63 | + if (class_exists($class, false) === false) { |
|
| 64 | 64 | require_once $file; |
| 65 | 65 | } |
| 66 | 66 | //already found |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | break; |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | - if (! $found){ |
|
| 71 | + if (!$found) { |
|
| 72 | 72 | //can't use show_error() at this time because some dependencies not yet loaded |
| 73 | 73 | set_http_status_header(503); |
| 74 | 74 | echo 'Cannot find the class [' . $class . ']'; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | /* |
| 79 | 79 | TODO use the best method to get the Log instance |
| 80 | 80 | */ |
| 81 | - if ($class == 'Log'){ |
|
| 81 | + if ($class == 'Log') { |
|
| 82 | 82 | //can't use the instruction like "return new Log()" |
| 83 | 83 | //because we need return the reference instance of the loaded class. |
| 84 | 84 | $log = new Log(); |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | function & class_loaded($class = null){ |
| 104 | 104 | static $list = array(); |
| 105 | - if ($class !== null){ |
|
| 105 | + if ($class !== null) { |
|
| 106 | 106 | $list[strtolower($class)] = $class; |
| 107 | 107 | } |
| 108 | 108 | return $list; |
@@ -117,14 +117,14 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | function & load_configurations(array $overwrite_values = array()){ |
| 119 | 119 | static $config; |
| 120 | - if (empty($config)){ |
|
| 120 | + if (empty($config)) { |
|
| 121 | 121 | $file = CONFIG_PATH . 'config.php'; |
| 122 | 122 | $found = false; |
| 123 | - if (file_exists($file)){ |
|
| 123 | + if (file_exists($file)) { |
|
| 124 | 124 | require_once $file; |
| 125 | 125 | $found = true; |
| 126 | 126 | } |
| 127 | - if (! $found){ |
|
| 127 | + if (!$found) { |
|
| 128 | 128 | set_http_status_header(503); |
| 129 | 129 | echo 'Unable to find the configuration file [' . $file . ']'; |
| 130 | 130 | die(); |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @return mixed the config value |
| 146 | 146 | */ |
| 147 | - function get_config($key, $default = null){ |
|
| 147 | + function get_config($key, $default = null) { |
|
| 148 | 148 | static $cfg; |
| 149 | - if (empty($cfg)){ |
|
| 149 | + if (empty($cfg)) { |
|
| 150 | 150 | $cfg[0] = & load_configurations(); |
| 151 | 151 | } |
| 152 | 152 | return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
@@ -160,9 +160,9 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @codeCoverageIgnore |
| 162 | 162 | */ |
| 163 | - function save_to_log($level, $message, $logger = null){ |
|
| 164 | - $log =& class_loader('Log', 'classes'); |
|
| 165 | - if ($logger){ |
|
| 163 | + function save_to_log($level, $message, $logger = null) { |
|
| 164 | + $log = & class_loader('Log', 'classes'); |
|
| 165 | + if ($logger) { |
|
| 166 | 166 | $log->setLogger($logger); |
| 167 | 167 | } |
| 168 | 168 | $log->writeLog($message, $level); |
@@ -175,11 +175,11 @@ discard block |
||
| 175 | 175 | * |
| 176 | 176 | * @codeCoverageIgnore |
| 177 | 177 | */ |
| 178 | - function set_http_status_header($code = 200, $text = null){ |
|
| 179 | - if (!is_int($code) || $code < 0 ){ |
|
| 178 | + function set_http_status_header($code = 200, $text = null) { |
|
| 179 | + if (!is_int($code) || $code < 0) { |
|
| 180 | 180 | show_error('HTTP status code must be an integer'); |
| 181 | 181 | } |
| 182 | - if (empty($text)){ |
|
| 182 | + if (empty($text)) { |
|
| 183 | 183 | $code = abs($code); |
| 184 | 184 | $http_status = array( |
| 185 | 185 | 100 => 'Continue', |
@@ -228,18 +228,18 @@ discard block |
||
| 228 | 228 | 504 => 'Gateway Timeout', |
| 229 | 229 | 505 => 'HTTP Version Not Supported', |
| 230 | 230 | ); |
| 231 | - if (isset($http_status[$code])){ |
|
| 231 | + if (isset($http_status[$code])) { |
|
| 232 | 232 | $text = $http_status[$code]; |
| 233 | 233 | } |
| 234 | - else{ |
|
| 234 | + else { |
|
| 235 | 235 | show_error('No HTTP status text found for your code please check it.'); |
| 236 | 236 | } |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 239 | + if (strpos(php_sapi_name(), 'cgi') === 0) { |
|
| 240 | 240 | header('Status: ' . $code . ' ' . $text, TRUE); |
| 241 | 241 | } |
| 242 | - else{ |
|
| 242 | + else { |
|
| 243 | 243 | $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
| 244 | 244 | header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
| 245 | 245 | } |
@@ -254,13 +254,13 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @codeCoverageIgnore |
| 256 | 256 | */ |
| 257 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 257 | + function show_error($msg, $title = 'error', $logging = true) { |
|
| 258 | 258 | $title = strtoupper($title); |
| 259 | 259 | $data = array(); |
| 260 | 260 | $data['error'] = $msg; |
| 261 | 261 | $data['title'] = $title; |
| 262 | - if ($logging){ |
|
| 263 | - save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 262 | + if ($logging) { |
|
| 263 | + save_to_log('error', '[' . $title . '] ' . strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 264 | 264 | } |
| 265 | 265 | $response = & class_loader('Response', 'classes'); |
| 266 | 266 | $response->sendError($data); |
@@ -274,18 +274,18 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @return boolean true if the web server uses the https protocol, false if not. |
| 276 | 276 | */ |
| 277 | - function is_https(){ |
|
| 277 | + function is_https() { |
|
| 278 | 278 | /* |
| 279 | 279 | * some servers pass the "HTTPS" parameter in the server variable, |
| 280 | 280 | * if is the case, check if the value is "on", "true", "1". |
| 281 | 281 | */ |
| 282 | - if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 282 | + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { |
|
| 283 | 283 | return true; |
| 284 | 284 | } |
| 285 | - else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 285 | + else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 286 | 286 | return true; |
| 287 | 287 | } |
| 288 | - else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 288 | + else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { |
|
| 289 | 289 | return true; |
| 290 | 290 | } |
| 291 | 291 | return false; |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | * |
| 301 | 301 | * @return boolean true if is a valid URL address or false. |
| 302 | 302 | */ |
| 303 | - function is_url($url){ |
|
| 303 | + function is_url($url) { |
|
| 304 | 304 | return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
| 305 | 305 | } |
| 306 | 306 | |
@@ -310,8 +310,8 @@ discard block |
||
| 310 | 310 | * @param string $controllerClass the controller class name to be loaded |
| 311 | 311 | * @codeCoverageIgnore |
| 312 | 312 | */ |
| 313 | - function autoload_controller($controllerClass){ |
|
| 314 | - if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 313 | + function autoload_controller($controllerClass) { |
|
| 314 | + if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')) { |
|
| 315 | 315 | require_once $path; |
| 316 | 316 | } |
| 317 | 317 | } |
@@ -325,11 +325,11 @@ discard block |
||
| 325 | 325 | * |
| 326 | 326 | * @return boolean |
| 327 | 327 | */ |
| 328 | - function php_exception_handler($ex){ |
|
| 329 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 330 | - show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 328 | + function php_exception_handler($ex) { |
|
| 329 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
| 330 | + show_error('An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 331 | 331 | } |
| 332 | - else{ |
|
| 332 | + else { |
|
| 333 | 333 | save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
| 334 | 334 | } |
| 335 | 335 | return true; |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | * |
| 347 | 347 | * @return boolean |
| 348 | 348 | */ |
| 349 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 349 | + function php_error_handler($errno, $errstr, $errfile, $errline) { |
|
| 350 | 350 | $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
| 351 | - if ($isError){ |
|
| 351 | + if ($isError) { |
|
| 352 | 352 | set_http_status_header(500); |
| 353 | 353 | } |
| 354 | - if (! (error_reporting() & $errno)) { |
|
| 354 | + if (!(error_reporting() & $errno)) { |
|
| 355 | 355 | save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
| 356 | 356 | return; |
| 357 | 357 | } |
| 358 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 358 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
| 359 | 359 | $errorType = 'error'; |
| 360 | 360 | switch ($errno) { |
| 361 | 361 | case E_USER_ERROR: |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | $errorType = 'error'; |
| 372 | 372 | break; |
| 373 | 373 | } |
| 374 | - show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 374 | + show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline . '</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 375 | 375 | } |
| 376 | - if ($isError){ |
|
| 376 | + if ($isError) { |
|
| 377 | 377 | die(); |
| 378 | 378 | } |
| 379 | 379 | return true; |
@@ -383,10 +383,10 @@ discard block |
||
| 383 | 383 | * This function is used to run in shutdown situation of the script |
| 384 | 384 | * @codeCoverageIgnore |
| 385 | 385 | */ |
| 386 | - function php_shudown_handler(){ |
|
| 386 | + function php_shudown_handler() { |
|
| 387 | 387 | $lastError = error_get_last(); |
| 388 | 388 | if (isset($lastError) && |
| 389 | - ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 389 | + ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { |
|
| 390 | 390 | php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
| 391 | 391 | } |
| 392 | 392 | } |
@@ -404,11 +404,11 @@ discard block |
||
| 404 | 404 | * |
| 405 | 405 | * @return string string of the HTML attribute. |
| 406 | 406 | */ |
| 407 | - function attributes_to_string(array $attributes){ |
|
| 407 | + function attributes_to_string(array $attributes) { |
|
| 408 | 408 | $str = ' '; |
| 409 | 409 | //we check that the array passed as an argument is not empty. |
| 410 | - if (! empty($attributes)){ |
|
| 411 | - foreach($attributes as $key => $value){ |
|
| 410 | + if (!empty($attributes)) { |
|
| 411 | + foreach ($attributes as $key => $value) { |
|
| 412 | 412 | $key = trim(htmlspecialchars($key)); |
| 413 | 413 | $value = trim(htmlspecialchars($value)); |
| 414 | 414 | /* |
@@ -418,10 +418,10 @@ discard block |
||
| 418 | 418 | * $attr = array('placeholder' => 'I am a "puple"') |
| 419 | 419 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 420 | 420 | */ |
| 421 | - if ($value && strpos('"', $value) !== false){ |
|
| 421 | + if ($value && strpos('"', $value) !== false) { |
|
| 422 | 422 | $value = addslashes($value); |
| 423 | 423 | } |
| 424 | - $str .= $key.' = "'.$value.'" '; |
|
| 424 | + $str .= $key . ' = "' . $value . '" '; |
|
| 425 | 425 | } |
| 426 | 426 | } |
| 427 | 427 | //remove the space after using rtrim() |
@@ -437,7 +437,7 @@ discard block |
||
| 437 | 437 | * |
| 438 | 438 | * @return string the stringfy value |
| 439 | 439 | */ |
| 440 | - function stringfy_vars($var){ |
|
| 440 | + function stringfy_vars($var) { |
|
| 441 | 441 | return print_r($var, true); |
| 442 | 442 | } |
| 443 | 443 | |
@@ -448,18 +448,18 @@ discard block |
||
| 448 | 448 | * |
| 449 | 449 | * @return mixed the sanitize value |
| 450 | 450 | */ |
| 451 | - function clean_input($str){ |
|
| 452 | - if (is_array($str)){ |
|
| 451 | + function clean_input($str) { |
|
| 452 | + if (is_array($str)) { |
|
| 453 | 453 | $str = array_map('clean_input', $str); |
| 454 | 454 | } |
| 455 | - else if (is_object($str)){ |
|
| 455 | + else if (is_object($str)) { |
|
| 456 | 456 | $obj = $str; |
| 457 | 457 | foreach ($str as $var => $value) { |
| 458 | 458 | $obj->$var = clean_input($value); |
| 459 | 459 | } |
| 460 | 460 | $str = $obj; |
| 461 | 461 | } |
| 462 | - else{ |
|
| 462 | + else { |
|
| 463 | 463 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
| 464 | 464 | } |
| 465 | 465 | return $str; |
@@ -477,11 +477,11 @@ discard block |
||
| 477 | 477 | * |
| 478 | 478 | * @return string the string with the hidden part. |
| 479 | 479 | */ |
| 480 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 480 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*') { |
|
| 481 | 481 | //get the string length |
| 482 | 482 | $len = strlen($str); |
| 483 | 483 | //if str is empty |
| 484 | - if ($len <= 0){ |
|
| 484 | + if ($len <= 0) { |
|
| 485 | 485 | return str_repeat($hiddenChar, 6); |
| 486 | 486 | } |
| 487 | 487 | //if the length is less than startCount and endCount |
@@ -489,14 +489,14 @@ discard block |
||
| 489 | 489 | //or startCount is negative or endCount is negative |
| 490 | 490 | //return the full string hidden |
| 491 | 491 | |
| 492 | - if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 492 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)) { |
|
| 493 | 493 | return str_repeat($hiddenChar, $len); |
| 494 | 494 | } |
| 495 | 495 | //the start non hidden string |
| 496 | 496 | $startNonHiddenStr = substr($str, 0, $startCount); |
| 497 | 497 | //the end non hidden string |
| 498 | 498 | $endNonHiddenStr = null; |
| 499 | - if ($endCount > 0){ |
|
| 499 | + if ($endCount > 0) { |
|
| 500 | 500 | $endNonHiddenStr = substr($str, - $endCount); |
| 501 | 501 | } |
| 502 | 502 | //the hidden string |
@@ -509,31 +509,31 @@ discard block |
||
| 509 | 509 | * This function is used to set the initial session config regarding the configuration |
| 510 | 510 | * @codeCoverageIgnore |
| 511 | 511 | */ |
| 512 | - function set_session_config(){ |
|
| 512 | + function set_session_config() { |
|
| 513 | 513 | //$_SESSION is not available on cli mode |
| 514 | - if (! IS_CLI){ |
|
| 515 | - $logger =& class_loader('Log', 'classes'); |
|
| 514 | + if (!IS_CLI) { |
|
| 515 | + $logger = & class_loader('Log', 'classes'); |
|
| 516 | 516 | $logger->setLogger('PHPSession'); |
| 517 | 517 | //set session params |
| 518 | 518 | $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
| 519 | 519 | $sessionName = get_config('session_name'); |
| 520 | - if ($sessionName){ |
|
| 520 | + if ($sessionName) { |
|
| 521 | 521 | session_name($sessionName); |
| 522 | 522 | } |
| 523 | 523 | $logger->info('Session handler: ' . $sessionHandler); |
| 524 | 524 | $logger->info('Session name: ' . $sessionName); |
| 525 | 525 | |
| 526 | - if ($sessionHandler == 'files'){ |
|
| 526 | + if ($sessionHandler == 'files') { |
|
| 527 | 527 | $sessionSavePath = get_config('session_save_path'); |
| 528 | - if ($sessionSavePath){ |
|
| 529 | - if (! is_dir($sessionSavePath)){ |
|
| 528 | + if ($sessionSavePath) { |
|
| 529 | + if (!is_dir($sessionSavePath)) { |
|
| 530 | 530 | mkdir($sessionSavePath, 1773); |
| 531 | 531 | } |
| 532 | 532 | session_save_path($sessionSavePath); |
| 533 | 533 | $logger->info('Session save path: ' . $sessionSavePath); |
| 534 | 534 | } |
| 535 | 535 | } |
| 536 | - else if ($sessionHandler == 'database'){ |
|
| 536 | + else if ($sessionHandler == 'database') { |
|
| 537 | 537 | //load database session handle library |
| 538 | 538 | //Model |
| 539 | 539 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
@@ -541,11 +541,11 @@ discard block |
||
| 541 | 541 | //Database Session handler Model |
| 542 | 542 | require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
| 543 | 543 | |
| 544 | - $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 544 | + $DBS = & class_loader('DBSessionHandler', 'classes'); |
|
| 545 | 545 | session_set_save_handler($DBS, true); |
| 546 | 546 | $logger->info('session save path: ' . get_config('session_save_path')); |
| 547 | 547 | } |
| 548 | - else{ |
|
| 548 | + else { |
|
| 549 | 549 | show_error('Invalid session handler configuration'); |
| 550 | 550 | } |
| 551 | 551 | $lifetime = get_config('session_cookie_lifetime', 0); |
@@ -568,9 +568,9 @@ discard block |
||
| 568 | 568 | $logger->info('Session lifetime: ' . $lifetime); |
| 569 | 569 | $logger->info('Session cookie path: ' . $path); |
| 570 | 570 | $logger->info('Session domain: ' . $domain); |
| 571 | - $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 571 | + $logger->info('Session is secure: ' . ($secure ? 'TRUE' : 'FALSE')); |
|
| 572 | 572 | |
| 573 | - if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 573 | + if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) { |
|
| 574 | 574 | $logger->info('Session not yet start, start it now'); |
| 575 | 575 | session_start(); |
| 576 | 576 | } |