PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/legacy/component/helper.php

https://github.com/Hackwar/joomla-platform
PHP | 416 lines | 238 code | 53 blank | 125 comment | 31 complexity | cc467911c5eaf6119812d9b0d79b687c 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. * @deprecated 13.3
  17. */
  18. class JComponentHelper
  19. {
  20. /**
  21. * The component list cache
  22. *
  23. * @var array
  24. * @since 11.1
  25. */
  26. protected static $components = array();
  27. /**
  28. * Get the component information.
  29. *
  30. * @param string $option The component option.
  31. * @param boolean $strict If set and the component does not exist, the enabled attribute will be set to false.
  32. *
  33. * @return object An object with the information for the component.
  34. *
  35. * @since 11.1
  36. */
  37. public static function getComponent($option, $strict = false)
  38. {
  39. if (!isset(self::$components[$option]))
  40. {
  41. if (self::_load($option))
  42. {
  43. $result = self::$components[$option];
  44. }
  45. else
  46. {
  47. $result = new stdClass;
  48. $result->enabled = $strict ? false : true;
  49. $result->params = new JRegistry;
  50. }
  51. }
  52. else
  53. {
  54. $result = self::$components[$option];
  55. }
  56. return $result;
  57. }
  58. /**
  59. * Checks if the component is enabled
  60. *
  61. * @param string $option The component option.
  62. *
  63. * @return boolean
  64. *
  65. * @since 11.1
  66. */
  67. public static function isEnabled($option)
  68. {
  69. $result = self::getComponent($option, true);
  70. return $result->enabled;
  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. *
  253. * @return object
  254. *
  255. * @since 11.1
  256. * @throws Exception
  257. */
  258. public static function renderComponent($option)
  259. {
  260. $app = JFactory::getApplication();
  261. // Load template language files.
  262. $template = $app->getTemplate(true)->template;
  263. $lang = JFactory::getLanguage();
  264. $lang->load('tpl_' . $template, JPATH_BASE, null, false, false)
  265. || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, false)
  266. || $lang->load('tpl_' . $template, JPATH_BASE, $lang->getDefault(), false, false)
  267. || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", $lang->getDefault(), false, false);
  268. if (empty($option))
  269. {
  270. throw new Exception(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
  271. }
  272. // Record the scope
  273. $scope = $app->scope;
  274. // Set scope to component name
  275. $app->scope = $option;
  276. // Build the component path.
  277. $option = preg_replace('/[^A-Z0-9_\.-]/i', '', $option);
  278. $file = substr($option, 4);
  279. // Define component path.
  280. define('JPATH_COMPONENT', JPATH_BASE . '/components/' . $option);
  281. define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/' . $option);
  282. define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/' . $option);
  283. $path = JPATH_COMPONENT . '/' . $file . '.php';
  284. // If component is disabled throw error
  285. if (!self::isEnabled($option) || !file_exists($path))
  286. {
  287. throw new Exception(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
  288. }
  289. // Load common and local language files.
  290. $lang->load($option, JPATH_BASE, null, false, false) || $lang->load($option, JPATH_COMPONENT, null, false, false)
  291. || $lang->load($option, JPATH_BASE, $lang->getDefault(), false, false)
  292. || $lang->load($option, JPATH_COMPONENT, $lang->getDefault(), false, false);
  293. // Handle template preview outlining.
  294. $contents = null;
  295. // Execute the component.
  296. $contents = self::executeComponent($path);
  297. // Revert the scope
  298. $app->scope = $scope;
  299. return $contents;
  300. }
  301. /**
  302. * Execute the component.
  303. *
  304. * @param string $path The component path.
  305. *
  306. * @return string The component output
  307. *
  308. * @since 11.3
  309. */
  310. protected static function executeComponent($path)
  311. {
  312. ob_start();
  313. require_once $path;
  314. $contents = ob_get_contents();
  315. ob_end_clean();
  316. return $contents;
  317. }
  318. /**
  319. * Load the installed components into the components property.
  320. *
  321. * @param string $option The element value for the extension
  322. *
  323. * @return boolean True on success
  324. *
  325. * @since 11.1
  326. */
  327. protected static function _load($option)
  328. {
  329. $db = JFactory::getDbo();
  330. $query = $db->getQuery(true);
  331. $query->select('extension_id AS id, element AS "option", params, enabled');
  332. $query->from('#__extensions');
  333. $query->where($query->qn('type') . ' = ' . $db->quote('component'));
  334. $query->where($query->qn('element') . ' = ' . $db->quote($option));
  335. $db->setQuery($query);
  336. $cache = JFactory::getCache('_system', 'callback');
  337. try
  338. {
  339. self::$components[$option] = $cache->get(array($db, 'loadObject'), null, $option, false);
  340. }
  341. catch (RuntimeException $e)
  342. {
  343. // Fatal error.
  344. JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING', $option, $e->getMessage()), JLog::WARNING, 'jerror');
  345. return false;
  346. }
  347. if (empty(self::$components[$option]))
  348. {
  349. // Fatal error.
  350. $error = JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND');
  351. JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING', $option, $error), JLog::WARNING, 'jerror');
  352. return false;
  353. }
  354. // Convert the params to an object.
  355. if (is_string(self::$components[$option]->params))
  356. {
  357. $temp = new JRegistry;
  358. $temp->loadString(self::$components[$option]->params);
  359. self::$components[$option]->params = $temp;
  360. }
  361. return true;
  362. }
  363. }