]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-ftpsockets.php
WordPress 3.6.1-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
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                 //Set defaults:
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         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_AUTOASCII);
79                 $this->ftp->Passive(true);
80                 $this->ftp->setTimeout(FS_TIMEOUT);
81                 return true;
82         }
83
84         function get_contents($file, $type = '', $resumepos = 0) {
85                 if ( ! $this->exists($file) )
86                         return false;
87
88                 if ( empty($type) )
89                         $type = FTP_AUTOASCII;
90                 $this->ftp->SetType($type);
91
92                 $temp = wp_tempnam( $file );
93
94                 if ( ! $temphandle = fopen($temp, 'w+') )
95                         return false;
96
97                 if ( ! $this->ftp->fget($temphandle, $file) ) {
98                         fclose($temphandle);
99                         unlink($temp);
100                         return ''; //Blank document, File does exist, It's just blank.
101                 }
102
103                 fseek($temphandle, 0); //Skip back to the start of the file being written to
104                 $contents = '';
105
106                 while ( ! feof($temphandle) )
107                         $contents .= fread($temphandle, 8192);
108
109                 fclose($temphandle);
110                 unlink($temp);
111                 return $contents;
112         }
113
114         function get_contents_array($file) {
115                 return explode("\n", $this->get_contents($file) );
116         }
117
118         function put_contents($file, $contents, $mode = false ) {
119                 $temp = wp_tempnam( $file );
120                 if ( ! $temphandle = @fopen($temp, 'w+') ) {
121                         unlink($temp);
122                         return false;
123                 }
124
125                 fwrite($temphandle, $contents);
126                 fseek($temphandle, 0); //Skip back to the start of the file being written to
127
128                 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
129                 $this->ftp->SetType($type);
130
131                 $ret = $this->ftp->fput($file, $temphandle);
132
133                 fclose($temphandle);
134                 unlink($temp);
135
136                 $this->chmod($file, $mode);
137
138                 return $ret;
139         }
140
141         function cwd() {
142                 $cwd = $this->ftp->pwd();
143                 if ( $cwd )
144                         $cwd = trailingslashit($cwd);
145                 return $cwd;
146         }
147
148         function chdir($file) {
149                 return $this->ftp->chdir($file);
150         }
151
152         function chgrp($file, $group, $recursive = false ) {
153                 return false;
154         }
155
156         function chmod($file, $mode = false, $recursive = false ) {
157                 if ( ! $mode ) {
158                         if ( $this->is_file($file) )
159                                 $mode = FS_CHMOD_FILE;
160                         elseif ( $this->is_dir($file) )
161                                 $mode = FS_CHMOD_DIR;
162                         else
163                                 return false;
164                 }
165
166                 // chmod any sub-objects if recursive.
167                 if ( $recursive && $this->is_dir($file) ) {
168                         $filelist = $this->dirlist($file);
169                         foreach ( (array)$filelist as $filename => $filemeta )
170                                 $this->chmod($file . '/' . $filename, $mode, $recursive);
171                 }
172
173                 // chmod the file or directory
174                 return $this->ftp->chmod($file, $mode);
175         }
176
177         function chown($file, $owner, $recursive = false ) {
178                 return false;
179         }
180
181         function owner($file) {
182                 $dir = $this->dirlist($file);
183                 return $dir[$file]['owner'];
184         }
185
186         function getchmod($file) {
187                 $dir = $this->dirlist($file);
188                 return $dir[$file]['permsn'];
189         }
190
191         function group($file) {
192                 $dir = $this->dirlist($file);
193                 return $dir[$file]['group'];
194         }
195
196         function copy($source, $destination, $overwrite = false, $mode = false) {
197                 if ( ! $overwrite && $this->exists($destination) )
198                         return false;
199
200                 $content = $this->get_contents($source);
201                 if ( false === $content )
202                         return false;
203
204                 return $this->put_contents($destination, $content, $mode);
205         }
206
207         function move($source, $destination, $overwrite = false ) {
208                 return $this->ftp->rename($source, $destination);
209         }
210
211         function delete($file, $recursive = false, $type = false) {
212                 if ( empty($file) )
213                         return false;
214                 if ( 'f' == $type || $this->is_file($file) )
215                         return $this->ftp->delete($file);
216                 if ( !$recursive )
217                         return $this->ftp->rmdir($file);
218
219                 return $this->ftp->mdel($file);
220         }
221
222         function exists($file) {
223                 return $this->ftp->is_exists($file);
224         }
225
226         function is_file($file) {
227                 if ( $this->is_dir($file) )
228                         return false;
229                 if ( $this->exists($file) )
230                         return true;
231                 return false;
232         }
233
234         function is_dir($path) {
235                 $cwd = $this->cwd();
236                 if ( $this->chdir($path) ) {
237                         $this->chdir($cwd);
238                         return true;
239                 }
240                 return false;
241         }
242
243         function is_readable($file) {
244                 //Get dir list, Check if the file is writable by the current user??
245                 return true;
246         }
247
248         function is_writable($file) {
249                 //Get dir list, Check if the file is writable by the current user??
250                 return true;
251         }
252
253         function atime($file) {
254                 return false;
255         }
256
257         function mtime($file) {
258                 return $this->ftp->mdtm($file);
259         }
260
261         function size($file) {
262                 return $this->ftp->filesize($file);
263         }
264
265         function touch($file, $time = 0, $atime = 0 ) {
266                 return false;
267         }
268
269         function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
270                 $path = untrailingslashit($path);
271                 if ( empty($path) )
272                         return false;
273
274                 if ( ! $this->ftp->mkdir($path) )
275                         return false;
276                 if ( ! $chmod )
277                         $chmod = FS_CHMOD_DIR;
278                 $this->chmod($path, $chmod);
279                 if ( $chown )
280                         $this->chown($path, $chown);
281                 if ( $chgrp )
282                         $this->chgrp($path, $chgrp);
283                 return true;
284         }
285
286         function rmdir($path, $recursive = false ) {
287                 $this->delete($path, $recursive);
288         }
289
290         function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
291                 if ( $this->is_file($path) ) {
292                         $limit_file = basename($path);
293                         $path = dirname($path) . '/';
294                 } else {
295                         $limit_file = false;
296                 }
297
298                 $list = $this->ftp->dirlist($path);
299                 if ( empty($list) && !$this->exists($path) )
300                         return false;
301
302                 $ret = array();
303                 foreach ( $list as $struc ) {
304
305                         if ( '.' == $struc['name'] || '..' == $struc['name'] )
306                                 continue;
307
308                         if ( ! $include_hidden && '.' == $struc['name'][0] )
309                                 continue;
310
311                         if ( $limit_file && $struc['name'] != $limit_file )
312                                 continue;
313
314                         if ( 'd' == $struc['type'] ) {
315                                 if ( $recursive )
316                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
317                                 else
318                                         $struc['files'] = array();
319                         }
320
321                         // Replace symlinks formatted as "source -> target" with just the source name
322                         if ( $struc['islink'] )
323                                 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
324
325                         $ret[ $struc['name'] ] = $struc;
326                 }
327                 return $ret;
328         }
329
330         function __destruct() {
331                 $this->ftp->quit();
332         }
333 }