<?php

include_once dirname(__FILE__).'/Smarty_Compiler.class.php';

Global $Lens_SmartyVars;
$Lens_SmartyVars = false;

/* 
	section not supported here - Code below based on Smarty 1.4.4 

	Code modified to generate GetUFO('tag'); when {$tag} encountered.
	Smarty assumes variables are standard alphanumeric names, but 
	ufos can have spaces in them, so we urldecode the ufos.
*/

class Smarty_Lens_Compiler extends Smarty_Compiler {
var $_vars = array();


	function _compile_file($tpl_file, $template_source, &$template_compiled)
	{
	Global $Lens_SmartyVars;
	
		$rez = parent::_compile_file($tpl_file,$template_source,&$template_compiled);
		$Lens_SmartyVars = $this->_vars;
		return $rez;
	}
	
	 function _parse_vars_props(&$tokens)
	 {
	 	$tokens[0] = $this->_parse_var(substr($tokens[0],1));
	 }
	
	function _parse_var($var_expr)
    {
		$v = addslashes($var_expr);
		//if (strpos($var_expr,';') !== false) 
			$output = "GetUFO('$v')";
		//else $output = "\$this->_tpl_vars['$v']";
		@$this->_vars[$var_expr] += 1;
		$modifiers = '';
        return $output;
    }
	
	function _compile_tag($template_tag)
    {
        /* Matched comment. */
        if ($template_tag{0} == '*' && $template_tag{strlen($template_tag)-1} == '*')
            return '';

        $qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';

        /* Split tag into two parts: command and the arguments. */
        preg_match('/^(
                       (?: ' . $qstr_regexp . ' | (?>[^"\'\s]+))+
                      )
                      (?:\s+(.*))?
                    /xs', $template_tag, $match);
        $tag_command = $match[1];
        $tag_args = isset($match[2]) ? $match[2] : ''; ## '
	

        /* If the tag name matches a variable or section property definition,
           we simply process it. */
       
	     if (preg_match('!^\$.*$!', $tag_command) ||   // if a variable
            preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)     ||  // or a configuration variable
            preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) {    // or a section property
            settype($tag_command, 'array');
            $tag_command = $this->_parse_var(substr($tag_command[0],1));
            return "<?php echo $tag_command; ?>\n";
        }

        switch ($tag_command) {
            case 'include':
                return $this->_compile_include_tag($tag_args);

            case 'if':
                return $this->_compile_if_tag($tag_args);

            case 'else':
                return '<?php else: ?>';

            case 'elseif':
                return $this->_compile_if_tag($tag_args, true);

            case '/if':
                return '<?php endif; ?>';

            case 'capture':
                return '<?php ob_start(); ?>';
                
            case '/capture':
                return '<?php $this->assign("return", ob_get_contents()); ob_end_clean(); ?>';
                
            case 'ldelim':
                return $this->left_delimiter;

            case 'rdelim':
                return $this->right_delimiter;

            case 'section':
                array_push($this->_sectionelse_stack, false);
                return $this->_compile_section_start($tag_args);

            case 'sectionelse':
                $this->_sectionelse_stack[count($this->_sectionelse_stack)-1] = true;
                return "<?php endfor; else: ?>";

            case '/section':
                if (array_pop($this->_sectionelse_stack))
                    return "<?php endif; ?>";
                else
                    return "<?php endfor; endif; ?>";

            case 'config_load':
                return $this->_compile_config_load_tag($tag_args);

            case 'strip':
            case '/strip':
                return $this->left_delimiter.$tag_command.$this->right_delimiter;

            case 'literal':
                list (,$literal_block) = each($this->_literal_blocks);
                $this->_current_line_no += substr_count($literal_block, "\n");
                return "<?php echo '".str_replace("'","\'",$literal_block)."'; ?>\n";

            case 'php':
                if ($this->security && !$this->security_settings['PHP_TAGS']) {
                    $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING);
                    return;
                }
                list (,$php_block) = each($this->_php_blocks);
                $this->_current_line_no += substr_count($php_block, "\n");
                return '<?php '.$php_block.' ?>';
                
            case 'insert':
                return $this->_compile_insert_tag($tag_args);

            default:
                if (isset($this->compiler_funcs[$tag_command])) {
                    return $this->_compile_compiler_tag($tag_command, $tag_args);
                } else if (isset($this->custom_funcs[$tag_command])) {
                    return $this->_compile_custom_tag($tag_command, $tag_args);
                } else {
                    $this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING);
                    return;
                }
        }
    }
};

?>