|
1 <?php |
|
2 /** |
|
3 * WordPress FTP Filesystem. |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Filesystem |
|
7 */ |
|
8 |
|
9 /** |
|
10 * WordPress Filesystem Class for implementing FTP. |
|
11 * |
|
12 * @since 2.5 |
|
13 * @package WordPress |
|
14 * @subpackage Filesystem |
|
15 * @uses WP_Filesystem_Base Extends class |
|
16 */ |
|
17 class WP_Filesystem_FTPext extends WP_Filesystem_Base { |
|
18 var $link; |
|
19 var $errors = null; |
|
20 var $options = array(); |
|
21 |
|
22 function __construct($opt='') { |
|
23 $this->method = 'ftpext'; |
|
24 $this->errors = new WP_Error(); |
|
25 |
|
26 // Check if possible to use ftp functions. |
|
27 if ( ! extension_loaded('ftp') ) { |
|
28 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available')); |
|
29 return false; |
|
30 } |
|
31 |
|
32 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis. |
|
33 |
|
34 if ( ! defined('FS_TIMEOUT') ) |
|
35 define('FS_TIMEOUT', 240); |
|
36 |
|
37 if ( empty($opt['port']) ) |
|
38 $this->options['port'] = 21; |
|
39 else |
|
40 $this->options['port'] = $opt['port']; |
|
41 |
|
42 if ( empty($opt['hostname']) ) |
|
43 $this->errors->add('empty_hostname', __('FTP hostname is required')); |
|
44 else |
|
45 $this->options['hostname'] = $opt['hostname']; |
|
46 |
|
47 if ( ! empty($opt['base']) ) |
|
48 $this->wp_base = $opt['base']; |
|
49 |
|
50 // Check if the options provided are OK. |
|
51 if ( empty($opt['username']) ) |
|
52 $this->errors->add('empty_username', __('FTP username is required')); |
|
53 else |
|
54 $this->options['username'] = $opt['username']; |
|
55 |
|
56 if ( empty($opt['password']) ) |
|
57 $this->errors->add('empty_password', __('FTP password is required')); |
|
58 else |
|
59 $this->options['password'] = $opt['password']; |
|
60 |
|
61 $this->options['ssl'] = false; |
|
62 if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] ) |
|
63 $this->options['ssl'] = true; |
|
64 } |
|
65 |
|
66 function connect() { |
|
67 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') ) |
|
68 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT); |
|
69 else |
|
70 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT); |
|
71 |
|
72 if ( ! $this->link ) { |
|
73 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
|
74 return false; |
|
75 } |
|
76 |
|
77 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) { |
|
78 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); |
|
79 return false; |
|
80 } |
|
81 |
|
82 // Set the Connection to use Passive FTP |
|
83 @ftp_pasv( $this->link, true ); |
|
84 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT ) |
|
85 @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT); |
|
86 |
|
87 return true; |
|
88 } |
|
89 |
|
90 function get_contents( $file ) { |
|
91 $tempfile = wp_tempnam($file); |
|
92 $temp = fopen($tempfile, 'w+'); |
|
93 |
|
94 if ( ! $temp ) |
|
95 return false; |
|
96 |
|
97 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) ) |
|
98 return false; |
|
99 |
|
100 fseek( $temp, 0 ); // Skip back to the start of the file being written to |
|
101 $contents = ''; |
|
102 |
|
103 while ( ! feof($temp) ) |
|
104 $contents .= fread($temp, 8192); |
|
105 |
|
106 fclose($temp); |
|
107 unlink($tempfile); |
|
108 return $contents; |
|
109 } |
|
110 |
|
111 function get_contents_array($file) { |
|
112 return explode("\n", $this->get_contents($file)); |
|
113 } |
|
114 |
|
115 function put_contents($file, $contents, $mode = false ) { |
|
116 $tempfile = wp_tempnam($file); |
|
117 $temp = fopen( $tempfile, 'wb+' ); |
|
118 if ( ! $temp ) |
|
119 return false; |
|
120 |
|
121 mbstring_binary_safe_encoding(); |
|
122 |
|
123 $data_length = strlen( $contents ); |
|
124 $bytes_written = fwrite( $temp, $contents ); |
|
125 |
|
126 reset_mbstring_encoding(); |
|
127 |
|
128 if ( $data_length !== $bytes_written ) { |
|
129 fclose( $temp ); |
|
130 unlink( $tempfile ); |
|
131 return false; |
|
132 } |
|
133 |
|
134 fseek( $temp, 0 ); // Skip back to the start of the file being written to |
|
135 |
|
136 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY ); |
|
137 |
|
138 fclose($temp); |
|
139 unlink($tempfile); |
|
140 |
|
141 $this->chmod($file, $mode); |
|
142 |
|
143 return $ret; |
|
144 } |
|
145 |
|
146 function cwd() { |
|
147 $cwd = @ftp_pwd($this->link); |
|
148 if ( $cwd ) |
|
149 $cwd = trailingslashit($cwd); |
|
150 return $cwd; |
|
151 } |
|
152 |
|
153 function chdir($dir) { |
|
154 return @ftp_chdir($this->link, $dir); |
|
155 } |
|
156 |
|
157 function chgrp($file, $group, $recursive = false ) { |
|
158 return false; |
|
159 } |
|
160 |
|
161 function chmod($file, $mode = false, $recursive = false) { |
|
162 if ( ! $mode ) { |
|
163 if ( $this->is_file($file) ) |
|
164 $mode = FS_CHMOD_FILE; |
|
165 elseif ( $this->is_dir($file) ) |
|
166 $mode = FS_CHMOD_DIR; |
|
167 else |
|
168 return false; |
|
169 } |
|
170 |
|
171 // chmod any sub-objects if recursive. |
|
172 if ( $recursive && $this->is_dir($file) ) { |
|
173 $filelist = $this->dirlist($file); |
|
174 foreach ( (array)$filelist as $filename => $filemeta ) |
|
175 $this->chmod($file . '/' . $filename, $mode, $recursive); |
|
176 } |
|
177 |
|
178 // chmod the file or directory |
|
179 if ( ! function_exists('ftp_chmod') ) |
|
180 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file)); |
|
181 return (bool)@ftp_chmod($this->link, $mode, $file); |
|
182 } |
|
183 |
|
184 function owner($file) { |
|
185 $dir = $this->dirlist($file); |
|
186 return $dir[$file]['owner']; |
|
187 } |
|
188 |
|
189 function getchmod($file) { |
|
190 $dir = $this->dirlist($file); |
|
191 return $dir[$file]['permsn']; |
|
192 } |
|
193 |
|
194 function group($file) { |
|
195 $dir = $this->dirlist($file); |
|
196 return $dir[$file]['group']; |
|
197 } |
|
198 |
|
199 function copy($source, $destination, $overwrite = false, $mode = false) { |
|
200 if ( ! $overwrite && $this->exists($destination) ) |
|
201 return false; |
|
202 $content = $this->get_contents($source); |
|
203 if ( false === $content ) |
|
204 return false; |
|
205 return $this->put_contents($destination, $content, $mode); |
|
206 } |
|
207 |
|
208 function move($source, $destination, $overwrite = false) { |
|
209 return ftp_rename($this->link, $source, $destination); |
|
210 } |
|
211 |
|
212 function delete($file, $recursive = false, $type = false) { |
|
213 if ( empty($file) ) |
|
214 return false; |
|
215 if ( 'f' == $type || $this->is_file($file) ) |
|
216 return @ftp_delete($this->link, $file); |
|
217 if ( !$recursive ) |
|
218 return @ftp_rmdir($this->link, $file); |
|
219 |
|
220 $filelist = $this->dirlist( trailingslashit($file) ); |
|
221 if ( !empty($filelist) ) |
|
222 foreach ( $filelist as $delete_file ) |
|
223 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] ); |
|
224 return @ftp_rmdir($this->link, $file); |
|
225 } |
|
226 |
|
227 function exists($file) { |
|
228 $list = @ftp_nlist($this->link, $file); |
|
229 return !empty($list); //empty list = no file, so invert. |
|
230 } |
|
231 |
|
232 function is_file($file) { |
|
233 return $this->exists($file) && !$this->is_dir($file); |
|
234 } |
|
235 |
|
236 function is_dir($path) { |
|
237 $cwd = $this->cwd(); |
|
238 $result = @ftp_chdir($this->link, trailingslashit($path) ); |
|
239 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) { |
|
240 @ftp_chdir($this->link, $cwd); |
|
241 return true; |
|
242 } |
|
243 return false; |
|
244 } |
|
245 |
|
246 function is_readable($file) { |
|
247 return true; |
|
248 } |
|
249 |
|
250 function is_writable($file) { |
|
251 return true; |
|
252 } |
|
253 |
|
254 function atime($file) { |
|
255 return false; |
|
256 } |
|
257 |
|
258 function mtime($file) { |
|
259 return ftp_mdtm($this->link, $file); |
|
260 } |
|
261 |
|
262 function size($file) { |
|
263 return ftp_size($this->link, $file); |
|
264 } |
|
265 |
|
266 function touch($file, $time = 0, $atime = 0) { |
|
267 return false; |
|
268 } |
|
269 |
|
270 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { |
|
271 $path = untrailingslashit($path); |
|
272 if ( empty($path) ) |
|
273 return false; |
|
274 |
|
275 if ( !@ftp_mkdir($this->link, $path) ) |
|
276 return false; |
|
277 $this->chmod($path, $chmod); |
|
278 if ( $chown ) |
|
279 $this->chown($path, $chown); |
|
280 if ( $chgrp ) |
|
281 $this->chgrp($path, $chgrp); |
|
282 return true; |
|
283 } |
|
284 |
|
285 function rmdir($path, $recursive = false) { |
|
286 return $this->delete($path, $recursive); |
|
287 } |
|
288 |
|
289 function parselisting($line) { |
|
290 static $is_windows; |
|
291 if ( is_null($is_windows) ) |
|
292 $is_windows = stripos( ftp_systype($this->link), 'win') !== false; |
|
293 |
|
294 if ( $is_windows && preg_match('/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer) ) { |
|
295 $b = array(); |
|
296 if ( $lucifer[3] < 70 ) |
|
297 $lucifer[3] +=2000; |
|
298 else |
|
299 $lucifer[3] += 1900; // 4digit year fix |
|
300 $b['isdir'] = ( $lucifer[7] == '<DIR>'); |
|
301 if ( $b['isdir'] ) |
|
302 $b['type'] = 'd'; |
|
303 else |
|
304 $b['type'] = 'f'; |
|
305 $b['size'] = $lucifer[7]; |
|
306 $b['month'] = $lucifer[1]; |
|
307 $b['day'] = $lucifer[2]; |
|
308 $b['year'] = $lucifer[3]; |
|
309 $b['hour'] = $lucifer[4]; |
|
310 $b['minute'] = $lucifer[5]; |
|
311 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]); |
|
312 $b['am/pm'] = $lucifer[6]; |
|
313 $b['name'] = $lucifer[8]; |
|
314 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) { |
|
315 //echo $line."\n"; |
|
316 $lcount = count($lucifer); |
|
317 if ( $lcount < 8 ) |
|
318 return ''; |
|
319 $b = array(); |
|
320 $b['isdir'] = $lucifer[0]{0} === 'd'; |
|
321 $b['islink'] = $lucifer[0]{0} === 'l'; |
|
322 if ( $b['isdir'] ) |
|
323 $b['type'] = 'd'; |
|
324 elseif ( $b['islink'] ) |
|
325 $b['type'] = 'l'; |
|
326 else |
|
327 $b['type'] = 'f'; |
|
328 $b['perms'] = $lucifer[0]; |
|
329 $b['number'] = $lucifer[1]; |
|
330 $b['owner'] = $lucifer[2]; |
|
331 $b['group'] = $lucifer[3]; |
|
332 $b['size'] = $lucifer[4]; |
|
333 if ( $lcount == 8 ) { |
|
334 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']); |
|
335 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']); |
|
336 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']); |
|
337 $b['name'] = $lucifer[7]; |
|
338 } else { |
|
339 $b['month'] = $lucifer[5]; |
|
340 $b['day'] = $lucifer[6]; |
|
341 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) { |
|
342 $b['year'] = date("Y"); |
|
343 $b['hour'] = $l2[1]; |
|
344 $b['minute'] = $l2[2]; |
|
345 } else { |
|
346 $b['year'] = $lucifer[7]; |
|
347 $b['hour'] = 0; |
|
348 $b['minute'] = 0; |
|
349 } |
|
350 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) ); |
|
351 $b['name'] = $lucifer[8]; |
|
352 } |
|
353 } |
|
354 |
|
355 // Replace symlinks formatted as "source -> target" with just the source name |
|
356 if ( $b['islink'] ) |
|
357 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] ); |
|
358 |
|
359 return $b; |
|
360 } |
|
361 |
|
362 function dirlist($path = '.', $include_hidden = true, $recursive = false) { |
|
363 if ( $this->is_file($path) ) { |
|
364 $limit_file = basename($path); |
|
365 $path = dirname($path) . '/'; |
|
366 } else { |
|
367 $limit_file = false; |
|
368 } |
|
369 |
|
370 $pwd = @ftp_pwd($this->link); |
|
371 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist |
|
372 return false; |
|
373 $list = @ftp_rawlist($this->link, '-a', false); |
|
374 @ftp_chdir($this->link, $pwd); |
|
375 |
|
376 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least) |
|
377 return false; |
|
378 |
|
379 $dirlist = array(); |
|
380 foreach ( $list as $k => $v ) { |
|
381 $entry = $this->parselisting($v); |
|
382 if ( empty($entry) ) |
|
383 continue; |
|
384 |
|
385 if ( '.' == $entry['name'] || '..' == $entry['name'] ) |
|
386 continue; |
|
387 |
|
388 if ( ! $include_hidden && '.' == $entry['name'][0] ) |
|
389 continue; |
|
390 |
|
391 if ( $limit_file && $entry['name'] != $limit_file) |
|
392 continue; |
|
393 |
|
394 $dirlist[ $entry['name'] ] = $entry; |
|
395 } |
|
396 |
|
397 $ret = array(); |
|
398 foreach ( (array)$dirlist as $struc ) { |
|
399 if ( 'd' == $struc['type'] ) { |
|
400 if ( $recursive ) |
|
401 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); |
|
402 else |
|
403 $struc['files'] = array(); |
|
404 } |
|
405 |
|
406 $ret[ $struc['name'] ] = $struc; |
|
407 } |
|
408 return $ret; |
|
409 } |
|
410 |
|
411 function __destruct() { |
|
412 if ( $this->link ) |
|
413 ftp_close($this->link); |
|
414 } |
|
415 } |