/protected/components/ezcomponents/Url/src/url_tools.php

https://github.com/kamarulismail/kamarul-playground · PHP · 173 lines · 73 code · 11 blank · 89 comment · 15 complexity · a848a774c579eb091f6fee3a2a591473 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcUrlTools class.
  4. *
  5. * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
  6. * @license http://ez.no/licenses/new_bsd New BSD License
  7. * @version 1.2.2
  8. * @filesource
  9. * @package Url
  10. */
  11. /**
  12. * Class providing methods for URL parsing.
  13. *
  14. * Static methods contained in this class:
  15. * - parseQueryString() - It implements the functionality of the PHP function
  16. * parse_str(), but without converting dots to underscores in parameter names.
  17. * - getCurrentUrl() - Returns the current URL as a string from the provided
  18. * array (by default $_SERVER).
  19. *
  20. * @package Url
  21. * @version 1.2.2
  22. */
  23. class ezcUrlTools
  24. {
  25. /**
  26. * Parses the provided string and returns an associative array structure.
  27. *
  28. * It implements the functionality of the PHP function parse_str(), but
  29. * without converting dots to underscores in parameter names.
  30. *
  31. * Example:
  32. * <code>
  33. * $str = 'foo[]=bar&openid.nonce=123456';
  34. *
  35. * parse_str( $str, $params );
  36. * $params = ezcUrlTools::parseQueryString( $str );
  37. * </code>
  38. *
  39. * In the first case (parse_str()), $params will be:
  40. * <code>
  41. * array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
  42. * </code>
  43. *
  44. * In the second case (ezcUrlTools::parseQueryString()), $params will be:
  45. * <code>
  46. * array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
  47. * </code>
  48. *
  49. * @param array(string=>mixed) $str The string to parse
  50. * @return array(string=>mixed)
  51. */
  52. public static function parseQueryString( $str )
  53. {
  54. $result = array();
  55. // $params will be returned, but first we have to ensure that the dots
  56. // are not converted to underscores
  57. parse_str( $str, $params );
  58. $separator = ini_get( 'arg_separator.input' );
  59. if ( empty( $separator ) )
  60. {
  61. $separator = '&';
  62. }
  63. // go through $params and ensure that the dots are not converted to underscores
  64. $args = explode( $separator, $str );
  65. foreach ( $args as $arg )
  66. {
  67. $parts = explode( '=', $arg, 2 );
  68. if ( !isset( $parts[1] ) )
  69. {
  70. $parts[1] = null;
  71. }
  72. if ( substr_count( $parts[0], '[' ) === 0 )
  73. {
  74. $key = $parts[0];
  75. }
  76. else
  77. {
  78. $key = substr( $parts[0], 0, strpos( $parts[0], '[' ) );
  79. }
  80. $paramKey = str_replace( '.', '_', $key );
  81. if ( isset( $params[$paramKey] ) && strpos( $paramKey, '_' ) !== false )
  82. {
  83. $newKey = '';
  84. for ( $i = 0; $i < strlen( $paramKey ); $i++ )
  85. {
  86. $newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
  87. }
  88. $keys = array_keys( $params );
  89. if ( ( $pos = array_search( $paramKey, $keys ) ) !== false )
  90. {
  91. $keys[$pos] = $newKey;
  92. }
  93. $values = array_values( $params );
  94. $params = array_combine( $keys, $values );
  95. }
  96. }
  97. return $params;
  98. }
  99. /**
  100. * Returns the current URL as a string from the array $source
  101. * (by default $_SERVER).
  102. *
  103. * The following fields are used in building the URL:
  104. * - HTTPS - determines the scheme ('http' or 'https'). 'https' only if
  105. * the 'HTTPS' field is set or if it is 'on' or '1'
  106. * - SERVER_NAME
  107. * - SERVER_PORT - determines if port is default (80 = do not include port)
  108. * or not default (other than 80 = include port)
  109. * - REQUEST_URI
  110. *
  111. * For example, if $_SERVER has these fields:
  112. * <code>
  113. * $_SERVER = array(
  114. * 'HTTPS' => '1',
  115. * 'SERVER_NAME' => 'www.example.com',
  116. * 'SERVER_PORT' => 80,
  117. * 'REQUEST_URI' => '/index.php'
  118. * );
  119. * </code>
  120. *
  121. * Then by calling this function (with no parameters), this URL will be
  122. * returned: 'http://www.example.com/index.php'.
  123. *
  124. * The source of the URL parts can be changed to be other than $_SERVER by
  125. * specifying an array parameter when calling this function.
  126. *
  127. * @todo check if REQUEST_URI works in Windows + IIS.
  128. * - Use PHP_SELF instead?
  129. * - Or use SCRIPT_NAME + QUERY_STRING?
  130. * - Or even use an ISAPI filter?
  131. * @todo check for proxy servers
  132. * - Use $_SERVER['HTTP_X_FORWARDED_SERVER']?
  133. *
  134. * @param array(string=>mixed) $source The default array source, default $_SERVER
  135. * @return string
  136. */
  137. public static function getCurrentUrl( array $source = null )
  138. {
  139. if ( $source === null )
  140. {
  141. $source = $_SERVER;
  142. }
  143. $url = '';
  144. if ( isset( $source['HTTPS'] ) &&
  145. ( strtolower( $source['HTTPS'] ) === 'on' || $source['HTTPS'] === '1' ) )
  146. {
  147. $url .= 'https://';
  148. }
  149. else
  150. {
  151. $url .= 'http://';
  152. }
  153. $url .= isset( $source['SERVER_NAME'] ) ? $source['SERVER_NAME'] : null;
  154. if ( isset( $source['SERVER_PORT'] ) && $source['SERVER_PORT'] != 80 )
  155. {
  156. $url .= ":{$source['SERVER_PORT']}";
  157. }
  158. $url .= isset( $source['REQUEST_URI'] ) ? $source['REQUEST_URI'] : null;
  159. return $url;
  160. }
  161. }
  162. ?>