2 class WP_Filesystem_ftpsockets{
6 var $options = array();
9 var $permission = null;
11 var $filetypes = array(
26 function WP_Filesystem_ftpsockets($opt='') {
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 find_base_dir($base = '.',$echo = false, $loop = false) {
90 //Sanitize the Windows path formats, This allows easier conparison and aligns it to FTP output.
91 $abspath = str_replace('\\','/',ABSPATH); //windows: Straighten up the paths..
92 if( strpos($abspath, ':') ){ //Windows, Strip out the driveletter
93 if( preg_match("|.{1}\:(.+)|i", $abspath, $mat) )
97 //Set up the base directory (Which unless specified, is the current one)
98 if( empty( $base ) || '.' == $base ) $base = $this->cwd();
99 $base = trailingslashit($base);
101 //Can we see the Current directory as part of the ABSPATH?
102 $location = strpos($abspath, $base);
103 if( false !== $location ) {
104 $newbase = path_join($base, substr($abspath, $location + strlen($base)));
106 if( false !== $this->chdir($newbase) ){ //chdir sometimes returns null under certain circumstances, even when its changed correctly, FALSE will be returned if it doesnt change correctly.
107 if($echo) printf( __('Changing to %s') . '<br/>', $newbase );
108 //Check to see if it exists in that folder.
109 if( $this->exists($newbase . 'wp-settings.php') ){
110 if($echo) printf( __('Found %s'), $newbase . 'wp-settings.php<br/>' );
116 //Ok, Couldnt do a magic location from that particular folder level
118 //Get a list of the files in the current directory, See if we can locate where we are in the folder stucture.
119 $files = $this->dirlist($base);
121 $arrPath = explode('/', $abspath);
122 foreach($arrPath as $key){
123 //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
124 // If its found, change into it and follow through looking for it.
125 // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
126 // If it reaches the end, and still cant find it, it'll return false for the entire function.
127 if( isset($files[ $key ]) ){
128 //Lets try that folder:
129 $folder = path_join($base, $key);
130 if($echo) printf( __('Changing to %s') . '<br/>', $folder );
131 $ret = $this->find_base_dir( $folder, $echo, $loop);
136 //Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
137 if(isset( $files[ 'wp-settings.php' ]) ){
138 if($echo) printf( __('Found %s'), $base . 'wp-settings.php<br/>' );
142 return false;//Prevent tihs function looping again.
143 //As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
144 return $this->find_base_dir('/', $echo, true);
147 function get_base_dir($base = '.', $echo = false){
148 if( defined('FTP_BASE') )
149 $this->wp_base = FTP_BASE;
150 if( empty($this->wp_base) )
151 $this->wp_base = $this->find_base_dir($base, $echo);
152 return $this->wp_base;
155 function get_contents($file,$type='',$resumepos=0){
156 if( ! $this->exists($file) )
160 $extension = substr(strrchr($file, "."), 1);
161 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
163 $this->ftp->SetType($type);
167 if ( ! $this->ftp->fget($temp, $file) ) {
169 return ''; //Blank document, File does exist, Its just blank.
171 fseek($temp, 0); //Skip back to the start of the file being written to
173 while ( !feof($temp) )
174 $contents .= fread($temp, 8192);
179 function get_contents_array($file){
180 return explode("\n",$this->get_contents($file));
183 function put_contents($file,$contents,$type=''){
185 $extension = substr(strrchr($file, "."), 1);
186 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
188 $this->ftp->SetType($type);
193 fwrite($temp,$contents);
194 fseek($temp, 0); //Skip back to the start of the file being written to
195 $ret = $this->ftp->fput($file, $temp);
201 $cwd = $this->ftp->pwd();
203 $cwd = trailingslashit($cwd);
207 function chdir($file){
208 return $this->ftp->chdir($file);
211 function chgrp($file,$group,$recursive=false){
215 function chmod($file,$mode=false,$recursive=false){
217 $mode = $this->permission;
220 //if( ! $this->exists($file) )
222 if( ! $recursive || ! $this->is_dir($file) ){
223 return $this->ftp->chmod($file,$mode);
225 //Is a directory, and we want recursive
226 $filelist = $this->dirlist($file);
227 foreach($filelist as $filename){
228 $this->chmod($file.'/'.$filename,$mode,$recursive);
233 function chown($file,$owner,$recursive=false){
237 function owner($file){
238 $dir = $this->dirlist($file);
239 return $dir[$file]['owner'];
242 function getchmod($file){
243 $dir = $this->dirlist($file);
244 return $dir[$file]['permsn'];
247 function gethchmod($file){
248 //From the PHP.net page for ...?
249 $perms = $this->getchmod($file);
250 if (($perms & 0xC000) == 0xC000) {
253 } elseif (($perms & 0xA000) == 0xA000) {
256 } elseif (($perms & 0x8000) == 0x8000) {
259 } elseif (($perms & 0x6000) == 0x6000) {
262 } elseif (($perms & 0x4000) == 0x4000) {
265 } elseif (($perms & 0x2000) == 0x2000) {
268 } elseif (($perms & 0x1000) == 0x1000) {
277 $info .= (($perms & 0x0100) ? 'r' : '-');
278 $info .= (($perms & 0x0080) ? 'w' : '-');
279 $info .= (($perms & 0x0040) ?
280 (($perms & 0x0800) ? 's' : 'x' ) :
281 (($perms & 0x0800) ? 'S' : '-'));
284 $info .= (($perms & 0x0020) ? 'r' : '-');
285 $info .= (($perms & 0x0010) ? 'w' : '-');
286 $info .= (($perms & 0x0008) ?
287 (($perms & 0x0400) ? 's' : 'x' ) :
288 (($perms & 0x0400) ? 'S' : '-'));
291 $info .= (($perms & 0x0004) ? 'r' : '-');
292 $info .= (($perms & 0x0002) ? 'w' : '-');
293 $info .= (($perms & 0x0001) ?
294 (($perms & 0x0200) ? 't' : 'x' ) :
295 (($perms & 0x0200) ? 'T' : '-'));
299 function getnumchmodfromh($mode) {
301 $legal = array("","w","r","x","-");
302 $attarray = preg_split("//",$mode);
303 for($i=0;$i<count($attarray);$i++){
304 if($key = array_search($attarray[$i],$legal)){
305 $realmode .= $legal[$key];
308 $mode = str_pad($realmode,9,'-');
309 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
310 $mode = strtr($mode,$trans);
312 $newmode .= $mode[0]+$mode[1]+$mode[2];
313 $newmode .= $mode[3]+$mode[4]+$mode[5];
314 $newmode .= $mode[6]+$mode[7]+$mode[8];
318 function group($file){
319 $dir = $this->dirlist($file);
320 return $dir[$file]['group'];
323 function copy($source,$destination,$overwrite=false){
324 if( ! $overwrite && $this->exists($destination) )
327 $content = $this->get_contents($source);
328 if ( false === $content )
331 return $this->put_contents($destination,$content);
334 function move($source,$destination,$overwrite=false){
335 return $this->ftp->rename($source,$destination);
338 function delete($file,$recursive=false) {
339 if ( $this->is_file($file) )
340 return $this->ftp->delete($file);
342 return $this->ftp->rmdir($file);
344 return $this->ftp->mdel($file);
347 function exists($file){
348 return $this->ftp->is_exists($file);
351 function is_file($file){
352 return $this->is_dir($file) ? false : true;
355 function is_dir($path){
357 if ( $this->chdir($path) ) {
364 function is_readable($file){
365 //Get dir list, Check if the file is writable by the current user??
369 function is_writable($file){
370 //Get dir list, Check if the file is writable by the current user??
374 function atime($file){
378 function mtime($file){
379 return $this->ftp->mdtm($file);
382 function size($file){
383 return $this->ftp->filesize($file);
386 function touch($file,$time=0,$atime=0){
390 function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
391 if( ! $this->ftp->mkdir($path) )
394 $this->chmod($path, $chmod);
396 $this->chown($path, $chown);
398 $this->chgrp($path, $chgrp);
402 function rmdir($path,$recursive=false){
404 return $this->ftp->rmdir($path);
406 return $this->ftp->mdel($path);
409 function dirlist($path='.',$incdot=false,$recursive=false){
410 if( $this->is_file($path) ){
411 $limitFile = basename($path);
412 $path = dirname($path) . '/';
417 $list = $this->ftp->dirlist($path);
424 foreach ( $list as $struc ) {
426 if ( 'd' == $struc['type'] ) {
427 $struc['files'] = array();
430 //We're including the doted starts
431 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
433 $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
437 $struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
441 $ret[$struc['name']] = $struc;
446 function __destruct(){