‪TYPO3CMS  9.5
AbstractNode.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 
19 
24 abstract class ‪AbstractNode
25 {
29  protected ‪$name = '';
30 
34  protected ‪$targetPermission;
35 
39  protected ‪$parent;
40 
44  protected ‪$children = [];
45 
51  public function ‪getName()
52  {
53  return ‪$this->name;
54  }
55 
63  protected function ‪getTargetPermission()
64  {
66  }
67 
73  protected function ‪setTargetPermission($permission)
74  {
75  // Normalize the permission string to "4 characters", padding with leading "0" if necessary:
76  $permission = substr($permission, 0, 4);
77  $permission = str_pad($permission, 4, '0', STR_PAD_LEFT);
78  $this->targetPermission = $permission;
79  }
80 
86  protected function ‪getChildren()
87  {
88  return ‪$this->children;
89  }
90 
96  protected function ‪getParent()
97  {
98  return ‪$this->parent;
99  }
100 
106  public function ‪getAbsolutePath()
107  {
108  return $this->‪getParent()->‪getAbsolutePath() . '/' . ‪$this->name;
109  }
110 
116  public function ‪isWritable()
117  {
118  return $this->‪getParent()->‪isWritable();
119  }
120 
128  protected function ‪exists()
129  {
130  if (@is_link($this->‪getAbsolutePath())) {
131  return true;
132  }
133  return @file_exists($this->‪getAbsolutePath());
134  }
135 
142  protected function ‪fixPermission(): FlashMessage
143  {
144  if ($this->‪isPermissionCorrect()) {
145  throw new Exception(
146  'Permission on ' . $this->‪getAbsolutePath() . ' are already ok',
147  1366744035
148  );
149  }
150  $result = @chmod($this->‪getAbsolutePath(), octdec($this->‪getTargetPermission()));
151  if ($result === true) {
152  return new FlashMessage(
153  '',
154  'Fixed permission on ' . $this->‪getRelativePathBelowSiteRoot() . '.'
155  );
156  }
157  return new FlashMessage(
158  'Permissions could not be changed to ' . $this->‪getTargetPermission()
159  . '. This only is a problem if files and folders within this node cannot be written.',
160  'Permission change on ' . $this->‪getRelativePathBelowSiteRoot() . ' not successful',
162  );
163  }
164 
170  protected function ‪isPermissionCorrect()
171  {
172  if ($this->‪isWindowsOs()) {
173  return true;
174  }
175  if ($this->‪getCurrentPermission() === $this->‪getTargetPermission()) {
176  return true;
177  }
178  return false;
179  }
180 
186  protected function ‪getCurrentPermission()
187  {
188  $permissions = decoct(fileperms($this->‪getAbsolutePath()));
189  return substr($permissions, -4);
190  }
191 
197  protected function ‪isWindowsOs()
198  {
199  return ‪Environment::isWindows();
200  }
201 
209  protected function ‪getRelativePathBelowSiteRoot($path = null)
210  {
211  if ($path === null) {
212  $path = $this->‪getAbsolutePath();
213  }
214  $publicPath = ‪Environment::getPublicPath();
215  if (strpos($path, $publicPath, 0) !== 0) {
216  throw new Exception\InvalidArgumentException(
217  'Public path is not first part of given path',
218  1366398198
219  );
220  }
221  $relativePath = substr($path, strlen($publicPath), strlen($path));
222  // Add a forward slash again, so we don't end up with an empty string
223  if ($relativePath === '') {
224  $relativePath = '/';
225  }
226  return $relativePath;
227  }
228 }
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isPermissionCorrect
‪bool isPermissionCorrect()
Definition: AbstractNode.php:166
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$targetPermission
‪string null $targetPermission
Definition: AbstractNode.php:32
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\exists
‪bool exists()
Definition: AbstractNode.php:124
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getName
‪string getName()
Definition: AbstractNode.php:47
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:266
‪TYPO3\CMS\Install\FolderStructure
Definition: AbstractNode.php:2
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getCurrentPermission
‪getCurrentPermission()
Definition: AbstractNode.php:182
‪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\AbstractNode\string
‪string
Definition: AbstractNode.php:182
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getParent
‪NodeInterface null getParent()
Definition: AbstractNode.php:92
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\fixPermission
‪FlashMessage fixPermission()
Definition: AbstractNode.php:138
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$name
‪string $name
Definition: AbstractNode.php:28
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\getAbsolutePath
‪string getAbsolutePath()
‪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\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$parent
‪NodeInterface null $parent
Definition: AbstractNode.php:36
‪TYPO3\CMS\Install\FolderStructure\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\$children
‪array $children
Definition: AbstractNode.php:40
‪TYPO3\CMS\Core\Messaging\AbstractMessage\NOTICE
‪const NOTICE
Definition: AbstractMessage.php:25
‪TYPO3\CMS\Install\FolderStructure\NodeInterface\isWritable
‪bool isWritable()
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\getChildren
‪array getChildren()
Definition: AbstractNode.php:82
‪TYPO3\CMS\Install\FolderStructure\AbstractNode
Definition: AbstractNode.php:25
‪TYPO3\CMS\Install\FolderStructure\AbstractNode\isWindowsOs
‪bool isWindowsOs()
Definition: AbstractNode.php:193
‪TYPO3\CMS\Install\FolderStructure\NodeInterface
Definition: NodeInterface.php:23