* made the usingPackage function to behave in the case of the same package added...
[vsc.git] / _res / _libs / functions.inc.php
blobd3d0090d1d03793832b8fc94b9b8ceca9cf8e694
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 function d () {
24 $aRgs = func_get_args();
25 $iExit = 1;
27 if (php_sapi_name() != 'cli') {
28 // not running in console
29 echo '<pre>';
30 $iExit = 0;
32 foreach ($aRgs as $object) {
33 var_dump($object);
36 die ($iExit);
39 /**
40 * @see usingPackage
41 * @param string $sPackageName
42 * @return bool
44 function import ($sPackageName) {
45 return usingPackage($sPackageName);
47 /**
48 * Adds the package name to the include path
49 * @todo make sure that the path isn't aleady in the include path
50 * @param string $packageName
51 * @return bool
52 * @throws tsExceptionPackageImport
54 function usingPackage ($packageName) {
55 $pkgLower = strtolower($packageName);
56 $pkgPath = LIB_PATH . DIRECTORY_SEPARATOR . $pkgLower;
58 $path = get_include_path();
59 if (is_dir($pkgPath)) {
60 if (strpos ($path, $pkgPath) === false) {
61 // adding exceptions dir to include path if it exists
62 if (is_dir ($pkgPath. DIRECTORY_SEPARATOR . 'exceptions')) {
63 // adding the exceptions if they exist
64 $pkgPath .= PATH_SEPARATOR . $pkgPath . DIRECTORY_SEPARATOR . 'exceptions';
66 set_include_path(
67 $path . PATH_SEPARATOR .
68 $pkgPath
70 } else {
71 echo $pkgPath . "\n";
73 return true;
74 } else {
75 usingPackage ('coreexceptions');
76 throw new tsExceptionPackageImport ("bad package " . $packageName);
80 function usingClass ($className) {
81 if (class_exists ($className, false)) {
82 return true;
85 $classNameLow = strtolower($className);
88 $fileIncluded = @include ($classNameLow . '.class.php');
89 if ( !$fileIncluded ) {
90 $fileIncluded = @include ($classNameLow . DIRECTORY_SEPARATOR . $classNameLow . '.class.php');
92 return $fileIncluded;
95 /**
96 * the __autoload automagic function for class initialization,
97 * see usingClass
99 * @param string $className
101 function __autoload ($className) {
102 if (class_exists ($className, false)) {
103 return true;
105 return usingClass ($className);
109 * returns true if the user's ip is in our list of debug ips
111 * @return bool
113 function isDebug (){
114 if (stristr(C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR']) && C_SYSTEM_DEBUG)
115 return true;
117 return false;
120 if (!function_exists('usingClass')) {
122 * The php4 function for including the class file
124 * @param string $className
125 * @return bool
127 function usingClass($className) {
128 if (class_exists($className)) {
129 return true;
132 $classNameLow = strtolower($className);
135 $classPaths = array (
136 $classNameLow . '.class.php', // regular homegrown class
137 $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page
138 $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported
141 foreach ($classPaths as $classPath) {
142 $fileIncluded = @include($classPath);
144 if ($fileIncluded) {
145 return true;
149 if (!$fileIncluded) {
150 trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR);
151 return false;
154 if (!class_exists($className)) {
155 trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR);
156 return false;
162 * Dumb function for email validation
164 * @param string $address
165 * @return bool
167 function emailIsValid ($address) {
168 return (
169 ereg(
170 '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
171 $address
176 function getDirFiles ( $dir, $showHidden = false){
178 $files = array();
179 if (!is_dir($dir)){
180 trigger_error('Can not find : '.$dir);
181 return false;
183 if ( $root = @opendir($dir) ){
184 while ($file = readdir ($root)){
185 if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;}
187 if (substr($dir, -1) != '/') $dir .= '/';
189 if( is_dir ($dir . $file) ){
190 $files = array_merge($files, getDirFiles($dir . $file));
191 } else {
192 /*if ( stristr($file, 'tpl') )*/ $files[] = $dir . $file;
196 return $files;
199 function isDBLink($incData) {
200 if (sqlFactory::validType(DB_TYPE) && is_resource($incData) && stristr(get_resource_type($incData),DB_TYPE)) {
201 return true;
203 return false;
206 if (!function_exists('mime_content_type')) {
207 function mime_content_type ($filename) {
208 $t = getimagesize($filename);
210 if (is_array($t))
211 return $t['mime'];
212 else
213 return 'application/unknown';