diff --git a/public_html/index.php b/public_html/index.php index ef5d883e9..0b3d3a116 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -84,8 +84,7 @@ function fixTopic(&$A, $tid_list) } // If URL routing is enabled, then let the router handle the request -if (isset($_CONF['url_routing']) && !empty($_CONF['url_routing'])) { - require_once $_CONF['path_system'] . 'classes/router.class.php'; +if ($_CONF['url_rewrite'] && isset($_CONF['url_routing']) && !empty($_CONF['url_routing'])) { Router::dispatch(); } diff --git a/public_html/lib-common.php b/public_html/lib-common.php index 7aff2c940..ce5f6613e 100755 --- a/public_html/lib-common.php +++ b/public_html/lib-common.php @@ -199,8 +199,9 @@ * This provides optional URL rewriting functionality. */ -require_once( $_CONF['path_system'] . 'classes/url.class.php' ); -$_URL = new url( $_CONF['url_rewrite'] ); +require_once $_CONF['path_system'] . 'classes/router.class.php'; +require_once $_CONF['path_system'] . 'classes/url.class.php'; +$_URL = new Url($_CONF['url_rewrite'], $_CONF['url_routing']); /** * This is our HTML template class. It is the same one found in PHPLib and is diff --git a/system/classes/router.class.php b/system/classes/router.class.php index 8c53c5888..7d29d0ab8 100644 --- a/system/classes/router.class.php +++ b/system/classes/router.class.php @@ -44,6 +44,11 @@ public static function dispatch() { global $_CONF, $_TABLES; + // URL rewrite is disabled + if (!$_CONF['url_rewrite']) { + return false; + } + // URL routing is not supported if (!isset($_CONF['url_routing'])) { return false; @@ -184,6 +189,11 @@ public static function convertUrl($url, $requestMethod = self::HTTP_REQUEST_GET) { global $_CONF, $_TABLES; + // URL rewrite is disabled + if (!$_CONF['url_rewrite']) { + return $url; + } + // URL routing is not supported if (!isset($_CONF['url_routing'])) { return $url; @@ -231,22 +241,26 @@ public static function convertUrl($url, $requestMethod = self::HTTP_REQUEST_GET) // Try simple comparison without placeholders if (strcasecmp($route, $url) === 0) { + $retval = $rule; + if ($routingType === self::ROUTING_WITH_INDEX_PHP) { - $rule = $_CONF['site_url'] . $rule; + $retval = '/index.php' . $retval; } + $retval = $_CONF['site_url'] . $retval; + if (self::$debug) { - COM_errorLog(__METHOD__ . ': matched with simple comparison rule "' . $A['route'] . '"'); + COM_errorLog(__METHOD__ . ': matched with simple comparison route "' . $A['route'] . '"'); } - return $rule; + return $retval; } // Try comparison with placeholders if (preg_match_all(self::PATTERN_PLACEHOLDER, $route, $matches, PREG_SET_ORDER)) { $placeHolders = array(); - // Replace placeholders in a rule with ones for regular expressions + // Replace placeholders in a route with ones for regular expressions foreach ($matches as $match) { $placeHolders[] = $match[1]; $route = str_ireplace($match[1], '([^/]+)', $route); @@ -269,18 +283,22 @@ public static function convertUrl($url, $requestMethod = self::HTTP_REQUEST_GET) foreach ($matches as $match) { $match = urlencode($match); $placeHolder = array_shift($placeHolders); - $url = str_ireplace($placeHolder, $match, $url); + $rule = str_ireplace($placeHolder, $match, $rule); } + $retval = $rule; + if ($routingType === self::ROUTING_WITH_INDEX_PHP) { - $url = $_CONF['site_url'] . $url; + $retval = '/index.php' . $retval; } + $retval = $_CONF['site_url'] . $retval; + if (self::$debug) { COM_errorLog(__METHOD__ . ': matched with regular expression rule "' . $A['route'] . '"'); } - return $url; + return $retval; } } diff --git a/system/classes/url.class.php b/system/classes/url.class.php index 482ce5b09..d858eb3c6 100644 --- a/system/classes/url.class.php +++ b/system/classes/url.class.php @@ -2,7 +2,7 @@ /* Reminder: always indent with 4 spaces (no tabs). */ // +---------------------------------------------------------------------------+ -// | Geeklog 1.8 | +// | Geeklog 2.1 | // +---------------------------------------------------------------------------+ // | url.class.php | // | | @@ -30,152 +30,103 @@ // +---------------------------------------------------------------------------+ /** -* This class will allow you to use friendlier URL's, like: -* http://www.example.com/index.php/arg_value_1/arg_value_2/ instead of -* uglier http://www.example.com?arg1=value1&arg2=value2. -* NOTE: this does not currently work under windows as there is a well documented -* bug with IIS and PATH_INFO. Not sure yet if this will work with windows under -* apache. This was built so you could use this class and just disable it -* if you are an IIS user. -* -* @author Tony Bibbs -* -*/ -class url { + * This class will allow you to use friendlier URL's, like: + * http://www.example.com/index.php/arg_value_1/arg_value_2/ instead of + * uglier http://www.example.com?arg1=value1&arg2=value2. + * NOTE: this does not currently work under windows as there is a well documented + * bug with IIS and PATH_INFO. Not sure yet if this will work with windows under + * apache. This was built so you could use this class and just disable it + * if you are an IIS user. + * + * @author Tony Bibbs + */ +class Url +{ /** - * @access private - */ - private $_arguments = array(); // Array of argument names + * @var array + */ + private $arguments = array(); // Array of argument names + /** - * @access private - */ - private $_enabled = true; + * @var bool + */ + private $urlRewrite = true; /** - * Constructor - * - * @param boolean $enabled whether rewriting is enabled - * - */ - function url($enabled=true) - { - $this->setEnabled($enabled); - $this->_arguments = array(); - if ($this->_enabled) { - $this->_getArguments(); - } - } + * @var int + */ + private $urlRouting; /** - * Grabs any variables from the query string - * - * @access private - */ - function _getArguments() + * Constructor + * + * @param bool $urlRewrite whether rewriting is enabled + * @param int $urlRouting URL routing mode, see Router class + */ + public function __construct($urlRewrite = true, $urlRouting = Router::ROUTING_DISABLED) { - if (isset($_SERVER['PATH_INFO'])) { - if ($_SERVER['PATH_INFO'] == '') { - if (isset($_ENV['ORIG_PATH_INFO'])) { - $this->_arguments = explode('/', $_ENV['ORIG_PATH_INFO']); - } else { - $this->_arguments = array(); - } - } else { - $this->_arguments = explode('/', $_SERVER['PATH_INFO']); - } - array_shift($this->_arguments); - } elseif (isset($_ENV['ORIG_PATH_INFO'])) { - $this->_arguments = explode('/', substr($_ENV['ORIG_PATH_INFO'], 1)); - } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { - $this->_arguments = explode('/', substr($_SERVER['ORIG_PATH_INFO'], 1)); - - // Added for IIS 7 to work in FastCGI mode - array_shift ($this->_arguments); - if ( $this->_arguments[0] == substr($_SERVER['SCRIPT_NAME'],1) ) { - array_shift($this->_arguments); - } - // end of add + $this->urlRewrite = (bool) $urlRewrite; + $urlRouting = intval($urlRouting, 10); + if (($urlRouting >= Router::ROUTING_DISABLED) && ($urlRouting <= Router::ROUTING_WITHOUT_INDEX_PHP)) { + $this->urlRouting = $urlRouting; } else { - $this->_arguments = array(); + $this->urlRouting = Router::ROUTING_DISABLED; } - } - /** - * Enables url rewriting, otherwise URL's are passed back - * - * @param boolean $switch turns URL rewriting on/off - * - */ - function setEnabled($switch) - { - if ($switch) { - $this->_enabled = true; - } else { - $this->_enabled = false; - } - } + $this->arguments = array(); - /** - * Returns whether or not URL rewriting is enabled - * - * @return boolean true if URl rewriting is enabled, otherwise false - * - */ - function isEnabled() - { - return $this->_enabled; + if ($this->urlRewrite) { + $this->getArguments(); + } } /** - * Returns the number of variables found in query string - * - * This is particularly useful just before calling setArgNames() method - * - * @return int Number of arguments found in URL - * - */ - function numArguments() + * Returns the number of variables found in query string + * This is particularly useful just before calling setArgNames() method + * + * @return int Number of arguments found in URL + */ + public function numArguments() { - return count($this->_arguments); + return count($this->arguments); } /** - * Assigns logical names to query string variables - * - * @param array $names String array of names to assign to variables pulled from query string - * @return boolean true on success otherwise false - * - */ - function setArgNames($names) + * Assigns logical names to query string variables + * + * @param array $names String array of names to assign to variables pulled from query string + * @return boolean true on success otherwise false + */ + public function setArgNames($names) { - if (count($names) < count($this->_arguments)) { + if (count($names) < count($this->arguments)) { print "URL Class: number of names passed to setArgNames must be equal or greater than number of arguments found in URL"; exit; } if (is_array($names)) { $newArray = array(); - for ($i = 1; $i <= count($this->_arguments); $i++) { - $newArray[current($names)] = current($this->_arguments); + for ($i = 1; $i <= count($this->arguments); $i++) { + $newArray[current($names)] = current($this->arguments); next($names); - next($this->_arguments); + next($this->arguments); } - $this->_arguments = $newArray; - reset($this->_arguments); + $this->arguments = $newArray; + reset($this->arguments); } else { return false; } + return true; } /** - * Gets the value for an argument - * - * @param string $name Name of argument to fetch value for - * @return mixed returns value for a given argument - * - */ - function getArgument($name) + * Gets the value for an argument + * + * @param string $name Name of argument to fetch value for + * @return mixed returns value for a given argument + */ + public function getArgument($name) { // if in GET VARS array return it if (!empty($_GET[$name])) { @@ -190,35 +141,43 @@ function getArgument($name) // end of add // ok, pull from query string - if (in_array($name,array_keys($this->_arguments))) { - return $this->_arguments[$name]; + if (in_array($name, array_keys($this->arguments))) { + return $this->arguments[$name]; } return ''; } /** - * Builds crawler friendly URL if URL rewriting is enabled - * - * This function will attempt to build a crawler friendly URL. If this feature is - * disabled because of platform issue it just returns original $url value - * - * @param string $url URL to try and convert - * @return string rewritten if _isenabled is true otherwise original url - * - */ - function buildURL($url) + * Builds crawler friendly URL if URL rewriting is enabled + * This function will attempt to build a crawler friendly URL. If this feature is + * disabled because of platform issue it just returns original $url value + * + * @param string $url URL to try and convert + * @return string rewritten if $this->urlRewrite is true otherwise original url + */ + public function buildURL($url) { - if (!$this->isEnabled()) { + if (!$this->urlRewrite) { return $url; } - $pos = strpos($url,'?'); - $query_string = substr($url,$pos+1); + if (($this->urlRouting === Router::ROUTING_WITH_INDEX_PHP) || + ($this->urlRouting === Router::ROUTING_WITHOUT_INDEX_PHP)) { + $newUrl = Router::convertUrl($url); + + if ($newUrl !== $url) { + return $newUrl; + } + } + + $pos = strpos($url, '?'); + $query_string = substr($url, $pos + 1); $finalList = array(); - $paramList = explode('&',$query_string); + $paramList = explode('&', $query_string); + for ($i = 1; $i <= count($paramList); $i++) { - $keyValuePairs = explode('=',current($paramList)); + $keyValuePairs = explode('=', current($paramList)); if (is_array($keyValuePairs)) { $argName = current($keyValuePairs); next($keyValuePairs); @@ -226,6 +185,7 @@ function buildURL($url) } next($paramList); } + $newArgs = '/'; for ($i = 1; $i <= count($finalList); $i++) { $newArgs .= current($finalList); @@ -234,8 +194,39 @@ function buildURL($url) } next($finalList); } - return str_replace('?' . $query_string,$newArgs,$url); + + return str_replace('?' . $query_string, $newArgs, $url); } -} -?> + /** + * Grabs any variables from the query string + */ + private function getArguments() + { + if (isset($_SERVER['PATH_INFO'])) { + if ($_SERVER['PATH_INFO'] == '') { + if (isset($_ENV['ORIG_PATH_INFO'])) { + $this->arguments = explode('/', $_ENV['ORIG_PATH_INFO']); + } else { + $this->arguments = array(); + } + } else { + $this->arguments = explode('/', $_SERVER['PATH_INFO']); + } + array_shift($this->arguments); + } elseif (isset($_ENV['ORIG_PATH_INFO'])) { + $this->arguments = explode('/', substr($_ENV['ORIG_PATH_INFO'], 1)); + } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { + $this->arguments = explode('/', substr($_SERVER['ORIG_PATH_INFO'], 1)); + + // Added for IIS 7 to work in FastCGI mode + array_shift($this->arguments); + if ($this->arguments[0] == substr($_SERVER['SCRIPT_NAME'], 1)) { + array_shift($this->arguments); + } + // end of add + } else { + $this->arguments = array(); + } + } +}