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

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

https://github.com/meuru-muthuthanthri/joomla-cms
PHP | 303 lines | 206 code | 32 blank | 65 comment | 26 complexity | 5005aa353a6903824fba281b7b12a2e4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_content
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Content component helper.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_content
  15. * @since 1.6
  16. */
  17. class ContentHelper
  18. {
  19. public static $extension = 'com_content';
  20. /**
  21. * Configure the Linkbar.
  22. *
  23. * @param string $vName The name of the active view.
  24. *
  25. * @return void
  26. * @since 1.6
  27. */
  28. public static function addSubmenu($vName)
  29. {
  30. JHtmlSidebar::addEntry(
  31. JText::_('JGLOBAL_ARTICLES'),
  32. 'index.php?option=com_content&view=articles',
  33. $vName == 'articles'
  34. );
  35. JHtmlSidebar::addEntry(
  36. JText::_('COM_CONTENT_SUBMENU_CATEGORIES'),
  37. 'index.php?option=com_categories&extension=com_content',
  38. $vName == 'categories');
  39. JHtmlSidebar::addEntry(
  40. JText::_('COM_CONTENT_SUBMENU_FEATURED'),
  41. 'index.php?option=com_content&view=featured',
  42. $vName == 'featured'
  43. );
  44. }
  45. /**
  46. * Gets a list of the actions that can be performed.
  47. *
  48. * @param integer The category ID.
  49. * @param integer The article ID.
  50. *
  51. * @return JObject
  52. * @since 1.6
  53. */
  54. public static function getActions($categoryId = 0, $articleId = 0)
  55. {
  56. // Reverted a change for version 2.5.6
  57. $user = JFactory::getUser();
  58. $result = new JObject;
  59. if (empty($articleId) && empty($categoryId))
  60. {
  61. $assetName = 'com_content';
  62. }
  63. elseif (empty($articleId))
  64. {
  65. $assetName = 'com_content.category.'.(int) $categoryId;
  66. }
  67. else
  68. {
  69. $assetName = 'com_content.article.'.(int) $articleId;
  70. }
  71. $actions = array(
  72. 'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete'
  73. );
  74. foreach ($actions as $action)
  75. {
  76. $result->set($action, $user->authorise($action, $assetName));
  77. }
  78. return $result;
  79. }
  80. /**
  81. * Applies the content tag filters to arbitrary text as per settings for current user group
  82. * @param text The string to filter
  83. * @return string The filtered string
  84. */
  85. public static function filterText($text)
  86. {
  87. // Filter settings
  88. $config = JComponentHelper::getParams('com_config');
  89. $user = JFactory::getUser();
  90. $userGroups = JAccess::getGroupsByUser($user->get('id'));
  91. $filters = $config->get('filters');
  92. $blackListTags = array();
  93. $blackListAttributes = array();
  94. $customListTags = array();
  95. $customListAttributes = array();
  96. $whiteListTags = array();
  97. $whiteListAttributes = array();
  98. $noHtml = false;
  99. $whiteList = false;
  100. $blackList = false;
  101. $customList = false;
  102. $unfiltered = false;
  103. // Cycle through each of the user groups the user is in.
  104. // Remember they are included in the Public group as well.
  105. foreach ($userGroups as $groupId)
  106. {
  107. // May have added a group but not saved the filters.
  108. if (!isset($filters->$groupId))
  109. {
  110. continue;
  111. }
  112. // Each group the user is in could have different filtering properties.
  113. $filterData = $filters->$groupId;
  114. $filterType = strtoupper($filterData->filter_type);
  115. if ($filterType == 'NH')
  116. {
  117. // Maximum HTML filtering.
  118. $noHtml = true;
  119. }
  120. elseif ($filterType == 'NONE')
  121. {
  122. // No HTML filtering.
  123. $unfiltered = true;
  124. }
  125. else {
  126. // Black, white or custom list.
  127. // Preprocess the tags and attributes.
  128. $tags = explode(',', $filterData->filter_tags);
  129. $attributes = explode(',', $filterData->filter_attributes);
  130. $tempTags = array();
  131. $tempAttributes = array();
  132. foreach ($tags as $tag)
  133. {
  134. $tag = trim($tag);
  135. if ($tag)
  136. {
  137. $tempTags[] = $tag;
  138. }
  139. }
  140. foreach ($attributes as $attribute)
  141. {
  142. $attribute = trim($attribute);
  143. if ($attribute)
  144. {
  145. $tempAttributes[] = $attribute;
  146. }
  147. }
  148. // Collect the black or white list tags and attributes.
  149. // Each lists is cummulative.
  150. if ($filterType == 'BL')
  151. {
  152. $blackList = true;
  153. $blackListTags = array_merge($blackListTags, $tempTags);
  154. $blackListAttributes = array_merge($blackListAttributes, $tempAttributes);
  155. }
  156. elseif ($filterType == 'CBL')
  157. {
  158. // Only set to true if Tags or Attributes were added
  159. if ($tempTags || $tempAttributes)
  160. {
  161. $customList = true;
  162. $customListTags = array_merge($customListTags, $tempTags);
  163. $customListAttributes = array_merge($customListAttributes, $tempAttributes);
  164. }
  165. }
  166. elseif ($filterType == 'WL')
  167. {
  168. $whiteList = true;
  169. $whiteListTags = array_merge($whiteListTags, $tempTags);
  170. $whiteListAttributes = array_merge($whiteListAttributes, $tempAttributes);
  171. }
  172. }
  173. }
  174. // Remove duplicates before processing (because the black list uses both sets of arrays).
  175. $blackListTags = array_unique($blackListTags);
  176. $blackListAttributes = array_unique($blackListAttributes);
  177. $customListTags = array_unique($customListTags);
  178. $customListAttributes = array_unique($customListAttributes);
  179. $whiteListTags = array_unique($whiteListTags);
  180. $whiteListAttributes = array_unique($whiteListAttributes);
  181. // Unfiltered assumes first priority.
  182. if ($unfiltered)
  183. {
  184. // Dont apply filtering.
  185. }
  186. else
  187. {
  188. // Custom blacklist precedes Default blacklist
  189. if ($customList)
  190. {
  191. $filter = JFilterInput::getInstance(array(), array(), 1, 1);
  192. // Override filter's default blacklist tags and attributes
  193. if ($customListTags)
  194. {
  195. $filter->tagBlacklist = $customListTags;
  196. }
  197. if ($customListAttributes)
  198. {
  199. $filter->attrBlacklist = $customListAttributes;
  200. }
  201. }
  202. // Black lists take third precedence.
  203. elseif ($blackList)
  204. {
  205. // Remove the white-listed attributes from the black-list.
  206. $filter = JFilterInput::getInstance(
  207. // Blacklisted tags
  208. array_diff($blackListTags, $whiteListTags),
  209. // Blacklisted attributes
  210. array_diff($blackListAttributes, $whiteListAttributes),
  211. // Blacklist tags
  212. 1,
  213. // Blacklist attributes
  214. 1
  215. );
  216. // Remove white listed tags from filter's default blacklist
  217. if ($whiteListTags)
  218. {
  219. $filter->tagBlacklist = array_diff($filter->tagBlacklist, $whiteListTags);
  220. }
  221. // Remove white listed attributes from filter's default blacklist
  222. if ($whiteListAttributes)
  223. {
  224. $filter->attrBlacklist = array_diff($filter->attrBlacklist);
  225. }
  226. }
  227. // White lists take fourth precedence.
  228. elseif ($whiteList)
  229. {
  230. $filter = JFilterInput::getInstance($whiteListTags, $whiteListAttributes, 0, 0, 0); // turn off xss auto clean
  231. }
  232. // No HTML takes last place.
  233. else {
  234. $filter = JFilterInput::getInstance();
  235. }
  236. $text = $filter->clean($text, 'html');
  237. }
  238. return $text;
  239. }
  240. public static function getAssociations($pk)
  241. {
  242. $associations = array();
  243. $db = JFactory::getDbo();
  244. $query = $db->getQuery(true);
  245. $query->from('#__content as c');
  246. $query->innerJoin('#__associations as a ON a.id = c.id AND a.context='.$db->quote('com_content.item'));
  247. $query->innerJoin('#__associations as a2 ON a.key = a2.key');
  248. $query->innerJoin('#__content as c2 ON a2.id = c2.id');
  249. $query->innerJoin('#__categories as ca ON c2.catid = ca.id AND ca.extension = '.$db->quote('com_content'));
  250. $query->where('c.id =' . (int) $pk);
  251. $select = array(
  252. 'c2.language',
  253. $query->concatenate(array('c2.id', 'c2.alias'), ':') . ' AS id',
  254. $query->concatenate(array('ca.id', 'ca.alias'), ':') . ' AS catid'
  255. );
  256. $query->select($select);
  257. $db->setQuery($query);
  258. $contentitems = $db->loadObjectList('language');
  259. // Check for a database error.
  260. if ($error = $db->getErrorMsg())
  261. {
  262. JError::raiseWarning(500, $error);
  263. return false;
  264. }
  265. foreach ($contentitems as $tag => $item)
  266. {
  267. $associations[$tag] = $item;
  268. }
  269. return $associations;
  270. }
  271. }