c5f1b22b30d5644bb1a47a19b6e5c6284aed7121
[yaffs-website] / web / core / lib / Drupal / Core / FileTransfer / FTPExtension.php
1 <?php
2
3 namespace Drupal\Core\FileTransfer;
4
5 /**
6  * Defines a file transfer class using the PHP FTP extension.
7  */
8 class FTPExtension extends FTP implements ChmodInterface {
9
10   /**
11    * {@inheritdoc}
12    */
13   public function connect() {
14     $this->connection = ftp_connect($this->hostname, $this->port);
15
16     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 to directory @directory", NULL, ['@directory' => $directory]);
49     }
50     $list = @ftp_nlist($this->connection, '.');
51     if (!$list) {
52       $list = [];
53     }
54     foreach ($list as $item) {
55       if ($item == '.' || $item == '..') {
56         continue;
57       }
58       if (@ftp_chdir($this->connection, $item)) {
59         ftp_cdup($this->connection);
60         $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 to directory @directory", NULL, ['@directory' => $directory]);
69     }
70   }
71
72   /**
73    * {@inheritdoc}
74    */
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]);
78     }
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function isDirectory($path) {
85     $result = FALSE;
86     $curr = ftp_pwd($this->connection);
87     if (@ftp_chdir($this->connection, $path)) {
88       $result = TRUE;
89     }
90     ftp_chdir($this->connection, $curr);
91     return $result;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function isFile($path) {
98     return ftp_size($this->connection, $path) != -1;
99   }
100
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     if ($this->isDirectory($path) && $recursive) {
109       $filelist = @ftp_nlist($this->connection, $path);
110       if (!$filelist) {
111         //empty directory - returns false
112         return;
113       }
114       foreach ($filelist as $file) {
115         $this->chmodJailed($file, $mode, $recursive);
116       }
117     }
118   }
119
120 }