1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

[ticket/17361] Use filesystem classes for tempnam and temp dir. Code reviews.

PHPBB-17361
This commit is contained in:
Ruben Calvo
2024-12-24 19:30:05 +01:00
parent d6953a0422
commit b33f17e7ce
7 changed files with 86 additions and 38 deletions

View File

@@ -13,8 +13,20 @@
namespace phpbb\filesystem;
use phpbb\filesystem\exception\filesystem_exception;
class temp
{
/**
* @var filesystem
*/
protected $filesystem;
/**
* @var string
*/
protected $cache_temp_dir;
/**
* @var string Temporary directory path
*/
@@ -23,23 +35,10 @@ class temp
/**
* Constructor
*/
public function __construct($filesystem, $cache_temp_dir)
public function __construct(filesystem $filesystem, string $cache_temp_dir)
{
$tmp_dir = (function_exists('sys_get_temp_dir')) ? sys_get_temp_dir() : '';
// Prevent trying to write to system temp dir in case of open_basedir
// restrictions being in effect
if (empty($tmp_dir) || !@file_exists($tmp_dir) || !@is_writable($tmp_dir))
{
$tmp_dir = $cache_temp_dir;
if (!is_dir($tmp_dir))
{
$filesystem->mkdir($tmp_dir, 0777);
}
}
$this->temp_dir = helper::realpath($tmp_dir);
$this->filesystem = $filesystem;
$this->cache_temp_dir = $cache_temp_dir;
}
/**
@@ -49,6 +48,30 @@ class temp
*/
public function get_dir()
{
if ($this->temp_dir === null)
{
$tmp_dir = (function_exists('sys_get_temp_dir')) ? sys_get_temp_dir() : '';
// Prevent trying to write to system temp dir in case of open_basedir
// restrictions being in effect
if (empty($tmp_dir) || !@file_exists($tmp_dir) || !@is_writable($tmp_dir))
{
$tmp_dir = $this->cache_temp_dir;
if (!is_dir($tmp_dir))
{
$this->filesystem->mkdir($tmp_dir, 0777);
}
}
$this->temp_dir = helper::realpath($tmp_dir);
if ($this->temp_dir === false)
{
throw new filesystem_exception('FILESYSTEM_CANNOT_CREATE_DIRECTORY', $tmp_dir);
}
}
return $this->temp_dir;
}
}