1
|
<?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
|
* @subpackage Accessfiles
|
26
|
* @copyright Manuela v.d.Decken <manuela@isteam.de>
|
27
|
* @author Manuela v.d.Decken <manuela@isteam.de>
|
28
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
29
|
* @version 0.0.1
|
30
|
* @revision $Revision: $
|
31
|
* @link $HeadURL: $
|
32
|
* @lastmodified $Date: $
|
33
|
* @since File available since 01.08.2013
|
34
|
* @description this class provides some helper methodes to handle access files
|
35
|
*/
|
36
|
if (class_exists('AccesFile')) {;}
|
37
|
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
|
* clear logs
|
49
|
*/
|
50
|
const LOG_CLEAR = true;
|
51
|
/**
|
52
|
* preserve logs
|
53
|
*/
|
54
|
const LOG_PRESERVE = false;
|
55
|
/**
|
56
|
* 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
|
if (!is_readable($sFileName)) { throw new AccessFileInvalidFilePathException('invalid filename ['.$sFileName.']');
|
75
|
$bRetval = false;
|
76
|
if (($sFile = file_get_contents($sFileName)) !== false) {
|
77
|
$sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
|
78
|
. '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
|
79
|
$bRetval = (bool) preg_match($sPattern, $sFile);
|
80
|
unset($sFile);
|
81
|
}
|
82
|
return $bRetval;
|
83
|
}
|
84
|
}
|
85
|
/**
|
86
|
* Delete all contents of basedir, but not the basedir itself
|
87
|
* @param string $sRootDir the content of which should be deleted
|
88
|
* @param integer $iMode the mode can be set to self::DEL_ROOT_PRESERVE(default) or self::DEL_ROOT_DELETE
|
89
|
* @return boolean false if a file or directory can't be deleted
|
90
|
*/
|
91
|
static public function delTree($sRootDir, $iMode = self::DEL_ROOT_PRESERVE)
|
92
|
{
|
93
|
// check if root dir is inside the installation and is writeable
|
94
|
$oReg = WbAdaptor::getInstance();
|
95
|
self::$aDelTreeLog = array();
|
96
|
$bResult = true;
|
97
|
if (!is_writeable($sRootDir)) {
|
98
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
|
99
|
return false;
|
100
|
}
|
101
|
$oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sRootDir), RecursiveIteratorIterator::CHILD_FIRST);
|
102
|
foreach ($oIterator as $oPath) {
|
103
|
$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
|
104
|
if ($oPath->isDir() && !preg_match('/\.$/s', $sPath)) {
|
105
|
// proceed directories
|
106
|
if (!rmdir($sPath)) {
|
107
|
$bResult = false;
|
108
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
109
|
} else {
|
110
|
self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
|
111
|
}
|
112
|
} elseif ($oPath->isFile()) {
|
113
|
// proceed files
|
114
|
if (!unlink($sPath)) {
|
115
|
$bResult = false;
|
116
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
117
|
} else {
|
118
|
self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
if (($iMode & self::DEL_ROOT_DELETE)) {
|
123
|
if ($bResult) { // if there was no error before
|
124
|
if (rmdir($sRootDir)) {
|
125
|
self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sRootDir);
|
126
|
return $bResult;
|
127
|
}
|
128
|
}
|
129
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
|
130
|
}
|
131
|
return $bResult;
|
132
|
}
|
133
|
/**
|
134
|
* returns the log of the last delTree request
|
135
|
* @param bool $bClearLog TRUE clears the log after request, FALSE preserve the log
|
136
|
* @return array
|
137
|
*/
|
138
|
static public function getDelTreeLog($bClearLog = self::LOG_CLEAR)
|
139
|
{
|
140
|
$aRetval = self::$aDelTreeLog;
|
141
|
if($bClearLog) { self::$aDelTreeLog = array(); }
|
142
|
return $aRetval;
|
143
|
}
|
144
|
|
145
|
/* *** following methods still are in developing right now! Do not use it! ************ */
|
146
|
|
147
|
/**
|
148
|
* isPathInsideApplication
|
149
|
* @param string $sRootPath full base path<br />
|
150
|
* example1-a: '/www/htdocs/subdir/'
|
151
|
* example2-a: 'c://www/htdocs/subdir/'
|
152
|
* example3-a: 'http://user:pass@isteam.de:123/www/htdocs/subdir/'
|
153
|
* @param string $sFullPath full path to test<br />
|
154
|
* example1-b: '/www/htdocs/subdir/targetdir/../testfile.php'
|
155
|
* example2-b: 'c://www/htdocs/subdir/targetdir/../testfile.php'
|
156
|
* example3-b: 'http://user:pass@isteam.de:123/www/htdocs/subdir/targetdir/../testfile.php'
|
157
|
* @return type
|
158
|
*/
|
159
|
static public function isRootPartOfPath($sRootPath, $sFullPath)
|
160
|
{
|
161
|
$sRootPath = self::getRealPath($sRootPath);
|
162
|
$sFullPath = self::getRealPath($sFullPath);
|
163
|
$sRetval = (preg_match('/^'.preg_quote($sRootPath, '/').'/', $sFullPath) ? $sFullPath : null );
|
164
|
return $sRetval;;
|
165
|
}
|
166
|
|
167
|
static public function getRealPath($sPath)
|
168
|
{
|
169
|
$iCount = 0;
|
170
|
$aResultPath = array();
|
171
|
$sPath = str_replace(array('\\', '/./'), array('/', '/'), $sPath);
|
172
|
if(preg_match('/^\/?\.\//', $sPath)) {
|
173
|
$sPath = str_replace('\\', '/', getcwd()).'/'.preg_replace('/^\/?\.\//', '', $sPath);
|
174
|
}
|
175
|
|
176
|
$sPattern = '/((?:^[a-z]?\:\/+)|(?:^.*?\:\/\/.*?\/)|(?:^\/+))[^\/].*$/is';
|
177
|
// extract host/root - part from path save and then remove it from path
|
178
|
$sPrefix = preg_replace($sPattern, '\1', $sPath);
|
179
|
$sPath = preg_replace('/^'.preg_quote($sPrefix, '/').'/', '', $sPath);
|
180
|
// replace any tripple or more dots by doubble dots and split the path at '/' into an array.
|
181
|
// save number of replaced .. in $iCount and add somme buffer elements
|
182
|
$aTestPath = array_merge(($iCount ? array_fill(0, $iCount * 2, '###') : array()),
|
183
|
explode('/', preg_replace(array('/\.{2,}/s', '/./'), array('..', '/'), $sPath, -1, $iCount))
|
184
|
);
|
185
|
foreach ($aTestPath as $sPart) {
|
186
|
switch ($sPart) {
|
187
|
case '.': // skip part
|
188
|
continue;
|
189
|
break;
|
190
|
case '..': // step back
|
191
|
array_pop($aResultPath);
|
192
|
break;
|
193
|
default: // take part
|
194
|
$aResultPath[] = $sPart;
|
195
|
break;
|
196
|
}
|
197
|
}
|
198
|
while($aResultPath[0] == '###') {
|
199
|
array_shift($aResultPath);
|
200
|
}
|
201
|
$sResultPath = $sPrefix.implode('/', $aResultPath);
|
202
|
return $sResultPath;
|
203
|
}
|
204
|
|
205
|
|
206
|
} // end of class AccessFileHelper
|