From caa3c95332fe159f15e16400a60090f0c43e3bc6 Mon Sep 17 00:00:00 2001 From: Dayson Pais Date: Mon, 22 Jul 2013 15:05:56 +0530 Subject: [PATCH] - Introduced get_uri function which retrieves the URL parts from the clean url. This works across all server configurations and also helps build relative URLs support - Improvised methodology to check if homepage - Introduced the $base_url variable which holds the base URL of the daux. Can be used in index.php to make URLs relative --- libs/functions.php | 84 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 7 deletions(-) diff --git a/libs/functions.php b/libs/functions.php index 0283e0f..6c88c15 100644 --- a/libs/functions.php +++ b/libs/functions.php @@ -16,11 +16,15 @@ error_reporting(E_ALL); require_once('libs/markdown_extended.php'); // Check for homepage -$path = url_path(); -if ($path === '/' || $path === '') { - $homepage = true; -} else { - $homepage = false; +$homepage = (get_uri(false) === "") ? true : false; + +// Stores the base url under which daux is running +$base_url = '/'; + +// Set the base url of where the script is located +if (isset($_SERVER['SCRIPT_NAME'])) +{ + $base_url = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'] , '/')); // find the full URL to this application from server root } // Daux.io Functions @@ -136,7 +140,7 @@ function url_path() { } function url_params() { - $url = url_path(); + $url = get_uri(); $params = explode('/', trim($url, '/')); return $params; } @@ -261,4 +265,70 @@ function get_tree($path = '.', $clean_path = '', $title = ''){ return $tree; } -?> \ No newline at end of file + +/** + * Get's the current "pretty" URI from the URL. It will also correct the QUERY_STRING server var and the $_GET array. + * It supports all forms of mod_rewrite and the following forms of URL: + * + * http://example.com/index.php/foo (returns '/foo') + * http://example.com/index.php?/foo (returns '/foo') + * http://example.com/index.php/foo?baz=bar (returns '/foo') + * http://example.com/index.php?/foo?baz=bar (returns '/foo') + * + * Similarly using mod_rewrite to remove index.php: + * http://example.com/foo (returns '/foo') + * http://example.com/foo?baz=bar (returns '/foo') + * + * @author Dan Horrigan + * @copyright Dan Horrigan + * @license MIT License + * @param bool $prefix_slash whether to return the uri with a '/' in front + * @return string the uri + */ +function get_uri($prefix_slash = true) +{ + if (isset($_SERVER['PATH_INFO'])) + { + $uri = $_SERVER['PATH_INFO']; + } + elseif (isset($_SERVER['REQUEST_URI'])) + { + $uri = $_SERVER['REQUEST_URI']; + if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) + { + $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); + } + elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) + { + $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); + } + + // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct + // URI is found, and also fixes the QUERY_STRING server var and $_GET array. + if (strncmp($uri, '?/', 2) === 0) + { + $uri = substr($uri, 2); + } + $parts = preg_split('#\?#i', $uri, 2); + $uri = $parts[0]; + if (isset($parts[1])) + { + $_SERVER['QUERY_STRING'] = $parts[1]; + parse_str($_SERVER['QUERY_STRING'], $_GET); + } + else + { + $_SERVER['QUERY_STRING'] = ''; + $_GET = array(); + } + $uri = parse_url($uri, PHP_URL_PATH); + } + else + { + // Couldn't determine the URI, so just return false + return false; + } + + // Do some final cleaning of the URI and return it + return ($prefix_slash ? '/' : '').str_replace(array('//', '../'), '/', trim($uri, '/')); +} \ No newline at end of file