[!!!][TASK] Rewrite install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / FolderStructure / AbstractNode.php
1 <?php
2 namespace TYPO3\CMS\Install\FolderStructure;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
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.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
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.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Install\Status;
28
29 /**
30 * Abstract node implements common methods
31 */
32 abstract class AbstractNode {
33
34 /**
35 * @var string Name
36 */
37 protected $name = '';
38
39 /**
40 * @var NULL|string Target permissions for unix, eg. 2770
41 */
42 protected $targetPermission = NULL;
43
44 /**
45 * @var NULL|NodeInterface Parent object of this structure node
46 */
47 protected $parent = NULL;
48
49 /**
50 * @var array Directories and root may have children, files and link always empty array
51 */
52 protected $children = array();
53
54 /**
55 * Get name
56 *
57 * @return string Name
58 */
59 public function getName() {
60 return $this->name;
61 }
62
63 /**
64 * Get target permission
65 *
66 * @return string Permission, eg. 2770
67 */
68 public function getTargetPermission() {
69 return $this->targetPermission;
70 }
71
72 /**
73 * Get children
74 *
75 * @return array
76 */
77 public function getChildren() {
78 return $this->children;
79 }
80
81 /**
82 * Get parent
83 *
84 * @return NULL|NodeInterface
85 */
86 public function getParent() {
87 return $this->parent;
88 }
89
90 /**
91 * Get absolute path of node
92 *
93 * @return string
94 */
95 public function getAbsolutePath() {
96 return $this->parent->getAbsolutePath() . '/' . $this->name;
97 }
98
99 /**
100 * Current node is writable if parent is writable
101 *
102 * @return boolean TRUE if parent is writable
103 */
104 public function isWritable() {
105 return $this->parent->isWritable();
106 }
107
108 /**
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!
112 *
113 * @return boolean
114 */
115 protected function exists() {
116 if (is_link($this->getAbsolutePath())) {
117 return TRUE;
118 } else {
119 return file_exists($this->getAbsolutePath());
120 }
121 }
122
123 /**
124 * Fix permission if they are not equal to target permission
125 *
126 * @throws Exception
127 * @return \TYPO3\CMS\Install\Status\StatusInterface
128 */
129 protected function fixPermission() {
130 if ($this->isPermissionCorrect()) {
131 throw new Exception(
132 'Permission on ' . $this->getAbsolutePath() . ' are already ok',
133 1366744035
134 );
135 }
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() . '.');
140 } else {
141 $status = new Status\ErrorStatus();
142 $status->setTitle('Permission change on ' . $this->getRelativePathBelowSiteRoot() . ' not successful!');
143 $status->setMessage(
144 'Permissions could not be changed to ' . $this->targetPermission . '. There is probably some' .
145 ' group or owner permission problem on the parent directory.'
146 );
147 }
148 return $status;
149 }
150
151 /**
152 * Checks if current permission are identical to target permission
153 *
154 * @return boolean
155 */
156 protected function isPermissionCorrect() {
157 if ($this->isWindowsOs()) {
158 return TRUE;
159 }
160 if ($this->getCurrentPermission() === $this->targetPermission) {
161 return TRUE;
162 } else {
163 return FALSE;
164 }
165 }
166
167 /**
168 * Get current permission of node
169 *
170 * @return string, eg. 2770 for dirs, 0660 for files
171 */
172 protected function getCurrentPermission() {
173 $absolutePath = $this->getAbsolutePath();
174 $permissions = decoct(fileperms($this->getAbsolutePath()));
175 if (is_dir($absolutePath)) {
176 $result = substr($permissions, 1);
177 } else {
178 $result = substr($permissions, 2);
179 }
180 return $result;
181 }
182
183 /**
184 * Returns TRUE if OS is windows
185 *
186 * @return boolean TRUE on windows
187 */
188 protected function isWindowsOs() {
189 if (TYPO3_OS === 'WIN') {
190 return TRUE;
191 }
192 return FALSE;
193 }
194
195 /**
196 * Cut off PATH_site from given path
197 *
198 * @param string $path Given path
199 * @return string Relative path, but beginning with /
200 * @throws Exception\InvalidArgumentException
201 */
202 protected function getRelativePathBelowSiteRoot($path = NULL) {
203 if (is_null($path)) {
204 $path = $this->getAbsolutePath();
205 }
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',
210 1366398198
211 );
212 }
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) {
216 $relativePath = '/';
217 }
218 return $relativePath;
219 }
220 }
221 ?>