]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-ftpsockets.php
Wordpress 2.7.1
[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 $timeout = 5;
20         var $errors;
21         var $options = array();
22
23         var $permission = null;
24
25         function WP_Filesystem_ftpsockets($opt = '') {
26                 $this->method = 'ftpsockets';
27                 $this->errors = new WP_Error();
28
29                 //Check if possible to use ftp functions.
30                 if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
31                                 return false;
32                 $this->ftp = new ftp();
33
34                 //Set defaults:
35                 if ( empty($opt['port']) )
36                         $this->options['port'] = 21;
37                 else
38                         $this->options['port'] = $opt['port'];
39
40                 if ( empty($opt['hostname']) )
41                         $this->errors->add('empty_hostname', __('FTP hostname is required'));
42                 else
43                         $this->options['hostname'] = $opt['hostname'];
44
45                 if ( isset($opt['base']) && ! empty($opt['base']) )
46                         $this->wp_base = $opt['base'];
47
48                 // Check if the options provided are OK.
49                 if ( empty ($opt['username']) )
50                         $this->errors->add('empty_username', __('FTP username is required'));
51                 else
52                         $this->options['username'] = $opt['username'];
53
54                 if ( empty ($opt['password']) )
55                         $this->errors->add('empty_password', __('FTP password is required'));
56                 else
57                         $this->options['password'] = $opt['password'];
58         }
59
60         function connect() {
61                 if ( ! $this->ftp )
62                         return false;
63
64                 //$this->ftp->Verbose = true;
65
66                 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
67                         $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
68                         return false;
69                 }
70                 if ( ! $this->ftp->connect() ) {
71                         $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
72                         return false;
73                 }
74
75                 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
76                         $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
77                         return false;
78                 }
79
80                 $this->ftp->SetType(FTP_AUTOASCII);
81                 $this->ftp->Passive(true);
82                 return true;
83         }
84
85         function setDefaultPermissions($perm) {
86                 $this->permission = $perm;
87         }
88
89         function get_contents($file, $type = '', $resumepos = 0) {
90                 if( ! $this->exists($file) )
91                         return false;
92
93                 if( empty($type) )
94                         $type = FTP_AUTOASCII;
95                 $this->ftp->SetType($type);
96
97                 $temp = wp_tempnam( $file );
98
99                 if ( ! $temphandle = fopen($temp, 'w+') )
100                         return false;
101
102                 if ( ! $this->ftp->fget($temphandle, $file) ) {
103                         fclose($temphandle);
104                         unlink($temp);
105                         return ''; //Blank document, File does exist, Its just blank.
106                 }
107
108                 fseek($temphandle, 0); //Skip back to the start of the file being written to
109                 $contents = '';
110
111                 while ( ! feof($temphandle) )
112                         $contents .= fread($temphandle, 8192);
113
114                 fclose($temphandle);
115                 unlink($temp);
116                 return $contents;
117         }
118
119         function get_contents_array($file) {
120                 return explode("\n", $this->get_contents($file) );
121         }
122
123         function put_contents($file, $contents, $type = '' ) {
124                 if( empty($type) )
125                         $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
126
127                 $this->ftp->SetType($type);
128
129                 $temp = wp_tempnam( $file );
130                 if ( ! $temphandle = fopen($temp, 'w+') ){
131                         unlink($temp);
132                         return false;
133                 }
134
135                 fwrite($temphandle, $contents);
136                 fseek($temphandle, 0); //Skip back to the start of the file being written to
137
138                 $ret = $this->ftp->fput($file, $temphandle);
139
140                 fclose($temphandle);
141                 unlink($temp);
142                 return $ret;
143         }
144
145         function cwd() {
146                 $cwd = $this->ftp->pwd();
147                 if( $cwd )
148                         $cwd = trailingslashit($cwd);
149                 return $cwd;
150         }
151
152         function chdir($file) {
153                 return $this->ftp->chdir($file);
154         }
155
156         function chgrp($file, $group, $recursive = false ) {
157                 return false;
158         }
159
160         function chmod($file, $mode = false, $recursive = false ) {
161                 if( ! $mode )
162                         $mode = $this->permission;
163                 if( ! $mode )
164                         return false;
165                 //if( ! $this->exists($file) )
166                 //      return false;
167                 if( ! $recursive || ! $this->is_dir($file) ) {
168                         return $this->ftp->chmod($file,$mode);
169                 }
170                 //Is a directory, and we want recursive
171                 $filelist = $this->dirlist($file);
172                 foreach($filelist as $filename){
173                         $this->chmod($file . '/' . $filename, $mode, $recursive);
174                 }
175                 return true;
176         }
177
178         function chown($file, $owner, $recursive = false ) {
179                 return false;
180         }
181
182         function owner($file) {
183                 $dir = $this->dirlist($file);
184                 return $dir[$file]['owner'];
185         }
186
187         function getchmod($file) {
188                 $dir = $this->dirlist($file);
189                 return $dir[$file]['permsn'];
190         }
191
192         function group($file) {
193                 $dir = $this->dirlist($file);
194                 return $dir[$file]['group'];
195         }
196
197         function copy($source, $destination, $overwrite = false ) {
198                 if( ! $overwrite && $this->exists($destination) )
199                         return false;
200
201                 $content = $this->get_contents($source);
202                 if ( false === $content )
203                         return false;
204
205                 return $this->put_contents($destination, $content);
206         }
207
208         function move($source, $destination, $overwrite = false ) {
209                 return $this->ftp->rename($source, $destination);
210         }
211
212         function delete($file, $recursive = false ) {
213                 if ( $this->is_file($file) )
214                         return $this->ftp->delete($file);
215                 if ( !$recursive )
216                         return $this->ftp->rmdir($file);
217
218                 return $this->ftp->mdel($file);
219         }
220
221         function exists($file) {
222                 return $this->ftp->is_exists($file);
223         }
224
225         function is_file($file) {
226                 return $this->is_dir($file) ? false : true;
227         }
228
229         function is_dir($path) {
230                 $cwd = $this->cwd();
231                 if ( $this->chdir($path) ) {
232                         $this->chdir($cwd);
233                         return true;
234                 }
235                 return false;
236         }
237
238         function is_readable($file) {
239                 //Get dir list, Check if the file is writable by the current user??
240                 return true;
241         }
242
243         function is_writable($file) {
244                 //Get dir list, Check if the file is writable by the current user??
245                 return true;
246         }
247
248         function atime($file) {
249                 return false;
250         }
251
252         function mtime($file) {
253                 return $this->ftp->mdtm($file);
254         }
255
256         function size($file) {
257                 return $this->ftp->filesize($file);
258         }
259
260         function touch($file, $time = 0, $atime = 0 ) {
261                 return false;
262         }
263
264         function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
265                 if( ! $this->ftp->mkdir($path) )
266                         return false;
267                 if( $chmod )
268                         $this->chmod($path, $chmod);
269                 if( $chown )
270                         $this->chown($path, $chown);
271                 if( $chgrp )
272                         $this->chgrp($path, $chgrp);
273                 return true;
274         }
275
276         function rmdir($path, $recursive = false ) {
277                 if( ! $recursive )
278                         return $this->ftp->rmdir($path);
279
280                 return $this->ftp->mdel($path);
281         }
282
283         function dirlist($path = '.', $incdot = false, $recursive = false ) {
284                 if( $this->is_file($path) ) {
285                         $limitFile = basename($path);
286                         $path = dirname($path) . '/';
287                 } else {
288                         $limitFile = false;
289                 }
290
291                 $list = $this->ftp->dirlist($path);
292                 if( ! $list )
293                         return false;
294                 if( empty($list) )
295                         return array();
296
297                 $ret = array();
298                 foreach ( $list as $struc ) {
299
300                         if ( 'd' == $struc['type'] ) {
301                                 $struc['files'] = array();
302
303                                 if ( $incdot ){
304                                         //We're including the doted starts
305                                         if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
306                                                 if ($recursive)
307                                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
308                                         }
309                                 } else { //No dots
310                                         if ($recursive)
311                                                 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
312                                 }
313                         }
314                         //File
315                         $ret[$struc['name']] = $struc;
316                 }
317                 return $ret;
318         }
319
320         function __destruct() {
321                 $this->ftp->quit();
322         }
323 }
324
325 ?>