PalmaSolutions

phpd.local

Mar 18th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.89 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4.  * Bundled by phar-composer with the help of php-box.
  5.  *
  6.  * @link https://github.com/clue/phar-composer
  7.  */
  8. define('BOX_EXTRACT_PATTERN_DEFAULT', '__HALT' . '_COMPILER(); ?>');
  9. define('BOX_EXTRACT_PATTERN_OPEN', "__HALT" . "_COMPILER(); ?>\r\n");
  10. if (class_exists('Phar')) {
  11. Phar::mapPhar('');
  12. require 'phar://' . __FILE__ . '/bin/psocksd';
  13. } else {
  14. $extract = new Extract(__FILE__, Extract::findStubLength(__FILE__));
  15. $dir = $extract->go();
  16. set_include_path($dir . PATH_SEPARATOR . get_include_path());
  17. require "$dir/bin/psocksd";
  18. }
  19. class Extract
  20. {
  21. const PATTERN_DEFAULT = BOX_EXTRACT_PATTERN_DEFAULT;
  22. const PATTERN_OPEN = BOX_EXTRACT_PATTERN_OPEN;
  23. const GZ = 0x1000;
  24. const BZ2 = 0x2000;
  25. const MASK = 0x3000;
  26. private $file;
  27. private $handle;
  28. private $stub;
  29. public function __construct($file, $stub)
  30. {
  31. if (!is_file($file)) {
  32. throw new InvalidArgumentException(
  33. sprintf(
  34. 'The path "%s" is not a file or does not exist.',
  35. $file
  36. )
  37. );
  38. }
  39. $this->file = $file;
  40. $this->stub = $stub;
  41. }
  42. public static function findStubLength(
  43. $file,
  44. $pattern = self::PATTERN_OPEN
  45. ) {
  46. if (!($fp = fopen($file, 'rb'))) {
  47. throw new RuntimeException(
  48. sprintf(
  49. 'The phar "%s" could not be opened for reading.',
  50. $file
  51. )
  52. );
  53. }
  54. $stub = null;
  55. $offset = 0;
  56. $combo = str_split($pattern);
  57. while (!feof($fp)) {
  58. if (fgetc($fp) === $combo[$offset]) {
  59. $offset++;
  60. if (!isset($combo[$offset])) {
  61. $stub = ftell($fp);
  62. break;
  63. }
  64. } else {
  65. $offset = 0;
  66. }
  67. }
  68. fclose($fp);
  69. if (null === $stub) {
  70. throw new InvalidArgumentException(
  71. sprintf(
  72. 'The pattern could not be found in "%s".',
  73. $file
  74. )
  75. );
  76. }
  77. return $stub;
  78. }
  79. public function go($dir = null)
  80. {
  81.  if (null === $dir) {
  82. $dir = rtrim(sys_get_temp_dir(), '\\/')
  83. . DIRECTORY_SEPARATOR
  84. . 'pharextract'
  85. . DIRECTORY_SEPARATOR
  86. . basename($this->file, '.phar');
  87. } else {
  88. $dir = realpath($dir);
  89. }
  90.  $md5 = $dir . DIRECTORY_SEPARATOR . md5_file($this->file);
  91. if (file_exists($md5)) {
  92. return $dir;
  93. }
  94. if (!is_dir($dir)) {
  95. $this->createDir($dir);
  96. }
  97.  $this->open();
  98. if (-1 === fseek($this->handle, $this->stub)) {
  99. throw new RuntimeException(
  100. sprintf(
  101. 'Could not seek to %d in the file "%s".',
  102. $this->stub,
  103. $this->file
  104. )
  105. );
  106. }
  107.  $info = $this->readManifest();
  108. if ($info['flags'] & self::GZ) {
  109. if (!function_exists('gzinflate')) {
  110. throw new RuntimeException(
  111. 'The zlib extension is (gzinflate()) is required for "%s.',
  112. $this->file
  113. );
  114. }
  115. }
  116. if ($info['flags'] & self::BZ2) {
  117. if (!function_exists('bzdecompress')) {
  118. throw new RuntimeException(
  119. 'The bzip2 extension (bzdecompress()) is required for "%s".',
  120. $this->file
  121. );
  122. }
  123. }
  124. self::purge($dir);
  125. $this->createDir($dir);
  126. $this->createFile($md5);
  127. foreach ($info['files'] as $info) {
  128. $path = $dir . DIRECTORY_SEPARATOR . $info['path'];
  129. $parent = dirname($path);
  130. if (!is_dir($parent)) {
  131. $this->createDir($parent);
  132. }
  133. if (preg_match('{/$}', $info['path'])) {
  134. $this->createDir($path, 0777, false);
  135. } else {
  136. $this->createFile(
  137. $path,
  138. $this->extractFile($info)
  139. );
  140. }
  141. }
  142. return $dir;
  143. }
  144. public static function purge($path)
  145. {
  146. if (is_dir($path)) {
  147. foreach (scandir($path) as $item) {
  148. if (('.' === $item) || ('..' === $item)) {
  149. continue;
  150. }
  151. self::purge($path . DIRECTORY_SEPARATOR . $item);
  152. }
  153. if (!rmdir($path)) {
  154. throw new RuntimeException(
  155. sprintf(
  156. 'The directory "%s" could not be deleted.',
  157. $path
  158. )
  159. );
  160. }
  161. } else {
  162. if (!unlink($path)) {
  163. throw new RuntimeException(
  164. sprintf(
  165. 'The file "%s" could not be deleted.',
  166. $path
  167. )
  168. );
  169. }
  170. }
  171. }
  172. private function createDir($path, $chmod = 0777, $recursive = true)
  173. {
  174. if (!mkdir($path, $chmod, $recursive)) {
  175. throw new RuntimeException(
  176. sprintf(
  177. 'The directory path "%s" could not be created.',
  178. $path
  179. )
  180. );
  181. }
  182. }
  183. private function createFile($path, $contents = '', $mode = 0666)
  184. {
  185. if (false === file_put_contents($path, $contents)) {
  186. throw new RuntimeException(
  187. sprintf(
  188. 'The file "%s" could not be written.',
  189. $path
  190. )
  191. );
  192. }
  193. if (!chmod($path, $mode)) {
  194. throw new RuntimeException(
  195. sprintf(
  196. 'The file "%s" could not be chmodded to %o.',
  197. $path,
  198. $mode
  199. )
  200. );
  201. }
  202. }
  203. private function extractFile($info)
  204. {
  205. if (0 === $info['size']) {
  206. return '';
  207. }
  208. $data = $this->read($info['compressed_size']);
  209. if ($info['flags'] & self::GZ) {
  210. if (false === ($data = gzinflate($data))) {
  211. throw new RuntimeException(
  212. sprintf(
  213. 'The "%s" file could not be inflated (gzip) from "%s".',
  214. $info['path'],
  215. $this->file
  216. )
  217. );
  218. }
  219. } elseif ($info['flags'] & self::BZ2) {
  220. if (false === ($data = bzdecompress($data))) {
  221. throw new RuntimeException(
  222. sprintf(
  223. 'The "%s" file could not be inflated (bzip2) from "%s".',
  224. $info['path'],
  225. $this->file
  226. )
  227. );
  228. }
  229. }
  230. if (($actual = strlen($data)) !== $info['size']) {
  231. throw new UnexpectedValueException(
  232. sprintf(
  233. 'The size of "%s" (%d) did not match what was expected (%d) in "%s".',
  234. $info['path'],
  235. $actual,
  236. $info['size'],
  237. $this->file
  238. )
  239. );
  240. }
  241. $crc32 = sprintf('%u', crc32($data) & 0xffffffff);
  242. if ($info['crc32'] != $crc32) {
  243. throw new UnexpectedValueException(
  244. sprintf(
  245. 'The crc32 checksum (%s) for "%s" did not match what was expected (%s) in "%s".',
  246. $crc32,
  247. $info['path'],
  248. $info['crc32'],
  249. $this->file
  250. )
  251. );
  252. }
  253. return $data;
  254. }
  255. private function open()
  256. {
  257. if (null === ($this->handle = fopen($this->file, 'rb'))) {
  258. $this->handle = null;
  259. throw new RuntimeException(
  260. sprintf(
  261. 'The file "%s" could not be opened for reading.',
  262. $this->file
  263. )
  264. );
  265. }
  266. }
  267. private function read($bytes)
  268. {
  269. $read = '';
  270. $total = $bytes;
  271. while (!feof($this->handle) && $bytes) {
  272. if (false === ($chunk = fread($this->handle, $bytes))) {
  273. throw new RuntimeException(
  274. sprintf(
  275. 'Could not read %d bytes from "%s".',
  276. $bytes,
  277. $this->file
  278. )
  279. );
  280. }
  281. $read .= $chunk;
  282. $bytes -= strlen($chunk);
  283. }
  284. if (($actual = strlen($read)) !== $total) {
  285. throw new RuntimeException(
  286. sprintf(
  287. 'Only read %d of %d in "%s".',
  288. $actual,
  289. $total,
  290. $this->file
  291. )
  292. );
  293. }
  294. return $read;
  295. }
  296. private function readManifest()
  297. {
  298. $size = unpack('V', $this->read(4));
  299. $size = $size[1];
  300. $raw = $this->read($size);
  301.  $count = unpack('V', substr($raw, 0, 4));
  302. $count = $count[1];
  303. $aliasSize = unpack('V', substr($raw, 10, 4));
  304. $aliasSize = $aliasSize[1];
  305. $raw = substr($raw, 14 + $aliasSize);
  306. $metaSize = unpack('V', substr($raw, 0, 4));
  307. $metaSize = $metaSize[1];
  308. $offset = 0;
  309. $start = 4 + $metaSize;
  310. $manifest = array(
  311. 'files' => array(),
  312. 'flags' => 0,
  313. );
  314. for ($i = 0; $i < $count; $i++) {
  315. $length = unpack('V', substr($raw, $start, 4));
  316. $length = $length[1];
  317. $start += 4;
  318. $path = substr($raw, $start, $length);
  319. $start += $length;
  320. $file = unpack(
  321. 'Vsize/Vtimestamp/Vcompressed_size/Vcrc32/Vflags/Vmetadata_length',
  322. substr($raw, $start, 24)
  323. );
  324. $file['path'] = $path;
  325. $file['crc32'] = sprintf('%u', $file['crc32'] & 0xffffffff);
  326. $file['offset'] = $offset;
  327. $offset += $file['compressed_size'];
  328. $start += 24 + $file['metadata_length'];
  329. $manifest['flags'] |= $file['flags'] & self::MASK;
  330. $manifest['files'][] = $file;
  331. }
  332. return $manifest;
  333. }
  334. }
  335. __HALT_COMPILER(); ?>
  336. 6
Advertisement
Add Comment
Please, Sign In to add comment