Source for file factory.php

Documentation is available at factory.php

  1. <?php
  2. /**
  3.  * @package    Joomla.Platform
  4.  *
  5.  * @copyright  Copyright (C) 2005 - 2013 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.      * Global application object
  21.      *
  22.      * @var    JApplicationCms 
  23.      * @since  11.1
  24.      */
  25.     public static $application null;
  26.  
  27.     /**
  28.      * Global cache object
  29.      *
  30.      * @var    JCache 
  31.      * @since  11.1
  32.      */
  33.     public static $cache null;
  34.  
  35.     /**
  36.      * Global configuraiton object
  37.      *
  38.      * @var    JConfig 
  39.      * @since  11.1
  40.      */
  41.     public static $config null;
  42.  
  43.     /**
  44.      * Container for JDate instances
  45.      *
  46.      * @var    array 
  47.      * @since  11.3
  48.      */
  49.     public static $dates array();
  50.  
  51.     /**
  52.      * Global session object
  53.      *
  54.      * @var    JSession 
  55.      * @since  11.1
  56.      */
  57.     public static $session null;
  58.  
  59.     /**
  60.      * Global language object
  61.      *
  62.      * @var    JLanguage 
  63.      * @since  11.1
  64.      */
  65.     public static $language null;
  66.  
  67.     /**
  68.      * Global document object
  69.      *
  70.      * @var    JDocument 
  71.      * @since  11.1
  72.      */
  73.     public static $document null;
  74.  
  75.     /**
  76.      * Global ACL object
  77.      *
  78.      * @var    JAccess 
  79.      * @since  11.1
  80.      * @deprecated  13.3 (Platform) & 4.0 (CMS)
  81.      */
  82.     public static $acl null;
  83.  
  84.     /**
  85.      * Global database object
  86.      *
  87.      * @var    JDatabaseDriver 
  88.      * @since  11.1
  89.      */
  90.     public static $database null;
  91.  
  92.     /**
  93.      * Global mailer object
  94.      *
  95.      * @var    JMail 
  96.      * @since  11.1
  97.      */
  98.     public static $mailer null;
  99.  
  100.     /**
  101.      * Get a application object.
  102.      *
  103.      * Returns the global {@link JApplicationCms} object, only creating it if it doesn't already exist.
  104.      *
  105.      * @param   mixed   $id      A client identifier or name.
  106.      * @param   array   $config  An optional associative array of configuration settings.
  107.      * @param   string  $prefix  Application prefix
  108.      *
  109.      * @return  JApplicationCms object
  110.      *
  111.      * @see     JApplication
  112.      * @since   11.1
  113.      * @throws  Exception
  114.      */
  115.     public static function getApplication($id nullarray $config array()$prefix 'J')
  116.     {
  117.         if (!self::$application)
  118.         {
  119.             if (!$id)
  120.             {
  121.                 throw new Exception('Application Instantiation Error'500);
  122.             }
  123.  
  124.             self::$application JApplicationCms::getInstance($id);
  125.         }
  126.  
  127.         return self::$application;
  128.     }
  129.  
  130.     /**
  131.      * Get a configuration object
  132.      *
  133.      * Returns the global {@link JRegistry} object, only creating it if it doesn't already exist.
  134.      *
  135.      * @param   string  $file       The path to the configuration file
  136.      * @param   string  $type       The type of the configuration file
  137.      * @param   string  $namespace  The namespace of the configuration file
  138.      *
  139.      * @return  JRegistry 
  140.      *
  141.      * @see     JRegistry
  142.      * @since   11.1
  143.      */
  144.     public static function getConfig($file null$type 'PHP'$namespace '')
  145.     {
  146.         if (!self::$config)
  147.         {
  148.             if ($file === null)
  149.             {
  150.                 $file JPATH_PLATFORM '/config.php';
  151.             }
  152.  
  153.             self::$config self::createConfig($file$type$namespace);
  154.         }
  155.  
  156.         return self::$config;
  157.     }
  158.  
  159.     /**
  160.      * Get a session object.
  161.      *
  162.      * Returns the global {@link JSession} object, only creating it if it doesn't already exist.
  163.      *
  164.      * @param   array  $options  An array containing session options
  165.      *
  166.      * @return  JSession object
  167.      *
  168.      * @see     JSession
  169.      * @since   11.1
  170.      */
  171.     public static function getSession(array $options array())
  172.     {
  173.         if (!self::$session)
  174.         {
  175.             self::$session self::createSession($options);
  176.         }
  177.  
  178.         return self::$session;
  179.     }
  180.  
  181.     /**
  182.      * Get a language object.
  183.      *
  184.      * Returns the global {@link JLanguage} object, only creating it if it doesn't already exist.
  185.      *
  186.      * @return  JLanguage object
  187.      *
  188.      * @see     JLanguage
  189.      * @since   11.1
  190.      */
  191.     public static function getLanguage()
  192.     {
  193.         if (!self::$language)
  194.         {
  195.             self::$language self::createLanguage();
  196.         }
  197.  
  198.         return self::$language;
  199.     }
  200.  
  201.     /**
  202.      * Get a document object.
  203.      *
  204.      * Returns the global {@link JDocument} object, only creating it if it doesn't already exist.
  205.      *
  206.      * @return  JDocument object
  207.      *
  208.      * @see     JDocument
  209.      * @since   11.1
  210.      */
  211.     public static function getDocument()
  212.     {
  213.         if (!self::$document)
  214.         {
  215.             self::$document self::createDocument();
  216.         }
  217.  
  218.         return self::$document;
  219.     }
  220.  
  221.     /**
  222.      * Get an user object.
  223.      *
  224.      * Returns the global {@link JUser} object, only creating it if it doesn't already exist.
  225.      *
  226.      * @param   integer  $id  The user to load - Can be an integer or string - If string, it is converted to ID automatically.
  227.      *
  228.      * @return  JUser object
  229.      *
  230.      * @see     JUser
  231.      * @since   11.1
  232.      */
  233.     public static function getUser($id null)
  234.     {
  235.         $instance self::getSession()->get('user');
  236.  
  237.         if (is_null($id))
  238.         {
  239.             if (!($instance instanceof JUser))
  240.             {
  241.                 $instance JUser::getInstance();
  242.             }
  243.         }
  244.         elseif ($instance->id != $id)
  245.         {
  246.             $instance JUser::getInstance($id);
  247.         }
  248.  
  249.         return $instance;
  250.     }
  251.  
  252.     /**
  253.      * Get a cache object
  254.      *
  255.      * Returns the global {@link JCache} object
  256.      *
  257.      * @param   string  $group    The cache group name
  258.      * @param   string  $handler  The handler to use
  259.      * @param   string  $storage  The storage method
  260.      *
  261.      * @return  JCacheController object
  262.      *
  263.      * @see     JCache
  264.      * @since   11.1
  265.      */
  266.     public static function getCache($group ''$handler 'callback'$storage null)
  267.     {
  268.         $hash md5($group $handler $storage);
  269.  
  270.         if (isset(self::$cache[$hash]))
  271.         {
  272.             return self::$cache[$hash];
  273.         }
  274.  
  275.         $handler ($handler == 'function''callback' $handler;
  276.  
  277.         $options array('defaultgroup' => $group);
  278.  
  279.         if (isset($storage))
  280.         {
  281.             $options['storage'$storage;
  282.         }
  283.  
  284.         $cache JCache::getInstance($handler$options);
  285.  
  286.         self::$cache[$hash$cache;
  287.  
  288.         return self::$cache[$hash];
  289.     }
  290.  
  291.     /**
  292.      * Get an authorization object
  293.      *
  294.      * Returns the global {@link JAccess} object, only creating it
  295.      * if it doesn't already exist.
  296.      *
  297.      * @return  JAccess object
  298.      *
  299.      * @deprecated  13.3 (Platform) & 4.0 (CMS) - Use JAccess directly.
  300.      */
  301.     public static function getACL()
  302.     {
  303.         JLog::add(__METHOD__ . ' is deprecated. Use JAccess directly.'JLog::WARNING'deprecated');
  304.  
  305.         if (!self::$acl)
  306.         {
  307.             self::$acl new JAccess;
  308.         }
  309.  
  310.         return self::$acl;
  311.     }
  312.  
  313.     /**
  314.      * Get a database object.
  315.      *
  316.      * Returns the global {@link JDatabaseDriver} object, only creating it if it doesn't already exist.
  317.      *
  318.      * @return  JDatabaseDriver 
  319.      *
  320.      * @see     JDatabaseDriver
  321.      * @since   11.1
  322.      */
  323.     public static function getDbo()
  324.     {
  325.         if (!self::$database)
  326.         {
  327.             self::$database self::createDbo();
  328.         }
  329.  
  330.         return self::$database;
  331.     }
  332.  
  333.     /**
  334.      * Get a mailer object.
  335.      *
  336.      * Returns the global {@link JMail} object, only creating it if it doesn't already exist.
  337.      *
  338.      * @return  JMail object
  339.      *
  340.      * @see     JMail
  341.      * @since   11.1
  342.      */
  343.     public static function getMailer()
  344.     {
  345.         if (!self::$mailer)
  346.         {
  347.             self::$mailer self::createMailer();
  348.         }
  349.  
  350.         $copy clone self::$mailer;
  351.  
  352.         return $copy;
  353.     }
  354.  
  355.     /**
  356.      * Get a parsed XML Feed Source
  357.      *
  358.      * @param   string   $url         Url for feed source.
  359.      * @param   integer  $cache_time  Time to cache feed for (using internal cache mechanism).
  360.      *
  361.      * @return  mixed  SimplePie parsed object on success, false on failure.
  362.      *
  363.      * @since   11.1
  364.      * @throws  BadMethodCallException
  365.      * @deprecated  4.0  Use directly JFeedFactory or supply SimplePie instead. Mehod will be proxied to JFeedFactory beginning in 3.2
  366.      */
  367.     public static function getFeedParser($url$cache_time 0)
  368.     {
  369.         if (!class_exists('JSimplepieFactory'))
  370.         {
  371.             throw new BadMethodCallException('JSimplepieFactory not found');
  372.         }
  373.  
  374.         JLog::add(__METHOD__ . ' is deprecated.   Use JFeedFactory() or supply SimplePie instead.'JLog::WARNING'deprecated');
  375.  
  376.         return JSimplepieFactory::getFeedParser($url$cache_time);
  377.     }
  378.  
  379.     /**
  380.      * Reads a XML file.
  381.      *
  382.      * @param   string   $data    Full path and file name.
  383.      * @param   boolean  $isFile  true to load a file or false to load a string.
  384.      *
  385.      * @return  mixed    JXMLElement or SimpleXMLElement on success or false on error.
  386.      *
  387.      * @see     JXMLElement
  388.      * @since   11.1
  389.      * @note    When JXMLElement is not present a SimpleXMLElement will be returned.
  390.      * @deprecated  13.3 (Platform) & 4.0 (CMS) - Use SimpleXML directly.
  391.      */
  392.     public static function getXML($data$isFile true)
  393.     {
  394.         JLog::add(__METHOD__ . ' is deprecated. Use SimpleXML directly.'JLog::WARNING'deprecated');
  395.  
  396.         $class 'SimpleXMLElement';
  397.  
  398.         if (class_exists('JXMLElement'))
  399.         {
  400.             $class 'JXMLElement';
  401.         }
  402.  
  403.         // Disable libxml errors and allow to fetch error information as needed
  404.         libxml_use_internal_errors(true);
  405.  
  406.         if ($isFile)
  407.         {
  408.             // Try to load the XML file
  409.             $xml simplexml_load_file($data$class);
  410.         }
  411.         else
  412.         {
  413.             // Try to load the XML string
  414.             $xml simplexml_load_string($data$class);
  415.         }
  416.  
  417.         if ($xml === false)
  418.         {
  419.             JLog::add(JText::_('JLIB_UTIL_ERROR_XML_LOAD')JLog::WARNING'jerror');
  420.  
  421.             if ($isFile)
  422.             {
  423.                 JLog::add($dataJLog::WARNING'jerror');
  424.             }
  425.  
  426.             foreach (libxml_get_errors(as $error)
  427.             {
  428.                 JLog::add($error->messageJLog::WARNING'jerror');
  429.             }
  430.         }
  431.  
  432.         return $xml;
  433.     }
  434.  
  435.     /**
  436.      * Get an editor object.
  437.      *
  438.      * @param   string  $editor  The editor to load, depends on the editor plugins that are installed
  439.      *
  440.      * @return  JEditor instance of JEditor
  441.      *
  442.      * @since   11.1
  443.      * @throws  BadMethodCallException
  444.      * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use JEditor directly
  445.      */
  446.     public static function getEditor($editor null)
  447.     {
  448.         JLog::add(__METHOD__ . ' is deprecated. Use JEditor directly.'JLog::WARNING'deprecated');
  449.  
  450.         if (!class_exists('JEditor'))
  451.         {
  452.             throw new BadMethodCallException('JEditor not found');
  453.         }
  454.  
  455.         // Get the editor configuration setting
  456.         if (is_null($editor))
  457.         {
  458.             $conf self::getConfig();
  459.             $editor $conf->get('editor');
  460.         }
  461.  
  462.         return JEditor::getInstance($editor);
  463.     }
  464.  
  465.     /**
  466.      * Return a reference to the {@link JUri} object
  467.      *
  468.      * @param   string  $uri  Uri name.
  469.      *
  470.      * @return  JUri object
  471.      *
  472.      * @see     JUri
  473.      * @since   11.1
  474.      * @deprecated  13.3 (Platform) & 4.0 (CMS) - Use JUri directly.
  475.      */
  476.     public static function getURI($uri 'SERVER')
  477.     {
  478.         JLog::add(__METHOD__ . ' is deprecated. Use JUri directly.'JLog::WARNING'deprecated');
  479.  
  480.         return JUri::getInstance($uri);
  481.     }
  482.  
  483.     /**
  484.      * Return the {@link JDate} object
  485.      *
  486.      * @param   mixed  $time      The initial time for the JDate object
  487.      * @param   mixed  $tzOffset  The timezone offset.
  488.      *
  489.      * @return  JDate object
  490.      *
  491.      * @see     JDate
  492.      * @since   11.1
  493.      */
  494.     public static function getDate($time 'now'$tzOffset null)
  495.     {
  496.         static $classname;
  497.         static $mainLocale;
  498.  
  499.         $language self::getLanguage();
  500.         $locale $language->getTag();
  501.  
  502.         if (!isset($classname|| $locale != $mainLocale)
  503.         {
  504.             // Store the locale for future reference
  505.             $mainLocale $locale;
  506.  
  507.             if ($mainLocale !== false)
  508.             {
  509.                 $classname str_replace('-''_'$mainLocale'Date';
  510.  
  511.                 if (!class_exists($classname))
  512.                 {
  513.                     // The class does not exist, default to JDate
  514.                     $classname 'JDate';
  515.                 }
  516.             }
  517.             else
  518.             {
  519.                 // No tag, so default to JDate
  520.                 $classname 'JDate';
  521.             }
  522.         }
  523.  
  524.         $key $time '-' ($tzOffset instanceof DateTimeZone $tzOffset->getName(: (string) $tzOffset);
  525.  
  526.         if (!isset(self::$dates[$classname][$key]))
  527.         {
  528.             self::$dates[$classname][$keynew $classname($time$tzOffset);
  529.         }
  530.  
  531.         $date clone self::$dates[$classname][$key];
  532.  
  533.         return $date;
  534.     }
  535.  
  536.     /**
  537.      * Create a configuration object
  538.      *
  539.      * @param   string  $file       The path to the configuration file.
  540.      * @param   string  $type       The type of the configuration file.
  541.      * @param   string  $namespace  The namespace of the configuration file.
  542.      *
  543.      * @return  JRegistry 
  544.      *
  545.      * @see     JRegistry
  546.      * @since   11.1
  547.      */
  548.     protected static function createConfig($file$type 'PHP'$namespace '')
  549.     {
  550.         if (is_file($file))
  551.         {
  552.             include_once $file;
  553.         }
  554.  
  555.         // Create the registry with a default namespace of config
  556.         $registry new JRegistry;
  557.  
  558.         // Sanitize the namespace.
  559.         $namespace ucfirst((string) preg_replace('/[^A-Z_]/i'''$namespace));
  560.  
  561.         // Build the config name.
  562.         $name 'JConfig' $namespace;
  563.  
  564.         // Handle the PHP configuration type.
  565.         if ($type == 'PHP' && class_exists($name))
  566.         {
  567.             // Create the JConfig object
  568.             $config new $name;
  569.  
  570.             // Load the configuration values into the registry
  571.             $registry->loadObject($config);
  572.         }
  573.  
  574.         return $registry;
  575.     }
  576.  
  577.     /**
  578.      * Create a session object
  579.      *
  580.      * @param   array  $options  An array containing session options
  581.      *
  582.      * @return  JSession object
  583.      *
  584.      * @since   11.1
  585.      */
  586.     protected static function createSession(array $options array())
  587.     {
  588.         // Get the editor configuration setting
  589.         $conf self::getConfig();
  590.         $handler $conf->get('session_handler''none');
  591.  
  592.         // Config time is in minutes
  593.         $options['expire'($conf->get('lifetime')) $conf->get('lifetime'60 900;
  594.  
  595.         $session JSession::getInstance($handler$options);
  596.  
  597.         if ($session->getState(== 'expired')
  598.         {
  599.             $session->restart();
  600.         }
  601.  
  602.         return $session;
  603.     }
  604.  
  605.     /**
  606.      * Create an database object
  607.      *
  608.      * @return  JDatabaseDriver 
  609.      *
  610.      * @see     JDatabaseDriver
  611.      * @since   11.1
  612.      */
  613.     protected static function createDbo()
  614.     {
  615.         $conf self::getConfig();
  616.  
  617.         $host $conf->get('host');
  618.         $user $conf->get('user');
  619.         $password $conf->get('password');
  620.         $database $conf->get('db');
  621.         $prefix $conf->get('dbprefix');
  622.         $driver $conf->get('dbtype');
  623.         $debug $conf->get('debug');
  624.  
  625.         $options array('driver' => $driver'host' => $host'user' => $user'password' => $password'database' => $database'prefix' => $prefix);
  626.  
  627.         try
  628.         {
  629.             $db JDatabaseDriver::getInstance($options);
  630.         }
  631.         catch (RuntimeException $e)
  632.         {
  633.             if (!headers_sent())
  634.             {
  635.                 header('HTTP/1.1 500 Internal Server Error');
  636.             }
  637.  
  638.             jexit('Database Error: ' $e->getMessage());
  639.         }
  640.  
  641.         $db->setDebug($debug);
  642.  
  643.         return $db;
  644.     }
  645.  
  646.     /**
  647.      * Create a mailer object
  648.      *
  649.      * @return  JMail object
  650.      *
  651.      * @see     JMail
  652.      * @since   11.1
  653.      */
  654.     protected static function createMailer()
  655.     {
  656.         $conf self::getConfig();
  657.  
  658.         $smtpauth ($conf->get('smtpauth'== 0null 1;
  659.         $smtpuser $conf->get('smtpuser');
  660.         $smtppass $conf->get('smtppass');
  661.         $smtphost $conf->get('smtphost');
  662.         $smtpsecure $conf->get('smtpsecure');
  663.         $smtpport $conf->get('smtpport');
  664.         $mailfrom $conf->get('mailfrom');
  665.         $fromname $conf->get('fromname');
  666.         $mailer $conf->get('mailer');
  667.  
  668.         // Create a JMail object
  669.         $mail JMail::getInstance();
  670.  
  671.         // Set default sender without Reply-to
  672.         $mail->SetFrom(JMailHelper::cleanLine($mailfrom)JMailHelper::cleanLine($fromname)0);
  673.  
  674.         // Default mailer is to use PHP's mail function
  675.         switch ($mailer)
  676.         {
  677.             case 'smtp':
  678.                 $mail->useSMTP($smtpauth$smtphost$smtpuser$smtppass$smtpsecure$smtpport);
  679.                 break;
  680.  
  681.             case 'sendmail':
  682.                 $mail->IsSendmail();
  683.                 break;
  684.  
  685.             default:
  686.                 $mail->IsMail();
  687.                 break;
  688.         }
  689.  
  690.         return $mail;
  691.     }
  692.  
  693.     /**
  694.      * Create a language object
  695.      *
  696.      * @return  JLanguage object
  697.      *
  698.      * @see     JLanguage
  699.      * @since   11.1
  700.      */
  701.     protected static function createLanguage()
  702.     {
  703.         $conf self::getConfig();
  704.         $locale $conf->get('language');
  705.         $debug $conf->get('debug_lang');
  706.         $lang JLanguage::getInstance($locale$debug);
  707.  
  708.         return $lang;
  709.     }
  710.  
  711.     /**
  712.      * Create a document object
  713.      *
  714.      * @return  JDocument object
  715.      *
  716.      * @see     JDocument
  717.      * @since   11.1
  718.      */
  719.     protected static function createDocument()
  720.     {
  721.         $lang self::getLanguage();
  722.  
  723.         $input self::getApplication()->input;
  724.         $type $input->get('format''html''word');
  725.  
  726.         $version new JVersion;
  727.  
  728.         $attributes array(
  729.             'charset' => 'utf-8',
  730.             'lineend' => 'unix',
  731.             'tab' => '  ',
  732.             'language' => $lang->getTag(),
  733.             'direction' => $lang->isRTL('rtl' 'ltr',
  734.             'mediaversion' => $version->getMediaVersion()
  735.         );
  736.  
  737.         return JDocument::getInstance($type$attributes);
  738.     }
  739.  
  740.     /**
  741.      * Creates a new stream object with appropriate prefix
  742.      *
  743.      * @param   boolean  $use_prefix   Prefix the connections for writing
  744.      * @param   boolean  $use_network  Use network if available for writing; use false to disable (e.g. FTP, SCP)
  745.      * @param   string   $ua           UA User agent to use
  746.      * @param   boolean  $uamask       User agent masking (prefix Mozilla)
  747.      *
  748.      * @return  JStream 
  749.      *
  750.      * @see     JStream
  751.      * @since   11.1
  752.      */
  753.     public static function getStream($use_prefix true$use_network true$ua null$uamask false)
  754.     {
  755.         jimport('joomla.filesystem.stream');
  756.  
  757.         // Setup the context; Joomla! UA and overwrite
  758.         $context array();
  759.         $version new JVersion;
  760.  
  761.         // Set the UA for HTTP and overwrite for FTP
  762.         $context['http']['user_agent'$version->getUserAgent($ua$uamask);
  763.         $context['ftp']['overwrite'true;
  764.  
  765.         if ($use_prefix)
  766.         {
  767.             $FTPOptions JClientHelper::getCredentials('ftp');
  768.             $SCPOptions JClientHelper::getCredentials('scp');
  769.  
  770.             if ($FTPOptions['enabled'== && $use_network)
  771.             {
  772.                 $prefix 'ftp://' $FTPOptions['user'':' $FTPOptions['pass''@' $FTPOptions['host'];
  773.                 $prefix .= $FTPOptions['port'':' $FTPOptions['port''';
  774.                 $prefix .= $FTPOptions['root'];
  775.             }
  776.             elseif ($SCPOptions['enabled'== && $use_network)
  777.             {
  778.                 $prefix 'ssh2.sftp://' $SCPOptions['user'':' $SCPOptions['pass''@' $SCPOptions['host'];
  779.                 $prefix .= $SCPOptions['port'':' $SCPOptions['port''';
  780.                 $prefix .= $SCPOptions['root'];
  781.             }
  782.             else
  783.             {
  784.                 $prefix JPATH_ROOT '/';
  785.             }
  786.  
  787.             $retval new JStream($prefixJPATH_ROOT$context);
  788.         }
  789.         else
  790.         {
  791.             $retval new JStream(''''$context);
  792.         }
  793.  
  794.         return $retval;
  795.     }
  796. }

Documentation generated on Tue, 19 Nov 2013 15:02:49 +0100 by phpDocumentor 1.4.3