## Apache Zeta Components Manual :: File Source for url_tools.php

## Source for file url_tools.php

Documentation is available at url_tools.php

1. <?php
2. /**
3. * File containing the ezcUrlTools class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogen//
24. * @filesource
25. * @package Url
26. */
28. /**
29. * Class providing methods for URL parsing.
30. *
31. * Static methods contained in this class:
32. * - parseQueryString() - It implements the functionality of the PHP function
33. * parse_str(), but without converting dots to underscores in parameter names.
34. * - getCurrentUrl() - Returns the current URL as a string from the provided
35. * array (by default $_SERVER).
36. *
37. * @package Url
38. * @version //autogen//
39. */
40. class ezcUrlTools
41. {
42. /**
43. * Parses the provided string and returns an associative array structure.
44. *
45. * It implements the functionality of the PHP function parse_str(), but
46. * without converting dots to underscores in parameter names.
47. *
48. * Example:
49. * <code>
50. * $str = 'foo[]=bar&openid.nonce=123456';
51. *
52. * parse_str( $str, $params );
53. * $params = ezcUrlTools::parseQueryString( $str );
54. * </code>
55. *
56. * In the first case (parse_str()), $params will be:
57. * <code>
58. * array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
59. * </code>
60. *
61. * In the second case (ezcUrlTools::parseQueryString()), $params will be:
62. * <code>
63. * array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
64. * </code>
65. *
66. * @param array(string=>mixed) $str The string to parse
67. * @return array(string=>mixed)
68. */
69. public static function parseQueryString ( $str )
70. {
71. $result = array();
73. // $params will be returned, but first we have to ensure that the dots
74. // are not converted to underscores
75. parse_str ( $str, $params );
77. $separator = ini_get ( 'arg_separator.input' );
78. if ( empty( $separator ) )
79. {
80. $separator = '&';
81. }
83. // go through $params and ensure that the dots are not converted to underscores
84. $args = explode ( $separator, $str );
85. foreach ( $args as $arg )
86. {
87. $parts = explode ( '=', $arg, 2 );
88. if ( !isset( $parts[1] ) )
89. {
90. $parts[1] = null;
91. }
93. if ( substr_count ( $parts[0], '[' ) === 0 )
94. {
95. $key = $parts[0];
96. }
97. else
98. {
99. $key = substr ( $parts[0], 0, strpos ( $parts[0], '[' ) );
100. }
102. $paramKey = str_replace ( '.', '_', $key );
103. if ( isset( $params[$paramKey] ) && strpos ( $paramKey, '_' ) !== false )
104. {
105. $newKey = '';
106. for ( $i = 0; $i < strlen ( $paramKey ); $i++ )
107. {
108. $newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
109. }
111. $keys = array_keys ( $params );
112. if ( ( $pos = array_search ( $paramKey, $keys ) ) !== false )
113. {
114. $keys[$pos] = $newKey;
115. }
116. $values = array_values ( $params );
117. $params = array_combine ( $keys, $values );
118. }
119. }
121. return $params;
122. }
124. /**
125. * Returns the current URL as a string from the array $source
126. * (by default $_SERVER).
127. *
128. * The following fields are used in building the URL:
129. * - HTTPS - determines the scheme ('http' or 'https'). 'https' only if
130. * the 'HTTPS' field is set or if it is 'on' or '1'
131. * - SERVER_NAME
132. * - SERVER_PORT - determines if port is default (80 = do not include port)
133. * or not default (other than 80 = include port)
134. * - REQUEST_URI
135. *
136. * For example, if $_SERVER has these fields:
137. * <code>
138. * $_SERVER = array(
139. * 'HTTPS' => '1',
140. * 'SERVER_NAME' => 'www.example.com',
141. * 'SERVER_PORT' => 80,
142. * 'REQUEST_URI' => '/index.php'
143. * );
144. * </code>
145. *
146. * Then by calling this function (with no parameters), this URL will be
147. * returned: 'http://www.example.com/index.php'.
148. *
149. * The source of the URL parts can be changed to be other than $_SERVER by
150. * specifying an array parameter when calling this function.
151. *
152. * @todo check if REQUEST_URI works in Windows + IIS.
153. * - Use PHP_SELF instead?
154. * - Or use SCRIPT_NAME + QUERY_STRING?
155. * - Or even use an ISAPI filter?
156. * @todo check for proxy servers
157. * - Use $_SERVER['HTTP_X_FORWARDED_SERVER']?
158. *
159. * @param array(string=>mixed) $source The default array source, default $_SERVER
160. * @return string
161. */
162. public static function getCurrentUrl ( array $source = null )
163. {
164. if ( $source === null )
165. {
166. $source = $_SERVER;
167. }
169. $url = '';
170. if ( isset( $source['HTTPS'] ) &&
171. ( strtolower( $source['HTTPS'] ) === 'on' || $source['HTTPS'] === '1' ) )
172. {
173. $url .= 'https://';
174. }
175. else
176. {
177. $url .= 'http://';
178. }
180. $url .= isset( $source['SERVER_NAME'] ) ? $source['SERVER_NAME'] : null;
181. if ( isset( $source['SERVER_PORT'] ) && $source['SERVER_PORT'] != 80 )
182. {
183. $url .= ":{$source['SERVER_PORT']}";
184. }
185. $url .= isset( $source['REQUEST_URI'] ) ? $source['REQUEST_URI'] : null;
186. return $url;
187. }
188. }
189. ?>

Documentation generated by phpDocumentor 1.4.3