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

Bug fix for relative directory removal #2055

Merged
merged 2 commits into from
Dec 6, 2012
Merged
Changes from 1 commit
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
21 changes: 20 additions & 1 deletion system/core/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,26 @@ protected function _parse_request_uri()
}

// Do some final cleaning of the URI and return it
return str_replace(array('//', '../'), '/', trim($uri, '/'));
return $this->_remove_relative_directory_str($uri);
}

// --------------------------------------------------------------------

/**
* Remove relative directory (../) and multi slashes (///)
* @param string $url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use only tabs as separators - no spaces (between 'string' and '$url' + although followed by a tab, you have spaces after both @param and @return).
Additionally, for the sake of consistency there should be an empty description line between 228 & 229.

* @return string
*/
private function _remove_relative_directory_str($url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No private methods please (protected is good) - this makes it impossible to extend parts of the core.
Also, why does this method need to be separated if it's only used by _parse_request_uri()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, would you prefer if it's merged into the previous method _parse_request_uri()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it would be used elsewhere - yes, otherwise it doesn't make sense (functionally). Just leave a comment above it to describe what it does.

{
$uris = array();
$tok = strtok($url, '/');
while ($tok !== false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false -> FALSE

Also might be a good idea to just do this and remove lines 235 & 239 (the latter doesn't seem correct anyway - shouldn't $url be present in there?):

while (($tok = strtok($url, '/')) !== FALSE)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to PHP manual: http://php.net/manual/en/function.strtok.php

<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well  */
$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}
?>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, agreed.

{
($tok != '..' && ! empty($tok) || $tok === '0') && $uris[] = $tok;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks really weird, should be converted to an if, with several style changes:

if (( ! empty($tok) && $tok !== '0') && $tok !== '..')
{
    $uris[] = $tok;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work :(

$ time php UnitTest-e8af5d1.php | grep -E '^F'
F 0/abc
F abc/0
F abc/0/def

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, my fault ... should've been this:

if (( ! empty($tok) OR $tok === '0') && $tok !== '..')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay fixed, committing now

$tok = strtok('/');
}
return implode('/', $uris);
}

// --------------------------------------------------------------------
Expand Down