PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/gnomeontherun/square-one
PHP | 434 lines | 245 code | 53 blank | 136 comment | 36 complexity | fe22010d19a6d131d518d3c7574c3b24 MD5 | raw file
Possible License(s): 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. $filter = JFilterInput::getInstance($whiteListTags, $whiteListAttributes, 0, 0, 0); // turn off xss auto clean
  245. }
  246. // No HTML takes last place.
  247. else
  248. {
  249. $filter = JFilterInput::getInstance();
  250. }
  251. $text = $filter->clean($text, 'html');
  252. }
  253. return $text;
  254. }
  255. /**
  256. * Render the component.
  257. *
  258. * @param string $option The component option.
  259. * @param array $params The component parameters
  260. *
  261. * @return object
  262. *
  263. * @since 11.1
  264. */
  265. public static function renderComponent($option, $params = array())
  266. {
  267. // Initialise variables.
  268. $app = JFactory::getApplication();
  269. // Load template language files.
  270. $template = $app->getTemplate(true)->template;
  271. $lang = JFactory::getLanguage();
  272. $lang->load('tpl_' . $template, JPATH_BASE, null, false, false)
  273. || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, false)
  274. || $lang->load('tpl_' . $template, JPATH_BASE, $lang->getDefault(), false, false)
  275. || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", $lang->getDefault(), false, false);
  276. if (empty($option))
  277. {
  278. // Throw 404 if no component
  279. JError::raiseError(404, JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'));
  280. return;
  281. }
  282. // Record the scope
  283. $scope = $app->scope;
  284. // Set scope to component name
  285. $app->scope = $option;
  286. // Build the component path.
  287. $option = preg_replace('/[^A-Z0-9_\.-]/i', '', $option);
  288. $file = substr($option, 4);
  289. // Define component path.
  290. define('JPATH_COMPONENT', JPATH_BASE . '/components/' . $option);
  291. define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/' . $option);
  292. define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/' . $option);
  293. // Get component path
  294. if ($app->isAdmin() && file_exists(JPATH_COMPONENT . '/admin.' . $file . '.php'))
  295. {
  296. $path = JPATH_COMPONENT . '/admin.' . $file . '.php';
  297. }
  298. else
  299. {
  300. $path = JPATH_COMPONENT . '/' . $file . '.php';
  301. }
  302. // If component is disabled throw error
  303. if (!self::isEnabled($option) || !file_exists($path))
  304. {
  305. JError::raiseError(404, JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'));
  306. }
  307. $task = JRequest::getString('task');
  308. // Load common and local language files.
  309. $lang->load($option, JPATH_BASE, null, false, false) || $lang->load($option, JPATH_COMPONENT, null, false, false)
  310. || $lang->load($option, JPATH_BASE, $lang->getDefault(), false, false)
  311. || $lang->load($option, JPATH_COMPONENT, $lang->getDefault(), false, false);
  312. // Handle template preview outlining.
  313. $contents = null;
  314. // Execute the component.
  315. $contents = self::executeComponent($path);
  316. // Build the component toolbar
  317. $path = JApplicationHelper::getPath('toolbar');
  318. if ($path && $app->isAdmin())
  319. {
  320. // Get the task again, in case it has changed
  321. $task = JRequest::getString('task');
  322. // Make the toolbar
  323. include_once $path;
  324. }
  325. // Revert the scope
  326. $app->scope = $scope;
  327. return $contents;
  328. }
  329. /**
  330. * Execute the component.
  331. *
  332. * @param string $path The component path.
  333. *
  334. * @return string The component output
  335. *
  336. * @since 11.3
  337. */
  338. protected static function executeComponent($path)
  339. {
  340. ob_start();
  341. require_once $path;
  342. $contents = ob_get_contents();
  343. ob_end_clean();
  344. return $contents;
  345. }
  346. /**
  347. * Load the installed components into the _components property.
  348. *
  349. * @param string $option The element value for the extension
  350. *
  351. * @return boolean True on success
  352. *
  353. * @since 11.1
  354. */
  355. protected static function _load($option)
  356. {
  357. $db = JFactory::getDbo();
  358. $query = $db->getQuery(true);
  359. $query->select('extension_id AS "id", element AS "option", params, enabled');
  360. $query->from('#__extensions');
  361. $query->where($query->qn('type') . ' = ' . $db->quote('component'));
  362. $query->where($query->qn('element') . ' = ' . $db->quote($option));
  363. $db->setQuery($query);
  364. $cache = JFactory::getCache('_system', 'callback');
  365. self::$components[$option] = $cache->get(array($db, 'loadObject'), null, $option, false);
  366. if ($error = $db->getErrorMsg() || empty(self::$components[$option]))
  367. {
  368. // Fatal error.
  369. JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING', $option, $error));
  370. return false;
  371. }
  372. // Convert the params to an object.
  373. if (is_string(self::$components[$option]->params))
  374. {
  375. $temp = new JRegistry;
  376. $temp->loadString(self::$components[$option]->params);
  377. self::$components[$option]->params = $temp;
  378. }
  379. return true;
  380. }
  381. }