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
10
 * @throws vscExceptionError
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
20
		include_once(realpath(VSC_LIB_PATH . 'exceptions/vscexceptionerror.class.php'));
21
		throw new vscExceptionError ($sMessage, 0, $iSeverity, $sFilename, $iLineNo);
22
	}
23
}
24
25
/**
26
 * @return bool
27
 */
28
function isCli () {
29
	return (php_sapi_name() == 'cli');
30
}
31
32
if (!function_exists('d') ) {
33
function d () {
34
	$aRgs = func_get_args();
35
	$iExit = 1;
36
37
	for ($i = 0; $i < ob_get_level(); $i++) {
38
		// cleaning the buffers
39
		ob_end_clean();
40
	}
41
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
49
		try {
50
			var_dump($object);
51
			if (isCli()) echo "\n\n";
52
		} catch (Exception $e) {
53
			//
54
		}
55
	}
56
	debug_print_backtrace();
57
58
	if (!isCli()) {
59
		// not running in console
60
		echo '</pre>';
61
	}
62
	exit ();
63
}
64
}
65
66
/**
67
 * the __autoload automagic function for class instantiation,
68
 * @param string $className
69
 */
70
function __autoload ($className) {
71
	if (class_exists ($className, false)) {
72
		return true;
73
	}
74
	$fileIncluded = false;
75
76
	$classNameLow = strtolower($className);
77
78
	$sFilePath	= $classNameLow . '.class.php';
79
	if (stristr ($classNameLow, 'exception')) {
80
		$sExceptionsFilePath = 'exceptions' . DIRECTORY_SEPARATOR . $sFilePath;
81
		$fileIncluded = include ($sExceptionsFilePath);
82
	}
83
	if (!$fileIncluded) {
84
		$fileIncluded = include ($sFilePath);
85
	}
86
	if ( !$fileIncluded || ( !in_array ($className,get_declared_classes()) && !in_array($className,get_declared_interfaces() ) ) ) {
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');
90
91
		$sExport = var_export(getPaths(),true);
92
		throw new vscExceptionAutoload('Could not load class ['.$className.'] in path: <pre style="font-weight:normal">' . $sExport . '</pre>');
93
	}
94
	return true;
95
}
96
97
function getPaths () {
98
	return explode (PATH_SEPARATOR, get_include_path());
99
}
100
101
function cleanBuffers ($iLevel = null) {
102
	$sErrors = '';
103
	if (is_null($iLevel))
104
		$iLevel = ob_get_level();
105
106
		for ($i = 0; $i < $iLevel; $i++) {
107
		$sErrors .= ob_get_clean();
108
	}
109
	return $sErrors;
110
}
111
112
function addPath ($pkgPath, $sIncludePath = null) {
113
	// removing the trailing / if it exists
114
	if (substr($pkgPath,-1) == DIRECTORY_SEPARATOR) {
115
		$pkgPath = substr ($pkgPath,0, -strlen (DIRECTORY_SEPARATOR));
116
	}
117
118
	if (is_null($sIncludePath)) {
119
		$sIncludePath 	= get_include_path();
120
	}
121
122
	// checking to see if the path exists already in the included path
123
	if (strpos ($sIncludePath, $pkgPath . PATH_SEPARATOR) === false) {
124
		set_include_path (
125
			$pkgPath . PATH_SEPARATOR .
126
			$sIncludePath
127
		);
128
	}
129
	return true;
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) {
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);
142
	$bStatus 	= false;
143
	$sPkgLower 	= strtolower ($sIncPath);
144
	$sIncludePath 	= get_include_path();
145
146
	if (is_dir ($sIncPath)) {
147
		return addPath ($sIncPath, $sIncludePath);
148
	}
149
150
	$aPaths 		= explode(PATH_SEPARATOR, $sIncludePath);
151
	krsort ($aPaths);
152
153
	// this definitely needs improvement
154
	foreach ($aPaths as $sPath) {
155
		$pkgPath 	= $sPath . DIRECTORY_SEPARATOR . $sPkgLower;
156
		if (is_dir($pkgPath)) {
157
			$bStatus |= addPath ($pkgPath);
158
		}
159
	}
160
161
	if (!$bStatus) {
162
		// to avoid an infinite loop, we include these execeptions manually
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');
166
167
		throw new vscExceptionPackageImport ('Bad package [' . $sIncPath . ']');
168
	} else {
169
		return true;
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
	}
180
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;
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>';
206
		$sRet .= '<title>Internal Error' . (!$e ? '' : ': '. substr($e->getMessage(), 0, 20) . '...') . '</title>';
207
		$sRet .= '</head>';
208
		$sRet .= '<body>';
209
		$sRet .= '<strong>Internal Error' . (!$e ? '' : ': '. $e->getMessage()) . '</strong>';
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>';
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) {
222
		$sErrors = cleanBuffers();
223
		header ('HTTP/1.1 500 Internal Server Error');
224
		echo getErrorHeaderOutput ($e);
225
		if (isDebug()) {
226
			echo $e ? $e->getTraceAsString() : '';
227
		}
228
		if ($sErrors)
229
		echo '<p>' . $sErrors . '</p>';
230
		echo '</pre>';
231
		echo '</body>';
232
		echo '</html>';
233
		exit (0);
234
	}
235
}