Permalink
Browse files

BUGFIX #5188 Upload and Folder don't handle the duplicate naming of f…

…iles that have no extension (from r101050)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111572 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 9f2ca79 commit 2ea0e3e331abd0cde88bcc04e617883a0383c9d8 @sminnee sminnee committed Oct 4, 2010
Showing with 139 additions and 2 deletions.
  1. +9 −0 filesystem/Folder.php
  2. +12 −2 filesystem/Upload.php
  3. +118 −0 tests/filesystem/UploadTest.php
View
@@ -250,6 +250,15 @@ function addUploadToFolder($tmpFile) {
$i++;
$oldFile = $file;
$file = "$origFile-$i";
+
+ if(strpos($file, '.') !== false) {
+ $file = ereg_replace('[0-9]*(\.[^.]+$)',$i . '\\1', $file);
+ } else if (strpos($file, '_') !== false) {
+ $file = ereg_replace('_([^_]+$)', '_'.$i, $file);
+ } else {
+ $file .= "_$i";
+ }
+
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file$ext with $i", E_USER_ERROR);
}
View
@@ -127,8 +127,12 @@ function load($tmpFile, $folderPath = false) {
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' ||
substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.bz2')) == '.tar.bz2') {
$relativeFilePath = ereg_replace('[0-9]*(\.tar\.[^.]+$)',$i . '\\1', $relativeFilePath);
- } else {
+ } else if (strpos($relativeFilePath, '.') !== false) {
$relativeFilePath = ereg_replace('[0-9]*(\.[^.]+$)',$i . '\\1', $relativeFilePath);
+ } else if (strpos($relativeFilePath, '_') !== false) {
+ $relativeFilePath = ereg_replace('_([^_]+$)', '_'.$i, $relativeFilePath);
+ } else {
+ $relativeFilePath .= "_$i";
}
if($oldFilePath == $relativeFilePath && $i > 2) user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
}
@@ -433,7 +437,13 @@ public function isValidSize() {
*/
public function isValidExtension() {
$pathInfo = pathinfo($this->tmpFile['name']);
- return (!count($this->allowedExtensions) || in_array(strtolower($pathInfo['extension']), $this->allowedExtensions));
+
+ // Special case for filenames with an extension
+ if(!isset($pathInfo['extension'])) {
+ return (in_array('', $this->allowedExtensions, true)) ? true : false;
+ } else {
+ return (!count($this->allowedExtensions) || in_array(strtolower($pathInfo['extension']), $this->allowedExtensions));
+ }
}
/**
@@ -127,6 +127,124 @@ function testUploadAcceptsAllowedExtension() {
$file->delete();
}
+ function testUploadDeniesNoExtensionFilesIfNoEmptyStringSetForValidatorExtensions() {
+ // create tmp file
+ $tmpFileName = 'UploadTest_testUpload';
+ $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
+ $tmpFileContent = '';
+ for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
+ file_put_contents($tmpFilePath, $tmpFileContent);
+
+ // emulates the $_FILES array
+ $tmpFile = array(
+ 'name' => $tmpFileName,
+ 'type' => 'text/plaintext',
+ 'size' => filesize($tmpFilePath),
+ 'tmp_name' => $tmpFilePath,
+ 'extension' => 'txt',
+ 'error' => UPLOAD_ERR_OK,
+ );
+
+ $v = new UploadTest_Validator();
+ $v->setAllowedExtensions(array('txt'));
+
+ // test upload into default folder
+ $u = new Upload();
+ $result = $u->load($tmpFile);
+
+ $this->assertFalse($result, 'Load failed because extension was not accepted');
+ $this->assertEquals(1, count($u->getErrors()), 'There is a single error of the file extension');
+
+ }
+
+ function testUploadTarGzFileTwiceAppendsNumber() {
+ // create tmp file
+ $tmpFileName = 'UploadTest_testUpload.tar.gz';
+ $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
+ $tmpFileContent = '';
+ for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
+ file_put_contents($tmpFilePath, $tmpFileContent);
+
+ // emulates the $_FILES array
+ $tmpFile = array(
+ 'name' => $tmpFileName,
+ 'type' => 'text/plaintext',
+ 'size' => filesize($tmpFilePath),
+ 'tmp_name' => $tmpFilePath,
+ 'extension' => 'txt',
+ 'error' => UPLOAD_ERR_OK,
+ );
+
+ // test upload into default folder
+ $u = new Upload();
+ $u->load($tmpFile);
+ $file = $u->getFile();
+ $this->assertEquals(
+ 'UploadTesttestUpload.tar.gz',
+ $file->Name,
+ 'File has a name without a number because it\'s not a duplicate'
+ );
+
+ $u = new Upload();
+ $u->load($tmpFile);
+ $file2 = $u->getFile();
+ $this->assertEquals(
+ 'UploadTesttestUpload2.tar.gz',
+ $file2->Name,
+ 'File receives a number attached to the end before the extension'
+ );
+
+ $file->delete();
+ $file2->delete();
+ }
+
+ function testUploadFileWithNoExtensionTwiceAppendsNumber() {
+ // create tmp file
+ $tmpFileName = 'UploadTest_testUpload';
+ $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
+ $tmpFileContent = '';
+ for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
+ file_put_contents($tmpFilePath, $tmpFileContent);
+
+ // emulates the $_FILES array
+ $tmpFile = array(
+ 'name' => $tmpFileName,
+ 'type' => 'text/plaintext',
+ 'size' => filesize($tmpFilePath),
+ 'tmp_name' => $tmpFilePath,
+ 'extension' => 'txt',
+ 'error' => UPLOAD_ERR_OK,
+ );
+
+ $v = new UploadTest_Validator();
+ $v->setAllowedExtensions(array(''));
+
+ // test upload into default folder
+ $u = new Upload();
+ $u->setValidator($v);
+ $u->load($tmpFile);
+ $file = $u->getFile();
+
+ $this->assertEquals(
+ 'UploadTesttestUpload',
+ $file->Name,
+ 'File is uploaded without extension'
+ );
+
+ $u = new Upload();
+ $u->setValidator($v);
+ $u->load($tmpFile);
+ $file2 = $u->getFile();
+ $this->assertEquals(
+ 'UploadTesttestUpload_2',
+ $file2->Name,
+ 'File receives a number attached to the end'
+ );
+
+ $file->delete();
+ $file2->delete();
+ }
+
}
class UploadTest_Validator extends Upload_Validator implements TestOnly {

0 comments on commit 2ea0e3e

Please sign in to comment.