/administrator/components/com_content/helpers/content.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 239 lines · 154 code · 28 blank · 57 comment · 27 complexity · 1adc803c6e8cadeccf98ddac195d16b2 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE.txt
  5. */
  6. // No direct access
  7. defined('_JEXEC') or die;
  8. /**
  9. * Content component helper.
  10. *
  11. * @package Joomla.Administrator
  12. * @subpackage com_content
  13. * @since 1.6
  14. */
  15. class ContentHelper
  16. {
  17. public static $extension = 'com_content';
  18. /**
  19. * Configure the Linkbar.
  20. *
  21. * @param string $vName The name of the active view.
  22. *
  23. * @return void
  24. * @since 1.6
  25. */
  26. public static function addSubmenu($vName)
  27. {
  28. JSubMenuHelper::addEntry(
  29. JText::_('JGLOBAL_ARTICLES'),
  30. 'index.php?option=com_content&view=articles',
  31. $vName == 'articles'
  32. );
  33. JSubMenuHelper::addEntry(
  34. JText::_('COM_CONTENT_SUBMENU_CATEGORIES'),
  35. 'index.php?option=com_categories&extension=com_content',
  36. $vName == 'categories');
  37. JSubMenuHelper::addEntry(
  38. JText::_('COM_CONTENT_SUBMENU_FEATURED'),
  39. 'index.php?option=com_content&view=featured',
  40. $vName == 'featured'
  41. );
  42. }
  43. /**
  44. * Gets a list of the actions that can be performed.
  45. *
  46. * @param int The category ID.
  47. * @param int The article ID.
  48. *
  49. * @return JObject
  50. * @since 1.6
  51. */
  52. public static function getActions($categoryId = 0, $articleId = 0)
  53. {
  54. $user = JFactory::getUser();
  55. $result = new JObject;
  56. if (empty($articleId) && empty($categoryId)) {
  57. $assetName = 'com_content';
  58. }
  59. elseif (empty($articleId)) {
  60. $assetName = 'com_content.category.'.(int) $categoryId;
  61. }
  62. else {
  63. $assetName = 'com_content.article.'.(int) $articleId;
  64. }
  65. $actions = array(
  66. 'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete'
  67. );
  68. foreach ($actions as $action) {
  69. $result->set($action, $user->authorise($action, $assetName));
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Applies the content tag filters to arbitrary text as per settings for current user group
  75. * @param text The string to filter
  76. * @return string The filtered string
  77. */
  78. public static function filterText($text)
  79. {
  80. // Filter settings
  81. $config = JComponentHelper::getParams('com_config');
  82. $user = JFactory::getUser();
  83. $userGroups = JAccess::getGroupsByUser($user->get('id'));
  84. $filters = $config->get('filters');
  85. $blackListTags = array();
  86. $blackListAttributes = array();
  87. $customListTags = array();
  88. $customListAttributes = array();
  89. $whiteListTags = array();
  90. $whiteListAttributes = array();
  91. $noHtml = false;
  92. $whiteList = false;
  93. $blackList = false;
  94. $customList = false;
  95. $unfiltered = false;
  96. // Cycle through each of the user groups the user is in.
  97. // Remember they are included in the Public group as well.
  98. foreach ($userGroups as $groupId)
  99. {
  100. // May have added a group but not saved the filters.
  101. if (!isset($filters->$groupId)) {
  102. continue;
  103. }
  104. // Each group the user is in could have different filtering properties.
  105. $filterData = $filters->$groupId;
  106. $filterType = strtoupper($filterData->filter_type);
  107. if ($filterType == 'NH') {
  108. // Maximum HTML filtering.
  109. $noHtml = true;
  110. }
  111. elseif ($filterType == 'NONE') {
  112. // No HTML filtering.
  113. $unfiltered = true;
  114. }
  115. else {
  116. // Black, white or custom list.
  117. // Preprocess the tags and attributes.
  118. $tags = explode(',', $filterData->filter_tags);
  119. $attributes = explode(',', $filterData->filter_attributes);
  120. $tempTags = array();
  121. $tempAttributes = array();
  122. foreach ($tags as $tag)
  123. {
  124. $tag = trim($tag);
  125. if ($tag) {
  126. $tempTags[] = $tag;
  127. }
  128. }
  129. foreach ($attributes as $attribute)
  130. {
  131. $attribute = trim($attribute);
  132. if ($attribute) {
  133. $tempAttributes[] = $attribute;
  134. }
  135. }
  136. // Collect the black or white list tags and attributes.
  137. // Each lists is cummulative.
  138. if ($filterType == 'BL') {
  139. $blackList = true;
  140. $blackListTags = array_merge($blackListTags, $tempTags);
  141. $blackListAttributes = array_merge($blackListAttributes, $tempAttributes);
  142. }
  143. elseif ($filterType == 'CBL') {
  144. // Only set to true if Tags or Attributes were added
  145. if ($tempTags || $tempAttributes) {
  146. $customList = true;
  147. $customListTags = array_merge($customListTags, $tempTags);
  148. $customListAttributes = array_merge($customListAttributes, $tempAttributes);
  149. }
  150. }
  151. elseif ($filterType == 'WL') {
  152. $whiteList = true;
  153. $whiteListTags = array_merge($whiteListTags, $tempTags);
  154. $whiteListAttributes = array_merge($whiteListAttributes, $tempAttributes);
  155. }
  156. }
  157. }
  158. // Remove duplicates before processing (because the black list uses both sets of arrays).
  159. $blackListTags = array_unique($blackListTags);
  160. $blackListAttributes = array_unique($blackListAttributes);
  161. $customListTags = array_unique($customListTags);
  162. $customListAttributes = array_unique($customListAttributes);
  163. $whiteListTags = array_unique($whiteListTags);
  164. $whiteListAttributes = array_unique($whiteListAttributes);
  165. // Unfiltered assumes first priority.
  166. if ($unfiltered) {
  167. // Dont apply filtering.
  168. }
  169. else {
  170. // Custom blacklist precedes Default blacklist
  171. if ($customList) {
  172. $filter = JFilterInput::getInstance(array(), array(), 1, 1);
  173. // Override filter's default blacklist tags and attributes
  174. if ($customListTags) {
  175. $filter->tagBlacklist = $customListTags;
  176. }
  177. if ($customListAttributes) {
  178. $filter->attrBlacklist = $customListAttributes;
  179. }
  180. }
  181. // Black lists take third precedence.
  182. elseif ($blackList) {
  183. // Remove the white-listed attributes from the black-list.
  184. $filter = JFilterInput::getInstance(
  185. array_diff($blackListTags, $whiteListTags), // blacklisted tags
  186. array_diff($blackListAttributes, $whiteListAttributes), // blacklisted attributes
  187. 1, // blacklist tags
  188. 1 // blacklist attributes
  189. );
  190. // Remove white listed tags from filter's default blacklist
  191. if ($whiteListTags) {
  192. $filter->tagBlacklist = array_diff($filter->tagBlacklist, $whiteListTags);
  193. }
  194. // Remove white listed attributes from filter's default blacklist
  195. if ($whiteListAttributes) {
  196. $filter->attrBlacklist = array_diff($filter->attrBlacklist);
  197. }
  198. }
  199. // White lists take fourth precedence.
  200. elseif ($whiteList) {
  201. $filter = JFilterInput::getInstance($whiteListTags, $whiteListAttributes, 0, 0, 0); // turn off xss auto clean
  202. }
  203. // No HTML takes last place.
  204. else {
  205. $filter = JFilterInput::getInstance();
  206. }
  207. $text = $filter->clean($text, 'html');
  208. }
  209. return $text;
  210. }
  211. }