FTPExtension.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Drupal\Core\FileTransfer;
  3. /**
  4. * Defines a file transfer class using the PHP FTP extension.
  5. */
  6. class FTPExtension extends FTP implements ChmodInterface {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public function connect() {
  11. $this->connection = ftp_connect($this->hostname, $this->port);
  12. if (!$this->connection) {
  13. throw new FileTransferException("Cannot connect to FTP Server, check settings");
  14. }
  15. if (!ftp_login($this->connection, $this->username, $this->password)) {
  16. throw new FileTransferException("Cannot log in to FTP server. Check username and password");
  17. }
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function copyFileJailed($source, $destination) {
  23. if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) {
  24. throw new FileTransferException("Cannot move @source to @destination", NULL, ["@source" => $source, "@destination" => $destination]);
  25. }
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function createDirectoryJailed($directory) {
  31. if (!ftp_mkdir($this->connection, $directory)) {
  32. throw new FileTransferException("Cannot create directory @directory", NULL, ["@directory" => $directory]);
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function removeDirectoryJailed($directory) {
  39. $pwd = ftp_pwd($this->connection);
  40. if (!ftp_chdir($this->connection, $directory)) {
  41. throw new FileTransferException("Unable to change the current directory to @directory", NULL, ['@directory' => $directory]);
  42. }
  43. $list = @ftp_nlist($this->connection, '.');
  44. if (!$list) {
  45. $list = [];
  46. }
  47. foreach ($list as $item) {
  48. if ($item == '.' || $item == '..') {
  49. continue;
  50. }
  51. if (@ftp_chdir($this->connection, $item)) {
  52. ftp_cdup($this->connection);
  53. $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item);
  54. }
  55. else {
  56. $this->removeFile(ftp_pwd($this->connection) . '/' . $item);
  57. }
  58. }
  59. ftp_chdir($this->connection, $pwd);
  60. if (!ftp_rmdir($this->connection, $directory)) {
  61. throw new FileTransferException("Unable to remove the directory @directory", NULL, ['@directory' => $directory]);
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function removeFileJailed($destination) {
  68. if (!ftp_delete($this->connection, $destination)) {
  69. throw new FileTransferException("Unable to remove the file @file", NULL, ['@file' => $destination]);
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function isDirectory($path) {
  76. $result = FALSE;
  77. $curr = ftp_pwd($this->connection);
  78. if (@ftp_chdir($this->connection, $path)) {
  79. $result = TRUE;
  80. }
  81. ftp_chdir($this->connection, $curr);
  82. return $result;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function isFile($path) {
  88. return ftp_size($this->connection, $path) != -1;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function chmodJailed($path, $mode, $recursive) {
  94. if (!ftp_chmod($this->connection, $mode, $path)) {
  95. throw new FileTransferException("Unable to set permissions on %file", NULL, ['%file' => $path]);
  96. }
  97. if ($this->isDirectory($path) && $recursive) {
  98. $filelist = @ftp_nlist($this->connection, $path);
  99. if (!$filelist) {
  100. // empty directory - returns false
  101. return;
  102. }
  103. foreach ($filelist as $file) {
  104. $this->chmodJailed($file, $mode, $recursive);
  105. }
  106. }
  107. }
  108. }