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.

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