PageRenderTime 222ms CodeModel.GetById 146ms app.highlight 59ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/tbs.php

https://github.com/vivaserver/sys
PHP | 3265 lines | 2818 code | 279 blank | 168 comment | 688 complexity | 7d0e3e00f094caeb39c26ecdabe4531e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/*
   3********************************************************
   4TinyButStrong - Template Engine for Pro and Beginners
   5------------------------
   6Version  : 3.1.1 for PHP >= 5.0
   7Date     : 2006-06-25
   8Web site : www.tinybutstrong.com
   9Author   : skrol29@freesurf.fr
  10********************************************************
  11This library is free software.
  12You can redistribute and modify it even for commercial usage,
  13but you must accept and respect the LPGL License version 2.1.
  14*/
  15// Check PHP version
  16if (PHP_VERSION<'5') echo '<br><b>TinyButStrong Error</b> (PHP Version Check) : Your PHP version is '.PHP_VERSION.' while this TinyButStrong needs PHP version 5 or higher. There is also TinyButStrong for PHP >= 4.0.6.';
  17if (PHP_VERSION<'5.1.0') {
  18	function property_exists(&$obj,$prop) {return true;}
  19}
  20
  21// Render flags
  22define('TBS_NOTHING', 0);
  23define('TBS_OUTPUT', 1);
  24define('TBS_EXIT', 2);
  25
  26// Plug-ins actions
  27define('TBS_INSTALL', -1);
  28define('TBS_ISINSTALLED', -3);
  29
  30// *********************************************
  31
  32class clsTbsLocator {
  33	public $PosBeg = false;
  34	public $PosEnd = false;
  35	public $Enlarged = false;
  36	public $FullName = false;
  37	public $SubName = '';
  38	public $SubOk = false;
  39	public $SubLst = array();
  40	public $SubNbr = 0;
  41	public $PrmLst = array();
  42	public $PrmIfNbr = false;
  43	public $MagnetId = false;
  44	public $BlockFound = false;
  45	public $FirstMerge = true;
  46	public $ConvProtect = true;
  47	public $ConvHtml = true;
  48	public $ConvMode = 1; // Normal
  49	public $ConvBr = true;
  50}
  51
  52// *********************************************
  53
  54class clsTbsDataSource {
  55
  56public $Type = false;
  57public $SubType = 0;
  58public $SrcId = false;
  59public $Query = '';
  60public $RecSet = false;
  61public $RecKey = '';
  62public $RecNum = 0;
  63public $RecNumInit = 0;
  64public $RecSaving = false;
  65public $RecSaved = false;
  66public $RecBuffer = false;
  67public $CurrRec = false;
  68public $TBS = false;
  69public $OnDataOk = false;
  70public $OnDataPrm = false;
  71public $OnDataPrmDone = array();
  72public $OnDataPi = false;
  73
  74function DataAlert($Msg) {
  75	return $this->TBS->meth_Misc_Alert('when merging block '.$this->TBS->_ChrOpen.$this->TBS->_CurrBlock.$this->TBS->_ChrClose,$Msg);
  76}
  77
  78function DataPrepare(&$SrcId,&$TBS) {
  79
  80	$this->SrcId =& $SrcId;
  81	$this->TBS =& $TBS;
  82
  83	if (is_array($SrcId)) {
  84		$this->Type = 0;
  85	} elseif (is_resource($SrcId)) {
  86
  87		$Key = get_resource_type($SrcId);
  88		switch ($Key) {
  89		case 'mysql link'            : $this->Type = 1; break;
  90		case 'mysql link persistent' : $this->Type = 1; break;
  91		case 'mysql result'          : $this->Type = 1; $this->SubType = 1; break;
  92		case 'pgsql link'            : $this->Type = 8; break;
  93		case 'pgsql link persistent' : $this->Type = 8; break;
  94		case 'pgsql result'          : $this->Type = 8; $this->SubType = 1; break;
  95		case 'sqlite database'       : $this->Type = 9; break;
  96		case 'sqlite database (persistent)'	: $this->Type = 9; break;
  97		case 'sqlite result'         : $this->Type = 9; $this->SubType = 1; break;
  98		default :
  99			$SubKey = 'resource type';
 100			$this->Type = 7;
 101			$x = strtolower($Key);
 102			$x = str_replace('-','_',$x);
 103			$Function = '';
 104			$i = 0;
 105			$iMax = strlen($x);
 106			while ($i<$iMax) {
 107				if (($x[$i]==='_') or (($x[$i]>='a') and ($x[$i]<='z')) or (($x[$i]>='0') and ($x[$i]<='9'))) {
 108					$Function .= $x[$i];
 109					$i++;
 110				} else {
 111					$i = $iMax;
 112				}
 113			}
 114		}
 115
 116	} elseif (is_string($SrcId)) {
 117
 118		switch (strtolower($SrcId)) {
 119		case 'array' : $this->Type = 0; $this->SubType = 1; break;
 120		case 'clear' : $this->Type = 0; $this->SubType = 3; break;
 121		case 'mysql' : $this->Type = 1; $this->SubType = 2; break;
 122		case 'text'  : $this->Type = 4; break;
 123		case 'num'   : $this->Type = 6; break;
 124		default :
 125			if ($SrcId[0]==='~') {
 126				$FctInfo = $SrcId;
 127				$ErrMsg = false;
 128				if ($TBS->meth_Misc_UserFctCheck($FctInfo,$ErrMsg,true)) {
 129					$this->FctOpen  =& $FctInfo[0];
 130					$this->FctFetch =& $FctInfo[1];
 131					$this->FctClose =& $FctInfo[2];
 132					$this->FctPrm = array(false,0);
 133					$this->SrcId =& $FctInfo[0][0];
 134					$this->Type = 11;
 135				} else {
 136					$this->Type = $this->DataAlert($ErrMsg);
 137				}
 138			} else {
 139				$Key = $SrcId;
 140				$SubKey = 'keyword';
 141				$this->Type = 7;
 142				$Function = $SrcId;
 143			}
 144		}
 145
 146	} elseif (is_object($SrcId)) {
 147		if (method_exists($SrcId,'tbsdb_open')) {
 148			if (!method_exists($SrcId,'tbsdb_fetch')) {
 149				$this->Type = $this->DataAlert('the expected method \'tbsdb_fetch\' is not found for the class '.get_class($SrcId).'.');
 150			} elseif (!method_exists($SrcId,'tbsdb_close')) {
 151				$this->Type = $this->DataAlert('the expected method \'tbsdb_close\' is not found for the class '.get_class($SrcId).'.');
 152			} else {
 153				$this->Type = 10;
 154			}
 155		} else {
 156			$Key = get_class($SrcId);
 157			$SubKey = 'object type';
 158			$this->Type = 7;
 159			$Function = $Key;
 160		}
 161	} elseif ($SrcId===false) {
 162		$this->DataAlert('the specified source is set to FALSE. Maybe your connection has failed.');
 163	} else {
 164		$this->DataAlert('unsupported variable type : \''.gettype($SrcId).'\'.');
 165	}
 166
 167	if ($this->Type===7) {
 168		$this->FctOpen  = 'tbsdb_'.$Function.'_open';
 169		$Ok = function_exists($this->FctOpen);
 170		if (!$Ok) { // Some extended call can have a suffix in the class name, we check without the suffix
 171			$i = strpos($Function,'_');
 172			if ($i!==false) {
 173				$x = substr($Function,0,$i);
 174				$z  = 'tbsdb_'.$x.'_open';
 175				$Ok = function_exists($z);
 176				if ($Ok) {
 177					$Function = $x;
 178					$this->FctOpen = $z;
 179				}
 180			}
 181		}
 182		if ($Ok) {
 183			$this->FctFetch = 'tbsdb_'.$Function.'_fetch';
 184			$this->FctClose = 'tbsdb_'.$Function.'_close';
 185			if (function_exists($this->FctFetch)) {
 186				if (!function_exists($this->FctClose)) $this->Type = $this->DataAlert('the expected custom function \''.$this->FctClose.'\' is not found.');
 187			} else {
 188				$this->Type = $this->DataAlert('the expected custom function \''.$this->FctFetch.'\' is not found.');
 189			}
 190		} else {
 191			$this->Type = $this->DataAlert('the data source Id \''.$Key.'\' is an unsupported '.$SubKey.' because custom function \''.$this->FctOpen.'\' is not found.');
 192		}
 193	}
 194
 195	return ($this->Type!==false);
 196
 197}
 198
 199function DataOpen(&$Query) {
 200
 201	// Init values
 202	unset($this->CurrRec); $this->CurrRec = true;
 203	if ($this->RecSaved) {
 204		$this->FirstRec = true;
 205		unset($this->RecKey); $this->RecKey = '';
 206		$this->RecNum = $this->RecNumInit;
 207		if ($this->OnDataOk) $this->OnDataArgs[1] =& $this->CurrRec;
 208		return true;
 209	}
 210	unset($this->RecSet); $this->RecSet = false;
 211	$this->RecNumInit = 0;
 212	$this->RecNum = 0;
 213
 214	if (isset($this->TBS->_piOnData)) {
 215		$this->OnDataPi = true;
 216		$this->OnDataPiRef =& $this->TBS->_piOnData;
 217		$this->OnDataOk = true;
 218	}
 219	if ($this->OnDataOk) {
 220		$this->OnDataArgs = array();
 221		$this->OnDataArgs[0] =& $this->TBS->_CurrBlock;
 222		$this->OnDataArgs[1] =& $this->CurrRec;
 223		$this->OnDataArgs[2] =& $this->RecNum;
 224		$this->OnDataArgs[3] =& $this->TBS;
 225	}
 226
 227	switch ($this->Type) {
 228	case 0: // Array
 229		if (($this->SubType===1) and (is_string($Query))) $this->SubType = 2;
 230		if ($this->SubType===0) {
 231			$this->RecSet =& $this->SrcId;
 232		} elseif ($this->SubType===1) {
 233			if (is_array($Query)) {
 234				$this->RecSet =& $Query;
 235			} else {
 236				$this->DataAlert('type \''.gettype($Query).'\' not supported for the Query Parameter going with \'array\' Source Type.');
 237			}
 238		} elseif ($this->SubType===2) {
 239			// TBS query string for array and objects, syntax: "var[item1][item2]->item3[item4]..."
 240			$x = trim($Query);
 241			$z = chr(0);
 242			$x = str_replace(']->',$z,$x);
 243			$x = str_replace('][',$z,$x);
 244			$x = str_replace('->',$z,$x);
 245			$x = str_replace('[',$z,$x);
 246			if (substr($x,strlen($x)-1,1)===']') $x = substr($x,0,strlen($x)-1);
 247			$ItemLst = explode($z,$x);
 248			$ItemNbr = count($ItemLst);
 249			$Item0 =& $ItemLst[0];
 250			// Check first item
 251			if ($Item0[0]==='~') {
 252				$Item0 = substr($Item0,1);
 253				if ($this->TBS->ObjectRef!==false) {
 254					$Var =& $this->TBS->ObjectRef;
 255					$i = 0;
 256				} else {
 257					$i = $this->DataAlert('invalid query \''.$Query.'\' because property ObjectRef is not set.');
 258				}
 259			} else {
 260				if (isset($GLOBALS[$Item0])) {
 261					$Var =& $GLOBALS[$Item0];
 262					$i = 1;
 263				} else {
 264					$i = $this->DataAlert('invalid query \''.$Query.'\' because global variable \''.$Item0.'\' is not found.');
 265				}
 266			}
 267			// Check sub-items
 268			$Empty = false;
 269			while (($i!==false) and ($i<$ItemNbr) and ($Empty===false)) {
 270				$x = $ItemLst[$i];
 271				if (is_array($Var)) {
 272					if (isset($Var[$x])) {
 273						$Var =& $Var[$x];
 274					} else {
 275						$Empty = true;
 276					}
 277				} elseif (is_object($Var)) {
 278					$ArgLst = tbs_Misc_CheckArgLst($x);
 279					if (method_exists($Var,$x)) {
 280						$f = array(&$Var,$x); unset($Var);
 281						$Var = call_user_func_array($f,$ArgLst);
 282					} elseif (isset($Var->$x)) {
 283						$Var =& $Var->$x;
 284					} else {
 285						$Empty = true;
 286					}
 287				} else {
 288					$i = $this->DataAlert('invalid query \''.$Query.'\' because item \''.$ItemLst[$i].'\' is neither an Array nor an Object. Its type is \''.gettype($Var).'\'.');
 289				}
 290				if ($i!==false) $i++;
 291			}
 292			// Assign data
 293			if ($i!==false) {
 294				if ($Empty) {
 295					$this->RecSet = array();
 296				} else {
 297					$this->RecSet =& $Var;
 298				}
 299			}
 300		} elseif ($this->SubType===3) { // Clear
 301			$this->RecSet = array();
 302		}
 303		// First record
 304		if ($this->RecSet!==false) {
 305			$this->RecNbr = $this->RecNumInit + count($this->RecSet);
 306			$this->FirstRec = true;
 307			$this->RecSaved = true;
 308			$this->RecSaving = false;
 309		}
 310		break;
 311	case 1: // MySQL
 312		switch ($this->SubType) {
 313		case 0: $this->RecSet = @mysql_query($Query,$this->SrcId); break;
 314		case 1: $this->RecSet = $this->SrcId; break;
 315		case 2: $this->RecSet = @mysql_query($Query); break;
 316		}
 317		if ($this->RecSet===false) $this->DataAlert('MySql error message when opening the query: '.mysql_error());
 318		break;
 319	case 4: // Text
 320		if (is_string($Query)) {
 321			$this->RecSet =& $Query;
 322		} else {
 323			$this->RecSet = ''.$Query;
 324		}
 325		break;
 326	case 6: // Num
 327		$this->RecSet = true;
 328		$this->NumMin = 1;
 329		$this->NumMax = 1;
 330		$this->NumStep = 1;
 331		if (is_array($Query)) {
 332			if (isset($Query['min'])) $this->NumMin = $Query['min'];
 333			if (isset($Query['step'])) $this->NumStep = $Query['step'];
 334			if (isset($Query['max'])) {
 335				$this->NumMax = $Query['max'];
 336			} else {
 337				$this->RecSet = $this->DataAlert('the \'num\' source is an array that has no value for the \'max\' key.');
 338			}
 339			if ($this->NumStep==0) $this->RecSet = $this->DataAlert('the \'num\' source is an array that has a step value set to zero.');
 340		} else {
 341			$this->NumMax = ceil($Query);
 342		}
 343		if ($this->RecSet) {
 344			if ($this->NumStep>0) {
 345				$this->NumVal = $this->NumMin;
 346			} else {
 347				$this->NumVal = $this->NumMax;
 348			}
 349		}
 350		break;
 351	case 7: // Custom function
 352		$FctOpen = $this->FctOpen;
 353		$this->RecSet = $FctOpen($this->SrcId,$Query);
 354		break;
 355	case 8: // PostgreSQL
 356		switch ($this->SubType) {
 357		case 0: $this->RecSet = @pg_query($this->SrcId,$Query); break;
 358		case 1: $this->RecSet = $this->SrcId; break;
 359		}
 360		if ($this->RecSet===false) $this->DataAlert('PostgreSQL error message when opening the query: '.pg_last_error($this->SrcId));
 361		break;
 362	case 9: // SQLite
 363		switch ($this->SubType) {
 364		case 0: $this->RecSet = @sqlite_query($this->SrcId,$Query); break;
 365		case 1: $this->RecSet = $this->SrcId; break;
 366		}
 367		if ($this->RecSet===false) $this->DataAlert('SQLite error message when opening the query:'.sqlite_error_string(sqlite_last_error($this->SrcId)));
 368		break;
 369	case 10: // Custom method
 370		$this->RecSet = $this->SrcId->tbsdb_open($this->SrcId,$Query);
 371		break;
 372	case 11: // ObjectRef
 373		$this->RecSet = call_user_func_array($this->FctOpen,array(&$this->SrcId,&$Query));
 374		break;
 375	}
 376
 377	if ($this->Type===0) {
 378		unset($this->RecKey); $this->RecKey = '';
 379	} else {
 380		if ($this->RecSaving) {
 381			unset($this->RecBuffer); $this->RecBuffer = array();
 382		}
 383		$this->RecKey =& $this->RecNum; // Not array: RecKey = RecNum
 384	}
 385
 386	return ($this->RecSet!==false);
 387
 388}
 389
 390function DataFetch() {
 391
 392	if ($this->RecSaved) {
 393		if ($this->RecNum<$this->RecNbr) {
 394			if ($this->FirstRec) {
 395				if ($this->SubType===2) { // From string
 396					reset($this->RecSet);
 397					$this->RecKey = key($this->RecSet);
 398					$this->CurrRec =& $this->RecSet[$this->RecKey];
 399				} else {
 400					$this->CurrRec = reset($this->RecSet);
 401					$this->RecKey = key($this->RecSet);
 402				}
 403				$this->FirstRec = false;
 404			} else {
 405				if ($this->SubType===2) { // From string
 406					next($this->RecSet);
 407					$this->RecKey = key($this->RecSet);
 408					$this->CurrRec =& $this->RecSet[$this->RecKey];
 409				} else {
 410					$this->CurrRec = next($this->RecSet);
 411					$this->RecKey = key($this->RecSet);
 412				}
 413			}
 414			if ((!is_array($this->CurrRec)) and (!is_object($this->CurrRec))) $this->CurrRec = array('key'=>$this->RecKey, 'val'=>$this->CurrRec);
 415			$this->RecNum++;
 416			if ($this->OnDataOk) {
 417				if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
 418				if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
 419				if ($this->SubType!==2) $this->RecSet[$this->RecKey] = $this->CurrRec; // save modifications because array reading is done without reference :(
 420			}
 421		} else {
 422			unset($this->CurrRec); $this->CurrRec = false;
 423		}
 424		return;
 425	}
 426
 427	switch ($this->Type) {
 428	case 1: // MySQL
 429		$this->CurrRec = mysql_fetch_assoc($this->RecSet);
 430		break;
 431	case 4: // Text
 432		if ($this->RecNum===0) {
 433			if ($this->RecSet==='') {
 434				$this->CurrRec = false;
 435			} else {
 436				$this->CurrRec =& $this->RecSet;
 437			}
 438		} else {
 439			$this->CurrRec = false;
 440		}
 441		break;
 442	case 6: // Num
 443		if (($this->NumVal>=$this->NumMin) and ($this->NumVal<=$this->NumMax)) {
 444			$this->CurrRec = array('val'=>$this->NumVal);
 445			$this->NumVal += $this->NumStep;
 446		} else {
 447			$this->CurrRec = false;
 448		}
 449		break;
 450	case 7: // Custom function
 451		$FctFetch = $this->FctFetch;
 452		$this->CurrRec = $FctFetch($this->RecSet,$this->RecNum+1);
 453		break;
 454	case 8: // PostgreSQL
 455		$this->CurrRec = pg_fetch_assoc($this->RecSet);
 456		break;
 457	case 9: // SQLite
 458		$this->CurrRec = sqlite_fetch_array($this->RecSet,SQLITE_ASSOC);
 459		break;
 460	case 10: // Custom method
 461		$this->CurrRec = $this->SrcId->tbsdb_fetch($this->RecSet,$this->RecNum+1);
 462		break;
 463	case 11: // ObjectRef
 464		$this->FctPrm[0] =& $this->RecSet; $this->FctPrm[1] = $this->RecNum+1;
 465		$this->CurrRec = call_user_func_array($this->FctFetch,$this->FctPrm);
 466		break;
 467	}
 468
 469	// Set the row count
 470	if ($this->CurrRec!==false) {
 471		$this->RecNum++;
 472		if ($this->OnDataOk) {
 473			$this->OnDataArgs[1] =& $this->CurrRec; // Reference has changed if ($this->SubType===2)
 474			if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
 475			if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
 476		}
 477		if ($this->RecSaving) $this->RecBuffer[$this->RecKey] = $this->CurrRec;
 478	}
 479
 480}
 481
 482function DataClose() {
 483	$this->OnDataOk = false;
 484	$this->OnDataPrm = false;
 485	$this->OnDataPi = false;
 486	if ($this->RecSaved) return;
 487	switch ($this->Type) {
 488	case 1: mysql_free_result($this->RecSet); break;
 489	case 7: $FctClose=$this->FctClose; $FctClose($this->RecSet); break;
 490	case 8: pg_free_result($this->RecSet); break;
 491	case 10: $this->SrcId->tbsdb_close($this->RecSet); break;
 492	case 11: call_user_func_array($this->FctClose,array(&$this->RecSet)); break;
 493	}
 494	if ($this->RecSaving) {
 495		$this->RecSet =& $this->RecBuffer;
 496		$this->RecNbr = $this->RecNumInit + count($this->RecSet);
 497		$this->RecSaving = false;
 498		$this->RecSaved = true;
 499	}
 500}
 501
 502}
 503
 504// *********************************************
 505
 506class clsTinyButStrong {
 507
 508// Public properties
 509public $Source = '';
 510public $Render = 3;
 511public $TplVars = array();
 512public $ObjectRef = false;
 513public $NoErr = false;
 514// Undocumented (can change at any version)
 515public $Version = '3.1.1';
 516public $HtmlCharSet = '';
 517public $TurboBlock = true;
 518public $VarPrefix = '';
 519public $Protect = true;
 520// Private
 521public $_LastFile = '';
 522public $_HtmlCharFct = false;
 523public $_Mode = 0;
 524public $_CurrBlock = '';
 525public $_ChrOpen = '[';
 526public $_ChrClose = ']';
 527public $_ChrVal = '[val]';
 528public $_ChrProtect = '&#91;';
 529public $_PlugIns = array();
 530public $_PlugIns_Ok = false;
 531public $_piOnFrm_Ok = false;
 532
 533function clsTinyButStrong($Chrs='',$VarPrefix='') {
 534	if ($Chrs!=='') {
 535		$Ok = false;
 536		$Len = strlen($Chrs);
 537		if ($Len===2) { // For compatibility
 538			$this->_ChrOpen = $Chrs[0];
 539			$this->_ChrClose = $Chrs[1];
 540			$Ok = true;
 541		} else {
 542			$Pos = strpos($Chrs,',');
 543			if (($Pos!==false) and ($Pos>0) and ($Pos<$Len-1)) {
 544				$this->_ChrOpen = substr($Chrs,0,$Pos);
 545				$this->_ChrClose = substr($Chrs,$Pos+1);
 546				$Ok = true;
 547			}
 548		}
 549		if ($Ok) {
 550			$this->_ChrVal = $this->_ChrOpen.'val'.$this->_ChrClose;
 551			$this->_ChrProtect = '&#'.ord($this->_ChrOpen[0]).';'.substr($this->_ChrOpen,1);
 552		} else {
 553			$this->meth_Misc_Alert('with clsTinyButStrong() function','value \''.$Chrs.'\' is a bad tag delimitor definition.');
 554		}
 555	}
 556	$this->VarPrefix = $VarPrefix;
 557	// Liaison avec variables globales
 558	global $_TBS_FrmMultiLst, $_TBS_FrmSimpleLst, $_TBS_UserFctLst, $_TBS_AutoInstallPlugIns;
 559	if (!isset($_TBS_ObjectRefLink)) {
 560		$_TBS_FrmMultiLst = array();
 561		$_TBS_FrmSimpleLst = array();
 562		$_TBS_UserFctLst = array();
 563	}
 564	$this->_FrmMultiLst =& $_TBS_FrmMultiLst;
 565	$this->_FrmSimpleLst =& $_TBS_FrmSimpleLst;
 566	$this->_UserFctLst =& $_TBS_UserFctLst; 
 567	// Auto-installing plug-ins
 568	if (isset($_TBS_AutoInstallPlugIns)) foreach ($_TBS_AutoInstallPlugIns as $pi) $this->PlugIn(TBS_INSTALL,$pi);
 569}
 570
 571// Public methods
 572function LoadTemplate($File,$HtmlCharSet='') {
 573	$Ok = true;
 574	if ($this->_PlugIns_Ok) {
 575		if (isset($this->_piBeforeLoadTemplate) or isset($this->_piAfterLoadTemplate)) {
 576			// Plug-ins
 577			$ArgLst = func_get_args();
 578			$ArgLst[0] =& $File;
 579			$ArgLst[1] =& $HtmlCharSet;
 580			if (isset($this->_piBeforeLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeLoadTemplate,$ArgLst);
 581		}
 582	}
 583	// Load the file
 584	if ($Ok!==false) {
 585		$x = '';
 586		if (!tbs_Misc_GetFile($x,$File)) return $this->meth_Misc_Alert('with LoadTemplate() method','file \''.$File.'\' is not found or not readable.');
 587		// CharSet analysis
 588		if ($HtmlCharSet==='+') {
 589			$this->Source .= $x;
 590		} else {
 591			$this->Source = $x;
 592			if ($this->_Mode==0) {
 593			$this->_LastFile = $File;
 594			$this->_HtmlCharFct = false;
 595			$this->TplVars = array();
 596			if (is_string($HtmlCharSet)) {
 597				if (($HtmlCharSet!=='') and ($HtmlCharSet[0]==='=')) {
 598					$ErrMsg = false;
 599					$HtmlCharSet = substr($HtmlCharSet,1);
 600					if ($this->meth_Misc_UserFctCheck($HtmlCharSet,$ErrMsg,false)) {
 601						$this->_HtmlCharFct = true;
 602					} else {
 603						$this->meth_Misc_Alert('with LoadTemplate() method',$ErrMsg);
 604						$HtmlCharSet = '';
 605					}
 606				}
 607			} elseif ($HtmlCharSet===false) {
 608				$this->Protect = false;
 609			} else {
 610				$this->meth_Misc_Alert('with LoadTemplate() method','the CharSet argument is not a string.');
 611				$HtmlCharSet = '';
 612			}
 613			$this->HtmlCharSet = $HtmlCharSet;
 614			}
 615		}
 616		// Automatic fields and blocks
 617		$this->meth_Merge_AutoOn($this->Source,'onload',true,true);
 618	}
 619	// Plug-ins
 620	if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piAfterLoadTemplate,$ArgLst);
 621	return $Ok;
 622}
 623
 624function GetBlockSource($BlockName,$List=false,$KeepDefTags=true) {
 625	$RetVal = array();
 626	$Nbr = 0;
 627	$Pos = 0;
 628	$FieldOutside = false;
 629	$P1 = false;
 630	$Mode = ($KeepDefTags) ? 3 : 2;
 631	while ($Loc = $this->meth_Locator_FindBlockNext($this->Source,$BlockName,$Pos,'.',$Mode,$P1,$FieldOutside)) {
 632		$P1 = false;
 633		$Nbr++;
 634		$RetVal[$Nbr] = $Loc->BlockSrc;
 635		if (!$List) return $RetVal[$Nbr];
 636		$Pos = $Loc->PosEnd;
 637	}
 638	if ($List) {
 639		return $RetVal;
 640	} else {
 641		return false;
 642	}
 643}
 644
 645function MergeBlock($BlockLst,$SrcId,$Query='') {
 646	if ($SrcId==='cond') {
 647		$Nbr = 0;
 648		$BlockLst = explode(',',$BlockLst);
 649		foreach ($BlockLst as $Block) {
 650			$Block = trim($Block);
 651			if ($Block!=='') $Nbr += $this->meth_Merge_AutoOn($this->Source,$Block,false,false);
 652		}
 653		return $Nbr;
 654	} else {
 655		return $this->meth_Merge_Block($this->Source,$BlockLst,$SrcId,$Query,false,0);
 656	}
 657}
 658
 659function MergeField($NameLst,$Value=null,$IsUserFct=false) {
 660
 661	$FctCheck = $IsUserFct;
 662	if ($PlugIn = isset($this->_piOnMergeField)) $ArgPi = array('','',&$Value,0,&$this->Source,0,0);
 663	$SubStart = 0;
 664	$Ok = true;
 665
 666	$NameLst = explode(',',$NameLst);
 667	foreach ($NameLst as $Name) {
 668		$Name = trim($Name);
 669		if ($Name==='') continue;
 670		if ($this->meth_Merge_AutoAny($Name)) continue;
 671		if ($PlugIn) $ArgPi[0] = $Name;
 672		$PosBeg = 0;
 673		// Initilize the user function (only once)
 674		if ($FctCheck) {
 675			$FctInfo = $Value;
 676			$ErrMsg = false;
 677			if (!$this->meth_Misc_UserFctCheck($FctInfo,$ErrMsg,false)) return $this->meth_Misc_Alert('with MergeField() method',$ErrMsg);
 678			$FctArg = array('','');
 679			$SubStart = false;
 680			$FctCheck = false;
 681		}
 682		while ($Loc = $this->meth_Locator_FindTbs($this->Source,$Name,$PosBeg,'.')) {
 683			// Apply user function
 684			if ($IsUserFct) {
 685				$FctArg[0] =& $Loc->SubName; $FctArg[1] =& $Loc->PrmLst;
 686				$Value = call_user_func_array($FctInfo,$FctArg);
 687			}
 688			// Plug-ins
 689			if ($PlugIn) {
 690				$ArgPi[1] = $Loc->SubName; $ArgPi[3] =& $Loc->PrmLst; $ArgPi[5] =& $Loc->PosBeg; $ArgPi[6] =& $Loc->PosEnd;
 691				$Ok = $this->meth_PlugIn_RunAll($this->_piOnMergeField,$ArgPi);
 692			}
 693			// Merge the field
 694			if ($Ok) {
 695				$PosBeg = $this->meth_Locator_Replace($this->Source,$Loc,$Value,$SubStart);
 696			} else {
 697				$PosBeg = $Loc->PosEnd;
 698			}
 699		}
 700	}
 701}
 702
 703function Show($Render=false) {
 704	$Ok = true;
 705	if ($Render===false) $Render = $this->Render;
 706	if ($this->_PlugIns_Ok) {
 707		if (isset($this->_piBeforeShow) or isset($this->_piAfterShow)) {
 708			// Plug-ins
 709			$ArgLst = func_get_args();
 710			$ArgLst[0] =& $Render;
 711			if (isset($this->_piBeforeShow)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeShow,$ArgLst);
 712		}
 713	}
 714	if ($Ok!==false) {
 715		$this->meth_Merge_AutoAny('onshow');
 716		$this->meth_Merge_AutoAny('var');
 717	}
 718	if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterShow)) $this->meth_PlugIn_RunAll($this->_piAfterShow,$ArgLst);
 719	if (($Render & TBS_OUTPUT)==TBS_OUTPUT) echo $this->Source;
 720	if (($this->_Mode==0) and (($Render & TBS_EXIT)==TBS_EXIT)) exit;
 721	return $Ok;
 722}
 723
 724function PlugIn($Prm1,$Prm2=0) {
 725
 726	if (is_numeric($Prm1)) {
 727		switch ($Prm1) {
 728		case TBS_INSTALL:
 729			$PlugInId = $Prm2;
 730	  	// Try to install the plug-in
 731			if (isset($this->_PlugIns[$PlugInId])) {
 732				return $this->meth_Misc_Alert('with PlugIn() method','plug-in \''.$PlugInId.'\' is already installed.');
 733			} else {
 734				$ArgLst = func_get_args();
 735				array_shift($ArgLst); array_shift($ArgLst);
 736				return $this->meth_PlugIn_Install($PlugInId,$ArgLst,false);
 737			}
 738		case TBS_ISINSTALLED:
 739	  	// Check if the plug-in is installed
 740			return isset($this->_PlugIns[$Prm2]);
 741		case -4: // Deactivate special plug-ins
 742			$this->_PlugIns_Ok_save = $this->_PlugIns_Ok;
 743			$this->_PlugIns_Ok = false;
 744			return true;
 745		case -5: // Deactivate OnFormat
 746			$this->_piOnFrm_Ok_save = $this->_piOnFrm_Ok;
 747			$this->_piOnFrm_Ok = false;
 748			return true;
 749		case -10:  // Restore
 750			$this->_PlugIns_Ok = $this->_PlugIns_Ok_save;
 751			$this->_piOnFrm_Ok = $this->_piOnFrm_Ok_save;
 752			return true;
 753		}
 754
 755  } elseif (is_string($Prm1)) {
 756  	// Plug-in's command
 757  	$PlugInId = $Prm1;
 758		if (!isset($this->_PlugIns[$PlugInId])) {
 759			if (!$this->meth_PlugIn_Install($PlugInId,array(),true)) return false;
 760		}
 761		if (!isset($this->_piOnCommand[$PlugInId])) return $this->meth_Misc_Alert('with PlugIn() method','plug-in \''.$PlugInId.'\' can\'t run any command because the OnCommand event is not defined or activated.');
 762		$ArgLst = func_get_args();
 763		array_shift($ArgLst);
 764		$Ok = call_user_func_array($this->_piOnCommand[$PlugInId],$ArgLst);
 765		if (is_null($Ok)) $Ok = true;
 766		return $Ok;
 767  }
 768	return $this->meth_Misc_Alert('with PlugIn() method','\''.$Prm1.'\' is an invalid plug-in key, the type of the value is \''.gettype($Prm1).'\'.');
 769
 770}
 771
 772// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
 773
 774function meth_Locator_FindTbs(&$Txt,$Name,$Pos,$ChrSub) {
 775// Find a TBS Locator
 776
 777	$PosEnd = false;
 778	$PosMax = strlen($Txt) -1;
 779	$Start = $this->_ChrOpen.$Name;
 780
 781	do {
 782		// Search for the opening char
 783		if ($Pos>$PosMax) return false;
 784		$Pos = strpos($Txt,$Start,$Pos);
 785
 786		// If found => next chars are analyzed
 787		if ($Pos===false) {
 788			return false;
 789		} else {
 790			$Loc = new clsTbsLocator;
 791			$ReadPrm = false;
 792			$PosX = $Pos + strlen($Start);
 793			$x = $Txt[$PosX];
 794
 795			if ($x===$this->_ChrClose) {
 796				$PosEnd = $PosX;
 797			} elseif ($x===$ChrSub) {
 798				$Loc->SubOk = true; // it is no longer the false value
 799				$ReadPrm = true;
 800				$PosX++;
 801			} elseif (strpos(';',$x)!==false) {
 802				$ReadPrm = true;
 803				$PosX++;
 804			} else {
 805				$Pos++;
 806			}
 807
 808			if ($ReadPrm) {
 809				tbs_Locator_PrmRead($Txt,$PosX,false,'\'',$this->_ChrOpen,$this->_ChrClose,$Loc,$PosEnd);
 810				if ($PosEnd===false) {
 811					$this->meth_Misc_Alert('','can\'t found the end of the tag \''.substr($Txt,$Pos,$PosX-$Pos+10).'...\'.');
 812					$Pos++;
 813				}
 814			}
 815
 816		}
 817
 818	} while ($PosEnd===false);
 819
 820	$Loc->PosBeg = $Pos;
 821	$Loc->PosEnd = $PosEnd;
 822	if ($Loc->SubOk) {
 823		$Loc->FullName = $Name.'.'.$Loc->SubName;
 824		$Loc->SubLst = explode('.',$Loc->SubName);
 825		$Loc->SubNbr = count($Loc->SubLst);
 826	} else {
 827		$Loc->FullName = $Name;
 828	}
 829	if ($ReadPrm and isset($Loc->PrmLst['comm'])) {
 830		$Loc->PosBeg0 = $Loc->PosBeg;
 831		$Loc->PosEnd0 = $Loc->PosEnd;
 832		$comm = $Loc->PrmLst['comm'];
 833		if (($comm===true) or ($comm==='')) {
 834			$Loc->Enlarged = tbs_Locator_EnlargeToStr($Txt,$Loc,'<!--' ,'-->');
 835		} else {
 836			$Loc->Enlarged = tbs_Locator_EnlargeToTag($Txt,$Loc,$comm,false);
 837		}
 838	}
 839
 840	return $Loc;
 841
 842}
 843
 844// Search and cache TBS locators founded in $Txt.
 845function meth_Locator_SectionCache(&$LocR,$Bid) {
 846
 847	$LocR->BlockChk[$Bid] = false;
 848
 849	$LocLst =& $LocR->BlockLoc[$Bid];
 850	$Txt =& $LocR->BlockSrc[$Bid];
 851	$BlockName =& $LocR->BlockName[$Bid];
 852
 853	$Pos = 0;
 854	$PrevEnd = -1;
 855	$Nbr = 0;
 856	while ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$Pos,'.')) {
 857		if (($Loc->SubName==='#') or ($Loc->SubName==='$')) {
 858			$Loc->IsRecInfo = true;
 859			$Loc->RecInfo = $Loc->SubName;
 860			$Loc->SubName = '';
 861		} else {
 862			$Loc->IsRecInfo = false;
 863		}
 864		if ($Loc->PosBeg>$PrevEnd) {
 865			// The previous tag is not embeding => increment
 866			$Nbr++;
 867		} else {
 868			// The previous tag is embeding => no increment, then previous is over writed
 869			$LocR->BlockChk[$Bid] = true;
 870		}
 871		$PrevEnd = $Loc->PosEnd;
 872		if ($Loc->Enlarged) { // Parameter 'comm'
 873			$Pos = $Loc->PosBeg0+1;
 874			$Loc->Enlarged = false;
 875		} else {
 876			$Pos = $Loc->PosBeg+1;
 877		}
 878		$LocLst[$Nbr] = $Loc;
 879	}
 880
 881	$LocLst[0] = $Nbr;
 882
 883}
 884
 885function meth_Locator_Replace(&$Txt,&$Loc,&$Value,$SubStart) {
 886// This function enables to merge a locator with a text and returns the position just after the replaced block
 887// This position can be useful because we don't know in advance how $Value will be replaced.
 888
 889	// Found the value if there is a subname
 890	if (($SubStart!==false) and $Loc->SubOk) {
 891		for ($i=$SubStart;$i<$Loc->SubNbr;$i++) {
 892			$x = $Loc->SubLst[$i]; // &$Loc... brings an error with Event Example, I don't know why.
 893			if (is_array($Value)) {
 894				if (isset($Value[$x])) {
 895					$Value =& $Value[$x];
 896				} elseif (array_key_exists($x,$Value)) {// can happens when value is NULL
 897					$Value =& $Value[$x];
 898				} else {
 899					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'item \''.$x.'\' is not an existing key in the array.',true);
 900					unset($Value); $Value = ''; break;
 901				}
 902			} elseif (is_object($Value)) {
 903				$ArgLst = tbs_Misc_CheckArgLst($x);
 904				if (method_exists($Value,$x)) {
 905					$x = call_user_func_array(array(&$Value,$x),$ArgLst);
 906				} elseif (property_exists($Value,$x)) {
 907					$x =& $Value->$x;
 908				} else {
 909					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'item '.$x.'\' is neither a method nor a property in the class \''.get_class($Value).'\'.',true);
 910					unset($Value); $Value = ''; break;
 911				}
 912				$Value =& $x; unset($x); $x = '';
 913			} else {
 914				if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'item before \''.$x.'\' is neither an object nor an array. Its type is '.gettype($Value).'.',true);
 915				unset($Value); $Value = ''; break;
 916			}
 917		}
 918	}
 919
 920	$CurrVal = $Value; // Unlink
 921
 922	if (isset($Loc->PrmLst['onformat'])) {
 923		if ($Loc->FirstMerge) {
 924			$Loc->OnFrmInfo = $Loc->PrmLst['onformat'];
 925			$Loc->OnFrmArg = array(&$Loc->FullName,'',&$Loc->PrmLst,&$this);
 926			$ErrMsg = false;
 927			if (!$this->meth_Misc_UserFctCheck($Loc->OnFrmInfo,$ErrMsg,false)) {
 928				unset($Loc->PrmLst['onformat']);
 929				if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'(parameter onformat) '.$ErrMsg);
 930				$Loc->OnFrmInfo = 'pi'; // Execute the function pi() just to avoid extra error messages 
 931			}
 932		}
 933		$Loc->OnFrmArg[1] =& $CurrVal;
 934		if (isset($Loc->PrmLst['subtpl'])) {
 935			$this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
 936			call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
 937			$this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
 938			$Loc->ConvProtect = false;
 939			$Loc->ConvHtml = false;
 940		} else {
 941			call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
 942		}
 943	}
 944
 945	if ($Loc->FirstMerge) {
 946		if (isset($Loc->PrmLst['frm'])) {
 947			$Loc->ConvMode = 0; // Frm
 948			$Loc->ConvProtect = false;
 949		} else {
 950			// Analyze parameter 'htmlconv'
 951			if (isset($Loc->PrmLst['htmlconv'])) {
 952				$x = strtolower($Loc->PrmLst['htmlconv']);
 953				$x = '+'.str_replace(' ','',$x).'+';
 954				if (strpos($x,'+esc+')!==false)  {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvEsc = true; }
 955				if (strpos($x,'+wsp+')!==false)  {tbs_Misc_ConvSpe($Loc); $Loc->ConvWS = true; }
 956				if (strpos($x,'+js+')!==false)   {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvJS = true; }
 957				if (strpos($x,'+no+')!==false)   $Loc->ConvHtml = false;
 958				if (strpos($x,'+yes+')!==false)  $Loc->ConvHtml = true;
 959				if (strpos($x,'+nobr+')!==false) {$Loc->ConvHtml = true; $Loc->ConvBr = false; }
 960			} else {
 961				if ($this->HtmlCharSet===false) $Loc->ConvHtml = false; // No HTML
 962			}
 963			// Analyze parameter 'protect'
 964			if (isset($Loc->PrmLst['protect'])) {
 965				$x = strtolower($Loc->PrmLst['protect']);
 966				if ($x==='no') {
 967					$Loc->ConvProtect = false;
 968				} elseif ($x==='yes') {
 969					$Loc->ConvProtect = true;
 970				}
 971			} elseif ($this->Protect===false) {
 972				$Loc->ConvProtect = false;
 973			}
 974		}
 975		if ($Loc->Ope = isset($Loc->PrmLst['ope'])) {
 976			$ope = $Loc->PrmLst['ope'];
 977			if ($ope==='list') {
 978				$Loc->OpeId = 1;
 979				$Loc->OpeSep = (isset($Loc->PrmLst['valsep'])) ? $Loc->PrmLst['valsep'] : ',';
 980			} else {
 981				$x = substr($ope,0,4);
 982				if ($x==='max:') {
 983					$Loc->OpeId = (isset($Loc->PrmLst['maxhtml'])) ? 2 : 3;
 984					$Loc->OpeN = intval(trim(substr($ope,4)));
 985					$Loc->OpeEnd = (isset($Loc->PrmLst['maxend'])) ? $Loc->PrmLst['maxend'] : '...';
 986					if ($Loc->OpeN<=0) $Loc->Ope = false;
 987				} elseif ($x==='mod:') {
 988					$Loc->OpeId = 4; $Loc->OpeN = intval(trim(substr($ope,4)));
 989				} elseif ($x==='add:') {
 990					$Loc->OpeId = 5; $Loc->OpeN = intval(trim(substr($ope,4)));
 991				} elseif (isset($this->_piOnOperation)) {
 992					$Loc->OpeId = 0;
 993					$Loc->OpeArg = array(&$Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$Txt,$Loc->PosBeg,$Loc->PosEnd,&$Loc);
 994				} else {
 995					$Loc->Ope = false;
 996					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'parameter ope doesn\'t support value \''.$ope.'\'.',true);
 997				}
 998			}
 999		}
