‪TYPO3CMS  10.4
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 
21 
26 abstract class ‪AbstractNode
27 {
31  protected ‪$name = '';
32 
36  protected ‪$targetPermission;
37 
41  protected ‪$parent;
42 
46  protected ‪$children = [];
47 
53  public function ‪getName()
54  {
55  return ‪$this->name;
56  }
57 
65  protected function ‪getTargetPermission()
66  {
68  }
69 
75  protected function ‪setTargetPermission($permission)
76  {
77  // Normalize the permission string to "4 characters", padding with leading "0" if necessary:
78  $permission = substr($permission, 0, 4);
79  $permission = str_pad($permission, 4, '0', STR_PAD_LEFT);
80  $this->targetPermission = $permission;
81  }
82 
88  protected function ‪getChildren()
89  {
90  return ‪$this->children;
91  }
92 
98  protected function ‪getParent()
99  {
100  return ‪$this->parent;
101  }
102 
108  public function ‪getAbsolutePath()
109  {
110  return $this->‪getParent()->‪getAbsolutePath() . '/' . ‪$this->name;
111  }
112 
118  public function ‪isWritable()
119  {
120  return $this->‪getParent()->‪isWritable();
121  }
122 
130  protected function ‪exists()
131  {
132  if (@is_link($this->‪getAbsolutePath())) {
133  return true;
134  }
135  return @file_exists($this->‪getAbsolutePath());
136  }
137 
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(), 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',
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:34
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\exists
‪bool exists()
Definition: AbstractNode.php:126
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getName
‪string getName()
Definition: AbstractNode.php:49
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:292
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getCurrentPermission
‪string getCurrentPermission()
Definition: AbstractNode.php:184
‪TYPO3\CMS\Install\FolderStructure
Definition: AbstractNode.php:16
‪TYPO3\CMS\Install\FolderStructure\Exception\InvalidArgumentException
Definition: InvalidArgumentException.php:24
‪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:169
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWritable
‪bool isWritable()
Definition: AbstractNode.php:114
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getParent
‪NodeInterface null getParent()
Definition: AbstractNode.php:94
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\fixPermission
‪FlashMessage fixPermission()
Definition: AbstractNode.php:140
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$name
‪string $name
Definition: AbstractNode.php:30
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\getAbsolutePath
‪string getAbsolutePath()
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\setTargetPermission
‪setTargetPermission($permission)
Definition: AbstractNode.php:71
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getTargetPermission
‪string getTargetPermission()
Definition: AbstractNode.php:61
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getAbsolutePath
‪string getAbsolutePath()
Definition: AbstractNode.php:104
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$parent
‪NodeInterface null $parent
Definition: AbstractNode.php:38
‪TYPO3\CMS\Install\FolderStructure\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$children
‪array $children
Definition: AbstractNode.php:42
‪TYPO3\CMS\Core\Messaging\AbstractMessage\NOTICE
‪const NOTICE
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\isWritable
‪bool isWritable()
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getChildren
‪array getChildren()
Definition: AbstractNode.php:84
‪TYPO3\CMS\Install\FolderStructure\AbstractNode
Definition: AbstractNode.php:27
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWindowsOs
‪bool isWindowsOs()
Definition: AbstractNode.php:195
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:24