* small changes to make the unittesting slightly more easy
[vsc.git] / _res / _libs / functions.inc.php
blobbaccaf5afc624083c6db271c502b7a5d0f359354
1 <?php
2 /**
3 * Function to turn the triggered errors into exceptions
4 * @author troelskn at gmail dot com
5 * @from 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 unknown_type
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 function import ($sPackageName) {
24 return usingPackage($sPackageName);
27 function usingPackage ($packageName) {
28 $pkgLower = strtolower($packageName);
29 $pkgPath = LIB_PATH . DIRECTORY_SEPARATOR . $pkgLower;
31 $path = get_include_path();
32 if (is_dir($pkgPath) && !strpos ($pkgPath, $path)) {
33 set_include_path(
34 $pkgPath . PATH_SEPARATOR .
35 $path
37 return true;
38 } else {
39 import ('coreexceptions');
40 throw new PackageImportException ("bad package " . $packageName);
44 function usingClass ($className) {
45 if (class_exists ($className, false)) {
46 return true;
49 $classNameLow = strtolower($className);
51 try {
52 // regular package classes
53 // echo $classNameLow . '.class.php<br/>' ;
54 $fileIncluded = include ($classNameLow . '.class.php');
55 } catch (ErrorException $e) {
56 // classes inside page path
57 try {
58 // echo $classNameLow . DIRECTORY_SEPARATOR . $classNameLow . '.class.php<br/>';
59 $fileIncluded = include ($classNameLow . DIRECTORY_SEPARATOR . $classNameLow . '.class.php');
60 } catch (ErrorException $e) {
61 import ('coreexceptions');
62 throw new tsExceptionAutoload ('Not found file containing '. $className .' in path ' . get_include_path());
65 return $fileIncluded;
68 /**
69 * the __autoload automagic function for class initialization,
70 * see usingClass
72 * @param string $className
74 function __autoload ($className) {
75 return usingClass ($className);
78 /**
79 * returns true if the user's ip is in our list of debug ips
81 * @return bool
83 function isDebug (){
84 if (stristr(C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR']) && C_SYSTEM_DEBUG)
85 return true;
87 return false;
90 if (!function_exists('usingClass')) {
91 /**
92 * The php4 function for including the class file
94 * @param string $className
95 * @return bool
97 function usingClass($className) {
98 if (class_exists($className)) {
99 return true;
102 $classNameLow = strtolower($className);
105 $classPaths = array (
106 $classNameLow . '.class.php', // regular homegrown class
107 $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page
108 $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported
111 foreach ($classPaths as $classPath) {
112 $fileIncluded = @include($classPath);
114 if ($fileIncluded) {
115 return true;
119 if (!$fileIncluded) {
120 trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR);
121 return false;
124 if (!class_exists($className)) {
125 trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR);
126 return false;
131 * Dumb function for email validation
133 * @param string $address
134 * @return bool
136 function emailIsValid ($address) {
137 return (
138 ereg(
139 '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
140 $address
145 function getDirFiles ( $dir, $showHidden = false){
147 $files = array();
148 if (!is_dir($dir)){
149 trigger_error('Can not find : '.$dir);
150 return false;
152 if ( $root = @opendir($dir) ){
153 while ($file = readdir ($root)){
154 if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;}
156 if (substr($dir, -1) != '/') $dir .= '/';
158 if( is_dir ($dir . $file) ){
159 $files = array_merge($files, getDirFiles($dir . $file));
160 } else {
161 /*if ( stristr($file, 'tpl') )*/ $files[] = $dir . $file;
165 return $files;
168 function isDBLink($incData) {
169 if (sqlFactory::validType(DB_TYPE) && is_resource($incData) && stristr(get_resource_type($incData),DB_TYPE)) {
170 return true;
172 return false;
175 if (!function_exists('mime_content_type')) {
176 function mime_content_type ($filename) {
177 $t = getimagesize($filename);
179 if (is_array($t))
180 return $t['mime'];
181 else
182 return 'application/unknown';