Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 370 lines (339 sloc) 9.617 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
<?php
/**
* @package Habari
*
*/

/**
* Habari LogEntry class
*
* Represents a log entry
*
* @todo Apply system error handling
*/
class LogEntry extends QueryRecord
{

/**
* Defined event severities
*
* @final
*/
private static function severities($translate = true)
{
return array(
0 => $translate ? _t( 'any' ) : 'any',
1 => $translate ? _t( 'none' ) : 'none',
2 => $translate ? _t( 'debug' ) : 'debug',
3 => $translate ? _t( 'info' ) : 'info',
4 => $translate ? _t( 'notice' ) : 'notice',
5 => $translate ? _t( 'warning' ) : 'warning',
6 => $translate ? _t( 'err' ) : 'err',
7 => $translate ? _t( 'crit' ) : 'crit',
8 => $translate ? _t( 'alert' ) : 'alert',
9 => $translate ? _t( 'emerg' ) : 'emerg',
);
}

/**
* Cache for log_types
*/
private static $types = array();

/**
* Return the defined database columns for an Event
*
* @return array Array of columns in the LogEntry table
*/
public static function default_fields()
{
return array(
'id' => 0,
'user_id' => 0,
'type_id' => null,
'severity_id' => null,
'message' => '',
'data' => '',
'timestamp' => HabariDateTime::date_create(),
'ip' => 0,
);
}

/**
* Constructor for the LogEntry class
*
* @param array $paramarray an associative array of initial LogEntry field values
*/
public function __construct( $paramarray = array() )
{
// Defaults
$this->fields = array_merge(
self::default_fields(),
$this->fields );

parent::__construct( $paramarray );
if ( !isset( $this->fields['module'] ) ) {
$this->fields['module'] = 'habari';
}
if ( !isset( $this->fields['type'] ) ) {
$this->fields['type'] = 'default';
}
if ( !isset( $this->fields['severity'] ) ) {
$this->fields['severity'] = 'info';
}
if ( isset( $this->fields['timestamp'] ) ) {
$this->fields['timestamp'] = HabariDateTime::date_create( $this->fields['timestamp'] );
}
$this->exclude_fields( 'id' );
}

/**
* Returns an associative array of LogEntry types
*
* @param bool whether to force a refresh of the cached values
* @return array An array of log entry type names => integer values
*/
public static function list_logentry_types( $force = false )
{
if ( $force || empty( self::$types ) ) {
self::$types = array();
$res = DB::get_results( 'SELECT id, module, type FROM {log_types}' );
foreach ( $res as $x ) {
self::$types[ $x->module ][ $x->type ] = $x->id;
}
}
return self::$types;
}

/**
* Return an array of Severities
* @return array An array of severity ID => name pairs
**/
public static function list_severities()
{
$results = self::severities();
unset($results[1]);
return $results;
}

/**
* Returns an array of LogEntry modules
* @param bool Whether to refresh the cached values
* @return array An array of LogEntry module id => name pairs
**/
public static function list_modules( $refresh = false )
{
$types = self::list_logentry_types( $refresh );
foreach ( $types as $module => $types ) {
$modules[] = $module;
}
}

/**
* Returns an array of LogEntry types
* @param bool Whether to refresh the cached values
* @return array An array of LogEntry id => name pairs
**/
public static function list_types( $refresh = false )
{
$types = array();
$matrix = self::list_logentry_types( $refresh );
foreach ( $matrix as $module => $module_types ) {
$types = array_merge( $types, $module_types );
}
return array_flip( $types );
}

/**
* Get the integer value for the given severity, or <code>false</code>.
*
* @param string $severity The severity name
* @return mixed numeric value for the given severity, or <code>false</code>
*/
public static function severity( $severity )
{
if ( is_numeric( $severity ) && array_key_exists( $severity, self::severities() ) ) {
return $severity;
}
return array_search( $severity, self::severities(false) );
}

/**
* Get the string representation of the severity numeric value.
*
* @param integer $severity The severity index.
* @return string The string name of the severity, or 'Unknown'.
*/
public static function severity_name( $severity )
{
$severities = self::severities();
return isset( $severities[$severity] ) ? $severities[$severity] : _t( 'Unknown' );
}

/**
* Get the integer value for the given module/type, or <code>false</code>.
*
* @param string $module the module
* @param string $type the type
* @return mixed numeric value for the given module/type, or <code>false</code>
*/
public static function type( $module, $type )
{
self::list_logentry_types();
if ( array_key_exists( $module, self::$types ) && array_key_exists( $type, self::$types[$module] ) ) {
return self::$types[$module][$type];
}
if ( $type != null && $module != null ) {
EventLog::register_type( $type, $module );
self::list_logentry_types( true );
return self::$types[$module][$type];
}
return false;
}

/**
* Insert this LogEntry data into the database
*/
public function insert()
{
if ( isset( $this->fields['severity'] ) ) {
$this->severity_id = LogEntry::severity( $this->fields['severity'] );
unset( $this->fields['severity'] );
}
if ( isset( $this->fields['module'] ) && isset( $this->fields['type'] ) ) {
$this->type_id = LogEntry::type( $this->fields['module'], $this->fields['type'] );
unset( $this->fields['module'] );
unset( $this->fields['type'] );
}

// if we're set to only log entries greater than a sertain level, make sure we're that level or higher
if ( $this->fields['severity_id'] < Options::get( 'log_min_severity' ) ) {
return;
}

// make sure data is a string and can be stored. lots of times it's convenient to hand in an array of data values
if ( is_array( $this->fields['data'] ) || is_object( $this->fields['data'] ) ) {
$this->fields['data'] = serialize( $this->fields['data'] );
}

Plugins::filter( 'insert_logentry', $this );
parent::insertRecord( DB::table( 'log' ) );

$this->id = DB::last_insert_id();

}

/**
* Return a single requested log entry.
*
* <code>
* $log= LogEntry::get( array( 'id' => 5 ) );
* </code>
*
* @param array $paramarray An associated array of parameters, or a querystring
* @return object LogEntry The first log entry that matched the given criteria
*/
public static function get( $paramarray = array() )
{
// Default parameters.
$defaults = array (
'fetch_fn' => 'get_row',
);
$user = User::identify();
if ( $user->loggedin ) {
$defaults['where'][] = array(
'user_id' => $user->id,
);
}
foreach ( $defaults['where'] as $index => $where ) {
$defaults['where'][$index] = array_merge( $where, Utils::get_params( $paramarray ) );
}
// Make sure we fetch only a single event. (LIMIT 1)
$defaults['limit'] = 1;

return EventLog::get( $defaults );
}

/**
* Return the log entry's event type.
*
* <code>$log->type</code>
*
* @return string Human-readable event type
*/
public function get_event_type()
{
$type = DB::get_value( 'SELECT type FROM {log_types} WHERE id=' . $this->type_id );
return $type ? $type : _t( 'Unknown' );
}

/**
* Return the log entry's event module.
*
* <code>$log->module</code>
*
* @return string Human-readable event module
*/
public function get_event_module()
{
$module = DB::get_value( 'SELECT module FROM {log_types} WHERE id=' . $this->type_id );
return $module ? $module : _t( 'Unknown' );
}

/**
* Return the log entry's event severity.
*
* <code>$log->severity</code>
*
* @return string Human-readable event severity
*/
public function get_event_severity()
{
return self::severity_name( $this->severity_id );
}

/**
* function delete
* Deletes this logentry
*/
public function delete()
{
$allow = true;
$allow = Plugins::filter( 'logentry_delete_allow', $allow, $this );
if ( ! $allow ) {
return;
}
Plugins::act( 'logentry_delete_before', $this );
return parent::deleteRecord( DB::table( 'log' ), array( 'id'=>$this->id ) );
Plugins::act( 'logentry_delete_after', $this );
}

/**
* Overrides QueryRecord __get to implement custom object properties
*
* @param string Name of property to return
* @return mixed The requested field value
*/
public function __get( $name )
{
$fieldnames = array_merge( array_keys( $this->fields ), array( 'module', 'type', 'severity' ) );
$filter = false;
if ( !in_array( $name, $fieldnames ) && strpos( $name, '_' ) !== false ) {
$field_matches = implode('|', $fieldnames);
if(preg_match( '/^(' . $field_matches . ')_(.+)$/', $name, $matches )) {
list( $junk, $name, $filter )= $matches;
}
}

switch ( $name ) {
case 'module':
$out = $this->get_event_module();
break;
case 'type':
$out = $this->get_event_type();
break;
case 'severity':
$out = $this->get_event_severity();
break;
default:
$out = parent::__get( $name );
break;
}
$out = Plugins::filter( "logentry_{$name}", $out, $this );
if ( $filter ) {
$out = Plugins::filter( "logentry_{$name}_{$filter}", $out, $this );
}
return $out;
}

/**
* Overrides QueryRecord __set to implement custom object properties
*
* @param string Name of property to return
* @return mixed The requested field value
*/
public function __set( $name, $value )
{
switch ( $name ) {
case 'timestamp':
if ( !( $value instanceOf HabariDateTime ) ) {
$value = HabariDateTime::date_create( $value );
}
break;
}
return parent::__set( $name, $value );
}

}

?>
Something went wrong with that request. Please try again.