Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rollback.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
25  public function execute()
26  {
27  if (!$this->_objectManager->get(\Magento\Backup\Helper\Data::class)->isRollbackAllowed()) {
28  $this->_forward('denied');
29  }
30 
31  if (!$this->getRequest()->isAjax()) {
32  return $this->_redirect('*/*/index');
33  }
34 
35  $helper = $this->_objectManager->get(\Magento\Backup\Helper\Data::class);
36  $response = new \Magento\Framework\DataObject();
37 
38  try {
39  /* @var $backup \Magento\Backup\Model\Backup */
40  $backup = $this->_backupModelFactory->create(
41  $this->getRequest()->getParam('time'),
42  $this->getRequest()->getParam('type')
43  );
44 
45  if (!$backup->getTime() || !$backup->exists()) {
46  return $this->_redirect('backup/*');
47  }
48 
49  if (!$backup->getTime()) {
50  throw new \Magento\Framework\Backup\Exception\CantLoadSnapshot(__('Can\'t load snapshot archive'));
51  }
52 
53  $type = $backup->getType();
54 
55  $backupManager = $this->_backupFactory->create(
56  $type
57  )->setBackupExtension(
58  $helper->getExtensionByType($type)
59  )->setTime(
60  $backup->getTime()
61  )->setBackupsDir(
62  $helper->getBackupsDir()
63  )->setName(
64  $backup->getName(),
65  false
66  )->setResourceModel(
67  $this->_objectManager->create(\Magento\Backup\Model\ResourceModel\Db::class)
68  );
69 
70  $this->_coreRegistry->register('backup_manager', $backupManager);
71 
72  $passwordValid = $this->_objectManager->create(
73  \Magento\Backup\Model\Backup::class
74  )->validateUserPassword(
75  $this->getRequest()->getParam('password')
76  );
77 
78  if (!$passwordValid) {
79  $response->setError(__('Please correct the password.'));
80  $backupManager->setErrorMessage(__('Please correct the password.'));
81  return $this->getResponse()->representJson($response->toJson());
82  }
83 
84  if ($this->getRequest()->getParam('maintenance_mode')) {
85  if (!$this->maintenanceMode->set(true)) {
86  $response->setError(
87  __(
88  'You need more permissions to activate maintenance mode right now.'
89  ) . ' ' . __(
90  'To complete the rollback, please deselect '
91  . '"Put store into maintenance mode" or update your permissions.'
92  )
93  );
94  $backupManager->setErrorMessage(
95  __('Something went wrong while putting your store into maintenance mode.')
96  );
97  return $this->getResponse()->representJson($response->toJson());
98  }
99  }
100 
101  if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) {
103  $filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
104  $backupManager->setRootDir($filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath())
105  ->addIgnorePaths($helper->getRollbackIgnorePaths());
106 
107  if ($this->getRequest()->getParam('use_ftp', false)) {
108  $backupManager->setUseFtp(
109  $this->getRequest()->getParam('ftp_host', ''),
110  $this->getRequest()->getParam('ftp_user', ''),
111  $this->getRequest()->getParam('ftp_pass', ''),
112  $this->getRequest()->getParam('ftp_path', '')
113  );
114  }
115  }
116 
117  $backupManager->rollback();
118 
119  $helper->invalidateCache();
120 
121  $adminSession = $this->_getSession();
122  $adminSession->destroy();
123 
124  $response->setRedirectUrl($this->getUrl('*'));
125  } catch (\Magento\Framework\Backup\Exception\CantLoadSnapshot $e) {
126  $errorMsg = __('We can\'t find the backup file.');
127  } catch (\Magento\Framework\Backup\Exception\FtpConnectionFailed $e) {
128  $errorMsg = __('We can\'t connect to the FTP right now.');
129  } catch (\Magento\Framework\Backup\Exception\FtpValidationFailed $e) {
130  $errorMsg = __('Failed to validate FTP.');
131  } catch (\Magento\Framework\Backup\Exception\NotEnoughPermissions $e) {
132  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->info($e->getMessage());
133  $errorMsg = __('You need more permissions to perform a rollback.');
134  } catch (\Exception $e) {
135  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->info($e->getMessage());
136  $errorMsg = __('Failed to rollback.');
137  }
138 
139  if (!empty($errorMsg)) {
140  $response->setError($errorMsg);
141  $backupManager->setErrorMessage($errorMsg);
142  }
143 
144  if ($this->getRequest()->getParam('maintenance_mode')) {
145  $this->maintenanceMode->set(false);
146  }
147 
148  $this->getResponse()->representJson($response->toJson());
149  }
150 }
$response
Definition: 404.php:11
$helper
Definition: iframe.phtml:13
return false
Definition: gallery.phtml:36
__()
Definition: __.php:13
$type
Definition: item.phtml:13
_forward($action, $controller=null, $module=null, array $params=null)
$filesystem