PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/joomlapp.com/components/com_content/helpers/legacyrouter.php

https://gitlab.com/iromanos/joomad-gradle
PHP | 404 lines | 253 code | 66 blank | 85 comment | 90 complexity | a9d2acd8becde59118e36ae1b22c5988 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_content
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 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. * Legacy routing rules class from com_content
  12. *
  13. * @since 3.6
  14. * @deprecated 4.0
  15. */
  16. class ContentRouterRulesLegacy implements JComponentRouterRulesInterface
  17. {
  18. /**
  19. * Constructor for this legacy router
  20. *
  21. * @param JComponentRouterView $router The router this rule belongs to
  22. *
  23. * @since 3.6
  24. * @deprecated 4.0
  25. */
  26. public function __construct($router)
  27. {
  28. $this->router = $router;
  29. }
  30. /**
  31. * Preprocess the route for the com_content component
  32. *
  33. * @param array &$query An array of URL arguments
  34. *
  35. * @return void
  36. *
  37. * @since 3.6
  38. * @deprecated 4.0
  39. */
  40. public function preprocess(&$query)
  41. {
  42. }
  43. /**
  44. * Build the route for the com_content component
  45. *
  46. * @param array &$query An array of URL arguments
  47. * @param array &$segments The URL arguments to use to assemble the subsequent URL.
  48. *
  49. * @return void
  50. *
  51. * @since 3.6
  52. * @deprecated 4.0
  53. */
  54. public function build(&$query, &$segments)
  55. {
  56. // Get a menu item based on Itemid or currently active
  57. $params = JComponentHelper::getParams('com_content');
  58. $advanced = $params->get('sef_advanced_link', 0);
  59. // We need a menu item. Either the one specified in the query, or the current active one if none specified
  60. if (empty($query['Itemid'])) {
  61. $menuItem = $this->router->menu->getActive();
  62. $menuItemGiven = false;
  63. } else {
  64. $menuItem = $this->router->menu->getItem($query['Itemid']);
  65. $menuItemGiven = true;
  66. }
  67. // Check again
  68. if ($menuItemGiven && isset($menuItem) && $menuItem->component != 'com_content') {
  69. $menuItemGiven = false;
  70. unset($query['Itemid']);
  71. }
  72. if (isset($query['view'])) {
  73. $view = $query['view'];
  74. } else {
  75. // We need to have a view in the query or it is an invalid URL
  76. return;
  77. }
  78. // Are we dealing with an article or category that is attached to a menu item?
  79. if ($menuItem !== null
  80. && $menuItem->query['view'] == $query['view']
  81. && isset($menuItem->query['id'], $query['id'])
  82. && $menuItem->query['id'] == (int)$query['id']) {
  83. unset($query['view']);
  84. if (isset($query['catid'])) {
  85. unset($query['catid']);
  86. }
  87. if (isset($query['layout'])) {
  88. unset($query['layout']);
  89. }
  90. unset($query['id']);
  91. return;
  92. }
  93. if ($view == 'category' || $view == 'article') {
  94. if (!$menuItemGiven) {
  95. $segments[] = $view;
  96. }
  97. unset($query['view']);
  98. if ($view == 'article') {
  99. if (isset($query['id']) && isset($query['catid']) && $query['catid']) {
  100. $catid = $query['catid'];
  101. // Make sure we have the id and the alias
  102. if (strpos($query['id'], ':') === false) {
  103. $db = JFactory::getDbo();
  104. $dbQuery = $db->getQuery(true)
  105. ->select('alias')
  106. ->from('#__content')
  107. ->where('id=' . (int)$query['id']);
  108. $db->setQuery($dbQuery);
  109. $alias = $db->loadResult();
  110. $query['id'] = $query['id'] . ':' . $alias;
  111. }
  112. } else {
  113. // We should have these two set for this view. If we don't, it is an error
  114. return;
  115. }
  116. } else {
  117. if (isset($query['id'])) {
  118. $catid = $query['id'];
  119. } else {
  120. // We should have id set for this view. If we don't, it is an error
  121. return;
  122. }
  123. }
  124. if ($menuItemGiven && isset($menuItem->query['id'])) {
  125. $mCatid = $menuItem->query['id'];
  126. } else {
  127. $mCatid = 0;
  128. }
  129. $categories = JCategories::getInstance('Content');
  130. $category = $categories->get($catid);
  131. if (!$category) {
  132. // We couldn't find the category we were given. Bail.
  133. return;
  134. }
  135. $path = array_reverse($category->getPath());
  136. $array = array();
  137. foreach ($path as $id) {
  138. if ((int)$id == (int)$mCatid) {
  139. break;
  140. }
  141. list($tmp, $id) = explode(':', $id, 2);
  142. $array[] = $id;
  143. }
  144. $array = array_reverse($array);
  145. if (!$advanced && count($array)) {
  146. $array[0] = (int)$catid . ':' . $array[0];
  147. }
  148. $segments = array_merge($segments, $array);
  149. if ($view == 'article') {
  150. if ($advanced) {
  151. list($tmp, $id) = explode(':', $query['id'], 2);
  152. } else {
  153. $id = $query['id'];
  154. }
  155. $segments[] = $id;
  156. }
  157. unset($query['id'], $query['catid']);
  158. }
  159. if ($view == 'archive') {
  160. if (!$menuItemGiven) {
  161. $segments[] = $view;
  162. unset($query['view']);
  163. }
  164. if (isset($query['year'])) {
  165. if ($menuItemGiven) {
  166. $segments[] = $query['year'];
  167. unset($query['year']);
  168. }
  169. }
  170. if (isset($query['year']) && isset($query['month'])) {
  171. if ($menuItemGiven) {
  172. $segments[] = $query['month'];
  173. unset($query['month']);
  174. }
  175. }
  176. }
  177. if ($view == 'featured') {
  178. if (!$menuItemGiven) {
  179. $segments[] = $view;
  180. }
  181. unset($query['view']);
  182. }
  183. /*
  184. * If the layout is specified and it is the same as the layout in the menu item, we
  185. * unset it so it doesn't go into the query string.
  186. */
  187. if (isset($query['layout'])) {
  188. if ($menuItemGiven && isset($menuItem->query['layout'])) {
  189. if ($query['layout'] == $menuItem->query['layout']) {
  190. unset($query['layout']);
  191. }
  192. } else {
  193. if ($query['layout'] == 'default') {
  194. unset($query['layout']);
  195. }
  196. }
  197. }
  198. $total = count($segments);
  199. for ($i = 0; $i < $total; $i++) {
  200. $segments[$i] = str_replace(':', '-', $segments[$i]);
  201. }
  202. }
  203. /**
  204. * Parse the segments of a URL.
  205. *
  206. * @param array &$segments The segments of the URL to parse.
  207. * @param array &$vars The URL attributes to be used by the application.
  208. *
  209. * @return void
  210. *
  211. * @since 3.6
  212. * @deprecated 4.0
  213. */
  214. public function parse(&$segments, &$vars)
  215. {
  216. $total = count($segments);
  217. for ($i = 0; $i < $total; $i++) {
  218. $segments[$i] = preg_replace('/-/', ':', $segments[$i], 1);
  219. }
  220. // Get the active menu item.
  221. $item = $this->router->menu->getActive();
  222. $params = JComponentHelper::getParams('com_content');
  223. $advanced = $params->get('sef_advanced_link', 0);
  224. $db = JFactory::getDbo();
  225. // Count route segments
  226. $count = count($segments);
  227. /*
  228. * Standard routing for articles. If we don't pick up an Itemid then we get the view from the segments
  229. * the first segment is the view and the last segment is the id of the article or category.
  230. */
  231. if (!isset($item)) {
  232. $vars['view'] = $segments[0];
  233. $vars['id'] = $segments[$count - 1];
  234. return;
  235. }
  236. /*
  237. * If there is only one segment, then it points to either an article or a category.
  238. * We test it first to see if it is a category. If the id and alias match a category,
  239. * then we assume it is a category. If they don't we assume it is an article
  240. */
  241. if ($count == 1) {
  242. // We check to see if an alias is given. If not, we assume it is an article
  243. if (strpos($segments[0], ':') === false) {
  244. $vars['view'] = 'article';
  245. $vars['id'] = (int)$segments[0];
  246. return;
  247. }
  248. list($id, $alias) = explode(':', $segments[0], 2);
  249. // First we check if it is a category
  250. $category = JCategories::getInstance('Content')->get($id);
  251. if ($category && $category->alias == $alias) {
  252. $vars['view'] = 'category';
  253. $vars['id'] = $id;
  254. return;
  255. } else {
  256. $query = $db->getQuery(true)
  257. ->select($db->quoteName(array('alias', 'catid')))
  258. ->from($db->quoteName('#__content'))
  259. ->where($db->quoteName('id') . ' = ' . (int)$id);
  260. $db->setQuery($query);
  261. $article = $db->loadObject();
  262. if ($article) {
  263. if ($article->alias == $alias) {
  264. $vars['view'] = 'article';
  265. $vars['catid'] = (int)$article->catid;
  266. $vars['id'] = (int)$id;
  267. return;
  268. }
  269. }
  270. }
  271. }
  272. /*
  273. * If there was more than one segment, then we can determine where the URL points to
  274. * because the first segment will have the target category id prepended to it. If the
  275. * last segment has a number prepended, it is an article, otherwise, it is a category.
  276. */
  277. if (!$advanced) {
  278. $cat_id = (int)$segments[0];
  279. $article_id = (int)$segments[$count - 1];
  280. if ($article_id > 0) {
  281. $vars['view'] = 'article';
  282. $vars['catid'] = $cat_id;
  283. $vars['id'] = $article_id;
  284. } else {
  285. $vars['view'] = 'category';
  286. $vars['id'] = $cat_id;
  287. }
  288. return;
  289. }
  290. // We get the category id from the menu item and search from there
  291. $id = $item->query['id'];
  292. $category = JCategories::getInstance('Content')->get($id);
  293. if (!$category) {
  294. JError::raiseError(404, JText::_('COM_CONTENT_ERROR_PARENT_CATEGORY_NOT_FOUND'));
  295. return;
  296. }
  297. $categories = $category->getChildren();
  298. $vars['catid'] = $id;
  299. $vars['id'] = $id;
  300. $found = 0;
  301. foreach ($segments as $segment) {
  302. $segment = str_replace(':', '-', $segment);
  303. foreach ($categories as $category) {
  304. if ($category->alias == $segment) {
  305. $vars['id'] = $category->id;
  306. $vars['catid'] = $category->id;
  307. $vars['view'] = 'category';
  308. $categories = $category->getChildren();
  309. $found = 1;
  310. break;
  311. }
  312. }
  313. if ($found == 0) {
  314. if ($advanced) {
  315. $db = JFactory::getDbo();
  316. $query = $db->getQuery(true)
  317. ->select($db->quoteName('id'))
  318. ->from('#__content')
  319. ->where($db->quoteName('catid') . ' = ' . (int)$vars['catid'])
  320. ->where($db->quoteName('alias') . ' = ' . $db->quote($segment));
  321. $db->setQuery($query);
  322. $cid = $db->loadResult();
  323. } else {
  324. $cid = $segment;
  325. }
  326. $vars['id'] = $cid;
  327. if ($item->query['view'] == 'archive' && $count != 1) {
  328. $vars['year'] = $count >= 2 ? $segments[$count - 2] : null;
  329. $vars['month'] = $segments[$count - 1];
  330. $vars['view'] = 'archive';
  331. } else {
  332. $vars['view'] = 'article';
  333. }
  334. }
  335. $found = 0;
  336. }
  337. }
  338. }