summaryrefslogtreecommitdiff
path: root/extlib/Net
diff options
context:
space:
mode:
authorEvan Prodromou <evan@controlyourself.ca>2009-02-26 13:22:51 -0800
committerEvan Prodromou <evan@controlyourself.ca>2009-02-26 13:22:51 -0800
commit34a61b40f47a52d1fdcf7cd907f3ac53b96b038e (patch)
tree14ddeb18d9e020760d8c2308a12d858b6eab4a17 /extlib/Net
parent478192fa3b2375a0ff0a5ff03956381e5da7a6ac (diff)
parent8c3fe83c645265a11a3fca80cac2ac48ea72cdab (diff)
Merge branch '0.7.x' into 0.8.x
Diffstat (limited to 'extlib/Net')
-rw-r--r--extlib/Net/URL.php485
-rw-r--r--extlib/Net/URL/Mapper.php324
-rw-r--r--extlib/Net/URL/Mapper/Exception.php104
-rw-r--r--extlib/Net/URL/Mapper/Part.php142
-rw-r--r--extlib/Net/URL/Mapper/Part/Dynamic.php81
-rw-r--r--extlib/Net/URL/Mapper/Part/Fixed.php70
-rw-r--r--extlib/Net/URL/Mapper/Part/Wildcard.php80
-rw-r--r--extlib/Net/URL/Mapper/Path.php430
8 files changed, 1716 insertions, 0 deletions
diff --git a/extlib/Net/URL.php b/extlib/Net/URL.php
new file mode 100644
index 000000000..3dcfef60d
--- /dev/null
+++ b/extlib/Net/URL.php
@@ -0,0 +1,485 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Copyright (c) 2002-2004, Richard Heyes |
+// | All rights reserved. |
+// | |
+// | Redistribution and use in source and binary forms, with or without |
+// | modification, are permitted provided that the following conditions |
+// | are met: |
+// | |
+// | o Redistributions of source code must retain the above copyright |
+// | notice, this list of conditions and the following disclaimer. |
+// | o Redistributions in binary form must reproduce the above copyright |
+// | notice, this list of conditions and the following disclaimer in the |
+// | documentation and/or other materials provided with the distribution.|
+// | o The names of the authors may not be used to endorse or promote |
+// | products derived from this software without specific prior written |
+// | permission. |
+// | |
+// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// | |
+// +-----------------------------------------------------------------------+
+// | Author: Richard Heyes <richard at php net> |
+// +-----------------------------------------------------------------------+
+//
+// $Id: URL.php,v 1.49 2007/06/28 14:43:07 davidc Exp $
+//
+// Net_URL Class
+
+
+class Net_URL
+{
+ var $options = array('encode_query_keys' => false);
+ /**
+ * Full url
+ * @var string
+ */
+ var $url;
+
+ /**
+ * Protocol
+ * @var string
+ */
+ var $protocol;
+
+ /**
+ * Username
+ * @var string
+ */
+ var $username;
+
+ /**
+ * Password
+ * @var string
+ */
+ var $password;
+
+ /**
+ * Host
+ * @var string
+ */
+ var $host;
+
+ /**
+ * Port
+ * @var integer
+ */
+ var $port;
+
+ /**
+ * Path
+ * @var string
+ */
+ var $path;
+
+ /**
+ * Query string
+ * @var array
+ */
+ var $querystring;
+
+ /**
+ * Anchor
+ * @var string
+ */
+ var $anchor;
+
+ /**
+ * Whether to use []
+ * @var bool
+ */
+ var $useBrackets;
+
+ /**
+ * PHP4 Constructor
+ *
+ * @see __construct()
+ */
+ function Net_URL($url = null, $useBrackets = true)
+ {
+ $this->__construct($url, $useBrackets);
+ }
+
+ /**
+ * PHP5 Constructor
+ *
+ * Parses the given url and stores the various parts
+ * Defaults are used in certain cases
+ *
+ * @param string $url Optional URL
+ * @param bool $useBrackets Whether to use square brackets when
+ * multiple querystrings with the same name
+ * exist
+ */
+ function __construct($url = null, $useBrackets = true)
+ {
+ $this->url = $url;
+ $this->useBrackets = $useBrackets;
+
+ $this->initialize();
+ }
+
+ function initialize()
+ {
+ $HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
+
+ $this->user = '';
+ $this->pass = '';
+ $this->host = '';
+ $this->port = 80;
+ $this->path = '';
+ $this->querystring = array();
+ $this->anchor = '';
+
+ // Only use defaults if not an absolute URL given
+ if (!preg_match('/^[a-z0-9]+:\/\//i', $this->url)) {
+ $this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
+
+ /**
+ * Figure out host/port
+ */
+ if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) &&
+ preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches))
+ {
+ $host = $matches[1];
+ if (!empty($matches[3])) {
+ $port = $matches[3];
+ } else {
+ $port = $this->getStandardPort($this->protocol);
+ }
+ }
+
+ $this->user = '';
+ $this->pass = '';
+ $this->host = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
+ $this->port = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this->getStandardPort($this->protocol));
+ $this->path = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
+ $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
+ $this->anchor = '';
+ }
+
+ // Parse the url and store the various parts
+ if (!empty($this->url)) {
+ $urlinfo = parse_url($this->url);
+
+ // Default querystring
+ $this->querystring = array();
+
+ foreach ($urlinfo as $key => $value) {
+ switch ($key) {
+ case 'scheme':
+ $this->protocol = $value;
+ $this->port = $this->getStandardPort($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;
+ }
+ }
+ }
+ }
+ /**
+ * Returns full url
+ *
+ * @return string Full url
+ * @access public
+ */
+ function getURL()
+ {
+ $querystring = $this->getQueryString();
+
+ $this->url = $this->protocol . '://'
+ . $this->user . (!empty($this->pass) ? ':' : '')
+ . $this->pass . (!empty($this->user) ? '@' : '')
+ . $this->host . ($this->port == $this->getStandardPort($this->protocol) ? '' : ':' . $this->port)
+ . $this->path
+ . (!empty($querystring) ? '?' . $querystring : '')
+ . (!empty($this->anchor) ? '#' . $this->anchor : '');
+
+ return $this->url;
+ }
+
+ /**
+ * Adds or updates a querystring item (URL parameter).
+ * Automatically encodes parameters with rawurlencode() if $preencoded
+ * is false.
+ * You can pass an array to $value, it gets mapped via [] in the URL if
+ * $this->useBrackets is activated.
+ *
+ * @param string $name Name of item
+ * @param string $value Value of item
+ * @param bool $preencoded Whether value is urlencoded or not, default = not
+ * @access public
+ */
+ function addQueryString($name, $value, $preencoded = false)
+ {
+ if ($this->getOption('encode_query_keys')) {
+ $name = rawurlencode($name);
+ }
+
+ if ($preencoded) {
+ $this->querystring[$name] = $value;
+ } else {
+ $this->querystring[$name] = is_array($value) ? array_map('rawurlencode', $value): rawurlencode($value);
+ }
+ }
+
+ /**
+ * Removes a querystring item
+ *
+ * @param string $name Name of item
+ * @access public
+ */
+ function removeQueryString($name)
+ {
+ if ($this->getOption('encode_query_keys')) {
+ $name = rawurlencode($name);
+ }
+
+ if (isset($this->querystring[$name])) {
+ unset($this->querystring[$name]);
+ }
+ }
+
+ /**
+ * Sets the querystring to literally what you supply
+ *
+ * @param string $querystring The querystring data. Should be of the format foo=bar&x=y etc
+ * @access public
+ */
+ function addRawQueryString($querystring)
+ {
+ $this->querystring = $this->_parseRawQueryString($querystring);
+ }
+
+ /**
+ * Returns flat querystring
+ *
+ * @return string Querystring
+ * @access public
+ */
+ function getQueryString()
+ {
+ if (!empty($this->querystring)) {
+ foreach ($this->querystring as $name => $value) {
+ // Encode var name
+ $name = rawurlencode($name);
+
+ if (is_array($value)) {
+ foreach ($value as $k => $v) {
+ $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
+ }
+ } elseif (!is_null($value)) {
+ $querystring[] = $name . '=' . $value;
+ } else {
+ $querystring[] = $name;
+ }
+ }
+ $querystring = implode(ini_get('arg_separator.output'), $querystring);
+ } else {
+ $querystring = '';
+ }
+
+ return $querystring;
+ }
+
+ /**
+ * Parses raw querystring and returns an array of it
+ *
+ * @param string $querystring The querystring to parse
+ * @return array An array of the querystring data
+ * @access private
+ */
+ function _parseRawQuerystring($querystring)
+ {
+ $parts = preg_split('/[' . preg_quote(ini_get('arg_separator.input'), '/') . ']/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
+ $return = array();
+
+ foreach ($parts as $part) {
+ if (strpos($part, '=') !== false) {
+ $value = substr($part, strpos($part, '=') + 1);
+ $key = substr($part, 0, strpos($part, '='));
+ } else {
+ $value = null;
+ $key = $part;
+ }
+
+ if (!$this->getOption('encode_query_keys')) {
+ $key = rawurldecode($key);
+ }
+
+ if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) {
+ $key = $matches[1];
+ $idx = $matches[2];
+
+ // Ensure is an array
+ if (empty($return[$key]) || !is_array($return[$key])) {
+ $return[$key] = array();
+ }
+
+ // Add data
+ if ($idx === '') {
+ $return[$key][] = $value;
+ } else {
+ $return[$key][$idx] = $value;
+ }
+ } elseif (!$this->useBrackets AND !empty($return[$key])) {
+ $return[$key] = (array)$return[$key];
+ $return[$key][] = $value;
+ } else {
+ $return[$key] = $value;
+ }
+ }
+
+ 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 $path 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);
+ }
+
+ /**
+ * Returns the standard port number for a protocol
+ *
+ * @param string $scheme The protocol to lookup
+ * @return integer Port number or NULL if no scheme matches
+ *
+ * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
+ */
+ function getStandardPort($scheme)
+ {
+ switch (strtolower($scheme)) {
+ case 'http': return 80;
+ case 'https': return 443;
+ case 'ftp': return 21;
+ case 'imap': return 143;
+ case 'imaps': return 993;
+ case 'pop3': return 110;
+ case 'pop3s': return 995;
+ default: return null;
+ }
+ }
+
+ /**
+ * Forces the URL to a particular protocol
+ *
+ * @param string $protocol Protocol to force the URL to
+ * @param integer $port Optional port (standard port is used by default)
+ */
+ function setProtocol($protocol, $port = null)
+ {
+ $this->protocol = $protocol;
+ $this->port = is_null($port) ? $this->getStandardPort($protocol) : $port;
+ }
+
+ /**
+ * Set an option
+ *
+ * This function set an option
+ * to be used thorough the script.
+ *
+ * @access public
+ * @param string $optionName The optionname to set
+ * @param string $value The value of this option.
+ */
+ function setOption($optionName, $value)
+ {
+ if (!array_key_exists($optionName, $this->options)) {
+ return false;
+ }
+
+ $this->options[$optionName] = $value;
+ $this->initialize();
+ }
+
+ /**
+ * Get an option
+ *
+ * This function gets an option
+ * from the $this->options array
+ * and return it's value.
+ *
+ * @access public
+ * @param string $opionName The name of the option to retrieve
+ * @see $this->options
+ */
+ function getOption($optionName)
+ {
+ if (!isset($this->options[$optionName])) {
+ return false;
+ }
+
+ return $this->options[$optionName];
+ }
+
+}
+?>
diff --git a/extlib/Net/URL/Mapper.php b/extlib/Net/URL/Mapper.php
new file mode 100644
index 000000000..65e38818b
--- /dev/null
+++ b/extlib/Net/URL/Mapper.php
@@ -0,0 +1,324 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Mapper.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+require_once 'Net/URL/Mapper/Path.php';
+require_once 'Net/URL/Mapper/Exception.php';
+
+/**
+ * URL parser and mapper class
+ *
+ * This class takes an URL and a configuration and returns formatted data
+ * about the request according to a configuration parameter
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @version Release: @package_version@
+ */
+class Net_URL_Mapper
+{
+ /**
+ * Array of Net_URL_Mapper instances
+ * @var array
+ */
+ private static $instances = array();
+
+ /**
+ * Mapped paths collection
+ * @var array
+ */
+ protected $paths = array();
+
+ /**
+ * Prefix used for url mapping
+ * @var string
+ */
+ protected $prefix = '';
+
+ /**
+ * Optional scriptname if mod_rewrite is not available
+ * @var string
+ */
+ protected $scriptname = '';
+
+ /**
+ * Mapper instance id
+ * @var string
+ */
+ protected $id = '__default__';
+
+ /**
+ * Class constructor
+ * Constructor is private, you should use getInstance() instead.
+ */
+ private function __construct() { }
+
+ /**
+ * Returns a singleton object corresponding to the requested instance id
+ * @param string Requested instance name
+ * @return Object Net_URL_Mapper Singleton
+ */
+ public static function getInstance($id = '__default__')
+ {
+ if (!isset(self::$instances[$id])) {
+ $m = new Net_URL_Mapper();
+ $m->id = $id;
+ self::$instances[$id] = $m;
+ }
+ return self::$instances[$id];
+ }
+
+ /**
+ * Returns the instance id
+ * @return string Mapper instance id
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Parses a path and creates a connection
+ * @param string The path to connect
+ * @param array Default values for path parts
+ * @param array Regular expressions for path parts
+ * @return object Net_URL_Mapper_Path
+ */
+ public function connect($path, $defaults = array(), $rules = array())
+ {
+ $pathObj = new Net_URL_Mapper_Path($path, $defaults, $rules);
+ $this->addPath($pathObj);
+ return $pathObj;
+ }
+
+ /**
+ * Set the url prefix if needed
+ *
+ * Example: using the prefix to differenciate mapper instances
+ * <code>
+ * $fr = Net_URL_Mapper::getInstance('fr');
+ * $fr->setPrefix('/fr');
+ * $en = Net_URL_Mapper::getInstance('en');
+ * $en->setPrefix('/en');
+ * </code>
+ *
+ * @param string URL prefix
+ */
+ public function setPrefix($prefix)
+ {
+ $this->prefix = '/'.trim($prefix, '/');
+ }
+
+ /**
+ * Set the scriptname if mod_rewrite not available
+ *
+ * Example: will match and generate url like
+ * - index.php/view/product/1
+ * <code>
+ * $m = Net_URL_Mapper::getInstance();
+ * $m->setScriptname('index.php');
+ * </code>
+ * @param string URL prefix
+ */
+ public function setScriptname($scriptname)
+ {
+ $this->scriptname = $scriptname;
+ }
+
+ /**
+ * Will attempt to match an url with a defined path
+ *
+ * If an url corresponds to a path, the resulting values are returned
+ * in an array. If none is found, null is returned. In case an url is
+ * matched but its content doesn't validate the path rules, an exception is
+ * thrown.
+ *
+ * @param string URL
+ * @return array|null array if match found, null otherwise
+ * @throws Net_URL_Mapper_InvalidException
+ */
+ public function match($url)
+ {
+ $nurl = '/'.trim($url, '/');
+
+ // Remove scriptname if needed
+
+ if (!empty($this->scriptname) &&
+ strpos($nurl, $this->scriptname) === 0) {
+ $nurl = substr($nurl, strlen($this->scriptname));
+ if (empty($nurl)) {
+ $nurl = '/';
+ }
+ }
+
+ // Remove prefix
+
+ if (!empty($this->prefix)) {
+ if (strpos($nurl, $this->prefix) !== 0) {
+ return null;
+ }
+ $nurl = substr($nurl, strlen($this->prefix));
+ if (empty($nurl)) {
+ $nurl = '/';
+ }
+ }
+
+ // Remove query string
+
+ if (($pos = strpos($nurl, '?')) !== false) {
+ $nurl = substr($nurl, 0, $pos);
+ }
+
+ $paths = array();
+ $values = null;
+
+ // Make a list of paths that conform to route format
+
+ foreach ($this->paths as $path) {
+ $regex = $path->getFormat();
+ if (preg_match($regex, $nurl)) {
+ $paths[] = $path;
+ }
+ }
+
+ // Make sure one of the paths found is valid
+
+ foreach ($paths as $path) {
+ $regex = $path->getRule();
+ if (preg_match($regex, $nurl, $matches)) {
+ $values = $path->getDefaults();
+ array_shift($matches);
+ $clean = array();
+ foreach ($matches as $k => $v) {
+ $v = trim($v, '/');
+ if (!is_int($k) && $v !== '') {
+ $values[$k] = $v;
+ }
+ }
+ break;
+ }
+ }
+
+ // A path conforms but does not validate
+
+ if (is_null($values) && !empty($paths)) {
+ $e = new Net_URL_Mapper_InvalidException('A path was found but is invalid.');
+ $e->setPath($paths[0]);
+ $e->setUrl($url);
+ throw $e;
+ }
+
+ return $values;
+ }
+
+ /**
+ * Generate an url based on given parameters
+ *
+ * Will attempt to find a path definition that matches the given parameters and
+ * will generate an url based on this path.
+ *
+ * @param array Values to be used for the url generation
+ * @param array Key/value pairs for query string if needed
+ * @param string Anchor (fragment) if needed
+ * @return string|false String if a rule was found, false otherwise
+ */
+ public function generate($values = array(), $qstring = array(), $anchor = '')
+ {
+ // Use root path if any
+
+ if (empty($values) && isset($this->paths['/'])) {
+ return $this->scriptname.$this->prefix.$this->paths['/']->generate($values, $qstring, $anchor);
+ }
+
+ foreach ($this->paths as $path) {
+ $set = array();
+ foreach ($values as $k => $v) {
+ if ($path->hasKey($k, $v)) {
+ $set[$k] = $v;
+ }
+ }
+
+ if (count($set) == count($values) &&
+ count($set) <= $path->getMaxKeys()) {
+
+ $req = $path->getRequired();
+ if (count(array_intersect(array_keys($set), $req)) != count($req)) {
+ continue;
+ }
+ $gen = $path->generate($set, $qstring, $anchor);
+ return $this->scriptname.$this->prefix.$gen;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns defined paths
+ * @return array Array of paths
+ */
+ public function getPaths()
+ {
+ return $this->paths;
+ }
+
+ /**
+ * Reset all paths
+ * This is probably only useful for testing
+ */
+ public function reset()
+ {
+ $this->paths = array();
+ $this->prefix = '';
+ }
+
+ /**
+ * Add a new path to the mapper
+ * @param object Net_URL_Mapper_Path object
+ */
+ public function addPath(Net_URL_Mapper_Path $path)
+ {
+ $this->paths[$path->getPath()] = $path;
+ }
+
+}
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Exception.php b/extlib/Net/URL/Mapper/Exception.php
new file mode 100644
index 000000000..ac3ad172b
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Exception.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Exception classes for Net_URL_Mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Exception.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+/**
+ * Base class for exceptions in PEAR
+ */
+require_once 'PEAR/Exception.php';
+
+/**
+ * Base class for exceptions in Net_URL_Mapper package
+ *
+ * Such a base class is required by the Exception RFC:
+ * http://pear.php.net/pepr/pepr-proposal-show.php?id=132
+ * It will rarely be thrown directly, its specialized subclasses will be
+ * thrown most of the time.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @version Release: @package_version@
+ */
+class Net_URL_Mapper_Exception extends PEAR_Exception
+{
+}
+
+/**
+ * Exception thrown when a path is invalid
+ *
+ * A path can conform to a given structure, but contain invalid parameters.
+ * <code>
+ * $m = Net_URL_Mapper::getInstance();
+ * $m->connect('hi/:name', null, array('name'=>'[a-z]+'));
+ * $m->match('/hi/FOXY'); // Will throw the exception
+ * </code>
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @version Release: @package_version@
+ */
+class Net_URL_Mapper_InvalidException extends Net_URL_Mapper_Exception
+{
+ protected $path;
+ protected $url;
+
+ public function setPath($path)
+ {
+ $this->path = $path;
+ }
+
+ public function getPath()
+ {
+ return $this->path;
+ }
+
+ public function setUrl($url)
+ {
+ $this->url = $url;
+ }
+
+ public function getUrl()
+ {
+ return $this->url;
+ }
+}
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Part.php b/extlib/Net/URL/Mapper/Part.php
new file mode 100644
index 000000000..2f15b2c16
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Part.php
@@ -0,0 +1,142 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Part.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+abstract class Net_URL_Mapper_Part
+{
+ protected $defaults;
+ protected $rule;
+ protected $public;
+ protected $type;
+ protected $required = false;
+
+ /**
+ * Part name if dynamic or content, generated from path
+ * @var string
+ */
+ public $content;
+
+ const DYNAMIC = 1;
+ const WILDCARD = 2;
+ const FIXED = 3;
+
+ public function __construct($content, $path)
+ {
+ $this->content = $content;
+ $this->path = $path;
+ }
+
+ public function setRule($rule)
+ {
+ $this->rule = $rule;
+ }
+
+ abstract public function getFormat();
+
+ abstract public function getRule();
+
+ public function addSlash($str)
+ {
+ $str = trim($str, '/');
+ if (($pos = strpos($this->path, '/')) !== false) {
+ if ($pos == 0) {
+ $str = '/'.$str;
+ } else {
+ $str .= '/';
+ }
+ }
+ return $str;
+ }
+
+ public function addSlashRegex($str)
+ {
+ $str = trim($str, '/');
+ if (($pos = strpos($this->path, '/')) !== false) {
+ if ($pos == 0) {
+ $str = '\/'.$str;
+ } else {
+ $str .= '\/';
+ }
+ }
+ if (!$this->isRequired()) {
+ $str = '('.$str.'|)';
+ }
+ return $str;
+ }
+
+ public function setDefaults($defaults)
+ {
+ $this->defaults = (string)$defaults;
+ }
+
+ public function getType()
+ {
+ return $this->type;
+ }
+
+ public function accept($visitor, $method = null)
+ {
+ $args = func_get_args();
+ $visitor->$method($this, $args);
+ }
+
+ public function setRequired($required)
+ {
+ $this->required = $required;
+ }
+
+ public function isRequired()
+ {
+ return $this->required;
+ }
+
+ abstract public function generate($value = null);
+
+ public function match($value)
+ {
+ $rule = $this->getRule();
+ return preg_match('/^'.$rule.'$/', $this->addSlash($value));
+ }
+
+}
+
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Part/Dynamic.php b/extlib/Net/URL/Mapper/Part/Dynamic.php
new file mode 100644
index 000000000..349d87338
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Part/Dynamic.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Dynamic.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+require_once 'Net/URL/Mapper/Part.php';
+
+class Net_URL_Mapper_Part_Dynamic extends Net_URL_Mapper_Part
+{
+
+ public function __construct($content, $path)
+ {
+ $this->type = Net_URL_Mapper_Part::DYNAMIC;
+ $this->setRequired(true);
+ parent::__construct($content, $path);
+ }
+
+ public function getFormat()
+ {
+ return $this->addSlashRegex('[^\/]+');
+ }
+
+ public function getRule()
+ {
+ if (!empty($this->rule)) {
+ return '(?P<'.$this->content.'>'.$this->addSlashRegex($this->rule).')';
+ }
+ return '(?P<'.$this->content.'>'.$this->addSlashRegex('[^\/]+').')';
+ }
+
+ public function generate($value = null)
+ {
+ if (is_array($value) && isset($value[$this->content])) {
+ $val = $value[$this->content];
+ } elseif (!is_array($value) && !is_null($value)) {
+ $val = $value;
+ } else {
+ $val = $this->defaults;
+ }
+ return $this->addSlash(urlencode($val));
+ }
+}
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Part/Fixed.php b/extlib/Net/URL/Mapper/Part/Fixed.php
new file mode 100644
index 000000000..b315b442d
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Part/Fixed.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Fixed.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+require_once 'Net/URL/Mapper/Part.php';
+
+class Net_URL_Mapper_Part_Fixed extends Net_URL_Mapper_Part
+{
+
+ public function __construct($content, $path)
+ {
+ $this->type = Net_URL_Mapper_Part::FIXED;
+ parent::__construct($content, $path);
+ }
+
+ public function getFormat()
+ {
+ return $this->getRule();
+ }
+
+ public function getRule()
+ {
+ return preg_quote($this->path, '/');
+ }
+
+ public function generate($value = null)
+ {
+ return $this->path;
+ }
+}
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Part/Wildcard.php b/extlib/Net/URL/Mapper/Part/Wildcard.php
new file mode 100644
index 000000000..6085ff648
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Part/Wildcard.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Wildcard.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+require_once 'Net/URL/Mapper/Part.php';
+
+class Net_URL_Mapper_Part_Wildcard extends Net_URL_Mapper_Part
+{
+
+ public function __construct($content, $path)
+ {
+ $this->type = Net_URL_Mapper_Part::WILDCARD;
+ $this->setRequired(true);
+ parent::__construct($content, $path);
+ }
+
+ public function getFormat()
+ {
+ return $this->addSlashRegex('.*');;
+ }
+
+ public function getRule()
+ {
+ return '(?P<'.$this->content.'>'.$this->addSlashRegex('.*').')';
+ }
+
+ public function generate($value = null)
+ {
+ if (is_array($value) && isset($value[$this->content])) {
+ $val = $value[$this->content];
+ } elseif (!is_array($value) && !is_null($value)) {
+ $val = $value;
+ } else {
+ $val = $this->defaults;
+ }
+ return $this->addSlash(str_replace(
+ array('%2F', '%23'),
+ array('/', '#'), urlencode($val)));
+ }
+}
+?> \ No newline at end of file
diff --git a/extlib/Net/URL/Mapper/Path.php b/extlib/Net/URL/Mapper/Path.php
new file mode 100644
index 000000000..b541002c7
--- /dev/null
+++ b/extlib/Net/URL/Mapper/Path.php
@@ -0,0 +1,430 @@
+<?php
+/**
+ * URL parser and mapper
+ *
+ * PHP version 5
+ *
+ * LICENSE:
+ *
+ * Copyright (c) 2006, Bertrand Mansion <golgote@mamasam.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * The names of the authors may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Mapper
+ * @author Bertrand Mansion <golgote@mamasam.com>
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Path.php,v 1.1 2007/03/28 10:23:04 mansion Exp $
+ * @link http://pear.php.net/package/Net_URL_Mapper
+ */
+
+require_once 'Net/URL.php';
+require_once 'Net/URL/Mapper/Part/Dynamic.php';
+require_once 'Net/URL/Mapper/Part/Wildcard.php';
+require_once 'Net/URL/Mapper/Part/Fixed.php';
+
+class Net_URL_Mapper_Path
+{
+ private $path = '';
+ private $N = 0;
+ public $token;
+ public $value;
+ private $line = 1;
+ private $state = 1;
+
+
+ protected $alias;
+ protected $rules = array();
+ protected $defaults = array();
+ protected $parts = array();
+ protected $rule;
+ protected $format;
+ protected $minKeys;
+ protected $maxKeys;
+ protected $fixed = true;
+ protected $required;
+
+ public function __construct($path = '', $defaults = array(), $rules = array())
+ {
+ $this->path = '/'.trim(Net_URL::resolvePath($path), '/');
+ $this->setDefaults($defaults);
+ $this->setRules($rules);
+
+ try {
+ $this->parsePath();
+ } catch (Exception $e) {
+ // The path could not be parsed correctly, treat it as fixed
+ $this->fixed = true;
+ $part = self::createPart(Net_URL_Mapper_Part::FIXED, $this->path, $this->path);
+ $this->parts = array($part);
+ }
+ $this->getRequired();
+ }
+
+ public function getPath()
+ {
+ return $this->path;
+ }
+
+ protected function parsePath()
+ {
+ while ($this->yylex()) { }
+ }
+
+ /**
+ * Get the path alias
+ * Path aliases can be used instead of full path
+ * @return null|string
+ */
+ public function getAlias()
+ {
+ return $this->alias;
+ }
+
+ /**
+ * Set the path name
+ * @param string Set the path name
+ * @see getAlias()
+ */
+ public function setAlias($alias)
+ {
+ $this->alias = $alias;
+ return $this;
+ }
+
+ /**
+ * Get the path parts default values
+ * @return null|array
+ */
+ public function getDefaults()
+ {
+ return $this->defaults;
+ }
+
+ /**
+ * Set the path parts default values
+ * @param array Associative array with format partname => value
+ */
+ public function setDefaults($defaults)
+ {
+ if (is_array($defaults)) {
+ $this->defaults = $defaults;
+ } else {
+ $this->defaults = array();
+ }
+ }
+
+ /**
+ * Set the path parts default values
+ * @param array Associative array with format partname => value
+ */
+ public function setRules($rules)
+ {
+ if (is_array($rules)) {
+ $this->rules = $rules;
+ } else {
+ $this->rules = array();
+ }
+ }
+
+ /**
+ * Returns the regular expression used to match this path
+ * @return string PERL Regular expression
+ */
+ public function getRule()
+ {
+ if (is_null($this->rule)) {
+ $this->rule = '/^';
+ foreach ($this->parts as $path => $part) {
+ $this->rule .= $part->getRule();
+ }
+ $this->rule .= '$/';
+ }
+ return $this->rule;
+ }
+
+ public function getFormat()
+ {
+ if (is_null($this->format)) {
+ $this->format = '/^';
+ foreach ($this->parts as $path => $part) {
+ $this->format .= $part->getFormat();
+ }
+ $this->format .= '$/';
+ }
+ return $this->format;
+ }
+
+ protected function addPart($part)
+ {
+ if (array_key_exists($part->content, $this->defaults)) {
+ $part->setRequired(false);
+ $part->setDefaults($this->defaults[$part->content]);
+ }
+ if (isset($this->rules[$part->content])) {
+ $part->setRule($this->rules[$part->content]);
+ }
+ $this->rule = null;
+ if ($part->getType() != Net_URL_Mapper_Part::FIXED) {
+ $this->fixed = false;
+ $this->parts[$part->content] = $part;
+ } else {
+ $this->parts[] = $part;
+ }
+ return $part;
+ }
+
+ public static function createPart($type, $content, $path)
+ {
+ switch ($type) {
+ case Net_URL_Mapper_Part::DYNAMIC:
+ return new Net_URL_Mapper_Part_Dynamic($content, $path);
+ break;
+ case Net_URL_Mapper_Part::WILDCARD:
+ return new Net_URL_Mapper_Part_Wildcard($content, $path);
+ break;
+ default:
+ return new Net_URL_Mapper_Part_Fixed($content, $path);
+ }
+ }
+
+ /**
+ * Checks whether the path contains the given part by name
+ * If value parameter is given, the part also checks if the
+ * given value conforms to the part rule.
+ * @param string Part name
+ * @param mixed The value to check against
+ */
+ public function hasKey($partName, $value = null)
+ {
+ if (array_key_exists($partName, $this->parts)) {
+ if (!is_null($value) && $value !== false) {
+ return $this->parts[$partName]->match($value);
+ } else {
+ return true;
+ }
+ } elseif (array_key_exists($partName, $this->defaults) &&
+ $value == $this->defaults[$partName]) {
+ return true;
+ }
+ return false;
+ }
+
+ public function generate($values = array(), $qstring = array(), $anchor = '')
+ {
+ $path = '';
+ foreach ($this->parts as $part) {
+ $path .= $part->generate($values);
+ }
+ $path = '/'.trim(Net_URL::resolvePath($path), '/');
+ if (!empty($qstring)) {
+ $path .= '?'.http_build_query($qstring);
+ }
+ if (!empty($anchor)) {
+ $path .= '#'.ltrim($anchor, '#');
+ }
+ return $path;
+ }
+
+ public function getRequired()
+ {
+ if (!isset($this->required)) {
+ $req = array();
+ foreach ($this->parts as $part) {
+ if ($part->isRequired()) {
+ $req[] = $part->content;
+ }
+ }
+ $this->required = $req;
+ }
+ return $this->required;
+ }
+
+ public function getMaxKeys()
+ {
+ if (is_null($this->maxKeys)) {
+ $this->maxKeys = count($this->required);
+ $this->maxKeys += count($this->defaults);
+ }
+ return $this->maxKeys;
+ }
+
+
+
+
+ private $_yy_state = 1;
+ private $_yy_stack = array();
+
+ function yylex()
+ {
+ return $this->{'yylex' . $this->_yy_state}();
+ }
+
+ function yypushstate($state)
+ {
+ array_push($this->_yy_stack, $this->_yy_state);
+ $this->_yy_state = $state;
+ }
+
+ function yypopstate()
+ {
+ $this->_yy_state = array_pop($this->_yy_stack);
+ }
+
+ function yybegin($state)
+ {
+ $this->_yy_state = $state;
+ }
+
+
+
+ function yylex1()
+ {
+ $tokenMap = array (
+ 1 => 1,
+ 3 => 1,
+ 5 => 1,
+ 7 => 1,
+ 9 => 1,
+ );
+ if ($this->N >= strlen($this->path)) {
+ return false; // end of input
+ }
+ $yy_global_pattern = "/^(\/?:\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?\\*\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))/";
+
+ do {
+ if (preg_match($yy_global_pattern, substr($this->path, $this->N), $yymatches)) {
+ $yysubmatches = $yymatches;
+ $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
+ if (!count($yymatches)) {
+ throw new Exception('Error: lexing failed because a rule matched' .
+ 'an empty string. Input "' . substr($this->path,
+ $this->N, 5) . '... state START');
+ }
+ next($yymatches); // skip global match
+ $this->token = key($yymatches); // token number
+ if ($tokenMap[$this->token]) {
+ // extract sub-patterns for passing to lex function
+ $yysubmatches = array_slice($yysubmatches, $this->token + 1,
+ $tokenMap[$this->token]);
+ } else {
+ $yysubmatches = array();
+ }
+ $this->value = current($yymatches); // token value
+ $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
+ if ($r === null) {
+ $this->N += strlen($this->value);
+ $this->line += substr_count("\n", $this->value);
+ // accept this token
+ return true;
+ } elseif ($r === true) {
+ // we have changed state
+ // process this token in the new state
+ return $this->yylex();
+ } elseif ($r === false) {
+ $this->N += strlen($this->value);
+ $this->line += substr_count("\n", $this->value);
+ if ($this->N >= strlen($this->path)) {
+ return false; // end of input
+ }
+ // skip this token
+ continue;
+ } else { $yy_yymore_patterns = array(
+ 1 => "^(\/?\\*\/?\\(([a-zA-Z0-9_]+)\\))|^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
+ 3 => "^(\/?:([a-zA-Z0-9_]+))|^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
+ 5 => "^(\/?\\*([a-zA-Z0-9_]+))|^(\/?([^\/:*]+))",
+ 7 => "^(\/?([^\/:*]+))",
+ 9 => "",
+ );
+
+ // yymore is needed
+ do {
+ if (!strlen($yy_yymore_patterns[$this->token])) {
+ throw new Exception('cannot do yymore for the last token');
+ }
+ if (preg_match($yy_yymore_patterns[$this->token],
+ substr($this->path, $this->N), $yymatches)) {
+ $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
+ next($yymatches); // skip global match
+ $this->token = key($yymatches); // token number
+ $this->value = current($yymatches); // token value
+ $this->line = substr_count("\n", $this->value);
+ }
+ } while ($this->{'yy_r1_' . $this->token}() !== null);
+ // accept
+ $this->N += strlen($this->value);
+ $this->line += substr_count("\n", $this->value);
+ return true;
+ }
+ } else {
+ throw new Exception('Unexpected input at line' . $this->line .
+ ': ' . $this->path[$this->N]);
+ }
+ break;
+ } while (true);
+ } // end function
+
+
+ const START = 1;
+ function yy_r1_1($yy_subpatterns)
+ {
+
+ $c = $yy_subpatterns[0];
+ $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
+ $this->addPart($part);
+ }
+ function yy_r1_3($yy_subpatterns)
+ {
+
+ $c = $yy_subpatterns[0];
+ $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
+ $this->addPart($part);
+ }
+ function yy_r1_5($yy_subpatterns)
+ {
+
+ $c = $yy_subpatterns[0];
+ $part = self::createPart(Net_URL_Mapper_Part::DYNAMIC, $c, $this->value);
+ $this->addPart($part);
+ }
+ function yy_r1_7($yy_subpatterns)
+ {
+
+ $c = $yy_subpatterns[0];
+ $part = self::createPart(Net_URL_Mapper_Part::WILDCARD, $c, $this->value);
+ $this->addPart($part);
+ }
+ function yy_r1_9($yy_subpatterns)
+ {
+
+ $c = $yy_subpatterns[0];
+ $part = self::createPart(Net_URL_Mapper_Part::FIXED, $c, $this->value);
+ $this->addPart($part);
+ }
+
+}
+
+?> \ No newline at end of file