Annotation of embedaddon/php/ext/phar/tests/phar_setdefaultstub.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Phar: Phar::setDefaultStub() with and without arg
3: --SKIPIF--
4: <?php if (!extension_loaded("phar")) die("skip"); ?>
5: --INI--
6: phar.readonly=0
7: --FILE--
8: <?php
9:
10: $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar';
11:
12: $phar = new Phar($fname);
13: $phar['a.php'] = '<php echo "this is a\n"; ?>';
14: $phar['b.php'] = '<php echo "this is b\n"; ?>';
15: $phar->setDefaultStub();
16: $phar->stopBuffering();
17:
18: var_dump($phar->getStub());
19:
20: echo "============================================================================\n";
21: echo "============================================================================\n";
22:
23: $phar->setDefaultStub('my/custom/thingy.php');
24: $phar->stopBuffering();
25: var_dump($phar->getStub());
26:
27: echo "============================================================================\n";
28: echo "============================================================================\n";
29:
30: $phar->setDefaultStub('my/custom/thingy.php', 'the/web.php');
31: $phar->stopBuffering();
32: var_dump($phar->getStub());
33:
34: echo "============================================================================\n";
35: echo "============================================================================\n";
36:
37: try {
38: $phar->setDefaultStub(str_repeat('a', 400));
39: $phar->stopBuffering();
40: var_dump(strlen($phar->getStub()));
41:
42: $phar->setDefaultStub(str_repeat('a', 401));
43: $phar->stopBuffering();
44: var_dump(strlen($phar->getStub()));
45:
46: } catch(Exception $e) {
47: echo $e->getMessage() . "\n";
48: }
49:
50: ?>
51: ===DONE===
52: --CLEAN--
53: <?php
54: unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar');
55: ?>
56: --EXPECT--
57: string(6685) "<?php
58:
59: $web = 'index.php';
60:
61: if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
62: Phar::interceptFileFuncs();
63: set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
64: Phar::webPhar(null, $web);
65: include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
66: return;
67: }
68:
69: if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
70: Extract_Phar::go(true);
71: $mimes = array(
72: 'phps' => 2,
73: 'c' => 'text/plain',
74: 'cc' => 'text/plain',
75: 'cpp' => 'text/plain',
76: 'c++' => 'text/plain',
77: 'dtd' => 'text/plain',
78: 'h' => 'text/plain',
79: 'log' => 'text/plain',
80: 'rng' => 'text/plain',
81: 'txt' => 'text/plain',
82: 'xsd' => 'text/plain',
83: 'php' => 1,
84: 'inc' => 1,
85: 'avi' => 'video/avi',
86: 'bmp' => 'image/bmp',
87: 'css' => 'text/css',
88: 'gif' => 'image/gif',
89: 'htm' => 'text/html',
90: 'html' => 'text/html',
91: 'htmls' => 'text/html',
92: 'ico' => 'image/x-ico',
93: 'jpe' => 'image/jpeg',
94: 'jpg' => 'image/jpeg',
95: 'jpeg' => 'image/jpeg',
96: 'js' => 'application/x-javascript',
97: 'midi' => 'audio/midi',
98: 'mid' => 'audio/midi',
99: 'mod' => 'audio/mod',
100: 'mov' => 'movie/quicktime',
101: 'mp3' => 'audio/mp3',
102: 'mpg' => 'video/mpeg',
103: 'mpeg' => 'video/mpeg',
104: 'pdf' => 'application/pdf',
105: 'png' => 'image/png',
106: 'swf' => 'application/shockwave-flash',
107: 'tif' => 'image/tiff',
108: 'tiff' => 'image/tiff',
109: 'wav' => 'audio/wav',
110: 'xbm' => 'image/xbm',
111: 'xml' => 'text/xml',
112: );
113:
114: header("Cache-Control: no-cache, must-revalidate");
115: header("Pragma: no-cache");
116:
117: $basename = basename(__FILE__);
118: if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
119: chdir(Extract_Phar::$temp);
120: include $web;
121: return;
122: }
123: $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
124: if (!$pt || $pt == '/') {
125: $pt = $web;
126: header('HTTP/1.1 301 Moved Permanently');
127: header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
128: exit;
129: }
130: $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
131: if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
132: header('HTTP/1.0 404 Not Found');
133: 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>";
134: exit;
135: }
136: $b = pathinfo($a);
137: if (!isset($b['extension'])) {
138: header('Content-Type: text/plain');
139: header('Content-Length: ' . filesize($a));
140: readfile($a);
141: exit;
142: }
143: if (isset($mimes[$b['extension']])) {
144: if ($mimes[$b['extension']] === 1) {
145: include $a;
146: exit;
147: }
148: if ($mimes[$b['extension']] === 2) {
149: highlight_file($a);
150: exit;
151: }
152: header('Content-Type: ' .$mimes[$b['extension']]);
153: header('Content-Length: ' . filesize($a));
154: readfile($a);
155: exit;
156: }
157: }
158:
159: class Extract_Phar
160: {
161: static $temp;
162: static $origdir;
163: const GZ = 0x1000;
164: const BZ2 = 0x2000;
165: const MASK = 0x3000;
166: const START = 'index.php';
167: const LEN = 6685;
168:
169: static function go($return = false)
170: {
171: $fp = fopen(__FILE__, 'rb');
172: fseek($fp, self::LEN);
173: $L = unpack('V', $a = (binary)fread($fp, 4));
174: $m = (binary)'';
175:
176: do {
177: $read = 8192;
178: if ($L[1] - strlen($m) < 8192) {
179: $read = $L[1] - strlen($m);
180: }
181: $last = (binary)fread($fp, $read);
182: $m .= $last;
183: } while (strlen($last) && strlen($m) < $L[1]);
184:
185: if (strlen($m) < $L[1]) {
186: die('ERROR: manifest length read was "' .
187: strlen($m) .'" should be "' .
188: $L[1] . '"');
189: }
190:
191: $info = self::_unpack($m);
192: $f = $info['c'];
193:
194: if ($f & self::GZ) {
195: if (!function_exists('gzinflate')) {
196: die('Error: zlib extension is not enabled -' .
197: ' gzinflate() function needed for zlib-compressed .phars');
198: }
199: }
200:
201: if ($f & self::BZ2) {
202: if (!function_exists('bzdecompress')) {
203: die('Error: bzip2 extension is not enabled -' .
204: ' bzdecompress() function needed for bz2-compressed .phars');
205: }
206: }
207:
208: $temp = self::tmpdir();
209:
210: if (!$temp || !is_writable($temp)) {
211: $sessionpath = session_save_path();
212: if (strpos ($sessionpath, ";") !== false)
213: $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
214: if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
215: die('Could not locate temporary directory to extract phar');
216: }
217: $temp = $sessionpath;
218: }
219:
220: $temp .= '/pharextract/'.basename(__FILE__, '.phar');
221: self::$temp = $temp;
222: self::$origdir = getcwd();
223: @mkdir($temp, 0777, true);
224: $temp = realpath($temp);
225:
226: if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
227: self::_removeTmpFiles($temp, getcwd());
228: @mkdir($temp, 0777, true);
229: @file_put_contents($temp . '/' . md5_file(__FILE__), '');
230:
231: foreach ($info['m'] as $path => $file) {
232: $a = !file_exists(dirname($temp . '/' . $path));
233: @mkdir(dirname($temp . '/' . $path), 0777, true);
234: clearstatcache();
235:
236: if ($path[strlen($path) - 1] == '/') {
237: @mkdir($temp . '/' . $path, 0777);
238: } else {
239: file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
240: @chmod($temp . '/' . $path, 0666);
241: }
242: }
243: }
244:
245: chdir($temp);
246:
247: if (!$return) {
248: include self::START;
249: }
250: }
251:
252: static function tmpdir()
253: {
254: if (strpos(PHP_OS, 'WIN') !== false) {
255: if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
256: return $var;
257: }
258: if (is_dir('/temp') || mkdir('/temp')) {
259: return realpath('/temp');
260: }
261: return false;
262: }
263: if ($var = getenv('TMPDIR')) {
264: return $var;
265: }
266: return realpath('/tmp');
267: }
268:
269: static function _unpack($m)
270: {
271: $info = unpack('V', substr($m, 0, 4));
272: $l = unpack('V', substr($m, 10, 4));
273: $m = substr($m, 14 + $l[1]);
274: $s = unpack('V', substr($m, 0, 4));
275: $o = 0;
276: $start = 4 + $s[1];
277: $ret['c'] = 0;
278:
279: for ($i = 0; $i < $info[1]; $i++) {
280: $len = unpack('V', substr($m, $start, 4));
281: $start += 4;
282: $savepath = substr($m, $start, $len[1]);
283: $start += $len[1];
284: $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
285: $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
286: & 0xffffffff);
287: $ret['m'][$savepath][7] = $o;
288: $o += $ret['m'][$savepath][2];
289: $start += 24 + $ret['m'][$savepath][5];
290: $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
291: }
292: return $ret;
293: }
294:
295: static function extractFile($path, $entry, $fp)
296: {
297: $data = '';
298: $c = $entry[2];
299:
300: while ($c) {
301: if ($c < 8192) {
302: $data .= @fread($fp, $c);
303: $c = 0;
304: } else {
305: $c -= 8192;
306: $data .= @fread($fp, 8192);
307: }
308: }
309:
310: if ($entry[4] & self::GZ) {
311: $data = gzinflate($data);
312: } elseif ($entry[4] & self::BZ2) {
313: $data = bzdecompress($data);
314: }
315:
316: if (strlen($data) != $entry[0]) {
317: die("Invalid internal .phar file (size error " . strlen($data) . " != " .
318: $stat[7] . ")");
319: }
320:
321: if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
322: die("Invalid internal .phar file (checksum error)");
323: }
324:
325: return $data;
326: }
327:
328: static function _removeTmpFiles($temp, $origdir)
329: {
330: chdir($temp);
331:
332: foreach (glob('*') as $f) {
333: if (file_exists($f)) {
334: is_dir($f) ? @rmdir($f) : @unlink($f);
335: if (file_exists($f) && is_dir($f)) {
336: self::_removeTmpFiles($f, getcwd());
337: }
338: }
339: }
340:
341: @rmdir($temp);
342: clearstatcache();
343: chdir($origdir);
344: }
345: }
346:
347: Extract_Phar::go();
348: __HALT_COMPILER(); ?>
349: "
350: ============================================================================
351: ============================================================================
352: string(6696) "<?php
353:
354: $web = 'index.php';
355:
356: if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
357: Phar::interceptFileFuncs();
358: set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
359: Phar::webPhar(null, $web);
360: include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
361: return;
362: }
363:
364: if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
365: Extract_Phar::go(true);
366: $mimes = array(
367: 'phps' => 2,
368: 'c' => 'text/plain',
369: 'cc' => 'text/plain',
370: 'cpp' => 'text/plain',
371: 'c++' => 'text/plain',
372: 'dtd' => 'text/plain',
373: 'h' => 'text/plain',
374: 'log' => 'text/plain',
375: 'rng' => 'text/plain',
376: 'txt' => 'text/plain',
377: 'xsd' => 'text/plain',
378: 'php' => 1,
379: 'inc' => 1,
380: 'avi' => 'video/avi',
381: 'bmp' => 'image/bmp',
382: 'css' => 'text/css',
383: 'gif' => 'image/gif',
384: 'htm' => 'text/html',
385: 'html' => 'text/html',
386: 'htmls' => 'text/html',
387: 'ico' => 'image/x-ico',
388: 'jpe' => 'image/jpeg',
389: 'jpg' => 'image/jpeg',
390: 'jpeg' => 'image/jpeg',
391: 'js' => 'application/x-javascript',
392: 'midi' => 'audio/midi',
393: 'mid' => 'audio/midi',
394: 'mod' => 'audio/mod',
395: 'mov' => 'movie/quicktime',
396: 'mp3' => 'audio/mp3',
397: 'mpg' => 'video/mpeg',
398: 'mpeg' => 'video/mpeg',
399: 'pdf' => 'application/pdf',
400: 'png' => 'image/png',
401: 'swf' => 'application/shockwave-flash',
402: 'tif' => 'image/tiff',
403: 'tiff' => 'image/tiff',
404: 'wav' => 'audio/wav',
405: 'xbm' => 'image/xbm',
406: 'xml' => 'text/xml',
407: );
408:
409: header("Cache-Control: no-cache, must-revalidate");
410: header("Pragma: no-cache");
411:
412: $basename = basename(__FILE__);
413: if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
414: chdir(Extract_Phar::$temp);
415: include $web;
416: return;
417: }
418: $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
419: if (!$pt || $pt == '/') {
420: $pt = $web;
421: header('HTTP/1.1 301 Moved Permanently');
422: header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
423: exit;
424: }
425: $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
426: if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
427: header('HTTP/1.0 404 Not Found');
428: 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>";
429: exit;
430: }
431: $b = pathinfo($a);
432: if (!isset($b['extension'])) {
433: header('Content-Type: text/plain');
434: header('Content-Length: ' . filesize($a));
435: readfile($a);
436: exit;
437: }
438: if (isset($mimes[$b['extension']])) {
439: if ($mimes[$b['extension']] === 1) {
440: include $a;
441: exit;
442: }
443: if ($mimes[$b['extension']] === 2) {
444: highlight_file($a);
445: exit;
446: }
447: header('Content-Type: ' .$mimes[$b['extension']]);
448: header('Content-Length: ' . filesize($a));
449: readfile($a);
450: exit;
451: }
452: }
453:
454: class Extract_Phar
455: {
456: static $temp;
457: static $origdir;
458: const GZ = 0x1000;
459: const BZ2 = 0x2000;
460: const MASK = 0x3000;
461: const START = 'my/custom/thingy.php';
462: const LEN = 6696;
463:
464: static function go($return = false)
465: {
466: $fp = fopen(__FILE__, 'rb');
467: fseek($fp, self::LEN);
468: $L = unpack('V', $a = (binary)fread($fp, 4));
469: $m = (binary)'';
470:
471: do {
472: $read = 8192;
473: if ($L[1] - strlen($m) < 8192) {
474: $read = $L[1] - strlen($m);
475: }
476: $last = (binary)fread($fp, $read);
477: $m .= $last;
478: } while (strlen($last) && strlen($m) < $L[1]);
479:
480: if (strlen($m) < $L[1]) {
481: die('ERROR: manifest length read was "' .
482: strlen($m) .'" should be "' .
483: $L[1] . '"');
484: }
485:
486: $info = self::_unpack($m);
487: $f = $info['c'];
488:
489: if ($f & self::GZ) {
490: if (!function_exists('gzinflate')) {
491: die('Error: zlib extension is not enabled -' .
492: ' gzinflate() function needed for zlib-compressed .phars');
493: }
494: }
495:
496: if ($f & self::BZ2) {
497: if (!function_exists('bzdecompress')) {
498: die('Error: bzip2 extension is not enabled -' .
499: ' bzdecompress() function needed for bz2-compressed .phars');
500: }
501: }
502:
503: $temp = self::tmpdir();
504:
505: if (!$temp || !is_writable($temp)) {
506: $sessionpath = session_save_path();
507: if (strpos ($sessionpath, ";") !== false)
508: $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
509: if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
510: die('Could not locate temporary directory to extract phar');
511: }
512: $temp = $sessionpath;
513: }
514:
515: $temp .= '/pharextract/'.basename(__FILE__, '.phar');
516: self::$temp = $temp;
517: self::$origdir = getcwd();
518: @mkdir($temp, 0777, true);
519: $temp = realpath($temp);
520:
521: if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
522: self::_removeTmpFiles($temp, getcwd());
523: @mkdir($temp, 0777, true);
524: @file_put_contents($temp . '/' . md5_file(__FILE__), '');
525:
526: foreach ($info['m'] as $path => $file) {
527: $a = !file_exists(dirname($temp . '/' . $path));
528: @mkdir(dirname($temp . '/' . $path), 0777, true);
529: clearstatcache();
530:
531: if ($path[strlen($path) - 1] == '/') {
532: @mkdir($temp . '/' . $path, 0777);
533: } else {
534: file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
535: @chmod($temp . '/' . $path, 0666);
536: }
537: }
538: }
539:
540: chdir($temp);
541:
542: if (!$return) {
543: include self::START;
544: }
545: }
546:
547: static function tmpdir()
548: {
549: if (strpos(PHP_OS, 'WIN') !== false) {
550: if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
551: return $var;
552: }
553: if (is_dir('/temp') || mkdir('/temp')) {
554: return realpath('/temp');
555: }
556: return false;
557: }
558: if ($var = getenv('TMPDIR')) {
559: return $var;
560: }
561: return realpath('/tmp');
562: }
563:
564: static function _unpack($m)
565: {
566: $info = unpack('V', substr($m, 0, 4));
567: $l = unpack('V', substr($m, 10, 4));
568: $m = substr($m, 14 + $l[1]);
569: $s = unpack('V', substr($m, 0, 4));
570: $o = 0;
571: $start = 4 + $s[1];
572: $ret['c'] = 0;
573:
574: for ($i = 0; $i < $info[1]; $i++) {
575: $len = unpack('V', substr($m, $start, 4));
576: $start += 4;
577: $savepath = substr($m, $start, $len[1]);
578: $start += $len[1];
579: $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
580: $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
581: & 0xffffffff);
582: $ret['m'][$savepath][7] = $o;
583: $o += $ret['m'][$savepath][2];
584: $start += 24 + $ret['m'][$savepath][5];
585: $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
586: }
587: return $ret;
588: }
589:
590: static function extractFile($path, $entry, $fp)
591: {
592: $data = '';
593: $c = $entry[2];
594:
595: while ($c) {
596: if ($c < 8192) {
597: $data .= @fread($fp, $c);
598: $c = 0;
599: } else {
600: $c -= 8192;
601: $data .= @fread($fp, 8192);
602: }
603: }
604:
605: if ($entry[4] & self::GZ) {
606: $data = gzinflate($data);
607: } elseif ($entry[4] & self::BZ2) {
608: $data = bzdecompress($data);
609: }
610:
611: if (strlen($data) != $entry[0]) {
612: die("Invalid internal .phar file (size error " . strlen($data) . " != " .
613: $stat[7] . ")");
614: }
615:
616: if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
617: die("Invalid internal .phar file (checksum error)");
618: }
619:
620: return $data;
621: }
622:
623: static function _removeTmpFiles($temp, $origdir)
624: {
625: chdir($temp);
626:
627: foreach (glob('*') as $f) {
628: if (file_exists($f)) {
629: is_dir($f) ? @rmdir($f) : @unlink($f);
630: if (file_exists($f) && is_dir($f)) {
631: self::_removeTmpFiles($f, getcwd());
632: }
633: }
634: }
635:
636: @rmdir($temp);
637: clearstatcache();
638: chdir($origdir);
639: }
640: }
641:
642: Extract_Phar::go();
643: __HALT_COMPILER(); ?>
644: "
645: ============================================================================
646: ============================================================================
647: string(6698) "<?php
648:
649: $web = 'the/web.php';
650:
651: if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
652: Phar::interceptFileFuncs();
653: set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
654: Phar::webPhar(null, $web);
655: include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
656: return;
657: }
658:
659: if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
660: Extract_Phar::go(true);
661: $mimes = array(
662: 'phps' => 2,
663: 'c' => 'text/plain',
664: 'cc' => 'text/plain',
665: 'cpp' => 'text/plain',
666: 'c++' => 'text/plain',
667: 'dtd' => 'text/plain',
668: 'h' => 'text/plain',
669: 'log' => 'text/plain',
670: 'rng' => 'text/plain',
671: 'txt' => 'text/plain',
672: 'xsd' => 'text/plain',
673: 'php' => 1,
674: 'inc' => 1,
675: 'avi' => 'video/avi',
676: 'bmp' => 'image/bmp',
677: 'css' => 'text/css',
678: 'gif' => 'image/gif',
679: 'htm' => 'text/html',
680: 'html' => 'text/html',
681: 'htmls' => 'text/html',
682: 'ico' => 'image/x-ico',
683: 'jpe' => 'image/jpeg',
684: 'jpg' => 'image/jpeg',
685: 'jpeg' => 'image/jpeg',
686: 'js' => 'application/x-javascript',
687: 'midi' => 'audio/midi',
688: 'mid' => 'audio/midi',
689: 'mod' => 'audio/mod',
690: 'mov' => 'movie/quicktime',
691: 'mp3' => 'audio/mp3',
692: 'mpg' => 'video/mpeg',
693: 'mpeg' => 'video/mpeg',
694: 'pdf' => 'application/pdf',
695: 'png' => 'image/png',
696: 'swf' => 'application/shockwave-flash',
697: 'tif' => 'image/tiff',
698: 'tiff' => 'image/tiff',
699: 'wav' => 'audio/wav',
700: 'xbm' => 'image/xbm',
701: 'xml' => 'text/xml',
702: );
703:
704: header("Cache-Control: no-cache, must-revalidate");
705: header("Pragma: no-cache");
706:
707: $basename = basename(__FILE__);
708: if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
709: chdir(Extract_Phar::$temp);
710: include $web;
711: return;
712: }
713: $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
714: if (!$pt || $pt == '/') {
715: $pt = $web;
716: header('HTTP/1.1 301 Moved Permanently');
717: header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
718: exit;
719: }
720: $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
721: if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
722: header('HTTP/1.0 404 Not Found');
723: 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>";
724: exit;
725: }
726: $b = pathinfo($a);
727: if (!isset($b['extension'])) {
728: header('Content-Type: text/plain');
729: header('Content-Length: ' . filesize($a));
730: readfile($a);
731: exit;
732: }
733: if (isset($mimes[$b['extension']])) {
734: if ($mimes[$b['extension']] === 1) {
735: include $a;
736: exit;
737: }
738: if ($mimes[$b['extension']] === 2) {
739: highlight_file($a);
740: exit;
741: }
742: header('Content-Type: ' .$mimes[$b['extension']]);
743: header('Content-Length: ' . filesize($a));
744: readfile($a);
745: exit;
746: }
747: }
748:
749: class Extract_Phar
750: {
751: static $temp;
752: static $origdir;
753: const GZ = 0x1000;
754: const BZ2 = 0x2000;
755: const MASK = 0x3000;
756: const START = 'my/custom/thingy.php';
757: const LEN = 6698;
758:
759: static function go($return = false)
760: {
761: $fp = fopen(__FILE__, 'rb');
762: fseek($fp, self::LEN);
763: $L = unpack('V', $a = (binary)fread($fp, 4));
764: $m = (binary)'';
765:
766: do {
767: $read = 8192;
768: if ($L[1] - strlen($m) < 8192) {
769: $read = $L[1] - strlen($m);
770: }
771: $last = (binary)fread($fp, $read);
772: $m .= $last;
773: } while (strlen($last) && strlen($m) < $L[1]);
774:
775: if (strlen($m) < $L[1]) {
776: die('ERROR: manifest length read was "' .
777: strlen($m) .'" should be "' .
778: $L[1] . '"');
779: }
780:
781: $info = self::_unpack($m);
782: $f = $info['c'];
783:
784: if ($f & self::GZ) {
785: if (!function_exists('gzinflate')) {
786: die('Error: zlib extension is not enabled -' .
787: ' gzinflate() function needed for zlib-compressed .phars');
788: }
789: }
790:
791: if ($f & self::BZ2) {
792: if (!function_exists('bzdecompress')) {
793: die('Error: bzip2 extension is not enabled -' .
794: ' bzdecompress() function needed for bz2-compressed .phars');
795: }
796: }
797:
798: $temp = self::tmpdir();
799:
800: if (!$temp || !is_writable($temp)) {
801: $sessionpath = session_save_path();
802: if (strpos ($sessionpath, ";") !== false)
803: $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
804: if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
805: die('Could not locate temporary directory to extract phar');
806: }
807: $temp = $sessionpath;
808: }
809:
810: $temp .= '/pharextract/'.basename(__FILE__, '.phar');
811: self::$temp = $temp;
812: self::$origdir = getcwd();
813: @mkdir($temp, 0777, true);
814: $temp = realpath($temp);
815:
816: if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
817: self::_removeTmpFiles($temp, getcwd());
818: @mkdir($temp, 0777, true);
819: @file_put_contents($temp . '/' . md5_file(__FILE__), '');
820:
821: foreach ($info['m'] as $path => $file) {
822: $a = !file_exists(dirname($temp . '/' . $path));
823: @mkdir(dirname($temp . '/' . $path), 0777, true);
824: clearstatcache();
825:
826: if ($path[strlen($path) - 1] == '/') {
827: @mkdir($temp . '/' . $path, 0777);
828: } else {
829: file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
830: @chmod($temp . '/' . $path, 0666);
831: }
832: }
833: }
834:
835: chdir($temp);
836:
837: if (!$return) {
838: include self::START;
839: }
840: }
841:
842: static function tmpdir()
843: {
844: if (strpos(PHP_OS, 'WIN') !== false) {
845: if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
846: return $var;
847: }
848: if (is_dir('/temp') || mkdir('/temp')) {
849: return realpath('/temp');
850: }
851: return false;
852: }
853: if ($var = getenv('TMPDIR')) {
854: return $var;
855: }
856: return realpath('/tmp');
857: }
858:
859: static function _unpack($m)
860: {
861: $info = unpack('V', substr($m, 0, 4));
862: $l = unpack('V', substr($m, 10, 4));
863: $m = substr($m, 14 + $l[1]);
864: $s = unpack('V', substr($m, 0, 4));
865: $o = 0;
866: $start = 4 + $s[1];
867: $ret['c'] = 0;
868:
869: for ($i = 0; $i < $info[1]; $i++) {
870: $len = unpack('V', substr($m, $start, 4));
871: $start += 4;
872: $savepath = substr($m, $start, $len[1]);
873: $start += $len[1];
874: $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
875: $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
876: & 0xffffffff);
877: $ret['m'][$savepath][7] = $o;
878: $o += $ret['m'][$savepath][2];
879: $start += 24 + $ret['m'][$savepath][5];
880: $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
881: }
882: return $ret;
883: }
884:
885: static function extractFile($path, $entry, $fp)
886: {
887: $data = '';
888: $c = $entry[2];
889:
890: while ($c) {
891: if ($c < 8192) {
892: $data .= @fread($fp, $c);
893: $c = 0;
894: } else {
895: $c -= 8192;
896: $data .= @fread($fp, 8192);
897: }
898: }
899:
900: if ($entry[4] & self::GZ) {
901: $data = gzinflate($data);
902: } elseif ($entry[4] & self::BZ2) {
903: $data = bzdecompress($data);
904: }
905:
906: if (strlen($data) != $entry[0]) {
907: die("Invalid internal .phar file (size error " . strlen($data) . " != " .
908: $stat[7] . ")");
909: }
910:
911: if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
912: die("Invalid internal .phar file (checksum error)");
913: }
914:
915: return $data;
916: }
917:
918: static function _removeTmpFiles($temp, $origdir)
919: {
920: chdir($temp);
921:
922: foreach (glob('*') as $f) {
923: if (file_exists($f)) {
924: is_dir($f) ? @rmdir($f) : @unlink($f);
925: if (file_exists($f) && is_dir($f)) {
926: self::_removeTmpFiles($f, getcwd());
927: }
928: }
929: }
930:
931: @rmdir($temp);
932: clearstatcache();
933: chdir($origdir);
934: }
935: }
936:
937: Extract_Phar::go();
938: __HALT_COMPILER(); ?>
939: "
940: ============================================================================
941: ============================================================================
942: int(7076)
943: Illegal filename passed in for stub creation, was 401 characters long, and only 400 or less is allowed
944: ===DONE===
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>