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 class Mage_Backup_Model_Backup extends Varien_Object
00035 {
00036
00037 const BACKUP_DB = 'db';
00038 const BACKUP_VIEW = 'view';
00039 const BACKUP_MEDIA = 'media';
00040
00041
00042 const BACKUP_EXTENSION = 'gz';
00043 const COMPRESS_RATE = 9;
00044
00045
00046
00047
00048
00049
00050 private $_type = 'db';
00051
00052
00053
00054
00055
00056
00057 protected $_handler = null;
00058
00059
00060
00061
00062
00063
00064
00065
00066 public function load($fileName, $filePath)
00067 {
00068 list ($time, $type) = explode("_", substr($fileName, 0, strrpos($fileName, ".")));
00069 $this->addData(array(
00070 'id' => $filePath . DS . $fileName,
00071 'time' => (int)$time,
00072 'path' => $filePath,
00073 'time_formated' => date('Y-m-d H:i:s', (int)$time))
00074 );
00075 $this->setType($type);
00076 return $this;
00077 }
00078
00079
00080
00081
00082
00083
00084 public function exists()
00085 {
00086 return is_file($this->getPath() . DS . $this->getFileName());
00087 }
00088
00089
00090
00091
00092
00093
00094 public function getFileName()
00095 {
00096 return $this->getTime() . "_" . $this->getType()
00097 . "." . self::BACKUP_EXTENSION;
00098 }
00099
00100
00101
00102
00103
00104
00105 public function setType($value='db')
00106 {
00107 if(!in_array($value, array('db','media','view'))) {
00108 $value = 'db';
00109 }
00110
00111 $this->_type = $value;
00112 $this->setData('type', $this->_type);
00113
00114 return $this;
00115 }
00116
00117
00118
00119
00120
00121
00122 public function getType()
00123 {
00124 return $this->_type;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 public function setFile(&$content)
00135 {
00136 if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
00137 Mage::throwException(Mage::helper('backup')->__('Wrong order of creation for new backup'));
00138 }
00139
00140 $ioProxy = new Varien_Io_File();
00141 $ioProxy->setAllowCreateFolders(true);
00142 $ioProxy->open(array('path'=>$this->getPath()));
00143
00144 $compress = 0;
00145 if (extension_loaded("zlib")) {
00146 $compress = 1;
00147 }
00148
00149 $rawContent = '';
00150 if ( $compress ) {
00151 $rawContent = gzcompress( $content, self::COMPRESS_RATE );
00152 } else {
00153 $rawContent = $content;
00154 }
00155
00156 $fileHeaders = pack("ll", $compress, strlen($rawContent));
00157 $ioProxy->write($this->getFileName(), $fileHeaders . $rawContent);
00158 return $this;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168 public function &getFile()
00169 {
00170
00171 if (!$this->exists()) {
00172 Mage::throwException(Mage::helper('backup')->__("Backup file doesn't exist"));
00173 }
00174
00175 $fResource = @fopen($this->getPath() . DS . $this->getFileName(), "rb");
00176 if (!$fResource) {
00177 Mage::throwException(Mage::helper('backup')->__("Cannot read backup file"));
00178 }
00179
00180 $content = '';
00181 $compressed = 0;
00182
00183 $info = unpack("lcompress/llength", fread($fResource, 8));
00184 if ($info['compress']) {
00185 $compressed = 1;
00186 }
00187
00188 if ($compressed && !extension_loaded("zlib")) {
00189 fclose($fResource);
00190 Mage::throwException(Mage::helper('backup')->__('File compressed with Zlib, but this extension is not installed on server'));
00191 }
00192
00193 if ($compressed) {
00194 $content = gzuncompress(fread($fResource, $info['length']));
00195 } else {
00196 $content = fread($fResource, $info['length']);
00197 }
00198
00199 fclose($fResource);
00200
00201 return $content;
00202 }
00203
00204
00205
00206
00207
00208
00209 public function deleteFile()
00210 {
00211 if (!$this->exists()) {
00212 Mage::throwException(Mage::helper('backup')->__("Backup file doesn't exist"));
00213 }
00214
00215 $ioProxy = new Varien_Io_File();
00216 $ioProxy->open(array('path'=>$this->getPath()));
00217 $ioProxy->rm($this->getFileName());
00218 return $this;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227 public function open($write = false)
00228 {
00229 if (is_null($this->getPath())) {
00230 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file path don\'t specify'));
00231 }
00232
00233 $ioAdapter = new Varien_Io_File();
00234 try {
00235 $path = $ioAdapter->getCleanPath($this->getPath());
00236 $ioAdapter->checkAndCreateFolder($path);
00237 $filePath = $path . DS . $this->getFileName();
00238 }
00239 catch (Exception $e) {
00240 Mage::exception('Mage_Backup', $e->getMessage());
00241 }
00242
00243 if ($write && $ioAdapter->fileExists($filePath)) {
00244 $ioAdapter->rm($filePath);
00245 }
00246 if (!$write && !$ioAdapter->fileExists($filePath)) {
00247 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" doesn\'t exist', $this->getFileName()));
00248 }
00249
00250 $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
00251
00252 try {
00253 $this->_handler = gzopen($filePath, $mode);
00254 }
00255 catch (Exception $e) {
00256 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" can\'t read or write', $this->getFileName()));
00257 }
00258
00259 return $this;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268 public function read($length)
00269 {
00270 if (is_null($this->_handler)) {
00271 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00272 }
00273
00274 return gzread($this->_handler, $length);
00275 }
00276
00277 public function eof()
00278 {
00279 if (is_null($this->_handler)) {
00280 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00281 }
00282
00283 return gzeof($this->_handler);
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 public function write($string)
00293 {
00294 if (is_null($this->_handler)) {
00295 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00296 }
00297
00298 try {
00299 gzwrite($this->_handler, $string);
00300 }
00301 catch (Exception $e) {
00302 Mage::exception('Mage_Backup', Mage::helper('backup')->__('Error write to Backup file "%s"', $this->getFileName()));
00303 }
00304
00305 return $this;
00306 }
00307
00308
00309
00310
00311
00312
00313 public function close()
00314 {
00315 @gzclose($this->_handler);
00316 $this->_handler = null;
00317
00318 return $this;
00319 }
00320
00321
00322
00323
00324
00325 public function output()
00326 {
00327 if (!$this->exists()) {
00328 return ;
00329 }
00330
00331 $ioAdapter = new Varien_Io_File();
00332 $ioAdapter->open(array('path' => $this->getPath()));
00333
00334 $ioAdapter->streamOpen($this->getFileName(), 'r');
00335 while ($buffer = $ioAdapter->streamRead()) {
00336 echo $buffer;
00337 }
00338 $ioAdapter->streamClose();
00339 }
00340
00341 public function getSize()
00342 {
00343 if (!is_null($this->getData('size'))) {
00344 return $this->getData('size');
00345 }
00346
00347 if ($this->exists()) {
00348 $this->setData('size', filesize($this->getPath() . DS . $this->getFileName()));
00349 return $this->getData('size');
00350 }
00351
00352 return 0;
00353 }
00354 }