00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00080 class t3lib_TSparser {
00081 var $strict = 1;
00082
00083
00084 var $setup = Array();
00085 var $raw;
00086 var $rawP;
00087 var $lastComment='';
00088 var $commentSet=0;
00089 var $multiLineEnabled=0;
00090 var $multiLineObject='';
00091 var $multiLineValue=array();
00092 var $inBrace = 0;
00093 var $lastConditionTrue = 1;
00094 var $sections=array();
00095 var $sectionsMatch=array();
00096 var $syntaxHighLight = 0;
00097 var $highLightData=array();
00098 var $highLightData_bracelevel = array();
00099
00100
00101 var $regComments = 0;
00102 var $regLinenumbers = 0;
00103 var $errors=array();
00104 var $lineNumberOffset=0;
00105 var $breakPointLN=0;
00106 var $highLightStyles=array(
00107 'prespace' => array('<span class="ts-prespace">','</span>'),
00108 'objstr_postspace' => array('<span class="ts-objstr_postspace">','</span>'),
00109 'operator_postspace' => array('<span class="ts-operator_postspace">','</span>'),
00110 'operator' => array('<span class="ts-operator">','</span>'),
00111 'value' => array('<span class="ts-value">','</span>'),
00112 'objstr' => array('<span class="ts-objstr">','</span>'),
00113 'value_copy' => array('<span class="ts-value_copy">','</span>'),
00114 'value_unset' => array('<span class="ts-value_unset">','</span>'),
00115 'ignored' => array('<span class="ts-ignored">','</span>'),
00116 'default' => array('<span class="ts-default">','</span>'),
00117 'comment' => array('<span class="ts-comment">','</span>'),
00118 'condition' => array('<span class="ts-condition">','</span>'),
00119 'error' => array('<span class="ts-error">','</span>'),
00120 'linenum' => array('<span class="ts-linenum">','</span>'),
00121 );
00122 var $highLightBlockStyles = '';
00123 var $highLightBlockStyles_basecolor = '#cccccc';
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;}
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
00193 if ($this->breakPointLN && ($this->lineNumberOffset+$this->rawP-1)==($this->breakPointLN+1)) {
00194 return '[_BREAK]';
00195 }
00196
00197
00198 if (!$this->multiLineEnabled && substr($line,0,2)=='
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 ') $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();
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));
00450 reset($allParts);
00451 while(list($c,$v)=each($allParts)) {
00452 if (!$c) {
00453 $newString.=$v;
00454 } elseif (ereg("\r?\n[ ]*$",$allParts[$c-1])) {
00455 $subparts=explode('>',$v,2);
00456 if (ereg("^[ ]*\r?\n",$subparts[1])) {
00457
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,'')) {
00466 if (@is_file($filename) && filesize($filename)<100000) {
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);
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
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);
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
00573 $errA=array();
00574 foreach($this->errors as $err) {
00575 $errA[$err[2]][]=$err[0];
00576 }
00577
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 ?>