Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
44 / 77
27.27% covered (danger)
27.27%
3 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
base
57.14% covered (warning)
57.14%
44 / 77
27.27% covered (danger)
27.27%
3 / 11
144.76
0.00% covered (danger)
0.00%
0 / 1
 purge
84.00% covered (warning)
84.00%
21 / 25
0.00% covered (danger)
0.00%
0 / 1
12.59
 unload
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 sql_load
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 sql_exists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 sql_fetchrow
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 sql_fetchfield
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 sql_rowseek
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 sql_freeresult
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 remove_file
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 remove_dir
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 _read
n/a
0 / 0
n/a
0 / 0
0
 clean_query_id
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
4.59
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\cache\driver;
15
16abstract class base implements \phpbb\cache\driver\driver_interface
17{
18    var $vars = array();
19    var $is_modified = false;
20
21    var $sql_rowset = array();
22    var $sql_row_pointer = array();
23    var $cache_dir = '';
24
25    /**
26    * {@inheritDoc}
27    */
28    function purge()
29    {
30        // Purge all phpbb cache files
31        try
32        {
33            $iterator = new \DirectoryIterator($this->cache_dir);
34        }
35        catch (\Exception $e)
36        {
37            return;
38        }
39
40        foreach ($iterator as $fileInfo)
41        {
42            if ($fileInfo->isDot())
43            {
44                continue;
45            }
46            $filename = $fileInfo->getFilename();
47            if ($fileInfo->isDir())
48            {
49                $this->remove_dir($fileInfo->getPathname());
50            }
51            else if (strpos($filename, 'container_') === 0 ||
52                strpos($filename, 'autoload_') === 0 ||
53                strpos($filename, 'url_matcher') === 0 ||
54                strpos($filename, 'url_generator') === 0 ||
55                strpos($filename, 'sql_') === 0 ||
56                strpos($filename, 'data_') === 0)
57            {
58                $this->remove_file($fileInfo->getPathname());
59            }
60        }
61
62        unset($this->vars);
63        unset($this->sql_rowset);
64        unset($this->sql_row_pointer);
65
66        if (function_exists('opcache_reset'))
67        {
68            @opcache_reset();
69        }
70
71        $this->vars = array();
72        $this->sql_rowset = array();
73        $this->sql_row_pointer = array();
74
75        $this->is_modified = false;
76    }
77
78    /**
79    * {@inheritDoc}
80    */
81    function unload()
82    {
83        $this->save();
84        unset($this->vars);
85        unset($this->sql_rowset);
86        unset($this->sql_row_pointer);
87
88        $this->vars = array();
89        $this->sql_rowset = array();
90        $this->sql_row_pointer = array();
91    }
92
93    /**
94    * {@inheritDoc}
95    */
96    function sql_load($query)
97    {
98        // Remove extra spaces and tabs
99        $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
100        $query_id = md5($query);
101
102        if (($result = $this->_read('sql_' . $query_id)) === false)
103        {
104            return false;
105        }
106
107        $this->sql_rowset[$query_id] = $result;
108        $this->sql_row_pointer[$query_id] = 0;
109
110        return $query_id;
111    }
112
113    /**
114    * {@inheritDoc}
115    */
116    function sql_exists($query_id)
117    {
118        $query_id = $this->clean_query_id($query_id);
119        return isset($this->sql_rowset[$query_id]);
120    }
121
122    /**
123    * {@inheritDoc}
124    */
125    function sql_fetchrow($query_id)
126    {
127        $query_id = $this->clean_query_id($query_id);
128        if ($this->sql_row_pointer[$query_id] < count($this->sql_rowset[$query_id]))
129        {
130            return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
131        }
132
133        return false;
134    }
135
136    /**
137    * {@inheritDoc}
138    */
139    function sql_fetchfield($query_id, $field)
140    {
141        $query_id = $this->clean_query_id($query_id);
142        if ($this->sql_row_pointer[$query_id] < count($this->sql_rowset[$query_id]))
143        {
144            return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++][$field] : false;
145        }
146
147        return false;
148    }
149
150    /**
151    * {@inheritDoc}
152    */
153    function sql_rowseek($rownum, $query_id)
154    {
155        $query_id = $this->clean_query_id($query_id);
156        if ($rownum >= count($this->sql_rowset[$query_id]))
157        {
158            return false;
159        }
160
161        $this->sql_row_pointer[$query_id] = $rownum;
162        return true;
163    }
164
165    /**
166    * {@inheritDoc}
167    */
168    function sql_freeresult($query_id)
169    {
170        $query_id = $this->clean_query_id($query_id);
171        if (!isset($this->sql_rowset[$query_id]))
172        {
173            return false;
174        }
175
176        unset($this->sql_rowset[$query_id]);
177        unset($this->sql_row_pointer[$query_id]);
178
179        return true;
180    }
181
182    /**
183    * Removes/unlinks file
184    *
185    * @param string $filename Filename to remove
186    * @param bool $check Check file permissions
187    * @return bool True if the file was successfully removed, otherwise false
188    */
189    function remove_file($filename, $check = false)
190    {
191        global $phpbb_filesystem;
192
193        if ($check && !$phpbb_filesystem->is_writable($this->cache_dir))
194        {
195            // E_USER_ERROR - not using language entry - intended.
196            trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
197        }
198
199        return @unlink($filename);
200    }
201
202    /**
203    * Remove directory
204    *
205    * @param string $dir Directory to remove
206    *
207    * @return void
208    */
209    protected function remove_dir($dir)
210    {
211        try
212        {
213            $iterator = new \DirectoryIterator($dir);
214        }
215        catch (\Exception $e)
216        {
217            return;
218        }
219
220        foreach ($iterator as $fileInfo)
221        {
222            if ($fileInfo->isDot())
223            {
224                continue;
225            }
226
227            if ($fileInfo->isDir())
228            {
229                $this->remove_dir($fileInfo->getPathname());
230            }
231            else
232            {
233                $this->remove_file($fileInfo->getPathname());
234            }
235        }
236
237        @rmdir($dir);
238    }
239
240    /**
241     * Fetch an item from the cache
242     *
243     * @param string $var Cache key
244     *
245     * @return mixed Cached data
246     */
247    abstract protected function _read(string $var);
248
249    /**
250     * {@inheritDoc}
251     */
252    public function clean_query_id($query_id)
253    {
254        // Some DBMS functions accept/return objects and/or resources instead of integer identifier
255        // Attempting to cast object to int will throw error, hence correctly handle all cases
256        if (is_resource($query_id))
257        {
258            return function_exists('get_resource_id') ? get_resource_id($query_id) : (int) $query_id;
259        }
260        else
261        {
262            return is_object($query_id) ? spl_object_id($query_id) : $query_id;
263        }
264    }
265}