‪TYPO3CMS  ‪main
AbstractNode.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
27 abstract class ‪AbstractNode
28 {
32  protected ‪$name = '';
33 
37  protected ‪$targetPermission;
38 
42  protected ‪$parent;
43 
47  protected ‪$children = [];
48 
54  public function ‪getName()
55  {
56  return ‪$this->name;
57  }
58 
66  protected function ‪getTargetPermission()
67  {
68  return $this->targetPermission ?? '';
69  }
70 
76  protected function ‪setTargetPermission($permission)
77  {
78  // Normalize the permission string to "4 characters", padding with leading "0" if necessary:
79  $permission = substr($permission, 0, 4);
80  $permission = str_pad($permission, 4, '0', STR_PAD_LEFT);
81  $this->targetPermission = $permission;
82  }
83 
89  protected function ‪getChildren()
90  {
91  return ‪$this->children;
92  }
93 
99  protected function ‪getParent()
100  {
101  return ‪$this->parent;
102  }
103 
109  public function ‪getAbsolutePath()
110  {
111  return $this->‪getParent()->‪getAbsolutePath() . '/' . ‪$this->name;
112  }
113 
119  public function ‪isWritable()
120  {
121  return $this->‪getParent()->‪isWritable();
122  }
123 
131  protected function ‪exists()
132  {
133  if (@is_link($this->‪getAbsolutePath())) {
134  return true;
135  }
136  return @file_exists($this->‪getAbsolutePath());
137  }
138 
144  protected function ‪fixPermission(): FlashMessage
145  {
146  if ($this->‪isPermissionCorrect()) {
147  throw new Exception(
148  'Permission on ' . $this->‪getAbsolutePath() . ' are already ok',
149  1366744035
150  );
151  }
152  $result = @chmod($this->‪getAbsolutePath(), (int)octdec($this->‪getTargetPermission()));
153  if ($result === true) {
154  return new FlashMessage(
155  '',
156  'Fixed permission on ' . $this->‪getRelativePathBelowSiteRoot() . '.'
157  );
158  }
159  return new FlashMessage(
160  'Permissions could not be changed to ' . $this->‪getTargetPermission()
161  . '. This only is a problem if files and folders within this node cannot be written.',
162  'Permission change on ' . $this->‪getRelativePathBelowSiteRoot() . ' not successful',
163  ContextualFeedbackSeverity::NOTICE
164  );
165  }
166 
172  protected function ‪isPermissionCorrect()
173  {
174  if ($this->‪isWindowsOs()) {
175  return true;
176  }
177  if ($this->‪getCurrentPermission() === $this->‪getTargetPermission()) {
178  return true;
179  }
180  return false;
181  }
182 
188  protected function ‪getCurrentPermission()
189  {
190  $permissions = decoct((int)fileperms($this->‪getAbsolutePath()));
191  return substr($permissions, -4);
192  }
193 
199  protected function ‪isWindowsOs()
200  {
201  return ‪Environment::isWindows();
202  }
203 
211  protected function ‪getRelativePathBelowSiteRoot($path = null)
212  {
213  if ($path === null) {
214  $path = $this->‪getAbsolutePath();
215  }
216  $projectPath = ‪Environment::getProjectPath();
217  if (strpos($path, $projectPath, 0) !== 0) {
218  throw new InvalidArgumentException(
219  'Public path is not first part of given path',
220  1366398198
221  );
222  }
223  $relativePath = substr($path, strlen($projectPath), strlen($path));
224  // Add a forward slash again, so we don't end up with an empty string
225  if ($relativePath === '') {
226  $relativePath = '/';
227  }
228  return $relativePath;
229  }
230 }
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isPermissionCorrect
‪bool isPermissionCorrect()
Definition: AbstractNode.php:168
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$targetPermission
‪string null $targetPermission
Definition: AbstractNode.php:35
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\exists
‪bool exists()
Definition: AbstractNode.php:127
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getName
‪string getName()
Definition: AbstractNode.php:50
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getCurrentPermission
‪string getCurrentPermission()
Definition: AbstractNode.php:184
‪TYPO3\CMS\Install\FolderStructure
Definition: AbstractNode.php:16
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
Definition: InvalidArgumentException.php:23
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getRelativePathBelowSiteRoot
‪string getRelativePathBelowSiteRoot($path=null)
Definition: AbstractNode.php:207
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWritable
‪bool isWritable()
Definition: AbstractNode.php:115
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getParent
‪NodeInterface null getParent()
Definition: AbstractNode.php:95
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$name
‪string $name
Definition: AbstractNode.php:31
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\getAbsolutePath
‪string getAbsolutePath()
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\setTargetPermission
‪setTargetPermission($permission)
Definition: AbstractNode.php:72
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getTargetPermission
‪string getTargetPermission()
Definition: AbstractNode.php:62
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\fixPermission
‪fixPermission()
Definition: AbstractNode.php:140
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getAbsolutePath
‪string getAbsolutePath()
Definition: AbstractNode.php:105
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$parent
‪NodeInterface null $parent
Definition: AbstractNode.php:39
‪TYPO3\CMS\Install\FolderStructure\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$children
‪array $children
Definition: AbstractNode.php:43
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\isWritable
‪bool isWritable()
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getChildren
‪array getChildren()
Definition: AbstractNode.php:85
‪TYPO3\CMS\Install\FolderStructure\AbstractNode
Definition: AbstractNode.php:28
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWindowsOs
‪bool isWindowsOs()
Definition: AbstractNode.php:195
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:24
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276