You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ftp.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Philipp Kapfer <philipp.kapfer@gmx.at>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Files\Storage;
  31. class FTP extends \OC\Files\Storage\StreamWrapper{
  32. private $password;
  33. private $user;
  34. private $host;
  35. private $secure;
  36. private $root;
  37. private static $tempFiles=array();
  38. public function __construct($params) {
  39. if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
  40. $this->host=$params['host'];
  41. $this->user=$params['user'];
  42. $this->password=$params['password'];
  43. if (isset($params['secure'])) {
  44. if (is_string($params['secure'])) {
  45. $this->secure = ($params['secure'] === 'true');
  46. } else {
  47. $this->secure = (bool)$params['secure'];
  48. }
  49. } else {
  50. $this->secure = false;
  51. }
  52. $this->root=isset($params['root'])?$params['root']:'/';
  53. if ( ! $this->root || $this->root[0]!='/') {
  54. $this->root='/'.$this->root;
  55. }
  56. if (substr($this->root, -1) !== '/') {
  57. $this->root .= '/';
  58. }
  59. } else {
  60. throw new \Exception('Creating \OC\Files\Storage\FTP storage failed');
  61. }
  62. }
  63. public function getId(){
  64. return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root;
  65. }
  66. /**
  67. * construct the ftp url
  68. * @param string $path
  69. * @return string
  70. */
  71. public function constructUrl($path) {
  72. $url='ftp';
  73. if ($this->secure) {
  74. $url.='s';
  75. }
  76. $url.='://'.$this->user.':'.$this->password.'@'.$this->host.$this->root.$path;
  77. return $url;
  78. }
  79. /**
  80. * Unlinks file or directory
  81. * @param string $path
  82. */
  83. public function unlink($path) {
  84. if ($this->is_dir($path)) {
  85. return $this->rmdir($path);
  86. }
  87. else {
  88. $url = $this->constructUrl($path);
  89. $result = unlink($url);
  90. clearstatcache(true, $url);
  91. return $result;
  92. }
  93. }
  94. public function fopen($path,$mode) {
  95. switch($mode) {
  96. case 'r':
  97. case 'rb':
  98. case 'w':
  99. case 'wb':
  100. case 'a':
  101. case 'ab':
  102. //these are supported by the wrapper
  103. $context = stream_context_create(array('ftp' => array('overwrite' => true)));
  104. return fopen($this->constructUrl($path), $mode, false, $context);
  105. case 'r+':
  106. case 'w+':
  107. case 'wb+':
  108. case 'a+':
  109. case 'x':
  110. case 'x+':
  111. case 'c':
  112. case 'c+':
  113. //emulate these
  114. if (strrpos($path, '.')!==false) {
  115. $ext=substr($path, strrpos($path, '.'));
  116. } else {
  117. $ext='';
  118. }
  119. $tmpFile=\OCP\Files::tmpFile($ext);
  120. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  121. if ($this->file_exists($path)) {
  122. $this->getFile($path, $tmpFile);
  123. }
  124. self::$tempFiles[$tmpFile]=$path;
  125. return fopen('close://'.$tmpFile, $mode);
  126. }
  127. return false;
  128. }
  129. public function writeBack($tmpFile) {
  130. if (isset(self::$tempFiles[$tmpFile])) {
  131. $this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
  132. unlink($tmpFile);
  133. }
  134. }
  135. /**
  136. * check if php-ftp is installed
  137. */
  138. public static function checkDependencies() {
  139. if (function_exists('ftp_login')) {
  140. return(true);
  141. } else {
  142. return array('ftp');
  143. }
  144. }
  145. }