function request_path()

Public

bootstrap.inc

Get raw version
php
  1. /**
  2.  * Returns the requested URL path of the page being viewed.
  3.  *
  4.  * Examples:
  5.  * - http://example.com/node/306 returns "node/306".
  6.  * - http://example.com/drupalfolder/node/306 returns "node/306" while
  7.  * base_path() returns "/drupalfolder/".
  8.  * - http://example.com/path/alias (which is a path alias for node/306) returns
  9.  * "path/alias" as opposed to the internal path.
  10.  * - http://example.com/index.php returns an empty string (meaning: front page).
  11.  * - http://example.com/index.php?page=1 returns an empty string.
  12.  *
  13.  * @return
  14.  * The requested Drupal URL path.
  15.  *
  16.  * @see current_path()
  17.  */
  18. function request_path() {
  19. static $path;
  20.  
  21. if (isset($path)) {
  22. return $path;
  23. }
  24.  
  25. if (isset($_GET['q']) && is_string($_GET['q'])) {
  26. // This is a request with a ?q=foo/bar query string. $_GET['q'] is
  27. // overwritten in drupal_path_initialize(), but request_path() is called
  28. // very early in the bootstrap process, so the original value is saved in
  29. // $path and returned in later calls.
  30. $path = $_GET['q'];
  31. }
  32. elseif (isset($_SERVER['REQUEST_URI'])) {
  33. // This request is either a clean URL, or 'index.php', or nonsense.
  34. // Extract the path from REQUEST_URI.
  35. $request_path = strtok($_SERVER['REQUEST_URI'], '?');
  36. $base_path_len = strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/'));
  37. // Unescape and strip $base_path prefix, leaving q without a leading slash.
  38. $path = substr(urldecode($request_path), $base_path_len + 1);
  39. // If the path equals the script filename, either because 'index.php' was
  40. // explicitly provided in the URL, or because the server added it to
  41. // $_SERVER['REQUEST_URI'] even when it wasn't provided in the URL (some
  42. // versions of Microsoft IIS do this), the front page should be served.
  43. if ($path == basename($_SERVER['PHP_SELF'])) {
  44. $path = '';
  45. }
  46. }
  47. else {
  48. // This is the front page.
  49. $path = '';
  50. }
  51.  
  52. // Under certain conditions Apache's RewriteRule directive prepends the value
  53. // assigned to $_GET['q'] with a slash. Moreover we can always have a trailing
  54. // slash in place, hence we need to normalize $_GET['q'].
  55. $path = trim($path, '/');
  56.  
  57. return $path;
  58. }

function arg - Returns a component of the current Drupal path.

Get raw version
php
  1. /**
  2.  * Returns a component of the current Drupal path.
  3.  *
  4.  * When viewing a page at the path "admin/structure/types", for example, arg(0)
  5.  * returns "admin", arg(1) returns "structure", and arg(2) returns "types".
  6.  *
  7.  * Avoid use of this function where possible, as resulting code is hard to
  8.  * read. In menu callback functions, attempt to use named arguments. See the
  9.  * explanation in menu.inc for how to construct callbacks that take arguments.
  10.  * When attempting to use this function to load an element from the current
  11.  * path, e.g. loading the node on a node page, use menu_get_object() instead.
  12.  *
  13.  * @param $index
  14.  * The index of the component, where each component is separated by a '/'
  15.  * (forward-slash), and where the first component has an index of 0 (zero).
  16.  * @param $path
  17.  * A path to break into components. Defaults to the path of the current page.
  18.  *
  19.  * @return
  20.  * The component specified by $index, or NULL if the specified component was
  21.  * not found. If called without arguments, it returns an array containing all
  22.  * the components of the current path.
  23.  */
  24. function arg($index = NULL, $path = NULL) {
  25. // Even though $arguments doesn't need to be resettable for any functional
  26. // reasons (the result of explode() does not depend on any run-time
  27. // information), it should be resettable anyway in case a module needs to
  28. // free up the memory used by it.
  29. // Use the advanced drupal_static() pattern, since this is called very often.
  30. static $drupal_static_fast;
  31. if (!isset($drupal_static_fast)) {
  32. $drupal_static_fast['arguments'] = &drupal_static(__FUNCTION__);
  33. }
  34. $arguments = &$drupal_static_fast['arguments'];
  35.  
  36. if (!isset($path)) {
  37. $path = $_GET['q'];
  38. }
  39. if (!isset($arguments[$path])) {
  40. $arguments[$path] = explode('/', $path);
  41. }
  42. if (!isset($index)) {
  43. return $arguments[$path];
  44. }
  45. if (isset($arguments[$path][$index])) {
  46. return $arguments[$path][$index];
  47. }
  48. }

function ip_address() - Returns the IP address of the client machine.

Get raw version
php
  1. /**
  2.  * Returns the IP address of the client machine.
  3.  *
  4.  * If Drupal is behind a reverse proxy, we use the X-Forwarded-For header
  5.  * instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of
  6.  * the proxy server, and not the client's. The actual header name can be
  7.  * configured by the reverse_proxy_header variable.
  8.  *
  9.  * @return
  10.  * IP address of client machine, adjusted for reverse proxy and/or cluster
  11.  * environments.
  12.  */
  13. function ip_address() {
  14. $ip_address = &drupal_static(__FUNCTION__);
  15.  
  16. if (!isset($ip_address)) {
  17. $ip_address = $_SERVER['REMOTE_ADDR'];
  18.  
  19. if (variable_get('reverse_proxy', 0)) {
  20. $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
  21. if (!empty($_SERVER[$reverse_proxy_header])) {
  22. // If an array of known reverse proxy IPs is provided, then trust
  23. // the XFF header if request really comes from one of them.
  24. $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
  25.  
  26. // Turn XFF header into an array.
  27. $forwarded = explode(',', $_SERVER[$reverse_proxy_header]);
  28.  
  29. // Trim the forwarded IPs; they may have been delimited by commas and spaces.
  30. $forwarded = array_map('trim', $forwarded);
  31.  
  32. // Tack direct client IP onto end of forwarded array.
  33. $forwarded[] = $ip_address;
  34.  
  35. // Eliminate all trusted IPs.
  36. $untrusted = array_diff($forwarded, $reverse_proxy_addresses);
  37.  
  38. // The right-most IP is the most specific we can trust.
  39. $ip_address = array_pop($untrusted);
  40. }
  41. }
  42. }
  43.  
  44. return $ip_address;
  45. }