# | Code | Descriptions |
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 ( ! 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); | //Arbitrary file manipulations |
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); | //Arbitrary file disclosing |
108 | ||
109 | fclose($temphandle); | |
110 | unlink($temp); | //Arbitrary file manipulations |
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); | //Arbitrary file manipulations |
122 | return false; | |
123 | } | |
124 | ||
125 | fwrite($temphandle, $contents); | //Arbitrary file manipulations |
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); | //Arbitrary file disclosing |
132 | ||
133 | fclose($temphandle); | |
134 | unlink($temp); | //Arbitrary file manipulations |
135 | ||
136 | $this->chmod($file, $mode); | //Arbitrary file disclosing //Arbitrary file manipulations |
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); | //Arbitrary file disclosing //Arbitrary file manipulations |
171 | } | |
172 | ||
173 | // chmod the file or directory | |
174 | return $this->ftp->chmod($file, $mode); | //Arbitrary file disclosing //Arbitrary file manipulations |
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 ) { | |
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); | |
205 | } | |
206 | ||
207 | function move($source, $destination, $overwrite = false ) { | |
208 | return $this->ftp->rename($source, $destination); | //Arbitrary file manipulations |
209 | } | |
210 | ||
211 | function delete($file, $recursive = false ) { | |
212 | if ( empty($file) ) | |
213 | return false; | |
214 | if ( $this->is_file($file) ) | |
215 | return $this->ftp->delete($file); | |
216 | if ( !$recursive ) | |
217 | return $this->ftp->rmdir($file); | //Arbitrary file manipulations |
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 | if ( ! $this->ftp->mkdir($path) ) | |
271 | return false; | |
272 | if ( ! $chmod ) | |
273 | $chmod = FS_CHMOD_DIR; | |
274 | $this->chmod($path, $chmod); | //Arbitrary file manipulations |
275 | if ( $chown ) | |
276 | $this->chown($path, $chown); | //Arbitrary file manipulations |
277 | if ( $chgrp ) | |
278 | $this->chgrp($path, $chgrp); | //Arbitrary file manipulations |
279 | return true; | |
280 | } | |
281 | ||
282 | function rmdir($path, $recursive = false ) { | |
283 | $this->delete($path, $recursive); | |
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 ( empty($list) && !$this->exists($path) ) | |
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); | //Arbitrary code inclusion |
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 | ?> | |
328 |