ftp.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Base class for FTP implementations.
  4. */
  5. abstract class FileTransferFTP extends FileTransfer {
  6. public function __construct($jail, $username, $password, $hostname, $port) {
  7. $this->username = $username;
  8. $this->password = $password;
  9. $this->hostname = $hostname;
  10. $this->port = $port;
  11. parent::__construct($jail);
  12. }
  13. /**
  14. * Return an object which can implement the FTP protocol.
  15. *
  16. * @param string $jail
  17. * @param array $settings
  18. * @return FileTransferFTP
  19. * The appropriate FileTransferFTP subclass based on the available
  20. * options. If the FTP PHP extension is available, use it.
  21. */
  22. static function factory($jail, $settings) {
  23. $username = empty($settings['username']) ? '' : $settings['username'];
  24. $password = empty($settings['password']) ? '' : $settings['password'];
  25. $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
  26. $port = empty($settings['advanced']['port']) ? 21 : $settings['advanced']['port'];
  27. if (function_exists('ftp_connect')) {
  28. $class = 'FileTransferFTPExtension';
  29. }
  30. else {
  31. throw new FileTransferException('No FTP backend available.');
  32. }
  33. return new $class($jail, $username, $password, $hostname, $port);
  34. }
  35. /**
  36. * Returns the form to configure the FileTransfer class for FTP.
  37. */
  38. public function getSettingsForm() {
  39. $form = parent::getSettingsForm();
  40. $form['advanced']['port']['#default_value'] = 21;
  41. return $form;
  42. }
  43. }
  44. class FileTransferFTPExtension extends FileTransferFTP implements FileTransferChmodInterface {
  45. public function connect() {
  46. $this->connection = ftp_connect($this->hostname, $this->port);
  47. if (!$this->connection) {
  48. throw new FileTransferException("Cannot connect to FTP Server, check settings");
  49. }
  50. if (!ftp_login($this->connection, $this->username, $this->password)) {
  51. throw new FileTransferException("Cannot log in to FTP server. Check username and password");
  52. }
  53. }
  54. protected function copyFileJailed($source, $destination) {
  55. if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) {
  56. throw new FileTransferException("Cannot move @source to @destination", NULL, array("@source" => $source, "@destination" => $destination));
  57. }
  58. }
  59. protected function createDirectoryJailed($directory) {
  60. if (!ftp_mkdir($this->connection, $directory)) {
  61. throw new FileTransferException("Cannot create directory @directory", NULL, array("@directory" => $directory));
  62. }
  63. }
  64. protected function removeDirectoryJailed($directory) {
  65. $pwd = ftp_pwd($this->connection);
  66. if (!ftp_chdir($this->connection, $directory)) {
  67. throw new FileTransferException("Unable to change to directory @directory", NULL, array('@directory' => $directory));
  68. }
  69. $list = @ftp_nlist($this->connection, '.');
  70. if (!$list) {
  71. $list = array();
  72. }
  73. foreach ($list as $item) {
  74. if ($item == '.' || $item == '..') {
  75. continue;
  76. }
  77. if (@ftp_chdir($this->connection, $item)) {
  78. ftp_cdup($this->connection);
  79. $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item);
  80. }
  81. else {
  82. $this->removeFile(ftp_pwd($this->connection) . '/' . $item);
  83. }
  84. }
  85. ftp_chdir($this->connection, $pwd);
  86. if (!ftp_rmdir($this->connection, $directory)) {
  87. throw new FileTransferException("Unable to remove to directory @directory", NULL, array('@directory' => $directory));
  88. }
  89. }
  90. protected function removeFileJailed($destination) {
  91. if (!ftp_delete($this->connection, $destination)) {
  92. throw new FileTransferException("Unable to remove to file @file", NULL, array('@file' => $destination));
  93. }
  94. }
  95. public function isDirectory($path) {
  96. $result = FALSE;
  97. $curr = ftp_pwd($this->connection);
  98. if (@ftp_chdir($this->connection, $path)) {
  99. $result = TRUE;
  100. }
  101. ftp_chdir($this->connection, $curr);
  102. return $result;
  103. }
  104. public function isFile($path) {
  105. return ftp_size($this->connection, $path) != -1;
  106. }
  107. function chmodJailed($path, $mode, $recursive) {
  108. if (!ftp_chmod($this->connection, $mode, $path)) {
  109. throw new FileTransferException("Unable to set permissions on %file", NULL, array('%file' => $path));
  110. }
  111. if ($this->isDirectory($path) && $recursive) {
  112. $filelist = @ftp_nlist($this->connection, $path);
  113. if (!$filelist) {
  114. //empty directory - returns false
  115. return;
  116. }
  117. foreach ($filelist as $file) {
  118. $this->chmodJailed($file, $mode, $recursive);
  119. }
  120. }
  121. }
  122. }
  123. if (!function_exists('ftp_chmod')) {
  124. function ftp_chmod($ftp_stream, $mode, $filename) {
  125. return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
  126. }
  127. }