Local.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Drupal\Core\FileTransfer;
  3. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  4. use Drupal\Core\File\FileSystemInterface;
  5. /**
  6. * Defines the local connection class for copying files as the httpd user.
  7. */
  8. class Local extends FileTransfer implements ChmodInterface {
  9. use DependencySerializationTrait;
  10. /**
  11. * The file system service.
  12. *
  13. * @var \Drupal\Core\File\FileSystemInterface
  14. */
  15. protected $fileSystem;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function __construct($jail, FileSystemInterface $file_system = NULL) {
  20. parent::__construct($jail);
  21. if (!isset($file_system)) {
  22. @trigger_error('The $file_system parameter was added in Drupal 8.8.0 and will be required in 9.0.0. See https://www.drupal.org/node/3021434.', E_USER_DEPRECATED);
  23. $file_system = \Drupal::service('file_system');
  24. }
  25. $this->fileSystem = $file_system;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function connect() {
  31. // No-op
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function factory($jail, $settings) {
  37. return new Local($jail, \Drupal::service('file_system'));
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function copyFileJailed($source, $destination) {
  43. if (@!copy($source, $destination)) {
  44. throw new FileTransferException('Cannot copy %source to %destination.', NULL, ['%source' => $source, '%destination' => $destination]);
  45. }
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function createDirectoryJailed($directory) {
  51. if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
  52. throw new FileTransferException('Cannot create directory %directory.', NULL, ['%directory' => $directory]);
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function removeDirectoryJailed($directory) {
  59. if (!is_dir($directory)) {
  60. // Programmer error assertion, not something we expect users to see.
  61. throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, ['%directory' => $directory]);
  62. }
  63. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  64. $file_system = \Drupal::service('file_system');
  65. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
  66. if ($file->isDir()) {
  67. if (@!$file_system->rmdir($filename)) {
  68. throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $filename]);
  69. }
  70. }
  71. elseif ($file->isFile()) {
  72. if (@!$this->fileSystem->unlink($filename)) {
  73. throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $filename]);
  74. }
  75. }
  76. }
  77. if (@!$file_system->rmdir($directory)) {
  78. throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $directory]);
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function removeFileJailed($file) {
  85. if (@!$this->fileSystem->unlink($file)) {
  86. throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $file]);
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function isDirectory($path) {
  93. return is_dir($path);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function isFile($path) {
  99. return is_file($path);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function chmodJailed($path, $mode, $recursive) {
  105. if ($recursive && is_dir($path)) {
  106. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
  107. if (@!chmod($filename, $mode)) {
  108. throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $filename]);
  109. }
  110. }
  111. }
  112. elseif (@!chmod($path, $mode)) {
  113. throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $path]);
  114. }
  115. }
  116. }