2 lines
13 KiB
PHP
2 lines
13 KiB
PHP
|
<?php
|
||
|
class Net_URL2 { const OPTION_STRICT = 'strict'; const OPTION_USE_BRACKETS = 'use_brackets'; const OPTION_DROP_SEQUENCE = 'drop_sequence'; const OPTION_ENCODE_KEYS = 'encode_keys'; const OPTION_SEPARATOR_INPUT = 'input_separator'; const OPTION_SEPARATOR_OUTPUT = 'output_separator'; private $_options = array( self::OPTION_STRICT => true, self::OPTION_USE_BRACKETS => true, self::OPTION_DROP_SEQUENCE => true, self::OPTION_ENCODE_KEYS => true, self::OPTION_SEPARATOR_INPUT => '&', self::OPTION_SEPARATOR_OUTPUT => '&', ); private $_scheme = false; private $_userinfo = false; private $_host = false; private $_port = false; private $_path = ''; private $_query = false; private $_fragment = false; public function __construct($url, array $options = array()) { foreach ($options as $optionName => $value) { if (array_key_exists($optionName, $this->_options)) { $this->_options[$optionName] = $value; } } $this->parseUrl($url); } public function __set($var, $arg) { $method = 'set' . $var; if (method_exists($this, $method)) { $this->$method($arg); } } public function __get($var) { $method = 'get' . $var; if (method_exists($this, $method)) { return $this->$method(); } return false; } public function getScheme() { return $this->_scheme; } public function setScheme($scheme) { $this->_scheme = $scheme; return $this; } public function getUser() { return $this->_userinfo !== false ? preg_replace('(:.*$)', '', $this->_userinfo) : false; } public function getPassword() { return $this->_userinfo !== false ? substr(strstr($this->_userinfo, ':'), 1) : false; } public function getUserinfo() { return $this->_userinfo; } public function setUserinfo($userinfo, $password = false) { if ($password !== false) { $userinfo .= ':' . $password; } if ($userinfo !== false) { $userinfo = $this->_encodeData($userinfo); } $this->_userinfo = $userinfo; return $this; } public function getHost() { return $this->_host; } public function setHost($host) { $this->_host = $host; return $this; } public function getPort() { return $this->_port; } public function setPort($port) { $this->_port = $port; return $this; } public function getAuthority() { if (false === $this->_host) { return false; } $authority = ''; if (strlen($this->_userinfo)) { $authority .= $this->_userinfo . '@'; } $authority .= $this->_host; if ($this->_port !== false) { $authority .= ':' . $this->_port; } return $authority; } public function setAuthority($authority) { $this->_userinfo = false; $this->_host = false; $this->_port = false; if ('' === $authority) { $this->_host = $authority; return $this; } if (!preg_match('(^(([^@]*)@)?(.+?)(:(\d*))?$)', $authority, $matches)) { return $this; } if ($matches[1]) { $this->_userinfo = $this->_encodeData($matches[2]); } $this->_host = $matches[3]; if (isset($matches[5]) && strlen($matches[5])) { $this->_port = $matches[5]; } return $this; } public function getPath() { return $this->_path; } public function setPath($path) { $this->_path = $path; return $this; } public function getQuery() { return $this->_query; } public function setQuery($query) { $this->_query = $query; return $this; } public function getFragment() { return $this->_fragment; } public function setFragment($fragment) { $this->_fragment = $fragment; return $this; } public function getQueryVariables() { $separator = $this->getOption(self::OPTION_SEPARATOR_INPUT); $encodeKeys = $this->getOption(self::OPTION_ENCODE_KEYS); $useBrackets = $this->getOption(self::OPTION_USE_BRACKETS); $return = array(); for ($part = strtok($this->_query, $separator); strlen($part); $part = strtok($separator) ) { list($key, $value) = explode('=', $part, 2) + array(1 => ''); if ($encodeKeys) { $key = rawurldecode($key); } $value = rawurldecode($value); if ($useBrackets) { $return = $this->_queryArrayByKey($key, $value, $return); } else { if (isset($return[$key])) { $return[$key] = (array) $return[$key]; $return[$key][] = $value; } else { $return[$key] = $value; } } } return $return; } private function _queryArrayByKey($key, $value, array $array = array()) { if (!strlen($key)) { return $array; } $offset = $this->_queryKeyBra
|