0a3a9be by morcsik at 2009-07-03 1
<?php
2
/**
3
 * Function to turn the triggered errors into exceptions
4
 * @author troelskn at gmail dot com
5
 * @see http://php.net/manual/en/class.errorexception.php
6
 * @param $severity
7
 * @param $message
8
 * @param $filename
9
 * @param $lineno
b670eb0 by Marius Orcsik at 2009-07-10 10
 * @throws vscExceptionError
0a3a9be by morcsik at 2009-07-03 11
 * @return void
12
 */
13
function exceptions_error_handler ($iSeverity, $sMessage, $sFilename, $iLineNo) {
14
	if (error_reporting() == 0) {
15
		return;
16
	}
17
18
	if (error_reporting() & $iSeverity) {
19
		// the __autoload seems not to be working here
d3d77a4 by Marius Orcsik at 2010-01-29 20
		include_once(realpath(VSC_LIB_PATH . 'exceptions/vscexceptionerror.class.php'));
b670eb0 by Marius Orcsik at 2009-07-10 21
		throw new vscExceptionError ($sMessage, 0, $iSeverity, $sFilename, $iLineNo);
0a3a9be by morcsik at 2009-07-03 22
	}
23
}
24
25
/**
26
 * @return bool
27
 */
28
function isCli () {
29
	return (php_sapi_name() == 'cli');
30
}
31
841f128 by Marius Orcsik at 2011-10-10 32
if (!function_exists('d') ) {
0a3a9be by morcsik at 2009-07-03 33
function d () {
34
	$aRgs = func_get_args();
35
	$iExit = 1;
36
d4daabf by morcsik at 2009-08-30 37
	for ($i = 0; $i < ob_get_level(); $i++) {
38
		// cleaning the buffers
39
		ob_end_clean();
40
	}
41
0a3a9be by morcsik at 2009-07-03 42
	if (!isCli()) {
43
		// not running in console
44
		echo '<pre>';
45
	}
46
47
	foreach ($aRgs as $object) {
48
		// maybe I should just output the whole array $aRgs
f53ad23 by Marius Orcsik at 2010-05-27 49
		try {
50
			var_dump($object);
841f128 by Marius Orcsik at 2011-10-10 51
			if (isCli()) echo "\n\n";
52
		} catch (Exception $e) {
f53ad23 by Marius Orcsik at 2010-05-27 53
			//
54
		}
0a3a9be by morcsik at 2009-07-03 55
	}
903ffea by Marius Orcsik at 2009-09-02 56
	debug_print_backtrace();
0a3a9be by morcsik at 2009-07-03 57
58
	if (!isCli()) {
59
		// not running in console
60
		echo '</pre>';
61
	}
7ddfe2a by morcsik at 2009-08-30 62
	exit ();
0a3a9be by morcsik at 2009-07-03 63
}
841f128 by Marius Orcsik at 2011-10-10 64
}
0a3a9be by morcsik at 2009-07-03 65
66
/**
493b840 by Marius Orcsik at 2009-12-02 67
 * the __autoload automagic function for class instantiation,
0a3a9be by morcsik at 2009-07-03 68
 * @param string $className
69
 */
