123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace Drupal\Core\FileTransfer;
- use Drupal\Core\DependencyInjection\DependencySerializationTrait;
- use Drupal\Core\File\FileSystemInterface;
- /**
- * Defines the local connection class for copying files as the httpd user.
- */
- class Local extends FileTransfer implements ChmodInterface {
- use DependencySerializationTrait;
- /**
- * The file system service.
- *
- * @var \Drupal\Core\File\FileSystemInterface
- */
- protected $fileSystem;
- /**
- * {@inheritdoc}
- */
- public function __construct($jail, FileSystemInterface $file_system = NULL) {
- parent::__construct($jail);
- if (!isset($file_system)) {
- @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);
- $file_system = \Drupal::service('file_system');
- }
- $this->fileSystem = $file_system;
- }
- /**
- * {@inheritdoc}
- */
- public function connect() {
- // No-op
- }
- /**
- * {@inheritdoc}
- */
- public static function factory($jail, $settings) {
- return new Local($jail, \Drupal::service('file_system'));
- }
- /**
- * {@inheritdoc}
- */
- protected function copyFileJailed($source, $destination) {
- if (@!copy($source, $destination)) {
- throw new FileTransferException('Cannot copy %source to %destination.', NULL, ['%source' => $source, '%destination' => $destination]);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function createDirectoryJailed($directory) {
- if (!is_dir($directory) && @!mkdir($directory, 0777, TRUE)) {
- throw new FileTransferException('Cannot create directory %directory.', NULL, ['%directory' => $directory]);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function removeDirectoryJailed($directory) {
- if (!is_dir($directory)) {
- // Programmer error assertion, not something we expect users to see.
- throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, ['%directory' => $directory]);
- }
- /** @var \Drupal\Core\File\FileSystemInterface $file_system */
- $file_system = \Drupal::service('file_system');
- foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
- if ($file->isDir()) {
- if (@!$file_system->rmdir($filename)) {
- throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $filename]);
- }
- }
- elseif ($file->isFile()) {
- if (@!$this->fileSystem->unlink($filename)) {
- throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $filename]);
- }
- }
- }
- if (@!$file_system->rmdir($directory)) {
- throw new FileTransferException('Cannot remove directory %directory.', NULL, ['%directory' => $directory]);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function removeFileJailed($file) {
- if (@!$this->fileSystem->unlink($file)) {
- throw new FileTransferException('Cannot remove file %file.', NULL, ['%file' => $file]);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function isDirectory($path) {
- return is_dir($path);
- }
- /**
- * {@inheritdoc}
- */
- public function isFile($path) {
- return is_file($path);
- }
- /**
- * {@inheritdoc}
- */
- public function chmodJailed($path, $mode, $recursive) {
- if ($recursive && is_dir($path)) {
- foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
- if (@!chmod($filename, $mode)) {
- throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $filename]);
- }
- }
- }
- elseif (@!chmod($path, $mode)) {
- throw new FileTransferException('Cannot chmod %path.', NULL, ['%path' => $path]);
- }
- }
- }
|