PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_users/router.php

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