PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/component/helper.php

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