Browse Source

Fixing (moving) session string warning (Ticket #2377) and adding automatic logging code

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4846 3807eeeb-6ff5-0310-8944-8be069107fe0
nate 19 years ago
parent
commit
b0307904ae

+ 0 - 6
cake/bootstrap.php

@@ -48,12 +48,6 @@ if (!defined('PHP5')) {
 	Configure::load('class.paths');
 	Configure::write('debug', DEBUG);
 /**
- * Verify that the application's salt has been changed from the default value
- */
-	if (CAKE_SESSION_STRING == 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi') {
-		trigger_error('Please change the value of CAKE_SESSION_STRING in app/config/core.php to a salt value specific to your application', E_USER_NOTICE);
-	}
-/**
  * Get the application path and request URL
  */
 	if (empty($uri) && defined('BASE_URL')) {

+ 41 - 2
cake/libs/cake_log.php

@@ -34,12 +34,32 @@
 		 uses('file');
 	}
 /**
+ * Set up error level constants to be used within the framework if they are not defined within the
+ * system.
+ *
+ */
+	if (!defined('LOG_WARNING')) {
+		define('LOG_WARNING', 3);
+	}
+	if (!defined('LOG_ERR')) {
+		define('LOG_ERR', LOG_ERROR);
+	}
+	if (!defined('LOG_NOTICE')) {
+		define('LOG_NOTICE', 4);
+	}
+	if (!defined('LOG_DEBUG')) {
+		define('LOG_DEBUG', 5);
+	}
+	if (!defined('LOG_INFO')) {
+		define('LOG_INFO', 6);
+	}
+/**
  * Logs messages to text files
  *
  * @package		cake
  * @subpackage	cake.cake.libs
  */
-class CakeLog{
+class CakeLog {
 /**
  * Writes given message to a log file in the logs directory.
  *
@@ -48,7 +68,26 @@ class CakeLog{
  * @return boolean Success
  */
 	function write($type, $msg) {
-		$filename = LOGS . $type . '.log';
+		$levels = array(
+			LOG_WARNING => 'warning',
+			LOG_NOTICE => 'notice',
+			LOG_INFO => 'info',
+			LOG_DEBUG => 'debug',
+			LOG_ERR => 'error',
+			LOG_ERROR => 'error'
+		);
+
+		if (is_int($type) && isset($levels[$type])) {
+			$type = $levels[$type];
+		}
+
+		if ($type == 'error' || $type == 'warning') {
+			$filename = LOGS . 'error.log';
+		} elseif (in_array($type, $levels)) {
+			$filename = LOGS . 'debug.log';
+		} else {
+			$filename = LOGS . $type . '.log';
+		}
 		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $msg . "\n";
 		$log = new File($filename);
 		return $log->append($output);

+ 5 - 0
cake/libs/configure.php

@@ -146,8 +146,13 @@ class Configure extends Object {
 				if(!class_exists('Debugger')) {
 					require LIBS . 'debugger.php';
 				}
+				if (!class_exists('CakeLog')) {
+					uses('cake_log');
+				}
+				Configure::write('log', LOG_NOTICE);
 			} else {
 				error_reporting(0);
+				Configure::write('log', LOG_NOTICE);
 			}
 		}
 	}

+ 1 - 0
cake/libs/controller/components/auth.php

@@ -232,6 +232,7 @@ class AuthComponent extends Object {
 				CAKE_ADMIN . '_delete'	=> 'delete'
 			));
 		}
+		Debugger::checkSessionKey();
 	}
 /**
  * Main execution method.  Handles redirecting of invalid users, and processing

+ 29 - 2
cake/libs/debugger.php

@@ -33,6 +33,7 @@
 	if (!class_exists('Object')) {
 		 uses('object');
 	}
+	uses('cake_log');
 /**
  * Provide custom logging and error handling.
  *
@@ -118,21 +119,25 @@ class Debugger extends Object {
 			return;
 		}
 
+		$level = LOG_DEBUG;
 		switch ($code) {
 			case E_PARSE:
 			case E_ERROR:
 			case E_CORE_ERROR:
 			case E_COMPILE_ERROR:
 				$error = 'Fatal Error';
+				$level = LOG_ERROR;
 			break;
 			case E_WARNING:
 			case E_USER_WARNING:
 			case E_COMPILE_WARNING:
 			case E_RECOVERABLE_ERROR:
 				$error = 'Warning';
+				$level = LOG_WARNING;
 			break;
 			case E_NOTICE:
 				$error = 'Notice';
+				$level = LOG_NOTICE;
 			break;
 			default:
 				return false;
@@ -178,6 +183,10 @@ class Debugger extends Object {
 
 		pr($this->trace(array('start' => 1)));
 		e('</div>');
+
+		if (Configure::read('log')) {
+			CakeLog::write($level, "{$error} ({$code}): {$description} in [{$file}, line {$line}]");
+		}
 		
 		if ($error == 'Fatal Error') {
 			die();
@@ -344,11 +353,29 @@ class Debugger extends Object {
 			break;
 		}
 	}
+/**
+ * Verify that the application's salt has been changed from the default value
+ *
+ */
+	function checkSessionKey() {
+		if (CAKE_SESSION_STRING == 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi') {
+			trigger_error('Please change the value of CAKE_SESSION_STRING in app/config/core.php to a salt value specific to your application', E_USER_NOTICE);
+		}
+	}
+/**
+ * Invokes the given debugger object as the current error handler, taking over control from the previous handler
+ * in a stack-like hierarchy.
+ *
+ * @param object $debugger A reference to the Debugger object
+ * @return void
+ */
+	function invoke(&$debugger) {
+		set_error_handler(array(&$debugger, 'handleError'));
+	}
 }
 
 if (!defined('DISABLE_DEFAULT_ERROR_HANDLING')) {
-	$debugger =& Debugger::getInstance();
-	set_error_handler(array(&$debugger, 'handleError'));
+	Debugger::invoke(Debugger::getInstance());
 }
 
 ?>

+ 5 - 9
cake/libs/object.php

@@ -36,7 +36,7 @@
  * @package		cake
  * @subpackage	cake.cake.libs
  */
-class Object{
+class Object {
 /**
  * Log object
  *
@@ -123,18 +123,14 @@ class Object{
 		if (!is_string($msg)) {
 			ob_start();
 			print_r ($msg);
-			$msg=ob_get_contents();
+			$msg = ob_get_contents();
 			ob_end_clean();
 		}
 
-		switch($type) {
-			case LOG_DEBUG:
-				return $this->_log->write('debug', $msg);
-			break;
-			default:
-				return $this->_log->write('error', $msg);
-			break;
+		if (!isset($this->_log->levels[$type])) {
+			$type = LOG_ERROR;
 		}
+		return $this->_log->write($type, $msg);
 	}
 /**
  * Allows setting of multiple properties of the object in a single line of code.

+ 1 - 0
cake/libs/view/templates/pages/home.ctp

@@ -24,6 +24,7 @@
  * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
  */
 ?>
+<?php Debugger::checkSessionKey(); ?>
 <p>
 	<span class="notice">
 		<?php