class_ErrorEvent.inc.php 4.81 KB
<?php
/**
 * @file	ErrorEvent.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
class ErrorEvent {
	private static $zeilenumbruch = '
';
	private static $tab = '	';

	public static $count_handleError =0;
	public static $count_handleNotice =0;



	public static function handleError($param_typ, $param_notice, $param_file, $param_row, $param_More) {
		global $_E_LOGFILE_ERROR;
		global $_E_CODEOFFSET;
		global $_E_MAILADDR;
		global $_E_MAILSUBJECT;

		self::$count_handleError++;
		//		$var_ErrorHandling = CONFIG::getConfig('ErrorHandling');
		$var_message = self::mkMessage($param_typ, $param_notice, $param_file, $param_row, $param_More, $_E_CODEOFFSET);
		self::logMessage($_E_LOGFILE_ERROR, $var_message);
		self::mailToAdmin($_E_MAILADDR, $_E_MAILSUBJECT, $var_message);
		self::informUser();
	}

	public static function handleNotice($param_typ, $param_notice, $param_file, $param_row, $param_More) {
		global $_E_LOGFILE_WARNINGNOTICE;
		global $_E_CODEOFFSET;

		self::$count_handleNotice++;
		//		$var_ErrorHandling = CONFIG::getConfig('ErrorHandling');
		$var_message = self::mkMessage($param_typ, $param_notice, $param_file, $param_row, $param_More, $_E_CODEOFFSET);
		self::logMessage($_E_LOGFILE_WARNINGNOTICE, $var_message);
	}

	private static function mkMessage($param_typ, $param_notice, $param_file, $param_row, $param_More, $param_offset) {
		$var_message = self::$zeilenumbruch.'-------------------------------------------------------------'.self::$zeilenumbruch;
		if(array_key_exists($param_typ, Error::$ErrorArray)) {
			$var_message .= 'Typ:'.self::$tab.Error::$ErrorArray[$param_typ].self::$tab.date('d. m. Y  G:i').self::$zeilenumbruch;
		} else {
			$var_message .= 'Typ:'.self::$tab.$param_typ.self::$tab.date('d. m. Y  G:i').self::$zeilenumbruch;
		}

		$var_message .= 'Notice:'.self::$tab.$param_notice.self::$zeilenumbruch;
		$var_message .= 'File:'.self::$tab.$param_file.' (row: '.$param_row.')'.self::$zeilenumbruch;
		$var_message .= 'Url:'.self::$tab.urldecode(http_build_query($_GET));
		//		$var_message .= self::$zeilenumbruch;
		$var_message .= self::getCodeFromRow($param_file, $param_row, $param_offset);
		//		$var_message .= self::$zeilenumbruch.self::$zeilenumbruch;
		return $var_message;
	}

	private static function informUser() {
		//		D::li('leider ist ein Fehler aufgetreten, der Admin wurde informiert, wir hoffen das er bald behoben ist ;) ');
	}

	private static function mailToAdmin($param_to, $param_subject, $param_message, $param_logfile=null) {
		mail($param_to, $param_subject, $param_message);
	}

	private static function logMessage($param_logfile, $param_message, $param_MailTo=null) {
		@$var_file = fopen($param_logfile, 'a');
		//		@$var_file = fopen($param_logfile, 'w');
		if ($var_file === false) {
			if($param_MailTo) {
				self::mailToAdmin($param_MailTo, '[The logfile does not exist]', $param_message);
			} else {
				D::li("The logfile $param_logfile does not exist");
			}
		} else {
			fputs($var_file, $param_message);
			fputs($var_file, self::$zeilenumbruch.self::$zeilenumbruch);
		}
	}

	private static function getCodeFromRow($param_file, $param_row, $param_offset=0) {
		$var_result = '';
		@$var_file = file($param_file);
		if($var_file === false) {
			$var_result .= 'could not read file'.self::$zeilenumbruch;
			$var_result .= 'Systemmessage: '.php_errormsg.self::$zeilenumbruch;
		} else {
			if(count($var_file) < $param_row) {
				$var_result .= 'row not in the file'.self::$zeilenumbruch;
			} else {
				if($param_row-$param_offset < 0) {
					$var_StartCode = 0;
				} else {
					$var_StartCode = $param_row - $param_offset ;
				}

				$var_EndCode = $param_row +  $param_offset;

				$var_result .= self::$zeilenumbruch.'<php>'.self::$zeilenumbruch;
				for($i=$var_StartCode; $i < count($var_file);$i++) {
					if($i > $var_EndCode) {
						break;
					}
					if($i == $param_row) {
						$var_marker = ' => ';
					} else {
						$var_marker = '    ';
					}
					if($i-1 >=0) {
						$var_result .= $i.$var_marker.self::$tab.$var_file[$i-1];
					}


				}
				$var_result .= '</php>';
			}
		}
		return $var_result;
	}

}
?>