1000		$Loc->FirstMerge = false;
1001	}
1002	$ConvProtect = $Loc->ConvProtect;
1003
1004	// Plug-in OnFormat
1005	if ($this->_piOnFrm_Ok) {
1006		if (isset($Loc->OnFrmArgPi)) {
1007			$Loc->OnFrmArgPi[1] =& $CurrVal;
1008		} else {
1009			$Loc->OnFrmArgPi = array(&$Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$this);
1010		}
1011		$this->meth_PlugIn_RunAll($this->_piOnFormat,$Loc->OnFrmArgPi);
1012	}
1013
1014	// Operation
1015	if ($Loc->Ope) {
1016		switch ($Loc->OpeId) {
1017		case 0: 
1018			$Loc->OpeArg[1] =& $CurrVal; $Loc->OpeArg[3] =& $Txt;
1019			if (!$this->meth_PlugIn_RunAll($this->_piOnOperation,$Loc->OpeArg)) return $Loc->PosBeg;
1020			break;
1021		case 1: if (is_array($CurrVal)) $CurrVal = implode($Loc->OpeSep,$CurrVal); break;
1022		case 2: if (strlen(''.$CurrVal)>$Loc->OpeN) tbs_Html_Max($CurrVal,$Loc->OpeN,$Loc->OpeEnd); break;
1023		case 3: if (strlen(''.$CurrVal)>$Loc->OpeN) $CurrVal = substr(''.$CurrVal,0,$Loc->OpeN).$Loc->OpeEnd; break;
1024		case 4: $CurrVal = intval($CurrVal) % $Loc->OpeN; break;
1025		case 5: $CurrVal = intval($CurrVal) + $Loc->OpeN; break;
1026		}
1027	}
1028
1029	// HTML conversion or format
1030	if ($Loc->ConvMode===1) { // Html simple
1031		if (!is_string($CurrVal)) $CurrVal = @strval($CurrVal);
1032		if ($Loc->ConvHtml) {
1033			$this->meth_Conv_Html($CurrVal);
1034			if ($Loc->ConvBr) $CurrVal = nl2br($CurrVal);
1035		}
1036	} elseif ($Loc->ConvMode===0) { // Format
1037		$CurrVal = $this->meth_Misc_Format($CurrVal,$Loc->PrmLst);
1038	} elseif ($Loc->ConvMode===2) { // Html special
1039		if (!is_string($CurrVal)) $CurrVal = @strval($CurrVal);
1040		if ($Loc->ConvHtml) {
1041			$this->meth_Conv_Html($CurrVal);
1042			if ($Loc->ConvBr) $CurrVal = nl2br($CurrVal);
1043		}
1044		if ($Loc->ConvEsc) $CurrVal = str_replace('\'','\'\'',$CurrVal);
1045		if ($Loc->ConvWS) {
1046			$check = '  ';
1047			$nbsp = '&nbsp;';
1048			do {
1049				$pos = strpos($CurrVal,$check);
1050				if ($pos!==false) $CurrVal = substr_replace($CurrVal,$nbsp,$pos,1);
1051			} while ($pos!==false);
1052		}
1053		if ($Loc->ConvJS) {
1054			$CurrVal = addslashes($CurrVal); // apply to ('), ("), (\) and (null)
1055			$CurrVal = str_replace("\n",'\n',$CurrVal);
1056			$CurrVal = str_replace("\r",'\r',$CurrVal);
1057			$CurrVal = str_replace("\t",'\t',$CurrVal);
1058		}
1059	}
1060
1061	// if/then/else process, there may be several if/then
1062	if ($Loc->PrmIfNbr) {
1063		$z = false;
1064		$i = 1;
1065		while ($i!==false) {
1066			if ($Loc->PrmIfVar[$i]) $Loc->PrmIfVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmIf[$i],true);
1067			$x = str_replace($this->_ChrVal,$CurrVal,$Loc->PrmIf[$i]);
1068			if (tbs_Misc_CheckCondition($x)) {
1069				if (isset($Loc->PrmThen[$i])) {
1070					if ($Loc->PrmThenVar[$i]) $Loc->PrmThenVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmThen[$i],true);
1071					$z = $Loc->PrmThen[$i];
1072				}
1073				$i = false;
1074			} else {
1075				$i++;
1076				if ($i>$Loc->PrmIfNbr) {
1077					if (isset($Loc->PrmLst['else'])) {
1078						if ($Loc->PrmElseVar) $Loc->PrmElseVar = $this->meth_Merge_AutoVar($Loc->PrmLst['else'],true);
1079						$z =$Loc->PrmLst['else'];
1080					}
1081					$i = false;
1082				}
1083			}
1084		}
1085		if ($z!==false) {
1086			if ($ConvProtect) {
1087				$CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
1088				$ConvProtect = false;
1089			}
1090			$CurrVal = str_replace($this->_ChrVal,$CurrVal,$z);
1091		}
1092	}
1093
1094	if (isset($Loc->PrmLst['file'])) {
1095		$x = $Loc->PrmLst['file'];
1096		if ($x===true) $x = $CurrVal;
1097		$this->meth_Merge_AutoVar($x,false);
1098		$x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
1099		$CurrVal = '';
1100		if ($x!=='') {
1101			if (tbs_Misc_GetFile($CurrVal,$x)) {
1102				if (isset($Loc->PrmLst['getbody'])) $CurrVal = tbs_Html_GetPart($CurrVal,$Loc->PrmLst['getbody'],false,true);
1103			} else {
1104				if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter file is not found or not readable.',true);
1105			}
1106			$ConvProtect = false;
1107		}
1108	}
1109
1110	if (isset($Loc->PrmLst['script'])) {// Include external PHP script
1111		$x = $Loc->PrmLst['script'];
1112		if ($x===true) $x = $CurrVal;
1113		$this->meth_Merge_AutoVar($x,false);
1114		$x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
1115		if ($x!=='') {
1116			$this->_Subscript = $x;
1117			$this->CurrPrm =& $Loc->PrmLst;
1118			$sub = isset($Loc->PrmLst['subtpl']);
1119			if ($sub) $this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
1120			if ($this->meth_Misc_RunSubscript($CurrVal,$Loc->PrmLst)===false) {
1121				if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter script is not found or not readable.',true);
1122			}
1123			if ($sub) $this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
1124			if (isset($Loc->PrmLst['getbody'])) $CurrVal = tbs_Html_GetPart($CurrVal,$Loc->PrmLst['getbody'],false,true);
1125			unset($this->CurrPrm);
1126			$ConvProtect = false;
1127		}
1128	}
1129
1130	// Case when it's an empty string
1131	if ($CurrVal==='') {
1132
1133		if ($Loc->MagnetId===false) {
1134			if (isset($Loc->PrmLst['.'])) {
1135				$Loc->MagnetId = -1;
1136			} elseif (isset($Loc->PrmLst['ifempty'])) {
1137				$Loc->MagnetId = -2;
1138			} elseif (isset($Loc->PrmLst['magnet'])) {
1139				$Loc->MagnetId = 1;
1140				$Loc->PosBeg0 = $Loc->PosBeg;
1141				$Loc->PosEnd0 = $Loc->PosEnd;
1142				if (isset($Loc->PrmLst['mtype'])) {
1143					switch ($Loc->PrmLst['mtype']) {
1144					case 'm+m': $Loc->MagnetId = 2; break;
1145					case 'm*': $Loc->MagnetId = 3; break;
1146					case '*m': $Loc->MagnetId = 4; break;
1147					}
1148				}
1149			} else {
1150				$Loc->MagnetId = 0;
1151			}
1152		}
1153
1154		switch ($Loc->MagnetId) {
1155		case 0: break;
1156		case -1: $CurrVal = '&nbsp;'; break; // Enables to avoid blanks in HTML tables
1157		case -2: $CurrVal = $Loc->PrmLst['ifempty']; break;
1158		case 1:
1159			$Loc->Enlarged = true;
1160			tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],false);
1161			break;
1162		case 2:
1163			$Loc->Enlarged = true;
1164			$CurrVal = tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],true);
1165			break;
1166		case 3:
1167			$Loc->Enlarged = true;
1168			$Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,false,1,false);
1169			if ($Loc2!==false) {
1170				$Loc->PosBeg = $Loc2->PosBeg;
1171				if ($Loc->PosEnd<$Loc2->PosEnd) $Loc->PosEnd = $Loc2->PosEnd;
1172			}
1173			break;
1174		case 4:
1175			$Loc->Enlarged = true;
1176			$Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,true,1,false);
1177			if ($Loc2!==false) $Loc->PosEnd = $Loc2->PosEnd;
1178			break;
1179		}
1180		$NewEnd = $Loc->PosBeg; // Useful when mtype='m+m'
1181	} else {
1182
1183		if ($ConvProtect) $CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
1184		$NewEnd = $Loc->PosBeg + strlen($CurrVal);
1185
1186	}
1187
1188	$Txt = substr_replace($Txt,$CurrVal,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
1189	return $NewEnd; // Return the new end position of the field
1190
1191}
1192
1193function meth_Locator_FindBlockNext(&$Txt,$BlockName,$PosBeg,$ChrSub,$Mode,&$P1,&$FieldBefore) {
1194// Return the first block locator just after the PosBeg position
1195// Mode = 1 : Merge_Auto => doesn't save $Loc->BlockSrc, save the bounds of TBS Def tags instead, return also fields
1196// Mode = 2 : FindBlockLst or GetBlockSource => save $Loc->BlockSrc without TBS Def tags
1197// Mode = 3 : GetBlockSource => save $Loc->BlockSrc with TBS Def tags
1198
1199	$SearchDef = true;
1200	$FirstField = false;
1201
1202	// Search for the first tag with parameter "block"
1203	while ($SearchDef and ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub))) {
1204		if (isset($Loc->PrmLst['block'])) {
1205			if ($P1) {
1206				if (isset($Loc->PrmLst['p1'])) return false;
1207			} else {
1208				if (isset($Loc->PrmLst['p1'])) $P1 = true;
1209			}
1210			$Block = $Loc->PrmLst['block'];
1211			$SearchDef = false;
1212		} elseif ($Mode===1) {
1213			return $Loc;
1214		} elseif ($FirstField===false) {
1215			$FirstField = $Loc;
1216		}
1217		$PosBeg = $Loc->PosEnd;
1218	}
1219
1220	if ($SearchDef) {
1221		if ($FirstField!==false) $FieldBefore = true;
1222		return false;
1223	}
1224
1225	if ($Block==='begin') { // Block definied using begin/end
1226
1227		if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
1228
1229		$Opened = 1;
1230		while ($Loc2 = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub)) {
1231			if (isset($Loc2->PrmLst['block'])) {
1232				switch ($Loc2->PrmLst['block']) {
1233				case 'end':   $Opened--; break;
1234				case 'begin': $Opened++; break;
1235				}
1236				if ($Opened==0) {
1237					if ($Mode===1) {
1238						$Loc->PosBeg2 = $Loc2->PosBeg;
1239						$Loc->PosEnd2 = $Loc2->PosEnd;
1240					} else {
1241						if ($Mode===2) {
1242							$Loc->BlockSrc = substr($Txt,$Loc->PosEnd+1,$Loc2->PosBeg-$Loc->PosEnd-1);
1243						} else {
1244							$Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc2->PosEnd-$Loc->PosBeg+1);
1245						}
1246						$Loc->PosEnd = $Loc2->PosEnd;
1247						$Loc->PosDef = 0;
1248					}
1249					$Loc->BlockFound = true;
1250					return $Loc;
1251				}
1252			}
1253			$PosBeg = $Loc2->PosEnd;
1254		}
1255
1256		return $this->meth_Misc_Alert($Loc,'a least one tag with parameter \'block=end\' is missing.',false,'in block\'s definition');
1257
1258	}
1259
1260	if ($Mode===1) {
1261		$Loc->PosBeg2 = false;
1262	} else {
1263
1264		$Loc->PosDef = $Loc->PosBeg;
1265		if (!$Loc->SubOk) {
1266			$PosBeg1 = $Loc->PosBeg;
1267			$PosEnd1 = $Loc->PosEnd;
1268		}
1269		if (tbs_Locator_EnlargeToTag($Txt,$Loc,$Block,false)===false) return $this->meth_Misc_Alert($Loc,'tag <'.$Loc->PrmLst['block'].'> or </'.$Loc->PrmLst['block'].'> is not found.',false,'in block\'s definition');
1270		$Loc->PosDef = $Loc->PosDef - $Loc->PosBeg;
1271		if ($Loc->SubOk or ($Mode===3)) {
1272			$Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
1273			$Loc->PosDef++;
1274		} else {
1275			$Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$PosBeg1-$Loc->PosBeg).substr($Txt,$PosEnd1+1,$Loc->PosEnd-$PosEnd1);
1276		}
1277	}
1278
1279	$Loc->BlockFound = true;
1280	if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
1281	return $Loc; // methods return by ref by default
1282
1283}
1284
1285function meth_Locator_FindBlockLst(&$Txt,$BlockName,$Pos,$SpePrm) {
1286// Return a locator object covering all block definitions, even if there is no block definition found.
1287
1288	$LocR = new clsTbsLocator;
1289	$LocR->P1 = false;
1290	$LocR->FieldOutside = false;
1291	$LocR->BlockNbr = 0; // Any section (normal, nodata, when, serial, condition, grp, ...)
1292	$LocR->BlockSrc = array();  // 1 to BlockNbr
1293	$LocR->BlockLoc = array();  // idem
1294	$LocR->BlockChk = array();  // idem
1295	$LocR->BlockName = array(); // idem
1296	$LocR->BlockPrm = array();  // idem. Provided only for plug-ins.
1297	$LocR->NoDataBid = false;
1298	$LocR->SpecialBid = false;
1299	$LocR->HeaderFound = false;
1300	$LocR->FooterFound = false;
1301	$LocR->WhenFound = false;
1302	$LocR->WhenDefaultBid = false;
1303	$LocR->SectionNbr = 0; // Normal section
1304	$LocR->SectionBid = array();       // 1 to SectionNbr
1305	$LocR->SectionIsSerial = array();  // idem
1306	$LocR->SectionSerialBid = array(); // idem
1307	$LocR->SectionSerialOrd = array(); // idem
1308	$LocR->SerialEmpty = false;
1309
1310	$Bid =& $LocR->BlockNbr;
1311	$Sid =& $LocR->SectionNbr;
1312	$Pid = 0;
1313
1314	do {
1315
1316		if ($BlockName==='') {
1317			$Loc = false;
1318		} else {
1319			$Loc = $this->meth_Locator_FindBlockNext($Txt,$BlockName,$Pos,'.',2,$LocR->P1,$LocR->FieldOutside);
1320		}
1321
1322		if ($Loc===false) {
1323
1324			if ($Pid>0) { // parentgrp mode => disconnect $Txt from the source
1325				$Src = $Txt;
1326				$Txt =& $Parent[$Pid]['txt'];
1327				if ($LocR->BlockFound) {
1328					// Redefine the Header block
1329					$i = $Parent[$Pid]['bid'];
1330					$LocR->BlockSrc[$i] = substr($Src,0,$LocR->PosBeg);
1331					// Add a Footer block
1332					tbs_Locator_SectionAddBlk($LocR,$BlockName,substr($Src,$LocR->PosEnd+1),$LocR->BlockPrm[$i]);
1333					tbs_Locator_SectionAddGrp($LocR,$Bid,'F',$Parent[$Pid]['fld']);
1334				}
1335				// Now gowing down to previous level
1336				$Pos = $Parent[$Pid]['pos'];
1337				$LocR->PosBeg = $Parent[$Pid]['beg'];
1338				$LocR->PosEnd = $Parent[$Pid]['end'];
1339				$LocR->BlockFound = true;
1340				unset($Parent[$Pid]);
1341				$Pid--;
1342				$Loc = true;
1343			}
1344
1345		} else {
1346
1347			$Pos = $Loc->PosEnd;
1348
1349			// Define the block limits
1350			if ($LocR->BlockFound) {
1351				if ( $LocR->PosBeg > $Loc->PosBeg ) $LocR->PosBeg = $Loc->PosBeg;
1352				if ( $LocR->PosEnd < $Loc->PosEnd ) $LocR->PosEnd = $Loc->PosEnd;
1353			} else {
1354				$LocR->BlockFound = true;
1355				$LocR->PosBeg = $Loc->PosBeg;
1356				$LocR->PosEnd = $Loc->PosEnd;
1357			}
1358
1359			// Merge block parameters
1360			if (count($Loc->PrmLst)>0) $LocR->PrmLst = array_merge($LocR->PrmLst,$Loc->PrmLst);
1361
1362			// Save the block and cache its tags (incrments $LocR->BlockNbr)
1363			tbs_Locator_SectionAddBlk($LocR,$BlockName,$Loc->BlockSrc,$Loc->PrmLst);
1364
1365			// Add the text in the list of blocks
1366			if (isset($Loc->PrmLst['nodata'])) { // Nodata section
1367				$LocR->NoDataBid = $Bid;
1368			} elseif (($SpePrm!==false) and isset($Loc->PrmLst[$SpePrm])) { // Special section (used for navigation bar)
1369				$LocR->SpecialBid = $Bid;
1370			} elseif (isset($Loc->PrmLst['when'])) {
1371				if ($LocR->WhenFound===false) {
1372					$LocR->WhenFound = true;
1373					$LocR->WhenSeveral = false;
1374					$LocR->WhenNbr = 0;
1375					$LocR->WhenSectionBid[] = array(); // Bid of the section to display
1376					$LocR->WhenCondBid[] = array();    // Bid of the condition to check
1377					$LocR->WhenBeforeNS[] = array();   // True if the When section must be displayed before a 
1378				}
1379				$LocR->WhenNbr++;
1380				if (isset($Loc->PrmLst['several'])) $LocR->WhenSeveral = true;
1381				$LocR->WhenSectionBid[$LocR->WhenNbr] = $Bid;
1382				$this->meth_Merge_AutoVar($Loc->PrmLst['when'],false);
1383				tbs_Locator_SectionAddBlk($LocR,$BlockName,$Loc->PrmLst['when'],array());
1384				$LocR->WhenCondBid[$LocR->WhenNbr] = $Bid;
1385				$LocR->WhenBeforeNS[$LocR->WhenNbr] = ($Sid===0);
1386			} elseif (isset($Loc->PrmLst['default'])) {
1387				$LocR->WhenDefaultBid = $Bid;
1388				$LocR->WhenDefaultBeforeNS = ($Sid===0);
1389			} elseif (isset($Loc->PrmLst['headergrp'])) {
1390				tbs_Locator_SectionAddGrp($LocR,$Bid,'H',$Loc->PrmLst['headergrp']);
1391			} elseif (isset($Loc->PrmLst['footergrp'])) {
1392				tbs_Locator_SectionAddGrp($LocR,$Bid,'F',$Loc->PrmLst['footergrp']);
1393			} elseif (isset($Loc->PrmLst['splittergrp'])) {
1394				tbs_Locator_SectionAddGrp($LocR,$Bid,'S',$Loc->PrmLst['splittergrp']);
1395			} elseif (isset($Loc->PrmLst['parentgrp'])) {
1396				tbs_Locator_SectionAddGrp($LocR,$Bid,'H',$Loc->PrmLst['parentgrp']);
1397				$Pid++;
1398				$Parent[$Pid]['bid'] = $Bid;
1399				$Parent[$Pid]['fld'] = $Loc->PrmLst['parentgrp'];
1400				$Parent[$Pid]['txt'] =& $Txt;
1401				$Parent[$Pid]['pos'] = $Pos;
1402				$Parent[$Pid]['beg'] = $LocR->PosBeg;
1403				$Parent[$Pid]['end'] = $LocR->PosEnd;
1404				$Txt =& $LocR->BlockSrc[$Bid];
1405				$Pos = $Loc->PosDef + 1;
1406				$LocR->BlockFound = false;
1407				$LocR->PosBeg = false;
1408				$LocR->PosEnd = false;
1409			} elseif (isset($Loc->PrmLst['serial'])) {
1410				// Section	with Serial Sub-Sections
1411				$Src =& $LocR->BlockSrc[$Bid];
1412				$Loc0 = false;
1413				if ($LocR->SerialEmpty===false) {
1414					$NameSr = $BlockName.'_0';
1415					$x = false;
1416					$LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
1417					if ($LocSr!==false) {
1418						$LocR->SerialEmpty = $LocSr->BlockSrc;
1419						$Src = substr_replace($Src,'',$LocSr->PosBeg,$LocSr->PosEnd-$LocSr->PosBeg+1);
1420					}
1421				}
1422				$NameSr = $BlockName.'_1';
1423				$x = false;
1424				$LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
1425				if ($LocSr!==false) {
1426					$Sid++;
1427					$LocR->SectionBid[$Sid] = $Bid;
1428					$LocR->SectionIsSerial[$Sid] = true;
1429					$LocR->SectionSerialBid[$Sid] = array();
1430					$LocR->SectionSerialOrd[$Sid] = array();
1431					$SrBid =& $LocR->SectionSerialBid[$Sid];
1432					$SrOrd =& $LocR->SectionSerialOrd[$Sid];
1433					$BidParent = $Bid;
1434					$SrNum = 1;
1435					do {
1436						// Save previous sub-section
1437						$LocR->BlockLoc[$BidParent][$SrNum] = $LocSr;
1438						tbs_Locator_SectionAddBlk($LocR,$NameSr,$LocSr->BlockSrc,$LocSr->PrmLst);
1439						$SrBid[$SrNum] = $Bid;
1440						$SrOrd[$SrNum] = $SrNum;
1441						$i = $SrNum;
1442						while (($i>1) and ($LocSr->PosBeg<$LocR->BlockLoc[$BidParent][$SrOrd[$i-1]]->PosBeg)) {
1443							$SrOrd[$i] = $SrOrd[$i-1];
1444							$SrOrd[$i-1] = $SrNum;
1445							$i--;
1446						}
1447						// Search next section
1448						$SrNum++;
1449						$NameSr = $BlockName.'_'.$SrNum;
1450						$x = false;
1451						$LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
1452					} while ($LocSr!==false);
1453					$SrBid[0] = $SrNum-1;
1454				}
1455			} else {
1456				// Normal section
1457				$Sid++;
1458				$LocR->SectionBid[$Sid] = $Bid;
1459				$LocR->SectionIsSerial[$Sid] = false;
1460			}
1461
1462		}
1463
1464	} while ($Loc!==false);
1465
1466	if ($LocR->WhenFound and ($Sid===0)) {
1467		// Add a blank section if When is used without a normal section
1468		tbs_Locator_SectionAddBlk($LocR,$BlockName,'',array());
1469		$Sid++;
1470		$LocR->SectionBid[$Sid] = $Bid;
1471		$LocR->SectionIsSerial[$Sid] = false;
1472	}
1473
1474	// Calculate Cache
1475	if ($this->TurboBlock) {
1476		for ($i=1;$i<=$LocR->BlockNbr;$i++) {
1477			$this->meth_Locator_SectionCache($LocR,$i);
1478		}
1479	}
1480
1481	return $LocR; // methods return by ref by default
1482
1483}
1484
1485function meth_Merge_Block(&$Txt,&$BlockLst,&$SrcId,&$Query,$SpePrm,$SpeRecNum) {
1486
1487	$BlockSave = $this->_CurrBlock;
1488	$this->_CurrBlock = $BlockLst;
1489
1490	// Get source type and info
1491	$Src = new clsTbsDataSource;
1492	if (!$Src->DataPrepare($SrcId,$this)) {
1493		$this->_CurrBlock = $BlockSave;
1494		return 0;
1495	}
1496
1497	$BlockLst = explode(',',$BlockLst);
1498	$BlockNbr = count($BlockLst);
1499	$BlockId = 0;
1500	$WasP1 = false;
1501	$NbrRecTot = 0;
1502	$QueryZ =& $Query;
1503	$ReturnData = false;
1504
1505	while ($BlockId<$BlockNbr) {
1506
1507		$RecSpe = 0;  // Row with a special block's definition (used for the navigation bar)
1508		$QueryOk = true;
1509		$this->_CurrBlock = trim($BlockLst[$BlockId]);
1510		if ($this->_CurrBlock==='*') {
1511			$ReturnData = true;
1512			if ($Src->RecSaved===false) $Src->RecSaving = true;
1513			$this->_CurrBlock = '';
1514		}
1515
1516		// Search the block
1517		$LocR = $this->meth_Locator_FindBlockLst($Txt,$this->_CurrBlock,0,$SpePrm);
1518
1519		if ($WasP1) $this->meth_Merge_FieldOutside($Txt,$Src,$LocR);
1520
1521		if ($LocR->BlockFound)

Large files files are truncated, but you can click here to view the full file