PageRenderTime 43ms CodeModel.GetById 26ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/libraries/legacy/component/helper.php

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