00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Io_Ftp extends Varien_Io_Abstract
00036 {
00037 const ERROR_EMPTY_HOST = 1;
00038 const ERROR_INVALID_CONNECTION = 2;
00039 const ERROR_INVALID_LOGIN = 3;
00040 const ERROR_INVALID_PATH = 4;
00041 const ERROR_INVALID_MODE = 5;
00042 const ERROR_INVALID_DESTINATION = 6;
00043 const ERROR_INVALID_SOURCE = 7;
00044
00045
00046
00047
00048
00049
00050 protected $_config;
00051
00052
00053
00054
00055
00056
00057 protected $_conn;
00058
00059
00060
00061
00062
00063
00064 protected $_error;
00065
00066 protected $_tmpFilename;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 public function open(array $args=array())
00086 {
00087 if (empty($args['host'])) {
00088 $this->_error = self::ERROR_EMPTY_HOST;
00089 throw new Varien_Io_Exception('Empty host specified');
00090 }
00091
00092 if (empty($args['port'])) {
00093 $args['port'] = 21;
00094 }
00095
00096 if (empty($args['user'])) {
00097 $args['user'] = 'anonymous';
00098 $args['password'] = 'anonymous@noserver.com';
00099 }
00100
00101 if (empty($args['password'])) {
00102 $args['password'] = '';
00103 }
00104
00105 if (empty($args['timeout'])) {
00106 $args['timeout'] = 90;
00107 }
00108
00109 if (empty($args['file_mode'])) {
00110 $args['file_mode'] = FTP_BINARY;
00111 }
00112
00113 $this->_config = $args;
00114
00115 if (empty($this->_config['ssl'])) {
00116 $this->_conn = @ftp_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
00117 } else {
00118 $this->_conn = @ftp_ssl_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
00119 }
00120 if (!$this->_conn) {
00121 $this->_error = self::ERROR_INVALID_CONNECTION;
00122 throw new Varien_Io_Exception('Could not establish FTP connection, invalid host or port');
00123 }
00124
00125 if (!@ftp_login($this->_conn, $this->_config['user'], $this->_config['password'])) {
00126 $this->_error = self::ERROR_INVALID_LOGIN;
00127 $this->close();
00128 throw new Varien_Io_Exception('Invalid user name or password');
00129 }
00130
00131 if (!empty($this->_config['path'])) {
00132 if (!@ftp_chdir($this->_conn, $this->_config['path'])) {
00133 $this->_error = self::ERROR_INVALID_PATH;
00134 $this->close();
00135 throw new Varien_Io_Exception('Invalid path');
00136 }
00137 }
00138
00139 if (!empty($this->_config['passive'])) {
00140 if (!@ftp_pasv($this->_conn, true)) {
00141 $this->_error = self::ERROR_INVALID_MODE;
00142 $this->close();
00143 throw new Varien_Io_Exception('Invalid file transfer mode');
00144 }
00145 }
00146
00147 return true;
00148 }
00149
00150
00151
00152
00153
00154
00155 public function close()
00156 {
00157 return @ftp_close($this->_conn);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 public function mkdir($dir, $mode=0777, $recursive=true)
00170 {
00171 return @ftp_mkdir($this->_conn, $dir);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180 public function rmdir($dir, $recursive=false)
00181 {
00182 return @ftp_rmdir($this->_conn, $dir);
00183 }
00184
00185
00186
00187
00188
00189
00190 public function pwd()
00191 {
00192 return @ftp_pwd($this->_conn);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201 public function cd($dir)
00202 {
00203 return @ftp_chdir($this->_conn, $dir);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213 public function read($filename, $dest=null)
00214 {
00215 if (is_string($dest)) {
00216 $result = ftp_get($this->_conn, $dest, $filename, $this->_config['file_mode']);
00217 } else {
00218 if (is_resource($dest)) {
00219 $stream = $dest;
00220 } elseif (is_null($dest)) {
00221 $stream = tmpfile();
00222 } else {
00223 $this->_error = self::ERROR_INVALID_DESTINATION;
00224 return false;
00225 }
00226
00227 $result = ftp_fget($this->_conn, $stream, $filename, $this->_config['file_mode']);
00228
00229 if (is_null($dest)) {
00230 fseek($stream, 0);
00231 $result = '';
00232 for ($result = ''; $s = fread($stream, 4096); $result .= $s);
00233 fclose($stream);
00234 }
00235 }
00236 return $result;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 public function write($filename, $src, $mode=null)
00247 {
00248 if (is_string($src) && is_readable($src)) {
00249 return @ftp_put($this->_conn, $filename, $src, $this->_config['file_mode']);
00250 } else {
00251 if (is_string($src)) {
00252 $stream = tmpfile();
00253 fputs($stream, $src);
00254 fseek($stream, 0);
00255 } elseif (is_resource($src)) {
00256 $stream = $src;
00257 } else {
00258 $this->_error = self::ERROR_INVALID_SOURCE;
00259 return false;
00260 }
00261
00262 $result = ftp_fput($this->_conn, $filename, $stream, $this->_config['file_mode']);
00263 if (is_string($src)) {
00264 fclose($stream);
00265 }
00266 return $result;
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 public function rm($filename)
00277 {
00278 return @ftp_delete($this->_conn, $filename);
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288 public function mv($src, $dest)
00289 {
00290 return @ftp_rename($this->_conn, $src, $dest);
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 public function chmod($filename, $mode)
00301 {
00302 return @ftp_chmod($this->_conn, $mode, $filename);
00303 }
00304
00305 public function ls($grep=null)
00306 {
00307 $ls = @ftp_nlist($this->_conn, '.');
00308
00309 $list = array();
00310 foreach ($ls as $file) {
00311 $list[] = array(
00312 'text'=>$file,
00313 'id'=>$this->pwd().'/'.$file,
00314 );
00315 }
00316
00317 return $list;
00318 }
00319
00320 protected function _tmpFilename($new=false)
00321 {
00322 if ($new || !$this->_tmpFilename) {
00323 $this->_tmpFilename = tempnam( md5(uniqid(rand(), TRUE)), '' );
00324 }
00325 return $this->_tmpFilename;
00326 }
00327 }