PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Backup/Model/Backup.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 402 lines | 215 code | 57 blank | 130 comment | 31 complexity | 163966b51cb4df0584200c39375b21ae MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Backup
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Backup file item model
  28. *
  29. * @category Mage
  30. * @package Mage_Backup
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Backup_Model_Backup extends Varien_Object
  34. {
  35. /* internal constants */
  36. const COMPRESS_RATE = 9;
  37. /**
  38. * Type of backup file
  39. *
  40. * @var string
  41. */
  42. private $_type = 'db';
  43. /**
  44. * Gz file pointer
  45. *
  46. * @var resource
  47. */
  48. protected $_handler = null;
  49. /**
  50. * Load backup file info
  51. *
  52. * @param string fileName
  53. * @param string filePath
  54. * @return Mage_Backup_Model_Backup
  55. */
  56. public function load($fileName, $filePath)
  57. {
  58. $backupData = Mage::helper('backup')->extractDataFromFilename($fileName);
  59. $this->addData(array(
  60. 'id' => $filePath . DS . $fileName,
  61. 'time' => (int)$backupData->getTime(),
  62. 'path' => $filePath,
  63. 'extension' => Mage::helper('backup')->getExtensionByType($backupData->getType()),
  64. 'display_name' => Mage::helper('backup')->nameToDisplayName($backupData->getName()),
  65. 'name' => $backupData->getName(),
  66. 'date_object' => new Zend_Date((int)$backupData->getTime(), Mage::app()->getLocale()->getLocaleCode())
  67. ));
  68. $this->setType($backupData->getType());
  69. return $this;
  70. }
  71. /**
  72. * Checks backup file exists.
  73. *
  74. * @return boolean
  75. */
  76. public function exists()
  77. {
  78. return is_file($this->getPath() . DS . $this->getFileName());
  79. }
  80. /**
  81. * Return file name of backup file
  82. *
  83. * @return string
  84. */
  85. public function getFileName()
  86. {
  87. $filename = $this->getTime() . "_" . $this->getType();
  88. $backupName = $this->getName();
  89. if (!empty($backupName)) {
  90. $filename .= '_' . $backupName;
  91. }
  92. $filename .= '.' . Mage::helper('backup')->getExtensionByType($this->getType());
  93. return $filename;
  94. }
  95. /**
  96. * Sets type of file
  97. *
  98. * @param string $value
  99. * @return Mage_Backup_Model_Backup
  100. */
  101. public function setType($value='db')
  102. {
  103. $possibleTypes = Mage::helper('backup')->getBackupTypesList();
  104. if(!in_array($value, $possibleTypes)) {
  105. $value = Mage::helper('backup')->getDefaultBackupType();
  106. }
  107. $this->_type = $value;
  108. $this->setData('type', $this->_type);
  109. return $this;
  110. }
  111. /**
  112. * Returns type of backup file
  113. *
  114. * @return string
  115. */
  116. public function getType()
  117. {
  118. return $this->_type;
  119. }
  120. /**
  121. * Set the backup file content
  122. *
  123. * @param string $content
  124. * @return Mage_Backup_Model_Backup
  125. * @throws Mage_Backup_Exception
  126. */
  127. public function setFile(&$content)
  128. {
  129. if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
  130. Mage::throwException(Mage::helper('backup')->__('Wrong order of creation for new backup.'));
  131. }
  132. $ioProxy = new Varien_Io_File();
  133. $ioProxy->setAllowCreateFolders(true);
  134. $ioProxy->open(array('path'=>$this->getPath()));
  135. $compress = 0;
  136. if (extension_loaded("zlib")) {
  137. $compress = 1;
  138. }
  139. $rawContent = '';
  140. if ( $compress ) {
  141. $rawContent = gzcompress( $content, self::COMPRESS_RATE );
  142. } else {
  143. $rawContent = $content;
  144. }
  145. $fileHeaders = pack("ll", $compress, strlen($rawContent));
  146. $ioProxy->write($this->getFileName(), $fileHeaders . $rawContent);
  147. return $this;
  148. }
  149. /**
  150. * Return content of backup file
  151. *
  152. * @todo rewrite to Varien_IO, but there no possibility read part of files.
  153. * @return string
  154. * @throws Mage_Backup_Exception
  155. */
  156. public function &getFile()
  157. {
  158. if (!$this->exists()) {
  159. Mage::throwException(Mage::helper('backup')->__("Backup file does not exist."));
  160. }
  161. $fResource = @fopen($this->getPath() . DS . $this->getFileName(), "rb");
  162. if (!$fResource) {
  163. Mage::throwException(Mage::helper('backup')->__("Cannot read backup file."));
  164. }
  165. $content = '';
  166. $compressed = 0;
  167. $info = unpack("lcompress/llength", fread($fResource, 8));
  168. if ($info['compress']) { // If file compressed by zlib
  169. $compressed = 1;
  170. }
  171. if ($compressed && !extension_loaded("zlib")) {
  172. fclose($fResource);
  173. Mage::throwException(Mage::helper('backup')->__('The file was compressed with Zlib, but this extension is not installed on server.'));
  174. }
  175. if ($compressed) {
  176. $content = gzuncompress(fread($fResource, $info['length']));
  177. } else {
  178. $content = fread($fResource, $info['length']);
  179. }
  180. fclose($fResource);
  181. return $content;
  182. }
  183. /**
  184. * Delete backup file
  185. *
  186. * @throws Mage_Backup_Exception
  187. * @return Mage_Backup_Model_Backup
  188. */
  189. public function deleteFile()
  190. {
  191. if (!$this->exists()) {
  192. Mage::throwException(Mage::helper('backup')->__("Backup file does not exist."));
  193. }
  194. $ioProxy = new Varien_Io_File();
  195. $ioProxy->open(array('path'=>$this->getPath()));
  196. $ioProxy->rm($this->getFileName());
  197. return $this;
  198. }
  199. /**
  200. * Open backup file (write or read mode)
  201. *
  202. * @param bool $write
  203. * @return Mage_Backup_Model_Backup
  204. */
  205. public function open($write = false)
  206. {
  207. if (is_null($this->getPath())) {
  208. Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file path was not specified.'));
  209. }
  210. $ioAdapter = new Varien_Io_File();
  211. try {
  212. $path = $ioAdapter->getCleanPath($this->getPath());
  213. $ioAdapter->checkAndCreateFolder($path);
  214. $filePath = $path . DS . $this->getFileName();
  215. }
  216. catch (Exception $e) {
  217. Mage::exception('Mage_Backup', $e->getMessage());
  218. }
  219. if ($write && $ioAdapter->fileExists($filePath)) {
  220. $ioAdapter->rm($filePath);
  221. }
  222. if (!$write && !$ioAdapter->fileExists($filePath)) {
  223. Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" does not exist.', $this->getFileName()));
  224. }
  225. $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
  226. $this->_handler = @gzopen($filePath, $mode);
  227. if (!$this->_handler) {
  228. throw new Mage_Backup_Exception_NotEnoughPermissions(
  229. Mage::helper('backup')->__('Backup file "%s" cannot be read from or written to.', $this->getFileName())
  230. );
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Read backup uncomressed data
  236. *
  237. * @param int $length
  238. * @return string
  239. */
  240. public function read($length)
  241. {
  242. if (is_null($this->_handler)) {
  243. Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
  244. }
  245. return gzread($this->_handler, $length);
  246. }
  247. public function eof()
  248. {
  249. if (is_null($this->_handler)) {
  250. Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
  251. }
  252. return gzeof($this->_handler);
  253. }
  254. /**
  255. * Write to backup file
  256. *
  257. * @param string $string
  258. * @return Mage_Backup_Model_Backup
  259. */
  260. public function write($string)
  261. {
  262. if (is_null($this->_handler)) {
  263. Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler was unspecified.'));
  264. }
  265. try {
  266. gzwrite($this->_handler, $string);
  267. }
  268. catch (Exception $e) {
  269. Mage::exception('Mage_Backup', Mage::helper('backup')->__('An error occurred while writing to the backup file "%s".', $this->getFileName()));
  270. }
  271. return $this;
  272. }
  273. /**
  274. * Close open backup file
  275. *
  276. * @return Mage_Backup_Model_Backup
  277. */
  278. public function close()
  279. {
  280. @gzclose($this->_handler);
  281. $this->_handler = null;
  282. return $this;
  283. }
  284. /**
  285. * Print output
  286. *
  287. */
  288. public function output()
  289. {
  290. if (!$this->exists()) {
  291. return ;
  292. }
  293. $ioAdapter = new Varien_Io_File();
  294. $ioAdapter->open(array('path' => $this->getPath()));
  295. $ioAdapter->streamOpen($this->getFileName(), 'r');
  296. while ($buffer = $ioAdapter->streamRead()) {
  297. echo $buffer;
  298. }
  299. $ioAdapter->streamClose();
  300. }
  301. public function getSize()
  302. {
  303. if (!is_null($this->getData('size'))) {
  304. return $this->getData('size');
  305. }
  306. if ($this->exists()) {
  307. $this->setData('size', filesize($this->getPath() . DS . $this->getFileName()));
  308. return $this->getData('size');
  309. }
  310. return 0;
  311. }
  312. /**
  313. * Validate user password
  314. *
  315. * @param string $password
  316. * @return bool
  317. */
  318. public function validateUserPassword($password)
  319. {
  320. $userPasswordHash = Mage::getModel('admin/session')->getUser()->getPassword();
  321. return Mage::helper('core')->validateHash($password, $userPasswordHash);
  322. }
  323. /**
  324. * Load backup by it's type and creation timestamp
  325. *
  326. * @param int $timestamp
  327. * @param string $type
  328. * @return Mage_Backup_Model_Backup
  329. */
  330. public function loadByTimeAndType($timestamp, $type)
  331. {
  332. $backupsCollection = Mage::getSingleton('backup/fs_collection');
  333. $backupId = $timestamp . '_' . $type;
  334. foreach ($backupsCollection as $backup) {
  335. if ($backup->getId() == $backupId) {
  336. $this->setType($backup->getType())
  337. ->setTime($backup->getTime())
  338. ->setName($backup->getName())
  339. ->setPath($backup->getPath());
  340. break;
  341. }
  342. }
  343. return $this;
  344. }
  345. }