Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Backup_Exception
  • Mage_Backup_Helper_Data
  • Mage_Backup_Model_Backup
  • Mage_Backup_Model_Config_Backend_Cron
  • Mage_Backup_Model_Config_Source_Type
  • Mage_Backup_Model_Db
  • Mage_Backup_Model_Fs_Collection
  • Mage_Backup_Model_Mysql4_Db
  • Mage_Backup_Model_Observer
  • Mage_Backup_Model_Resource_Db
  • Mage_Backup_Model_Resource_Helper_Mysql4
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Backup
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * Backup file item model
 29:  *
 30:  * @category   Mage
 31:  * @package    Mage_Backup
 32:  * @author     Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Backup_Model_Backup extends Varien_Object
 35: {
 36:     /* internal constants */
 37:     const COMPRESS_RATE     = 9;
 38: 
 39:     /**
 40:      * Type of backup file
 41:      *
 42:      * @var string
 43:      */
 44:     private $_type  = 'db';
 45: 
 46:     /**
 47:      * Gz file pointer
 48:      *
 49:      * @var resource
 50:      */
 51:     protected $_handler = null;
 52: 
 53:     /**
 54:      * Load backup file info
 55:      *
 56:      * @param string fileName
 57:      * @param string filePath
 58:      * @return Mage_Backup_Model_Backup
 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:      * Checks backup file exists.
 80:      *
 81:      * @return boolean
 82:      */
 83:     public function exists()
 84:     {
 85:         return is_file($this->getPath() . DS . $this->getFileName());
 86:     }
 87: 
 88:     /**
 89:      * Return file name of backup file
 90:      *
 91:      * @return string
 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:      * Sets type of file
109:      *
110:      * @param string $value
111:      * @return Mage_Backup_Model_Backup
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:      * Returns type of backup file
128:      *
129:      * @return string
130:      */
131:     public function getType()
132:     {
133:         return $this->_type;
134:     }
135: 
136:     /**
137:      * Set the backup file content
138:      *
139:      * @param string $content
140:      * @return Mage_Backup_Model_Backup
141:      * @throws Mage_Backup_Exception
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:      * Return content of backup file
172:      *
173:      * @todo rewrite to Varien_IO, but there no possibility read part of files.
174:      * @return string
175:      * @throws Mage_Backup_Exception
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']) { // If file compressed by zlib
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:      * Delete backup file
215:      *
216:      * @throws Mage_Backup_Exception
217:      * @return Mage_Backup_Model_Backup
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:      * Open backup file (write or read mode)
233:      *
234:      * @param bool $write
235:      * @return Mage_Backup_Model_Backup
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:      * Read backup uncomressed data
275:      *
276:      * @param int $length
277:      * @return string
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:      * Write to backup file
299:      *
300:      * @param string $string
301:      * @return Mage_Backup_Model_Backup
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:      * Close open backup file
321:      *
322:      * @return Mage_Backup_Model_Backup
323:      */
324:     public function close()
325:     {
326:         @gzclose($this->_handler);
327:         $this->_handler = null;
328: 
329:         return $this;
330:     }
331: 
332:     /**
333:      * Print output
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:      * Validate user password
368:      *
369:      * @param string $password
370:      * @return bool
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:      * Load backup by it's type and creation timestamp
380:      *
381:      * @param int $timestamp
382:      * @param string $type
383:      * @return Mage_Backup_Model_Backup
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0