* simplified a bit the __call method for getters/setters
[vsc.git] / _res / _libs / functions.inc.php
blob0792f55b0f64d749214a188e2db5c402e0af1c51
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);
113 $fileIncluded = @include ($classNameLow . '.class.php');
114 if ( !$fileIncluded ) {
115 $fileIncluded = @include ($classNameLow . DIRECTORY_SEPARATOR . $classNameLow . '.class.php');
117 return $fileIncluded;
121 * the __autoload automagic function for class initialization,
122 * see usingClass
124 * @param string $className
126 function __autoload ($className) {
127 if (class_exists ($className, false)) {
128 return true;
130 return usingClass ($className);
134 * returns true if the user's ip is in our list of debug ips
136 * @return bool
138 function isDebug (){
139 if (stristr(C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR']) && C_SYSTEM_DEBUG)
140 return true;
142 return false;
145 if (!function_exists('usingClass')) {
147 * The php4 function for including the class file
149 * @param string $className
150 * @return bool
152 function usingClass($className) {
153 if (class_exists($className)) {
154 return true;
157 $classNameLow = strtolower($className);
160 $classPaths = array (
161 $classNameLow . '.class.php', // regular homegrown class
162 $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page
163 $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported
166 foreach ($classPaths as $classPath) {
167 $fileIncluded = @include($classPath);
169 if ($fileIncluded) {
170 return true;
174 if (!$fileIncluded) {
175 trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR);
176 return false;
179 if (!class_exists($className)) {
180 trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR);
181 return false;
187 * Dumb function for email validation
189 * @param string $address
190 * @return bool
192 function emailIsValid ($address) {
193 return (
194 ereg(
195 '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
196 $address
201 function getDirFiles ( $dir, $showHidden = false){
203 $files = array();
204 if (!is_dir($dir)){
205 trigger_error('Can not find : '.$dir);
206 return false;
208 if ( $root = @opendir($dir) ){
209 while ($file = readdir ($root)){
210 if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;}
212 if (substr($dir, -1) != '/') $dir .= '/';
214 if( is_dir ($dir . $file) ){
215 $files = array_merge($files, getDirFiles($dir . $file));
216 } else {
217 /*if ( stristr($file, 'tpl') )*/ $files[] = $dir . $file;
221 return $files;
224 function isDBLink($incData) {
225 if (sqlFactory::validType(DB_TYPE) && is_resource($incData) && stristr(get_resource_type($incData),DB_TYPE)) {
226 return true;
228 return false;
231 if (!function_exists('mime_content_type')) {
232 function mime_content_type ($filename) {
233 $t = getimagesize($filename);
235 if (is_array($t))
236 return $t['mime'];
237 else
238 return 'application/unknown';