File.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. <?php
  2. /**
  3. * Origin filesystem driver
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Filesystem\Driver;
  9. use Magento\Framework\Exception\FileSystemException;
  10. use Magento\Framework\Filesystem\DriverInterface;
  11. use Magento\Framework\Filesystem\Glob;
  12. /**
  13. * Class File
  14. *
  15. * @package Magento\Framework\Filesystem\Driver
  16. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  17. */
  18. class File implements DriverInterface
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $scheme = '';
  24. /**
  25. * Returns last warning message string
  26. *
  27. * @return string
  28. */
  29. protected function getWarningMessage()
  30. {
  31. $warning = error_get_last();
  32. if ($warning && $warning['type'] == E_WARNING) {
  33. return 'Warning!' . $warning['message'];
  34. }
  35. return null;
  36. }
  37. /**
  38. * Is file or directory exist in file system
  39. *
  40. * @param string $path
  41. * @return bool
  42. * @throws FileSystemException
  43. */
  44. public function isExists($path)
  45. {
  46. clearstatcache();
  47. $result = @file_exists($this->getScheme() . $path);
  48. if ($result === null) {
  49. throw new FileSystemException(
  50. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  51. );
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Gathers the statistics of the given path
  57. *
  58. * @param string $path
  59. * @return array
  60. * @throws FileSystemException
  61. */
  62. public function stat($path)
  63. {
  64. clearstatcache();
  65. $result = @stat($this->getScheme() . $path);
  66. if (!$result) {
  67. throw new FileSystemException(
  68. new \Magento\Framework\Phrase('Cannot gather stats! %1', [$this->getWarningMessage()])
  69. );
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Check permissions for reading file or directory
  75. *
  76. * @param string $path
  77. * @return bool
  78. * @throws FileSystemException
  79. */
  80. public function isReadable($path)
  81. {
  82. clearstatcache();
  83. $result = @is_readable($this->getScheme() . $path);
  84. if ($result === null) {
  85. throw new FileSystemException(
  86. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  87. );
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Tells whether the filename is a regular file
  93. *
  94. * @param string $path
  95. * @return bool
  96. * @throws FileSystemException
  97. */
  98. public function isFile($path)
  99. {
  100. clearstatcache();
  101. $result = @is_file($this->getScheme() . $path);
  102. if ($result === null) {
  103. throw new FileSystemException(
  104. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  105. );
  106. }
  107. return $result;
  108. }
  109. /**
  110. * Tells whether the filename is a regular directory
  111. *
  112. * @param string $path
  113. * @return bool
  114. * @throws FileSystemException
  115. */
  116. public function isDirectory($path)
  117. {
  118. clearstatcache();
  119. $result = @is_dir($this->getScheme() . $path);
  120. if ($result === null) {
  121. throw new FileSystemException(
  122. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  123. );
  124. }
  125. return $result;
  126. }
  127. /**
  128. * Retrieve file contents from given path
  129. *
  130. * @param string $path
  131. * @param string|null $flag
  132. * @param resource|null $context
  133. * @return string
  134. * @throws FileSystemException
  135. */
  136. public function fileGetContents($path, $flag = null, $context = null)
  137. {
  138. clearstatcache();
  139. $result = @file_get_contents($this->getScheme() . $path, $flag, $context);
  140. if (false === $result) {
  141. throw new FileSystemException(
  142. new \Magento\Framework\Phrase(
  143. 'The contents from the "%1" file can\'t be read. %2',
  144. [$path, $this->getWarningMessage()]
  145. )
  146. );
  147. }
  148. return $result;
  149. }
  150. /**
  151. * Check if given path is writable
  152. *
  153. * @param string $path
  154. * @return bool
  155. * @throws FileSystemException
  156. */
  157. public function isWritable($path)
  158. {
  159. clearstatcache();
  160. $result = @is_writable($this->getScheme() . $path);
  161. if ($result === null) {
  162. throw new FileSystemException(
  163. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  164. );
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Returns parent directory's path
  170. *
  171. * @param string $path
  172. * @return string
  173. */
  174. public function getParentDirectory($path)
  175. {
  176. return dirname($this->getScheme() . $path);
  177. }
  178. /**
  179. * Create directory
  180. *
  181. * @param string $path
  182. * @param int $permissions
  183. * @return bool
  184. * @throws FileSystemException
  185. */
  186. public function createDirectory($path, $permissions = 0777)
  187. {
  188. return $this->mkdirRecursive($path, $permissions);
  189. }
  190. /**
  191. * Create a directory recursively taking into account race conditions
  192. *
  193. * @param string $path
  194. * @param int $permissions
  195. * @return bool
  196. * @throws FileSystemException
  197. */
  198. private function mkdirRecursive($path, $permissions = 0777)
  199. {
  200. $path = $this->getScheme() . $path;
  201. if (is_dir($path)) {
  202. return true;
  203. }
  204. $parentDir = dirname($path);
  205. while (!is_dir($parentDir)) {
  206. $this->mkdirRecursive($parentDir, $permissions);
  207. }
  208. $result = @mkdir($path, $permissions);
  209. if (!$result) {
  210. if (is_dir($path)) {
  211. $result = true;
  212. } else {
  213. throw new FileSystemException(
  214. new \Magento\Framework\Phrase(
  215. 'Directory "%1" cannot be created %2',
  216. [$path, $this->getWarningMessage()]
  217. )
  218. );
  219. }
  220. }
  221. return $result;
  222. }
  223. /**
  224. * Read directory
  225. *
  226. * @param string $path
  227. * @return string[]
  228. * @throws FileSystemException
  229. */
  230. public function readDirectory($path)
  231. {
  232. try {
  233. $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
  234. $iterator = new \FilesystemIterator($path, $flags);
  235. $result = [];
  236. /** @var \FilesystemIterator $file */
  237. foreach ($iterator as $file) {
  238. $result[] = $file->getPathname();
  239. }
  240. sort($result);
  241. return $result;
  242. } catch (\Exception $e) {
  243. throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
  244. }
  245. }
  246. /**
  247. * Search paths by given regex
  248. *
  249. * @param string $pattern
  250. * @param string $path
  251. * @return string[]
  252. * @throws FileSystemException
  253. */
  254. public function search($pattern, $path)
  255. {
  256. clearstatcache();
  257. $globPattern = rtrim($path, '/') . '/' . ltrim($pattern, '/');
  258. $result = Glob::glob($globPattern, Glob::GLOB_BRACE);
  259. return is_array($result) ? $result : [];
  260. }
  261. /**
  262. * Renames a file or directory
  263. *
  264. * @param string $oldPath
  265. * @param string $newPath
  266. * @param DriverInterface|null $targetDriver
  267. * @return bool
  268. * @throws FileSystemException
  269. */
  270. public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
  271. {
  272. $result = false;
  273. $targetDriver = $targetDriver ?: $this;
  274. if (get_class($targetDriver) == get_class($this)) {
  275. $result = @rename($this->getScheme() . $oldPath, $newPath);
  276. } else {
  277. $content = $this->fileGetContents($oldPath);
  278. if (false !== $targetDriver->filePutContents($newPath, $content)) {
  279. $result = $this->deleteFile($newPath);
  280. }
  281. }
  282. if (!$result) {
  283. throw new FileSystemException(
  284. new \Magento\Framework\Phrase(
  285. 'The path "%1" cannot be renamed into "%2" %3',
  286. [$oldPath, $newPath, $this->getWarningMessage()]
  287. )
  288. );
  289. }
  290. return $result;
  291. }
  292. /**
  293. * Copy source into destination
  294. *
  295. * @param string $source
  296. * @param string $destination
  297. * @param DriverInterface|null $targetDriver
  298. * @return bool
  299. * @throws FileSystemException
  300. */
  301. public function copy($source, $destination, DriverInterface $targetDriver = null)
  302. {
  303. $targetDriver = $targetDriver ?: $this;
  304. if (get_class($targetDriver) == get_class($this)) {
  305. $result = @copy($this->getScheme() . $source, $destination);
  306. } else {
  307. $content = $this->fileGetContents($source);
  308. $result = $targetDriver->filePutContents($destination, $content);
  309. }
  310. if (!$result) {
  311. throw new FileSystemException(
  312. new \Magento\Framework\Phrase(
  313. 'The file or directory "%1" cannot be copied to "%2" %3',
  314. [
  315. $source,
  316. $destination,
  317. $this->getWarningMessage()
  318. ]
  319. )
  320. );
  321. }
  322. return $result;
  323. }
  324. /**
  325. * Create symlink on source and place it into destination
  326. *
  327. * @param string $source
  328. * @param string $destination
  329. * @param DriverInterface|null $targetDriver
  330. * @return bool
  331. * @throws FileSystemException
  332. */
  333. public function symlink($source, $destination, DriverInterface $targetDriver = null)
  334. {
  335. $result = false;
  336. if ($targetDriver === null || get_class($targetDriver) == get_class($this)) {
  337. $result = @symlink($this->getScheme() . $source, $destination);
  338. }
  339. if (!$result) {
  340. throw new FileSystemException(
  341. new \Magento\Framework\Phrase(
  342. 'A symlink for "%1" can\'t be created and placed to "%2". %3',
  343. [
  344. $source,
  345. $destination,
  346. $this->getWarningMessage()
  347. ]
  348. )
  349. );
  350. }
  351. return $result;
  352. }
  353. /**
  354. * Delete file
  355. *
  356. * @param string $path
  357. * @return bool
  358. * @throws FileSystemException
  359. */
  360. public function deleteFile($path)
  361. {
  362. $result = @unlink($this->getScheme() . $path);
  363. if (!$result) {
  364. throw new FileSystemException(
  365. new \Magento\Framework\Phrase(
  366. 'The "%1" file can\'t be deleted. %2',
  367. [$path, $this->getWarningMessage()]
  368. )
  369. );
  370. }
  371. return $result;
  372. }
  373. /**
  374. * Recursive delete directory
  375. *
  376. * @param string $path
  377. * @return bool
  378. * @throws FileSystemException
  379. */
  380. public function deleteDirectory($path)
  381. {
  382. $exceptionMessages = [];
  383. $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
  384. $iterator = new \FilesystemIterator($path, $flags);
  385. /** @var \FilesystemIterator $entity */
  386. foreach ($iterator as $entity) {
  387. try {
  388. if ($entity->isDir()) {
  389. $this->deleteDirectory($entity->getPathname());
  390. } else {
  391. $this->deleteFile($entity->getPathname());
  392. }
  393. } catch (FileSystemException $exception) {
  394. $exceptionMessages[] = $exception->getMessage();
  395. }
  396. }
  397. if (!empty($exceptionMessages)) {
  398. throw new FileSystemException(
  399. new \Magento\Framework\Phrase(
  400. \implode(' ', $exceptionMessages)
  401. )
  402. );
  403. }
  404. $fullPath = $this->getScheme() . $path;
  405. if (is_link($fullPath)) {
  406. $result = @unlink($fullPath);
  407. } else {
  408. $result = @rmdir($fullPath);
  409. }
  410. if (!$result) {
  411. throw new FileSystemException(
  412. new \Magento\Framework\Phrase(
  413. 'The directory "%1" cannot be deleted %2',
  414. [$path, $this->getWarningMessage()]
  415. )
  416. );
  417. }
  418. return $result;
  419. }
  420. /**
  421. * Change permissions of given path
  422. *
  423. * @param string $path
  424. * @param int $permissions
  425. * @return bool
  426. * @throws FileSystemException
  427. */
  428. public function changePermissions($path, $permissions)
  429. {
  430. $result = @chmod($this->getScheme() . $path, $permissions);
  431. if (!$result) {
  432. throw new FileSystemException(
  433. new \Magento\Framework\Phrase(
  434. 'The permissions can\'t be changed for the "%1" path. %2.',
  435. [$path, $this->getWarningMessage()]
  436. )
  437. );
  438. }
  439. return $result;
  440. }
  441. /**
  442. * Recursively change permissions of given path
  443. *
  444. * @param string $path
  445. * @param int $dirPermissions
  446. * @param int $filePermissions
  447. * @return bool
  448. * @throws FileSystemException
  449. */
  450. public function changePermissionsRecursively($path, $dirPermissions, $filePermissions)
  451. {
  452. $result = true;
  453. if ($this->isFile($path)) {
  454. $result = @chmod($path, $filePermissions);
  455. } else {
  456. $result = @chmod($path, $dirPermissions);
  457. }
  458. if (!$result) {
  459. throw new FileSystemException(
  460. new \Magento\Framework\Phrase(
  461. 'The permissions can\'t be changed for the "%1" path. %2.',
  462. [$path, $this->getWarningMessage()]
  463. )
  464. );
  465. }
  466. $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
  467. $iterator = new \RecursiveIteratorIterator(
  468. new \RecursiveDirectoryIterator($path, $flags),
  469. \RecursiveIteratorIterator::CHILD_FIRST
  470. );
  471. /** @var \FilesystemIterator $entity */
  472. foreach ($iterator as $entity) {
  473. if ($entity->isDir()) {
  474. $result = @chmod($entity->getPathname(), $dirPermissions);
  475. } else {
  476. $result = @chmod($entity->getPathname(), $filePermissions);
  477. }
  478. if (!$result) {
  479. throw new FileSystemException(
  480. new \Magento\Framework\Phrase(
  481. 'The permissions can\'t be changed for the "%1" path. %2.',
  482. [$path, $this->getWarningMessage()]
  483. )
  484. );
  485. }
  486. }
  487. return $result;
  488. }
  489. /**
  490. * Sets access and modification time of file.
  491. *
  492. * @param string $path
  493. * @param int|null $modificationTime
  494. * @return bool
  495. * @throws FileSystemException
  496. */
  497. public function touch($path, $modificationTime = null)
  498. {
  499. if (!$modificationTime) {
  500. $result = @touch($this->getScheme() . $path);
  501. } else {
  502. $result = @touch($this->getScheme() . $path, $modificationTime);
  503. }
  504. if (!$result) {
  505. throw new FileSystemException(
  506. new \Magento\Framework\Phrase(
  507. 'The "%1" file or directory can\'t be touched. %2',
  508. [$path, $this->getWarningMessage()]
  509. )
  510. );
  511. }
  512. return $result;
  513. }
  514. /**
  515. * Write contents to file in given path
  516. *
  517. * @param string $path
  518. * @param string $content
  519. * @param string|null $mode
  520. * @return int The number of bytes that were written.
  521. * @throws FileSystemException
  522. */
  523. public function filePutContents($path, $content, $mode = null)
  524. {
  525. $result = @file_put_contents($this->getScheme() . $path, $content, $mode);
  526. if (!$result) {
  527. throw new FileSystemException(
  528. new \Magento\Framework\Phrase(
  529. 'The specified "%1" file couldn\'t be written. %2',
  530. [$path, $this->getWarningMessage()]
  531. )
  532. );
  533. }
  534. return $result;
  535. }
  536. /**
  537. * Open file
  538. *
  539. * @param string $path
  540. * @param string $mode
  541. * @return resource file
  542. * @throws FileSystemException
  543. */
  544. public function fileOpen($path, $mode)
  545. {
  546. $result = @fopen($this->getScheme() . $path, $mode);
  547. if (!$result) {
  548. throw new FileSystemException(
  549. new \Magento\Framework\Phrase('File "%1" cannot be opened %2', [$path, $this->getWarningMessage()])
  550. );
  551. }
  552. return $result;
  553. }
  554. /**
  555. * Reads the line content from file pointer (with specified number of bytes from the current position).
  556. *
  557. * @param resource $resource
  558. * @param int $length
  559. * @param string $ending [optional]
  560. * @return string
  561. * @throws FileSystemException
  562. */
  563. public function fileReadLine($resource, $length, $ending = null)
  564. {
  565. $result = @stream_get_line($resource, $length, $ending);
  566. if (false === $result) {
  567. throw new FileSystemException(
  568. new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
  569. );
  570. }
  571. return $result;
  572. }
  573. /**
  574. * Reads the specified number of bytes from the current position.
  575. *
  576. * @param resource $resource
  577. * @param int $length
  578. * @return string
  579. * @throws FileSystemException
  580. */
  581. public function fileRead($resource, $length)
  582. {
  583. $result = @fread($resource, $length);
  584. if ($result === false) {
  585. throw new FileSystemException(
  586. new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
  587. );
  588. }
  589. return $result;
  590. }
  591. /**
  592. * Reads one CSV row from the file
  593. *
  594. * @param resource $resource
  595. * @param int $length [optional]
  596. * @param string $delimiter [optional]
  597. * @param string $enclosure [optional]
  598. * @param string $escape [optional]
  599. * @return array|bool|null
  600. * @throws FileSystemException
  601. */
  602. public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\')
  603. {
  604. $result = @fgetcsv($resource, $length, $delimiter, $enclosure, $escape);
  605. if ($result === null) {
  606. throw new FileSystemException(
  607. new \Magento\Framework\Phrase(
  608. 'The "%1" CSV handle is incorrect. Verify the handle and try again.',
  609. [$this->getWarningMessage()]
  610. )
  611. );
  612. }
  613. return $result;
  614. }
  615. /**
  616. * Returns position of read/write pointer
  617. *
  618. * @param resource $resource
  619. * @return int
  620. * @throws FileSystemException
  621. */
  622. public function fileTell($resource)
  623. {
  624. $result = @ftell($resource);
  625. if ($result === null) {
  626. throw new FileSystemException(
  627. new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
  628. );
  629. }
  630. return $result;
  631. }
  632. /**
  633. * Seeks to the specified offset
  634. *
  635. * @param resource $resource
  636. * @param int $offset
  637. * @param int $whence
  638. * @return int
  639. * @throws FileSystemException
  640. */
  641. public function fileSeek($resource, $offset, $whence = SEEK_SET)
  642. {
  643. $result = @fseek($resource, $offset, $whence);
  644. if ($result === -1) {
  645. throw new FileSystemException(
  646. new \Magento\Framework\Phrase(
  647. 'An error occurred during "%1" fileSeek execution.',
  648. [$this->getWarningMessage()]
  649. )
  650. );
  651. }
  652. return $result;
  653. }
  654. /**
  655. * Returns true if pointer at the end of file or in case of exception
  656. *
  657. * @param resource $resource
  658. * @return boolean
  659. */
  660. public function endOfFile($resource)
  661. {
  662. return feof($resource);
  663. }
  664. /**
  665. * Close file
  666. *
  667. * @param resource $resource
  668. * @return boolean
  669. * @throws FileSystemException
  670. */
  671. public function fileClose($resource)
  672. {
  673. $result = @fclose($resource);
  674. if (!$result) {
  675. throw new FileSystemException(
  676. new \Magento\Framework\Phrase(
  677. 'An error occurred during "%1" fileClose execution.',
  678. [$this->getWarningMessage()]
  679. )
  680. );
  681. }
  682. return $result;
  683. }
  684. /**
  685. * Writes data to file
  686. *
  687. * @param resource $resource
  688. * @param string $data
  689. * @return int
  690. * @throws FileSystemException
  691. */
  692. public function fileWrite($resource, $data)
  693. {
  694. $lenData = strlen($data);
  695. for ($result = 0; $result < $lenData; $result += $fwrite) {
  696. $fwrite = @fwrite($resource, substr($data, $result));
  697. if (0 === $fwrite) {
  698. $this->fileSystemException('Unable to write');
  699. }
  700. if (false === $fwrite) {
  701. $this->fileSystemException(
  702. 'An error occurred during "%1" fileWrite execution.',
  703. [$this->getWarningMessage()]
  704. );
  705. }
  706. }
  707. return $result;
  708. }
  709. /**
  710. * Throw a FileSystemException with a Phrase of message and optional arguments
  711. *
  712. * @param string $message
  713. * @param array $arguments
  714. * @return void
  715. * @throws FileSystemException
  716. */
  717. private function fileSystemException($message, $arguments = [])
  718. {
  719. throw new FileSystemException(new \Magento\Framework\Phrase($message, $arguments));
  720. }
  721. /**
  722. * Writes one CSV row to the file.
  723. *
  724. * @param resource $resource
  725. * @param array $data
  726. * @param string $delimiter
  727. * @param string $enclosure
  728. * @return int
  729. * @throws FileSystemException
  730. */
  731. public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure = '"')
  732. {
  733. /**
  734. * Security enhancement for CSV data processing by Excel-like applications.
  735. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
  736. *
  737. * @var $value string|\Magento\Framework\Phrase
  738. */
  739. foreach ($data as $key => $value) {
  740. if (!is_string($value)) {
  741. $value = (string)$value;
  742. }
  743. if (isset($value[0]) && in_array($value[0], ['=', '+', '-'])) {
  744. $data[$key] = ' ' . $value;
  745. }
  746. }
  747. $result = @fputcsv($resource, $data, $delimiter, $enclosure);
  748. if (!$result) {
  749. throw new FileSystemException(
  750. new \Magento\Framework\Phrase(
  751. 'An error occurred during "%1" filePutCsv execution.',
  752. [$this->getWarningMessage()]
  753. )
  754. );
  755. }
  756. return $result;
  757. }
  758. /**
  759. * Flushes the output
  760. *
  761. * @param resource $resource
  762. * @return bool
  763. * @throws FileSystemException
  764. */
  765. public function fileFlush($resource)
  766. {
  767. $result = @fflush($resource);
  768. if (!$result) {
  769. throw new FileSystemException(
  770. new \Magento\Framework\Phrase(
  771. 'An error occurred during "%1" fileFlush execution.',
  772. [$this->getWarningMessage()]
  773. )
  774. );
  775. }
  776. return $result;
  777. }
  778. /**
  779. * Lock file in selected mode
  780. *
  781. * @param resource $resource
  782. * @param int $lockMode
  783. * @return bool
  784. * @throws FileSystemException
  785. */
  786. public function fileLock($resource, $lockMode = LOCK_EX)
  787. {
  788. $result = @flock($resource, $lockMode);
  789. if (!$result) {
  790. throw new FileSystemException(
  791. new \Magento\Framework\Phrase(
  792. 'An error occurred during "%1" fileLock execution.',
  793. [$this->getWarningMessage()]
  794. )
  795. );
  796. }
  797. return $result;
  798. }
  799. /**
  800. * Unlock file
  801. *
  802. * @param resource $resource
  803. * @return bool
  804. * @throws FileSystemException
  805. */
  806. public function fileUnlock($resource)
  807. {
  808. $result = @flock($resource, LOCK_UN);
  809. if (!$result) {
  810. throw new FileSystemException(
  811. new \Magento\Framework\Phrase(
  812. 'An error occurred during "%1" fileUnlock execution.',
  813. [$this->getWarningMessage()]
  814. )
  815. );
  816. }
  817. return $result;
  818. }
  819. /**
  820. * Returns an absolute path for the given one.
  821. *
  822. * @param string $basePath
  823. * @param string $path
  824. * @param string|null $scheme
  825. * @return string
  826. */
  827. public function getAbsolutePath($basePath, $path, $scheme = null)
  828. {
  829. // check if the path given is already an absolute path containing the
  830. // basepath. so if the basepath starts at position 0 in the path, we
  831. // must not concatinate them again because path is already absolute.
  832. if (0 === strpos($path, $basePath)) {
  833. return $this->getScheme($scheme) . $path;
  834. }
  835. return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
  836. }
  837. /**
  838. * Retrieves relative path
  839. *
  840. * @param string $basePath
  841. * @param string $path
  842. * @return string
  843. */
  844. public function getRelativePath($basePath, $path = null)
  845. {
  846. $path = $this->fixSeparator($path);
  847. if (strpos($path, $basePath) === 0 || $basePath == $path . '/') {
  848. $result = substr($path, strlen($basePath));
  849. } else {
  850. $result = $path;
  851. }
  852. return $result;
  853. }
  854. /**
  855. * Fixes path separator.
  856. *
  857. * Utility method.
  858. *
  859. * @param string $path
  860. * @return string
  861. */
  862. protected function fixSeparator($path)
  863. {
  864. return str_replace('\\', '/', $path);
  865. }
  866. /**
  867. * Return path with scheme
  868. *
  869. * @param null|string $scheme
  870. * @return string
  871. */
  872. protected function getScheme($scheme = null)
  873. {
  874. return $scheme ? $scheme . '://' : '';
  875. }
  876. /**
  877. * Read directory recursively
  878. *
  879. * @param string $path
  880. * @return string[]
  881. * @throws FileSystemException
  882. */
  883. public function readDirectoryRecursively($path = null)
  884. {
  885. $result = [];
  886. $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
  887. try {
  888. $iterator = new \RecursiveIteratorIterator(
  889. new \RecursiveDirectoryIterator($path, $flags),
  890. \RecursiveIteratorIterator::CHILD_FIRST
  891. );
  892. /** @var \FilesystemIterator $file */
  893. foreach ($iterator as $file) {
  894. $result[] = $file->getPathname();
  895. }
  896. } catch (\Exception $e) {
  897. throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
  898. }
  899. return $result;
  900. }
  901. /**
  902. * Get real path
  903. *
  904. * @param string $path
  905. *
  906. * @return string|bool
  907. */
  908. public function getRealPath($path)
  909. {
  910. return realpath($path);
  911. }
  912. /**
  913. * Return correct path for link
  914. *
  915. * @param string $path
  916. * @return mixed
  917. */
  918. public function getRealPathSafety($path)
  919. {
  920. if (strpos($path, DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) === false) {
  921. return $path;
  922. }
  923. //Removing redundant directory separators.
  924. $path = preg_replace(
  925. '/\\' .DIRECTORY_SEPARATOR .'\\' .DIRECTORY_SEPARATOR .'+/',
  926. DIRECTORY_SEPARATOR,
  927. $path
  928. );
  929. $pathParts = explode(DIRECTORY_SEPARATOR, $path);
  930. $realPath = [];
  931. foreach ($pathParts as $pathPart) {
  932. if ($pathPart == '.') {
  933. continue;
  934. }
  935. if ($pathPart == '..') {
  936. array_pop($realPath);
  937. continue;
  938. }
  939. $realPath[] = $pathPart;
  940. }
  941. return implode(DIRECTORY_SEPARATOR, $realPath);
  942. }
  943. }