PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-backup/Model/Backup.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 436 lines | 205 code | 53 blank | 178 comment | 16 complexity | f4120b26a7ad16d19c39e537170ea711 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backup\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. /**
  10. * Backup file item model
  11. *
  12. * @method string getPath()
  13. * @method string getName()
  14. * @method string getTime()
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Backup extends \Magento\Framework\DataObject implements \Magento\Framework\Backup\Db\BackupInterface
  18. {
  19. /**
  20. * Compress rate
  21. */
  22. const COMPRESS_RATE = 9;
  23. /**
  24. * Type of backup file
  25. *
  26. * @var string
  27. */
  28. private $_type = 'db';
  29. /**
  30. * Gz file pointer
  31. *
  32. * @var \Magento\Framework\Filesystem\File\WriteInterface
  33. */
  34. protected $_stream = null;
  35. /**
  36. * @var \Magento\Framework\Filesystem
  37. */
  38. protected $_filesystem;
  39. /**
  40. * @var \Magento\Backup\Helper\Data
  41. */
  42. protected $_helper;
  43. /**
  44. * Locale model
  45. *
  46. * @var \Magento\Framework\Locale\ResolverInterface
  47. */
  48. protected $_localeResolver;
  49. /**
  50. * Backend auth session
  51. *
  52. * @var \Magento\Backend\Model\Auth\Session
  53. */
  54. protected $_backendAuthSession;
  55. /**
  56. * @var \Magento\Framework\Encryption\EncryptorInterface
  57. */
  58. protected $_encryptor;
  59. /**
  60. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  61. */
  62. protected $varDirectory;
  63. /**
  64. * @param \Magento\Backup\Helper\Data $helper
  65. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  66. * @param \Magento\Backend\Model\Auth\Session $authSession
  67. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  68. * @param \Magento\Framework\Filesystem $filesystem
  69. * @param array $data
  70. */
  71. public function __construct(
  72. \Magento\Backup\Helper\Data $helper,
  73. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  74. \Magento\Backend\Model\Auth\Session $authSession,
  75. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  76. \Magento\Framework\Filesystem $filesystem,
  77. $data = []
  78. ) {
  79. $this->_encryptor = $encryptor;
  80. parent::__construct($data);
  81. $this->_filesystem = $filesystem;
  82. $this->varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  83. $this->_helper = $helper;
  84. $this->_localeResolver = $localeResolver;
  85. $this->_backendAuthSession = $authSession;
  86. }
  87. /**
  88. * Set backup time
  89. *
  90. * @param int $time
  91. * @return $this
  92. */
  93. public function setTime($time)
  94. {
  95. $this->setData('time', $time);
  96. return $this;
  97. }
  98. /**
  99. * Set backup path
  100. *
  101. * @param string $path
  102. * @return $this
  103. */
  104. public function setPath($path)
  105. {
  106. $this->setData('path', $path);
  107. return $this;
  108. }
  109. /**
  110. * Set backup name
  111. *
  112. * @param string $name
  113. * @return $this
  114. */
  115. public function setName($name)
  116. {
  117. $this->setData('name', $name);
  118. return $this;
  119. }
  120. /**
  121. * Load backup file info
  122. *
  123. * @param string $fileName
  124. * @param string $filePath
  125. * @return $this
  126. */
  127. public function load($fileName, $filePath)
  128. {
  129. $backupData = $this->_helper->extractDataFromFilename($fileName);
  130. $this->addData(
  131. [
  132. 'id' => $filePath . '/' . $fileName,
  133. 'time' => (int)$backupData->getTime(),
  134. 'path' => $filePath,
  135. 'extension' => $this->_helper->getExtensionByType($backupData->getType()),
  136. 'display_name' => $this->_helper->nameToDisplayName($backupData->getName()),
  137. 'name' => $backupData->getName(),
  138. 'date_object' => (new \DateTime())->setTimestamp($backupData->getTime()),
  139. ]
  140. );
  141. $this->setType($backupData->getType());
  142. return $this;
  143. }
  144. /**
  145. * Checks backup file exists.
  146. *
  147. * @return bool
  148. */
  149. public function exists()
  150. {
  151. return $this->varDirectory->isFile($this->_getFilePath());
  152. }
  153. /**
  154. * Return file name of backup file
  155. *
  156. * @return string
  157. */
  158. public function getFileName()
  159. {
  160. $filename = $this->getTime() . "_" . $this->getType();
  161. $backupName = $this->getName();
  162. if (!empty($backupName)) {
  163. $filename .= '_' . $backupName;
  164. }
  165. $filename .= '.' . $this->_helper->getExtensionByType($this->getType());
  166. return $filename;
  167. }
  168. /**
  169. * Sets type of file
  170. *
  171. * @param string $value
  172. * @return $this
  173. */
  174. public function setType($value = 'db')
  175. {
  176. $possibleTypes = $this->_helper->getBackupTypesList();
  177. if (!in_array($value, $possibleTypes)) {
  178. $value = $this->_helper->getDefaultBackupType();
  179. }
  180. $this->_type = $value;
  181. $this->setData('type', $this->_type);
  182. return $this;
  183. }
  184. /**
  185. * Returns type of backup file
  186. *
  187. * @return string
  188. */
  189. public function getType()
  190. {
  191. return $this->_type;
  192. }
  193. /**
  194. * Set the backup file content
  195. *
  196. * @param string &$content
  197. * @return $this
  198. * @throws \Magento\Framework\Exception\LocalizedException
  199. */
  200. public function setFile(&$content)
  201. {
  202. if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
  203. throw new \Magento\Framework\Exception\LocalizedException(
  204. __('Please correct the order of creation for a new backup.')
  205. );
  206. }
  207. $this->varDirectory->writeFile($this->_getFilePath(), $content);
  208. return $this;
  209. }
  210. /**
  211. * Return content of backup file
  212. *
  213. * @return string
  214. * @throws \Magento\Framework\Exception\LocalizedException
  215. */
  216. public function &getFile()
  217. {
  218. if (!$this->exists()) {
  219. throw new \Magento\Framework\Exception\LocalizedException(__('The backup file does not exist.'));
  220. }
  221. return $this->varDirectory->read($this->_getFilePath());
  222. }
  223. /**
  224. * Delete backup file
  225. *
  226. * @return $this
  227. * @throws \Magento\Framework\Exception\LocalizedException
  228. */
  229. public function deleteFile()
  230. {
  231. if (!$this->exists()) {
  232. throw new \Magento\Framework\Exception\LocalizedException(__('The backup file does not exist.'));
  233. }
  234. $this->varDirectory->delete($this->_getFilePath());
  235. return $this;
  236. }
  237. /**
  238. * Open backup file (write or read mode)
  239. *
  240. * @param bool $write
  241. * @return $this
  242. * @throws \Magento\Framework\Exception\InputException
  243. * @throws \Magento\Framework\Backup\Exception\NotEnoughPermissions
  244. */
  245. public function open($write = false)
  246. {
  247. if ($this->getPath() === null) {
  248. throw new \Magento\Framework\Exception\InputException(__('The backup file path was not specified.'));
  249. }
  250. if ($write && $this->varDirectory->isFile($this->_getFilePath())) {
  251. $this->varDirectory->delete($this->_getFilePath());
  252. }
  253. if (!$write && !$this->varDirectory->isFile($this->_getFilePath())) {
  254. throw new \Magento\Framework\Exception\InputException(
  255. __('The backup file "%1" does not exist.', $this->getFileName())
  256. );
  257. }
  258. $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
  259. try {
  260. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */
  261. $varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB);
  262. $this->_stream = $varDirectory->openFile(
  263. $this->_getFilePath(),
  264. $mode
  265. );
  266. } catch (\Magento\Framework\Exception\FileSystemException $e) {
  267. throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(
  268. __('Sorry, but we cannot read from or write to backup file "%1".', $this->getFileName())
  269. );
  270. }
  271. return $this;
  272. }
  273. /**
  274. * Get zlib handler
  275. *
  276. * @return \Magento\Framework\Filesystem\File\WriteInterface
  277. * @throws \Magento\Framework\Exception\InputException
  278. */
  279. protected function _getStream()
  280. {
  281. if ($this->_stream === null) {
  282. throw new \Magento\Framework\Exception\InputException(__('The backup file handler was unspecified.'));
  283. }
  284. return $this->_stream;
  285. }
  286. /**
  287. * Read backup uncompressed data
  288. *
  289. * @param int $length
  290. * @return string
  291. */
  292. public function read($length)
  293. {
  294. return $this->_getStream()->read($length);
  295. }
  296. /**
  297. * Check end of file.
  298. *
  299. * @return bool
  300. */
  301. public function eof()
  302. {
  303. return $this->_getStream()->eof();
  304. }
  305. /**
  306. * Write to backup file
  307. *
  308. * @param string $string
  309. * @return $this
  310. * @throws \Magento\Framework\Exception\InputException
  311. */
  312. public function write($string)
  313. {
  314. try {
  315. $this->_getStream()->write($string);
  316. } catch (\Magento\Framework\Exception\FileSystemException $e) {
  317. throw new \Magento\Framework\Exception\InputException(
  318. __('Something went wrong while writing to the backup file "%1".', $this->getFileName())
  319. );
  320. }
  321. return $this;
  322. }
  323. /**
  324. * Close open backup file
  325. *
  326. * @return $this
  327. */
  328. public function close()
  329. {
  330. $this->_getStream()->close();
  331. $this->_stream = null;
  332. return $this;
  333. }
  334. /**
  335. * Print output
  336. *
  337. * @return string
  338. */
  339. public function output()
  340. {
  341. if (!$this->exists()) {
  342. return;
  343. }
  344. /** @var \Magento\Framework\Filesystem\Directory\ReadInterface $directory */
  345. $directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  346. $directory = $directory->readFile($this->_getFilePath());
  347. return $directory;
  348. }
  349. /**
  350. * @return int|mixed
  351. */
  352. public function getSize()
  353. {
  354. if ($this->getData('size') !== null) {
  355. return $this->getData('size');
  356. }
  357. if ($this->exists()) {
  358. $this->setData('size', $this->varDirectory->stat($this->_getFilePath())['size']);
  359. return $this->getData('size');
  360. }
  361. return 0;
  362. }
  363. /**
  364. * Validate user password
  365. *
  366. * @param string $password
  367. * @return bool
  368. */
  369. public function validateUserPassword($password)
  370. {
  371. $userPasswordHash = $this->_backendAuthSession->getUser()->getPassword();
  372. return $this->_encryptor->validateHash($password, $userPasswordHash);
  373. }
  374. /**
  375. * Get file path.
  376. *
  377. * @return string
  378. */
  379. protected function _getFilePath()
  380. {
  381. return $this->varDirectory->getRelativePath($this->getPath() . '/' . $this->getFileName());
  382. }
  383. }