From: Christian Weiske Date: Fri, 4 Jul 2014 05:33:29 +0000 (+0200) Subject: detect baseurl automatically, load config file from .phar X-Git-Tag: v0.4.0~30 X-Git-Url: https://git.cweiske.de/phorkie.git/commitdiff_plain/979fd71983f72991b7b698d0fa57b46213503672 detect baseurl automatically, load config file from .phar --- diff --git a/data/config.default.php b/data/config.default.php index 8f796f4..eb98efd 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -4,7 +4,7 @@ $GLOBALS['phorkie']['cfg'] = array( 'gitdir' => __DIR__ . '/../repos/git/', 'workdir' => __DIR__ . '/../repos/work/', 'tpl' => __DIR__ . '/templates/', - 'baseurl' => '/', + 'baseurl' => null, 'avatars' => true, 'css' => '', 'iconpng' => '',//phorkie browser icon (favicon) diff --git a/src/phorkie/Tools.php b/src/phorkie/Tools.php index fc815cc..7c9c46e 100644 --- a/src/phorkie/Tools.php +++ b/src/phorkie/Tools.php @@ -70,6 +70,31 @@ class Tools return $file; } -} + public static function detectBaseUrl() + { + if (!isset($_SERVER['REQUEST_URI']) + || !isset($_SERVER['SCRIPT_NAME']) + ) { + return '/'; + } + + $scriptName = $_SERVER['SCRIPT_NAME']; + $requestUri = $_SERVER['REQUEST_URI']; + if (substr($scriptName, -4) != '.php') { + //a phar + return $scriptName . '/'; + } + + if (substr($requestUri, -4) != '.php') { + $requestUri .= '.php'; + } + $snl = strlen($scriptName); + if (substr($requestUri, -$snl) == $scriptName) { + return substr($requestUri, 0, -$snl) . '/'; + } + + return '/'; + } +} ?> diff --git a/www/www-header.php b/www/www-header.php index b58bd95..94ee1d7 100644 --- a/www/www-header.php +++ b/www/www-header.php @@ -30,8 +30,21 @@ set_exception_handler( ); require_once __DIR__ . '/../data/config.default.php'; -if (file_exists(__DIR__ . '/../data/config.php')) { - require_once __DIR__ . '/../data/config.php'; +$pharFile = \Phar::running(); +if ($pharFile == '') { + $cfgFilePath = __DIR__ . '/../data/config.php'; +} else { + //remove phar:// from the path + $cfgFilePath = substr($pharFile, 7) . '.config.php'; +} +$GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = false; +if (file_exists($cfgFilePath)) { + $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = true; + require_once $cfgFilePath; +} + +if ($GLOBALS['phorkie']['cfg']['baseurl'] === null) { + $GLOBALS['phorkie']['cfg']['baseurl'] = Tools::detectBaseUrl(); } // Set/Get git commit session variables