class_mcReport.inc.php 4.97 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */
/**
  *	@filesource class_mcReport.inc.php
  * @category	mcClasses
  * @author		f@mensch.coop
  * @since		2010-12-20
  */
class mcReport {

	
	private static $Stack = array();

	protected static function add(mcReport $param_obj) {
		if(!array_key_exists(get_class($param_obj), self::$Stack)) {
			self::$Stack[get_class($param_obj)] = array();
		}
		self::$Stack[get_class($param_obj)][] = $param_obj;
	}

	/**
	 *
	 * @param array $param_config
	 */
	public static function distribute(array $param_config=null) {
		if($param_config !== null) {
//D::show($_SERVER);
			
			foreach($param_config as $kindOfReport => $whatToDo) {

				if(array_key_exists($kindOfReport, self::$Stack)) {

					$GENERAL_TEXT = mcCONST::LINEBREAK.date('d.m.Y  G:i:s').mcCONST::LINEBREAK;
					$GENERAL_TEXT .=  'File: '.mcCONST::TAB.$_SERVER['SCRIPT_NAME'].mcCONST::LINEBREAK;
					
					if(!empty ($_GET)) {
						$GENERAL_TEXT .=  'Url: '.mcCONST::TAB.urldecode(http_build_query($_GET)).mcCONST::LINEBREAK;
					}
					$GENERAL_TEXT .= 'PHP: '.mcCONST::TAB.PHP_VERSION.' ('.PHP_OS.')'.mcCONST::LINEBREAK;
					$GENERAL_TEXT .= 'User: '.mcCONST::TAB.$_SERVER['HTTP_USER_AGENT'].mcCONST::LINEBREAK;
					$GENERAL_TEXT .= mcCONST::LINEBREAK;


					
					foreach($whatToDo as $kindOfToDo => $params) {

						if($kindOfToDo == 'toScreen' ) {
							if(!empty ($params) && array_key_exists('text', $params)) {
								self::informUser($params['text']);
							} 
							
						} else if($kindOfToDo == 'toFile') {
							if(array_key_exists('file', $params)) {
								$Text = $GENERAL_TEXT;
								$Text .= 'KindOf: '.$kindOfReport.' ('.count(self::$Stack[$kindOfReport]).')'.mcCONST::LINEBREAK.mcCONST::LINEBREAK;
								$Text .= self::getTextMessageFrom($kindOfReport);

								if(array_key_exists('writemode', $params)) {
									$writemode = $params['writemode'];
								} else {
									$writemode = 'a';
								}

								mcFile::write($params['file'], $Text, $writemode);
							} 

						} else if($kindOfToDo == 'toMail') {
							if(array_key_exists('subject', $params)) {
								$MailSubject = $params['subject'];
							} else {
								$MailSubject = '';
							}
							
							if(array_key_exists('mail', $params)) {
								$MailAdr = $params['mail'];
							} else {
								break;
							}

							$MailMessage = $GENERAL_TEXT;
							$MailMessage .= self::getTextMessageFrom($kindOfReport);

							mcMail::mail($MailAdr, $MailSubject, $MailMessage);

						} else {
							D::li('kind of mcReport ('.$kindOfToDo.') not supported');
						}
					}
				}
			}
		}
	}

	private $Values = array();

	protected function setValues(array $param_values) {
		$this->Values = $param_values;
	}

	protected function getDOMElement(DOMDocument $DOMDocument) {
		$DOMElement = $DOMDocument->createElement(get_class($this));
		foreach($this->Values as $key => $value){
			$DOMElement->setAttribute($key,$value);
		}
		return $DOMElement;
	}

	private static $TextMessageReports = array();
	private static function getTextMessageFrom($param_KindOfReport) {
		if(array_key_exists($param_KindOfReport, self::$Stack)) {
			if(!array_key_exists($param_KindOfReport, self::$TextMessageReports)) {
				self::$TextMessageReports[$param_KindOfReport] = '';
				foreach(self::$Stack[$param_KindOfReport] as $mcReportObj) {
					self::$TextMessageReports[$param_KindOfReport] .= $mcReportObj->getTextMessage();
				}
			}
			return self::$TextMessageReports[$param_KindOfReport];
		} else {
			return false;
		}
	}

	protected $TextMessage = null;
	protected function getTextMessage() {
		if(is_null($this->TextMessage)) {
			$this->TextMessage = get_class($this);
		}
		return $this->TextMessage;
	}



	protected static function informUser($param_string = null) {
		if(is_null($param_string)) {
			$param_string = 'leider ist ein Fehler aufgetreten, der Admin wurde informiert, wir hoffen das er bald behoben ist ;) ';
		}
		/**
		 * @todo Achtung wie wird genau ein User informiert!!!!
		 */
		Error::newError('UserError',$param_string,'256');
	}

	protected static function getCodeFromRow($param_errfile, $param_errline, $param_offset=0) {
		$var_result = '';
		@$var_file = file($param_errfile);
		if($var_file === false) {
			$var_result .= 'could not read file'.mcCONST::LINEBREAK;
		} else {
			if(count($var_file) < $param_errline) {
				$var_result .= 'row not in the file'.mcCONST::LINEBREAK;
			} else {
				if($param_errline-$param_offset < 0) {
					$var_StartCode = 0;
				} else {
					$var_StartCode = $param_errline - $param_offset ;
				}

				$var_EndCode = $param_errline +  $param_offset;
				$var_result .= mcCONST::LINEBREAK.'<php>'.mcCONST::LINEBREAK;
				for($i=$var_StartCode; $i < count($var_file);$i++) {
					if($i > $var_EndCode) {
						break;
					}
					if($i == $param_errline) {
						$var_marker = ' => ';
					} else {
						$var_marker = '    ';
					}
					if($i-1 >=0) {
						$var_result .= $i.$var_marker.mcCONST::TAB.$var_file[$i-1];
					}
				}
				$var_result .= '</php>';
			}
		}
		return $var_result;
	}

}
?>