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

Documentation generated on Wed, 24 Dec 2008 02:13:22 +0100 by phpDocumentor 1.4.0