[FEATURE] Remove size bound of included TypoScript files
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_tsparser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains the TypoScript parser class
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 80: class t3lib_TSparser
40 * 133: function parse($string,$matchObj='')
41 * 169: function nextDivider()
42 * 185: function parseSub(&$setup)
43 * 389: function rollParseSub($string,&$setup)
44 * 413: function getVal($string,$setup)
45 * 439: function setVal($string,&$setup,$value,$wipeOut=0)
46 * 485: function error($err,$num=2)
47 * 497: function checkIncludeLines($string, $cycle_counter=1, $returnFiles=false)
48 * 541: function checkIncludeLines_array($array)
49 *
50 * SECTION: Syntax highlighting
51 * 584: function doSyntaxHighlight($string,$lineNum='',$highlightBlockMode=0)
52 * 605: function regHighLight($code,$pointer,$strlen=-1)
53 * 623: function syntaxHighlight_print($lineNumDat,$highlightBlockMode)
54 *
55 * TOTAL FUNCTIONS: 12
56 * (This index is automatically created/updated by the extension "extdeveval")
57 *
58 */
59
60
61 /**
62 * The TypoScript parser
63 *
64 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
65 * @package TYPO3
66 * @subpackage t3lib
67 * @see t3lib_tstemplate, t3lib_BEfunc::getPagesTSconfig(), t3lib_userAuthGroup::fetchGroupData(), t3lib_TStemplate::generateConfig()
68 */
69 class t3lib_TSparser {
70 var $strict = 1; // If set, then key names cannot contain characters other than [:alnum:]_\.-
71
72 // Internal
73 var $setup = array(); // TypoScript hierarchy being build during parsing.
74 var $raw; // raw data, the input string exploded by LF
75 var $rawP; // pointer to entry in raw data array
76 var $lastComment = ''; // Holding the value of the last comment
77 var $commentSet = 0; // Internally set, used as internal flag to create a multi-line comment (one of those like /*... */)
78 var $multiLineEnabled = 0; // Internally set, when multiline value is accumulated
79 var $multiLineObject = ''; // Internally set, when multiline value is accumulated
80 var $multiLineValue = array(); // Internally set, when multiline value is accumulated
81 var $inBrace = 0; // Internally set, when in brace. Counter.
82 var $lastConditionTrue = 1; // For each condition this flag is set, if the condition is true, else it's cleared. Then it's used by the [ELSE] condition to determine if the next part should be parsed.
83 var $sections = array(); // Tracking all conditions found
84 var $sectionsMatch = array(); // Tracking all matching conditions found
85 var $syntaxHighLight = 0; // If set, then syntax highlight mode is on; Call the function syntaxHighlight() to use this function
86 var $highLightData = array(); // Syntax highlight data is accumulated in this array. Used by syntaxHighlight_print() to construct the output.
87 var $highLightData_bracelevel = array(); // Syntax highlight data keeping track of the curly brace level for each line
88
89 // Debugging, analysis:
90 var $regComments = 0; // DO NOT register the comments. This is default for the ordinary sitetemplate!
91 var $regLinenumbers = 0; // DO NOT register the linenumbers. This is default for the ordinary sitetemplate!
92 var $errors = array(); // Error accumulation array.
93 var $lineNumberOffset = 0; // Used for the error messages line number reporting. Set externally.
94 var $breakPointLN = 0; // Line for break point.
95 var $highLightStyles = array(
96 'prespace' => array('<span class="ts-prespace">', '</span>'), // Space before any content on a line
97 'objstr_postspace' => array('<span class="ts-objstr_postspace">', '</span>'), // Space after the object string on a line
98 'operator_postspace' => array('<span class="ts-operator_postspace">', '</span>'), // Space after the operator on a line
99 'operator' => array('<span class="ts-operator">', '</span>'), // The operator char
100 'value' => array('<span class="ts-value">', '</span>'), // The value of a line
101 'objstr' => array('<span class="ts-objstr">', '</span>'), // The object string of a line
102 'value_copy' => array('<span class="ts-value_copy">', '</span>'), // The value when the copy syntax (<) is used; that means the object reference
103 'value_unset' => array('<span class="ts-value_unset">', '</span>'), // The value when an object is unset. Should not exist.
104 'ignored' => array('<span class="ts-ignored">', '</span>'), // The "rest" of a line which will be ignored.
105 'default' => array('<span class="ts-default">', '</span>'), // The default style if none other is applied.
106 'comment' => array('<span class="ts-comment">', '</span>'), // Comment lines
107 'condition' => array('<span class="ts-condition">', '</span>'), // Conditions
108 'error' => array('<span class="ts-error">', '</span>'), // Error messages
109 'linenum' => array('<span class="ts-linenum">', '</span>'), // Line numbers
110 );
111 var $highLightBlockStyles = ''; // Additional attributes for the <span> tags for a blockmode line
112 var $highLightBlockStyles_basecolor = '#cccccc'; // The hex-HTML color for the blockmode
113
114 public $parentObject; //Instance of parentObject, used by t3lib_tsparser_ext
115
116 /**
117 * Start parsing the input TypoScript text piece. The result is stored in $this->setup
118 *
119 * @param string The TypoScript text
120 * @param object If is object, then this is used to match conditions found in the TypoScript code. If matchObj not specified, then no conditions will work! (Except [GLOBAL])
121 * @return void
122 */
123 function parse($string, $matchObj = '') {
124 $this->raw = explode(LF, $string);
125 $this->rawP = 0;
126 $pre = '[GLOBAL]';
127 while ($pre) {
128 if ($this->breakPointLN && $pre == '[_BREAK]') {
129 $this->error('Breakpoint at ' . ($this->lineNumberOffset + $this->rawP - 2) . ': Line content was "' . $this->raw[$this->rawP - 2] . '"', 1);
130 break;
131 }
132
133 if (strtoupper($pre) == '[GLOBAL]' || strtoupper($pre) == '[END]' || (!$this->lastConditionTrue && strtoupper($pre) == '[ELSE]')) {
134 $pre = trim($this->parseSub($this->setup));
135 $this->lastConditionTrue = 1;
136 } else {
137 if (strtoupper($pre) != '[ELSE]') {
138 $this->sections[md5($pre)] = $pre;
139 } // we're in a specific section. Therefore we log this section
140 if ((is_object($matchObj) && $matchObj->match($pre)) || $this->syntaxHighLight) {
141 if (strtoupper($pre) != '[ELSE]') {
142 $this->sectionsMatch[md5($pre)] = $pre;
143 }
144 $pre = trim($this->parseSub($this->setup));
145 $this->lastConditionTrue = 1;
146 } else {
147 $pre = trim($this->nextDivider());
148 $this->lastConditionTrue = 0;
149 }
150 }
151 }
152 if ($this->inBrace) {
153 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': The script is short of ' . $this->inBrace . ' end brace(s)', 1);
154 }
155 if ($this->multiLineEnabled) {
156 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': A multiline value section is not ended with a parenthesis!', 1);
157 }
158 $this->lineNumberOffset += count($this->raw) + 1;
159 }
160
161 /**
162 * Will search for the next condition. When found it will return the line content (the condition value) and have advanced the internal $this->rawP pointer to point to the next line after the condition.
163 *
164 * @return string The condition value
165 * @see parse()
166 */
167 function nextDivider() {
168 while (isset($this->raw[$this->rawP])) {
169 $line = ltrim($this->raw[$this->rawP]);
170 $this->rawP++;
171 if ($line && substr($line, 0, 1) == '[') {
172 return $line;
173 }
174 }
175 }
176
177 /**
178 * Parsing the $this->raw TypoScript lines from pointer, $this->rawP
179 *
180 * @param array Reference to the setup array in which to accumulate the values.
181 * @return string Returns the string of the condition found, the exit signal or possible nothing (if it completed parsing with no interruptions)
182 */
183 function parseSub(&$setup) {
184 global $TYPO3_CONF_VARS;
185
186 while (isset($this->raw[$this->rawP])) {
187 $line = ltrim($this->raw[$this->rawP]);
188 $lineP = $this->rawP;
189 $this->rawP++;
190 if ($this->syntaxHighLight) {
191 $this->regHighLight("prespace", $lineP, strlen($line));
192 }
193
194 // Breakpoint?
195 if ($this->breakPointLN && ($this->lineNumberOffset + $this->rawP - 1) == ($this->breakPointLN + 1)) { // by adding 1 we get that line processed
196 return '[_BREAK]';
197 }
198
199 // Set comment flag?
200 if (!$this->multiLineEnabled && substr($line, 0, 2) == '/*') {
201 $this->commentSet = 1;
202 }
203
204 if (!$this->commentSet && ($line || $this->multiLineEnabled)) { // If $this->multiLineEnabled we will go and get the line values here because we know, the first if() will be true.
205 if ($this->multiLineEnabled) { // If multiline is enabled. Escape by ')'
206 if (substr($line, 0, 1) == ')') { // Multiline ends...
207 if ($this->syntaxHighLight) {
208 $this->regHighLight("operator", $lineP, strlen($line) - 1);
209 }
210 $this->multiLineEnabled = 0; // Disable multiline
211 $theValue = implode($this->multiLineValue, LF);
212 if (strstr($this->multiLineObject, '.')) {
213 $this->setVal($this->multiLineObject, $setup, array($theValue)); // Set the value deeper.
214 } else {
215 $setup[$this->multiLineObject] = $theValue; // Set value regularly
216 if ($this->lastComment && $this->regComments) {
217 $setup[$this->multiLineObject . '..'] .= $this->lastComment;
218 }
219 if ($this->regLinenumbers) {
220 $setup[$this->multiLineObject . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1);
221 }
222 }
223 } else {
224 if ($this->syntaxHighLight) {
225 $this->regHighLight("value", $lineP);
226 }
227 $this->multiLineValue[] = $this->raw[($this->rawP - 1)];
228 }
229 } elseif ($this->inBrace == 0 && substr($line, 0, 1) == '[') { // Beginning of condition (only on level zero compared to brace-levels
230 if ($this->syntaxHighLight) {
231 $this->regHighLight("condition", $lineP);
232 }
233 return $line;
234 } else {
235 if (substr($line, 0, 1) == '[' && strtoupper(trim($line)) == '[GLOBAL]') { // Return if GLOBAL condition is set - no matter what.
236 if ($this->syntaxHighLight) {
237 $this->regHighLight("condition", $lineP);
238 }
239 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': On return to [GLOBAL] scope, the script was short of ' . $this->inBrace . ' end brace(s)', 1);
240 $this->inBrace = 0;
241 return $line;
242 } elseif (strcspn($line, '}#/') != 0) { // If not brace-end or comment
243 $varL = strcspn($line, ' {=<>:('); // Find object name string until we meet an operator
244 $objStrName = trim(substr($line, 0, $varL));
245 if ($this->syntaxHighLight) {
246 $this->regHighLight("objstr", $lineP, strlen(substr($line, $varL)));
247 }
248 if (strlen($objStrName)) {
249 $r = array();
250 if ($this->strict && preg_match('/[^[:alnum:]_\.-]/i', $objStrName, $r)) {
251 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object Name String, "' . htmlspecialchars($objStrName) . '" contains invalid character "' . $r[0] . '". Must be alphanumeric or one of: "_-."');
252 } else {
253 $line = ltrim(substr($line, $varL));
254 if ($this->syntaxHighLight) {
255 $this->regHighLight("objstr_postspace", $lineP, strlen($line));
256 if (strlen($line) > 0) {
257 $this->regHighLight("operator", $lineP, strlen($line) - 1);
258 $this->regHighLight("operator_postspace", $lineP, strlen(ltrim(substr($line, 1))));
259 }
260 }
261
262 // Checking for special TSparser properties (to change TS values at parsetime)
263 $match = array();
264 if (preg_match('/^:=([^\(]+)\((.+)\).*/', $line, $match)) {
265 $tsFunc = trim($match[1]);
266 $tsFuncArg = $match[2];
267 list ($currentValue) = $this->getVal($objStrName, $setup);
268
269 $tsFuncArg = str_replace(
270 array('\\\\', '\n', '\t'),
271 array('\\', LF, TAB),
272 $tsFuncArg
273 );
274
275 switch ($tsFunc) {
276 case 'prependString':
277 $newValue = $tsFuncArg . $currentValue;
278 break;
279 case 'appendString':
280 $newValue = $currentValue . $tsFuncArg;
281 break;
282 case 'removeString':
283 $newValue = str_replace($tsFuncArg, '', $currentValue);
284 break;
285 case 'replaceString':
286 list($fromStr, $toStr) = explode('|', $tsFuncArg, 2);
287 $newValue = str_replace($fromStr, $toStr, $currentValue);
288 break;
289 case 'addToList':
290 $newValue = (strcmp('', $currentValue) ? $currentValue . ',' : '') . trim($tsFuncArg);
291 break;
292 case 'removeFromList':
293 $existingElements = t3lib_div::trimExplode(',', $currentValue);
294 $removeElements = t3lib_div::trimExplode(',', $tsFuncArg);
295 if (count($removeElements)) {
296 $newValue = implode(',', array_diff($existingElements, $removeElements));
297 }
298 break;
299 default:
300 if (isset($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tsparser.php']['preParseFunc'][$tsFunc])) {
301 $hookMethod = $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tsparser.php']['preParseFunc'][$tsFunc];
302 $params = array('currentValue' => $currentValue, 'functionArgument' => $tsFuncArg);
303 $fakeThis = FALSE;
304 $newValue = t3lib_div::callUserFunction($hookMethod, $params, $fakeThis);
305 } else {
306 t3lib_div::sysLog('Missing function definition for ' . $tsFunc . ' on TypoScript line ' . $lineP, 'Core', 2);
307 }
308 }
309
310 if (isset($newValue)) {
311 $line = '= ' . $newValue;
312 }
313 }
314
315 switch (substr($line, 0, 1)) {
316 case '=':
317 if ($this->syntaxHighLight) {
318 $this->regHighLight('value', $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
319 }
320
321 if (strstr($objStrName, '.')) {
322 $value = array();
323 $value[0] = trim(substr($line, 1));
324 $this->setVal($objStrName, $setup, $value);
325 } else {
326 $setup[$objStrName] = trim(substr($line, 1));
327 if ($this->lastComment && $this->regComments) { // Setting comment..
328 $setup[$objStrName . '..'] .= $this->lastComment;
329 }
330 if ($this->regLinenumbers) {
331 $setup[$objStrName . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1);
332 }
333 }
334 break;
335 case '{':
336 $this->inBrace++;
337 if (strstr($objStrName, '.')) {
338 $exitSig = $this->rollParseSub($objStrName, $setup);
339 if ($exitSig) {
340 return $exitSig;
341 }
342 } else {
343 if (!isset($setup[$objStrName . '.'])) {
344 $setup[$objStrName . '.'] = array();
345 }
346 $exitSig = $this->parseSub($setup[$objStrName . '.']);
347 if ($exitSig) {
348 return $exitSig;
349 }
350 }
351 break;
352 case '(':
353 $this->multiLineObject = $objStrName;
354 $this->multiLineEnabled = 1;
355 $this->multiLineValue = array();
356 break;
357 case '<':
358 if ($this->syntaxHighLight) {
359 $this->regHighLight("value_copy", $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
360 }
361 $theVal = trim(substr($line, 1));
362 if (substr($theVal, 0, 1) == '.') {
363 $res = $this->getVal(substr($theVal, 1), $setup);
364 } else {
365 $res = $this->getVal($theVal, $this->setup);
366 }
367 $this->setVal($objStrName, $setup, unserialize(serialize($res)), 1); // unserialize(serialize(...)) may look stupid but is needed because of some reference issues. See Kaspers reply to "[TYPO3-core] good question" from December 15 2005.
368 break;
369 case '>':
370 if ($this->syntaxHighLight) {
371 $this->regHighLight("value_unset", $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
372 }
373 $this->setVal($objStrName, $setup, 'UNSET');
374 break;
375 default:
376 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object Name String, "' . htmlspecialchars($objStrName) . '" was not preceded by any operator, =<>({');
377 break;
378 }
379 }
380 $this->lastComment = '';
381 }
382 } elseif (substr($line, 0, 1) == '}') {
383 $this->inBrace--;
384 $this->lastComment = '';
385 if ($this->syntaxHighLight) {
386 $this->regHighLight("operator", $lineP, strlen($line) - 1);
387 }
388 if ($this->inBrace < 0) {
389 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': An end brace is in excess.', 1);
390 $this->inBrace = 0;
391 } else {
392 break;
393 }
394 } else {
395 if ($this->syntaxHighLight) {
396 $this->regHighLight("comment", $lineP);
397 }
398
399 // Comment. The comments are concatenated in this temporary string:
400 if ($this->regComments) {
401 $this->lastComment .= trim($line) . LF;
402 }
403 }
404 }
405 }
406
407 // Unset comment
408 if ($this->commentSet) {
409 if ($this->syntaxHighLight) {
410 $this->regHighLight("comment", $lineP);
411 }
412 if (substr($line, 0, 2) == '*/') {
413 $this->commentSet = 0;
414 }
415 }
416 }
417 }
418
419 /**
420 * Parsing of TypoScript keys inside a curly brace where the key is composite of at least two keys, thus having to recursively call itself to get the value
421 *
422 * @param string The object sub-path, eg "thisprop.another_prot"
423 * @param array The local setup array from the function calling this function
424 * @return string Returns the exitSignal
425 * @see parseSub()
426 */
427 function rollParseSub($string, &$setup) {
428 if ((string) $string != '') {
429 $keyLen = strcspn($string, '.');
430 if ($keyLen == strlen($string)) {
431 $key = $string . '.';
432 if (!isset($setup[$key])) {
433 $setup[$key] = array();
434 }
435 $exitSig = $this->parseSub($setup[$key]);
436 if ($exitSig) {
437 return $exitSig;
438 }
439 } else {
440 $key = substr($string, 0, $keyLen) . '.';
441 if (!isset($setup[$key])) {
442 $setup[$key] = array();
443 }
444 $exitSig = $this->rollParseSub(substr($string, $keyLen + 1), $setup[$key]);
445 if ($exitSig) {
446 return $exitSig;
447 }
448 }
449 }
450 }
451
452 /**
453 * Get a value/property pair for an object path in TypoScript, eg. "myobject.myvalue.mysubproperty". Here: Used by the "copy" operator, <
454 *
455 * @param string Object path for which to get the value
456 * @param array Global setup code if $string points to a global object path. But if string is prefixed with "." then its the local setup array.
457 * @return array An array with keys 0/1 being value/property respectively
458 */
459 function getVal($string, $setup) {
460 if ((string) $string != '') {
461 $keyLen = strcspn($string, '.');
462 if ($keyLen == strlen($string)) {
463 $retArr = array(); // Added 6/6/03. Shouldn't hurt
464 if (isset($setup[$string])) {
465 $retArr[0] = $setup[$string];
466 }
467 if (isset($setup[$string . '.'])) {
468 $retArr[1] = $setup[$string . '.'];
469 }
470 return $retArr;
471 } else {
472 $key = substr($string, 0, $keyLen) . '.';
473 if ($setup[$key]) {
474 return $this->getVal(substr($string, $keyLen + 1), $setup[$key]);
475 }
476 }
477 }
478 }
479
480 /**
481 * Setting a value/property of an object string in the setup array.
482 *
483 * @param string The object sub-path, eg "thisprop.another_prot"
484 * @param array The local setup array from the function calling this function.
485 * @param array The value/property pair array to set. If only one of them is set, then the other is not touched (unless $wipeOut is set, which it is when copies are made which must include both value and property)
486 * @param boolean If set, then both value and property is wiped out when a copy is made of another value.
487 * @return void
488 */
489 function setVal($string, &$setup, $value, $wipeOut = 0) {
490 if ((string) $string != '') {
491 $keyLen = strcspn($string, '.');
492 if ($keyLen == strlen($string)) {
493 if ($value == 'UNSET') {
494 unset($setup[$string]);
495 unset($setup[$string . '.']);
496 if ($this->regLinenumbers) {
497 $setup[$string . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1) . '>';
498 }
499 } else {
500 $lnRegisDone = 0;
501 if ($wipeOut && $this->strict) {
502 if ((isset($setup[$string]) && !isset($value[0])) || (isset($setup[$string . '.']) && !isset($value[1]))) {
503 $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object copied in this line "' . trim($this->raw[($this->rawP - 1)]) . '" would leave either the value or properties untouched in TypoScript Version 1. Please check that this is not a problem for you.', 1);
504 }
505 unset($setup[$string]);
506 unset($setup[$string . '.']);
507 if ($this->regLinenumbers) {
508 $setup[$string . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1) . '<';
509 $lnRegisDone = 1;
510 }
511 }
512 if (isset($value[0])) {
513 $setup[$string] = $value[0];
514 }
515 if (isset($value[1])) {
516 $setup[$string . '.'] = $value[1];
517 }
518 if ($this->lastComment && $this->regComments) {
519 $setup[$string . '..'] .= $this->lastComment;
520 }
521 if ($this->regLinenumbers && !$lnRegisDone) {
522 $setup[$string . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1);
523 }
524 }
525 } else {
526 $key = substr($string, 0, $keyLen) . '.';
527 if (!isset($setup[$key])) {
528 $setup[$key] = array();
529 }
530 $this->setVal(substr($string, $keyLen + 1), $setup[$key], $value);
531 }
532 }
533 }
534
535 /**
536 * Stacks errors/messages from the TypoScript parser into an internal array, $this->error
537 * If "TT" is a global object (as it is in the frontend when backend users are logged in) the message will be registered here as well.
538 *
539 * @param string The error message string
540 * @param integer The error severity (in the scale of $GLOBALS['TT']->setTSlogMessage: Approx: 2=warning, 1=info, 0=nothing, 3=fatal.)
541 * @return void
542 */
543 function error($err, $num = 2) {
544 if (is_object($GLOBALS['TT'])) {
545 $GLOBALS['TT']->setTSlogMessage($err, $num);
546 }
547 $this->errors[] = array($err, $num, $this->rawP - 1, $this->lineNumberOffset);
548 }
549
550 /**
551 * Checks the input string (un-parsed TypoScript) for include-commands ("<INCLUDE_TYPOSCRIPT: ....")
552 * Use: t3lib_TSparser::checkIncludeLines()
553 *
554 * @param string Unparsed TypoScript
555 * @param integer Counter for detecting endless loops
556 * @param boolean When set an array containing the resulting typoscript and all included files will get returned
557 * @return string Complete TypoScript with includes added.
558 * @static
559 */
560 function checkIncludeLines($string, $cycle_counter = 1, $returnFiles = FALSE) {
561 $includedFiles = array();
562 if ($cycle_counter > 100) {
563 t3lib_div::sysLog('It appears like TypoScript code is looping over itself. Check your templates for "&lt;INCLUDE_TYPOSCRIPT: ..." tags', 'Core', 2);
564 if ($returnFiles) {
565 return array(
566 'typoscript' => '',
567 'files' => $includedFiles,
568 );
569 }
570 return "\n###\n### ERROR: Recursion!\n###\n";
571 }
572 $splitStr = '<INCLUDE_TYPOSCRIPT:';
573 if (strstr($string, $splitStr)) {
574 $newString = '';
575 $allParts = explode($splitStr, LF . $string . LF); // adds line break char before/after
576 foreach ($allParts as $c => $v) {
577 if (!$c) { // first goes through
578 $newString .= $v;
579 } elseif (preg_match('/\r?\n\s*$/', $allParts[$c - 1])) { // There must be a line-break char before.
580 $subparts = explode('>', $v, 2);
581 if (preg_match('/^\s*\r?\n/', $subparts[1])) { // There must be a line-break char after
582 // SO, the include was positively recognized:
583 $newString .= '### ' . $splitStr . $subparts[0] . '> BEGIN:' . LF;
584 $params = t3lib_div::get_tag_attributes($subparts[0]);
585 if ($params['source']) {
586 $sourceParts = explode(':', $params['source'], 2);
587 switch (strtolower(trim($sourceParts[0]))) {
588 case 'file':
589 $filename = t3lib_div::getFileAbsFileName(trim($sourceParts[1]));
590 if (strcmp($filename, '')) { // Must exist and must not contain '..' and must be relative
591 if (t3lib_div::verifyFilenameAgainstDenyPattern($filename)) { // Check for allowed files
592 if (@is_file($filename)) {
593 // check for includes in included text
594 $includedFiles[] = $filename;
595 $included_text = self::checkIncludeLines(t3lib_div::getUrl($filename), $cycle_counter + 1, $returnFiles);
596 // If the method also has to return all included files, merge currently included
597 // files with files included by recursively calling itself
598 if ($returnFiles && is_array($included_text)) {
599 $includedFiles = array_merge($includedFiles, $included_text['files']);
600 $included_text = $included_text['typoscript'];
601 }
602 $newString .= $included_text . LF;
603 } else {
604 $newString .= "\n###\n### ERROR: File \"" . $filename . "\" was not was not found.\n###\n\n";
605 t3lib_div::sysLog('File "' . $filename . '" was not found.', 'Core', 2);
606 }
607 } else {
608 $newString .= "\n###\n### ERROR: File \"" . $filename . "\" was not included since it is not allowed due to fileDenyPattern\n###\n\n";
609 t3lib_div::sysLog('File "' . $filename . '" was not included since it is not allowed due to fileDenyPattern', 'Core', 2);
610 }
611 }
612 break;
613 }
614 }
615 $newString .= '### ' . $splitStr . $subparts[0] . '> END:' . LF;
616 $newString .= $subparts[1];
617 } else {
618 $newString .= $splitStr . $v;
619 }
620 } else {
621 $newString .= $splitStr . $v;
622 }
623 }
624 $string = substr($newString, 1, -1); // not the first/last linebreak char.
625 }
626 // When all included files should get returned, simply return an compound array containing
627 // the TypoScript with all "includes" processed and the files which got included
628 if ($returnFiles) {
629 return array(
630 'typoscript' => $string,
631 'files' => $includedFiles,
632 );
633 }
634 return $string;
635 }
636
637 /**
638 * Parses the string in each value of the input array for include-commands
639 *
640 * @param array Array with TypoScript in each value
641 * @return array Same array but where the values has been parsed for include-commands
642 */
643 function checkIncludeLines_array($array) {
644 foreach ($array as $k => $v) {
645 $array[$k] = t3lib_TSparser::checkIncludeLines($array[$k]);
646 }
647 return $array;
648 }
649
650 /**
651 * Search for commented INCLUDE_TYPOSCRIPT statements
652 * and save the content between the BEGIN and the END line to the specified file
653 *
654 * @param string template content
655 * @param int Counter for detecting endless loops
656 * @return string template content with uncommented include statements
657 * @author Fabrizio Branca <typo3@fabrizio-branca.de>
658 */
659 function extractIncludes($string, $cycle_counter = 1, $extractedFileNames = array()) {
660
661 if ($cycle_counter > 10) {
662 t3lib_div::sysLog('It appears like TypoScript code is looping over itself. Check your templates for "&lt;INCLUDE_TYPOSCRIPT: ..." tags', 'Core', 2);
663 return "\n###\n### ERROR: Recursion!\n###\n";
664 }
665
666 $fileContent = array();
667 $restContent = array();
668 $fileName = NULL;
669 $inIncludePart = FALSE;
670 $lines = explode("\n", $string);
671 $skipNextLineIfEmpty = FALSE;
672 $openingCommentedIncludeStatement = NULL;
673 foreach ($lines as $line) {
674
675 // t3lib_TSparser::checkIncludeLines inserts an additional empty line, remove this again
676 if ($skipNextLineIfEmpty) {
677 if (trim($line) == '') {
678 continue;
679 }
680 $skipNextLineIfEmpty = FALSE;
681 }
682
683 if (!$inIncludePart) { // outside commented include statements
684
685 // search for beginning commented include statements
686 $matches = array();
687 if (preg_match('/###\s*<INCLUDE_TYPOSCRIPT:\s*source\s*=\s*"\s*FILE\s*:\s*(.*)\s*">\s*BEGIN/i', $line, $matches)) {
688
689 // save this line in case there is no ending tag
690 $openingCommentedIncludeStatement = trim($line);
691 $openingCommentedIncludeStatement = trim(preg_replace('/### Warning: .*###/', '', $openingCommentedIncludeStatement));
692
693 // found a commented include statement
694 $fileName = trim($matches[1]);
695 $inIncludePart = TRUE;
696
697 $expectedEndTag = '### <INCLUDE_TYPOSCRIPT: source="FILE:' . $fileName . '"> END';
698 // strip all whitespace characters to make comparision safer
699 $expectedEndTag = strtolower(preg_replace('/\s/', '', $expectedEndTag));
700 } else {
701 // if this is not a beginning commented include statement this line goes into the rest content
702 $restContent[] = $line;
703 }
704
705 } else { // inside commented include statements
706
707 // search for the matching ending commented include statement
708 $strippedLine = strtolower(preg_replace('/\s/', '', $line));
709 if (strpos($strippedLine, $expectedEndTag) !== FALSE) {
710
711 // found the matching ending include statement
712 $fileContentString = implode("\n", $fileContent);
713
714 // write the content to the file
715 $realFileName = t3lib_div::getFileAbsFileName($fileName);
716
717 // some file checks
718 if (empty($realFileName)) {
719 throw new UnexpectedValueException(sprintf('"%s" is not a valid file location.', $fileName), 1294586441);
720 }
721
722 if (!is_writable($realFileName)) {
723 throw new RuntimeException(sprintf('"%s" is not writable.', $fileName), 1294586442);
724 }
725
726 if (in_array($realFileName, $extractedFileNames)) {
727 throw new RuntimeException(sprintf('Recursive/multiple inclusion of file "%s"', $realFileName), 1294586443);
728 }
729 $extractedFileNames[] = $realFileName;
730
731 // recursive call to detected nested commented include statements
732 $fileContentString = self::extractIncludes($fileContentString, $cycle_counter + 1, $extractedFileNames);
733
734 if (!t3lib_div::writeFile($realFileName, $fileContentString)) {
735 throw new RuntimeException(sprintf('Could not write file "%s"', $realFileName), 1294586444);
736 }
737
738 // insert reference to the file in the rest content
739 $restContent[] = "<INCLUDE_TYPOSCRIPT: source=\"FILE:$fileName\">";
740
741 // reset variables (preparing for the next commented include statement)
742 $fileContent = array();
743 $fileName = NULL;
744 $inIncludePart = FALSE;
745 $openingCommentedIncludeStatement = NULL;
746 // t3lib_TSparser::checkIncludeLines inserts an additional empty line, remove this again
747 $skipNextLineIfEmpty = TRUE;
748 } else {
749 // if this is not a ending commented include statement this line goes into the file content
750 $fileContent[] = $line;
751 }
752
753 }
754
755 }
756
757 // if we're still inside commented include statements copy the lines back to the rest content
758 if ($inIncludePart) {
759 $restContent[] = $openingCommentedIncludeStatement . ' ### Warning: Corresponding end line missing! ###';
760 $restContent = array_merge($restContent, $fileContent);
761 }
762
763 $restContentString = implode("\n", $restContent);
764 return $restContentString;
765 }
766
767 /**
768 * Processes the string in each value of the input array with extractIncludes
769 *
770 * @param array Array with TypoScript in each value
771 * @return array Same array but where the values has been processed with extractIncludes
772 * @author Fabrizio Branca <typo3@fabrizio-branca.de>
773 */
774 function extractIncludes_array($array) {
775 foreach ($array as $k => $v) {
776 $array[$k] = t3lib_TSparser::extractIncludes($array[$k]);
777 }
778 return $array;
779 }
780
781
782 /**********************************
783 *
784 * Syntax highlighting
785 *
786 *********************************/
787
788 /**
789 * Syntax highlight a TypoScript text
790 * Will parse the content. Remember, the internal setup array may contain invalid parsed content since conditions are ignored!
791 *
792 * @param string The TypoScript text
793 * @param mixed If blank, linenumbers are NOT printed. If array then the first key is the linenumber offset to add to the internal counter.
794 * @param boolean If set, then the highlighted output will be formatted in blocks based on the brace levels. prespace will be ignored and empty lines represented with a single no-break-space.
795 * @return string HTML code for the syntax highlighted string
796 */
797 function doSyntaxHighlight($string, $lineNum = '', $highlightBlockMode = 0) {
798 $this->syntaxHighLight = 1;
799 $this->highLightData = array();
800 $this->error = array();
801 $string = str_replace(CR, '', $string); // This is done in order to prevent empty <span>..</span> sections around CR content. Should not do anything but help lessen the amount of HTML code.
802
803 $this->parse($string);
804
805 return $this->syntaxHighlight_print($lineNum, $highlightBlockMode);
806 }
807
808 /**
809 * Registers a part of a TypoScript line for syntax highlighting.
810 *
811 * @param string Key from the internal array $this->highLightStyles
812 * @param integer Pointer to the line in $this->raw which this is about
813 * @param integer The number of chars LEFT on this line before the end is reached.
814 * @return void
815 * @access private
816 * @see parse()
817 */
818 function regHighLight($code, $pointer, $strlen = -1) {
819 if ($strlen == -1) {
820 $this->highLightData[$pointer] = array(array($code, 0));
821 } else {
822 $this->highLightData[$pointer][] = array($code, $strlen);
823 }
824 $this->highLightData_bracelevel[$pointer] = $this->inBrace;
825 }
826
827 /**
828 * Formatting the TypoScript code in $this->raw based on the data collected by $this->regHighLight in $this->highLightData
829 *
830 * @param mixed If blank, linenumbers are NOT printed. If array then the first key is the linenumber offset to add to the internal counter.
831 * @param boolean If set, then the highlighted output will be formatted in blocks based on the brace levels. prespace will be ignored and empty lines represented with a single no-break-space.
832 * @return string HTML content
833 * @access private
834 * @see doSyntaxHighlight()
835 */
836 function syntaxHighlight_print($lineNumDat, $highlightBlockMode) {
837 // Registers all error messages in relation to their linenumber
838 $errA = array();
839 foreach ($this->errors as $err) {
840 $errA[$err[2]][] = $err[0];
841 }
842 // Generates the syntax highlighted output:
843 $lines = array();
844 foreach ($this->raw as $rawP => $value) {
845 $start = 0;
846 $strlen = strlen($value);
847 $lineC = '';
848
849 if (is_array($this->highLightData[$rawP])) {
850 foreach ($this->highLightData[$rawP] as $set) {
851 $len = $strlen - $start - $set[1];
852 if ($len > 0) {
853 $part = substr($value, $start, $len);
854 $start += $len;
855 $st = $this->highLightStyles[(isset($this->highLightStyles[$set[0]]) ? $set[0] : 'default')];
856 if (!$highlightBlockMode || $set[0] != 'prespace') {
857 $lineC .= $st[0] . htmlspecialchars($part) . $st[1];
858 }
859 } elseif ($len < 0) {
860 debug(array($len, $value, $rawP));
861 }
862 }
863 } else {
864 debug(array($value));
865 }
866
867 if (strlen(substr($value, $start))) {
868 $lineC .= $this->highLightStyles['ignored'][0] . htmlspecialchars(substr($value, $start)) . $this->highLightStyles['ignored'][1];
869 }
870
871 if ($errA[$rawP]) {
872 $lineC .= $this->highLightStyles['error'][0] . '<strong> - ERROR:</strong> ' . htmlspecialchars(implode(';', $errA[$rawP])) . $this->highLightStyles['error'][1];
873 }
874
875 if ($highlightBlockMode && $this->highLightData_bracelevel[$rawP]) {
876 $lineC = str_pad('', $this->highLightData_bracelevel[$rawP] * 2, ' ', STR_PAD_LEFT) . '<span style="' . $this->highLightBlockStyles . ($this->highLightBlockStyles_basecolor ? 'background-color: ' . t3lib_div::modifyHTMLColorAll($this->highLightBlockStyles_basecolor, -$this->highLightData_bracelevel[$rawP] * 16) : '') . '">' . (strcmp($lineC, '') ? $lineC : '&nbsp;') . '</span>';
877 }
878
879 if (is_array($lineNumDat)) {
880 $lineNum = $rawP + $lineNumDat[0];
881 if ($this->parentObject instanceof t3lib_tsparser_ext) {
882 $lineNum = $this->parentObject->ext_lnBreakPointWrap($lineNum, $lineNum);
883 }
884 $lineC = $this->highLightStyles['linenum'][0] . str_pad($lineNum, 4, ' ', STR_PAD_LEFT) . ':' . $this->highLightStyles['linenum'][1] . ' ' . $lineC;
885 }
886
887
888 $lines[] = $lineC;
889 }
890
891 return '<pre class="ts-hl">' . implode(LF, $lines) . '</pre>';
892 }
893 }
894
895
896 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser.php'])) {
897 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser.php']);
898 }
899
900 ?>