Source for file helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Component
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Component helper class
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Component
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * The component list cache
  22.      *
  23.      * @var    array 
  24.      * @since  1.6
  25.      */
  26.     protected static $components array();
  27.  
  28.     /**
  29.      * Get the component information.
  30.      *
  31.      * @param   string   $option  The component option.
  32.      * @param   boolean  $strict  If set and the component does not exist, the enabled attribute will be set to false.
  33.      *
  34.      * @return  object   An object with the information for the component.
  35.      *
  36.      * @since   1.5
  37.      */
  38.     public static function getComponent($option$strict false)
  39.     {
  40.         if (!isset(static::$components[$option]))
  41.         {
  42.             if (static::load($option))
  43.             {
  44.                 $result static::$components[$option];
  45.             }
  46.             else
  47.             {
  48.                 $result new stdClass;
  49.                 $result->enabled $strict false true;
  50.                 $result->params new JRegistry;
  51.             }
  52.         }
  53.         else
  54.         {
  55.             $result static::$components[$option];
  56.         }
  57.  
  58.         return $result;
  59.     }
  60.  
  61.     /**
  62.      * Checks if the component is enabled
  63.      *
  64.      * @param   string  $option  The component option.
  65.      *
  66.      * @return  boolean 
  67.      *
  68.      * @since   1.5
  69.      */
  70.     public static function isEnabled($option)
  71.     {
  72.         $result static::getComponent($optiontrue);
  73.  
  74.         return $result->enabled;
  75.     }
  76.  
  77.     /**
  78.      * Gets the parameter object for the component
  79.      *
  80.      * @param   string   $option  The option for the component.
  81.      * @param   boolean  $strict  If set and the component does not exist, false will be returned
  82.      *
  83.      * @return  JRegistry  A JRegistry object.
  84.      *
  85.      * @see     JRegistry
  86.      * @since   1.5
  87.      */
  88.     public static function getParams($option$strict false)
  89.     {
  90.         $component static::getComponent($option$strict);
  91.  
  92.         return $component->params;
  93.     }
  94.  
  95.     /**
  96.      * Applies the global text filters to arbitrary text as per settings for current user groups
  97.      *
  98.      * @param   string  $text  The string to filter
  99.      *
  100.      * @return  string  The filtered string
  101.      *
  102.      * @since   2.5
  103.      */
  104.     public static function filterText($text)
  105.     {
  106.         // Filter settings
  107.         $config     static::getParams('com_config');
  108.         $user       JFactory::getUser();
  109.         $userGroups JAccess::getGroupsByUser($user->get('id'));
  110.  
  111.         $filters $config->get('filters');
  112.  
  113.         $blackListTags       array();
  114.         $blackListAttributes array();
  115.  
  116.         $customListTags       array();
  117.         $customListAttributes array();
  118.  
  119.         $whiteListTags       array();
  120.         $whiteListAttributes array();
  121.  
  122.         $whiteList  false;
  123.         $blackList  false;
  124.         $customList false;
  125.         $unfiltered false;
  126.  
  127.         // Cycle through each of the user groups the user is in.
  128.         // Remember they are included in the Public group as well.
  129.         foreach ($userGroups as $groupId)
  130.         {
  131.             // May have added a group by not saved the filters.
  132.             if (!isset($filters->$groupId))
  133.             {
  134.                 continue;
  135.             }
  136.  
  137.             // Each group the user is in could have different filtering properties.
  138.             $filterData $filters->$groupId;
  139.             $filterType strtoupper($filterData->filter_type);
  140.  
  141.             if ($filterType == 'NH')
  142.             {
  143.                 // Maximum HTML filtering.
  144.             }
  145.             elseif ($filterType == 'NONE')
  146.             {
  147.                 // No HTML filtering.
  148.                 $unfiltered true;
  149.             }
  150.             else
  151.             {
  152.                 // Black or white list.
  153.                 // Preprocess the tags and attributes.
  154.                 $tags           explode(','$filterData->filter_tags);
  155.                 $attributes     explode(','$filterData->filter_attributes);
  156.                 $tempTags       array();
  157.                 $tempAttributes array();
  158.  
  159.                 foreach ($tags as $tag)
  160.                 {
  161.                     $tag trim($tag);
  162.  
  163.                     if ($tag)
  164.                     {
  165.                         $tempTags[$tag;
  166.                     }
  167.                 }
  168.  
  169.                 foreach ($attributes as $attribute)
  170.                 {
  171.                     $attribute trim($attribute);
  172.  
  173.                     if ($attribute)
  174.                     {
  175.                         $tempAttributes[$attribute;
  176.                     }
  177.                 }
  178.  
  179.                 // Collect the black or white list tags and attributes.
  180.                 // Each list is cummulative.
  181.                 if ($filterType == 'BL')
  182.                 {
  183.                     $blackList           true;
  184.                     $blackListTags       array_merge($blackListTags$tempTags);
  185.                     $blackListAttributes array_merge($blackListAttributes$tempAttributes);
  186.                 }
  187.                 elseif ($filterType == 'CBL')
  188.                 {
  189.                     // Only set to true if Tags or Attributes were added
  190.                     if ($tempTags || $tempAttributes)
  191.                     {
  192.                         $customList           true;
  193.                         $customListTags       array_merge($customListTags$tempTags);
  194.                         $customListAttributes array_merge($customListAttributes$tempAttributes);
  195.                     }
  196.                 }
  197.                 elseif ($filterType == 'WL')
  198.                 {
  199.                     $whiteList           true;
  200.                     $whiteListTags       array_merge($whiteListTags$tempTags);
  201.                     $whiteListAttributes array_merge($whiteListAttributes$tempAttributes);
  202.                 }
  203.             }
  204.         }
  205.  
  206.         // Remove duplicates before processing (because the black list uses both sets of arrays).
  207.         $blackListTags        array_unique($blackListTags);
  208.         $blackListAttributes  array_unique($blackListAttributes);
  209.         $customListTags       array_unique($customListTags);
  210.         $customListAttributes array_unique($customListAttributes);
  211.         $whiteListTags        array_unique($whiteListTags);
  212.         $whiteListAttributes  array_unique($whiteListAttributes);
  213.  
  214.         // Unfiltered assumes first priority.
  215.         if ($unfiltered)
  216.         {
  217.             // Dont apply filtering.
  218.         }
  219.         else
  220.         {
  221.             // Custom blacklist precedes Default blacklist
  222.             if ($customList)
  223.             {
  224.                 $filter JFilterInput::getInstance(array()array()11);
  225.  
  226.                 // Override filter's default blacklist tags and attributes
  227.                 if ($customListTags)
  228.                 {
  229.                     $filter->tagBlacklist $customListTags;
  230.                 }
  231.                 if ($customListAttributes)
  232.                 {
  233.                     $filter->attrBlacklist $customListAttributes;
  234.                 }
  235.             }
  236.             // Black lists take second precedence.
  237.             elseif ($blackList)
  238.             {
  239.                 // Remove the white-listed tags and attributes from the black-list.
  240.                 $blackListTags       array_diff($blackListTags$whiteListTags);
  241.                 $blackListAttributes array_diff($blackListAttributes$whiteListAttributes);
  242.  
  243.                 $filter JFilterInput::getInstance($blackListTags$blackListAttributes11);
  244.  
  245.                 // Remove white listed tags from filter's default blacklist
  246.                 if ($whiteListTags)
  247.                 {
  248.                     $filter->tagBlacklist array_diff($filter->tagBlacklist$whiteListTags);
  249.                 }
  250.                 // Remove white listed attributes from filter's default blacklist
  251.                 if ($whiteListAttributes)
  252.                 {
  253.                     $filter->attrBlacklist array_diff($filter->attrBlacklist);
  254.                 }
  255.             }
  256.             // White lists take third precedence.
  257.             elseif ($whiteList)
  258.             {
  259.                 // Turn off XSS auto clean
  260.                 $filter JFilterInput::getInstance($whiteListTags$whiteListAttributes000);
  261.             }
  262.             // No HTML takes last place.
  263.             else
  264.             {
  265.                 $filter JFilterInput::getInstance();
  266.             }
  267.  
  268.             $text $filter->clean($text'html');
  269.         }
  270.  
  271.         return $text;
  272.     }
  273.  
  274.     /**
  275.      * Render the component.
  276.      *
  277.      * @param   string  $option  The component option.
  278.      * @param   array   $params  The component parameters
  279.      *
  280.      * @return  object 
  281.      *
  282.      * @since   1.5
  283.      * @throws  Exception
  284.      */
  285.     public static function renderComponent($option$params array())
  286.     {
  287.         $app JFactory::getApplication();
  288.  
  289.         // Load template language files.
  290.         $template $app->getTemplate(true)->template;
  291.         $lang JFactory::getLanguage();
  292.         $lang->load('tpl_' $templateJPATH_BASEnullfalsetrue)
  293.             || $lang->load('tpl_' $templateJPATH_THEMES "/$template"nullfalsetrue);
  294.  
  295.         if (empty($option))
  296.         {
  297.             throw new Exception(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND')404);
  298.         }
  299.  
  300.         // Record the scope
  301.         $scope $app->scope;
  302.  
  303.         // Set scope to component name
  304.         $app->scope $option;
  305.  
  306.         // Build the component path.
  307.         $option preg_replace('/[^A-Z0-9_\.-]/i'''$option);
  308.         $file substr($option4);
  309.  
  310.         // Define component path.
  311.         define('JPATH_COMPONENT'JPATH_BASE '/components/' $option);
  312.         define('JPATH_COMPONENT_SITE'JPATH_SITE '/components/' $option);
  313.         define('JPATH_COMPONENT_ADMINISTRATOR'JPATH_ADMINISTRATOR '/components/' $option);
  314.  
  315.         $path JPATH_COMPONENT '/' $file '.php';
  316.  
  317.         // If component is disabled throw error
  318.         if (!static::isEnabled($option|| !file_exists($path))
  319.         {
  320.             throw new Exception(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND')404);
  321.         }
  322.  
  323.         // Load common and local language files.
  324.         $lang->load($optionJPATH_BASEnullfalsetrue|| $lang->load($optionJPATH_COMPONENTnullfalsetrue);
  325.  
  326.         // Handle template preview outlining.
  327.         $contents null;
  328.  
  329.         // Execute the component.
  330.         $contents static::executeComponent($path);
  331.  
  332.         // Revert the scope
  333.         $app->scope $scope;
  334.  
  335.         return $contents;
  336.     }
  337.  
  338.     /**
  339.      * Execute the component.
  340.      *
  341.      * @param   string  $path  The component path.
  342.      *
  343.      * @return  string  The component output
  344.      *
  345.      * @since   1.7
  346.      */
  347.     protected static function executeComponent($path)
  348.     {
  349.         ob_start();
  350.         require_once $path;
  351.         $contents ob_get_clean();
  352.  
  353.         return $contents;
  354.     }
  355.  
  356.     /**
  357.      * Load the installed components into the components property.
  358.      *
  359.      * @param   string  $option  The element value for the extension
  360.      *
  361.      * @return  boolean  True on success
  362.      *
  363.      * @since   1.5
  364.      * @deprecated  4.0  Use JComponentHelper::load() instead
  365.      */
  366.     protected static function _load($option)
  367.     {
  368.         return static::load($option);
  369.     }
  370.  
  371.     /**
  372.      * Load the installed components into the components property.
  373.      *
  374.      * @param   string  $option  The element value for the extension
  375.      *
  376.      * @return  boolean  True on success
  377.      *
  378.      * @since   3.2
  379.      */
  380.     protected static function load($option)
  381.     {
  382.         $db JFactory::getDbo();
  383.         $query $db->getQuery(true)
  384.             ->select('extension_id AS id, element AS "option", params, enabled')
  385.             ->from('#__extensions')
  386.             ->where($db->quoteName('type'' = ' $db->quote('component'))
  387.             ->where($db->quoteName('element'' = ' $db->quote($option));
  388.         $db->setQuery($query);
  389.  
  390.         $cache JFactory::getCache('_system''callback');
  391.  
  392.         try
  393.         {
  394.             static::$components[$option$cache->get(array($db'loadObject')null$optionfalse);
  395.         }
  396.         catch (RuntimeException $e)
  397.         {
  398.             // Fatal error.
  399.             JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING'$option$e->getMessage())JLog::WARNING'jerror');
  400.  
  401.             return false;
  402.         }
  403.  
  404.         if (empty(static::$components[$option]))
  405.         {
  406.             // Fatal error.
  407.             $error JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND');
  408.             JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING'$option$error)JLog::WARNING'jerror');
  409.  
  410.             return false;
  411.         }
  412.  
  413.         // Convert the params to an object.
  414.         if (is_string(static::$components[$option]->params))
  415.         {
  416.             $temp new JRegistry;
  417.             $temp->loadString(static::$components[$option]->params);
  418.             static::$components[$option]->params $temp;
  419.         }
  420.  
  421.         return true;
  422.     }
  423. }

Documentation generated on Tue, 19 Nov 2013 15:04:25 +0100 by phpDocumentor 1.4.3