*/ class LSlog_handler { // The handler configuration protected $config; // Default log formats protected $default_format = '%{requesturi} - %{remoteaddr} - %{ldapservername} - %{authuser} - %{logger} - %{level} - %{message}'; protected $default_cli_format = '%{clibinpath} - %{logger} - %{level} - %{message}'; // Default datetime prefix (enabled/disabled) protected $default_datetime_prefix = true; // Default datetime format protected $default_datetime_format = 'Y/m/d H:i:s'; /** * Constructor * * @param[in] $config array The handler configuration * * @retval void **/ public function __construct($config) { $this -> config = $config; $this -> loggers = $this -> getConfig('loggers', array()); if (!is_array($this -> loggers)) $this -> loggers = array($this -> loggers); } /** * Check system compatibility with this handler * * Note : LSlog do not generate no error about imcompatibly, it's * just omit this handler if system is incompatible. You have to * trigger it with this method if you want. * * @retval bool True if system is compatible, False otherwise **/ public function checkCompatibility() { return True; } /** * Get a configuration variable value * * @param[in] $var string The configuration variable name * @param[in] $default mixed The default value to return if configuration variable * is not set (Default : null) * @param[in] $cast string The type of expected value. The configuration variable * value will be cast as this type. Could be : bool, int, * float or string. (Optional, default : raw value) * * @retval mixed The configuration variable value **/ public function getConfig($var, $default=null, $cast=null) { return LSconfig :: get($var, $default, $cast, $this -> config); } /** * Check level against configured level * * @param[in] $level string The level * * @retval bool True if a message with this level have to be logged, False otherwise **/ public function checkLevel($level) { return LSlog :: checkLevel($level, $this -> getConfig('level')); } /** * Check logger against configured loggers filters * * @param[in] $logger string The logger * * @retval bool True if message of this logger have to be logged, False otherwise **/ public function checkLogger($logger) { return (!$this -> loggers || in_array($logger, $this -> loggers)); } /** * Log a message * * @param[in] $level string The message level * @param[in] $message string The message * @param[in] $logger string|null The logger name (optional, default: null) * * @retval void **/ public function logging($level, $message, $logger=null) { return; } /** * Format a message * * @param[in] $level string The message level * @param[in] $message string The message * @param[in] $logger string|null The logger name (optional, default: null) * * @retval string The formated message to log **/ protected function format($level, $message, $logger=null) { global $argv; if (php_sapi_name() == "cli") $format = $this -> getConfig('cli_format', $this -> default_cli_format, 'string'); else $format = $this -> getConfig('format', $this -> default_format, 'string'); // Add datetime prefix (if enabled) if ($this -> getConfig('datetime_prefix', $this -> default_datetime_prefix, 'boolean')) { $format = date($this -> getConfig('datetime_format', $this -> default_datetime_format, 'string'))." - $format"; } return getFData( $format, array( 'level' => $level, 'message' => $message, 'logger' => ($logger?$logger:'default'), 'datetime' => date('Y/m/d H:i:s'), 'clibinpath' => (isset($argv)?$argv[0]:'unknown bin path'), 'requesturi' => (isset($_SERVER)?$_SERVER['REQUEST_URI']:'unknown request URI'), 'remoteaddr' => (isset($_SERVER)?$_SERVER['REMOTE_ADDR']:'unknown remote address'), 'ldapservername' => self :: getLdapServerName(), 'authuser' => self :: getAuthenticatedUserDN(), ) ); } /** * Helper to retreive current LDAP server name * * @retval string Current LDAP server name **/ private static function getLdapServerName() { if (LSsession :: $ldapServer) { if (isset(LSsession :: $ldapServer['name'])) return LSsession :: $ldapServer['name']; else return "#".LSsession :: $ldapServerId; } return "Not connected"; } /** * Helper to retreive current authenticated user DN * * @retval string Current authenticated user DN **/ private static function getAuthenticatedUserDN() { $auth_dn = LSsession :: getLSuserObjectDn(); if ($auth_dn) return LSsession :: getLSuserObjectDn(); return "Anonymous"; } }