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