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.  */
  27.  
  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. {
  41.     /**
  42.      * Parses the provided string and returns an associative array structure.
  43.      *
  44.      * It implements the functionality of the PHP function parse_str(), but
  45.      * without converting dots to underscores in parameter names.
  46.      *
  47.      * Example:
  48.      * <code>
  49.      * $str = 'foo[]=bar&openid.nonce=123456';
  50.      *
  51.      * parse_str( $str, $params );
  52.      * $params = ezcUrlTools::parseQueryString( $str );
  53.      * </code>
  54.      *
  55.      * In the first case (parse_str()), $params will be:
  56.      * <code>
  57.      * array( 'foo' => array( 'bar' ), 'openid_nonce' => '123456' );
  58.      * </code>
  59.      *
  60.      * In the second case (ezcUrlTools::parseQueryString()), $params will be:
  61.      * <code>
  62.      * array( 'foo' => array( 'bar' ), 'openid.nonce' => '123456' );
  63.      * </code>
  64.      *
  65.      * @param array(string=>mixed) $str The string to parse
  66.      * @return array(string=>mixed) 
  67.      */
  68.     public static function parseQueryString$str )
  69.     {
  70.         $result array();
  71.  
  72.         // $params will be returned, but first we have to ensure that the dots
  73.         // are not converted to underscores
  74.         parse_str$str$params );
  75.  
  76.         $separator ini_get'arg_separator.input' );
  77.         if empty$separator ) )
  78.         {
  79.             $separator '&';
  80.         }
  81.  
  82.         // go through $params and ensure that the dots are not converted to underscores
  83.         $args explode$separator$str );
  84.         foreach $args as $arg )
  85.         {
  86.             $parts explode'='$arg);
  87.             if !isset$parts[1) )
  88.             {
  89.                 $parts[1null;
  90.             }
  91.  
  92.             if substr_count$parts[0]'[' === )
  93.             {
  94.                 $key $parts[0];
  95.             }
  96.             else
  97.             {
  98.                 $key substr$parts[0]0strpos$parts[0]'[' ) );
  99.             }
  100.  
  101.             $paramKey str_replace'.''_'$key );
  102.             if isset$params[$paramKey&& strpos$paramKey'_' !== false )
  103.             {
  104.                 $newKey '';
  105.                 for $i 0$i strlen$paramKey )$i++ )
  106.                 {
  107.                     $newKey .= $paramKey{$i=== '_' && $key{$i=== '.' '.' $paramKey{$i};
  108.                 }
  109.  
  110.                 $keys array_keys$params );
  111.                 if ( ( $pos array_search$paramKey$keys ) ) !== false )
  112.                 {
  113.                     $keys[$pos$newKey;
  114.                 }
  115.                 $values array_values$params );
  116.                 $params array_combine$keys$values );
  117.             }
  118.         }
  119.  
  120.         return $params;
  121.     }
  122.  
  123.     /**
  124.      * Returns the current URL as a string from the array $source
  125.      * (by default $_SERVER).
  126.      *
  127.      * The following fields are used in building the URL:
  128.      *  - HTTPS - determines the scheme ('http' or 'https'). 'https' only if
  129.      *    the 'HTTPS' field is set or if it is 'on' or '1'
  130.      *  - SERVER_NAME
  131.      *  - SERVER_PORT - determines if port is default (80 = do not include port)
  132.      *    or not default (other than 80 = include port)
  133.      *  - REQUEST_URI
  134.      *
  135.      * For example, if $_SERVER has these fields:
  136.      * <code>
  137.      * $_SERVER = array(
  138.      *     'HTTPS' => '1',
  139.      *     'SERVER_NAME' => 'www.example.com',
  140.      *     'SERVER_PORT' => 80,
  141.      *     'REQUEST_URI' => '/index.php'
  142.      * );
  143.      * </code>
  144.      *
  145.      * Then by calling this function (with no parameters), this URL will be
  146.      * returned: 'http://www.example.com/index.php'.
  147.      *
  148.      * The source of the URL parts can be changed to be other than $_SERVER by
  149.      * specifying an array parameter when calling this function.
  150.      *
  151.      * @todo check if REQUEST_URI works in Windows + IIS.
  152.      *         - Use PHP_SELF instead?
  153.      *         - Or use SCRIPT_NAME + QUERY_STRING?
  154.      *         - Or even use an ISAPI filter?
  155.      * @todo check for proxy servers
  156.      *         - Use $_SERVER['HTTP_X_FORWARDED_SERVER']?
  157.      *
  158.      * @param array(string=>mixed) $source The default array source, default $_SERVER
  159.      * @return string 
  160.      */
  161.     public static function getCurrentUrlarray $source null )
  162.     {
  163.         if $source === null )
  164.         {
  165.             $source $_SERVER;
  166.         }
  167.  
  168.         $url '';
  169.         if isset$source['HTTPS'&& 
  170.              strtolower$source['HTTPS'=== 'on' || $source['HTTPS'=== '1' ) )
  171.         {
  172.             $url .= 'https://';
  173.         }
  174.         else
  175.         {
  176.             $url .= 'http://';
  177.         }
  178.  
  179.         $url .= isset$source['SERVER_NAME'$source['SERVER_NAME'null;
  180.         if isset$source['SERVER_PORT'&& $source['SERVER_PORT'!= 80 )
  181.         {
  182.             $url .= ":{$source['SERVER_PORT']}";
  183.         }
  184.         $url .= isset$source['REQUEST_URI'$source['REQUEST_URI'null;
  185.         return $url;
  186.     }
  187. }
  188. ?>
Documentation generated by phpDocumentor 1.4.3