* various small changes
[vsc.git] / _res / _libs / functions.inc.php
blobda3453563883ade7e9cae0b2ec64784c527b829d
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 ErrorException
11 * @return void
13 function exceptions_error_handler ($severity, $message, $filename, $lineno) {
14 if (error_reporting() == 0) {
15 return;
18 if (error_reporting() & $severity) {
19 throw new ErrorException ($message, 0, $severity, $filename, $lineno);
23 /**
24 * @return bool
26 function isCli () {
27 return (php_sapi_name() == 'cli');
30 /**
31 * returns an end of line, based on the environment
32 * @return string
34 function nl () {
35 return isCli() ? "\n" : '<br/>'. "\n";
38 /**
39 * Removes all extra spaces from a string
40 * @param string $s
41 * @return string
43 function alltrim ($s) {
44 return trim(preg_replace('/\s+/', ' ', $s));
47 function d () {
48 $aRgs = func_get_args();
49 $iExit = 1;
51 if (!isCli()) {
52 // not running in console
53 echo '<pre>';
55 foreach ($aRgs as $object) {
56 var_dump($object);
59 if (!isCli()) {
60 // not running in console
61 echo '</pre>';
63 die ();
66 /**
67 * @see usingPackage
68 * @param string $sPackageName
69 * @return bool
71 function import ($sPackageName) {
72 return usingPackage($sPackageName);
74 /**
75 * Adds the package name to the include path
76 * @todo make sure that the path isn't aleady in the include path
77 * @param string $packageName
78 * @return bool
79 * @throws tsExceptionPackageImport
81 function usingPackage ($packageName) {
82 $pkgLower = strtolower($packageName);
83 $pkgPath = LIB_PATH . DIRECTORY_SEPARATOR . $pkgLower;
85 $path = get_include_path();
86 if (is_dir($pkgPath)) {
87 if (strpos ($path, $pkgPath) === false) {
88 // adding exceptions dir to include path if it exists
89 if (is_dir ($pkgPath. DIRECTORY_SEPARATOR . 'exceptions')) {
90 // adding the exceptions if they exist
91 $pkgPath .= PATH_SEPARATOR . $pkgPath . DIRECTORY_SEPARATOR . 'exceptions';
93 set_include_path(
94 $path . PATH_SEPARATOR .
95 $pkgPath
98 return true;
99 } else {
100 usingPackage ('coreexceptions');
101 throw new tsExceptionPackageImport ("bad package " . $packageName);
105 function usingClass ($className) {
106 if (class_exists ($className, false)) {
107 return true;
110 $classNameLow = strtolower($className);
112 $sFilePath = $classNameLow . '.class.php';
113 $fileIncluded = @include ($sFilePath);
114 if ( !$fileIncluded ) {
115 $sFilePath = $classNameLow . DIRECTORY_SEPARATOR . $sFilePath;
116 $fileIncluded = @include ($sFilePath);
119 return $fileIncluded;
123 * the __autoload automagic function for class initialization,
124 * see usingClass
126 * @param string $className
128 function __autoload ($className) {
129 if (class_exists ($className, false)) {
130 return true;
132 return usingClass ($className);
136 * returns true if the user's ip is in our list of debug ips
138 * @return bool
140 function isDebug (){
141 if (stristr(C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR']) && C_SYSTEM_DEBUG)
142 return true;
144 return false;
147 if (!function_exists('usingClass')) {
149 * The php4 function for including the class file
151 * @param string $className
152 * @return bool
154 function usingClass($className) {
155 if (class_exists($className)) {
156 return true;
159 $classNameLow = strtolower($className);
162 $classPaths = array (
163 $classNameLow . '.class.php', // regular homegrown class
164 $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page
165 $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported
168 foreach ($classPaths as $classPath) {
169 $fileIncluded = @include($classPath);
171 if ($fileIncluded) {
172 return true;
176 if (!$fileIncluded) {
177 trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR);
178 return false;
181 if (!class_exists($className)) {
182 trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR);
183 return false;
189 * Dumb function for email validation
191 * @param string $address
192 * @return bool
194 function emailIsValid ($address) {
195 return (
196 ereg(
197 '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
198 $address
203 function getDirFiles ( $dir, $showHidden = false){
205 $files = array();
206 if (!is_dir($dir)){
207 trigger_error('Can not find : '.$dir);
208 return false;
210 if ( $root = @opendir($dir) ){
211 while ($file = readdir ($root)){
212 if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;}
214 if (substr($dir, -1) != '/') $dir .= '/';
216 if( is_dir ($dir . $file) ){
217 $files = array_merge($files, getDirFiles($dir . $file));
218 } else {
219 /*if ( stristr($file, 'tpl') )*/ $files[] = $dir . $file;
223 return $files;
226 function isDBLink($incData) {
227 if (sqlFactory::validType(DB_TYPE) && is_resource($incData) && stristr(get_resource_type($incData),DB_TYPE)) {
228 return true;
230 return false;
233 if (!function_exists('mime_content_type')) {
234 function mime_content_type ($filename) {
235 $t = getimagesize($filename);
237 if (is_array($t))
238 return $t['mime'];
239 else
240 return 'application/unknown';