1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-23 06:44:38 +02:00

Add new $files->unlink() and $files->rename() methods to files API var, plus enhance many file methods with additional security for path verification, and update PW's various usages of unlink/rename to use the new versions provided by the files API var.

This commit is contained in:
Ryan Cramer
2018-10-31 09:30:15 -04:00
parent 0f9eb0aaf5
commit 54537e77fa
23 changed files with 359 additions and 91 deletions

View File

@@ -368,7 +368,7 @@ class ImageSizerEngineIMagick extends ImageSizerEngine {
}
// save to file
@unlink($dstFilename);
$this->wire('files')->unlink($dstFilename);
@clearstatcache(dirname($dstFilename));
##if(!$this->im->writeImage($this->destFilename)) {
// We use this approach for saving so that it behaves the same like core ImageSizer with images that

View File

@@ -760,8 +760,8 @@ class InputfieldFile extends Inputfield implements InputfieldItemList, Inputfiel
$err = false;
foreach($ul->getOverwrittenFiles() as $bakFile => $newFile) {
if(basename($newFile) != $filename) continue;
unlink($newFile);
rename($bakFile, $newFile); // restore
$this->wire('files')->unlink($newFile);
$this->wire('files')->rename($bakFile, $newFile); // restore
$ul->error(sprintf($this->_('Refused file %s because it is already on the file system and owned by a different field.'), $filename));
$err = true;
}

View File

@@ -439,8 +439,8 @@ class InputfieldImage extends InputfieldFile implements InputfieldItemList, Inpu
}
$pagefile2 = $pagefile->size($maxWidth, $maxHeight, array('cropping' => false));
if($pagefile->filename != $pagefile2->filename) {
unlink($pagefile->filename);
rename($pagefile2->filename, $pagefile->filename);
$this->wire('files')->unlink($pagefile->filename);
$this->wire('files')->rename($pagefile2->filename, $pagefile->filename);
}
$pagefile->getImageInfo(true); // force it to reload its dimensions
}
@@ -510,7 +510,7 @@ class InputfieldImage extends InputfieldFile implements InputfieldItemList, Inpu
$exists = is_file($f);
if($exists && $remove) {
unlink($f);
$this->wire('files')->unlink($f);
$exists = false;
}

View File

