TYPO3 CMS  TYPO3_7-6
TypoScriptParser.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
26 
31 {
37  public $strict = true;
38 
44  public $setup = [];
45 
51  public $raw;
52 
58  public $rawP;
59 
65  public $lastComment = '';
66 
72  public $commentSet = false;
73 
79  public $multiLineEnabled = false;
80 
86  public $multiLineObject = '';
87 
93  public $multiLineValue = [];
94 
100  public $inBrace = 0;
101 
108  public $lastConditionTrue = true;
109 
115  public $sections = [];
116 
122  public $sectionsMatch = [];
123 
129  public $syntaxHighLight = false;
130 
136  public $highLightData = [];
137 
144 
150  public $regComments = false;
151 
157  public $regLinenumbers = false;
158 
164  public $errors = [];
165 
171  public $lineNumberOffset = 0;
172 
178  public $breakPointLN = 0;
179 
183  public $highLightStyles = [
184  'prespace' => ['<span class="ts-prespace">', '</span>'],
185  // Space before any content on a line
186  'objstr_postspace' => ['<span class="ts-objstr_postspace">', '</span>'],
187  // Space after the object string on a line
188  'operator_postspace' => ['<span class="ts-operator_postspace">', '</span>'],
189  // Space after the operator on a line
190  'operator' => ['<span class="ts-operator">', '</span>'],
191  // The operator char
192  'value' => ['<span class="ts-value">', '</span>'],
193  // The value of a line
194  'objstr' => ['<span class="ts-objstr">', '</span>'],
195  // The object string of a line
196  'value_copy' => ['<span class="ts-value_copy">', '</span>'],
197  // The value when the copy syntax (<) is used; that means the object reference
198  'value_unset' => ['<span class="ts-value_unset">', '</span>'],
199  // The value when an object is unset. Should not exist.
200  'ignored' => ['<span class="ts-ignored">', '</span>'],
201  // The "rest" of a line which will be ignored.
202  'default' => ['<span class="ts-default">', '</span>'],
203  // The default style if none other is applied.
204  'comment' => ['<span class="ts-comment">', '</span>'],
205  // Comment lines
206  'condition' => ['<span class="ts-condition">', '</span>'],
207  // Conditions
208  'error' => ['<span class="ts-error">', '</span>'],
209  // Error messages
210  'linenum' => ['<span class="ts-linenum">', '</span>']
211  ];
212 
219 
226 
231 
240  public function parse($string, $matchObj = '')
241  {
242  $this->raw = explode(LF, $string);
243  $this->rawP = 0;
244  $pre = '[GLOBAL]';
245  while ($pre) {
246  if ($this->breakPointLN && $pre === '[_BREAK]') {
247  $this->error('Breakpoint at ' . ($this->lineNumberOffset + $this->rawP - 2) . ': Line content was "' . $this->raw[$this->rawP - 2] . '"', 1);
248  break;
249  }
250  $preUppercase = strtoupper($pre);
251  if ($pre[0] === '[' &&
252  ($preUppercase === '[GLOBAL]' ||
253  $preUppercase === '[END]' ||
254  !$this->lastConditionTrue && $preUppercase === '[ELSE]')
255  ) {
256  $pre = trim($this->parseSub($this->setup));
257  $this->lastConditionTrue = 1;
258  } else {
259  // We're in a specific section. Therefore we log this section
260  $specificSection = $preUppercase !== '[ELSE]';
261  if ($specificSection) {
262  $this->sections[md5($pre)] = $pre;
263  }
264  if (is_object($matchObj) && $matchObj->match($pre) || $this->syntaxHighLight) {
265  if ($specificSection) {
266  $this->sectionsMatch[md5($pre)] = $pre;
267  }
268  $pre = trim($this->parseSub($this->setup));
269  $this->lastConditionTrue = 1;
270  } else {
271  $pre = $this->nextDivider();
272  $this->lastConditionTrue = 0;
273  }
274  }
275  }
276  if ($this->inBrace) {
277  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': The script is short of ' . $this->inBrace . ' end brace(s)', 1);
278  }
279  if ($this->multiLineEnabled) {
280  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': A multiline value section is not ended with a parenthesis!', 1);
281  }
282  $this->lineNumberOffset += count($this->raw) + 1;
283  }
284 
291  public function nextDivider()
292  {
293  while (isset($this->raw[$this->rawP])) {
294  $line = trim($this->raw[$this->rawP]);
295  $this->rawP++;
296  if ($line && $line[0] === '[') {
297  return $line;
298  }
299  }
300  return '';
301  }
302 
309  public function parseSub(array &$setup)
310  {
311  while (isset($this->raw[$this->rawP])) {
312  $line = ltrim($this->raw[$this->rawP]);
313  $lineP = $this->rawP;
314  $this->rawP++;
315  if ($this->syntaxHighLight) {
316  $this->regHighLight('prespace', $lineP, strlen($line));
317  }
318  // Breakpoint?
319  // By adding 1 we get that line processed
320  if ($this->breakPointLN && $this->lineNumberOffset + $this->rawP - 1 === $this->breakPointLN + 1) {
321  return '[_BREAK]';
322  }
323  // Set comment flag?
324  if (!$this->multiLineEnabled && strpos($line, '/*') === 0) {
325  $this->commentSet = 1;
326  }
327  // If $this->multiLineEnabled we will go and get the line values here because we know, the first if() will be TRUE.
328  if (!$this->commentSet && ($line || $this->multiLineEnabled)) {
329  // If multiline is enabled. Escape by ')'
330  if ($this->multiLineEnabled) {
331  // Multiline ends...
332  if ($line[0] === ')') {
333  if ($this->syntaxHighLight) {
334  $this->regHighLight('operator', $lineP, strlen($line) - 1);
335  }
336  // Disable multiline
337  $this->multiLineEnabled = 0;
338  $theValue = implode($this->multiLineValue, LF);
339  if (strpos($this->multiLineObject, '.') !== false) {
340  // Set the value deeper.
341  $this->setVal($this->multiLineObject, $setup, [$theValue]);
342  } else {
343  // Set value regularly
344  $setup[$this->multiLineObject] = $theValue;
345  if ($this->lastComment && $this->regComments) {
346  $setup[$this->multiLineObject . '..'] .= $this->lastComment;
347  }
348  if ($this->regLinenumbers) {
349  $setup[$this->multiLineObject . '.ln..'][] = $this->lineNumberOffset + $this->rawP - 1;
350  }
351  }
352  } else {
353  if ($this->syntaxHighLight) {
354  $this->regHighLight('value', $lineP);
355  }
356  $this->multiLineValue[] = $this->raw[$this->rawP - 1];
357  }
358  } elseif ($this->inBrace === 0 && $line[0] === '[') {
359  // Beginning of condition (only on level zero compared to brace-levels
360  if ($this->syntaxHighLight) {
361  $this->regHighLight('condition', $lineP);
362  }
363  return $line;
364  } else {
365  // Return if GLOBAL condition is set - no matter what.
366  if ($line[0] === '[' && stripos($line, '[GLOBAL]') !== false) {
367  if ($this->syntaxHighLight) {
368  $this->regHighLight('condition', $lineP);
369  }
370  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': On return to [GLOBAL] scope, the script was short of ' . $this->inBrace . ' end brace(s)', 1);
371  $this->inBrace = 0;
372  return $line;
373  } elseif ($line[0] !== '}' && $line[0] !== '#' && $line[0] !== '/') {
374  // If not brace-end or comment
375  // Find object name string until we meet an operator
376  $varL = strcspn($line, TAB . ' {=<>(');
377  // check for special ":=" operator
378  if ($varL > 0 && substr($line, $varL-1, 2) === ':=') {
379  --$varL;
380  }
381  // also remove tabs after the object string name
382  $objStrName = substr($line, 0, $varL);
383  if ($this->syntaxHighLight) {
384  $this->regHighLight('objstr', $lineP, strlen(substr($line, $varL)));
385  }
386  if ($objStrName !== '') {
387  $r = [];
388  if ($this->strict && preg_match('/[^[:alnum:]_\\\\\\.:-]/i', $objStrName, $r)) {
389  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object Name String, "' . htmlspecialchars($objStrName) . '" contains invalid character "' . $r[0] . '". Must be alphanumeric or one of: "_:-\\."');
390  } else {
391  $line = ltrim(substr($line, $varL));
392  if ($this->syntaxHighLight) {
393  $this->regHighLight('objstr_postspace', $lineP, strlen($line));
394  if ($line !== '') {
395  $this->regHighLight('operator', $lineP, strlen($line) - 1);
396  $this->regHighLight('operator_postspace', $lineP, strlen(ltrim(substr($line, 1))));
397  }
398  }
399  if ($line === '') {
400  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object Name String, "' . htmlspecialchars($objStrName) . '" was not followed by any operator, =<>({');
401  } else {
402  // Checking for special TSparser properties (to change TS values at parsetime)
403  $match = [];
404  if ($line[0] === ':' && preg_match('/^:=\\s*([[:alpha:]]+)\\s*\\((.*)\\).*/', $line, $match)) {
405  $tsFunc = $match[1];
406  $tsFuncArg = $match[2];
407  list($currentValue) = $this->getVal($objStrName, $setup);
408  $tsFuncArg = str_replace(['\\\\', '\\n', '\\t'], ['\\', LF, TAB], $tsFuncArg);
409  $newValue = $this->executeValueModifier($tsFunc, $tsFuncArg, $currentValue);
410  if (isset($newValue)) {
411  $line = '= ' . $newValue;
412  }
413  }
414  switch ($line[0]) {
415  case '=':
416  if ($this->syntaxHighLight) {
417  $this->regHighLight('value', $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
418  }
419  if (strpos($objStrName, '.') !== false) {
420  $value = [];
421  $value[0] = trim(substr($line, 1));
422  $this->setVal($objStrName, $setup, $value);
423  } else {
424  $setup[$objStrName] = trim(substr($line, 1));
425  if ($this->lastComment && $this->regComments) {
426  // Setting comment..
427  $setup[$objStrName . '..'] .= $this->lastComment;
428  }
429  if ($this->regLinenumbers) {
430  $setup[$objStrName . '.ln..'][] = $this->lineNumberOffset + $this->rawP - 1;
431  }
432  }
433  break;
434  case '{':
435  $this->inBrace++;
436  if (strpos($objStrName, '.') !== false) {
437  $exitSig = $this->rollParseSub($objStrName, $setup);
438  if ($exitSig) {
439  return $exitSig;
440  }
441  } else {
442  if (!isset($setup[$objStrName . '.'])) {
443  $setup[$objStrName . '.'] = [];
444  }
445  $exitSig = $this->parseSub($setup[$objStrName . '.']);
446  if ($exitSig) {
447  return $exitSig;
448  }
449  }
450  break;
451  case '(':
452  $this->multiLineObject = $objStrName;
453  $this->multiLineEnabled = 1;
454  $this->multiLineValue = [];
455  break;
456  case '<':
457  if ($this->syntaxHighLight) {
458  $this->regHighLight('value_copy', $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
459  }
460  $theVal = trim(substr($line, 1));
461  if ($theVal[0] === '.') {
462  $res = $this->getVal(substr($theVal, 1), $setup);
463  } else {
464  $res = $this->getVal($theVal, $this->setup);
465  }
466  $this->setVal($objStrName, $setup, unserialize(serialize($res)), 1);
467  // 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.
468  break;
469  case '>':
470  if ($this->syntaxHighLight) {
471  $this->regHighLight('value_unset', $lineP, strlen(ltrim(substr($line, 1))) - strlen(trim(substr($line, 1))));
472  }
473  $this->setVal($objStrName, $setup, 'UNSET');
474  break;
475  default:
476  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': Object Name String, "' . htmlspecialchars($objStrName) . '" was not followed by any operator, =<>({');
477  }
478  }
479  }
480  $this->lastComment = '';
481  }
482  } elseif ($line[0] === '}') {
483  $this->inBrace--;
484  $this->lastComment = '';
485  if ($this->syntaxHighLight) {
486  $this->regHighLight('operator', $lineP, strlen($line) - 1);
487  }
488  if ($this->inBrace < 0) {
489  $this->error('Line ' . ($this->lineNumberOffset + $this->rawP - 1) . ': An end brace is in excess.', 1);
490  $this->inBrace = 0;
491  } else {
492  break;
493  }
494  } else {
495  if ($this->syntaxHighLight) {
496  $this->regHighLight('comment', $lineP);
497  }
498  // Comment. The comments are concatenated in this temporary string:
499  if ($this->regComments) {
500  $this->lastComment .= rtrim($line) . LF;
501  }
502  }
503  if (StringUtility::beginsWith($line, '### ERROR')) {
504  $this->error(substr($line, 11));
505  }
506  }
507  }
508  // Unset comment
509  if ($this->commentSet) {
510  if ($this->syntaxHighLight) {
511  $this->regHighLight('comment', $lineP);
512  }
513  if (strpos($line, '*/') === 0) {
514  $this->commentSet = 0;
515  }
516  }
517  }
518  return null;
519  }
520 
530  protected function executeValueModifier($modifierName, $modifierArgument = null, $currentValue = null)
531  {
532  $newValue = null;
533  switch ($modifierName) {
534  case 'prependString':
535  $newValue = $modifierArgument . $currentValue;
536  break;
537  case 'appendString':
538  $newValue = $currentValue . $modifierArgument;
539  break;
540  case 'removeString':
541  $newValue = str_replace($modifierArgument, '', $currentValue);
542  break;
543  case 'replaceString':
544  list($fromStr, $toStr) = explode('|', $modifierArgument, 2);
545  $newValue = str_replace($fromStr, $toStr, $currentValue);
546  break;
547  case 'addToList':
548  $newValue = ((string)$currentValue !== '' ? $currentValue . ',' : '') . $modifierArgument;
549  break;
550  case 'removeFromList':
551  $existingElements = GeneralUtility::trimExplode(',', $currentValue);
552  $removeElements = GeneralUtility::trimExplode(',', $modifierArgument);
553  if (!empty($removeElements)) {
554  $newValue = implode(',', array_diff($existingElements, $removeElements));
555  }
556  break;
557  case 'uniqueList':
558  $elements = GeneralUtility::trimExplode(',', $currentValue);
559  $newValue = implode(',', array_unique($elements));
560  break;
561  case 'reverseList':
562  $elements = GeneralUtility::trimExplode(',', $currentValue);
563  $newValue = implode(',', array_reverse($elements));
564  break;
565  case 'sortList':
566  $elements = GeneralUtility::trimExplode(',', $currentValue);
567  $arguments = GeneralUtility::trimExplode(',', $modifierArgument);
568  $arguments = array_map('strtolower', $arguments);
569  $sort_flags = SORT_REGULAR;
570  if (in_array('numeric', $arguments)) {
571  $sort_flags = SORT_NUMERIC;
572  // If the sorting modifier "numeric" is given, all values
573  // are checked and an exception is thrown if a non-numeric value is given
574  // otherwise there is a different behaviour between PHP7 and PHP 5.x
575  // See also the warning on http://us.php.net/manual/en/function.sort.php
576  foreach ($elements as $element) {
577  if (!is_numeric($element)) {
578  throw new \InvalidArgumentException('The list "' . $currentValue . '" should be sorted numerically but contains a non-numeric value', 1438191758);
579  }
580  }
581  }
582  sort($elements, $sort_flags);
583  if (in_array('descending', $arguments)) {
584  $elements = array_reverse($elements);
585  }
586  $newValue = implode(',', $elements);
587  break;
588  default:
589  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsparser.php']['preParseFunc'][$modifierName])) {
590  $hookMethod = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsparser.php']['preParseFunc'][$modifierName];
591  $params = ['currentValue' => $currentValue, 'functionArgument' => $modifierArgument];
592  $fakeThis = false;
593  $newValue = GeneralUtility::callUserFunction($hookMethod, $params, $fakeThis);
594  } else {
595  GeneralUtility::sysLog(
596  'Missing function definition for ' . $modifierName . ' on TypoScript',
597  'core',
599  );
600  }
601  }
602  return $newValue;
603  }
604 
614  public function rollParseSub($string, array &$setup)
615  {
616  if ((string)$string === '') {
617  return '';
618  }
619 
620  list($key, $remainingKey) = $this->parseNextKeySegment($string);
621  $key .= '.';
622  if (!isset($setup[$key])) {
623  $setup[$key] = [];
624  }
625  $exitSig = $remainingKey === ''
626  ? $this->parseSub($setup[$key])
627  : $this->rollParseSub($remainingKey, $setup[$key]);
628  return $exitSig ?: '';
629  }
630 
639  public function getVal($string, $setup)
640  {
641  if ((string)$string === '') {
642  return [];
643  }
644 
645  list($key, $remainingKey) = $this->parseNextKeySegment($string);
646  $subKey = $key . '.';
647  if ($remainingKey === '') {
648  $retArr = [];
649  if (isset($setup[$key])) {
650  $retArr[0] = $setup[$key];
651  }
652  if (isset($setup[$subKey])) {
653  $retArr[1] = $setup[$subKey];
654  }
655  return $retArr;
656  } else {
657  if ($setup[$subKey]) {
658  return $this->getVal($remainingKey, $setup[$subKey]);
659  }
660  }
661  return [];
662  }
663 
673  public function setVal($string, array &$setup, $value, $wipeOut = false)
674  {
675  if ((string)$string === '') {
676  return;
677  }
678 
679  list($key, $remainingKey) = $this->parseNextKeySegment($string);
680  $subKey = $key . '.';
681  if ($remainingKey === '') {
682  if ($value === 'UNSET') {
683  unset($setup[$key]);
684  unset($setup[$subKey]);
685  if ($this->regLinenumbers) {
686  $setup[$key . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1) . '>';
687  }
688  } else {
689  $lnRegisDone = 0;
690  if ($wipeOut && $this->strict) {
691  unset($setup[$key]);
692  unset($setup[$subKey]);
693  if ($this->regLinenumbers) {
694  $setup[$key . '.ln..'][] = ($this->lineNumberOffset + $this->rawP - 1) . '<';
695  $lnRegisDone = 1;
696  }
697  }
698  if (isset($value[0])) {
699  $setup[$key] = $value[0];
700  }
701  if (isset($value[1])) {
702  $setup[$subKey] = $value[1];
703  }
704  if ($this->lastComment && $this->regComments) {
705  $setup[$key . '..'] .= $this->lastComment;
706  }
707  if ($this->regLinenumbers && !$lnRegisDone) {
708  $setup[$key . '.ln..'][] = $this->lineNumberOffset + $this->rawP - 1;
709  }
710  }
711  } else {
712  if (!isset($setup[$subKey])) {
713  $setup[$subKey] = [];
714  }
715  $this->setVal($remainingKey, $setup[$subKey], $value);
716  }
717  }
718 
730  protected function parseNextKeySegment($key)
731  {
732  // if no dot is in the key, nothing to do
733  $dotPosition = strpos($key, '.');
734  if ($dotPosition === false) {
735  return [$key, ''];
736  }
737 
738  if (strpos($key, '\\') !== false) {
739  // backslashes are in the key, so we do further parsing
740 
741  while ($dotPosition !== false) {
742  if ($dotPosition > 0 && $key[$dotPosition - 1] !== '\\' || $dotPosition > 1 && $key[$dotPosition - 2] === '\\') {
743  break;
744  }
745  // escaped dot found, continue
746  $dotPosition = strpos($key, '.', $dotPosition + 1);
747  }
748 
749  if ($dotPosition === false) {
750  // no regular dot found
751  $keySegment = $key;
752  $remainingKey = '';
753  } else {
754  if ($dotPosition > 1 && $key[$dotPosition - 2] === '\\' && $key[$dotPosition - 1] === '\\') {
755  $keySegment = substr($key, 0, $dotPosition - 1);
756  } else {
757  $keySegment = substr($key, 0, $dotPosition);
758  }
759  $remainingKey = substr($key, $dotPosition + 1);
760  }
761 
762  // fix key segment by removing escape sequences
763  $keySegment = str_replace('\\.', '.', $keySegment);
764  } else {
765  // no backslash in the key, we're fine off
766  list($keySegment, $remainingKey) = explode('.', $key, 2);
767  }
768  return [$keySegment, $remainingKey];
769  }
770 
779  public function error($err, $num = 2)
780  {
781  $tt = $this->getTimeTracker();
782  if ($tt !== null) {
783  $tt->setTSlogMessage($err, $num);
784  }
785  $this->errors[] = [$err, $num, $this->rawP - 1, $this->lineNumberOffset];
786  }
787 
799  public static function checkIncludeLines($string, $cycle_counter = 1, $returnFiles = false, $parentFilenameOrPath = '')
800  {
801  $includedFiles = [];
802  if ($cycle_counter > 100) {
803  GeneralUtility::sysLog('It appears like TypoScript code is looping over itself. Check your templates for "&lt;INCLUDE_TYPOSCRIPT: ..." tags', 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
804  if ($returnFiles) {
805  return [
806  'typoscript' => '',
807  'files' => $includedFiles
808  ];
809  }
810  return '
811 ###
812 ### ERROR: Recursion!
813 ###
814 ';
815  }
816 
817  // If no tags found, no need to do slower preg_split
818  if (strpos($string, '<INCLUDE_TYPOSCRIPT:') !== false) {
819  $splitRegEx = '/\r?\n\s*<INCLUDE_TYPOSCRIPT:\s*(?i)source\s*=\s*"((?i)file|dir):\s*([^"]*)"(.*)>[\ \t]*/';
820  $parts = preg_split($splitRegEx, LF . $string . LF, -1, PREG_SPLIT_DELIM_CAPTURE);
821  // First text part goes through
822  $newString = $parts[0] . LF;
823  $partCount = count($parts);
824  for ($i = 1; $i + 3 < $partCount; $i += 4) {
825  // $parts[$i] contains 'FILE' or 'DIR'
826  // $parts[$i+1] contains relative file or directory path to be included
827  // $parts[$i+2] optional properties of the INCLUDE statement
828  // $parts[$i+3] next part of the typoscript string (part in between include-tags)
829  $includeType = $parts[$i];
830  $filename = $parts[$i + 1];
831  $originalFilename = $filename;
832  $optionalProperties = $parts[$i + 2];
833  $tsContentsTillNextInclude = $parts[$i + 3];
834 
835  // Check condition
836  $matches = preg_split('#(?i)condition\\s*=\\s*"((?:\\\\\\\\|\\\\"|[^\\"])*)"(\\s*|>)#', $optionalProperties, 2, PREG_SPLIT_DELIM_CAPTURE);
837  // If there was a condition
838  if (count($matches) > 1) {
839  // Unescape the condition
840  $condition = trim(stripslashes($matches[1]));
841  // If necessary put condition in square brackets
842  if ($condition[0] !== '[') {
843  $condition = '[' . $condition . ']';
844  }
845 
847  $conditionMatcher = null;
848  if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE) {
849  $conditionMatcher = GeneralUtility::makeInstance(FrontendConditionMatcher::class);
850  } else {
851  $conditionMatcher = GeneralUtility::makeInstance(BackendConditionMatcher::class);
852  }
853 
854  // If it didn't match then proceed to the next include, but prepend next normal (not file) part to output string
855  if (!$conditionMatcher->match($condition)) {
856  $newString .= $tsContentsTillNextInclude . LF;
857  continue;
858  }
859  }
860 
861  // Resolve a possible relative paths if a parent file is given
862  if ($parentFilenameOrPath !== '' && $filename[0] === '.') {
863  $filename = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($parentFilenameOrPath, $filename);
864  }
865 
866  // There must be a line-break char after - not sure why this check is necessary, kept it for being 100% backwards compatible
867  // An empty string is also ok (means that the next line is also a valid include_typoscript tag)
868  if (!preg_match('/(^\\s*\\r?\\n|^$)/', $tsContentsTillNextInclude)) {
869  $newString .= self::typoscriptIncludeError('Invalid characters after <INCLUDE_TYPOSCRIPT: source="' . $includeType . ':' . $filename . '">-tag (rest of line must be empty).');
870  } elseif (strpos('..', $filename) !== false) {
871  $newString .= self::typoscriptIncludeError('Invalid filepath "' . $filename . '" (containing "..").');
872  } else {
873  switch (strtolower($includeType)) {
874  case 'file':
875  self::includeFile($originalFilename, $cycle_counter, $returnFiles, $newString, $includedFiles, $optionalProperties, $parentFilenameOrPath);
876  break;
877  case 'dir':
878  self::includeDirectory($originalFilename, $cycle_counter, $returnFiles, $newString, $includedFiles, $optionalProperties, $parentFilenameOrPath);
879  break;
880  default:
881  $newString .= self::typoscriptIncludeError('No valid option for INCLUDE_TYPOSCRIPT source property (valid options are FILE or DIR)');
882  }
883  }
884  // Prepend next normal (not file) part to output string
885  $newString .= $tsContentsTillNextInclude . LF;
886 
887  // load default TypoScript for content rendering templates like
888  // css_styled_content if those have been included through f.e.
889  // <INCLUDE_TYPOSCRIPT: source="FILE:EXT:css_styled_content/static/setup.txt">
890  if (StringUtility::beginsWith(strtolower($filename), 'ext:')) {
891  $filePointerPathParts = explode('/', substr($filename, 4));
892 
893  // remove file part, determine whether to load setup or constants
894  list($includeType, ) = explode('.', array_pop($filePointerPathParts));
895 
896  if (in_array($includeType, ['setup', 'constants'])) {
897  // adapt extension key to required format (no underscores)
898  $filePointerPathParts[0] = str_replace('_', '', $filePointerPathParts[0]);
899 
900  // load default TypoScript
901  $defaultTypoScriptKey = implode('/', $filePointerPathParts) . '/';
902  if (in_array($defaultTypoScriptKey, $GLOBALS['TYPO3_CONF_VARS']['FE']['contentRenderingTemplates'], true)) {
903  $newString .= $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_' . $includeType . '.']['defaultContentRendering'];
904  }
905  }
906  }
907  }
908  // Add a line break before and after the included code in order to make sure that the parser always has a LF.
909  $string = LF . trim($newString) . LF;
910  }
911  // When all included files should get returned, simply return an compound array containing
912  // the TypoScript with all "includes" processed and the files which got included
913  if ($returnFiles) {
914  return [
915  'typoscript' => $string,
916  'files' => $includedFiles
917  ];
918  }
919  return $string;
920  }
921 
935  public static function includeFile($filename, $cycle_counter = 1, $returnFiles = false, &$newString = '', array &$includedFiles = [], $optionalProperties = '', $parentFilenameOrPath = '')
936  {
937  // Resolve a possible relative paths if a parent file is given
938  if ($parentFilenameOrPath !== '' && $filename[0] === '.') {
939  $absfilename = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($parentFilenameOrPath, $filename);
940  } else {
941  $absfilename = $filename;
942  }
943  $absfilename = GeneralUtility::getFileAbsFileName($absfilename);
944 
945  $newString .= LF . '### <INCLUDE_TYPOSCRIPT: source="FILE:' . $filename . '"' . $optionalProperties . '> BEGIN:' . LF;
946  if ((string)$filename !== '') {
947  // Must exist and must not contain '..' and must be relative
948  // Check for allowed files
950  $newString .= self::typoscriptIncludeError('File "' . $filename . '" was not included since it is not allowed due to fileDenyPattern.');
951  } elseif (!@file_exists($absfilename)) {
952  $newString .= self::typoscriptIncludeError('File "' . $filename . '" was not found.');
953  } else {
954  $includedFiles[] = $absfilename;
955  // check for includes in included text
956  $included_text = self::checkIncludeLines(GeneralUtility::getUrl($absfilename), $cycle_counter + 1, $returnFiles, $absfilename);
957  // If the method also has to return all included files, merge currently included
958  // files with files included by recursively calling itself
959  if ($returnFiles && is_array($included_text)) {
960  $includedFiles = array_merge($includedFiles, $included_text['files']);
961  $included_text = $included_text['typoscript'];
962  }
963  $newString .= $included_text . LF;
964  }
965  }
966  $newString .= '### <INCLUDE_TYPOSCRIPT: source="FILE:' . $filename . '"' . $optionalProperties . '> END:' . LF . LF;
967  }
968 
984  protected static function includeDirectory($dirPath, $cycle_counter = 1, $returnFiles = false, &$newString = '', array &$includedFiles = [], $optionalProperties = '', $parentFilenameOrPath = '')
985  {
986  // Extract the value of the property extensions="..."
987  $matches = preg_split('#(?i)extensions\s*=\s*"([^"]*)"(\s*|>)#', $optionalProperties, 2, PREG_SPLIT_DELIM_CAPTURE);
988  if (count($matches) > 1) {
989  $includedFileExtensions = $matches[1];
990  } else {
991  $includedFileExtensions = '';
992  }
993 
994  // Resolve a possible relative paths if a parent file is given
995  if ($parentFilenameOrPath !== '' && $dirPath[0] === '.') {
996  $resolvedDirPath = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($parentFilenameOrPath, $dirPath);
997  } else {
998  $resolvedDirPath = $dirPath;
999  }
1000  $absDirPath = GeneralUtility::getFileAbsFileName($resolvedDirPath);
1001  if ($absDirPath) {
1002  $absDirPath = rtrim($absDirPath, '/') . '/';
1003  $newString .= LF . '### <INCLUDE_TYPOSCRIPT: source="DIR:' . $dirPath . '"' . $optionalProperties . '> BEGIN:' . LF;
1004  // Get alphabetically sorted file index in array
1005  $fileIndex = GeneralUtility::getAllFilesAndFoldersInPath([], $absDirPath, $includedFileExtensions);
1006  // Prepend file contents to $newString
1007  $prefixLength = strlen(PATH_site);
1008  foreach ($fileIndex as $absFileRef) {
1009  $relFileRef = substr($absFileRef, $prefixLength);
1010  self::includeFile($relFileRef, $cycle_counter, $returnFiles, $newString, $includedFiles, '', $absDirPath);
1011  }
1012  $newString .= '### <INCLUDE_TYPOSCRIPT: source="DIR:' . $dirPath . '"' . $optionalProperties . '> END:' . LF . LF;
1013  } else {
1014  $newString .= self::typoscriptIncludeError('The path "' . $resolvedDirPath . '" is invalid.');
1015  }
1016  }
1017 
1026  protected static function typoscriptIncludeError($error)
1027  {
1028  GeneralUtility::sysLog($error, 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
1029  return "\n###\n### ERROR: " . $error . "\n###\n\n";
1030  }
1031 
1038  public static function checkIncludeLines_array(array $array)
1039  {
1040  foreach ($array as $k => $v) {
1041  $array[$k] = self::checkIncludeLines($array[$k]);
1042  }
1043  return $array;
1044  }
1045 
1059  public static function extractIncludes($string, $cycle_counter = 1, array $extractedFileNames = [], $parentFilenameOrPath = '')
1060  {
1061  if ($cycle_counter > 10) {
1062  GeneralUtility::sysLog('It appears like TypoScript code is looping over itself. Check your templates for "&lt;INCLUDE_TYPOSCRIPT: ..." tags', 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
1063  return '
1064 ###
1065 ### ERROR: Recursion!
1066 ###
1067 ';
1068  }
1069  $expectedEndTag = '';
1070  $fileContent = [];
1071  $restContent = [];
1072  $fileName = null;
1073  $inIncludePart = false;
1074  $lines = preg_split("/\r\n|\n|\r/", $string);
1075  $skipNextLineIfEmpty = false;
1076  $openingCommentedIncludeStatement = null;
1077  $optionalProperties = '';
1078  foreach ($lines as $line) {
1079  // \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines inserts
1080  // an additional empty line, remove this again
1081  if ($skipNextLineIfEmpty) {
1082  if (trim($line) === '') {
1083  continue;
1084  }
1085  $skipNextLineIfEmpty = false;
1086  }
1087 
1088  // Outside commented include statements
1089  if (!$inIncludePart) {
1090  // Search for beginning commented include statements
1091  if (preg_match('/###\\s*<INCLUDE_TYPOSCRIPT:\\s*source\\s*=\\s*"\\s*((?i)file|dir)\\s*:\\s*([^"]*)"(.*)>\\s*BEGIN/i', $line, $matches)) {
1092  // Found a commented include statement
1093 
1094  // Save this line in case there is no ending tag
1095  $openingCommentedIncludeStatement = trim($line);
1096  $openingCommentedIncludeStatement = preg_replace('/\\s*### Warning: .*###\\s*/', '', $openingCommentedIncludeStatement);
1097 
1098  // type of match: FILE or DIR
1099  $inIncludePart = strtoupper($matches[1]);
1100  $fileName = $matches[2];
1101  $optionalProperties = $matches[3];
1102 
1103  $expectedEndTag = '### <INCLUDE_TYPOSCRIPT: source="' . $inIncludePart . ':' . $fileName . '"' . $optionalProperties . '> END';
1104  // Strip all whitespace characters to make comparison safer
1105  $expectedEndTag = strtolower(preg_replace('/\s/', '', $expectedEndTag));
1106  } else {
1107  // If this is not a beginning commented include statement this line goes into the rest content
1108  $restContent[] = $line;
1109  }
1110  //if (is_array($matches)) GeneralUtility::devLog('matches', 'TypoScriptParser', 0, $matches);
1111  } else {
1112  // Inside commented include statements
1113  // Search for the matching ending commented include statement
1114  $strippedLine = preg_replace('/\s/', '', $line);
1115  if (stripos($strippedLine, $expectedEndTag) !== false) {
1116  // Found the matching ending include statement
1117  $fileContentString = implode(PHP_EOL, $fileContent);
1118 
1119  // Write the content to the file
1120 
1121  // Resolve a possible relative paths if a parent file is given
1122  if ($parentFilenameOrPath !== '' && $fileName[0] === '.') {
1123  $realFileName = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($parentFilenameOrPath, $fileName);
1124  } else {
1125  $realFileName = $fileName;
1126  }
1127  $realFileName = GeneralUtility::getFileAbsFileName($realFileName);
1128 
1129  if ($inIncludePart === 'FILE') {
1130  // Some file checks
1132  throw new \UnexpectedValueException(sprintf('File "%s" was not included since it is not allowed due to fileDenyPattern.', $fileName), 1382651858);
1133  }
1134  if (empty($realFileName)) {
1135  throw new \UnexpectedValueException(sprintf('"%s" is not a valid file location.', $fileName), 1294586441);
1136  }
1137  if (!is_writable($realFileName)) {
1138  throw new \RuntimeException(sprintf('"%s" is not writable.', $fileName), 1294586442);
1139  }
1140  if (in_array($realFileName, $extractedFileNames)) {
1141  throw new \RuntimeException(sprintf('Recursive/multiple inclusion of file "%s"', $realFileName), 1294586443);
1142  }
1143  $extractedFileNames[] = $realFileName;
1144 
1145  // Recursive call to detected nested commented include statements
1146  $fileContentString = self::extractIncludes($fileContentString, $cycle_counter + 1, $extractedFileNames, $realFileName);
1147 
1148  // Write the content to the file
1149  if (!GeneralUtility::writeFile($realFileName, $fileContentString)) {
1150  throw new \RuntimeException(sprintf('Could not write file "%s"', $realFileName), 1294586444);
1151  }
1152  // Insert reference to the file in the rest content
1153  $restContent[] = '<INCLUDE_TYPOSCRIPT: source="FILE:' . $fileName . '"' . $optionalProperties . '>';
1154  } else {
1155  // must be DIR
1156 
1157  // Some file checks
1158  if (empty($realFileName)) {
1159  throw new \UnexpectedValueException(sprintf('"%s" is not a valid location.', $fileName), 1366493602);
1160  }
1161  if (!is_dir($realFileName)) {
1162  throw new \RuntimeException(sprintf('"%s" is not a directory.', $fileName), 1366493603);
1163  }
1164  if (in_array($realFileName, $extractedFileNames)) {
1165  throw new \RuntimeException(sprintf('Recursive/multiple inclusion of directory "%s"', $realFileName), 1366493604);
1166  }
1167  $extractedFileNames[] = $realFileName;
1168 
1169  // Recursive call to detected nested commented include statements
1170  self::extractIncludes($fileContentString, $cycle_counter + 1, $extractedFileNames, $realFileName);
1171 
1172  // just drop content between tags since it should usually just contain individual files from that dir
1173 
1174  // Insert reference to the dir in the rest content
1175  $restContent[] = '<INCLUDE_TYPOSCRIPT: source="DIR:' . $fileName . '"' . $optionalProperties . '>';
1176  }
1177 
1178  // Reset variables (preparing for the next commented include statement)
1179  $fileContent = [];
1180  $fileName = null;
1181  $inIncludePart = false;
1182  $openingCommentedIncludeStatement = null;
1183  // \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines inserts
1184  // an additional empty line, remove this again
1185  $skipNextLineIfEmpty = true;
1186  } else {
1187  // If this is not an ending commented include statement this line goes into the file content
1188  $fileContent[] = $line;
1189  }
1190  }
1191  }
1192  // If we're still inside commented include statements copy the lines back to the rest content
1193  if ($inIncludePart) {
1194  $restContent[] = $openingCommentedIncludeStatement . ' ### Warning: Corresponding end line missing! ###';
1195  $restContent = array_merge($restContent, $fileContent);
1196  }
1197  $restContentString = implode(PHP_EOL, $restContent);
1198  return $restContentString;
1199  }
1200 
1207  public static function extractIncludes_array(array $array)
1208  {
1209  foreach ($array as $k => $v) {
1210  $array[$k] = self::extractIncludes($array[$k]);
1211  }
1212  return $array;
1213  }
1214 
1215  /**********************************
1216  *
1217  * Syntax highlighting
1218  *
1219  *********************************/
1229  public function doSyntaxHighlight($string, $lineNum = '', $highlightBlockMode = false)
1230  {
1231  $this->syntaxHighLight = 1;
1232  $this->highLightData = [];
1233  $this->errors = [];
1234  // 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.
1235  $string = str_replace(CR, '', $string);
1236  $this->parse($string);
1237  return $this->syntaxHighlight_print($lineNum, $highlightBlockMode);
1238  }
1239 
1250  public function regHighLight($code, $pointer, $strlen = -1)
1251  {
1252  if ($strlen === -1) {
1253  $this->highLightData[$pointer] = [[$code, 0]];
1254  } else {
1255  $this->highLightData[$pointer][] = [$code, $strlen];
1256  }
1257  $this->highLightData_bracelevel[$pointer] = $this->inBrace;
1258  }
1259 
1269  public function syntaxHighlight_print($lineNumDat, $highlightBlockMode)
1270  {
1271  // Registers all error messages in relation to their linenumber
1272  $errA = [];
1273  foreach ($this->errors as $err) {
1274  $errA[$err[2]][] = $err[0];
1275  }
1276  // Generates the syntax highlighted output:
1277  $lines = [];
1278  foreach ($this->raw as $rawP => $value) {
1279  $start = 0;
1280  $strlen = strlen($value);
1281  $lineC = '';
1282  if (is_array($this->highLightData[$rawP])) {
1283  foreach ($this->highLightData[$rawP] as $set) {
1284  $len = $strlen - $start - $set[1];
1285  if ($len > 0) {
1286  $part = substr($value, $start, $len);
1287  $start += $len;
1288  $st = $this->highLightStyles[isset($this->highLightStyles[$set[0]]) ? $set[0] : 'default'];
1289  if (!$highlightBlockMode || $set[0] !== 'prespace') {
1290  $lineC .= $st[0] . htmlspecialchars($part) . $st[1];
1291  }
1292  } elseif ($len < 0) {
1293  debug([$len, $value, $rawP]);
1294  }
1295  }
1296  } else {
1297  debug([$value]);
1298  }
1299  if (strlen($value) > $start) {
1300  $lineC .= $this->highLightStyles['ignored'][0] . htmlspecialchars(substr($value, $start)) . $this->highLightStyles['ignored'][1];
1301  }
1302  if ($errA[$rawP]) {
1303  $lineC .= $this->highLightStyles['error'][0] . '<strong> - ERROR:</strong> ' . htmlspecialchars(implode(';', $errA[$rawP])) . $this->highLightStyles['error'][1];
1304  }
1305  if ($highlightBlockMode && $this->highLightData_bracelevel[$rawP]) {
1306  $lineC = str_pad('', $this->highLightData_bracelevel[$rawP] * 2, ' ', STR_PAD_LEFT) . '<span style="' . $this->highLightBlockStyles . ($this->highLightBlockStyles_basecolor ? 'background-color: ' . $this->modifyHTMLColorAll($this->highLightBlockStyles_basecolor, -$this->highLightData_bracelevel[$rawP] * 16) : '') . '">' . ($lineC !== '' ? $lineC : '&nbsp;') . '</span>';
1307  }
1308  if (is_array($lineNumDat)) {
1309  $lineNum = $rawP + $lineNumDat[0];
1310  if ($this->parentObject instanceof ExtendedTemplateService) {
1311  $lineNum = $this->parentObject->ext_lnBreakPointWrap($lineNum, $lineNum);
1312  }
1313  $lineC = $this->highLightStyles['linenum'][0] . str_pad($lineNum, 4, ' ', STR_PAD_LEFT) . ':' . $this->highLightStyles['linenum'][1] . ' ' . $lineC;
1314  }
1315  $lines[] = $lineC;
1316  }
1317  return '<pre class="ts-hl">' . implode(LF, $lines) . '</pre>';
1318  }
1319 
1323  protected function getTimeTracker()
1324  {
1325  return isset($GLOBALS['TT']) ? $GLOBALS['TT'] : null;
1326  }
1327 
1338  protected function modifyHTMLColor($color, $R, $G, $B)
1339  {
1340  // This takes a hex-color (# included!) and adds $R, $G and $B to the HTML-color (format: #xxxxxx) and returns the new color
1341  $nR = MathUtility::forceIntegerInRange(hexdec(substr($color, 1, 2)) + $R, 0, 255);
1342  $nG = MathUtility::forceIntegerInRange(hexdec(substr($color, 3, 2)) + $G, 0, 255);
1343  $nB = MathUtility::forceIntegerInRange(hexdec(substr($color, 5, 2)) + $B, 0, 255);
1344  return '#' . substr(('0' . dechex($nR)), -2) . substr(('0' . dechex($nG)), -2) . substr(('0' . dechex($nB)), -2);
1345  }
1346 
1355  protected function modifyHTMLColorAll($color, $all)
1356  {
1357  return $this->modifyHTMLColor($color, $all, $all, $all);
1358  }
1359 }
static extractIncludes($string, $cycle_counter=1, array $extractedFileNames=[], $parentFilenameOrPath='')
static includeFile($filename, $cycle_counter=1, $returnFiles=false, &$newString='', array &$includedFiles=[], $optionalProperties='', $parentFilenameOrPath='')
debug($variable='', $name=' *variable *', $line=' *line *', $file=' *file *', $recursiveDepth=3, $debugLevel='E_DEBUG')
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
doSyntaxHighlight($string, $lineNum='', $highlightBlockMode=false)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static getAbsolutePathOfRelativeReferencedFileOrPath($baseFilenameOrPath, $includeFileName)
static verifyFilenameAgainstDenyPattern($filename)
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
setVal($string, array &$setup, $value, $wipeOut=false)
syntaxHighlight_print($lineNumDat, $highlightBlockMode)
static includeDirectory($dirPath, $cycle_counter=1, $returnFiles=false, &$newString='', array &$includedFiles=[], $optionalProperties='', $parentFilenameOrPath='')
static getAllFilesAndFoldersInPath(array $fileArr, $path, $extList='', $regDirs=false, $recursivityLevels=99, $excludePattern='')
executeValueModifier($modifierName, $modifierArgument=null, $currentValue=null)
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
static beginsWith($haystack, $needle)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static writeFile($file, $content, $changePermissions=false)