 |
01.10.2007, 08:55
| Nach oben
#1 | | Erfahrener Benutzer
Registriert seit: 18.03.2005
Beiträge: 597
| URL korrekt zusammensetzen ?
Gibt es eine Klasse oder Funktion womit man URLs korrekt zusammensetzt ?
Beispiel: Code: www.domain.de/uhu/aha/ und /uhu/index.php
oder
http://www.domain.de/uhu/aha/ und ../../index.php
oder
www.domain.de/uhu und ./index.php
usw. usw. usw.
Dabei können alle möglichen Varianten vorkommen, die mir vorher nicht bekannt sind.
Bekannt sind nur 2 Teile, die Domain eventuell mit Pfad und die Datei auch mit eventuell Pfad.
Stehe irgendwie total eben auf dem Schlauch.
Was eigentlich jeder Browser macht, bräuchte ich als Funktion.
Geändert von CIX88 (01.10.2007 um 09:00 Uhr)
|
| |
01.10.2007, 14:02
| Nach oben
#2 | | Daniel Golowin
Registriert seit: 17.11.2005 Ort: Rheinland-Pfalz, Osthofen
Beiträge: 122
|
Hi CIX88,
irgendwo hatte ich schon mal diese Funktionalität gesehen, keine Ahnung allerdings wo das war. An sich lässt sich das ganze aber auch schnell selber bauen. Dazu sind diese Funktionen sehr hilfreich:
parse_url, pathinfo, implode, explode
Ich hab hier eine Klasse nur im Ansatz, allerdings könnte ich die noch heute Abend zu ende schreiben. Wenn also Interesse besteht, kann ich den Code auch als Beispiel dann noch posten.
|
| |
01.10.2007, 15:07
| Nach oben
#3 | | Erfahrener Benutzer
Registriert seit: 18.03.2005
Beiträge: 597
|
Jo die Funktion kenne ich alle, aber damit habe ich bisher keine saubere Lösung hinbekommen.
Wie gesagt, bei meinen Vorhaben liegen die einzelnen Teile der URL in allen möglichen Kombinationen vor.
|
| |
04.10.2007, 19:07
| Nach oben
#4 | | Neuer Benutzer
Registriert seit: 03.03.2006 Ort: Msobach
Beiträge: 14
|
willst du nur URLs unter deiner eigenen Domain zusammensetzen bzw. liegen die Dateien auf deinem Server oder willst du auch URLs von fremden Domains und Dateien zusammensetzen? Was ich eigentlich wissen will, ob man Zugriff auf's Dateisystem hat um realpath() benutzen zu können?
Wenn ja ist es ja relativ einfach, wenn nicht kenne ich keine kurze, schöne Lösung.
|
| |
05.10.2007, 00:13
| Nach oben
#5 | | Bastian Fenske
Registriert seit: 04.01.2006 Ort: Kassel
Beiträge: 853
| |
| |
05.10.2007, 07:42
| Nach oben
#6 | | Erfahrener Benutzer
Registriert seit: 18.03.2005
Beiträge: 597
| Zitat: |
auch URLs von fremden Domains und Dateien zusammensetzen
| Korrekt !
Es werden Dateien nach ihren HTTP-Status überprüft, dazu brauch ich die komplette URL, auch dann wenn die Datei in ein Frame etc.. ist. Überprüft werden alle Dateien die CSS, Bilder, JS, Flash, Frame, iFrame etc. sind.
Sprich alles das was ein Webbrowser auch macht, und der kann das auch
Wenn ich z.B. gerade auf deiser Seite bin, dann ist der Pfad erstmal developers-guide.net/forums/ und finde ich /files/images/rating/rating_3.gif.
Daraus muss dann developers-guide.net/files/images/rating/rating_3.gif werden.
Soweit hab ich das auch hinbekommen, Probleme machen jetzt noch Angaben wie ../../etc...
Nehmen wir als Beispiel :
URL : developers-guide.net/forums/6129,url-korrekt-zusammensetzen
Hier ist nun ein Bild ../images/rating/rating_3.gif
Dann muss developers-guide.net/images/rating/rating_3.gif dabei rauskommen.
Oder noch ein Beispiel:
Wir haben eine URL, in der sich ein Frame befindet. Nun sollen auch die URLs aus dem Frame erfasst werden (rekursives auslesen).
Mit parse_url() hab ich schon angefangen, bin aber damit auf die Fresse geflogen, wenn kein http:// mit in der URL steht.
Auch sollen alle Umleitungen mit 301, 302 mit beachtet werden, wo teilweise nur ein Dateiname steht.
Egal, wir bekommen das schon in den Griff.
|
| |
05.10.2007, 13:20
| Nach oben
#7 | | Benjamin Steininger
Registriert seit: 02.06.2005 Ort: weiher im tiefsten Odenwald
Beiträge: 1.208
| Zitat:
Zitat von CIX88 Zitat: |
auch URLs von fremden Domains und Dateien zusammensetzen
| Korrekt !
Es werden Dateien nach ihren HTTP-Status überprüft, dazu brauch ich die komplette URL, auch dann wenn die Datei in ein Frame etc.. ist. Überprüft werden alle Dateien die CSS, Bilder, JS, Flash, Frame, iFrame etc. sind.
Sprich alles das was ein Webbrowser auch macht, und der kann das auch
Wenn ich z.B. gerade auf deiser Seite bin, dann ist der Pfad erstmal developers-guide.net/forums/ und finde ich /files/images/rating/rating_3.gif.
Daraus muss dann developers-guide.net/files/images/rating/rating_3.gif werden.
Soweit hab ich das auch hinbekommen, Probleme machen jetzt noch Angaben wie ../../etc...
Nehmen wir als Beispiel :
URL : developers-guide.net/forums/6129,url-korrekt-zusammensetzen
Hier ist nun ein Bild ../images/rating/rating_3.gif
Dann muss developers-guide.net/images/rating/rating_3.gif dabei rauskommen.
Oder noch ein Beispiel:
Wir haben eine URL, in der sich ein Frame befindet. Nun sollen auch die URLs aus dem Frame erfasst werden (rekursives auslesen).
Mit parse_url() hab ich schon angefangen, bin aber damit auf die Fresse geflogen, wenn kein http:// mit in der URL steht.
Auch sollen alle Umleitungen mit 301, 302 mit beachtet werden, wo teilweise nur ein Dateiname steht.
Egal, wir bekommen das schon in den Griff. | Ich weis nicht wie oft das im Einsatz ist, hab es persönlich noch nirgends gesehen, aber eine Sache die den Pfad für relative Links auch noch verändern kann ist base: http://de.selfhtml.org/html/kopfdaten/basis.htm#adresse |
| |
07.10.2007, 23:44
| Nach oben
#8 | | Daniel Golowin
Registriert seit: 17.11.2005 Ort: Rheinland-Pfalz, Osthofen
Beiträge: 122
| Zitat:
Zitat von dago Ich hab hier eine Klasse nur im Ansatz, allerdings könnte ich die noch heute Abend zu ende schreiben. Wenn also Interesse besteht, kann ich den Code auch als Beispiel dann noch posten. | Ja, jetzt ist aus heute doch erst jetzt geworden. War viel unterwegs die Woche.
Ich hab zwar zuerst vor gehabt das ganze mit parse_url, implode, explode zu realisieren, bin aber dann doch auf regex Variante umgestiegen.
Danke dir CIX88 für dein Regex-Tester!
Also hier mal der jetziger Stand der Klasse: PHP-Code: <?php /** * DG-Library * * @author Daniel Golowin, contact(at)dg-scripts.de * @license http://creativecommons.org/licenses/by-sa/2.0/ * @version v2007100701 */
require_once('DGLib/URI/Scheme/Exception' . PHP_FILE_END); require_once('DGLib/URI/Scheme/Interface' . PHP_FILE_END); require_once('DGLib/URI/Scheme/Abstract' . PHP_FILE_END);
/** * @category DGLib * @package URI * @subpackage Scheme * @version v01 */ class DGLib_URISchemeHTTP extends DGLib_URISchemeAbstract implements DGLib_URIScheme { /** * Defined the uri scheme of class */ const SCHEME = 'http'; /** * Regular expression for parse uri to parts */ protected $_parseURIPattern = '~^(([^:]+:(-)?(//(([^@:]+)(\:([^@]*))?@)?([^/?#:]*)(\:([^/?#]*))?)?((.*\/)?([^?#]*))(\?([^#]*))?(#(.*))?~'; /** * Regular expression grammar rules for validation uri parts */ protected $_validatePattern = array( 'username' => '/^([^\W_]|[-_.!~*\'()\[\]]|(?:%[\da-fA-F]{2})|[;:&=+$,])+$/', 'password' => '/^.+$/', 'host' => '/^[a-zA-Z0-9._-]+$/', 'port' => '/^[0-9]{1,5}$/', 'path' => '/^(?:\/(?:(?:(?:[^\W_]|[-_.!~*\'()\[\]])|(?:%[\da-fA-F]{2})|[:@&=+$,;])*)?)+$/', 'path_relative' => '/^(?:(?:(?:(?:[^\W_]|[-_.!~*\'()\[\]])|(?:%[\da-fA-F]{2})|[:@&=+$,;])*)|\/?)+$/', 'query' => '/^(?:[;\/?:@&=+$,]|(?:[^\W_]|[-_.!~*\'()\[\]])|(?:%[\da-fA-F]{2}))*$/', 'fragment' => '/^(?:[;\/?:@&=+$,]|(?:[^\W_]|[-_.!~*\'()\[\]])|(?:%[\da-fA-F]{2}))*$/' ); /** * Array keys for uri parts */ protected $_uriData = array( 'username' => NULL, 'password' => NULL, 'host' => NULL, 'port' => NULL, 'path' => NULL, 'query' => NULL, 'fragment' => NULL ); /** * Array with get methods for the __call funcion on abstact class DGLib_URISchemeAbstract */ protected $_getMethods = array( 'getusername' => 'username', 'getpassword' => 'password', 'gethost' => 'host', 'getport' => 'port', 'getpath' => 'path', 'getquery' => 'query', 'getfragment' => 'fragment' ); /** * Array with set methods for the __call funcion on abstact class DGLib_URISchemeAbstract */ protected $_setMethods = array( 'setusername' => 'username', 'setpassword' => 'password', 'sethost' => 'host', 'setport' => 'port', 'setpath' => 'path', 'setquery' => 'query', 'setfragment' => 'fragment' ); /** * param string|array $uri The uri string or array with uri parts */ public function __construct($uri) { $this->setURI($uri); } /** * Returns a uri based on current values * * return string */ public function __toString() { return $this->getURI(); } /** * Returns a uri scheme * * return string */ public function getScheme() { return self::SCHEME; } /** * Returns a uri based on current values * * return string */ public function getURI() { $result = ''; if (!is_null($this->_uriData['host'])) { $result .= 'http://';
if (!is_null($this->_uriData['username'])) { $result .= $this->_uriData['username'];
if (!is_null($this->_uriData['password'])) $result .= ':' . $this->_uriData['password'];
$result .= '@'; }
$result .= $this->_uriData['host'];
if (!is_null($this->_uriData['port'])) $result .= ':' . $this->_uriData['port']; }
$result .= $this->_uriData['path'];
if (!is_null($this->_uriData['query'])) $result .= '?' . $this->_uriData['query']; if (!is_null($this->_uriData['fragment'])) $result .= '#' . $this->_uriData['fragment'];
return $result; } /** * Sets the uri, and returns the current uri * * @param string|uri $uri The uri string or array with uri parts * @throws DGLib_URISchemeException * @return string */ public function setURI($uri) { if (empty($uri)) { throw new DGLib_URISchemeException("The param \$uri is empty"); } if (is_string($uri)) { $uri = $this->_parseURI($uri); } $this_uri = $this->getURI(); foreach ($uri as $key => $value) { if (array_key_exists($key, $this->_uriData)) { $method = "set{$key}"; $this->$method($value); } else { throw new DGLib_URISchemeException("The property {$key} is not a part of http"); } } return $this_uri; } /** * Returns the path. Accept a path string to merge with current path * * @param string $path * @throws DGLib_URISchemeException * @return string */ public function getPath($path = '') { if (empty($path)) { $path = $this->_uriData['path']; } else { $path = $this->_parsePath($path);
if (!$this->_validateURIPart('path', $path)) { throw new DGLib_URISchemeException("Path {$path} is not a valid http path"); } } return $path; } /** * Returns the relative path of current path. Accept a path string to merge * with current path * * @param string $path * @throws DGLib_URISchemeException * @return string */ public function getRelativePath($path = '') { if (empty($path)) { return dirname($this->_uriData['path']) . '/'; } if (strpos($path, '/') === 0) { $path = $this->_parsePath($path); } else { $pattern = '~/[^/]*(?<!/[.]{2})/[.]{2}/|/[^/]*(?<!/[.]{2})/[.]{2}$|/[.]{1}$~'; while (preg_match($pattern, $path)) { $path = preg_replace($pattern, '/', $path); } $path = preg_replace('~^[.]{1,2}$|^[^/]*(?<!^[.]{2})/[.]{2}/|^[.]{1}/|(?<=/)[.]{1}/~', '', $path); } if (!$this->_validateURIPart('path_relative', $path)) { throw new DGLib_URISchemeException("Path {$path} is not a valid http path"); }
return $path; } /** * Sets the path for the current uri, and returns the current path * * @param string $path * @throws DGLib_URISchemeException * @return string */ public function setPath($path) { if (empty($path)) { $path = '/'; } else { $path = $this->_parsePath($path);
if (!$this->_validateURIPart('path', $path)) { throw new DGLib_URISchemeException("Path {$path} is not a valid http path"); } } $this_path = $this->_uriData['path']; $this->_uriData['path'] = $path; return $this_path; } /** * Sets the query for the current uri, and returns the current query. Accept * a query string or array with params and values * * @param string|array $query * @param string $prefix * @param string $separator * @throws DGLib_URISchemeException * @return string */ public function setQuery($query, $prefix = '', $separator = '&') { if (is_array($query)) { $query = http_build_query($query, $prefix, $separator); } if (!$this->_validateURIPart('query', $query)) { throw new DGLib_URISchemeException("Query {$query} is not a valid http $query"); } $this_query = $this->_uriData['query']; $this->_uriData['query'] = $query; return $this_query; } /** * Parse the uri string and returns the array with uri parts * * @param string $uri * @throws DGLib_URISchemeException * @return array */ protected function _parseURI($uri) { if (!preg_match($this->_parseURIPattern, $uri, $matches)) { throw new DGLib_URISchemeException(); } $uri_data = array(); if (!empty($matches[5])) $uri_data['username'] = $matches[5]; if (!empty($matches[7])) $uri_data['password'] = $matches[7]; if (!empty($matches[8])) $uri_data['host'] = $matches[8]; if (!empty($matches[10])) $uri_data['port'] = $matches[10]; if (!empty($matches[11])) $uri_data['path'] = $matches[11]; if (!empty($matches[15])) $uri_data['query'] = $matches[15]; if (!empty($matches[17])) $uri_data['fragment'] = $matches[17]; return $uri_data; } /** * Parse the path string and merge it with a current path * * @param string $path * @throws DGLib_URISchemeException * @return string */ protected function _parsePath($path) { if (strpos($path, '/') !== 0) { $path = ($this->_uriData['path'] != '/') ? dirname($this->_uriData['path']) . '/' . $path : '/' . $path; } $pattern = '~^[.]{1,2}/|/[^/]*(?<!/[.]{2})/[.]{2}/~'; while (preg_match($pattern, $path)) { $path = preg_replace($pattern, '/', $path); } $path = preg_replace('~^/?[.]{1,2}$|/[.]{1,2}/|/[^/]*(?<!/[.]{2})/[.]{2}$|/[.]{1}$~', '/', $path); return $path; } /** * Validate the uri. Returns true if all parts pass validation. Accept only * a array with the uri parts * * @param array $uri * @return boolean */ protected function _validateURI($uri) { if (empty($uri) or !is_array($uri)) { return FALSE; } foreach ($uri as $key => $value) { if (!array_key_exists($key, $this->_uriData) OR !$this->_validateURIPart($key, $value)) { return FALSE; } } return TRUE; } /** * Validate the part of uri. Returns true if the part pass validation. * * @param string $uri_part * @param string $value * @return boolean */ protected function _validateURIPart($uri_part, $value) { $uri_part = strtolower($uri_part); if (array_key_exists($uri_part, $this->_validatePattern)) { return preg_match($this->_validatePattern[$uri_part], $value) ? TRUE : FALSE; } else { throw new DGLib_URISchemeException(); } } } ?>
Da ich etwas Schreibfaul bin gehört noch diese abstrakte Klasse noch dazu: PHP-Code: <?php /** * DG-Library * * @author Daniel Golowin, contact(at)dg-scripts.de * @license http://creativecommons.org/licenses/by-sa/2.0/ * @version v2007100401 */
require_once('DGLib/URI/Scheme/Exception' . PHP_FILE_END);
/** * @category DGLib * @package URI * @subpackage Scheme * @version v01 */ abstract class DGLib_URISchemeAbstract { /** * Regular expression grammar rules for validation uri parts */ protected $_validatePattern = array(); /** * Array keys for uri parts */ protected $_uriData = array(); /** * Array with get methods for the __call funcion */ protected $_getMethods = array(); /** * Array with set methods for the __call funcion */ protected $_setMethods = array(); public function __get($property) { if (array_key_exists($property, $this->_uriData)) { $method = "get{$property}"; return $this->$method($value); } else { throw new DGLib_URISchemeException("The property {$property} is not a part of {$this->getScheme()}"); } } public function __set($property, $value) { if (array_key_exists($property, $this->_uriData)) { $method = "set{$property}"; $this->$method($value); } else { throw new DGLib_URISchemaException("The property {$property} is not a part of {$this->getScheme()}"); } } public function __isset($property) { if (array_key_exists($property, $this->_uriData) AND !is_null($this->_uriData[$property])) { return TRUE; } else { return FALSE; } } public function __unset($property) { if (array_key_exists($property, $this->_uriData)) { $this->_uriData[$property] = NULL; } } public function __call($method, $args) { $method = strtolower($method); $value = !empty($args[0]) ? $args[0] : NULL; if (array_key_exists($method, $this->_getMethods)) { if (empty($this->_uriData[$this->_getMethods[$method]])) { return FALSE; } return $this->_uriData[$this->_getMethods[$method]]; } else if (array_key_exists($method, $this->_setMethods)) { if ($this->_validateURIPart($this->_setMethods[$method], $value)) { $this_part_value = $this->_uriData[$this->_setMethods[$method]]; $this->_uriData[$this->_setMethods[$method]] = $value; return $this_part_value; } else { throw new DGLib_URISchemeException("Value {$value} is not a valid {$this->getScheme()} {$this->_setMethods[$method]}"); } } else { throw new DGLib_URISchemeException("The method {$method} is not exists"); } } /** * Abstract function for validate the uri parts. * * @param array $uri * @return boolean */ abstract protected function _validateURI($uri); /** * Abstract function for validate the a part of uri. * * @param string $uri_part * @param string $value * @return boolean */ abstract protected function _validateURIPart($uri_part, $value); } ?>
Beispiel Skript: PHP-Code: <?php
define('PHP_FILE_END', '.php'); define('BASE_DIR', realpath(dirname(__FILE__) . '/../'));
error_reporting(E_ALL | E_STRICT);
set_include_path(get_include_path() . PATH_SEPARATOR . BASE_DIR . PATH_SEPARATOR . BASE_DIR . '/library/' );
require_once('DGLib/URI/Scheme/HTTP' . PHP_FILE_END);
$uri = "http://user:pass@www.example.tld:80/it/is/an/path/to/file.txt?query=value#frag"; $UriHTTP = new DGLib_URISchemeHTTP($uri);
$paths = array( "../../../g", "../../../../g", "/./g", "/../g", "g.", ".g", "g..", "..g", "./../g", "./g/.", "g/./h", "g/../h", ".", "..", "g/h/.", "g/h/..", "/.", "/..", "/g/h/.", "/g/h/..", );
print '<pre>'; foreach ($paths as $path) { print $path . '<br>'; print ' relative: ' . $UriHTTP->getRelativePath($path) . '<br />'; print ' absolute: ' . $Uri | |