Source for file Compiler.php

Documentation is available at Compiler.php

  1. <?php
  2.  
  3. include dirname(__FILE__'/Compilation/Exception.php';
  4.  
  5. /**
  6.  * default dwoo compiler class, compiles dwoo templates into php
  7.  *
  8.  * This software is provided 'as-is', without any express or implied warranty.
  9.  * In no event will the authors be held liable for any damages arising from the use of this software.
  10.  *
  11.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  12.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  13.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  14.  * @link       http://dwoo.org/
  15.  * @version    1.1.0
  16.  * @date       2009-07-18
  17.  * @package    Dwoo
  18.  */
  19. class Dwoo_Compiler implements Dwoo_ICompiler
  20. {
  21.     /**
  22.      * constant that represents a php opening tag
  23.      *
  24.      * use it in case it needs to be adjusted
  25.      *
  26.      * @var string 
  27.      */
  28.     const PHP_OPEN "<?php ";
  29.  
  30.     /**
  31.      * constant that represents a php closing tag
  32.      *
  33.      * use it in case it needs to be adjusted
  34.      *
  35.      * @var string 
  36.      */
  37.     const PHP_CLOSE "?>";
  38.  
  39.     /**
  40.      * boolean flag to enable or disable debugging output
  41.      *
  42.      * @var bool 
  43.      */
  44.     public $debug = false;
  45.  
  46.     /**
  47.      * left script delimiter
  48.      *
  49.      * @var string 
  50.      */
  51.     protected $ld = '{';
  52.  
  53.     /**
  54.      * left script delimiter with escaped regex meta characters
  55.      *
  56.      * @var string 
  57.      */
  58.     protected $ldr = '\\{';
  59.  
  60.     /**
  61.      * right script delimiter
  62.      *
  63.      * @var string 
  64.      */
  65.     protected $rd = '}';
  66.  
  67.     /**
  68.      * right script delimiter with escaped regex meta characters
  69.      *
  70.      * @var string 
  71.      */
  72.     protected $rdr = '\\}';
  73.  
  74.     /**
  75.      * defines whether the nested comments should be parsed as nested or not
  76.      *
  77.      * defaults to false (classic block comment parsing as in all languages)
  78.      *
  79.      * @var bool 
  80.      */
  81.     protected $allowNestedComments = false;
  82.  
  83.     /**
  84.      * defines whether opening and closing tags can contain spaces before valid data or not
  85.      *
  86.      * turn to true if you want to be sloppy with the syntax, but when set to false it allows
  87.      * to skip javascript and css tags as long as they are in the form "{ something", which is
  88.      * nice. default is false.
  89.      *
  90.      * @var bool 
  91.      */
  92.     protected $allowLooseOpenings = false;
  93.  
  94.     /**
  95.      * defines whether the compiler will automatically html-escape variables or not
  96.      *
  97.      * default is false
  98.      *
  99.      * @var bool 
  100.      */
  101.     protected $autoEscape = false;
  102.  
  103.     /**
  104.      * security policy object
  105.      *
  106.      * @var Dwoo_Security_Policy 
  107.      */
  108.     protected $securityPolicy;
  109.  
  110.     /**
  111.      * stores the custom plugins registered with this compiler
  112.      *
  113.      * @var array 
  114.      */
  115.     protected $customPlugins = array();
  116.  
  117.     /**
  118.      * stores the template plugins registered with this compiler
  119.      *
  120.      * @var array 
  121.      */
  122.     protected $templatePlugins = array();
  123.  
  124.     /**
  125.      * stores the pre- and post-processors callbacks
  126.      *
  127.      * @var array 
  128.      */
  129.     protected $processors = array('pre'=>array()'post'=>array());
  130.  
  131.     /**
  132.      * stores a list of plugins that are used in the currently compiled
  133.      * template, and that are not compilable. these plugins will be loaded
  134.      * during the template's runtime if required.
  135.      *
  136.      * it is a 1D array formatted as key:pluginName value:pluginType
  137.      *
  138.      * @var array 
  139.      */
  140.     protected $usedPlugins;
  141.  
  142.     /**
  143.      * stores the template undergoing compilation
  144.      *
  145.      * @var string 
  146.      */
  147.     protected $template;
  148.  
  149.     /**
  150.      * stores the current pointer position inside the template
  151.      *
  152.      * @var int 
  153.      */
  154.     protected $pointer;
  155.  
  156.     /**
  157.      * stores the current line count inside the template for debugging purposes
  158.      *
  159.      * @var int 
  160.      */
  161.     protected $line;
  162.  
  163.     /**
  164.      * stores the current template source while compiling it
  165.      *
  166.      * @var string 
  167.      */
  168.     protected $templateSource;
  169.  
  170.     /**
  171.      * stores the data within which the scope moves
  172.      *
  173.      * @var array 
  174.      */
  175.     protected $data;
  176.  
  177.     /**
  178.      * variable scope of the compiler, set to null if
  179.      * it can not be resolved to a static string (i.e. if some
  180.      * plugin defines a new scope based on a variable array key)
  181.      *
  182.      * @var mixed 
  183.      */
  184.     protected $scope;
  185.  
  186.     /**
  187.      * variable scope tree, that allows to rebuild the current
  188.      * scope if required, i.e. when going to a parent level
  189.      *
  190.      * @var array 
  191.      */
  192.     protected $scopeTree;
  193.  
  194.     /**
  195.      * block plugins stack, accessible through some methods
  196.      *
  197.      * @see findBlock
  198.      * @see getCurrentBlock
  199.      * @see addBlock
  200.      * @see addCustomBlock
  201.      * @see injectBlock
  202.      * @see removeBlock
  203.      * @see removeTopBlock
  204.      *
  205.      * @var array 
  206.      */
  207.     protected $stack = array();
  208.  
  209.     /**
  210.      * current block at the top of the block plugins stack,
  211.      * accessible through getCurrentBlock
  212.      *
  213.      * @see getCurrentBlock
  214.      *
  215.      * @var Dwoo_Block_Plugin 
  216.      */
  217.     protected $curBlock;
  218.  
  219.     /**
  220.      * current dwoo object that uses this compiler, or null
  221.      *
  222.      * @var Dwoo 
  223.      */
  224.     protected $dwoo;
  225.  
  226.     /**
  227.      * holds an instance of this class, used by getInstance when you don't
  228.      * provide a custom compiler in order to save resources
  229.      *
  230.      * @var Dwoo_Compiler 
  231.      */
  232.     protected static $instance;
  233.  
  234.     /**
  235.      * constructor
  236.      *
  237.      * saves the created instance so that child templates get the same one
  238.      */
  239.     public function __construct()
  240.     {
  241.         self::$instance $this;
  242.     }
  243.  
  244.     /**
  245.      * sets the delimiters to use in the templates
  246.      *
  247.      * delimiters can be multi-character strings but should not be one of those as they will
  248.      * make it very hard to work with templates or might even break the compiler entirely : "\", "$", "|", ":" and finally "#" only if you intend to use config-vars with the #var# syntax.
  249.      *
  250.      * @param string $left left delimiter
  251.      * @param string $right right delimiter
  252.      */
  253.     public function setDelimiters($left$right)
  254.     {
  255.         $this->ld = $left;
  256.         $this->rd = $right;
  257.         $this->ldr = preg_quote($left'/');
  258.         $this->rdr = preg_quote($right'/');
  259.     }
  260.  
  261.     /**
  262.      * returns the left and right template delimiters
  263.      *
  264.      * @return array containing the left and the right delimiters
  265.      */
  266.     public function getDelimiters()
  267.     {
  268.         return array($this->ld$this->rd);
  269.     }
  270.  
  271.     /**
  272.      * sets the way to handle nested comments, if set to true
  273.      * {* foo {* some other *} comment *} will be stripped correctly.
  274.      *
  275.      * if false it will remove {* foo {* some other *} and leave "comment *}" alone,
  276.      * this is the default behavior
  277.      *
  278.      * @param bool $allow allow nested comments or not, defaults to true (but the default internal value is false)
  279.      */
  280.     public function setNestedCommentsHandling($allow true{
  281.         $this->allowNestedComments = (bool) $allow;
  282.     }
  283.  
  284.     /**
  285.      * returns the nested comments handling setting
  286.      *
  287.      * @see setNestedCommentsHandling
  288.      * @return bool true if nested comments are allowed
  289.      */
  290.     public function getNestedCommentsHandling({
  291.         return $this->allowNestedComments;
  292.     }
  293.  
  294.     /**
  295.      * sets the tag openings handling strictness, if set to true, template tags can
  296.      * contain spaces before the first function/string/variable such as { $foo} is valid.
  297.      *
  298.      * if set to false (default setting), { $foo} is invalid but that is however a good thing
  299.      * as it allows css (i.e. #foo { color:red; }) to be parsed silently without triggering
  300.      * an error, same goes for javascript.
  301.      *
  302.      * @param bool $allow true to allow loose handling, false to restore default setting
  303.      */
  304.     public function setLooseOpeningHandling($allow false)
  305.     {
  306.         $this->allowLooseOpenings = (bool) $allow;
  307.     }
  308.  
  309.     /**
  310.      * returns the tag openings handling strictness setting
  311.      *
  312.      * @see setLooseOpeningHandling
  313.      * @return bool true if loose tags are allowed
  314.      */
  315.     public function getLooseOpeningHandling()
  316.     {
  317.         return $this->allowLooseOpenings;
  318.     }
  319.  
  320.     /**
  321.      * changes the auto escape setting
  322.      *
  323.      * if enabled, the compiler will automatically html-escape variables,
  324.      * unless they are passed through the safe function such as {$var|safe}
  325.      * or {safe $var}
  326.      *
  327.      * default setting is disabled/false
  328.      *
  329.      * @param bool $enabled set to true to enable, false to disable
  330.      */
  331.     public function setAutoEscape($enabled)
  332.     {
  333.         $this->autoEscape = (bool) $enabled;
  334.     }
  335.  
  336.     /**
  337.      * returns the auto escape setting
  338.      *
  339.      * default setting is disabled/false
  340.      *
  341.      * @return bool 
  342.      */
  343.     public function getAutoEscape()
  344.     {
  345.         return $this->autoEscape;
  346.     }
  347.  
  348.     /**
  349.      * adds a preprocessor to the compiler, it will be called
  350.      * before the template is compiled
  351.      *
  352.      * @param mixed $callback either a valid callback to the preprocessor or a simple name if the autoload is set to true
  353.      * @param bool $autoload if set to true, the preprocessor is auto-loaded from one of the plugin directories, else you must provide a valid callback
  354.      */
  355.     public function addPreProcessor($callback$autoload false)
  356.     {
  357.         if ($autoload{
  358.             $name str_replace('Dwoo_Processor_'''$callback);
  359.             $class 'Dwoo_Processor_'.$name;
  360.  
  361.             if (class_exists($classfalse)) {
  362.                 $callback array(new $class($this)'process');
  363.             elseif (function_exists($class)) {
  364.                 $callback $class;
  365.             else {
  366.                 $callback array('autoload'=>true'class'=>$class'name'=>$name);
  367.             }
  368.  
  369.             $this->processors['pre'][$callback;
  370.         else {
  371.             $this->processors['pre'][$callback;
  372.         }
  373.     }
  374.  
  375.     /**
  376.      * removes a preprocessor from the compiler
  377.      *
  378.      * @param mixed $callback either a valid callback to the preprocessor or a simple name if it was autoloaded
  379.      */
  380.     public function removePreProcessor($callback)
  381.     {
  382.         if (($index array_search($callback$this->processors['pre']true)) !== false{
  383.             unset($this->processors['pre'][$index]);
  384.         elseif (($index array_search('Dwoo_Processor_'.str_replace('Dwoo_Processor_'''$callback)$this->processors['pre']true)) !== false{
  385.             unset($this->processors['pre'][$index]);
  386.         else {
  387.             $class 'Dwoo_Processor_' str_replace('Dwoo_Processor_'''$callback);
  388.             foreach ($this->processors['pre'as $index=>$proc{
  389.                 if (is_array($proc&& ($proc[0instanceof $class|| (isset($proc['class']&& $proc['class'== $class)) {
  390.                     unset($this->processors['pre'][$index]);
  391.                     break;
  392.                 }
  393.             }
  394.         }
  395.     }
  396.  
  397.     /**
  398.      * adds a postprocessor to the compiler, it will be called
  399.      * before the template is compiled
  400.      *
  401.      * @param mixed $callback either a valid callback to the postprocessor or a simple name if the autoload is set to true
  402.      * @param bool $autoload if set to true, the postprocessor is auto-loaded from one of the plugin directories, else you must provide a valid callback
  403.      */
  404.     public function addPostProcessor($callback$autoload false)
  405.     {
  406.         if ($autoload{
  407.             $name str_replace('Dwoo_Processor_'''$callback);
  408.             $class 'Dwoo_Processor_'.$name;
  409.  
  410.             if (class_exists($classfalse)) {
  411.                 $callback array(new $class($this)'process');
  412.             elseif (function_exists($class)) {
  413.                 $callback $class;
  414.             else {
  415.                 $callback array('autoload'=>true'class'=>$class'name'=>$name);
  416.             }
  417.  
  418.             $this->processors['post'][$callback;
  419.         else {
  420.             $this->processors['post'][$callback;
  421.         }
  422.     }
  423.  
  424.     /**
  425.      * removes a postprocessor from the compiler
  426.      *
  427.      * @param mixed $callback either a valid callback to the postprocessor or a simple name if it was autoloaded
  428.      */
  429.     public function removePostProcessor($callback)
  430.     {
  431.         if (($index array_search($callback$this->processors['post']true)) !== false{
  432.             unset($this->processors['post'][$index]);
  433.         elseif (($index array_search('Dwoo_Processor_'.str_replace('Dwoo_Processor_'''$callback)$this->processors['post']true)) !== false{
  434.             unset($this->processors['post'][$index]);
  435.         else    {
  436.             $class 'Dwoo_Processor_' str_replace('Dwoo_Processor_'''$callback);
  437.             foreach ($this->processors['post'as $index=>$proc{
  438.                 if (is_array($proc&& ($proc[0instanceof $class|| (isset($proc['class']&& $proc['class'== $class)) {
  439.                     unset($this->processors['post'][$index]);
  440.                     break;
  441.                 }
  442.             }
  443.         }
  444.     }
  445.  
  446.     /**
  447.      * internal function to autoload processors at runtime if required
  448.      *
  449.      * @param string $class the class/function name
  450.      * @param string $name the plugin name (without Dwoo_Plugin_ prefix)
  451.      */
  452.     protected function loadProcessor($class$name)
  453.     {
  454.         if (!class_exists($classfalse&& !function_exists($class)) {
  455.             try {
  456.                 $this->dwoo->getLoader()->loadPlugin($name);
  457.             catch (Dwoo_Exception $e{
  458.                 throw new Dwoo_Exception('Processor '.$name.' could not be found in your plugin directories, please ensure it is in a file named '.$name.'.php in the plugin directory');
  459.             }
  460.         }
  461.  
  462.         if (class_exists($classfalse)) {
  463.             return array(new $class($this)'process');
  464.         }
  465.  
  466.         if (function_exists($class)) {
  467.             return $class;
  468.         }
  469.  
  470.         throw new Dwoo_Exception('Wrong processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Processor_name"');
  471.     }
  472.  
  473.     /**
  474.      * adds an used plugin, this is reserved for use by the {template} plugin
  475.      *
  476.      * this is required so that plugin loading bubbles up from loaded
  477.      * template files to the current one
  478.      *
  479.      * @private
  480.      * @param string $name function name
  481.      * @param int $type plugin type (Dwoo::*_PLUGIN)
  482.      */
  483.     public function addUsedPlugin($name$type)
  484.     {
  485.         $this->usedPlugins[$name$type;
  486.     }
  487.  
  488.     /**
  489.      * returns all the plugins this template uses
  490.      *
  491.      * @private
  492.      * @return array the list of used plugins in the parsed template
  493.      */
  494.     public function getUsedPlugins()
  495.     {
  496.         return $this->usedPlugins;
  497.     }
  498.  
  499.     /**
  500.      * adds a template plugin, this is reserved for use by the {template} plugin
  501.      *
  502.      * this is required because the template functions are not declared yet
  503.      * during compilation, so we must have a way of validating their argument
  504.      * signature without using the reflection api
  505.      *
  506.      * @private
  507.      * @param string $name function name
  508.      * @param array $params parameter array to help validate the function call
  509.      * @param string $uuid unique id of the function
  510.      * @param string $body function php code
  511.      */
  512.     public function addTemplatePlugin($namearray $params$uuid$body null)
  513.     {
  514.         $this->templatePlugins[$namearray('params'=> $params'body' => $body'uuid' => $uuid);
  515.     }
  516.  
  517.     /**
  518. /**
  519.      * returns all the parsed sub-templates
  520.      *
  521.      * @private
  522.      * @return array the parsed sub-templates
  523.      */
  524.     public function getTemplatePlugins()
  525.     {
  526.         return $this->templatePlugins;
  527.     }
  528.  
  529.     /**
  530.      * marks a template plugin as being called, which means its source must be included in the compiled template
  531.      *
  532.      * @param string $name function name
  533.      */
  534.     public function useTemplatePlugin($name)
  535.     {
  536.         $this->templatePlugins[$name]['called'true;
  537.     }
  538.  
  539.     /**
  540.      * adds the custom plugins loaded into Dwoo to the compiler so it can load them
  541.      *
  542.      * @see Dwoo::addPlugin
  543.      * @param array $customPlugins an array of custom plugins
  544.      */
  545.     public function setCustomPlugins(array $customPlugins)
  546.     {
  547.         $this->customPlugins $customPlugins;
  548.     }
  549.  
  550.     /**
  551. /**
  552.      * sets the security policy object to enforce some php security settings
  553.      *
  554.      * use this if untrusted persons can modify templates,
  555.      * set it on the Dwoo object as it will be passed onto the compiler automatically
  556.      *
  557.      * @param Dwoo_Security_Policy $policy the security policy object
  558.      */
  559.     public function setSecurityPolicy(Dwoo_Security_Policy $policy null)
  560.     {
  561.         $this->securityPolicy = $policy;
  562.     }
  563.  
  564.     /**
  565.      * returns the current security policy object or null by default
  566.      *
  567.      * @return Dwoo_Security_Policy|nullthe security policy object if any
  568.      */
  569.     public function getSecurityPolicy()
  570.     {
  571.         return $this->securityPolicy;
  572.     }
  573.  
  574.     /**
  575.      * sets the pointer position
  576.      *
  577.      * @param int $position the new pointer position
  578.      * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position
  579.      */
  580.     public function setPointer($position$isOffset false)
  581.     {
  582.         if ($isOffset{
  583.             $this->pointer += $position;
  584.         else {
  585.             $this->pointer = $position;
  586.         }
  587.     }
  588.  
  589.     /**
  590.      * returns the current pointer position, only available during compilation of a template
  591.      *
  592.      * @return int 
  593.      */
  594.     public function getPointer()
  595.     {
  596.         return $this->pointer;
  597.     }
  598.  
  599.     /**
  600.      * sets the line number
  601.      *
  602.      * @param int $number the new line number
  603.      * @param bool $isOffset if set to true, the position acts as an offset and not an absolute position
  604.      */
  605.     public function setLine($number$isOffset false)
  606.     {
  607.         if ($isOffset{
  608.             $this->line += $number;
  609.         else {
  610.             $this->line = $number;
  611.         }
  612.     }
  613.  
  614.     /**
  615.      * returns the current line number, only available during compilation of a template
  616.      *
  617.      * @return int 
  618.      */
  619.     public function getLine()
  620.     {
  621.         return $this->line;
  622.     }
  623.  
  624.     /**
  625.      * returns the dwoo object that initiated this template compilation, only available during compilation of a template
  626.      *
  627.      * @return Dwoo 
  628.      */
  629.     public function getDwoo()
  630.     {
  631.         return $this->dwoo;
  632.     }
  633.  
  634.     /**
  635.      * overwrites the template that is being compiled
  636.      *
  637.      * @param string $newSource the template source that must replace the current one
  638.      * @param bool $fromPointer if set to true, only the source from the current pointer position is replaced
  639.      * @return string the template or partial template
  640.      */
  641.     public function setTemplateSource($newSource$fromPointer false)
  642.     {
  643.         if ($fromPointer === true{
  644.             $this->templateSource = substr($this->templateSource0$this->pointer$newSource;
  645.         else {
  646.             $this->templateSource = $newSource;
  647.         }
  648.     }
  649.  
  650.     /**
  651.      * returns the template that is being compiled
  652.      *
  653.      * @param mixed $fromPointer if set to true, only the source from the current pointer
  654.      *                                position is returned, if a number is given it overrides the current pointer
  655.      * @return string the template or partial template
  656.      */
  657.     public function getTemplateSource($fromPointer false)
  658.     {
  659.         if ($fromPointer === true{
  660.             return substr($this->templateSource$this->pointer);
  661.         elseif (is_numeric($fromPointer)) {
  662.             return substr($this->templateSource$fromPointer);
  663.         else {
  664.             return $this->templateSource;
  665.         }
  666.     }
  667.  
  668.     /**
  669.      * resets the compilation pointer, effectively restarting the compilation process
  670.      *
  671.      * this is useful if a plugin modifies the template source since it might need to be recompiled
  672.      */
  673.     public function recompile()
  674.     {
  675.         $this->setPointer(0);
  676.     }
  677.  
  678.     /**
  679.      * compiles the provided string down to php code
  680.      *
  681.      * @param string $tpl the template to compile
  682.      * @return string a compiled php string
  683.      */
  684.     public function compile(Dwoo $dwooDwoo_ITemplate $template)
  685.     {
  686.         // init vars
  687.         $tpl $template->getSource();
  688.         $ptr 0;
  689.         $this->dwoo = $dwoo;
  690.         $this->template = $template;
  691.         $this->templateSource =$tpl;
  692.         $this->pointer =$ptr;
  693.  
  694.         while (true{
  695.             // if pointer is at the beginning, reset everything, that allows a plugin to externally reset the compiler if everything must be reparsed
  696.             if ($ptr===0{
  697.                 // resets variables
  698.                 $this->usedPlugins = array();
  699.                 $this->data = array();
  700.                 $this->scope =$this->data;
  701.                 $this->scopeTree = array();
  702.                 $this->stack = array();
  703.                 $this->line = 1;
  704.                 $this->templatePlugins = array();
  705.                 // add top level block
  706.                 $compiled $this->addBlock('topLevelBlock'array()0);
  707.                 $this->stack[0]['buffer''';
  708.  
  709.                 if ($this->debugecho 'COMPILER INIT<br />';
  710.  
  711.                 if ($this->debugecho 'PROCESSING PREPROCESSORS ('.count($this->processors['pre']).')<br>';
  712.  
  713.                 // runs preprocessors
  714.                 foreach ($this->processors['pre'as $preProc{
  715.                     if (is_array($preProc&& isset($preProc['autoload'])) {
  716.                         $preProc $this->loadProcessor($preProc['class']$preProc['name']);
  717.                     }
  718.                     if (is_array($preProc&& $preProc[0instanceof Dwoo_Processor{
  719.                         $tpl call_user_func($preProc$tpl);
  720.                     else {
  721.                         $tpl call_user_func($preProc$this$tpl);
  722.                     }
  723.                 }
  724.                 unset($preProc);
  725.  
  726.                 // show template source if debug
  727.                 if ($this->debugecho '<pre>'.print_r(htmlentities($tpl)true).'</pre><hr />';
  728.  
  729.                 // strips php tags if required by the security policy
  730.                 if ($this->securityPolicy !== null{
  731.                     $search array('{<\?php.*?\?>}');
  732.                     if (ini_get('short_open_tags')) {
  733.                         $search array('{<\?.*?\?>}''{<%.*?%>}');
  734.                     }
  735.                     switch($this->securityPolicy->getPhpHandling()) {
  736.  
  737.                     case Dwoo_Security_Policy::PHP_ALLOW:
  738.                         break;
  739.                     case Dwoo_Security_Policy::PHP_ENCODE:
  740.                         $tpl preg_replace_callback($searcharray($this'phpTagEncodingHelper')$tpl);
  741.                         break;
  742.                     case Dwoo_Security_Policy::PHP_REMOVE:
  743.                         $tpl preg_replace($search''$tpl);
  744.  
  745.                     }
  746.                 }
  747.             }
  748.  
  749.             $pos strpos($tpl$this->ld$ptr);
  750.  
  751.             if ($pos === false{
  752.                 $this->push(substr($tpl$ptr)0);
  753.                 break;
  754.             elseif (substr($tpl$pos-11=== '\\' && substr($tpl$pos-21!== '\\'{
  755.                 $this->push(substr($tpl$ptr$pos-$ptr-1$this->ld);
  756.                 $ptr $pos+strlen($this->ld);
  757.             elseif (preg_match('/^'.$this->ldr . ($this->allowLooseOpenings ? '\s*' '''literal' ($this->allowLooseOpenings ? '\s*' ''$this->rdr.'/s'substr($tpl$pos)$litOpen)) {
  758.                 if (!preg_match('/'.$this->ldr . ($this->allowLooseOpenings ? '\s*' '''\/literal' ($this->allowLooseOpenings ? '\s*' ''$this->rdr.'/s'$tpl$litClosePREG_OFFSET_CAPTURE$pos)) {
  759.                     throw new Dwoo_Compilation_Exception($this'The {literal} blocks must be closed explicitly with {/literal}');
  760.                 }
  761.                 $endpos $litClose[0][1];
  762.                 $this->push(substr($tpl$ptr$pos-$ptrsubstr($tpl$pos strlen($litOpen[0])$endpos-$pos-strlen($litOpen[0])));
  763.                 $ptr $endpos+strlen($litClose[0][0]);
  764.             else {
  765.                 if (substr($tpl$pos-21=== '\\' && substr($tpl$pos-11=== '\\'{
  766.                     $this->push(substr($tpl$ptr$pos-$ptr-1));
  767.                     $ptr $pos;
  768.                 }
  769.  
  770.                 $this->push(substr($tpl$ptr$pos-$ptr));
  771.                 $ptr $pos;
  772.  
  773.                 $pos += strlen($this->ld);
  774.                 if ($this->allowLooseOpenings{
  775.                     while (substr($tpl$pos1=== ' '{
  776.                         $pos+=1;
  777.                     }
  778.                 else {
  779.                     if (substr($tpl$pos1=== ' ' || substr($tpl$pos1=== "\r" || substr($tpl$pos1=== "\n" || substr($tpl$pos1=== "\t"{
  780.                         $ptr $pos;
  781.                         $this->push($this->ld);
  782.                         continue;
  783.                     }
  784.                 }
  785.  
  786.                 // check that there is an end tag present
  787.                 if (strpos($tpl$this->rd$pos=== false{
  788.                     throw new Dwoo_Compilation_Exception($this'A template tag was not closed, started with "'.substr($tpl$ptr30).'"');
  789.                 }
  790.  
  791.  
  792.                 $ptr += strlen($this->ld);
  793.                 $subptr $ptr;
  794.  
  795.                 while (true{
  796.                     $parsed $this->parse($tpl$subptrnullfalse'root'$subptr);
  797.  
  798.                     // reload loop if the compiler was reset
  799.                     if ($ptr === 0{
  800.                         continue 2;
  801.                     }
  802.  
  803.                     $len $subptr $ptr;
  804.                     $this->push($parsedsubstr_count(substr($tpl$ptr$len)"\n"));
  805.                     $ptr += $len;
  806.  
  807.                     if ($parsed === false{
  808.                         break;
  809.                     }
  810.                 }
  811.  
  812.                 // adds additional line breaks between php closing and opening tags because the php parser removes those if there is just a single line break
  813.                 if (substr($this->curBlock['buffer']-2=== '?>' && preg_match('{^(([\r\n])([\r\n]?))}'substr($tpl$ptr3)$m)) {
  814.                     if ($m[3=== ''{
  815.                         $ptr+=1;
  816.                         $this->push($m[1].$m[1]1);
  817.                     else {
  818.                         $ptr+=2;
  819.                         $this->push($m[1]."\n"2);
  820.                     }
  821.                 }
  822.             }
  823.         }
  824.  
  825.         $compiled .= $this->removeBlock('topLevelBlock');
  826.  
  827.         if ($this->debugecho 'PROCESSING POSTPROCESSORS<br>';
  828.  
  829.         foreach ($this->processors['post'as $postProc{
  830.             if (is_array($postProc&& isset($postProc['autoload'])) {
  831.                 $postProc $this->loadProcessor($postProc['class']$postProc['name']);
  832.             }
  833.             if (is_array($postProc&& $postProc[0instanceof Dwoo_Processor{
  834.                 $compiled call_user_func($postProc$compiled);
  835.             else {
  836.                 $compiled call_user_func($postProc$this$compiled);
  837.             }
  838.         }
  839.         unset($postProc);
  840.  
  841.         if ($this->debugecho 'COMPILATION COMPLETE : MEM USAGE : '.memory_get_usage().'<br>';
  842.  
  843.         $output "<?php\n/* template head */\n";
  844.  
  845.         // build plugin preloader
  846.         foreach ($this->usedPlugins as $plugin=>$type{
  847.             if ($type Dwoo::CUSTOM_PLUGIN{
  848.                 continue;
  849.             }
  850.  
  851.             switch($type{
  852.  
  853.             case Dwoo::BLOCK_PLUGIN:
  854.             case Dwoo::CLASS_PLUGIN:
  855.                 $output .= "if (class_exists('Dwoo_Plugin_$plugin', false)===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
  856.                 break;
  857.             case Dwoo::FUNC_PLUGIN:
  858.                 $output .= "if (function_exists('Dwoo_Plugin_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
  859.                 break;
  860.             case Dwoo::SMARTY_MODIFIER:
  861.                 $output .= "if (function_exists('smarty_modifier_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
  862.                 break;
  863.             case Dwoo::SMARTY_FUNCTION:
  864.                 $output .= "if (function_exists('smarty_function_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
  865.                 break;
  866.             case Dwoo::SMARTY_BLOCK:
  867.                 $output .= "if (function_exists('smarty_block_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
  868.                 break;
  869.             case Dwoo::PROXY_PLUGIN:
  870.                 $output .= $this->getDwoo()->getPluginProxy()->getPreloader($plugin);
  871.                 break;
  872.             default:
  873.                 throw new Dwoo_Compilation_Exception($this'Type error for '.$plugin.' with type'.$type);
  874.  
  875.             }
  876.         }
  877.  
  878.         foreach ($this->templatePlugins as $function => $attr{
  879.             if (isset($attr['called']&& $attr['called'=== true && !isset($attr['checked'])) {
  880.                 $this->resolveSubTemplateDependencies($function);
  881.             }
  882.         }
  883.         foreach ($this->templatePlugins as $function{
  884.             if (isset($function['called']&& $function['called'=== true{
  885.                 $output .= $function['body'].PHP_EOL;
  886.             }
  887.         }
  888.  
  889.         $output .= $compiled."\n?>";
  890.  
  891.         $output preg_replace('/(?<!;|\}|\*\/|\n|\{)(\s*'.preg_quote(self::PHP_CLOSE'/'preg_quote(self::PHP_OPEN'/').')/'";\n"$output);
  892.         $output str_replace(self::PHP_CLOSE self::PHP_OPEN"\n"$output);
  893.  
  894.         // handle <?xml tag at the beginning
  895.         $output preg_replace('#(/\* template body \*/ \?>\s*)<\?xml#is''$1<?php echo \'<?xml\'; ?>'$output);
  896.  
  897.         if ($this->debug{
  898.             echo '<hr><pre>';
  899.             $lines preg_split('{\r\n|\n|<br />}'highlight_string(($output)true));
  900.             array_shift($lines);
  901.             foreach ($lines as $i=>$line{
  902.                 echo ($i+1).'. '.$line."\r\n";
  903.             }
  904.         }
  905.         if ($this->debugecho '<hr></pre></pre>';
  906.  
  907.         $this->template = $this->dwoo = null;
  908.         $tpl null;
  909.  
  910.         return $output;
  911.     }
  912.  
  913.     /**
  914.      * checks what sub-templates are used in every sub-template so that we're sure they are all compiled
  915.      *
  916.      * @param string $function the sub-template name
  917.      */
  918.     protected function resolveSubTemplateDependencies($function)
  919.     {
  920.         $body $this->templatePlugins[$function]['body'];
  921.         foreach ($this->templatePlugins as $func => $attr{
  922.             if ($func !== $function && !isset($attr['called']&& strpos($body'Dwoo_Plugin_'.$func!== false{
  923.                 $this->templatePlugins[$func]['called'true;
  924.                 $this->resolveSubTemplateDependencies($func);
  925.             }
  926.         }
  927.         $this->templatePlugins[$function]['checked'true;
  928.     }
  929.  
  930.     /**
  931.      * adds compiled content to the current block
  932.      *
  933.      * @param string $content the content to push
  934.      * @param int $lineCount newlines count in content, optional
  935.      */
  936.     public function push($content$lineCount null)
  937.     {
  938.         if ($lineCount === null{
  939.             $lineCount substr_count($content"\n");
  940.         }
  941.  
  942.         if ($this->curBlock['buffer'=== null && count($this->stack1{
  943.             // buffer is not initialized yet (the block has just been created)
  944.             $this->stack[count($this->stack)-2]['buffer'.= (string) $content;
  945.             $this->curBlock['buffer''';
  946.         else {
  947.             if (!isset($this->curBlock['buffer'])) {
  948.                 throw new Dwoo_Compilation_Exception($this'The template has been closed too early, you probably have an extra block-closing tag somewhere');
  949.             }
  950.             // append current content to current block's buffer
  951.             $this->curBlock['buffer'.= (string) $content;
  952.         }
  953.         $this->line += $lineCount;
  954.     }
  955.  
  956.     /**
  957.      * sets the scope
  958.      *
  959.      * set to null if the scope becomes "unstable" (i.e. too variable or unknown) so that
  960.      * variables are compiled in a more evaluative way than just $this->scope['key']
  961.      *
  962.      * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2")
  963.      * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope
  964.      * @return array the current scope tree
  965.      */
  966.     public function setScope($scope$absolute false)
  967.     {
  968.         $old $this->scopeTree;
  969.  
  970.         if ($scope===null{
  971.             unset($this->scope);
  972.             $this->scope = null;
  973.         }
  974.  
  975.         if (is_array($scope)===false{
  976.             $scope explode('.'$scope);
  977.         }
  978.  
  979.         if ($absolute===true{
  980.             $this->scope =$this->data;
  981.             $this->scopeTree = array();
  982.         }
  983.  
  984.         while (($bit array_shift($scope)) !== null{
  985.             if ($bit === '_parent' || $bit === '_'{
  986.                 array_pop($this->scopeTree);
  987.                 reset($this->scopeTree);
  988.                 $this->scope =$this->data;
  989.                 $cnt count($this->scopeTree);
  990.                 for ($i=0;$i<$cnt;$i++)
  991.                     $this->scope =$this->scope[$this->scopeTree[$i]];
  992.             elseif ($bit === '_root' || $bit === '__'{
  993.                 $this->scope =$this->data;
  994.                 $this->scopeTree = array();
  995.             elseif (isset($this->scope[$bit])) {
  996.                 $this->scope =$this->scope[$bit];
  997.                 $this->scopeTree[$bit;
  998.             else {
  999.                 $this->scope[$bitarray();
  1000.                 $this->scope =$this->scope[$bit];
  1001.                 $this->scopeTree[$bit;
  1002.             }
  1003.         }
  1004.  
  1005.         return $old;
  1006.     }
  1007.  
  1008.     /**
  1009.      * adds a block to the top of the block stack
  1010.      *
  1011.      * @param string $type block type (name)
  1012.      * @param array $params the parameters array
  1013.      * @param int $paramtype the parameters type (see mapParams), 0, 1 or 2
  1014.      * @return string the preProcessing() method's output
  1015.      */
  1016.     public function addBlock($typearray $params$paramtype)
  1017.     {
  1018.         $class 'Dwoo_Plugin_'.$type;
  1019.         if (class_exists($classfalse=== false{
  1020.             $this->dwoo->getLoader()->loadPlugin($type);
  1021.         }
  1022.  
  1023.         $params $this->mapParams($paramsarray($class'init')$paramtype);
  1024.  
  1025.         $this->stack[array('type' => $type'params' => $params'custom' => false'class' => $class'buffer' => null);
  1026.         $this->curBlock =$this->stack[count($this->stack)-1];
  1027.         return call_user_func(array($class,'preProcessing')$this$params''''$type);
  1028.     }
  1029.  
  1030.     /**
  1031.      * adds a custom block to the top of the block stack
  1032.      *
  1033.      * @param string $type block type (name)
  1034.      * @param array $params the parameters array
  1035.      * @param int $paramtype the parameters type (see mapParams), 0, 1 or 2
  1036.      * @return string the preProcessing() method's output
  1037.      */
  1038.     public function addCustomBlock($typearray $params$paramtype)
  1039.     {
  1040.         $callback $this->customPlugins[$type]['callback'];
  1041.         if (is_array($callback)) {
  1042.             $class is_object($callback[0]get_class($callback[0]$callback[0];
  1043.         else {
  1044.             $class $callback;
  1045.         }
  1046.  
  1047.         $params $this->mapParams($paramsarray($class'init')$paramtype);
  1048.  
  1049.         $this->stack[array('type' => $type'params' => $params'custom' => true'class' => $class'buffer' => null);
  1050.         $this->curBlock =$this->stack[count($this->stack)-1];
  1051.         return call_user_func(array($class,'preProcessing')$this$params''''$type);
  1052.     }
  1053.  
  1054.     /**
  1055.      * injects a block at the top of the plugin stack without calling its preProcessing method
  1056.      *
  1057.      * used by {else} blocks to re-add themselves after having closed everything up to their parent
  1058.      *
  1059.      * @param string $type block type (name)
  1060.      * @param array $params parameters array
  1061.      */
  1062.     public function injectBlock($typearray $params)
  1063.     {
  1064.         $class 'Dwoo_Plugin_'.$type;
  1065.         if (class_exists($classfalse=== false{
  1066.             $this->dwoo->getLoader()->loadPlugin($type);
  1067.         }
  1068.         $this->stack[array('type' => $type'params' => $params'custom' => false'class' => $class'buffer' => null);
  1069.         $this->curBlock =$this->stack[count($this->stack)-1];
  1070.     }
  1071.  
  1072.     /**
  1073.      * removes the closest-to-top block of the given type and all other
  1074.      * blocks encountered while going down the block stack
  1075.      *
  1076.      * @param string $type block type (name)
  1077.      * @return string the output of all postProcessing() method's return values of the closed blocks
  1078.      */
  1079.     public function removeBlock($type)
  1080.     {
  1081.         $output '';
  1082.  
  1083.         $pluginType $this->getPluginType($type);
  1084.         if ($pluginType Dwoo::SMARTY_BLOCK{
  1085.             $type 'smartyinterface';
  1086.         }
  1087.         while (true{
  1088.             while ($top array_pop($this->stack)) {
  1089.                 if ($top['custom']{
  1090.                     $class $top['class'];
  1091.                 else {
  1092.                     $class 'Dwoo_Plugin_'.$top['type'];
  1093.                 }
  1094.                 if (count($this->stack)) {
  1095.                     $this->curBlock =$this->stack[count($this->stack)-1];
  1096.                     $this->push(call_user_func(array($class'postProcessing')$this$top['params']''''$top['buffer'])0);
  1097.                 else {
  1098.                     $null null;
  1099.                     $this->curBlock =$null;
  1100.                     $output call_user_func(array($class'postProcessing')$this$top['params']''''$top['buffer']);
  1101.                 }
  1102.  
  1103.                 if ($top['type'=== $type{
  1104.                     break 2;
  1105.                 }
  1106.             }
  1107.  
  1108.             throw new Dwoo_Compilation_Exception($this'Syntax malformation, a block of type "'.$type.'" was closed but was not opened');
  1109.             break;
  1110.         }
  1111.  
  1112.         return $output;
  1113.     }
  1114.  
  1115.     /**
  1116.      * returns a reference to the first block of the given type encountered and
  1117.      * optionally closes all blocks until it finds it
  1118.      *
  1119.      * this is mainly used by {else} plugins to close everything that was opened
  1120.      * between their parent and themselves
  1121.      *
  1122.      * @param string $type the block type (name)
  1123.      * @param bool $closeAlong whether to close all blocks encountered while going down the block stack or not
  1124.      * @return &array the array is as such: array('type'=>pluginName, 'params'=>parameter array,
  1125.      *                    'custom'=>bool defining whether it's a custom plugin or not, for internal use)
  1126.      */
  1127.     public function &findBlock($type$closeAlong false)
  1128.     {
  1129.         if ($closeAlong===true{
  1130.             while ($b end($this->stack)) {
  1131.                 if ($b['type']===$type{
  1132.                     return $this->stack[key($this->stack)];
  1133.                 }
  1134.                 $this->push($this->removeTopBlock()0);
  1135.             }
  1136.         else {
  1137.             end($this->stack);
  1138.             while ($b current($this->stack)) {
  1139.                 if ($b['type']===$type{
  1140.                     return $this->stack[key($this->stack)];
  1141.                 }
  1142.                 prev($this->stack);
  1143.             }
  1144.         }
  1145.  
  1146.         throw new Dwoo_Compilation_Exception($this'A parent block of type "'.$type.'" is required and can not be found');
  1147.     }
  1148.  
  1149.     /**
  1150.      * returns a reference to the current block array
  1151.      *
  1152.      * @return &array the array is as such: array('type'=>pluginName, 'params'=>parameter array,
  1153.      *                    'custom'=>bool defining whether it's a custom plugin or not, for internal use)
  1154.      */
  1155.     public function &getCurrentBlock()
  1156.     {
  1157.         return $this->curBlock;
  1158.     }
  1159.  
  1160.     /**
  1161.      * removes the block at the top of the stack and calls its postProcessing() method
  1162.      *
  1163.      * @return string the postProcessing() method's output
  1164.      */
  1165.     public function removeTopBlock()
  1166.     {
  1167.         $o array_pop($this->stack);
  1168.         if ($o === null{
  1169.             throw new Dwoo_Compilation_Exception($this'Syntax malformation, a block of unknown type was closed but was not opened.');
  1170.         }
  1171.         if ($o['custom']{
  1172.             $class $o['class'];
  1173.         else {
  1174.             $class 'Dwoo_Plugin_'.$o['type'];
  1175.         }
  1176.  
  1177.         $this->curBlock =$this->stack[count($this->stack)-1];
  1178.  
  1179.         return call_user_func(array($class'postProcessing')$this$o['params']''''$o['buffer']);
  1180.     }
  1181.  
  1182.     /**
  1183.      * returns the compiled parameters (for example a variable's compiled parameter will be "$this->scope['key']") out of the given parameter array
  1184.      *
  1185.      * @param array $params parameter array
  1186.      * @return array filtered parameters
  1187.      */
  1188.     public function getCompiledParams(array $params)
  1189.     {
  1190.         foreach ($params as $k=>$p{
  1191.             if (is_array($p)) {
  1192.                 $params[$k$p[0];
  1193.             }
  1194.         }
  1195.         return $params;
  1196.     }
  1197.  
  1198.     /**
  1199.      * returns the real parameters (for example a variable's real parameter will be its key, etc) out of the given parameter array
  1200.      *
  1201.      * @param array $params parameter array
  1202.      * @return array filtered parameters
  1203.      */
  1204.     public function getRealParams(array $params)
  1205.     {
  1206.         foreach ($params as $k=>$p{
  1207.             if (is_array($p)) {
  1208.                 $params[$k$p[1];
  1209.             }
  1210.         }
  1211.         return $params;
  1212.     }
  1213.  
  1214.     /**
  1215.      * entry point of the parser, it redirects calls to other parse* functions
  1216.      *
  1217.      * @param string $in the string within which we must parse something
  1218.      * @param int $from the starting offset of the parsed area
  1219.      * @param int $to the ending offset of the parsed area
  1220.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  1221.      * @param string $curBlock the current parser-block being processed
  1222.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  1223.      * @return string parsed values
  1224.      */
  1225.     protected function parse($in$from$to$parsingParams false$curBlock=''&$pointer null)
  1226.     {
  1227.         if ($to === null{
  1228.             $to strlen($in);
  1229.         }
  1230.         $first substr($in$from1);
  1231.  
  1232.         if ($first === false{
  1233.             throw new Dwoo_Compilation_Exception($this'Unexpected EOF, a template tag was not closed');
  1234.         }
  1235.  
  1236.         while ($first===" " || $first==="\n" || $first==="\t" || $first==="\r"{
  1237.             if ($curBlock === 'root' && substr($in$fromstrlen($this->rd)) === $this->rd{
  1238.                 // end template tag
  1239.                 $pointer += strlen($this->rd);
  1240.                 if ($this->debugecho 'TEMPLATE PARSING ENDED<br />';
  1241.                 return false;
  1242.             }
  1243.             $from++;
  1244.             if ($pointer !== null{
  1245.                 $pointer++;
  1246.             }
  1247.             if ($from >= $to{
  1248.                 if (is_array($parsingParams)) {
  1249.                     return $parsingParams;
  1250.                 else {
  1251.                     return '';
  1252.                 }
  1253.             }
  1254.             $first $in[$from];
  1255.         }
  1256.  
  1257.         $substr substr($in$from$to-$from);
  1258.  
  1259.         if ($this->debugecho '<br />PARSE CALL : PARSING "<b>'.htmlentities(substr($in$frommin($to-$from50))).(($to-$from50 '...':'').'</b>" @ '.$from.':'.$to.' in '.$curBlock.' : pointer='.$pointer.'<br/>';
  1260.         $parsed "";
  1261.  
  1262.         if ($curBlock === 'root' && $first === '*'{
  1263.             $src $this->getTemplateSource();
  1264.             $startpos $this->getPointer(strlen($this->ld);
  1265.             if (substr($src$startposstrlen($this->ld)) === $this->ld{
  1266.                 if ($startpos 0{
  1267.                     do {
  1268.                         $char substr($src--$startpos1);
  1269.                         if ($char == "\n"{
  1270.                             $startpos++;
  1271.                             $whitespaceStart true;
  1272.                             break;
  1273.                         }
  1274.                     while ($startpos && ($char == ' ' || $char == "\t"));
  1275.                 }
  1276.  
  1277.                 if (!isset($whitespaceStart)) {
  1278.                     $startpos $this->getPointer();
  1279.                 else {
  1280.                     $pointer -= $this->getPointer($startpos;
  1281.                 }
  1282.  
  1283.                 if ($this->allowNestedComments && strpos($src$this->ld.'*'$this->getPointer()) !== false{
  1284.                     $comOpen $this->ld.'*';
  1285.                     $comClose '*'.$this->rd;
  1286.                     $level 1;
  1287.                     $start $startpos;
  1288.                     $ptr $this->getPointer('*';
  1289.  
  1290.                     while ($level && $ptr strlen($src)) {
  1291.                         $open strpos($src$comOpen$ptr);
  1292.                         $close strpos($src$comClose$ptr);
  1293.  
  1294.                         if ($open !== false && $close !== false{
  1295.                             if ($open $close{
  1296.                                 $ptr $open strlen($comOpen);
  1297.                                 $level++;
  1298.                             else {
  1299.                                 $ptr $close strlen($comClose);
  1300.                                 $level--;
  1301.                             }
  1302.                         elseif ($open !== false{
  1303.                             $ptr $open strlen($comOpen);
  1304.                             $level++;
  1305.                         elseif ($close !== false{
  1306.                             $ptr $close strlen($comClose);
  1307.                             $level--;
  1308.                         else {
  1309.                             $ptr strlen($src);
  1310.                         }
  1311.                     }
  1312.                     $endpos $ptr strlen('*'.$this->rd);
  1313.                 else {
  1314.                     $endpos strpos($src'*'.$this->rd$startpos);
  1315.                     if ($endpos == false{
  1316.                         throw new Dwoo_Compilation_Exception($this'Un-ended comment');
  1317.                     }
  1318.                 }
  1319.                 $pointer += $endpos $startpos strlen('*'.$this->rd);
  1320.                 if (isset($whitespaceStart&& preg_match('#^[\t ]*\r?\n#'substr($src$endpos+strlen('*'.$this->rd))$m)) {
  1321.                     $pointer += strlen($m[0]);
  1322.                     $this->curBlock['buffer'substr($this->curBlock['buffer']0strlen($this->curBlock['buffer']($this->getPointer($startpos strlen($this->ld)));
  1323.                 }
  1324.                 return false;
  1325.             }
  1326.         }
  1327.  
  1328.         if ($first==='$'{
  1329.             // var
  1330.             $out $this->parseVar($in$from$to$parsingParams$curBlock$pointer);
  1331.             $parsed 'var';
  1332.         elseif ($first==='%' && preg_match('#^%[a-z]#i'$substr)) {
  1333.             // const
  1334.             $out $this->parseConst($in$from$to$parsingParams$curBlock$pointer);
  1335.         elseif ($first==='"' || $first==="'"{
  1336.             // string
  1337.             $out $this->parseString($in$from$to$parsingParams$curBlock$pointer);
  1338.         elseif (preg_match('/^[a-z][a-z0-9_]*(?:::[a-z][a-z0-9_]*)?('.(is_array($parsingParams)||$curBlock!='root'?'':'\s+[^(]|').'\s*\(|\s*'.$this->rdr.'|\s*;)/i'$substr)) {
  1339.             // func
  1340.             $out $this->parseFunction($in$from$to$parsingParams$curBlock$pointer);
  1341.             $parsed 'func';
  1342.         elseif ($first === ';'{
  1343.             // instruction end
  1344.             if ($this->debugecho 'END OF INSTRUCTION<br />';
  1345.             if ($pointer !== null{
  1346.                 $pointer++;
  1347.             }
  1348.             return $this->parse($in$from+1$tofalse'root'$pointer);
  1349.         elseif ($curBlock === 'root' && preg_match('#^/([a-z][a-z0-9_]*)?#i'$substr$match)) {
  1350.             // close block
  1351.             if (!empty($match[1]&& $match[1== 'else'{
  1352.                 throw new Dwoo_Compilation_Exception($this'Else blocks must not be closed explicitly, they are automatically closed when their parent block is closed');
  1353.             }
  1354.             if (!empty($match[1]&& $match[1== 'elseif'{
  1355.                 throw new Dwoo_Compilation_Exception($this'Elseif blocks must not be closed explicitly, they are automatically closed when their parent block is closed or a new else/elseif block is declared after them');
  1356.             }
  1357.             if ($pointer !== null{
  1358.                 $pointer += strlen($match[0]);
  1359.             }
  1360.             if (empty($match[1])) {
  1361.                 if ($this->curBlock['type'== 'else' || $this->curBlock['type'== 'elseif'{
  1362.                     $pointer -= strlen($match[0]);
  1363.                 }
  1364.                 if ($this->debugecho 'TOP BLOCK CLOSED<br />';
  1365.                 return $this->removeTopBlock();
  1366.             else {
  1367.                 if ($this->debugecho 'BLOCK OF TYPE '.$match[1].' CLOSED<br />';
  1368.                 return $this->removeBlock($match[1]);
  1369.             }
  1370.         elseif ($curBlock === 'root' && substr($substr0strlen($this->rd)) === $this->rd{
  1371.             // end template tag
  1372.             if ($this->debugecho 'TAG PARSING ENDED<br />';
  1373.             $pointer += strlen($this->rd);
  1374.             return false;
  1375.         elseif (is_array($parsingParams&& preg_match('#^([a-z0-9_]+\s*=)(?:\s+|[^=]).*#i'$substr$match)) {
  1376.             // named parameter
  1377.             if ($this->debugecho 'NAMED PARAM FOUND<br />';
  1378.             $len strlen($match[1]);
  1379.             while (substr($in$from+$len1)===' '{
  1380.                 $len++;
  1381.             }
  1382.             if ($pointer !== null{
  1383.                 $pointer += $len;
  1384.             }
  1385.  
  1386.             $output array(trim(substr(trim($match[1])0-1))$this->parse($in$from+$len$tofalse'namedparam'$pointer));
  1387.  
  1388.             $parsingParams[$output;
  1389.             return $parsingParams;
  1390.         elseif (preg_match('#^([a-z0-9_]+::\$[a-z0-9_]+)#i'$substr$match)) {
  1391.             // static member access
  1392.             $parsed 'var';
  1393.             if (is_array($parsingParams)) {
  1394.                 $parsingParams[array($match[1]$match[1]);
  1395.                 $out $parsingParams;
  1396.             else {
  1397.                 $out $match[1];
  1398.             }
  1399.             $pointer += strlen($match[1]);
  1400.         elseif ($substr!=='' && (is_array($parsingParams|| $curBlock === 'namedparam' || $curBlock === 'condition' || $curBlock === 'expression')) {
  1401.             // unquoted string, bool or number
  1402.             $out $this->parseOthers($in$from$to$parsingParams$curBlock$pointer);
  1403.         else {
  1404.             // parse error
  1405.             throw new Dwoo_Compilation_Exception($this'Parse error in "'.substr($in$from$to-$from).'"');
  1406.         }
  1407.  
  1408.         if (empty($out)) {
  1409.             return '';
  1410.         }
  1411.  
  1412.         $substr substr($in$pointer$to-$pointer);
  1413.  
  1414.         // var parsed, check if any var-extension applies
  1415.         if ($parsed==='var'{
  1416.             if (preg_match('#^\s*([/%+*-])\s*([a-z0-9]|\$)#i'$substr$match)) {
  1417.                 if($this->debugecho 'PARSING POST-VAR EXPRESSION '.$substr.'<br />';
  1418.                 // parse expressions
  1419.                 $pointer += strlen($match[0]1;
  1420.                 if (is_array($parsingParams)) {
  1421.                     if ($match[2== '$'{
  1422.                         $expr $this->parseVar($in$pointer$toarray()$curBlock$pointer);
  1423.                     else {
  1424.                         $expr $this->parse($in$pointer$toarray()'expression'$pointer);
  1425.                     }
  1426.                     $out[count($out)-1][0.= $match[1$expr[0][0];
  1427.                     $out[count($out)-1][1.= $match[1$expr[0][1];
  1428.                 else {
  1429.                     if ($match[2== '$'{
  1430.                         $expr $this->parseVar($in$pointer$tofalse$curBlock$pointer);
  1431.                     else {
  1432.                         $expr $this->parse($in$pointer$tofalse'expression'$pointer);
  1433.                     }
  1434.                     if (is_array($out&& is_array($expr)) {
  1435.                         $out[0.= $match[1$expr[0];
  1436.                         $out[1.= $match[1$expr[1];
  1437.                     elseif (is_array($out)) {
  1438.                         $out[0.= $match[1$expr;
  1439.                         $out[1.= $match[1$expr;
  1440.                     elseif (is_array($expr)) {
  1441.                         $out .= $match[1$expr[0];
  1442.                     else {
  1443.                         $out .= $match[1$expr;
  1444.                     }
  1445.                 }
  1446.             else if ($curBlock === 'root' && preg_match('#^(\s*(?:[+/*%-.]=|=|\+\+|--)\s*)(.*)#s'$substr$match)) {
  1447.                 if($this->debugecho 'PARSING POST-VAR ASSIGNMENT '.$substr.'<br />';
  1448.                 // parse assignment
  1449.                 $value $match[2];
  1450.                 $operator trim($match[1]);
  1451.                 if (substr($value01== '='{
  1452.                     throw new Dwoo_Compilation_Exception($this'Unexpected "=" in <em>'.$substr.'</em>');
  1453.                 }
  1454.  
  1455.                 if ($pointer !== null{
  1456.                     $pointer += strlen($match[1]);
  1457.                 }
  1458.  
  1459.                 if ($operator !== '++' && $operator !== '--'{
  1460.                     $parts array();
  1461.                     $ptr 0;
  1462.                     $parts $this->parse($value0strlen($value)$parts'condition'$ptr);
  1463.                     $pointer += $ptr;
  1464.  
  1465.                     // load if plugin
  1466.                     try {
  1467.                         $this->getPluginType('if');
  1468.                     catch (Dwoo_Exception $e{
  1469.                         throw new Dwoo_Compilation_Exception($this'Assignments require the "if" plugin to be accessible');
  1470.                     }
  1471.  
  1472.                     $parts $this->mapParams($partsarray('Dwoo_Plugin_if''init')1);
  1473.                     $parts $this->getCompiledParams($parts);
  1474.  
  1475.                     $value Dwoo_Plugin_if::replaceKeywords($parts['*']$this);
  1476.                     $echo '';
  1477.                 else {
  1478.                     $value array();
  1479.                     $echo 'echo ';
  1480.                 }
  1481.  
  1482.                 if ($this->autoEscape{
  1483.                     $out preg_replace('#\(is_string\(\$tmp=(.+?)\) \? htmlspecialchars\(\$tmp, ENT_QUOTES, \$this->charset\) : \$tmp\)#''$1'$out);
  1484.                 }
  1485.                 $out Dwoo_Compiler::PHP_OPEN$echo $out $operator implode(' '$valueDwoo_Compiler::PHP_CLOSE;
  1486.             }
  1487.         }
  1488.  
  1489.         if ($curBlock !== 'modifier' && ($parsed === 'func' || $parsed === 'var'&& preg_match('#^\|@?[a-z0-9_]+(:.*)?#i'$substr$match)) {
  1490.             // parse modifier on funcs or vars
  1491.             $srcPointer $pointer;
  1492.             if (is_array($parsingParams)) {
  1493.                 $tmp $this->replaceModifiers(array(nullnull$out[count($out)-1][0]$match[0])'var'$pointer);
  1494.                 $out[count($out)-1][0$tmp;
  1495.                 $out[count($out)-1][1.= substr($substr$srcPointer$srcPointer $pointer);
  1496.             else {
  1497.                 $out $this->replaceModifiers(array(nullnull$out$match[0])'var'$pointer);
  1498.             }
  1499.         }
  1500.  
  1501.         // func parsed, check if any func-extension applies
  1502.         if ($parsed==='func' && preg_match('#^->[a-z0-9_]+(\s*\(.+|->[a-z].*)?#is'$substr$match)) {
  1503.             // parse method call or property read
  1504.             $ptr 0;
  1505.  
  1506.             if (is_array($parsingParams)) {
  1507.                 $output $this->parseMethodCall($out[count($out)-1][1]$match[0]$curBlock$ptr);
  1508.  
  1509.                 $out[count($out)-1][0$output;
  1510.                 $out[count($out)-1][1.= substr($match[0]0$ptr);
  1511.             else {
  1512.                 $out $this->parseMethodCall($out$match[0]$curBlock$ptr);
  1513.             }
  1514.  
  1515.             $pointer += $ptr;
  1516.         }
  1517.  
  1518.         if ($curBlock === 'root' && substr($out0strlen(self::PHP_OPEN)) !== self::PHP_OPEN{
  1519.             return self::PHP_OPEN .'echo '.$out.';'self::PHP_CLOSE;
  1520.         else {
  1521.             return $out;
  1522.         }
  1523.     }
  1524.  
  1525.     /**
  1526.      * parses a function call
  1527.      *
  1528.      * @param string $in the string within which we must parse something
  1529.      * @param int $from the starting offset of the parsed area
  1530.      * @param int $to the ending offset of the parsed area
  1531.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  1532.      * @param string $curBlock the current parser-block being processed
  1533.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  1534.      * @return string parsed values
  1535.      */
  1536.     protected function parseFunction($in$from$to$parsingParams false$curBlock=''&$pointer null)
  1537.     {
  1538.         $cmdstr substr($in$from$to-$from);
  1539.         preg_match('/^([a-z][a-z0-9_]*(?:::[a-z][a-z0-9_]*)?)(\s*'.$this->rdr.'|\s*;)?/i'$cmdstr$match);
  1540.  
  1541.         if (empty($match[1])) {
  1542.             throw new Dwoo_Compilation_Exception($this'Parse error, invalid function name : '.substr($cmdstr015));
  1543.         }
  1544.  
  1545.         $func $match[1];
  1546.  
  1547.         if (!empty($match[2])) {
  1548.             $cmdstr $match[1];
  1549.         }
  1550.  
  1551.         if ($this->debugecho 'FUNC FOUND ('.$func.')<br />';
  1552.  
  1553.         $paramsep '';
  1554.  
  1555.         if (is_array($parsingParams|| $curBlock != 'root'{
  1556.             $paramspos strpos($cmdstr'(');
  1557.             $paramsep ')';
  1558.         elseif(preg_match_all('#[a-z0-9_]+(\s*\(|\s+[^(])#i'$cmdstr$matchPREG_OFFSET_CAPTURE)) {
  1559.             $paramspos $match[1][0][1];
  1560.             $paramsep substr($match[1][0][0]-1=== '(' ')':'';
  1561.             if($paramsep === ')'{
  1562.                 $paramspos += strlen($match[1][0][0]1;
  1563.                 if(substr($cmdstr02=== 'if' || substr($cmdstr06=== 'elseif'{
  1564.                     $paramsep '';
  1565.                     if(strlen($match[1][0][0]1{
  1566.                         $paramspos--;
  1567.                     }
  1568.                 }
  1569.             }
  1570.         else {
  1571.             $paramspos false;
  1572.         }
  1573.  
  1574.         $state 0;
  1575.  
  1576.         if ($paramspos === false{
  1577.             $params array();
  1578.  
  1579.             if ($curBlock !== 'root'{
  1580.                 return $this->parseOthers($in$from$to$parsingParams$curBlock$pointer);
  1581.             }
  1582.         else {
  1583.             if ($curBlock === 'condition'{
  1584.                 // load if plugin
  1585.                 $this->getPluginType('if');
  1586.                 if (Dwoo_Plugin_if::replaceKeywords(array($func)$this!== array($func)) {
  1587.                     return $this->parseOthers($in$from$to$parsingParams$curBlock$pointer);
  1588.                 }
  1589.             }
  1590.             $whitespace strlen(substr($cmdstrstrlen($func)$paramspos-strlen($func)));
  1591.             $paramstr substr($cmdstr$paramspos+1);
  1592.             if (substr($paramstr-11=== $paramsep{
  1593.                 $paramstr substr($paramstr0-1);
  1594.             }
  1595.  
  1596.             if (strlen($paramstr)===0{
  1597.                 $params array();
  1598.                 $paramstr '';
  1599.             else {
  1600.                 $ptr 0;
  1601.                 $params array();
  1602.                 if ($func === 'empty'{
  1603.                     $params $this->parseVar($paramstr$ptrstrlen($paramstr)$params'root'$ptr);
  1604.                 else {
  1605.                     while ($ptr strlen($paramstr)) {
  1606.                         while (true{
  1607.                             if ($ptr >= strlen($paramstr)) {
  1608.                                 break 2;
  1609.                             }
  1610.  
  1611.                             if ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr=== ')'{
  1612.                                 if ($this->debugecho 'PARAM PARSING ENDED, ")" FOUND, POINTER AT '.$ptr.'<br/>';
  1613.                                 break 2;
  1614.                             elseif ($paramstr[$ptr=== ';'{
  1615.                                 $ptr++;
  1616.                                 if ($this->debugecho 'PARAM PARSING ENDED, ";" FOUND, POINTER AT '.$ptr.'<br/>';
  1617.                                 break 2;
  1618.                             elseif ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr=== '/'{
  1619.                                 if ($this->debugecho 'PARAM PARSING ENDED, "/" FOUND, POINTER AT '.$ptr.'<br/>';
  1620.                                 break 2;
  1621.                             elseif (substr($paramstr$ptrstrlen($this->rd)) === $this->rd{
  1622.                                 if ($this->debugecho 'PARAM PARSING ENDED, RIGHT DELIMITER FOUND, POINTER AT '.$ptr.'<br/>';
  1623.                                 break 2;
  1624.                             }
  1625.  
  1626.                             if ($paramstr[$ptr=== ' ' || $paramstr[$ptr=== ',' || $paramstr[$ptr=== "\r" || $paramstr[$ptr=== "\n" || $paramstr[$ptr=== "\t"{
  1627.                                 $ptr++;
  1628.                             else {
  1629.                                 break;
  1630.                             }
  1631.                         }
  1632.  
  1633.                         if ($this->debugecho 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>';
  1634.  
  1635.                         if ($func === 'if' || $func === 'elseif' || $func === 'tif'{
  1636.                             $params $this->parse($paramstr$ptrstrlen($paramstr)$params'condition'$ptr);
  1637.                         else {
  1638.                             $params $this->parse($paramstr$ptrstrlen($paramstr)$params'function'$ptr);
  1639.                         }
  1640.  
  1641.                         if ($this->debugecho 'PARAM PARSED, POINTER AT '.$ptr.' ('.substr($paramstr$ptr-13).')<br/>';
  1642.                     }
  1643.                 }
  1644.                 $paramstr substr($paramstr0$ptr);
  1645.                 $state 0;
  1646.                 foreach ($params as $k=>$p{
  1647.                     if (is_array($p&& is_array($p[1])) {
  1648.                         $state |= 2;
  1649.                     else {
  1650.                         if (($state 2&& preg_match('#^(["\'])(.+?)\1$#'$p[0]$m)) {
  1651.                             $params[$karray($m[2]array('true''true'));
  1652.                         else {
  1653.                             if ($state 2{
  1654.                                 throw new Dwoo_Compilation_Exception($this'You can not use an unnamed parameter after a named one');
  1655.                             }
  1656.                             $state |= 1;
  1657.                         }
  1658.                     }
  1659.                 }
  1660.             }
  1661.         }
  1662.  
  1663.         if ($pointer !== null{
  1664.             $pointer += (isset($paramstrstrlen($paramstr0(')' === $paramsep ($paramspos === false 1)) strlen($func(isset($whitespace$whitespace 0);
  1665.             if ($this->debugecho 'FUNC ADDS '.((isset($paramstrstrlen($paramstr0(')' === $paramsep ($paramspos === false 1)) strlen($func)).' TO POINTER<br/>';
  1666.         }
  1667.  
  1668.         if ($curBlock === 'method' || $func === 'do' || strstr($func'::'!== false{
  1669.             $pluginType Dwoo::NATIVE_PLUGIN;
  1670.         else {
  1671.             $pluginType $this->getPluginType($func);
  1672.         }
  1673.  
  1674.         // blocks
  1675.         if ($pluginType Dwoo::BLOCK_PLUGIN{
  1676.             if ($curBlock !== 'root' || is_array($parsingParams)) {
  1677.                 throw new Dwoo_Compilation_Exception($this'Block plugins can not be used as other plugin\'s arguments');
  1678.             }
  1679.             if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1680.                 return $this->addCustomBlock($func$params$state);
  1681.             else {
  1682.                 return $this->addBlock($func$params$state);
  1683.             }
  1684.         elseif ($pluginType Dwoo::SMARTY_BLOCK{
  1685.             if ($curBlock !== 'root' || is_array($parsingParams)) {
  1686.                 throw new Dwoo_Compilation_Exception($this'Block plugins can not be used as other plugin\'s arguments');
  1687.             }
  1688.  
  1689.             if ($state 2{
  1690.                 array_unshift($paramsarray('__functype'array($pluginType$pluginType)));
  1691.                 array_unshift($paramsarray('__funcname'array($func$func)));
  1692.             else {
  1693.                 array_unshift($paramsarray($pluginType$pluginType));
  1694.                 array_unshift($paramsarray($func$func));
  1695.             }
  1696.  
  1697.             return $this->addBlock('smartyinterface'$params$state);
  1698.         }
  1699.  
  1700.         // funcs
  1701.         if ($pluginType Dwoo::NATIVE_PLUGIN || $pluginType Dwoo::SMARTY_FUNCTION || $pluginType Dwoo::SMARTY_BLOCK{
  1702.             $params $this->mapParams($paramsnull$state);
  1703.         elseif ($pluginType Dwoo::CLASS_PLUGIN{
  1704.             if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1705.                 $params $this->mapParams($paramsarray($this->customPlugins[$func]['class']$this->customPlugins[$func]['function'])$state);
  1706.             else {
  1707.                 $params $this->mapParams($paramsarray('Dwoo_Plugin_'.$func($pluginType Dwoo::COMPILABLE_PLUGIN'compile' 'process')$state);
  1708.             }
  1709.         elseif ($pluginType Dwoo::FUNC_PLUGIN{
  1710.             if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1711.                 $params $this->mapParams($params$this->customPlugins[$func]['callback']$state);
  1712.             else {
  1713.                 $params $this->mapParams($params'Dwoo_Plugin_'.$func.(($pluginType Dwoo::COMPILABLE_PLUGIN'_compile' '')$state);
  1714.             }
  1715.         elseif ($pluginType Dwoo::SMARTY_MODIFIER{
  1716.             $output 'smarty_modifier_'.$func.'('.implode(', '$params).')';
  1717.         elseif ($pluginType Dwoo::PROXY_PLUGIN{
  1718.             $params $this->mapParams($params$this->getDwoo()->getPluginProxy()->getCallback($func)$state);
  1719.         elseif ($pluginType Dwoo::TEMPLATE_PLUGIN{
  1720.             // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values))
  1721.             $map array();
  1722.             foreach ($this->templatePlugins[$func]['params'as $param=>$defValue{
  1723.                 if ($param == 'rest'{
  1724.                     $param '*';
  1725.                 }
  1726.                 $hasDefault $defValue !== null;
  1727.                 if ($defValue === 'null'{
  1728.                     $defValue null;
  1729.                 elseif ($defValue === 'false'{
  1730.                     $defValue false;
  1731.                 elseif ($defValue === 'true'{
  1732.                     $defValue true;
  1733.                 elseif (preg_match('#^([\'"]).*?\1$#'$defValue)) {
  1734.                     $defValue substr($defValue1-1);
  1735.                 }
  1736.                 $map[array($param$hasDefault$defValue);
  1737.             }
  1738.  
  1739.             $params $this->mapParams($paramsnull$state$map);
  1740.         }
  1741.  
  1742.         // only keep php-syntax-safe values for non-block plugins
  1743.         foreach ($params as &$p{
  1744.             $p $p[0];
  1745.         }
  1746.         if ($pluginType Dwoo::NATIVE_PLUGIN{
  1747.             if ($func === 'do'{
  1748.                 if (isset($params['*'])) {
  1749.                     $output implode(';'$params['*']).';';
  1750.                 else {
  1751.                     $output '';
  1752.                 }
  1753.  
  1754.                 if (is_array($parsingParams|| $curBlock !== 'root'{
  1755.                     throw new Dwoo_Compilation_Exception($this'Do can not be used inside another function or block');
  1756.                 else {
  1757.                     return self::PHP_OPEN.$output.self::PHP_CLOSE;
  1758.                 }
  1759.             else {
  1760.                 if (isset($params['*'])) {
  1761.                     $output $func.'('.implode(', '$params['*']).')';
  1762.                 else {
  1763.                     $output $func.'()';
  1764.                 }
  1765.             }
  1766.         elseif ($pluginType Dwoo::FUNC_PLUGIN{
  1767.             if ($pluginType Dwoo::COMPILABLE_PLUGIN{
  1768.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1769.                     $funcCompiler $this->customPlugins[$func]['callback'];
  1770.                 else {
  1771.                     $funcCompiler 'Dwoo_Plugin_'.$func.'_compile';
  1772.                 }
  1773.                 array_unshift($params$this);
  1774.                 $output call_user_func_array($funcCompiler$params);
  1775.             else {
  1776.                 array_unshift($params'$this');
  1777.                 $params self::implode_r($params);
  1778.  
  1779.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1780.                     $callback $this->customPlugins[$func]['callback'];
  1781.                     $output 'call_user_func(\''.$callback.'\', '.$params.')';
  1782.                 else {
  1783.                     $output 'Dwoo_Plugin_'.$func.'('.$params.')';
  1784.                 }
  1785.             }
  1786.         elseif ($pluginType Dwoo::CLASS_PLUGIN{
  1787.             if ($pluginType Dwoo::COMPILABLE_PLUGIN{
  1788.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1789.                     $callback $this->customPlugins[$func]['callback'];
  1790.                     if (!is_array($callback)) {
  1791.                         if (!method_exists($callback'compile')) {
  1792.                             throw new Dwoo_Exception('Custom plugin '.$func.' must implement the "compile" method to be compilable, or you should provide a full callback to the method to use');
  1793.                         }
  1794.                         if (($ref new ReflectionMethod($callback'compile')) && $ref->isStatic()) {
  1795.                             $funcCompiler array($callback'compile');
  1796.                         else {
  1797.                             $funcCompiler array(new $callback'compile');
  1798.                         }
  1799.                     else {
  1800.                         $funcCompiler $callback;
  1801.                     }
  1802.                 else {
  1803.                     $funcCompiler array('Dwoo_Plugin_'.$func'compile');
  1804.                     array_unshift($params$this);
  1805.                 }
  1806.                 $output call_user_func_array($funcCompiler$params);
  1807.             else {
  1808.                 $params self::implode_r($params);
  1809.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1810.                     $callback $this->customPlugins[$func]['callback'];
  1811.                     if (!is_array($callback)) {
  1812.                         if (!method_exists($callback'process')) {
  1813.                             throw new Dwoo_Exception('Custom plugin '.$func.' must implement the "process" method to be usable, or you should provide a full callback to the method to use');
  1814.                         }
  1815.                         if (($ref new ReflectionMethod($callback'process')) && $ref->isStatic()) {
  1816.                             $output 'call_user_func(array(\''.$callback.'\', \'process\'), '.$params.')';
  1817.                         else {
  1818.                             $output 'call_user_func(array($this->getObjectPlugin(\''.$callback.'\'), \'process\'), '.$params.')';
  1819.                         }
  1820.                     elseif (is_object($callback[0])) {
  1821.                         $output 'call_user_func(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), '.$params.')';
  1822.                     elseif (($ref new ReflectionMethod($callback[0]$callback[1])) && $ref->isStatic()) {
  1823.                         $output 'call_user_func(array(\''.$callback[0].'\', \''.$callback[1].'\'), '.$params.')';
  1824.                     else {
  1825.                         $output 'call_user_func(array($this->getObjectPlugin(\''.$callback[0].'\'), \''.$callback[1].'\'), '.$params.')';
  1826.                     }
  1827.                     if (empty($params)) {
  1828.                         $output substr($output0-3).')';
  1829.                     }
  1830.                 else {
  1831.                     $output '$this->classCall(\''.$func.'\', array('.$params.'))';
  1832.                 }
  1833.             }
  1834.         elseif ($pluginType Dwoo::PROXY_PLUGIN{
  1835.             $output call_user_func(array($this->dwoo->getPluginProxy()'getCode')$func$params);
  1836.         elseif ($pluginType Dwoo::SMARTY_FUNCTION{
  1837.             if (isset($params['*'])) {
  1838.                 $params self::implode_r($params['*']true);
  1839.             else {
  1840.                 $params '';
  1841.             }
  1842.  
  1843.             if ($pluginType Dwoo::CUSTOM_PLUGIN{
  1844.                 $callback $this->customPlugins[$func]['callback'];
  1845.                 if (is_array($callback)) {
  1846.                     if (is_object($callback[0])) {
  1847.                         $output 'call_user_func_array(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array(array('.$params.'), $this))';
  1848.                     else {
  1849.                         $output 'call_user_func_array(array(\''.$callback[0].'\', \''.$callback[1].'\'), array(array('.$params.'), $this))';
  1850.                     }
  1851.                 else {
  1852.                     $output $callback.'(array('.$params.'), $this)';
  1853.                 }
  1854.             else {
  1855.                 $output 'smarty_function_'.$func.'(array('.$params.'), $this)';
  1856.             }
  1857.         elseif ($pluginType Dwoo::TEMPLATE_PLUGIN{
  1858.             array_unshift($params'$this');
  1859.             $params self::implode_r($params);
  1860.             $output 'Dwoo_Plugin_'.$func.'_'.$this->templatePlugins[$func]['uuid'].'('.$params.')';
  1861.             $this->templatePlugins[$func]['called'true;
  1862.         }
  1863.  
  1864.         if (is_array($parsingParams)) {
  1865.             $parsingParams[array($output$output);
  1866.             return $parsingParams;
  1867.         elseif ($curBlock === 'namedparam'{
  1868.             return array($output$output);
  1869.         else {
  1870.             return $output;
  1871.         }
  1872.     }
  1873.  
  1874.     /**
  1875.      * parses a string
  1876.      *
  1877.      * @param string $in the string within which we must parse something
  1878.      * @param int $from the starting offset of the parsed area
  1879.      * @param int $to the ending offset of the parsed area
  1880.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  1881.      * @param string $curBlock the current parser-block being processed
  1882.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  1883.      * @return string parsed values
  1884.      */
  1885.     protected function parseString($in$from$to$parsingParams false$curBlock=''&$pointer null)
  1886.     {
  1887.         $substr substr($in$from$to-$from);
  1888.         $first $substr[0];
  1889.  
  1890.         if ($this->debugecho 'STRING FOUND (in '.htmlentities(substr($in$frommin($to-$from50))).(($to-$from50 '...':'').')<br />';
  1891.         $strend false;
  1892.         $o $from+1;
  1893.         while ($strend === false{
  1894.             $strend strpos($in$first$o);
  1895.             if ($strend === false{
  1896.                 throw new Dwoo_Compilation_Exception($this'Unfinished string, started with '.substr($in$from$to-$from));
  1897.             }
  1898.             if (substr($in$strend-11=== '\\'{
  1899.                 $o $strend+1;
  1900.                 $strend false;
  1901.             }
  1902.         }
  1903.         if ($this->debugecho 'STRING DELIMITED: '.substr($in$from$strend+1-$from).'<br/>';
  1904.  
  1905.         $srcOutput substr($in$from$strend+1-$from);
  1906.  
  1907.         if ($pointer !== null{
  1908.             $pointer += strlen($srcOutput);
  1909.         }
  1910.  
  1911.         $output $this->replaceStringVars($srcOutput$first);
  1912.  
  1913.         // handle modifiers
  1914.         if ($curBlock !== 'modifier' && preg_match('#^((?:\|(?:@?[a-z0-9_]+(?::.*)*))+)#i'substr($substr$strend+1-$from)$match)) {
  1915.             $modstr $match[1];
  1916.  
  1917.             if ($curBlock === 'root' && substr($modstr-1=== '}'{
  1918.                 $modstr substr($modstr0-1);
  1919.             }
  1920.             $modstr str_replace('\\'.$first$first$modstr);
  1921.             $ptr 0;
  1922.             $output $this->replaceModifiers(array(nullnull$output$modstr)'string'$ptr);
  1923.  
  1924.             $strend += $ptr;
  1925.             if ($pointer !== null{
  1926.                 $pointer += $ptr;
  1927.             }
  1928.             $srcOutput .= substr($substr$strend+1-$from$ptr);
  1929.         }
  1930.  
  1931.         if (is_array($parsingParams)) {
  1932.             $parsingParams[array($outputsubstr($srcOutput1-1));
  1933.             return $parsingParams;
  1934.         elseif ($curBlock === 'namedparam'{
  1935.             return array($outputsubstr($srcOutput1-1));
  1936.         else {
  1937.             return $output;
  1938.         }
  1939.     }
  1940.  
  1941.     /**
  1942.      * parses a constant
  1943.      *
  1944.      * @param string $in the string within which we must parse something
  1945.      * @param int $from the starting offset of the parsed area
  1946.      * @param int $to the ending offset of the parsed area
  1947.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  1948.      * @param string $curBlock the current parser-block being processed
  1949.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  1950.      * @return string parsed values
  1951.      */
  1952.     protected function parseConst($in$from$to$parsingParams false$curBlock=''&$pointer null)
  1953.     {
  1954.         $substr substr($in$from$to-$from);
  1955.  
  1956.         if ($this->debug{
  1957.             echo 'CONST FOUND : '.$substr.'<br />';
  1958.         }
  1959.  
  1960.         if (!preg_match('#^%([a-z0-9_:]+)#i'$substr$m)) {
  1961.             throw new Dwoo_Compilation_Exception($this'Invalid constant');
  1962.         }
  1963.  
  1964.         if ($pointer !== null{
  1965.             $pointer += strlen($m[0]);
  1966.         }
  1967.  
  1968.         $output $this->parseConstKey($m[1]$curBlock);
  1969.  
  1970.         if (is_array($parsingParams)) {
  1971.             $parsingParams[array($output$m[1]);
  1972.             return $parsingParams;
  1973.         elseif ($curBlock === 'namedparam'{
  1974.             return array($output$m[1]);
  1975.         else {
  1976.             return $output;
  1977.         }
  1978.     }
  1979.  
  1980.     /**
  1981.      * parses a constant
  1982.      *
  1983.      * @param string $key the constant to parse
  1984.      * @param string $curBlock the current parser-block being processed
  1985.      * @return string parsed constant
  1986.      */
  1987.     protected function parseConstKey($key$curBlock)
  1988.     {
  1989.         if ($this->securityPolicy !== null && $this->securityPolicy->getConstantHandling(=== Dwoo_Security_Policy::CONST_DISALLOW{
  1990.             return 'null';
  1991.         }
  1992.  
  1993.         if ($curBlock !== 'root'{
  1994.             $output '(defined("'.$key.'") ? '.$key.' : null)';
  1995.         else {
  1996.             $output $key;
  1997.         }
  1998.  
  1999.         return $output;
  2000.     }
  2001.  
  2002.     /**
  2003.      * parses a variable
  2004.      *
  2005.      * @param string $in the string within which we must parse something
  2006.      * @param int $from the starting offset of the parsed area
  2007.      * @param int $to the ending offset of the parsed area
  2008.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  2009.      * @param string $curBlock the current parser-block being processed
  2010.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  2011.      * @return string parsed values
  2012.      */
  2013.     protected function parseVar($in$from$to$parsingParams false$curBlock=''&$pointer null)
  2014.     {
  2015.         $substr substr($in$from$to-$from);
  2016.  
  2017.         if (preg_match('#(\$?\.?[a-z0-9_:]*(?:(?:(?:\.|->)(?:[a-z0-9_:]+|(?R))|\[(?:[a-z0-9_:]+|(?R)|(["\'])[^\2]*?\2)\]))*)' // var key
  2018.             ($curBlock==='root' || $curBlock==='function' || $curBlock==='namedparam' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='expression' '(\(.*)?' '()'// method call
  2019.             ($curBlock==='root' || $curBlock==='function' || $curBlock==='namedparam' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='delimited_string' '((?:(?:[+/*%=-])(?:(?<!=)=?-?[$%][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|(?<!=)=?-?[0-9.,]*|[+-]))*)':'()'// simple math expressions
  2020.             ($curBlock!=='modifier' '((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').*?\5|:[^`]*))*))+)?':'(())'// modifiers
  2021.             '#i'$substr$match)) {
  2022.             $key substr($match[1]1);
  2023.  
  2024.             $matchedLength strlen($match[0]);
  2025.             $hasModifiers !empty($match[5]);
  2026.             $hasExpression !empty($match[4]);
  2027.             $hasMethodCall !empty($match[3]);
  2028.  
  2029.             if (substr($key-1== "."{
  2030.                 $key substr($key0-1);
  2031.                 $matchedLength--;
  2032.             }
  2033.  
  2034.             if ($hasMethodCall{
  2035.                 $matchedLength -= strlen($match[3]strlen(substr($match[1]strrpos($match[1]'->')));
  2036.                 $key substr($match[1]1strrpos($match[1]'->')-1);
  2037.                 $methodCall substr($match[1]strrpos($match[1]'->')) $match[3];
  2038.             }
  2039.  
  2040.             if ($hasModifiers{
  2041.                 $matchedLength -= strlen($match[5]);
  2042.             }
  2043.  
  2044.             if ($pointer !== null{
  2045.                 $pointer += $matchedLength;
  2046.             }
  2047.  
  2048.             // replace useless brackets by dot accessed vars
  2049.             $key preg_replace('#\[([^$%\[.>-]+)\]#''.$1'$key);
  2050.  
  2051.             // prevent $foo->$bar calls because it doesn't seem worth the trouble
  2052.             if (strpos($key'->$'!== false{
  2053.                 throw new Dwoo_Compilation_Exception($this'You can not access an object\'s property using a variable name.');
  2054.             }
  2055.  
  2056.             if ($this->debug{
  2057.                 if ($hasMethodCall{
  2058.                     echo 'METHOD CALL FOUND : $'.$key.substr($methodCall030).'<br />';
  2059.                 else {
  2060.                     echo 'VAR FOUND : $'.$key.'<br />';
  2061.                 }
  2062.             }
  2063.  
  2064.             $key str_replace('"''\\"'$key);
  2065.  
  2066.             $cnt=substr_count($key'$');
  2067.             if ($cnt 0{
  2068.                 $uid 0;
  2069.                 $parsed array($uid => '');
  2070.                 $current =$parsed;
  2071.                 $curTxt =$parsed[$uid++];
  2072.                 $tree array();
  2073.                 $chars str_split($key1);
  2074.                 $inSplittedVar false;
  2075.                 $bracketCount 0;
  2076.  
  2077.                 while (($char array_shift($chars)) !== null{
  2078.                     if ($char === '['{
  2079.                         if (count($tree0{
  2080.                             $bracketCount++;
  2081.                         else {
  2082.                             $tree[=$current;
  2083.                             $current[$uidarray($uid+=> '');
  2084.                             $current =$current[$uid++];
  2085.                             $curTxt =$current[$uid++];
  2086.                             continue;
  2087.                         }
  2088.                     elseif ($char === ']'{
  2089.                         if ($bracketCount 0{
  2090.                             $bracketCount--;
  2091.                         else {
  2092.                             $current =$tree[count($tree)-1];
  2093.                             array_pop($tree);
  2094.                             if (current($chars!== '[' && current($chars!== false && current($chars!== ']'{
  2095.                                 $current[$uid'';
  2096.                                 $curTxt =$current[$uid++];
  2097.                             }
  2098.                             continue;
  2099.                         }
  2100.                     elseif ($char === '$'{
  2101.                         if (count($tree== 0{
  2102.                             $curTxt =$current[$uid++];
  2103.                             $inSplittedVar true;
  2104.                         }
  2105.                     elseif (($char === '.' || $char === '-'&& count($tree== && $inSplittedVar{
  2106.                         $curTxt =$current[$uid++];
  2107.                         $inSplittedVar false;
  2108.                     }
  2109.  
  2110.                     $curTxt .= $char;
  2111.                 }
  2112.                 unset($uid$current$curTxt$tree$chars);
  2113.  
  2114.                 if ($this->debugecho 'RECURSIVE VAR REPLACEMENT : '.$key.'<br>';
  2115.  
  2116.                 $key $this->flattenVarTree($parsed);
  2117.  
  2118.                 if ($this->debugecho 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>';
  2119.  
  2120.                 $output preg_replace('#(^""\.|""\.|\.""$|(\()""\.|\.""(\)))#''$2$3''$this->readVar("'.$key.'")');
  2121.             else {
  2122.                 $output $this->parseVarKey($key$hasModifiers 'modifier' $curBlock);
  2123.             }
  2124.  
  2125.             // methods
  2126.             if ($hasMethodCall{
  2127.                 $ptr 0;
  2128.  
  2129.                 $output $this->parseMethodCall($output$methodCall$curBlock$ptr);
  2130.  
  2131.                 if ($pointer !== null{
  2132.                     $pointer += $ptr;
  2133.                 }
  2134.                 $matchedLength += $ptr;
  2135.             }
  2136.  
  2137.             if ($hasExpression{
  2138.                 // expressions
  2139.                 preg_match_all('#(?:([+/*%=-])(=?-?[%$][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|=?-?[0-9.,]+|\1))#i'$match[4]$expMatch);
  2140.  
  2141.                 foreach ($expMatch[1as $k=>$operator{
  2142.                     if (substr($expMatch[2][$k]01)==='='{
  2143.                         $assign true;
  2144.                         if ($operator === '='{
  2145.                             throw new Dwoo_Compilation_Exception($this'Invalid expression <em>'.$substr.'</em>, can not use "==" in expressions');
  2146.                         }
  2147.                         if ($curBlock !== 'root'{
  2148.                             throw new Dwoo_Compilation_Exception($this'Invalid expression <em>'.$substr.'</em>, assignments can only be used in top level expressions like {$foo+=3} or {$foo="bar"}');
  2149.                         }
  2150.                         $operator .= '=';
  2151.                         $expMatch[2][$ksubstr($expMatch[2][$k]1);
  2152.                     }
  2153.  
  2154.                     if (substr($expMatch[2][$k]01)==='-' && strlen($expMatch[2][$k]1{
  2155.                         $operator .= '-';
  2156.                         $expMatch[2][$ksubstr($expMatch[2][$k]1);
  2157.                     }
  2158.                     if (($operator==='+'||$operator==='-'&& $expMatch[2][$k]===$operator{
  2159.                         $output '('.$output.$operator.$operator.')';
  2160.                         break;
  2161.                     elseif (substr($expMatch[2][$k]01=== '$'{
  2162.                         $output '('.$output.' '.$operator.' '.$this->parseVar($expMatch[2][$k]0strlen($expMatch[2][$k])false'expression').')';
  2163.                     elseif (substr($expMatch[2][$k]01=== '%'{
  2164.                         $output '('.$output.' '.$operator.' '.$this->parseConst($expMatch[2][$k]0strlen($expMatch[2][$k])false'expression').')';
  2165.                     elseif (!empty($expMatch[2][$k])) {
  2166.                         $output '('.$output.' '.$operator.' '.str_replace(',''.'$expMatch[2][$k]).')';
  2167.                     else {
  2168.                         throw new Dwoo_Compilation_Exception($this'Unfinished expression <em>'.$substr.'</em>, missing var or number after math operator');
  2169.                     }
  2170.                 }
  2171.             }
  2172.  
  2173.             if ($this->autoEscape === true{
  2174.                 $output '(is_string($tmp='.$output.') ? htmlspecialchars($tmp, ENT_QUOTES, $this->charset) : $tmp)';
  2175.             }
  2176.  
  2177.             // handle modifiers
  2178.             if ($curBlock !== 'modifier' && $hasModifiers{
  2179.                 $ptr 0;
  2180.                 $output $this->replaceModifiers(array(nullnull$output$match[5])'var'$ptr);
  2181.                 if ($pointer !== null{
  2182.                     $pointer += $ptr;
  2183.                 }
  2184.                 $matchedLength += $ptr;
  2185.             }
  2186.  
  2187.             if (is_array($parsingParams)) {
  2188.                 $parsingParams[array($output$key);
  2189.                 return $parsingParams;
  2190.             elseif ($curBlock === 'namedparam'{
  2191.                 return array($output$key);
  2192.             elseif ($curBlock === 'string' || $curBlock === 'delimited_string'{
  2193.                 return array($matchedLength$output);
  2194.             elseif ($curBlock === 'expression' || $curBlock === 'variable'{
  2195.                 return $output;
  2196.             elseif (isset($assign)) {
  2197.                 return self::PHP_OPEN.$output.';'.self::PHP_CLOSE;
  2198.             else {
  2199.                 return $output;
  2200.             }
  2201.         else {
  2202.             if ($curBlock === 'string' || $curBlock === 'delimited_string'{
  2203.                 return array(0'');
  2204.             else {
  2205.                 throw new Dwoo_Compilation_Exception($this'Invalid variable name <em>'.$substr.'</em>');
  2206.             }
  2207.         }
  2208.     }
  2209.  
  2210.     /**
  2211.      * parses any number of chained method calls/property reads
  2212.      *
  2213.      * @param string $output the variable or whatever upon which the method are called
  2214.      * @param string $methodCall method call source, starting at "->"
  2215.      * @param string $curBlock the current parser-block being processed
  2216.      * @param int $pointer a reference to a pointer that will be increased by the amount of characters parsed
  2217.      * @return string parsed call(s)/read(s)
  2218.      */
  2219.     protected function parseMethodCall($output$methodCall$curBlock&$pointer)
  2220.     {
  2221.         $ptr 0;
  2222.         $len strlen($methodCall);
  2223.  
  2224.         while ($ptr $len{
  2225.             if (strpos($methodCall'->'$ptr=== $ptr{
  2226.                 $ptr += 2;
  2227.             }
  2228.  
  2229.             if (in_array($methodCall[$ptr]array(';''/'' '"\t""\r""\n"')''+''*''%''=''-''|')) || substr($methodCall$ptrstrlen($this->rd)) === $this->rd{
  2230.                 // break char found
  2231.                 break;
  2232.             }
  2233.  
  2234.             if(!preg_match('/^([a-z0-9_]+)(\(.*?\))?/i'substr($methodCall$ptr)$methMatch)) {
  2235.                 throw new Dwoo_Compilation_Exception($this'Invalid method name : '.substr($methodCall$ptr20));
  2236.             }
  2237.  
  2238.             if (empty($methMatch[2])) {
  2239.                 // property
  2240.                 if ($curBlock === 'root'{
  2241.                     $output .= '->'.$methMatch[1];
  2242.                 else {
  2243.                     $output '(($tmp = '.$output.') ? $tmp->'.$methMatch[1].' : null)';
  2244.                 }
  2245.                 $ptr += strlen($methMatch[1]);
  2246.             else {
  2247.                 // method
  2248.                 if (substr($methMatch[2]02=== '()'{
  2249.                     $parsedCall '->'.$methMatch[1].'()';
  2250.                     $ptr += strlen($methMatch[1]2;
  2251.                 else {
  2252.                     $parsedCall '->'.$this->parseFunction($methodCall$ptrstrlen($methodCall)false'method'$ptr);
  2253.                 }
  2254.                 if ($curBlock === 'root'{
  2255.                     $output .= $parsedCall;
  2256.                 else {
  2257.                     $output '(($tmp = '.$output.') ? $tmp'.$parsedCall.' : null)';
  2258.                 }
  2259.             }
  2260.         }
  2261.  
  2262.         $pointer += $ptr;
  2263.         return $output;
  2264.     }
  2265.  
  2266.     /**
  2267.      * parses a constant variable (a variable that doesn't contain another variable) and preprocesses it to save runtime processing time
  2268.      *
  2269.      * @param string $key the variable to parse
  2270.      * @param string $curBlock the current parser-block being processed
  2271.      * @return string parsed variable
  2272.      */
  2273.     protected function parseVarKey($key$curBlock)
  2274.     {
  2275.         if ($key === ''{
  2276.             return '$this->scope';
  2277.         }
  2278.         if (substr($key01=== '.'{
  2279.             $key 'dwoo'.$key;
  2280.         }
  2281.         if (preg_match('#dwoo\.(get|post|server|cookies|session|env|request)((?:\.[a-z0-9_-]+)+)#i'$key$m)) {
  2282.             $global strtoupper($m[1]);
  2283.             if ($global === 'COOKIES'{
  2284.                 $global 'COOKIE';
  2285.             }
  2286.             $key '$_'.$global;
  2287.             foreach (explode('.'ltrim($m[2]'.')) as $part)
  2288.                 $key .= '['.var_export($parttrue).']';
  2289.             if ($curBlock === 'root'{
  2290.                 $output $key;
  2291.             else {
  2292.                 $output '(isset('.$key.')?'.$key.':null)';
  2293.             }
  2294.         elseif (preg_match('#dwoo\.const\.([a-z0-9_:]+)#i'$key$m)) {
  2295.             return $this->parseConstKey($m[1]$curBlock);
  2296.         elseif ($this->scope !== null{
  2297.             if (strstr($key'.'=== false && strstr($key'['=== false && strstr($key'->'=== false{
  2298.                 if ($key === 'dwoo'{
  2299.                     $output '$this->globals';
  2300.                 elseif ($key === '_root' || $key === '__'{
  2301.                     $output '$this->data';
  2302.                 elseif ($key === '_parent' || $key === '_'{
  2303.                     $output '$this->readParentVar(1)';
  2304.                 elseif ($key === '_key'{
  2305.                     $output '$tmp_key';
  2306.                 else {
  2307.                     if ($curBlock === 'root'{
  2308.                         $output '$this->scope["'.$key.'"]';
  2309.                     else {
  2310.                         $output '(isset($this->scope["'.$key.'"]) ? $this->scope["'.$key.'"] : null)';
  2311.                     }
  2312.                 }
  2313.             else {
  2314.                 preg_match_all('#(\[|->|\.)?((?:[a-z0-9_]|-(?!>))+|(\\\?[\'"])[^\3]*?\3)\]?#i'$key$m);
  2315.  
  2316.                 $i $m[2][0];
  2317.                 if ($i === '_parent' || $i === '_'{
  2318.                     $parentCnt 0;
  2319.  
  2320.                     while (true{
  2321.                         $parentCnt++;
  2322.                         array_shift($m[2]);
  2323.                         array_shift($m[1]);
  2324.                         if (current($m[2]=== '_parent'{
  2325.                             continue;
  2326.                         }
  2327.                         break;
  2328.                     }
  2329.  
  2330.                     $output '$this->readParentVar('.$parentCnt.')';
  2331.                 else {
  2332.                     if ($i === 'dwoo'{
  2333.                         $output '$this->globals';
  2334.                         array_shift($m[2]);
  2335.                         array_shift($m[1]);
  2336.                     elseif ($i === '_root' || $i === '__'{
  2337.                         $output '$this->data';
  2338.                         array_shift($m[2]);
  2339.                         array_shift($m[1]);
  2340.                     elseif ($i === '_key'{
  2341.                         $output '$tmp_key';
  2342.                     else {
  2343.                         $output '$this->scope';
  2344.                     }
  2345.  
  2346.                     while (count($m[1]&& $m[1][0!== '->'{
  2347.                         $m[2][0preg_replace('/(^\\\([\'"])|\\\([\'"])$)/x''$2$3'$m[2][0]);
  2348.                         if(substr($m[2][0]01== '"' || substr($m[2][0]01== "'"{
  2349.                             $output .= '['.$m[2][0].']';
  2350.                         else {
  2351.                             $output .= '["'.$m[2][0].'"]';
  2352.                         }
  2353.                         array_shift($m[2]);
  2354.                         array_shift($m[1]);
  2355.                     }
  2356.  
  2357.                     if ($curBlock !== 'root'{
  2358.                         $output '(isset('.$output.') ? '.$output.':null)';
  2359.                     }
  2360.                 }
  2361.  
  2362.                 if (count($m[2])) {
  2363.                     unset($m[0]);
  2364.                     $output '$this->readVarInto('.str_replace("\n"''var_export($mtrue)).', '.$output.', '.($curBlock == 'root' 'false''true').')';
  2365.                 }
  2366.             }
  2367.         else {
  2368.             preg_match_all('#(\[|->|\.)?((?:[a-z0-9_]|-(?!>))+)\]?#i'$key$m);
  2369.             unset($m[0]);
  2370.             $output '$this->readVar('.str_replace("\n"''var_export($mtrue)).')';
  2371.         }
  2372.  
  2373.         return $output;
  2374.     }
  2375.  
  2376.     /**
  2377.      * flattens a variable tree, this helps in parsing very complex variables such as $var.foo[$foo.bar->baz].baz,
  2378.      * it computes the contents of the brackets first and works out from there
  2379.      *
  2380.      * @param array $tree the variable tree parsed by he parseVar() method that must be flattened
  2381.      * @param bool $recursed leave that to false by default, it is only for internal use
  2382.      * @return string flattened tree
  2383.      */
  2384.     protected function flattenVarTree(array $tree$recursed=false)
  2385.     {
  2386.         $out $recursed ?  '".$this->readVarInto(' '';
  2387.         foreach ($tree as $bit{
  2388.             if (is_array($bit)) {
  2389.                 $out.='.'.$this->flattenVarTree($bitfalse);
  2390.             else {
  2391.                 $key str_replace('"''\\"'$bit);
  2392.  
  2393.                 if (substr($key01)==='$'{
  2394.                     $out .= '".'.$this->parseVar($key0strlen($key)false'variable').'."';
  2395.                 else {
  2396.                     $cnt substr_count($key'$');
  2397.  
  2398.                     if ($this->debugecho 'PARSING SUBVARS IN : '.$key.'<br>';
  2399.                     if ($cnt 0{
  2400.                         while (--$cnt >= 0{
  2401.                             if (isset($last)) {
  2402.                                 $last strrpos($key'$'(strlen($key$last 1));
  2403.                             else {
  2404.                                 $last strrpos($key'$');
  2405.                             }
  2406.                             preg_match('#\$[a-z0-9_]+((?:(?:\.|->)(?:[a-z0-9_]+|(?R))|\[(?:[a-z0-9_]+|(?R))\]))*'.
  2407.                                       '((?:(?:[+/*%-])(?:\$[a-z0-9.[\]>_:-]+(?:\([^)]*\))?|[0-9.,]*))*)#i'substr($key$last)$submatch);
  2408.  
  2409.                             $len strlen($submatch[0]);
  2410.                             $key substr_replace(
  2411.                                 $key,
  2412.                                 preg_replace_callback(
  2413.                                     '#(\$[a-z0-9_]+((?:(?:\.|->)(?:[a-z0-9_]+|(?R))|\[(?:[a-z0-9_]+|(?R))\]))*)'.
  2414.                                     '((?:(?:[+/*%-])(?:\$[a-z0-9.[\]>_:-]+(?:\([^)]*\))?|[0-9.,]*))*)#i',
  2415.                                     array($this'replaceVarKeyHelper')substr($key$last$len)
  2416.                                 ),
  2417.                                 $last,
  2418.                                 $len
  2419.                             );
  2420.                             if ($this->debugecho 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>';
  2421.                         }
  2422.                         unset($last);
  2423.  
  2424.                         $out .= $key;
  2425.                     else {
  2426.                         $out .= $key;
  2427.                     }
  2428.                 }
  2429.             }
  2430.         }
  2431.         $out .= $recursed ', true)."' '';
  2432.         return $out;
  2433.     }
  2434.  
  2435.     /**
  2436.      * helper function that parses a variable
  2437.      *
  2438.      * @param array $match the matched variable, array(1=>"string match")
  2439.      * @return string parsed variable
  2440.      */
  2441.     protected function replaceVarKeyHelper($match)
  2442.     {
  2443.         return '".'.$this->parseVar($match[0]0strlen($match[0])false'variable').'."';
  2444.     }
  2445.  
  2446.     /**
  2447.      * parses various constants, operators or non-quoted strings
  2448.      *
  2449.      * @param string $in the string within which we must parse something
  2450.      * @param int $from the starting offset of the parsed area
  2451.      * @param int $to the ending offset of the parsed area
  2452.      * @param mixed $parsingParams must be an array if we are parsing a function or modifier's parameters, or false by default
  2453.      * @param string $curBlock the current parser-block being processed
  2454.      * @param mixed $pointer a reference to a pointer that will be increased by the amount of characters parsed, or null by default
  2455.      * @return string parsed values
  2456.      */
  2457.     protected function parseOthers($in$from$to$parsingParams false$curBlock=''&$pointer null)
  2458.     {
  2459.         $first $in[$from];
  2460.         $substr substr($in$from$to-$from);
  2461.  
  2462.         $end strlen($substr);
  2463.  
  2464.         if ($curBlock === 'condition'{
  2465.             $breakChars array('('')'' ''||''&&''|''&''>=''<=''===''==''=''!==''!=''<<''<''>>''>''^''~'',''+''-''*''/''%''!''?'':'$this->rd';');
  2466.         elseif ($curBlock === 'modifier'{
  2467.             $breakChars array(' '','')'':''|'"\r""\n""\t"";"$this->rd);
  2468.         elseif ($curBlock === 'expression'{
  2469.             $breakChars array('/''%''+''-''*'' '','')'"\r""\n""\t"";"$this->rd);
  2470.         else {
  2471.             $breakChars array(' '','')'"\r""\n""\t"";"$this->rd);
  2472.         }
  2473.  
  2474.         $breaker false;
  2475.         while (list($k,$chareach($breakChars)) {
  2476.             $test strpos($substr$char);
  2477.             if ($test !== false && $test $end{
  2478.                 $end $test;
  2479.                 $breaker $k;
  2480.             }
  2481.         }
  2482.  
  2483.         if ($curBlock === 'condition'{
  2484.             if ($end === && $breaker !== false{
  2485.                 $end strlen($breakChars[$breaker]);
  2486.             }
  2487.         }
  2488.  
  2489.         if ($end !== false{
  2490.             $substr substr($substr0$end);
  2491.         }
  2492.  
  2493.         if ($pointer !== null{
  2494.             $pointer += strlen($substr);
  2495.         }
  2496.  
  2497.         $src $substr;
  2498.         $substr trim($substr);
  2499.  
  2500.         if (strtolower($substr=== 'false' || strtolower($substr=== 'no' || strtolower($substr=== 'off'{
  2501.             if ($this->debugecho 'BOOLEAN(FALSE) PARSED<br />';
  2502.             $substr 'false';
  2503.         elseif (strtolower($substr=== 'true' || strtolower($substr=== 'yes' || strtolower($substr=== 'on'{
  2504.             if ($this->debugecho 'BOOLEAN(TRUE) PARSED<br />';
  2505.             $substr 'true';
  2506.         elseif ($substr === 'null' || $substr === 'NULL'{
  2507.             if ($this->debugecho 'NULL PARSED<br />';
  2508.             $substr 'null';
  2509.         elseif (is_numeric($substr)) {
  2510.             $substr = (float) $substr;
  2511.             if ((int) $substr == $substr{
  2512.                 $substr = (int) $substr;
  2513.             }
  2514.             if ($this->debugecho 'NUMBER ('.$substr.') PARSED<br />';
  2515.         elseif (preg_match('{^-?(\d+|\d*(\.\d+))\s*([/*%+-]\s*-?(\d+|\d*(\.\d+)))+$}'$substr)) {
  2516.             if ($this->debugecho 'SIMPLE MATH PARSED<br />';
  2517.             $substr '('.$substr.')';
  2518.         elseif ($curBlock === 'condition' && array_search($substr$breakCharstrue!== false{
  2519.             if ($this->debugecho 'BREAKCHAR ('.$substr.') PARSED<br />';
  2520.             //$substr = '"'.$substr.'"';
  2521.         else {
  2522.             $substr $this->replaceStringVars('\''.str_replace('\'''\\\''$substr).'\'''\''$curBlock);
  2523.  
  2524.             if ($this->debugecho 'BLABBER ('.$substr.') CASTED AS STRING<br />';
  2525.         }
  2526.  
  2527.         if (is_array($parsingParams)) {
  2528.             $parsingParams[array($substr$src);
  2529.             return $parsingParams;
  2530.         elseif ($curBlock === 'namedparam'{
  2531.             return array($substr$src);
  2532.         elseif ($curBlock === 'expression'{
  2533.             return $substr;
  2534.         else {
  2535.             throw new Exception('Something went wrong');
  2536.         }
  2537.     }
  2538.  
  2539.     /**
  2540.      * replaces variables within a parsed string
  2541.      *
  2542.      * @param string $string the parsed string
  2543.      * @param string $first the first character parsed in the string, which is the string delimiter (' or ")
  2544.      * @param string $curBlock the current parser-block being processed
  2545.      * @return string the original string with variables replaced
  2546.      */
  2547.     protected function replaceStringVars($string$first$curBlock='')
  2548.     {
  2549.         $pos 0;
  2550.         if ($this->debugecho 'STRING VAR REPLACEMENT : '.$string.'<br>';
  2551.         // replace vars
  2552.         while (($pos strpos($string'$'$pos)) !== false{
  2553.             $prev substr($string$pos-11);
  2554.             if ($prev === '\\'{
  2555.                 $pos++;
  2556.                 continue;
  2557.             }
  2558.  
  2559.             $var $this->parse($string$posnullfalse($curBlock === 'modifier' 'modifier' ($prev === '`' 'delimited_string':'string')));
  2560.             $len $var[0];
  2561.             $var $this->parse(str_replace('\\'.$first$first$string)$posnullfalse($curBlock === 'modifier' 'modifier' ($prev === '`' 'delimited_string':'string')));
  2562.  
  2563.             if ($prev === '`' && substr($string$pos+$len1=== '`'{
  2564.                 $string substr_replace($string$first.'.'.$var[1].'.'.$first$pos-1$len+2);
  2565.             else {
  2566.                 $string substr_replace($string$first.'.'.$var[1].'.'.$first$pos$len);
  2567.             }
  2568.             $pos += strlen($var[1]2;
  2569.             if ($this->debugecho 'STRING VAR REPLACEMENT DONE : '.$string.'<br>';
  2570.         }
  2571.  
  2572.         // handle modifiers
  2573.         // TODO Obsolete?
  2574.         $string preg_replace_callback('#("|\')\.(.+?)\.\1((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').+?\4|:[^`]*))*))+)#i'array($this'replaceModifiers')$string);
  2575.  
  2576.         // replace escaped dollar operators by unescaped ones if required
  2577.         if ($first==="'"{
  2578.             $string str_replace('\\$''$'$string);
  2579.         }
  2580.  
  2581.         return $string;
  2582.     }
  2583.  
  2584.     /**
  2585.      * replaces the modifiers applied to a string or a variable
  2586.      *
  2587.      * @param array $m the regex matches that must be array(1=>"double or single quotes enclosing a string, when applicable", 2=>"the string or var", 3=>"the modifiers matched")
  2588.      * @param string $curBlock the current parser-block being processed
  2589.      * @return string the input enclosed with various function calls according to the modifiers found
  2590.      */
  2591.     protected function replaceModifiers(array $m$curBlock null&$pointer null)
  2592.     {
  2593.         if ($this->debugecho 'PARSING MODIFIERS : '.$m[3].'<br />';
  2594.  
  2595.         if ($pointer !== null{
  2596.             $pointer += strlen($m[3]);
  2597.         }
  2598.         // remove first pipe
  2599.         $cmdstrsrc substr($m[3]1);
  2600.         // remove last quote if present
  2601.         if (substr($cmdstrsrc-11=== $m[1]{
  2602.             $cmdstrsrc substr($cmdstrsrc0-1);
  2603.             $add $m[1];
  2604.         }
  2605.  
  2606.         $output $m[2];
  2607.  
  2608.         $continue true;
  2609.         while (strlen($cmdstrsrc&& $continue{
  2610.             if ($cmdstrsrc[0=== '|'{
  2611.                 $cmdstrsrc substr($cmdstrsrc1);
  2612.                 continue;
  2613.             }
  2614.             if ($cmdstrsrc[0=== ' ' || $cmdstrsrc[0=== ';' || substr($cmdstrsrc0strlen($this->rd)) === $this->rd{
  2615.                 if ($this->debugecho 'MODIFIER PARSING ENDED, RIGHT DELIMITER or ";" FOUND<br/>';
  2616.                 $continue false;
  2617.                 if ($pointer !== null{
  2618.                     $pointer -= strlen($cmdstrsrc);
  2619.                 }
  2620.                 break;
  2621.             }
  2622.             $cmdstr $cmdstrsrc;
  2623.             $paramsep ':';
  2624.             if (!preg_match('/^(@{0,2}[a-z][a-z0-9_]*)(:)?/i'$cmdstr$match)) {
  2625.                 throw new Dwoo_Compilation_Exception($this'Invalid modifier name, started with : '.substr($cmdstr010));
  2626.             }
  2627.             $paramspos !empty($match[2]strlen($match[1]false;
  2628.             $func $match[1];
  2629.  
  2630.             $state 0;
  2631.             if ($paramspos === false{
  2632.                 $cmdstrsrc substr($cmdstrsrcstrlen($func));
  2633.                 $params array();
  2634.                 if ($this->debugecho 'MODIFIER ('.$func.') CALLED WITH NO PARAMS<br/>';
  2635.             else {
  2636.                 $paramstr substr($cmdstr$paramspos+1);
  2637.                 if (substr($paramstr-11=== $paramsep{
  2638.                     $paramstr substr($paramstr0-1);
  2639.                 }
  2640.  
  2641.                 $ptr 0;
  2642.                 $params array();
  2643.                 while ($ptr strlen($paramstr)) {
  2644.                     if ($this->debugecho 'MODIFIER ('.$func.') START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>';
  2645.                     if ($this->debugecho $paramstr.'--'.$ptr.'--'.strlen($paramstr).'--modifier<br/>';
  2646.                     $params $this->parse($paramstr$ptrstrlen($paramstr)$params'modifier'$ptr);
  2647.                     if ($this->debugecho 'PARAM PARSED, POINTER AT '.$ptr.'<br/>';
  2648.  
  2649.                     if ($ptr >= strlen($paramstr)) {
  2650.                         if ($this->debugecho 'PARAM PARSING ENDED, PARAM STRING CONSUMED<br/>';
  2651.                         break;
  2652.                     }
  2653.  
  2654.                     if ($paramstr[$ptr=== ' ' || $paramstr[$ptr=== '|' || $paramstr[$ptr=== ';' || substr($paramstr$ptrstrlen($this->rd)) === $this->rd{
  2655.                         if ($this->debugecho 'PARAM PARSING ENDED, " ", "|", RIGHT DELIMITER or ";" FOUND, POINTER AT '.$ptr.'<br/>';
  2656.                         if ($paramstr[$ptr!== '|'{
  2657.                             $continue false;
  2658.                             if ($pointer !== null{
  2659.                                 $pointer -= strlen($paramstr$ptr;
  2660.                             }
  2661.                         }
  2662.                         $ptr++;
  2663.                         break;
  2664.                     }
  2665.                     if ($ptr strlen($paramstr&& $paramstr[$ptr=== ':'{
  2666.                         $ptr++;
  2667.                     }
  2668.                 }
  2669.                 $cmdstrsrc substr($cmdstrsrcstrlen($func)+1+$ptr);
  2670.                 $paramstr substr($paramstr0$ptr);
  2671.                 foreach ($params as $k=>$p{
  2672.                     if (is_array($p&& is_array($p[1])) {
  2673.                         $state |= 2;
  2674.                     else {
  2675.                         if (($state 2&& preg_match('#^(["\'])(.+?)\1$#'$p[0]$m)) {
  2676.                             $params[$karray($m[2]array('true''true'));
  2677.                         else {
  2678.                             if ($state 2{
  2679.                                 throw new Dwoo_Compilation_Exception($this'You can not use an unnamed parameter after a named one');
  2680.                             }
  2681.                             $state |= 1;
  2682.                         }
  2683.                     }
  2684.                 }
  2685.             }
  2686.  
  2687.             // check if we must use array_map with this plugin or not
  2688.             $mapped false;
  2689.             if (substr($func01=== '@'{
  2690.                 $func substr($func1);
  2691.                 $mapped true;
  2692.             }
  2693.  
  2694.             $pluginType $this->getPluginType($func);
  2695.  
  2696.             if ($state 2{
  2697.                 array_unshift($paramsarray('value'array($output$output)));
  2698.             else {
  2699.                 array_unshift($paramsarray($output$output));
  2700.             }
  2701.  
  2702.             if ($pluginType Dwoo::NATIVE_PLUGIN{
  2703.                 $params $this->mapParams($paramsnull$state);
  2704.  
  2705.                 $params $params['*'][0];
  2706.  
  2707.                 $params self::implode_r($params);
  2708.  
  2709.                 if ($mapped{
  2710.                     $output '$this->arrayMap(\''.$func.'\', array('.$params.'))';
  2711.                 else {
  2712.                     $output $func.'('.$params.')';
  2713.                 }
  2714.             elseif ($pluginType Dwoo::PROXY_PLUGIN{
  2715.                 $params $this->mapParams($params$this->getDwoo()->getPluginProxy()->getCallback($func)$state);
  2716.                 foreach ($params as &$p)
  2717.                     $p $p[0];
  2718.                 $output call_user_func(array($this->dwoo->getPluginProxy()'getCode')$func$params);
  2719.             elseif ($pluginType Dwoo::SMARTY_MODIFIER{
  2720.                 $params $this->mapParams($paramsnull$state);
  2721.                 $params $params['*'][0];
  2722.  
  2723.                 $params self::implode_r($params);
  2724.  
  2725.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  2726.                     $callback $this->customPlugins[$func]['callback'];
  2727.                     if (is_array($callback)) {
  2728.                         if (is_object($callback[0])) {
  2729.                             $output ($mapped '$this->arrayMap' 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))';
  2730.                         else {
  2731.                             $output ($mapped '$this->arrayMap' 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))';
  2732.                         }
  2733.                     elseif ($mapped{
  2734.                         $output '$this->arrayMap(\''.$callback.'\', array('.$params.'))';
  2735.                     else {
  2736.                         $output $callback.'('.$params.')';
  2737.                     }
  2738.                 elseif ($mapped{
  2739.                     $output '$this->arrayMap(\'smarty_modifier_'.$func.'\', array('.$params.'))';
  2740.                 else {
  2741.                     $output 'smarty_modifier_'.$func.'('.$params.')';
  2742.                 }
  2743.             else {
  2744.                 if ($pluginType Dwoo::CUSTOM_PLUGIN{
  2745.                     $callback $this->customPlugins[$func]['callback'];
  2746.                     $pluginName $callback;
  2747.                 else {
  2748.                     $pluginName 'Dwoo_Plugin_'.$func;
  2749.  
  2750.                     if ($pluginType Dwoo::CLASS_PLUGIN{
  2751.                         $callback array($pluginName($pluginType Dwoo::COMPILABLE_PLUGIN'compile' 'process');
  2752.                     else {
  2753.                         $callback $pluginName (($pluginType Dwoo::COMPILABLE_PLUGIN'_compile' '');
  2754.                     }
  2755.                 }
  2756.  
  2757.                 $params $this->mapParams($params$callback$state);
  2758.  
  2759.                 foreach ($params as &$p)
  2760.                     $p $p[0];
  2761.  
  2762.                 if ($pluginType Dwoo::FUNC_PLUGIN{
  2763.                     if ($pluginType Dwoo::COMPILABLE_PLUGIN{
  2764.                         if ($mapped{
  2765.                             throw new Dwoo_Compilation_Exception($this'The @ operator can not be used on compiled plugins.');
  2766.                         }
  2767.                         if ($pluginType Dwoo::CUSTOM_PLUGIN{
  2768.                             $funcCompiler $this->customPlugins[$func]['callback'];
  2769.                         else {
  2770.                             $funcCompiler 'Dwoo_Plugin_'.$func.'_compile';
  2771.                         }
  2772.                         array_unshift($params$this);
  2773.                         $output call_user_func_array($funcCompiler$params);
  2774.                     else {
  2775.                         array_unshift($params'$this');
  2776.  
  2777.                         $params self::implode_r($params);
  2778.                         if ($mapped{
  2779.                             $output '$this->arrayMap(\''.$pluginName.'\', array('.$params.'))';
  2780.                         else {
  2781.                             $output $pluginName.'('.$params.')';
  2782.                         }
  2783.                     }
  2784.                 else {
  2785.                     if ($pluginType Dwoo::COMPILABLE_PLUGIN{
  2786.                         if ($mapped{
  2787.                             throw new Dwoo_Compilation_Exception($this'The @ operator can not be used on compiled plugins.');
  2788.                         }
  2789.                         if ($pluginType Dwoo::CUSTOM_PLUGIN{
  2790.                             $callback $this->customPlugins[$func]['callback'];
  2791.                             if (!is_array($callback)) {
  2792.                                 if (!method_exists($callback'compile')) {
  2793.                                     throw new Dwoo_Exception('Custom plugin '.$func.' must implement the "compile" method to be compilable, or you should provide a full callback to the method to use');
  2794.                                 }
  2795.                                 if (($ref new ReflectionMethod($callback'compile')) && $ref->isStatic()) {
  2796.                                     $funcCompiler array($callback'compile');
  2797.                                 else {
  2798.                                     $funcCompiler array(new $callback'compile');
  2799.                                 }
  2800.                             else {
  2801.                                 $funcCompiler $callback;
  2802.                             }
  2803.                         else {
  2804.                             $funcCompiler array('Dwoo_Plugin_'.$func'compile');
  2805.                             array_unshift($params$this);
  2806.                         }
  2807.                         $output call_user_func_array($funcCompiler$params);
  2808.                     else {
  2809.                         $params self::implode_r($params);
  2810.  
  2811.                         if ($pluginType Dwoo::CUSTOM_PLUGIN{
  2812.                             if (is_object($callback[0])) {
  2813.                                 $output ($mapped '$this->arrayMap' 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))';
  2814.                             else {
  2815.                                 $output ($mapped '$this->arrayMap' 'call_user_func_array').'(array(\''.$callback[0].'\', \''.$callback[1].'\'), array('.$params.'))';
  2816.                             }
  2817.                         elseif ($mapped{
  2818.                             $output '$this->arrayMap(array($this->getObjectPlugin(\'Dwoo_Plugin_'.$func.'\'), \'process\'), array('.$params.'))';
  2819.                         else {
  2820.                             $output '$this->classCall(\''.$func.'\', array('.$params.'))';
  2821.                         }
  2822.                     }
  2823.                 }
  2824.             }
  2825.         }
  2826.  
  2827.         if ($curBlock === 'var' || $m[1=== null{
  2828.             return $output;
  2829.         elseif ($curBlock === 'string' || $curBlock === 'root'{
  2830.             return $m[1].'.'.$output.'.'.$m[1].(isset($add)?$add:null);
  2831.         }
  2832.     }
  2833.  
  2834.     /**
  2835.      * recursively implodes an array in a similar manner as var_export() does but with some tweaks
  2836.      * to handle pre-compiled values and the fact that we do not need to enclose everything with
  2837.      * "array" and do not require top-level keys to be displayed
  2838.      *
  2839.      * @param array $params the array to implode
  2840.      * @param bool $recursiveCall if set to true, the function outputs key names for the top level
  2841.      * @return string the imploded array
  2842.      */
  2843.     public static function implode_r(array $params$recursiveCall false)
  2844.     {
  2845.         $out '';
  2846.         foreach ($params as $k=>$p{
  2847.             if (is_array($p)) {
  2848.                 $out2 'array(';
  2849.                 foreach ($p as $k2=>$v)
  2850.                     $out2 .= var_export($k2true).' => '.(is_array($v'array('.self::implode_r($vtrue).')' $v).', ';
  2851.                 $p rtrim($out2', ').')';
  2852.             }
  2853.             if ($recursiveCall{
  2854.                 $out .= var_export($ktrue).' => '.$p.', ';
  2855.             else {
  2856.                 $out .= $p.', ';
  2857.             }
  2858.         }
  2859.         return rtrim($out', ');
  2860.     }
  2861.  
  2862.     /**
  2863.      * returns the plugin type of a plugin and adds it to the used plugins array if required
  2864.      *
  2865.      * @param string $name plugin name, as found in the template
  2866.      * @return int type as a multi bit flag composed of the Dwoo plugin types constants
  2867.      */
  2868.     protected function getPluginType($name)
  2869.     {
  2870.         $pluginType = -1;
  2871.  
  2872.         if (($this->securityPolicy === null && (function_exists($name|| strtolower($name=== 'isset' || strtolower($name=== 'empty')) ||
  2873.             ($this->securityPolicy !== null && in_array(strtolower($name)$this->securityPolicy->getAllowedPhpFunctions()) !== false)) {
  2874.             $phpFunc true;
  2875.         }
  2876.  
  2877.         while ($pluginType <= 0{
  2878.             if (isset($this->templatePlugins[$name])) {
  2879.                 $pluginType Dwoo::TEMPLATE_PLUGIN Dwoo::COMPILABLE_PLUGIN;
  2880.             elseif (isset($this->customPlugins[$name])) {
  2881.                 $pluginType $this->customPlugins[$name]['type'Dwoo::CUSTOM_PLUGIN;
  2882.             elseif (class_exists('Dwoo_Plugin_'.$namefalse!== false{
  2883.                 if (is_subclass_of('Dwoo_Plugin_'.$name'Dwoo_Block_Plugin')) {
  2884.                     $pluginType Dwoo::BLOCK_PLUGIN;
  2885.                 else {
  2886.                     $pluginType Dwoo::CLASS_PLUGIN;
  2887.                 }
  2888.                 $interfaces class_implements('Dwoo_Plugin_'.$namefalse);
  2889.                 if (in_array('Dwoo_ICompilable'$interfaces!== false || in_array('Dwoo_ICompilable_Block'$interfaces!== false{
  2890.                     $pluginType |= Dwoo::COMPILABLE_PLUGIN;
  2891.                 }
  2892.             elseif (function_exists('Dwoo_Plugin_'.$name!== false{
  2893.                 $pluginType Dwoo::FUNC_PLUGIN;
  2894.             elseif (function_exists('Dwoo_Plugin_'.$name.'_compile')) {
  2895.                 $pluginType Dwoo::FUNC_PLUGIN Dwoo::COMPILABLE_PLUGIN;
  2896.             elseif (function_exists('smarty_modifier_'.$name!== false{
  2897.                 $pluginType Dwoo::SMARTY_MODIFIER;
  2898.             elseif (function_exists('smarty_function_'.$name!== false{
  2899.                 $pluginType Dwoo::SMARTY_FUNCTION;
  2900.             elseif (function_exists('smarty_block_'.$name!== false{
  2901.                 $pluginType Dwoo::SMARTY_BLOCK;
  2902.             else {
  2903.                 if ($pluginType===-1{
  2904.                     try {
  2905.                         $this->dwoo->getLoader()->loadPlugin($nameisset($phpFunc)===false);
  2906.                     catch (Exception $e{
  2907.                         if (isset($phpFunc)) {
  2908.                             $pluginType Dwoo::NATIVE_PLUGIN;
  2909.                         elseif (is_object($this->dwoo->getPluginProxy()) && $this->dwoo->getPluginProxy()->handles($name)) {
  2910.                             $pluginType Dwoo::PROXY_PLUGIN;
  2911.                             break;
  2912.                         else {
  2913.                             throw $e;
  2914.                         }
  2915.                     }
  2916.                 else {
  2917.                     throw new Dwoo_Exception('Plugin "'.$name.'" could not be found');
  2918.                 }
  2919.                 $pluginType++;
  2920.             }
  2921.         }
  2922.  
  2923.         if (($pluginType Dwoo::COMPILABLE_PLUGIN=== && ($pluginType Dwoo::NATIVE_PLUGIN=== && ($pluginType Dwoo::PROXY_PLUGIN=== 0{
  2924.             $this->addUsedPlugin($name$pluginType);
  2925.         }
  2926.  
  2927.         return $pluginType;
  2928.     }
  2929.  
  2930.     /**
  2931.      * allows a plugin to load another one at compile time, this will also mark
  2932.      * it as used by this template so it will be loaded at runtime (which can be
  2933.      * useful for compiled plugins that rely on another plugin when their compiled
  2934.      * code runs)
  2935.      *
  2936.      * @param string $name the plugin name
  2937.      */
  2938.     public function loadPlugin($name{
  2939.         $this->getPluginType($name);
  2940.     }
  2941.  
  2942.     /**
  2943.      * runs htmlentities over the matched <?php ?> blocks when the security policy enforces that
  2944.      *
  2945.      * @param array $match matched php block
  2946.      * @return string the htmlentities-converted string
  2947.      */
  2948.     protected function phpTagEncodingHelper($match)
  2949.     {
  2950.         return htmlspecialchars($match[0]);
  2951.     }
  2952.  
  2953.     /**
  2954.      * maps the parameters received from the template onto the parameters required by the given callback
  2955.      *
  2956.      * @param array $params the array of parameters
  2957.      * @param callback $callback the function or method to reflect on to find out the required parameters
  2958.      * @param int $callType the type of call in the template, 0 = no params, 1 = php-style call, 2 = named parameters call
  2959.      * @param array $map the parameter map to use, if not provided it will be built from the callback
  2960.      * @return array parameters sorted in the correct order with missing optional parameters filled
  2961.      */
  2962.     protected function mapParams(array $params$callback$callType=2$map null)
  2963.     {
  2964.         if (!$map{
  2965.             $map $this->getParamMap($callback);
  2966.         }
  2967.  
  2968.         $paramlist array();
  2969.  
  2970.         // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values))
  2971.         $ps array();
  2972.         foreach ($params as $p{
  2973.             if (is_array($p[1])) {
  2974.                 $ps[$p[0]] $p[1];
  2975.             else {
  2976.                 $ps[$p;
  2977.             }
  2978.         }
  2979.  
  2980.         // loops over the param map and assigns values from the template or default value for unset optional params
  2981.         while (list($k,$veach($map)) {
  2982.             if ($v[0=== '*'{
  2983.                 // "rest" array parameter, fill every remaining params in it and then break
  2984.                 if (count($ps=== 0{
  2985.                     if ($v[1]===false{
  2986.                         throw new Dwoo_Compilation_Exception($this'Rest argument missing for '.str_replace(array('Dwoo_Plugin_''_compile')''(is_array($callback$callback[0$callback)));
  2987.                     else {
  2988.                         break;
  2989.                     }
  2990.                 }
  2991.                 $tmp array();
  2992.                 $tmp2 array();
  2993.                 foreach ($ps as $i=>$p{
  2994.                     $tmp[$i$p[0];
  2995.                     $tmp2[$i$p[1];
  2996.                     unset($ps[$i]);
  2997.                 }
  2998.                 $paramlist[$v[0]] array($tmp$tmp2);
  2999.                 unset($tmp$tmp2$i$p);
  3000.                 break;
  3001.             elseif (isset($ps[$v[0]])) {
  3002.                 // parameter is defined as named param
  3003.                 $paramlist[$v[0]] $ps[$v[0]];
  3004.                 unset($ps[$v[0]]);
  3005.             elseif (isset($ps[$k])) {
  3006.                 // parameter is defined as ordered param
  3007.                 $paramlist[$v[0]] $ps[$k];
  3008.                 unset($ps[$k]);
  3009.             elseif ($v[1]===false{
  3010.                 // parameter is not defined and not optional, throw error
  3011.                 if (is_array($callback)) {
  3012.                     if (is_object($callback[0])) {
  3013.                         $name get_class($callback[0]'::' $callback[1];
  3014.                     else {
  3015.                         $name $callback[0];
  3016.                     }
  3017.                 else {
  3018.                     $name $callback;
  3019.                 }
  3020.  
  3021.                 throw new Dwoo_Compilation_Exception($this'Argument '.$k.'/'.$v[0].' missing for '.str_replace(array('Dwoo_Plugin_''_compile')''$name));
  3022.             elseif ($v[2]===null{
  3023.                 // enforce lowercased null if default value is null (php outputs NULL with var export)
  3024.                 $paramlist[$v[0]] array('null'null);
  3025.             else {
  3026.                 // outputs default value with var_export
  3027.                 $paramlist[$v[0]] array(var_export($v[2]true)$v[2]);
  3028.             }
  3029.         }
  3030.  
  3031.         if (count($ps)) {
  3032.             foreach ($ps as $i=>$p{
  3033.                 array_push($paramlist$p);
  3034.             }
  3035.         }
  3036.  
  3037.         return $paramlist;
  3038.     }
  3039.  
  3040.     /**
  3041.      * returns the parameter map of the given callback, it filters out entries typed as Dwoo and Dwoo_Compiler and turns the rest parameter into a "*"
  3042.      *
  3043.      * @param callback $callback the function/method to reflect on
  3044.      * @return array processed parameter map
  3045.      */
  3046.     protected function getParamMap($callback)
  3047.     {
  3048.         if (is_null($callback)) {
  3049.             return array(array('*'true));
  3050.         }
  3051.         if (is_array($callback)) {
  3052.             $ref new ReflectionMethod($callback[0]$callback[1]);
  3053.         else {
  3054.             $ref new ReflectionFunction($callback);
  3055.         }
  3056.  
  3057.         $out array();
  3058.         foreach ($ref->getParameters(as $param{
  3059.             if (($class $param->getClass()) !== null && $class->name === 'Dwoo'{
  3060.                 continue;
  3061.             }
  3062.             if (($class $param->getClass()) !== null && $class->name === 'Dwoo_Compiler'{
  3063.                 continue;
  3064.             }
  3065.             if ($param->getName(=== 'rest' && $param->isArray(=== true{
  3066.                 $out[array('*'$param->isOptional()null);
  3067.             }
  3068.             $out[array($param->getName()$param->isOptional()$param->isOptional($param->getDefaultValue(null);
  3069.         }
  3070.  
  3071.         return $out;
  3072.     }
  3073.  
  3074.     /**
  3075.      * returns a default instance of this compiler, used by default by all Dwoo templates that do not have a
  3076.      * specific compiler assigned and when you do not override the default compiler factory function
  3077.      *
  3078.      * @see Dwoo::setDefaultCompilerFactory()
  3079.      * @return Dwoo_Compiler 
  3080.      */
  3081.     public static function compilerFactory()
  3082.     {
  3083.         if (self::$instance === null{
  3084.             new self;
  3085.         }
  3086.         return self::$instance;
  3087.     }
  3088. }

Documentation generated on Sun, 07 Feb 2010 17:53:29 +0000 by phpDocumentor 1.4.0