3 namespace Drupal\Core\FileTransfer;
6 * Defines a file transfer class using the PHP FTP extension.
8 class FTPExtension extends FTP implements ChmodInterface {
13 public function connect() {
14 $this->connection = ftp_connect($this->hostname, $this->port);
16 if (!$this->connection) {
17 throw new FileTransferException("Cannot connect to FTP Server, check settings");
19 if (!ftp_login($this->connection, $this->username, $this->password)) {
20 throw new FileTransferException("Cannot log in to FTP server. Check username and password");
27 protected function copyFileJailed($source, $destination) {
28 if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) {
29 throw new FileTransferException("Cannot move @source to @destination", NULL, ["@source" => $source, "@destination" => $destination]);
36 protected function createDirectoryJailed($directory) {
37 if (!ftp_mkdir($this->connection, $directory)) {
38 throw new FileTransferException("Cannot create directory @directory", NULL, ["@directory" => $directory]);
45 protected function removeDirectoryJailed($directory) {
46 $pwd = ftp_pwd($this->connection);
47 if (!ftp_chdir($this->connection, $directory)) {
48 throw new FileTransferException("Unable to change to directory @directory", NULL, ['@directory' => $directory]);
50 $list = @ftp_nlist($this->connection, '.');
54 foreach ($list as $item) {
55 if ($item == '.' || $item == '..') {
58 if (@ftp_chdir($this->connection, $item)) {
59 ftp_cdup($this->connection);
60 $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item);
63 $this->removeFile(ftp_pwd($this->connection) . '/' . $item);
66 ftp_chdir($this->connection, $pwd);
67 if (!ftp_rmdir($this->connection, $directory)) {
68 throw new FileTransferException("Unable to remove to directory @directory", NULL, ['@directory' => $directory]);
75 protected function removeFileJailed($destination) {
76 if (!ftp_delete($this->connection, $destination)) {
77 throw new FileTransferException("Unable to remove to file @file", NULL, ['@file' => $destination]);
84 public function isDirectory($path) {
86 $curr = ftp_pwd($this->connection);
87 if (@ftp_chdir($this->connection, $path)) {
90 ftp_chdir($this->connection, $curr);
97 public function isFile($path) {
98 return ftp_size($this->connection, $path) != -1;
104 public function chmodJailed($path, $mode, $recursive) {
105 if (!ftp_chmod($this->connection, $mode, $path)) {
106 throw new FileTransferException("Unable to set permissions on %file", NULL, ['%file' => $path]);
108 if ($this->isDirectory($path) && $recursive) {
109 $filelist = @ftp_nlist($this->connection, $path);
111 //empty directory - returns false
114 foreach ($filelist as $file) {
115 $this->chmodJailed($file, $mode, $recursive);