/components/com_users/router.php

https://github.com/bolis97/joomla-cms · PHP · 202 lines · 133 code · 28 blank · 41 comment · 51 complexity · d405d501caa18dd5decbc458f5f93b98 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Site
  5. * @subpackage com_users
  6. * @copyright Copyright (C) 2005 - 2011 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. * Function to build a Users URL route.
  12. *
  13. * @param array The array of query string values for which to build a route.
  14. * @return array The URL route with segments represented as an array.
  15. * @since 1.5
  16. */
  17. function UsersBuildRoute(&$query)
  18. {
  19. // Declare static variables.
  20. static $items;
  21. static $default;
  22. static $registration;
  23. static $profile;
  24. static $login;
  25. static $remind;
  26. static $resend;
  27. static $reset;
  28. // Initialise variables.
  29. $segments = array();
  30. // Get the relevant menu items if not loaded.
  31. if (empty($items)) {
  32. // Get all relevant menu items.
  33. $app = JFactory::getApplication();
  34. $menu = $app->getMenu();
  35. $items = $menu->getItems('component', 'com_users');
  36. // Build an array of serialized query strings to menu item id mappings.
  37. for ($i = 0, $n = count($items); $i < $n; $i++) {
  38. // Check to see if we have found the resend menu item.
  39. if (empty($resend) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'resend')) {
  40. $resend = $items[$i]->id;
  41. }
  42. // Check to see if we have found the reset menu item.
  43. if (empty($reset) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'reset')) {
  44. $reset = $items[$i]->id;
  45. }
  46. // Check to see if we have found the remind menu item.
  47. if (empty($remind) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'remind')) {
  48. $remind = $items[$i]->id;
  49. }
  50. // Check to see if we have found the login menu item.
  51. if (empty($login) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'login')) {
  52. $login = $items[$i]->id;
  53. }
  54. // Check to see if we have found the registration menu item.
  55. if (empty($registration) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'registration')) {
  56. $registration = $items[$i]->id;
  57. }
  58. // Check to see if we have found the profile menu item.
  59. if (empty($profile) && !empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'profile')) {
  60. $profile = $items[$i]->id;
  61. }
  62. }
  63. // Set the default menu item to use for com_users if possible.
  64. if ($profile) {
  65. $default = $profile;
  66. } elseif ($registration) {
  67. $default = $registration;
  68. } elseif ($login) {
  69. $default = $login;
  70. }
  71. }
  72. if (!empty($query['view'])) {
  73. switch ($query['view']) {
  74. case 'reset':
  75. if ($query['Itemid'] = $reset) {
  76. unset ($query['view']);
  77. } else {
  78. $query['Itemid'] = $default;
  79. }
  80. break;
  81. case 'resend':
  82. if ($query['Itemid'] = $resend) {
  83. unset ($query['view']);
  84. } else {
  85. $query['Itemid'] = $default;
  86. }
  87. break;
  88. case 'remind':
  89. if ($query['Itemid'] = $remind) {
  90. unset ($query['view']);
  91. } else {
  92. $query['Itemid'] = $default;
  93. }
  94. break;
  95. case 'login':
  96. if ($query['Itemid'] = $login) {
  97. unset ($query['view']);
  98. } else {
  99. $query['Itemid'] = $default;
  100. }
  101. break;
  102. case 'registration':
  103. if ($query['Itemid'] = $registration) {
  104. unset ($query['view']);
  105. } else {
  106. $query['Itemid'] = $default;
  107. }
  108. break;
  109. default:
  110. case 'profile':
  111. if (!empty($query['view'])) {
  112. $segments[] = $query['view'];
  113. }
  114. unset ($query['view']);
  115. if ($query['Itemid'] = $profile) {
  116. unset ($query['view']);
  117. } else {
  118. $query['Itemid'] = $default;
  119. }
  120. // Only append the user id if not "me".
  121. $user = JFactory::getUser();
  122. if (!empty($query['user_id']) && ($query['user_id'] != $user->id)) {
  123. $segments[] = $query['user_id'];
  124. }
  125. unset ($query['user_id']);
  126. break;
  127. }
  128. }
  129. return $segments;
  130. }
  131. /**
  132. * Function to parse a Users URL route.
  133. *
  134. * @param array The URL route with segments represented as an array.
  135. * @return array The array of variables to set in the request.
  136. * @since 1.5
  137. */
  138. function UsersParseRoute($segments)
  139. {
  140. // Initialise variables.
  141. $vars = array();
  142. // Only run routine if there are segments to parse.
  143. if (count($segments) < 1) {
  144. return;
  145. }
  146. // Get the package from the route segments.
  147. $userId = array_pop($segments);
  148. if (!is_numeric($userId)) {
  149. $vars['view'] = 'profile';
  150. return $vars;
  151. }
  152. if (is_numeric($userId)) {
  153. // Get the package id from the packages table by alias.
  154. $db = JFactory::getDbo();
  155. $db->setQuery(
  156. 'SELECT `id`' .
  157. ' FROM `#__users`' .
  158. ' WHERE `id` = '.(int) $userId
  159. );
  160. $userId = $db->loadResult();
  161. }
  162. // Set the package id if present.
  163. if ($userId) {
  164. // Set the package id.
  165. $vars['user_id'] = (int)$userId;
  166. // Set the view to package if not already set.
  167. if (empty($vars['view'])) {
  168. $vars['view'] = 'profile';
  169. }
  170. } else {
  171. JError::raiseError(404, JText::_('JGLOBAL_RESOURCE_NOT_FOUND'));
  172. }
  173. return $vars;
  174. }