2 namespace TYPO3\CMS\Install\FolderStructure
;
4 /***************************************************************
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 use TYPO3\CMS\Install\Status
;
30 * Abstract node implements common methods
32 abstract class AbstractNode
{
40 * @var NULL|string Target permissions for unix, eg. 2770
42 protected $targetPermission = NULL;
45 * @var NULL|NodeInterface Parent object of this structure node
47 protected $parent = NULL;
50 * @var array Directories and root may have children, files and link always empty array
52 protected $children = array();
59 public function getName() {
64 * Get target permission
66 * @return string Permission, eg. 2770
68 public function getTargetPermission() {
69 return $this->targetPermission
;
77 public function getChildren() {
78 return $this->children
;
84 * @return NULL|NodeInterface
86 public function getParent() {
91 * Get absolute path of node
95 public function getAbsolutePath() {
96 return $this->parent
->getAbsolutePath() . '/' . $this->name
;
100 * Current node is writable if parent is writable
102 * @return boolean TRUE if parent is writable
104 public function isWritable() {
105 return $this->parent
->isWritable();
109 * Checks if node exists.
110 * Returns TRUE if it is there, even if it is only a link.
111 * Does not check the type!
115 protected function exists() {
116 if (@is_link
($this->getAbsolutePath())) {
119 return @file_exists
($this->getAbsolutePath());
124 * Fix permission if they are not equal to target permission
127 * @return \TYPO3\CMS\Install\Status\StatusInterface
129 protected function fixPermission() {
130 if ($this->isPermissionCorrect()) {
132 'Permission on ' . $this->getAbsolutePath() . ' are already ok',
136 $result = @chmod
($this->getAbsolutePath(), octdec($this->targetPermission
));
137 if ($result === TRUE) {
138 $status = new Status\
OkStatus();
139 $status->setTitle('Fixed permission on ' . $this->getRelativePathBelowSiteRoot() . '.');
141 $status = new Status\
ErrorStatus();
142 $status->setTitle('Permission change on ' . $this->getRelativePathBelowSiteRoot() . ' not successful!');
144 'Permissions could not be changed to ' . $this->targetPermission
. '. There is probably some' .
145 ' group or owner permission problem on the parent directory.'
152 * Checks if current permission are identical to target permission
156 protected function isPermissionCorrect() {
157 if ($this->isWindowsOs()) {
160 if ($this->getCurrentPermission() === $this->targetPermission
) {
168 * Get current permission of node
170 * @return string, eg. 2770 for dirs, 0660 for files
172 protected function getCurrentPermission() {
173 $absolutePath = $this->getAbsolutePath();
174 $permissions = decoct(fileperms($this->getAbsolutePath()));
175 if (is_dir($absolutePath)) {
176 $result = substr($permissions, 1);
178 $result = substr($permissions, 2);
184 * Returns TRUE if OS is windows
186 * @return boolean TRUE on windows
188 protected function isWindowsOs() {
189 if (TYPO3_OS
=== 'WIN') {
196 * Cut off PATH_site from given path
198 * @param string $path Given path
199 * @return string Relative path, but beginning with /
200 * @throws Exception\InvalidArgumentException
202 protected function getRelativePathBelowSiteRoot($path = NULL) {
203 if (is_null($path)) {
204 $path = $this->getAbsolutePath();
206 $pathSiteWithoutTrailingSlash = substr(PATH_site
, 0, -1);
207 if (strpos($path, $pathSiteWithoutTrailingSlash, 0) !== 0) {
208 throw new \TYPO3\CMS\Install\FolderStructure\Exception\
InvalidArgumentException(
209 'PATH_site is not first part of given path',
213 $relativePath = substr($path, strlen($pathSiteWithoutTrailingSlash), strlen($path));
214 // Add a forward slash again, so we don't end up with an empty string
215 if (strlen($relativePath) === 0) {
218 return $relativePath;