@@ -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 | } |
@@ -290,7 +290,7 @@ |
||
| 290 | 290 | return call_user_func_array(array($obj, $method), $args); |
| 291 | 291 | } |
| 292 | 292 | } |
| 293 | - $dwoo->triggerError('The current security policy prevents you from calling ' . get_class($obj) . '::' . $method . '()'); |
|
| 293 | + $dwoo->triggerError('The current security policy prevents you from calling '.get_class($obj).'::'.$method.'()'); |
|
| 294 | 294 | |
| 295 | 295 | return null; |
| 296 | 296 | } |
@@ -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 |
@@ -293,7 +293,7 @@ |
||
| 293 | 293 | * @param Core $core the dwoo instance that requests it |
| 294 | 294 | * @param string $output the template output |
| 295 | 295 | * |
| 296 | - * @return mixed full path of the cached file or false upon failure |
|
| 296 | + * @return false|string full path of the cached file or false upon failure |
|
| 297 | 297 | */ |
| 298 | 298 | public function cache(Core $core, $output) |
| 299 | 299 | { |
@@ -29,509 +29,509 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class String implements ITemplate |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Template name. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $name; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Template compilation id. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $compileId; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Template cache id, if not provided in the constructor, it is set to |
|
| 48 | - * the md4 hash of the request_uri. it is however highly recommended to |
|
| 49 | - * provide one that will fit your needs. |
|
| 50 | - * in all cases, the compilation id is prepended to the cache id to separate |
|
| 51 | - * templates with similar cache ids from one another |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $cacheId; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Validity duration of the generated cache file (in seconds). |
|
| 59 | - * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
| 60 | - * |
|
| 61 | - * @var int |
|
| 62 | - */ |
|
| 63 | - protected $cacheTime; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Boolean flag that defines whether the compilation should be enforced (once) or |
|
| 67 | - * not use this if you have issues with the compiled templates not being updated |
|
| 68 | - * but if you do need this it's most likely that you should file a bug report. |
|
| 69 | - * |
|
| 70 | - * @var bool |
|
| 71 | - */ |
|
| 72 | - protected $compilationEnforced; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Caches the results of the file checks to save some time when the same |
|
| 76 | - * templates is rendered several times. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected static $cache = array( |
|
| 81 | - 'cached' => array(), |
|
| 82 | - 'compiled' => array() |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Holds the compiler that built this template. |
|
| 87 | - * |
|
| 88 | - * @var ICompiler |
|
| 89 | - */ |
|
| 90 | - protected $compiler; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Chmod value for all files written (cached or compiled ones). |
|
| 94 | - * set to null if you don't want any chmod operation to happen |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - protected $chmod = 0777; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Containing template string. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - protected $template; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Creates a template from a string. |
|
| 109 | - * |
|
| 110 | - * @param string $templateString the template to use |
|
| 111 | - * @param int $cacheTime duration of the cache validity for this template, |
|
| 112 | - * if null it defaults to the Dwoo instance that will |
|
| 113 | - * render this template, set to -1 for infinite cache or 0 to disable |
|
| 114 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 115 | - * makes this template's content unique, if null it defaults |
|
| 116 | - * to the current url |
|
| 117 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 118 | - * template from others, if null it defaults to the md4 hash of the template |
|
| 119 | - */ |
|
| 120 | - public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
| 121 | - { |
|
| 122 | - $this->template = $templateString; |
|
| 123 | - $this->name = hash('md4', $templateString); |
|
| 124 | - $this->cacheTime = $cacheTime; |
|
| 125 | - |
|
| 126 | - if ($compileId !== null) { |
|
| 127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($cacheId !== null) { |
|
| 131 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Returns the cache duration for this template. |
|
| 137 | - * defaults to null if it was not provided |
|
| 138 | - * |
|
| 139 | - * @return int|null |
|
| 140 | - */ |
|
| 141 | - public function getCacheTime() |
|
| 142 | - { |
|
| 143 | - return $this->cacheTime; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Sets the cache duration for this template. |
|
| 148 | - * can be used to set it after the object is created if you did not provide |
|
| 149 | - * it in the constructor |
|
| 150 | - * |
|
| 151 | - * @param int $seconds duration of the cache validity for this template, if |
|
| 152 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 153 | - * -1 = infinite cache |
|
| 154 | - */ |
|
| 155 | - public function setCacheTime($seconds = null) |
|
| 156 | - { |
|
| 157 | - $this->cacheTime = $seconds; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Returns the chmod value for all files written (cached or compiled ones). |
|
| 162 | - * defaults to 0777 |
|
| 163 | - * |
|
| 164 | - * @return int|null |
|
| 165 | - */ |
|
| 166 | - public function getChmod() |
|
| 167 | - { |
|
| 168 | - return $this->chmod; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Set the chmod value for all files written (cached or compiled ones). |
|
| 173 | - * set to null if you don't want to do any chmod() operation |
|
| 174 | - * |
|
| 175 | - * @param int $mask new bitmask to use for all files |
|
| 176 | - */ |
|
| 177 | - public function setChmod($mask = null) |
|
| 178 | - { |
|
| 179 | - $this->chmod = $mask; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the template name. |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - public function getName() |
|
| 188 | - { |
|
| 189 | - return $this->name; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Returns the resource name for this template class. |
|
| 194 | - * |
|
| 195 | - * @return string |
|
| 196 | - */ |
|
| 197 | - public function getResourceName() |
|
| 198 | - { |
|
| 199 | - return 'string'; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
| 204 | - * |
|
| 205 | - * @return false |
|
| 206 | - */ |
|
| 207 | - public function getResourceIdentifier() |
|
| 208 | - { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Returns the template source of this template. |
|
| 214 | - * |
|
| 215 | - * @return string |
|
| 216 | - */ |
|
| 217 | - public function getSource() |
|
| 218 | - { |
|
| 219 | - return $this->template; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Returns an unique value identifying the current version of this template, |
|
| 224 | - * in this case it's the md4 hash of the content. |
|
| 225 | - * |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - public function getUid() |
|
| 229 | - { |
|
| 230 | - return $this->name; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 235 | - * |
|
| 236 | - * @return ICompiler |
|
| 237 | - */ |
|
| 238 | - public function getCompiler() |
|
| 239 | - { |
|
| 240 | - return $this->compiler; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Marks this template as compile-forced, which means it will be recompiled even if it |
|
| 245 | - * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
| 246 | - * it's only meant to be used in development (and the development of dwoo particularly). |
|
| 247 | - */ |
|
| 248 | - public function forceCompilation() |
|
| 249 | - { |
|
| 250 | - $this->compilationEnforced = true; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 255 | - * or false if it's not cached. |
|
| 256 | - * |
|
| 257 | - * @param Core $core the dwoo instance that requests it |
|
| 258 | - * |
|
| 259 | - * @return string|bool |
|
| 260 | - */ |
|
| 261 | - public function getCachedTemplate(Core $core) |
|
| 262 | - { |
|
| 263 | - if ($this->cacheTime !== null) { |
|
| 264 | - $cacheLength = $this->cacheTime; |
|
| 265 | - } else { |
|
| 266 | - $cacheLength = $core->getCacheTime(); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // file is not cacheable |
|
| 270 | - if ($cacheLength == 0) { |
|
| 271 | - return false; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - $cachedFile = $this->getCacheFilename($core); |
|
| 275 | - |
|
| 276 | - if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
| 277 | - // already checked, return cache file |
|
| 278 | - return $cachedFile; |
|
| 279 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | - // cache is still valid and can be loaded |
|
| 281 | - self::$cache['cached'][$this->cacheId] = true; |
|
| 282 | - |
|
| 283 | - return $cachedFile; |
|
| 284 | - } else { |
|
| 285 | - // file is cacheable |
|
| 286 | - return true; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Caches the provided output into the cache file. |
|
| 292 | - * |
|
| 293 | - * @param Core $core the dwoo instance that requests it |
|
| 294 | - * @param string $output the template output |
|
| 295 | - * |
|
| 296 | - * @return mixed full path of the cached file or false upon failure |
|
| 297 | - */ |
|
| 298 | - public function cache(Core $core, $output) |
|
| 299 | - { |
|
| 300 | - $cacheDir = $core->getCacheDir(); |
|
| 301 | - $cachedFile = $this->getCacheFilename($core); |
|
| 302 | - |
|
| 303 | - // the code below is courtesy of Rasmus Schultz, |
|
| 304 | - // thanks for his help on avoiding concurency issues |
|
| 305 | - $temp = tempnam($cacheDir, 'temp'); |
|
| 306 | - if (!($file = @fopen($temp, 'wb'))) { |
|
| 307 | - $temp = $cacheDir . uniqid('temp'); |
|
| 308 | - if (!($file = @fopen($temp, 'wb'))) { |
|
| 309 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 310 | - |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - fwrite($file, $output); |
|
| 316 | - fclose($file); |
|
| 317 | - |
|
| 318 | - $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
| 319 | - if (!@rename($temp, $cachedFile)) { |
|
| 320 | - @unlink($cachedFile); |
|
| 321 | - @rename($temp, $cachedFile); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if ($this->chmod !== null) { |
|
| 325 | - chmod($cachedFile, $this->chmod); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - self::$cache['cached'][$this->cacheId] = true; |
|
| 329 | - |
|
| 330 | - return $cachedFile; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Clears the cached template if it's older than the given time. |
|
| 335 | - * |
|
| 336 | - * @param Core $core the dwoo instance that was used to cache that template |
|
| 337 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 338 | - * |
|
| 339 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 340 | - */ |
|
| 341 | - public function clearCache(Core $core, $olderThan = - 1) |
|
| 342 | - { |
|
| 343 | - $cachedFile = $this->getCacheFilename($core); |
|
| 344 | - |
|
| 345 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Returns the compiled template file name. |
|
| 350 | - * |
|
| 351 | - * @param Core $core the dwoo instance that requests it |
|
| 352 | - * @param ICompiler $compiler the compiler that must be used |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
| 357 | - { |
|
| 358 | - $compiledFile = $this->getCompiledFilename($core); |
|
| 359 | - |
|
| 360 | - if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
| 361 | - // already checked, return compiled file |
|
| 362 | - } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
| 363 | - // template is compiled |
|
| 364 | - self::$cache['compiled'][$this->compileId] = true; |
|
| 365 | - } else { |
|
| 366 | - // compiles the template |
|
| 367 | - $this->compilationEnforced = false; |
|
| 368 | - |
|
| 369 | - if ($compiler === null) { |
|
| 370 | - $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
| 371 | - |
|
| 372 | - if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
| 373 | - $compiler = Compiler::compilerFactory(); |
|
| 374 | - } else { |
|
| 375 | - $compiler = call_user_func($compiler); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $this->compiler = $compiler; |
|
| 380 | - |
|
| 381 | - $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
| 382 | - $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
| 383 | - $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
| 384 | - file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
| 385 | - if ($this->chmod !== null) { |
|
| 386 | - chmod($compiledFile, $this->chmod); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if (extension_loaded('Zend OPcache')) { |
|
| 390 | - opcache_invalidate($compiledFile); |
|
| 391 | - } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
| 392 | - apc_delete_file($compiledFile); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - self::$cache['compiled'][$this->compileId] = true; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - return $compiledFile; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Checks if compiled file is valid (it exists). |
|
| 403 | - * |
|
| 404 | - * @param string $file |
|
| 405 | - * |
|
| 406 | - * @return bool True cache file existance |
|
| 407 | - */ |
|
| 408 | - protected function isValidCompiledFile($file) |
|
| 409 | - { |
|
| 410 | - return file_exists($file); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Returns a new template string object with the resource id being the template source code. |
|
| 415 | - * |
|
| 416 | - * @param Core $core the dwoo instance requiring it |
|
| 417 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
| 418 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 419 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 420 | - * instance that will render this template |
|
| 421 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 422 | - * template's content unique, if null it defaults to the current url makes this |
|
| 423 | - * template's content unique, if null it defaults to the current url |
|
| 424 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 425 | - * others, if null it defaults to the filename+bits of the path template from |
|
| 426 | - * others, if null it defaults to the filename+bits of the path |
|
| 427 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 428 | - * extends or any other plugin) an include, extends or any other plugin) |
|
| 429 | - * |
|
| 430 | - * @return $this |
|
| 431 | - */ |
|
| 432 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 433 | - { |
|
| 434 | - return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Returns the full compiled file name and assigns a default value to it if |
|
| 439 | - * required. |
|
| 440 | - * |
|
| 441 | - * @param Core $core the dwoo instance that requests the file name |
|
| 442 | - * |
|
| 443 | - * @return string the full path to the compiled file |
|
| 444 | - */ |
|
| 445 | - protected function getCompiledFilename(Core $core) |
|
| 446 | - { |
|
| 447 | - // no compile id was provided, set default |
|
| 448 | - if ($this->compileId === null) { |
|
| 449 | - $this->compileId = $this->name; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Returns the full cached file name and assigns a default value to it if |
|
| 457 | - * required. |
|
| 458 | - * |
|
| 459 | - * @param Core $core the dwoo instance that requests the file name |
|
| 460 | - * |
|
| 461 | - * @return string the full path to the cached file |
|
| 462 | - */ |
|
| 463 | - protected function getCacheFilename(Core $core) |
|
| 464 | - { |
|
| 465 | - // no cache id provided, use request_uri as default |
|
| 466 | - if ($this->cacheId === null) { |
|
| 467 | - if (isset($_SERVER['REQUEST_URI']) === true) { |
|
| 468 | - $cacheId = $_SERVER['REQUEST_URI']; |
|
| 469 | - } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
| 470 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 471 | - } else { |
|
| 472 | - $cacheId = ''; |
|
| 473 | - } |
|
| 474 | - // force compiled id generation |
|
| 475 | - $this->getCompiledFilename($core); |
|
| 476 | - |
|
| 477 | - $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Returns some php code that will check if this template has been modified or not. |
|
| 485 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 486 | - * |
|
| 487 | - * @return string |
|
| 488 | - */ |
|
| 489 | - public function getIsModifiedCode() |
|
| 490 | - { |
|
| 491 | - return null; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Ensures the given path exists. |
|
| 496 | - * |
|
| 497 | - * @param string $path any path |
|
| 498 | - * @param string $baseDir the base directory where the directory is created |
|
| 499 | - * ($path must still contain the full path, $baseDir |
|
| 500 | - * is only used for unix permissions) |
|
| 501 | - * |
|
| 502 | - * @throws Exception |
|
| 503 | - */ |
|
| 504 | - protected function makeDirectory($path, $baseDir = null) |
|
| 505 | - { |
|
| 506 | - if (is_dir($path) === true) { |
|
| 507 | - return; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - if ($this->chmod === null) { |
|
| 511 | - $chmod = 0777; |
|
| 512 | - } else { |
|
| 513 | - $chmod = $this->chmod; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - $retries = 3; |
|
| 517 | - while ($retries --) { |
|
| 518 | - @mkdir($path, $chmod, true); |
|
| 519 | - if (is_dir($path)) { |
|
| 520 | - break; |
|
| 521 | - } |
|
| 522 | - usleep(20); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - // enforce the correct mode for all directories created |
|
| 526 | - if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
| 527 | - $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
| 528 | - $folders = explode('/', trim($path, '/')); |
|
| 529 | - foreach ($folders as $folder) { |
|
| 530 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 531 | - if (!chmod($baseDir, $chmod)) { |
|
| 532 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - } |
|
| 32 | + /** |
|
| 33 | + * Template name. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $name; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Template compilation id. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $compileId; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Template cache id, if not provided in the constructor, it is set to |
|
| 48 | + * the md4 hash of the request_uri. it is however highly recommended to |
|
| 49 | + * provide one that will fit your needs. |
|
| 50 | + * in all cases, the compilation id is prepended to the cache id to separate |
|
| 51 | + * templates with similar cache ids from one another |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $cacheId; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Validity duration of the generated cache file (in seconds). |
|
| 59 | + * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
| 60 | + * |
|
| 61 | + * @var int |
|
| 62 | + */ |
|
| 63 | + protected $cacheTime; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Boolean flag that defines whether the compilation should be enforced (once) or |
|
| 67 | + * not use this if you have issues with the compiled templates not being updated |
|
| 68 | + * but if you do need this it's most likely that you should file a bug report. |
|
| 69 | + * |
|
| 70 | + * @var bool |
|
| 71 | + */ |
|
| 72 | + protected $compilationEnforced; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Caches the results of the file checks to save some time when the same |
|
| 76 | + * templates is rendered several times. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected static $cache = array( |
|
| 81 | + 'cached' => array(), |
|
| 82 | + 'compiled' => array() |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Holds the compiler that built this template. |
|
| 87 | + * |
|
| 88 | + * @var ICompiler |
|
| 89 | + */ |
|
| 90 | + protected $compiler; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Chmod value for all files written (cached or compiled ones). |
|
| 94 | + * set to null if you don't want any chmod operation to happen |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + protected $chmod = 0777; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Containing template string. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + protected $template; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Creates a template from a string. |
|
| 109 | + * |
|
| 110 | + * @param string $templateString the template to use |
|
| 111 | + * @param int $cacheTime duration of the cache validity for this template, |
|
| 112 | + * if null it defaults to the Dwoo instance that will |
|
| 113 | + * render this template, set to -1 for infinite cache or 0 to disable |
|
| 114 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 115 | + * makes this template's content unique, if null it defaults |
|
| 116 | + * to the current url |
|
| 117 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 118 | + * template from others, if null it defaults to the md4 hash of the template |
|
| 119 | + */ |
|
| 120 | + public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
| 121 | + { |
|
| 122 | + $this->template = $templateString; |
|
| 123 | + $this->name = hash('md4', $templateString); |
|
| 124 | + $this->cacheTime = $cacheTime; |
|
| 125 | + |
|
| 126 | + if ($compileId !== null) { |
|
| 127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($cacheId !== null) { |
|
| 131 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Returns the cache duration for this template. |
|
| 137 | + * defaults to null if it was not provided |
|
| 138 | + * |
|
| 139 | + * @return int|null |
|
| 140 | + */ |
|
| 141 | + public function getCacheTime() |
|
| 142 | + { |
|
| 143 | + return $this->cacheTime; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Sets the cache duration for this template. |
|
| 148 | + * can be used to set it after the object is created if you did not provide |
|
| 149 | + * it in the constructor |
|
| 150 | + * |
|
| 151 | + * @param int $seconds duration of the cache validity for this template, if |
|
| 152 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 153 | + * -1 = infinite cache |
|
| 154 | + */ |
|
| 155 | + public function setCacheTime($seconds = null) |
|
| 156 | + { |
|
| 157 | + $this->cacheTime = $seconds; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Returns the chmod value for all files written (cached or compiled ones). |
|
| 162 | + * defaults to 0777 |
|
| 163 | + * |
|
| 164 | + * @return int|null |
|
| 165 | + */ |
|
| 166 | + public function getChmod() |
|
| 167 | + { |
|
| 168 | + return $this->chmod; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Set the chmod value for all files written (cached or compiled ones). |
|
| 173 | + * set to null if you don't want to do any chmod() operation |
|
| 174 | + * |
|
| 175 | + * @param int $mask new bitmask to use for all files |
|
| 176 | + */ |
|
| 177 | + public function setChmod($mask = null) |
|
| 178 | + { |
|
| 179 | + $this->chmod = $mask; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the template name. |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + public function getName() |
|
| 188 | + { |
|
| 189 | + return $this->name; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Returns the resource name for this template class. |
|
| 194 | + * |
|
| 195 | + * @return string |
|
| 196 | + */ |
|
| 197 | + public function getResourceName() |
|
| 198 | + { |
|
| 199 | + return 'string'; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
| 204 | + * |
|
| 205 | + * @return false |
|
| 206 | + */ |
|
| 207 | + public function getResourceIdentifier() |
|
| 208 | + { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Returns the template source of this template. |
|
| 214 | + * |
|
| 215 | + * @return string |
|
| 216 | + */ |
|
| 217 | + public function getSource() |
|
| 218 | + { |
|
| 219 | + return $this->template; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Returns an unique value identifying the current version of this template, |
|
| 224 | + * in this case it's the md4 hash of the content. |
|
| 225 | + * |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + public function getUid() |
|
| 229 | + { |
|
| 230 | + return $this->name; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 235 | + * |
|
| 236 | + * @return ICompiler |
|
| 237 | + */ |
|
| 238 | + public function getCompiler() |
|
| 239 | + { |
|
| 240 | + return $this->compiler; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Marks this template as compile-forced, which means it will be recompiled even if it |
|
| 245 | + * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
| 246 | + * it's only meant to be used in development (and the development of dwoo particularly). |
|
| 247 | + */ |
|
| 248 | + public function forceCompilation() |
|
| 249 | + { |
|
| 250 | + $this->compilationEnforced = true; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 255 | + * or false if it's not cached. |
|
| 256 | + * |
|
| 257 | + * @param Core $core the dwoo instance that requests it |
|
| 258 | + * |
|
| 259 | + * @return string|bool |
|
| 260 | + */ |
|
| 261 | + public function getCachedTemplate(Core $core) |
|
| 262 | + { |
|
| 263 | + if ($this->cacheTime !== null) { |
|
| 264 | + $cacheLength = $this->cacheTime; |
|
| 265 | + } else { |
|
| 266 | + $cacheLength = $core->getCacheTime(); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // file is not cacheable |
|
| 270 | + if ($cacheLength == 0) { |
|
| 271 | + return false; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + $cachedFile = $this->getCacheFilename($core); |
|
| 275 | + |
|
| 276 | + if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
| 277 | + // already checked, return cache file |
|
| 278 | + return $cachedFile; |
|
| 279 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | + // cache is still valid and can be loaded |
|
| 281 | + self::$cache['cached'][$this->cacheId] = true; |
|
| 282 | + |
|
| 283 | + return $cachedFile; |
|
| 284 | + } else { |
|
| 285 | + // file is cacheable |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Caches the provided output into the cache file. |
|
| 292 | + * |
|
| 293 | + * @param Core $core the dwoo instance that requests it |
|
| 294 | + * @param string $output the template output |
|
| 295 | + * |
|
| 296 | + * @return mixed full path of the cached file or false upon failure |
|
| 297 | + */ |
|
| 298 | + public function cache(Core $core, $output) |
|
| 299 | + { |
|
| 300 | + $cacheDir = $core->getCacheDir(); |
|
| 301 | + $cachedFile = $this->getCacheFilename($core); |
|
| 302 | + |
|
| 303 | + // the code below is courtesy of Rasmus Schultz, |
|
| 304 | + // thanks for his help on avoiding concurency issues |
|
| 305 | + $temp = tempnam($cacheDir, 'temp'); |
|
| 306 | + if (!($file = @fopen($temp, 'wb'))) { |
|
| 307 | + $temp = $cacheDir . uniqid('temp'); |
|
| 308 | + if (!($file = @fopen($temp, 'wb'))) { |
|
| 309 | + trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 310 | + |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + fwrite($file, $output); |
|
| 316 | + fclose($file); |
|
| 317 | + |
|
| 318 | + $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
| 319 | + if (!@rename($temp, $cachedFile)) { |
|
| 320 | + @unlink($cachedFile); |
|
| 321 | + @rename($temp, $cachedFile); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if ($this->chmod !== null) { |
|
| 325 | + chmod($cachedFile, $this->chmod); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + self::$cache['cached'][$this->cacheId] = true; |
|
| 329 | + |
|
| 330 | + return $cachedFile; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Clears the cached template if it's older than the given time. |
|
| 335 | + * |
|
| 336 | + * @param Core $core the dwoo instance that was used to cache that template |
|
| 337 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 338 | + * |
|
| 339 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 340 | + */ |
|
| 341 | + public function clearCache(Core $core, $olderThan = - 1) |
|
| 342 | + { |
|
| 343 | + $cachedFile = $this->getCacheFilename($core); |
|
| 344 | + |
|
| 345 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Returns the compiled template file name. |
|
| 350 | + * |
|
| 351 | + * @param Core $core the dwoo instance that requests it |
|
| 352 | + * @param ICompiler $compiler the compiler that must be used |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
| 357 | + { |
|
| 358 | + $compiledFile = $this->getCompiledFilename($core); |
|
| 359 | + |
|
| 360 | + if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
| 361 | + // already checked, return compiled file |
|
| 362 | + } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
| 363 | + // template is compiled |
|
| 364 | + self::$cache['compiled'][$this->compileId] = true; |
|
| 365 | + } else { |
|
| 366 | + // compiles the template |
|
| 367 | + $this->compilationEnforced = false; |
|
| 368 | + |
|
| 369 | + if ($compiler === null) { |
|
| 370 | + $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
| 371 | + |
|
| 372 | + if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
| 373 | + $compiler = Compiler::compilerFactory(); |
|
| 374 | + } else { |
|
| 375 | + $compiler = call_user_func($compiler); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $this->compiler = $compiler; |
|
| 380 | + |
|
| 381 | + $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
| 382 | + $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
| 383 | + $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
| 384 | + file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
| 385 | + if ($this->chmod !== null) { |
|
| 386 | + chmod($compiledFile, $this->chmod); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if (extension_loaded('Zend OPcache')) { |
|
| 390 | + opcache_invalidate($compiledFile); |
|
| 391 | + } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
| 392 | + apc_delete_file($compiledFile); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + self::$cache['compiled'][$this->compileId] = true; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + return $compiledFile; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Checks if compiled file is valid (it exists). |
|
| 403 | + * |
|
| 404 | + * @param string $file |
|
| 405 | + * |
|
| 406 | + * @return bool True cache file existance |
|
| 407 | + */ |
|
| 408 | + protected function isValidCompiledFile($file) |
|
| 409 | + { |
|
| 410 | + return file_exists($file); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Returns a new template string object with the resource id being the template source code. |
|
| 415 | + * |
|
| 416 | + * @param Core $core the dwoo instance requiring it |
|
| 417 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
| 418 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 419 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 420 | + * instance that will render this template |
|
| 421 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 422 | + * template's content unique, if null it defaults to the current url makes this |
|
| 423 | + * template's content unique, if null it defaults to the current url |
|
| 424 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 425 | + * others, if null it defaults to the filename+bits of the path template from |
|
| 426 | + * others, if null it defaults to the filename+bits of the path |
|
| 427 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 428 | + * extends or any other plugin) an include, extends or any other plugin) |
|
| 429 | + * |
|
| 430 | + * @return $this |
|
| 431 | + */ |
|
| 432 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 433 | + { |
|
| 434 | + return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Returns the full compiled file name and assigns a default value to it if |
|
| 439 | + * required. |
|
| 440 | + * |
|
| 441 | + * @param Core $core the dwoo instance that requests the file name |
|
| 442 | + * |
|
| 443 | + * @return string the full path to the compiled file |
|
| 444 | + */ |
|
| 445 | + protected function getCompiledFilename(Core $core) |
|
| 446 | + { |
|
| 447 | + // no compile id was provided, set default |
|
| 448 | + if ($this->compileId === null) { |
|
| 449 | + $this->compileId = $this->name; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Returns the full cached file name and assigns a default value to it if |
|
| 457 | + * required. |
|
| 458 | + * |
|
| 459 | + * @param Core $core the dwoo instance that requests the file name |
|
| 460 | + * |
|
| 461 | + * @return string the full path to the cached file |
|
| 462 | + */ |
|
| 463 | + protected function getCacheFilename(Core $core) |
|
| 464 | + { |
|
| 465 | + // no cache id provided, use request_uri as default |
|
| 466 | + if ($this->cacheId === null) { |
|
| 467 | + if (isset($_SERVER['REQUEST_URI']) === true) { |
|
| 468 | + $cacheId = $_SERVER['REQUEST_URI']; |
|
| 469 | + } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
| 470 | + $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 471 | + } else { |
|
| 472 | + $cacheId = ''; |
|
| 473 | + } |
|
| 474 | + // force compiled id generation |
|
| 475 | + $this->getCompiledFilename($core); |
|
| 476 | + |
|
| 477 | + $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Returns some php code that will check if this template has been modified or not. |
|
| 485 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 486 | + * |
|
| 487 | + * @return string |
|
| 488 | + */ |
|
| 489 | + public function getIsModifiedCode() |
|
| 490 | + { |
|
| 491 | + return null; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Ensures the given path exists. |
|
| 496 | + * |
|
| 497 | + * @param string $path any path |
|
| 498 | + * @param string $baseDir the base directory where the directory is created |
|
| 499 | + * ($path must still contain the full path, $baseDir |
|
| 500 | + * is only used for unix permissions) |
|
| 501 | + * |
|
| 502 | + * @throws Exception |
|
| 503 | + */ |
|
| 504 | + protected function makeDirectory($path, $baseDir = null) |
|
| 505 | + { |
|
| 506 | + if (is_dir($path) === true) { |
|
| 507 | + return; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + if ($this->chmod === null) { |
|
| 511 | + $chmod = 0777; |
|
| 512 | + } else { |
|
| 513 | + $chmod = $this->chmod; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + $retries = 3; |
|
| 517 | + while ($retries --) { |
|
| 518 | + @mkdir($path, $chmod, true); |
|
| 519 | + if (is_dir($path)) { |
|
| 520 | + break; |
|
| 521 | + } |
|
| 522 | + usleep(20); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + // enforce the correct mode for all directories created |
|
| 526 | + if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
| 527 | + $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
| 528 | + $folders = explode('/', trim($path, '/')); |
|
| 529 | + foreach ($folders as $folder) { |
|
| 530 | + $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 531 | + if (!chmod($baseDir, $chmod)) { |
|
| 532 | + throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | 537 | } |
@@ -124,11 +124,11 @@ discard block |
||
| 124 | 124 | $this->cacheTime = $cacheTime; |
| 125 | 125 | |
| 126 | 126 | if ($compileId !== null) { |
| 127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | if ($cacheId !== null) { |
| 131 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 131 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
| 277 | 277 | // already checked, return cache file |
| 278 | 278 | return $cachedFile; |
| 279 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 279 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME']-$cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | 280 | // cache is still valid and can be loaded |
| 281 | 281 | self::$cache['cached'][$this->cacheId] = true; |
| 282 | 282 | |
@@ -304,9 +304,9 @@ discard block |
||
| 304 | 304 | // thanks for his help on avoiding concurency issues |
| 305 | 305 | $temp = tempnam($cacheDir, 'temp'); |
| 306 | 306 | if (!($file = @fopen($temp, 'wb'))) { |
| 307 | - $temp = $cacheDir . uniqid('temp'); |
|
| 307 | + $temp = $cacheDir.uniqid('temp'); |
|
| 308 | 308 | if (!($file = @fopen($temp, 'wb'))) { |
| 309 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 309 | + trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); |
|
| 310 | 310 | |
| 311 | 311 | return false; |
| 312 | 312 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | { |
| 343 | 343 | $cachedFile = $this->getCacheFilename($core); |
| 344 | 344 | |
| 345 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 345 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time()-$olderThan) && unlink($cachedFile)); |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | /** |
@@ -449,7 +449,7 @@ discard block |
||
| 449 | 449 | $this->compileId = $this->name; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 452 | + return $core->getCompileDir().$this->compileId.'.d'.Core::RELEASE_TAG.'.php'; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -467,17 +467,17 @@ discard block |
||
| 467 | 467 | if (isset($_SERVER['REQUEST_URI']) === true) { |
| 468 | 468 | $cacheId = $_SERVER['REQUEST_URI']; |
| 469 | 469 | } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
| 470 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 470 | + $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); |
|
| 471 | 471 | } else { |
| 472 | 472 | $cacheId = ''; |
| 473 | 473 | } |
| 474 | 474 | // force compiled id generation |
| 475 | 475 | $this->getCompiledFilename($core); |
| 476 | 476 | |
| 477 | - $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 477 | + $this->cacheId = str_replace('../', '__', $this->compileId.strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 480 | + return $core->getCacheDir().$this->cacheId.'.html'; |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | /** |
@@ -514,7 +514,7 @@ discard block |
||
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | $retries = 3; |
| 517 | - while ($retries --) { |
|
| 517 | + while ($retries--) { |
|
| 518 | 518 | @mkdir($path, $chmod, true); |
| 519 | 519 | if (is_dir($path)) { |
| 520 | 520 | break; |
@@ -527,9 +527,9 @@ discard block |
||
| 527 | 527 | $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
| 528 | 528 | $folders = explode('/', trim($path, '/')); |
| 529 | 529 | foreach ($folders as $folder) { |
| 530 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 530 | + $baseDir .= $folder.DIRECTORY_SEPARATOR; |
|
| 531 | 531 | if (!chmod($baseDir, $chmod)) { |
| 532 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 532 | + throw new Exception('Unable to chmod '."$baseDir to $chmod: ".print_r(error_get_last(), true)); |
|
| 533 | 533 | } |
| 534 | 534 | } |
| 535 | 535 | } |
@@ -36,93 +36,93 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | function PluginMailto(Core $dwoo, $address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null) |
| 38 | 38 | { |
| 39 | - if (empty($address)) { |
|
| 40 | - return ''; |
|
| 41 | - } |
|
| 42 | - if (empty($text)) { |
|
| 43 | - $text = $address; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - // build address string |
|
| 47 | - $address .= '?'; |
|
| 48 | - |
|
| 49 | - if (!empty($subject)) { |
|
| 50 | - $address .= 'subject=' . rawurlencode($subject) . '&'; |
|
| 51 | - } |
|
| 52 | - if (!empty($cc)) { |
|
| 53 | - $address .= 'cc=' . rawurlencode($cc) . '&'; |
|
| 54 | - } |
|
| 55 | - if (!empty($bcc)) { |
|
| 56 | - $address .= 'bcc=' . rawurlencode($bcc) . '&'; |
|
| 57 | - } |
|
| 58 | - if (!empty($newsgroups)) { |
|
| 59 | - $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&'; |
|
| 60 | - } |
|
| 61 | - if (!empty($followupto)) { |
|
| 62 | - $address .= 'followupto=' . rawurlencode($followupto) . '&'; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $address = rtrim($address, '?&'); |
|
| 66 | - |
|
| 67 | - // output |
|
| 68 | - switch ($encode) { |
|
| 69 | - |
|
| 70 | - case 'none': |
|
| 71 | - case null: |
|
| 72 | - return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 73 | - |
|
| 74 | - case 'js': |
|
| 75 | - case 'javascript': |
|
| 76 | - $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
|
| 77 | - $len = strlen($str); |
|
| 78 | - |
|
| 79 | - $out = ''; |
|
| 80 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 81 | - $out .= '%' . bin2hex($str[$i]); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
|
| 85 | - |
|
| 86 | - break; |
|
| 87 | - case 'javascript_charcode': |
|
| 88 | - case 'js_charcode': |
|
| 89 | - case 'jscharcode': |
|
| 90 | - case 'jschar': |
|
| 91 | - $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 92 | - $len = strlen($str); |
|
| 93 | - |
|
| 94 | - $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 95 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 96 | - $out .= ord($str[$i]) . ','; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return rtrim($out, ',') . "));\n-->\n</script>\n"; |
|
| 100 | - |
|
| 101 | - break; |
|
| 102 | - |
|
| 103 | - case 'hex': |
|
| 104 | - if (strpos($address, '?') !== false) { |
|
| 105 | - $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $out = '<a href="mailto:'; |
|
| 109 | - $len = strlen($address); |
|
| 110 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 111 | - if (preg_match('#\w#', $address[$i])) { |
|
| 112 | - $out .= '%' . bin2hex($address[$i]); |
|
| 113 | - } else { |
|
| 114 | - $out .= $address[$i]; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - $out .= '" ' . $extra . '>'; |
|
| 118 | - $len = strlen($text); |
|
| 119 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 120 | - $out .= '&#x' . bin2hex($text[$i]); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return $out . '</a>'; |
|
| 124 | - |
|
| 125 | - default: |
|
| 126 | - $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
|
| 127 | - } |
|
| 39 | + if (empty($address)) { |
|
| 40 | + return ''; |
|
| 41 | + } |
|
| 42 | + if (empty($text)) { |
|
| 43 | + $text = $address; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + // build address string |
|
| 47 | + $address .= '?'; |
|
| 48 | + |
|
| 49 | + if (!empty($subject)) { |
|
| 50 | + $address .= 'subject=' . rawurlencode($subject) . '&'; |
|
| 51 | + } |
|
| 52 | + if (!empty($cc)) { |
|
| 53 | + $address .= 'cc=' . rawurlencode($cc) . '&'; |
|
| 54 | + } |
|
| 55 | + if (!empty($bcc)) { |
|
| 56 | + $address .= 'bcc=' . rawurlencode($bcc) . '&'; |
|
| 57 | + } |
|
| 58 | + if (!empty($newsgroups)) { |
|
| 59 | + $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&'; |
|
| 60 | + } |
|
| 61 | + if (!empty($followupto)) { |
|
| 62 | + $address .= 'followupto=' . rawurlencode($followupto) . '&'; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $address = rtrim($address, '?&'); |
|
| 66 | + |
|
| 67 | + // output |
|
| 68 | + switch ($encode) { |
|
| 69 | + |
|
| 70 | + case 'none': |
|
| 71 | + case null: |
|
| 72 | + return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 73 | + |
|
| 74 | + case 'js': |
|
| 75 | + case 'javascript': |
|
| 76 | + $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
|
| 77 | + $len = strlen($str); |
|
| 78 | + |
|
| 79 | + $out = ''; |
|
| 80 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 81 | + $out .= '%' . bin2hex($str[$i]); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
|
| 85 | + |
|
| 86 | + break; |
|
| 87 | + case 'javascript_charcode': |
|
| 88 | + case 'js_charcode': |
|
| 89 | + case 'jscharcode': |
|
| 90 | + case 'jschar': |
|
| 91 | + $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 92 | + $len = strlen($str); |
|
| 93 | + |
|
| 94 | + $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 95 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 96 | + $out .= ord($str[$i]) . ','; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return rtrim($out, ',') . "));\n-->\n</script>\n"; |
|
| 100 | + |
|
| 101 | + break; |
|
| 102 | + |
|
| 103 | + case 'hex': |
|
| 104 | + if (strpos($address, '?') !== false) { |
|
| 105 | + $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $out = '<a href="mailto:'; |
|
| 109 | + $len = strlen($address); |
|
| 110 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 111 | + if (preg_match('#\w#', $address[$i])) { |
|
| 112 | + $out .= '%' . bin2hex($address[$i]); |
|
| 113 | + } else { |
|
| 114 | + $out .= $address[$i]; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + $out .= '" ' . $extra . '>'; |
|
| 118 | + $len = strlen($text); |
|
| 119 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 120 | + $out .= '&#x' . bin2hex($text[$i]); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return $out . '</a>'; |
|
| 124 | + |
|
| 125 | + default: |
|
| 126 | + $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -67,62 +67,62 @@ |
||
| 67 | 67 | // output |
| 68 | 68 | switch ($encode) { |
| 69 | 69 | |
| 70 | - case 'none': |
|
| 71 | - case null: |
|
| 72 | - return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 73 | - |
|
| 74 | - case 'js': |
|
| 75 | - case 'javascript': |
|
| 76 | - $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
|
| 77 | - $len = strlen($str); |
|
| 78 | - |
|
| 79 | - $out = ''; |
|
| 80 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 81 | - $out .= '%' . bin2hex($str[$i]); |
|
| 70 | + case 'none': |
|
| 71 | + case null: |
|
| 72 | + return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 73 | + |
|
| 74 | + case 'js': |
|
| 75 | + case 'javascript': |
|
| 76 | + $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
|
| 77 | + $len = strlen($str); |
|
| 78 | + |
|
| 79 | + $out = ''; |
|
| 80 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 81 | + $out .= '%' . bin2hex($str[$i]); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
|
| 85 | + |
|
| 86 | + break; |
|
| 87 | + case 'javascript_charcode': |
|
| 88 | + case 'js_charcode': |
|
| 89 | + case 'jscharcode': |
|
| 90 | + case 'jschar': |
|
| 91 | + $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 92 | + $len = strlen($str); |
|
| 93 | + |
|
| 94 | + $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 95 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 96 | + $out .= ord($str[$i]) . ','; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return rtrim($out, ',') . "));\n-->\n</script>\n"; |
|
| 100 | + |
|
| 101 | + break; |
|
| 102 | + |
|
| 103 | + case 'hex': |
|
| 104 | + if (strpos($address, '?') !== false) { |
|
| 105 | + $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $out = '<a href="mailto:'; |
|
| 109 | + $len = strlen($address); |
|
| 110 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 111 | + if (preg_match('#\w#', $address[$i])) { |
|
| 112 | + $out .= '%' . bin2hex($address[$i]); |
|
| 113 | + } else { |
|
| 114 | + $out .= $address[$i]; |
|
| 82 | 115 | } |
| 116 | + } |
|
| 117 | + $out .= '" ' . $extra . '>'; |
|
| 118 | + $len = strlen($text); |
|
| 119 | + for ($i = 0; $i < $len; ++ $i) { |
|
| 120 | + $out .= '&#x' . bin2hex($text[$i]); |
|
| 121 | + } |
|
| 83 | 122 | |
| 84 | - return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
|
| 123 | + return $out . '</a>'; |
|
| 85 | 124 | |
| 86 | - break; |
|
| 87 | - case 'javascript_charcode': |
|
| 88 | - case 'js_charcode': |
|
| 89 | - case 'jscharcode': |
|
| 90 | - case 'jschar': |
|
| 91 | - $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 92 | - $len = strlen($str); |
|
| 93 | - |
|
| 94 | - $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 95 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 96 | - $out .= ord($str[$i]) . ','; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return rtrim($out, ',') . "));\n-->\n</script>\n"; |
|
| 100 | - |
|
| 101 | - break; |
|
| 102 | - |
|
| 103 | - case 'hex': |
|
| 104 | - if (strpos($address, '?') !== false) { |
|
| 105 | - $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $out = '<a href="mailto:'; |
|
| 109 | - $len = strlen($address); |
|
| 110 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 111 | - if (preg_match('#\w#', $address[$i])) { |
|
| 112 | - $out .= '%' . bin2hex($address[$i]); |
|
| 113 | - } else { |
|
| 114 | - $out .= $address[$i]; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - $out .= '" ' . $extra . '>'; |
|
| 118 | - $len = strlen($text); |
|
| 119 | - for ($i = 0; $i < $len; ++ $i) { |
|
| 120 | - $out .= '&#x' . bin2hex($text[$i]); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return $out . '</a>'; |
|
| 124 | - |
|
| 125 | - default: |
|
| 126 | - $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
|
| 125 | + default: |
|
| 126 | + $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
|
| 127 | 127 | } |
| 128 | 128 | } |
@@ -47,19 +47,19 @@ discard block |
||
| 47 | 47 | $address .= '?'; |
| 48 | 48 | |
| 49 | 49 | if (!empty($subject)) { |
| 50 | - $address .= 'subject=' . rawurlencode($subject) . '&'; |
|
| 50 | + $address .= 'subject='.rawurlencode($subject).'&'; |
|
| 51 | 51 | } |
| 52 | 52 | if (!empty($cc)) { |
| 53 | - $address .= 'cc=' . rawurlencode($cc) . '&'; |
|
| 53 | + $address .= 'cc='.rawurlencode($cc).'&'; |
|
| 54 | 54 | } |
| 55 | 55 | if (!empty($bcc)) { |
| 56 | - $address .= 'bcc=' . rawurlencode($bcc) . '&'; |
|
| 56 | + $address .= 'bcc='.rawurlencode($bcc).'&'; |
|
| 57 | 57 | } |
| 58 | 58 | if (!empty($newsgroups)) { |
| 59 | - $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&'; |
|
| 59 | + $address .= 'newsgroups='.rawurlencode($newsgroups).'&'; |
|
| 60 | 60 | } |
| 61 | 61 | if (!empty($followupto)) { |
| 62 | - $address .= 'followupto=' . rawurlencode($followupto) . '&'; |
|
| 62 | + $address .= 'followupto='.rawurlencode($followupto).'&'; |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $address = rtrim($address, '?&'); |
@@ -69,34 +69,34 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | case 'none': |
| 71 | 71 | case null: |
| 72 | - return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 72 | + return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
|
| 73 | 73 | |
| 74 | 74 | case 'js': |
| 75 | 75 | case 'javascript': |
| 76 | - $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
|
| 76 | + $str = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');'; |
|
| 77 | 77 | $len = strlen($str); |
| 78 | 78 | |
| 79 | 79 | $out = ''; |
| 80 | 80 | for ($i = 0; $i < $len; ++ $i) { |
| 81 | - $out .= '%' . bin2hex($str[$i]); |
|
| 81 | + $out .= '%'.bin2hex($str[$i]); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
|
| 84 | + return '<script type="text/javascript">eval(unescape(\''.$out.'\'));</script>'; |
|
| 85 | 85 | |
| 86 | 86 | break; |
| 87 | 87 | case 'javascript_charcode': |
| 88 | 88 | case 'js_charcode': |
| 89 | 89 | case 'jscharcode': |
| 90 | 90 | case 'jschar': |
| 91 | - $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
|
| 91 | + $str = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
|
| 92 | 92 | $len = strlen($str); |
| 93 | 93 | |
| 94 | - $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 94 | + $out = '<script type="text/javascript">'."\n<!--\ndocument.write(Str.fromCharCode("; |
|
| 95 | 95 | for ($i = 0; $i < $len; ++ $i) { |
| 96 | - $out .= ord($str[$i]) . ','; |
|
| 96 | + $out .= ord($str[$i]).','; |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - return rtrim($out, ',') . "));\n-->\n</script>\n"; |
|
| 99 | + return rtrim($out, ',')."));\n-->\n</script>\n"; |
|
| 100 | 100 | |
| 101 | 101 | break; |
| 102 | 102 | |
@@ -109,18 +109,18 @@ discard block |
||
| 109 | 109 | $len = strlen($address); |
| 110 | 110 | for ($i = 0; $i < $len; ++ $i) { |
| 111 | 111 | if (preg_match('#\w#', $address[$i])) { |
| 112 | - $out .= '%' . bin2hex($address[$i]); |
|
| 112 | + $out .= '%'.bin2hex($address[$i]); |
|
| 113 | 113 | } else { |
| 114 | 114 | $out .= $address[$i]; |
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | - $out .= '" ' . $extra . '>'; |
|
| 117 | + $out .= '" '.$extra.'>'; |
|
| 118 | 118 | $len = strlen($text); |
| 119 | 119 | for ($i = 0; $i < $len; ++ $i) { |
| 120 | - $out .= '&#x' . bin2hex($text[$i]); |
|
| 120 | + $out .= '&#x'.bin2hex($text[$i]); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - return $out . '</a>'; |
|
| 123 | + return $out.'</a>'; |
|
| 124 | 124 | |
| 125 | 125 | default: |
| 126 | 126 | $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
@@ -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 | } |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | protected function isValidCompiledFile($file) |
| 111 | 111 | { |
| 112 | - return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
| 112 | + return parent::isValidCompiledFile($file) && (int) $this->getUid() <= filemtime($file); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -147,12 +147,12 @@ discard block |
||
| 147 | 147 | } else { |
| 148 | 148 | foreach ($this->getIncludePath() as $path) { |
| 149 | 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; |
|
| 150 | + if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { |
|
| 151 | + return $this->resolvedPath = $path.DIRECTORY_SEPARATOR.$this->file; |
|
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
| 155 | + throw new DwooException('Template "'.$this->file.'" could not be found in any of your include path(s)'); |
|
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | */ |
| 165 | 165 | public function getUid() |
| 166 | 166 | { |
| 167 | - return (string)filemtime($this->getResourceIdentifier()); |
|
| 167 | + return (string) filemtime($this->getResourceIdentifier()); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
| 224 | 224 | } |
| 225 | 225 | } else { |
| 226 | - $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
| 226 | + $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; |
|
| 227 | 227 | if (file_exists($resourceId) === false) { |
| 228 | 228 | return null; |
| 229 | 229 | } |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | if ($policy = $core->getSecurityPolicy()) { |
| 237 | 237 | while (true) { |
| 238 | 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>.'); |
|
| 239 | + throw new SecurityException('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | if ($includePath) { |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | break 2; |
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | - throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
| 253 | + throw new SecurityException('The security policy prevents you to read <em>'.$resourceId.'</em>'); |
|
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - return $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 280 | + return $this->compileId.'.d'.Core::RELEASE_TAG.'.php'; |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | /** |
@@ -288,6 +288,6 @@ discard block |
||
| 288 | 288 | */ |
| 289 | 289 | public function getIsModifiedCode() |
| 290 | 290 | { |
| 291 | - return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
| 291 | + return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; |
|
| 292 | 292 | } |
| 293 | 293 | } |