public function write_log($level = 'error', $msg, $php_error = FALSE)
{
// Start the session if not already started
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Write data to the session
$_SESSION['last_log_time'] = date($this->_date_fmt);
$_SESSION['last_log_message'] = $msg;
// Check if logging is enabled
if ($this->_enabled === FALSE) {
echo "Logging is disabled.\n";
return FALSE;
}
$level = strtoupper($level);
// Validate the log level
if (!isset($this->_levels[$level]) || ($this->_levels[$level] > $this->_threshold)) {
return FALSE;
}
$filepath = $this->_log_path . 'log-' . date('Y-m-d') . '.php';
$message = '';
// Add a PHP header to the log file if it doesn't exist
if (!file_exists($filepath)) {
$message .= "
<?php if ( ! defined ( 'BASEPATH' ) ) exit ( 'No direct script access allowed' ) ; ?> \n\n";
}
// Try to open the log file
if (!$fp = @fopen($filepath, 'a')) { // Use 'a' for append mode
return FALSE;
}
// Append the log message
$message .= $level . ' ' . (($level === 'INFO') ? ' -' : '-') . ' ' . date($this->_date_fmt) . ' --> ' . $msg . "\n";
// Lock file, write the message, and unlock
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
// Set file permissions
@chmod($filepath, 0644); // Use an octal value for permissions
// Print session data to the output
echo "Session Data:\n";
echo "Last Log Time: " . $_SESSION['last_log_time'] . "\n";
echo "Last Log Message: " . $_SESSION['last_log_message'] . "\n";
return TRUE;
}
cHVibGljIGZ1bmN0aW9uIHdyaXRlX2xvZygkbGV2ZWwgPSAnZXJyb3InLCAkbXNnLCAkcGhwX2Vycm9yID0gRkFMU0UpCnsKICAgIC8vIFN0YXJ0IHRoZSBzZXNzaW9uIGlmIG5vdCBhbHJlYWR5IHN0YXJ0ZWQKICAgIGlmIChzZXNzaW9uX3N0YXR1cygpID09IFBIUF9TRVNTSU9OX05PTkUpIHsKICAgICAgICBzZXNzaW9uX3N0YXJ0KCk7CiAgICB9CgogICAgLy8gV3JpdGUgZGF0YSB0byB0aGUgc2Vzc2lvbgogICAgJF9TRVNTSU9OWydsYXN0X2xvZ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