FTPExtension.php 3.33 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\FileTransfer;
4

5 6 7 8
/**
 * Defines a file transfer class using the PHP FTP extension.
 */
class FTPExtension extends FTP implements ChmodInterface {
9 10

  /**
11
   * {@inheritdoc}
12
   */
13
  public function connect() {
14 15 16
    $this->connection = ftp_connect($this->hostname, $this->port);

    if (!$this->connection) {
17
      throw new FileTransferException("Cannot connect to FTP Server, check settings");
18 19
    }
    if (!ftp_login($this->connection, $this->username, $this->password)) {
20
      throw new FileTransferException("Cannot log in to FTP server. Check username and password");
21 22
    }
  }
23

24
  /**
25
   * {@inheritdoc}
26
   */
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]);
30 31 32
    }
  }

33
  /**
34
   * {@inheritdoc}
35
   */
36
  protected function createDirectoryJailed($directory) {
37
    if (!ftp_mkdir($this->connection, $directory)) {
38
      throw new FileTransferException("Cannot create directory @directory", NULL, ["@directory" => $directory]);
39 40 41
    }
  }

42
  /**
43
   * {@inheritdoc}
44
   */
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 the current directory to @directory", NULL, ['@directory' => $directory]);
49 50
    }
    $list = @ftp_nlist($this->connection, '.');
51
    if (!$list) {
52
      $list = [];
53
    }
54
    foreach ($list as $item) {
55 56 57
      if ($item == '.' || $item == '..') {
        continue;
      }
58
      if (@ftp_chdir($this->connection, $item)) {
59 60
        ftp_cdup($this->connection);
        $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item);
61 62
      }
      else {
63
        $this->removeFile(ftp_pwd($this->connection) . '/' . $item);
64 65 66
      }
    }
    ftp_chdir($this->connection, $pwd);
67
    if (!ftp_rmdir($this->connection, $directory)) {
68
      throw new FileTransferException("Unable to remove the directory @directory", NULL, ['@directory' => $directory]);
69 70 71
    }
  }

72
  /**
73
   * {@inheritdoc}
74
   */
75 76
  protected function removeFileJailed($destination) {
    if (!ftp_delete($this->connection, $destination)) {
77
      throw new FileTransferException("Unable to remove the file @file", NULL, ['@file' => $destination]);
78 79
    }
  }
80

81
  /**
82
   * {@inheritdoc}
83
   */
84 85 86
  public function isDirectory($path) {
    $result = FALSE;
    $curr = ftp_pwd($this->connection);
87
    if (@ftp_chdir($this->connection, $path)) {
88
      $result = TRUE;
89
    }
90 91
    ftp_chdir($this->connection, $curr);
    return $result;
92
  }
93

94
  /**
95
   * {@inheritdoc}
96
   */
97 98 99 100
  public function isFile($path) {
    return ftp_size($this->connection, $path) != -1;
  }

101
  /**
102
   * {@inheritdoc}
103
   */
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]);
107 108 109 110
    }
    if ($this->isDirectory($path) && $recursive) {
      $filelist = @ftp_nlist($this->connection, $path);
      if (!$filelist) {
111
        // empty directory - returns false
112 113 114 115 116 117 118
        return;
      }
      foreach ($filelist as $file) {
        $this->chmodJailed($file, $mode, $recursive);
      }
    }
  }
119

120
}