Project

General

Profile

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: 2086 $
31
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/AccessFileHelper.php $
32
 * @lastmodified $Date: 2014-01-16 04:41:48 +0100 (Thu, 16 Jan 2014) $
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
 * 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
		if (!is_writeable($sRootDir)) {
97
			self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
98
			return false;
99
		}
100
		$oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sRootDir), RecursiveIteratorIterator::CHILD_FIRST);
101
		foreach ($oIterator as $oPath) {
102
			$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
103
			if ($oPath->isDir() && !preg_match('/\.$/s', $sPath)) {
104
				// proceed directories
105
				if (!rmdir($sPath)) {
106
					$bResult = false;
107
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
108
				} else {
109
					self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
110
				}
111
			} elseif ($oPath->isFile()) {
112
				// proceed files
113
				if (!unlink($sPath)) {
114
					$bResult = false;
115
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
116
				} else {
117
					self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
118
				}
119
			}
120
		}
121
		if (($iMode & self::DEL_ROOT_DELETE)) {
122
			if ($bResult) { // if there was no error before
123
				if (rmdir($sRootDir)) {
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
 * @param  bool  $bClearLog   TRUE clears the log after request, FALSE preserve the log
135
 * @return array
136
 */
137
	static public function getDelTreeLog($bClearLog = self::LOG_CLEAR)
138
	{
139
		$aRetval = self::$aDelTreeLog;
140
		if($bClearLog) { self::$aDelTreeLog = array(); }
141
		return $aRetval;
142
	}
143

    
144
/* *** 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
		return $sRetval;
164
	}
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
} // end of class AccessFileHelper
(2-2/38)