Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ftp.php
Go to the documentation of this file.
1 <?php
7 
14 
20 class Ftp extends AbstractRollback
21 {
27  protected $_ftpClient;
28 
37  public function run()
38  {
39  $snapshotPath = $this->_snapshot->getBackupPath();
40 
41  if (!is_file($snapshotPath) || !is_readable($snapshotPath)) {
42  throw new CantLoadSnapshot(
43  new Phrase('Can\'t load snapshot archive')
44  );
45  }
46 
47  $this->_initFtpClient();
48  $this->_validateFtp();
49 
50  $tmpDir = $this->_createTmpDir();
51  $this->_unpackSnapshot($tmpDir);
52 
53  $fsHelper = new Helper();
54 
55  $this->_cleanupFtp();
56  $this->_uploadBackupToFtp($tmpDir);
57 
58  if ($this->_snapshot->keepSourceFile() === false) {
59  $fsHelper->rm($tmpDir, [], true);
60  $this->_ftpClient->delete($snapshotPath);
61  }
62  }
63 
70  protected function _initFtpClient()
71  {
72  try {
73  $this->_ftpClient = new \Magento\Framework\System\Ftp();
74  $this->_ftpClient->connect($this->_snapshot->getFtpConnectString());
75  } catch (\Exception $e) {
76  throw new FtpConnectionFailed(
77  new Phrase($e->getMessage())
78  );
79  }
80  }
81 
88  protected function _validateFtp()
89  {
90  $validationFilename = '~validation-' . microtime(true) . '.tmp';
91  $validationFilePath = $this->_snapshot->getBackupsDir() . '/' . $validationFilename;
92 
93  $fh = @fopen($validationFilePath, 'w');
94  @fclose($fh);
95 
96  if (!is_file($validationFilePath)) {
97  throw new LocalizedException(
98  new Phrase('Unable to validate ftp account')
99  );
100  }
101 
102  $rootDir = $this->_snapshot->getRootDir();
103  $ftpPath = $this->_snapshot->getFtpPath() . '/' . str_replace($rootDir, '', $validationFilePath);
104 
105  $fileExistsOnFtp = $this->_ftpClient->fileExists($ftpPath);
106  @unlink($validationFilePath);
107 
108  if (!$fileExistsOnFtp) {
109  throw new FtpValidationFailed(
110  new Phrase('Failed to validate ftp account')
111  );
112  }
113  }
114 
121  protected function _unpackSnapshot($tmpDir)
122  {
123  $snapshotPath = $this->_snapshot->getBackupPath();
124 
125  $archiver = new \Magento\Framework\Archive();
126  $archiver->unpack($snapshotPath, $tmpDir);
127  }
128 
133  protected function _createTmpDir()
134  {
135  $tmpDir = $this->_snapshot->getBackupsDir() . '/~tmp-' . microtime(true);
136 
137  $result = @mkdir($tmpDir);
138 
139  if (false === $result) {
140  throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(
141  new Phrase('Failed to create directory %1', [$tmpDir])
142  );
143  }
144 
145  return $tmpDir;
146  }
147 
153  protected function _cleanupFtp()
154  {
155  $rootDir = $this->_snapshot->getRootDir();
156 
157  $filesystemIterator = new \RecursiveIteratorIterator(
158  new \RecursiveDirectoryIterator($rootDir),
159  \RecursiveIteratorIterator::CHILD_FIRST
160  );
161 
162  $iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter(
163  $filesystemIterator,
164  $this->_snapshot->getIgnorePaths()
165  );
166 
167  foreach ($iterator as $item) {
168  $ftpPath = $this->_snapshot->getFtpPath() . '/' . str_replace($rootDir, '', $item->__toString());
169  $ftpPath = str_replace('\\', '/', $ftpPath);
170 
171  $this->_ftpClient->delete($ftpPath);
172  }
173  }
174 
183  protected function _uploadBackupToFtp($tmpDir)
184  {
185  $filesystemIterator = new \RecursiveIteratorIterator(
186  new \RecursiveDirectoryIterator($tmpDir),
187  \RecursiveIteratorIterator::SELF_FIRST
188  );
189 
190  $iterator = new \Magento\Framework\Backup\Filesystem\Iterator\Filter(
191  $filesystemIterator,
192  $this->_snapshot->getIgnorePaths()
193  );
194 
195  foreach ($filesystemIterator as $item) {
196  $ftpPath = $this->_snapshot->getFtpPath() . '/' . str_replace($tmpDir, '', $item->__toString());
197  $ftpPath = str_replace('\\', '/', $ftpPath);
198 
199  if ($item->isLink()) {
200  continue;
201  }
202 
203  if ($item->isDir()) {
204  $this->_ftpClient->mkdirRecursive($ftpPath);
205  } else {
206  $result = $this->_ftpClient->put($ftpPath, $item->__toString());
207  if (false === $result) {
208  throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(
209  new Phrase('Failed to upload file %1 to ftp', [$item->__toString()])
210  );
211  }
212  }
213  }
214  }
215 }
$rootDir
Definition: website.php:12
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25