|
1 <?php |
|
2 /** |
|
3 * WordPress FTP Sockets Filesystem. |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Filesystem |
|
7 */ |
|
8 |
|
9 /** |
|
10 * WordPress Filesystem Class for implementing FTP Sockets. |
|
11 * |
|
12 * @since 2.5 |
|
13 * @package WordPress |
|
14 * @subpackage Filesystem |
|
15 * @uses WP_Filesystem_Base Extends class |
|
16 */ |
|
17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { |
|
18 var $ftp = false; |
|
19 var $errors = null; |
|
20 var $options = array(); |
|
21 |
|
22 function __construct($opt = '') { |
|
23 $this->method = 'ftpsockets'; |
|
24 $this->errors = new WP_Error(); |
|
25 |
|
26 // Check if possible to use ftp functions. |
|
27 if ( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) |
|
28 return false; |
|
29 $this->ftp = new ftp(); |
|
30 |
|
31 if ( empty($opt['port']) ) |
|
32 $this->options['port'] = 21; |
|
33 else |
|
34 $this->options['port'] = $opt['port']; |
|
35 |
|
36 if ( empty($opt['hostname']) ) |
|
37 $this->errors->add('empty_hostname', __('FTP hostname is required')); |
|
38 else |
|
39 $this->options['hostname'] = $opt['hostname']; |
|
40 |
|
41 if ( ! empty($opt['base']) ) |
|
42 $this->wp_base = $opt['base']; |
|
43 |
|
44 // Check if the options provided are OK. |
|
45 if ( empty ($opt['username']) ) |
|
46 $this->errors->add('empty_username', __('FTP username is required')); |
|
47 else |
|
48 $this->options['username'] = $opt['username']; |
|
49 |
|
50 if ( empty ($opt['password']) ) |
|
51 $this->errors->add('empty_password', __('FTP password is required')); |
|
52 else |
|
53 $this->options['password'] = $opt['password']; |
|
54 } |
|
55 |
|
56 function connect() { |
|
57 if ( ! $this->ftp ) |
|
58 return false; |
|
59 |
|
60 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT); |
|
61 |
|
62 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) { |
|
63 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
|
64 return false; |
|
65 } |
|
66 |
|
67 if ( ! $this->ftp->connect() ) { |
|
68 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
|
69 return false; |
|
70 } |
|
71 |
|
72 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) { |
|
73 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); |
|
74 return false; |
|
75 } |
|
76 |
|
77 $this->ftp->SetType( FTP_BINARY ); |
|
78 $this->ftp->Passive( true ); |
|
79 $this->ftp->setTimeout( FS_TIMEOUT ); |
|
80 return true; |
|
81 } |
|
82 |
|
83 function get_contents( $file ) { |
|
84 if ( ! $this->exists($file) ) |
|
85 return false; |
|
86 |
|
87 $temp = wp_tempnam( $file ); |
|
88 |
|
89 if ( ! $temphandle = fopen($temp, 'w+') ) |
|
90 return false; |
|
91 |
|
92 mbstring_binary_safe_encoding(); |
|
93 |
|
94 if ( ! $this->ftp->fget($temphandle, $file) ) { |
|
95 fclose($temphandle); |
|
96 unlink($temp); |
|
97 |
|
98 reset_mbstring_encoding(); |
|
99 |
|
100 return ''; // Blank document, File does exist, It's just blank. |
|
101 } |
|
102 |
|
103 reset_mbstring_encoding(); |
|
104 |
|
105 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to |
|
106 $contents = ''; |
|
107 |
|
108 while ( ! feof($temphandle) ) |
|
109 $contents .= fread($temphandle, 8192); |
|
110 |
|
111 fclose($temphandle); |
|
112 unlink($temp); |
|
113 return $contents; |
|
114 } |
|
115 |
|
116 function get_contents_array($file) { |
|
117 return explode("\n", $this->get_contents($file) ); |
|
118 } |
|
119 |
|
120 function put_contents($file, $contents, $mode = false ) { |
|
121 $temp = wp_tempnam( $file ); |
|
122 if ( ! $temphandle = @fopen($temp, 'w+') ) { |
|
123 unlink($temp); |
|
124 return false; |
|
125 } |
|
126 |
|
127 // The FTP class uses string functions internally during file download/upload |
|
128 mbstring_binary_safe_encoding(); |
|
129 |
|
130 $bytes_written = fwrite( $temphandle, $contents ); |
|
131 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) { |
|
132 fclose( $temphandle ); |
|
133 unlink( $temp ); |
|
134 |
|
135 reset_mbstring_encoding(); |
|
136 |
|
137 return false; |
|
138 } |
|
139 |
|
140 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to |
|
141 |
|
142 $ret = $this->ftp->fput($file, $temphandle); |
|
143 |
|
144 reset_mbstring_encoding(); |
|
145 |
|
146 fclose($temphandle); |
|
147 unlink($temp); |
|
148 |
|
149 $this->chmod($file, $mode); |
|
150 |
|
151 return $ret; |
|
152 } |
|
153 |
|
154 function cwd() { |
|
155 $cwd = $this->ftp->pwd(); |
|
156 if ( $cwd ) |
|
157 $cwd = trailingslashit($cwd); |
|
158 return $cwd; |
|
159 } |
|
160 |
|
161 function chdir($file) { |
|
162 return $this->ftp->chdir($file); |
|
163 } |
|
164 |
|
165 function chgrp($file, $group, $recursive = false ) { |
|
166 return false; |
|
167 } |
|
168 |
|
169 function chmod($file, $mode = false, $recursive = false ) { |
|
170 if ( ! $mode ) { |
|
171 if ( $this->is_file($file) ) |
|
172 $mode = FS_CHMOD_FILE; |
|
173 elseif ( $this->is_dir($file) ) |
|
174 $mode = FS_CHMOD_DIR; |
|
175 else |
|
176 return false; |
|
177 } |
|
178 |
|
179 // chmod any sub-objects if recursive. |
|
180 if ( $recursive && $this->is_dir($file) ) { |
|
181 $filelist = $this->dirlist($file); |
|
182 foreach ( (array)$filelist as $filename => $filemeta ) |
|
183 $this->chmod($file . '/' . $filename, $mode, $recursive); |
|
184 } |
|
185 |
|
186 // chmod the file or directory |
|
187 return $this->ftp->chmod($file, $mode); |
|
188 } |
|
189 |
|
190 function owner($file) { |
|
191 $dir = $this->dirlist($file); |
|
192 return $dir[$file]['owner']; |
|
193 } |
|
194 |
|
195 function getchmod($file) { |
|
196 $dir = $this->dirlist($file); |
|
197 return $dir[$file]['permsn']; |
|
198 } |
|
199 |
|
200 function group($file) { |
|
201 $dir = $this->dirlist($file); |
|
202 return $dir[$file]['group']; |
|
203 } |
|
204 |
|
205 function copy($source, $destination, $overwrite = false, $mode = false) { |
|
206 if ( ! $overwrite && $this->exists($destination) ) |
|
207 return false; |
|
208 |
|
209 $content = $this->get_contents($source); |
|
210 if ( false === $content ) |
|
211 return false; |
|
212 |
|
213 return $this->put_contents($destination, $content, $mode); |
|
214 } |
|
215 |
|
216 function move($source, $destination, $overwrite = false ) { |
|
217 return $this->ftp->rename($source, $destination); |
|
218 } |
|
219 |
|
220 function delete($file, $recursive = false, $type = false) { |
|
221 if ( empty($file) ) |
|
222 return false; |
|
223 if ( 'f' == $type || $this->is_file($file) ) |
|
224 return $this->ftp->delete($file); |
|
225 if ( !$recursive ) |
|
226 return $this->ftp->rmdir($file); |
|
227 |
|
228 return $this->ftp->mdel($file); |
|
229 } |
|
230 |
|
231 function exists( $file ) { |
|
232 $list = $this->ftp->nlist( $file ); |
|
233 return !empty( $list ); //empty list = no file, so invert. |
|
234 // return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server |
|
235 } |
|
236 |
|
237 function is_file($file) { |
|
238 if ( $this->is_dir($file) ) |
|
239 return false; |
|
240 if ( $this->exists($file) ) |
|
241 return true; |
|
242 return false; |
|
243 } |
|
244 |
|
245 function is_dir($path) { |
|
246 $cwd = $this->cwd(); |
|
247 if ( $this->chdir($path) ) { |
|
248 $this->chdir($cwd); |
|
249 return true; |
|
250 } |
|
251 return false; |
|
252 } |
|
253 |
|
254 function is_readable($file) { |
|
255 return true; |
|
256 } |
|
257 |
|
258 function is_writable($file) { |
|
259 return true; |
|
260 } |
|
261 |
|
262 function atime($file) { |
|
263 return false; |
|
264 } |
|
265 |
|
266 function mtime($file) { |
|
267 return $this->ftp->mdtm($file); |
|
268 } |
|
269 |
|
270 function size($file) { |
|
271 return $this->ftp->filesize($file); |
|
272 } |
|
273 |
|
274 function touch($file, $time = 0, $atime = 0 ) { |
|
275 return false; |
|
276 } |
|
277 |
|
278 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) { |
|
279 $path = untrailingslashit($path); |
|
280 if ( empty($path) ) |
|
281 return false; |
|
282 |
|
283 if ( ! $this->ftp->mkdir($path) ) |
|
284 return false; |
|
285 if ( ! $chmod ) |
|
286 $chmod = FS_CHMOD_DIR; |
|
287 $this->chmod($path, $chmod); |
|
288 if ( $chown ) |
|
289 $this->chown($path, $chown); |
|
290 if ( $chgrp ) |
|
291 $this->chgrp($path, $chgrp); |
|
292 return true; |
|
293 } |
|
294 |
|
295 function rmdir($path, $recursive = false ) { |
|
296 $this->delete($path, $recursive); |
|
297 } |
|
298 |
|
299 function dirlist($path = '.', $include_hidden = true, $recursive = false ) { |
|
300 if ( $this->is_file($path) ) { |
|
301 $limit_file = basename($path); |
|
302 $path = dirname($path) . '/'; |
|
303 } else { |
|
304 $limit_file = false; |
|
305 } |
|
306 |
|
307 mbstring_binary_safe_encoding(); |
|
308 |
|
309 $list = $this->ftp->dirlist($path); |
|
310 if ( empty( $list ) && ! $this->exists( $path ) ) { |
|
311 |
|
312 reset_mbstring_encoding(); |
|
313 |
|
314 return false; |
|
315 } |
|
316 |
|
317 $ret = array(); |
|
318 foreach ( $list as $struc ) { |
|
319 |
|
320 if ( '.' == $struc['name'] || '..' == $struc['name'] ) |
|
321 continue; |
|
322 |
|
323 if ( ! $include_hidden && '.' == $struc['name'][0] ) |
|
324 continue; |
|
325 |
|
326 if ( $limit_file && $struc['name'] != $limit_file ) |
|
327 continue; |
|
328 |
|
329 if ( 'd' == $struc['type'] ) { |
|
330 if ( $recursive ) |
|
331 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); |
|
332 else |
|
333 $struc['files'] = array(); |
|
334 } |
|
335 |
|
336 // Replace symlinks formatted as "source -> target" with just the source name |
|
337 if ( $struc['islink'] ) |
|
338 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] ); |
|
339 |
|
340 $ret[ $struc['name'] ] = $struc; |
|
341 } |
|
342 |
|
343 reset_mbstring_encoding(); |
|
344 |
|
345 return $ret; |
|
346 } |
|
347 |
|
348 function __destruct() { |
|
349 $this->ftp->quit(); |
|
350 } |
|
351 } |