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 |
|
|
* @copyright M.v.d.Decken <manuela@isteam.de>
|
26 |
|
|
* @author M.v.d.Decken <manuela@isteam.de>
|
27 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
28 |
|
|
* @version 0.0.1
|
29 |
|
|
* @revision $Revision: $
|
30 |
|
|
* @link $HeadURL: $
|
31 |
|
|
* @lastmodified $Date: $
|
32 |
|
|
* @since File available since 01.08.2013
|
33 |
|
|
* @description this class provides some helper methodes to handle access files
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
class AccessFileHelper {
|
37 |
|
|
|
38 |
|
|
/**
|
39 |
|
|
* do not delete start directory of deltree
|
40 |
|
|
*/
|
41 |
|
|
const DEL_ROOT_PRESERVE = 0;
|
42 |
|
|
/**
|
43 |
|
|
* delete start directory of deltree
|
44 |
|
|
*/
|
45 |
|
|
const DEL_ROOT_DELETE = 1;
|
46 |
|
|
/**
|
47 |
|
|
* to store the last delTree log
|
48 |
|
|
*/
|
49 |
|
|
static $aDelTreeLog = array();
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* private constructor to deny instantiating
|
53 |
|
|
*/
|
54 |
|
|
private function __construct() {
|
55 |
|
|
;
|
56 |
|
|
}
|
57 |
|
|
/**
|
58 |
|
|
* Test if file is an access file
|
59 |
|
|
* @param string $sFileName qualified path/file
|
60 |
|
|
* @return boolean
|
61 |
|
|
* @description test given file for typical accessfile signature
|
62 |
|
|
*/
|
63 |
|
|
static public function isAccessFile($sFileName)
|
64 |
|
|
{
|
65 |
|
|
$bRetval = false;
|
66 |
|
|
if (($sFile = file_get_contents($sFileName)) !== false)
|
67 |
|
|
{
|
68 |
|
|
$sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
|
69 |
|
|
. '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
|
70 |
|
|
$bRetval = (bool) preg_match($sPattern, $sFile);
|
71 |
|
|
unset($sFile);
|
72 |
|
|
}
|
73 |
|
|
return $bRetval;
|
74 |
|
|
}
|
75 |
|
|
/**
|
76 |
|
|
* Delete all contents of basedir, but not the basedir itself
|
77 |
|
|
* @param string $sRootDir the content of which should be deleted
|
78 |
|
|
* @param integer $iMode the mode can be set to self::DEL_ROOT_PRESERVE(default) or self::DEL_ROOT_DELETE
|
79 |
|
|
* @return boolean false if a file or directory can't be deleted
|
80 |
|
|
*/
|
81 |
|
|
static public function delTree($sRootDir, $iMode = self::DEL_ROOT_PRESERVE)
|
82 |
|
|
{
|
83 |
|
|
// check if root dir is inside the installation and is writeable
|
84 |
|
|
$oReg = WbAdaptor::getInstance();
|
85 |
|
|
self::$aDelTreeLog = array();
|
86 |
|
|
$bResult = true;
|
87 |
|
|
if (!is_writeable($sRootDir))
|
88 |
|
|
{
|
89 |
1976
|
darkviper
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
|
90 |
1947
|
darkviper
|
return false;
|
91 |
|
|
}
|
92 |
|
|
$oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sRootDir), RecursiveIteratorIterator::CHILD_FIRST);
|
93 |
|
|
foreach ($oIterator as $oPath)
|
94 |
|
|
{
|
95 |
1976
|
darkviper
|
$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
|
96 |
|
|
if ($oPath->isDir() && !preg_match('/\.$/s', $sPath))
|
97 |
1947
|
darkviper
|
{
|
98 |
|
|
// proceed directories
|
99 |
|
|
if (!rmdir($sPath))
|
100 |
|
|
{
|
101 |
|
|
$bResult = false;
|
102 |
|
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
103 |
|
|
}else {
|
104 |
|
|
self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
|
105 |
|
|
}
|
106 |
|
|
} elseif ($oPath->isFile())
|
107 |
|
|
{
|
108 |
|
|
// proceed files
|
109 |
|
|
if (!unlink($sPath))
|
110 |
|
|
{
|
111 |
|
|
$bResult = false;
|
112 |
|
|
self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
|
113 |
|
|
}else {
|
114 |
|
|
self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
if (($iMode & self::DEL_ROOT_DELETE))
|
119 |
|
|
{
|
120 |
|
|
if ($bResult)
|
121 |
|
|
{ // if there was no error before
|
122 |
|
|
if (rmdir($sRootDir))
|
123 |
|
|
{
|
124 |
|
|
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 |
|
|
* @return array
|
135 |
|
|
*/
|
136 |
|
|
static function getDelTreeLog()
|
137 |
|
|
{
|
138 |
|
|
return self::$aDelTreeLog;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
} // end of class AccessFileHelper
|