‪TYPO3CMS  9.5
FileNode.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
24 {
28  protected ‪$targetPermission = '0664';
29 
33  protected ‪$targetContent;
34 
42  public function ‪__construct(array $structure, ‪NodeInterface ‪$parent = null)
43  {
44  if (‪$parent === null) {
45  throw new Exception\InvalidArgumentException(
46  'File node must have parent',
47  1366927513
48  );
49  }
50  $this->parent = ‪$parent;
51 
52  // Ensure name is a single segment, but not a path like foo/bar or an absolute path /foo
53  if (strstr($structure['name'], '/') !== false) {
54  throw new Exception\InvalidArgumentException(
55  'File name must not contain forward slash',
56  1366222207
57  );
58  }
59  $this->name = $structure['name'];
60 
61  if (isset($structure['targetPermission'])) {
62  $this->‪setTargetPermission($structure['targetPermission']);
63  }
64 
65  if (isset($structure['targetContent']) && isset($structure['targetContentFile'])) {
66  throw new Exception\InvalidArgumentException(
67  'Either targetContent or targetContentFile can be set, but not both',
68  1380364361
69  );
70  }
71 
72  if (isset($structure['targetContent'])) {
73  $this->targetContent = $structure['targetContent'];
74  }
75  if (isset($structure['targetContentFile'])) {
76  if (!is_readable($structure['targetContentFile'])) {
77  throw new Exception\InvalidArgumentException(
78  'targetContentFile ' . $structure['targetContentFile'] . ' does not exist or is not readable',
79  1380364362
80  );
81  }
82  $this->targetContent = file_get_contents($structure['targetContentFile']);
83  }
84  }
85 
93  public function ‪getStatus(): array
94  {
95  $result = [];
96  if (!$this->‪exists()) {
97  $result[] = new ‪FlashMessage(
98  'By using "Try to fix errors" we can try to create it',
99  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' does not exist',
101  );
102  } else {
103  $result = $this->‪getSelfStatus();
104  }
105  return $result;
106  }
107 
115  public function ‪fix(): array
116  {
117  $result = $this->‪fixSelf();
118  return $result;
119  }
120 
126  protected function ‪fixSelf(): array
127  {
128  $result = [];
129  if (!$this->‪exists()) {
130  $resultCreateFile = $this->‪createFile();
131  $result[] = $resultCreateFile;
132  if ($resultCreateFile->getSeverity() === ‪FlashMessage::OK
133  && $this->targetContent !== null
134  ) {
135  $result[] = $this->‪setContent();
136  if (!$this->‪isPermissionCorrect()) {
137  $result[] = $this->‪fixPermission();
138  }
139  }
140  } elseif (!$this->‪isFile()) {
141  $fileType = @filetype($this->‪getAbsolutePath());
142  if ($fileType) {
143  $messageBody =
144  'The target ' . $this->‪getRelativePathBelowSiteRoot() . ' should be a file,' .
145  ' but is of type ' . $fileType . '. This cannot be fixed automatically. Please investigate.'
146  ;
147  } else {
148  $messageBody =
149  'The target ' . $this->‪getRelativePathBelowSiteRoot() . ' should be a file,' .
150  ' but is of unknown type, probably because an upper level directory does not exist. Please investigate.'
151  ;
152  }
153  $result[] = new ‪FlashMessage(
154  $messageBody,
155  'Path ' . $this->‪getRelativePathBelowSiteRoot() . ' is not a file',
157  );
158  } elseif (!$this->‪isPermissionCorrect()) {
159  $result[] = $this->‪fixPermission();
160  }
161  return $result;
162  }
163 
170  protected function ‪createFile(): ‪FlashMessage
171  {
172  if ($this->‪exists()) {
173  throw new ‪Exception(
174  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' already exists',
175  1367048077
176  );
177  }
178  $result = @touch($this->‪getAbsolutePath());
179  if ($result === true) {
180  return new ‪FlashMessage(
181  '',
182  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' successfully created.'
183  );
184  }
185  return new ‪FlashMessage(
186  'The target file could not be created. There is probably a'
187  . ' group or owner permission problem on the parent directory.',
188  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' not created!',
190  );
191  }
192 
198  protected function ‪getSelfStatus(): array
199  {
200  $result = [];
201  if (!$this->‪isFile()) {
202  $result[] = new ‪FlashMessage(
203  'Path ' . $this->‪getAbsolutePath() . ' should be a file,'
204  . ' but is of type ' . filetype($this->‪getAbsolutePath()),
205  $this->‪getRelativePathBelowSiteRoot() . ' is not a file',
207  );
208  } elseif (!$this->‪isWritable()) {
209  $result[] = new ‪FlashMessage(
210  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' exists, but is not writable.',
211  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' is not writable',
213  );
214  } elseif (!$this->‪isPermissionCorrect()) {
215  $result[] = new ‪FlashMessage(
216  'Default configured permissions are ' . $this->‪getTargetPermission()
217  . ' but file permissions are ' . $this->‪getCurrentPermission(),
218  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' permissions mismatch',
220  );
221  }
222  if ($this->‪isFile() && !$this->‪isContentCorrect()) {
223  $result[] = new ‪FlashMessage(
224  'File content is not identical to default content. This file may have been changed manually.'
225  . ' The Install Tool will not overwrite the current version!',
226  'File ' . $this->‪getRelativePathBelowSiteRoot() . ' content differs',
228  );
229  } else {
230  $result[] = new ‪FlashMessage(
231  'Is a file with the default content and configured permissions of ' . $this->‪getTargetPermission(),
232  'File ' . $this->‪getRelativePathBelowSiteRoot()
233  );
234  }
235  return $result;
236  }
237 
244  protected function ‪isContentCorrect()
245  {
246  $absolutePath = $this->‪getAbsolutePath();
247  if (is_link($absolutePath) || !is_file($absolutePath)) {
248  throw new ‪Exception(
249  'File ' . $absolutePath . ' must exist',
250  1367056363
251  );
252  }
253  $result = false;
254  if ($this->targetContent === null) {
255  $result = true;
256  } else {
257  $targetContentHash = md5($this->targetContent);
258  $currentContentHash = md5(file_get_contents($absolutePath));
259  if ($targetContentHash === $currentContentHash) {
260  $result = true;
261  }
262  }
263  return $result;
264  }
265 
272  protected function ‪setContent(): ‪FlashMessage
273  {
274  $absolutePath = $this->‪getAbsolutePath();
275  if (is_link($absolutePath) || !is_file($absolutePath)) {
276  throw new ‪Exception(
277  'File ' . $absolutePath . ' must exist',
278  1367060201
279  );
280  }
281  if ($this->targetContent === null) {
282  throw new ‪Exception(
283  'Target content not defined for ' . $absolutePath,
284  1367060202
285  );
286  }
287  $result = @file_put_contents($absolutePath, $this->targetContent);
288  if ($result !== false) {
289  return new ‪FlashMessage(
290  '',
291  'Set content to ' . $this->‪getRelativePathBelowSiteRoot()
292  );
293  }
294  return new ‪FlashMessage(
295  'Setting content of the file failed for unknown reasons.',
296  'Setting content to ' . $this->‪getRelativePathBelowSiteRoot() . ' failed',
298  );
299  }
300 
306  protected function ‪isFile()
307  {
308  $path = $this->‪getAbsolutePath();
309  return !is_link($path) && is_file($path);
310  }
311 }
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isPermissionCorrect
‪bool isPermissionCorrect()
Definition: AbstractNode.php:166
‪TYPO3\CMS\Install\FolderStructure\FileNode\fixSelf
‪FlashMessage[] fixSelf()
Definition: FileNode.php:124
‪TYPO3\CMS\Install\FolderStructure\FileNode\setContent
‪FlashMessage setContent()
Definition: FileNode.php:270
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\exists
‪bool exists()
Definition: AbstractNode.php:124
‪TYPO3\CMS\Install\FolderStructure\FileNode\fix
‪FlashMessage[] fix()
Definition: FileNode.php:113
‪TYPO3\CMS\Install\FolderStructure\FileNode\$targetPermission
‪int null $targetPermission
Definition: FileNode.php:27
‪TYPO3\CMS\Install\FolderStructure
Definition: AbstractNode.php:2
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getCurrentPermission
‪getCurrentPermission()
Definition: AbstractNode.php:182
‪TYPO3\CMS\Install\FolderStructure\FileNode\__construct
‪__construct(array $structure, NodeInterface $parent=null)
Definition: FileNode.php:40
‪TYPO3\CMS\Install\FolderStructure\FileNode\getStatus
‪FlashMessage[] getStatus()
Definition: FileNode.php:91
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getRelativePathBelowSiteRoot
‪string getRelativePathBelowSiteRoot($path=null)
Definition: AbstractNode.php:205
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWritable
‪bool isWritable()
Definition: AbstractNode.php:112
‪TYPO3\CMS\Install\FolderStructure\FileNode
Definition: FileNode.php:24
‪TYPO3\CMS\Install\FolderStructure\FileNode\$targetContent
‪string null $targetContent
Definition: FileNode.php:31
‪TYPO3\CMS\Install\FolderStructure\FileNode\createFile
‪FlashMessage createFile()
Definition: FileNode.php:168
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\fixPermission
‪FlashMessage fixPermission()
Definition: AbstractNode.php:138
‪TYPO3\CMS\Install\FolderStructure\FileNode\isFile
‪bool isFile()
Definition: FileNode.php:304
‪TYPO3\CMS\Install\FolderStructure\FileNode\getSelfStatus
‪FlashMessage[] getSelfStatus()
Definition: FileNode.php:196
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\setTargetPermission
‪setTargetPermission($permission)
Definition: AbstractNode.php:69
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getTargetPermission
‪string getTargetPermission()
Definition: AbstractNode.php:59
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getAbsolutePath
‪string getAbsolutePath()
Definition: AbstractNode.php:102
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$parent
‪NodeInterface null $parent
Definition: AbstractNode.php:36
‪TYPO3\CMS\Install\FolderStructure\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Messaging\AbstractMessage\NOTICE
‪const NOTICE
Definition: AbstractMessage.php:25
‪TYPO3\CMS\Install\FolderStructure\FileNode\isContentCorrect
‪bool isContentCorrect()
Definition: FileNode.php:242
‪TYPO3\CMS\Install\FolderStructure\AbstractNode
Definition: AbstractNode.php:25
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:23
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29