| 1 | <?php
 | 
  
    | 2 | /**
 | 
  
    | 3 |  *
 | 
  
    | 4 |  * @category        event logging
 | 
  
    | 5 |  * @package         core
 | 
  
    | 6 |  * @author          Independend-Software-Team
 | 
  
    | 7 |  * @author          WebsiteBaker Project
 | 
  
    | 8 |  * @copyright       2009-2011, Website Baker Org. e.V.
 | 
  
    | 9 |  * @link            http://www.websitebaker2.org/
 | 
  
    | 10 |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
  
    | 11 |  * @platform        WebsiteBaker 2.8.2
 | 
  
    | 12 |  * @requirements    PHP 5.2.2 and higher
 | 
  
    | 13 |  * @version         $Id: class.logfile.php 2 2017-07-02 15:14:29Z Manuela $
 | 
  
    | 14 |  * @filesource        $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/trunk/framework/class.logfile.php $
 | 
  
    | 15 |  * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
 | 
  
    | 16 |  * @description     definition of all core constants.
 | 
  
    | 17 |  */
 | 
  
    | 18 | 
 | 
  
    | 19 | /**
 | 
  
    | 20 |  * Description of classlog
 | 
  
    | 21 |  *
 | 
  
    | 22 |  * @author wkl
 | 
  
    | 23 |  */
 | 
  
    | 24 | class LogFile {
 | 
  
    | 25 | 
 | 
  
    | 26 |     private $_fh;                  // file-handle for logfile
 | 
  
    | 27 |     private $_log_path;            // path to logfile
 | 
  
    | 28 |     private $_log_file;            // name of logfile
 | 
  
    | 29 |     private $_error = false;       // store internal errors
 | 
  
    | 30 | /*
 | 
  
    | 31 |  * class can not be instanciated standalone
 | 
  
    | 32 |  */
 | 
  
    | 33 |     protected function __construct( $log_file )
 | 
  
    | 34 |     {
 | 
  
    | 35 |         $this->_log_file = $log_file;
 | 
  
    | 36 |     }
 | 
  
    | 37 | 
 | 
  
    | 38 | /*
 | 
  
    | 39 |  * open the logfile for append
 | 
  
    | 40 |  */
 | 
  
    | 41 |     private function openLogFile()
 | 
  
    | 42 |     {
 | 
  
    | 43 |         $this->_fh = fopen($this->_log_path.$this->_log_file, 'ab');
 | 
  
    | 44 |         return isset($this->_fh);
 | 
  
    | 45 |     }
 | 
  
    | 46 | /*
 | 
  
    | 47 |  * provide read-only properties
 | 
  
    | 48 |  */
 | 
  
    | 49 |     public function __get($property)
 | 
  
    | 50 |     {
 | 
  
    | 51 |         switch(strtolower($property)):
 | 
  
    | 52 |             case 'error':
 | 
  
    | 53 |                 return $this->_error;
 | 
  
    | 54 |                 break;
 | 
  
    | 55 |             default:
 | 
  
    | 56 |                 return null;
 | 
  
    | 57 |         endswitch;
 | 
  
    | 58 |     }
 | 
  
    | 59 | /*
 | 
  
    | 60 |  * flush and close logfile
 | 
  
    | 61 |  */
 | 
  
    | 62 |     private function closeLogFile()
 | 
  
    | 63 |     {
 | 
  
    | 64 |         if( isset($this->_fh) )
 | 
  
    | 65 |         {
 | 
  
    | 66 |             fflush($this->_fh);
 | 
  
    | 67 |             fclose($this->_fh);
 | 
  
    | 68 |             unset($this->_fh);
 | 
  
    | 69 |         }
 | 
  
    | 70 |     }
 | 
  
    | 71 | 
 | 
  
    | 72 | /*
 | 
  
    | 73 |  * @param  string $logdir: directory to place the logfile
 | 
  
    | 74 |  * @return bool: true if directory is valid and writeable
 | 
  
    | 75 |  * @description:
 | 
  
    | 76 |  */
 | 
  
    | 77 |     public function setLogDir( $logdir )
 | 
  
    | 78 |     {
 | 
  
    | 79 |         $this->_error = false;
 | 
  
    | 80 |         $retval = false;
 | 
  
    | 81 |         if( ($logdir = realpath($logdir)) )
 | 
  
    | 82 |         {
 | 
  
    | 83 |             $logdir = rtrim(str_replace('\\', '/', $logdir), '/');
 | 
  
    | 84 |             if( defined('WB_PATH') )
 | 
  
    | 85 |             {
 | 
  
    | 86 |                 $sysroot = WB_PATH;
 | 
  
    | 87 |             }
 | 
  
    | 88 |             else
 | 
  
    | 89 |             {
 | 
  
    | 90 |                 $script_filename = str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']);
 | 
  
    | 91 |                 $script_name = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
 | 
  
    | 92 |                 $sysroot = str_replace($script_name, '', $script_filename);
 | 
  
    | 93 |             }
 | 
  
    | 94 |             if( stripos($logdir, $sysroot) === 0 )
 | 
  
    | 95 |             {
 | 
  
    | 96 |                 if( is_writable($logdir))
 | 
  
    | 97 |                 {
 | 
  
    | 98 |                     if( file_exists($logdir.'/'.$this->_log_file) )
 | 
  
    | 99 |                     {
 | 
  
    | 100 |                         if( is_writable($logdir.'/'.$this->_log_file) )
 | 
  
    | 101 |                         {
 | 
  
    | 102 |                             $this->_log_path = $logdir.'/';
 | 
  
    | 103 |                             $retval = true;
 | 
  
    | 104 |                         }else
 | 
  
    | 105 |                         {
 | 
  
    | 106 |                             $this->_error = 'existing logfile is not writable! ['.$logdir.$this->_log_file.']';
 | 
  
    | 107 |                         }
 | 
  
    | 108 |                     }
 | 
  
    | 109 |                     else
 | 
  
    | 110 |                     {
 | 
  
    | 111 |                         $this->_log_path = $logdir.'/';
 | 
  
    | 112 |                         $retval = true;
 | 
  
    | 113 |                     }
 | 
  
    | 114 |                 }else
 | 
  
    | 115 |                 {
 | 
  
    | 116 |                     $this->_error = 'access denied for directory ['.$logdir.']';
 | 
  
    | 117 |                 }
 | 
  
    | 118 |             }else
 | 
  
    | 119 |             {
 | 
  
    | 120 |                 $this->_error = 'logdir [ '.$logdir.' ] points outside of DOCUMENT_ROOT [ '.$sysroot.' ]';
 | 
  
    | 121 |             }
 | 
  
    | 122 |         }else
 | 
  
    | 123 |         {
 | 
  
    | 124 |             $this->_error = 'logdir can not be resolved ['.$logdir.']';
 | 
  
    | 125 |         }
 | 
  
    | 126 |         return $retval;
 | 
  
    | 127 |     }
 | 
  
    | 128 | 
 | 
  
    | 129 | /*
 | 
  
    | 130 |  * @param string $line: preformatted message to write into the logfile
 | 
  
    | 131 |  * @return none: an error will throw a exception
 | 
  
    | 132 |  */
 | 
  
    | 133 |     protected function writeRaw( $message )
 | 
  
    | 134 |     {
 | 
  
    | 135 |         array_unshift( $message, (defined($_SESSION['USER_ID'])?$_SESSION['USER_ID']:0) );
 | 
  
    | 136 |         array_unshift( $message, (isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'#') );
 | 
  
    | 137 |         array_unshift( $message, gmdate(DATE_W3C) );
 | 
  
    | 138 |         if( isset($this->_log_path) ){
 | 
  
    | 139 |             if($this->openLogFile())
 | 
  
    | 140 |             {
 | 
  
    | 141 |                 if( fputcsv($this->_fh, $message, ',', '"') !== false )
 | 
  
    | 142 |                 {
 | 
  
    | 143 |                     $this->closeLogFile();
 | 
  
    | 144 |                 }
 | 
  
    | 145 |                 else
 | 
  
    | 146 |                 {
 | 
  
    | 147 |                     throw new Exception('unable to append line ['.$this->_log_path.$this->_log_file.']');
 | 
  
    | 148 |                 }
 | 
  
    | 149 |             }
 | 
  
    | 150 |             else
 | 
  
    | 151 |             {
 | 
  
    | 152 |                 throw new Exception('unable to open logfile ['.$this->_log_path.$this->_log_file.']');
 | 
  
    | 153 |             }
 | 
  
    | 154 |         }else
 | 
  
    | 155 |         {
 | 
  
    | 156 |             throw new Exception('undefined path for logfile ['.$this->_log_file.']');
 | 
  
    | 157 |         }
 | 
  
    | 158 |     }
 | 
  
    | 159 | 
 | 
  
    | 160 | } // end of class
 | 
  
    | 161 | 
 | 
  
    | 162 | /*
 | 
  
    | 163 |  *  Errorlog handler
 | 
  
    | 164 |  */
 | 
  
    | 165 | class ErrorLog extends LogFile{
 | 
  
    | 166 | 
 | 
  
    | 167 |     private static $_instance;
 | 
  
    | 168 | 
 | 
  
    | 169 |     protected function __construct()
 | 
  
    | 170 |     {
 | 
  
    | 171 |         parent::__construct('error.log');
 | 
  
    | 172 |     }
 | 
  
    | 173 | 
 | 
  
    | 174 |     private function __clone() {}
 | 
  
    | 175 | 
 | 
  
    | 176 |     public static function handle()
 | 
  
    | 177 |     {
 | 
  
    | 178 |         if (!isset(self::$_instance)) {
 | 
  
    | 179 |             $c = __CLASS__;
 | 
  
    | 180 |             self::$_instance = new $c;
 | 
  
    | 181 |         }
 | 
  
    | 182 |         return self::$_instance;
 | 
  
    | 183 |     }
 | 
  
    | 184 | 
 | 
  
    | 185 | /*
 | 
  
    | 186 |  * @param string $message: message to write into the logfile
 | 
  
    | 187 |  * @param string $file: (optional) name of the file where the error occures
 | 
  
    | 188 |  * @param string $function: (optional) name of the function where the error occures
 | 
  
    | 189 |  * @param string $line: (optional) number of the line where the error occures
 | 
  
    | 190 |  * @return none: an error will throw a exception
 | 
  
    | 191 |  */
 | 
  
    | 192 |     public function write( $message, $file = '#', $function = '#', $line = '#' )
 | 
  
    | 193 |     {
 | 
  
    | 194 |         if( !is_array($message) )
 | 
  
    | 195 |         {
 | 
  
    | 196 |             $message = array($file, $function, $line, $message);
 | 
  
    | 197 |         }
 | 
  
    | 198 |         self::handle()->writeRaw( $message );
 | 
  
    | 199 |     }
 | 
  
    | 200 | } // end of class
 | 
  
    | 201 | 
 | 
  
    | 202 | /*
 | 
  
    | 203 |  *  Accesslog handler
 | 
  
    | 204 |  */
 | 
  
    | 205 | class AccessLog extends LogFile{
 | 
  
    | 206 | 
 | 
  
    | 207 |     private static $_instance;
 | 
  
    | 208 | 
 | 
  
    | 209 |     protected function __construct()
 | 
  
    | 210 |     {
 | 
  
    | 211 |         parent::__construct('access.log');
 | 
  
    | 212 |     }
 | 
  
    | 213 | 
 | 
  
    | 214 |     private function __clone() {}
 | 
  
    | 215 | 
 | 
  
    | 216 |     public static function handle()
 | 
  
    | 217 |     {
 | 
  
    | 218 |         if (!isset(self::$_instance)) {
 | 
  
    | 219 |             $c = __CLASS__;
 | 
  
    | 220 |             self::$_instance = new $c;
 | 
  
    | 221 |         }
 | 
  
    | 222 |         return self::$_instance;
 | 
  
    | 223 |     }
 | 
  
    | 224 | 
 | 
  
    | 225 | /*
 | 
  
    | 226 |  * @param string $message: message to write into the logfile
 | 
  
    | 227 |  * @return none: an error will throw a exception
 | 
  
    | 228 |  */
 | 
  
    | 229 |     public function write( $message )
 | 
  
    | 230 |     {
 | 
  
    | 231 |         if( !is_array($message) )
 | 
  
    | 232 |         {
 | 
  
    | 233 |             $message = array($message);
 | 
  
    | 234 |         }
 | 
  
    | 235 |         self::handle()->writeRaw( $message );
 | 
  
    | 236 |     }
 | 
  
    | 237 | } // end of class
 | 
  
    | 238 | 
 | 
  
    | 239 | 
 | 
  
    | 240 | ?>
 |