|
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 WP_Filesystem_ftpsockets($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 ( isset($opt['base']) && ! 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, Its 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, $type = '' ) { |
|
119 if ( empty($type) ) |
|
120 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII; |
|
121 |
|
122 $this->ftp->SetType($type); |
|
123 |
|
124 $temp = wp_tempnam( $file ); |
|
125 if ( ! $temphandle = fopen($temp, 'w+') ) { |
|
126 unlink($temp); |
|
127 return false; |
|
128 } |
|
129 |
|
130 fwrite($temphandle, $contents); |
|
131 fseek($temphandle, 0); //Skip back to the start of the file being written to |
|
132 |
|
133 $ret = $this->ftp->fput($file, $temphandle); |
|
134 |
|
135 fclose($temphandle); |
|
136 unlink($temp); |
|
137 return $ret; |
|
138 } |
|
139 |
|
140 function cwd() { |
|
141 $cwd = $this->ftp->pwd(); |
|
142 if ( $cwd ) |
|
143 $cwd = trailingslashit($cwd); |
|
144 return $cwd; |
|
145 } |
|
146 |
|
147 function chdir($file) { |
|
148 return $this->ftp->chdir($file); |
|
149 } |
|
150 |
|
151 function chgrp($file, $group, $recursive = false ) { |
|
152 return false; |
|
153 } |
|
154 |
|
155 function chmod($file, $mode = false, $recursive = false ) { |
|
156 |
|
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 if ( ! $recursive || ! $this->is_dir($file) ) { |
|
167 return $this->ftp->chmod($file, $mode); |
|
168 } |
|
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 ( empty($file) ) |
|
214 return false; |
|
215 if ( $this->is_file($file) ) |
|
216 return $this->ftp->delete($file); |
|
217 if ( !$recursive ) |
|
218 return $this->ftp->rmdir($file); |
|
219 |
|
220 return $this->ftp->mdel($file); |
|
221 } |
|
222 |
|
223 function exists($file) { |
|
224 return $this->ftp->is_exists($file); |
|
225 } |
|
226 |
|
227 function is_file($file) { |
|
228 return $this->is_dir($file) ? false : true; |
|
229 } |
|
230 |
|
231 function is_dir($path) { |
|
232 $cwd = $this->cwd(); |
|
233 if ( $this->chdir($path) ) { |
|
234 $this->chdir($cwd); |
|
235 return true; |
|
236 } |
|
237 return false; |
|
238 } |
|
239 |
|
240 function is_readable($file) { |
|
241 //Get dir list, Check if the file is writable by the current user?? |
|
242 return true; |
|
243 } |
|
244 |
|
245 function is_writable($file) { |
|
246 //Get dir list, Check if the file is writable by the current user?? |
|
247 return true; |
|
248 } |
|
249 |
|
250 function atime($file) { |
|
251 return false; |
|
252 } |
|
253 |
|
254 function mtime($file) { |
|
255 return $this->ftp->mdtm($file); |
|
256 } |
|
257 |
|
258 function size($file) { |
|
259 return $this->ftp->filesize($file); |
|
260 } |
|
261 |
|
262 function touch($file, $time = 0, $atime = 0 ) { |
|
263 return false; |
|
264 } |
|
265 |
|
266 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) { |
|
267 if ( ! $this->ftp->mkdir($path) ) |
|
268 return false; |
|
269 if ( ! $chmod ) |
|
270 $chmod = FS_CHMOD_DIR; |
|
271 $this->chmod($path, $chmod); |
|
272 if ( $chown ) |
|
273 $this->chown($path, $chown); |
|
274 if ( $chgrp ) |
|
275 $this->chgrp($path, $chgrp); |
|
276 return true; |
|
277 } |
|
278 |
|
279 function rmdir($path, $recursive = false ) { |
|
280 if ( ! $recursive ) |
|
281 return $this->ftp->rmdir($path); |
|
282 |
|
283 return $this->ftp->mdel($path); |
|
284 } |
|
285 |
|
286 function dirlist($path = '.', $include_hidden = true, $recursive = false ) { |
|
287 if ( $this->is_file($path) ) { |
|
288 $limit_file = basename($path); |
|
289 $path = dirname($path) . '/'; |
|
290 } else { |
|
291 $limit_file = false; |
|
292 } |
|
293 |
|
294 $list = $this->ftp->dirlist($path); |
|
295 if ( ! $list ) |
|
296 return false; |
|
297 |
|
298 $ret = array(); |
|
299 foreach ( $list as $struc ) { |
|
300 |
|
301 if ( '.' == $struc['name'] || '..' == $struc['name'] ) |
|
302 continue; |
|
303 |
|
304 if ( ! $include_hidden && '.' == $struc['name'][0] ) |
|
305 continue; |
|
306 |
|
307 if ( $limit_file && $struc['name'] != $limit_file ) |
|
308 continue; |
|
309 |
|
310 if ( 'd' == $struc['type'] ) { |
|
311 if ( $recursive ) |
|
312 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); |
|
313 else |
|
314 $struc['files'] = array(); |
|
315 } |
|
316 |
|
317 $ret[ $struc['name'] ] = $struc; |
|
318 } |
|
319 return $ret; |
|
320 } |
|
321 |
|
322 function __destruct() { |
|
323 $this->ftp->quit(); |
|
324 } |
|
325 } |
|
326 |
|
327 ?> |