<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package Debug
 */

include_once('classes/system/Dir.class.php');
include_once('classes/system/File.class.php');

define('DBG_OPEN', 'open');
define('DBG_CLOSE','close');
define('DBG_NONE', 'none');
define('DBG_ROOT', 'root');

/**
 * @author  cedseven <cedseven@users.sourceforge.net>
 * @package Debug
 */
class Debug {

    var $recurse_level;

    function Debug() {
      $this->recurse_level = 0;
		}

    function &getInstance() {
    	static $debug = null;
		
      if (is_null($debug)){
        	$debug = new Debug();
			}
      return $debug;
    } 
	
    function report($text, $level = 0, $recurse = DBG_NONE, $file = DEBUG_ACCESS_FILE) {
			$message = '';
			
			if(ENABLE_LOGGING <= 0) {
				return;
			}

      if($level > DEBUG_LEVEL) {
				return;
			}

      if($recurse == DBG_CLOSE) {
        --$this->recurse_level;
      }

      if($recurse != DBG_ROOT) {
  			for($i = 0; $i < $this->recurse_level; ++$i) {
          $message .= '  ';
        }
      }

      $message .= $text."\n";

      if($recurse == DBG_OPEN) {
        ++$this->recurse_level;
      }

      if(is_file($file)) {
        clearstatcache();
        $size = sprintf("%u", filesize($file));
        if($size > MAX_LOG_SIZE) {
          $this->rotate_log($file);
        }
      }

			if(!($handler = @fopen($file, 'a+'))) {
	      die("Fatal error : unable to open file '$file' (current directory is '".getcwd()."').");
	    }
	    if($handler) {
	    	if (!(@fwrite($handler, $message))) {
					die("Fatal error : unable to write to file '$file'.");
				}
				fclose($handler);
			}
    }

  function &getReport($level = 0) {
		return file_get_contents(DEBUG_ACCESS_FILE);
  }
    
  function rotate_log($file) {
    $logFile  = new File('', $file);
    $logDir   = new Dir('', $logFile->getPath());

    $count_files = 0;
    $prefix      = $logFile->getPrefix();

    if(is_file($file)) {
      $today = date('Ymd_his');
      $name = $logFile->getName();
      $logFile->rename($prefix."_$today.log");
    }

    $logs_history_indexes = array();
    $listing = &$logDir->getListing();
    
    for($i = 0; $i < count($listing); ++$i) {
      $node = &$listing[$i];
      if (get_class($node) == 'file') {
        $name = $node->getName();
        if(stristr($name, $prefix) && $name != $logFile->getName()) {
          ++$count_files;
          $logs_history_indexes[] = $i;
        }
      }
    }

    for($i = 0; $i <= ($count_files - MAX_LOG_FILES) ; ++$i) {
      $index = $logs_history_indexes[$i];
      $node = &$listing[$index];
      $full_name = $node->getNodeFullName();
      if(is_file($full_name)) {
        unlink($full_name);
      }
    }
  }
}




set_error_handler('errorHandler');
error_reporting(E_ALL);

function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
  // Define extra E_RECOVERABLE_ERROR for PHP < 5.2
  defined('E_STRICT') or define('E_STRICT', 2048);
  defined('E_RECOVERABLE_ERROR') or define('E_RECOVERABLE_ERROR', 4096);

/*
 1	E_ERROR          128	E_COMPILE_WARNING
 2	E_WARNING        256	E_USER_ERROR
 4	E_PARSE          512	E_USER_WARNING
 8	E_NOTICE         1024	E_USER_NOTICE
16	E_CORE_ERROR     2047	E_ALL
32	E_CORE_WARNING   2048	E_STRICT
64	E_COMPILE_ERROR  4096	E_RECOVERABLE_ERROR
*/

	$debug = &Debug::getInstance();
	$errorString = '';

  if($errno & E_ERROR) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'error';
  }

  if($errno & E_WARNING) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'warning';
  }

  if($errno & E_PARSE) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'parse';
  }

  if($errno & E_NOTICE) {
    return ;
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'notice';
  }

  if($errno & E_CORE_ERROR) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'core error';
  }
  
  if($errno & E_CORE_WARNING) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'core warning';
  }

  if($errno & E_COMPILE_ERROR) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'compile error';
  }

  if($errno & E_COMPILE_WARNING) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'compile warning';
  }

  if($errno & E_USER_ERROR) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'user error';
  }

  if($errno & E_USER_WARNING) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'user warning';
  }

  if($errno & E_USER_NOTICE) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'user notice';
  }

  if($errno & E_STRICT) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'strict syntax notice';
  }

  if($errno & E_RECOVERABLE_ERROR) {
    if(strlen($errorString) > 0) {
      $errorString .= ' & ';
    }
    $errorString = 'recoverable error';
  }

  $now = date("Y-m-d H:i:s");
	$message = "$now : $errorString (# $errno) : $errstr, in line $errline of file '$errfile' ";
	
	if (isset($errcontext['this'])) {
	  if (is_object($errcontext['this'])) {
	     $classname = get_class($errcontext['this']);
	     $parentclass = get_parent_class($errcontext['this']);
	     $message .= " :: Class: '$classname', Parent Class: '$parentclass'";
	  }
	}
	$debug->report($message, 0, DBG_ROOT, DEBUG_ERROR_FILE);
}
?>