eZ Publish  [4.0]
ezaudit.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZAudit class
00004 //
00005 // Created on: <01-aug-2006 11:00:54 vd>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 //include_once( 'lib/ezutils/classes/ezini.php' );
00032 //include_once( "lib/ezfile/classes/ezlog.php" );
00033 
00034 class eZAudit
00035 {
00036     const DEFAULT_LOG_DIR = 'var/log/audit';
00037 
00038     /*!
00039       Creates a new audit object.
00040     */
00041     function eZAudit( )
00042     {
00043     }
00044 
00045     /*
00046      \static
00047      Returns an associative array of all names of audit and the log files used by this class,
00048      Will be fetched from ini settings.
00049     */
00050     static function fetchAuditNameSettings()
00051     {
00052         $ini = eZINI::instance( 'audit.ini' );
00053 
00054         $auditNames = $ini->hasVariable( 'AuditSettings', 'AuditFileNames' )
00055                       ? $ini->variable( 'AuditSettings', 'AuditFileNames' )
00056                       : array();
00057         $logDir = $ini->hasVariable( 'AuditSettings', 'LogDir' ) ? $ini->variable( 'AuditSettings', 'LogDir' ): self::DEFAULT_LOG_DIR;
00058 
00059         $resultArray = array();
00060         foreach ( array_keys( $auditNames ) as $auditNameKey )
00061         {
00062             $auditNameValue = $auditNames[$auditNameKey];
00063             $resultArray[$auditNameKey] = array( 'dir' => $logDir,
00064                                                  'file_name' => $auditNameValue );
00065         }
00066         return $resultArray;
00067     }
00068 
00069     /*!
00070      \static
00071      Writes $auditName with $auditAttributes as content
00072      to file name that will be fetched from ini settings by auditNameSettings() for logging.
00073     */
00074     static function writeAudit( $auditName, $auditAttributes = array() )
00075     {
00076         $enabled = eZAudit::isAuditEnabled();
00077         if ( !$enabled )
00078             return false;
00079 
00080         $auditNameSettings = eZAudit::auditNameSettings();
00081 
00082         if ( !isset( $auditNameSettings[$auditName] ) )
00083             return false;
00084 
00085         $ip = eZSys::serverVariable( 'REMOTE_ADDR', true );
00086         if ( !$ip )
00087             $ip = eZSys::serverVariable( 'HOSTNAME', true );
00088 
00089         //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00090         $user = eZUser::currentUser();
00091         $userID = $user->attribute( 'contentobject_id' );
00092         $userLogin = $user->attribute( 'login' );
00093 
00094         $message = "[$ip] [$userLogin:$userID]\n";
00095 
00096         foreach ( array_keys( $auditAttributes ) as $attributeKey )
00097         {
00098             $attributeValue = $auditAttributes[$attributeKey];
00099             $message .= "$attributeKey: $attributeValue\n";
00100         }
00101 
00102         $logName = $auditNameSettings[$auditName]['file_name'];
00103         $dir = $auditNameSettings[$auditName]['dir'];
00104         eZLog::write( $message, $logName, $dir );
00105 
00106         return true;
00107     }
00108 
00109     /*!
00110      \static
00111      \return true if audit should be enabled.
00112     */
00113     static function isAuditEnabled()
00114     {
00115         if ( isset( $GLOBALS['eZAuditEnabled'] ) )
00116         {
00117             return $GLOBALS['eZAuditEnabled'];
00118         }
00119         $enabled = eZAudit::fetchAuditEnabled();
00120         $GLOBALS['eZAuditEnabled'] = $enabled;
00121         return $enabled;
00122     }
00123 
00124     /*!
00125      \static
00126      \return true if audit should be enabled.
00127      \note Will fetch from ini setting.
00128     */
00129     static function fetchAuditEnabled()
00130     {
00131         $ini = eZINI::instance( 'audit.ini' );
00132         $auditEnabled = $ini->hasVariable( 'AuditSettings', 'Audit' )
00133                       ? $ini->variable( 'AuditSettings', 'Audit' )
00134                       : 'disabled';
00135         $enabled = $auditEnabled == 'enabled';
00136         return $enabled;
00137     }
00138 
00139     /*!
00140      \static
00141      Returns an associative array of all names of audit and the log files used by this class
00142     */
00143     static function auditNameSettings()
00144     {
00145         if ( isset( $GLOBALS['eZAuditNameSettings'] ) )
00146         {
00147             return $GLOBALS['eZAuditNameSettings'];
00148         }
00149         $nameSettings = eZAudit::fetchAuditNameSettings();
00150         $GLOBALS['eZAuditNameSettings'] = $nameSettings;
00151         return $nameSettings;
00152     }
00153 }
00154 ?>