openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit e78a239ee791dfb3978d70bdc1109cbf247c03e4
parent 6de748c2344395391f1d9c55c12743c7751037f4
Author: Jan Dankert <develop@jandankert.de>
Date:   Fri,  8 Jan 2021 22:01:28 +0100

New: Implementing cache directory tag standard (CACHEDIR.TAG) and using a cleaner logic to detect the directory for temporary files.

Diffstat:
Mmodules/cms/model/Value.class.php | 11-----------
Mmodules/util/FileUtils.class.php | 75+++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 35 insertions(+), 51 deletions(-)

diff --git a/modules/cms/model/Value.class.php b/modules/cms/model/Value.class.php @@ -722,17 +722,6 @@ SQL /** - * Ermittelt einen tempor�ren Dateinamen f�r diesen Inhalt. - */ - function tmpfile() - { - $filename = \util\FileUtils::getTempFileName( ); - return $filename; - } - - - - /** * Ermittelt den unbearbeiteten, "rohen" Inhalt. * * @return mixed Inhalt diff --git a/modules/util/FileUtils.class.php b/modules/util/FileUtils.class.php @@ -2,6 +2,7 @@ namespace util; use cms\base\Configuration; +use LogicException; use Pfad; use RuntimeException; @@ -12,10 +13,10 @@ use RuntimeException; class FileUtils { /** - * Fuegt einen Slash ("/") an das Ende an, sofern nicht bereits vorhanden. + * Adds a trailing slash ('/') if not existing. * - * @param String $pfad - * @return Pfad mit angeh�ngtem Slash. + * @param String $pfad path + * @return string path with trailing slash. */ public static function slashify($pfad) { @@ -27,52 +28,46 @@ class FileUtils /** - * Liefert einen Verzeichnisnamen fuer temporaere Dateien. + * Gets a directory for temporary files. */ - public static function createTempFile() + public static function getTempDir() { - $tmpdir = Configuration::subset('cache')->get('tmp_dir',''); - $tmpfile = @tempnam($tmpdir, 'openrat_tmp'); - - // 2. Versuch: Temp-Dir aus "upload_tmp_dir". - if ($tmpfile === FALSE) { - $tmpdir = ini_get('upload_tmp_dir'); - $tmpfile = @tempnam($tmpdir, 'openrat_tmp'); - } elseif ($tmpfile === FALSE) { - $tmpfile = @tempnam('', 'openrat_tmp'); + $cacheConfig = Configuration::subset('cache'); + + $tmpdirs = [ + $cacheConfig->get('tmp_dir',''), // temporary cache dir from configuration + sys_get_temp_dir(), + // do not try /var/cache here as it is mostly not writable + '/var/tmp', // FHS, survives restarts + '/tmp', // FHS + null + ]; + + foreach( $tmpdirs as $tmpdir ) { + if ( is_dir($tmpdir) && is_writable($tmpdir) ) + break; } - return $tmpfile; - } + if ( ! $tmpdir ) + throw new LogicException('Could not find a directory for temporary files. Hint: You may config this with cache/tmp_dir'); + $tmpdir = FileUtils::slashify($tmpdir).$cacheConfig->get('name','openrat-cache'); + $tmpdir = FileUtils::slashify($tmpdir); - /** - * Liefert einen Verzeichnisnamen fuer temporaere Dateien. - */ - public static function getTempDir() - { - $tmpfile = FileUtils::createTempFile(); + if ( ! is_dir($tmpdir ) ) { + mkdir( $tmpdir ); - $tmpdir = dirname($tmpfile); - @unlink($tmpfile); + file_put_contents($tmpdir.'CACHEDIR.TAG', <<<CACHETAG +Signature: 8a477f597d28d172789f06886806bc55 +# This file is a cache directory tag created by OpenRat CMS. +# For information about cache directory tags, see: +# http://www.brynosaurus.com/cachedir/ +CACHETAG + ); - return FileUtils::slashify($tmpdir); - } - - - /** - * @param array $attr - * @return string - * @deprecated use \Cache - */ - public static function getTempFileName($attr = array()) - { - $filename = FileUtils::getTempDir() . '/openrat'; - foreach ($attr as $a => $w) - $filename .= '_' . $a . $w; + } - $filename .= '.tmp'; - return $filename; + return $tmpdir; }