[BUGFIX] t3lib_div::unlink_tempfile does not always work on Windows
authorStanislas Rolland <typo3@sjbr.ca>
Mon, 15 Oct 2012 20:41:39 +0000 (16:41 -0400)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 22 Oct 2012 15:58:44 +0000 (17:58 +0200)
Problem: The filename created by t3lib_div::tempnam may contain
backslashes.

Solution: Process the file name through t3lib_div::fixWindowsFilePath

Change-Id: I47f8fe39ffb29894d78ac28141b88ea7abf6fd2c
Releases: 4.5, 4.6, 4.7, 6.0
Resolves: #38699
Reviewed-on: http://review.typo3.org/12684
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index da5dcf1..33da77e 100644 (file)
@@ -3642,9 +3642,12 @@ Connection: close
         * @see upload_to_tempfile(), tempnam()
         */
        static public function unlink_tempfile($uploadedTempFileName) {
-               if ($uploadedTempFileName && self::validPathStr($uploadedTempFileName) && self::isFirstPartOfStr($uploadedTempFileName, PATH_site . 'typo3temp/') && @is_file($uploadedTempFileName)) {
-                       if (unlink($uploadedTempFileName)) {
-                               return TRUE;
+               if ($uploadedTempFileName) {
+                       $uploadedTempFileName = self::fixWindowsFilePath($uploadedTempFileName);
+                       if (self::validPathStr($uploadedTempFileName) && self::isFirstPartOfStr($uploadedTempFileName, PATH_site . 'typo3temp/') && @is_file($uploadedTempFileName)) {
+                               if (unlink($uploadedTempFileName)) {
+                                       return TRUE;
+                               }
                        }
                }
        }
index d3d228b..a25a9f3 100644 (file)
@@ -1871,6 +1871,52 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertEquals('', \TYPO3\CMS\Core\Utility\GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
        }
 
+       ////////////////////////////////////////
+       // Tests concerning sanitizeLocalUrl
+       ////////////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function unlink_tempfileRemovesValidFileInTypo3temp() {
+               $fixtureFile = __DIR__ . '/Fixtures/clear.gif';
+               $testFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.gif';
+               @copy($fixtureFile, $testFilename);
+               \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($testFilename);
+               $fileExists = file_exists($testFilename);
+               @unlink($testFilename);
+               $this->assertFalse($fileExists);
+       }
+
+       /**
+        * @test
+        */
+       public function unlink_tempfileReturnsTrueIfFileWasRemoved() {
+               $fixtureFile = __DIR__ . '/Fixtures/clear.gif';
+               $testFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.gif';
+               @copy($fixtureFile, $testFilename);
+               $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($testFilename);
+               @unlink($testFilename);
+               $this->assertTrue($returnValue);
+       }
+
+       /**
+        * @test
+        */
+       public function unlink_tempfileReturnsNullIfFileDoesNotExist() {
+               $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile(PATH_site . 'typo3temp/' . uniqid('i_do_not_exist'));
+               $this->assertNull($returnValue);
+       }
+
+       /**
+        * @test
+        */
+       public function unlink_tempfileReturnsNullIfFileIsNowWithinTypo3temp() {
+               $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile('/tmp/typo3-unit-test-unlink_tempfile');
+               $this->assertNull($returnValue);
+       }
+
+
        //////////////////////////////////////
        // Tests concerning addSlashesOnArray
        //////////////////////////////////////