| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |  * Smarty Internal Plugin CompileBase | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage Compiler | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @author Uwe Tews | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |  * This class does extend all internal compile plugins | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage Compiler | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | abstract class Smarty_Internal_CompileBase { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Array of names of required attribute required by tag | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $required_attributes = array(); | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Array of names of optional attribute required by tag | 
					
						
							|  |  |  |      * use array('_any') if there is no restriction of attributes names | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     public $optional_attributes = array(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Shorttag attribute order defined by its names | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     public $shorttag_order = array(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Array of names of valid option flags | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     public $option_flags = array('nocache'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * This function checks if the attributes passed are valid | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * The attributes passed for the tag to compile are checked against the list of required and | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * optional attributes. Required attributes must be present. Optional attributes are check against | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * the corresponding list. The keyword '_any' specifies that any attribute will be accepted | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * as valid | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param object $compiler   compiler object | 
					
						
							|  |  |  |      * @param array  $attributes attributes applied to the tag | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * @return array of mapped attributes for further processing | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function getAttributes($compiler, $attributes) | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $_indexed_attr = array(); | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |         // loop over attributes
 | 
					
						
							|  |  |  |         foreach ($attributes as $key => $mixed) { | 
					
						
							|  |  |  |             // shorthand ?
 | 
					
						
							|  |  |  |             if (!is_array($mixed)) { | 
					
						
							|  |  |  |                 // option flag ?
 | 
					
						
							|  |  |  |                 if (in_array(trim($mixed, '\'"'), $this->option_flags)) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                     $_indexed_attr[trim($mixed, '\'"')] = true; | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                     // shorthand attribute ?
 | 
					
						
							|  |  |  |                 } else if (isset($this->shorttag_order[$key])) { | 
					
						
							|  |  |  |                     $_indexed_attr[$this->shorttag_order[$key]] = $mixed; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     // too many shorthands
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                     $compiler->trigger_template_error('too many shorthand attributes', $compiler->lex->taglineno); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                 // named attribute
 | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 $kv = each($mixed); | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                 // option flag?
 | 
					
						
							|  |  |  |                 if (in_array($kv['key'], $this->option_flags)) { | 
					
						
							|  |  |  |                     if (is_bool($kv['value'])) { | 
					
						
							|  |  |  |                         $_indexed_attr[$kv['key']] = $kv['value']; | 
					
						
							|  |  |  |                     } else if (is_string($kv['value']) && in_array(trim($kv['value'], '\'"'), array('true', 'false'))) { | 
					
						
							|  |  |  |                         if (trim($kv['value']) == 'true') { | 
					
						
							|  |  |  |                             $_indexed_attr[$kv['key']] = true; | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             $_indexed_attr[$kv['key']] = false; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                     } else if (is_numeric($kv['value']) && in_array($kv['value'], array(0, 1))) { | 
					
						
							|  |  |  |                         if ($kv['value'] == 1) { | 
					
						
							|  |  |  |                             $_indexed_attr[$kv['key']] = true; | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             $_indexed_attr[$kv['key']] = false; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                         $compiler->trigger_template_error("illegal value of option flag \"{$kv['key']}\"", $compiler->lex->taglineno); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |                     // must be named attribute
 | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                     reset($mixed); | 
					
						
							| 
									
										
										
										
											2010-11-23 23:36:16 +00:00
										 |  |  |                     $_indexed_attr[key($mixed)] = $mixed[key($mixed)]; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // check if all required attributes present
 | 
					
						
							|  |  |  |         foreach ($this->required_attributes as $attr) { | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |             if (!array_key_exists($attr, $_indexed_attr)) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 $compiler->trigger_template_error("missing \"" . $attr . "\" attribute", $compiler->lex->taglineno); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // check for unallowed attributes
 | 
					
						
							|  |  |  |         if ($this->optional_attributes != array('_any')) { | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |             $tmp_array = array_merge($this->required_attributes, $this->optional_attributes, $this->option_flags); | 
					
						
							|  |  |  |             foreach ($_indexed_attr as $key => $dummy) { | 
					
						
							|  |  |  |                 if (!in_array($key, $tmp_array) && $key !== 0) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                     $compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", $compiler->lex->taglineno); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |         // default 'false' for all option flags not set
 | 
					
						
							|  |  |  |         foreach ($this->option_flags as $flag) { | 
					
						
							|  |  |  |             if (!isset($_indexed_attr[$flag])) { | 
					
						
							|  |  |  |                 $_indexed_attr[$flag] = false; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |         return $_indexed_attr; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * Push opening tag name on stack | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * Optionally additional data can be saved on stack | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param object    $compiler   compiler object | 
					
						
							|  |  |  |      * @param string    $openTag    the opening tag's name | 
					
						
							|  |  |  |      * @param mixed     $data       optional data saved | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function openTag($compiler, $openTag, $data = null) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         array_push($compiler->_tag_stack, array($openTag, $data)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * Pop closing tag | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * Raise an error if this stack-top doesn't match with expected opening tags | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param object       $compiler    compiler object | 
					
						
							|  |  |  |      * @param array|string $expectedTag the expected opening tag names | 
					
						
							|  |  |  |      * @return mixed any type the opening tag's name or saved data | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function closeTag($compiler, $expectedTag) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         if (count($compiler->_tag_stack) > 0) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             // get stacked info
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             list($_openTag, $_data) = array_pop($compiler->_tag_stack); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             // open tag must match with the expected ones
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             if (in_array($_openTag, (array) $expectedTag)) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 if (is_null($_data)) { | 
					
						
							|  |  |  |                     // return opening tag
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                     return $_openTag; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     // return restored data
 | 
					
						
							|  |  |  |                     return $_data; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             // wrong nesting of tags
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             $compiler->trigger_template_error("unclosed {" . $_openTag . "} tag"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // wrong nesting of tags
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $compiler->trigger_template_error("unexpected closing tag", $compiler->lex->taglineno); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-06 20:00:41 +00:00
										 |  |  | ?>
 |