@@ -25,298 +25,298 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class Policy |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * Php handling constants, defaults to PHP_REMOVE |
|
| 30 | - * PHP_ENCODE : run htmlentities over them |
|
| 31 | - * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template |
|
| 32 | - * PHP_ALLOW : leave them as they are |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - const PHP_ENCODE = 1; |
|
| 37 | - const PHP_REMOVE = 2; |
|
| 38 | - const PHP_ALLOW = 3; |
|
| 28 | + /** |
|
| 29 | + * Php handling constants, defaults to PHP_REMOVE |
|
| 30 | + * PHP_ENCODE : run htmlentities over them |
|
| 31 | + * PHP_REMOVE : remove all <?php ?> (+ short tags if your short tags option is on) from the input template |
|
| 32 | + * PHP_ALLOW : leave them as they are |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + const PHP_ENCODE = 1; |
|
| 37 | + const PHP_REMOVE = 2; |
|
| 38 | + const PHP_ALLOW = 3; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Constant handling constants, defaults to CONST_DISALLOW |
|
| 42 | - * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template |
|
| 43 | - * CONST_ALLOW : allow {$dwoo.const.*} calls |
|
| 44 | - */ |
|
| 45 | - const CONST_DISALLOW = false; |
|
| 46 | - const CONST_ALLOW = true; |
|
| 40 | + /** |
|
| 41 | + * Constant handling constants, defaults to CONST_DISALLOW |
|
| 42 | + * CONST_DISALLOW : throw an error if {$dwoo.const.*} is used in the template |
|
| 43 | + * CONST_ALLOW : allow {$dwoo.const.*} calls |
|
| 44 | + */ |
|
| 45 | + const CONST_DISALLOW = false; |
|
| 46 | + const CONST_ALLOW = true; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Php functions that are allowed to be used within the template. |
|
| 50 | - * |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - protected $allowedPhpFunctions = array( |
|
| 54 | - 'str_repeat' => true, |
|
| 55 | - 'number_format' => true, |
|
| 56 | - 'htmlentities' => true, |
|
| 57 | - 'htmlspecialchars' => true, |
|
| 58 | - 'long2ip' => true, |
|
| 59 | - 'strlen' => true, |
|
| 60 | - 'list' => true, |
|
| 61 | - 'empty' => true, |
|
| 62 | - 'count' => true, |
|
| 63 | - 'sizeof' => true, |
|
| 64 | - 'in_array' => true, |
|
| 65 | - 'is_array' => true, |
|
| 66 | - ); |
|
| 48 | + /** |
|
| 49 | + * Php functions that are allowed to be used within the template. |
|
| 50 | + * |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + protected $allowedPhpFunctions = array( |
|
| 54 | + 'str_repeat' => true, |
|
| 55 | + 'number_format' => true, |
|
| 56 | + 'htmlentities' => true, |
|
| 57 | + 'htmlspecialchars' => true, |
|
| 58 | + 'long2ip' => true, |
|
| 59 | + 'strlen' => true, |
|
| 60 | + 'list' => true, |
|
| 61 | + 'empty' => true, |
|
| 62 | + 'count' => true, |
|
| 63 | + 'sizeof' => true, |
|
| 64 | + 'in_array' => true, |
|
| 65 | + 'is_array' => true, |
|
| 66 | + ); |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Methods that are allowed to be used within the template. |
|
| 70 | - * |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected $allowedMethods = array(); |
|
| 68 | + /** |
|
| 69 | + * Methods that are allowed to be used within the template. |
|
| 70 | + * |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected $allowedMethods = array(); |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Paths that are safe to use with include or other file-access plugins. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $allowedDirectories = array(); |
|
| 75 | + /** |
|
| 76 | + * Paths that are safe to use with include or other file-access plugins. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $allowedDirectories = array(); |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Stores the php handling level. |
|
| 84 | - * defaults to self::PHP_REMOVE |
|
| 85 | - * |
|
| 86 | - * @var int |
|
| 87 | - */ |
|
| 88 | - protected $phpHandling = self::PHP_REMOVE; |
|
| 82 | + /** |
|
| 83 | + * Stores the php handling level. |
|
| 84 | + * defaults to self::PHP_REMOVE |
|
| 85 | + * |
|
| 86 | + * @var int |
|
| 87 | + */ |
|
| 88 | + protected $phpHandling = self::PHP_REMOVE; |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Stores the constant handling level. |
|
| 92 | - * defaults to self::CONST_DISALLOW |
|
| 93 | - * |
|
| 94 | - * @var bool |
|
| 95 | - */ |
|
| 96 | - protected $constHandling = self::CONST_DISALLOW; |
|
| 90 | + /** |
|
| 91 | + * Stores the constant handling level. |
|
| 92 | + * defaults to self::CONST_DISALLOW |
|
| 93 | + * |
|
| 94 | + * @var bool |
|
| 95 | + */ |
|
| 96 | + protected $constHandling = self::CONST_DISALLOW; |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Adds a php function to the allowed list. |
|
| 100 | - * |
|
| 101 | - * @param mixed $func function name or array of function names |
|
| 102 | - */ |
|
| 103 | - public function allowPhpFunction($func) |
|
| 104 | - { |
|
| 105 | - if (is_array($func)) { |
|
| 106 | - foreach ($func as $fname) { |
|
| 107 | - $this->allowedPhpFunctions[strtolower($fname)] = true; |
|
| 108 | - } |
|
| 109 | - } else { |
|
| 110 | - $this->allowedPhpFunctions[strtolower($func)] = true; |
|
| 111 | - } |
|
| 112 | - } |
|
| 98 | + /** |
|
| 99 | + * Adds a php function to the allowed list. |
|
| 100 | + * |
|
| 101 | + * @param mixed $func function name or array of function names |
|
| 102 | + */ |
|
| 103 | + public function allowPhpFunction($func) |
|
| 104 | + { |
|
| 105 | + if (is_array($func)) { |
|
| 106 | + foreach ($func as $fname) { |
|
| 107 | + $this->allowedPhpFunctions[strtolower($fname)] = true; |
|
| 108 | + } |
|
| 109 | + } else { |
|
| 110 | + $this->allowedPhpFunctions[strtolower($func)] = true; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Removes a php function from the allowed list. |
|
| 116 | - * |
|
| 117 | - * @param mixed $func function name or array of function names |
|
| 118 | - */ |
|
| 119 | - public function disallowPhpFunction($func) |
|
| 120 | - { |
|
| 121 | - if (is_array($func)) { |
|
| 122 | - foreach ($func as $fname) { |
|
| 123 | - unset($this->allowedPhpFunctions[strtolower($fname)]); |
|
| 124 | - } |
|
| 125 | - } else { |
|
| 126 | - unset($this->allowedPhpFunctions[strtolower($func)]); |
|
| 127 | - } |
|
| 128 | - } |
|
| 114 | + /** |
|
| 115 | + * Removes a php function from the allowed list. |
|
| 116 | + * |
|
| 117 | + * @param mixed $func function name or array of function names |
|
| 118 | + */ |
|
| 119 | + public function disallowPhpFunction($func) |
|
| 120 | + { |
|
| 121 | + if (is_array($func)) { |
|
| 122 | + foreach ($func as $fname) { |
|
| 123 | + unset($this->allowedPhpFunctions[strtolower($fname)]); |
|
| 124 | + } |
|
| 125 | + } else { |
|
| 126 | + unset($this->allowedPhpFunctions[strtolower($func)]); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Returns the list of php functions allowed to run, note that the function names |
|
| 132 | - * are stored in the array keys and not values. |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - public function getAllowedPhpFunctions() |
|
| 137 | - { |
|
| 138 | - return $this->allowedPhpFunctions; |
|
| 139 | - } |
|
| 130 | + /** |
|
| 131 | + * Returns the list of php functions allowed to run, note that the function names |
|
| 132 | + * are stored in the array keys and not values. |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + public function getAllowedPhpFunctions() |
|
| 137 | + { |
|
| 138 | + return $this->allowedPhpFunctions; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Adds a class method to the allowed list, this must be used for |
|
| 143 | - * both static and non static method by providing the class name |
|
| 144 | - * and method name to use. |
|
| 145 | - * |
|
| 146 | - * @param mixed $class class name or array of array('class', 'method') couples |
|
| 147 | - * @param string $method method name |
|
| 148 | - */ |
|
| 149 | - public function allowMethod($class, $method = null) |
|
| 150 | - { |
|
| 151 | - if (is_array($class)) { |
|
| 152 | - foreach ($class as $elem) { |
|
| 153 | - $this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])] = true; |
|
| 154 | - } |
|
| 155 | - } else { |
|
| 156 | - $this->allowedMethods[strtolower($class)][strtolower($method)] = true; |
|
| 157 | - } |
|
| 158 | - } |
|
| 141 | + /** |
|
| 142 | + * Adds a class method to the allowed list, this must be used for |
|
| 143 | + * both static and non static method by providing the class name |
|
| 144 | + * and method name to use. |
|
| 145 | + * |
|
| 146 | + * @param mixed $class class name or array of array('class', 'method') couples |
|
| 147 | + * @param string $method method name |
|
| 148 | + */ |
|
| 149 | + public function allowMethod($class, $method = null) |
|
| 150 | + { |
|
| 151 | + if (is_array($class)) { |
|
| 152 | + foreach ($class as $elem) { |
|
| 153 | + $this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])] = true; |
|
| 154 | + } |
|
| 155 | + } else { |
|
| 156 | + $this->allowedMethods[strtolower($class)][strtolower($method)] = true; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Removes a class method from the allowed list. |
|
| 162 | - * |
|
| 163 | - * @param mixed $class class name or array of array('class', 'method') couples |
|
| 164 | - * @param string $method method name |
|
| 165 | - */ |
|
| 166 | - public function disallowMethod($class, $method = null) |
|
| 167 | - { |
|
| 168 | - if (is_array($class)) { |
|
| 169 | - foreach ($class as $elem) { |
|
| 170 | - unset($this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])]); |
|
| 171 | - } |
|
| 172 | - } else { |
|
| 173 | - unset($this->allowedMethods[strtolower($class)][strtolower($method)]); |
|
| 174 | - } |
|
| 175 | - } |
|
| 160 | + /** |
|
| 161 | + * Removes a class method from the allowed list. |
|
| 162 | + * |
|
| 163 | + * @param mixed $class class name or array of array('class', 'method') couples |
|
| 164 | + * @param string $method method name |
|
| 165 | + */ |
|
| 166 | + public function disallowMethod($class, $method = null) |
|
| 167 | + { |
|
| 168 | + if (is_array($class)) { |
|
| 169 | + foreach ($class as $elem) { |
|
| 170 | + unset($this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])]); |
|
| 171 | + } |
|
| 172 | + } else { |
|
| 173 | + unset($this->allowedMethods[strtolower($class)][strtolower($method)]); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Returns the list of class methods allowed to run, note that the class names |
|
| 179 | - * and method names are stored in the array keys and not values. |
|
| 180 | - * |
|
| 181 | - * @return array |
|
| 182 | - */ |
|
| 183 | - public function getAllowedMethods() |
|
| 184 | - { |
|
| 185 | - return $this->allowedMethods; |
|
| 186 | - } |
|
| 177 | + /** |
|
| 178 | + * Returns the list of class methods allowed to run, note that the class names |
|
| 179 | + * and method names are stored in the array keys and not values. |
|
| 180 | + * |
|
| 181 | + * @return array |
|
| 182 | + */ |
|
| 183 | + public function getAllowedMethods() |
|
| 184 | + { |
|
| 185 | + return $this->allowedMethods; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Adds a directory to the safelist for includes and other file-access plugins. |
|
| 190 | - * note that all the includePath directories you provide to the Dwoo_Template_File class |
|
| 191 | - * are automatically marked as safe |
|
| 192 | - * |
|
| 193 | - * @param mixed $path a path name or an array of paths |
|
| 194 | - */ |
|
| 195 | - public function allowDirectory($path) |
|
| 196 | - { |
|
| 197 | - if (is_array($path)) { |
|
| 198 | - foreach ($path as $dir) { |
|
| 199 | - $this->allowedDirectories[realpath($dir)] = true; |
|
| 200 | - } |
|
| 201 | - } else { |
|
| 202 | - $this->allowedDirectories[realpath($path)] = true; |
|
| 203 | - } |
|
| 204 | - } |
|
| 188 | + /** |
|
| 189 | + * Adds a directory to the safelist for includes and other file-access plugins. |
|
| 190 | + * note that all the includePath directories you provide to the Dwoo_Template_File class |
|
| 191 | + * are automatically marked as safe |
|
| 192 | + * |
|
| 193 | + * @param mixed $path a path name or an array of paths |
|
| 194 | + */ |
|
| 195 | + public function allowDirectory($path) |
|
| 196 | + { |
|
| 197 | + if (is_array($path)) { |
|
| 198 | + foreach ($path as $dir) { |
|
| 199 | + $this->allowedDirectories[realpath($dir)] = true; |
|
| 200 | + } |
|
| 201 | + } else { |
|
| 202 | + $this->allowedDirectories[realpath($path)] = true; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Removes a directory from the safe list. |
|
| 208 | - * |
|
| 209 | - * @param mixed $path a path name or an array of paths |
|
| 210 | - */ |
|
| 211 | - public function disallowDirectory($path) |
|
| 212 | - { |
|
| 213 | - if (is_array($path)) { |
|
| 214 | - foreach ($path as $dir) { |
|
| 215 | - unset($this->allowedDirectories[realpath($dir)]); |
|
| 216 | - } |
|
| 217 | - } else { |
|
| 218 | - unset($this->allowedDirectories[realpath($path)]); |
|
| 219 | - } |
|
| 220 | - } |
|
| 206 | + /** |
|
| 207 | + * Removes a directory from the safe list. |
|
| 208 | + * |
|
| 209 | + * @param mixed $path a path name or an array of paths |
|
| 210 | + */ |
|
| 211 | + public function disallowDirectory($path) |
|
| 212 | + { |
|
| 213 | + if (is_array($path)) { |
|
| 214 | + foreach ($path as $dir) { |
|
| 215 | + unset($this->allowedDirectories[realpath($dir)]); |
|
| 216 | + } |
|
| 217 | + } else { |
|
| 218 | + unset($this->allowedDirectories[realpath($path)]); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * Returns the list of safe paths, note that the paths are stored in the array |
|
| 224 | - * keys and not values. |
|
| 225 | - * |
|
| 226 | - * @return array |
|
| 227 | - */ |
|
| 228 | - public function getAllowedDirectories() |
|
| 229 | - { |
|
| 230 | - return $this->allowedDirectories; |
|
| 231 | - } |
|
| 222 | + /** |
|
| 223 | + * Returns the list of safe paths, note that the paths are stored in the array |
|
| 224 | + * keys and not values. |
|
| 225 | + * |
|
| 226 | + * @return array |
|
| 227 | + */ |
|
| 228 | + public function getAllowedDirectories() |
|
| 229 | + { |
|
| 230 | + return $this->allowedDirectories; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Sets the php handling level, defaults to REMOVE. |
|
| 235 | - * |
|
| 236 | - * @param int $level one of the Dwoo_Security_Policy::PHP_* constants |
|
| 237 | - */ |
|
| 238 | - public function setPhpHandling($level = self::PHP_REMOVE) |
|
| 239 | - { |
|
| 240 | - $this->phpHandling = $level; |
|
| 241 | - } |
|
| 233 | + /** |
|
| 234 | + * Sets the php handling level, defaults to REMOVE. |
|
| 235 | + * |
|
| 236 | + * @param int $level one of the Dwoo_Security_Policy::PHP_* constants |
|
| 237 | + */ |
|
| 238 | + public function setPhpHandling($level = self::PHP_REMOVE) |
|
| 239 | + { |
|
| 240 | + $this->phpHandling = $level; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Returns the php handling level. |
|
| 245 | - * |
|
| 246 | - * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants |
|
| 247 | - */ |
|
| 248 | - public function getPhpHandling() |
|
| 249 | - { |
|
| 250 | - return $this->phpHandling; |
|
| 251 | - } |
|
| 243 | + /** |
|
| 244 | + * Returns the php handling level. |
|
| 245 | + * |
|
| 246 | + * @return int the current level, one of the Dwoo_Security_Policy::PHP_* constants |
|
| 247 | + */ |
|
| 248 | + public function getPhpHandling() |
|
| 249 | + { |
|
| 250 | + return $this->phpHandling; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Sets the constant handling level, defaults to CONST_DISALLOW. |
|
| 255 | - * |
|
| 256 | - * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants |
|
| 257 | - */ |
|
| 258 | - public function setConstantHandling($level = self::CONST_DISALLOW) |
|
| 259 | - { |
|
| 260 | - $this->constHandling = $level; |
|
| 261 | - } |
|
| 253 | + /** |
|
| 254 | + * Sets the constant handling level, defaults to CONST_DISALLOW. |
|
| 255 | + * |
|
| 256 | + * @param bool $level one of the Dwoo_Security_Policy::CONST_* constants |
|
| 257 | + */ |
|
| 258 | + public function setConstantHandling($level = self::CONST_DISALLOW) |
|
| 259 | + { |
|
| 260 | + $this->constHandling = $level; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Returns the constant handling level. |
|
| 265 | - * |
|
| 266 | - * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants |
|
| 267 | - */ |
|
| 268 | - public function getConstantHandling() |
|
| 269 | - { |
|
| 270 | - return $this->constHandling; |
|
| 271 | - } |
|
| 263 | + /** |
|
| 264 | + * Returns the constant handling level. |
|
| 265 | + * |
|
| 266 | + * @return bool the current level, one of the Dwoo_Security_Policy::CONST_* constants |
|
| 267 | + */ |
|
| 268 | + public function getConstantHandling() |
|
| 269 | + { |
|
| 270 | + return $this->constHandling; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * This is used at run time to check whether method calls are allowed or not. |
|
| 275 | - * |
|
| 276 | - * @param Core $dwoo dwoo instance that calls this |
|
| 277 | - * @param object $obj any object on which the method must be called |
|
| 278 | - * @param string $method lowercased method name |
|
| 279 | - * @param array $args arguments array |
|
| 280 | - * |
|
| 281 | - * @return mixed result of method call or unll + E_USER_NOTICE if not allowed |
|
| 282 | - */ |
|
| 283 | - public function callMethod(Core $dwoo, $obj, $method, $args) |
|
| 284 | - { |
|
| 285 | - foreach ($this->allowedMethods as $class => $methods) { |
|
| 286 | - if (!isset($methods[$method])) { |
|
| 287 | - continue; |
|
| 288 | - } |
|
| 289 | - if ($obj instanceof $class) { |
|
| 290 | - return call_user_func_array(array($obj, $method), $args); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - $dwoo->triggerError('The current security policy prevents you from calling ' . get_class($obj) . '::' . $method . '()'); |
|
| 273 | + /** |
|
| 274 | + * This is used at run time to check whether method calls are allowed or not. |
|
| 275 | + * |
|
| 276 | + * @param Core $dwoo dwoo instance that calls this |
|
| 277 | + * @param object $obj any object on which the method must be called |
|
| 278 | + * @param string $method lowercased method name |
|
| 279 | + * @param array $args arguments array |
|
| 280 | + * |
|
| 281 | + * @return mixed result of method call or unll + E_USER_NOTICE if not allowed |
|
| 282 | + */ |
|
| 283 | + public function callMethod(Core $dwoo, $obj, $method, $args) |
|
| 284 | + { |
|
| 285 | + foreach ($this->allowedMethods as $class => $methods) { |
|
| 286 | + if (!isset($methods[$method])) { |
|
| 287 | + continue; |
|
| 288 | + } |
|
| 289 | + if ($obj instanceof $class) { |
|
| 290 | + return call_user_func_array(array($obj, $method), $args); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + $dwoo->triggerError('The current security policy prevents you from calling ' . get_class($obj) . '::' . $method . '()'); |
|
| 294 | 294 | |
| 295 | - return null; |
|
| 296 | - } |
|
| 295 | + return null; |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * This is used at compile time to check whether static method calls are allowed or not. |
|
| 300 | - * |
|
| 301 | - * @param mixed $class lowercased class name or array('class', 'method') couple |
|
| 302 | - * @param string $method lowercased method name |
|
| 303 | - * |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - public function isMethodAllowed($class, $method = null) |
|
| 307 | - { |
|
| 308 | - if (is_array($class)) { |
|
| 309 | - list($class, $method) = $class; |
|
| 310 | - } |
|
| 311 | - foreach ($this->allowedMethods as $allowedClass => $methods) { |
|
| 312 | - if (!isset($methods[$method])) { |
|
| 313 | - continue; |
|
| 314 | - } |
|
| 315 | - if ($class === $allowedClass || is_subclass_of($class, $allowedClass)) { |
|
| 316 | - return true; |
|
| 317 | - } |
|
| 318 | - } |
|
| 298 | + /** |
|
| 299 | + * This is used at compile time to check whether static method calls are allowed or not. |
|
| 300 | + * |
|
| 301 | + * @param mixed $class lowercased class name or array('class', 'method') couple |
|
| 302 | + * @param string $method lowercased method name |
|
| 303 | + * |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + public function isMethodAllowed($class, $method = null) |
|
| 307 | + { |
|
| 308 | + if (is_array($class)) { |
|
| 309 | + list($class, $method) = $class; |
|
| 310 | + } |
|
| 311 | + foreach ($this->allowedMethods as $allowedClass => $methods) { |
|
| 312 | + if (!isset($methods[$method])) { |
|
| 313 | + continue; |
|
| 314 | + } |
|
| 315 | + if ($class === $allowedClass || is_subclass_of($class, $allowedClass)) { |
|
| 316 | + return true; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - return false; |
|
| 321 | - } |
|
| 320 | + return false; |
|
| 321 | + } |
|
| 322 | 322 | } |
@@ -23,23 +23,23 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Adapter extends Processor |
| 25 | 25 | { |
| 26 | - public $callback; |
|
| 26 | + public $callback; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param string $input |
|
| 30 | - * |
|
| 31 | - * @return mixed |
|
| 32 | - */ |
|
| 33 | - public function process($input) |
|
| 34 | - { |
|
| 35 | - return call_user_func($this->callback, $input); |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * @param string $input |
|
| 30 | + * |
|
| 31 | + * @return mixed |
|
| 32 | + */ |
|
| 33 | + public function process($input) |
|
| 34 | + { |
|
| 35 | + return call_user_func($this->callback, $input); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param $callback |
|
| 40 | - */ |
|
| 41 | - public function registerCallback($callback) |
|
| 42 | - { |
|
| 43 | - $this->callback = $callback; |
|
| 44 | - } |
|
| 38 | + /** |
|
| 39 | + * @param $callback |
|
| 40 | + */ |
|
| 41 | + public function registerCallback($callback) |
|
| 42 | + { |
|
| 43 | + $this->callback = $callback; |
|
| 44 | + } |
|
| 45 | 45 | } |
| 46 | 46 | \ No newline at end of file |
@@ -29,265 +29,265 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class File extends Str |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Template filename. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $file; |
|
| 32 | + /** |
|
| 33 | + * Template filename. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $file; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Include path(s) to look into to find this template. |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $includePath = array(); |
|
| 39 | + /** |
|
| 40 | + * Include path(s) to look into to find this template. |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $includePath = array(); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Resolved path cache when looking for a file in multiple include paths. |
|
| 48 | - * this is reset when the include path is changed |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $resolvedPath = null; |
|
| 46 | + /** |
|
| 47 | + * Resolved path cache when looking for a file in multiple include paths. |
|
| 48 | + * this is reset when the include path is changed |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $resolvedPath = null; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Creates a template from a file. |
|
| 56 | - * |
|
| 57 | - * @param string $file the path to the template file, make sure it exists |
|
| 58 | - * @param int $cacheTime duration of the cache validity for this template, |
|
| 59 | - * if null it defaults to the Dwoo instance that will |
|
| 60 | - * render this template |
|
| 61 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 62 | - * makes this template's content unique, if null it defaults |
|
| 63 | - * to the current url |
|
| 64 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 65 | - * template from others, if null it defaults to the filename+bits of the path |
|
| 66 | - * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths |
|
| 67 | - */ |
|
| 68 | - public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = array()) |
|
| 69 | - { |
|
| 70 | - parent::__construct($file, $cacheTime, $cacheId, $compileId); |
|
| 71 | - $this->template = null; |
|
| 72 | - $this->file = $file; |
|
| 73 | - $this->name = basename($file); |
|
| 74 | - $this->setIncludePath($includePath); |
|
| 75 | - } |
|
| 54 | + /** |
|
| 55 | + * Creates a template from a file. |
|
| 56 | + * |
|
| 57 | + * @param string $file the path to the template file, make sure it exists |
|
| 58 | + * @param int $cacheTime duration of the cache validity for this template, |
|
| 59 | + * if null it defaults to the Dwoo instance that will |
|
| 60 | + * render this template |
|
| 61 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 62 | + * makes this template's content unique, if null it defaults |
|
| 63 | + * to the current url |
|
| 64 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 65 | + * template from others, if null it defaults to the filename+bits of the path |
|
| 66 | + * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths |
|
| 67 | + */ |
|
| 68 | + public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = array()) |
|
| 69 | + { |
|
| 70 | + parent::__construct($file, $cacheTime, $cacheId, $compileId); |
|
| 71 | + $this->template = null; |
|
| 72 | + $this->file = $file; |
|
| 73 | + $this->name = basename($file); |
|
| 74 | + $this->setIncludePath($includePath); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Sets the include path(s) to where the given template filename must be looked up. |
|
| 79 | - * |
|
| 80 | - * @param mixed $paths the path to look into, can be string for a single path or an array of paths |
|
| 81 | - */ |
|
| 82 | - public function setIncludePath($paths) |
|
| 83 | - { |
|
| 84 | - if (is_array($paths) === false) { |
|
| 85 | - $paths = array($paths); |
|
| 86 | - } |
|
| 77 | + /** |
|
| 78 | + * Sets the include path(s) to where the given template filename must be looked up. |
|
| 79 | + * |
|
| 80 | + * @param mixed $paths the path to look into, can be string for a single path or an array of paths |
|
| 81 | + */ |
|
| 82 | + public function setIncludePath($paths) |
|
| 83 | + { |
|
| 84 | + if (is_array($paths) === false) { |
|
| 85 | + $paths = array($paths); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - $this->includePath = $paths; |
|
| 89 | - $this->resolvedPath = null; |
|
| 90 | - } |
|
| 88 | + $this->includePath = $paths; |
|
| 89 | + $this->resolvedPath = null; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Return the current include path(s). |
|
| 94 | - * |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public function getIncludePath() |
|
| 98 | - { |
|
| 99 | - return $this->includePath; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * Return the current include path(s). |
|
| 94 | + * |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public function getIncludePath() |
|
| 98 | + { |
|
| 99 | + return $this->includePath; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Checks if compiled file is valid (exists and it's the modification is greater or |
|
| 104 | - * equal to the modification time of the template file). |
|
| 105 | - * |
|
| 106 | - * @param string file |
|
| 107 | - * |
|
| 108 | - * @return bool True cache file existance and it's modification time |
|
| 109 | - */ |
|
| 110 | - protected function isValidCompiledFile($file) |
|
| 111 | - { |
|
| 112 | - return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
| 113 | - } |
|
| 102 | + /** |
|
| 103 | + * Checks if compiled file is valid (exists and it's the modification is greater or |
|
| 104 | + * equal to the modification time of the template file). |
|
| 105 | + * |
|
| 106 | + * @param string file |
|
| 107 | + * |
|
| 108 | + * @return bool True cache file existance and it's modification time |
|
| 109 | + */ |
|
| 110 | + protected function isValidCompiledFile($file) |
|
| 111 | + { |
|
| 112 | + return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Returns the template source of this template. |
|
| 117 | - * |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getSource() |
|
| 121 | - { |
|
| 122 | - return file_get_contents($this->getResourceIdentifier()); |
|
| 123 | - } |
|
| 115 | + /** |
|
| 116 | + * Returns the template source of this template. |
|
| 117 | + * |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getSource() |
|
| 121 | + { |
|
| 122 | + return file_get_contents($this->getResourceIdentifier()); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Returns the resource name for this template class. |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getResourceName() |
|
| 131 | - { |
|
| 132 | - return 'file'; |
|
| 133 | - } |
|
| 125 | + /** |
|
| 126 | + * Returns the resource name for this template class. |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getResourceName() |
|
| 131 | + { |
|
| 132 | + return 'file'; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Returns this template's source filename. |
|
| 137 | - * |
|
| 138 | - * @return string |
|
| 139 | - * @throws DwooException |
|
| 140 | - */ |
|
| 141 | - public function getResourceIdentifier() |
|
| 142 | - { |
|
| 143 | - if ($this->resolvedPath !== null) { |
|
| 144 | - return $this->resolvedPath; |
|
| 145 | - } elseif (array_filter($this->getIncludePath()) == array()) { |
|
| 146 | - return $this->file; |
|
| 147 | - } else { |
|
| 148 | - foreach ($this->getIncludePath() as $path) { |
|
| 149 | - $path = rtrim($path, DIRECTORY_SEPARATOR); |
|
| 150 | - if (file_exists($path . DIRECTORY_SEPARATOR . $this->file) === true) { |
|
| 151 | - return $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; |
|
| 152 | - } |
|
| 153 | - } |
|
| 135 | + /** |
|
| 136 | + * Returns this template's source filename. |
|
| 137 | + * |
|
| 138 | + * @return string |
|
| 139 | + * @throws DwooException |
|
| 140 | + */ |
|
| 141 | + public function getResourceIdentifier() |
|
| 142 | + { |
|
| 143 | + if ($this->resolvedPath !== null) { |
|
| 144 | + return $this->resolvedPath; |
|
| 145 | + } elseif (array_filter($this->getIncludePath()) == array()) { |
|
| 146 | + return $this->file; |
|
| 147 | + } else { |
|
| 148 | + foreach ($this->getIncludePath() as $path) { |
|
| 149 | + $path = rtrim($path, DIRECTORY_SEPARATOR); |
|
| 150 | + if (file_exists($path . DIRECTORY_SEPARATOR . $this->file) === true) { |
|
| 151 | + return $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
| 156 | - } |
|
| 157 | - } |
|
| 155 | + throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Returns an unique value identifying the current version of this template, |
|
| 161 | - * in this case it's the unix timestamp of the last modification. |
|
| 162 | - * |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - public function getUid() |
|
| 166 | - { |
|
| 167 | - return (string)filemtime($this->getResourceIdentifier()); |
|
| 168 | - } |
|
| 159 | + /** |
|
| 160 | + * Returns an unique value identifying the current version of this template, |
|
| 161 | + * in this case it's the unix timestamp of the last modification. |
|
| 162 | + * |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + public function getUid() |
|
| 166 | + { |
|
| 167 | + return (string)filemtime($this->getResourceIdentifier()); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Returns a new template object from the given include name, null if no include is |
|
| 172 | - * possible (resource not found), or false if include is not permitted by this resource type. |
|
| 173 | - * |
|
| 174 | - * @param Core $core the dwoo instance requiring it |
|
| 175 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to |
|
| 176 | - * include |
|
| 177 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults |
|
| 178 | - * to the Dwoo instance that will render this template if null it |
|
| 179 | - * defaults to the Dwoo instance that will render this template if null |
|
| 180 | - * it defaults to the Dwoo instance that will render this template |
|
| 181 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes |
|
| 182 | - * this template's content unique, if null it defaults to the current |
|
| 183 | - * url makes this template's content unique, if null it defaults to the |
|
| 184 | - * current url makes this template's content unique, if null it defaults |
|
| 185 | - * to the current url |
|
| 186 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 187 | - * template from others, if null it defaults to the filename+bits of the |
|
| 188 | - * path template from others, if null it defaults to the filename+bits |
|
| 189 | - * of the path template from others, if null it defaults to the |
|
| 190 | - * filename+bits of the path |
|
| 191 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an |
|
| 192 | - * include, extends or any other plugin) an include, extends or any |
|
| 193 | - * other plugin) an include, extends or any other plugin) |
|
| 194 | - * |
|
| 195 | - * @return TemplateFile|null |
|
| 196 | - * @throws DwooException |
|
| 197 | - * @throws SecurityException |
|
| 198 | - */ |
|
| 199 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 200 | - { |
|
| 201 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
| 202 | - $resourceId = str_replace( |
|
| 203 | - array("\t", "\n", "\r", "\f", "\v"), array( |
|
| 204 | - '\\t', |
|
| 205 | - '\\n', |
|
| 206 | - '\\r', |
|
| 207 | - '\\f', |
|
| 208 | - '\\v' |
|
| 209 | - ), $resourceId |
|
| 210 | - ); |
|
| 211 | - } |
|
| 212 | - $resourceId = strtr($resourceId, '\\', '/'); |
|
| 170 | + /** |
|
| 171 | + * Returns a new template object from the given include name, null if no include is |
|
| 172 | + * possible (resource not found), or false if include is not permitted by this resource type. |
|
| 173 | + * |
|
| 174 | + * @param Core $core the dwoo instance requiring it |
|
| 175 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to |
|
| 176 | + * include |
|
| 177 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults |
|
| 178 | + * to the Dwoo instance that will render this template if null it |
|
| 179 | + * defaults to the Dwoo instance that will render this template if null |
|
| 180 | + * it defaults to the Dwoo instance that will render this template |
|
| 181 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes |
|
| 182 | + * this template's content unique, if null it defaults to the current |
|
| 183 | + * url makes this template's content unique, if null it defaults to the |
|
| 184 | + * current url makes this template's content unique, if null it defaults |
|
| 185 | + * to the current url |
|
| 186 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 187 | + * template from others, if null it defaults to the filename+bits of the |
|
| 188 | + * path template from others, if null it defaults to the filename+bits |
|
| 189 | + * of the path template from others, if null it defaults to the |
|
| 190 | + * filename+bits of the path |
|
| 191 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an |
|
| 192 | + * include, extends or any other plugin) an include, extends or any |
|
| 193 | + * other plugin) an include, extends or any other plugin) |
|
| 194 | + * |
|
| 195 | + * @return TemplateFile|null |
|
| 196 | + * @throws DwooException |
|
| 197 | + * @throws SecurityException |
|
| 198 | + */ |
|
| 199 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 200 | + { |
|
| 201 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
| 202 | + $resourceId = str_replace( |
|
| 203 | + array("\t", "\n", "\r", "\f", "\v"), array( |
|
| 204 | + '\\t', |
|
| 205 | + '\\n', |
|
| 206 | + '\\r', |
|
| 207 | + '\\f', |
|
| 208 | + '\\v' |
|
| 209 | + ), $resourceId |
|
| 210 | + ); |
|
| 211 | + } |
|
| 212 | + $resourceId = strtr($resourceId, '\\', '/'); |
|
| 213 | 213 | |
| 214 | - $includePath = null; |
|
| 214 | + $includePath = null; |
|
| 215 | 215 | |
| 216 | - if (file_exists($resourceId) === false) { |
|
| 217 | - if ($parentTemplate === null) { |
|
| 218 | - $parentTemplate = $core->getTemplate(); |
|
| 219 | - } |
|
| 220 | - if ($parentTemplate instanceof self) { |
|
| 221 | - if ($includePath = $parentTemplate->getIncludePath()) { |
|
| 222 | - if (strstr($resourceId, '../')) { |
|
| 223 | - throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
|
| 224 | - } |
|
| 225 | - } else { |
|
| 226 | - $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
| 227 | - if (file_exists($resourceId) === false) { |
|
| 228 | - return null; |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } else { |
|
| 232 | - return null; |
|
| 233 | - } |
|
| 234 | - } |
|
| 216 | + if (file_exists($resourceId) === false) { |
|
| 217 | + if ($parentTemplate === null) { |
|
| 218 | + $parentTemplate = $core->getTemplate(); |
|
| 219 | + } |
|
| 220 | + if ($parentTemplate instanceof self) { |
|
| 221 | + if ($includePath = $parentTemplate->getIncludePath()) { |
|
| 222 | + if (strstr($resourceId, '../')) { |
|
| 223 | + throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
|
| 224 | + } |
|
| 225 | + } else { |
|
| 226 | + $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
| 227 | + if (file_exists($resourceId) === false) { |
|
| 228 | + return null; |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } else { |
|
| 232 | + return null; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - if ($policy = $core->getSecurityPolicy()) { |
|
| 237 | - while (true) { |
|
| 238 | - if (preg_match('{^([a-z]+?)://}i', $resourceId)) { |
|
| 239 | - throw new SecurityException('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.'); |
|
| 240 | - } |
|
| 236 | + if ($policy = $core->getSecurityPolicy()) { |
|
| 237 | + while (true) { |
|
| 238 | + if (preg_match('{^([a-z]+?)://}i', $resourceId)) { |
|
| 239 | + throw new SecurityException('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.'); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - if ($includePath) { |
|
| 243 | - break; |
|
| 244 | - } |
|
| 242 | + if ($includePath) { |
|
| 243 | + break; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - $resourceId = realpath($resourceId); |
|
| 247 | - $dirs = $policy->getAllowedDirectories(); |
|
| 248 | - foreach ($dirs as $dir => $dummy) { |
|
| 249 | - if (strpos($resourceId, $dir) === 0) { |
|
| 250 | - break 2; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
| 254 | - } |
|
| 255 | - } |
|
| 246 | + $resourceId = realpath($resourceId); |
|
| 247 | + $dirs = $policy->getAllowedDirectories(); |
|
| 248 | + foreach ($dirs as $dir => $dummy) { |
|
| 249 | + if (strpos($resourceId, $dir) === 0) { |
|
| 250 | + break 2; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - $class = 'Dwoo\Template\File'; |
|
| 258 | - if ($parentTemplate) { |
|
| 259 | - $class = get_class($parentTemplate); |
|
| 260 | - } |
|
| 257 | + $class = 'Dwoo\Template\File'; |
|
| 258 | + if ($parentTemplate) { |
|
| 259 | + $class = get_class($parentTemplate); |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); |
|
| 263 | - } |
|
| 262 | + return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Returns the full compiled file name and assigns a default value to it if |
|
| 267 | - * required. |
|
| 268 | - * |
|
| 269 | - * @param Core $core the dwoo instance that requests the file name |
|
| 270 | - * |
|
| 271 | - * @return string the full path to the compiled file |
|
| 272 | - */ |
|
| 273 | - protected function getCompiledFilename(Core $core) |
|
| 274 | - { |
|
| 275 | - // no compile id was provided, set default |
|
| 276 | - if ($this->compileId === null) { |
|
| 277 | - $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); |
|
| 278 | - } |
|
| 265 | + /** |
|
| 266 | + * Returns the full compiled file name and assigns a default value to it if |
|
| 267 | + * required. |
|
| 268 | + * |
|
| 269 | + * @param Core $core the dwoo instance that requests the file name |
|
| 270 | + * |
|
| 271 | + * @return string the full path to the compiled file |
|
| 272 | + */ |
|
| 273 | + protected function getCompiledFilename(Core $core) |
|
| 274 | + { |
|
| 275 | + // no compile id was provided, set default |
|
| 276 | + if ($this->compileId === null) { |
|
| 277 | + $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - return $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 281 | - } |
|
| 280 | + return $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * Returns some php code that will check if this template has been modified or not. |
|
| 285 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 286 | - * |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public function getIsModifiedCode() |
|
| 290 | - { |
|
| 291 | - return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
| 292 | - } |
|
| 283 | + /** |
|
| 284 | + * Returns some php code that will check if this template has been modified or not. |
|
| 285 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 286 | + * |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public function getIsModifiedCode() |
|
| 290 | + { |
|
| 291 | + return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
| 292 | + } |
|
| 293 | 293 | } |
@@ -30,14 +30,14 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class PluginIsset extends Plugin implements ICompilable |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @param Compiler $compiler |
|
| 35 | - * @param mixed $var |
|
| 36 | - * |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public static function compile(Compiler $compiler, $var) |
|
| 40 | - { |
|
| 41 | - return '(' . $var . ' !== null)'; |
|
| 42 | - } |
|
| 33 | + /** |
|
| 34 | + * @param Compiler $compiler |
|
| 35 | + * @param mixed $var |
|
| 36 | + * |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public static function compile(Compiler $compiler, $var) |
|
| 40 | + { |
|
| 41 | + return '(' . $var . ' !== null)'; |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -31,62 +31,62 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class PluginDateFormat extends Plugin |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * @param mixed $value |
|
| 36 | - * @param string $format |
|
| 37 | - * @param null $default |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function process($value, $format = '%b %e, %Y', $default = null) |
|
| 42 | - { |
|
| 43 | - if (!empty($value)) { |
|
| 44 | - // convert if it's not a valid unix timestamp |
|
| 45 | - if (preg_match('#^-?\d{1,10}$#', $value) === 0) { |
|
| 46 | - $value = strtotime($value); |
|
| 47 | - } |
|
| 48 | - } elseif (!empty($default)) { |
|
| 49 | - // convert if it's not a valid unix timestamp |
|
| 50 | - if (preg_match('#^-?\d{1,10}$#', $default) === 0) { |
|
| 51 | - $value = strtotime($default); |
|
| 52 | - } else { |
|
| 53 | - $value = $default; |
|
| 54 | - } |
|
| 55 | - } else { |
|
| 56 | - return ''; |
|
| 57 | - } |
|
| 34 | + /** |
|
| 35 | + * @param mixed $value |
|
| 36 | + * @param string $format |
|
| 37 | + * @param null $default |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function process($value, $format = '%b %e, %Y', $default = null) |
|
| 42 | + { |
|
| 43 | + if (!empty($value)) { |
|
| 44 | + // convert if it's not a valid unix timestamp |
|
| 45 | + if (preg_match('#^-?\d{1,10}$#', $value) === 0) { |
|
| 46 | + $value = strtotime($value); |
|
| 47 | + } |
|
| 48 | + } elseif (!empty($default)) { |
|
| 49 | + // convert if it's not a valid unix timestamp |
|
| 50 | + if (preg_match('#^-?\d{1,10}$#', $default) === 0) { |
|
| 51 | + $value = strtotime($default); |
|
| 52 | + } else { |
|
| 53 | + $value = $default; |
|
| 54 | + } |
|
| 55 | + } else { |
|
| 56 | + return ''; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin |
|
| 60 | - if (DIRECTORY_SEPARATOR == '\\') { |
|
| 61 | - $_win_from = array( |
|
| 62 | - '%D', |
|
| 63 | - '%h', |
|
| 64 | - '%n', |
|
| 65 | - '%r', |
|
| 66 | - '%R', |
|
| 67 | - '%t', |
|
| 68 | - '%T' |
|
| 69 | - ); |
|
| 70 | - $_win_to = array( |
|
| 71 | - '%m/%d/%y', |
|
| 72 | - '%b', |
|
| 73 | - "\n", |
|
| 74 | - '%I:%M:%S %p', |
|
| 75 | - '%H:%M', |
|
| 76 | - "\t", |
|
| 77 | - '%H:%M:%S' |
|
| 78 | - ); |
|
| 79 | - if (strpos($format, '%e') !== false) { |
|
| 80 | - $_win_from[] = '%e'; |
|
| 81 | - $_win_to[] = sprintf('%\' 2d', date('j', $value)); |
|
| 82 | - } |
|
| 83 | - if (strpos($format, '%l') !== false) { |
|
| 84 | - $_win_from[] = '%l'; |
|
| 85 | - $_win_to[] = sprintf('%\' 2d', date('h', $value)); |
|
| 86 | - } |
|
| 87 | - $format = str_replace($_win_from, $_win_to, $format); |
|
| 88 | - } |
|
| 59 | + // Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin |
|
| 60 | + if (DIRECTORY_SEPARATOR == '\\') { |
|
| 61 | + $_win_from = array( |
|
| 62 | + '%D', |
|
| 63 | + '%h', |
|
| 64 | + '%n', |
|
| 65 | + '%r', |
|
| 66 | + '%R', |
|
| 67 | + '%t', |
|
| 68 | + '%T' |
|
| 69 | + ); |
|
| 70 | + $_win_to = array( |
|
| 71 | + '%m/%d/%y', |
|
| 72 | + '%b', |
|
| 73 | + "\n", |
|
| 74 | + '%I:%M:%S %p', |
|
| 75 | + '%H:%M', |
|
| 76 | + "\t", |
|
| 77 | + '%H:%M:%S' |
|
| 78 | + ); |
|
| 79 | + if (strpos($format, '%e') !== false) { |
|
| 80 | + $_win_from[] = '%e'; |
|
| 81 | + $_win_to[] = sprintf('%\' 2d', date('j', $value)); |
|
| 82 | + } |
|
| 83 | + if (strpos($format, '%l') !== false) { |
|
| 84 | + $_win_from[] = '%l'; |
|
| 85 | + $_win_to[] = sprintf('%\' 2d', date('h', $value)); |
|
| 86 | + } |
|
| 87 | + $format = str_replace($_win_from, $_win_to, $format); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return strftime($format, $value); |
|
| 91 | - } |
|
| 90 | + return strftime($format, $value); |
|
| 91 | + } |
|
| 92 | 92 | } |
| 93 | 93 | \ No newline at end of file |
@@ -36,19 +36,19 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class PluginReturn extends Plugin implements ICompilable |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * @param Compiler $compiler |
|
| 41 | - * @param array $rest |
|
| 42 | - * |
|
| 43 | - * @return string |
|
| 44 | - */ |
|
| 45 | - public static function compile(Compiler $compiler, array $rest = array()) |
|
| 46 | - { |
|
| 47 | - $out = array(); |
|
| 48 | - foreach ($rest as $var => $val) { |
|
| 49 | - $out[] = '$this->setReturnValue(' . var_export($var, true) . ', ' . $val . ')'; |
|
| 50 | - } |
|
| 39 | + /** |
|
| 40 | + * @param Compiler $compiler |
|
| 41 | + * @param array $rest |
|
| 42 | + * |
|
| 43 | + * @return string |
|
| 44 | + */ |
|
| 45 | + public static function compile(Compiler $compiler, array $rest = array()) |
|
| 46 | + { |
|
| 47 | + $out = array(); |
|
| 48 | + foreach ($rest as $var => $val) { |
|
| 49 | + $out[] = '$this->setReturnValue(' . var_export($var, true) . ', ' . $val . ')'; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - return '(' . implode('.', $out) . ')'; |
|
| 53 | - } |
|
| 52 | + return '(' . implode('.', $out) . ')'; |
|
| 53 | + } |
|
| 54 | 54 | } |
| 55 | 55 | \ No newline at end of file |
@@ -39,15 +39,15 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class PluginWhitespace extends Plugin implements ICompilable |
| 41 | 41 | { |
| 42 | - /** |
|
| 43 | - * @param Compiler $compiler |
|
| 44 | - * @param string $value |
|
| 45 | - * @param string $with |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public static function compile(Compiler $compiler, $value, $with = ' ') |
|
| 50 | - { |
|
| 51 | - return "preg_replace('#\s+#'.(strcasecmp(\$this->charset, 'utf-8')===0?'u':''), $with, $value)"; |
|
| 52 | - } |
|
| 42 | + /** |
|
| 43 | + * @param Compiler $compiler |
|
| 44 | + * @param string $value |
|
| 45 | + * @param string $with |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public static function compile(Compiler $compiler, $value, $with = ' ') |
|
| 50 | + { |
|
| 51 | + return "preg_replace('#\s+#'.(strcasecmp(\$this->charset, 'utf-8')===0?'u':''), $with, $value)"; |
|
| 52 | + } |
|
| 53 | 53 | } |
| 54 | 54 | \ No newline at end of file |
@@ -30,8 +30,8 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class PluginCountWords extends Plugin implements ICompilable |
| 32 | 32 | { |
| 33 | - public static function compile(Compiler $compiler, $value) |
|
| 34 | - { |
|
| 35 | - return 'preg_match_all(strcasecmp($this->charset, \'utf-8\')===0 ? \'#[\w\pL]+#u\' : \'#\w+#\', ' . $value . ', $tmp)'; |
|
| 36 | - } |
|
| 33 | + public static function compile(Compiler $compiler, $value) |
|
| 34 | + { |
|
| 35 | + return 'preg_match_all(strcasecmp($this->charset, \'utf-8\')===0 ? \'#[\w\pL]+#u\' : \'#\w+#\', ' . $value . ', $tmp)'; |
|
| 36 | + } |
|
| 37 | 37 | } |
| 38 | 38 | \ No newline at end of file |
@@ -29,26 +29,26 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class PluginReverse extends Plugin |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * @param string $value |
|
| 34 | - * @param bool $preserve_keys |
|
| 35 | - * |
|
| 36 | - * @return array|string |
|
| 37 | - */ |
|
| 38 | - public function process($value, $preserve_keys = false) |
|
| 39 | - { |
|
| 40 | - if (is_array($value)) { |
|
| 41 | - return array_reverse($value, $preserve_keys); |
|
| 42 | - } elseif (($charset = $this->core->getCharset()) === 'iso-8859-1') { |
|
| 43 | - return strrev((string)$value); |
|
| 44 | - } |
|
| 32 | + /** |
|
| 33 | + * @param string $value |
|
| 34 | + * @param bool $preserve_keys |
|
| 35 | + * |
|
| 36 | + * @return array|string |
|
| 37 | + */ |
|
| 38 | + public function process($value, $preserve_keys = false) |
|
| 39 | + { |
|
| 40 | + if (is_array($value)) { |
|
| 41 | + return array_reverse($value, $preserve_keys); |
|
| 42 | + } elseif (($charset = $this->core->getCharset()) === 'iso-8859-1') { |
|
| 43 | + return strrev((string)$value); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - $strlen = mb_strlen($value); |
|
| 47 | - $out = ''; |
|
| 48 | - while ($strlen --) { |
|
| 49 | - $out .= mb_substr($value, $strlen, 1, $charset); |
|
| 50 | - } |
|
| 46 | + $strlen = mb_strlen($value); |
|
| 47 | + $out = ''; |
|
| 48 | + while ($strlen --) { |
|
| 49 | + $out .= mb_substr($value, $strlen, 1, $charset); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - return $out; |
|
| 53 | - } |
|
| 52 | + return $out; |
|
| 53 | + } |
|
| 54 | 54 | } |
| 55 | 55 | \ No newline at end of file |