phar_setdefaultstub.phpt 22 KB

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