[BUGFIX] Add fileSuffix option to GeneralUtility::tempnam() 50/26650/8
authorFrans Saris <franssaris@gmail.com>
Sun, 5 Jan 2014 16:26:57 +0000 (17:26 +0100)
committerSteffen Ritter <info@rs-websystems.de>
Wed, 15 Jan 2014 15:53:01 +0000 (16:53 +0100)
Currently it is only possible to create a
temporary file with a given prefix and without
any file extension. But for some use-cases you
need a file with a certain file extension.

This patch introduces a new optional parameter to
GeneralUtility::tempnam() to make it possible to add
e.g. a specific extension to the created file.

In addition the occurrences in core, which can benefit
from this, are adapted resulting in less orphaned
temporary files in typo3temp.

Releases: 6.2
Resolves: #54750
Change-Id: I19383bc8f6b387133bff2f4033f05b28e883d427
Reviewed-on: https://review.typo3.org/26650
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
Reviewed-by: Markus Klein
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
typo3/sysext/backend/Classes/Sprite/SpriteGenerator.php
typo3/sysext/core/Classes/Resource/Driver/AbstractDriver.php
typo3/sysext/core/Classes/Resource/Processing/LocalPreviewHelper.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php

index 41a46b6..b324c36 100644 (file)
@@ -431,7 +431,7 @@ class SpriteGenerator {
         * @return void
         */
        protected function generateGraphic() {
-               $tempSprite = GeneralUtility::tempnam($this->spriteName);
+               $tempSprite = GeneralUtility::tempnam($this->spriteName, '.png');
                $filePath = PATH_site . $this->spriteFolder . $this->spriteName . '.png';
 
                // Create black true color image with given size
@@ -446,9 +446,9 @@ class SpriteGenerator {
                                imagecopy($newSprite, $currentIcon, $icon['left'], $icon['top'], 0, 0, $icon['width'], $icon['height']);
                        }
                }
-               imagepng($newSprite, $tempSprite . '.png');
-               GeneralUtility::upload_copy_move($tempSprite . '.png', $filePath);
-               GeneralUtility::unlink_tempfile($tempSprite . '.png');
+               imagepng($newSprite, $tempSprite);
+               GeneralUtility::upload_copy_move($tempSprite, $filePath);
+               GeneralUtility::unlink_tempfile($tempSprite);
        }
 
        /**
@@ -457,7 +457,7 @@ class SpriteGenerator {
         * @return void
         */
        protected function generateHighDensityGraphic() {
-               $tempSprite = GeneralUtility::tempnam($this->spriteName . '@x2');
+               $tempSprite = GeneralUtility::tempnam($this->spriteName . '@x2', '.png');
                $filePath = PATH_site . $this->spriteFolder . $this->spriteName . '@x2.png';
 
                // Create black true color image with given size
@@ -479,9 +479,9 @@ class SpriteGenerator {
                                }
                        }
                }
-               imagepng($newSprite, $tempSprite . '.png');
-               GeneralUtility::upload_copy_move($tempSprite . '.png', $filePath);
-               GeneralUtility::unlink_tempfile($tempSprite . '.png');
+               imagepng($newSprite, $tempSprite);
+               GeneralUtility::upload_copy_move($tempSprite, $filePath);
+               GeneralUtility::unlink_tempfile($tempSprite);
        }
        /**
         * Arranges icons in sprites,
index 173cec4..966b26a 100644 (file)
@@ -139,7 +139,7 @@ abstract class AbstractDriver implements DriverInterface {
         * @return string
         */
        protected function getTemporaryPathForFile($fileIdentifier) {
-               return \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('fal-tempfile-') . '.' . PathUtility::basename($fileIdentifier);
+               return \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('fal-tempfile-', '.' . PathUtility::pathinfo($fileIdentifier, PATHINFO_EXTENSION));
        }
 
        /**
index 80bea78..603e1c4 100644 (file)
@@ -67,8 +67,8 @@ class LocalPreviewHelper {
 
                $originalFileName = $sourceFile->getForLocalProcessing(FALSE);
 
-                       // Create the thumb filename in typo3temp/preview_....jpg
-               $temporaryFileName = Utility\GeneralUtility::tempnam('preview_') . '.' . $task->getTargetFileExtension();
+                       // Create a temporaryFile
+               $temporaryFileName = Utility\GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
                        // Check file extension
                if ($sourceFile->getType() != Resource\File::FILETYPE_IMAGE &&
                        !Utility\GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $sourceFile->getExtension())) {
index 9da156f..4de6a74 100644 (file)
@@ -3815,12 +3815,23 @@ Connection: close
         * This function should be used for getting temporary file names - will make your applications safe for open_basedir = on
         * REMEMBER to delete the temporary files after use! This is done by \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile()
         *
-        * @param string $filePrefix Prefix to temp file (which will have no extension btw)
+        * @param string $filePrefix Prefix for temporary file
+        * @param string $fileSuffix Suffix for temporary file, for example a special file extension
         * @return string result from PHP function tempnam() with PATH_site . 'typo3temp/' set for temp path.
         * @see unlink_tempfile(), upload_to_tempfile()
         */
-       static public function tempnam($filePrefix) {
-               return tempnam(PATH_site . 'typo3temp/', $filePrefix);
+       static public function tempnam($filePrefix, $fileSuffix = '') {
+               $temporaryPath = PATH_site . 'typo3temp/';
+               if ($fileSuffix === '') {
+                       $tempFileName = tempnam($temporaryPath, $filePrefix);
+               } else {
+                       do {
+                               $tempFileName = $temporaryPath . $filePrefix . mt_rand(1, PHP_INT_MAX) . $fileSuffix;
+                       } while (file_exists($tempFileName));
+                       touch($tempFileName);
+                       clearstatcache(NULL, $tempFileName);
+               }
+               return $tempFileName;
        }
 
        /**