Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// $Id: bug-561101.php 198623 2005-10-17 18:37:50Z jeichorn $
3
 
4
/**
5
* Class ClubBase
6
*
7
* Basisklasse, die Debugging- und ErrorHandling/Logging-Funktionen liefert
8
* Alle komplexeren Klassen sollten von ClubBase abgeleitet werden.
9
*
10
* @link      http://www.swr3clubde
11
* @package   tests SWR3.online-Edition
12
* @version   $Revision: 198623 $ ($Date: 2005-10-17 13:37:50 -0500 (Mon, 17 Oct 2005) $),
13
* @copyright Copyright (c) 2001 SWR3.online. All rights reserved.
14
* @author    Karsten Kraus <kk@swr3.de>
15
* @access    private
16
*/
17
 
18
///////////////////////////// REQUIRES //////////////////////////////////////////////////////////////
19
/**
20
* @var const 	CLUB_CLASS_PATH		Der Pfad zu den Club-Klassen
21
*/
22
if(!defined('CLUB_CLASS_PATH')) @define('CLUB_CLASS_PATH', '/wwwhome/swr3club.de/php.inc/swr3club/');
23
/**
24
* In dieser Datei werden Pfade zu den verschiedenen Spezial-Klassen
25
* (storages, products, commands) definiert
26
* include		Function _require_once_
27
*/
28
@require_once(CLUB_CLASS_PATH . 'config/paths.conf.php');
29
/**
30
* In dieser Datei werden Konstanten, die fuer alle Klassen gelten definiert
31
* include		Function _require_once_
32
*/
33
@require_once(CLUB_CONFIG_PATH . 'common.conf.php');
34
/**
35
* ClubBase erbt von PEAR.php, deshalb binden wir sie hier ein
36
* include		Function _require_once_
37
*/
38
@require_once(CLUB_PEAR_PATH . 'PEAR.php');
39
/**
40
* Für das Logging verwenden wir erstmal die Standard PEAR::Log-Klasse
41
*/
42
@require_once(CLUB_PEAR_PATH . 'Log.php');
43
////////////////////////////////////////////////////////////////////////////////////////////////////
44
 
45
/**
46
* _ClubDebugOptions	globale Debugging-Einstellungen fuer alle Klassen
47
*
48
* Sichert die Werte, wenn ClubBase::setdebug() aufgerufen wird.
49
* Die hier gesetzten Werte gelten fuer ALLE Klassen, die keine eigenen
50
* Werte uerber $class->setdebug() gesetzt haben
51
*
52
* @access	private
53
* @var 		array	$ClubDebugOptions	array('level' => 0, 'hide' => true, 'file' = '')
54
* @see		ClubBase::set_debug, ClubBase::_DEBUG()
55
*/
56
$_ClubDebugOptions = array('level' => null, 'hide' => true, 'file' => '', 'print' => true, 'flush' => false);
57
 
58
/**
59
* DEBUG_XXX	Konstanten, die bestimmte Debuglevel definieren
60
*
61
* Diese Konstanten dienen einfach dazu, den Quellkode lesbarer zu machen.
62
* Ob das Debuglevel als Zahl angeben wird, oder mit einer Konstante, ist
63
* letztlich egal.
64
* Die Konstanten decken immer Bereiche ab (z.B.  DEBUG_INFO = 6, DEBUG_NOTICE = 9)
65
define('DEBUG_ERROR', 1);
66
*/
67
@define('DEBUG_ERROR', 3);
68
 
69
/**
70
* @const	DEBUG_INFO		wichtige Zusatz-Informationen
71
*/
72
@define('DEBUG_INFO', 5);
73
/**
74
* @const	DEBUG_NOTICE	Details, die nur selten angezeigt werden muessen
75
*/
76
define('DEBUG_NOTICE', 6);
77
 
78
@define('DEBUG_INTERNALS', 7);
79
 
