<?php
/**
 * DirectSmarty
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @author     HATA,Shinya
 * @copyright  2006 Cybozu Labs, Inc.
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version    1.0 2006/06/08
 */

if (! defined('SMARTY_VERSION')) {
    define('SMARTY_VERSION', '2.6.14');
}
require_once 'Smarty-'.SMARTY_VERSION.'/libs/Smarty.class.php';

function var_template($name, &$source, &$smarty)
{
    $source = $name;
    return TRUE;
}

function var_timestamp($name, &$timestamp, &$smarty)
{
    $timestamp = filemtime($_SERVER['SCRIPT_FILENAME']);
    return TRUE;
}

function var_secure($name, &$smarty)
{
    return TRUE;
}

function var_trusted($name, &$smarty)
{
}

class DirectSmarty extends Smarty
{
    function DirectSmarty()
    {
        parent::Smarty();
        $this->register_resource(
            'var', array('var_template', 'var_timestamp', 'var_secure', 'var_trusted'));
    }

    function displayDirect()
    {
        $template = file_get_contents($_SERVER['SCRIPT_FILENAME']);
        $tpl = '';
        $offset = 0;
        do {
            $start = strpos($template, '<?', $offset);
            if ($start === FALSE) break;
            $tpl .= substr($template, $offset, $start);
            $end = strpos($template, '?>', $start);
            if ($end === FALSE) break;
            $offset = $end + 2;
        } while (TRUE);

        if ($end !== FALSE) {
            $tpl .= substr($template, $offset);
        }

        $this->display('var:'.$tpl);
        exit();
    }
}
?>