Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00080 class t3lib_TSparser { 00081 var $strict = 1; // If set, then key names cannot contain characters other than [:alnum:]_\.- 00082 00083 // Internal 00084 var $setup = Array(); // TypoScript hierarchy being build during parsing. 00085 var $raw; // raw data, the input string exploded by chr(10) 00086 var $rawP; // pointer to entry in raw data array 00087 var $lastComment=''; // Holding the value of the last comment 00088 var $commentSet=0; // Internally set, used as internal flag to create a multi-line comment (one of those like /*... */) 00089 var $multiLineEnabled=0; // Internally set, when multiline value is accumulated 00090 var $multiLineObject=''; // Internally set, when multiline value is accumulated 00091 var $multiLineValue=array(); // Internally set, when multiline value is accumulated 00092 var $inBrace = 0; // Internally set, when in brace. Counter. 00093 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. 00094 var $sections=array(); // Tracking all conditions found 00095 var $sectionsMatch=array(); // Tracking all matching conditions found 00096 var $syntaxHighLight = 0; // If set, then syntax highlight mode is on; Call the function syntaxHighlight() to use this function 00097 var $highLightData=array(); // Syntax highlight data is accumulated in this array. Used by syntaxHighlight_print() to construct the output. 00098 var $highLightData_bracelevel = array(); // Syntax highlight data keeping track of the curly brace level for each line 00099 00100 // Debugging, analysis: 00101 var $regComments = 0; // DO NOT register the comments. This is default for the ordinary sitetemplate! 00102 var $regLinenumbers = 0; // DO NOT register the linenumbers. This is default for the ordinary sitetemplate! 00103 var $errors=array(); // Error accumulation array. 00104 var $lineNumberOffset=0; // Used for the error messages line number reporting. Set externally. 00105 var $breakPointLN=0; // Line for break point. 00106 var $highLightStyles=array( 00107 'prespace' => array('<span class="ts-prespace">','</span>'), // Space before any content on a line 00108 'objstr_postspace' => array('<span class="ts-objstr_postspace">','</span>'), // Space after the object string on a line 00109 'operator_postspace' => array('<span class="ts-operator_postspace">','</span>'), // Space after the operator on a line 00110 'operator' => array('<span class="ts-operator">','</span>'), // The operator char 00111 'value' => array('<span class="ts-value">','</span>'), // The value of a line 00112 'objstr' => array('<span class="ts-objstr">','</span>'), // The object string of a line 00113 'value_copy' => array('<span class="ts-value_copy">','</span>'), // The value when the copy syntax (<) is used; that means the object reference 00114 'value_unset' => array('<span class="ts-value_unset">','</span>'), // The value when an object is unset. Should not exist. 00115 'ignored' => array('<span class="ts-ignored">','</span>'), // The "rest" of a line which will be ignored. 00116 'default' => array('<span class="ts-default">','</span>'), // The default style if none other is applied. 00117 'comment' => array('<span class="ts-comment">','</span>'), // Comment lines 00118 'condition' => array('<span class="ts-condition">','</span>'), // Conditions 00119 'error' => array('<span class="ts-error">','</span>'), // Error messages 00120 'linenum' => array('<span class="ts-linenum">','</span>'), // Line numbers 00121 ); 00122 var $highLightBlockStyles = ''; // Additional attributes for the <span> tags for a blockmode line 00123 var $highLightBlockStyles_basecolor = '#cccccc'; // The hex-HTML color for the blockmode 00124 00125 00133 function parse($string,$matchObj='') { 00134 $this->raw = explode(chr(10),$string); 00135 $this->rawP = 0; 00136 $pre = '[GLOBAL]'; 00137 while($pre) { 00138 if ($this->breakPointLN && $pre=='[_BREAK]') { 00139 $this->error('Breakpoint at '.($this->lineNumberOffset+$this->rawP-2).': Line content was "'.$this->raw[$this->rawP-2].'"',1); 00140 break; 00141 } 00142 00143 if (strtoupper($pre)=='[GLOBAL]' || strtoupper($pre)=='[END]' || (!$this->lastConditionTrue && strtoupper($pre)=='[ELSE]')) { 00144 $pre = trim($this->parseSub($this->setup)); 00145 $this->lastConditionTrue=1; 00146 } else { 00147 if (strtoupper($pre)!='[ELSE]') {$this->sections[md5($pre)]=$pre;} // we're in a specific section. Therefore we log this section 00148 if ((is_object($matchObj) && $matchObj->match($pre)) || $this->syntaxHighLight) { 00149 if (strtoupper($pre)!='[ELSE]') {$this->sectionsMatch[md5($pre)]=$pre;} 00150 $pre = trim($this->parseSub($this->setup)); 00151 $this->lastConditionTrue=1; 00152 } else { 00153 $pre = trim($this->nextDivider()); 00154 $this->lastConditionTrue=0; 00155 } 00156 } 00157 } 00158 if ($this->inBrace) {$this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': The script is short of '.$this->inBrace.' end brace(s)',1); } 00159 if ($this->multiLineEnabled) {$this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': A multiline value section is not ended with a parenthesis!',1); } 00160 $this->lineNumberOffset+=count($this->raw)+1; 00161 } 00162 00169 function nextDivider() { 00170 while (isset($this->raw[$this->rawP])) { 00171 $line = ltrim($this->raw[$this->rawP]); 00172 $this->rawP++; 00173 if ($line && substr($line,0,1)=='[') { 00174 return $line; 00175 } 00176 } 00177 } 00178 00185 function parseSub(&$setup) { 00186 while (isset($this->raw[$this->rawP])) { 00187 $line = ltrim($this->raw[$this->rawP]); 00188 $lineP = $this->rawP; 00189 $this->rawP++; 00190 if ($this->syntaxHighLight) $this->regHighLight("prespace",$lineP,strlen($line)); 00191 00192 // Breakpoint? 00193 if ($this->breakPointLN && ($this->lineNumberOffset+$this->rawP-1)==($this->breakPointLN+1)) { // by adding 1 we get that line processed 00194 return '[_BREAK]'; 00195 } 00196 00197 // Set comment flag? 00198 if (!$this->multiLineEnabled && substr($line,0,2)=='/*') { 00199 $this->commentSet=1; 00200 } 00201 00202 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. 00203 if ($this->multiLineEnabled) { // If multiline is enabled. Escape by ')' 00204 if (substr($line,0,1)==')') { // Multiline ends... 00205 if ($this->syntaxHighLight) $this->regHighLight("operator",$lineP,strlen($line)-1); 00206 $this->multiLineEnabled=0; // Disable multiline 00207 $theValue = implode($this->multiLineValue,chr(10)); 00208 if (strstr($this->multiLineObject,'.')) { 00209 $this->setVal($this->multiLineObject,$setup,array($theValue)); // Set the value deeper. 00210 } else { 00211 $setup[$this->multiLineObject] = $theValue; // Set value regularly 00212 if ($this->lastComment && $this->regComments) { 00213 $setup[$this->multiLineObject.'..'].=$this->lastComment; 00214 } 00215 if ($this->regLinenumbers) { 00216 $setup[$this->multiLineObject.'.ln..'][]=($this->lineNumberOffset+$this->rawP-1); 00217 } 00218 } 00219 } else{ 00220 if ($this->syntaxHighLight) $this->regHighLight("value",$lineP); 00221 $this->multiLineValue[]=$this->raw[($this->rawP-1)]; 00222 } 00223 } elseif ($this->inBrace==0 && substr($line,0,1)=='[') { // Beginning of condition (only on level zero compared to brace-levels 00224 if ($this->syntaxHighLight) $this->regHighLight("condition",$lineP); 00225 return $line; 00226 } else { 00227 if (substr($line,0,1)=='[' && strtoupper(trim($line))=='[GLOBAL]') { // Return if GLOBAL condition is set - no matter what. 00228 if ($this->syntaxHighLight) $this->regHighLight("condition",$lineP); 00229 $this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': On return to [GLOBAL] scope, the script was short of '.$this->inBrace.' end brace(s)',1); 00230 $this->inBrace=0; 00231 return $line; 00232 } elseif (strcspn($line,'}#/')!=0) { // If not brace-end or comment 00233 $varL = strcspn($line,' {=<>('); // Find object name string until we meet an operator VER2: Added '>'!! 00234 $objStrName=trim(substr($line,0,$varL)); 00235 if ($this->syntaxHighLight) $this->regHighLight("objstr",$lineP,strlen(substr($line,$varL))); 00236 if ($objStrName) { 00237 if ($this->strict && eregi('[^[:alnum:]_\.-]',$objStrName,$r)) { 00238 $this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': Object Name String, "'.htmlspecialchars($objStrName).'" contains invalid character "'.$r[0].'". Must be alphanumeric or one of: "_-."'); 00239 } else { 00240 $line = ltrim(substr($line,$varL)); 00241 if ($this->syntaxHighLight) { 00242 $this->regHighLight("objstr_postspace", $lineP, strlen($line)); 00243 if (strlen($line)>0) { 00244 $this->regHighLight("operator", $lineP, strlen($line)-1); 00245 $this->regHighLight("operator_postspace", $lineP, strlen(ltrim(substr($line,1)))); 00246 } 00247 } 00248 switch(substr($line,0,1)) { 00249 case '=': 00250 if ($this->syntaxHighLight) $this->regHighLight("value", $lineP, strlen(ltrim(substr($line,1)))-strlen(trim(substr($line,1)))); 00251 if (strstr($objStrName,'.')) { 00252 $value = Array(); 00253 $value[0] = trim(substr($line,1)); 00254 $this->setVal($objStrName,$setup,$value); 00255 } else { 00256 $setup[$objStrName] = trim(substr($line,1)); 00257 if ($this->lastComment && $this->regComments) { // Setting comment.. 00258 $setup[$objStrName.'..'].=$this->lastComment; 00259 } 00260 if ($this->regLinenumbers) { 00261 $setup[$objStrName.'.ln..'][]=($this->lineNumberOffset+$this->rawP-1); 00262 } 00263 } 00264 break; 00265 case '{': 00266 $this->inBrace++; 00267 if (strstr($objStrName,'.')) { 00268 $exitSig=$this->rollParseSub($objStrName,$setup); 00269 if ($exitSig) return $exitSig; 00270 } else { 00271 if (!isset($setup[$objStrName.'.'])) {$setup[$objStrName.'.'] = Array();} 00272 $exitSig=$this->parseSub($setup[$objStrName.'.']); 00273 if ($exitSig) return $exitSig; 00274 } 00275 break; 00276 case '(': 00277 $this->multiLineObject = $objStrName; 00278 $this->multiLineEnabled=1; 00279 $this->multiLineValue=array(); 00280 break; 00281 case '<': 00282 if ($this->syntaxHighLight) $this->regHighLight("value_copy", $lineP, strlen(ltrim(substr($line,1)))-strlen(trim(substr($line,1)))); 00283 $theVal = trim(substr($line,1)); 00284 if (substr($theVal,0,1)=='.') { 00285 $res = $this->getVal(substr($theVal,1),$setup); 00286 } else { 00287 $res = $this->getVal($theVal,$this->setup); 00288 } 00289 $this->setVal($objStrName,$setup,unserialize(serialize($res)),1); 00290 break; 00291 case '>': 00292 if ($this->syntaxHighLight) $this->regHighLight("value_unset", $lineP, strlen(ltrim(substr($line,1)))-strlen(trim(substr($line,1)))); 00293 $this->setVal($objStrName,$setup,'UNSET'); 00294 break; 00295 default: 00296 $this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': Object Name String, "'.htmlspecialchars($objStrName).'" was not preceeded by any operator, =<>({'); 00297 break; 00298 } 00299 } 00300 $this->lastComment=''; 00301 } 00302 } elseif (substr($line,0,1)=='}') { 00303 $this->inBrace--; 00304 $this->lastComment=''; 00305 if ($this->syntaxHighLight) $this->regHighLight("operator", $lineP, strlen($line)-1); 00306 if ($this->inBrace<0) { 00307 $this->error('Line '.($this->lineNumberOffset+$this->rawP-1).': An end brace is in excess.',1); 00308 $this->inBrace=0; 00309 } else { 00310 break; 00311 } 00312 } else { 00313 if ($this->syntaxHighLight) $this->regHighLight("comment", $lineP); 00314 00315 // Comment. The comments are concatenated in this temporary string: 00316 if ($this->regComments) $this->lastComment.= trim($line).chr(10); 00317 } 00318 } 00319 } 00320 00321 // Unset comment 00322 if ($this->commentSet) { 00323 if ($this->syntaxHighLight) $this->regHighLight("comment", $lineP); 00324 if (substr($line,0,2)=='*/') $this->commentSet=0; 00325 } 00326 } 00327 } 00328 00337 function rollParseSub($string,&$setup) { 00338 if ((string)$string!='') { 00339 $keyLen = strcspn($string,'.'); 00340 if ($keyLen==strlen($string)) { 00341 $key = $string.'.'; 00342 if (!isset($setup[$key])){$setup[$key]=Array();} 00343 $exitSig=$this->parseSub($setup[$key]); 00344 if ($exitSig) return $exitSig; 00345 } else { 00346 $key = substr($string,0,$keyLen).'.'; 00347 if (!isset($setup[$key])){$setup[$key]=Array();} 00348 $exitSig=$this->rollParseSub(substr($string,$keyLen+1),$setup[$key]); 00349 if ($exitSig) return $exitSig; 00350 } 00351 } 00352 } 00353 00361 function getVal($string,$setup) { 00362 if ((string)$string!='') { 00363 $keyLen = strcspn($string,'.'); 00364 if ($keyLen==strlen($string)) { 00365 $retArr=array(); // Added 6/6/03. Shouldn't hurt 00366 if (isset($setup[$string])) {$retArr[0]=$setup[$string]; } 00367 if (isset($setup[$string.'.'])) {$retArr[1]=$setup[$string.'.']; } 00368 return $retArr; 00369 } else { 00370 $key = substr($string,0,$keyLen).'.'; 00371 if ($setup[$key]) { 00372 return $this->getVal(substr($string,$keyLen+1),$setup[$key]); 00373 } 00374 } 00375 } 00376 } 00377 00387 function setVal($string,&$setup,$value,$wipeOut=0) { 00388 if ((string)$string!='') { 00389 $keyLen = strcspn($string,'.'); 00390 if ($keyLen==strlen($string)) { 00391 if ($value=='UNSET') { 00392 unset($setup[$string]); 00393 unset($setup[$string.'.']); 00394 if ($this->regLinenumbers) { 00395 $setup[$string.'.ln..'][]=($this->lineNumberOffset+$this->rawP-1).'>'; 00396 } 00397 } else { 00398 $lnRegisDone=0; 00399 if ($wipeOut && $this->strict) { 00400 if ((isset($setup[$string]) && !isset($value[0])) || (isset($setup[$string.'.']) && !isset($value[1]))) {$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);} 00401 unset($setup[$string]); 00402 unset($setup[$string.'.']); 00403 if ($this->regLinenumbers) { 00404 $setup[$string.'.ln..'][]=($this->lineNumberOffset+$this->rawP-1).'<'; 00405 $lnRegisDone=1; 00406 } 00407 } 00408 if (isset($value[0])) {$setup[$string] = $value[0];} 00409 if (isset($value[1])) {$setup[$string.'.'] = $value[1];} 00410 if ($this->lastComment && $this->regComments) { 00411 $setup[$string.'..'].=$this->lastComment; 00412 } 00413 if ($this->regLinenumbers && !$lnRegisDone) { 00414 $setup[$string.'.ln..'][]=($this->lineNumberOffset+$this->rawP-1); 00415 } 00416 } 00417 } else { 00418 $key = substr($string,0,$keyLen).'.'; 00419 if (!isset($setup[$key])){$setup[$key]=Array();} 00420 $this->setVal(substr($string,$keyLen+1),$setup[$key],$value); 00421 } 00422 } 00423 } 00424 00433 function error($err,$num=2) { 00434 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage($err,$num); 00435 $this->errors[]=array($err,$num,$this->rawP-1,$this->lineNumberOffset); 00436 } 00437 00445 function checkIncludeLines($string) { 00446 $splitStr='<INCLUDE_TYPOSCRIPT:'; 00447 if (strstr($string,$splitStr)) { 00448 $newString=''; 00449 $allParts = explode($splitStr,chr(10).$string.chr(10)); // adds line break char before/after 00450 reset($allParts); 00451 while(list($c,$v)=each($allParts)) { 00452 if (!$c) { // first goes through 00453 $newString.=$v; 00454 } elseif (ereg("\r?\n[ ]*$",$allParts[$c-1])) { // There must be a line-break char before. 00455 $subparts=explode('>',$v,2); 00456 if (ereg("^[ ]*\r?\n",$subparts[1])) { // There must be a line-break char after 00457 // SO, the include was positively recognized: 00458 $newString.='### '.$splitStr.$subparts[0].'> BEGIN:'.chr(10); 00459 $params = t3lib_div::get_tag_attributes($subparts[0]); 00460 if ($params['source']) { 00461 $sourceParts = explode(':',$params['source'],2); 00462 switch(strtolower(trim($sourceParts[0]))) { 00463 case 'file': 00464 $filename = t3lib_div::getFileAbsFileName(trim($sourceParts[1])); 00465 if (strcmp($filename,'')) { // Must exist and must not contain '..' and must be relative 00466 if (@is_file($filename) && filesize($filename)<100000) { // Max. 100 KB include files! 00467 $newString.=t3lib_div::getUrl($filename).chr(10); 00468 } 00469 } 00470 break; 00471 } 00472 } 00473 $newString.='### '.$splitStr.$subparts[0].'> END:'.chr(10); 00474 $newString.=$subparts[1]; 00475 } else $newString.=$splitStr.$v; 00476 } else $newString.=$splitStr.$v; 00477 } 00478 $string=substr($newString,1,-1); // not the first/last linebreak char. 00479 } 00480 return $string; 00481 } 00482 00489 function checkIncludeLines_array($array) { 00490 reset($array); 00491 while(list($k)=each($array)) { 00492 $array[$k]=t3lib_TSparser::checkIncludeLines($array[$k]); 00493 } 00494 return $array; 00495 } 00496 00497 00498 00499 00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513 00514 00515 00516 00517 /********************************** 00518 * 00519 * Syntax highlighting 00520 * 00521 *********************************/ 00522 00532 function doSyntaxHighlight($string,$lineNum='',$highlightBlockMode=0) { 00533 $this->syntaxHighLight=1; 00534 $this->highLightData=array(); 00535 $this->error=array(); 00536 $string = str_replace(chr(13),'',$string); // This is done in order to prevent empty <span>..</span> sections around chr(13) content. Should not do anything but help lessen the amount of HTML code. 00537 00538 $this->parse($string); 00539 00540 return $this->syntaxHighlight_print($lineNum,$highlightBlockMode); 00541 } 00542 00553 function regHighLight($code,$pointer,$strlen=-1) { 00554 if ($strlen==-1) { 00555 $this->highLightData[$pointer] = array(array($code,0)); 00556 } else { 00557 $this->highLightData[$pointer][] = array($code,$strlen); 00558 } 00559 $this->highLightData_bracelevel[$pointer] = $this->inBrace; 00560 } 00561 00571 function syntaxHighlight_print($lineNumDat,$highlightBlockMode) { 00572 // Registers all error messages in relation to their linenumber 00573 $errA=array(); 00574 foreach($this->errors as $err) { 00575 $errA[$err[2]][]=$err[0]; 00576 } 00577 // Generates the syntax highlighted output: 00578 $lines=array(); 00579 foreach($this->raw as $rawP => $value) { 00580 $start=0; 00581 $strlen=strlen($value); 00582 $lineC=''; 00583 00584 if (is_array($this->highLightData[$rawP])) { 00585 foreach($this->highLightData[$rawP] as $set) { 00586 $len = $strlen-$start-$set[1]; 00587 if ($len > 0) { 00588 $part = substr($value,$start,$len); 00589 $start+=$len; 00590 $st = $this->highLightStyles[(isset($this->highLightStyles[$set[0]])?$set[0]:'default')]; 00591 if (!$highlightBlockMode || $set[0]!='prespace') $lineC.=$st[0].htmlspecialchars($part).$st[1]; 00592 }elseif ($len < 0) debug(array($len,$value,$rawP)); 00593 } 00594 } else debug(array($value)); 00595 00596 if (strlen(substr($value,$start))) $lineC.=$this->highLightStyles['ignored'][0].htmlspecialchars(substr($value,$start)).$this->highLightStyles['ignored'][1]; 00597 00598 if ($errA[$rawP]) { 00599 $lineC.=$this->highLightStyles['error'][0].'<strong> - ERROR:</strong> '.htmlspecialchars(implode(';',$errA[$rawP])).$this->highLightStyles['error'][1]; 00600 } 00601 00602 if ($highlightBlockMode && $this->highLightData_bracelevel[$rawP]) { 00603 $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:' ').'</span>'; 00604 } 00605 00606 if (is_array($lineNumDat)) { 00607 $lineNum = $rawP+$lineNumDat[0]; 00608 $lineC = $this->highLightStyles['linenum'][0].str_pad($lineNum,4,' ',STR_PAD_LEFT).':'.$this->highLightStyles['linenum'][1].' '.$lineC; 00609 } 00610 00611 00612 $lines[] = $lineC; 00613 } 00614 00615 return '<pre class="ts-hl">'.implode(chr(10),$lines).'</pre>'; 00616 } 00617 } 00618 00619 00620 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser.php']) { 00621 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tsparser.php']); 00622 } 00623 ?>