Source for file joomla.php

Documentation is available at joomla.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  platform
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. /**
  12.  * Part of the FOF Platform Abstraction Layer.
  13.  *
  14.  * This implements the platform class for Joomla! 2.5 or later
  15.  *
  16.  * @package  FrameworkOnFramework
  17.  * @since    2.1
  18.  */
  19. class FOFPlatformJoomla extends FOFPlatform implements FOFPlatformInterface
  20. {
  21.     private $_cache null;
  22.  
  23.     /**
  24.      * Is this platform enabled?
  25.      *
  26.      * @see FOFPlatformInterface::isEnabled()
  27.      *
  28.      * @return  boolean 
  29.      */
  30.     public function isEnabled()
  31.     {
  32.         if (is_null($this->isEnabled))
  33.         {
  34.             $this->isEnabled = true;
  35.  
  36.             // Make sure _JEXEC is defined
  37.             if (!defined('_JEXEC'))
  38.             {
  39.                 $this->isEnabled = false;
  40.             }
  41.  
  42.             // We need JVERSION to be defined
  43.             if ($this->isEnabled)
  44.             {
  45.                 if (!defined('JVERSION'))
  46.                 {
  47.                     $this->isEnabled = false;
  48.                 }
  49.             }
  50.  
  51.             // Check if JFactory exists
  52.             if ($this->isEnabled)
  53.             {
  54.                 if (!class_exists('JFactory'))
  55.                 {
  56.                     $this->isEnabled = false;
  57.                 }
  58.             }
  59.  
  60.             // Check if JApplication exists
  61.             if ($this->isEnabled)
  62.             {
  63.                 $appExists class_exists('JApplication');
  64.                 $appExists $appExists || class_exists('JCli');
  65.                 $appExists $appExists || class_exists('JApplicationCli');
  66.  
  67.                 if (!$appExists)
  68.                 {
  69.                     $this->isEnabled = false;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         return $this->isEnabled;
  75.     }
  76.  
  77.     /**
  78.      * Main function to detect if we're running in a CLI environment and we're admin
  79.      *
  80.      * @return  array  isCLI and isAdmin. It's not an associtive array, so we can use list.
  81.      */
  82.     protected function isCliAdmin()
  83.     {
  84.         static $isCLI   null;
  85.         static $isAdmin null;
  86.  
  87.         if (is_null($isCLI&& is_null($isAdmin))
  88.         {
  89.             try
  90.             {
  91.                 if (is_null(JFactory::$application))
  92.                 {
  93.                     $isCLI true;
  94.                 }
  95.                 else
  96.                 {
  97.                     $isCLI JFactory::getApplication(instanceof JException;
  98.                 }
  99.             }
  100.             catch (Exception $e)
  101.             {
  102.                 $isCLI true;
  103.             }
  104.  
  105.             if ($isCLI)
  106.             {
  107.                 $isAdmin false;
  108.             }
  109.             else
  110.             {
  111.                 $isAdmin !JFactory::$application false JFactory::getApplication()->isAdmin();
  112.             }
  113.         }
  114.  
  115.         return array($isCLI$isAdmin);
  116.     }
  117.  
  118.     /**
  119.      * Returns the base (root) directories for a given component.
  120.      *
  121.      * @param   string  $component  The name of the component. For Joomla! this
  122.      *                               is something like "com_example"
  123.      *
  124.      * @see FOFPlatformInterface::getComponentBaseDirs()
  125.      *
  126.      * @return  array  A hash array with keys main, alt, site and admin.
  127.      */
  128.     public function getComponentBaseDirs($component)
  129.     {
  130.         if ($this->isFrontend())
  131.         {
  132.             $mainPath    JPATH_SITE '/components/' $component;
  133.             $altPath    JPATH_ADMINISTRATOR '/components/' $component;
  134.         }
  135.         else
  136.         {
  137.             $mainPath    JPATH_ADMINISTRATOR '/components/' $component;
  138.             $altPath    JPATH_SITE '/components/' $component;
  139.         }
  140.  
  141.         return array(
  142.             'main'    => $mainPath,
  143.             'alt'    => $altPath,
  144.             'site'    => JPATH_SITE '/components/' $component,
  145.             'admin'    => JPATH_ADMINISTRATOR '/components/' $component,
  146.         );
  147.     }
  148.  
  149.     /**
  150.      * Return a list of the view template paths for this component.
  151.      *
  152.      * @param   string   $component  The name of the component. For Joomla! this
  153.      *                                is something like "com_example"
  154.      * @param   string   $view       The name of the view you're looking a
  155.      *                                template for
  156.      * @param   string   $layout     The layout name to load, e.g. 'default'
  157.      * @param   string   $tpl        The sub-template name to load (null by default)
  158.      * @param   boolean  $strict     If true, only the specified layout will be searched for.
  159.      *                                Otherwise we'll fall back to the 'default' layout if the
  160.      *                                specified layout is not found.
  161.      *
  162.      * @see FOFPlatformInterface::getViewTemplateDirs()
  163.      *
  164.      * @return  array 
  165.      */
  166.     public function getViewTemplatePaths($component$view$layout 'default'$tpl null$strict false)
  167.     {
  168.         $isAdmin $this->isBackend();
  169.  
  170.         $basePath $isAdmin 'admin:' 'site:';
  171.         $basePath .= $component '/';
  172.         $altBasePath $basePath;
  173.         $basePath .= $view '/';
  174.         $altBasePath .= (FOFInflector::isSingular($viewFOFInflector::pluralize($viewFOFInflector::singularize($view)) '/';
  175.  
  176.         if ($strict)
  177.         {
  178.             $paths array(
  179.                 $basePath $layout ($tpl "_$tpl''),
  180.                 $altBasePath $layout ($tpl "_$tpl''),
  181.             );
  182.         }
  183.         else
  184.         {
  185.             $paths array(
  186.                 $basePath $layout ($tpl "_$tpl''),
  187.                 $basePath $layout,
  188.                 $basePath 'default' ($tpl "_$tpl''),
  189.                 $basePath 'default',
  190.                 $altBasePath $layout ($tpl "_$tpl''),
  191.                 $altBasePath $layout,
  192.                 $altBasePath 'default' ($tpl "_$tpl''),
  193.                 $altBasePath 'default',
  194.             );
  195.             $paths array_unique($paths);
  196.         }
  197.  
  198.         return $paths;
  199.     }
  200.  
  201.     /**
  202.      * Get application-specific suffixes to use with template paths. This allows
  203.      * you to look for view template overrides based on the application version.
  204.      *
  205.      * @return  array  A plain array of suffixes to try in template names
  206.      */
  207.     public function getTemplateSuffixes()
  208.     {
  209.         $jversion new JVersion;
  210.         $versionParts explode('.'$jversion->RELEASE);
  211.         $majorVersion array_shift($versionParts);
  212.         $suffixes array(
  213.             '.j' str_replace('.'''$jversion->getHelpVersion()),
  214.             '.j' $majorVersion,
  215.         );
  216.  
  217.         return $suffixes;
  218.     }
  219.  
  220.     /**
  221.      * Return the absolute path to the application's template overrides
  222.      * directory for a specific component. We will use it to look for template
  223.      * files instead of the regular component directorues. If the application
  224.      * does not have such a thing as template overrides return an empty string.
  225.      *
  226.      * @param   string   $component  The name of the component for which to fetch the overrides
  227.      * @param   boolean  $absolute   Should I return an absolute or relative path?
  228.      *
  229.      * @return  string  The path to the template overrides directory
  230.      */
  231.     public function getTemplateOverridePath($component$absolute true)
  232.     {
  233.         list($isCli$isAdmin$this->isCliAdmin();
  234.  
  235.         if (!$isCli)
  236.         {
  237.             if ($absolute)
  238.             {
  239.                 $path JPATH_THEMES '/';
  240.             }
  241.             else
  242.             {
  243.                 $path $isAdmin 'administrator/templates/' 'templates/';
  244.             }
  245.  
  246.             if (substr($component07== 'media:/')
  247.             {
  248.                 $directory 'media/' substr($component7);
  249.             }
  250.             else
  251.             {
  252.                 $directory 'html/' $component;
  253.             }
  254.  
  255.             $path .= JFactory::getApplication()->getTemplate(.
  256.                 '/' $directory;
  257.         }
  258.         else
  259.         {
  260.             $path '';
  261.         }
  262.  
  263.         return $path;
  264.     }
  265.  
  266.     /**
  267.      * Load the translation files for a given component.
  268.      *
  269.      * @param   string  $component  The name of the component. For Joomla! this
  270.      *                               is something like "com_example"
  271.      *
  272.      * @see FOFPlatformInterface::loadTranslations()
  273.      *
  274.      * @return  void 
  275.      */
  276.     public function loadTranslations($component)
  277.     {
  278.         if ($this->isBackend())
  279.         {
  280.             $paths array(JPATH_ROOTJPATH_ADMINISTRATOR);
  281.         }
  282.         else
  283.         {
  284.             $paths array(JPATH_ADMINISTRATORJPATH_ROOT);
  285.         }
  286.  
  287.         $jlang JFactory::getLanguage();
  288.         $jlang->load($component$paths[0]'en-GB'true);
  289.         $jlang->load($component$paths[0]nulltrue);
  290.         $jlang->load($component$paths[1]'en-GB'true);
  291.         $jlang->load($component$paths[1]nulltrue);
  292.     }
  293.  
  294.     /**
  295.      * Authorise access to the component in the back-end.
  296.      *
  297.      * @param   string  $component  The name of the component.
  298.      *
  299.      * @see FOFPlatformInterface::authorizeAdmin()
  300.      *
  301.      * @return  boolean  True to allow loading the component, false to halt loading
  302.      */
  303.     public function authorizeAdmin($component)
  304.     {
  305.         if ($this->isBackend())
  306.         {
  307.             // Master access check for the back-end, Joomla! 1.6 style.
  308.             $user JFactory::getUser();
  309.  
  310.             if (!$user->authorise('core.manage'$component)
  311.                 && !$user->authorise('core.admin'$component))
  312.             {
  313.                 return false;
  314.             }
  315.         }
  316.  
  317.         return true;
  318.     }
  319.  
  320.     /**
  321.      * Return a user object.
  322.      *
  323.      * @param   integer  $id  The user ID to load. Skip or use null to retrieve
  324.      *                         the object for the currently logged in user.
  325.      *
  326.      * @see FOFPlatformInterface::getUser()
  327.      *
  328.      * @return  JUser  The JUser object for the specified user
  329.      */
  330.     public function getUser($id null)
  331.     {
  332.         return JFactory::getUser($id);
  333.     }
  334.  
  335.     /**
  336.      * Returns the JDocument object which handles this component's response.
  337.      *
  338.      * @see FOFPlatformInterface::getDocument()
  339.      *
  340.      * @return  JDocument 
  341.      */
  342.     public function getDocument()
  343.     {
  344.         $document null;
  345.  
  346.         if (!$this->isCli())
  347.         {
  348.             try
  349.             {
  350.                 $document JFactory::getDocument();
  351.             }
  352.             catch (Exception $exc)
  353.             {
  354.                 $document null;
  355.             }
  356.         }
  357.  
  358.         return $document;
  359.     }
  360.  
  361.     /**
  362.      * This method will try retrieving a variable from the request (input) data.
  363.      *
  364.      * @param   string    $key           The user state key for the variable
  365.      * @param   string    $request       The request variable name for the variable
  366.      * @param   FOFInput  $input         The FOFInput object with the request (input) data
  367.      * @param   mixed     $default       The default value. Default: null
  368.      * @param   string    $type          The filter type for the variable data. Default: none (no filtering)
  369.      * @param   boolean   $setUserState  Should I set the user state with the fetched value?
  370.      *
  371.      * @see FOFPlatformInterface::getUserStateFromRequest()
  372.      *
  373.      * @return  mixed  The value of the variable
  374.      */
  375.     public function getUserStateFromRequest($key$request$input$default null$type 'none'$setUserState true)
  376.     {
  377.         list($isCLI$isAdmin$this->isCliAdmin();
  378.  
  379.         if ($isCLI)
  380.         {
  381.             return $input->get($request$default$type);
  382.         }
  383.  
  384.         $app JFactory::getApplication();
  385.  
  386.         if (method_exists($app'getUserState'))
  387.         {
  388.             $old_state $app->getUserState($key$default);
  389.         }
  390.         else
  391.         {
  392.             $old_state null;
  393.         }
  394.  
  395.         $cur_state (!is_null($old_state)) $old_state $default;
  396.         $new_state $input->get($requestnull$type);
  397.  
  398.         // Save the new value only if it was set in this request
  399.         if ($setUserState)
  400.         {
  401.             if ($new_state !== null)
  402.             {
  403.                 $app->setUserState($key$new_state);
  404.             }
  405.             else
  406.             {
  407.                 $new_state $cur_state;
  408.             }
  409.         }
  410.         elseif (is_null($new_state))
  411.         {
  412.             $new_state $cur_state;
  413.         }
  414.  
  415.         return $new_state;
  416.     }
  417.  
  418.     /**
  419.      * Load plugins of a specific type. Obviously this seems to only be required
  420.      * in the Joomla! CMS.
  421.      *
  422.      * @param   string  $type  The type of the plugins to be loaded
  423.      *
  424.      * @see FOFPlatformInterface::importPlugin()
  425.      *
  426.      * @return void 
  427.      */
  428.     public function importPlugin($type)
  429.     {
  430.         if (!$this->isCli())
  431.         {
  432.             JPluginHelper::importPlugin($type);
  433.         }
  434.     }
  435.  
  436.     /**
  437.      * Execute plugins (system-level triggers) and fetch back an array with
  438.      * their return values.
  439.      *
  440.      * @param   string  $event  The event (trigger) name, e.g. onBeforeScratchMyEar
  441.      * @param   array   $data   A hash array of data sent to the plugins as part of the trigger
  442.      *
  443.      * @see FOFPlatformInterface::runPlugins()
  444.      *
  445.      * @return  array  A simple array containing the resutls of the plugins triggered
  446.      */
  447.     public function runPlugins($event$data)
  448.     {
  449.         if (!$this->isCli())
  450.         {
  451.             $dispatcher JDispatcher::getInstance();
  452.  
  453.             return $dispatcher->trigger($event$data);
  454.         }
  455.         else
  456.         {
  457.             return array();
  458.         }
  459.     }
  460.  
  461.     /**
  462.      * Perform an ACL check.
  463.      *
  464.      * @param   string  $action     The ACL privilege to check, e.g. core.edit
  465.      * @param   string  $assetname  The asset name to check, typically the component's name
  466.      *
  467.      * @see FOFPlatformInterface::authorise()
  468.      *
  469.      * @return  boolean  True if the user is allowed this action
  470.      */
  471.     public function authorise($action$assetname)
  472.     {
  473.         if ($this->isCli())
  474.         {
  475.             return true;
  476.         }
  477.  
  478.         return JFactory::getUser()->authorise($action$assetname);
  479.     }
  480.  
  481.     /**
  482.      * Is this the administrative section of the component?
  483.      *
  484.      * @see FOFPlatformInterface::isBackend()
  485.      *
  486.      * @return  boolean 
  487.      */
  488.     public function isBackend()
  489.     {
  490.         list ($isCli$isAdmin$this->isCliAdmin();
  491.  
  492.         return $isAdmin && !$isCli;
  493.     }
  494.  
  495.     /**
  496.      * Is this the public section of the component?
  497.      *
  498.      * @see FOFPlatformInterface::isFrontend()
  499.      *
  500.      * @return  boolean 
  501.      */
  502.     public function isFrontend()
  503.     {
  504.         list ($isCli$isAdmin$this->isCliAdmin();
  505.  
  506.         return !$isAdmin && !$isCli;
  507.     }
  508.  
  509.     /**
  510.      * Is this a component running in a CLI application?
  511.      *
  512.      * @see FOFPlatformInterface::isCli()
  513.      *
  514.      * @return  boolean 
  515.      */
  516.     public function isCli()
  517.     {
  518.         list ($isCli$isAdmin$this->isCliAdmin();
  519.  
  520.         return !$isAdmin && $isCli;
  521.     }
  522.  
  523.     /**
  524.      * Is AJAX re-ordering supported? This is 100% Joomla!-CMS specific. All
  525.      * other platforms should return false and never ask why.
  526.      *
  527.      * @see FOFPlatformInterface::supportsAjaxOrdering()
  528.      *
  529.      * @return  boolean 
  530.      */
  531.     public function supportsAjaxOrdering()
  532.     {
  533.         return $this->checkVersion(JVERSION'3.0''ge');
  534.     }
  535.  
  536.     /**
  537.      * Is the global FOF cache enabled?
  538.      *
  539.      * @return  boolean 
  540.      */
  541.     public function isGlobalFOFCacheEnabled()
  542.     {
  543.         return !(defined('JDEBUG'&& JDEBUG);
  544.     }
  545.  
  546.     /**
  547.      * Saves something to the cache. This is supposed to be used for system-wide
  548.      * FOF data, not application data.
  549.      *
  550.      * @param   string  $key      The key of the data to save
  551.      * @param   string  $content  The actual data to save
  552.      *
  553.      * @return  boolean  True on success
  554.      */
  555.     public function setCache($key$content)
  556.     {
  557.         $registry $this->getCacheObject();
  558.  
  559.         $registry->set($key$content);
  560.  
  561.         return $this->saveCache();
  562.     }
  563.  
  564.     /**
  565.      * Retrieves data from the cache. This is supposed to be used for system-side
  566.      * FOF data, not application data.
  567.      *
  568.      * @param   string  $key      The key of the data to retrieve
  569.      * @param   string  $default  The default value to return if the key is not found or the cache is not populated
  570.      *
  571.      * @return  string  The cached value
  572.      */
  573.     public function getCache($key$default null)
  574.     {
  575.         $registry $this->getCacheObject();
  576.  
  577.         return $registry->get($key$default);
  578.     }
  579.  
  580.     /**
  581.      * Gets a reference to the cache object, loading it from the disk if
  582.      * needed.
  583.      *
  584.      * @param   boolean  $force  Should I forcibly reload the registry?
  585.      *
  586.      * @return  JRegistry 
  587.      */
  588.     private function &getCacheObject($force false)
  589.     {
  590.         // Check if we have to load the cache file or we are forced to do that
  591.         if (is_null($this->_cache|| $force)
  592.         {
  593.             // Create a new JRegistry object
  594.             JLoader::import('joomla.registry.registry');
  595.             $this->_cache new JRegistry;
  596.  
  597.             // Try to get data from Joomla!'s cache
  598.             $cache JFactory::getCache('fof''');
  599.             $data $cache->get('cache''fof');
  600.  
  601.             // If data is not found, fall back to the legacy (FOF 2.1.rc3 and earlier) method
  602.             if ($data === false)
  603.             {
  604.                 // Find the path to the file
  605.                 $cachePath JPATH_CACHE '/fof';
  606.                 $filename  $cachePath '/cache.php';
  607.  
  608.                 JLoader::import('joomla.filesystem.file');
  609.  
  610.                 // Load the cache file if it exists. JRegistryFormatPHP fails
  611.                 // miserably, so I have to work around it.
  612.                 if (JFile::exists($filename))
  613.                 {
  614.                     @include_once $filename;
  615.  
  616.                     JFile::delete($filename);
  617.  
  618.                     $className 'FOFCacheStorage';
  619.  
  620.                     if (class_exists($className))
  621.                     {
  622.                         $object new $className;
  623.                         $this->_cache->loadObject($object);
  624.  
  625.                         $options array(
  626.                             'class' => 'FOFCacheStorage'
  627.                         );
  628.                         $cache->store($this->_cache'cache''fof');
  629.                     }
  630.                 }
  631.             }
  632.             else
  633.             {
  634.                 $this->_cache $data;
  635.             }
  636.         }
  637.  
  638.         return $this->_cache;
  639.     }
  640.  
  641.     /**
  642.      * Save the cache object back to disk
  643.      *
  644.      * @return  boolean  True on success
  645.      */
  646.     private function saveCache()
  647.     {
  648.         // Get the JRegistry object of our cached data
  649.         $registry $this->getCacheObject();
  650.  
  651.         $cache JFactory::getCache('fof''');
  652.         return $cache->store($registry'cache''fof');
  653.     }
  654.  
  655.     /**
  656.      * Clears the cache of system-wide FOF data. You are supposed to call this in
  657.      * your components' installation script post-installation and post-upgrade
  658.      * methods or whenever you are modifying the structure of database tables
  659.      * accessed by FOF. Please note that FOF's cache never expires and is not
  660.      * purged by Joomla!. You MUST use this method to manually purge the cache.
  661.      *
  662.      * @return  boolean  True on success
  663.      */
  664.     public function clearCache()
  665.     {
  666.         $false false;
  667.         $cache JFactory::getCache('fof''');
  668.         $cache->store($false'cache''fof');
  669.     }
  670.  
  671.     /**
  672.      * logs in a user
  673.      *
  674.      * @param   array  $authInfo  authentification information
  675.      *
  676.      * @return  boolean  True on success
  677.      */
  678.     public function loginUser($authInfo)
  679.     {
  680.         JLoader::import('joomla.user.authentication');
  681.         $options array('remember'         => false);
  682.         $authenticate JAuthentication::getInstance();
  683.         $response $authenticate->authenticate($authInfo$options);
  684.  
  685.         if ($response->status == JAuthentication::STATUS_SUCCESS)
  686.         {
  687.             $this->importPlugin('user');
  688.             $results $this->runPlugins('onLoginUser'array((array) $response$options));
  689.  
  690.             JLoader::import('joomla.user.helper');
  691.             $userid JUserHelper::getUserId($response->username);
  692.             $user $this->getUser($userid);
  693.  
  694.             $session JFactory::getSession();
  695.             $session->set('user'$user);
  696.  
  697.             return true;
  698.         }
  699.  
  700.         return false;
  701.     }
  702.  
  703.     /**
  704.      * logs out a user
  705.      *
  706.      * @return  boolean  True on success
  707.      */
  708.     public function logoutUser()
  709.     {
  710.         JLoader::import('joomla.user.authentication');
  711.         $app JFactory::getApplication();
  712.         $options array('remember'     => false);
  713.         $parameters array('username'     => $this->getUser()->username);
  714.  
  715.         return $app->triggerEvent('onLogoutUser'array($parameters$options));
  716.     }
  717. }

Documentation generated on Tue, 19 Nov 2013 15:06:10 +0100 by phpDocumentor 1.4.3