phar_createdefaultstub.phpt 22 KB

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