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

## Source for file url.php

Documentation is available at url.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationUrl 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. * @filesource
24. * @package Authentication
25. * @version //autogen//
26. */
28. /**
29. * Class which provides a methods for handling URLs.
30. *
31. * @package Authentication
32. * @version //autogen//
33. */
34. class ezcAuthenticationUrl
35. {
36. /**
37. * Normalizes the provided URL.
38. *
39. * The operations performed on the provided URL:
40. * - trim
41. * - add 'http://' in front if it is missing
42. *
43. * @param string $url The URL to normalize
44. * @return string
45. */
46. public static function normalize ( $url )
47. {
48. $url = trim ( $url );
49. if ( strpos ( $url, '://' ) === false )
50. {
51. $url = 'http://' . $url;
52. }
54. return $url;
55. }
57. /**
58. * Appends a query value to the provided URL and returns the complete URL.
59. *
60. * @param string $url The URL to append a query value to
61. * @param string $key The query key to append to the URL
62. * @param string $value The query value to append to the URL
63. * @return string
64. */
65. public static function appendQuery ( $url, $key, $value )
66. {
67. $parts = parse_url ( $url );
68. if ( isset( $parts['query'] ) )
69. {
70. $parts['query'] = self::parseQueryString( $parts['query'] );
71. }
73. $parts['query'][$key] = $value;
74. return self::buildUrl( $parts );
75. }
77. /**
78. * Fetches the value of key $key from the query of the provided URL.
79. *
80. * @param string $url The URL from which to fetch the query value
81. * @param string $key The query key for which to get the value
82. * @return true
83. */
84. public static function fetchQuery ( $url, $key )
85. {
86. $parts = parse_url ( $url );
87. if ( isset( $parts['query'] ) )
88. {
89. $parts['query'] = self::parseQueryString( $parts['query'] );
90. return ( isset( $parts['query'][$key] ) ) ? $parts['query'][$key] : null;
91. }
92. return null;
93. }
95. /**
96. * Creates a string URL from the provided $parts array.
97. *
98. * The format of the $parts array is similar to the one returned by
99. * parse_url(), with the 'query' part as an array(key=>value) (obtained with
100. * the function parse_str()).
101. *
102. * @param array(string=>mixed) $parts The parts of the URL
103. * @return string
104. */
105. public static function buildUrl ( array $parts )
106. {
107. $path = ( isset( $parts['path'] ) ) ? $parts['path'] : '/';
108. $query = ( isset( $parts['query'] ) ) ? '?' . http_build_query( $parts['query'] ) : '';
109. $fragment = ( isset( $parts['fragment'] ) ) ? '#' . $parts['fragment'] : '';
111. if ( isset( $parts['host'] ) )
112. {
113. $host = $parts['host'];
114. $scheme = ( isset( $parts['scheme'] ) ) ? $parts['scheme'] . '://' : 'http://';
115. $port = ( isset( $parts['port'] ) ) ? ':' . $parts['port'] : '';
116. $result = "{$scheme}{$host}{$port}{$path}{$query}{$fragment}";

}
- else
- {
- $result = "{$path}{$query}{$fragment}";
- }
- return $result;
- }
- /**
- * Parses the provided string and returns an associative array structure.
- *
- * It implements the functionality of the PHP function parse_str(), but
- * without converting dots to underscores in parameter names.
- *
- * Example:
- * <code>
- * $str = 'foo[]=bar&openid.nonce=123456';
- *
- * parse_str( $str, $params );
- * $params = ezcUrlTools::parseQuery( $str );
- * </code>
- *
- * In the first case (parse_str()), $params will be:
- * <code>
- * array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
- * </code>
- *
- * In the second case (ezcUrlTools::parseQueryString()), $params will be:
- * <code>
- * array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
- * </code>
- *
- * The same function is defined in {@link ezcUrlTools} in the Url component.
- *
- * @param array(string=>mixed) $str The string to parse
- * @return array(string=>mixed)
- */
- public static function parseQueryString( $str )
- {
- $result = array();
- // $params will be returned, but first we have to ensure that the dots
- // are not converted to underscores
- parse_str( $str, $params );
- $separator = ini_get( 'arg_separator.input' );
- if ( empty( $separator ) )
- {
- $separator = '&';
- }
- // go through $params and ensure that the dots are not converted to underscores
- $args = explode ( $separator, $str );
- foreach ( $args as $arg )
- {
- $parts = explode ( '=', $arg, 2 );
- if ( !isset( $parts[1] ) )
- {
- $parts[1] = null;
- }
- if ( substr_count ( $parts[0], '[' ) === 0 )
- {
- $key = $parts[0];
- }
- else
- {
- $key = substr ( $parts[0], 0, strpos ( $parts[0], '[' ) );
- }
- $paramKey = str_replace ( '.', '_', $key );
- if ( isset( $params[$paramKey] ) && strpos ( $paramKey, '_' ) !== false )
- {
- $newKey = '';
- for ( $i = 0; $i < strlen ( $paramKey ); $i++ )
- {
- $newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
- }
- $keys = array_keys ( $params );
- if ( ( $pos = array_search ( $paramKey, $keys ) ) !== false )
- {
- $keys[$pos] = $newKey;
- }
- $values = array_values ( $params );
- $params = array_combine ( $keys, $values );
- }
- }
- return $params;
- }
- /**
- * Retrieves the headers and contents of $url using the HTTP method
- * $method (GET, HEAD, POST), with an optional Accept $type
- * (default 'text/html').
- *
- * @param string $url Then URL to retrieve
- * @param string $method HTTP method to use, default GET
- * @param string $type Accept type to use, eg. 'application/xrds+xml'
- *
- */
- public static function getUrl ( $url, $method = 'GET', $type = 'text/html' )
- {
- $opts = array( 'http' =>
- array(
- 'method' => $method,
- 'header' => "Accept: {$type}"
- )
- );
- $context = stream_context_create( $opts );
- if ( !$file = @fopen( $url, 'r', false, $context ) )
- {
- throw new <a href="../Authentication/ezcAuthenticationOpenidConnectionException.html">ezcAuthenticationOpenidConnectionException</a>( $url, $type );
- }
- // get the HTTP headers
- $metadata = stream_get_meta_data ( $file );
- if ( array_key_exists ( 'headers', $metadata['wrapper_data'] ) )
- {
- // for php compiled with --with-curlwrappers
- $headers = implode ( "\n", $metadata['wrapper_data']['headers'] );
- }
- else
- {
- // for php compiled without --with-curlwrappers
- $headers = implode ( "\n", $metadata['wrapper_data'] );
- }
- // get the contents of the $url
- $contents = file_get_contents ( $url, false, $context );
- // append the contents to the headers
- return $headers . "\n" . $contents;
- }
- }
Documentation generated by phpDocumentor 1.4.3