Location: PHPKode > projects > Gnew > Gnew-2013.1/includes/cache.php
<?php
// -----------------------------------------------------------------------------
// $Id: cache.php 663 2013-03-23 06:06:52Z raoul $
//
// Copyright (C) 2013 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
// -----------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

class cache
{
    private $db = './../db/cache.db';
    private $dir = './../cache';
    private $ext = '.cache';

    public function __construct()
    {
        $cache_files = $this->read_db();
        $file = $this->locate();

        // Is the page cached?
        if (file_exists($file) && (is_array($cache_files) && array_key_exists($file, $cache_files)))
        {
            // Cache file still valid?
            if ($cache_files[$file]['expiry'] >= time())
            {
                // Modifications in the template file?
                if (filemtime($file) < filemtime($cache_files[$file]['template']))
                {
                    $this->clean($file);
                }
                else
                {
                    readfile($file);
                    $this->close();
                }
            }
            else
            {
                $this->clean($file);
            }
        }

        return true;
    }

    public function clean($file)
    {
        $cache_files = $this->read_db();

        // Purge cache files
        if ($file == 'purge')
        {
            if ($handle = opendir($this->dir))
            {
                while (($file = readdir($handle)) !== false)
                {
                    if (preg_match('`' . $this->ext . '$`', $file))
                    {
                        unlink($this->dir . '/' . $file);
                    }
                }
                closedir($handle);
            }
            $this->write_db();
        }

        // Clean expired or modified cache file
        if (file_exists($file) && preg_match('`' . $this->ext . '$`', $file))
        {
            // Remove the cache file from the server
            unlink($file);

            // Other cache files in the database?
            if (is_array($cache_files) && count($cache_files) > 1)
            {
                foreach ($cache_files as $key => $value)
                {
                    if ($key == $file)
                    {
                        // Remove the cache file from the database
                        unset($cache_files[$key]);
                    }
                }
                $str = serialize($cache_files);
            }
            else
            {
                $str = '';
            }
            $this->write_db($str);
        }

        // Clean not up-to-date cache file
        if (preg_match('`^(' . TABLE_ANSWERS . '|' . TABLE_ARTICLES . '|' . TABLE_CATEGORIES . '|' . TABLE_COMMENTS . '|' . TABLE_EMOTICONS . '|' . TABLE_NEWS . '|' . TABLE_POSTS . '|' . TABLE_QUESTIONS . '|' . TABLE_SESSIONS . '|' . TABLE_SETTINGS . '|' . TABLE_USERS . '|' . TABLE_VOTES . ')$`', $file))
        {
            if (is_array($cache_files))
            {
                foreach ($cache_files as $key => $value)
                {
                    if (is_array($value['tables']) && in_array($file, $value['tables']))
                    {
                        unlink($key);
                        unset($cache_files[$key]);
                    }
                }

                // Is there still cache files in the database?
                if (count($cache_files) > 0)
                {
                    $str = serialize($cache_files);
                }
                else
                {
                    $str = '';
                }
                $this->write_db($str);
            }
        }

        return true;
    }

    private function close()
    {
        session_write_close();
        $GLOBALS['sql']->close();
        exit();
    }

    private function locate()
    {
        // Files are stored by user ID
        if ($_SESSION['user_id'] > 0)
        {
            $str = '/' . $_SESSION['user_id'];
        }
        else
        {
            $str = '';
        }

        // then by template and language
        $str .= '/' . get_template() . '/' . get_language();

        // and finally by URI
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $str .= '/POST' . $_SERVER['REQUEST_URI'];
        }
        else
        {
            $str .= $_SERVER['REQUEST_URI'];
        }

        $file = $this->dir . '/' . sha1($str) . $this->ext;
        return $file;
    }

    private function read_db()
    {
        $database = @file_get_contents($this->db);
        if ($database !== false)
        {
            return unserialize($database);
        }
        else
        {
            printf($GLOBALS['lang']['ERROR_CACHE_FILE_OPEN'], $this->db);
            $this->close();
        }
    }

    private function write_db($str = '')
    {
        $fp = fopen($this->db, 'wb');
        flock($fp, LOCK_EX);
        fwrite($fp, $str, strlen($str));
        flock($fp, LOCK_UN);
        fclose($fp);

        return true;
    }

    public function write_file($data, $expiry, $template, $tables)
    {
        $cache_files = $this->read_db();
        $file = $this->locate();

        // Write cache file on the server
        $fp = fopen($file, 'wb');
        flock($fp, LOCK_EX);
        fwrite($fp, $data, strlen($data));
        flock($fp, LOCK_UN);
        fclose($fp);

        // Add cache file to the database
        $cache_files[$file]['expiry'] = time() + $expiry;
        $cache_files[$file]['template'] = $template;
        $cache_files[$file]['tables'] = $tables;
        $str = serialize($cache_files);
        $this->write_db($str);

        return true;
    }
}

?>
Return current item: Gnew