Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
12 / 21
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
local
57.14% covered (warning)
57.14%
12 / 21
33.33% covered (danger)
33.33%
2 / 6
23.34
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
 configure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 read
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 write
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
3.47
 delete
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
3.19
 free_space
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
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\storage\adapter;
15
16use phpbb\storage\exception\storage_exception;
17use phpbb\filesystem\exception\filesystem_exception;
18use phpbb\filesystem\filesystem;
19use phpbb\filesystem\helper as filesystem_helper;
20
21/**
22 * Experimental
23 */
24class local implements adapter_interface
25{
26    /**
27     * Filesystem component
28     *
29     * @var filesystem
30     */
31    protected $filesystem;
32
33    /**
34     * @var string path
35     */
36    protected $phpbb_root_path;
37
38    /**
39     * Absolute path to the storage folder
40     * Always finish with DIRECTORY_SEPARATOR
41     * Example:
42     * - /var/www/phpBB/images/avatar/upload/
43     * - C:\phpBB\images\avatars\upload\
44     *
45     * @var string path
46     */
47    protected $root_path;
48
49    /**
50     * Constructor
51     *
52     * @param filesystem $filesystem
53     * @param string $phpbb_root_path
54     */
55    public function __construct(filesystem $filesystem, string $phpbb_root_path)
56    {
57        $this->filesystem = $filesystem;
58        $this->phpbb_root_path = $phpbb_root_path;
59    }
60
61    /**
62     * {@inheritdoc}
63     *
64     *
65     */
66    public function configure(array $options): void
67    {
68        $this->root_path = filesystem_helper::realpath($this->phpbb_root_path . $options['path']) . DIRECTORY_SEPARATOR;
69    }
70
71    /**
72     * {@inheritdoc}
73     */
74    public function read(string $path)
75    {
76        $stream = @fopen($this->root_path . $path, 'rb');
77
78        if (!$stream)
79        {
80            throw new storage_exception('STORAGE_CANNOT_OPEN_FILE', $path);
81        }
82
83        return $stream;
84    }
85
86    /**
87     * {@inheritdoc}
88     */
89    public function write(string $path, $resource): int
90    {
91        $stream = @fopen($this->root_path . $path, 'w+b');
92
93        if (!$stream)
94        {
95            throw new storage_exception('STORAGE_CANNOT_CREATE_FILE', $path);
96        }
97
98        if (($size = stream_copy_to_stream($resource, $stream)) === false)
99        {
100            fclose($stream);
101            throw new storage_exception('STORAGE_CANNOT_COPY_RESOURCE');
102        }
103
104        fclose($stream);
105
106        return $size;
107    }
108
109    /**
110     * {@inheritdoc}
111     */
112    public function delete(string $path): void
113    {
114        try
115        {
116            $this->filesystem->remove($this->root_path . $path);
117        }
118        catch (filesystem_exception $e)
119        {
120            throw new storage_exception('STORAGE_CANNOT_DELETE', $path, array(), $e);
121        }
122    }
123
124    /**
125     * {@inheritdoc}
126     */
127    public function free_space(): float
128    {
129        if (!function_exists('disk_free_space') || ($free_space = @disk_free_space($this->root_path)) === false)
130        {
131            throw new storage_exception('STORAGE_CANNOT_GET_FREE_SPACE');
132        }
133
134        return $free_space;
135    }
136
137}