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