Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Backup.php
Go to the documentation of this file.
1 <?php
7 
10 
21 class Backup extends \Magento\Framework\DataObject implements \Magento\Framework\Backup\Db\BackupInterface
22 {
26  const COMPRESS_RATE = 9;
27 
33  private $_type = 'db';
34 
40  protected $_stream = null;
41 
45  protected $_filesystem;
46 
50  protected $_helper;
51 
57  protected $_localeResolver;
58 
65 
69  protected $_encryptor;
70 
74  protected $varDirectory;
75 
84  public function __construct(
85  \Magento\Backup\Helper\Data $helper,
86  \Magento\Framework\Locale\ResolverInterface $localeResolver,
87  \Magento\Backend\Model\Auth\Session $authSession,
88  \Magento\Framework\Encryption\EncryptorInterface $encryptor,
89  \Magento\Framework\Filesystem $filesystem,
90  $data = []
91  ) {
92  $this->_encryptor = $encryptor;
93  parent::__construct($data);
94 
95  $this->_filesystem = $filesystem;
96  $this->varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
97  $this->_helper = $helper;
98  $this->_localeResolver = $localeResolver;
99  $this->_backendAuthSession = $authSession;
100  }
101 
108  public function setTime($time)
109  {
110  $this->setData('time', $time);
111  return $this;
112  }
113 
120  public function setPath($path)
121  {
122  $this->setData('path', $path);
123  return $this;
124  }
125 
132  public function setName($name)
133  {
134  $this->setData('name', $name);
135  return $this;
136  }
137 
145  public function load($fileName, $filePath)
146  {
147  $backupData = $this->_helper->extractDataFromFilename($fileName);
148 
149  $this->addData(
150  [
151  'id' => $filePath . '/' . $fileName,
152  'time' => (int)$backupData->getTime(),
153  'path' => $filePath,
154  'extension' => $this->_helper->getExtensionByType($backupData->getType()),
155  'display_name' => $this->_helper->nameToDisplayName($backupData->getName()),
156  'name' => $backupData->getName(),
157  'date_object' => (new \DateTime())->setTimestamp($backupData->getTime()),
158  ]
159  );
160 
161  $this->setType($backupData->getType());
162  return $this;
163  }
164 
170  public function exists()
171  {
172  return $this->varDirectory->isFile($this->_getFilePath());
173  }
174 
180  public function getFileName()
181  {
182  $filename = $this->getTime() . "_" . $this->getType();
183  $backupName = $this->getName();
184 
185  if (!empty($backupName)) {
186  $filename .= '_' . $backupName;
187  }
188 
189  $filename .= '.' . $this->_helper->getExtensionByType($this->getType());
190 
191  return $filename;
192  }
193 
200  public function setType($value = 'db')
201  {
202  $possibleTypes = $this->_helper->getBackupTypesList();
203  if (!in_array($value, $possibleTypes)) {
204  $value = $this->_helper->getDefaultBackupType();
205  }
206 
207  $this->_type = $value;
208  $this->setData('type', $this->_type);
209 
210  return $this;
211  }
212 
218  public function getType()
219  {
220  return $this->_type;
221  }
222 
230  public function setFile(&$content)
231  {
232  if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
233  throw new \Magento\Framework\Exception\LocalizedException(
234  __('Please correct the order of creation for a new backup.')
235  );
236  }
237 
238  $this->varDirectory->writeFile($this->_getFilePath(), $content);
239  return $this;
240  }
241 
248  public function &getFile()
249  {
250  if (!$this->exists()) {
251  throw new \Magento\Framework\Exception\LocalizedException(__('The backup file does not exist.'));
252  }
253 
254  return $this->varDirectory->read($this->_getFilePath());
255  }
256 
263  public function deleteFile()
264  {
265  if (!$this->exists()) {
266  throw new \Magento\Framework\Exception\LocalizedException(__('The backup file does not exist.'));
267  }
268 
269  $this->varDirectory->delete($this->_getFilePath());
270  return $this;
271  }
272 
281  public function open($write = false)
282  {
283  if ($this->getPath() === null) {
284  throw new \Magento\Framework\Exception\InputException(__('The backup file path was not specified.'));
285  }
286 
287  if ($write && $this->varDirectory->isFile($this->_getFilePath())) {
288  $this->varDirectory->delete($this->_getFilePath());
289  }
290  if (!$write && !$this->varDirectory->isFile($this->_getFilePath())) {
291  throw new \Magento\Framework\Exception\InputException(
292  __('The backup file "%1" does not exist.', $this->getFileName())
293  );
294  }
295 
296  $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
297 
298  try {
300  $varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB);
301  $this->_stream = $varDirectory->openFile(
302  $this->_getFilePath(),
303  $mode
304  );
305  } catch (\Magento\Framework\Exception\FileSystemException $e) {
306  throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(
307  __('Sorry, but we cannot read from or write to backup file "%1".', $this->getFileName())
308  );
309  }
310 
311  return $this;
312  }
313 
320  protected function _getStream()
321  {
322  if ($this->_stream === null) {
323  throw new \Magento\Framework\Exception\InputException(__('The backup file handler was unspecified.'));
324  }
325  return $this->_stream;
326  }
327 
334  public function read($length)
335  {
336  return $this->_getStream()->read($length);
337  }
338 
344  public function eof()
345  {
346  return $this->_getStream()->eof();
347  }
348 
356  public function write($string)
357  {
358  try {
359  $this->_getStream()->write($string);
360  } catch (\Magento\Framework\Exception\FileSystemException $e) {
361  throw new \Magento\Framework\Exception\InputException(
362  __('Something went wrong while writing to the backup file "%1".', $this->getFileName())
363  );
364  }
365 
366  return $this;
367  }
368 
374  public function close()
375  {
376  $this->_getStream()->close();
377  $this->_stream = null;
378 
379  return $this;
380  }
381 
387  public function output()
388  {
389  if (!$this->exists()) {
390  return;
391  }
392 
394  $directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
395  $directory = $directory->readFile($this->_getFilePath());
396 
397  return $directory;
398  }
399 
403  public function getSize()
404  {
405  if ($this->getData('size') !== null) {
406  return $this->getData('size');
407  }
408 
409  if ($this->exists()) {
410  $this->setData('size', $this->varDirectory->stat($this->_getFilePath())['size']);
411  return $this->getData('size');
412  }
413 
414  return 0;
415  }
416 
423  public function validateUserPassword($password)
424  {
425  $userPasswordHash = $this->_backendAuthSession->getUser()->getPassword();
426  return $this->_encryptor->validateHash($password, $userPasswordHash);
427  }
428 
434  protected function _getFilePath()
435  {
436  return $this->varDirectory->getRelativePath($this->getPath() . '/' . $this->getFileName());
437  }
438 }
output($string, $level=INFO, $label='')
$helper
Definition: iframe.phtml:13
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(\Magento\Backup\Helper\Data $helper, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Backend\Model\Auth\Session $authSession, \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Framework\Filesystem $filesystem, $data=[])
Definition: Backup.php:84
load($fileName, $filePath)
Definition: Backup.php:145
validateUserPassword($password)
Definition: Backup.php:423
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
setData($key, $value=null)
Definition: DataObject.php:72
$filesystem
if(!isset($_GET['name'])) $name
Definition: log.php:14