1<?php 2/** 3 * Smarty Internal Plugin CompileBase 4 * 5 * @package Smarty 6 * @subpackage Compiler 7 * @author Uwe Tews 8 */ 9 10/** 11 * This class does extend all internal compile plugins 12 * 13 * @package Smarty 14 * @subpackage Compiler 15 */ 16abstract class Smarty_Internal_CompileBase 17{ 18 /** 19 * Array of names of required attribute required by tag 20 * 21 * @var array 22 */ 23 public $required_attributes = array(); 24 25 /** 26 * Array of names of optional attribute required by tag 27 * use array('_any') if there is no restriction of attributes names 28 * 29 * @var array 30 */ 31 public $optional_attributes = array(); 32 33 /** 34 * Shorttag attribute order defined by its names 35 * 36 * @var array 37 */ 38 public $shorttag_order = array(); 39 40 /** 41 * Array of names of valid option flags 42 * 43 * @var array 44 */ 45 public $option_flags = array('nocache'); 46 47 /** 48 * Mapping array for boolean option value 49 * 50 * @var array 51 */ 52 public $optionMap = array(1 => true, 0 => false, 'true' => true, 'false' => false); 53 54 /** 55 * Mapping array with attributes as key 56 * 57 * @var array 58 */ 59 public $mapCache = array(); 60 61 /** 62 * This function checks if the attributes passed are valid 63 * The attributes passed for the tag to compile are checked against the list of required and 64 * optional attributes. Required attributes must be present. Optional attributes are check against 65 * the corresponding list. The keyword '_any' specifies that any attribute will be accepted 66 * as valid 67 * 68 * @param object $compiler compiler object 69 * @param array $attributes attributes applied to the tag 70 * 71 * @return array of mapped attributes for further processing 72 */ 73 public function getAttributes($compiler, $attributes) 74 { 75 $_indexed_attr = array(); 76 if (!isset($this->mapCache[ 'option' ])) { 77 $this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true); 78 } 79 foreach ($attributes as $key => $mixed) { 80 // shorthand ? 81 if (!is_array($mixed)) { 82 // option flag ? 83 if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) { 84 $_indexed_attr[ trim($mixed, '\'"') ] = true; 85 // shorthand attribute ? 86 } elseif (isset($this->shorttag_order[ $key ])) { 87 $_indexed_attr[ $this->shorttag_order[ $key ] ] = $mixed; 88 } else { 89 // too many shorthands 90 $compiler->trigger_template_error('too many shorthand attributes', null, true); 91 } 92 // named attribute 93 } else { 94 foreach ($mixed as $k => $v) { 95 // option flag? 96 if (isset($this->mapCache[ 'option' ][ $k ])) { 97 if (is_bool($v)) { 98 $_indexed_attr[ $k ] = $v; 99 } else { 100 if (is_string($v)) { 101 $v = trim($v, '\'" '); 102 } 103 if (isset($this->optionMap[ $v ])) { 104 $_indexed_attr[ $k ] = $this->optionMap[ $v ]; 105 } else { 106 $compiler->trigger_template_error("illegal value '" . var_export($v, true) . 107 "' for option flag '{$k}'", null, true); 108 } 109 } 110 // must be named attribute 111 } else { 112 $_indexed_attr[ $k ] = $v; 113 } 114 } 115 } 116 } 117 // check if all required attributes present 118 foreach ($this->required_attributes as $attr) { 119 if (!isset($_indexed_attr[ $attr ])) { 120 $compiler->trigger_template_error("missing '{$attr}' attribute", null, true); 121 } 122 } 123 // check for not allowed attributes 124 if ($this->optional_attributes !== array('_any')) { 125 if (!isset($this->mapCache[ 'all' ])) { 126 $this->mapCache[ 'all' ] = 127 array_fill_keys(array_merge($this->required_attributes, $this->optional_attributes, 128 $this->option_flags), true); 129 } 130 foreach ($_indexed_attr as $key => $dummy) { 131 if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) { 132 $compiler->trigger_template_error("unexpected '{$key}' attribute", null, true); 133 } 134 } 135 } 136 // default 'false' for all option flags not set 137 foreach ($this->option_flags as $flag) { 138 if (!isset($_indexed_attr[ $flag ])) { 139 $_indexed_attr[ $flag ] = false; 140 } 141 } 142 if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) { 143 $compiler->tag_nocache = true; 144 } 145 return $_indexed_attr; 146 } 147 148 /** 149 * Push opening tag name on stack 150 * Optionally additional data can be saved on stack 151 * 152 * @param object $compiler compiler object 153 * @param string $openTag the opening tag's name 154 * @param mixed $data optional data saved 155 */ 156 public function openTag($compiler, $openTag, $data = null) 157 { 158 array_push($compiler->_tag_stack, array($openTag, $data)); 159 } 160 161 /** 162 * Pop closing tag 163 * Raise an error if this stack-top doesn't match with expected opening tags 164 * 165 * @param object $compiler compiler object 166 * @param array|string $expectedTag the expected opening tag names 167 * 168 * @return mixed any type the opening tag's name or saved data 169 */ 170 public function closeTag($compiler, $expectedTag) 171 { 172 if (count($compiler->_tag_stack) > 0) { 173 // get stacked info 174 list($_openTag, $_data) = array_pop($compiler->_tag_stack); 175 // open tag must match with the expected ones 176 if (in_array($_openTag, (array) $expectedTag)) { 177 if (is_null($_data)) { 178 // return opening tag 179 return $_openTag; 180 } else { 181 // return restored data 182 return $_data; 183 } 184 } 185 // wrong nesting of tags 186 $compiler->trigger_template_error("unclosed '{$compiler->smarty->left_delimiter}{$_openTag}{$compiler->smarty->right_delimiter}' tag"); 187 188 return; 189 } 190 // wrong nesting of tags 191 $compiler->trigger_template_error('unexpected closing tag', null, true); 192 193 return; 194 } 195} 196