PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/application/component/helper.php

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