Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid double decoding of URL #97

Merged
merged 4 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions lib/ezutils/classes/ezsys.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,34 +1154,25 @@ public static function init( $index = 'index.php', $forceVirtualHost = null )
}
}

// remove url and hash parameters
if ( isset( $requestUri[1] ) && $requestUri !== '/' )
// extract path and query string from URL
if ( strlen( $requestUri ) && $requestUri !== '/' )
{
$uriGetPos = strpos( $requestUri, '?' );
if ( $uriGetPos !== false )
{
$queryString = substr( $requestUri, $uriGetPos );
if ( $uriGetPos === 0 )
$requestUri = '';
else
$requestUri = substr( $requestUri, 0, $uriGetPos );
}

$uriHashPos = strpos( $requestUri, '#' );
if ( $uriHashPos === 0 )
$requestUri = '';
elseif ( $uriHashPos !== false )
$requestUri = substr( $requestUri, 0, $uriHashPos );
$uriParts = parse_url( $requestUri );
$requestUri = isset( $uriParts[ 'path' ] ) ? $uriParts[ 'path' ] : '';
$queryString = isset( $uriParts[ 'query' ] ) ? '?' . $uriParts[ 'query' ] : '';
}

// normalize slash use and url decode url if needed
if ( $requestUri === '/' || $requestUri === '' )
// normalize slash use
$requestUri = '/' . trim( $requestUri, '/ ' );

// url decode url if needed
if ( $requestUri !== '/' )
{
$requestUri = '';
$requestUri = urldecode( $requestUri );
}
else
{
$requestUri = '/' . urldecode( trim( $requestUri, '/ ' ) );
$requestUri = '';
}

$instance->AccessPath = array( 'siteaccess' => array( 'name' => '', 'url' => array() ),
Expand Down
23 changes: 16 additions & 7 deletions lib/ezutils/classes/ezuri.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ class eZURI
* Initializes with the URI string $uri. The URI string is split on / into an array.
*
* @param string $uri the URI to use
* @param boolean $decode controls if the $uri needs to be decoded first
* @return void
*/
public function __construct( $uri )
public function __construct( $uri, $decode = false )
{
$this->setURIString( $uri );
$this->setURIString( $uri, true, $decode );
}

/**
Expand All @@ -84,9 +85,8 @@ public function __construct( $uri )
* @param string $str the string to decode
* @return string decoded version of $str
*/
public static function decodeIRI( $str )
protected static function decodeIRI( $str )
{
$str = urldecode( $str ); // Decode %xx entries, we now have a utf-8 string
$codec = eZTextCodec::instance( 'utf-8' ); // Make sure string is converted from utf-8 to internal encoding
return $codec->convertString( $str );
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public static function codecURL( $url, $encode )
if ( $encode )
$data['path'] = eZURI::encodeIRI( $data['path'] ); // Make sure it is encoded to IRI format
else
$data['path'] = eZURI::decodeIRI( $data['path'] ); // Make sure it is dencoded to internal encoding
$data['path'] = eZURI::decodeIRI( urldecode( $data['path'] ) ); // Make sure it is decoded to internal encoding
}

// Reconstruct the URL
Expand Down Expand Up @@ -213,14 +213,20 @@ public static function decodeURL( $url )
*
* @param string $uri
* @param boolean $fullInitialize
* @param boolean $decode controls if the $uri needs to be decoded first
* @return void
*/
public function setURIString( $uri, $fullInitialize = true )
public function setURIString( $uri, $fullInitialize = true, $decode = false )
{
if ( strlen( $uri ) > 0 and
$uri[0] == '/' )
$uri = substr( $uri, 1 );

if( $decode )
{
$uri = urldecode( $uri );
}

$uri = eZURI::decodeIRI( $uri );

$this->URI = $uri;
Expand Down Expand Up @@ -576,7 +582,10 @@ public static function instance( $uri = false )
{
if ( !isset( $GLOBALS['eZURIRequestInstance'] ) )
{
$GLOBALS['eZURIRequestInstance'] = new eZURI( eZSys::requestURI() );
// Why urlencode? Because, eZURI expects an encoded URI but eZSYS returns a non-encoded URI
$uri = eZSys::requestURI();

$GLOBALS['eZURIRequestInstance'] = new eZURI( $uri );
}
return $GLOBALS['eZURIRequestInstance'];
}
Expand Down