2010-05-16 11:14:19 +00:00
< ? php
2014-01-10 07:36:54 -08:00
/**
2015-02-14 23:34:15 -08:00
* e107 website system
*
* Copyright ( C ) 2008 - 2010 e107 Inc ( e107 . org )
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
2014-01-10 07:36:54 -08:00
* Admin Log Handler
*
* USAGE :
*
* @ example Log and Add to Message Handler : e107 :: getAdminLog () -> addSuccess ( " Successfully executed " ) -> save ( 'PREF_01' );
* @ example Log Only : e107 :: getAdminLog () -> addSuccess ( " Successfully executed " , false ) -> save ( 'PREF_01' );
* @ example Log Array Diff : e107 :: getAdminLog () -> addArray ( $array1 , $array2 ) -> save ( 'PREF_01' );
2015-02-14 23:34:15 -08:00
* @ example Log Array Diff and Add to Message Handler : e107 :: getAdminLog () -> addArray ( $array1 , $array2 , E_MESSAGE_ERROR ) -> save ( 'PREF_01' );
*
*/
2010-04-20 21:35:20 +00:00
if ( ! defined ( 'e107_INIT' ))
{
exit ;
}
2015-02-14 23:34:15 -08:00
define ( 'LOG_MESSAGE_NODISPLAY' , 'nodisplay' );
2010-04-20 21:35:20 +00:00
/**
* Admin logging class .
*
* @ package e107
* @ subpackage e107_handlers
2015-02-14 23:34:15 -08:00
* @ version $Id $ ;
* @ author e107steved
2010-04-20 21:35:20 +00:00
*/
class e_admin_log
{
/**
* Contains default class options , plus any that are overidden by the constructor
*
* @ var array
*/
protected $_options = array ( 'log_level' => 2 , 'backtrace' => false , );
protected $rldb = NULL ; // Database used by logging routine
2013-05-12 20:56:35 -07:00
protected $logFile = null ;
2015-02-14 23:34:15 -08:00
/**
* Log messages
* @ var array
*/
2013-05-12 20:56:35 -07:00
protected $_messages ;
protected $_allMessages ; // similar to $_messages except it is never flushed.
2015-02-14 23:34:15 -08:00
2010-04-20 21:35:20 +00:00
/**
* Constructor . Sets up constants and overwrites default options where set .
*
* @ param array $options
* @ return none
*/
public function __construct ( $options = array ())
{
foreach ( $options as $key => $val )
{
$this -> _options [ $key ] = $val ;
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
define ( " E_LOG_INFORMATIVE " , 0 ); // Minimal Log Level, including really minor stuff
define ( " E_LOG_NOTICE " , 1 ); // More important than informative, but less important than notice
define ( " E_LOG_WARNING " , 2 ); // Not anything serious, but important information
define ( " E_LOG_FATAL " , 3 ); // An event so bad your site ceased execution.
define ( " E_LOG_PLUGIN " , 4 ); // Plugin information
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// Logging actions
define ( " LOG_TO_ADMIN " , 1 );
define ( " LOG_TO_AUDIT " , 2 );
define ( " LOG_TO_ROLLING " , 4 );
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// User audit logging (intentionally start at 10 - stick to 2 digits)
// The last two digits must match that for the corresponding log message
define ( 'USER_AUDIT_ADMIN' , 10 ); // User data changed by admin
define ( 'USER_AUDIT_SIGNUP' , 11 ); // User signed up
define ( 'USER_AUDIT_EMAILACK' , 12 ); // User responded to registration email
define ( 'USER_AUDIT_LOGIN' , 13 ); // User logged in
define ( 'USER_AUDIT_LOGOUT' , 14 ); // User logged out
define ( 'USER_AUDIT_NEW_DN' , 15 ); // User changed display name
define ( 'USER_AUDIT_NEW_PW' , 16 ); // User changed password
define ( 'USER_AUDIT_NEW_EML' , 17 ); // User changed email
define ( 'USER_AUDIT_PW_RES' , 18 ); // Password reset/resent activation email
define ( 'USER_AUDIT_NEW_SET' , 19 ); // User changed other settings
define ( 'USER_AUDIT_ADD_ADMIN' , 20 ); // User added by admin
define ( 'USER_AUDIT_MAIL_BOUNCE' , 21 ); // User mail bounce
define ( 'USER_AUDIT_BANNED' , 22 ); // User banned
define ( 'USER_AUDIT_BOUNCE_RESET' , 23 ); // User bounce reset
define ( 'USER_AUDIT_TEMP_ACCOUNT' , 24 ); // User temporary account
2015-02-14 23:34:15 -08:00
// Init E_MESSAGE_* constants if not already done
2010-10-26 07:41:20 +00:00
// e107::getMessage(); - just include, message handler is creating session in construct
// it breaks stuff (see class2 - language detection and comments)
2015-02-14 23:34:15 -08:00
require_once ( e_HANDLER . 'message_handler.php' );
2013-05-12 20:56:35 -07:00
$this -> _messages = array ();
$this -> _allMessages = array ();
2015-02-14 23:34:15 -08:00
2010-04-20 21:35:20 +00:00
}
2013-04-17 13:54:09 -07:00
/**
2013-05-12 20:56:35 -07:00
* @ DEPRECATED
2013-04-17 13:54:09 -07:00
* BC Alias of add ();
*/
public function log_event ( $event_title , $event_detail , $event_type = E_LOG_INFORMATIVE , $event_code = '' )
{
return $this -> add ( $event_title , $event_detail , $event_type , $event_code );
}
2010-04-20 21:35:20 +00:00
2014-01-10 07:36:54 -08:00
/**
* Save all logs in the queue to the database and render any unhidden messages with the message handler .
* @ see alias flushMessages () method below .
* @ param string $logTitle - title for log entry eg . 'PREF_01'
* @ param int $logImportance [ optional ] default E_LOG_INFORMATIVE - passed directly to admin log
* @ param string $logEventCode [ optional ] - passed directly to admin log
* @ param string $mstack [ optional ] message stack passed to message handler
*/
public function save ( $logTitle , $logImportance = E_LOG_INFORMATIVE , $logEventCode = '' , $mstack = false )
{
return $this -> flushMessages ( $logTitle , $logImportance , $logEventCode , $mstack );
}
2010-04-20 21:35:20 +00:00
/**
2015-02-07 11:57:28 -08:00
* Add and Save an event into the admin , rolling or user log .
* @ param string $event_title
* @ param mixed $event_details
* @ param integer $event_type [ optional ] Log level eg . E_LOG_INFORMATIVE , E_LOG_NOTICE , E_LOG_WARNING , E_LOG_FATAL
* @ param string $event_code [ optional ] - eg . 'BOUNCE'
* @ param integer $target [ optional ] LOG_TO_ADMIN , LOG_TO_AUDIT , LOG_TO_ROLLING
* @ return e_admin_log
2014-01-10 09:32:14 -08:00
*
2010-04-20 21:35:20 +00:00
* Alternative admin log entry point - compatible with legacy calls , and a bit simpler to use than the generic entry point .
* ( $eventcode has been added - give it a reference to identify the source module , such as 'NEWS_12' or 'ECAL_03' )
* We also log everything ( unlike 0.7 , where admin log and debug stuff were all mixed up together )
*
* For multi - lingual logging ( where the event title is shown in the language of the current user ), LAN defines may be used in the title
*
* For generic calls , leave $event_code as empty , and specify a constant string STRING_nn of less than 10 characters for the event title
* Typically the 'STRING' part of the name defines the area originating the log event , and the 'nn' is a numeric code
* This is stored as 'LAN_AL_STRING_NN' , and must be defined in a language file which is loaded during log display .
*
2015-02-07 11:57:28 -08:00
2010-04-20 21:35:20 +00:00
*/
2014-10-22 13:26:03 -07:00
public function add ( $event_title , $event_detail , $event_type = E_LOG_INFORMATIVE , $event_code = '' , $target = LOG_TO_ADMIN )
2010-04-20 21:35:20 +00:00
{
if ( $event_code == '' )
{
2014-01-10 09:32:14 -08:00
if ( strlen ( $event_title ) <= 12 )
2010-04-20 21:35:20 +00:00
{ // Assume the title is actually a reference to the event
$event_code = $event_title ;
$event_title = 'LAN_AL_' . $event_title ;
}
else
{
$event_code = 'ADMIN' ;
}
}
//SecretR - now supports DB array as event_detail (see e.g. db::db_Insert())
if ( is_array ( $event_detail ))
{
2014-01-10 07:36:54 -08:00
// handled inside e_log_event();
2015-02-14 23:34:15 -08:00
/*
2010-04-20 21:35:20 +00:00
$tmp = array ();
2014-01-10 07:36:54 -08:00
if ( isset ( $event_detail [ 'data' ]))
{
$event_detail = $event_detail [ 'data' ];
}
foreach ( $event_detail as $k => $v )
{
$tmp [] = $k . '=>' . $v ;
}
$event_detail = implode ( " [!br!] \n " , $tmp );
unset ( $tmp );
2015-02-14 23:34:15 -08:00
*/
2014-01-10 07:36:54 -08:00
2010-04-20 21:35:20 +00:00
}
2010-10-26 07:41:20 +00:00
else
{
// auto-format long details - TODO - shrink details on administration log page, expand/show in DHTML window full details.
$event_detail = str_replace ( " \n " , " [!br!] " , $event_detail );
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( $this -> _options [ 'backtrace' ] == true )
{
2014-01-10 09:32:14 -08:00
$event_detail .= " \n \n " . debug_backtrace ( false );
2010-04-20 21:35:20 +00:00
}
2014-01-10 07:36:54 -08:00
2014-10-22 13:26:03 -07:00
$this -> e_log_event ( $event_type , - 1 , $event_code , $event_title , $event_detail , FALSE , $target );
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
return $this ;
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
/**
Generic log entry point
-----------------------
Example call : ( Deliberately pick separators that shouldn ' t be in file names )
e_log_event ( E_LOG_NOTICE , __FILE__ . " | " . __FUNCTION__ . " @ " . __LINE__ , " ECODE " , " Event Title " , " explanatory message " , FALSE , LOG_TO_ADMIN );
or :
e_log_event ( E_LOG_NOTICE , debug_backtrace (), " ECODE " , " Event Title " , " explanatory message " , TRUE , LOG_TO_ROLLING );
2010-05-16 11:14:19 +00:00
*
2010-04-20 21:35:20 +00:00
* @ param int $importance - importance of event - 0. . 4 or so
* @ param mixed $source_call - either : string identifying calling file / routine
* or : a number 0. . 9 identifying info to log from debug_backtrace ()
* or : empty string , in which case first entry from debug_backtrace () logged
* or : an array , assumed to be from passing debug_backtrace () as a parameter , in which case relevant
* information is extracted and the argument list from the first entry logged
* or : - 1 , in which case no information logged
* @ param string $eventcode - abbreviation listing event type
* @ param string $event_title - title of event - pass standard 'LAN_ERROR_nn' defines to allow language translation
* @ param string $explain - detail of event
* @ param bool $finished - if TRUE , aborts execution
* @ param int $target_logs - flags indicating which logs to update - if entry to be posted in several logs , add ( or 'OR' ) their defines :
* LOG_TO_ADMIN - admin log
* LOG_TO_AUDIT - audit log
* LOG_TO_ROLLING - rolling log
*
* @ return none
2010-05-16 11:14:19 +00:00
* @ todo - check microtime () call
2010-04-20 21:35:20 +00:00
*/
public function e_log_event ( $importance , $source_call , $eventcode = " GEN " , $event_title = " Untitled " , $explain = " " , $finished = FALSE , $target_logs = LOG_TO_AUDIT )
{
2010-05-16 11:14:19 +00:00
$e107 = e107 :: getInstance ();
$pref = e107 :: getPref ();
$tp = e107 :: getParser ();
2010-04-20 21:35:20 +00:00
list ( $time_usec , $time_sec ) = explode ( " " , microtime ( FALSE )); // Log event time immediately to minimise uncertainty
$time_usec = $time_usec * 1000000 ;
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( $this -> rldb == NULL )
2010-05-16 11:14:19 +00:00
$this -> rldb = e107 :: getDb ( 'adminlog' ); // Better use our own db - don't know what else is going on
2010-04-20 21:35:20 +00:00
if ( is_bool ( $target_logs ))
{ // Handle the legacy stuff for now - some old code used a boolean to select admin or rolling logs
$target_logs = $target_logs ? LOG_TO_ADMIN : LOG_TO_ROLLING ;
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
//---------------------------------------
// Calculations common to all logs
//---------------------------------------
2013-05-31 18:36:43 -07:00
$userid = deftrue ( 'USER' ) ? USERID : 0 ;
$userstring = deftrue ( 'USER' ) ? USERNAME : 'LAN_ANONYMOUS' ;
$userIP = e107 :: getIPHandler () -> getIP ( FALSE );
2010-05-16 11:14:19 +00:00
2013-05-31 18:36:43 -07:00
$importance = $tp -> toDB ( $importance , true , false , 'no_html' );
$eventcode = $tp -> toDB ( $eventcode , true , false , 'no_html' );
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( is_array ( $explain ))
{
2014-01-10 07:36:54 -08:00
/*
2010-04-20 21:35:20 +00:00
$line = '' ;
$spacer = '' ;
foreach ( $explain as $k => $v )
{
$line .= $spacer . $k . '=>' . $v ;
$spacer = '[!br!]' ;
}
$explain = $line ;
unset ( $line );
2014-01-10 07:36:54 -08:00
*/
$explain = str_replace ( " \n " , '[!br!]' , print_r ( $explain , true ));
2010-04-20 21:35:20 +00:00
}
2013-05-12 20:56:35 -07:00
2010-04-20 21:35:20 +00:00
$explain = mysql_real_escape_string ( $tp -> toDB ( $explain , true , false , 'no_html' ));
$event_title = $tp -> toDB ( $event_title , true , false , 'no_html' );
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
//---------------------------------------
// Admin Log
//---------------------------------------
2013-05-12 20:56:35 -07:00
if ( $target_logs & LOG_TO_ADMIN ) // Admin log - assume all fields valid
{
// $qry = " null, ".intval($time_sec).','.intval($time_usec).", '{$importance}', '{$eventcode}', {$userid}, '{$userIP}', '{$event_title}', '{$explain}' ";
$adminLogInsert = array (
'dblog_id' => null ,
'dblog_type' => $importance ,
'dblog_eventcode' => $eventcode ,
'dblog_datestamp' => time (),
'dblog_microtime' => intval ( $time_usec ),
'dblog_user_id' => $userid ,
'dblog_ip' => $userIP ,
'dblog_title' => $event_title ,
'dblog_remarks' => $explain
);
$this -> rldb -> db_Insert ( " admin_log " , $adminLogInsert );
2010-04-20 21:35:20 +00:00
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
//---------------------------------------
// Audit Log
//---------------------------------------
// Add in audit log here
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
//---------------------------------------
// Rolling Log
//---------------------------------------
2015-02-14 23:34:15 -08:00
if (( $target_logs & LOG_TO_ROLLING ) && vartrue ( $pref [ 'roll_log_active' ]))
2010-04-20 21:35:20 +00:00
{ // Rolling log
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// Process source_call info
//---------------------------------------
if ( is_numeric ( $source_call ) && ( $source_call >= 0 ))
{
$back_count = 1 ;
$i = 0 ;
if ( is_numeric ( $source_call ) || ( $source_call == '' ))
{
$back_count = $source_call + 1 ;
$source_call = debug_backtrace ();
$i = 1 ; // Don't want to print the entry parameters to this function - we know all that!
}
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( is_array ( $source_call ))
{ // Print the debug_backtrace() array
while ( $i < $back_count )
{
$source_call [ $i ][ 'file' ] = $e107 -> fix_windows_paths ( $source_call [ $i ][ 'file' ]); // Needed for Windoze hosts.
$source_call [ $i ][ 'file' ] = str_replace ( $e107 -> file_path , " " , $source_call [ $i ][ 'file' ]); // We really just want a e107 root-relative path. Strip out the root bit
$tmp = $source_call [ $i ][ 'file' ] . " | " . $source_call [ $i ][ 'class' ] . $source_call [ $i ][ 'type' ] . $source_call [ $i ][ 'function' ] . " @ " . $source_call [ $i ][ 'line' ];
foreach ( $source_call [ $i ][ 'args' ] as $k => $v )
{ // Add in the arguments
$explain .= " [!br!] " . $k . " = " . $v ;
}
$i ++ ;
if ( $i < $back_count )
$explain .= " [!br!]------------------- " ;
if ( ! isset ( $tmp1 ))
$tmp1 = $tmp ; // Pick off the immediate caller as the source
}
if ( isset ( $tmp1 )) $source_call = $tmp1 ;
else $source_call = 'Root level' ;
}
else
{
$source_call = $e107 -> fix_windows_paths ( $source_call ); // Needed for Windoze hosts.
$source_call = str_replace ( $e107 -> file_path , " " , $source_call ); // We really just want a e107 root-relative path. Strip out the root bit
$source_call = $tp -> toDB ( $source_call , true , false , 'no_html' );
}
// else $source_call is a string
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// Save new rolling log record
$this -> rldb -> db_Insert ( " dblog " , " 0, " . intval ( $time_sec ) . ', ' . intval ( $time_usec ) . " , ' { $importance } ', ' { $eventcode } ', { $userid } , ' { $userstring } ', ' { $userIP } ', ' { $source_call } ', ' { $event_title } ', ' { $explain } ' " );
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// Now delete any old stuff
$this -> rldb -> db_Delete ( " dblog " , " dblog_datestamp < ' " . intval ( time () - ( varset ( $pref [ 'roll_log_days' ], 7 ) * 86400 )) . " ' " );
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( $finished )
exit ; // Optional abort for all logs
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
/**--------------------------------------
* USER AUDIT ENTRY
*--------------------------------------
* Log user - related events
* @ param int $event_code is a defined constant ( see above ) which specifies the event
* @ param array $event_data is an array of data fields whose keys and values are logged ( usually user data , but doesn ' t have to be - can add messages here )
* @ param int $id
2010-05-16 11:14:19 +00:00
* @ param string $u_name
2010-04-20 21:35:20 +00:00
* both $id and $u_name are left blank except for admin edits and user login , where they specify the id and login name of the 'target' user
*
* @ return none
*/
function user_audit ( $event_type , $event_data , $id = '' , $u_name = '' )
{
2014-10-14 16:23:16 -07:00
global $e107 , $tp ;
2010-04-20 21:35:20 +00:00
list ( $time_usec , $time_sec ) = explode ( " " , microtime ()); // Log event time immediately to minimise uncertainty
$time_usec = $time_usec * 1000000 ;
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
// See whether we should log this
2014-10-14 16:23:16 -07:00
$user_logging_opts = e107 :: getConfig () -> get ( 'user_audit_opts' );
2010-04-20 21:35:20 +00:00
if ( ! isset ( $user_logging_opts [ $event_type ]))
return ; // Finished if not set to log this event type
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( $this -> rldb == NULL )
$this -> rldb = new db ; // Better use our own db - don't know what else is going on
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
if ( $id ) $userid = $id ;
else $userid = ( USER === TRUE ) ? USERID : 0 ;
if ( $u_name ) $userstring = $u_name ;
else $userstring = ( USER === true ? USERNAME : " LAN_ANONYMOUS " );
2012-01-02 22:06:22 +00:00
$userIP = e107 :: getIPHandler () -> getIP ( FALSE );
2010-04-20 21:35:20 +00:00
$eventcode = 'USER_' . $event_type ;
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
$title = 'LAN_AUDIT_LOG_0' . $event_type ; // This creates a string which will be displayed as a constant
$spacer = '' ;
$detail = '' ;
foreach ( $event_data as $k => $v )
{
$detail .= $spacer . $k . '=>' . $v ;
$spacer = '<br />' ;
}
$this -> rldb -> db_Insert ( " audit_log " , " 0, " . intval ( $time_sec ) . ', ' . intval ( $time_usec ) . " , ' { $eventcode } ', { $userid } , ' { $userstring } ', ' { $userIP } ', ' { $title } ', ' { $detail } ' " );
}
/* Legacy function probably not needed
function get_log_events ( $count = 15 , $offset )
{
global $sql ;
$count = intval ( $count );
return " Not implemented yet " ;
}
*/
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
/**
* Removes all events older than $days , or truncates the table if $days == false
*
* @ param integer | false $days
* @ return void
*/
public function purge_log_events ( $days )
{
global $sql ;
if ( $days == false )
{ // $days is false, so truncate the log table
$sql -> db_Select_gen ( " TRUNCATE TABLE #dblog " );
}
else
{ // $days is set, so remove all entries older than that.
$days = intval ( $days );
$mintime = $days * 24 * 60 * 60 ;
$time = time () - $mintime ;
$sql -> db_Delete ( " dblog " , " WHERE `dblog_datestamp` < { $time } " , true );
}
}
2010-05-16 11:14:19 +00:00
2010-04-20 21:35:20 +00:00
//--------------------------------------
// HELPER ROUTINES
//--------------------------------------
/**
* Generic routine to log changes to an array . Only elements in $new are checked
*
* @ param array $new - most recent data being saved
* @ param array $old existing data - array is updated with changes , but not saved anywhere
* @ param string $event - LAN define or string used as title in log
*
* @ return bool true if changes found and logged , false otherwise .
*/
2015-02-14 23:34:15 -08:00
function logArrayDiffs ( $new , $old , $event , $logNow = true )
2010-04-20 21:35:20 +00:00
{
2014-01-10 07:36:54 -08:00
// $changes = array();
$changes = array_diff_recursive ( $new , $old );
if ( count ( $changes ))
2010-04-20 21:35:20 +00:00
{
2014-01-10 07:36:54 -08:00
if ( $logNow )
2010-04-20 21:35:20 +00:00
{
2014-01-10 07:36:54 -08:00
$this -> add ( $event , print_r ( $changes , true ), E_LOG_INFORMATIVE , '' );
2010-04-20 21:35:20 +00:00
}
2014-01-10 07:36:54 -08:00
else
{
$this -> logMessage ( $changes , LOG_MESSAGE_NODISPLAY , E_MESSAGE_INFO );
}
2015-02-14 23:34:15 -08:00
2010-04-20 21:35:20 +00:00
return TRUE ;
}
2014-01-10 07:36:54 -08:00
2010-04-20 21:35:20 +00:00
return FALSE ;
}
/**
* Logs an entry with all the data from an array , one field per line .
2014-01-10 07:36:54 -08:00
* @ deprecated
2010-04-20 21:35:20 +00:00
* @ param string $event - LAN define or string used as title in log
* @ param array $target - data to be logged
* @ param string $extra - if non - empty , it goes on the first line .
* @ param array $niceNames - Normally data is logged in the format keyname => value , one per line .
* If the $niceName array exists and has a definition , the 'nice Name' is displayed instead of the key name
*
* @ return none
*/
public function logArrayAll ( $event , $target , $extra = '' , $niceNames = NULL )
{
2014-01-10 07:36:54 -08:00
if ( $extra == '' && $niceNames == null )
{
return $this -> add ( $event , $target , E_LOG_INFORMATIVE , '' ); // supports arrays
}
2010-04-20 21:35:20 +00:00
$logString = '' ;
if ( $extra )
{
$logString = $extra . '[!br!]' ;
}
$spacer = '' ;
$checkNice = ( $niceNames != NULL ) && is_array ( $niceNames );
foreach ( $target as $k => $v )
{
if ( $checkNice && isset ( $niceNames [ $k ][ 'niceName' ]))
{
$logString .= $spacer . $niceNames [ $k ][ 'niceName' ] . '=>' . $v ;
}
else
{
$logString .= $spacer . $k . '=>' . $v ;
}
$spacer = '[!br!]' ;
}
2013-05-12 20:56:35 -07:00
$this -> add ( $event , $logString , E_LOG_INFORMATIVE , '' );
2010-04-20 21:35:20 +00:00
}
2015-02-14 23:34:15 -08:00
2010-04-20 21:35:20 +00:00
/**
2015-02-14 23:34:15 -08:00
* The next two routines accept and buffers messages which are destined for both admin log and message handler
2010-04-20 21:35:20 +00:00
*/
/**
* Add a message to the queue
*
* @ param string $text - the message text for logging / display
2015-02-14 23:34:15 -08:00
* @ param int $type - the 'importance' of the message . E_MESSAGE_SUCCESS | E_MESSAGE_ERROR | E_MESSAGE_INFO | E_MESSAGE_DEBUG | E_MESSAGE_NODISPLAY
2010-04-20 21:35:20 +00:00
* ( Values as used in message handler , apart from the last , which causes the message to not be passed to the message handler
2010-05-16 11:14:19 +00:00
* @ param boolean | int $logLevel - TRUE to give same importance as for message display . FALSE to not log .
2010-04-20 21:35:20 +00:00
* one of the values specified for $mesLevel to determine the prefix attached to the log message
2015-02-14 23:34:15 -08:00
* @ param boolean $session add session message
*
* @ return e_admin_log
2010-04-20 21:35:20 +00:00
*/
2015-02-14 23:34:15 -08:00
public function logMessage ( $text , $type = '' , $logLevel = TRUE , $session = FALSE )
2014-01-17 16:46:24 -08:00
{
if ( is_array ( $text ))
{
$text = print_r ( $text , true );
}
elseif ( empty ( $text ))
2014-01-12 08:06:29 -08:00
{
2014-01-13 11:07:02 -08:00
$bt = debug_backtrace ( true );
e107 :: getMessage () -> addDebug ( " Log Message was empty: " . print_a ( $bt [ 1 ], true ));
2014-01-12 08:06:29 -08:00
return $this ; // changing to text will break chained methods.
2014-01-17 16:46:24 -08:00
}
2015-02-14 23:34:15 -08:00
if ( ! $type ) $type = E_MESSAGE_INFO ;
2013-05-12 20:56:35 -07:00
if ( $logLevel === TRUE ) $logLevel = $type ;
$logArray = array ( 'message' => $text , 'dislevel' => $type , 'loglevel' => $logLevel , 'session' => $session , 'time' => time ());
2015-02-14 23:34:15 -08:00
2013-05-12 20:56:35 -07:00
$this -> _messages [] = $logArray ;
2014-01-12 08:06:29 -08:00
$this -> _allMessages [] = $logArray ;
2015-02-14 23:34:15 -08:00
return $this ;
}
2013-05-12 20:56:35 -07:00
/**
* @ DEPRECATED
* BC Alias for addSuccess ();
*/
public function logSuccess ( $text , $message = true , $session = false )
2010-04-20 21:35:20 +00:00
{
2013-05-12 20:56:35 -07:00
return $this -> addSuccess ( $text , $message , $session );
2010-04-20 21:35:20 +00:00
}
2013-05-12 20:56:35 -07:00
/**
* @ DEPRECATED
* BC Alias for addError ();
*/
public function logError ( $text , $message = true , $session = false )
{
return $this -> addError ( $text , $message , $session );
}
/**
2014-01-10 07:36:54 -08:00
* Add a success message to the log queue
2013-05-12 20:56:35 -07:00
*
* @ param string $text
* @ param boolean $message if true - register with eMessage handler
* @ param boolean $session add session message
* @ return e_admin_log
*/
public function addSuccess ( $text , $message = true , $session = false )
{
return $this -> logMessage ( $text , ( $message ? E_MESSAGE_SUCCESS : LOG_MESSAGE_NODISPLAY ), E_MESSAGE_SUCCESS , $session );
}
2015-02-14 23:34:15 -08:00
/**
* Add an error message to the log queue
*
* @ param string $text
* @ param boolean $message if true ( default ) - register with eMessage handler , set to false to hide .
* @ param boolean $session add session message
* @ return e_admin_log
*/
public function addError ( $text , $message = true , $session = false )
{
return $this -> logMessage ( $text , ( $message ? E_MESSAGE_ERROR : LOG_MESSAGE_NODISPLAY ), E_MESSAGE_ERROR , $session );
}
2010-04-20 21:35:20 +00:00
2013-05-12 20:56:35 -07:00
/**
2014-01-10 07:36:54 -08:00
* Add an Debug message to the log queue
2013-05-12 20:56:35 -07:00
*
* @ param string $text
2014-01-10 07:36:54 -08:00
* @ param boolean $message if true ( default ) - register with eMessage handler , set to false to hide .
2013-05-12 20:56:35 -07:00
* @ param boolean $session add session message
* @ return e_admin_log
*/
public function addDebug ( $text , $message = true , $session = false )
{
return $this -> logMessage ( $text , ( $message ? E_MESSAGE_DEBUG : LOG_MESSAGE_NODISPLAY ), E_MESSAGE_NOTICE , $session );
}
/**
2014-01-10 07:36:54 -08:00
* Add an Warning message to the log queue
2013-05-12 20:56:35 -07:00
*
* @ param string $text
2014-01-10 07:36:54 -08:00
* @ param boolean $message if true ( default ) - register with eMessage handler , set to false to hide .
2013-05-12 20:56:35 -07:00
* @ param boolean $session add session message
* @ return e_admin_log
*/
public function addWarning ( $text , $message = true , $session = false )
{
return $this -> logMessage ( $text , ( $message ? E_MESSAGE_WARNING : LOG_MESSAGE_NODISPLAY ), E_MESSAGE_WARNING , $session );
}
2014-01-10 07:36:54 -08:00
/**
* Add an array to the log queue
* @ param $array
* @ param $oldArray ( optional ) - when included , only the changes between the arrays is saved .
* @ param $type ( optional ) default : LOG_MESSAGE_NODISPLAY . or E_MESSAGE_WARNING , E_MESSAGE_NOTICE , E_MESSAGE_SUCCESS
*/
2014-01-12 08:06:29 -08:00
public function addArray ( $array , $oldArray = null , $type = LOG_MESSAGE_NODISPLAY , $session = false )
2014-01-10 07:36:54 -08:00
{
if ( is_array ( $oldArray ))
{
$text = array_diff_recursive ( $array , $oldArray ); // Located in core_functions.php
2014-01-17 16:46:24 -08:00
if ( count ( $text ) < 1 )
{
$text = " No differences found " ;
}
2014-01-10 07:36:54 -08:00
}
2014-01-12 08:06:29 -08:00
else
{
$text = $array ;
}
2014-01-13 11:07:02 -08:00
2014-01-10 07:36:54 -08:00
return $this -> logMessage ( $text , $type , $type , $session );
}
2013-05-12 20:56:35 -07:00
2010-04-20 21:35:20 +00:00
/**
* Empty the messages - pass to both admin log and message handler
*
* @ param string $logTitle - title for log entry
* @ param int $logImportance - passed directly to admin log
* @ param string $logEventCode - passed directly to admin log
2013-02-27 19:33:50 +02:00
* @ param string $mstack [ optional ] message stack passed to message handler
2015-02-14 23:34:15 -08:00
* @ return e_admin_log
2010-04-20 21:35:20 +00:00
*/
2013-02-27 19:33:50 +02:00
public function flushMessages ( $logTitle , $logImportance = E_LOG_INFORMATIVE , $logEventCode = '' , $mstack = false )
2010-04-20 21:35:20 +00:00
{
$mes = e107 :: getMessage ();
2013-05-12 20:56:35 -07:00
2010-04-20 21:35:20 +00:00
$resultTypes = array ( E_MESSAGE_SUCCESS - 'Success' , E_MESSAGE_ERROR => 'Fail' ); // Add LANS here. Could add other codes
$separator = '' ;
$logString = '' ;
2015-02-14 23:34:15 -08:00
foreach ( $this -> _messages as $m )
2010-04-20 21:35:20 +00:00
{
if ( $m [ 'loglevel' ] !== FALSE )
{
$logString .= $separator ;
if ( $m [ 'loglevel' ] == LOG_MESSAGE_NODISPLAY ) { $logString .= ' ' ; } // Indent supplementary messages
2015-02-14 23:34:15 -08:00
// Not sure about next line - might want to log the <br /> as text, rather than it forcing a newline
$logString .= strip_tags ( str_replace ( array ( '<br>' , '<br/>' , '<br />' ), '[!br!]' , $m [ 'message' ]));
2010-04-20 21:35:20 +00:00
if ( isset ( $resultTypes [ $m [ 'loglevel' ]]))
{
$logString .= ' - ' . $resultTypes [ $m [ 'loglevel' ]];
}
$separator = '[!br!]' ;
}
2015-02-14 23:34:15 -08:00
if ( $m [ 'dislevel' ] != LOG_MESSAGE_NODISPLAY )
2010-04-20 21:35:20 +00:00
{
2013-02-27 19:33:50 +02:00
if ( $mstack )
{
$mes -> addStack ( $m [ 'message' ], $mstack , $m [ 'dislevel' ], $m [ 'session' ]);
// move to main stack OUTSIDE if needed
}
2015-02-14 23:34:15 -08:00
else $mes -> add ( $m [ 'message' ], $m [ 'dislevel' ], $m [ 'session' ]);
2010-04-20 21:35:20 +00:00
}
}
2013-05-12 20:56:35 -07:00
e107 :: getAdminLog () -> add ( $logTitle , $logString , $logImportance , $logEventCode );
$this -> _messages = array (); // Clear the memory for reuse
2015-02-14 23:34:15 -08:00
return $this ;
2010-04-20 21:35:20 +00:00
}
2013-05-12 20:56:35 -07:00
2014-01-17 16:46:24 -08:00
/**
* Clear all messages in 'memory' .
*/
public function clear ()
{
$this -> _messages = array ();
return $this ;
}
2013-05-12 20:56:35 -07:00
/**
* Save Message stack to File .
*/
2013-06-01 04:36:58 -07:00
private function saveToFile ( $logTitle = '' , $append = false )
2013-05-12 20:56:35 -07:00
{
if ( $this -> logFile == null )
{
return ;
}
2013-06-02 14:31:25 -07:00
2013-05-12 20:56:35 -07:00
if ( count ( $this -> _allMessages ))
{
2013-06-02 14:31:25 -07:00
$head = " e107 CMS Log file : " . $logTitle . " " . date ( 'Y-m-d_H-i-s' ) . " \n " ;
$head .= " ------------------------------------------------------------------------------------------- \n \n " ;
2013-05-15 16:07:10 -07:00
}
else
2015-02-14 23:34:15 -08:00
{
return ;
2013-05-12 20:56:35 -07:00
}
foreach ( $this -> _allMessages as $m )
{
2013-06-02 14:31:25 -07:00
$text .= date ( 'Y-m-d H:i:s' , $m [ 'time' ]) . " \t " . str_pad ( $m [ 'dislevel' ], 10 , " " , STR_PAD_RIGHT ) . " \t " . strip_tags ( $m [ 'message' ]) . " \n " ;
2013-05-12 20:56:35 -07:00
}
2013-06-01 04:36:58 -07:00
$date = ( $append == true ) ? date ( 'Y-m-d' ) : date ( 'Y-m-d_H-i-s' ) . '_' . crc32 ( $text );
2013-06-03 14:17:08 -07:00
$dir = e_LOG ;
if ( e_CURRENT_PLUGIN ) // If it's a plugin, create a subfolder.
{
$dir = e_LOG . e_CURRENT_PLUGIN . " / " ;
if ( ! is_dir ( $dir ))
{
mkdir ( $dir , 0755 );
}
}
$fileName = $dir . $date . " _ " . $this -> logFile . " .log " ;
2013-05-12 20:56:35 -07:00
2013-06-01 04:36:58 -07:00
if ( $append == true )
{
$app = FILE_APPEND ;
2013-06-02 14:31:25 -07:00
if ( ! file_exists ( $fileName ))
{
$text = $head . $text ;
}
2013-06-01 04:36:58 -07:00
}
else
2015-02-14 23:34:15 -08:00
{
2013-06-02 14:31:25 -07:00
$app = null ;
2015-02-14 23:34:15 -08:00
$text = $head . $text ;
2013-06-01 04:36:58 -07:00
}
2013-06-03 14:17:08 -07:00
2013-06-01 04:36:58 -07:00
if ( file_put_contents ( $fileName , $text , $app ))
2013-05-12 20:56:35 -07:00
{
$this -> _allMessages = array ();
return $this -> logFile ;
2013-06-03 14:17:08 -07:00
}
elseif ( getperms ( '0' ) && E107_DEBUG_LEVEL > 0 )
2015-02-14 23:34:15 -08:00
{
echo " Could Save to Log File: " . $fileName ;
2013-05-12 20:56:35 -07:00
}
return false ;
}
/**
2013-06-01 04:36:58 -07:00
* Set and save accumulated log to a file .
* Use addDebug (), addError () or addSuccess () prior to executing .
* @ param string name without the extension . ( ie . date prefix and . log suffix will be added automatically )
* @ param string Title for use inside the Log file
* @ param boolean true = append to file , false = new file each save .
2013-05-12 20:56:35 -07:00
*/
2013-06-19 19:54:29 -07:00
public function toFile ( $name , $logTitle = '' , $append = false )
2013-05-12 20:56:35 -07:00
{
2013-06-19 19:54:29 -07:00
2013-05-15 16:07:10 -07:00
$this -> logFile = $name ;
2013-06-19 19:54:29 -07:00
$file = $this -> saveToFile ( $logTitle , $append );
2013-05-12 20:56:35 -07:00
$this -> logFile = null ;
2013-06-19 19:54:29 -07:00
return $file ;
2013-05-12 20:56:35 -07:00
}
2010-04-20 21:35:20 +00:00
}