1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bart Visscher <[email protected]> |
4
|
|
|
* @author Felix Moeller <[email protected]> |
5
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
6
|
|
|
* @author Michael Gapczynski <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* @author Philipp Kapfer <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Robin McCorkell <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
12
|
|
|
* @author Vincent Petry <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright (c) 2016, 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
|
|
|
|
31
|
|
|
namespace OCA\Files_External\Lib\Storage; |
32
|
|
|
|
33
|
|
|
use Icewind\Streams\RetryWrapper; |
34
|
|
|
|
35
|
|
|
class FTP extends StreamWrapper{ |
36
|
|
|
private $password; |
37
|
|
|
private $user; |
38
|
|
|
private $host; |
39
|
|
|
private $secure; |
40
|
|
|
private $root; |
41
|
|
|
|
42
|
|
|
private static $tempFiles=array(); |
43
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getId(){ |
68
|
|
|
return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* construct the ftp url |
73
|
|
|
* @param string $path |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function constructUrl($path) { |
77
|
|
|
$url='ftp'; |
78
|
|
|
if ($this->secure) { |
79
|
|
|
$url.='s'; |
80
|
|
|
} |
81
|
|
|
$url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
82
|
|
|
return $url; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Unlinks file or directory |
87
|
|
|
* @param string $path |
88
|
|
|
*/ |
89
|
|
|
public function unlink($path) { |
90
|
|
|
if ($this->is_dir($path)) { |
91
|
|
|
return $this->rmdir($path); |
92
|
|
|
} |
93
|
|
|
else { |
94
|
|
|
$url = $this->constructUrl($path); |
95
|
|
|
$result = unlink($url); |
96
|
|
|
clearstatcache(true, $url); |
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
public function fopen($path,$mode) { |
101
|
|
|
switch($mode) { |
102
|
|
|
case 'r': |
103
|
|
|
case 'rb': |
104
|
|
|
case 'w': |
105
|
|
|
case 'wb': |
106
|
|
|
case 'a': |
107
|
|
View Code Duplication |
case 'ab': |
|
|
|
|
108
|
|
|
//these are supported by the wrapper |
109
|
|
|
$context = stream_context_create(array('ftp' => array('overwrite' => true))); |
110
|
|
|
$handle = fopen($this->constructUrl($path), $mode, false, $context); |
111
|
|
|
return RetryWrapper::wrap($handle); |
112
|
|
|
case 'r+': |
113
|
|
|
case 'w+': |
114
|
|
|
case 'wb+': |
115
|
|
|
case 'a+': |
116
|
|
|
case 'x': |
117
|
|
|
case 'x+': |
118
|
|
|
case 'c': |
119
|
|
View Code Duplication |
case 'c+': |
|
|
|
|
120
|
|
|
//emulate these |
121
|
|
|
if (strrpos($path, '.')!==false) { |
122
|
|
|
$ext=substr($path, strrpos($path, '.')); |
123
|
|
|
} else { |
124
|
|
|
$ext=''; |
125
|
|
|
} |
126
|
|
|
$tmpFile=\OCP\Files::tmpFile($ext); |
127
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
128
|
|
|
if ($this->file_exists($path)) { |
129
|
|
|
$this->getFile($path, $tmpFile); |
130
|
|
|
} |
131
|
|
|
self::$tempFiles[$tmpFile]=$path; |
132
|
|
|
return fopen('close://'.$tmpFile, $mode); |
133
|
|
|
} |
134
|
|
|
return false; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function writeBack($tmpFile) { |
138
|
|
View Code Duplication |
if (isset(self::$tempFiles[$tmpFile])) { |
|
|
|
|
139
|
|
|
$this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]); |
140
|
|
|
unlink($tmpFile); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* check if php-ftp is installed |
146
|
|
|
*/ |
147
|
|
|
public static function checkDependencies() { |
148
|
|
|
if (function_exists('ftp_login')) { |
149
|
|
|
return(true); |
150
|
|
|
} else { |
151
|
|
|
return array('ftp'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.