-
Notifications
You must be signed in to change notification settings - Fork 7
/
CreatePhar.php
59 lines (58 loc) · 2.18 KB
/
CreatePhar.php
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
<?php
$pharFile = 'grima.phar';
$pharDir = '.';
$pharPort = 23115;
@unlink($pharFile);
ini_set("phar.readonly", 0);
$p = new Phar($pharFile);
$p->buildFromDirectory($pharDir,",(?!(?:/config(?:|/.*)|.*\.sql)),");
$p->setSignatureAlgorithm(Phar::SHA512);
$p->compressFiles(Phar::GZ);
$p->setStub('<?php
$pharFile = "' . $pharFile . '";
$pharPort = ' . $pharPort . ';
try {
if(PHP_SAPI === "cli") {
if(function_exists("pcntl_exec")) {
pcntl_exec(PHP_BINARY, array("-S","127.0.0.1:$pharPort",$argv[0]));
} else {
$cmd = PHP_BINARY . " -S 127.0.0.1:$pharPort " . escapeshellarg($argv[0]);
print("Run $cmd instead\n");
system("open http://127.0.0.1:$pharPort/index.html;exec $cmd");
}
} else {
Phar::mapPhar($pharFile);
$script = "phar://$pharFile{$_SERVER["SCRIPT_NAME"]}";
error_log($script);
if( $_SERVER["SCRIPT_NAME"] == "/" ) {
$script = "{$script}index.html";
}
if( file_exists($script) ) {
$pathinfo = pathinfo($script);
switch( $pathinfo["extension"] ) {
case "mjs":
case "js": header("Content-type: text/javascript"); break;
case "css": header("Content-type: text/css"); break;
case "html": header("Content-type: text/html"); break;
case "svg": header("Content-type: image/svg+xml"); break;
case "png": header("Content-type: image/png"); break;
case "jpeg":
case "jpg": header("Content-type: image/jpeg"); break;
case "gif": header("Content-type: image/gif"); break;
case "ttf": header("Content-type: application/font-sfnt"); break;
case "woff": header("Content-type: application/font-woff"); break;
default: error_log("Hrm, new extension: $script");
}
require( $script );
} else {
error_log("no such $script");
http_response_code(404);
}
}
} catch( Error $x ) {
error_log($x);
}
__HALT_COMPILER();');
$p = new Phar($pharFile);
$s = $p->getSignature();
echo "$pharFile successfully created\n";