From: Christian Weiske Date: Thu, 10 Apr 2014 05:49:28 +0000 (+0200) Subject: fix redirects when running as phar X-Git-Tag: v0.1.0~20 X-Git-Url: https://git.cweiske.de/phancap.git/commitdiff_plain/6ec6cc280a3312a755cfa8e8e813b1ac771f13cd?ds=sidebyside fix redirects when running as phar --- diff --git a/build.xml b/build.xml index 2bf0ce1..f1d99e0 100644 --- a/build.xml +++ b/build.xml @@ -50,6 +50,7 @@ + diff --git a/src/phancap/Config.php b/src/phancap/Config.php index 1bb4379..5034b0f 100644 --- a/src/phancap/Config.php +++ b/src/phancap/Config.php @@ -118,15 +118,23 @@ class Config . preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']); } + /** + * @return string Directory of URL without trailing slash, + * and without .phar file + */ protected function getCurrentUrlDir() { $url = $this->getCurrentUrl(); $url = preg_replace('/\?.*$/', '', $url); - if (substr($url, -1) == '/') { - return $url; + if (substr($url, -1) != '/') { + $url = substr($url, 0, -strlen(basename($url)) - 1); + } + if (\Phar::running()) { + //remove .phar file name + $url = substr($url, 0, -strlen(basename($url)) - 1); } - return substr($url, 0, -strlen(basename($url)) - 1); + return $url; } } ?> diff --git a/src/phar-stub.php b/src/phar-stub.php index 04aa111..5188983 100644 --- a/src/phar-stub.php +++ b/src/phar-stub.php @@ -28,25 +28,31 @@ $web = 'www/index.php'; */ function rewritePath($path) { - if ($path == '' || $path == '/') { + if ($path == '') { + //we need a / to get the relative links on index.php work + if (!isset($_SERVER['REQUEST_SCHEME'])) { + $_SERVER['REQUEST_SCHEME'] = 'http'; + } + $url = $_SERVER['REQUEST_SCHEME'] . '://' + . $_SERVER['HTTP_HOST'] + . preg_replace('/[?#].*$/', '', $_SERVER['REQUEST_URI']) + . '/'; + header('Location: ' . $url); + exit(0); + } else if( $path == '/') { return 'www/index.php'; - } else if ($path == '/get' || $path == '/get.php') { - return 'www/get.php'; - } else if ($path == '/setup' || $path == '/setup.php') { - return 'www/setup.php'; } - return $path; + return 'www' . $path; } -//Phar::interceptFileFuncs(); set_include_path( 'phar://' . __FILE__ . PATH_SEPARATOR . 'phar://' . __FILE__ . '/lib/' ); Phar::webPhar(null, $web, null, array(), 'rewritePath'); -//work around https://bugs.php.net/bug.php?id=52322 -//require 'phar://' . __FILE__ . '/' . $web; -echo "cli\n"; +//TODO: implement CLI setup check +echo "phancap can only be used in the browser\n"; +exit(1); __HALT_COMPILER(); ?>