-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
A.Grandt
committed
Jun 3, 2012
1 parent
af1bd30
commit ce08efc
Showing
2 changed files
with
187 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,105 @@ | ||
<?php | ||
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; | ||
include_once 'RelativePath.php'; | ||
$docRoot = $_SERVER["DOCUMENT_ROOT"] . "/"; | ||
|
||
if (!empty($_POST['path'])) { | ||
$paths = array(stripslashes($_POST['path'])); | ||
} else { | ||
// These paths are designed to be as insane as reasonably possible. Do not try to make much sense of them please :) | ||
$paths = array( | ||
"../testdir/subdir/anotherdir\\testfile.html", | ||
$docRoot . "/../../home/./John Doe/work/site/test/../../www/Project.1", | ||
$docRoot . "/../../../../../../../../home//./\\\\/John Doe/work\\site/test/../../www/Project.1", | ||
"../../../home/./John Doe/work/site/test/../../www/Project.1/", | ||
"../../../home/./John Doe/work/site/test/../../www/Project.1/" . "/../Project.2/index.html", | ||
"./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1" . "/" . "../Project.2/index.html", | ||
"../../home/../../../John Doe/work/site/test/../../www/Project.1" . "/" . "../Project.2/index.html/../", | ||
"/media/Projects/www/test/images/../../home/../../../John Doe/work/site/test.2/../../www/Project.1"); | ||
} | ||
|
||
function execTime($function, $title="", $iterations = 100000) { | ||
list($usec, $sec) = explode(" ",microtime()); | ||
$t1 = ($sec+$usec) * 1000; | ||
for ($i = 0 ; $i < $iterations ; $i++) { | ||
$function(); | ||
} | ||
list($usec, $sec) = explode(" ",microtime()); | ||
$t2 = ($sec+$usec) * 1000; | ||
$t2 = $t2 - $t1; | ||
if (!empty($title)) { | ||
print "<pre>$title: " . number_format((double)$t2, 2) . " ms.</pre>\n"; | ||
} | ||
return $t2; | ||
} | ||
?> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | ||
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8" /> | ||
<style type="text/css"> | ||
/*<![CDATA[*/ | ||
body,table,form { | ||
font-size: 10pt; | ||
font-family: verdana, helvetica, sans-serif; | ||
} | ||
|
||
dt { | ||
font-weight: bold; | ||
margin-bottom: 0px; | ||
padding-bottom: 0px; | ||
background-color: #eeeeee; | ||
} | ||
|
||
dd { | ||
margin-top: 0px; | ||
padding-top: 0px; | ||
margin-bottom: 2ex; | ||
} | ||
|
||
pre { | ||
margin: 0px; | ||
padding: 0px; | ||
} | ||
/*]]>*/ | ||
</style> | ||
<title>Relative Path tests</title> | ||
</head> | ||
<body> | ||
<h1>Relative Path tests</h1> | ||
<?php | ||
print "<p>docRoot: $docRoot</p>\n"; | ||
?> | ||
<form method="post"> | ||
<p>Path:<br /> | ||
<input type="text" size="120" name="path" | ||
value="<?php echo stripslashes($_POST['path']); ?>" /></p> | ||
<p><input type="submit" /></p> | ||
</form> | ||
<dl> | ||
<?php | ||
foreach ($paths as $path) { | ||
echo "<dt><pre>Path '$path' becomes:</pre></dt>\n"; | ||
echo "<dd><pre>"; | ||
echo "'" . RelativePath::getRelativePath($path) . "'\n"; | ||
print "</pre></dd>\n"; | ||
} | ||
?> | ||
</dl> | ||
<?php | ||
// This test requires PHP 5.3, due to the use of an anonymous function. | ||
// execTime(function() { | ||
// RelativePath::getRelativePath("./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1/../Project.2/index.html"); | ||
// }, "100,000 iterations took"); | ||
?> | ||
</body> | ||
</html> | ||
<?php | ||
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; | ||
require_once 'RelativePath.php'; | ||
$docRoot = $_SERVER["DOCUMENT_ROOT"] . "/"; | ||
|
||
if (!empty($_POST['path'])) { | ||
$paths = array(stripslashes($_POST['path'])); | ||
} else { | ||
// These paths are designed to be as insane as reasonably possible. Do not try to make much sense of them please :) | ||
$paths = array( | ||
"../testdir/subdir/anotherdir\\testfile.html", | ||
$docRoot . "/../../home/./John Doe/work/site/test/../../www/Project.1", | ||
$docRoot . "/../../../../../../../../home//./\\\\/John Doe/work\\site/test/../../www/Project.1", | ||
"../../../home/./John Doe/work/site/test/../../www/Project.1/", | ||
"../../../home/./John Doe/work/site/test/../../www/Project.1/" . "/../Project.2/index.html", | ||
"./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1" . "/" . "../Project.2/index.html", | ||
"../../home/../../../John Doe/work/site/test/../../www/Project.1" . "/" . "../Project.2/index.html/../", | ||
"/media/Projects/www/test/images/../../home/../../../John Doe/work/site/test.2/../../www/Project.1"); | ||
} | ||
|
||
/** | ||
* | ||
* Time execution time for a function | ||
* | ||
* @param unknown_type $function | ||
* @param unknown_type $title | ||
* @param unknown_type $iterations | ||
*/ | ||
function execTime($function, $title="", $iterations = 100000) { | ||
list($usec, $sec) = explode(" ", microtime()); | ||
$t1 = ($sec+$usec) * 1000; | ||
for ($i = 0 ; $i < $iterations ; $i++) { | ||
$function(); | ||
} | ||
list($usec, $sec) = explode(" ", microtime()); | ||
$t2 = ($sec+$usec) * 1000; | ||
$t2 = $t2 - $t1; | ||
if (!empty($title)) { | ||
print "<pre>$title: " . number_format((double)$t2, 2) . " ms.</pre>\n"; | ||
} | ||
return $t2; | ||
} | ||
?> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | ||
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8" /> | ||
<style type="text/css"> | ||
/*<![CDATA[*/ | ||
body,table,form { | ||
font-size: 10pt; | ||
font-family: verdana, helvetica, sans-serif; | ||
} | ||
|
||
dt { | ||
font-weight: bold; | ||
margin-bottom: 0px; | ||
padding-bottom: 0px; | ||
background-color: #eeeeee; | ||
} | ||
|
||
dd { | ||
margin-top: 0px; | ||
padding-top: 0px; | ||
margin-bottom: 2ex; | ||
} | ||
|
||
pre { | ||
margin: 0px; | ||
padding: 0px; | ||
} | ||
/*]]>*/ | ||
</style> | ||
<title>Relative Path tests</title> | ||
</head> | ||
<body> | ||
<h1>Relative Path tests</h1> | ||
<?php | ||
print "<p>docRoot: $docRoot</p>\n"; | ||
?> | ||
<form method="post"> | ||
<p>Path:<br /> | ||
<input type="text" size="120" name="path" | ||
value="<?php echo stripslashes($_POST['path']); ?>" /></p> | ||
<p><input type="submit" /></p> | ||
</form> | ||
<dl> | ||
<?php | ||
foreach ($paths as $path) { | ||
echo "<dt><pre>Path '$path' becomes:</pre></dt>\n"; | ||
echo "<dd><pre>"; | ||
echo "'" . RelativePath::getRelativePath($path) . "'\n"; | ||
print "</pre></dd>\n"; | ||
} | ||
?> | ||
</dl> | ||
<?php | ||
// This test requires PHP 5.3, due to the use of an anonymous function. | ||
// execTime(function() { | ||
// RelativePath::getRelativePath("./././../../../../../../../../../home/./John Doe/work/site/test/../../www/Project.1/../Project.2/index.html"); | ||
// }, "100,000 iterations took"); | ||
?> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,83 @@ | ||
<?php | ||
/** | ||
* Simple relative path normalizer utility. | ||
* | ||
* The utility was inspired by PHP's realPath function, though that required the given path to exist, RelativePath does not. | ||
* | ||
* Particularly useful is you need to parse and normalize paths gathered from for instance URL's and HTML pages. | ||
* | ||
* These functions are also included in Zip and ZipStream classes version 1.23 onwards on PHPClasses.org. | ||
* http://www.phpclasses.org/package/6110 and http://www.phpclasses.org/package/6616 respectively. | ||
* | ||
* License: GNU LGPL, Attribution required for commercial implementations, requested for everything else. | ||
* | ||
* @author A. Grandt | ||
* @copyright A. Grandt 2011 | ||
* @license GNU LGPL, Attribution required for commercial implementations, requested for everything else. | ||
* @link http://www.phpclasses.org/package/6844 | ||
* @version 1.01 | ||
*/ | ||
class RelativePath { | ||
const VERSION = 1.01; | ||
|
||
/** | ||
* Join $file to $dir path, and clean up any excess slashes. | ||
* | ||
* @param String $dir | ||
* @param String $file | ||
*/ | ||
public static function pathJoin($dir, $file) { | ||
if (empty($dir) || empty($file)) { | ||
return self::getRelativePath($dir . $file); | ||
} | ||
return self::getRelativePath($dir . '/' . $file); | ||
} | ||
|
||
/** | ||
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments. | ||
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off. | ||
* The returned path will not end in a "/". | ||
* | ||
* @param String $relPath The path to clean up | ||
* @return String the clean path | ||
*/ | ||
public static function getRelativePath($path) { | ||
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path)); | ||
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/')); | ||
|
||
$offset = 0; | ||
$sub = 0; | ||
$subOffset = 0; | ||
$root = ""; | ||
|
||
if (empty($dirs[0])) { | ||
$root = "/"; | ||
$dirs = array_splice($dirs, 1); | ||
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) { | ||
$root = strtoupper($dirs[0]) . "/"; | ||
$dirs = array_splice($dirs, 1); | ||
} | ||
|
||
$newDirs = array(); | ||
foreach($dirs as $dir) { | ||
if ($dir !== "..") { | ||
$subOffset--; | ||
$newDirs[++$offset] = $dir; | ||
} else { | ||
$subOffset++; | ||
if (--$offset < 0) { | ||
$offset = 0; | ||
if ($subOffset > $sub) { | ||
$sub++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (empty($root)) { | ||
$root = str_repeat("../", $sub); | ||
} | ||
return $root . implode("/", array_slice($newDirs, 0, $offset)); | ||
} | ||
} | ||
<?php | ||
/** | ||
* Simple relative path normalizer utility. | ||
* | ||
* The utility was inspired by PHP's realPath function, though that required the given path to exist, RelativePath does not. | ||
* | ||
* Particularly useful is you need to parse and normalize paths gathered from for instance URL's and HTML pages. | ||
* | ||
* These functions are also included in Zip and ZipStream classes version 1.23 onwards on PHPClasses.org. | ||
* http://www.phpclasses.org/package/6110 and http://www.phpclasses.org/package/6616 respectively. | ||
* | ||
* License: GNU LGPL, Attribution required for commercial implementations, requested for everything else. | ||
* | ||
* @author A. Grandt <[email protected]> | ||
* @copyright 2011 A. Grandt | ||
* @license GNU LGPL, Attribution required for commercial implementations, requested for everything else. | ||
* @link http://www.phpclasses.org/package/6844 | ||
* @version 1.01 | ||
*/ | ||
class RelativePath { | ||
const VERSION = 1.01; | ||
|
||
/** | ||
* Join $file to $dir path, and clean up any excess slashes. | ||
* | ||
* @param String $dir | ||
* @param String $file | ||
*/ | ||
public static function pathJoin($dir, $file) { | ||
if (empty($dir) || empty($file)) { | ||
return self::getRelativePath($dir . $file); | ||
} | ||
return self::getRelativePath($dir . '/' . $file); | ||
} | ||
|
||
/** | ||
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments. | ||
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off. | ||
* The returned path will not end in a "/". | ||
* | ||
* @param String $path The path to clean up | ||
* @return String the clean path | ||
*/ | ||
public static function getRelativePath($path) { | ||
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path)); | ||
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/')); | ||
|
||
$offset = 0; | ||
$sub = 0; | ||
$subOffset = 0; | ||
$root = ""; | ||
|
||
if (empty($dirs[0])) { | ||
$root = "/"; | ||
$dirs = array_splice($dirs, 1); | ||
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) { | ||
$root = strtoupper($dirs[0]) . "/"; | ||
$dirs = array_splice($dirs, 1); | ||
} | ||
|
||
$newDirs = array(); | ||
foreach ($dirs as $dir) { | ||
if ($dir !== "..") { | ||
$subOffset--; | ||
$newDirs[++$offset] = $dir; | ||
} else { | ||
$subOffset++; | ||
if (--$offset < 0) { | ||
$offset = 0; | ||
if ($subOffset > $sub) { | ||
$sub++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (empty($root)) { | ||
$root = str_repeat("../", $sub); | ||
} | ||
return $root . implode("/", array_slice($newDirs, 0, $offset)); | ||
} | ||
} | ||
?> |