Apache Zeta Components Manual :: File Source for url.php
Source for file url.php
Documentation is available at url.php
* File containing the ezcAuthenticationUrl class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @package Authentication
* Class which provides a methods for handling URLs.
* @package Authentication
* Normalizes the provided URL.
* The operations performed on the provided URL:
* - add 'http://' in front if it is missing
* @param string $url The URL to normalize
if ( strpos( $url, '://' ) ===
false )
* Appends a query value to the provided URL and returns the complete URL.
* @param string $url The URL to append a query value to
* @param string $key The query key to append to the URL
* @param string $value The query value to append to the URL
public static function appendQuery( $url, $key, $value )
if ( isset
( $parts['query'] ) )
$parts['query'] =
self::parseQueryString( $parts['query'] );
$parts['query'][$key] =
$value;
return self::buildUrl( $parts );
* Fetches the value of key $key from the query of the provided URL.
* @param string $url The URL from which to fetch the query value
* @param string $key The query key for which to get the value
if ( isset
( $parts['query'] ) )
$parts['query'] =
self::parseQueryString( $parts['query'] );
return ( isset
( $parts['query'][$key] ) ) ?
$parts['query'][$key] :
null;
* Creates a string URL from the provided $parts array.
* The format of the $parts array is similar to the one returned by
* parse_url(), with the 'query' part as an array(key=>value) (obtained with
* the function parse_str()).
* @param array(string=>mixed) $parts The parts of the URL
public static function buildUrl( array $parts )
$path =
( isset
( $parts['path'] ) ) ?
$parts['path'] :
'/';
$query =
( isset
( $parts['query'] ) ) ?
'?' .
http_build_query( $parts['query'] ) :
'';
$fragment =
( isset
( $parts['fragment'] ) ) ?
'#' .
$parts['fragment'] :
'';
if ( isset
( $parts['host'] ) )
$scheme =
( isset
( $parts['scheme'] ) ) ?
$parts['scheme'] .
'://' :
'http://';
$port =
( isset
( $parts['port'] ) ) ?
':' .
$parts['port'] :
'';
$result =
"{
$scheme}{
$host}{
$port}{
$path}{
$query}{
$fragment}";
$result = "{
$path}{
$query}{
$fragment}";
* 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.
* $str = 'foo[]=bar&openid.nonce=123456';
* parse_str( $str, $params );
* $params = ezcUrlTools::parseQuery( $str );
* In the first case (parse_str()), $params will be:
* array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
* In the second case (ezcUrlTools::parseQueryString()), $params will be:
* array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
* 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 )
// $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 ) )
// go through $params and ensure that the dots are not converted to underscores
$args = explode( $separator, $str );
foreach ( $args as $arg )
if ( !isset( $parts[1] ) )
if ( isset( $params[$paramKey] ) && strpos( $paramKey, '_' ) !== false )
for ( $i = 0; $i < strlen( $paramKey ); $i++ )
$newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
if ( ( $pos = array_search( $paramKey, $keys ) ) !== false )
* Retrieves the headers and contents of $url using the HTTP method
* $method (GET, HEAD, POST), with an optional Accept $type
* @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' )
'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 );
// for php compiled with --with-curlwrappers
$headers = implode( "\n", $metadata['wrapper_data']['headers'] );
// for php compiled without --with-curlwrappers
$headers = implode( "\n", $metadata['wrapper_data'] );
// get the contents of the $url
// append the contents to the headers
return $headers . "\n" . $contents;