2 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
6 var $options = array();
8 var $permission = null;
10 var $filetypes = array(
25 function WP_Filesystem_ftpsockets($opt='') {
26 $this->method = 'ftpsockets';
27 $this->errors = new WP_Error();
29 //Check if possible to use ftp functions.
30 if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
32 $this->ftp = new ftp();
35 if ( empty($opt['port']) )
36 $this->options['port'] = 21;
38 $this->options['port'] = $opt['port'];
40 if ( empty($opt['hostname']) )
41 $this->errors->add('empty_hostname', __('FTP hostname is required'));
43 $this->options['hostname'] = $opt['hostname'];
45 if ( isset($opt['base']) && ! empty($opt['base']) )
46 $this->wp_base = $opt['base'];
48 // Check if the options provided are OK.
49 if ( empty ($opt['username']) )
50 $this->errors->add('empty_username', __('FTP username is required'));
52 $this->options['username'] = $opt['username'];
54 if ( empty ($opt['password']) )
55 $this->errors->add('empty_password', __('FTP password is required'));
57 $this->options['password'] = $opt['password'];
64 //$this->ftp->Verbose = true;
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']));
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']));
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']));
80 $this->ftp->SetType(FTP_AUTOASCII);
81 $this->ftp->Passive(true);
85 function setDefaultPermissions($perm) {
86 $this->permission = $perm;
89 function get_contents($file, $type = '', $resumepos = 0){
90 if( ! $this->exists($file) )
94 $extension = substr(strrchr($file, '.'), 1);
95 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
97 $this->ftp->SetType($type);
98 $temp = wp_tempnam( $file );
99 if ( ! $temphandle = fopen($temp, 'w+') )
101 if ( ! $this->ftp->fget($temphandle, $file) ) {
104 return ''; //Blank document, File does exist, Its just blank.
106 fseek($temphandle, 0); //Skip back to the start of the file being written to
108 while ( ! feof($temphandle) )
109 $contents .= fread($temphandle, 8192);
115 function get_contents_array($file){
116 return explode("\n", $this->get_contents($file) );
119 function put_contents($file, $contents, $type = '' ) {
121 $extension = substr(strrchr($file, '.'), 1);
122 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
124 $this->ftp->SetType($type);
126 $temp = wp_tempnam( $file );
127 if ( ! $temphandle = fopen($temp, 'w+') ){
131 fwrite($temphandle, $contents);
132 fseek($temphandle, 0); //Skip back to the start of the file being written to
133 $ret = $this->ftp->fput($file, $temphandle);
140 $cwd = $this->ftp->pwd();
142 $cwd = trailingslashit($cwd);
146 function chdir($file) {
147 return $this->ftp->chdir($file);
150 function chgrp($file, $group, $recursive = false ) {
154 function chmod($file, $mode = false, $recursive = false ){
156 $mode = $this->permission;
159 //if( ! $this->exists($file) )
161 if( ! $recursive || ! $this->is_dir($file) ) {
162 return $this->ftp->chmod($file,$mode);
164 //Is a directory, and we want recursive
165 $filelist = $this->dirlist($file);
166 foreach($filelist as $filename){
167 $this->chmod($file . '/' . $filename, $mode, $recursive);
172 function chown($file, $owner, $recursive = false ) {
176 function owner($file) {
177 $dir = $this->dirlist($file);
178 return $dir[$file]['owner'];
181 function getchmod($file) {
182 $dir = $this->dirlist($file);
183 return $dir[$file]['permsn'];
186 function group($file) {
187 $dir = $this->dirlist($file);
188 return $dir[$file]['group'];
191 function copy($source, $destination, $overwrite = false ) {
192 if( ! $overwrite && $this->exists($destination) )
195 $content = $this->get_contents($source);
196 if ( false === $content )
199 return $this->put_contents($destination, $content);
202 function move($source, $destination, $overwrite = false ) {
203 return $this->ftp->rename($source, $destination);
206 function delete($file, $recursive = false ) {
207 if ( $this->is_file($file) )
208 return $this->ftp->delete($file);
210 return $this->ftp->rmdir($file);
212 return $this->ftp->mdel($file);
215 function exists($file) {
216 return $this->ftp->is_exists($file);
219 function is_file($file) {
220 return $this->is_dir($file) ? false : true;
223 function is_dir($path) {
225 if ( $this->chdir($path) ) {
232 function is_readable($file) {
233 //Get dir list, Check if the file is writable by the current user??
237 function is_writable($file) {
238 //Get dir list, Check if the file is writable by the current user??
242 function atime($file) {
246 function mtime($file) {
247 return $this->ftp->mdtm($file);
250 function size($file) {
251 return $this->ftp->filesize($file);
254 function touch($file, $time = 0, $atime = 0 ){
258 function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
259 if( ! $this->ftp->mkdir($path) )
262 $this->chmod($path, $chmod);
264 $this->chown($path, $chown);
266 $this->chgrp($path, $chgrp);
270 function rmdir($path, $recursive = false ) {
272 return $this->ftp->rmdir($path);
274 return $this->ftp->mdel($path);
277 function dirlist($path = '.', $incdot = false, $recursive = false ) {
278 if( $this->is_file($path) ) {
279 $limitFile = basename($path);
280 $path = dirname($path) . '/';
285 $list = $this->ftp->dirlist($path);
292 foreach ( $list as $struc ) {
294 if ( 'd' == $struc['type'] ) {
295 $struc['files'] = array();
298 //We're including the doted starts
299 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
301 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
305 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
309 $ret[$struc['name']] = $struc;
314 function __destruct(){