fix bootstrap config
[phancap.git] / src / phar-stub.php
1 <?php
2 /**
3  * Phar stub file for bdrem. Handles startup of the .phar file.
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Phancap
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/phancap.htm
13  */
14 if (!in_array('phar', stream_get_wrappers()) || !class_exists('Phar', false)) {
15     echo "Phar extension not avaiable\n";
16     exit(255);
17 }
18
19 $web = 'www/index.php';
20
21 /**
22  * Rewrite the HTTP request path to an internal file.
23  * Maps "" and "/" to "www/index.php".
24  *
25  * @param string $path Path from the browser, relative to the .phar
26  *
27  * @return string Internal path.
28  */
29 function rewritePath($path)
30 {
31     if ($path == '') {
32         //we need a / to get the relative links on index.php work
33         if (!isset($_SERVER['REQUEST_SCHEME'])) {
34             $_SERVER['REQUEST_SCHEME'] = 'http';
35         }
36         $url = $_SERVER['REQUEST_SCHEME'] . '://'
37             . $_SERVER['HTTP_HOST']
38             . preg_replace('/[?#].*$/', '', $_SERVER['REQUEST_URI'])
39             . '/';
40         header('Location: ' . $url);
41         exit(0);
42     } else if( $path == '/') {
43         return 'www/index.php';
44     }
45     return 'www' . $path;
46 }
47
48 set_include_path(
49     'phar://' . __FILE__
50     . PATH_SEPARATOR . 'phar://' . __FILE__ . '/lib/'
51 );
52 Phar::webPhar(null, $web, null, array(), 'rewritePath');
53
54 //TODO: implement CLI setup check
55 echo "phancap can only be used in the browser\n";
56 exit(1);
57 __HALT_COMPILER();
58 ?>