70
function __autoload ($className) {
8110edc by Marius Orcsik at 2011-04-20 71
	if (class_exists ($className, false)) {
0a3a9be by morcsik at 2009-07-03 72
		return true;
73
	}
d3d77a4 by Marius Orcsik at 2010-01-29 74
	$fileIncluded = false;
0a3a9be by morcsik at 2009-07-03 75
76
	$classNameLow = strtolower($className);
77
78
	$sFilePath	= $classNameLow . '.class.php';
d3d77a4 by Marius Orcsik at 2010-01-29 79
	if (stristr ($classNameLow, 'exception')) {
80
		$sExceptionsFilePath = 'exceptions' . DIRECTORY_SEPARATOR . $sFilePath;
8110edc by Marius Orcsik at 2011-04-20 81
		$fileIncluded = include ($sExceptionsFilePath);
d3d77a4 by Marius Orcsik at 2010-01-29 82
	}
83
	if (!$fileIncluded) {
8110edc by Marius Orcsik at 2011-04-20 84
		$fileIncluded = include ($sFilePath);
0a3a9be by morcsik at 2009-07-03 85
	}
6aeeed5 by Marius Orcsik at 2011-12-11 86
	if ( !$fileIncluded || ( !in_array ($className,get_declared_classes()) && !in_array($className,get_declared_interfaces() ) ) ) {
8110edc by Marius Orcsik at 2011-04-20 87
		include_once (VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexception.class.php');
88
		include_once (VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexceptionpath.class.php');
89
		include_once (VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexceptionautoload.class.php');
d3d77a4 by Marius Orcsik at 2010-01-29 90
06faa85 by Marius Orcsik at 2011-04-19 91
		$sExport = var_export(getPaths(),true);
afc4926 by marius orcsik at 2010-09-26 92
		throw new vscExceptionAutoload('Could not load class ['.$className.'] in path: <pre style="font-weight:normal">' . $sExport . '</pre>');
b103ac7 by Marius Orcsik at 2010-04-13 93
	}
6aeeed5 by Marius Orcsik at 2011-12-11 94
	return true;
0a3a9be by morcsik at 2009-07-03 95
}
96
ab70839 by Marius Orcsik at 2009-12-01 97
function getPaths () {
98
	return explode (PATH_SEPARATOR, get_include_path());
99
}
100
48bd88b by Marius Orcsik at 2010-01-29 101
function cleanBuffers ($iLevel = null) {
93a1746 by Marius Orcsik at 2010-05-28 102
	$sErrors = '';
48bd88b by Marius Orcsik at 2010-01-29 103
	if (is_null($iLevel))
104
		$iLevel = ob_get_level();
105
106
		for ($i = 0; $i < $iLevel; $i++) {
93a1746 by Marius Orcsik at 2010-05-28 107
		$sErrors .= ob_get_clean();
48bd88b by Marius Orcsik at 2010-01-29 108
	}
93a1746 by Marius Orcsik at 2010-05-28 109
	return $sErrors;
48bd88b by Marius Orcsik at 2010-01-29 110
}
111
969247e by Marius Orcsik at 2010-03-12 112
function addPath ($pkgPath, $sIncludePath = null) {
60591bd by Marius Orcsik at 2010-05-21 113
	// removing the trailing / if it exists
8110edc by Marius Orcsik at 2011-04-20 114
	if (substr($pkgPath,-1) == DIRECTORY_SEPARATOR) {
380df76 by Marius Orcsik at 2011-09-10 115
		$pkgPath = substr ($pkgPath,0, -strlen (DIRECTORY_SEPARATOR));
969247e by Marius Orcsik at 2010-03-12 116
	}
8bb48b1 by Marius Orcsik at 2009-11-29 117
969247e by Marius Orcsik at 2010-03-12 118
	if (is_null($sIncludePath)) {
119
		$sIncludePath 	= get_include_path();
8bb48b1 by Marius Orcsik at 2009-11-29 120
	}
121
60591bd by Marius Orcsik at 2010-05-21 122
	// checking to see if the path exists already in the included path
969247e by Marius Orcsik at 2010-03-12 123
	if (strpos ($sIncludePath, $pkgPath . PATH_SEPARATOR) === false) {
124
		set_include_path (
125
			$pkgPath . PATH_SEPARATOR .
126
			$sIncludePath
127
		);
128
	}
129
	return true;
8bb48b1 by Marius Orcsik at 2009-11-29 130
}
131
132
/**
133
 * Adds the package name to the include path
134
 * Also we are checking if an existing import exists, which would define some application specific import rules
135
 * @param string $sIncPath
136
 * @return bool
137
 * @throws vscExceptionPackageImport
138
 */
139
function import ($sIncPath) {
06faa85 by Marius Orcsik at 2011-04-19 140
	// fixing the paths to be fully compliant with the OS - indifferently how they are set
141
	$sIncPath	= str_replace(array('/','\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR),$sIncPath);
8bb48b1 by Marius Orcsik at 2009-11-29 142
	$bStatus 	= false;
143
	$sPkgLower 	= strtolower ($sIncPath);
969247e by Marius Orcsik at 2010-03-12 144
	$sIncludePath 	= get_include_path();
145
8bb48b1 by Marius Orcsik at 2009-11-29 146
	if (is_dir ($sIncPath)) {
969247e by Marius Orcsik at 2010-03-12 147
		return addPath ($sIncPath, $sIncludePath);
8bb48b1 by Marius Orcsik at 2009-11-29 148
	}
149
150
	$aPaths 		= explode(PATH_SEPARATOR, $sIncludePath);
60591bd by Marius Orcsik at 2010-05-21 151
	krsort ($aPaths);
8bb48b1 by Marius Orcsik at 2009-11-29 152
8110edc by Marius Orcsik at 2011-04-20 153
	// this definitely needs improvement
8bb48b1 by Marius Orcsik at 2009-11-29 154
	foreach ($aPaths as $sPath) {
8110edc by Marius Orcsik at 2011-04-20 155
		$pkgPath 	= $sPath . DIRECTORY_SEPARATOR . $sPkgLower;
156
		if (is_dir($pkgPath)) {
60591bd by Marius Orcsik at 2010-05-21 157
			$bStatus |= addPath ($pkgPath);
8bb48b1 by Marius Orcsik at 2009-11-29 158
		}
159
	}
ab70839 by Marius Orcsik at 2009-12-01 160
8bb48b1 by Marius Orcsik at 2009-11-29 161
	if (!$bStatus) {
60591bd by Marius Orcsik at 2010-05-21 162
		// to avoid an infinite loop, we include these execeptions manually
8110edc by Marius Orcsik at 2011-04-20 163
		include_once(VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexception.class.php');
164
		include_once(VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexceptionpath.class.php');
165
		include_once(VSC_LIB_PATH . 'exceptions'.DIRECTORY_SEPARATOR.'vscexceptionpackageimport.class.php');
d3d77a4 by Marius Orcsik at 2010-01-29 166
8bb48b1 by Marius Orcsik at 2009-11-29 167
		throw new vscExceptionPackageImport ('Bad package [' . $sIncPath . ']');
168
	} else {
169
		return true;
0a3a9be by morcsik at 2009-07-03 170
	}
171
}
172
173
174
function getDirFiles ( $dir, $showHidden = false){
175
	$files =  array();
176
	if (!is_dir($dir)){
177
		trigger_error('Can not find : '.$dir);
178
		return false;
179
	}
8bb48b1 by Marius Orcsik at 2009-11-29 180
0a3a9be by morcsik at 2009-07-03 181
	if ( $root = @opendir($dir) ){
182
		while ($file = readdir ($root)){
183
			if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;}
184
185
			if (substr($dir, -1) != '/') $dir .= '/';
186
187
			if( is_dir ($dir . $file) ){
188
				$files = array_merge($files, getDirFiles($dir . $file));
189
			} else {
190
				/*if ( stristr($file, 'tpl') )*/ $files[] = $dir . $file;
191
			}
192
		}
193
	}
194
	return $files;
48bd88b by Marius Orcsik at 2010-01-29 195
}
196
197
if (!function_exists('_e')) {
198
	function getErrorHeaderOutput ($e = null) {
199
		header ('HTTP/1.1 500 Internal Server Error');
200
		$sRet = '<?xml version="1.0" encoding="utf-8"?>';
201
		$sRet .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
202
		$sRet .= '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
203
		$sRet .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
204
		$sRet .= '<head>';
205
		$sRet .= '<style>ul {padding:0; font-size:0.8em} li {padding:0.2em;display:inline} address {position:fixed;bottom:0;}</style>';
48d3988 by marius orcsik at 2010-09-19 206
		$sRet .= '<title>Internal Error' . (!$e ? '' : ': '. substr($e->getMessage(), 0, 20) . '...') . '</title>';
48bd88b by Marius Orcsik at 2010-01-29 207
		$sRet .= '</head>';
208
		$sRet .= '<body>';
209
		$sRet .= '<strong>Internal Error' . (!$e ? '' : ': '. $e->getMessage()) . '</strong>';
7b6e239 by Marius Orcsik at 2011-04-19 210
		$sRet .= '<address>&copy; VSC</address>';
211
		$sRet .= '<ul><li><a href="#" onclick="p = document.getElementById(\'trace\'); if (p.style.display==\'block\') p.style.display=\'none\';else p.style.display=\'block\'; return false">toggle trace</a></li><li><a href="javascript: p = document.getElementById(\'trace\'); document.location.href =\'mailto:'.ROOT_MAIL.'?subject=Problems&body=\' + p.innerHTML; return false">mail me</a></li></ul>';
48bd88b by Marius Orcsik at 2010-01-29 212
213
		if ($e instanceof Exception)
214
			$sRet .= '<p style="font-size:.8em">Triggered in <strong>' . $e->getFile() . '</strong> at line ' . $e->getLine() .'</p>';
215
216
		$sRet .= '<pre style="position:fixed;bottom:2em;display:none;font-size:.8em" id="trace">';
217
218
		return $sRet;
219
	}
220
221
	function _e ($e) {
93a1746 by Marius Orcsik at 2010-05-28 222
		$sErrors = cleanBuffers();
48bd88b by Marius Orcsik at 2010-01-29 223
		header ('HTTP/1.1 500 Internal Server Error');
224
		echo getErrorHeaderOutput ($e);
225
		if (isDebug()) {
226
			echo $e ? $e->getTraceAsString() : '';
227
		}
93a1746 by Marius Orcsik at 2010-05-28 228
		if ($sErrors)
229
		echo '<p>' . $sErrors . '</p>';
48bd88b by Marius Orcsik at 2010-01-29 230
		echo '</pre>';
231
		echo '</body>';
232
		echo '</html>';
233
		exit (0);
234
	}
db80d90 by Marius Orcsik at 2010-04-09 235
}