1 |
1947
|
darkviper
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
5 |
|
|
*
|
6 |
|
|
* This program is free software: you can redistribute it and/or modify
|
7 |
|
|
* it under the terms of the GNU General Public License as published by
|
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
9 |
|
|
* (at your option) any later version.
|
10 |
|
|
*
|
11 |
|
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
* GNU General Public License for more details.
|
15 |
|
|
*
|
16 |
|
|
* You should have received a copy of the GNU General Public License
|
17 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 |
|
|
*/
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* AccessFileHelper.php
|
22 |
|
|
*
|
23 |
|
|
* @category Core
|
24 |
|
|
* @package Core_Routing
|
25 |
2045
|
Luisehahne
|
* @subpackage Accessfiles
|
26 |
|
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
27 |
|
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
28 |
1947
|
darkviper
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
29 |
|
|
* @version 0.0.1
|
30 |
2070
|
darkviper
|
* @revision $Revision$
|
31 |
|
|
* @link $HeadURL$
|
32 |
|
|
* @lastmodified $Date$
|
33 |
1947
|
darkviper
|
* @since File available since 01.08.2013
|
34 |
|
|
* @description this class provides some helper methodes to handle access files
|
35 |
|
|
*/
|
36 |
2045
|
Luisehahne
|
if (class_exists('AccesFile')) {;}
|
37 |
1947
|
darkviper
|
class AccessFileHelper {
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* do not delete start directory of deltree
|
41 |
|
|
*/
|
42 |
|
|
const DEL_ROOT_PRESERVE = 0;
|
43 |
|
|
/**
|
44 |
|
|
* delete start directory of deltree
|
45 |
|
|
*/
|
46 |
|
|
const DEL_ROOT_DELETE = 1;
|
47 |
|
|
/**
|
48 |
1982
|
darkviper
|
* clear logs
|
49 |
|
|
*/
|
50 |
|
|
const LOG_CLEAR = true;
|
51 |
|
|
/**
|
52 |
|
|
* preserve logs
|
53 |
|
|
*/
|
54 |
|
|
const LOG_PRESERVE = false;
|
55 |
|
|
/**
|
56 |
1947
|
darkviper
|
* to store the last delTree log
|
57 |
|
|
*/
|
58 |
|
|
static $aDelTreeLog = array();
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* private constructor to deny instantiating
|
62 |
|
|
*/
|
63 |
|
|
private function __construct() {
|
64 |
|
|
;
|
65 |
|
|
}
|
66 |
|
|
/**
|
67 |
|
|
* Test if file is an access file
|
68 |
|
|
* @param string $sFileName qualified path/file
|
69 |
|
|
* @return boolean
|
70 |
|
|
* @description test given file for typical accessfile signature
|
71 |
|
|
*/
|
72 |
|
|
static public function isAccessFile($sFileName)
|
73 |
|
|
{
|
74 |
2046
|
darkviper
|
if (!is_readable($sFileName)) { throw new AccessFileInvalidFilePathException('invalid filename ['.$sFileName.']'); }
|
75 |
1947
|
darkviper
|
$bRetval = false;
|
76 |
2045
|
Luisehahne
|
if (($sFile = file_get_contents($sFileName)) !== false) {
|
77 |
1947
|
darkviper
|
$sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
|
78 |
2045
|
Luisehahne
|
. '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
|
79 |
1947
|
darkviper
|
$bRetval = (bool) preg_match($sPattern, $sFile);
|
80 |
|
|
unset($sFile);
|
81 |
|
|
}
|
82 |
|
|
return $bRetval;
|
83 |
2045
|
Luisehahne
|
}
|
84 |
1947
|
darkviper
|
/**
|
85 |
|
|
* Delete all contents of basedir, but not the basedir itself
|
86 |
|
|
* @param string $sRootDir the content of which should be deleted
|
87 |
|
|
* @param integer $iMode the mode can be set to self::DEL_ROOT_PRESERVE(default) or self::DEL_ROOT_DELETE
|
88 |
|
|
* @return boolean false if a file or directory can't be deleted
|
89 |
|
|
*/
|
90 |
|
|
static public function delTree($sRootDir, $iMode = self::DEL_ROOT_PRESERVE)
|
91 |
|
|
{
|
92 |
|
|
// check if root dir is inside the installation and is writeable
|
93 |
|
|
$oReg = WbAdaptor::getInstance();
|
94 |
|
|
self::$aDelTreeLog = array();
|
95 |
|
|
$bResult = true;
|
96 |
2045
|
Luisehahne
|
if (!is_writeable($sRootDir)) {
|
97 |
1976
|
darkviper
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
|
98 |
1947
|
darkviper
|
return false;
|
99 |
|
|
}
|
100 |
|
|
$oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sRootDir), RecursiveIteratorIterator::CHILD_FIRST);
|
101 |
2045
|
Luisehahne
|
foreach ($oIterator as $oPath) {
|
102 |
1976
|
darkviper
|
$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
|
103 |
2045
|
Luisehahne
|
if ($oPath->isDir() && !preg_match('/\.$/s', $sPath)) {
|
104 |
1947
|
darkviper
|
// proceed directories
|
105 |
2045
|
Luisehahne
|
if (!rmdir($sPath)) {
|
106 |
1947
|
darkviper
|
$bResult = false;
|
107 |
|
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
108 |
2045
|
Luisehahne
|
} else {
|
109 |
1947
|
darkviper
|
self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
|
110 |
|
|
}
|
111 |
2045
|
Luisehahne
|
} elseif ($oPath->isFile()) {
|
112 |
1947
|
darkviper
|
// proceed files
|
113 |
2045
|
Luisehahne
|
if (!unlink($sPath)) {
|
114 |
1947
|
darkviper
|
$bResult = false;
|
115 |
|
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
116 |
2045
|
Luisehahne
|
} else {
|
117 |
1947
|
darkviper
|
self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
2045
|
Luisehahne
|
if (($iMode & self::DEL_ROOT_DELETE)) {
|
122 |
|
|
if ($bResult) { // if there was no error before
|
123 |
|
|
if (rmdir($sRootDir)) {
|
124 |
1947
|
darkviper
|
self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sRootDir);
|
125 |
|
|
return $bResult;
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
|
129 |
|
|
}
|
130 |
|
|
return $bResult;
|
131 |
|
|
}
|
132 |
|
|
/**
|
133 |
|
|
* returns the log of the last delTree request
|
134 |
1982
|
darkviper
|
* @param bool $bClearLog TRUE clears the log after request, FALSE preserve the log
|
135 |
1947
|
darkviper
|
* @return array
|
136 |
|
|
*/
|
137 |
2045
|
Luisehahne
|
static public function getDelTreeLog($bClearLog = self::LOG_CLEAR)
|
138 |
1947
|
darkviper
|
{
|
139 |
1982
|
darkviper
|
$aRetval = self::$aDelTreeLog;
|
140 |
|
|
if($bClearLog) { self::$aDelTreeLog = array(); }
|
141 |
|
|
return $aRetval;
|
142 |
1947
|
darkviper
|
}
|
143 |
|
|
|
144 |
2045
|
Luisehahne
|
/* *** following methods still are in developing right now! Do not use it! ************ */
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
|
|
* isPathInsideApplication
|
148 |
|
|
* @param string $sRootPath full base path<br />
|
149 |
|
|
* example1-a: '/www/htdocs/subdir/'
|
150 |
|
|
* example2-a: 'c://www/htdocs/subdir/'
|
151 |
|
|
* example3-a: 'http://user:pass@isteam.de:123/www/htdocs/subdir/'
|
152 |
|
|
* @param string $sFullPath full path to test<br />
|
153 |
|
|
* example1-b: '/www/htdocs/subdir/targetdir/../testfile.php'
|
154 |
|
|
* example2-b: 'c://www/htdocs/subdir/targetdir/../testfile.php'
|
155 |
|
|
* example3-b: 'http://user:pass@isteam.de:123/www/htdocs/subdir/targetdir/../testfile.php'
|
156 |
|
|
* @return type
|
157 |
|
|
*/
|
158 |
|
|
static public function isRootPartOfPath($sRootPath, $sFullPath)
|
159 |
|
|
{
|
160 |
|
|
$sRootPath = self::getRealPath($sRootPath);
|
161 |
|
|
$sFullPath = self::getRealPath($sFullPath);
|
162 |
|
|
$sRetval = (preg_match('/^'.preg_quote($sRootPath, '/').'/', $sFullPath) ? $sFullPath : null );
|
163 |
2086
|
darkviper
|
return $sRetval;
|
164 |
2045
|
Luisehahne
|
}
|
165 |
|
|
|
166 |
|
|
static public function getRealPath($sPath)
|
167 |
|
|
{
|
168 |
|
|
$iCount = 0;
|
169 |
|
|
$aResultPath = array();
|
170 |
|
|
$sPath = str_replace(array('\\', '/./'), array('/', '/'), $sPath);
|
171 |
|
|
if(preg_match('/^\/?\.\//', $sPath)) {
|
172 |
|
|
$sPath = str_replace('\\', '/', getcwd()).'/'.preg_replace('/^\/?\.\//', '', $sPath);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
$sPattern = '/((?:^[a-z]?\:\/+)|(?:^.*?\:\/\/.*?\/)|(?:^\/+))[^\/].*$/is';
|
176 |
|
|
// extract host/root - part from path save and then remove it from path
|
177 |
|
|
$sPrefix = preg_replace($sPattern, '\1', $sPath);
|
178 |
|
|
$sPath = preg_replace('/^'.preg_quote($sPrefix, '/').'/', '', $sPath);
|
179 |
|
|
// replace any tripple or more dots by doubble dots and split the path at '/' into an array.
|
180 |
|
|
// save number of replaced .. in $iCount and add somme buffer elements
|
181 |
|
|
$aTestPath = array_merge(($iCount ? array_fill(0, $iCount * 2, '###') : array()),
|
182 |
|
|
explode('/', preg_replace(array('/\.{2,}/s', '/./'), array('..', '/'), $sPath, -1, $iCount))
|
183 |
|
|
);
|
184 |
|
|
foreach ($aTestPath as $sPart) {
|
185 |
|
|
switch ($sPart) {
|
186 |
|
|
case '.': // skip part
|
187 |
|
|
continue;
|
188 |
|
|
break;
|
189 |
|
|
case '..': // step back
|
190 |
|
|
array_pop($aResultPath);
|
191 |
|
|
break;
|
192 |
|
|
default: // take part
|
193 |
|
|
$aResultPath[] = $sPart;
|
194 |
|
|
break;
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
while($aResultPath[0] == '###') {
|
198 |
|
|
array_shift($aResultPath);
|
199 |
|
|
}
|
200 |
|
|
$sResultPath = $sPrefix.implode('/', $aResultPath);
|
201 |
|
|
return $sResultPath;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
|
205 |
1947
|
darkviper
|
} // end of class AccessFileHelper
|