2008-01-18 13:42:16 +08:00
|
|
|
<?php
|
2008-01-29 19:58:27 +08:00
|
|
|
|
2008-01-20 08:49:45 +08:00
|
|
|
$web = '000';
|
2008-01-29 19:58:27 +08:00
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
|
|
|
|
Phar::interceptFileFuncs();
|
2008-03-12 11:55:12 +08:00
|
|
|
set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
|
2008-02-19 05:45:02 +08:00
|
|
|
Phar::webPhar(null, $web);
|
2008-01-29 19:58:27 +08:00
|
|
|
include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
|
|
|
|
return;
|
2008-01-29 05:23:02 +08:00
|
|
|
}
|
2008-01-29 19:58:27 +08:00
|
|
|
|
2008-02-21 22:48:41 +08:00
|
|
|
if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
|
2008-02-19 05:45:02 +08:00
|
|
|
Extract_Phar::go(true);
|
|
|
|
$mimes = array(
|
|
|
|
'phps' => 2,
|
|
|
|
'c' => 'text/plain',
|
|
|
|
'cc' => 'text/plain',
|
|
|
|
'cpp' => 'text/plain',
|
|
|
|
'c++' => 'text/plain',
|
|
|
|
'dtd' => 'text/plain',
|
|
|
|
'h' => 'text/plain',
|
|
|
|
'log' => 'text/plain',
|
|
|
|
'rng' => 'text/plain',
|
|
|
|
'txt' => 'text/plain',
|
|
|
|
'xsd' => 'text/plain',
|
|
|
|
'php' => 1,
|
|
|
|
'inc' => 1,
|
|
|
|
'avi' => 'video/avi',
|
|
|
|
'bmp' => 'image/bmp',
|
|
|
|
'css' => 'text/css',
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
'htm' => 'text/html',
|
|
|
|
'html' => 'text/html',
|
|
|
|
'htmls' => 'text/html',
|
|
|
|
'ico' => 'image/x-ico',
|
|
|
|
'jpe' => 'image/jpeg',
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
'js' => 'application/x-javascript',
|
|
|
|
'midi' => 'audio/midi',
|
|
|
|
'mid' => 'audio/midi',
|
|
|
|
'mod' => 'audio/mod',
|
|
|
|
'mov' => 'movie/quicktime',
|
|
|
|
'mp3' => 'audio/mp3',
|
|
|
|
'mpg' => 'video/mpeg',
|
|
|
|
'mpeg' => 'video/mpeg',
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
'png' => 'image/png',
|
|
|
|
'swf' => 'application/shockwave-flash',
|
|
|
|
'tif' => 'image/tiff',
|
|
|
|
'tiff' => 'image/tiff',
|
|
|
|
'wav' => 'audio/wav',
|
|
|
|
'xbm' => 'image/xbm',
|
|
|
|
'xml' => 'text/xml',
|
|
|
|
);
|
2008-01-29 05:23:02 +08:00
|
|
|
|
2008-02-19 05:45:02 +08:00
|
|
|
header("Cache-Control: no-cache, must-revalidate");
|
|
|
|
header("Pragma: no-cache");
|
2008-01-29 05:23:02 +08:00
|
|
|
|
2008-02-19 05:45:02 +08:00
|
|
|
$basename = basename(__FILE__);
|
|
|
|
if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
|
|
|
|
chdir(Extract_Phar::$temp);
|
|
|
|
include $web;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
|
|
|
|
if (!$pt || $pt == '/') {
|
|
|
|
$pt = $web;
|
|
|
|
header('HTTP/1.1 301 Moved Permanently');
|
|
|
|
header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
|
|
|
|
if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
|
|
|
|
header('HTTP/1.0 404 Not Found');
|
|
|
|
echo "<html>\n <head>\n <title>File Not Found<title>\n </head>\n <body>\n <h1>404 - File ", $pt, " Not Found</h1>\n </body>\n</html>";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$b = pathinfo($a);
|
|
|
|
if (!isset($b['extension'])) {
|
|
|
|
header('Content-Type: text/plain');
|
|
|
|
header('Content-Length: ' . filesize($a));
|
|
|
|
readfile($a);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (isset($mimes[$b['extension']])) {
|
|
|
|
if ($mimes[$b['extension']] === 1) {
|
|
|
|
include $a;
|
2008-01-26 01:26:18 +08:00
|
|
|
exit;
|
|
|
|
}
|
2008-02-19 05:45:02 +08:00
|
|
|
if ($mimes[$b['extension']] === 2) {
|
|
|
|
highlight_file($a);
|
2008-01-20 08:49:45 +08:00
|
|
|
exit;
|
|
|
|
}
|
2008-02-19 05:45:02 +08:00
|
|
|
header('Content-Type: ' .$mimes[$b['extension']]);
|
|
|
|
header('Content-Length: ' . filesize($a));
|
|
|
|
readfile($a);
|
|
|
|
exit;
|
2008-01-20 08:49:45 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 05:23:02 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
class Extract_Phar
|
|
|
|
{
|
2008-01-20 08:49:45 +08:00
|
|
|
static $temp;
|
2008-01-18 13:42:16 +08:00
|
|
|
static $origdir;
|
|
|
|
const GZ = 0x1000;
|
|
|
|
const BZ2 = 0x2000;
|
|
|
|
const MASK = 0x3000;
|
2008-01-19 12:26:22 +08:00
|
|
|
const START = 'index.php';
|
2008-01-20 08:49:45 +08:00
|
|
|
const LEN = XXXX;
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-20 08:49:45 +08:00
|
|
|
static function go($return = false)
|
2008-01-18 13:42:16 +08:00
|
|
|
{
|
|
|
|
$fp = fopen(__FILE__, 'rb');
|
2008-01-20 08:49:45 +08:00
|
|
|
fseek($fp, self::LEN);
|
2008-05-20 03:05:42 +08:00
|
|
|
$L = unpack('V', $a = (binary)fread($fp, 4));
|
|
|
|
$m = (binary)'';
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
do {
|
|
|
|
$read = 8192;
|
|
|
|
if ($L[1] - strlen($m) < 8192) {
|
|
|
|
$read = $L[1] - strlen($m);
|
|
|
|
}
|
2008-05-20 03:05:42 +08:00
|
|
|
$last = (binary)fread($fp, $read);
|
2008-01-18 13:42:16 +08:00
|
|
|
$m .= $last;
|
|
|
|
} while (strlen($last) && strlen($m) < $L[1]);
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
if (strlen($m) < $L[1]) {
|
|
|
|
die('ERROR: manifest length read was "' .
|
|
|
|
strlen($m) .'" should be "' .
|
|
|
|
$L[1] . '"');
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
$info = self::_unpack($m);
|
|
|
|
$f = $info['c'];
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
if ($f & self::GZ) {
|
|
|
|
if (!function_exists('gzinflate')) {
|
2008-01-26 01:26:18 +08:00
|
|
|
die('Error: zlib extension is not enabled -' .
|
|
|
|
' gzinflate() function needed for zlib-compressed .phars');
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
if ($f & self::BZ2) {
|
|
|
|
if (!function_exists('bzdecompress')) {
|
2008-01-26 01:26:18 +08:00
|
|
|
die('Error: bzip2 extension is not enabled -' .
|
|
|
|
' bzdecompress() function needed for bz2-compressed .phars');
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
$temp = self::tmpdir();
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-26 08:12:00 +08:00
|
|
|
if (!$temp || !is_writable($temp)) {
|
2008-01-18 13:42:16 +08:00
|
|
|
$sessionpath = session_save_path();
|
2008-01-26 08:12:00 +08:00
|
|
|
if (strpos ($sessionpath, ";") !== false)
|
2008-01-18 13:42:16 +08:00
|
|
|
$sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
|
2008-01-26 01:26:18 +08:00
|
|
|
if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
|
2008-01-18 13:42:16 +08:00
|
|
|
die('Could not locate temporary directory to extract phar');
|
|
|
|
}
|
|
|
|
$temp = $sessionpath;
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
$temp .= '/pharextract/'.basename(__FILE__, '.phar');
|
2008-01-20 08:49:45 +08:00
|
|
|
self::$temp = $temp;
|
2008-01-29 05:23:02 +08:00
|
|
|
self::$origdir = getcwd();
|
|
|
|
@mkdir($temp, 0777, true);
|
2008-01-20 08:49:45 +08:00
|
|
|
$temp = realpath($temp);
|
2008-01-29 17:45:11 +08:00
|
|
|
|
|
|
|
if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
|
2008-01-29 15:10:22 +08:00
|
|
|
self::_removeTmpFiles($temp, getcwd());
|
2008-01-29 17:45:11 +08:00
|
|
|
@mkdir($temp, 0777, true);
|
|
|
|
@file_put_contents($temp . '/' . md5_file(__FILE__), '');
|
|
|
|
|
|
|
|
foreach ($info['m'] as $path => $file) {
|
|
|
|
$a = !file_exists(dirname($temp . '/' . $path));
|
|
|
|
@mkdir(dirname($temp . '/' . $path), 0777, true);
|
|
|
|
clearstatcache();
|
|
|
|
|
|
|
|
if ($path[strlen($path) - 1] == '/') {
|
|
|
|
@mkdir($temp . '/' . $path, 0777);
|
|
|
|
} else {
|
|
|
|
file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
|
|
|
|
@chmod($temp . '/' . $path, 0666);
|
|
|
|
}
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
chdir($temp);
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
if (!$return) {
|
|
|
|
include self::START;
|
|
|
|
}
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static function tmpdir()
|
|
|
|
{
|
|
|
|
if (strpos(PHP_OS, 'WIN') !== false) {
|
2008-01-26 01:26:18 +08:00
|
|
|
if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
|
2008-01-18 13:42:16 +08:00
|
|
|
return $var;
|
|
|
|
}
|
2008-01-26 01:26:18 +08:00
|
|
|
if (is_dir('/temp') || mkdir('/temp')) {
|
|
|
|
return realpath('/temp');
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
2008-01-26 01:26:18 +08:00
|
|
|
return false;
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
2008-01-26 01:26:18 +08:00
|
|
|
if ($var = getenv('TMPDIR')) {
|
2008-01-18 13:42:16 +08:00
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
return realpath('/tmp');
|
|
|
|
}
|
|
|
|
|
|
|
|
static function _unpack($m)
|
|
|
|
{
|
|
|
|
$info = unpack('V', substr($m, 0, 4));
|
|
|
|
// skip API version, phar flags, alias, metadata
|
|
|
|
$l = unpack('V', substr($m, 10, 4));
|
|
|
|
$m = substr($m, 14 + $l[1]);
|
|
|
|
$s = unpack('V', substr($m, 0, 4));
|
|
|
|
$o = 0;
|
|
|
|
$start = 4 + $s[1];
|
|
|
|
$ret['c'] = 0;
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
for ($i = 0; $i < $info[1]; $i++) {
|
|
|
|
// length of the file name
|
|
|
|
$len = unpack('V', substr($m, $start, 4));
|
|
|
|
$start += 4;
|
|
|
|
// file name
|
|
|
|
$savepath = substr($m, $start, $len[1]);
|
|
|
|
$start += $len[1];
|
|
|
|
// retrieve manifest data:
|
|
|
|
// 0 = size, 1 = timestamp, 2 = compressed size, 3 = crc32, 4 = flags
|
|
|
|
// 5 = metadata length
|
|
|
|
$ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
|
|
|
|
$ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
|
|
|
|
& 0xffffffff);
|
|
|
|
$ret['m'][$savepath][7] = $o;
|
|
|
|
$o += $ret['m'][$savepath][2];
|
|
|
|
$start += 24 + $ret['m'][$savepath][5];
|
|
|
|
$ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function extractFile($path, $entry, $fp)
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
$c = $entry[2];
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
while ($c) {
|
|
|
|
if ($c < 8192) {
|
|
|
|
$data .= @fread($fp, $c);
|
|
|
|
$c = 0;
|
|
|
|
} else {
|
|
|
|
$c -= 8192;
|
|
|
|
$data .= @fread($fp, 8192);
|
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
if ($entry[4] & self::GZ) {
|
|
|
|
$data = gzinflate($data);
|
|
|
|
} elseif ($entry[4] & self::BZ2) {
|
|
|
|
$data = bzdecompress($data);
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
if (strlen($data) != $entry[0]) {
|
2008-01-26 01:26:18 +08:00
|
|
|
die("Invalid internal .phar file (size error " . strlen($data) . " != " .
|
2008-01-18 13:42:16 +08:00
|
|
|
$stat[7] . ")");
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-05-15 05:27:31 +08:00
|
|
|
if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
|
2008-01-26 01:26:18 +08:00
|
|
|
die("Invalid internal .phar file (checksum error)");
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-18 13:42:16 +08:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
static function _removeTmpFiles($temp, $origdir)
|
2008-01-18 13:42:16 +08:00
|
|
|
{
|
2008-01-29 05:23:02 +08:00
|
|
|
chdir($temp);
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
foreach (glob('*') as $f) {
|
|
|
|
if (file_exists($f)) {
|
|
|
|
is_dir($f) ? @rmdir($f) : @unlink($f);
|
|
|
|
if (file_exists($f) && is_dir($f)) {
|
|
|
|
self::_removeTmpFiles($f, getcwd());
|
|
|
|
}
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 17:45:11 +08:00
|
|
|
|
2008-01-29 05:23:02 +08:00
|
|
|
@rmdir($temp);
|
|
|
|
clearstatcache();
|
|
|
|
chdir($origdir);
|
2008-01-18 13:42:16 +08:00
|
|
|
}
|
2008-01-26 01:26:18 +08:00
|
|
|
}
|