Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
8 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
temp
66.67% covered (warning)
66.67%
8 / 12
50.00% covered (danger)
50.00%
1 / 2
12.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_dir
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
12.10
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\filesystem;
15
16use phpbb\filesystem\exception\filesystem_exception;
17
18class temp
19{
20    /**
21     * @var filesystem
22     */
23    protected $filesystem;
24
25    /**
26     * @var string
27     */
28    protected $cache_temp_dir;
29
30    /**
31    * @var string    Temporary directory path
32    */
33    protected $temp_dir;
34
35    /**
36     * Constructor
37     */
38    public function __construct(filesystem $filesystem, string $cache_temp_dir)
39    {
40        $this->filesystem = $filesystem;
41        $this->cache_temp_dir = $cache_temp_dir;
42    }
43
44    /**
45     * Get a temporary directory to write files
46     *
47     * @return string    returns the directory
48     */
49    public function get_dir()
50    {
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
75        return $this->temp_dir;
76    }
77}