1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Backup_Model_Backup extends Varien_Object
35: {
36:
37: const COMPRESS_RATE = 9;
38:
39: 40: 41: 42: 43:
44: private $_type = 'db';
45:
46: 47: 48: 49: 50:
51: protected $_handler = null;
52:
53: 54: 55: 56: 57: 58: 59:
60: public function load($fileName, $filePath)
61: {
62: $backupData = Mage::helper('backup')->extractDataFromFilename($fileName);
63:
64: $this->addData(array(
65: 'id' => $filePath . DS . $fileName,
66: 'time' => (int)$backupData->getTime(),
67: 'path' => $filePath,
68: 'extension' => Mage::helper('backup')->getExtensionByType($backupData->getType()),
69: 'display_name' => Mage::helper('backup')->nameToDisplayName($backupData->getName()),
70: 'name' => $backupData->getName(),
71: 'date_object' => new Zend_Date((int)$backupData->getTime(), Mage::app()->getLocale()->getLocaleCode())
72: ));
73:
74: $this->setType($backupData->getType());
75: return $this;
76: }
77:
78: 79: 80: 81: 82:
83: public function exists()
84: {
85: return is_file($this->getPath() . DS . $this->getFileName());
86: }
87:
88: 89: 90: 91: 92:
93: public function getFileName()
94: {
95: $filename = $this->getTime() . "_" . $this->getType();
96: $backupName = $this->getName();
97:
98: if (!empty($backupName)) {
99: $filename .= '_' . $backupName;
100: }
101:
102: $filename .= '.' . Mage::helper('backup')->getExtensionByType($this->getType());
103:
104: return $filename;
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function setType($value='db')
114: {
115: $possibleTypes = Mage::helper('backup')->getBackupTypesList();
116: if(!in_array($value, $possibleTypes)) {
117: $value = Mage::helper('backup')->getDefaultBackupType();
118: }
119:
120: $this->_type = $value;
121: $this->setData('type', $this->_type);
122:
123: return $this;
124: }
125:
126: 127: 128: 129: 130:
131: public function getType()
132: {
133: return $this->_type;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function setFile(&$content)
144: {
145: if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
146: Mage::throwException(Mage::helper('backup')->__('Wrong order of creation for new backup.'));
147: }
148:
149: $ioProxy = new Varien_Io_File();
150: $ioProxy->setAllowCreateFolders(true);
151: $ioProxy->open(array('path'=>$this->getPath()));
152:
153: $compress = 0;
154: if (extension_loaded("zlib")) {
155: $compress = 1;
156: }
157:
158: $rawContent = '';
159: if ( $compress ) {
160: $rawContent = gzcompress( $content, self::COMPRESS_RATE );
161: } else {
162: $rawContent = $content;
163: }
164:
165: $fileHeaders = pack("ll", $compress, strlen($rawContent));
166: $ioProxy->write($this->getFileName(), $fileHeaders . $rawContent);
167: return $this;
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: public function &getFile()
178: {
179:
180: if (!$this->exists()) {
181: Mage::throwException(Mage::helper('backup')->__("Backup file does not exist."));
182: }
183:
184: $fResource = @fopen($this->getPath() . DS . $this->getFileName(), "rb");
185: if (!$fResource) {
186: Mage::throwException(Mage::helper('backup')->__("Cannot read backup file."));
187: }
188:
189: $content = '';
190: $compressed = 0;
191:
192: $info = unpack("lcompress/llength", fread($fResource, 8));
193: if ($info['compress']) {
194: $compressed = 1;
195: }
196:
197: if ($compressed && !extension_loaded("zlib")) {
198: fclose($fResource);
199: Mage::throwException(Mage::helper('backup')->__('The file was compressed with Zlib, but this extension is not installed on server.'));
200: }
201:
202: if ($compressed) {
203: $content = gzuncompress(fread($fResource, $info['length']));
204: } else {
205: $content = fread($fResource, $info['length']);
206: }
207:
208: fclose($fResource);
209:
210: return $content;
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function deleteFile()
220: {
221: if (!$this->exists()) {
222: Mage::throwException(Mage::helper('backup')->__("Backup file does not exist."));
223: }
224:
225: $ioProxy = new Varien_Io_File();
226: $ioProxy->open(array('path'=>$this->getPath()));
227: $ioProxy->rm($this->getFileName());
228: return $this;
229: }
230:
231: 232: 233: 234: 235: 236:
237: public function open($write = false)
238: {
239: if (is_null($this->getPath())) {
240: Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file path was not specified.'));
241: }
242:
243: $ioAdapter = new Varien_Io_File();
244: try {
245: $path = $ioAdapter->getCleanPath($this->getPath());
246: $ioAdapter->checkAndCreateFolder($path);
247: $filePath = $path . DS . $this->getFileName();
248: }
249: catch (Exception $e) {
250: Mage::exception('Mage_Backup', $e->getMessage());
251: }
252:
253: if ($write && $ioAdapter->fileExists($filePath)) {
254: $ioAdapter->rm($filePath);
255: }
256: if (!$write && !$ioAdapter->fileExists($filePath)) {
257: Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" does not exist.', $this->getFileName()));
258: }
259:
260: $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
261:
262: $this->_handler = @gzopen($filePath, $mode);
263:
264: if (!$this->_handler) {
265: throw new Mage_Backup_Exception_NotEnoughPermissions(
266: Mage::helper('backup')->__('Backup file "%s" cannot be read from or written to.', $this->getFileName())
267: );
268: }
269:
270: return $this;
271: }
272:
273: 274: 275: 276: 277: 278:
279: public function read($length)
280: {
281: if (is_null($this->_handler)) {
282: Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
283: }
284:
285: return gzread($this->_handler, $length);
286: }
287:
288: public function eof()
289: {
290: if (is_null($this->_handler)) {
291: Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
292: }
293:
294: return gzeof($this->_handler);
295: }
296:
297: 298: 299: 300: 301: 302:
303: public function write($string)
304: {
305: if (is_null($this->_handler)) {
306: Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
307: }
308:
309: try {
310: gzwrite($this->_handler, $string);
311: }
312: catch (Exception $e) {
313: Mage::exception('Mage_Backup', Mage::helper('backup')->__('An error occurred while writing to the backup file "%s".', $this->getFileName()));
314: }
315:
316: return $this;
317: }
318:
319: 320: 321: 322: 323:
324: public function close()
325: {
326: @gzclose($this->_handler);
327: $this->_handler = null;
328:
329: return $this;
330: }
331:
332: 333: 334: 335:
336: public function output()
337: {
338: if (!$this->exists()) {
339: return ;
340: }
341:
342: $ioAdapter = new Varien_Io_File();
343: $ioAdapter->open(array('path' => $this->getPath()));
344:
345: $ioAdapter->streamOpen($this->getFileName(), 'r');
346: while ($buffer = $ioAdapter->streamRead()) {
347: echo $buffer;
348: }
349: $ioAdapter->streamClose();
350: }
351:
352: public function getSize()
353: {
354: if (!is_null($this->getData('size'))) {
355: return $this->getData('size');
356: }
357:
358: if ($this->exists()) {
359: $this->setData('size', filesize($this->getPath() . DS . $this->getFileName()));
360: return $this->getData('size');
361: }
362:
363: return 0;
364: }
365:
366: 367: 368: 369: 370: 371:
372: public function validateUserPassword($password)
373: {
374: $userPasswordHash = Mage::getModel('admin/session')->getUser()->getPassword();
375: return Mage::helper('core')->validateHash($password, $userPasswordHash);
376: }
377:
378: 379: 380: 381: 382: 383: 384:
385: public function loadByTimeAndType($timestamp, $type)
386: {
387: $backupsCollection = Mage::getSingleton('backup/fs_collection');
388: $backupId = $timestamp . '_' . $type;
389:
390: foreach ($backupsCollection as $backup) {
391: if ($backup->getId() == $backupId) {
392: $this->setType($backup->getType())
393: ->setTime($backup->getTime())
394: ->setName($backup->getName())
395: ->setPath($backup->getPath());
396: break;
397: }
398: }
399:
400: return $this;
401: }
402: }
403: