Permalink
Browse files

Added resolvePath() method

git-svn-id: http://svn.php.net/repository/pear/packages/Net_URL/trunk@113969 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
Richard Heyes
Richard Heyes committed Jan 30, 2003
1 parent fe2d382 commit c753e5647b0c3af27e1b6537f6fd6bba3771f817
Showing with 151 additions and 106 deletions.
  1. +79 −46 URL.php
  2. +1 −1 docs/example.php
  3. +71 −59 package.xml
View
125 URL.php
@@ -34,8 +34,8 @@
//
// Net_URL Class
-class Net_URL {
-
+class Net_URL
+{
/**
* Full url
* @var string
@@ -131,53 +131,44 @@ function Net_URL($url = null, $useBrackets = true)
$this->anchor = '';
$this->useBrackets = $useBrackets;
- // Parse the uri and store the various parts
+ // Parse the url and store the various parts
if (!empty($url)) {
$urlinfo = parse_url($url);
+
+ // Default path and querystring
+ $this->path = '/';
+ $this->querystring = array();
- // Protocol
- if (!empty($urlinfo['scheme'])) {
- $this->protocol = $urlinfo['scheme'];
- }
-
- // Username
- if (!empty($urlinfo['user'])) {
- $this->user = $urlinfo['user'];
- }
-
- // Password
- if (!empty($urlinfo['pass'])) {
- $this->pass = $urlinfo['pass'];
- }
-
- // Host
- if (!empty($urlinfo['host'])) {
- $this->host = $urlinfo['host'];
- }
-
- // Port
- if (!empty($urlinfo['port'])) {
- $this->port = $urlinfo['port'];
- }
-
- // Path
- if (!empty($urlinfo['path'])) {
- if ($urlinfo['path'][0] == '/') {
- $this->path = $urlinfo['path'];
- } else {
- $path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
- $this->path = sprintf('%s/%s', $path, $urlinfo['path']);
+ foreach ($urlinfo as $key => $value) {
+ switch ($key) {
+ case 'scheme':
+ $this->protocol = $value;
+ break;
+
+ case 'user':
+ case 'pass':
+ case 'host':
+ case 'port':
+ $this->$key = $value;
+ break;
+
+ case 'path':
+ if ($value[0] == '/') {
+ $this->path = $value;
+ } else {
+ $path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
+ $this->path = sprintf('%s/%s', $path, $value);
+ }
+ break;
+
+ case 'query':
+ $this->querystring = $this->_parseRawQueryString($value);
+ break;
+
+ case 'fragment':
+ $this->anchor = $value;
+ break;
}
- } else {
- $this->path = '/';
- }
-
- // Querystring
- $this->querystring = !empty($urlinfo['query']) ? $this->_parseRawQueryString($urlinfo['query']) : array();
-
- // Anchor
- if (!empty($urlinfo['fragment'])) {
- $this->anchor = $urlinfo['fragment'];
}
}
}
@@ -313,6 +304,48 @@ function _parseRawQuerystring($querystring)
}
return $return;
- }
+ }
+
+ /**
+ * Resolves //, ../ and ./ from a path and returns
+ * the result. Eg:
+ *
+ * /foo/bar/../boo.php => /foo/boo.php
+ * /foo/bar/../../boo.php => /boo.php
+ * /foo/bar/.././/boo.php => /foo/boo.php
+ *
+ * This method can also be called statically.
+ *
+ * @param string $url URL path to resolve
+ * @return string The result
+ */
+ function resolvePath($path)
+ {
+ $path = explode('/', str_replace('//', '/', $path));
+
+ for ($i=0; $i<count($path); $i++) {
+ if ($path[$i] == '.') {
+ unset($path[$i]);
+ $path = array_values($path);
+ $i--;
+
+ } elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != '') ) ) {
+ unset($path[$i]);
+ unset($path[$i-1]);
+ $path = array_values($path);
+ $i -= 2;
+
+ } elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
+ unset($path[$i]);
+ $path = array_values($path);
+ $i--;
+
+ } else {
+ continue;
+ }
+ }
+
+ return implode('/', $path);
+ }
}
?>
View
@@ -37,7 +37,7 @@
* constituent parts.
*/
- include('../URL.php');
+ include('Net/URL.php');
$url = &new Net_URL('http://www.example.com/foo/bar/index.php?foo=bar');
?>
View
@@ -2,73 +2,85 @@
<!DOCTYPE package SYSTEM "../package.dtd">
<package version="1.0">
- <name>Net_URL</name>
- <summary>Easy parsing of Urls</summary>
- <description>
+ <name>Net_URL</name>
+ <summary>Easy parsing of Urls</summary>
+ <description>
Provides easy parsing of URLs and their constituent parts.
- </description>
- <license>BSD</license>
+ </description>
+ <license>BSD</license>
- <maintainers>
- <maintainer>
- <user>richard</user>
- <name>Richard heyes</name>
- <email>richard@php.net</email>
- <role>lead</role>
- </maintainer>
- </maintainers>
+ <maintainers>
+ <maintainer>
+ <user>richard</user>
+ <name>Richard heyes</name>
+ <email>richard@php.net</email>
+ <role>lead</role>
+ </maintainer>
+ </maintainers>
- <release>
- <version>1.0.5</version>
- <date>2002-11-14</date>
- <state>stable</state>
- <notes>Allow for URLS such as ...?foo</notes>
- <filelist>
- <dir name="/" baseinstalldir="Net">
- <file role="php">URL.php</file>
- <dir name="docs" role="doc">
- <file role="doc">example.php</file>
- </dir>
- </dir>
- </filelist>
- </release>
-
- <changelog>
- <release>
- <version>1.0.4</version>
- <date>2002-07-27</date>
- <state>stable</state>
- <notes>License change</notes>
+ <release>
+ <version>1.0.7</version>
+ <date>2002-12-07</date>
+ <state>stable</state>
+ <notes>Added method to resolve URL paths of //, ../ and ./</notes>
+ <filelist>
+ <dir name="/" baseinstalldir="Net">
+ <file role="php">URL.php</file>
+ <dir name="docs" role="doc">
+ <file role="doc">example.php</file>
+ </dir>
+ </dir>
+ </filelist>
</release>
- <release>
- <version>1.0.3</version>
- <date>2002-06-20</date>
- <state>stable</state>
- <notes>Now uses HTTP_HOST if available.</notes>
- </release>
+ <changelog>
+ <release>
+ <version>1.0.6</version>
+ <date>2002-12-07</date>
+ <state>stable</state>
+ <notes>Make usage of [] optional</notes>
+ </release>
+
+ <release>
+ <version>1.0.5</version>
+ <date>2002-11-14</date>
+ <state>stable</state>
+ <notes>Allow for URLS such as ...?foo</notes>
+ </release>
- <release>
- <version>1.0.2</version>
- <date>2002-04-28</date>
- <state>stable</state>
- <notes>updated to fix a minor irritation when running on windows</notes>
- </release>
+ <release>
+ <version>1.0.4</version>
+ <date>2002-07-27</date>
+ <state>stable</state>
+ <notes>License change</notes>
+ </release>
- <release>
- <version>1.0.1</version>
- <date>2002-04-28</date>
- <state>stable</state>
- <notes>Maintenance release. Bugs fixed with path detection and defaults.</notes>
- </release>
+ <release>
+ <version>1.0.3</version>
+ <date>2002-06-20</date>
+ <state>stable</state>
+ <notes>Now uses HTTP_HOST if available.</notes>
+ </release>
- <release>
- <version>1.0</version>
- <date>2002-02-17</date>
- <state>stable</state>
- <notes>This is the initial release of the Net_URL package.</notes>
- </release>
+ <release>
+ <version>1.0.2</version>
+ <date>2002-04-28</date>
+ <state>stable</state>
+ <notes>updated to fix a minor irritation when running on windows</notes>
+ </release>
- </changelog>
+ <release>
+ <version>1.0.1</version>
+ <date>2002-04-28</date>
+ <state>stable</state>
+ <notes>Maintenance release. Bugs fixed with path detection and defaults.</notes>
+ </release>
+ <release>
+ <version>1.0</version>
+ <date>2002-02-17</date>
+ <state>stable</state>
+ <notes>This is the initial release of the Net_URL package.</notes>
+ </release>
+ </changelog>
</package>

0 comments on commit c753e56

Please sign in to comment.