@@ -648,7 +648,7 @@ class ProcessLanguageTranslator extends Process {
if(is_null($this->fp)) {
$f = $this->language->filesManager()->path() . '.phrase-index.txt'; // @todo make hidden
if(is_file($f)) unlink($f);
if(is_file($f)) $this->wire('files')->unlink($f);
$this->fp = fopen($f, "a");
}

View File

@@ -151,7 +151,7 @@ class LazyCron extends WireData implements Module {
// other LazyCron process potentially running
if(filemtime($lockfile) < (time() - 3600)) {
// expired lock file, some fatal error must have occurred during last LazyCron run
@unlink($lockfile);
$this->wire('files')->unlink($lockfile);
} else {
// skip running this time as an active lock file exists
return;
@@ -168,7 +168,7 @@ class LazyCron extends WireData implements Module {
// file is probably locked, so skip it this time
if($filedata === false) {
@unlink($lockfile);
$this->wire('files')->unlink($lockfile);
return;
}
} else {
@@ -210,7 +210,7 @@ class LazyCron extends WireData implements Module {
if($this->config->chmodFile) @chmod($filename, octdec($this->config->chmodFile));
}
@unlink($lockfile);
$this->wire('files')->unlink($lockfile);
}
/**

View File

@@ -743,10 +743,10 @@ class PageRender extends WireData implements Module, ConfigurableModule {
foreach($d as $f) {
if(!$f->isDir() && preg_match('/\.cache$/D', $f->getFilename())) {
$numFiles++;
@unlink($f->getPathname());
$this->wire('files')->unlink($f->getPathname());
}
}
@rmdir($file->getPathname());
$this->wire('files')->rmdir($file->getPathname());
}
if($clearNow) {

View File

@@ -234,13 +234,13 @@ class ProcessModuleInstall extends Wire {
$mkdirDestination = false;
try {
$files = wireUnzipFile($file, $tempDir);
if(is_file($file)) unlink($file);
$files = $this->wire('files')->unzip($file, $tempDir);
if(is_file($file)) $this->wire('files')->unlink($file, true);
foreach($files as $f) $this->message("Extracted: $f", Notice::debug);
} catch(\Exception $e) {
$this->error($e->getMessage());
if(is_file($file)) unlink($file);
if(is_file($file)) $this->wire('files')->unlink($file, true);
return false;
}
@@ -283,7 +283,7 @@ class ProcessModuleInstall extends Wire {
if(!$success) {
$this->error($this->_('Unable to copy module files:') . ' ' . $dirLabel);
if($mkdirDestination && !wireRmdir($destinationDir, true)) {
if($mkdirDestination && !$this->wire('files')->rmdir($destinationDir, true)) {
$this->error($this->_('Could not delete failed module dir:') . ' ' . $destinationDir, Notice::log);
}
}
@@ -306,11 +306,13 @@ class ProcessModuleInstall extends Wire {
// remove symbolic link
unlink(rtrim($moduleDir, '/'));
$dir = str_replace($this->wire('config')->paths->root, '/', $moduleDir);
$this->error(sprintf($this->_('Please note that %s was a symbolic link and has been converted to a regular directory'), $dir), Notice::warning);
$this->warning(sprintf(
$this->_('Please note that %s was a symbolic link and has been converted to a regular directory'), $dir
));
} else {
// module is a regular directory
// just rename it to become the new backup dir
if(rename($moduleDir, $backupDir)) $success = true;
if($this->wire('files')->rename($moduleDir, $backupDir)) $success = true;
}
if($success) {
@@ -352,6 +354,7 @@ class ProcessModuleInstall extends Wire {
$tempDir = $this->getTempDir();
/** @var WireUpload $ul */
$ul = $this->wire(new WireUpload($inputName));
$ul->setValidExtensions(array('zip'));
$ul->setMaxFiles(1);
@@ -416,7 +419,7 @@ class ProcessModuleInstall extends Wire {
} catch(\Exception $e) {
$this->error($e->getMessage());
@unlink($tempZIP);
$this->wire('files')->unlink($tempZIP);
}
return $success ? $destinationDir : false;

View File

@@ -1154,15 +1154,15 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$thumb = $thumb['thumb'];
if($thumb->url != $original->url) {
// there is a thumbnail, distinct from the original image
unlink($thumb->filename);
$this->wire('files')->unlink($thumb->filename);
}
}
}
// replace original image
$target = $path . $this->original;
unlink($target);
rename($image2->filename(), $target);
$this->wire('files')->unlink($target);
$this->wire('files')->rename($image2->filename(), $target);
$this->wire('pages')->uncacheAll();
$page = $this->wire('pages')->get($this->page->id);
@@ -1365,7 +1365,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
foreach($delete as $name) {
if(!isset($variations[$name])) continue;
$info = $variations[$name];
if(is_file($info['path']) && unlink($info['path'])) {
if(is_file($info['path']) && $this->wire('files')->unlink($info['path'])) {
$this->message($this->_('Deleted image variation') . " - $info[url]");
unset($variations[$name]);
} else {

View File

@@ -199,7 +199,7 @@ class ProcessPagesExportImport extends Process {
if(!$zipFile || !is_file($zipFile)) throw new WireException('No ZIP file found: ' . $zipFile);
$unzipPath = $this->exportImport->getExportPath('import-zip');
$zipFileItems = $this->wire('files')->unzip($zipFile, $unzipPath);
unlink($zipFile);
$this->wire('files')->unlink($zipFile);
if(empty($zipFileItems)) throw new WireException("No files found in ZIP");
$jsonFile = $unzipPath . "pages.json";
$this->wire('session')->setFor($this, 'filesPath', $unzipPath);
@@ -1109,7 +1109,7 @@ class ProcessPagesExportImport extends Process {
'forceDownload' => true,
'exit' => false
));
unlink($zipFile);
$this->wire('files')->unlink($zipFile);
exit;
} else {
throw new WireException('Export failed during ZIP file generation');