o2system /
parser
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Parser\Template\Engines; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Parser\Template\Abstracts\AbstractEngine; |
||||
| 19 | use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Class Noodle |
||||
| 23 | * |
||||
| 24 | * @package O2System\Parser\Template\Engines |
||||
| 25 | */ |
||||
| 26 | class Noodle extends AbstractEngine |
||||
| 27 | { |
||||
| 28 | use ConfigCollectorTrait; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * Noodle::$extensions |
||||
| 32 | * |
||||
| 33 | * List of noodle file extensions. |
||||
| 34 | * |
||||
| 35 | * @var array |
||||
| 36 | */ |
||||
| 37 | protected $extensions = [ |
||||
| 38 | '.php', |
||||
| 39 | '.htm', |
||||
| 40 | '.html', |
||||
| 41 | '.noodle.php', |
||||
| 42 | '.noodle.phtml', |
||||
| 43 | '.phtml', |
||||
| 44 | ]; |
||||
| 45 | |||||
| 46 | // ------------------------------------------------------------------------ |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * Noodle::__construct |
||||
| 50 | * |
||||
| 51 | * @param array $config |
||||
| 52 | */ |
||||
| 53 | public function __construct(array $config = []) |
||||
| 54 | { |
||||
| 55 | $this->config = array_merge([ |
||||
| 56 | 'allowPhpGlobals' => true, |
||||
| 57 | 'allowPhpFunctions' => true, |
||||
| 58 | 'allowPhpConstants' => true, |
||||
| 59 | ], $config); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | // ------------------------------------------------------------------------ |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * Noodle::parseString |
||||
| 66 | * |
||||
| 67 | * @param string $string |
||||
| 68 | * @param array $vars |
||||
| 69 | * |
||||
| 70 | * @return false|string Returns FALSE if failed. |
||||
| 71 | * @throws \Exception |
||||
| 72 | */ |
||||
| 73 | public function parseString($string, array $vars = []) |
||||
| 74 | { |
||||
| 75 | if ($this->config[ 'allowPhpGlobals' ] === false) { |
||||
| 76 | $string = str_replace( |
||||
| 77 | [ |
||||
| 78 | '{{$GLOBALS}}', |
||||
| 79 | '{{$GLOBALS[%%]}}', |
||||
| 80 | '{{$_SERVER}}', |
||||
| 81 | '{{$_SERVER[%%]}}', |
||||
| 82 | '{{$_GET}}', |
||||
| 83 | '{{$_GET[%%]}}', |
||||
| 84 | '{{$_POST}}', |
||||
| 85 | '{{$_POST[%%]}}', |
||||
| 86 | '{{$_FILES}}', |
||||
| 87 | '{{$_FILES[%%]}}', |
||||
| 88 | '{{$_COOKIE}}', |
||||
| 89 | '{{$_COOKIE[%%]}}', |
||||
| 90 | '{{$_SESSION}}', |
||||
| 91 | '{{$_SESSION[%%]}}', |
||||
| 92 | '{{$_REQUEST}}', |
||||
| 93 | '{{$_REQUEST[%%]}}', |
||||
| 94 | '{{$_ENV}}', |
||||
| 95 | '{{$_ENV[%%]}}', |
||||
| 96 | |||||
| 97 | // with spaces |
||||
| 98 | '{{ $GLOBALS }}', |
||||
| 99 | '{{ $GLOBALS[%%] }}', |
||||
| 100 | '{{ $_SERVER }}', |
||||
| 101 | '{{ $_SERVER[%%] }}', |
||||
| 102 | '{{ $_GET }}', |
||||
| 103 | '{{ $_GET[%%] }}', |
||||
| 104 | '{{ $_POST }}', |
||||
| 105 | '{{ $_POST[%%] }}', |
||||
| 106 | '{{ $_FILES }}', |
||||
| 107 | '{{ $_FILES[%%] }}', |
||||
| 108 | '{{ $_COOKIE }}', |
||||
| 109 | '{{ $_COOKIE[%%] }}', |
||||
| 110 | '{{ $_SESSION }}', |
||||
| 111 | '{{ $_SESSION[%%] }}', |
||||
| 112 | '{{ $_REQUEST }}', |
||||
| 113 | '{{ $_REQUEST[%%] }}', |
||||
| 114 | '{{ $_ENV }}', |
||||
| 115 | '{{ $_ENV[%%] }}', |
||||
| 116 | ], |
||||
| 117 | '', |
||||
| 118 | $string |
||||
| 119 | ); |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | // php logical codes |
||||
| 123 | $logicalCodes = [ |
||||
| 124 | '{{if(%%)}}' => '<?php if(\1): ?>', |
||||
| 125 | '{{elseif(%%)}}' => '<?php elseif(\1): ?>', |
||||
| 126 | '{{/if}}' => '<?php endif; ?>', |
||||
| 127 | '{{endif}}' => '<?php endif; ?>', |
||||
| 128 | '{{else}}' => '<?php else: ?>', |
||||
| 129 | '{{unless(%%)}}' => '<?php if(\1): ?>', |
||||
| 130 | '{{endunless}}' => '<?php endif; ?>', |
||||
| 131 | |||||
| 132 | // with spaces |
||||
| 133 | '{{ if(%%) }}' => '<?php if(\1): ?>', |
||||
| 134 | '{{ elseif(%%) }}' => '<?php elseif(\1): ?>', |
||||
| 135 | '{{ /if }}' => '<?php endif; ?>', |
||||
| 136 | '{{ endif }}' => '<?php endif; ?>', |
||||
| 137 | '{{ else }}' => '<?php else: ?>', |
||||
| 138 | '{{ unless(%%) }}' => '<?php if(\1): ?>', |
||||
| 139 | '{{ endunless }}' => '<?php endif; ?>', |
||||
| 140 | ]; |
||||
| 141 | |||||
| 142 | // php loop codes |
||||
| 143 | $loopCodes = [ |
||||
| 144 | '{{for(%%)}}' => '<?php for(\1): ?>', |
||||
| 145 | '{{/for}}' => '<?php endfor; ?>', |
||||
| 146 | '{{endfor}}' => '<?php endfor; ?>', |
||||
| 147 | '{{foreach(%%)}}' => '<?php foreach(\1): ?>', |
||||
| 148 | '{{/foreach}}' => '<?php endforeach; ?>', |
||||
| 149 | '{{endforeach}}' => '<?php endforeach; ?>', |
||||
| 150 | '{{while(%%)}}' => '<?php while(\1): ?>', |
||||
| 151 | '{{/while}}' => '<?php endwhile; ?>', |
||||
| 152 | '{{endwhile}}' => '<?php endwhile; ?>', |
||||
| 153 | '{{continue}}' => '<?php continue; ?>', |
||||
| 154 | '{{break}}' => '<?php break; ?>', |
||||
| 155 | |||||
| 156 | // with spaces |
||||
| 157 | '{{ for(%%) }}' => '<?php for(\1): ?>', |
||||
| 158 | '{{ /for }}' => '<?php endfor; ?>', |
||||
| 159 | '{{ endfor }}' => '<?php endfor; ?>', |
||||
| 160 | '{{ foreach(%%) }}' => '<?php foreach(\1): ?>', |
||||
| 161 | '{{ /foreach }}' => '<?php endforeach; ?>', |
||||
| 162 | '{{ endforeach }}' => '<?php endforeach; ?>', |
||||
| 163 | '{{ while(%%) }}' => '<?php while(\1): ?>', |
||||
| 164 | '{{ /while }}' => '<?php endwhile; ?>', |
||||
| 165 | '{{ endwhile }}' => '<?php endwhile; ?>', |
||||
| 166 | '{{ continue }}' => '<?php continue; ?>', |
||||
| 167 | '{{ break }}' => '<?php break; ?>', |
||||
| 168 | ]; |
||||
| 169 | |||||
| 170 | // php function codes |
||||
| 171 | $functionsCodes = []; |
||||
| 172 | if ($this->config[ 'allowPhpFunctions' ] === false) { |
||||
| 173 | $functionsCodes = [ |
||||
| 174 | '{{%%(%%)}}' => '', |
||||
| 175 | ]; |
||||
| 176 | } elseif (is_array($this->config[ 'allowPhpFunctions' ]) AND count( |
||||
| 177 | $this->config[ 'allowPhpFunctions' ] |
||||
| 178 | ) > 0 |
||||
| 179 | ) { |
||||
| 180 | foreach ($this->config[ 'allowPhpFunctions' ] as $function_name) { |
||||
| 181 | if (function_exists($function_name)) { |
||||
| 182 | $functionsCodes[ '{{' . $function_name . '(%%)}}' ] = '<?php echo ' . $function_name . '(\1); ?>'; |
||||
| 183 | } |
||||
| 184 | } |
||||
| 185 | } else { |
||||
| 186 | $functionsCodes = [ |
||||
| 187 | '{{%%()}}' => '<?php echo \1(); ?>', |
||||
| 188 | '{{%%(%%)}}' => '<?php echo \1(\2); ?>', |
||||
| 189 | '{{lang(%%)}}' => '<?php echo $language->getLine(\1); ?>', |
||||
| 190 | '{{each(%%, %%, %%)}}' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||||
| 191 | '{{include(%%)}}' => '<?php echo $this->parseFile(\1); ?>', |
||||
| 192 | '{{include(%%, %%)}}' => '<?php echo $this->parseFile(\1, \2); ?>', |
||||
| 193 | |||||
| 194 | // with spaces |
||||
| 195 | '{{ %%() }}' => '<?php echo \1(); ?>', |
||||
| 196 | '{{ %%(%%) }}' => '<?php echo \1(\2); ?>', |
||||
| 197 | '{{ lang(%%) }}' => '<?php echo $language->getLine(\1); ?>', |
||||
| 198 | '{{ each(%%, %%, %%) }}' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||||
| 199 | '{{ include(%%) }}' => '<?php echo $this->parseFile(\1); ?>', |
||||
| 200 | '{{ include(%%, %%) }}' => '<?php echo $this->parseFile(\1, \2); ?>', |
||||
| 201 | ]; |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | // php variables codes |
||||
| 205 | $variablesCodes = [ |
||||
| 206 | '{{%% ? %% : %%}}' => '<?php echo (\1 ? \2 : \3); ?>', |
||||
| 207 | '{{%% or %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>', |
||||
| 208 | '{{%% || %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>', |
||||
| 209 | '{{$%%->%%(%%)}}' => '<?php echo $\1->\2(\3); ?>', |
||||
| 210 | '{{$%%->%%}}' => '<?php echo @$\1->\2; ?>', |
||||
| 211 | '{{$%%[%%]}}' => '<?php echo @$\1[\2]; ?>', |
||||
| 212 | '{{$%%.%%}}' => '<?php echo @$\1[\2]; ?>', |
||||
| 213 | '{{$%% = %%}}' => '<?php $\1 = \2; ?>', |
||||
| 214 | '{{$%%++}}' => '<?php $\1++; ?>', |
||||
| 215 | '{{$%%--}}' => '<?php $\1--; ?>', |
||||
| 216 | '{{$%%}}' => '<?php echo (isset($\1) ? $\1 : ""); ?>', |
||||
| 217 | '{{/*}}' => '<?php /*', |
||||
| 218 | '{{*/}}' => '*/ ?>', |
||||
| 219 | '{{!!$%%!!}}' => '<?php echo htmlentities($\1, ENT_HTML5); ?>', |
||||
| 220 | '{{--%%--}}' => '', |
||||
| 221 | |||||
| 222 | // with spaces |
||||
| 223 | '{{ %% ? %% : %% }}' => '<?php echo (\1 ? \2 : \3); ?>', |
||||
| 224 | '{{ %% or %% }}' => '<?php echo ( empty(\1) ? \'\2\' : \1 ); ?>', |
||||
| 225 | '{{ %% || %% }}' => '<?php echo ( empty(\1) ? \'\2\' : \1 ); ?>', |
||||
| 226 | '{{ $%%->%%(%%) }}' => '<?php echo $\1->\2(\3); ?>', |
||||
| 227 | '{{ $%%->%% }}' => '<?php echo $\1->\2; ?>', |
||||
| 228 | '{{ $%%[%%] }}' => '<?php echo $\1->\2; ?>', |
||||
| 229 | '{{ $%%.%% }}' => '<?php echo $\1->\2; ?>', |
||||
| 230 | '{{ $%% = %% }}' => '<?php $\1 = \2; ?>', |
||||
| 231 | '{{ $%%++ }}' => '<?php $\1++; ?>', |
||||
| 232 | '{{ $%%-- }}' => '<?php $\1--; ?>', |
||||
| 233 | '{{ $%% }}' => '<?php echo (isset($\1) ? $\1 : ""); ?>', |
||||
| 234 | '{{ /* }}' => '<?php /*', |
||||
| 235 | '{{ */ }}' => '*/ ?>', |
||||
| 236 | '{{ !!$%%!! }}' => '<?php echo htmlentities($\1, ENT_HTML5); ?>', |
||||
| 237 | '{{ --%%-- }}' => '', |
||||
| 238 | ]; |
||||
| 239 | |||||
| 240 | if ($this->config[ 'allowPhpConstants' ] === true) { |
||||
| 241 | $constantsVariables = get_defined_constants(true); |
||||
| 242 | |||||
| 243 | if ( ! empty($constantsVariables[ 'user' ])) { |
||||
| 244 | foreach ($constantsVariables[ 'user' ] as $constant => $value) { |
||||
| 245 | if (defined($constant)) { |
||||
| 246 | $variablesCodes[ '{{' . $constant . '}}' ] = '<?php echo ' . $constant . '; ?>'; |
||||
| 247 | } |
||||
| 248 | } |
||||
| 249 | } |
||||
| 250 | } |
||||
| 251 | |||||
| 252 | $phpCodes = array_merge($logicalCodes, $loopCodes, $variablesCodes, $functionsCodes); |
||||
| 253 | |||||
| 254 | $patterns = $replace = []; |
||||
| 255 | foreach ($phpCodes as $tplCode => $phpCode) { |
||||
| 256 | $patterns[] = '#' . str_replace('%%', '(.+)', preg_quote($tplCode, '#')) . '#U'; |
||||
| 257 | $replace[] = $phpCode; |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | /*replace our pseudo language in template with php code*/ |
||||
| 261 | $string = preg_replace($patterns, $replace, $string); |
||||
| 262 | |||||
| 263 | extract($vars); |
||||
| 264 | |||||
| 265 | /* |
||||
| 266 | * Buffer the output |
||||
| 267 | * |
||||
| 268 | * We buffer the output for two reasons: |
||||
| 269 | * 1. Speed. You get a significant speed boost. |
||||
| 270 | * 2. So that the final rendered template can be post-processed by |
||||
| 271 | * the output class. Why do we need post processing? For one thing, |
||||
| 272 | * in order to show the elapsed page load time. Unless we can |
||||
| 273 | * intercept the content right before it's sent to the browser and |
||||
| 274 | * then stop the timer it won't be accurate. |
||||
| 275 | */ |
||||
| 276 | ob_start(); |
||||
| 277 | |||||
| 278 | try { |
||||
| 279 | echo @eval('?>' . @preg_replace('/;*\s*\?>/', '; ?>', $string)); |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 280 | } catch (\Exception $e) { |
||||
| 281 | throw new \Exception($e->getMessage(), $e->getCode(), $e); |
||||
| 282 | |||||
| 283 | } |
||||
| 284 | |||||
| 285 | $output = ob_get_contents(); |
||||
| 286 | @ob_end_clean(); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
ob_end_clean(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 287 | |||||
| 288 | return $output; |
||||
| 289 | } |
||||
| 290 | } |