80
/**
81
* @const	DEBUG_SQL		alle Ausgaben, die (lange) SQL-Statements ausgeben
82
*/
83
@define('DEBUG_SQL', 8);
84
/**
85
* @const 	DEBUG_CTRACE	 zeigt Aufrufe (Init der Klassen) an
86
*/
87
@define('DEBUG_CTRACE', 9);
88
/**
89
* @const	DEBUG_MTRACE	zeigt Aufrufe von Methoden/Funktionen an
90
*/
91
@define('DEBUG_MTRACE', 10);
92
/**
93
* @const	PHPDOCUMENTOR_DEBUG_ALL	Alles anzeigen
94
*/
95
define('DEBUG_ALL', 11);
96
 
97
// Schwerer Fehler
98
define('DEBUG_FATAL', 0);
99
 
100
 
101
// ----------------------------------------------------------------------------------------------------
102
 
103
class ClubBase extends PEAR {
104
 
105
    /**
106
    * $_bDebugLevel	Debug-Level
107
    *
108
    * Diese Variablen speichern die Debugging-Einstellungen
109
    * innerhalb des Objekts.
110
    * Sie ueberschreiben ggf. die Werte in _ClubDebugOptions
111
    *
112
    * @access	private
113
    * @var	integer	$_bDebugLevel
114
    * @final
115
    * @see	$_ClubDebugOptions, setdebug(), _PHPDOCUMENTOR_DEBUG()
116
    */
117
    var $_bDebugLevel = -1;
118
 
119
    /**
120
    * $__bHideDebug	Debugausgabe in HTMl-Kommentare setzen?
121
    *
122
    * @brother	_bDebugLevel
123
    * @access	private
124
    * @var		boolean	$_bHideDebug
125
    */
126
    var $_bHideDebug = true;
127
 
128
    /**
129
    * $_sLogFile	Logfile, in der Debugmeldungen geschrieben werden
130
    *
131
    * @brother	_bDebugLevel
132
    * @access	private
133
    * @var		string	$_sLogFile
134
    */
135
    var $_sLogFile = '';
136
 
137
    /**
138
    * $_bPrintDebug	Sollen Meldungen auch in HTM geschrieben werden, wenn sie in Datei geloggt werden?
139
    *
140
    * @brother	_bDebugLevel
141
    * @access	private
142
    * @var		boolean	$_bPrintDebug
143
    */
144
    var $_bPrintDebug = true;
145
 
146
    var $_bPrintFlush = false;
147
 
148
 
149
    /**
150
    * ClubBase()    Der Konstruktor
151
	*
152
	* Macht nicht viel mehr, als fuer alle abgeleiteten Klassen
153
	* eine Debug-Meldung abzusetzten und PEAR-Funktionen einzubinden
154
	*
155
	*
156
    * @access    public
157
    */
158
    function ClubBase() {
159
        $this->_PHPDOCUMENTOR_DEBUG("Initialisiere Klasse " . phpDocumentor_get_class($this), PHPDOCUMENTOR_DEBUG_CTRACE);
160
	$this->PEAR();
161
    }
162
 
163
    /**
164
    * getProperty()	Gibt den Wert einer Eigenschaft zurueck
165
    *
166
    * Es werden nur 'oeffentliche' Eigenschaften (ohne '_' davor) zurueckgegeben/bearbeitet
167
    * Wird ein optionales 'Target' angegeben wird versucht den Werte eines
168
    * Hashes auszulesen (z.B. $class->$target[$property]) bzw. zu schreiben
169
    *
170
    * @access       public
171
    * @param        string	$property		Name der Eigenschaft/Schluessel
172
    * @param		string	$target			Name des Targets
173
    * @return       mixed					Wert der Eigenschaft/Schluessels
174
	* @see	getProperty(), getAllProperties(), setProperty(), getPropType()
175
    */
176
    function getProperty($property, $target = '', $index = 0, $simplify = false) {
177
	$allvars = get_object_vars($this);
178
	$prop = (empty($target)) ? $property : $target;
179
	$key = (empty($target)) ? null : $property;
180
	if(!in_array($prop, @array_keys($allvars))) {
181
	    // Fehlerbehandlung
182
	    return $this->_ERROR("Eigenschaft $prop gibt's net");
183
	}
184
	if(eregi('^_', $prop) or (isset($key) and eregi('^_', $key))) {
185
	    // Fehlerbehandlung
186
    	    $this->_ERROR("Auf private Variablen darf nicht zugegriffen werden");
187
	}
188
	if(isset($key) and !in_array($key, array_keys($allvars[$prop]), true)) {
189
	    if(isset($allvars[$prop][$index])) {
190
		if(in_array($key, @array_keys($allvars[$prop][$index]))) {
191
		    return $allvars[$prop][$index][$key];
192
		}
193
	    }
194
	    // Fehlerbehandlung
195
	    return $this->_ERROR("Eigenschaft $prop hat den Schluessel $key nicht");
196
	}
197
	$val = (isset($key)) ? $allvars[$prop][$key] : $allvars[$prop];
198
	if($simplify) {
199
	    if(isset($val[0]) and count($val) < 2) $val = $val[0];
200
	}
201
	return $val;
202
    }
203
 
204
    /**
205
    * getAllProperties()	Gibt die Wert aller Eigenschaften einer Klasse zurueck
206
    *
207
    * Es werden nur 'oeffentliche' Eigenschaften (ohne '_' davor) zurueckgegeben/bearbeitet
208
    * Wird ein optionales 'Target' angegeben wird versucht den Werte eines
209
    * Hashes auszulesen (z.B. $class->$target[$property]) bzw. zu schreiben
210
    *
211
    * @access		public
212
    * @param		array	$exclude	diese Eigenschaften werden ignoriert
213
    * @return       array				Array mit den Namen der Eigenschaften als Schluesseln
214
	* @see	getProperty(), getAllProperties(), setProperty(), getPropType()
215
    */
216
    function getAllProperties($simplify = false, $exclude = array()) {
217
	if(isset($exclude) and !is_array($exclude)) $exclude = array($exclude);
218
	$allvars = get_object_vars($this);
219
	$props = array();
220
	    while(list($key, $val) = each($allvars)) {
221
		if(!isset($key) or eregi('^_', $key) or in_array($key, $exclude)) continue;
222
		if(is_array($val)) {
223
		if($simplify) {
224
		    if(isset($val[0]) and count($val) < 2) $val = $val[0];
225
		}
226
	    }
227
	    $props[$key] = $val;
228
	}
229
	return $props;
230
    }
231
 
232
    /**
233
    * setProperty()	Setzt den Wert einer Eigenschaft
234
    *
235
    * @brother		getProperty()
236
	* @access		public
237
    * @param		mixed	$value	Wert der der Eigenschaft zugewiesen werden soll
238
    * @return       mixed			true im Erfolgsfall, sonst Fehler
239
    */
240
    function setProperty($property, $value, $target = '', $index = 0, $force = false) {
241
	$oldval = $this->getProperty($property, $target, $index);
242
	// Fehlerbehandlung
243
	if(isset($oldval) and (gettype($value) != gettype($oldval)) and !$force) {
244
	// Fehlerbehandlung
245
	    return $this->_ERROR("Falscher Typ (" . gettype($value) . ") fuer '$property' (" . gettype($oldval) . ")!");
246
	} else {
247
	if(!empty($target)) {
248
	    $temp = &$this->$target;
249
	    if(isset($temp[$property]) or $force) {
250
	        $temp[$property] = $value;
251
	    } elseif((isset($temp[$index]) and isset($temp[$index][$property]))) {
252
		    $temp[$index][$property] = $value;
253
	    } else {
254
		return $this->_ERROR("Eigenschaft $property gibt es nicht");
255
	    }
256
	} else {
257
	    $this->$property = $value;
258
	}
259
	    return true;
260
	}
261
    }
262
 
263
	/**
264
	* getPropType	gibt den Datentyp eines Objekt-Eigenschaft zurueck.
265
	*
266
	* @access	public
267
	* @brother	getProperty()
268
	*
269
	* @return	string	Datentyp der Eigenschaft
270
	*/
271
    function getPropType($property, $target = null, $index = 0) {
272
	$val = $this->getProperty($property, $target, $index);
273
	// Fehlerbehandlung
274
	return gettype($val);
275
    }
276
 
277
 
278
    /**
279
    * set_debug()   Debug-Level fuer die Klasse setzen
280
	*
281
	* Wird diese Methode statisch (ClubBase::set_debug()) aufgerufen, dann wird die globale Variable
282
	* $_ClubDebugOptions gesetzt.
283
	* Diese gilt fuer ALLE KLASSEN, die keine eigenen Werte mit $obj->set_debug() gestzt haben.
284
	*
285
    * @access    public
286
	* @static
287
	*
288
    * @param     integer    $level	Level, bis zu dem Debugausgaben angezeigt werden
289
	* @param	 boolean	$hide	Sollen Debugausgaben im HTML in Kommentare gepackt werden?
290
	* @param	 string		$file	Soll in eine Datei geschrieben werden (null = nein)
291
	* @param	 boolean	$print  Sollen Kommentare in HTML geschrieben werden,
292
	*								auch wenn schon in Datei geloggt wird?
293
	*
294
	* @global	 array		Globale Debugging-Einstellungen
295
	* @see		 $_bDebugLevel, $_bHideDebug, $_sLogFile, $_bPrintDebug
296
	* @see		 $_ClubDebugOptions, ClubBase::set_debug(), ClubBase::_PHPDOCUMENTOR_DEBUG()
297
    */
298
    function setDebug($level, $hide = true, $file = '', $print = true, $flush = false) {
299
	if(!empty($file)) $file = (is_string($file)) ? $file : CLUB_LOG_FILE;
300
	if(!isset($this)) {
301
	    global $_ClubDebugOptions;
302
	    $_ClubDebugOptions = array(
303
		'level' => (integer)$level,
304
		'hide' => (boolean)$hide,
305
		'file' => (string)$file,
306
		'print' => (boolean)$print,
307
                'flush' => (boolean)$flush
308
	    );
309
	    return;
310
	}
311
	if($this->_bDebugLevel < 0) $this->_bDebugLevel = 0;
312
        $this->_bDebugLevel = empty($level) ? !(boolean)$this->_bDebugLevel : (integer)$level;
313
	$this->_bHideDebug = (boolean)$hide;
314
	$this->_sLogFile = (string)$file;
315
	$this->_bPrintDebug = (boolean)$print;
316
        $this->_bPrintFlush = (boolean)$flush;
317
    }
318
 
319
    function printFlush() {
320
        $args = func_get_args();
321
        if(empty($args)) $args = array(' ');
322
        eval("printf(\"% 256s\", sprintf(\"" . implode("\", \"", $args) . "\"));");
323
        flush();
324
    }
325
 
326
    function printVar($var, $info = '') {
327
	if (ClubBase::isError(ClubBase::loadClass('var_dump', CLUB_CLASS_PATH))) return false;
328
	if (!empty($info)) print "<H2>$info</H2>";
329
	Var_Dump::display($var);
330
        return true;
331
    }
332
 
333
    function loadClass($classname, $path = './', $ext = '.class.inc', $require = false) {
334
	ClubBase::_PHPDOCUMENTOR_DEBUG("ClubBase::loadclass($path, $classname, $ext)", PHPDOCUMENTOR_DEBUG_MTRACE);
335
	if(empty($classname) or empty($ext))  {
336
	    return ClubBase::_ERROR("Fehlende Daten: (Pfad: $path, Klasse: $classname, Ext.: $ext)");
337
	}
338
	if(!in_array(strtolower($classname), get_declared_classes())) {
339
	    ClubBase::_PHPDOCUMENTOR_DEBUG("Klasse '$classname' noch nicht deklariert, lade neu...", PHPDOCUMENTOR_DEBUG_NOTICE);
340
	    $classfile = $path . $classname . $ext;
341
	    if(!file_exists($classfile) or !is_readable($classfile)) {
342
	        //Fehlerbehandlung
343
		return ClubBase::_ERROR("Kann Klasse '$classfile' nicht laden");
344
	    } elseif($require) {
345
		if(!@require_once($classfile)) {
346
		    //Fehlerbehandlung
347
		    return ClubBase::_ERROR('Kann Command nicht includieren');
348
		}
349
	    } elseif(!@include_once($classfile)) {
350
		//Fehlerbehandlung
351
		ClubBase::_ERROR("Kann Klasse '$classfile' nicht includieren");
352
	    }
353
	} else {
354
	    ClubBase::_PHPDOCUMENTOR_DEBUG('Klasse schon deklariert.', PHPDOCUMENTOR_DEBUG_INTERNALS);
355
	}
356
	return true;
357
    }
358
 
359
    /**
360
    * _PHPDOCUMENTOR_DEBUG()   Debugmeldungen ausgeben
361
    *
362
    * Mit dieser Funktion koennen alle Klassen einheitlich Debug-Meldungnen ausgeben.
363
	*
364
    * @access    public
365
    * @static
366
	*
367
	 @todo		 die() bei Level -999 rausnehmen, wenn Error-Handling implmentiert
368
	*
369
    * @param     string		$message	Meldungstext
370
	* @param	 integer	$level		das Debuglevel der Meldung
371
	* @global	 array		Globale Debugging-Einstellungen
372
	* @see		 $_bDebugLevel, $_bHideDebug, $_sLogFile, $_bPrintDebug
373
	* @see		 $_ClubDebugOptions, ClubBase::set_debug(), ClubBase::_PHPDOCUMENTOR_DEBUG()
374
    */
375
    function _PHPDOCUMENTOR_DEBUG($message, $level = PHPDOCUMENTOR_DEBUG_INFO) {
376
	static $called = 0; ++$called;
377
	global $_ClubDebugOptions;
378
	$blevel = 0; $hide = true; $file = null; $print = false;
379
	if(isset($_ClubDebugOptions['level'])) {
380
		$blevel = $_ClubDebugOptions['level'];
381
		$hide = $_ClubDebugOptions['hide'];
382
		$file = $_ClubDebugOptions['file'];
383
		$print = $_ClubDebugOptions['print'];
384
                $flush = $_ClubDebugOptions['flush'];
385
 
386
	}
387
       	if(isset($this) and isset($this->_bDebugLevel) and $this->_bDebugLevel >= 0) {
388
			$blevel = $this->_bDebugLevel;
389
			$hide = $this->_bHideDebug;
390
			$file = $this->_sLogFile;
391
			$print = $this->_bPrintDebug;
392
                        $flush = $this->_bPrintFlush;
393
	}
394
	$class = (isset($this)) ? phpDocumentor_get_class($this) ."($called) -" : '';
395
	if($level <= $blevel) {
396
	    $output = "$class $message ($level/$blevel)";
397
	    if($print) {
398
                $output = ($hide) ? "<!-- " . $output : "<HR size='1' noshade><PRE><B>" . $output;
399
		$output .= ($hide) ? " -->\n" : "</PRE><HR size='1' noshade><B>\n";
400
                ($flush) ? ClubBase::printFlush($output) : print $output;
401
	    }
402
	    if(!empty($file)) {
403
		$file = (!is_string($file)) ? CLUB_LOG_FILE : $file;
404
		$log = &Log::singleton('file', $file);
405
		$log->log($output, $level);
406
	    }
407
	}
408
	if($level === PHPDOCUMENTOR_DEBUG_FATAL) {
409
	    trigger_error($message, E_USER_ERROR);
410
	}
411
    }
412
 
413
    /**
414
    * _ERROR()   Fehler registrieren und ggf. zur Debug-Ausgabe weiterleiten
415
	*
416
	* Dies muss erst noch implementiert werden ;-))
417
	 @todo		implement this
418
	*
419
    * @access    public
420
    * @final
421
    * @static
422
    */
423
    function _ERROR ($message = 'unknown error', $code = PHPDOCUMENTOR_DEBUG_ERROR,
424
                     $mode = null, $options = null, $userinfo = null, $error_class = null)
425
    {
426
	$msg = ClubBase::isError($message) ? $message->getMessage() : $message;
427
        (isset($this)) ? $this->_PHPDOCUMENTOR_DEBUG($msg, PHPDOCUMENTOR_DEBUG_ERROR) : ClubBase::_PHPDOCUMENTOR_DEBUG($msg, $code);
428
        if(!isset($this) and empty($error_class)) $error_class = 'PEAR_Error';
429
        return (isset($this))
430
                    ? $this->raiseError($message, $code, $mode, $options, $userinfo)
431
                    : ClubBase::raiseError($message, $code, $mode, $options, $userinfo, $error_class);
432
    }
433
}
434
 
435
 
436
?>