[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/application/component/ -> helper.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1