/includes/template/tbs_class.php
PHP | 3727 lines | 3173 code | 366 blank | 188 comment | 882 complexity | 41c6a89de40054813191ac103a451e25 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /*
- ********************************************************
- TinyButStrong - Template Engine for Pro and Beginners
- ------------------------
- Version : 3.5.3 for PHP 4
- Date : 2010-04-12
- Web site : http://www.tinybutstrong.com
- Author : http://www.tinybutstrong.com/onlyyou.html
- ********************************************************
- This library is free software.
- You can redistribute and modify it even for commercial usage,
- but you must accept and respect the LPGL License version 3.
- */
- // Check PHP version
- if (version_compare(PHP_VERSION,'4.0.6')<0) echo '<br><b>TinyButStrong Error</b> (PHP Version Check) : Your PHP version is '.PHP_VERSION.' while TinyButStrong needs PHP version 4.0.6 or higher.';
- if (!is_callable('array_key_exists')) {
- function array_key_exists (&$key,&$array) {return key_exists($key,$array);}
- }
- if (!is_callable('property_exists')) {
- function property_exists(&$obj,$prop) {return true;}
- }
- // Render flags
- define('TBS_NOTHING', 0);
- define('TBS_OUTPUT', 1);
- define('TBS_EXIT', 2);
- // Plug-ins actions
- define('TBS_INSTALL', -1);
- define('TBS_ISINSTALLED', -3);
- // *********************************************
- class clsTbsLocator {
- var $PosBeg = false;
- var $PosEnd = false;
- var $Enlarged = false;
- var $FullName = false;
- var $SubName = '';
- var $SubOk = false;
- var $SubLst = array();
- var $SubNbr = 0;
- var $PrmLst = array();
- var $PrmIfNbr = false;
- var $MagnetId = false;
- var $BlockFound = false;
- var $FirstMerge = true;
- var $ConvProtect = true;
- var $ConvStr = true;
- var $ConvMode = 1; // Normal
- var $ConvBr = true;
- }
- // *********************************************
- class clsTbsDataSource {
- var $Type = false;
- var $SubType = 0;
- var $SrcId = false;
- var $Query = '';
- var $RecSet = false;
- var $RecKey = '';
- var $RecNum = 0;
- var $RecNumInit = 0;
- var $RecSaving = false;
- var $RecSaved = false;
- var $RecBuffer = false;
- var $CurrRec = false;
- var $TBS = false;
- var $OnDataOk = false;
- var $OnDataPrm = false;
- var $OnDataPrmDone = array();
- var $OnDataPi = false;
- function DataAlert($Msg) {
- return $this->TBS->meth_Misc_Alert('when merging block '.$this->TBS->_ChrOpen.$this->TBS->_CurrBlock.$this->TBS->_ChrClose,$Msg);
- }
- function DataPrepare(&$SrcId,&$TBS) {
- $this->SrcId = &$SrcId;
- $this->TBS = &$TBS;
- $FctInfo = false;
- $FctObj = false;
- if (is_array($SrcId)) {
- $this->Type = 0;
- } elseif (is_resource($SrcId)) {
- $Key = get_resource_type($SrcId);
- switch ($Key) {
- case 'mysql link' : $this->Type = 6; break;
- case 'mysql link persistent' : $this->Type = 6; break;
- case 'mysql result' : $this->Type = 6; $this->SubType = 1; break;
- case 'pgsql link' : $this->Type = 7; break;
- case 'pgsql link persistent' : $this->Type = 7; break;
- case 'pgsql result' : $this->Type = 7; $this->SubType = 1; break;
- case 'sqlite database' : $this->Type = 8; break;
- case 'sqlite database (persistent)' : $this->Type = 8; break;
- case 'sqlite result' : $this->Type = 8; $this->SubType = 1; break;
- default :
- $FctInfo = $Key;
- $FctCat = 'r';
- }
- } elseif (is_string($SrcId)) {
- switch (strtolower($SrcId)) {
- case 'array' : $this->Type = 0; $this->SubType = 1; break;
- case 'clear' : $this->Type = 0; $this->SubType = 3; break;
- case 'mysql' : $this->Type = 6; $this->SubType = 2; break;
- case 'text' : $this->Type = 2; break;
- case 'num' : $this->Type = 1; break;
- default :
- $FctInfo = $SrcId;
- $FctCat = 'k';
- }
- } elseif (is_a($SrcId,'Iterator')) {
- $this->Type = 9; $this->SubType = 1;
- } elseif (is_a($SrcId,'ArrayObject')) {
- $this->Type = 9; $this->SubType = 2;
- } elseif (is_a($SrcId,'IteratorAggregate')) {
- $this->Type = 9; $this->SubType = 3;
- } elseif (is_object($SrcId)) {
- $FctInfo = get_class($SrcId);
- $FctCat = 'o';
- $FctObj = &$SrcId;
- $this->SrcId = &$SrcId;
- } elseif ($SrcId===false) {
- $this->DataAlert('the specified source is set to FALSE. Maybe your connection has failed.');
- } else {
- $this->DataAlert('unsupported variable type : \''.gettype($SrcId).'\'.');
- }
- if ($FctInfo!==false) {
- $ErrMsg = false;
- if ($TBS->meth_Misc_UserFctCheck($FctInfo,$FctCat,$FctObj,$ErrMsg)) {
- $this->Type = $FctInfo['type'];
- if ($this->Type!==5) {
- if ($this->Type===4) {
- $this->FctPrm = array(false,0);
- $this->SrcId = &$FctInfo['open'][0];
- }
- $this->FctOpen = &$FctInfo['open'];
- $this->FctFetch = &$FctInfo['fetch'];
- $this->FctClose = &$FctInfo['close'];
- }
- } else {
- $this->Type = $this->DataAlert($ErrMsg);
- }
- }
- return ($this->Type!==false);
- }
- function DataOpen(&$Query) {
- // Init values
- unset($this->CurrRec); $this->CurrRec = true;
- if ($this->RecSaved) {
- $this->FirstRec = true;
- unset($this->RecKey); $this->RecKey = '';
- $this->RecNum = $this->RecNumInit;
- if ($this->OnDataOk) $this->OnDataArgs[1] = &$this->CurrRec;
- return true;
- }
- unset($this->RecSet); $this->RecSet = false;
- $this->RecNumInit = 0;
- $this->RecNum = 0;
- if (isset($this->TBS->_piOnData)) {
- $this->OnDataPi = true;
- $this->OnDataPiRef = &$this->TBS->_piOnData;
- $this->OnDataOk = true;
- }
- if ($this->OnDataOk) {
- $this->OnDataArgs = array();
- $this->OnDataArgs[0] = &$this->TBS->_CurrBlock;
- $this->OnDataArgs[1] = &$this->CurrRec;
- $this->OnDataArgs[2] = &$this->RecNum;
- $this->OnDataArgs[3] = &$this->TBS;
- }
- switch ($this->Type) {
- case 0: // Array
- if (($this->SubType===1) and (is_string($Query))) $this->SubType = 2;
- if ($this->SubType===0) {
- if (PHP_VERSION==='4.4.1') {$this->RecSet = $this->SrcId;} else {$this->RecSet = &$this->SrcId;} // bad bug in PHP 4.4.1
- } elseif ($this->SubType===1) {
- if (is_array($Query)) {
- if (PHP_VERSION==='4.4.1') {$this->RecSet = $Query;} else {$this->RecSet = &$Query;}
- } else {
- $this->DataAlert('type \''.gettype($Query).'\' not supported for the Query Parameter going with \'array\' Source Type.');
- }
- } elseif ($this->SubType===2) {
- // TBS query string for array and objects, syntax: "var[item1][item2]->item3[item4]..."
- $x = trim($Query);
- $z = chr(0);
- $x = str_replace(array(']->','][','->','['),$z,$x);
- if (substr($x,strlen($x)-1,1)===']') $x = substr($x,0,strlen($x)-1);
- $ItemLst = explode($z,$x);
- $ItemNbr = count($ItemLst);
- $Item0 = &$ItemLst[0];
- // Check first item
- if ($Item0[0]==='~') {
- $Item0 = substr($Item0,1);
- if ($this->TBS->ObjectRef!==false) {
- $Var = &$this->TBS->ObjectRef;
- $i = 0;
- } else {
- $i = $this->DataAlert('invalid query \''.$Query.'\' because property ObjectRef is not set.');
- }
- } else {
- if (isset($GLOBALS[$Item0])) {
- if ((PHP_VERSION==='4.4.1') and is_array($GLOBALS[$Item0])) {$Var = $GLOBALS[$Item0];} else {$Var = &$GLOBALS[$Item0];}
- $i = 1;
- } else {
- $i = $this->DataAlert('invalid query \''.$Query.'\' because global variable \''.$Item0.'\' is not found.');
- }
- }
- // Check sub-items
- $Empty = false;
- while (($i!==false) and ($i<$ItemNbr) and ($Empty===false)) {
- $x = $ItemLst[$i];
- if (is_array($Var)) {
- if (isset($Var[$x])) {
- $Var = &$Var[$x];
- } else {
- $Empty = true;
- }
- } elseif (is_object($Var)) {
- $ArgLst = $this->TBS->f_Misc_CheckArgLst($x);
- if (method_exists($Var,$x)) {
- $f = array(&$Var,$x); unset($Var);
- $Var = call_user_func_array($f,$ArgLst);
- } elseif (property_exists(get_class($Var),$x)) {
- if (isset($Var->$x)) $Var = &$Var->$x;
- } elseif (isset($Var->$x)) {
- $Var = $Var->$x; // useful for overloaded property
- } else {
- $Empty = true;
- }
- } else {
- $i = $this->DataAlert('invalid query \''.$Query.'\' because item \''.$ItemLst[$i].'\' is neither an Array nor an Object. Its type is \''.gettype($Var).'\'.');
- }
- if ($i!==false) $i++;
- }
- // Assign data
- if ($i!==false) {
- if ($Empty) {
- $this->RecSet = array();
- } else {
- $this->RecSet = &$Var;
- }
- }
- } elseif ($this->SubType===3) { // Clear
- $this->RecSet = array();
- }
- // First record
- if ($this->RecSet!==false) {
- $this->RecNbr = $this->RecNumInit + count($this->RecSet);
- $this->FirstRec = true;
- $this->RecSaved = true;
- $this->RecSaving = false;
- }
- break;
- case 6: // MySQL
- switch ($this->SubType) {
- case 0: $this->RecSet = @mysql_query($Query,$this->SrcId); break;
- case 1: $this->RecSet = $this->SrcId; break;
- case 2: $this->RecSet = @mysql_query($Query); break;
- }
- if ($this->RecSet===false) $this->DataAlert('MySql error message when opening the query: '.mysql_error());
- break;
- case 1: // Num
- $this->RecSet = true;
- $this->NumMin = 1;
- $this->NumMax = 1;
- $this->NumStep = 1;
- if (is_array($Query)) {
- if (isset($Query['min'])) $this->NumMin = $Query['min'];
- if (isset($Query['step'])) $this->NumStep = $Query['step'];
- if (isset($Query['max'])) {
- $this->NumMax = $Query['max'];
- } else {
- $this->RecSet = $this->DataAlert('the \'num\' source is an array that has no value for the \'max\' key.');
- }
- if ($this->NumStep==0) $this->RecSet = $this->DataAlert('the \'num\' source is an array that has a step value set to zero.');
- } else {
- $this->NumMax = ceil($Query);
- }
- if ($this->RecSet) {
- if ($this->NumStep>0) {
- $this->NumVal = $this->NumMin;
- } else {
- $this->NumVal = $this->NumMax;
- }
- }
- break;
- case 2: // Text
- if (is_string($Query)) {
- $this->RecSet = &$Query;
- } else {
- $this->RecSet = ''.$Query;
- }
- break;
- case 3: // Custom function
- $FctOpen = $this->FctOpen;
- $this->RecSet = $FctOpen($this->SrcId,$Query);
- if ($this->RecSet===false) $this->DataAlert('function '.$FctOpen.'() has failed to open query {'.$Query.'}');
- break;
- case 4: // Custom method from ObjectRef
- $this->RecSet = call_user_func_array($this->FctOpen,array(&$this->SrcId,&$Query));
- if ($this->RecSet===false) $this->DataAlert('method '.get_class($this->FctOpen[0]).'::'.$this->FctOpen[1].'() has failed to open query {'.$Query.'}');
- break;
- case 5: // Custom method of object
- $this->RecSet = $this->SrcId->tbsdb_open($this->SrcId,$Query);
- if ($this->RecSet===false) $this->DataAlert('method '.get_class($this->SrcId).'::tbsdb_open() has failed to open query {'.$Query.'}');
- break;
- case 7: // PostgreSQL
- switch ($this->SubType) {
- case 0: $this->RecSet = @pg_query($this->SrcId,$Query); break;
- case 1: $this->RecSet = $this->SrcId; break;
- }
- if ($this->RecSet===false) $this->DataAlert('PostgreSQL error message when opening the query: '.pg_last_error($this->SrcId));
- break;
- case 8: // SQLite
- switch ($this->SubType) {
- case 0: $this->RecSet = @sqlite_query($this->SrcId,$Query); break;
- case 1: $this->RecSet = $this->SrcId; break;
- }
- if ($this->RecSet===false) $this->DataAlert('SQLite error message when opening the query:'.sqlite_error_string(sqlite_last_error($this->SrcId)));
- break;
- case 9: // Iterator
- if ($this->SubType==1) {
- $this->RecSet = $this->SrcId;
- } else { // 2 or 3
- $this->RecSet = $this->SrcId->getIterator();
- }
- $this->RecSet->rewind();
- break;
- }
- if (($this->Type===0) or ($this->Type===9)) {
- unset($this->RecKey); $this->RecKey = '';
- } else {
- if ($this->RecSaving) {
- unset($this->RecBuffer); $this->RecBuffer = array();
- }
- $this->RecKey = &$this->RecNum; // Not array: RecKey = RecNum
- }
- return ($this->RecSet!==false);
- }
- function DataFetch() {
- if ($this->RecSaved) {
- if ($this->RecNum<$this->RecNbr) {
- if ($this->FirstRec) {
- if ($this->SubType===2) { // From string
- reset($this->RecSet);
- $this->RecKey = key($this->RecSet);
- $this->CurrRec = &$this->RecSet[$this->RecKey];
- } else {
- $this->CurrRec = reset($this->RecSet);
- $this->RecKey = key($this->RecSet);
- }
- $this->FirstRec = false;
- } else {
- if ($this->SubType===2) { // From string
- next($this->RecSet);
- $this->RecKey = key($this->RecSet);
- $this->CurrRec = &$this->RecSet[$this->RecKey];
- } else {
- $this->CurrRec = next($this->RecSet);
- $this->RecKey = key($this->RecSet);
- }
- }
- if ((!is_array($this->CurrRec)) and (!is_object($this->CurrRec))) $this->CurrRec = array('key'=>$this->RecKey, 'val'=>$this->CurrRec);
- $this->RecNum++;
- if ($this->OnDataOk) {
- if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
- if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
- if ($this->SubType!==2) $this->RecSet[$this->RecKey] = $this->CurrRec; // save modifications because array reading is done without reference :(
- }
- } else {
- unset($this->CurrRec); $this->CurrRec = false;
- }
- return;
- }
- switch ($this->Type) {
- case 6: // MySQL
- $this->CurrRec = mysql_fetch_assoc($this->RecSet);
- break;
- case 1: // Num
- if (($this->NumVal>=$this->NumMin) and ($this->NumVal<=$this->NumMax)) {
- $this->CurrRec = array('val'=>$this->NumVal);
- $this->NumVal += $this->NumStep;
- } else {
- $this->CurrRec = false;
- }
- break;
- case 2: // Text
- if ($this->RecNum===0) {
- if ($this->RecSet==='') {
- $this->CurrRec = false;
- } else {
- $this->CurrRec = &$this->RecSet;
- }
- } else {
- $this->CurrRec = false;
- }
- break;
- case 3: // Custom function
- $FctFetch = $this->FctFetch;
- $this->CurrRec = $FctFetch($this->RecSet,$this->RecNum+1);
- break;
- case 4: // Custom method from ObjectRef
- $this->FctPrm[0] = &$this->RecSet; $this->FctPrm[1] = $this->RecNum+1;
- $this->CurrRec = call_user_func_array($this->FctFetch,$this->FctPrm);
- break;
- case 5: // Custom method of object
- $this->CurrRec = $this->SrcId->tbsdb_fetch($this->RecSet,$this->RecNum+1);
- break;
- case 7: // PostgreSQL
- $this->CurrRec = @pg_fetch_array($this->RecSet,$this->RecNum,PGSQL_ASSOC); // warning comes when no record left.
- break;
- case 8: // SQLite
- $this->CurrRec = sqlite_fetch_array($this->RecSet,SQLITE_ASSOC);
- break;
- case 9: // Iterator
- if ($this->RecSet->valid()) {
- $this->CurrRec = $this->RecSet->current();
- $this->RecKey = $this->RecSet->key();
- $this->RecSet->next();
- } else {
- $this->CurrRec = false;
- }
- break;
- }
- // Set the row count
- if ($this->CurrRec!==false) {
- $this->RecNum++;
- if ($this->OnDataOk) {
- $this->OnDataArgs[1] = &$this->CurrRec; // Reference has changed if ($this->SubType===2)
- if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
- if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
- }
- if ($this->RecSaving) $this->RecBuffer[$this->RecKey] = $this->CurrRec;
- }
- }
- function DataClose() {
- $this->OnDataOk = false;
- $this->OnDataPrm = false;
- $this->OnDataPi = false;
- if ($this->RecSaved) return;
- switch ($this->Type) {
- case 6: mysql_free_result($this->RecSet); break;
- case 3: $FctClose=$this->FctClose; $FctClose($this->RecSet); break;
- case 4: call_user_func_array($this->FctClose,array(&$this->RecSet)); break;
- case 5: $this->SrcId->tbsdb_close($this->RecSet); break;
- case 7: pg_free_result($this->RecSet); break;
- }
- if ($this->RecSaving) {
- $this->RecSet = &$this->RecBuffer;
- $this->RecNbr = $this->RecNumInit + count($this->RecSet);
- $this->RecSaving = false;
- $this->RecSaved = true;
- }
- }
- }
- // *********************************************
- class clsTinyButStrong {
- // Public properties
- var $Source = '';
- var $Render = 3;
- var $TplVars = array();
- var $ObjectRef = false;
- var $NoErr = false;
- var $Assigned = array();
- // Undocumented (can change at any version)
- var $Version = '3.5.3';
- var $Charset = '';
- var $TurboBlock = true;
- var $VarPrefix = '';
- var $Protect = true;
- var $ErrCount = 0;
- var $ErrMsg = '';
- var $AttDelim = false;
- var $MethodsAllowed = false;
- // Private
- var $_ErrMsgName = '';
- var $_LastFile = '';
- var $_CharsetFct = false;
- var $_Mode = 0;
- var $_CurrBlock = '';
- var $_ChrOpen = '[';
- var $_ChrClose = ']';
- var $_ChrVal = '[val]';
- var $_ChrProtect = '[';
- var $_PlugIns = array();
- var $_PlugIns_Ok = false;
- var $_piOnFrm_Ok = false;
- function clsTinyButStrong($Chrs='',$VarPrefix='') {
- if ($Chrs!=='') {
- $Ok = false;
- $Len = strlen($Chrs);
- if ($Len===2) { // For compatibility
- $this->_ChrOpen = $Chrs[0];
- $this->_ChrClose = $Chrs[1];
- $Ok = true;
- } else {
- $Pos = strpos($Chrs,',');
- if (($Pos!==false) and ($Pos>0) and ($Pos<$Len-1)) {
- $this->_ChrOpen = substr($Chrs,0,$Pos);
- $this->_ChrClose = substr($Chrs,$Pos+1);
- $Ok = true;
- }
- }
- if ($Ok) {
- $this->_ChrVal = $this->_ChrOpen.'val'.$this->_ChrClose;
- $this->_ChrProtect = '&#'.ord($this->_ChrOpen[0]).';'.substr($this->_ChrOpen,1);
- } else {
- $this->meth_Misc_Alert('with clsTinyButStrong() function','value \''.$Chrs.'\' is a bad tag delimitor definition.');
- }
- }
- $this->VarPrefix = $VarPrefix;
- // Links to global variables
- global $_TBS_FormatLst, $_TBS_UserFctLst, $_TBS_AutoInstallPlugIns;
- if (!isset($_TBS_FormatLst)) $_TBS_FormatLst = array();
- if (!isset($_TBS_UserFctLst)) $_TBS_UserFctLst = array();
- $this->_FormatLst = &$_TBS_FormatLst;
- $this->_UserFctLst = &$_TBS_UserFctLst;
- // Auto-installing plug-ins
- if (isset($_TBS_AutoInstallPlugIns)) foreach ($_TBS_AutoInstallPlugIns as $pi) $this->PlugIn(TBS_INSTALL,$pi);
- }
- // Public methods
- function LoadTemplate($File,$Charset='') {
- if ($File==='') {
- $this->meth_Misc_Charset($Charset);
- return true;
- }
- $Ok = true;
- if ($this->_PlugIns_Ok) {
- if (isset($this->_piBeforeLoadTemplate) or isset($this->_piAfterLoadTemplate)) {
- // Plug-ins
- $ArgLst = func_get_args();
- $ArgLst[0] = &$File;
- $ArgLst[1] = &$Charset;
- if (isset($this->_piBeforeLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeLoadTemplate,$ArgLst);
- }
- }
- // Load the file
- if ($Ok!==false) {
- if (!is_null($File)) {
- $x = '';
- if (!$this->f_Misc_GetFile($x,$File,$this->_LastFile)) return $this->meth_Misc_Alert('with LoadTemplate() method','file \''.$File.'\' is not found or not readable.');
- if ($Charset==='+') {
- $this->Source .= $x;
- } else {
- $this->Source = $x;
- }
- }
- if ($this->_Mode==0) {
- if (!is_null($File)) $this->_LastFile = $File;
- if ($Charset!=='+') $this->TplVars = array();
- $this->meth_Misc_Charset($Charset);
- }
- // Automatic fields and blocks
- $this->meth_Merge_AutoOn($this->Source,'onload',true,true);
- }
- // Plug-ins
- if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piAfterLoadTemplate,$ArgLst);
- return $Ok;
- }
- function GetBlockSource($BlockName,$List=false,$KeepDefTags=true,$ReplaceWith=false) {
- $RetVal = array();
- $Nbr = 0;
- $Pos = 0;
- $FieldOutside = false;
- $P1 = false;
- $Mode = ($KeepDefTags) ? 3 : 2;
- $PosBeg1 = 0;
- while ($Loc = $this->meth_Locator_FindBlockNext($this->Source,$BlockName,$Pos,'.',$Mode,$P1,$FieldOutside)) {
- $Nbr++;
- if ($Nbr==1) $PosBeg1 = $Loc->PosBeg;
- if ($List) $RetVal[$Nbr] = $Loc->BlockSrc;
- $Pos = $Loc->PosEnd;
- $P1 = false;
- }
- if ($Nbr==0) return false;
- if (!$List) $RetVal = substr($this->Source,$PosBeg1,$Pos-$PosBeg1+1);
- if ($ReplaceWith!==false) $this->Source = substr($this->Source,0,$PosBeg1).$ReplaceWith.substr($this->Source,$Pos+1);
- return $RetVal;
- }
- function MergeBlock($BlockLst,$SrcId='assigned',$Query='') {
- if ($SrcId==='assigned') {
- $Arg = array($BlockLst,&$SrcId,&$Query);
- if (!$this->meth_Misc_Assign($BlockLst, $Arg, 'MergeBlock')) return 0;
- $BlockLst = $Arg[0]; $SrcId = &$Arg[1]; $Query = &$Arg[2];
- }
- if (is_string($BlockLst)) $BlockLst = explode(',',$BlockLst);
- if ($SrcId==='cond') {
- $Nbr = 0;
- foreach ($BlockLst as $Block) {
- $Block = trim($Block);
- if ($Block!=='') $Nbr += $this->meth_Merge_AutoOn($this->Source,$Block,true,true);
- }
- return $Nbr;
- } else {
- return $this->meth_Merge_Block($this->Source,$BlockLst,$SrcId,$Query,false,0);
- }
- }
- function MergeField($NameLst,$Value='assigned',$IsUserFct=false,$DefaultPrm=false) {
- $FctCheck = $IsUserFct;
- if ($PlugIn = isset($this->_piOnMergeField)) $ArgPi = array('','',&$Value,0,&$this->Source,0,0);
- $SubStart = 0;
- $Ok = true;
- $Prm = is_array($DefaultPrm);
- if ( ($Value==='assigned') and ($NameLst!=='var') and ($NameLst!=='onshow') and ($NameLst!=='onload') ) {
- $Arg = array($NameLst,&$Value,&$IsUserFct,&$DefaultPrm);
- if (!$this->meth_Misc_Assign($NameLst, $Arg, 'MergeField')) return false;
- $NameLst = $Arg[0]; $Value = &$Arg[1]; $IsUserFct = &$Arg[2]; $DefaultPrm = &$Arg[3];
- }
- $NameLst = explode(',',$NameLst);
- foreach ($NameLst as $Name) {
- $Name = trim($Name);
- $Cont = false;
- switch ($Name) {
- case '': $Cont=true;break;
- case 'onload': $this->meth_Merge_AutoOn($this->Source,'onload',true,true);$Cont=true;break;
- case 'onshow': $this->meth_Merge_AutoOn($this->Source,'onshow',true,true);$Cont=true;break;
- case 'var': $this->meth_Merge_AutoVar($this->Source,true);$Cont=true;break;
- }
- if ($Cont) continue;
- if ($PlugIn) $ArgPi[0] = $Name;
- $PosBeg = 0;
- // Initilize the user function (only once)
- if ($FctCheck) {
- $FctInfo = $Value;
- $ErrMsg = false;
- if (!$this->meth_Misc_UserFctCheck($FctInfo,'f',$ErrMsg,$ErrMsg)) return $this->meth_Misc_Alert('with MergeField() method',$ErrMsg);
- $FctArg = array('','');
- $SubStart = false;
- $FctCheck = false;
- }
- while ($Loc = $this->meth_Locator_FindTbs($this->Source,$Name,$PosBeg,'.')) {
- if ($Prm) $Loc->PrmLst = array_merge($DefaultPrm,$Loc->PrmLst);
- // Apply user function
- if ($IsUserFct) {
- $FctArg[0] = &$Loc->SubName; $FctArg[1] = &$Loc->PrmLst;
- $Value = call_user_func_array($FctInfo,$FctArg);
- }
- // Plug-ins
- if ($PlugIn) {
- $ArgPi[1] = $Loc->SubName; $ArgPi[3] = &$Loc->PrmLst; $ArgPi[5] = &$Loc->PosBeg; $ArgPi[6] = &$Loc->PosEnd;
- $Ok = $this->meth_PlugIn_RunAll($this->_piOnMergeField,$ArgPi);
- }
- // Merge the field
- if ($Ok) {
- $PosBeg = $this->meth_Locator_Replace($this->Source,$Loc,$Value,$SubStart);
- } else {
- $PosBeg = $Loc->PosEnd;
- }
- }
- }
- }
- function Show($Render=false) {
- $Ok = true;
- if ($Render===false) $Render = $this->Render;
- if ($this->_PlugIns_Ok) {
- if (isset($this->_piBeforeShow) or isset($this->_piAfterShow)) {
- // Plug-ins
- $ArgLst = func_get_args();
- $ArgLst[0] = &$Render;
- if (isset($this->_piBeforeShow)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeShow,$ArgLst);
- }
- }
- if ($Ok!==false) {
- $this->meth_Merge_AutoOn($this->Source,'onshow',true,true);
- $this->meth_Merge_AutoVar($this->Source,true);
- }
- if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterShow)) $this->meth_PlugIn_RunAll($this->_piAfterShow,$ArgLst);
- if ($this->_ErrMsgName!=='') $this->MergeField($this->_ErrMsgName, $this->ErrMsg);
- if (($Render & TBS_OUTPUT)==TBS_OUTPUT) echo $this->Source;
- if (($this->_Mode==0) and (($Render & TBS_EXIT)==TBS_EXIT)) exit;
- return $Ok;
- }
- function PlugIn($Prm1,$Prm2=0) {
- if (is_numeric($Prm1)) {
- switch ($Prm1) {
- case TBS_INSTALL:
- $PlugInId = $Prm2;
- // Try to install the plug-in
- if (isset($this->_PlugIns[$PlugInId])) {
- return $this->meth_Misc_Alert('with PlugIn() method','plug-in \''.$PlugInId.'\' is already installed.');
- } else {
- $ArgLst = func_get_args();
- array_shift($ArgLst); array_shift($ArgLst);
- return $this->meth_PlugIn_Install($PlugInId,$ArgLst,false);
- }
- case TBS_ISINSTALLED:
- // Check if the plug-in is installed
- return isset($this->_PlugIns[$Prm2]);
- case -4: // Deactivate special plug-ins
- $this->_PlugIns_Ok_save = $this->_PlugIns_Ok;
- $this->_PlugIns_Ok = false;
- return true;
- case -5: // Deactivate OnFormat
- $this->_piOnFrm_Ok_save = $this->_piOnFrm_Ok;
- $this->_piOnFrm_Ok = false;
- return true;
- case -10: // Restore
- if (isset($this->_PlugIns_Ok_save)) $this->_PlugIns_Ok = $this->_PlugIns_Ok_save;
- if (isset($this->_piOnFrm_Ok_save)) $this->_piOnFrm_Ok = $this->_piOnFrm_Ok_save;
- return true;
- }
- } elseif (is_string($Prm1)) {
- // Plug-in's command
- $PlugInId = $Prm1;
- if (!isset($this->_PlugIns[$PlugInId])) {
- if (!$this->meth_PlugIn_Install($PlugInId,array(),true)) return false;
- }
- 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.');
- $ArgLst = func_get_args();
- array_shift($ArgLst);
- $Ok = call_user_func_array($this->_piOnCommand[$PlugInId],$ArgLst);
- if (is_null($Ok)) $Ok = true;
- return $Ok;
- }
- return $this->meth_Misc_Alert('with PlugIn() method','\''.$Prm1.'\' is an invalid plug-in key, the type of the value is \''.gettype($Prm1).'\'.');
- }
- // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- function meth_Locator_FindTbs(&$Txt,$Name,$Pos,$ChrSub) {
- // Find a TBS Locator
- $PosEnd = false;
- $PosMax = strlen($Txt) -1;
- $Start = $this->_ChrOpen.$Name;
- do {
- // Search for the opening char
- if ($Pos>$PosMax) return false;
- $Pos = strpos($Txt,$Start,$Pos);
- // If found => next chars are analyzed
- if ($Pos===false) {
- return false;
- } else {
- $Loc = &new clsTbsLocator;
- $ReadPrm = false;
- $PosX = $Pos + strlen($Start);
- $x = $Txt[$PosX];
- if ($x===$this->_ChrClose) {
- $PosEnd = $PosX;
- } elseif ($x===$ChrSub) {
- $Loc->SubOk = true; // it is no longer the false value
- $ReadPrm = true;
- $PosX++;
- } elseif (strpos(';',$x)!==false) {
- $ReadPrm = true;
- $PosX++;
- } else {
- $Pos++;
- }
- $Loc->PosBeg = $Pos;
- if ($ReadPrm) {
- $this->f_Loc_PrmRead($Txt,$PosX,false,'\'',$this->_ChrOpen,$this->_ChrClose,$Loc,$PosEnd);
- if ($PosEnd===false) {
- $this->meth_Misc_Alert('','can\'t found the end of the tag \''.substr($Txt,$Pos,$PosX-$Pos+10).'...\'.');
- $Pos++;
- }
- }
- }
- } while ($PosEnd===false);
- $Loc->PosEnd = $PosEnd;
- if ($Loc->SubOk) {
- $Loc->FullName = $Name.'.'.$Loc->SubName;
- $Loc->SubLst = explode('.',$Loc->SubName);
- $Loc->SubNbr = count($Loc->SubLst);
- } else {
- $Loc->FullName = $Name;
- }
- if ($ReadPrm and isset($Loc->PrmLst['comm'])) {
- $Loc->PosBeg0 = $Loc->PosBeg;
- $Loc->PosEnd0 = $Loc->PosEnd;
- $comm = $Loc->PrmLst['comm'];
- if (($comm===true) or ($comm==='')) {
- $Loc->Enlarged = $this->f_Loc_EnlargeToStr($Txt,$Loc,'<!--' ,'-->');
- } else {
- $Loc->Enlarged = $this->f_Loc_EnlargeToTag($Txt,$Loc,$comm,false);
- }
- }
- return $Loc;
- }
- function &meth_Locator_SectionNewBDef(&$LocR,$BlockName,$Txt,$PrmLst) {
- $Chk = true;
- $LocLst = array();
- $LocNbr = 0;
- // Cache TBS locators
- if ($this->TurboBlock) {
- $Chk = false;
- $Pos = 0;
- $PrevEnd = -1;
- $PrevIsAMF = false;
- while ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$Pos,'.')) {
- $IsAMF = false;
- if (($Loc->SubName==='#') or ($Loc->SubName==='$')) {
- $Loc->IsRecInfo = true;
- $Loc->RecInfo = $Loc->SubName;
- $Loc->SubName = '';
- } else {
- $Loc->IsRecInfo = false;
- }
- if ($Loc->PosBeg>$PrevEnd) { // No embedding
- if (isset($Loc->PrmLst['att'])) {
- $LocSrc = substr($Txt,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
- $this->f_Xml_AttFind($Txt,$Loc,true,$this->AttDelim);
- if ($Loc->AttForward) {
- $IsAMF = true;
- } else {
- for ($i=$LocNbr;$i>0;$i--) {
- if ($LocLst[$i]->PosEnd>=$Loc->PosBeg) {
- $LocNbr--;
- } else {
- $i = 0;
- }
- }
- }
- unset($Loc->PrmLst['att']);
- }
- $LocNbr++;
- } else {
- // The previous tag is embedding => no increment, then previous Loc is overwrited
- $Chk = true;
- if ($PrevIsAMF) {
- $l = &$LocLst[$LocNbr];
- $this->meth_Misc_Alert('','TBS is not able to merge the field '.$LocSrc.' because parameter \'att\' makes this fied moving forward over another TBS field.');
- }
- }
- $PrevEnd = $Loc->PosEnd;
- $PrevIsAMF = false;
- if ($IsAMF) {
- $Pos = $Loc->PrevPosBeg;
- $PrevIsAMF = true;
- } elseif ($Loc->Enlarged) { // Parameter 'comm'
- $Pos = $Loc->PosBeg0+1;
- $Loc->Enlarged = false;
- } else {
- $Pos = $Loc->PosBeg+1;
- }
- $LocLst[$LocNbr] = $Loc;
- }
- }
- // Create the object
- $o = (object) null;
- $o->Prm = $PrmLst;
- $o->LocLst = $LocLst;
- $o->LocNbr = $LocNbr;
- $o->Name = $BlockName;
- $o->Src = $Txt;
- $o->Chk = $Chk;
- $o->IsSerial = false;
- $o->AutoSub = false;
- $i = 1;
- while (isset($PrmLst['sub'.$i])) {
- $o->AutoSub = $i;
- $i++;
- }
- $LocR->BDefLst[] = &$o; // Can be usefull for plug-in
- return $o;
- }
- function meth_Locator_SectionAddGrp(&$LocR,$BlockName,&$BDef,$Type,$Field,$Prm) {
- $BDef->PrevValue = false;
- $BDef->Type = $Type;
- // Save sub items in a structure near to Locator.
- $Field0 = $Field;
- if (strpos($Field,$this->_ChrOpen)===false) $Field = $this->_ChrOpen.$BlockName.'.'.$Field.$this->_ChrClose;
- $BDef->FDef = &$this->meth_Locator_SectionNewBDef($LocR,$BlockName,$Field,array());
- if ($BDef->FDef->LocNbr==0) $this->meth_Misc_Alert('Parameter '.$Prm,'The value \''.$Field0.'\' is unvalide for this parameter.');
- if ($Type==='H') {
- if ($LocR->HeaderFound===false) {
- $LocR->HeaderFound = true;
- $LocR->HeaderNbr = 0;
- $LocR->HeaderDef = array(); // 1 to HeaderNbr
- }
- $i = ++$LocR->HeaderNbr;
- $LocR->HeaderDef[$i] = &$BDef;
- } else {
- if ($LocR->FooterFound===false) {
- $LocR->FooterFound = true;
- $LocR->FooterNbr = 0;
- $LocR->FooterDef = array(); // 1 to FooterNbr
- }
- $BDef->AddLastGrp = ($Type==='F');
- $i = ++$LocR->FooterNbr;
- $LocR->FooterDef[$i] = &$BDef;
- }
- }
- function meth_Locator_Replace(&$Txt,&$Loc,&$Value,$SubStart) {
- // This function enables to merge a locator with a text and returns the position just after the replaced block
- // This position can be useful because we don't know in advance how $Value will be replaced.
- // Found the value if there is a subname
- if (($SubStart!==false) and $Loc->SubOk) {
- for ($i=$SubStart;$i<$Loc->SubNbr;$i++) {
- $x = $Loc->SubLst[$i]; // &$Loc... brings an error with Event Example, I don't know why.
- if (is_array($Value)) {
- if (isset($Value[$x])) {
- $Value = &$Value[$x];
- } elseif (array_key_exists($x,$Value)) {// can happens when value is NULL
- $Value = &$Value[$x];
- } else {
- if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'item \''.$x.'\' is not an existing key in the array.',true);
- unset($Value); $Value = ''; break;
- }
- } elseif (is_object($Value)) {
- $ArgLst = $this->f_Misc_CheckArgLst($x);
- if (method_exists($Value,$x)) {
- if ($this->MethodsAllowed or !in_array(strtok($Loc->FullName,'.'),array('onload','onshow','var')) ) {
- $x = call_user_func_array(array(&$Value,$x),$ArgLst);
- } else {
- if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'\''.$x.'\' is a method and the current TBS settings do not allow to call methods on automatic fields.',true);
- $x = '';
- }
- } elseif (property_exists($Value,$x)) {
- $x = &$Value->$x;
- } elseif (isset($Value->$x)) {
- $x = $Value->$x; // useful for overloaded property
- } else {
- 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);
- unset($Value); $Value = ''; break;
- }
- $Value = &$x; unset($x); $x = '';
- } else {
- 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);
- unset($Value); $Value = ''; break;
- }
- }
- }
- $CurrVal = $Value; // Unlink
- if (isset($Loc->PrmLst['onformat'])) {
- if ($Loc->FirstMerge) {
- $Loc->OnFrmInfo = $Loc->PrmLst['onformat'];
- $Loc->OnFrmArg = array($Loc->FullName,'',&$Loc->PrmLst,&$this);
- $ErrMsg = false;
- if (!$this->meth_Misc_UserFctCheck($Loc->OnFrmInfo,'f',$ErrMsg,$ErrMsg)) {
- unset($Loc->PrmLst['onformat']);
- if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'(parameter onformat) '.$ErrMsg);
- $Loc->OnFrmInfo = 'pi'; // Execute the function pi() just to avoid extra error messages
- }
- } else {
- $Loc->OnFrmArg[3] = &$this; // bugs.php.net/51174
- }
- $Loc->OnFrmArg[1] = &$CurrVal;
- if (isset($Loc->PrmLst['subtpl'])) {
- $this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
- call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
- $this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
- $Loc->ConvProtect = false;
- $Loc->ConvStr = false;
- } else {
- call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
- }
- }
- if ($Loc->FirstMerge) {
- if (isset($Loc->PrmLst['frm'])) {
- $Loc->ConvMode = 0; // Frm
- $Loc->ConvProtect = false;
- } else {
- // Analyze parameter 'htmlconv'
- if (isset($Loc->PrmLst['htmlconv'])) {
- $x = strtolower($Loc->PrmLst['htmlconv']);
- $x = '+'.str_replace(' ','',$x).'+';
- if (strpos($x,'+esc+')!==false) {$this->f_Misc_ConvSpe($Loc); $Loc->ConvStr = false; $Loc->ConvEsc = true; }
- if (strpos($x,'+wsp+')!==false) {$this->f_Misc_ConvSpe($Loc); $Loc->ConvWS = true; }
- if (strpos($x,'+js+')!==false) {$this->f_Misc_ConvSpe($Loc); $Loc->ConvStr = false; $Loc->ConvJS = true; }
- if (strpos($x,'+url+')!==false) {$this->f_Misc_ConvSpe($Loc); $Loc->ConvStr = false; $Loc->ConvUrl = true; }
- if (strpos($x,'+no+')!==false) $Loc->ConvStr = false;
- if (strpos($x,'+yes+')!==false) $Loc->ConvStr = true;
- if (strpos($x,'+nobr+')!==false) {$Loc->ConvStr = true; $Loc->ConvBr = false; }
- } else {
- if ($this->Charset===false) $Loc->ConvStr = false; // No conversion
- }
- // Analyze parameter 'protect'
- if (isset($Loc->PrmLst['protect'])) {
- $x = strtolower($Loc->PrmLst['protect']);
- if ($x==='no') {
- $Loc->ConvProtect = false;
- } elseif ($x==='yes') {
- $Loc->ConvProtect = true;
- }
- } elseif ($this->Protect===false) {
- $Loc->ConvProtect = false;
- }
- }
- if ($Loc->Ope = isset($Loc->PrmLst['ope'])) {
- $OpeLst = explode(',',$Loc->PrmLst['ope']);
- $Loc->OpeAct = array();
- $Loc->OpeArg = array();
- foreach ($OpeLst as $i=>$ope) {
- if ($ope==='list') {
- $Loc->OpeAct[$i] = 1;
- $Loc->OpePrm[$i] = (isset($Loc->PrmLst['valsep'])) ? $Loc->PrmLst['valsep'] : ',';
- } elseif ($ope==='minv') {
- $Loc->OpeAct[$i] = 11;
- $Loc->MSave = $Loc->MagnetId;
- } else {
- $x = substr($ope,0,4);
- if ($x==='max:') {
- if (isset($Loc->PrmLst['maxhtml'])) {$Loc->OpeAct[$i]=2;} elseif (isset($Loc->PrmLst['maxutf8'])) {$Loc->OpeAct[$i]=4;} else {$Loc->OpeAct[$i]=3;}
- $Loc->OpePrm[$i] = intval(trim(substr($ope,4)));
- $Loc->OpeEnd = (isset($Loc->PrmLst['maxend'])) ? $Loc->PrmLst['maxend'] : '...';
- if ($Loc->OpePrm[$i]<=0) $Loc->Ope = false;
- } elseif ($x==='mod:') {$Loc->OpeAct[$i] = 5; $Loc->OpePrm[$i] = '0'+trim(substr($ope,4));
- } elseif ($x==='add:') {$Loc->OpeAct[$i] = 6; $Loc->OpePrm[$i] = '0'+trim(substr($ope,4));
- } elseif ($x==='mul:') {$Loc->OpeAct[$i] = 7; $Loc->OpePrm[$i] = '0'+trim(substr($ope,4));
- } elseif ($x==='div:') {$Loc->OpeAct[$i] = 8; $Loc->OpePrm[$i] = '0'+trim(substr($ope,4));
- } elseif ($x==='mok:') {$Loc->OpeAct[$i] = 9; $Loc->OpeMOK[] = trim(substr($ope,4)); $Loc->MSave = $Loc->MagnetId;
- } elseif ($x==='mko:') {$Loc->OpeAct[$i] =10; $Loc->OpeMKO[] = trim(substr($ope,4)); $Loc->MSave = $Loc->MagnetId;
- } elseif ($x==='nif:') {$Loc->OpeAct[$i] =12; $Loc->OpePrm[$i] = trim(substr($ope,4));
- } elseif (isset($this->_piOnOperation)) {
- $Loc->OpeAct[$i] = 0;
- $Loc->OpePrm[$i] = $ope;
- $Loc->OpeArg[$i] = array($Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$Txt,$Loc->PosBeg,$Loc->PosEnd,&$Loc);
- $Loc->PrmLst['_ope'] = $Loc->PrmLst['ope'];
- } elseif (!isset($Loc->PrmLst['noerr'])) {
- $this->meth_Misc_Alert($Loc,'parameter ope doesn\'t support value \''.$ope.'\'.',true);
- }
- }
- }
- }
- $Loc->FirstMerge = false;
- }
- $ConvProtect = $Loc->ConvProtect;
- // Plug-in OnFormat
- if ($this->_piOnFrm_Ok) {
- if (isset($Loc->OnFrmArgPi)) {
- $Loc->OnFrmArgPi[1] = &$CurrVal;
- $Loc->OnFrmArgPi[3] = &$this; // bugs.php.net/51174
- } else {
- $Loc->OnFrmArgPi = array($Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$this);
- }
- $this->meth_PlugIn_RunAll($this->_piOnFormat,$Loc->OnFrmArgPi);
- }
- // Operation
- if ($Loc->Ope) {
- foreach ($Loc->OpeAct as $i=>$ope) {
- switch ($ope) {
- case 0:
- $Loc->PrmLst['ope'] = $Loc->OpePrm[$i]; // for compatibility
- $OpeArg = &$Loc->OpeArg[$i];
- $OpeArg[1] = &$CurrVal; $OpeArg[3] = &$Txt;
- if (!$this->meth_PlugIn_RunAll($this->_piOnOperation,$OpeArg)) return $Loc->PosBeg;
- break;
- case 1: if (is_array($CurrVal)) $CurrVal = implode($Loc->OpePrm[$i],$CurrVal); break;
- case 2: if (strlen(''.$CurrVal)>$Loc->OpePrm[$i]) $this->f_Xml_Max($CurrVal,$Loc->OpePrm[$i],$Loc->OpeEnd); break;
- case 3: if (strlen(''.$CurrVal)>$Loc->OpePrm[$i]) $CurrVal = substr(''.$CurrVal,0,$Loc->OpePrm[$i]).$Loc->OpeEnd; break;
- case 4: if (strlen(''.$CurrVal)>$Loc->OpePrm[$i]) $CurrVal = mb_substr(''.$CurrVal,0,$Loc->OpePrm[$i],'UTF-8').$Loc->OpeEnd; break;
- case 5: $CurrVal = ('0'+$CurrVal) % $Loc->OpePrm[$i]; break;
- case 6: $CurrVal = ('0'+$CurrVal) + $Loc->OpePrm[$i]; break;
- case 7: $CurrVal = ('0'+$CurrVal) * $Loc->OpePrm[$i]; break;
- case 8: $CurrVal = ('0'+$CurrVal) / $Loc->OpePrm[$i]; break;
- case 9; case 10:
- if ($ope===9) {
- $CurrVal = (in_array((string)$CurrVal,$Loc->OpeMOK)) ? ' ' : '';
- } else {
- $CurrVal = (in_array((string)$CurrVal,$Loc->OpeMKO)) ? '' : ' ';
- } // no break here
- case 11:
- if ((string)$CurrVal==='') {
- if ($Loc->MagnetId===0) $Loc->MagnetId = $Loc->MSave;
- } else {
- if ($Loc->MagnetId!==0) {
- $Loc->MSave = $Loc->MagnetId;
- $Loc->MagnetId = 0;
- }
- $CurrVal = '';
- }
- break;
- case 12: if ((string)$CurrVal===$Loc->OpePrm[$i]) $CurrVal = ''; break;
- }
- }
- }
- // String conversion or format
- if ($Loc->ConvMode===1) { // Usual string conversion
- if (!is_string($CurrVal)) $CurrVal =(string)$CurrVal; // (string) is faster than strval() and settype()
- if ($Loc->ConvStr) $this->meth_Conv_Str($CurrVal,$Loc->ConvBr);
- } elseif ($Loc->ConvMode===0) { // Format
- $CurrVal = $this->meth_Misc_Format($CurrVal,$Loc->PrmLst);
- } elseif ($Loc->ConvMode===2) { // Special string conversion
- if (!is_string($CurrVal)) $CurrVal = (string)$CurrVal;
- if ($Loc->ConvStr) $this->meth_Conv_Str($CurrVal,$Loc->ConvBr);
- if ($Loc->ConvEsc) $CurrVal = str_replace('\'','\'\'',$CurrVal);
- if ($Loc->ConvWS) {
- $check = ' ';
- $nbsp = ' ';
- do {
- $pos = strpos($CurrVal,$check);
- if ($pos!==false) $CurrVal = substr_replace($CurrVal,$nbsp,$pos,1);
- } while ($pos!==false);
- }
- if ($Loc->ConvJS) {
- $CurrVal = addslashes($CurrVal); // apply to ('), ("), (\) and (null)
- $CurrVal = str_replace(array("\n","\r","\t"),array('\n','\r','\t'),$CurrVal);
- }
- if ($Loc->ConvUrl) $CurrVal = urlencode($CurrVal);
- }
- // if/then/else process, there may be several if/then
- if ($Loc->PrmIfNbr) {
- $z = false;
- $i = 1;
- while ($i!==false) {
- if ($Loc->PrmIfVar[$i]) $Loc->PrmIfVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmIf[$i],true);
- $x = str_replace($this->_ChrVal,$CurrVal,$Loc->PrmIf[$i]);
- if ($this->f_Misc_CheckCondition($x)) {
- if (isset($Loc->PrmThen[$i])) {
- if ($Loc->PrmThenVar[$i]) $Loc->PrmThenVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmThen[$i],true);
- $z = $Loc->PrmThen[$i];
- }
- $i = false;
- } else {
- $i++;
- if ($i>$Loc->PrmIfNbr) {
- if (isset($Loc->PrmLst['else'])) {
- if ($Loc->PrmElseVar) $Loc->PrmElseVar = $this->meth_Merge_AutoVar($Loc->PrmLst['else'],true);
- $z =$Loc->PrmLst['else'];
- }
- $i = false;
- }
- }
- }
- if ($z!==false) {
- if ($ConvProtect) {
- $CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
- $ConvProtect = false;
- }
- $CurrVal = str_replace($this->_ChrVal,$CurrVal,$z);
- }
- }
- if (isset($Loc->PrmLst['file'])) {
- $x = $Loc->PrmLst['file'];
- if ($x===true) $x = $CurrVal;
- $this->meth_Merge_AutoVar($x,false);
- $x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
- $CurrVal = '';
- if ($x!=='') {
- if ($this->f_Misc_GetFile($CurrVal,$x,$this->_LastFile)) {
- if (isset($Loc->PrmLst['getbody'])) $CurrVal = $this->f_Xml_GetPart($CurrVal,$Loc->PrmLst['getbody'],true);
- if (isset($Loc->PrmLst['rename'])) $this->meth_Locator_Rename($CurrVal, $Loc->PrmLst['rename']);
- } else {
- if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter file is not found or not readable.',true);
- }
- $ConvProtect = false;
- }
- }
- if (isset($Loc->PrmLst['script'])) {// Include external PHP script
- $x = $Loc->PrmLst['script'];
- if ($x===true) $x = $CurrVal;
- $this->meth_Merge_AutoVar($x,false);
- $x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
- if ($x!=='') {
- $this->_Subscript = $x;
- $this->CurrPrm = &$Loc->PrmLst;
- $sub = isset($Loc->PrmLst['subtpl']);
- if ($sub) $this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
- if ($this->meth_Misc_RunSubscript($CurrVal,$Loc->PrmLst)===false) {
- if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter script is not found or not readable.',true);
- }
- if ($sub) $this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
- if (isset($Loc->PrmLst['getbody'])) $CurrVal = $this->f_Xml_GetPart($CurrVal,$Loc->PrmLst['getbody'],true);
- if (isset($Loc->PrmLst['rename'])) $this->meth_Locator_Rename($CurrVal, $Loc->PrmLst['rename']);
- unset($this->CurrPrm);
- $ConvProtect = false;
- }
- }
- if (isset($Loc->PrmLst['att'])) $this->f_Xml_AttFind($Txt,$Loc,true,$this->AttDelim);
- // Case when it's an empty string
- if ($CurrVal==='') {
- if ($Loc->MagnetId===false) {
- if (isset($Loc->PrmLst['.'])) {
- $Loc->MagnetId = -1;
- } elseif (isset($Loc->PrmLst['ifempty'])) {
- $Loc->MagnetId = -2;
- } elseif (isset($Loc->PrmLst['magnet'])) {
- $Loc->MagnetId = 1;
- $Loc->PosBeg0 = $Loc->PosBeg;
- $Loc->PosEnd0 = $Loc->PosEnd;
- if ($Loc->PrmLst['magnet']==='#') {
- if (isset($Loc->AttBeg)) {
- $Loc->MagnetId = -3;
- } else {
- $this->meth_Misc_Alert($Loc,'parameter \'magnet=#\' cannot be processed because parameter \'att\' is not set or the attribute is not found.',true);
- }
- } elseif (isset($Loc->PrmLst['mtype'])) {
- switch ($Loc->PrmLst['mtype']) {
- case 'm+m': $Loc->MagnetId = 2; break;
- case 'm*': $Loc->MagnetId = 3; break;
- case '*m': $Loc->MagnetId = 4; break;
- }
- }
- } elseif (isset($Loc->PrmLst['attadd'])) {
- // In order to delete extra space
- $Loc->PosBeg0 = $Loc->PosBeg;
- $Loc->PosEnd0 = $Loc->PosEnd;
- $Loc->MagnetId = 5;
- } else {
- $Loc->MagnetId = 0;
- }
- }
- switch ($Loc->MagnetId) {
- case 0: break;
- case -1: $CurrVal = ' '; break; // Enables to avoid null cells in HTML tables
- case -2: $CurrVal = $Loc->PrmLst['ifempty']; break;
- case -3: $Loc->Enlarged = true; $Loc->PosBeg = $Loc->AttBegM; $Loc->PosEnd = $Loc->AttEnd; break;
- case 1:
- $Loc->Enlarged = true;
- $this->f_Loc_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],false);
- break;
- case 2:
- $Loc->Enlarged = true;
- $CurrVal = $this->f_Loc_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],true);
- break;
- case 3:
- $Loc->Enlarged = true;
- $Loc2 = $this->f_Xml_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,false,1,false);
- if ($Loc2!==false) {
- $Loc->PosBeg = $Loc2->PosBeg;
- if ($Loc->PosEnd<$Loc2->PosEnd) $Loc->PosEnd = $Loc2->PosEnd;
- }
- break;
- case 4:
- $Loc->Enlarged = true;
- $Loc2 = $this->f_Xml_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,true,1,false);
- if ($Loc2!==false) $Loc->PosEnd = $Loc2->PosEnd;
- break;
- case 5:
- $Loc->Enlarged = true;
- if (substr($Txt,$Loc->PosBeg-1,1)==' ') $Loc->PosBeg--;
- break;
- }
- $NewEnd = $Loc->PosBeg; // Useful when mtype='m+m'
- } else {
- if ($ConvProtect) $CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
- $NewEnd = $Loc->PosBeg + strlen($CurrVal);
- }
- $Txt = substr_replace($Txt,$CurrVal,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
- return $NewEnd; // Return the new end position of the field
- }
- function meth_Locator_FindBlockNext(&$Txt,$BlockName,$PosBeg,$ChrSub,$Mode,&$P1,&$FieldBefore) {
- // Return the first block locator just after the PosBeg position
- // Mode = 1 : Merge_Auto => doesn't save $Loc->BlockSrc, save the bounds of TBS Def tags instead, return also fields
- // Mode = 2 : FindBlockLst or GetBlockSource => save $Loc->BlockSrc without TBS Def tags
- // Mode = 3 : GetBlockSource => save $Loc->BlockSrc with TBS Def tags
- $SearchDef = true;
- $FirstField = false;
- // Search for the first tag with parameter "block"
- while ($SearchDef and ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub))) {
- if (isset($Loc->PrmLst['block'])) {
- if (isset($Loc->PrmLst['p1'])) {
- if ($P1) return false;
- $P1 = true;
- }
- $Block = $Loc->PrmLst['block'];
- $SearchDef = false;
- } elseif ($Mode===1) {
- return $Loc;
- } elseif ($FirstField===false) {
- $FirstField = $Loc;
- }
- $PosBeg = $Loc->PosEnd;
- }
- if ($SearchDef) {
- if ($FirstField!==false) $FieldBefore = true;
- return false;
- }
- $Loc->PosDefBeg = -1;
- if ($Block==='begin') { // Block definied using begin/end
- if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
- $Opened = 1;
- while ($Loc2 = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub)) {
- if (isset($Loc2->PrmLst['block'])) {
- switch ($Loc2->PrmLst['block']) {
- case 'end': $Opened--; break;
- case 'begin': $Opened++; break;
- }
- if ($Opened==0) {
- if ($Mode===1) {
- $Loc->PosBeg2 = $Loc2->PosBeg;
- $Loc->PosEnd2 = $Loc2->PosEnd;
- } else {
- if ($Mode===2) {
- $Loc->BlockSrc = substr($Txt,$Loc->PosEnd+1,$Loc2->PosBeg-$Loc->PosEnd-1);
- } else {
- $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc2->PosEnd-$Loc->PosBeg+1);
- }
- $Loc->PosEnd = $Loc2->PosEnd;
- }
- $Loc->BlockFound = true;
- return $Loc;
- }
- }
- $PosBeg = $Loc2->PosEnd;
- }
- return $this->meth_Misc_Alert($Loc,'a least one tag with parameter \'block=end\' is missing.',false,'in block\'s definition');
- }
- if ($Mode===1) {
- $Loc->PosBeg2 = false;
- } else {
- $beg = $Loc->PosBeg;
- $end = $Loc->PosEnd;
- if ($this->f_Loc_EnlargeToTag($Txt,$Loc,$Block,false)===false) return $this->meth_Misc_Alert($Loc,'at least one tag corresponding to '.$Loc->PrmLst['block'].' is not found. Check opening tags, closing tags and embedding levels.',false,'in block\'s definition');
- if ($Loc->SubOk or ($Mode===3)) {
- $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
- $Loc->PosDefBeg = $beg - $Loc->PosBeg;
- $Loc->PosDefEnd = $end - $Loc->PosBeg;
- } else {
- $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$beg-$Loc->PosBeg).substr($Txt,$end+1,$Loc->PosEnd-$end);
- }
- }
- $Loc->BlockFound = true;
- if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
- return $Loc; // methods return by ref by default
- }
- function meth_Locator_Rename(&$Txt, $Replace) {
- // Rename or delete TBS tags names
- if (is_string($Replace)) $Replace = explode(',',$Replace);
- foreach ($Replace as $x) {
- if (is_string($x)) $x = explode('=', $x);
- if (count($x)==2) {
- $old = trim($x[0]);
- $new = trim($x[1]);
- if ($old!=='') {
- if ($new==='') {
- $q = false;
- $s = 'clear';
- $this->meth_Merge_Block($Txt, $old, $s, $q, false, false);
- } else {
- $old = $this->_ChrOpen.$old;
- $old = array($old.'.', $old.' ', $old.';');
- $new = $this->_ChrOpen.$new;
- $new = array($new.'.', $new.' ', $new.';');
- $Txt = str_replace($old,$new,$Txt);
- }
- }
- }
- }
- }
- function meth_Locator_FindBlockLst(&$Txt,$BlockName,$Pos,$SpePrm) {
- // Return a locator object covering all block definitions, even if there is no block definition found.
- $LocR = &new clsTbsLocator;
- $LocR->P1 = false;
- $LocR->FieldOutside = false;
- $LocR->FOStop = false;
- $LocR->BDefLst = array();
- $LocR->NoData = false;
- $LocR->Special = false;
- $LocR->HeaderFound = false;
- $LocR->FooterFound = false;
- $LocR->SerialEmpty = false;
- $LocR->GrpBreak = false; // Only for plug-ins
- $LocR->WhenFound = false;
- $LocR->WhenDefault = false;
- $LocR->SectionNbr = 0; // Normal sections
- $LocR->SectionLst = array(); // 1 to SectionNbr
- $BDef = false;
- $ParentLst = array();
- $Pid = 0;
- do {
- if ($BlockName==='') {
- $Loc = false;
- } else {
- $Loc = $this->meth_Locator_FindBlockNext($Txt,$BlockName,$Pos,'.',2,$LocR->P1,$LocR->FieldOutside);
- }
- if ($Loc===false) {
- if ($Pid>0) { // parentgrp mode => disconnect $Txt from the source
- $Parent = &$ParentLst[$Pid];
- $Src = $Txt;
- $Txt = &$Parent->Txt;
- if ($LocR->BlockFound) {
- // Redefine the Header block
- $Parent->Src = substr($Src,0,$LocR->PosBeg);
- // Add a Footer block
- $BDef = &$this->meth_Locator_SectionNewBDef($LocR,$BlockName,substr($Src,$LocR->PosEnd+1),$Parent->Prm);
- $this->meth_Locator_SectionAddGrp($LocR,$BlockName,$BDef,'F',$Parent->Fld,'parentgrp');
- }
- // Now go down to previous level
- $Pos = $Parent->Pos;
- $LocR->PosBeg = $Parent->Beg;
- $LocR->PosEnd = $Parent->End;
- $LocR->BlockFound = true;
- unset($Parent);
- unset($ParentLst[$Pid]);
- $Pid--;
- $Loc = true;
- }
- } else {
- $Pos = $Loc->PosEnd;
- // Define the block limits
- if ($LocR->BlockFound) {
- if ( $LocR->PosBeg > $Loc->PosBeg ) $LocR->PosBeg = $Loc->PosBeg;
- if ( $LocR->PosEnd < $Loc->PosEnd ) $LocR->PosEnd = $Loc->PosEnd;
- } else {
- $LocR->BlockFound = true;
- $LocR->PosBeg = $Loc->PosBeg;
- $LocR->PosEnd = $Loc->PosEnd;
- }
- // Merge block parameters
- if (count($Loc->PrmLst)>0) $LocR->PrmLst = array_merge($LocR->PrmLst,$Loc->PrmLst);
- // Force dynamic parameter to be cachable
- if ($Loc->PosDefBeg>=0) {
- $dynprm = array('when','headergrp','footergrp','parentgrp');
- foreach($dynprm as $dp) {
- $n = 0;
- if ((isset($Loc->PrmLst[$dp])) and (strpos($Loc->PrmLst[$dp],$this->_ChrOpen.$BlockName)!==false)) {
- $n++;
- if ($n==1) {
- $len = $Loc->PosDefEnd - $Loc->PosDefBeg + 1;
- $x = substr($Loc->BlockSrc,$Loc->PosDefBe…
Large files files are truncated, but you can click here to view the full file