Project

General

Profile

« Previous | Next » 

Revision 2045

Added by Dietmar almost 11 years ago

! update Access Files Classes with easier handling
for modules who create their own accessfiles

View differences:

AccessFileHelper.php
22 22
 *
23 23
 * @category     Core
24 24
 * @package      Core_Routing
25
 * @copyright    M.v.d.Decken <manuela@isteam.de>
26
 * @author       M.v.d.Decken <manuela@isteam.de>
25
 * @subpackage   Accessfiles
26
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
27
 * @author       Manuela v.d.Decken <manuela@isteam.de>
27 28
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28 29
 * @version      0.0.1
29 30
 * @revision     $Revision: $
......
32 33
 * @since        File available since 01.08.2013
33 34
 * @description  this class provides some helper methodes to handle access files
34 35
 */
35

  
36
if (class_exists('AccesFile')) {;}
36 37
class AccessFileHelper {
37 38

  
38 39
/**
......
70 71
 */
71 72
	static public function isAccessFile($sFileName)
72 73
	{
74
		if (!is_readable($sFileName)) { throw new AccessFileInvalidFilePathException('invalid filename ['.$sFileName.']');
73 75
		$bRetval = false;
74
		if (($sFile = file_get_contents($sFileName)) !== false)
75
		{
76
		if (($sFile = file_get_contents($sFileName)) !== false) {
76 77
			$sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
77
					. '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
78
			          . '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
78 79
			$bRetval = (bool) preg_match($sPattern, $sFile);
79 80
			unset($sFile);
80 81
		}
81 82
		return $bRetval;
82
	}
83
    	}
84
    }
83 85
/**
84 86
 * Delete all contents of basedir, but not the basedir itself
85 87
 * @param string  $sRootDir the content of which should be deleted
......
92 94
		$oReg = WbAdaptor::getInstance();
93 95
		self::$aDelTreeLog = array();
94 96
		$bResult = true;
95
		if (!is_writeable($sRootDir))
96
		{
97
		if (!is_writeable($sRootDir)) {
97 98
			self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sRootDir);
98 99
			return false;
99 100
		}
100 101
		$oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sRootDir), RecursiveIteratorIterator::CHILD_FIRST);
101
		foreach ($oIterator as $oPath)
102
		{
102
		foreach ($oIterator as $oPath) {
103 103
			$sPath = rtrim(str_replace('\\', '/', $oPath->__toString()), '/');
104
			if ($oPath->isDir() && !preg_match('/\.$/s', $sPath))
105
			{
104
			if ($oPath->isDir() && !preg_match('/\.$/s', $sPath)) {
106 105
				// proceed directories
107
				if (!rmdir($sPath))
108
				{
106
				if (!rmdir($sPath)) {
109 107
					$bResult = false;
110 108
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
111
				}else {
109
				} else {
112 110
					self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sPath);
113 111
				}
114
			} elseif ($oPath->isFile())
115
			{
112
			} elseif ($oPath->isFile()) {
116 113
				// proceed files
117
				if (!unlink($sPath))
118
				{
114
				if (!unlink($sPath)) {
119 115
					$bResult = false;
120 116
					self::$aDelTreeLog[] = 'insufficient rights or directory not empty in : '.str_replace($oReg->AppPath, '', $sPath);
121
				}else {
117
				} else {
122 118
					self::$aDelTreeLog[] = 'File successful deleted : '.str_replace($oReg->AppPath, '', $sPath);
123 119
				}
124 120
			}
125 121
		}
126
		if (($iMode & self::DEL_ROOT_DELETE))
127
		{
128
			if ($bResult)
129
			{ // if there was no error before
130
				if (rmdir($sRootDir))
131
				{
122
		if (($iMode & self::DEL_ROOT_DELETE)) {
123
			if ($bResult) { // if there was no error before
124
				if (rmdir($sRootDir)) {
132 125
					self::$aDelTreeLog[] = 'Directory successful removed : '.str_replace($oReg->AppPath, '', $sRootDir);
133 126
					return $bResult;
134 127
				}
......
142 135
 * @param  bool  $bClearLog   TRUE clears the log after request, FALSE preserve the log
143 136
 * @return array
144 137
 */
145
	static function getDelTreeLog($bClearLog = self::LOG_CLEAR)
138
	static public function getDelTreeLog($bClearLog = self::LOG_CLEAR)
146 139
	{
147 140
		$aRetval = self::$aDelTreeLog;
148 141
		if($bClearLog) { self::$aDelTreeLog = array(); }
149 142
		return $aRetval;
150 143
	}
151 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

  
152 206
} // end of class AccessFileHelper

Also available in: Unified diff