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.  */
  27.  
  28. /**
  29.  * Class which provides a methods for handling URLs.
  30.  *
  31.  * @package Authentication
  32.  * @version //autogen//
  33.  */
  34. {
  35.     /**
  36.      * Normalizes the provided URL.
  37.      *
  38.      * The operations performed on the provided URL:
  39.      *  - trim
  40.      *  - add 'http://' in front if it is missing
  41.      *
  42.      * @param string $url The URL to normalize
  43.      * @return string 
  44.      */
  45.     public static function normalize$url )
  46.     {
  47.         $url trim$url );
  48.         if strpos$url'://' === false )
  49.         {
  50.             $url 'http://' $url;
  51.         }
  52.  
  53.         return $url;
  54.     }
  55.  
  56.     /**
  57.      * Appends a query value to the provided URL and returns the complete URL.
  58.      *
  59.      * @param string $url The URL to append a query value to
  60.      * @param string $key The query key to append to the URL
  61.      * @param string $value The query value to append to the URL
  62.      * @return string 
  63.      */
  64.     public static function appendQuery$url$key$value )
  65.     {
  66.         $parts parse_url$url );
  67.         if isset$parts['query') )
  68.         {
  69.             $parts['query'self::parseQueryString$parts['query');
  70.         }
  71.  
  72.         $parts['query'][$key$value;
  73.         return self::buildUrl$parts );
  74.     }
  75.  
  76.     /**
  77.      * Fetches the value of key $key from the query of the provided URL.
  78.      *
  79.      * @param string $url The URL from which to fetch the query value
  80.      * @param string $key The query key for which to get the value
  81.      * @return true 
  82.      */
  83.     public static function fetchQuery$url$key )
  84.     {
  85.         $parts parse_url$url );
  86.         if isset$parts['query') )
  87.         {
  88.             $parts['query'self::parseQueryString$parts['query');
  89.             return isset$parts['query'][$key) ) $parts['query'][$keynull;
  90.         }
  91.         return null;
  92.     }
  93.  
  94.     /**
  95.      * Creates a string URL from the provided $parts array.
  96.      *
  97.      * The format of the $parts array is similar to the one returned by
  98.      * parse_url(), with the 'query' part as an array(key=>value) (obtained with
  99.      * the function parse_str()).
  100.      *
  101.      * @param array(string=>mixed) $parts The parts of the URL
  102.      * @return string 
  103.      */
  104.     public static function buildUrlarray $parts )
  105.     {
  106.         $path isset$parts['path') ) $parts['path''/';
  107.         $query isset$parts['query') ) '?' http_build_query$parts['query''';
  108.         $fragment isset$parts['fragment') ) '#' $parts['fragment''';
  109.  
  110.         if isset$parts['host') )
  111.         {
  112.             $host $parts['host'];
  113.             $scheme isset$parts['scheme') ) $parts['scheme''://' 'http://';
  114.             $port isset$parts['port') ) ':' $parts['port''';
  115.             $result "{$scheme}{$host}{$port}{$path}{$query}{$fragment}";
  116.         }
  117.         else
  118.         {
  119.             $result = "{$path}{$query}{$fragment}";
  120.         }
  121.  
  122.         return $result;
  123.     }
  124.  
  125.     /**
  126.      * Parses the provided string and returns an associative array structure.
  127.      *
  128.      * It implements the functionality of the PHP function parse_str(), but
  129.      * without converting dots to underscores in parameter names.
  130.      *
  131.      * Example:
  132.      * <code>
  133.      * $str = 'foo[]=bar&openid.nonce=123456';
  134.      *
  135.      * parse_str( $str, $params );
  136.      * $params = ezcUrlTools::parseQuery( $str );
  137.      * </code>
  138.      *
  139.      * In the first case (parse_str()), $params will be:
  140.      * <code>
  141.      * array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
  142.      * </code>
  143.      *
  144.      * In the second case (ezcUrlTools::parseQueryString()), $params will be:
  145.      * <code>
  146.      * array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
  147.      * </code>
  148.      *
  149.      * The same function is defined in {@link ezcUrlTools} in the Url component.
  150.      *
  151.      * @param array(string=>mixed) $str The string to parse
  152.      * @return array(string=>mixed)
  153.      */
  154.     public static function parseQueryString( $str )
  155.     {
  156.         $result = array();
  157.  
  158.         // $params will be returned, but first we have to ensure that the dots
  159.         // are not converted to underscores
  160.         parse_str( $str, $params );
  161.  
  162.         $separator = ini_get( 'arg_separator.input' );
  163.         if ( empty( $separator ) )
  164.         {
  165.             $separator = '&';
  166.         }
  167.  
  168.         // go through $params and ensure that the dots are not converted to underscores
  169.         $args = explode( $separator, $str );
  170.         foreach ( $args as $arg )
  171.         {
  172.             $parts = explode( '=', $arg, 2 );
  173.             if ( !isset( $parts[1] ) )
  174.             {
  175.                 $parts[1] = null;
  176.             }
  177.  
  178.             if ( substr_count( $parts[0], '[' ) === 0 )
  179.             {
  180.                 $key = $parts[0];
  181.             }
  182.             else
  183.             {
  184.                 $key = substr( $parts[0], 0, strpos( $parts[0], '[' ) );
  185.             }
  186.  
  187.             $paramKey = str_replace( '.', '_', $key );
  188.             if ( isset( $params[$paramKey] ) && strpos( $paramKey, '_' ) !== false )
  189.             {
  190.                 $newKey = '';
  191.                 for ( $i = 0; $i < strlen( $paramKey ); $i++ )
  192.                 {
  193.                     $newKey .= ( $paramKey{$i} === '_' && $key{$i} === '.' ) ? '.' : $paramKey{$i};
  194.                 }
  195.  
  196.                 $keys = array_keys( $params );
  197.                 if ( ( $pos = array_search( $paramKey, $keys ) ) !== false )
  198.                 {
  199.                     $keys[$pos] = $newKey;
  200.                 }
  201.                 $values = array_values( $params );
  202.                 $params = array_combine( $keys, $values );
  203.             }
  204.         }
  205.  
  206.         return $params;
  207.     }
  208.  
  209. /**    
  210.      * Retrieves the headers and contents of $url using the HTTP method
  211.      * $method (GET, HEAD, POST), with an optional Accept $type
  212.      * (default 'text/html').
  213.      *
  214.      * @param string $url Then URL to retrieve
  215.      * @param string $method HTTP method to use, default GET
  216.      * @param string $type Accept type to use, eg. 'application/xrds+xml'
  217.      *
  218.      */
  219.     public static function getUrl( $url, $method = 'GET', $type = 'text/html' )
  220.     {
  221.         $opts = array( 'http' =>
  222.             array(
  223.                 'method'  => $method,
  224.                 'header'  => "Accept: {$type}"
  225.             )
  226.         );
  227.  
  228.         $context  = stream_context_create( $opts );
  229.  
  230.         if ( !$file = @fopen( $url, 'r', false, $context ) )
  231.         {
  232.             throw new <a href="../Authentication/ezcAuthenticationOpenidConnectionException.html">ezcAuthenticationOpenidConnectionException</a>( $url, $type );
  233.         }
  234.  
  235.         // get the HTTP headers
  236.         $metadata = stream_get_meta_data( $file );
  237.         if ( array_key_exists( 'headers', $metadata['wrapper_data'] ) )
  238.         {
  239.             // for php compiled with --with-curlwrappers
  240.             $headers = implode( "\n", $metadata['wrapper_data']['headers'] );
  241.         }
  242.         else
  243.         {
  244.             // for php compiled without --with-curlwrappers
  245.             $headers = implode( "\n", $metadata['wrapper_data'] );
  246.         }
  247.  
  248.         // get the contents of the $url
  249.         $contents = file_get_contents( $url, false, $context );
  250.  
  251.         // append the contents to the headers
  252.         return $headers . "\n" . $contents;
  253.     }
  254. }
Documentation generated by phpDocumentor 1.4.3