Location: PHPKode > projects > Gnew > Gnew-2013.1/includes/template.php
<?php
// -----------------------------------------------------------------------------
// $Id: template.php 648 2013-03-22 14:32:47Z 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 template
{
    // ---------------------------
    private $files = array();
    private $keys = array();
    private $paths = array();
    private $root = '.';
    private $values = array();
    // ---------------------------

    public function __construct($root)
    {
        if (!is_dir($root))
        {
            $this->error(sprintf($GLOBALS['lang']['ERROR_TEMPLATE_ROOT'], $root));
            return false;
        }
        $this->root = $root;
        return true;
    }

    private function error($str)
    {
        printf($GLOBALS['lang']['ERROR_TEMPLATE'], $str);
        session_write_close();
        $GLOBALS['sql']->close();
        exit();
    }

    public function parse($name, $block = '', $loop = false, $related_tables = null)
    {
        if (array_key_exists($name, $this->files))
        {
            static $expiry = 0;
            static $output = '';
            static $path = '';
            static $tables = array();
            static $write = false;

            // CACHE variable
            if (preg_match('`{CACHE:([0-9]{1,5})(M|H|D)?}`', $this->files[$name], $matches))
            {
                if (array_key_exists(1, $matches))
                {
                    if (array_key_exists(2, $matches))
                    {
                        // Minutes
                        if ($matches[2] == 'M')
                        {
                            $expiry = $matches[1] * 60;
                        }
                        // Hours
                        if ($matches[2] == 'H')
                        {
                            $expiry = $matches[1] * 3600;
                        }
                        // Days
                        if ($matches[2] == 'D')
                        {
                            $expiry = $matches[1] * 86400;
                        }
                    }
                    //Second
                    else
                    {
                        $expiry = $matches[1];
                    }
                }
                $path = $this->paths[$name];
                if ($expiry > 0)
                {
                    $write = true;
                }
            }

            // Feeds variables (UTF-8 string: /u modifier)
            if (preg_match_all('`{((ARTICLES|COMMENTS|NEWS|POLLS|POSTS)_FEED(?::([\*\-\.o·•]))?(?::([0-9]{1,2}))?)}`u', $this->files[$name], $matches))
            {
                $num_matches = count($matches[1]);
                for ($i = 0; $i < $num_matches; $i++)
                {
                    if (array_key_exists($i, $matches[1]))
                    {
                        $this->keys[$matches[1][$i]] = '{' . $matches[1][$i] . '}';
                        if (!empty($matches[3][$i]))
                        {
                            if (!empty($matches[4][$i]))
                            {
                                $this->values[$matches[1][$i]] = parse_feed('./../feeds/' . strtolower($matches[2][$i]) . '.xml', utf8_entities($matches[3][$i]) . ' ', $matches[4][$i]);
                            }
                            else
                            {
                                $this->values[$matches[1][$i]] = parse_feed('./../feeds/' . strtolower($matches[2][$i]) . '.xml', utf8_entities($matches[3][$i]) . ' ');
                            }
                        }
                        elseif (!empty($matches[4][$i]))
                        {
                            if (!empty($matches[3][$i]))
                            {
                                $this->values[$matches[1][$i]] = parse_feed('./../feeds/' . strtolower($matches[2][$i]) . '.xml', utf8_entities($matches[3][$i]) . ' ', $matches[4][$i]);
                            }
                            else
                            {
                                $this->values[$matches[1][$i]] = parse_feed('./../feeds/' . strtolower($matches[2][$i]) . '.xml', '', $matches[4][$i]);
                            }
                        }
                        else
                        {
                            $this->values[$matches[1][$i]] = parse_feed('./../feeds/' . strtolower($matches[2][$i]) . '.xml');
                        }
                    }
                }
            }

            // Cache management: storing all tables related to the template file content
            if (is_array($related_tables))
            {
                $tables = array_merge($tables, $related_tables);
            }
            else
            {
                $tables = array_merge($tables, array($related_tables));
            }

            // Replacing variables
            $output .= str_replace($this->keys, $this->values, $this->files[$name]);

            if ($name == 'footer')
            {
                // Removing variables with no value
                $output = preg_replace('`{[^ \n\r\t}]+}`', '', $output);

                // Sending data to write into the cache file
                if ($write)
                {
                    $date_format = get_date_format();
                    $date_offset = get_date_offset();
                    $output = str_replace($this->values['CURRENT_TIME'], '', $output);
                    $output = str_replace($this->values['ONLINE_USERS'], '', $output);
                    $output = str_replace($this->values['PAGE_GENERATED'], sprintf($GLOBALS['lang']['CACHE_EXPIRY'], date($date_format, (time() + $date_offset + $expiry))), $output);
                    $GLOBALS['cache']->write_file($output, $expiry, $path, $tables);
                }

                // Displaying the page
                echo $output;

                // Unset variables
                unset($output);
                unset($path);
                $expiry = 0;
                $write = false;

                // Unset arrays
                unset($tables);
                unset($this->files);
                unset($this->keys);
                unset($this->paths);
                unset($this->values);
            }
        }
        elseif ($loop)
        {
            if (array_key_exists($name, $this->values))
            {
                $str = str_replace($this->keys, $this->values, $this->values[$name]);
                if (array_key_exists($block, $this->values))
                {
                    $this->set_var($block, $this->values[$block] . $str);
                }
                else
                {
                    $this->set_var($block, $str);
                }
            }
            else
            {
                $this->error(sprintf($GLOBALS['lang']['ERROR_PARSE_BLOCK'], $name));
                return false;
            }
        }
        else
        {
            $this->error(sprintf($GLOBALS['lang']['ERROR_PARSE_FILE'], $name));
            return false;
        }
        return true;
    }

    public function set_block($parent, $name, $block)
    {
        if (array_key_exists($parent, $this->files))
        {
            if (preg_match('`^[0-9a-z]+(_[0-9a-z]+)*$`i', $name))
            {
                $pattern = '`[ \t]*<!-- BEGIN ' . $name . ' -->\s*?\n?(\s*.*?\n?)\s*<!-- END ' . $name . ' -->\s*?\n?`s';
                if (preg_match_all($pattern, $this->files[$parent], $matches, PREG_SET_ORDER))
                {
                    $this->files[$parent] = preg_replace($pattern, '{' . $block . '}', $this->files[$parent]);
                    $this->set_var($name, $matches[0][1]);
                }
                else
                {
                    $this->error($GLOBALS['lang']['ERROR_SET_BLOCK']);
                    return false;
                }
            }
        }
        else
        {
            $this->error($GLOBALS['lang']['ERROR_OPEN_PARENT']);
            return false;
        }
        return true;
    }

    public function set_file($name, $file)
    {
        if (!empty($file))
        {
            $handle = rtrim($this->root, '/') . '/' . $file;
            if ($fp = @fopen($handle, 'rb'))
            {
                if (!empty($name))
                {
                    $this->files[$name] = fread($fp, filesize($handle));
                    $this->paths[$name] = $handle;
                    fclose($fp);
                }
                else
                {
                    $this->error($GLOBALS['lang']['ERROR_SET_FILE_NAME']);
                    return false;
                }
            }
            else
            {
                $this->error(sprintf($GLOBALS['lang']['ERROR_SET_FILE_OPEN'], $handle));
                return false;
            }
        }
        else
        {
            $this->error($GLOBALS['lang']['ERROR_SET_FILE']);
            return false;
        }
        return true;
    }

    public function set_var($name, $value = '')
    {
        if (!empty($name))
        {
            if (!is_array($name))
            {
                if (preg_match('`^[0-9a-z]+(_[0-9a-z]+)*$`i', $name))
                {
                    $this->keys[$name] = '{' . $name . '}';
                    $this->values[$name] = $value;
                }
            }
            else
            {
                reset($name);
                foreach ($name as $key => $value)
                {
                    if (preg_match('`^[0-9a-z]+(_[0-9a-z]+)*$`i', $key))
                    {
                        $this->keys[$key] = '{' . $key . '}';
                        $this->values[$key] = $value;
                    }
                }
            }
            return true;
        }
        else
        {
            $this->error($GLOBALS['lang']['ERROR_SET_VAR']);
            return false;
        }

        // Temporary form data (stored after an error)
        /*
        foreach ($_SESSION as $key => $value)
        {
            if ($key != 'user_id' && $key != 'user_captcha' && $key != 'check_version')
            {
                $old = strtoupper(trim($key, '_'));
                if (array_key_exists($old, $this->keys))
                {
                    $name = $old;
                }
                else
                {
                    $name = $key;
                }
                $this->keys[$name] = '{' . $name . '}';
                $this->values[$name] = $value;
            }
        }
        return true;
        */
    }
}

?>
Return current item: Gnew