| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Platform 4 * 5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 defined('JPATH_PLATFORM') or die; 10 11 /** 12 * Joomla Platform Factory class 13 * 14 * @package Joomla.Platform 15 * @since 11.1 16 */ 17 abstract class JFactory 18 { 19 /** 20 * @var JApplication 21 * @since 11.1 22 */ 23 public static $application = null; 24 25 /** 26 * @var JCache 27 * @since 11.1 28 */ 29 public static $cache = null; 30 31 /** 32 * @var JConfig 33 * @since 11.1 34 */ 35 public static $config = null; 36 37 /** 38 * @var array 39 * @since 11.3 40 */ 41 public static $dates = array(); 42 43 /** 44 * @var JSession 45 * @since 11.1 46 */ 47 public static $session = null; 48 49 /** 50 * @var JLanguage 51 * @since 11.1 52 */ 53 public static $language = null; 54 55 /** 56 * @var JDocument 57 * @since 11.1 58 */ 59 public static $document = null; 60 61 /** 62 * @var JAccess 63 * @since 11.1 64 */ 65 public static $acl = null; 66 67 /** 68 * @var JDatabase 69 * @since 11.1 70 */ 71 public static $database = null; 72 73 /** 74 * @var JMail 75 * @since 11.1 76 */ 77 public static $mailer = null; 78 79 /** 80 * Get a application object. 81 * 82 * Returns the global {@link JApplication} object, only creating it if it doesn't already exist. 83 * 84 * @param mixed $id A client identifier or name. 85 * @param array $config An optional associative array of configuration settings. 86 * @param string $prefix Application prefix 87 * 88 * @return JApplication object 89 * 90 * @see JApplication 91 * @since 11.1 92 */ 93 public static function getApplication($id = null, $config = array(), $prefix = 'J') 94 { 95 if (!self::$application) 96 { 97 if (!$id) 98 { 99 JError::raiseError(500, 'Application Instantiation Error'); 100 } 101 102 self::$application = JApplication::getInstance($id, $config, $prefix); 103 } 104 105 return self::$application; 106 } 107 108 /** 109 * Get a configuration object 110 * 111 * Returns the global {@link JRegistry} object, only creating it if it doesn't already exist. 112 * 113 * @param string $file The path to the configuration file 114 * @param string $type The type of the configuration file 115 * 116 * @return JRegistry 117 * 118 * @see JRegistry 119 * @since 11.1 120 */ 121 public static function getConfig($file = null, $type = 'PHP') 122 { 123 if (!self::$config) 124 { 125 if ($file === null) 126 { 127 $file = JPATH_PLATFORM . '/config.php'; 128 } 129 130 self::$config = self::createConfig($file, $type); 131 } 132 133 return self::$config; 134 } 135 136 /** 137 * Get a session object. 138 * 139 * Returns the global {@link JSession} object, only creating it if it doesn't already exist. 140 * 141 * @param array $options An array containing session options 142 * 143 * @return JSession object 144 * 145 * @see JSession 146 * @since 11.1 147 */ 148 public static function getSession($options = array()) 149 { 150 if (!self::$session) 151 { 152 self::$session = self::createSession($options); 153 } 154 155 return self::$session; 156 } 157 158 /** 159 * Get a language object. 160 * 161 * Returns the global {@link JLanguage} object, only creating it if it doesn't already exist. 162 * 163 * @return JLanguage object 164 * 165 * @see JLanguage 166 * @since 11.1 167 */ 168 public static function getLanguage() 169 { 170 if (!self::$language) 171 { 172 self::$language = self::createLanguage(); 173 } 174 175 return self::$language; 176 } 177 178 /** 179 * Get a document object. 180 * 181 * Returns the global {@link JDocument} object, only creating it if it doesn't already exist. 182 * 183 * @return JDocument object 184 * 185 * @see JDocument 186 * @since 11.1 187 */ 188 public static function getDocument() 189 { 190 if (!self::$document) 191 { 192 self::$document = self::createDocument(); 193 } 194 195 return self::$document; 196 } 197 198 /** 199 * Get an user object. 200 * 201 * Returns the global {@link JUser} object, only creating it if it doesn't already exist. 202 * 203 * @param integer $id The user to load - Can be an integer or string - If string, it is converted to ID automatically. 204 * 205 * @return JUser object 206 * 207 * @see JUser 208 * @since 11.1 209 */ 210 public static function getUser($id = null) 211 { 212 if (is_null($id)) 213 { 214 $instance = self::getSession()->get('user'); 215 if (!($instance instanceof JUser)) 216 { 217 $instance = JUser::getInstance(); 218 } 219 } 220 else 221 { 222 $current = self::getSession()->get('user'); 223 if ($current->id != $id) 224 { 225 $instance = JUser::getInstance($id); 226 } 227 else 228 { 229 $instance = self::getSession()->get('user'); 230 } 231 } 232 233 return $instance; 234 } 235 236 /** 237 * Get a cache object 238 * 239 * Returns the global {@link JCache} object 240 * 241 * @param string $group The cache group name 242 * @param string $handler The handler to use 243 * @param string $storage The storage method 244 * 245 * @return JCache object 246 * 247 * @see JCache 248 */ 249 public static function getCache($group = '', $handler = 'callback', $storage = null) 250 { 251 $hash = md5($group . $handler . $storage); 252 if (isset(self::$cache[$hash])) 253 { 254 return self::$cache[$hash]; 255 } 256 $handler = ($handler == 'function') ? 'callback' : $handler; 257 258 $options = array('defaultgroup' => $group); 259 260 if (isset($storage)) 261 { 262 $options['storage'] = $storage; 263 } 264 265 $cache = JCache::getInstance($handler, $options); 266 267 self::$cache[$hash] = $cache; 268 269 return self::$cache[$hash]; 270 } 271 272 /** 273 * Get an authorization object 274 * 275 * Returns the global {@link JACL} object, only creating it 276 * if it doesn't already exist. 277 * 278 * @return JACL object 279 */ 280 public static function getACL() 281 { 282 if (!self::$acl) 283 { 284 self::$acl = new JAccess; 285 } 286 287 return self::$acl; 288 } 289 290 /** 291 * Get a database object. 292 * 293 * Returns the global {@link JDatabase} object, only creating it if it doesn't already exist. 294 * 295 * @return JDatabase object 296 * 297 * @see JDatabase 298 * @since 11.1 299 */ 300 public static function getDbo() 301 { 302 if (!self::$database) 303 { 304 //get the debug configuration setting 305 $conf = self::getConfig(); 306 $debug = $conf->get('debug'); 307 308 self::$database = self::createDbo(); 309 self::$database->setDebug($debug); 310 } 311 312 return self::$database; 313 } 314 315 /** 316 * Get a mailer object. 317 * 318 * Returns the global {@link JMail} object, only creating it if it doesn't already exist. 319 * 320 * @return JMail object 321 * 322 * @see JMail 323 * @since 11.1 324 */ 325 public static function getMailer() 326 { 327 if (!self::$mailer) 328 { 329 self::$mailer = self::createMailer(); 330 } 331 $copy = clone self::$mailer; 332 333 return $copy; 334 } 335 336 /** 337 * Get a parsed XML Feed Source 338 * 339 * @param string $url Url for feed source. 340 * @param integer $cache_time Time to cache feed for (using internal cache mechanism). 341 * 342 * @return mixed SimplePie parsed object on success, false on failure. 343 * 344 * @since 11.1 345 */ 346 public static function getFeedParser($url, $cache_time = 0) 347 { 348 jimport('simplepie.simplepie'); 349 350 $cache = self::getCache('feed_parser', 'callback'); 351 352 if ($cache_time > 0) 353 { 354 $cache->setLifeTime($cache_time); 355 } 356 357 $simplepie = new SimplePie(null, null, 0); 358 359 $simplepie->enable_cache(false); 360 $simplepie->set_feed_url($url); 361 $simplepie->force_feed(true); 362 363 $contents = $cache->get(array($simplepie, 'init'), null, false, false); 364 365 if ($contents) 366 { 367 return $simplepie; 368 } 369 else 370 { 371 JError::raiseWarning('SOME_ERROR_CODE', JText::_('JLIB_UTIL_ERROR_LOADING_FEED_DATA')); 372 } 373 374 return false; 375 } 376 377 /** 378 * Get an XML document 379 * 380 * @param string $type The type of XML parser needed 'DOM', 'RSS' or 'Simple' 381 * @param array $options ['rssUrl'] the rss url to parse when using "RSS", ['cache_time'] with ' 382 * RSS' - feed cache time. If not defined defaults to 3600 sec 383 * 384 * @return object Parsed XML document object 385 * 386 * @deprecated 12.1 Use JXMLElement instead. 387 * @see JXMLElement 388 */ 389 public static function getXMLParser($type = '', $options = array()) 390 { 391 // Deprecation warning. 392 JLog::add('JFactory::getXMLParser() is deprecated.', JLog::WARNING, 'deprecated'); 393 394 $doc = null; 395 396 switch (strtolower($type)) 397 { 398 case 'rss': 399 case 'atom': 400 $cache_time = isset($options['cache_time']) ? $options['cache_time'] : 0; 401 $doc = self::getFeedParser($options['rssUrl'], $cache_time); 402 break; 403 404 case 'simple': 405 // JError::raiseWarning('SOME_ERROR_CODE', 'JSimpleXML is deprecated. Use self::getXML instead'); 406 jimport('joomla.utilities.simplexml'); 407 $doc = new JSimpleXML; 408 break; 409 410 case 'dom': 411 JError::raiseWarning('SOME_ERROR_CODE', JText::_('JLIB_UTIL_ERROR_DOMIT')); 412 $doc = null; 413 break; 414 415 default: 416 $doc = null; 417 } 418 419 return $doc; 420 } 421 422 /** 423 * Reads a XML file. 424 * 425 * @param string $data Full path and file name. 426 * @param boolean $isFile true to load a file or false to load a string. 427 * 428 * @return mixed JXMLElement on success or false on error. 429 * 430 * @see JXMLElement 431 * @since 11.1 432 * @todo This may go in a separate class - error reporting may be improved. 433 */ 434 public static function getXML($data, $isFile = true) 435 { 436 jimport('joomla.utilities.xmlelement'); 437 438 // Disable libxml errors and allow to fetch error information as needed 439 libxml_use_internal_errors(true); 440 441 if ($isFile) 442 { 443 // Try to load the XML file 444 $xml = simplexml_load_file($data, 'JXMLElement'); 445 } 446 else 447 { 448 // Try to load the XML string 449 $xml = simplexml_load_string($data, 'JXMLElement'); 450 } 451 452 if (empty($xml)) 453 { 454 // There was an error 455 JError::raiseWarning(100, JText::_('JLIB_UTIL_ERROR_XML_LOAD')); 456 457 if ($isFile) 458 { 459 JError::raiseWarning(100, $data); 460 } 461 462 foreach (libxml_get_errors() as $error) 463 { 464 JError::raiseWarning(100, 'XML: ' . $error->message); 465 } 466 } 467 468 return $xml; 469 } 470 471 /** 472 * Get an editor object. 473 * 474 * @param string $editor The editor to load, depends on the editor plugins that are installed 475 * 476 * @return JEditor object 477 * 478 * @since 11.1 479 */ 480 public static function getEditor($editor = null) 481 { 482 jimport('joomla.html.editor'); 483 484 //get the editor configuration setting 485 if (is_null($editor)) 486 { 487 $conf = self::getConfig(); 488 $editor = $conf->get('editor'); 489 } 490 491 return JEditor::getInstance($editor); 492 } 493 494 /** 495 * Return a reference to the {@link JURI} object 496 * 497 * @param string $uri Uri name. 498 * 499 * @return JURI object 500 * 501 * @see JURI 502 * @since 11.1 503 */ 504 public static function getURI($uri = 'SERVER') 505 { 506 jimport('joomla.environment.uri'); 507 508 return JURI::getInstance($uri); 509 } 510 511 /** 512 * Return the {@link JDate} object 513 * 514 * @param mixed $time The initial time for the JDate object 515 * @param mixed $tzOffset The timezone offset. 516 * 517 * @return JDate object 518 * 519 * @see JDate 520 * @since 11.1 521 */ 522 public static function getDate($time = 'now', $tzOffset = null) 523 { 524 jimport('joomla.utilities.date'); 525 static $classname; 526 static $mainLocale; 527 528 $language = self::getLanguage(); 529 $locale = $language->getTag(); 530 531 if (!isset($classname) || $locale != $mainLocale) 532 { 533 //Store the locale for future reference 534 $mainLocale = $locale; 535 536 if ($mainLocale !== false) 537 { 538 $classname = str_replace('-', '_', $mainLocale) . 'Date'; 539 540 if (!class_exists($classname)) 541 { 542 //The class does not exist, default to JDate 543 $classname = 'JDate'; 544 } 545 } 546 else 547 { 548 //No tag, so default to JDate 549 $classname = 'JDate'; 550 } 551 } 552 553 $key = $time . '-' . ($tzOffset instanceof DateTimeZone ? $tzOffset->getName() : (string) $tzOffset); 554 555 if (!isset(self::$dates[$classname][$key])) 556 { 557 self::$dates[$classname][$key] = new $classname($time, $tzOffset); 558 } 559 560 $date = clone self::$dates[$classname][$key]; 561 562 return $date; 563 } 564 565 /** 566 * Create a configuration object 567 * 568 * @param string $file The path to the configuration file. 569 * @param string $type The type of the configuration file. 570 * @param string $namespace The namespace of the configuration file. 571 * 572 * @return JRegistry 573 * 574 * @see JRegistry 575 * @since 11.1 576 * @deprecated 12.3 577 */ 578 protected static function _createConfig($file, $type = 'PHP', $namespace = '') 579 { 580 JLog::add(__METHOD__ . '() is deprecated.', JLog::WARNING, 'deprecated'); 581 582 return self::createConfig($file, $type, $namespace); 583 } 584 585 /** 586 * Create a configuration object 587 * 588 * @param string $file The path to the configuration file. 589 * @param string $type The type of the configuration file. 590 * @param string $namespace The namespace of the configuration file. 591 * 592 * @return JRegistry 593 * 594 * @see JRegistry 595 * @since 11.1 596 */ 597 protected static function createConfig($file, $type = 'PHP', $namespace = '') 598 { 599 if (is_file($file)) 600 { 601 include_once $file; 602 } 603 604 // Create the registry with a default namespace of config 605 $registry = new JRegistry; 606 607 // Sanitize the namespace. 608 $namespace = ucfirst((string) preg_replace('/[^A-Z_]/i', '', $namespace)); 609 610 // Build the config name. 611 $name = 'JConfig' . $namespace; 612 613 // Handle the PHP configuration type. 614 if ($type == 'PHP' && class_exists($name)) 615 { 616 // Create the JConfig object 617 $config = new $name; 618 619 // Load the configuration values into the registry 620 $registry->loadObject($config); 621 } 622 623 return $registry; 624 } 625 626 /** 627 * Create a session object 628 * 629 * @param array $options An array containing session options 630 * 631 * @return JSession object 632 * 633 * @since 11.1 634 * @deprecated 12.3 635 */ 636 protected static function _createSession($options = array()) 637 { 638 JLog::add(__METHOD__ . '() is deprecated.', JLog::WARNING, 'deprecated'); 639 640 return self::createSession($options); 641 } 642 643 /** 644 * Create a session object 645 * 646 * @param array $options An array containing session options 647 * 648 * @return JSession object 649 * 650 * @since 11.1 651 */ 652 protected static function createSession($options = array()) 653 { 654 // Get the editor configuration setting 655 $conf = self::getConfig(); 656 $handler = $conf->get('session_handler', 'none'); 657 658 // Config time is in minutes 659 $options['expire'] = ($conf->get('lifetime')) ? $conf->get('lifetime') * 60 : 900; 660 661 $session = JSession::getInstance($handler, $options); 662 if ($session->getState() == 'expired') 663 { 664 $session->restart(); 665 } 666 667 return $session; 668 } 669 670 /** 671 * Create an database object 672 * 673 * @return JDatabase object 674 * 675 * @see JDatabase 676 * @since 11.1 677 * @deprecated 12.3 678 */ 679 protected static function _createDbo() 680 { 681 JLog::add(__METHOD__ . '() is deprecated.', JLog::WARNING, 'deprecated'); 682 683 return self::createDbo(); 684 } 685 686 /** 687 * Create an database object 688 * 689 * @return JDatabase object 690 * 691 * @see JDatabase 692 * @since 11.1 693 */ 694 protected static function createDbo() 695 { 696 jimport('joomla.database.table'); 697 698 $conf = self::getConfig(); 699 700 $host = $conf->get('host'); 701 $user = $conf->get('user'); 702 $password = $conf->get('password'); 703 $database = $conf->get('db'); 704 $prefix = $conf->get('dbprefix'); 705 $driver = $conf->get('dbtype'); 706 $debug = $conf->get('debug'); 707 708 $options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix); 709 710 $db = JDatabase::getInstance($options); 711 712 if ($db instanceof Exception) 713 { 714 if (!headers_sent()) 715 { 716 header('HTTP/1.1 500 Internal Server Error'); 717 } 718 jexit('Database Error: ' . (string) $db); 719 } 720 721 if ($db->getErrorNum() > 0) 722 { 723 die(sprintf('Database connection error (%d): %s', $db->getErrorNum(), $db->getErrorMsg())); 724 } 725 726 $db->setDebug($debug); 727 728 return $db; 729 } 730 731 /** 732 * Create a mailer object 733 * 734 * @return JMail object 735 * 736 * @see JMail 737 * @since 11.1 738 * @deprecated 12.3 739 */ 740 protected static function _createMailer() 741 { 742 JLog::add(__METHOD__ . '() is deprecated.', JLog::WARNING, 'deprecated'); 743 744 return self::createMailer(); 745 } 746 747 /** 748 * Create a mailer object 749 * 750 * @return JMail object 751 * 752 * @see JMail 753 * @since 11.1 754 */ 755 protected static function createMailer() 756 { 757 $conf = self::getConfig(); 758 759 $smtpauth = ($conf->get('smtpauth') == 0) ? null : 1; 760 $smtpuser = $conf->get('smtpuser'); 761 $smtppass = $conf->get('smtppass'); 762 $smtphost = $conf->get('smtphost'); 763 $smtpsecure = $conf->get('smtpsecure'); 764 $smtpport = $conf->get('smtpport'); 765 $mailfrom = $conf->get('mailfrom'); 766 $fromname = $conf->get('fromname'); 767 $mailer = $conf->get('mailer'); 768 769 // Create a JMail object 770 $mail = JMail::getInstance(); 771 772 // Set default sender without Reply-to 773 $mail->SetFrom(JMailHelper::cleanLine($mailfrom), JMailHelper::cleanLine($fromname), 0); 774 775 // Default mailer is to use PHP's mail function 776 switch ($mailer) 777 { 778 case 'smtp': 779 $mail->useSMTP($smtpauth, $smtphost, $smtpuser, $smtppass, $smtpsecure, $smtpport); 780 break; 781 782 case 'sendmail': 783 $mail->IsSendmail(); 784 break; 785 786 default: 787 $mail->IsMail(); 788 break; 789 } 790 791 return $mail; 792 } 793 794 /** 795 * Create a language object 796 * 797 * @return JLanguage object 798 * 799 * @see JLanguage 800 * @since 11.1 801 * @deprecated 12.3 802 */ 803 protected static function _createLanguage() 804 { 805 JLog::add(__METHOD__ . ' is deprecated.', JLog::WARNING, 'deprecated'); 806 807 return self::createLanguage(); 808 } 809 810 /** 811 * Create a language object 812 * 813 * @return JLanguage object 814 * 815 * @see JLanguage 816 * @since 11.1 817 */ 818 protected static function createLanguage() 819 { 820 $conf = self::getConfig(); 821 $locale = $conf->get('language'); 822 $debug = $conf->get('debug_lang'); 823 $lang = JLanguage::getInstance($locale, $debug); 824 825 return $lang; 826 } 827 828 /** 829 * Create a document object 830 * 831 * @return JDocument object 832 * 833 * @see JDocument 834 * @since 11.1 835 * @deprecated 12.3 836 */ 837 protected static function _createDocument() 838 { 839 JLog::add(__METHOD__ . ' is deprecated.', JLog::WARNING, 'deprecated'); 840 841 return self::createDocument(); 842 } 843 844 /** 845 * Create a document object 846 * 847 * @return JDocument object 848 * 849 * @see JDocument 850 * @since 11.1 851 */ 852 protected static function createDocument() 853 { 854 $lang = self::getLanguage(); 855 856 // Keep backwards compatibility with Joomla! 1.0 857 // @deprecated 12.1 This will be removed in the next version 858 $raw = JRequest::getBool('no_html'); 859 $type = JRequest::getWord('format', $raw ? 'raw' : 'html'); 860 861 $attributes = array('charset' => 'utf-8', 'lineend' => 'unix', 'tab' => ' ', 'language' => $lang->getTag(), 862 'direction' => $lang->isRTL() ? 'rtl' : 'ltr'); 863 864 return JDocument::getInstance($type, $attributes); 865 } 866 867 /** 868 * Creates a new stream object with appropriate prefix 869 * 870 * @param boolean $use_prefix Prefix the connections for writing 871 * @param boolean $use_network Use network if available for writing; use false to disable (e.g. FTP, SCP) 872 * @param string $ua UA User agent to use 873 * @param boolean $uamask User agent masking (prefix Mozilla) 874 * 875 * @return JStream 876 * 877 * @see JStream 878 * @since 11.1 879 */ 880 public static function getStream($use_prefix = true, $use_network = true, $ua = null, $uamask = false) 881 { 882 jimport('joomla.filesystem.stream'); 883 884 // Setup the context; Joomla! UA and overwrite 885 $context = array(); 886 $version = new JVersion; 887 // set the UA for HTTP and overwrite for FTP 888 $context['http']['user_agent'] = $version->getUserAgent($ua, $uamask); 889 $context['ftp']['overwrite'] = true; 890 891 if ($use_prefix) 892 { 893 $FTPOptions = JClientHelper::getCredentials('ftp'); 894 $SCPOptions = JClientHelper::getCredentials('scp'); 895 896 if ($FTPOptions['enabled'] == 1 && $use_network) 897 { 898 $prefix = 'ftp://' . $FTPOptions['user'] . ':' . $FTPOptions['pass'] . '@' . $FTPOptions['host']; 899 $prefix .= $FTPOptions['port'] ? ':' . $FTPOptions['port'] : ''; 900 $prefix .= $FTPOptions['root']; 901 } 902 elseif ($SCPOptions['enabled'] == 1 && $use_network) 903 { 904 $prefix = 'ssh2.sftp://' . $SCPOptions['user'] . ':' . $SCPOptions['pass'] . '@' . $SCPOptions['host']; 905 $prefix .= $SCPOptions['port'] ? ':' . $SCPOptions['port'] : ''; 906 $prefix .= $SCPOptions['root']; 907 } 908 else 909 { 910 $prefix = JPATH_ROOT . '/'; 911 } 912 913 $retval = new JStream($prefix, JPATH_ROOT, $context); 914 } 915 else 916 { 917 $retval = new JStream('', '', $context); 918 } 919 920 return $retval; 921 } 922 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |