Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
27.27% covered (danger)
27.27%
3 / 11
CRAP
68.75% covered (warning)
68.75%
55 / 80
base
0.00% covered (danger)
0.00%
0 / 1
27.27% covered (danger)
27.27%
3 / 11
78.78
68.75% covered (warning)
68.75%
55 / 80
 purge
0.00% covered (danger)
0.00%
0 / 1
12.22
88.46% covered (warning)
88.46%
23 / 26
 unload
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 8
 sql_load
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 sql_exists
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 sql_fetchrow
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 sql_fetchfield
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 4
 sql_rowseek
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 sql_freeresult
0.00% covered (danger)
0.00%
0 / 1
2.02
83.33% covered (warning)
83.33%
5 / 6
 remove_file
0.00% covered (danger)
0.00%
0 / 1
3.14
75.00% covered (warning)
75.00%
3 / 4
 remove_dir
0.00% covered (danger)
0.00%
0 / 1
5.15
81.82% covered (warning)
81.82%
9 / 11
 clean_query_id
0.00% covered (danger)
0.00%
0 / 1
4.59
66.67% covered (warning)
66.67%
2 / 3
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\cache\driver;
abstract class base implements \phpbb\cache\driver\driver_interface
{
    var $vars = array();
    var $is_modified = false;
    var $sql_rowset = array();
    var $sql_row_pointer = array();
    var $cache_dir = '';
    /**
    * {@inheritDoc}
    */
    function purge()
    {
        // Purge all phpbb cache files
        try
        {
            $iterator = new \DirectoryIterator($this->cache_dir);
        }
        catch (\Exception $e)
        {
            return;
        }
        foreach ($iterator as $fileInfo)
        {
            if ($fileInfo->isDot())
            {
                continue;
            }
            $filename = $fileInfo->getFilename();
            if ($fileInfo->isDir())
            {
                $this->remove_dir($fileInfo->getPathname());
            }
            else if (strpos($filename, 'container_') === 0 ||
                strpos($filename, 'autoload_') === 0 ||
                strpos($filename, 'url_matcher') === 0 ||
                strpos($filename, 'url_generator') === 0 ||
                strpos($filename, 'sql_') === 0 ||
                strpos($filename, 'data_') === 0)
            {
                $this->remove_file($fileInfo->getPathname());
            }
        }
        unset($this->vars);
        unset($this->sql_rowset);
        unset($this->sql_row_pointer);
        if (function_exists('opcache_reset'))
        {
            @opcache_reset();
        }
        $this->vars = array();
        $this->sql_rowset = array();
        $this->sql_row_pointer = array();
        $this->is_modified = false;
    }
    /**
    * {@inheritDoc}
    */
    function unload()
    {
        $this->save();
        unset($this->vars);
        unset($this->sql_rowset);
        unset($this->sql_row_pointer);
        $this->vars = array();
        $this->sql_rowset = array();
        $this->sql_row_pointer = array();
    }
    /**
    * {@inheritDoc}
    */
    function sql_load($query)
    {
        // Remove extra spaces and tabs
        $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
        $query_id = md5($query);
        if (($result = $this->_read('sql_' . $query_id)) === false)
        {
            return false;
        }
        $this->sql_rowset[$query_id] = $result;
        $this->sql_row_pointer[$query_id] = 0;
        return $query_id;
    }
    /**
    * {@inheritDoc}
    */
    function sql_exists($query_id)
    {
        $query_id = $this->clean_query_id($query_id);
        return isset($this->sql_rowset[$query_id]);
    }
    /**
    * {@inheritDoc}
    */
    function sql_fetchrow($query_id)
    {
        $query_id = $this->clean_query_id($query_id);
        if ($this->sql_row_pointer[$query_id] < count($this->sql_rowset[$query_id]))
        {
            return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
        }
        return false;
    }
    /**
    * {@inheritDoc}
    */
    function sql_fetchfield($query_id, $field)
    {
        $query_id = $this->clean_query_id($query_id);
        if ($this->sql_row_pointer[$query_id] < count($this->sql_rowset[$query_id]))
        {
            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;
        }
        return false;
    }
    /**
    * {@inheritDoc}
    */
    function sql_rowseek($rownum, $query_id)
    {
        $query_id = $this->clean_query_id($query_id);
        if ($rownum >= count($this->sql_rowset[$query_id]))
        {
            return false;
        }
        $this->sql_row_pointer[$query_id] = $rownum;
        return true;
    }
    /**
    * {@inheritDoc}
    */
    function sql_freeresult($query_id)
    {
        $query_id = $this->clean_query_id($query_id);
        if (!isset($this->sql_rowset[$query_id]))
        {
            return false;
        }
        unset($this->sql_rowset[$query_id]);
        unset($this->sql_row_pointer[$query_id]);
        return true;
    }
    /**
    * Removes/unlinks file
    *
    * @param string $filename Filename to remove
    * @param bool $check Check file permissions
    * @return bool True if the file was successfully removed, otherwise false
    */
    function remove_file($filename, $check = false)
    {
        global $phpbb_filesystem;
        if ($check && !$phpbb_filesystem->is_writable($this->cache_dir))
        {
            // E_USER_ERROR - not using language entry - intended.
            trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
        }
        return @unlink($filename);
    }
    /**
    * Remove directory
    *
    * @param string $dir Directory to remove
    *
    * @return null
    */
    protected function remove_dir($dir)
    {
        try
        {
            $iterator = new \DirectoryIterator($dir);
        }
        catch (\Exception $e)
        {
            return;
        }
        foreach ($iterator as $fileInfo)
        {
            if ($fileInfo->isDot())
            {
                continue;
            }
            if ($fileInfo->isDir())
            {
                $this->remove_dir($fileInfo->getPathname());
            }
            else
            {
                $this->remove_file($fileInfo->getPathname());
            }
        }
        @rmdir($dir);
    }
    /**
     * {@inheritDoc}
     */
    public function clean_query_id($query_id)
    {
        // Some DBMS functions accept/return objects and/or resources instead of integer identifier
        // Attempting to cast object to int will throw error, hence correctly handle all cases
        if (is_resource($query_id))
        {
            return function_exists('get_resource_id') ? get_resource_id($query_id) : (int) $query_id;
        }
        else
        {
            return is_object($query_id) ? spl_object_id($query_id) : $query_id;
        }
    }
}