From 301558cb5b76b26f586fe0242928afdc8c39d882 Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@enhancesoft.com> Date: Tue, 10 Sep 2013 21:44:01 +0000 Subject: [PATCH] Revisit root path to better handle chrooted installs Also, allow for the administrator to manually define the ROOT_PATH in the config file (the very last mile). --- include/class.misc.php | 20 -------------------- include/class.osticket.php | 33 +++++++++++++++++++++++++++++++++ include/ost-sampleconfig.php | 16 +++++++++++++++- main.inc.php | 22 ++++++++++++++-------- 4 files changed, 62 insertions(+), 29 deletions(-) diff --git a/include/class.misc.php b/include/class.misc.php index d49970e9f..27e259e33 100644 --- a/include/class.misc.php +++ b/include/class.misc.php @@ -139,25 +139,5 @@ class Misc { return $output; } - /* static */ - function siteRootPath($main_inc_path) { - if (!$_SERVER['DOCUMENT_ROOT']) - // Probably run from the command-line - return './'; - $root = str_replace('\\', '/', $main_inc_path); - $root2 = str_replace('\\','/', $_SERVER['DOCUMENT_ROOT']); - $path = ''; - while (strpos($_SERVER['DOCUMENT_ROOT'], $root) === false) { - $lastslash = strrpos($root, '/'); - if ($lastslash === false) - // Unable to find any commonality between $root and - // DOCUMENT_ROOT - return './'; - $path = substr($root, $lastslash) . $path; - $root = substr($root, 0, $lastslash); - } - return $path; - } - } ?> diff --git a/include/class.osticket.php b/include/class.osticket.php index 8755d3278..aa571e9b1 100644 --- a/include/class.osticket.php +++ b/include/class.osticket.php @@ -352,6 +352,39 @@ class osTicket { return null; } + /* static */ + function get_root_path($dir) { + + if(!$_SERVER['DOCUMENT_ROOT'] + || !strcasecmp($_SERVER['DOCUMENT_ROOT'], $dir)) + return '/'; + + $dir = str_replace('\\', '/', $dir); + $root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']); + + if(strpos($dir, $root)!==false) + return substr($dir, strlen($dir)); + + $origdir = $dir; + $path = ''; + while (strpos($root, $dir) === false) { + $lastslash = strrpos($dir, '/'); + $path = substr($dir, $lastslash) . $path; + $dir = substr($dir, 0, $lastslash); + if (!$dir) + break; + } + + if($dir && $path) + return $path; + + $path = substr($_SERVER['SCRIPT_FILENAME'], strlen(ROOT_DIR)); + if($path && ($pos=strpos($_SERVER['SCRIPT_NAME'], $path))!==false) + return substr($_SERVER['SCRIPT_NAME'], 0, $pos); + + return null; + } + /** * Returns TRUE if the request was made via HTTPS and false otherwise */ diff --git a/include/ost-sampleconfig.php b/include/ost-sampleconfig.php index 65ff21c0d..a5f896769 100644 --- a/include/ost-sampleconfig.php +++ b/include/ost-sampleconfig.php @@ -16,8 +16,22 @@ $Id: $ **********************************************************************/ +/** + * If you have a strange HTTP server configuration and osTicket cannot + * discover the URL path of where your osTicket is installed, define + * ROOT_PATH here. + * + * The ROOT_PATH is the part of the URL used to access your osTicket + * helpdesk before the '/scp' part and after the hostname. For instance, for + * http://mycompany.com/support', the ROOT_PATH should be '/support/' + * + * ROOT_PATH *must* end with a forward-slash! + */ +# define('ROOT_PATH', '/support/'); + #Disable direct access. -if(!strcasecmp(basename($_SERVER['SCRIPT_NAME']),basename(__FILE__)) || !defined('ROOT_PATH')) die('kwaheri rafiki!'); +if(!strcasecmp(basename($_SERVER['SCRIPT_NAME']),basename(__FILE__)) || !defined('INCLUDE_DIR')) + die('kwaheri rafiki!'); #Install flag define('OSTINSTALLED',FALSE); diff --git a/main.inc.php b/main.inc.php index 323a0a455..ce874b26c 100644 --- a/main.inc.php +++ b/main.inc.php @@ -68,17 +68,19 @@ define('UPGRADE_DIR', INCLUDE_DIR.'upgrader/'); define('I18N_DIR', INCLUDE_DIR.'i18n/'); - require(INCLUDE_DIR.'class.misc.php'); - - // Determine the path in the URI used as the base of the osTicket - // installation - if (!defined('ROOT_PATH')) - define('ROOT_PATH', Misc::siteRootPath(realpath(dirname(__file__))).'/'); //root path. Damn directories - /*############## Do NOT monkey with anything else beyond this point UNLESS you really know what you are doing ##############*/ #Current version && schema signature (Changes from version to version) define('THIS_VERSION','1.7.0+'); //Shown on admin panel + + + require(INCLUDE_DIR.'class.osticket.php'); + + // Determine the path in the URI used as the base of the osTicket + // installation + if (!defined('ROOT_PATH') && ($rp = osTicket::get_root_path(dirname(__file__)))) + define('ROOT_PATH', rtrim($rp, '/').'/'); + #load config info $configfile=''; if(file_exists(ROOT_DIR.'ostconfig.php')) //Old installs prior to v 1.6 RC5 @@ -98,6 +100,10 @@ require($configfile); define('CONFIG_FILE',$configfile); //used in admin.php to check perm. + //Die if root path is not defined + if(!defined('ROOT_PATH') || !ROOT_PATH) + die("<b>Fatal Error:</b> unknown root path. Define it in your 'ost-config.php'"); + //Path separator if(!defined('PATH_SEPARATOR')){ if(strpos($_ENV['OS'],'Win')!==false || !strcasecmp(substr(PHP_OS, 0, 3),'WIN')) @@ -111,7 +117,7 @@ #include required files - require(INCLUDE_DIR.'class.osticket.php'); + require(INCLUDE_DIR.'class.misc.php'); require(INCLUDE_DIR.'class.ostsession.php'); require(INCLUDE_DIR.'class.usersession.php'); require(INCLUDE_DIR.'class.pagenate.php'); //Pagenate helper! -- GitLab