| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | if(!defined('PRADO_DIR')) |
|---|
| 20 | define('PRADO_DIR',dirname(__FILE__)); |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | if(!defined('PRADO_CHMOD')) |
|---|
| 25 | define('PRADO_CHMOD',0777); |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | class PradoBase |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | const CLASS_FILE_EXT='.php'; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | private static $_aliases=array('System'=>PRADO_DIR); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | private static $_usings=array(); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | private static $_application=null; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | private static $_logger=null; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | public static function getVersion() |
|---|
| 68 | { |
|---|
| 69 | return '3.1.3a'; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | public static function initErrorHandlers() |
|---|
| 78 | { |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | set_error_handler(array('PradoBase','phpErrorHandler'),error_reporting()); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | set_exception_handler(array('PradoBase','exceptionHandler')); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | public static function autoload($className) |
|---|
| 95 | { |
|---|
| 96 | include_once($className.self::CLASS_FILE_EXT); |
|---|
| 97 | if(!class_exists($className,false) && !interface_exists($className,false)) |
|---|
| 98 | self::fatalError("Class file for '$className' cannot be found."); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | public static function poweredByPrado($logoType=0) |
|---|
| 106 | { |
|---|
| 107 | $logoName=$logoType==1?'powered2':'powered'; |
|---|
| 108 | if(self::$_application!==null) |
|---|
| 109 | { |
|---|
| 110 | $am=self::$_application->getAssetManager(); |
|---|
| 111 | $url=$am->publishFilePath(self::getPathOfNamespace('System.'.$logoName,'.gif')); |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | $url='http://www.pradosoft.com/images/'.$logoName.'.gif'; |
|---|
| 115 | return '<a title="Powered by PRADO" href="http://www.pradosoft.com/" target="_blank"><img src="'.$url.'" style="border-width:0px;" alt="Powered by PRADO" /></a>'; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public static function metaGenerator() |
|---|
| 119 | { |
|---|
| 120 | return 'PRADO - http://www.pradosoft.com/'; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | public static function phpErrorHandler($errno,$errstr,$errfile,$errline) |
|---|
| 134 | { |
|---|
| 135 | if(error_reporting()!=0) |
|---|
| 136 | throw new TPhpErrorException($errno,$errstr,$errfile,$errline); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | public static function exceptionHandler($exception) |
|---|
| 149 | { |
|---|
| 150 | if(self::$_application!==null && ($errorHandler=self::$_application->getErrorHandler())!==null) |
|---|
| 151 | { |
|---|
| 152 | $errorHandler->handleError(null,$exception); |
|---|
| 153 | } |
|---|
| 154 | else |
|---|
| 155 | { |
|---|
| 156 | echo $exception; |
|---|
| 157 | } |
|---|
| 158 | exit(1); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | public static function setApplication($application) |
|---|
| 171 | { |
|---|
| 172 | if(self::$_application!==null) |
|---|
| 173 | throw new TInvalidOperationException('prado_application_singleton_required'); |
|---|
| 174 | self::$_application=$application; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | public static function getApplication() |
|---|
| 181 | { |
|---|
| 182 | return self::$_application; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | public static function getFrameworkPath() |
|---|
| 189 | { |
|---|
| 190 | return PRADO_DIR; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | public static function serialize($data) |
|---|
| 201 | { |
|---|
| 202 | $arr[0]=$data; |
|---|
| 203 | return serialize($arr); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | public static function unserialize($str) |
|---|
| 214 | { |
|---|
| 215 | $arr=unserialize($str); |
|---|
| 216 | return isset($arr[0])?$arr[0]:null; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | public static function createComponent($type) |
|---|
| 233 | { |
|---|
| 234 | self::using($type); |
|---|
| 235 | if(($pos=strrpos($type,'.'))!==false) |
|---|
| 236 | $type=substr($type,$pos+1); |
|---|
| 237 | if(($n=func_num_args())>1) |
|---|
| 238 | { |
|---|
| 239 | $args=func_get_args(); |
|---|
| 240 | $s='$args[1]'; |
|---|
| 241 | for($i=2;$i<$n;++$i) |
|---|
| 242 | $s.=",\$args[$i]"; |
|---|
| 243 | eval("\$component=new $type($s);"); |
|---|
| 244 | return $component; |
|---|
| 245 | } |
|---|
| 246 | else |
|---|
| 247 | return new $type; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | public static function using($namespace,$checkClassExistence=true) |
|---|
| 260 | { |
|---|
| 261 | if(isset(self::$_usings[$namespace]) || class_exists($namespace,false)) |
|---|
| 262 | return; |
|---|
| 263 | if(($pos=strrpos($namespace,'.'))===false) |
|---|
| 264 | { |
|---|
| 265 | try |
|---|
| 266 | { |
|---|
| 267 | include_once($namespace.self::CLASS_FILE_EXT); |
|---|
| 268 | } |
|---|
| 269 | catch(Exception $e) |
|---|
| 270 | { |
|---|
| 271 | if($checkClassExistence && !class_exists($namespace,false)) |
|---|
| 272 | throw new TInvalidOperationException('prado_component_unknown',$namespace,$e->getMessage()); |
|---|
| 273 | else |
|---|
| 274 | throw $e; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | else if(($path=self::getPathOfNamespace($namespace,self::CLASS_FILE_EXT))!==null) |
|---|
| 278 | { |
|---|
| 279 | $className=substr($namespace,$pos+1); |
|---|
| 280 | if($className==='*') |
|---|
| 281 | { |
|---|
| 282 | self::$_usings[$namespace]=$path; |
|---|
| 283 | set_include_path(get_include_path().PATH_SEPARATOR.$path); |
|---|
| 284 | } |
|---|
| 285 | else |
|---|
| 286 | { |
|---|
| 287 | self::$_usings[$namespace]=$path; |
|---|
| 288 | if(!$checkClassExistence || !class_exists($className,false)) |
|---|
| 289 | { |
|---|
| 290 | try |
|---|
| 291 | { |
|---|
| 292 | include_once($path); |
|---|
| 293 | } |
|---|
| 294 | catch(Exception $e) |
|---|
| 295 | { |
|---|
| 296 | if($checkClassExistence && !class_exists($className,false)) |
|---|
| 297 | throw new TInvalidOperationException('prado_component_unknown',$className,$e->getMessage()); |
|---|
| 298 | else |
|---|
| 299 | throw $e; |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | else |
|---|
| 305 | throw new TInvalidDataValueException('prado_using_invalid',$namespace); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | public static function getPathOfNamespace($namespace,$ext='') |
|---|
| 321 | { |
|---|
| 322 | if(isset(self::$_usings[$namespace])) |
|---|
| 323 | return self::$_usings[$namespace]; |
|---|
| 324 | else if(isset(self::$_aliases[$namespace])) |
|---|
| 325 | return self::$_aliases[$namespace]; |
|---|
| 326 | else |
|---|
| 327 | { |
|---|
| 328 | $segs=explode('.',$namespace); |
|---|
| 329 | $alias=array_shift($segs); |
|---|
| 330 | if(($file=array_pop($segs))!==null && ($root=self::getPathOfAlias($alias))!==null) |
|---|
| 331 | return rtrim($root.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR ,$segs),'/\\').(($file==='*')?'':DIRECTORY_SEPARATOR.$file.$ext); |
|---|
| 332 | else |
|---|
| 333 | return null; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | public static function getPathOfAlias($alias) |
|---|
| 342 | { |
|---|
| 343 | return isset(self::$_aliases[$alias])?self::$_aliases[$alias]:null; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | protected static function getPathAliases() |
|---|
| 347 | { |
|---|
| 348 | return self::$_aliases; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | public static function setPathOfAlias($alias,$path) |
|---|
| 358 | { |
|---|
| 359 | if(isset(self::$_aliases[$alias])) |
|---|
| 360 | throw new TInvalidOperationException('prado_alias_redefined',$alias); |
|---|
| 361 | else if(($rp=realpath($path))!==false && is_dir($rp)) |
|---|
| 362 | { |
|---|
| 363 | if(strpos($alias,'.')===false) |
|---|
| 364 | self::$_aliases[$alias]=$rp; |
|---|
| 365 | else |
|---|
| 366 | throw new TInvalidDataValueException('prado_aliasname_invalid',$alias); |
|---|
| 367 | } |
|---|
| 368 | else |
|---|
| 369 | throw new TInvalidDataValueException('prado_alias_invalid',$alias,$path); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | public static function fatalError($msg) |
|---|
| 379 | { |
|---|
| 380 | echo '<h1>Fatal Error</h1>'; |
|---|
| 381 | echo '<p>'.$msg.'</p>'; |
|---|
| 382 | if(!function_exists('debug_backtrace')) |
|---|
| 383 | return; |
|---|
| 384 | echo '<h2>Debug Backtrace</h2>'; |
|---|
| 385 | echo '<pre>'; |
|---|
| 386 | $index=-1; |
|---|
| 387 | foreach(debug_backtrace() as $t) |
|---|
| 388 | { |
|---|
| 389 | $index++; |
|---|
| 390 | if($index==0) |
|---|
| 391 | continue; |
|---|
| 392 | echo '#'.$index.' '; |
|---|
| 393 | if(isset($t['file'])) |
|---|
| 394 | echo basename($t['file']) . ':' . $t['line']; |
|---|
| 395 | else |
|---|
| 396 | echo '<PHP inner-code>'; |
|---|
| 397 | echo ' -- '; |
|---|
| 398 | if(isset($t['class'])) |
|---|
| 399 | echo $t['class'] . $t['type']; |
|---|
| 400 | echo $t['function'] . '('; |
|---|
| 401 | if(isset($t['args']) && sizeof($t['args']) > 0) |
|---|
| 402 | { |
|---|
| 403 | $count=0; |
|---|
| 404 | foreach($t['args'] as $item) |
|---|
| 405 | { |
|---|
| 406 | if(is_string($item)) |
|---|
| 407 | { |
|---|
| 408 | $str=htmlentities(str_replace("\r\n", "", $item), ENT_QUOTES); |
|---|
| 409 | if (strlen($item) > 70) |
|---|
| 410 | echo "'". substr($str, 0, 70) . "...'"; |
|---|
| 411 | else |
|---|
| 412 | echo "'" . $str . "'"; |
|---|
| 413 | } |
|---|
| 414 | else if (is_int($item) || is_float($item)) |
|---|
| 415 | echo $item; |
|---|
| 416 | else if (is_object($item)) |
|---|
| 417 | echo get_class($item); |
|---|
| 418 | else if (is_array($item)) |
|---|
| 419 | echo 'array(' . count($item) . ')'; |
|---|
| 420 | else if (is_bool($item)) |
|---|
| 421 | echo $item ? 'true' : 'false'; |
|---|
| 422 | else if ($item === null) |
|---|
| 423 | echo 'NULL'; |
|---|
| 424 | else if (is_resource($item)) |
|---|
| 425 | echo get_resource_type($item); |
|---|
| 426 | $count++; |
|---|
| 427 | if (count($t['args']) > $count) |
|---|
| 428 | echo ', '; |
|---|
| 429 | } |
|---|
| 430 | } |
|---|
| 431 | echo ")\n"; |
|---|
| 432 | } |
|---|
| 433 | echo '</pre>'; |
|---|
| 434 | exit(1); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | public static function getUserLanguages() |
|---|
| 445 | { |
|---|
| 446 | static $languages=null; |
|---|
| 447 | if($languages===null) |
|---|
| 448 | { |
|---|
| 449 | if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) |
|---|
| 450 | $languages[0]='en'; |
|---|
| 451 | else |
|---|
| 452 | { |
|---|
| 453 | $languages=array(); |
|---|
| 454 | foreach(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']) as $language) |
|---|
| 455 | { |
|---|
| 456 | $array=split(';q=',trim($language)); |
|---|
| 457 | $languages[trim($array[0])]=isset($array[1])?(float)$array[1]:1.0; |
|---|
| 458 | } |
|---|
| 459 | arsort($languages); |
|---|
| 460 | $languages=array_keys($languages); |
|---|
| 461 | if(empty($languages)) |
|---|
| 462 | $languages[0]='en'; |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | return $languages; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | public static function getPreferredLanguage() |
|---|
| 473 | { |
|---|
| 474 | static $language=null; |
|---|
| 475 | if($language===null) |
|---|
| 476 | { |
|---|
| 477 | $langs=Prado::getUserLanguages(); |
|---|
| 478 | $lang=explode('-',$langs[0]); |
|---|
| 479 | if(empty($lang[0]) || !ctype_alpha($lang[0])) |
|---|
| 480 | $language='en'; |
|---|
| 481 | else |
|---|
| 482 | $language=$lang[0]; |
|---|
| 483 | } |
|---|
| 484 | return $language; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | public static function trace($msg,$category='Uncategorized') |
|---|
| 499 | { |
|---|
| 500 | if(self::$_application && self::$_application->getMode()===TApplicationMode::Performance) |
|---|
| 501 | return; |
|---|
| 502 | if(!self::$_application || self::$_application->getMode()===TApplicationMode::Debug) |
|---|
| 503 | { |
|---|
| 504 | $trace=debug_backtrace(); |
|---|
| 505 | if(isset($trace[0]['file']) && isset($trace[0]['line'])) |
|---|
| 506 | $msg.=" (line {$trace[0]['line']}, {$trace[0]['file']})"; |
|---|
| 507 | $level=TLogger::DEBUG; |
|---|
| 508 | } |
|---|
| 509 | else |
|---|
| 510 | $level=TLogger::INFO; |
|---|
| 511 | self::log($msg,$level,$category); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | public static function log($msg,$level=TLogger::INFO,$category='Uncategorized') |
|---|
| 526 | { |
|---|
| 527 | if(self::$_logger===null) |
|---|
| 528 | self::$_logger=new TLogger; |
|---|
| 529 | self::$_logger->log($msg,$level,$category); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | public static function getLogger() |
|---|
| 536 | { |
|---|
| 537 | if(self::$_logger===null) |
|---|
| 538 | self::$_logger=new TLogger; |
|---|
| 539 | return self::$_logger; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | public static function varDump($var,$depth=10,$highlight=false) |
|---|
| 552 | { |
|---|
| 553 | Prado::using('System.Util.TVarDumper'); |
|---|
| 554 | return TVarDumper::dump($var,$depth,$highlight); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | public static function localize($text, $parameters=array(), $catalogue=null, $charset=null) |
|---|
| 568 | { |
|---|
| 569 | Prado::using('System.I18N.Translation'); |
|---|
| 570 | $app = Prado::getApplication()->getGlobalization(false); |
|---|
| 571 | |
|---|
| 572 | $params = array(); |
|---|
| 573 | foreach($parameters as $key => $value) |
|---|
| 574 | $params['{'.$key.'}'] = $value; |
|---|
| 575 | |
|---|
| 576 | |
|---|
| 577 | if($app===null || ($config = $app->getTranslationConfiguration())===null) |
|---|
| 578 | return strtr($text, $params); |
|---|
| 579 | |
|---|
| 580 | if ($catalogue===null) |
|---|
| 581 | $catalogue=isset($config['catalogue'])?$config['catalogue']:'messages'; |
|---|
| 582 | |
|---|
| 583 | Translation::init($catalogue); |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | $appCharset = $app===null ? '' : $app->getCharset(); |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | $defaultCharset = ($app===null) ? 'UTF-8' : $app->getDefaultCharset(); |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | if(empty($charset)) $charset = $appCharset; |
|---|
| 593 | |
|---|