TYPO3 CMS  TYPO3_7-6
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 
18 
22 abstract class AbstractNode
23 {
27  protected $name = '';
28 
32  protected $targetPermission = null;
33 
37  protected $parent = null;
38 
42  protected $children = [];
43 
49  public function getName()
50  {
51  return $this->name;
52  }
53 
61  protected function getTargetPermission()
62  {
64  }
65 
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  } else {
133  return @file_exists($this->getAbsolutePath());
134  }
135  }
136 
143  protected function fixPermission()
144  {
145  if ($this->isPermissionCorrect()) {
146  throw new Exception(
147  'Permission on ' . $this->getAbsolutePath() . ' are already ok',
148  1366744035
149  );
150  }
151  $result = @chmod($this->getAbsolutePath(), octdec($this->getTargetPermission()));
152  if ($result === true) {
153  $status = new Status\OkStatus();
154  $status->setTitle('Fixed permission on ' . $this->getRelativePathBelowSiteRoot() . '.');
155  } else {
156  $status = new Status\NoticeStatus();
157  $status->setTitle('Permission change on ' . $this->getRelativePathBelowSiteRoot() . ' not successful');
158  $status->setMessage(
159  'Permissions could not be changed to ' . $this->getTargetPermission() .
160  '. This only is a problem if files and folders within this node cannot be written.'
161  );
162  }
163  return $status;
164  }
165 
171  protected function isPermissionCorrect()
172  {
173  if ($this->isWindowsOs()) {
174  return true;
175  }
176  if ($this->getCurrentPermission() === $this->getTargetPermission()) {
177  return true;
178  } else {
179  return false;
180  }
181  }
182 
188  protected function getCurrentPermission()
189  {
190  $permissions = decoct(fileperms($this->getAbsolutePath()));
191  return substr($permissions, -4);
192  }
193 
199  protected function isWindowsOs()
200  {
201  if (TYPO3_OS === 'WIN') {
202  return true;
203  }
204  return false;
205  }
206 
214  protected function getRelativePathBelowSiteRoot($path = null)
215  {
216  if (is_null($path)) {
217  $path = $this->getAbsolutePath();
218  }
219  $pathSiteWithoutTrailingSlash = substr(PATH_site, 0, -1);
220  if (strpos($path, $pathSiteWithoutTrailingSlash, 0) !== 0) {
221  throw new Exception\InvalidArgumentException(
222  'PATH_site is not first part of given path',
223  1366398198
224  );
225  }
226  $relativePath = substr($path, strlen($pathSiteWithoutTrailingSlash), strlen($path));
227  // Add a forward slash again, so we don't end up with an empty string
228  if ($relativePath === '') {
229  $relativePath = '/';
230  }
231  return $relativePath;
232  }
233 }