Skip to content

Commit e5f2f5c

Browse files
committed
[ticket/17361] Use filesystem classes for tempnam and temp dir. Code reviews.
PHPBB-17361
1 parent d01fa82 commit e5f2f5c

File tree

5 files changed

+78
-37
lines changed

5 files changed

+78
-37
lines changed

console/command/thumbnail/generate.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313

1414
namespace phpbb\console\command\thumbnail;
1515

16+
use phpbb\attachment\attachment_category;
1617
use phpbb\cache\service;
1718
use phpbb\db\driver\driver_interface;
19+
use phpbb\filesystem\temp;
1820
use phpbb\language\language;
1921
use phpbb\storage\storage;
2022
use phpbb\user;
2123
use Symfony\Component\Console\Command\Command as symfony_command;
2224
use Symfony\Component\Console\Input\InputInterface;
2325
use Symfony\Component\Console\Output\OutputInterface;
2426
use Symfony\Component\Console\Style\SymfonyStyle;
27+
use Symfony\Component\Filesystem\Filesystem as symfony_filesystem;
2528

2629
class generate extends \phpbb\console\command\command
2730
{
@@ -41,11 +44,21 @@ class generate extends \phpbb\console\command\command
4144
*/
4245
protected $language;
4346

47+
/**
48+
* @var symfony_filesystem
49+
*/
50+
protected $symfony_filesystem;
51+
4452
/**
4553
* @var storage
4654
*/
4755
protected $storage;
4856

57+
/**
58+
* @var temp
59+
*/
60+
protected $temp;
61+
4962
/**
5063
* phpBB root path
5164
* @var string
@@ -67,15 +80,18 @@ class generate extends \phpbb\console\command\command
6780
* @param service $cache The cache service
6881
* @param language $language Language
6982
* @param storage $storage Storage
83+
* @param temp $temp Temp
7084
* @param string $phpbb_root_path Root path
7185
* @param string $php_ext PHP extension
7286
*/
73-
public function __construct(user $user, driver_interface $db, service $cache, language $language, storage $storage, string $phpbb_root_path, string $php_ext)
87+
public function __construct(user $user, driver_interface $db, service $cache, language $language, storage $storage, temp $temp, string $phpbb_root_path, string $php_ext)
7488
{
7589
$this->db = $db;
7690
$this->cache = $cache;
91+
$this->symfony_filesystem = new symfony_filesystem();
7792
$this->language = $language;
7893
$this->storage = $storage;
94+
$this->temp = $temp;
7995
$this->phpbb_root_path = $phpbb_root_path;
8096
$this->php_ext = $php_ext;
8197

@@ -145,10 +161,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
145161
$thumbnail_created = array();
146162
while ($row = $this->db->sql_fetchrow($result))
147163
{
148-
if (isset($extensions[$row['extension']]['display_cat']) && $extensions[$row['extension']]['display_cat'] == \phpbb\attachment\attachment_category::IMAGE)
164+
if (isset($extensions[$row['extension']]['display_cat']) && $extensions[$row['extension']]['display_cat'] == attachment_category::IMAGE)
149165
{
150-
$source = tempnam(sys_get_temp_dir(), 'thumbnail_source');
151-
$destination = tempnam(sys_get_temp_dir(), 'thumbnail_destination');
166+
$source = $this->symfony_filesystem->tempnam($this->temp->get_dir(), 'thumbnail_source');
167+
$destination = $this->symfony_filesystem->tempnam($this->temp->get_dir(), 'thumbnail_destination');
152168

153169
file_put_contents($source, $this->storage->read($row['physical_filename']));
154170

console/command/thumbnail/recreate.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class recreate extends \phpbb\console\command\command
2929
/**
3030
* Constructor
3131
*
32+
* @param user $user User
3233
* @param language $language Language
3334
*/
3435
public function __construct(user $user, language $language)

db/migration/data/v400/storage_backup_data.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public function update_backup_data()
3636
$methods = ['sql', 'sql.gz', 'sql.bz2'];
3737

3838
$dir = $this->phpbb_root_path . 'store/';
39-
$dh = @opendir($dir);
39+
$handle = @opendir($dir);
4040

41-
if ($dh)
41+
if ($handle)
4242
{
43-
while (($file = readdir($dh)) !== false)
43+
while (($file = readdir($handle)) !== false)
4444
{
4545
if (preg_match('#^backup_(\d{10,})_(?:[a-z\d]{16}|[a-z\d]{32})\.(sql(?:\.(?:gz|bz2))?)$#i', $file, $matches))
4646
{
@@ -58,7 +58,7 @@ public function update_backup_data()
5858

5959
}
6060

61-
closedir($dh);
61+
closedir($handle);
6262
}
6363
}
6464
}

filesystem/temp.php

+39-16
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@
1313

1414
namespace phpbb\filesystem;
1515

16+
use phpbb\filesystem\exception\filesystem_exception;
17+
1618
class temp
1719
{
20+
/**
21+
* @var filesystem
22+
*/
23+
protected $filesystem;
24+
25+
/**
26+
* @var string
27+
*/
28+
protected $cache_temp_dir;
29+
1830
/**
1931
* @var string Temporary directory path
2032
*/
@@ -23,23 +35,10 @@ class temp
2335
/**
2436
* Constructor
2537
*/
26-
public function __construct($filesystem, $cache_temp_dir)
38+
public function __construct(filesystem $filesystem, string $cache_temp_dir)
2739
{
28-
$tmp_dir = (function_exists('sys_get_temp_dir')) ? sys_get_temp_dir() : '';
29-
30-
// Prevent trying to write to system temp dir in case of open_basedir
31-
// restrictions being in effect
32-
if (empty($tmp_dir) || !@file_exists($tmp_dir) || !@is_writable($tmp_dir))
33-
{
34-
$tmp_dir = $cache_temp_dir;
35-
36-
if (!is_dir($tmp_dir))
37-
{
38-
$filesystem->mkdir($tmp_dir, 0777);
39-
}
40-
}
41-
42-
$this->temp_dir = helper::realpath($tmp_dir);
40+
$this->filesystem = $filesystem;
41+
$this->cache_temp_dir = $cache_temp_dir;
4342
}
4443

4544
/**
@@ -49,6 +48,30 @@ public function __construct($filesystem, $cache_temp_dir)
4948
*/
5049
public function get_dir()
5150
{
51+
if ($this->temp_dir === null)
52+
{
53+
$tmp_dir = (function_exists('sys_get_temp_dir')) ? sys_get_temp_dir() : '';
54+
55+
// Prevent trying to write to system temp dir in case of open_basedir
56+
// restrictions being in effect
57+
if (empty($tmp_dir) || !@file_exists($tmp_dir) || !@is_writable($tmp_dir))
58+
{
59+
$tmp_dir = $this->cache_temp_dir;
60+
61+
if (!is_dir($tmp_dir))
62+
{
63+
$this->filesystem->mkdir($tmp_dir, 0777);
64+
}
65+
}
66+
67+
$this->temp_dir = helper::realpath($tmp_dir);
68+
69+
if ($this->temp_dir === false)
70+
{
71+
throw new filesystem_exception('FILESYSTEM_CANNOT_CREATE_DIRECTORY', $tmp_dir);
72+
}
73+
}
74+
5275
return $this->temp_dir;
5376
}
5477
}

storage/file_tracker.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
class file_tracker
2121
{
22-
2322
/**
2423
* @var db
2524
*/
@@ -59,11 +58,11 @@ public function __construct(db $db, cache $cache, string $storage_table)
5958
*/
6059
public function track_file(string $storage, string $path, int $size): void
6160
{
62-
$sql_ary = array(
61+
$sql_ary = [
6362
'file_path' => $path,
6463
'storage' => $storage,
6564
'filesize' => $size,
66-
);
65+
];
6766

6867
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
6968
$this->db->sql_query($sql);
@@ -80,10 +79,10 @@ public function track_file(string $storage, string $path, int $size): void
8079
*/
8180
public function untrack_file(string $storage, $path): void
8281
{
83-
$sql_ary = array(
82+
$sql_ary = [
8483
'file_path' => $path,
8584
'storage' => $storage,
86-
);
85+
];
8786

8887
$sql = 'DELETE FROM ' . $this->storage_table . '
8988
WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary);
@@ -103,13 +102,14 @@ public function untrack_file(string $storage, $path): void
103102
*/
104103
public function is_tracked(string $storage, string $path): bool
105104
{
106-
$sql_ary = array(
105+
$sql_ary = [
107106
'file_path' => $path,
108107
'storage' => $storage,
109-
);
108+
];
110109

111-
$sql = 'SELECT file_id FROM ' . $this->storage_table . '
112-
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
110+
$sql = 'SELECT file_id
111+
FROM ' . $this->storage_table . '
112+
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
113113
$result = $this->db->sql_query($sql);
114114
$row = $this->db->sql_fetchrow($result);
115115
$this->db->sql_freeresult($result);
@@ -128,13 +128,14 @@ public function is_tracked(string $storage, string $path): bool
128128
*/
129129
public function file_size(string $storage, string $path): int
130130
{
131-
$sql_ary = array(
131+
$sql_ary = [
132132
'file_path' => $path,
133133
'storage' => $storage,
134-
);
134+
];
135135

136-
$sql = 'SELECT filesize FROM ' . $this->storage_table . '
137-
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
136+
$sql = 'SELECT filesize
137+
FROM ' . $this->storage_table . '
138+
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
138139
$result = $this->db->sql_query($sql);
139140
$row = $this->db->sql_fetchrow($result);
140141
$this->db->sql_freeresult($result);

0 commit comments

Comments
 (0)