Back to index

php5  5.3.10
shortarc.php
Go to the documentation of this file.
00001 <?php
00002 
00003 $web = '000';
00004 
00005 if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
00006     Phar::interceptFileFuncs();
00007     set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
00008     Phar::webPhar(null, $web);
00009     include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
00010     return;
00011 }
00012 
00013 if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
00014     Extract_Phar::go(true);
00015     $mimes = array(
00016         'phps' => 2,
00017         'c' => 'text/plain',
00018         'cc' => 'text/plain',
00019         'cpp' => 'text/plain',
00020         'c++' => 'text/plain',
00021         'dtd' => 'text/plain',
00022         'h' => 'text/plain',
00023         'log' => 'text/plain',
00024         'rng' => 'text/plain',
00025         'txt' => 'text/plain',
00026         'xsd' => 'text/plain',
00027         'php' => 1,
00028         'inc' => 1,
00029         'avi' => 'video/avi',
00030         'bmp' => 'image/bmp',
00031         'css' => 'text/css',
00032         'gif' => 'image/gif',
00033         'htm' => 'text/html',
00034         'html' => 'text/html',
00035         'htmls' => 'text/html',
00036         'ico' => 'image/x-ico',
00037         'jpe' => 'image/jpeg',
00038         'jpg' => 'image/jpeg',
00039         'jpeg' => 'image/jpeg',
00040         'js' => 'application/x-javascript',
00041         'midi' => 'audio/midi',
00042         'mid' => 'audio/midi',
00043         'mod' => 'audio/mod',
00044         'mov' => 'movie/quicktime',
00045         'mp3' => 'audio/mp3',
00046         'mpg' => 'video/mpeg',
00047         'mpeg' => 'video/mpeg',
00048         'pdf' => 'application/pdf',
00049         'png' => 'image/png',
00050         'swf' => 'application/shockwave-flash',
00051         'tif' => 'image/tiff',
00052         'tiff' => 'image/tiff',
00053         'wav' => 'audio/wav',
00054         'xbm' => 'image/xbm',
00055         'xml' => 'text/xml',
00056        );
00057 
00058     header("Cache-Control: no-cache, must-revalidate");
00059     header("Pragma: no-cache");
00060 
00061     $basename = basename(__FILE__);
00062     if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
00063         chdir(Extract_Phar::$temp);
00064         include $web;
00065         return;
00066     }
00067     $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
00068     if (!$pt || $pt == '/') {
00069         $pt = $web;
00070         header('HTTP/1.1 301 Moved Permanently');
00071         header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
00072         exit;
00073     }
00074     $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
00075     if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
00076         header('HTTP/1.0 404 Not Found');
00077         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>";
00078         exit;
00079     }
00080     $b = pathinfo($a);
00081     if (!isset($b['extension'])) {
00082         header('Content-Type: text/plain');
00083         header('Content-Length: ' . filesize($a));
00084         readfile($a);
00085         exit;
00086     }
00087     if (isset($mimes[$b['extension']])) {
00088         if ($mimes[$b['extension']] === 1) {
00089             include $a;
00090             exit;
00091         }
00092         if ($mimes[$b['extension']] === 2) {
00093             highlight_file($a);
00094             exit;
00095         }
00096         header('Content-Type: ' .$mimes[$b['extension']]);
00097         header('Content-Length: ' . filesize($a));
00098         readfile($a);
00099         exit;
00100     }
00101 }
00102 
00103 class Extract_Phar
00104 {
00105     static $temp;
00106     static $origdir;
00107     const GZ = 0x1000;
00108     const BZ2 = 0x2000;
00109     const MASK = 0x3000;
00110     const START = 'index.php';
00111     const LEN = XXXX;
00112 
00113     static function go($return  = false)
00114     {
00115         $fp = fopen(__FILE__, 'rb');
00116         fseek($fp, self::LEN);
00117         $L = unpack('V', $a = (binary)fread($fp, 4));
00118         $m = (binary)'';
00119 
00120         do {
00121             $read = 8192;
00122             if ($L[1] - strlen($m) < 8192) {
00123                 $read = $L[1] - strlen($m);
00124             }
00125             $last = (binary)fread($fp, $read);
00126             $m .= $last;
00127         } while (strlen($last) && strlen($m) < $L[1]);
00128 
00129         if (strlen($m) < $L[1]) {
00130             die('ERROR: manifest length read was "' . 
00131                 strlen($m) .'" should be "' .
00132                 $L[1] . '"');
00133         }
00134 
00135         $info = self::_unpack($m);
00136         $f = $info['c'];
00137 
00138         if ($f & self::GZ) {
00139             if (!function_exists('gzinflate')) {
00140                 die('Error: zlib extension is not enabled -' .
00141                     ' gzinflate() function needed for zlib-compressed .phars');
00142             }
00143         }
00144 
00145         if ($f & self::BZ2) {
00146             if (!function_exists('bzdecompress')) {
00147                 die('Error: bzip2 extension is not enabled -' .
00148                     ' bzdecompress() function needed for bz2-compressed .phars');
00149             }
00150         }
00151 
00152         $temp = self::tmpdir();
00153 
00154         if (!$temp || !is_writable($temp)) {
00155             $sessionpath = session_save_path();
00156             if (strpos ($sessionpath, ";") !== false)
00157                 $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
00158             if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
00159                 die('Could not locate temporary directory to extract phar');
00160             }
00161             $temp = $sessionpath;
00162         }
00163 
00164         $temp .= '/pharextract/'.basename(__FILE__, '.phar');
00165         self::$temp = $temp;
00166         self::$origdir = getcwd();
00167         @mkdir($temp, 0777, true);
00168         $temp = realpath($temp);
00169 
00170         if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
00171             self::_removeTmpFiles($temp, getcwd());
00172             @mkdir($temp, 0777, true);
00173             @file_put_contents($temp . '/' . md5_file(__FILE__), '');
00174 
00175             foreach ($info['m'] as $path => $file) {
00176                 $a = !file_exists(dirname($temp . '/' . $path));
00177                 @mkdir(dirname($temp . '/' . $path), 0777, true);
00178                 clearstatcache();
00179 
00180                 if ($path[strlen($path) - 1] == '/') {
00181                     @mkdir($temp . '/' . $path, 0777);
00182                 } else {
00183                     file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
00184                     @chmod($temp . '/' . $path, 0666);
00185                 }
00186             }
00187         }
00188 
00189         chdir($temp);
00190 
00191         if (!$return) {
00192             include self::START;
00193         }
00194     }
00195 
00196     static function tmpdir()
00197     {
00198         if (strpos(PHP_OS, 'WIN') !== false) {
00199             if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
00200                 return $var;
00201             }
00202             if (is_dir('/temp') || mkdir('/temp')) {
00203                 return realpath('/temp');
00204             }
00205             return false;
00206         }
00207         if ($var = getenv('TMPDIR')) {
00208             return $var;
00209         }
00210         return realpath('/tmp');
00211     }
00212 
00213     static function _unpack($m)
00214     {
00215         $info = unpack('V', substr($m, 0, 4));
00216         // skip API version, phar flags, alias, metadata
00217         $l = unpack('V', substr($m, 10, 4));
00218         $m = substr($m, 14 + $l[1]);
00219         $s = unpack('V', substr($m, 0, 4));
00220         $o = 0;
00221         $start = 4 + $s[1];
00222         $ret['c'] = 0;
00223 
00224         for ($i = 0; $i < $info[1]; $i++) {
00225             // length of the file name
00226             $len = unpack('V', substr($m, $start, 4));
00227             $start += 4;
00228             // file name
00229             $savepath = substr($m, $start, $len[1]);
00230             $start += $len[1];
00231             // retrieve manifest data:
00232             // 0 = size, 1 = timestamp, 2 = compressed size, 3 = crc32, 4 = flags
00233             // 5 = metadata length
00234             $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
00235             $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
00236                 & 0xffffffff);
00237             $ret['m'][$savepath][7] = $o;
00238             $o += $ret['m'][$savepath][2];
00239             $start += 24 + $ret['m'][$savepath][5];
00240             $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
00241         }
00242         return $ret;
00243     }
00244 
00245     static function extractFile($path, $entry, $fp)
00246     {
00247         $data = '';
00248         $c = $entry[2];
00249 
00250         while ($c) {
00251             if ($c < 8192) {
00252                 $data .= @fread($fp, $c);
00253                 $c = 0;
00254             } else {
00255                 $c -= 8192;
00256                 $data .= @fread($fp, 8192);
00257             }
00258         }
00259 
00260         if ($entry[4] & self::GZ) {
00261             $data = gzinflate($data);
00262         } elseif ($entry[4] & self::BZ2) {
00263             $data = bzdecompress($data);
00264         }
00265 
00266         if (strlen($data) != $entry[0]) {
00267             die("Invalid internal .phar file (size error " . strlen($data) . " != " .
00268                 $stat[7] . ")");
00269         }
00270 
00271         if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
00272             die("Invalid internal .phar file (checksum error)");
00273         }
00274 
00275         return $data;
00276     }
00277 
00278     static function _removeTmpFiles($temp, $origdir)
00279     {
00280         chdir($temp);
00281 
00282         foreach (glob('*') as $f) {
00283             if (file_exists($f)) {
00284                 is_dir($f) ? @rmdir($f) : @unlink($f);
00285                 if (file_exists($f) && is_dir($f)) {
00286                     self::_removeTmpFiles($f, getcwd());
00287                 }
00288             }
00289         }
00290 
00291         @rmdir($temp);
00292         clearstatcache();
00293         chdir($origdir);
00294     }
00295 }