Revision 1949
Added by darkviper about 11 years ago
AccessFile.php | ||
---|---|---|
131 | 131 |
|
132 | 132 |
/** |
133 | 133 |
* Rename an existing Accessfile |
134 |
* @param string the new filename without path and without extension
|
|
134 |
* @param string $sNewName the new filename without path and without extension
|
|
135 | 135 |
* @return boolean |
136 | 136 |
* @throws AccessFileInvalidFilenameException |
137 | 137 |
* @throws AccessFileAlreadyExistsException |
... | ... | |
140 | 140 |
public function rename($sNewName) |
141 | 141 |
{ |
142 | 142 |
// sanitize new filename |
143 |
if (!preg_match('/[^\.\\\/]*/si', $sNewName)) { |
|
144 |
throw new AccessFileInvalidFilenameException('invalid filename [' . $sNewName . ']');
|
|
143 |
if (!preg_match('/[^\.\\ \/]*/si', $sNewName)) {
|
|
144 |
throw new AccessFileInvalidFilenameException('invalid filename [' . str_replace($this->_oReg->AppPath, '', $sNewName) . ']');
|
|
145 | 145 |
} |
146 | 146 |
// prepare old/new file-/dirname |
147 |
$sOldFilename = $this->_sFileName; |
|
147 |
$sOldFilename = $this->_sFileName;
|
|
148 | 148 |
$sOldSubDirname = dirname($sOldFilename) . '/'; |
149 |
$sBaseDirname = dirname($sOldSubDirname) . '/'; |
|
149 | 150 |
$sPattern = '/^(' . preg_quote($sOldSubDirname, '/') . ')([^\/\.]+?)(\.[a-z0-9]+)$/siU'; |
150 | 151 |
$sNewFilename = preg_replace($sPattern, '\1' . $sNewName . '\3', $sOldFilename); |
151 | 152 |
$sNewSubDirname = dirname($sNewFilename) . '/'; |
152 | 153 |
if (file_exists($sNewFilename) || file_exists($sNewSubDirname)) { |
153 |
// check if new filename already exists
|
|
154 |
throw new AccessFileAlreadyExistsException('new file/dirname already exists [' . $sNewName . ']');
|
|
154 |
// if new filename or directory already exists
|
|
155 |
throw new AccessFileAlreadyExistsException('new file/dirname already exists [' . str_replace($this->_oReg->AppPath, '', $sNewName) . ']');
|
|
155 | 156 |
} |
156 |
// try to rename a depending subdirectory
|
|
157 |
$bSubdirRenamed = false;
|
|
158 |
$bRetval = true;
|
|
159 |
if (file_exists($sOldSubDirname))
|
|
160 |
{
|
|
161 |
// old subdir exists but is it writeable?
|
|
162 |
if (($bRetval = is_writable($sOldSubDirname)))
|
|
157 |
if(!is_writeable($sOldFilename)) {
|
|
158 |
throw new AccessFileRenameException('unable to rename file or file not exists [' . str_replace($this->_oReg->AppPath, '', $sOldFilename) . ']');
|
|
159 |
}
|
|
160 |
$bSubdirRenamed = false; // set default value
|
|
161 |
if(file_exists($sOldSubDirname))
|
|
162 |
{ //
|
|
163 |
if(is_writeable($sOldSubDirname))
|
|
163 | 164 |
{ |
164 |
if (rename($sOldSubDirname, $sNewSubDirname)) {
|
|
165 |
$bRetval = $bSubdirRenamed = true;
|
|
165 |
if(!( $bSubdirRenamed = rename($sOldSubDirname, $sNewSubDirname))) {
|
|
166 |
throw new AccessFileRenameException('unable to rename directory [' . str_replace($this->_oReg->AppPath, '', $sOldSubDirname) . ']');
|
|
166 | 167 |
} |
168 |
} else { |
|
169 |
throw new AccessFileRenameException('directory is not writeable [' . str_replace($this->_oReg->AppPath, '', $sOldSubDirname) . ']'); |
|
167 | 170 |
} |
168 | 171 |
} |
169 |
if (!$bRetval) { |
|
170 |
// if old subdir exists and renaming failed |
|
171 |
throw new AccessFileRenameException('unable to rename directory [' . $sOldSubDirname . ']'); |
|
172 |
// try to rename accessfile |
|
173 |
if(!rename($sOldFilename, $sNewFilename)) { |
|
174 |
if($bSubdirRenamed) { rename($sNewSubDirname, $sOldSubDirname); } |
|
175 |
throw new AccessFileRenameException('unable to rename file [' . str_replace($this->_oReg->AppPath, '', $sOldFilename) . ']'); |
|
172 | 176 |
} |
173 |
if (($bRetval = is_writeable($this->_sFileName))) { |
|
174 |
// try to rename accessfile |
|
175 |
$bRetval = rename($sOldFilename, $sNewFilename); |
|
176 |
} |
|
177 |
if (!$bRetval) |
|
178 |
{ |
|
179 |
if ($bSubdirRenamed) { |
|
180 |
// on error undo renaming of subdir |
|
181 |
rename($sNewSubDirname, $sOldSubDirname); |
|
182 |
} |
|
183 |
throw new AccessFileRenameException('unable to rename file [' . $sOldFilename . ']'); |
|
184 |
} |
|
177 |
return true; |
|
185 | 178 |
} |
186 | 179 |
|
187 | 180 |
/** |
... | ... | |
337 | 330 |
{ |
338 | 331 |
$sFileContent |
339 | 332 |
= '<?php' . "\n" |
340 |
. '// *** This file is generated by ' . $this->_oReg->AppName
|
|
341 |
. ' Ver.' . $this->_oReg->AppVersion |
|
342 |
. ($this->_oReg->AppServicePack != '' ? $this->_oReg->AppServicePack : '') |
|
333 |
. '// *** This file was created automatically by ' ."\n"
|
|
334 |
. '// *** ' . $this->_oReg->AppName . ' Ver.' . $this->_oReg->AppVersion
|
|
335 |
. ($this->_oReg->AppServicePack != '' ? ' '.$this->_oReg->AppServicePack : '')
|
|
343 | 336 |
. ' Rev.' . $this->_oReg->AppRevision . "\n" |
344 |
. '// *** Creation date: ' . date('c') . "\n" |
|
345 |
. '// *** Do not modify this file manually' . "\n" |
|
346 |
. '// *** This file will be rebuild from time to time!!' . "\n" |
|
347 |
. '// *** Then all manual changes will get lost!! ' . "\n" |
|
348 |
. '// *************************************************' . "\n"; |
|
337 |
. '// *** on ' . date('c') . "\n" |
|
338 |
. '//' . "\n" |
|
339 |
. '// *** Warning *************************************' . "\n" |
|
340 |
. '// *** Please do not manually change this file!' . "\n" |
|
341 |
. '// *** It will be recreated from time to time and' . "\n" |
|
342 |
. '// *** then all manual changes will get lost!!' . "\n" |
|
343 |
. '// *************************************************' . "\n" |
|
344 |
. '//' . "\n"; |
|
349 | 345 |
foreach ($this->_aVars as $sKey => $sVar) { |
350 | 346 |
$sFileContent .= "\t" . '$' . $sKey . ' = ' . $sVar . ';' . "\n"; |
351 | 347 |
} |
352 | 348 |
foreach ($this->_aConsts as $sKey => $sVar) { |
353 |
$sFileContent .= "\t" . 'define(\'' . $sKey . '\', ' . $sVar . '); // *deprecated command*' . "\n";
|
|
349 |
$sFileContent .= "\t" . 'define(\'' . $sKey . '\', ' . $sVar . '); // ** deprecated command **' . "\n";
|
|
354 | 350 |
} |
355 | 351 |
$sFileContent |
356 |
.= "\t" . 'require(\'' . $sIndexFile . '\');' . "\n" |
|
352 |
.="\t" . 'require(\'' . $sIndexFile . '\');' . "\n" |
|
353 |
. "\t" . 'exit(0);' . "\n" |
|
357 | 354 |
. '// *************************************************' . "\n" |
358 | 355 |
. '// end of file' . "\n"; |
359 | 356 |
|
... | ... | |
381 | 378 |
class AccessFileException extends Exception { } |
382 | 379 |
class AccessFileConstForbiddenException extends AccessFileException { } |
383 | 380 |
class AccessFileInvalidFilePathException extends AccessFileException { } |
381 |
class AccessFileInvalidFilenameException extends AccessFileException { } |
|
384 | 382 |
class AccessFileAlreadyExistsException extends AccessFileException { } |
385 | 383 |
class AccessFileNotWriteableException extends AccessFileException { } |
386 | 384 |
class AccessFileIsInvalidFilenameException extends AccessFileException { } |
387 | 385 |
class AccessFileIsNoAccessfileException extends AccessFileException { } |
388 | 386 |
class AccessFileInvalidStructureException extends AccessFileException { } |
387 |
class AccessFileRenameException extends AccessFileException { } |
Also available in: Unified diff
updated for some corrections in class AccessFile