|
Erfahrener Benutzer
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 |