PharStreamWrapper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace TYPO3\PharStreamWrapper;
  3. /*
  4. * This file is part of the TYPO3 project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms
  7. * of the MIT License (MIT). For the full copyright and license information,
  8. * please read the LICENSE file that was distributed with this source code.
  9. *
  10. * The TYPO3 project - inspiring people to share!
  11. */
  12. use TYPO3\PharStreamWrapper\Resolver\PharInvocation;
  13. class PharStreamWrapper
  14. {
  15. /**
  16. * Internal stream constants that are not exposed to PHP, but used...
  17. * @see https://github.com/php/php-src/blob/e17fc0d73c611ad0207cac8a4a01ded38251a7dc/main/php_streams.h
  18. */
  19. const STREAM_OPEN_FOR_INCLUDE = 128;
  20. /**
  21. * @var resource
  22. */
  23. public $context;
  24. /**
  25. * @var resource
  26. */
  27. protected $internalResource;
  28. /**
  29. * @var PharInvocation
  30. */
  31. protected $invocation;
  32. /**
  33. * @return bool
  34. */
  35. public function dir_closedir()
  36. {
  37. if (!is_resource($this->internalResource)) {
  38. return false;
  39. }
  40. $this->invokeInternalStreamWrapper(
  41. 'closedir',
  42. $this->internalResource
  43. );
  44. return !is_resource($this->internalResource);
  45. }
  46. /**
  47. * @param string $path
  48. * @param int $options
  49. * @return bool
  50. */
  51. public function dir_opendir($path, $options)
  52. {
  53. $this->assert($path, Behavior::COMMAND_DIR_OPENDIR);
  54. $this->internalResource = $this->invokeInternalStreamWrapper(
  55. 'opendir',
  56. $path,
  57. $this->context
  58. );
  59. return is_resource($this->internalResource);
  60. }
  61. /**
  62. * @return string|false
  63. */
  64. public function dir_readdir()
  65. {
  66. return $this->invokeInternalStreamWrapper(
  67. 'readdir',
  68. $this->internalResource
  69. );
  70. }
  71. /**
  72. * @return bool
  73. */
  74. public function dir_rewinddir()
  75. {
  76. if (!is_resource($this->internalResource)) {
  77. return false;
  78. }
  79. $this->invokeInternalStreamWrapper(
  80. 'rewinddir',
  81. $this->internalResource
  82. );
  83. return is_resource($this->internalResource);
  84. }
  85. /**
  86. * @param string $path
  87. * @param int $mode
  88. * @param int $options
  89. * @return bool
  90. */
  91. public function mkdir($path, $mode, $options)
  92. {
  93. $this->assert($path, Behavior::COMMAND_MKDIR);
  94. return $this->invokeInternalStreamWrapper(
  95. 'mkdir',
  96. $path,
  97. $mode,
  98. (bool) ($options & STREAM_MKDIR_RECURSIVE),
  99. $this->context
  100. );
  101. }
  102. /**
  103. * @param string $path_from
  104. * @param string $path_to
  105. * @return bool
  106. */
  107. public function rename($path_from, $path_to)
  108. {
  109. $this->assert($path_from, Behavior::COMMAND_RENAME);
  110. $this->assert($path_to, Behavior::COMMAND_RENAME);
  111. return $this->invokeInternalStreamWrapper(
  112. 'rename',
  113. $path_from,
  114. $path_to,
  115. $this->context
  116. );
  117. }
  118. /**
  119. * @param string $path
  120. * @param int $options
  121. * @return bool
  122. */
  123. public function rmdir($path, $options)
  124. {
  125. $this->assert($path, Behavior::COMMAND_RMDIR);
  126. return $this->invokeInternalStreamWrapper(
  127. 'rmdir',
  128. $path,
  129. $this->context
  130. );
  131. }
  132. /**
  133. * @param int $cast_as
  134. */
  135. public function stream_cast($cast_as)
  136. {
  137. throw new Exception(
  138. 'Method stream_select() cannot be used',
  139. 1530103999
  140. );
  141. }
  142. public function stream_close()
  143. {
  144. $this->invokeInternalStreamWrapper(
  145. 'fclose',
  146. $this->internalResource
  147. );
  148. }
  149. /**
  150. * @return bool
  151. */
  152. public function stream_eof()
  153. {
  154. return $this->invokeInternalStreamWrapper(
  155. 'feof',
  156. $this->internalResource
  157. );
  158. }
  159. /**
  160. * @return bool
  161. */
  162. public function stream_flush()
  163. {
  164. return $this->invokeInternalStreamWrapper(
  165. 'fflush',
  166. $this->internalResource
  167. );
  168. }
  169. /**
  170. * @param int $operation
  171. * @return bool
  172. */
  173. public function stream_lock($operation)
  174. {
  175. return $this->invokeInternalStreamWrapper(
  176. 'flock',
  177. $this->internalResource,
  178. $operation
  179. );
  180. }
  181. /**
  182. * @param string $path
  183. * @param int $option
  184. * @param string|int $value
  185. * @return bool
  186. */
  187. public function stream_metadata($path, $option, $value)
  188. {
  189. $this->assert($path, Behavior::COMMAND_STEAM_METADATA);
  190. if ($option === STREAM_META_TOUCH) {
  191. return call_user_func_array(
  192. array($this, 'invokeInternalStreamWrapper'),
  193. array_merge(array('touch', $path), (array) $value)
  194. );
  195. }
  196. if ($option === STREAM_META_OWNER_NAME || $option === STREAM_META_OWNER) {
  197. return $this->invokeInternalStreamWrapper(
  198. 'chown',
  199. $path,
  200. $value
  201. );
  202. }
  203. if ($option === STREAM_META_GROUP_NAME || $option === STREAM_META_GROUP) {
  204. return $this->invokeInternalStreamWrapper(
  205. 'chgrp',
  206. $path,
  207. $value
  208. );
  209. }
  210. if ($option === STREAM_META_ACCESS) {
  211. return $this->invokeInternalStreamWrapper(
  212. 'chmod',
  213. $path,
  214. $value
  215. );
  216. }
  217. return false;
  218. }
  219. /**
  220. * @param string $path
  221. * @param string $mode
  222. * @param int $options
  223. * @param string|null $opened_path
  224. * @return bool
  225. */
  226. public function stream_open(
  227. $path,
  228. $mode,
  229. $options,
  230. &$opened_path = null
  231. ) {
  232. $this->assert($path, Behavior::COMMAND_STREAM_OPEN);
  233. $arguments = array($path, $mode, (bool) ($options & STREAM_USE_PATH));
  234. // only add stream context for non include/require calls
  235. if (!($options & static::STREAM_OPEN_FOR_INCLUDE)) {
  236. $arguments[] = $this->context;
  237. // work around https://bugs.php.net/bug.php?id=66569
  238. // for including files from Phar stream with OPcache enabled
  239. } else {
  240. Helper::resetOpCache();
  241. }
  242. $this->internalResource = call_user_func_array(
  243. array($this, 'invokeInternalStreamWrapper'),
  244. array_merge(array('fopen'), $arguments)
  245. );
  246. if (!is_resource($this->internalResource)) {
  247. return false;
  248. }
  249. if ($opened_path !== null) {
  250. $metaData = stream_get_meta_data($this->internalResource);
  251. $opened_path = $metaData['uri'];
  252. }
  253. return true;
  254. }
  255. /**
  256. * @param int $count
  257. * @return string
  258. */
  259. public function stream_read($count)
  260. {
  261. return $this->invokeInternalStreamWrapper(
  262. 'fread',
  263. $this->internalResource,
  264. $count
  265. );
  266. }
  267. /**
  268. * @param int $offset
  269. * @param int $whence
  270. * @return bool
  271. */
  272. public function stream_seek($offset, $whence = SEEK_SET)
  273. {
  274. return $this->invokeInternalStreamWrapper(
  275. 'fseek',
  276. $this->internalResource,
  277. $offset,
  278. $whence
  279. ) !== -1;
  280. }
  281. /**
  282. * @param int $option
  283. * @param int $arg1
  284. * @param int $arg2
  285. * @return bool
  286. */
  287. public function stream_set_option($option, $arg1, $arg2)
  288. {
  289. if ($option === STREAM_OPTION_BLOCKING) {
  290. return $this->invokeInternalStreamWrapper(
  291. 'stream_set_blocking',
  292. $this->internalResource,
  293. $arg1
  294. );
  295. }
  296. if ($option === STREAM_OPTION_READ_TIMEOUT) {
  297. return $this->invokeInternalStreamWrapper(
  298. 'stream_set_timeout',
  299. $this->internalResource,
  300. $arg1,
  301. $arg2
  302. );
  303. }
  304. if ($option === STREAM_OPTION_WRITE_BUFFER) {
  305. return $this->invokeInternalStreamWrapper(
  306. 'stream_set_write_buffer',
  307. $this->internalResource,
  308. $arg2
  309. ) === 0;
  310. }
  311. return false;
  312. }
  313. /**
  314. * @return array
  315. */
  316. public function stream_stat()
  317. {
  318. return $this->invokeInternalStreamWrapper(
  319. 'fstat',
  320. $this->internalResource
  321. );
  322. }
  323. /**
  324. * @return int
  325. */
  326. public function stream_tell()
  327. {
  328. return $this->invokeInternalStreamWrapper(
  329. 'ftell',
  330. $this->internalResource
  331. );
  332. }
  333. /**
  334. * @param int $new_size
  335. * @return bool
  336. */
  337. public function stream_truncate($new_size)
  338. {
  339. return $this->invokeInternalStreamWrapper(
  340. 'ftruncate',
  341. $this->internalResource,
  342. $new_size
  343. );
  344. }
  345. /**
  346. * @param string $data
  347. * @return int
  348. */
  349. public function stream_write($data)
  350. {
  351. return $this->invokeInternalStreamWrapper(
  352. 'fwrite',
  353. $this->internalResource,
  354. $data
  355. );
  356. }
  357. /**
  358. * @param string $path
  359. * @return bool
  360. */
  361. public function unlink($path)
  362. {
  363. $this->assert($path, Behavior::COMMAND_UNLINK);
  364. return $this->invokeInternalStreamWrapper(
  365. 'unlink',
  366. $path,
  367. $this->context
  368. );
  369. }
  370. /**
  371. * @param string $path
  372. * @param int $flags
  373. * @return array|false
  374. */
  375. public function url_stat($path, $flags)
  376. {
  377. $this->assert($path, Behavior::COMMAND_URL_STAT);
  378. $functionName = $flags & STREAM_URL_STAT_QUIET ? '@stat' : 'stat';
  379. return $this->invokeInternalStreamWrapper($functionName, $path);
  380. }
  381. /**
  382. * @param string $path
  383. * @param string $command
  384. */
  385. protected function assert($path, $command)
  386. {
  387. if (Manager::instance()->assert($path, $command) === true) {
  388. $this->collectInvocation($path);
  389. return;
  390. }
  391. throw new Exception(
  392. sprintf(
  393. 'Denied invocation of "%s" for command "%s"',
  394. $path,
  395. $command
  396. ),
  397. 1535189880
  398. );
  399. }
  400. /**
  401. * @param string $path
  402. */
  403. protected function collectInvocation($path)
  404. {
  405. if (isset($this->invocation)) {
  406. return;
  407. }
  408. $manager = Manager::instance();
  409. $this->invocation = $manager->resolve($path);
  410. if ($this->invocation === null) {
  411. throw new Exception(
  412. 'Expected invocation could not be resolved',
  413. 1556389591
  414. );
  415. }
  416. // confirm, previous interceptor(s) validated invocation
  417. $this->invocation->confirm();
  418. $collection = $manager->getCollection();
  419. if (!$collection->has($this->invocation)) {
  420. $collection->collect($this->invocation);
  421. }
  422. }
  423. /**
  424. * @return Manager|Assertable
  425. * @deprecated Use Manager::instance() directly
  426. */
  427. protected function resolveAssertable()
  428. {
  429. return Manager::instance();
  430. }
  431. /**
  432. * Invokes commands on the native PHP Phar stream wrapper.
  433. *
  434. * @param string $functionName
  435. * @param mixed ...$arguments
  436. * @return mixed
  437. */
  438. private function invokeInternalStreamWrapper($functionName)
  439. {
  440. $arguments = func_get_args();
  441. array_shift($arguments);
  442. $silentExecution = $functionName[0] === '@';
  443. $functionName = ltrim($functionName, '@');
  444. $this->restoreInternalSteamWrapper();
  445. try {
  446. if ($silentExecution) {
  447. $result = @call_user_func_array($functionName, $arguments);
  448. } else {
  449. $result = call_user_func_array($functionName, $arguments);
  450. }
  451. } catch (\Exception $exception) {
  452. $this->registerStreamWrapper();
  453. throw $exception;
  454. } catch (\Throwable $throwable) {
  455. $this->registerStreamWrapper();
  456. throw $throwable;
  457. }
  458. $this->registerStreamWrapper();
  459. return $result;
  460. }
  461. private function restoreInternalSteamWrapper()
  462. {
  463. stream_wrapper_restore('phar');
  464. }
  465. private function registerStreamWrapper()
  466. {
  467. stream_wrapper_unregister('phar');
  468. stream_wrapper_register('phar', get_class($this));
  469. }
  470. }