*/ class Mage_Connect_Backup { /** * Prefix for backuped files * * @var string */ protected $_prefix = '_backup_'; /** * Array of available to overwrite type of files * * @var array */ protected $_fileTypes = array(); /** * List of files to backup files * * @var array */ protected $_fileList = array(); /** * Get available file types for backup * * @return array */ public function getFileTypes() { return $this->_fileTypes; } /** * Set available file types for backup * * @param array $types */ public function setFileTypes(array $types) { foreach ($types as $type) { $this->_fileTypes[] = $type; } } /** * Add file to files list for backup * * @param string $file * @param string $rootPath * @return void */ public function addFile($file, $rootPath) { $dest = $rootPath . DS . $file; $type = $this->getFileType($file); if (file_exists($dest) && in_array($type, $this->getFileTypes())) { $this->_fileList[] = $file; } } /** * Get count of files * * @return int */ public function getFilesCount() { return count($this->_fileList); } /** * Clear list of files * * @return void */ public function unsetAllFiles() { $this->_fileList = array(); } /** * Get list of files * * @return array */ public function getAllFiles() { return $this->_fileList; } /** * Run backup process * * @param boolean $cleanUpQueue * @return void */ public function run($cleanUpQueue = false) { if ($this->getFilesCount() > 0) { $fileList = $this->getAllFiles(); foreach($fileList as $file) { $this->_backupFile($file); } if ($cleanUpQueue) { $this->unsetAllFiles(); } } } /** * Get File type * * @param string $file * @return string */ public function getFileType($file) { return pathinfo($file, PATHINFO_EXTENSION); } /** * Backup file * * @param string $file * @return void */ private function _backupFile($file) { $type = $this->getFileType($file); if ($type && $type != '') { $newName = $this->_prefix . time() . '.' . $type; @rename($file, str_replace('.' . $type, $newName, $file)); } } }