aboutsummaryrefslogtreecommitdiff
path: root/src/stub-phar.php
blob: 859152bf8c6d6e3993ec3ef00ab64efb83bc0f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
 * Phar stub file for phorkie. Handles startup of the .phar file.
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Phorkie
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      https://cweiske.de/phorkie.htm
 */
if (!in_array('phar', stream_get_wrappers()) || !class_exists('Phar', false)) {
    echo "Phar extension not avaiable\n";
    exit(255);
}

$web = 'www/index.php';
//FIXME
$cli = 'scripts/index.php';

/**
 * Rewrite the HTTP request path to an internal file.
 * Maps "" and "/" to "www/index.php".
 *
 * @param string $path Path from the browser, relative to the .phar
 *
 * @return string Internal path.
 */
function rewritePath($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';
    }

    $path = rewriteWithHtaccess($path);

    if (substr($path, -4) == '.css'
        || substr($path, -3) == '.js'
        || substr($path, 0, 9) == '/phorkie/'
    ) {
        header('Expires: ' . date('r', time() + 86400 * 7));
    }
    return 'www' . $path;
}

function rewriteWithHtaccess($path)
{
    //remove the leading slash /
    $cpath = substr($path, 1);
    $bFoundMatch = false;
    $map = include('phar://' . __FILE__ . '/src/gen-rewritemap.php');
    foreach ($map as $pattern => $replace) {
        if (preg_match($pattern, $cpath, $matches)) {
            $bFoundMatch = true;
            break;
        }
    }
    if (!$bFoundMatch) {
        return $path;
    }
    $newcpath = preg_replace($pattern, $replace, $cpath);
    if (strpos($newcpath, '?') === false) {
        return '/' . $newcpath;
    }
    list($cfile, $getParams) = explode('?', $newcpath, 2);
    if ($getParams != '') {
        parse_str($getParams, $_GET);
    }
    return '/' . $cfile;
}

//Phar::interceptFileFuncs();
set_include_path(
    'phar://' . __FILE__
    . PATH_SEPARATOR . 'phar://' . __FILE__ . '/lib/'
);
Phar::webPhar(null, $web, null, array(), 'rewritePath');

//TODO: implement CLI script runner
echo "phorkie can only be used in the browser\n";
exit(1);
__HALT_COMPILER();
?>