View source
<?php
namespace Drupal\Core\FileTransfer;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\File\FileSystemInterface;
class Local extends FileTransfer implements ChmodInterface {
use DependencySerializationTrait;
protected $fileSystem;
public function __construct($jail, FileSystemInterface $file_system) {
parent::__construct($jail);
$this->fileSystem = $file_system;
}
public function connect() {
}
public static function factory($jail, $settings) {
return new Local($jail, \Drupal::service('file_system'));
}
protected function copyFileJailed($source, $destination) {
if (@(!copy($source, $destination))) {
throw new FileTransferException('Cannot copy %source to %destination.', 0, [
'%source' => $source,
'%destination' => $destination,
]);
}
}
protected function createDirectoryJailed($directory) {
if (!is_dir($directory) && @(!mkdir($directory, 0777, TRUE))) {
throw new FileTransferException('Cannot create directory %directory.', 0, [
'%directory' => $directory,
]);
}
}
protected function removeDirectoryJailed($directory) {
if (!is_dir($directory)) {
throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', 0, [
'%directory' => $directory,
]);
}
$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.', 0, [
'%directory' => $filename,
]);
}
}
elseif ($file
->isFile()) {
if (@(!$this->fileSystem
->unlink($filename))) {
throw new FileTransferException('Cannot remove file %file.', 0, [
'%file' => $filename,
]);
}
}
}
if (@(!$file_system
->rmdir($directory))) {
throw new FileTransferException('Cannot remove directory %directory.', 0, [
'%directory' => $directory,
]);
}
}
protected function removeFileJailed($file) {
if (@(!$this->fileSystem
->unlink($file))) {
throw new FileTransferException('Cannot remove file %file.', 0, [
'%file' => $file,
]);
}
}
public function isDirectory($path) {
return is_dir($path);
}
public function isFile($path) {
return is_file($path);
}
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.', 0, [
'%path' => $filename,
]);
}
}
}
elseif (@(!chmod($path, $mode))) {
throw new FileTransferException('Cannot chmod %path.', 0, [
'%path' => $path,
]);
}
}
}