Project

General

Profile

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 1982 darkviper
 * clear logs
48
 */
49
	const LOG_CLEAR = true;
50
/**
51
 * preserve logs
52
 */
53
	const LOG_PRESERVE = false;
54
/**
55 1947 darkviper
 * to store the last delTree log
56
 */
57
	static $aDelTreeLog = array();
58
59
/**
60
 * private constructor to deny instantiating
61
 */
62
	private function __construct() {
63
		;
64
	}
65
/**
66
 * Test if file is an access file
67
 * @param       string $sFileName  qualified path/file
68
 * @return      boolean
69
 * @description test given file for typical accessfile signature
70
 */
71
	static public function isAccessFile($sFileName)
72
	{
73
		$bRetval = false;
74
		if (($sFile = file_get_contents($sFileName)) !== false)
75
		{
76
			$sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
77
					. '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
78
			$bRetval = (bool) preg_match($sPattern, $sFile);
79
			unset($sFile);
80
		}
81
		return $bRetval;
82
	}
83
/**
84
 * Delete all contents of basedir, but not the basedir itself
85
 * @param string  $sRootDir the content of which should be deleted
86
 * @param integer $iMode    the mode can be set to self::DEL_ROOT_PRESERVE(default) or self::DEL_ROOT_DELETE
87
 * @return boolean          false if a file or directory can't be deleted
88
 */
89
	static public function delTree($sRootDir, $iMode = self::DEL_ROOT_PRESERVE)
90
	{
91
		// check if root dir is inside the installation and is writeable
92
		$oReg = WbAdaptor::getInstance();
93
		self::$aDelTreeLog = array();
94
		$bResult = true;
95
		if (!is_writeable($sRootDir))
96
		{
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
		foreach ($oIterator as $oPath)
102
		{
103 1976 darkviper
			$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
104
			if ($oPath->isDir() && !preg_match('/\.$/s', $sPath))
105 1947 darkviper
			{
106
				// proceed directories
107
				if (!rmdir($sPath))
108
				{
109
					$bResult = false;
110
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
111
				}else {
112
					self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
113
				}
114
			} elseif ($oPath->isFile())
115
			{
116
				// proceed files
117
				if (!unlink($sPath))
118
				{
119
					$bResult = false;
120
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
121
				}else {
122
					self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
123
				}
124
			}
125
		}
126
		if (($iMode & self::DEL_ROOT_DELETE))
127
		{
128
			if ($bResult)
129
			{ // if there was no error before
130
				if (rmdir($sRootDir))
131
				{
132
					self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sRootDir);
133
					return $bResult;
134
				}
135
			}
136
			self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
137
		}
138
		return $bResult;
139
	}
140
/**
141
 * returns the log of the last delTree request
142 1982 darkviper
 * @param  bool  $bClearLog   TRUE clears the log after request, FALSE preserve the log
143 1947 darkviper
 * @return array
144
 */
145 1982 darkviper
	static function getDelTreeLog($bClearLog = self::LOG_CLEAR)
146 1947 darkviper
	{
147 1982 darkviper
		$aRetval = self::$aDelTreeLog;
148
		if($bClearLog) { self::$aDelTreeLog = array(); }
149
		return $aRetval;
150 1947 darkviper
	}
151
152
} // end of class AccessFileHelper