[TASK] Use simple file backend for core php cache
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_path.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Class with helper functions for file paths.
30 *
31 * @author Oliver Hader <oliver.hader@typo3.org>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 final class t3lib_utility_Path {
36 /**
37 * Gets the relative path from the current used script to a given directory.
38 * The allowed TYPO3 path is checked as well, thus it's not possible to go to upper levels.
39 *
40 * @param string $targetPath Absolute target path
41 * @return NULL|string
42 */
43 public static function getRelativePathTo($targetPath) {
44 return self::getRelativePath(
45 dirname(PATH_thisScript),
46 $targetPath
47 );
48 }
49
50 /**
51 * Gets the relative path from a source directory to a target directory.
52 * The allowed TYPO3 path is checked as well, thus it's not possible to go to upper levels.
53 *
54 * @param string $sourcePath Absolute source path
55 * @param string $targetPath Absolute target path
56 * @return NULL|string
57 */
58 public static function getRelativePath($sourcePath, $targetPath) {
59 $relativePath = NULL;
60
61 $sourcePath = rtrim($sourcePath, DIRECTORY_SEPARATOR);
62 $targetPath = rtrim($targetPath, DIRECTORY_SEPARATOR);
63
64 if ($sourcePath !== $targetPath) {
65 $commonPrefix = self::getCommonPrefix(array($sourcePath, $targetPath));
66
67 if ($commonPrefix !== NULL && t3lib_div::isAllowedAbsPath($commonPrefix)) {
68 $commonPrefixLength = strlen($commonPrefix);
69 $resolvedSourcePath = '';
70 $resolvedTargetPath = '';
71 $sourcePathSteps = 0;
72
73 if (strlen($sourcePath) > $commonPrefixLength) {
74 $resolvedSourcePath = (string) substr($sourcePath, $commonPrefixLength);
75 }
76 if (strlen($targetPath) > $commonPrefixLength) {
77 $resolvedTargetPath = (string) substr($targetPath, $commonPrefixLength);
78 }
79
80 if ($resolvedSourcePath !== '') {
81 $sourcePathSteps = count(explode(DIRECTORY_SEPARATOR, $resolvedSourcePath));
82 }
83
84 $relativePath = self::sanitizeTrailingSeparator(
85 str_repeat('../', $sourcePathSteps) .
86 str_replace(DIRECTORY_SEPARATOR, '/', $resolvedTargetPath)
87 );
88 }
89 }
90
91 return $relativePath;
92 }
93
94 /**
95 * Gets the common path prefix out of many paths.
96 * + /var/www/domain.com/typo3/sysext/cms/
97 * + /var/www/domain.com/typo3/sysext/em/
98 * + /var/www/domain.com/typo3/sysext/file/
99 * = /var/www/domain.com/typo3/sysext/
100 *
101 * @param array $paths Paths to be processed
102 * @return NULL|string
103 */
104 public static function getCommonPrefix(array $paths) {
105 $commonPath = NULL;
106
107 if (count($paths) === 1) {
108 $commonPath = array_shift($paths);
109 } elseif (count($paths) > 1) {
110 $parts = explode(DIRECTORY_SEPARATOR, array_shift($paths));
111 $comparePath = '';
112 $break = FALSE;
113
114 foreach ($parts as $part) {
115 $comparePath .= $part . DIRECTORY_SEPARATOR;
116
117 foreach ($paths as $path) {
118 if (strpos($path . DIRECTORY_SEPARATOR, $comparePath) !== 0) {
119 $break = TRUE;
120 break;
121 }
122 }
123
124 if ($break) {
125 break;
126 }
127
128 $commonPath = $comparePath;
129 }
130 }
131
132 if ($commonPath !== NULL) {
133 $commonPath = self::sanitizeTrailingSeparator(
134 $commonPath,
135 DIRECTORY_SEPARATOR
136 );
137 }
138
139 return $commonPath;
140 }
141
142 /**
143 * Sanitizes a trailing separator.
144 * (e.g. 'some/path' -> 'some/path/')
145 *
146 * @param string $path The path to be sanitized
147 * @param string $separator The separator to be used
148 * @return string
149 */
150 public static function sanitizeTrailingSeparator($path, $separator = '/') {
151 return rtrim($path, $separator) . $separator;
152 }
153 }
154 ?>