PageRenderTime 80ms CodeModel.GetById 33ms RepoModel.GetById 0ms 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
  1. <?php
  2. /*
  3. ********************************************************
  4. TinyButStrong - Template Engine for Pro and Beginners
  5. ------------------------
  6. Version : 3.1.1 for PHP >= 5.0
  7. Date : 2006-06-25
  8. Web site : www.tinybutstrong.com
  9. Author : skrol29@freesurf.fr
  10. ********************************************************
  11. This library is free software.
  12. You can redistribute and modify it even for commercial usage,
  13. but you must accept and respect the LPGL License version 2.1.
  14. */
  15. // Check PHP version
  16. if (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.';
  17. if (PHP_VERSION<'5.1.0') {
  18. function property_exists(&$obj,$prop) {return true;}
  19. }
  20. // Render flags
  21. define('TBS_NOTHING', 0);
  22. define('TBS_OUTPUT', 1);
  23. define('TBS_EXIT', 2);
  24. // Plug-ins actions
  25. define('TBS_INSTALL', -1);
  26. define('TBS_ISINSTALLED', -3);
  27. // *********************************************
  28. class clsTbsLocator {
  29. public $PosBeg = false;
  30. public $PosEnd = false;
  31. public $Enlarged = false;
  32. public $FullName = false;
  33. public $SubName = '';
  34. public $SubOk = false;
  35. public $SubLst = array();
  36. public $SubNbr = 0;
  37. public $PrmLst = array();
  38. public $PrmIfNbr = false;
  39. public $MagnetId = false;
  40. public $BlockFound = false;
  41. public $FirstMerge = true;
  42. public $ConvProtect = true;
  43. public $ConvHtml = true;
  44. public $ConvMode = 1; // Normal
  45. public $ConvBr = true;
  46. }
  47. // *********************************************
  48. class clsTbsDataSource {
  49. public $Type = false;
  50. public $SubType = 0;
  51. public $SrcId = false;
  52. public $Query = '';
  53. public $RecSet = false;
  54. public $RecKey = '';
  55. public $RecNum = 0;
  56. public $RecNumInit = 0;
  57. public $RecSaving = false;
  58. public $RecSaved = false;
  59. public $RecBuffer = false;
  60. public $CurrRec = false;
  61. public $TBS = false;
  62. public $OnDataOk = false;
  63. public $OnDataPrm = false;
  64. public $OnDataPrmDone = array();
  65. public $OnDataPi = false;
  66. function DataAlert($Msg) {
  67. return $this->TBS->meth_Misc_Alert('when merging block '.$this->TBS->_ChrOpen.$this->TBS->_CurrBlock.$this->TBS->_ChrClose,$Msg);
  68. }
  69. function DataPrepare(&$SrcId,&$TBS) {
  70. $this->SrcId =& $SrcId;
  71. $this->TBS =& $TBS;
  72. if (is_array($SrcId)) {
  73. $this->Type = 0;
  74. } elseif (is_resource($SrcId)) {
  75. $Key = get_resource_type($SrcId);
  76. switch ($Key) {
  77. case 'mysql link' : $this->Type = 1; break;
  78. case 'mysql link persistent' : $this->Type = 1; break;
  79. case 'mysql result' : $this->Type = 1; $this->SubType = 1; break;
  80. case 'pgsql link' : $this->Type = 8; break;
  81. case 'pgsql link persistent' : $this->Type = 8; break;
  82. case 'pgsql result' : $this->Type = 8; $this->SubType = 1; break;
  83. case 'sqlite database' : $this->Type = 9; break;
  84. case 'sqlite database (persistent)' : $this->Type = 9; break;
  85. case 'sqlite result' : $this->Type = 9; $this->SubType = 1; break;
  86. default :
  87. $SubKey = 'resource type';
  88. $this->Type = 7;
  89. $x = strtolower($Key);
  90. $x = str_replace('-','_',$x);
  91. $Function = '';
  92. $i = 0;
  93. $iMax = strlen($x);
  94. while ($i<$iMax) {
  95. if (($x[$i]==='_') or (($x[$i]>='a') and ($x[$i]<='z')) or (($x[$i]>='0') and ($x[$i]<='9'))) {
  96. $Function .= $x[$i];
  97. $i++;
  98. } else {
  99. $i = $iMax;
  100. }
  101. }
  102. }
  103. } elseif (is_string($SrcId)) {
  104. switch (strtolower($SrcId)) {
  105. case 'array' : $this->Type = 0; $this->SubType = 1; break;
  106. case 'clear' : $this->Type = 0; $this->SubType = 3; break;
  107. case 'mysql' : $this->Type = 1; $this->SubType = 2; break;
  108. case 'text' : $this->Type = 4; break;
  109. case 'num' : $this->Type = 6; break;
  110. default :
  111. if ($SrcId[0]==='~') {
  112. $FctInfo = $SrcId;
  113. $ErrMsg = false;
  114. if ($TBS->meth_Misc_UserFctCheck($FctInfo,$ErrMsg,true)) {
  115. $this->FctOpen =& $FctInfo[0];
  116. $this->FctFetch =& $FctInfo[1];
  117. $this->FctClose =& $FctInfo[2];
  118. $this->FctPrm = array(false,0);
  119. $this->SrcId =& $FctInfo[0][0];
  120. $this->Type = 11;
  121. } else {
  122. $this->Type = $this->DataAlert($ErrMsg);
  123. }
  124. } else {
  125. $Key = $SrcId;
  126. $SubKey = 'keyword';
  127. $this->Type = 7;
  128. $Function = $SrcId;
  129. }
  130. }
  131. } elseif (is_object($SrcId)) {
  132. if (method_exists($SrcId,'tbsdb_open')) {
  133. if (!method_exists($SrcId,'tbsdb_fetch')) {
  134. $this->Type = $this->DataAlert('the expected method \'tbsdb_fetch\' is not found for the class '.get_class($SrcId).'.');
  135. } elseif (!method_exists($SrcId,'tbsdb_close')) {
  136. $this->Type = $this->DataAlert('the expected method \'tbsdb_close\' is not found for the class '.get_class($SrcId).'.');
  137. } else {
  138. $this->Type = 10;
  139. }
  140. } else {
  141. $Key = get_class($SrcId);
  142. $SubKey = 'object type';
  143. $this->Type = 7;
  144. $Function = $Key;
  145. }
  146. } elseif ($SrcId===false) {
  147. $this->DataAlert('the specified source is set to FALSE. Maybe your connection has failed.');
  148. } else {
  149. $this->DataAlert('unsupported variable type : \''.gettype($SrcId).'\'.');
  150. }
  151. if ($this->Type===7) {
  152. $this->FctOpen = 'tbsdb_'.$Function.'_open';
  153. $Ok = function_exists($this->FctOpen);
  154. if (!$Ok) { // Some extended call can have a suffix in the class name, we check without the suffix
  155. $i = strpos($Function,'_');
  156. if ($i!==false) {
  157. $x = substr($Function,0,$i);
  158. $z = 'tbsdb_'.$x.'_open';
  159. $Ok = function_exists($z);
  160. if ($Ok) {
  161. $Function = $x;
  162. $this->FctOpen = $z;
  163. }
  164. }
  165. }
  166. if ($Ok) {
  167. $this->FctFetch = 'tbsdb_'.$Function.'_fetch';
  168. $this->FctClose = 'tbsdb_'.$Function.'_close';
  169. if (function_exists($this->FctFetch)) {
  170. if (!function_exists($this->FctClose)) $this->Type = $this->DataAlert('the expected custom function \''.$this->FctClose.'\' is not found.');
  171. } else {
  172. $this->Type = $this->DataAlert('the expected custom function \''.$this->FctFetch.'\' is not found.');
  173. }
  174. } else {
  175. $this->Type = $this->DataAlert('the data source Id \''.$Key.'\' is an unsupported '.$SubKey.' because custom function \''.$this->FctOpen.'\' is not found.');
  176. }
  177. }
  178. return ($this->Type!==false);
  179. }
  180. function DataOpen(&$Query) {
  181. // Init values
  182. unset($this->CurrRec); $this->CurrRec = true;
  183. if ($this->RecSaved) {
  184. $this->FirstRec = true;
  185. unset($this->RecKey); $this->RecKey = '';
  186. $this->RecNum = $this->RecNumInit;
  187. if ($this->OnDataOk) $this->OnDataArgs[1] =& $this->CurrRec;
  188. return true;
  189. }
  190. unset($this->RecSet); $this->RecSet = false;
  191. $this->RecNumInit = 0;
  192. $this->RecNum = 0;
  193. if (isset($this->TBS->_piOnData)) {
  194. $this->OnDataPi = true;
  195. $this->OnDataPiRef =& $this->TBS->_piOnData;
  196. $this->OnDataOk = true;
  197. }
  198. if ($this->OnDataOk) {
  199. $this->OnDataArgs = array();
  200. $this->OnDataArgs[0] =& $this->TBS->_CurrBlock;
  201. $this->OnDataArgs[1] =& $this->CurrRec;
  202. $this->OnDataArgs[2] =& $this->RecNum;
  203. $this->OnDataArgs[3] =& $this->TBS;
  204. }
  205. switch ($this->Type) {
  206. case 0: // Array
  207. if (($this->SubType===1) and (is_string($Query))) $this->SubType = 2;
  208. if ($this->SubType===0) {
  209. $this->RecSet =& $this->SrcId;
  210. } elseif ($this->SubType===1) {
  211. if (is_array($Query)) {
  212. $this->RecSet =& $Query;
  213. } else {
  214. $this->DataAlert('type \''.gettype($Query).'\' not supported for the Query Parameter going with \'array\' Source Type.');
  215. }
  216. } elseif ($this->SubType===2) {
  217. // TBS query string for array and objects, syntax: "var[item1][item2]->item3[item4]..."
  218. $x = trim($Query);
  219. $z = chr(0);
  220. $x = str_replace(']->',$z,$x);
  221. $x = str_replace('][',$z,$x);
  222. $x = str_replace('->',$z,$x);
  223. $x = str_replace('[',$z,$x);
  224. if (substr($x,strlen($x)-1,1)===']') $x = substr($x,0,strlen($x)-1);
  225. $ItemLst = explode($z,$x);
  226. $ItemNbr = count($ItemLst);
  227. $Item0 =& $ItemLst[0];
  228. // Check first item
  229. if ($Item0[0]==='~') {
  230. $Item0 = substr($Item0,1);
  231. if ($this->TBS->ObjectRef!==false) {
  232. $Var =& $this->TBS->ObjectRef;
  233. $i = 0;
  234. } else {
  235. $i = $this->DataAlert('invalid query \''.$Query.'\' because property ObjectRef is not set.');
  236. }
  237. } else {
  238. if (isset($GLOBALS[$Item0])) {
  239. $Var =& $GLOBALS[$Item0];
  240. $i = 1;
  241. } else {
  242. $i = $this->DataAlert('invalid query \''.$Query.'\' because global variable \''.$Item0.'\' is not found.');
  243. }
  244. }
  245. // Check sub-items
  246. $Empty = false;
  247. while (($i!==false) and ($i<$ItemNbr) and ($Empty===false)) {
  248. $x = $ItemLst[$i];
  249. if (is_array($Var)) {
  250. if (isset($Var[$x])) {
  251. $Var =& $Var[$x];
  252. } else {
  253. $Empty = true;
  254. }
  255. } elseif (is_object($Var)) {
  256. $ArgLst = tbs_Misc_CheckArgLst($x);
  257. if (method_exists($Var,$x)) {
  258. $f = array(&$Var,$x); unset($Var);
  259. $Var = call_user_func_array($f,$ArgLst);
  260. } elseif (isset($Var->$x)) {
  261. $Var =& $Var->$x;
  262. } else {
  263. $Empty = true;
  264. }
  265. } else {
  266. $i = $this->DataAlert('invalid query \''.$Query.'\' because item \''.$ItemLst[$i].'\' is neither an Array nor an Object. Its type is \''.gettype($Var).'\'.');
  267. }
  268. if ($i!==false) $i++;
  269. }
  270. // Assign data
  271. if ($i!==false) {
  272. if ($Empty) {
  273. $this->RecSet = array();
  274. } else {
  275. $this->RecSet =& $Var;
  276. }
  277. }
  278. } elseif ($this->SubType===3) { // Clear
  279. $this->RecSet = array();
  280. }
  281. // First record
  282. if ($this->RecSet!==false) {
  283. $this->RecNbr = $this->RecNumInit + count($this->RecSet);
  284. $this->FirstRec = true;
  285. $this->RecSaved = true;
  286. $this->RecSaving = false;
  287. }
  288. break;
  289. case 1: // MySQL
  290. switch ($this->SubType) {
  291. case 0: $this->RecSet = @mysql_query($Query,$this->SrcId); break;
  292. case 1: $this->RecSet = $this->SrcId; break;
  293. case 2: $this->RecSet = @mysql_query($Query); break;
  294. }
  295. if ($this->RecSet===false) $this->DataAlert('MySql error message when opening the query: '.mysql_error());
  296. break;
  297. case 4: // Text
  298. if (is_string($Query)) {
  299. $this->RecSet =& $Query;
  300. } else {
  301. $this->RecSet = ''.$Query;
  302. }
  303. break;
  304. case 6: // Num
  305. $this->RecSet = true;
  306. $this->NumMin = 1;
  307. $this->NumMax = 1;
  308. $this->NumStep = 1;
  309. if (is_array($Query)) {
  310. if (isset($Query['min'])) $this->NumMin = $Query['min'];
  311. if (isset($Query['step'])) $this->NumStep = $Query['step'];
  312. if (isset($Query['max'])) {
  313. $this->NumMax = $Query['max'];
  314. } else {
  315. $this->RecSet = $this->DataAlert('the \'num\' source is an array that has no value for the \'max\' key.');
  316. }
  317. if ($this->NumStep==0) $this->RecSet = $this->DataAlert('the \'num\' source is an array that has a step value set to zero.');
  318. } else {
  319. $this->NumMax = ceil($Query);
  320. }
  321. if ($this->RecSet) {
  322. if ($this->NumStep>0) {
  323. $this->NumVal = $this->NumMin;
  324. } else {
  325. $this->NumVal = $this->NumMax;
  326. }
  327. }
  328. break;
  329. case 7: // Custom function
  330. $FctOpen = $this->FctOpen;
  331. $this->RecSet = $FctOpen($this->SrcId,$Query);
  332. break;
  333. case 8: // PostgreSQL
  334. switch ($this->SubType) {
  335. case 0: $this->RecSet = @pg_query($this->SrcId,$Query); break;
  336. case 1: $this->RecSet = $this->SrcId; break;
  337. }
  338. if ($this->RecSet===false) $this->DataAlert('PostgreSQL error message when opening the query: '.pg_last_error($this->SrcId));
  339. break;
  340. case 9: // SQLite
  341. switch ($this->SubType) {
  342. case 0: $this->RecSet = @sqlite_query($this->SrcId,$Query); break;
  343. case 1: $this->RecSet = $this->SrcId; break;
  344. }
  345. if ($this->RecSet===false) $this->DataAlert('SQLite error message when opening the query:'.sqlite_error_string(sqlite_last_error($this->SrcId)));
  346. break;
  347. case 10: // Custom method
  348. $this->RecSet = $this->SrcId->tbsdb_open($this->SrcId,$Query);
  349. break;
  350. case 11: // ObjectRef
  351. $this->RecSet = call_user_func_array($this->FctOpen,array(&$this->SrcId,&$Query));
  352. break;
  353. }
  354. if ($this->Type===0) {
  355. unset($this->RecKey); $this->RecKey = '';
  356. } else {
  357. if ($this->RecSaving) {
  358. unset($this->RecBuffer); $this->RecBuffer = array();
  359. }
  360. $this->RecKey =& $this->RecNum; // Not array: RecKey = RecNum
  361. }
  362. return ($this->RecSet!==false);
  363. }
  364. function DataFetch() {
  365. if ($this->RecSaved) {
  366. if ($this->RecNum<$this->RecNbr) {
  367. if ($this->FirstRec) {
  368. if ($this->SubType===2) { // From string
  369. reset($this->RecSet);
  370. $this->RecKey = key($this->RecSet);
  371. $this->CurrRec =& $this->RecSet[$this->RecKey];
  372. } else {
  373. $this->CurrRec = reset($this->RecSet);
  374. $this->RecKey = key($this->RecSet);
  375. }
  376. $this->FirstRec = false;
  377. } else {
  378. if ($this->SubType===2) { // From string
  379. next($this->RecSet);
  380. $this->RecKey = key($this->RecSet);
  381. $this->CurrRec =& $this->RecSet[$this->RecKey];
  382. } else {
  383. $this->CurrRec = next($this->RecSet);
  384. $this->RecKey = key($this->RecSet);
  385. }
  386. }
  387. if ((!is_array($this->CurrRec)) and (!is_object($this->CurrRec))) $this->CurrRec = array('key'=>$this->RecKey, 'val'=>$this->CurrRec);
  388. $this->RecNum++;
  389. if ($this->OnDataOk) {
  390. if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
  391. if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
  392. if ($this->SubType!==2) $this->RecSet[$this->RecKey] = $this->CurrRec; // save modifications because array reading is done without reference :(
  393. }
  394. } else {
  395. unset($this->CurrRec); $this->CurrRec = false;
  396. }
  397. return;
  398. }
  399. switch ($this->Type) {
  400. case 1: // MySQL
  401. $this->CurrRec = mysql_fetch_assoc($this->RecSet);
  402. break;
  403. case 4: // Text
  404. if ($this->RecNum===0) {
  405. if ($this->RecSet==='') {
  406. $this->CurrRec = false;
  407. } else {
  408. $this->CurrRec =& $this->RecSet;
  409. }
  410. } else {
  411. $this->CurrRec = false;
  412. }
  413. break;
  414. case 6: // Num
  415. if (($this->NumVal>=$this->NumMin) and ($this->NumVal<=$this->NumMax)) {
  416. $this->CurrRec = array('val'=>$this->NumVal);
  417. $this->NumVal += $this->NumStep;
  418. } else {
  419. $this->CurrRec = false;
  420. }
  421. break;
  422. case 7: // Custom function
  423. $FctFetch = $this->FctFetch;
  424. $this->CurrRec = $FctFetch($this->RecSet,$this->RecNum+1);
  425. break;
  426. case 8: // PostgreSQL
  427. $this->CurrRec = pg_fetch_assoc($this->RecSet);
  428. break;
  429. case 9: // SQLite
  430. $this->CurrRec = sqlite_fetch_array($this->RecSet,SQLITE_ASSOC);
  431. break;
  432. case 10: // Custom method
  433. $this->CurrRec = $this->SrcId->tbsdb_fetch($this->RecSet,$this->RecNum+1);
  434. break;
  435. case 11: // ObjectRef
  436. $this->FctPrm[0] =& $this->RecSet; $this->FctPrm[1] = $this->RecNum+1;
  437. $this->CurrRec = call_user_func_array($this->FctFetch,$this->FctPrm);
  438. break;
  439. }
  440. // Set the row count
  441. if ($this->CurrRec!==false) {
  442. $this->RecNum++;
  443. if ($this->OnDataOk) {
  444. $this->OnDataArgs[1] =& $this->CurrRec; // Reference has changed if ($this->SubType===2)
  445. if ($this->OnDataPrm) call_user_func_array($this->OnDataPrmRef,$this->OnDataArgs);
  446. if ($this->OnDataPi) $this->TBS->meth_PlugIn_RunAll($this->OnDataPiRef,$this->OnDataArgs);
  447. }
  448. if ($this->RecSaving) $this->RecBuffer[$this->RecKey] = $this->CurrRec;
  449. }
  450. }
  451. function DataClose() {
  452. $this->OnDataOk = false;
  453. $this->OnDataPrm = false;
  454. $this->OnDataPi = false;
  455. if ($this->RecSaved) return;
  456. switch ($this->Type) {
  457. case 1: mysql_free_result($this->RecSet); break;
  458. case 7: $FctClose=$this->FctClose; $FctClose($this->RecSet); break;
  459. case 8: pg_free_result($this->RecSet); break;
  460. case 10: $this->SrcId->tbsdb_close($this->RecSet); break;
  461. case 11: call_user_func_array($this->FctClose,array(&$this->RecSet)); break;
  462. }
  463. if ($this->RecSaving) {
  464. $this->RecSet =& $this->RecBuffer;
  465. $this->RecNbr = $this->RecNumInit + count($this->RecSet);
  466. $this->RecSaving = false;
  467. $this->RecSaved = true;
  468. }
  469. }
  470. }
  471. // *********************************************
  472. class clsTinyButStrong {
  473. // Public properties
  474. public $Source = '';
  475. public $Render = 3;
  476. public $TplVars = array();
  477. public $ObjectRef = false;
  478. public $NoErr = false;
  479. // Undocumented (can change at any version)
  480. public $Version = '3.1.1';
  481. public $HtmlCharSet = '';
  482. public $TurboBlock = true;
  483. public $VarPrefix = '';
  484. public $Protect = true;
  485. // Private
  486. public $_LastFile = '';
  487. public $_HtmlCharFct = false;
  488. public $_Mode = 0;
  489. public $_CurrBlock = '';
  490. public $_ChrOpen = '[';
  491. public $_ChrClose = ']';
  492. public $_ChrVal = '[val]';
  493. public $_ChrProtect = '&#91;';
  494. public $_PlugIns = array();
  495. public $_PlugIns_Ok = false;
  496. public $_piOnFrm_Ok = false;
  497. function clsTinyButStrong($Chrs='',$VarPrefix='') {
  498. if ($Chrs!=='') {
  499. $Ok = false;
  500. $Len = strlen($Chrs);
  501. if ($Len===2) { // For compatibility
  502. $this->_ChrOpen = $Chrs[0];
  503. $this->_ChrClose = $Chrs[1];
  504. $Ok = true;
  505. } else {
  506. $Pos = strpos($Chrs,',');
  507. if (($Pos!==false) and ($Pos>0) and ($Pos<$Len-1)) {
  508. $this->_ChrOpen = substr($Chrs,0,$Pos);
  509. $this->_ChrClose = substr($Chrs,$Pos+1);
  510. $Ok = true;
  511. }
  512. }
  513. if ($Ok) {
  514. $this->_ChrVal = $this->_ChrOpen.'val'.$this->_ChrClose;
  515. $this->_ChrProtect = '&#'.ord($this->_ChrOpen[0]).';'.substr($this->_ChrOpen,1);
  516. } else {
  517. $this->meth_Misc_Alert('with clsTinyButStrong() function','value \''.$Chrs.'\' is a bad tag delimitor definition.');
  518. }
  519. }
  520. $this->VarPrefix = $VarPrefix;
  521. // Liaison avec variables globales
  522. global $_TBS_FrmMultiLst, $_TBS_FrmSimpleLst, $_TBS_UserFctLst, $_TBS_AutoInstallPlugIns;
  523. if (!isset($_TBS_ObjectRefLink)) {
  524. $_TBS_FrmMultiLst = array();
  525. $_TBS_FrmSimpleLst = array();
  526. $_TBS_UserFctLst = array();
  527. }
  528. $this->_FrmMultiLst =& $_TBS_FrmMultiLst;
  529. $this->_FrmSimpleLst =& $_TBS_FrmSimpleLst;
  530. $this->_UserFctLst =& $_TBS_UserFctLst;
  531. // Auto-installing plug-ins
  532. if (isset($_TBS_AutoInstallPlugIns)) foreach ($_TBS_AutoInstallPlugIns as $pi) $this->PlugIn(TBS_INSTALL,$pi);
  533. }
  534. // Public methods
  535. function LoadTemplate($File,$HtmlCharSet='') {
  536. $Ok = true;
  537. if ($this->_PlugIns_Ok) {
  538. if (isset($this->_piBeforeLoadTemplate) or isset($this->_piAfterLoadTemplate)) {
  539. // Plug-ins
  540. $ArgLst = func_get_args();
  541. $ArgLst[0] =& $File;
  542. $ArgLst[1] =& $HtmlCharSet;
  543. if (isset($this->_piBeforeLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeLoadTemplate,$ArgLst);
  544. }
  545. }
  546. // Load the file
  547. if ($Ok!==false) {
  548. $x = '';
  549. if (!tbs_Misc_GetFile($x,$File)) return $this->meth_Misc_Alert('with LoadTemplate() method','file \''.$File.'\' is not found or not readable.');
  550. // CharSet analysis
  551. if ($HtmlCharSet==='+') {
  552. $this->Source .= $x;
  553. } else {
  554. $this->Source = $x;
  555. if ($this->_Mode==0) {
  556. $this->_LastFile = $File;
  557. $this->_HtmlCharFct = false;
  558. $this->TplVars = array();
  559. if (is_string($HtmlCharSet)) {
  560. if (($HtmlCharSet!=='') and ($HtmlCharSet[0]==='=')) {
  561. $ErrMsg = false;
  562. $HtmlCharSet = substr($HtmlCharSet,1);
  563. if ($this->meth_Misc_UserFctCheck($HtmlCharSet,$ErrMsg,false)) {
  564. $this->_HtmlCharFct = true;
  565. } else {
  566. $this->meth_Misc_Alert('with LoadTemplate() method',$ErrMsg);
  567. $HtmlCharSet = '';
  568. }
  569. }
  570. } elseif ($HtmlCharSet===false) {
  571. $this->Protect = false;
  572. } else {
  573. $this->meth_Misc_Alert('with LoadTemplate() method','the CharSet argument is not a string.');
  574. $HtmlCharSet = '';
  575. }
  576. $this->HtmlCharSet = $HtmlCharSet;
  577. }
  578. }
  579. // Automatic fields and blocks
  580. $this->meth_Merge_AutoOn($this->Source,'onload',true,true);
  581. }
  582. // Plug-ins
  583. if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterLoadTemplate)) $Ok = $this->meth_PlugIn_RunAll($this->_piAfterLoadTemplate,$ArgLst);
  584. return $Ok;
  585. }
  586. function GetBlockSource($BlockName,$List=false,$KeepDefTags=true) {
  587. $RetVal = array();
  588. $Nbr = 0;
  589. $Pos = 0;
  590. $FieldOutside = false;
  591. $P1 = false;
  592. $Mode = ($KeepDefTags) ? 3 : 2;
  593. while ($Loc = $this->meth_Locator_FindBlockNext($this->Source,$BlockName,$Pos,'.',$Mode,$P1,$FieldOutside)) {
  594. $P1 = false;
  595. $Nbr++;
  596. $RetVal[$Nbr] = $Loc->BlockSrc;
  597. if (!$List) return $RetVal[$Nbr];
  598. $Pos = $Loc->PosEnd;
  599. }
  600. if ($List) {
  601. return $RetVal;
  602. } else {
  603. return false;
  604. }
  605. }
  606. function MergeBlock($BlockLst,$SrcId,$Query='') {
  607. if ($SrcId==='cond') {
  608. $Nbr = 0;
  609. $BlockLst = explode(',',$BlockLst);
  610. foreach ($BlockLst as $Block) {
  611. $Block = trim($Block);
  612. if ($Block!=='') $Nbr += $this->meth_Merge_AutoOn($this->Source,$Block,false,false);
  613. }
  614. return $Nbr;
  615. } else {
  616. return $this->meth_Merge_Block($this->Source,$BlockLst,$SrcId,$Query,false,0);
  617. }
  618. }
  619. function MergeField($NameLst,$Value=null,$IsUserFct=false) {
  620. $FctCheck = $IsUserFct;
  621. if ($PlugIn = isset($this->_piOnMergeField)) $ArgPi = array('','',&$Value,0,&$this->Source,0,0);
  622. $SubStart = 0;
  623. $Ok = true;
  624. $NameLst = explode(',',$NameLst);
  625. foreach ($NameLst as $Name) {
  626. $Name = trim($Name);
  627. if ($Name==='') continue;
  628. if ($this->meth_Merge_AutoAny($Name)) continue;
  629. if ($PlugIn) $ArgPi[0] = $Name;
  630. $PosBeg = 0;
  631. // Initilize the user function (only once)
  632. if ($FctCheck) {
  633. $FctInfo = $Value;
  634. $ErrMsg = false;
  635. if (!$this->meth_Misc_UserFctCheck($FctInfo,$ErrMsg,false)) return $this->meth_Misc_Alert('with MergeField() method',$ErrMsg);
  636. $FctArg = array('','');
  637. $SubStart = false;
  638. $FctCheck = false;
  639. }
  640. while ($Loc = $this->meth_Locator_FindTbs($this->Source,$Name,$PosBeg,'.')) {
  641. // Apply user function
  642. if ($IsUserFct) {
  643. $FctArg[0] =& $Loc->SubName; $FctArg[1] =& $Loc->PrmLst;
  644. $Value = call_user_func_array($FctInfo,$FctArg);
  645. }
  646. // Plug-ins
  647. if ($PlugIn) {
  648. $ArgPi[1] = $Loc->SubName; $ArgPi[3] =& $Loc->PrmLst; $ArgPi[5] =& $Loc->PosBeg; $ArgPi[6] =& $Loc->PosEnd;
  649. $Ok = $this->meth_PlugIn_RunAll($this->_piOnMergeField,$ArgPi);
  650. }
  651. // Merge the field
  652. if ($Ok) {
  653. $PosBeg = $this->meth_Locator_Replace($this->Source,$Loc,$Value,$SubStart);
  654. } else {
  655. $PosBeg = $Loc->PosEnd;
  656. }
  657. }
  658. }
  659. }
  660. function Show($Render=false) {
  661. $Ok = true;
  662. if ($Render===false) $Render = $this->Render;
  663. if ($this->_PlugIns_Ok) {
  664. if (isset($this->_piBeforeShow) or isset($this->_piAfterShow)) {
  665. // Plug-ins
  666. $ArgLst = func_get_args();
  667. $ArgLst[0] =& $Render;
  668. if (isset($this->_piBeforeShow)) $Ok = $this->meth_PlugIn_RunAll($this->_piBeforeShow,$ArgLst);
  669. }
  670. }
  671. if ($Ok!==false) {
  672. $this->meth_Merge_AutoAny('onshow');
  673. $this->meth_Merge_AutoAny('var');
  674. }
  675. if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterShow)) $this->meth_PlugIn_RunAll($this->_piAfterShow,$ArgLst);
  676. if (($Render & TBS_OUTPUT)==TBS_OUTPUT) echo $this->Source;
  677. if (($this->_Mode==0) and (($Render & TBS_EXIT)==TBS_EXIT)) exit;
  678. return $Ok;
  679. }
  680. function PlugIn($Prm1,$Prm2=0) {
  681. if (is_numeric($Prm1)) {
  682. switch ($Prm1) {
  683. case TBS_INSTALL:
  684. $PlugInId = $Prm2;
  685. // Try to install the plug-in
  686. if (isset($this->_PlugIns[$PlugInId])) {
  687. return $this->meth_Misc_Alert('with PlugIn() method','plug-in \''.$PlugInId.'\' is already installed.');
  688. } else {
  689. $ArgLst = func_get_args();
  690. array_shift($ArgLst); array_shift($ArgLst);
  691. return $this->meth_PlugIn_Install($PlugInId,$ArgLst,false);
  692. }
  693. case TBS_ISINSTALLED:
  694. // Check if the plug-in is installed
  695. return isset($this->_PlugIns[$Prm2]);
  696. case -4: // Deactivate special plug-ins
  697. $this->_PlugIns_Ok_save = $this->_PlugIns_Ok;
  698. $this->_PlugIns_Ok = false;
  699. return true;
  700. case -5: // Deactivate OnFormat
  701. $this->_piOnFrm_Ok_save = $this->_piOnFrm_Ok;
  702. $this->_piOnFrm_Ok = false;
  703. return true;
  704. case -10: // Restore
  705. $this->_PlugIns_Ok = $this->_PlugIns_Ok_save;
  706. $this->_piOnFrm_Ok = $this->_piOnFrm_Ok_save;
  707. return true;
  708. }
  709. } elseif (is_string($Prm1)) {
  710. // Plug-in's command
  711. $PlugInId = $Prm1;
  712. if (!isset($this->_PlugIns[$PlugInId])) {
  713. if (!$this->meth_PlugIn_Install($PlugInId,array(),true)) return false;
  714. }
  715. 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.');
  716. $ArgLst = func_get_args();
  717. array_shift($ArgLst);
  718. $Ok = call_user_func_array($this->_piOnCommand[$PlugInId],$ArgLst);
  719. if (is_null($Ok)) $Ok = true;
  720. return $Ok;
  721. }
  722. return $this->meth_Misc_Alert('with PlugIn() method','\''.$Prm1.'\' is an invalid plug-in key, the type of the value is \''.gettype($Prm1).'\'.');
  723. }
  724. // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  725. function meth_Locator_FindTbs(&$Txt,$Name,$Pos,$ChrSub) {
  726. // Find a TBS Locator
  727. $PosEnd = false;
  728. $PosMax = strlen($Txt) -1;
  729. $Start = $this->_ChrOpen.$Name;
  730. do {
  731. // Search for the opening char
  732. if ($Pos>$PosMax) return false;
  733. $Pos = strpos($Txt,$Start,$Pos);
  734. // If found => next chars are analyzed
  735. if ($Pos===false) {
  736. return false;
  737. } else {
  738. $Loc = new clsTbsLocator;
  739. $ReadPrm = false;
  740. $PosX = $Pos + strlen($Start);
  741. $x = $Txt[$PosX];
  742. if ($x===$this->_ChrClose) {
  743. $PosEnd = $PosX;
  744. } elseif ($x===$ChrSub) {
  745. $Loc->SubOk = true; // it is no longer the false value
  746. $ReadPrm = true;
  747. $PosX++;
  748. } elseif (strpos(';',$x)!==false) {
  749. $ReadPrm = true;
  750. $PosX++;
  751. } else {
  752. $Pos++;
  753. }
  754. if ($ReadPrm) {
  755. tbs_Locator_PrmRead($Txt,$PosX,false,'\'',$this->_ChrOpen,$this->_ChrClose,$Loc,$PosEnd);
  756. if ($PosEnd===false) {
  757. $this->meth_Misc_Alert('','can\'t found the end of the tag \''.substr($Txt,$Pos,$PosX-$Pos+10).'...\'.');
  758. $Pos++;
  759. }
  760. }
  761. }
  762. } while ($PosEnd===false);
  763. $Loc->PosBeg = $Pos;
  764. $Loc->PosEnd = $PosEnd;
  765. if ($Loc->SubOk) {
  766. $Loc->FullName = $Name.'.'.$Loc->SubName;
  767. $Loc->SubLst = explode('.',$Loc->SubName);
  768. $Loc->SubNbr = count($Loc->SubLst);
  769. } else {
  770. $Loc->FullName = $Name;
  771. }
  772. if ($ReadPrm and isset($Loc->PrmLst['comm'])) {
  773. $Loc->PosBeg0 = $Loc->PosBeg;
  774. $Loc->PosEnd0 = $Loc->PosEnd;
  775. $comm = $Loc->PrmLst['comm'];
  776. if (($comm===true) or ($comm==='')) {
  777. $Loc->Enlarged = tbs_Locator_EnlargeToStr($Txt,$Loc,'<!--' ,'-->');
  778. } else {
  779. $Loc->Enlarged = tbs_Locator_EnlargeToTag($Txt,$Loc,$comm,false);
  780. }
  781. }
  782. return $Loc;
  783. }
  784. // Search and cache TBS locators founded in $Txt.
  785. function meth_Locator_SectionCache(&$LocR,$Bid) {
  786. $LocR->BlockChk[$Bid] = false;
  787. $LocLst =& $LocR->BlockLoc[$Bid];
  788. $Txt =& $LocR->BlockSrc[$Bid];
  789. $BlockName =& $LocR->BlockName[$Bid];
  790. $Pos = 0;
  791. $PrevEnd = -1;
  792. $Nbr = 0;
  793. while ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$Pos,'.')) {
  794. if (($Loc->SubName==='#') or ($Loc->SubName==='$')) {
  795. $Loc->IsRecInfo = true;
  796. $Loc->RecInfo = $Loc->SubName;
  797. $Loc->SubName = '';
  798. } else {
  799. $Loc->IsRecInfo = false;
  800. }
  801. if ($Loc->PosBeg>$PrevEnd) {
  802. // The previous tag is not embeding => increment
  803. $Nbr++;
  804. } else {
  805. // The previous tag is embeding => no increment, then previous is over writed
  806. $LocR->BlockChk[$Bid] = true;
  807. }
  808. $PrevEnd = $Loc->PosEnd;
  809. if ($Loc->Enlarged) { // Parameter 'comm'
  810. $Pos = $Loc->PosBeg0+1;
  811. $Loc->Enlarged = false;
  812. } else {
  813. $Pos = $Loc->PosBeg+1;
  814. }
  815. $LocLst[$Nbr] = $Loc;
  816. }
  817. $LocLst[0] = $Nbr;
  818. }
  819. function meth_Locator_Replace(&$Txt,&$Loc,&$Value,$SubStart) {
  820. // This function enables to merge a locator with a text and returns the position just after the replaced block
  821. // This position can be useful because we don't know in advance how $Value will be replaced.
  822. // Found the value if there is a subname
  823. if (($SubStart!==false) and $Loc->SubOk) {
  824. for ($i=$SubStart;$i<$Loc->SubNbr;$i++) {
  825. $x = $Loc->SubLst[$i]; // &$Loc... brings an error with Event Example, I don't know why.
  826. if (is_array($Value)) {
  827. if (isset($Value[$x])) {
  828. $Value =& $Value[$x];
  829. } elseif (array_key_exists($x,$Value)) {// can happens when value is NULL
  830. $Value =& $Value[$x];
  831. } else {
  832. if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'item \''.$x.'\' is not an existing key in the array.',true);
  833. unset($Value); $Value = ''; break;
  834. }
  835. } elseif (is_object($Value)) {
  836. $ArgLst = tbs_Misc_CheckArgLst($x);
  837. if (method_exists($Value,$x)) {
  838. $x = call_user_func_array(array(&$Value,$x),$ArgLst);
  839. } elseif (property_exists($Value,$x)) {
  840. $x =& $Value->$x;
  841. } else {
  842. 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);
  843. unset($Value); $Value = ''; break;
  844. }
  845. $Value =& $x; unset($x); $x = '';
  846. } else {
  847. 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);
  848. unset($Value); $Value = ''; break;
  849. }
  850. }
  851. }
  852. $CurrVal = $Value; // Unlink
  853. if (isset($Loc->PrmLst['onformat'])) {
  854. if ($Loc->FirstMerge) {
  855. $Loc->OnFrmInfo = $Loc->PrmLst['onformat'];
  856. $Loc->OnFrmArg = array(&$Loc->FullName,'',&$Loc->PrmLst,&$this);
  857. $ErrMsg = false;
  858. if (!$this->meth_Misc_UserFctCheck($Loc->OnFrmInfo,$ErrMsg,false)) {
  859. unset($Loc->PrmLst['onformat']);
  860. if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'(parameter onformat) '.$ErrMsg);
  861. $Loc->OnFrmInfo = 'pi'; // Execute the function pi() just to avoid extra error messages
  862. }
  863. }
  864. $Loc->OnFrmArg[1] =& $CurrVal;
  865. if (isset($Loc->PrmLst['subtpl'])) {
  866. $this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
  867. call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
  868. $this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
  869. $Loc->ConvProtect = false;
  870. $Loc->ConvHtml = false;
  871. } else {
  872. call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmArg);
  873. }
  874. }
  875. if ($Loc->FirstMerge) {
  876. if (isset($Loc->PrmLst['frm'])) {
  877. $Loc->ConvMode = 0; // Frm
  878. $Loc->ConvProtect = false;
  879. } else {
  880. // Analyze parameter 'htmlconv'
  881. if (isset($Loc->PrmLst['htmlconv'])) {
  882. $x = strtolower($Loc->PrmLst['htmlconv']);
  883. $x = '+'.str_replace(' ','',$x).'+';
  884. if (strpos($x,'+esc+')!==false) {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvEsc = true; }
  885. if (strpos($x,'+wsp+')!==false) {tbs_Misc_ConvSpe($Loc); $Loc->ConvWS = true; }
  886. if (strpos($x,'+js+')!==false) {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvJS = true; }
  887. if (strpos($x,'+no+')!==false) $Loc->ConvHtml = false;
  888. if (strpos($x,'+yes+')!==false) $Loc->ConvHtml = true;
  889. if (strpos($x,'+nobr+')!==false) {$Loc->ConvHtml = true; $Loc->ConvBr = false; }
  890. } else {
  891. if ($this->HtmlCharSet===false) $Loc->ConvHtml = false; // No HTML
  892. }
  893. // Analyze parameter 'protect'
  894. if (isset($Loc->PrmLst['protect'])) {
  895. $x = strtolower($Loc->PrmLst['protect']);
  896. if ($x==='no') {
  897. $Loc->ConvProtect = false;
  898. } elseif ($x==='yes') {
  899. $Loc->ConvProtect = true;
  900. }
  901. } elseif ($this->Protect===false) {
  902. $Loc->ConvProtect = false;
  903. }
  904. }
  905. if ($Loc->Ope = isset($Loc->PrmLst['ope'])) {
  906. $ope = $Loc->PrmLst['ope'];
  907. if ($ope==='list') {
  908. $Loc->OpeId = 1;
  909. $Loc->OpeSep = (isset($Loc->PrmLst['valsep'])) ? $Loc->PrmLst['valsep'] : ',';
  910. } else {
  911. $x = substr($ope,0,4);
  912. if ($x==='max:') {
  913. $Loc->OpeId = (isset($Loc->PrmLst['maxhtml'])) ? 2 : 3;
  914. $Loc->OpeN = intval(trim(substr($ope,4)));
  915. $Loc->OpeEnd = (isset($Loc->PrmLst['maxend'])) ? $Loc->PrmLst['maxend'] : '...';
  916. if ($Loc->OpeN<=0) $Loc->Ope = false;
  917. } elseif ($x==='mod:') {
  918. $Loc->OpeId = 4; $Loc->OpeN = intval(trim(substr($ope,4)));
  919. } elseif ($x==='add:') {
  920. $Loc->OpeId = 5; $Loc->OpeN = intval(trim(substr($ope,4)));
  921. } elseif (isset($this->_piOnOperation)) {
  922. $Loc->OpeId = 0;
  923. $Loc->OpeArg = array(&$Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$Txt,$Loc->PosBeg,$Loc->PosEnd,&$Loc);
  924. } else {
  925. $Loc->Ope = false;
  926. if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'parameter ope doesn\'t support value \''.$ope.'\'.',true);
  927. }
  928. }
  929. }
  930. $Loc->FirstMerge = false;
  931. }
  932. $ConvProtect = $Loc->ConvProtect;
  933. // Plug-in OnFormat
  934. if ($this->_piOnFrm_Ok) {
  935. if (isset($Loc->OnFrmArgPi)) {
  936. $Loc->OnFrmArgPi[1] =& $CurrVal;
  937. } else {
  938. $Loc->OnFrmArgPi = array(&$Loc->FullName,&$CurrVal,&$Loc->PrmLst,&$this);
  939. }
  940. $this->meth_PlugIn_RunAll($this->_piOnFormat,$Loc->OnFrmArgPi);
  941. }
  942. // Operation
  943. if ($Loc->Ope) {
  944. switch ($Loc->OpeId) {
  945. case 0:
  946. $Loc->OpeArg[1] =& $CurrVal; $Loc->OpeArg[3] =& $Txt;
  947. if (!$this->meth_PlugIn_RunAll($this->_piOnOperation,$Loc->OpeArg)) return $Loc->PosBeg;
  948. break;
  949. case 1: if (is_array($CurrVal)) $CurrVal = implode($Loc->OpeSep,$CurrVal); break;
  950. case 2: if (strlen(''.$CurrVal)>$Loc->OpeN) tbs_Html_Max($CurrVal,$Loc->OpeN,$Loc->OpeEnd); break;
  951. case 3: if (strlen(''.$CurrVal)>$Loc->OpeN) $CurrVal = substr(''.$CurrVal,0,$Loc->OpeN).$Loc->OpeEnd; break;
  952. case 4: $CurrVal = intval($CurrVal) % $Loc->OpeN; break;
  953. case 5: $CurrVal = intval($CurrVal) + $Loc->OpeN; break;
  954. }
  955. }
  956. // HTML conversion or format
  957. if ($Loc->ConvMode===1) { // Html simple
  958. if (!is_string($CurrVal)) $CurrVal = @strval($CurrVal);
  959. if ($Loc->ConvHtml) {
  960. $this->meth_Conv_Html($CurrVal);
  961. if ($Loc->ConvBr) $CurrVal = nl2br($CurrVal);
  962. }
  963. } elseif ($Loc->ConvMode===0) { // Format
  964. $CurrVal = $this->meth_Misc_Format($CurrVal,$Loc->PrmLst);
  965. } elseif ($Loc->ConvMode===2) { // Html special
  966. if (!is_string($CurrVal)) $CurrVal = @strval($CurrVal);
  967. if ($Loc->ConvHtml) {
  968. $this->meth_Conv_Html($CurrVal);
  969. if ($Loc->ConvBr) $CurrVal = nl2br($CurrVal);
  970. }
  971. if ($Loc->ConvEsc) $CurrVal = str_replace('\'','\'\'',$CurrVal);
  972. if ($Loc->ConvWS) {
  973. $check = ' ';
  974. $nbsp = '&nbsp;';
  975. do {
  976. $pos = strpos($CurrVal,$check);
  977. if ($pos!==false) $CurrVal = substr_replace($CurrVal,$nbsp,$pos,1);
  978. } while ($pos!==false);
  979. }
  980. if ($Loc->ConvJS) {
  981. $CurrVal = addslashes($CurrVal); // apply to ('), ("), (\) and (null)
  982. $CurrVal = str_replace("\n",'\n',$CurrVal);
  983. $CurrVal = str_replace("\r",'\r',$CurrVal);
  984. $CurrVal = str_replace("\t",'\t',$CurrVal);
  985. }
  986. }
  987. // if/then/else process, there may be several if/then
  988. if ($Loc->PrmIfNbr) {
  989. $z = false;
  990. $i = 1;
  991. while ($i!==false) {
  992. if ($Loc->PrmIfVar[$i]) $Loc->PrmIfVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmIf[$i],true);
  993. $x = str_replace($this->_ChrVal,$CurrVal,$Loc->PrmIf[$i]);
  994. if (tbs_Misc_CheckCondition($x)) {
  995. if (isset($Loc->PrmThen[$i])) {
  996. if ($Loc->PrmThenVar[$i]) $Loc->PrmThenVar[$i] = $this->meth_Merge_AutoVar($Loc->PrmThen[$i],true);
  997. $z = $Loc->PrmThen[$i];
  998. }
  999. $i = false;
  1000. } else {
  1001. $i++;
  1002. if ($i>$Loc->PrmIfNbr) {
  1003. if (isset($Loc->PrmLst['else'])) {
  1004. if ($Loc->PrmElseVar) $Loc->PrmElseVar = $this->meth_Merge_AutoVar($Loc->PrmLst['else'],true);
  1005. $z =$Loc->PrmLst['else'];
  1006. }
  1007. $i = false;
  1008. }
  1009. }
  1010. }
  1011. if ($z!==false) {
  1012. if ($ConvProtect) {
  1013. $CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
  1014. $ConvProtect = false;
  1015. }
  1016. $CurrVal = str_replace($this->_ChrVal,$CurrVal,$z);
  1017. }
  1018. }
  1019. if (isset($Loc->PrmLst['file'])) {
  1020. $x = $Loc->PrmLst['file'];
  1021. if ($x===true) $x = $CurrVal;
  1022. $this->meth_Merge_AutoVar($x,false);
  1023. $x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
  1024. $CurrVal = '';
  1025. if ($x!=='') {
  1026. if (tbs_Misc_GetFile($CurrVal,$x)) {
  1027. if (isset($Loc->PrmLst['getbody'])) $CurrVal = tbs_Html_GetPart($CurrVal,$Loc->PrmLst['getbody'],false,true);
  1028. } else {
  1029. if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter file is not found or not readable.',true);
  1030. }
  1031. $ConvProtect = false;
  1032. }
  1033. }
  1034. if (isset($Loc->PrmLst['script'])) {// Include external PHP script
  1035. $x = $Loc->PrmLst['script'];
  1036. if ($x===true) $x = $CurrVal;
  1037. $this->meth_Merge_AutoVar($x,false);
  1038. $x = trim(str_replace($this->_ChrVal,$CurrVal,$x));
  1039. if ($x!=='') {
  1040. $this->_Subscript = $x;
  1041. $this->CurrPrm =& $Loc->PrmLst;
  1042. $sub = isset($Loc->PrmLst['subtpl']);
  1043. if ($sub) $this->meth_Misc_ChangeMode(true,$Loc,$CurrVal);
  1044. if ($this->meth_Misc_RunSubscript($CurrVal,$Loc->PrmLst)===false) {
  1045. if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert($Loc,'the file \''.$x.'\' given by parameter script is not found or not readable.',true);
  1046. }
  1047. if ($sub) $this->meth_Misc_ChangeMode(false,$Loc,$CurrVal);
  1048. if (isset($Loc->PrmLst['getbody'])) $CurrVal = tbs_Html_GetPart($CurrVal,$Loc->PrmLst['getbody'],false,true);
  1049. unset($this->CurrPrm);
  1050. $ConvProtect = false;
  1051. }
  1052. }
  1053. // Case when it's an empty string
  1054. if ($CurrVal==='') {
  1055. if ($Loc->MagnetId===false) {
  1056. if (isset($Loc->PrmLst['.'])) {
  1057. $Loc->MagnetId = -1;
  1058. } elseif (isset($Loc->PrmLst['ifempty'])) {
  1059. $Loc->MagnetId = -2;
  1060. } elseif (isset($Loc->PrmLst['magnet'])) {
  1061. $Loc->MagnetId = 1;
  1062. $Loc->PosBeg0 = $Loc->PosBeg;
  1063. $Loc->PosEnd0 = $Loc->PosEnd;
  1064. if (isset($Loc->PrmLst['mtype'])) {
  1065. switch ($Loc->PrmLst['mtype']) {
  1066. case 'm+m': $Loc->MagnetId = 2; break;
  1067. case 'm*': $Loc->MagnetId = 3; break;
  1068. case '*m': $Loc->MagnetId = 4; break;
  1069. }
  1070. }
  1071. } else {
  1072. $Loc->MagnetId = 0;
  1073. }
  1074. }
  1075. switch ($Loc->MagnetId) {
  1076. case 0: break;
  1077. case -1: $CurrVal = '&nbsp;'; break; // Enables to avoid blanks in HTML tables
  1078. case -2: $CurrVal = $Loc->PrmLst['ifempty']; break;
  1079. case 1:
  1080. $Loc->Enlarged = true;
  1081. tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],false);
  1082. break;
  1083. case 2:
  1084. $Loc->Enlarged = true;
  1085. $CurrVal = tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],true);
  1086. break;
  1087. case 3:
  1088. $Loc->Enlarged = true;
  1089. $Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,false,1,false);
  1090. if ($Loc2!==false) {
  1091. $Loc->PosBeg = $Loc2->PosBeg;
  1092. if ($Loc->PosEnd<$Loc2->PosEnd) $Loc->PosEnd = $Loc2->PosEnd;
  1093. }
  1094. break;
  1095. case 4:
  1096. $Loc->Enlarged = true;
  1097. $Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,true,1,false);
  1098. if ($Loc2!==false) $Loc->PosEnd = $Loc2->PosEnd;
  1099. break;
  1100. }
  1101. $NewEnd = $Loc->PosBeg; // Useful when mtype='m+m'
  1102. } else {
  1103. if ($ConvProtect) $CurrVal = str_replace($this->_ChrOpen,$this->_ChrProtect,$CurrVal); // TBS protection
  1104. $NewEnd = $Loc->PosBeg + strlen($CurrVal);
  1105. }
  1106. $Txt = substr_replace($Txt,$CurrVal,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
  1107. return $NewEnd; // Return the new end position of the field
  1108. }
  1109. function meth_Locator_FindBlockNext(&$Txt,$BlockName,$PosBeg,$ChrSub,$Mode,&$P1,&$FieldBefore) {
  1110. // Return the first block locator just after the PosBeg position
  1111. // Mode = 1 : Merge_Auto => doesn't save $Loc->BlockSrc, save the bounds of TBS Def tags instead, return also fields
  1112. // Mode = 2 : FindBlockLst or GetBlockSource => save $Loc->BlockSrc without TBS Def tags
  1113. // Mode = 3 : GetBlockSource => save $Loc->BlockSrc with TBS Def tags
  1114. $SearchDef = true;
  1115. $FirstField = false;
  1116. // Search for the first tag with parameter "block"
  1117. while ($SearchDef and ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub))) {
  1118. if (isset($Loc->PrmLst['block'])) {
  1119. if ($P1) {
  1120. if (isset($Loc->PrmLst['p1'])) return false;
  1121. } else {
  1122. if (isset($Loc->PrmLst['p1'])) $P1 = true;
  1123. }
  1124. $Block = $Loc->PrmLst['block'];
  1125. $SearchDef = false;
  1126. } elseif ($Mode===1) {
  1127. return $Loc;
  1128. } elseif ($FirstField===false) {
  1129. $FirstField = $Loc;
  1130. }
  1131. $PosBeg = $Loc->PosEnd;
  1132. }
  1133. if ($SearchDef) {
  1134. if ($FirstField!==false) $FieldBefore = true;
  1135. return false;
  1136. }
  1137. if ($Block==='begin') { // Block definied using begin/end
  1138. if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
  1139. $Opened = 1;
  1140. while ($Loc2 = $this->meth_Locator_FindTbs($Txt,$BlockName,$PosBeg,$ChrSub)) {
  1141. if (isset($Loc2->PrmLst['block'])) {
  1142. switch ($Loc2->PrmLst['block']) {
  1143. case 'end': $Opened--; break;
  1144. case 'begin': $Opened++; break;
  1145. }
  1146. if ($Opened==0) {
  1147. if ($Mode===1) {
  1148. $Loc->PosBeg2 = $Loc2->PosBeg;
  1149. $Loc->PosEnd2 = $Loc2->PosEnd;
  1150. } else {
  1151. if ($Mode===2) {
  1152. $Loc->BlockSrc = substr($Txt,$Loc->PosEnd+1,$Loc2->PosBeg-$Loc->PosEnd-1);
  1153. } else {
  1154. $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc2->PosEnd-$Loc->PosBeg+1);
  1155. }
  1156. $Loc->PosEnd = $Loc2->PosEnd;
  1157. $Loc->PosDef = 0;
  1158. }
  1159. $Loc->BlockFound = true;
  1160. return $Loc;
  1161. }
  1162. }
  1163. $PosBeg = $Loc2->PosEnd;
  1164. }
  1165. return $this->meth_Misc_Alert($Loc,'a least one tag with parameter \'block=end\' is missing.',false,'in block\'s definition');
  1166. }
  1167. if ($Mode===1) {
  1168. $Loc->PosBeg2 = false;
  1169. } else {
  1170. $Loc->PosDef = $Loc->PosBeg;
  1171. if (!$Loc->SubOk) {
  1172. $PosBeg1 = $Loc->PosBeg;
  1173. $PosEnd1 = $Loc->PosEnd;
  1174. }
  1175. 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');
  1176. $Loc->PosDef = $Loc->PosDef - $Loc->PosBeg;
  1177. if ($Loc->SubOk or ($Mode===3)) {
  1178. $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);
  1179. $Loc->PosDef++;
  1180. } else {
  1181. $Loc->BlockSrc = substr($Txt,$Loc->PosBeg,$PosBeg1-$Loc->PosBeg).substr($Txt,$PosEnd1+1,$Loc->PosEnd-$PosEnd1);
  1182. }
  1183. }
  1184. $Loc->BlockFound = true;
  1185. if (($FirstField!==false) and ($FirstField->PosEnd<$Loc->PosBeg)) $FieldBefore = true;
  1186. return $Loc; // methods return by ref by default
  1187. }
  1188. function meth_Locator_FindBlockLst(&$Txt,$BlockName,$Pos,$SpePrm) {
  1189. // Return a locator object covering all block definitions, even if there is no block definition found.
  1190. $LocR = new clsTbsLocator;
  1191. $LocR->P1 = false;
  1192. $LocR->FieldOutside = false;
  1193. $LocR->BlockNbr = 0; // Any section (normal, nodata, when, serial, condition, grp, ...)
  1194. $LocR->BlockSrc = array(); // 1 to BlockNbr
  1195. $LocR->BlockLoc = array(); // idem
  1196. $LocR->BlockChk = array(); // idem
  1197. $LocR->BlockName = array(); // idem
  1198. $LocR->BlockPrm = array(); // idem. Provided only for plug-ins.
  1199. $LocR->NoDataBid = false;
  1200. $LocR->SpecialBid = false;
  1201. $LocR->HeaderFound = false;
  1202. $LocR->FooterFound = false;
  1203. $LocR->WhenFound = false;
  1204. $LocR->WhenDefaultBid = false;
  1205. $LocR->SectionNbr = 0; // Normal section
  1206. $LocR->SectionBid = array(); // 1 to SectionNbr
  1207. $LocR->SectionIsSerial = array(); // idem
  1208. $LocR->SectionSerialBid = array(); // idem
  1209. $LocR->SectionSerialOrd = array(); // idem
  1210. $LocR->SerialEmpty = false;
  1211. $Bid =& $LocR->BlockNbr;
  1212. $Sid =& $LocR->SectionNbr;
  1213. $Pid = 0;
  1214. do {
  1215. if ($BlockName==='') {
  1216. $Loc = false;
  1217. } else {
  1218. $Loc = $this->meth_Locator_FindBlockNext($Txt,$BlockName,$Pos,'.',2,$LocR->P1,$LocR->FieldOutside);
  1219. }
  1220. if ($Loc===false) {
  1221. if ($Pid>0) { // parentgrp mode => disconnect $Txt from the source
  1222. $Src = $Txt;
  1223. $Txt =& $Parent[$Pid]['txt'];
  1224. if ($LocR->BlockFound) {
  1225. // Redefine the Header block
  1226. $i = $Parent[$Pid]['bid'];
  1227. $LocR->BlockSrc[$i] = substr($Src,0,$LocR->PosBeg);
  1228. // Add a Footer block
  1229. tbs_Locator_SectionAddBlk($LocR,$BlockName,substr($Src,$LocR->PosEnd+1),$LocR->BlockPrm[$i]);
  1230. tbs_Locator_SectionAddGrp($LocR,$Bid,'F',$Parent[$Pid]['fld']);
  1231. }
  1232. // Now gowing down to previous level
  1233. $Pos = $Parent[$Pid]['pos'];
  1234. $LocR->PosBeg = $Parent[$Pid]['beg'];
  1235. $LocR->PosEnd = $Parent[$Pid]['end'];
  1236. $LocR->BlockFound = true;
  1237. unset($Parent[$Pid]);
  1238. $Pid--;
  1239. $Loc = true;
  1240. }
  1241. } else {
  1242. $Pos = $Loc->PosEnd;
  1243. // Define the block limits
  1244. if ($LocR->BlockFound) {
  1245. if ( $LocR->PosBeg > $Loc->PosBeg ) $LocR->PosBeg = $Loc->PosBeg;
  1246. if ( $LocR->PosEnd < $Loc->PosEnd ) $LocR->PosEnd = $Loc->PosEnd;
  1247. } else {
  1248. $LocR->BlockFound = true;
  1249. $LocR->PosBeg = $Loc->PosBeg;
  1250. $LocR->PosEnd = $Loc->PosEnd;
  1251. }
  1252. // Merge block parameters
  1253. if (count($Loc->PrmLst)>0) $LocR->PrmLst = array_merge($LocR->PrmLst,$Loc->PrmLst);
  1254. // Save the block and cache its tags (incrments $LocR->BlockNbr)
  1255. tbs_Locator_SectionAddBlk($LocR,$BlockName,$Loc->BlockSrc,$Loc->PrmLst);
  1256. // Add the text in the list of blocks
  1257. if (isset($Loc->PrmLst['nodata'])) { // Nodata section
  1258. $LocR->NoDataBid = $Bid;
  1259. } elseif (($SpePrm!==false) and isset($Loc->PrmLst[$SpePrm])) { // Special section (used for navigation bar)
  1260. $LocR->SpecialBid = $Bid;
  1261. } elseif (isset($Loc->PrmLst['when'])) {
  1262. if ($LocR->WhenFound===false) {
  1263. $LocR->WhenFound = true;
  1264. $LocR->WhenSeveral = false;
  1265. $LocR->WhenNbr = 0;
  1266. $LocR->WhenSectionBid[] = array(); // Bid of the section to display
  1267. $LocR->WhenCondBid[] = array(); // Bid of the condition to check
  1268. $LocR->WhenBeforeNS[] = array(); // True if the When section must be displayed before a
  1269. }
  1270. $LocR->WhenNbr++;
  1271. if (isset($Loc->PrmLst['several'])) $LocR->WhenSeveral = true;
  1272. $LocR->WhenSectionBid[$LocR->WhenNbr] = $Bid;
  1273. $this->meth_Merge_AutoVar($Loc->PrmLst['when'],false);
  1274. tbs_Locator_SectionAddBlk($LocR,$BlockName,$Loc->PrmLst['when'],array());
  1275. $LocR->WhenCondBid[$LocR->WhenNbr] = $Bid;
  1276. $LocR->WhenBeforeNS[$LocR->WhenNbr] = ($Sid===0);
  1277. } elseif (isset($Loc->PrmLst['default'])) {
  1278. $LocR->WhenDefaultBid = $Bid;
  1279. $LocR->WhenDefaultBeforeNS = ($Sid===0);
  1280. } elseif (isset($Loc->PrmLst['headergrp'])) {
  1281. tbs_Locator_SectionAddGrp($LocR,$Bid,'H',$Loc->PrmLst['headergrp']);
  1282. } elseif (isset($Loc->PrmLst['footergrp'])) {
  1283. tbs_Locator_SectionAddGrp($LocR,$Bid,'F',$Loc->PrmLst['footergrp']);
  1284. } elseif (isset($Loc->PrmLst['splittergrp'])) {
  1285. tbs_Locator_SectionAddGrp($LocR,$Bid,'S',$Loc->PrmLst['splittergrp']);
  1286. } elseif (isset($Loc->PrmLst['parentgrp'])) {
  1287. tbs_Locator_SectionAddGrp($LocR,$Bid,'H',$Loc->PrmLst['parentgrp']);
  1288. $Pid++;
  1289. $Parent[$Pid]['bid'] = $Bid;
  1290. $Parent[$Pid]['fld'] = $Loc->PrmLst['parentgrp'];
  1291. $Parent[$Pid]['txt'] =& $Txt;
  1292. $Parent[$Pid]['pos'] = $Pos;
  1293. $Parent[$Pid]['beg'] = $LocR->PosBeg;
  1294. $Parent[$Pid]['end'] = $LocR->PosEnd;
  1295. $Txt =& $LocR->BlockSrc[$Bid];
  1296. $Pos = $Loc->PosDef + 1;
  1297. $LocR->BlockFound = false;
  1298. $LocR->PosBeg = false;
  1299. $LocR->PosEnd = false;
  1300. } elseif (isset($Loc->PrmLst['serial'])) {
  1301. // Section with Serial Sub-Sections
  1302. $Src =& $LocR->BlockSrc[$Bid];
  1303. $Loc0 = false;
  1304. if ($LocR->SerialEmpty===false) {
  1305. $NameSr = $BlockName.'_0';
  1306. $x = false;
  1307. $LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
  1308. if ($LocSr!==false) {
  1309. $LocR->SerialEmpty = $LocSr->BlockSrc;
  1310. $Src = substr_replace($Src,'',$LocSr->PosBeg,$LocSr->PosEnd-$LocSr->PosBeg+1);
  1311. }
  1312. }
  1313. $NameSr = $BlockName.'_1';
  1314. $x = false;
  1315. $LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
  1316. if ($LocSr!==false) {
  1317. $Sid++;
  1318. $LocR->SectionBid[$Sid] = $Bid;
  1319. $LocR->SectionIsSerial[$Sid] = true;
  1320. $LocR->SectionSerialBid[$Sid] = array();
  1321. $LocR->SectionSerialOrd[$Sid] = array();
  1322. $SrBid =& $LocR->SectionSerialBid[$Sid];
  1323. $SrOrd =& $LocR->SectionSerialOrd[$Sid];
  1324. $BidParent = $Bid;
  1325. $SrNum = 1;
  1326. do {
  1327. // Save previous sub-section
  1328. $LocR->BlockLoc[$BidParent][$SrNum] = $LocSr;
  1329. tbs_Locator_SectionAddBlk($LocR,$NameSr,$LocSr->BlockSrc,$LocSr->PrmLst);
  1330. $SrBid[$SrNum] = $Bid;
  1331. $SrOrd[$SrNum] = $SrNum;
  1332. $i = $SrNum;
  1333. while (($i>1) and ($LocSr->PosBeg<$LocR->BlockLoc[$BidParent][$SrOrd[$i-1]]->PosBeg)) {
  1334. $SrOrd[$i] = $SrOrd[$i-1];
  1335. $SrOrd[$i-1] = $SrNum;
  1336. $i--;
  1337. }
  1338. // Search next section
  1339. $SrNum++;
  1340. $NameSr = $BlockName.'_'.$SrNum;
  1341. $x = false;
  1342. $LocSr = $this->meth_Locator_FindBlockNext($Src,$NameSr,0,'.',2,$x,$x);
  1343. } while ($LocSr!==false);
  1344. $SrBid[0] = $SrNum-1;
  1345. }
  1346. } else {
  1347. // Normal section
  1348. $Sid++;
  1349. $LocR->SectionBid[$Sid] = $Bid;
  1350. $LocR->SectionIsSerial[$Sid] = false;
  1351. }
  1352. }
  1353. } while ($Loc!==false);
  1354. if ($LocR->WhenFound and ($Sid===0)) {
  1355. // Add a blank section if When is used without a normal section
  1356. tbs_Locator_SectionAddBlk($LocR,$BlockName,'',array());
  1357. $Sid++;
  1358. $LocR->SectionBid[$Sid] = $Bid;
  1359. $LocR->SectionIsSerial[$Sid] = false;
  1360. }
  1361. // Calculate Cache
  1362. if ($this->TurboBlock) {
  1363. for ($i=1;$i<=$LocR->BlockNbr;$i++) {
  1364. $this->meth_Locator_SectionCache($LocR,$i);
  1365. }
  1366. }
  1367. return $LocR; // methods return by ref by default
  1368. }
  1369. function meth_Merge_Block(&$Txt,&$BlockLst,&$SrcId,&$Query,$SpePrm,$SpeRecNum) {
  1370. $BlockSave = $this->_CurrBlock;
  1371. $this->_CurrBlock = $BlockLst;
  1372. // Get source type and info
  1373. $Src = new clsTbsDataSource;
  1374. if (!$Src->DataPrepare($SrcId,$this)) {
  1375. $this->_CurrBlock = $BlockSave;
  1376. return 0;
  1377. }
  1378. $BlockLst = explode(',',$BlockLst);
  1379. $BlockNbr = count($BlockLst);
  1380. $BlockId = 0;
  1381. $WasP1 = false;
  1382. $NbrRecTot = 0;
  1383. $QueryZ =& $Query;
  1384. $ReturnData = false;
  1385. while ($BlockId<$BlockNbr) {
  1386. $RecSpe = 0; // Row with a special block's definition (used for the navigation bar)
  1387. $QueryOk = true;
  1388. $this->_CurrBlock = trim($BlockLst[$BlockId]);
  1389. if ($this->_CurrBlock==='*') {
  1390. $ReturnData = true;
  1391. if ($Src->RecSaved===false) $Src->RecSaving = true;
  1392. $this->_CurrBlock = '';
  1393. }
  1394. // Search the block
  1395. $LocR = $this->meth_Locator_FindBlockLst($Txt,$this->_CurrBlock,0,$SpePrm);
  1396. if ($WasP1) $this->meth_Merge_FieldOutside($Txt,$Src,$LocR);
  1397. if ($LocR->BlockFound) {
  1398. if ($LocR->SpecialBid!==false) $RecSpe = $SpeRecNum;
  1399. // OnData
  1400. if ($Src->OnDataPrm = isset($LocR->PrmLst['ondata'])) {
  1401. $Src->OnDataPrmRef = $LocR->PrmLst['ondata'];
  1402. if (isset($Src->OnDataPrmDone[$Src->OnDataPrmRef])) {
  1403. $Src->OnDataPrm = false;
  1404. } else {
  1405. $ErrMsg = false;
  1406. if ($this->meth_Misc_UserFctCheck($Src->OnDataPrmRef,$ErrMsg,false)) {
  1407. $Src->OnDataOk = true;
  1408. } else {
  1409. $LocR->FullName = $this->_CurrBlock;
  1410. $Src->OnDataPrm = $this->meth_Misc_Alert($LocR,'(parameter ondata) '.$ErrMsg,false,'block');
  1411. }
  1412. }
  1413. }
  1414. // Dynamic query
  1415. if ($LocR->P1) {
  1416. if (is_string($Query)) {
  1417. $Src->RecSaved = false;
  1418. unset($QueryZ); $QueryZ = ''.$Query;
  1419. $i = 1;
  1420. do {
  1421. $x = 'p'.$i;
  1422. if (isset($LocR->PrmLst[$x])) {
  1423. $QueryZ = str_replace('%p'.$i.'%',$LocR->PrmLst[$x],$QueryZ);
  1424. $i++;
  1425. } else {
  1426. $i = false;
  1427. }
  1428. } while ($i!==false);
  1429. }
  1430. $WasP1 = true;
  1431. } elseif (($Src->RecSaved===false) and ($BlockNbr-$BlockId>1)) {
  1432. $Src->RecSaving = true;
  1433. }
  1434. } elseif ($WasP1) {
  1435. $QueryOk = false;
  1436. $WasP1 = false;
  1437. }
  1438. // Open the recordset
  1439. if ($QueryOk) {
  1440. if ((!$LocR->BlockFound) and (!$LocR->FieldOutside)) {
  1441. // Special case: return data without any block to merge
  1442. $QueryOk = false;
  1443. if ($ReturnData and (!$Src->RecSaved)) {
  1444. if ($Src->DataOpen($QueryZ)) {
  1445. do {$Src->DataFetch();} while ($Src->CurrRec!==false);
  1446. $Src->DataClose();
  1447. }
  1448. }
  1449. } else {
  1450. $QueryOk = $Src->DataOpen($QueryZ);
  1451. }
  1452. }
  1453. // Merge sections
  1454. if ($QueryOk) {
  1455. if ($Src->Type===4) { // Special for Text merge
  1456. if ($LocR->BlockFound) {
  1457. $Src->RecNum = 1;
  1458. $Src->CurrRec = false;
  1459. $Txt = substr_replace($Txt,$Src->RecSet,$LocR->PosBeg,$LocR->PosEnd-$LocR->PosBeg+1);
  1460. } else {
  1461. $Src->DataAlert('can\'t merge the block with a text value because the block definition is not found.');
  1462. }
  1463. } elseif ($LocR->BlockFound===false) {
  1464. $Src->DataFetch(); // Merge first record only
  1465. } else {
  1466. $this->meth_Merge_BlockSections($Txt,$LocR,$Src,$RecSpe);
  1467. }
  1468. $Src->DataClose(); // Close the resource
  1469. }
  1470. if (!$WasP1) {
  1471. $NbrRecTot += $Src->RecNum;
  1472. if ($LocR->FieldOutside and $QueryOk) {
  1473. $LocR->BlockFound = false; // Cancel Loc limit
  1474. $this->meth_Merge_FieldOutside($Txt,$Src,$LocR);
  1475. }
  1476. $BlockId++;
  1477. }
  1478. }
  1479. // End of the merge
  1480. unset($LocR);
  1481. $this->_CurrBlock = $BlockSave;
  1482. if ($ReturnData) {
  1483. return $Src->RecSet;
  1484. } else {
  1485. unset($Src);
  1486. return $NbrRecTot;
  1487. }
  1488. }
  1489. function meth_Merge_BlockSections(&$Txt,&$LocR,&$Src,&$RecSpe) {
  1490. // Initialise
  1491. $SecId = 0;
  1492. $SecOk = ($LocR->SectionNbr>0);
  1493. $SecIncr = true;
  1494. $SecSrc = '';
  1495. $BlockRes = ''; // The result of the chained merged blocks
  1496. $SerialMode = false;
  1497. $SerialNum = 0;
  1498. $SerialMax = 0;
  1499. $SerialTxt = array();
  1500. $GrpFound = ($LocR->HeaderFound or $LocR->FooterFound);
  1501. $piOMS = false;
  1502. // Plug-ins
  1503. if ($this->_PlugIns_Ok and isset($this->_piBeforeMergeBlock)) {
  1504. $ArgLst = array(&$Txt,&$LocR->PosBeg,&$LocR->PosEnd,$LocR->PrmLst,&$Src,&$LocR);
  1505. $this->meth_Plugin_RunAll($this->_piBeforeMergeBlock,$ArgLst);
  1506. }
  1507. if ($this->_PlugIns_Ok and isset($this->_piOnMergeSection)) {
  1508. $ArgLst = array(&$BlockRes,&$SecSrc);
  1509. $piOMS = true;
  1510. }
  1511. // Main loop
  1512. $Src->DataFetch();
  1513. while($Src->CurrRec!==false) {
  1514. // Headers and Footers
  1515. if ($GrpFound) {
  1516. $grp_change = false;
  1517. $grp_src = '';
  1518. if ($LocR->FooterFound) {
  1519. $change = false;
  1520. for ($i=$LocR->FooterNbr;$i>=1;$i--) {
  1521. $x = $Src->CurrRec[$LocR->FooterField[$i]];
  1522. if ($Src->RecNum===1) {
  1523. $LocR->FooterPrevValue[$i] = $x;
  1524. } else {
  1525. if ($LocR->FooterIsFooter[$i]) {
  1526. $change_i =& $change;
  1527. } else {
  1528. unset($change_i); $change_i = false;
  1529. }
  1530. if (!$change_i) $change_i = !($LocR->FooterPrevValue[$i]===$x);
  1531. if ($change_i) {
  1532. $grp_change = true;
  1533. $grp_src = $this->meth_Merge_SectionNormal($LocR,$LocR->FooterBid[$i],$PrevRec,$PrevNum,$PrevKey).$grp_src;
  1534. $LocR->FooterPrevValue[$i] = $x;
  1535. }
  1536. }
  1537. }
  1538. $PrevRec = $Src->CurrRec;
  1539. $PrevNum = $Src->RecNum;
  1540. $PrevKey = $Src->RecKey;
  1541. }
  1542. if ($LocR->HeaderFound) {
  1543. $change = ($Src->RecNum===1);
  1544. for ($i=1;$i<=$LocR->HeaderNbr;$i++) {
  1545. $x = $Src->CurrRec[$LocR->HeaderField[$i]];
  1546. if (!$change) $change = !($LocR->HeaderPrevValue[$i]===$x);
  1547. if ($change) {
  1548. $grp_src .= $this->meth_Merge_SectionNormal($LocR,$LocR->HeaderBid[$i],$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1549. $LocR->HeaderPrevValue[$i] = $x;
  1550. }
  1551. }
  1552. $grp_change = ($grp_change or $change);
  1553. }
  1554. if ($grp_change) {
  1555. if ($SerialMode) {
  1556. $BlockRes .= $this->meth_Merge_SectionSerial($LocR,$SecId,$SerialNum,$SerialMax,$SerialTxt);
  1557. $SecIncr = true;
  1558. }
  1559. $BlockRes .= $grp_src;
  1560. }
  1561. } // end of header and footer
  1562. // Increment Section
  1563. if ($SecIncr and $SecOk) {
  1564. $SecId++;
  1565. if ($SecId>$LocR->SectionNbr) $SecId = 1;
  1566. $SerialMode = $LocR->SectionIsSerial[$SecId];
  1567. if ($SerialMode) {
  1568. $SerialNum = 0;
  1569. $SerialMax = $LocR->SectionSerialBid[$SecId][0];
  1570. $SecIncr = false;
  1571. }
  1572. }
  1573. // Serial Mode Activation
  1574. if ($SerialMode) { // Serial Merge
  1575. $SerialNum++;
  1576. $Bid = $LocR->SectionSerialBid[$SecId][$SerialNum];
  1577. $SerialTxt[$SerialNum] = $this->meth_Merge_SectionNormal($LocR,$Bid,$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1578. if ($SerialNum>=$SerialMax) {
  1579. $SecSrc = $this->meth_Merge_SectionSerial($LocR,$SecId,$SerialNum,$SerialMax,$SerialTxt);
  1580. $BlockRes .= $SecSrc;
  1581. $SecIncr = true;
  1582. }
  1583. } else { // Classic merge
  1584. if ($SecOk) {
  1585. if ($Src->RecNum===$RecSpe) {
  1586. $Bid = $LocR->SpecialBid;
  1587. } else {
  1588. $Bid = $LocR->SectionBid[$SecId];
  1589. }
  1590. $SecSrc = $this->meth_Merge_SectionNormal($LocR,$Bid,$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1591. } else {
  1592. $SecSrc = '';
  1593. }
  1594. if ($LocR->WhenFound) { // With conditional blocks
  1595. $found = false;
  1596. $continue = true;
  1597. $i = 1;
  1598. do {
  1599. $cond = $this->meth_Merge_SectionNormal($LocR,$LocR->WhenCondBid[$i],$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1600. if (tbs_Misc_CheckCondition($cond)) {
  1601. $x_when = $this->meth_Merge_SectionNormal($LocR,$LocR->WhenSectionBid[$i],$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1602. if ($LocR->WhenBeforeNS[$i]) {$SecSrc = $x_when.$SecSrc;} else {$SecSrc = $SecSrc.$x_when;}
  1603. $found = true;
  1604. if ($LocR->WhenSeveral===false) $continue = false;
  1605. }
  1606. $i++;
  1607. if ($i>$LocR->WhenNbr) $continue = false;
  1608. } while ($continue);
  1609. if (($found===false) and ($LocR->WhenDefaultBid!==false)) {
  1610. $x_when = $this->meth_Merge_SectionNormal($LocR,$LocR->WhenDefaultBid,$Src->CurrRec,$Src->RecNum,$Src->RecKey);
  1611. if ($LocR->WhenDefaultBeforeNS) {$SecSrc = $x_when.$SecSrc;} else {$SecSrc = $SecSrc.$x_when;}
  1612. }
  1613. }
  1614. if ($piOMS) $this->meth_PlugIn_RunAll($this->_piOnMergeSection,$ArgLst);
  1615. $BlockRes .= $SecSrc;
  1616. }
  1617. // Next row
  1618. $Src->DataFetch();
  1619. } //--> while($CurrRec!==false) {
  1620. $SecSrc = '';
  1621. // Serial: merge the extra the sub-blocks
  1622. if ($SerialMode and !$SecIncr) {
  1623. $SecSrc .= $this->meth_Merge_SectionSerial($LocR,$SecId,$SerialNum,$SerialMax,$SerialTxt);
  1624. }
  1625. // Footer
  1626. if ($LocR->FooterFound) {
  1627. if ($Src->RecNum>0) {
  1628. for ($i=1;$i<=$LocR->FooterNbr;$i++) {
  1629. if ($LocR->FooterIsFooter[$i]) $SecSrc .= $this->meth_Merge_SectionNormal($LocR,$LocR->FooterBid[$i],$PrevRec,$PrevNum,$PrevKey);
  1630. }
  1631. }
  1632. }
  1633. // NoData
  1634. if ($Src->RecNum===0) {
  1635. if ($LocR->NoDataBid!==false) {
  1636. $SecSrc = $LocR->BlockSrc[$LocR->NoDataBid];
  1637. } elseif(isset($LocR->PrmLst['bmagnet'])) {
  1638. tbs_Locator_EnlargeToTag($Txt,$LocR,$LocR->PrmLst['bmagnet'],false);
  1639. }
  1640. }
  1641. // Plug-ins
  1642. if ($piOMS and ($SecSrc!=='')) $this->meth_PlugIn_RunAll($this->_piOnMergeSection,$ArgLst);
  1643. $BlockRes .= $SecSrc;
  1644. // Plug-ins
  1645. if ($this->_PlugIns_Ok and isset($ArgLst) and isset($this->_piAfterMergeBlock)) {
  1646. $ArgLst = array(&$BlockRes,&$Src,&$LocR);
  1647. $this->meth_PlugIn_RunAll($this->_piAfterMergeBlock,$ArgLst);
  1648. }
  1649. // Merge the result
  1650. $Txt = substr_replace($Txt,$BlockRes,$LocR->PosBeg,$LocR->PosEnd-$LocR->PosBeg+1);
  1651. }
  1652. function meth_Merge_AutoVar(&$Txt,$HtmlConv) {
  1653. // Merge [var.*] fields with global variables
  1654. $Pref =& $this->VarPrefix;
  1655. $PrefL = strlen($Pref);
  1656. $PrefOk = ($PrefL>0);
  1657. if ($HtmlConv===false) {
  1658. $HtmlCharSet = $this->HtmlCharSet;
  1659. $this->HtmlCharSet = false;
  1660. }
  1661. // Then we scann all fields in the model
  1662. $x = '';
  1663. $Pos = 0;
  1664. while ($Loc = $this->meth_Locator_FindTbs($Txt,'var',$Pos,'.')) {
  1665. if ($Loc->SubNbr==0) $Loc->SubLst[0]=''; // In order to force error message
  1666. if ($Loc->SubLst[0]==='') {
  1667. $Pos = $this->meth_Merge_AutoSpe($Txt,$Loc);
  1668. } elseif ($Loc->SubLst[0][0]==='~') {
  1669. if (!isset($ObjOk)) $ObjOk = (is_object($this->ObjectRef) or is_array($this->ObjectRef));
  1670. if ($ObjOk) {
  1671. $Loc->SubLst[0] = substr($Loc->SubLst[0],1);
  1672. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$this->ObjectRef,0);
  1673. } elseif ($this->NoErr or isset($Loc->PrmLst['noerr'])) {
  1674. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$x,false);
  1675. } else {
  1676. $this->meth_Misc_Alert($Loc,'property ObjectRef is neither an object nor an array. Its type is \''.gettype($this->ObjectRef).'\'.',true);
  1677. $Pos = $Loc->PosEnd + 1;
  1678. }
  1679. } elseif ($PrefOk and (substr($Loc->SubLst[0],0,$PrefL)!==$Pref)) {
  1680. if ($this->NoErr or isset($Loc->PrmLst['noerr'])) {
  1681. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$x,false);
  1682. } else {
  1683. $this->meth_Misc_Alert($Loc,'does not match the allowed prefix.',true);
  1684. $Pos = $Loc->PosEnd + 1;
  1685. }
  1686. } elseif (isset($GLOBALS[$Loc->SubLst[0]])) {
  1687. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$GLOBALS[$Loc->SubLst[0]],1);
  1688. } else {
  1689. if ($this->NoErr or isset($Loc->PrmLst['noerr'])) {
  1690. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$x,false);
  1691. } else {
  1692. $Pos = $Loc->PosEnd + 1;
  1693. $this->meth_Misc_Alert($Loc,'the PHP global variable named \''.$Loc->SubLst[0].'\' does not exist or is not set yet.',true);
  1694. }
  1695. }
  1696. }
  1697. if ($HtmlConv===false) $this->HtmlCharSet = $HtmlCharSet;
  1698. return false; // Useful property PrmIfVar & PrmThenVar
  1699. }
  1700. function meth_Merge_AutoSpe(&$Txt,&$Loc) {
  1701. // Merge [var..*] (Special Var Fields)
  1702. $ErrMsg = false;
  1703. $SubStart = false;
  1704. if (isset($Loc->SubLst[1])) {
  1705. switch ($Loc->SubLst[1]) {
  1706. case 'now': $x = mktime(); break;
  1707. case 'version': $x = $this->Version; break;
  1708. case 'script_name': $x = tbs_Misc_GetFilePart(((isset($_SERVER)) ? $_SERVER['PHP_SELF'] : $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] ),1); break;
  1709. case 'template_name': $x = $this->_LastFile; break;
  1710. case 'template_date': $x = filemtime($this->_LastFile); break;
  1711. case 'template_path': $x = tbs_Misc_GetFilePart($this->_LastFile,0); break;
  1712. case 'name': $x = 'TinyButStrong'; break;
  1713. case 'logo': $x = '**TinyButStrong**'; break;
  1714. case 'charset': $x = $this->HtmlCharSet; break;
  1715. case '': $ErrMsg = 'it doesn\'t have any keyword.'; break;
  1716. case 'tplvars':
  1717. if ($Loc->SubNbr==2) {
  1718. $SubStart = 2;
  1719. $x = implode(',',array_keys($this->TplVars));
  1720. } else {
  1721. if (isset($this->TplVars[$Loc->SubLst[2]])) {
  1722. $SubStart = 3;
  1723. $x =& $this->TplVars[$Loc->SubLst[2]];
  1724. } else {
  1725. $ErrMsg = 'property TplVars doesn\'t have any item named \''.$Loc->SubLst[2].'\'.';
  1726. }
  1727. }
  1728. break;
  1729. default:
  1730. $IsSupported = false;
  1731. if (isset($this->_piOnSpecialVar)) {
  1732. $x = '';
  1733. $ArgLst = array(substr($Loc->SubName,1),&$IsSupported ,&$x, &$Loc->PrmLst,&$Txt,&$Loc->PosBeg,&$Loc->PosEnd,&$Loc);
  1734. $this->meth_PlugIn_RunAll($this->_piOnSpecialVar,$ArgLst) ;
  1735. }
  1736. if (!$IsSupported) $ErrMsg = '\''.$Loc->SubLst[1].'\' is an unsupported keyword.';
  1737. }
  1738. } else {
  1739. $ErrMsg = 'it doesn\'t have any subname.';
  1740. }
  1741. if ($ErrMsg!==false) {
  1742. $this->meth_Misc_Alert($Loc,$ErrMsg);
  1743. $x = '';
  1744. }
  1745. if ($Loc->PosBeg===false) {
  1746. return $Loc->PosEnd;
  1747. } else {
  1748. return $this->meth_Locator_Replace($Txt,$Loc,$x,$SubStart);
  1749. }
  1750. }
  1751. function meth_Merge_AutoAny($Name) {
  1752. // Merge any automatic fields (var, onload, onshow)
  1753. switch ($Name) {
  1754. case 'var': $this->meth_Merge_AutoVar($this->Source,true); return true;
  1755. case 'onload': $this->meth_Merge_AutoOn($this->Source,'onload',true,true); return true;
  1756. case 'onshow': $this->meth_Merge_AutoOn($this->Source,'onshow',false,true);return true;
  1757. default: return false;
  1758. }
  1759. }
  1760. function meth_Merge_FieldOutside(&$Txt,&$Src,&$LocR) {
  1761. $Pos = 0;
  1762. $SubStart = ($Src->CurrRec===false) ? false : 0;
  1763. do {
  1764. $Loc = $this->meth_Locator_FindTbs($Txt,$this->_CurrBlock,$Pos,'.');
  1765. if ($LocR->BlockFound and ($Loc!==false)) {
  1766. $OldEnd = $Loc->PosEnd;
  1767. if ($Loc->PosEnd>=$LocR->PosBeg) $Loc = false;
  1768. }
  1769. if ($Loc!==false) {
  1770. if ($Loc->SubName==='#') {
  1771. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$Src->RecNum,false);
  1772. } else {
  1773. $Pos = $this->meth_Locator_Replace($Txt,$Loc,$Src->CurrRec,$SubStart);
  1774. }
  1775. if ($LocR->BlockFound) {
  1776. $Delta = $Pos - $OldEnd;
  1777. $LocR->PosBeg += $Delta;
  1778. $LocR->PosEnd += $Delta;
  1779. }
  1780. }
  1781. } while ($Loc!==false);
  1782. }
  1783. function meth_Merge_SectionNormal(&$LocR,&$BlockId,&$CurrRec,&$RecNum,&$RecKey) {
  1784. $Txt = $LocR->BlockSrc[$BlockId];
  1785. $LocLst =& $LocR->BlockLoc[$BlockId];
  1786. $iMax = $LocLst[0];
  1787. $PosMax = strlen($Txt);
  1788. if ($RecNum===false) { // Erase all fields
  1789. $x = '';
  1790. // Chached locators
  1791. for ($i=$iMax;$i>0;$i--) {
  1792. if ($LocLst[$i]->PosBeg<$PosMax) {
  1793. $this->meth_Locator_Replace($Txt,$LocLst[$i],$x,false);
  1794. if ($LocLst[$i]->Enlarged) {
  1795. $PosMax = $LocLst[$i]->PosBeg;
  1796. $LocLst[$i]->PosBeg = $LocLst[$i]->PosBeg0;
  1797. $LocLst[$i]->PosEnd = $LocLst[$i]->PosEnd0;
  1798. $LocLst[$i]->Enlarged = false;
  1799. }
  1800. }
  1801. }
  1802. // Unchached locators
  1803. if ($LocR->BlockChk[$BlockId]) {
  1804. $BlockName =& $LocR->BlockName[$BlockId];
  1805. $Pos = 0;
  1806. while ($Loc = $this->meth_Locator_FindTbs($Txt,$BlockName,$Pos,'.')) $Pos = $this->meth_Locator_Replace($Txt,$Loc,$x,false);
  1807. }
  1808. } else {
  1809. // Chached locators
  1810. for ($i=$iMax;$i>0;$i--) {
  1811. if ($LocLst[$i]->PosBeg<$PosMax) {
  1812. if ($LocLst[$i]->IsRecInfo) {
  1813. if ($LocLst[$i]->RecInfo==='#') {
  1814. $this->meth_Locator_Replace($Txt,$LocLst[$i],$RecNum,false);
  1815. } else {
  1816. $this->meth_Locator_Replace($Txt,$LocLst[$i],$RecKey,false);
  1817. }
  1818. } else {
  1819. $this->meth_Locator_Replace($Txt,$LocLst[$i],$CurrRec,0);
  1820. }
  1821. if ($LocLst[$i]->Enlarged) {
  1822. $PosMax = $LocLst[$i]->PosBeg;
  1823. $LocLst[$i]->PosBeg = $LocLst[$i]->PosBeg0;
  1824. $LocLst[$i]->PosEnd = $LocLst[$i]->PosEnd0;
  1825. $LocLst[$i]->Enlarged = false;
  1826. }
  1827. }
  1828. }
  1829. // Unchached locators
  1830. if ($LocR->BlockChk[$BlockId]) {
  1831. $BlockName =& $LocR->BlockName[$BlockId];
  1832. foreach ($CurrRec as $key => $val) {
  1833. $Pos = 0;
  1834. $Name = $BlockName.'.'.$key;
  1835. while ($Loc = $this->meth_Locator_FindTbs($Txt,$Name,$Pos,'.')) $Pos = $this->meth_Locator_Replace($Txt,$Loc,$val,0);
  1836. }
  1837. $Pos = 0;
  1838. $Name = $BlockName.'.#';
  1839. while ($Loc = $this->meth_Locator_FindTbs($Txt,$Name,$Pos,'.')) $Pos = $this->meth_Locator_Replace($Txt,$Loc,$RecNum,0);
  1840. $Pos = 0;
  1841. $Name = $BlockName.'.$';
  1842. while ($Loc = $this->meth_Locator_FindTbs($Txt,$Name,$Pos,'.')) $Pos = $this->meth_Locator_Replace($Txt,$Loc,$RecKey,0);
  1843. }
  1844. }
  1845. return $Txt;
  1846. }
  1847. function meth_Merge_SectionSerial(&$LocR,&$SecId,&$SerialNum,&$SerialMax,&$SerialTxt) {
  1848. $Txt = $LocR->BlockSrc[$LocR->SectionBid[$SecId]];
  1849. $LocLst =& $LocR->BlockLoc[$LocR->SectionBid[$SecId]];
  1850. $OrdLst =& $LocR->SectionSerialOrd[$SecId];
  1851. // Prepare the Empty Item
  1852. if ($SerialNum<$SerialMax) {
  1853. if ($LocR->SerialEmpty===false) {
  1854. $F = false;
  1855. } else {
  1856. $EmptySrc =& $LocR->SerialEmpty;
  1857. }
  1858. }
  1859. // All Items
  1860. for ($i=$SerialMax;$i>0;$i--) {
  1861. $Sr = $OrdLst[$i];
  1862. if ($Sr>$SerialNum) {
  1863. if ($LocR->SerialEmpty===false) {
  1864. $k = $LocR->SectionSerialBid[$SecId][$Sr];
  1865. $EmptySrc = $this->meth_Merge_SectionNormal($LocR,$k,$F,$F,$F);
  1866. }
  1867. $Txt = substr_replace($Txt,$EmptySrc,$LocLst[$Sr]->PosBeg,$LocLst[$Sr]->PosEnd-$LocLst[$Sr]->PosBeg+1);
  1868. } else {
  1869. $Txt = substr_replace($Txt,$SerialTxt[$Sr],$LocLst[$Sr]->PosBeg,$LocLst[$Sr]->PosEnd-$LocLst[$Sr]->PosBeg+1);
  1870. }
  1871. }
  1872. // Update variables
  1873. $SerialNum = 0;
  1874. $SerialTxt = array();
  1875. return $Txt;
  1876. }
  1877. function meth_Merge_AutoOn(&$Txt,$Name,$TplVar,$AcceptGrp) {
  1878. // Merge [onload] or [onshow] fields and blocks
  1879. $GrpDisplayed = array();
  1880. $GrpExclusive = array();
  1881. $P1 = false;
  1882. $FieldBefore = false;
  1883. $Pos = 0;
  1884. if ($AcceptGrp) {
  1885. $ChrSub = '_';
  1886. } else {
  1887. $ChrSub = '';
  1888. }
  1889. while ($LocA=$this->meth_Locator_FindBlockNext($Txt,$Name,$Pos,$ChrSub,1,$P1,$FieldBefore)) {
  1890. if ($LocA->BlockFound) {
  1891. if (!isset($GrpDisplayed[$LocA->SubName])) {
  1892. $GrpDisplayed[$LocA->SubName] = false;
  1893. $GrpExclusive[$LocA->SubName] = !($AcceptGrp and ($LocA->SubName===''));
  1894. }
  1895. $Displayed =& $GrpDisplayed[$LocA->SubName];
  1896. $Exclusive =& $GrpExclusive[$LocA->SubName];
  1897. $DelBlock = false;
  1898. $DelField = false;
  1899. if ($Displayed and $Exclusive) {
  1900. $DelBlock = true;
  1901. } else {
  1902. if (isset($LocA->PrmLst['when'])) {
  1903. if (isset($LocA->PrmLst['several'])) $Exclusive=false;
  1904. $x = $LocA->PrmLst['when'];
  1905. $this->meth_Merge_AutoVar($x,false);
  1906. if (tbs_Misc_CheckCondition($x)) {
  1907. $DelField = true;
  1908. $Displayed = true;
  1909. } else {
  1910. $DelBlock = true;
  1911. }
  1912. } elseif(isset($LocA->PrmLst['default'])) {
  1913. if ($Displayed) {
  1914. $DelBlock = true;
  1915. } else {
  1916. $Displayed = true;
  1917. $DelField = true;
  1918. }
  1919. $Exclusive = true; // No more block displayed for the group after VisElse
  1920. }
  1921. }
  1922. // Del parts
  1923. if ($DelField) {
  1924. if ($LocA->PosBeg2!==false) $Txt = substr_replace($Txt,'',$LocA->PosBeg2,$LocA->PosEnd2-$LocA->PosBeg2+1);
  1925. $Txt = substr_replace($Txt,'',$LocA->PosBeg,$LocA->PosEnd-$LocA->PosBeg+1);
  1926. $Pos = $LocA->PosBeg;
  1927. } else {
  1928. if ($LocA->PosBeg2===false) {
  1929. tbs_Locator_EnlargeToTag($Txt,$LocA,$LocA->PrmLst['block'],false);
  1930. } else {
  1931. $LocA->PosEnd = $LocA->PosEnd2;
  1932. }
  1933. if ($DelBlock) {
  1934. $Txt = substr_replace($Txt,'',$LocA->PosBeg,$LocA->PosEnd-$LocA->PosBeg+1);
  1935. } else {
  1936. // Merge the block as if it was a field
  1937. $x = '';
  1938. $this->meth_Locator_Replace($Txt,$LocA,$x,false);
  1939. }
  1940. $Pos = $LocA->PosBeg;
  1941. }
  1942. } else { // Field
  1943. // Check for Template Var
  1944. if ($TplVar and isset($LocA->PrmLst['tplvars'])) {
  1945. $Ok = false;
  1946. foreach ($LocA->PrmLst as $Key => $Val) {
  1947. if ($Ok) {
  1948. $this->TplVars[$Key] = $Val;
  1949. } else {
  1950. if ($Key==='tplvars') $Ok = true;
  1951. }
  1952. }
  1953. }
  1954. $x = '';
  1955. $Pos = $this->meth_Locator_Replace($Txt,$LocA,$x,false);
  1956. $Pos = $LocA->PosBeg;
  1957. }
  1958. }
  1959. return count($GrpDisplayed);
  1960. }
  1961. // Convert a string to Html with several options
  1962. function meth_Conv_Html(&$Txt) {
  1963. if ($this->HtmlCharSet==='') {
  1964. $Txt = htmlspecialchars($Txt); // Faster
  1965. } elseif ($this->_HtmlCharFct) {
  1966. $Txt = call_user_func($this->HtmlCharSet,$Txt);
  1967. } else {
  1968. $Txt = htmlspecialchars($Txt,ENT_COMPAT,$this->HtmlCharSet);
  1969. }
  1970. }
  1971. // Standard alert message provided by TinyButStrong, return False is the message is cancelled.
  1972. function meth_Misc_Alert($Src,$Msg,$NoErrMsg=false,$SrcType=false) {
  1973. if ($this->NoErr) return false;
  1974. if (!is_string($Src)) {
  1975. if ($SrcType===false) $SrcType='in field';
  1976. $Src = $SrcType.' '.$this->_ChrOpen.$Src->FullName.'...'.$this->_ChrClose;
  1977. }
  1978. $x = '<br /><b>TinyButStrong Error</b> '.$Src.' : '.htmlentities($Msg);
  1979. if ($NoErrMsg) $x = $x.' <em>This message can be cancelled using parameter \'noerr\'.</em>';
  1980. $x = $x."<br />\n";
  1981. $x = str_replace($this->_ChrOpen,$this->_ChrProtect,$x);
  1982. echo $x;
  1983. return false;
  1984. }
  1985. function meth_Misc_ChangeMode($Init,&$Loc,&$CurrVal) {
  1986. if ($Init) {
  1987. // Save contents configuration
  1988. $Loc->SaveSrc =& $this->Source;
  1989. $Loc->SaveRender = $this->Render;
  1990. $Loc->SaveMode = $this->_Mode;
  1991. unset($this->Source); $this->Source = '';
  1992. $this->Render = TBS_OUTPUT;
  1993. $this->_Mode++; // Mode>0 means subtemplate mode
  1994. ob_start(); // Start buffuring output
  1995. } else {
  1996. // Restore contents configuration
  1997. $this->Source =& $Loc->SaveSrc;
  1998. $this->Render = $Loc->SaveRender;
  1999. $this->_Mode = $Loc->SaveMode;
  2000. $CurrVal = ob_get_contents();
  2001. ob_end_clean();
  2002. }
  2003. }
  2004. function meth_Misc_UserFctCheck(&$FctInfo,&$ErrMsg,$DataFct) {
  2005. $Ref = ($FctInfo[0]==='~');
  2006. if (isset($this->_UserFctLst[$FctInfo])) {
  2007. if ($Ref) $FctInfo = $this->_UserFctLst[$FctInfo];
  2008. return true;
  2009. }
  2010. $FctStr = $FctInfo;
  2011. if ($Ref) {
  2012. $ObjRef =& $this->ObjectRef;
  2013. $Lst = explode('.',substr($FctStr,1));
  2014. $iMax = count($Lst) - 1;
  2015. $Suff = 'tbsdb';
  2016. $iMax0 = $iMax;
  2017. if ($DataFct) {
  2018. $Suff = $Lst[$iMax];
  2019. $iMax--;
  2020. }
  2021. // Reading sub items
  2022. for ($i=0;$i<=$iMax;$i++) {
  2023. $x =& $Lst[$i];
  2024. if (is_object($ObjRef)) {
  2025. $ArgLst = tbs_Misc_CheckArgLst($x);
  2026. if (method_exists($ObjRef,$x)) {
  2027. if ($i<$iMax) {
  2028. $f = array(&$ObjRef,$x); unset($ObjRef);
  2029. $ObjRef = call_user_func_array($f,$ArgLst);
  2030. }
  2031. } elseif ($i===$iMax0) {
  2032. $ErrMsg = 'Expression \''.$FctStr.'\' is invalid because \''.$x.'\' is not a method in the class \''.get_class($ObjRef).'\'.';
  2033. return false;
  2034. } elseif (isset($ObjRef->$x)) {
  2035. $ObjRef =& $ObjRef->$x;
  2036. } else {
  2037. $ErrMsg = 'Expression \''.$FctStr.'\' is invalid because sub-item \''.$x.'\' is neither a method nor a property in the class \''.get_class($ObjRef).'\'.';
  2038. return false;
  2039. }
  2040. } elseif (($i<$iMax0) and is_array($ObjRef)) {
  2041. if (isset($ObjRef[$x])) {
  2042. $ObjRef =& $ObjRef[$x];
  2043. } else {
  2044. $ErrMsg = 'Expression \''.$FctStr.'\' is invalid because sub-item \''.$x.'\' is not a existing key in the array.';
  2045. return false;
  2046. }
  2047. } else {
  2048. $ErrMsg = 'Expression \''.$FctStr.'\' is invalid because '.(($i===0)?'property ObjectRef':'sub-item \''.$x.'\'').' is not an object'.(($i<$iMax)?' or an array.':'.');
  2049. return false;
  2050. }
  2051. }
  2052. // Referencing last item
  2053. if ($DataFct) {
  2054. $FctInfo = array(0=>'open',1=>'fetch',2=>'close');
  2055. foreach ($FctInfo as $i=>$x) {
  2056. $FctName = $Suff.'_'.$x;
  2057. if (method_exists($ObjRef,$FctName)) {
  2058. $FctInfo[$i] = array(&$ObjRef,$FctName);
  2059. } else {
  2060. $ErrMsg = 'Expression \''.$FctStr.'\' is invalid because method '.$FctName.' is not found.';
  2061. return false;
  2062. }
  2063. }
  2064. } else {
  2065. $FctInfo = array(&$ObjRef,$x);
  2066. }
  2067. $this->_UserFctLst[$FctStr] =& $FctInfo;
  2068. } else {
  2069. if (!function_exists($FctStr)) {
  2070. $x = explode('.',$FctStr);
  2071. if (count($x)==2) {
  2072. if (class_exists($x[0])) {
  2073. $FctInfo = $x;
  2074. } else {
  2075. $ErrMsg = 'user function \''.$FctStr.'\' is not correct because \''.$x[0].'\' is not a class name.'; return false;
  2076. }
  2077. } else {
  2078. $ErrMsg = 'user function \''.$FctStr.'\' is not found.'; return false;
  2079. }
  2080. }
  2081. $this->_UserFctLst[$FctStr] = true;
  2082. }
  2083. return true;
  2084. }
  2085. function meth_Misc_RunSubscript(&$CurrVal,$CurrPrm) {
  2086. // Run a subscript without any local variable damage
  2087. return @include($this->_Subscript);
  2088. }
  2089. function meth_PlugIn_RunAll(&$FctBank,&$ArgLst) {
  2090. $OkAll = true;
  2091. foreach ($FctBank as $FctInfo) {
  2092. $Ok = call_user_func_array($FctInfo,$ArgLst);
  2093. if (!is_null($Ok)) $OkAll = ($OkAll and $Ok);
  2094. }
  2095. return $OkAll;
  2096. }
  2097. function meth_PlugIn_Install($PlugInId,$ArgLst,$Auto) {
  2098. $ErrMsg = 'with plug-in \''.$PlugInId.'\'';
  2099. if (class_exists($PlugInId)) {
  2100. // Create an instance
  2101. $IsObj = true;
  2102. $PiRef = new $PlugInId;
  2103. $PiRef->TBS =& $this;
  2104. if (!method_exists($PiRef,'OnInstall')) return $this->meth_Misc_Alert($ErrMsg,'OnInstall() method is not found.');
  2105. $FctRef = array(&$PiRef,'OnInstall');
  2106. } else {
  2107. $FctRef = 'tbspi_'.$PlugInId.'_OnInstall';
  2108. if(function_exists($FctRef)) {
  2109. $IsObj = false;
  2110. $PiRef = true;
  2111. } else {
  2112. return $this->meth_Misc_Alert($ErrMsg,'no class named \''.$PlugInId.'\' is found, and no function named \''.$FctRef.'\' is found.');
  2113. }
  2114. }
  2115. $EventLst = call_user_func_array($FctRef,$ArgLst);
  2116. if (is_string($EventLst)) $EventLst = explode(',',$EventLst);
  2117. if (!is_array($EventLst)) return $this->meth_Misc_Alert($ErrMsg,'OnInstall() method does not return an array.');
  2118. // Add activated methods
  2119. $EventRef = explode(',','OnCommand,BeforeLoadTemplate,AfterLoadTemplate,BeforeShow,AfterShow,OnData,OnFormat,OnOperation,BeforeMergeBlock,OnMergeSection,AfterMergeBlock,OnSpecialVar,OnMergeField');
  2120. foreach ($EventLst as $Event) {
  2121. $Event = trim($Event);
  2122. if (!in_array($Event,$EventRef)) return $this->meth_Misc_Alert($ErrMsg,'OnInstall() method return an unknowed plug-in event named \''.$Event.'\' (case-sensitive).');
  2123. if ($IsObj) {
  2124. if (!method_exists($PiRef,$Event)) return $this->meth_Misc_Alert($ErrMsg,'OnInstall() has returned a Plug-in event named \''.$Event.'\' which is not found.');
  2125. $FctRef = array(&$PiRef,$Event);
  2126. } else {
  2127. $FctRef = 'tbspi_'.$PlugInId.'_'.$Event;
  2128. if (!function_exists($FctRef)) return $this->meth_Misc_Alert($ErrMsg,'requiered function \''.$FctRef.'\' is not found.');
  2129. }
  2130. // Save information into the corresponding property
  2131. $PropName = '_pi'.$Event;
  2132. if (!isset($this->$PropName)) $this->$PropName = array();
  2133. $PropRef =& $this->$PropName;
  2134. $PropRef[$PlugInId] = $FctRef;
  2135. // Flags saying if a plugin is installed
  2136. switch ($Event) {
  2137. case 'OnCommand': break;
  2138. case 'OnSpecialVar': break;
  2139. case 'OnOperation': break;
  2140. case 'OnFormat': $this->_piOnFrm_Ok = true; break;
  2141. default: $this->_PlugIns_Ok = true; break;
  2142. }
  2143. }
  2144. $this->_PlugIns[$PlugInId] =& $PiRef;
  2145. return true;
  2146. }
  2147. function meth_Misc_Format(&$Value,&$PrmLst) {
  2148. // This function return the formated representation of a Date/Time or numeric variable using a 'VB like' format syntax instead of the PHP syntax.
  2149. $FrmStr = $PrmLst['frm'];
  2150. $CheckNumeric = true;
  2151. if (is_string($Value)) $Value = trim($Value);
  2152. // Manage Multi format strings
  2153. if (strpos($FrmStr,'|')!==false) {
  2154. // Save the format if it doesn't exist
  2155. if (isset($this->_FrmMultiLst[$FrmStr])) {
  2156. $FrmLst =& $this->_FrmMultiLst[$FrmStr];
  2157. } else {
  2158. $FrmLst = explode('|',$FrmStr); // syntax : Postive|Negative|Zero|Null
  2159. $FrmNbr = count($FrmLst);
  2160. $FrmLst['abs'] = ($FrmNbr>1);
  2161. if ($FrmNbr<3) $FrmLst[2] =& $FrmLst[0]; // zero
  2162. if ($FrmNbr<4) $FrmLst[3] = ''; // null
  2163. $this->_FrmMultiLst[$FrmStr] = $FrmLst;
  2164. }
  2165. // Select the format
  2166. if (is_numeric($Value)) {
  2167. if (is_string($Value)) $Value = 0.0 + $Value;
  2168. if ($Value>0) {
  2169. $FrmStr =& $FrmLst[0];
  2170. } elseif ($Value<0) {
  2171. $FrmStr =& $FrmLst[1];
  2172. if ($FrmLst['abs']) $Value = abs($Value);
  2173. } else { // zero
  2174. $FrmStr =& $FrmLst[2];
  2175. $Minus = '';
  2176. }
  2177. $CheckNumeric = false;
  2178. } else {
  2179. $Value = ''.$Value;
  2180. if ($Value==='') {
  2181. return $FrmLst[3]; // Null value
  2182. } else {
  2183. $t = strtotime($Value); // We look if it's a date
  2184. if (($t===-1) or ($t===false)) { // Date not recognized
  2185. return $FrmLst[1];
  2186. } elseif ($t===943916400) { // Date to zero
  2187. return $FrmLst[2];
  2188. } else { // It's a date
  2189. $Value = $t;
  2190. $FrmStr =& $FrmLst[0];
  2191. }
  2192. }
  2193. }
  2194. }
  2195. if ($FrmStr==='') return '';
  2196. // Retrieve the correct simple format
  2197. if (!isset($this->_FrmSimpleLst[$FrmStr])) $this->meth_Misc_FormatSave($FrmStr);
  2198. $Frm =& $this->_FrmSimpleLst[$FrmStr];
  2199. switch ($Frm['type']) {
  2200. case 'num' :
  2201. // NUMERIC
  2202. if ($CheckNumeric) {
  2203. if (is_numeric($Value)) {
  2204. if (is_string($Value)) $Value = 0.0 + $Value;
  2205. } else {
  2206. return ''.$Value;
  2207. }
  2208. }
  2209. if ($Frm['PerCent']) $Value = $Value * 100;
  2210. $Value = number_format($Value,$Frm['DecNbr'],$Frm['DecSep'],$Frm['ThsSep']);
  2211. return substr_replace($FrmStr,$Value,$Frm['Pos'],$Frm['Len']);
  2212. break;
  2213. case 'date' :
  2214. // DATE
  2215. if (is_string($Value)) {
  2216. if ($Value==='') return '';
  2217. $x = strtotime($Value);
  2218. if (($x===-1) or ($x===false)) {
  2219. if (!is_numeric($Value)) $Value = 0;
  2220. } else {
  2221. $Value =& $x;
  2222. }
  2223. } else {
  2224. if (!is_numeric($Value)) return ''.$Value;
  2225. }
  2226. if (isset($PrmLst['locale'])) {
  2227. return strftime($Frm['str_loc'],$Value);
  2228. } else {
  2229. return date($Frm['str_us'],$Value);
  2230. }
  2231. break;
  2232. default:
  2233. return $Frm['string'];
  2234. break;
  2235. }
  2236. }
  2237. function meth_Misc_FormatSave(&$FrmStr) {
  2238. $nPosEnd = strrpos($FrmStr,'0');
  2239. if ($nPosEnd!==false) {
  2240. // Numeric format
  2241. $nDecSep = '.';
  2242. $nDecNbr = 0;
  2243. $nDecOk = true;
  2244. if (substr($FrmStr,$nPosEnd+1,1)==='.') {
  2245. $nPosEnd++;
  2246. $nPosCurr = $nPosEnd;
  2247. } else {
  2248. $nPosCurr = $nPosEnd - 1;
  2249. while (($nPosCurr>=0) and ($FrmStr[$nPosCurr]==='0')) {
  2250. $nPosCurr--;
  2251. }
  2252. if (($nPosCurr>=1) and ($FrmStr[$nPosCurr-1]==='0')) {
  2253. $nDecSep = $FrmStr[$nPosCurr];
  2254. $nDecNbr = $nPosEnd - $nPosCurr;
  2255. } else {
  2256. $nDecOk = false;
  2257. }
  2258. }
  2259. // Thousand separator
  2260. $nThsSep = '';
  2261. if (($nDecOk) and ($nPosCurr>=5)) {
  2262. if ((substr($FrmStr,$nPosCurr-3,3)==='000') and ($FrmStr[$nPosCurr-4]!=='') and ($FrmStr[$nPosCurr-5]==='0')) {
  2263. $nPosCurr = $nPosCurr-4;
  2264. $nThsSep = $FrmStr[$nPosCurr];
  2265. }
  2266. }
  2267. // Pass next zero
  2268. if ($nDecOk) $nPosCurr--;
  2269. while (($nPosCurr>=0) and ($FrmStr[$nPosCurr]==='0')) {
  2270. $nPosCurr--;
  2271. }
  2272. // Percent
  2273. $nPerCent = (strpos($FrmStr,'%')===false) ? false : true;
  2274. $this->_FrmSimpleLst[$FrmStr] = array('type'=>'num','Pos'=>($nPosCurr+1),'Len'=>($nPosEnd-$nPosCurr),'ThsSep'=>$nThsSep,'DecSep'=>$nDecSep,'DecNbr'=>$nDecNbr,'PerCent'=>$nPerCent);
  2275. } else { // if ($nPosEnd!==false)
  2276. // Date format
  2277. $FrmPHP = '';
  2278. $FrmLOC = '';
  2279. $Local = false;
  2280. $StrIn = false;
  2281. $iMax = strlen($FrmStr);
  2282. $Cnt = 0;
  2283. for ($i=0;$i<$iMax;$i++) {
  2284. if ($StrIn) {
  2285. // We are in a string part
  2286. if ($FrmStr[$i]===$StrChr) {
  2287. if (substr($FrmStr,$i+1,1)===$StrChr) {
  2288. $FrmPHP .= '\\'.$FrmStr[$i]; // protected char
  2289. $FrmLOC .= $FrmStr[$i];
  2290. $i++;
  2291. } else {
  2292. $StrIn = false;
  2293. }
  2294. } else {
  2295. $FrmPHP .= '\\'.$FrmStr[$i]; // protected char
  2296. $FrmLOC .= $FrmStr[$i];
  2297. }
  2298. } else {
  2299. if (($FrmStr[$i]==='"') or ($FrmStr[$i]==='\'')) {
  2300. // Check if we have the opening string char
  2301. $StrIn = true;
  2302. $StrChr = $FrmStr[$i];
  2303. } else {
  2304. $Cnt++;
  2305. if (strcasecmp(substr($FrmStr,$i,2),'hh' )===0) { $FrmPHP .= 'H'; $FrmLOC .= '%H'; $i += 1; }
  2306. elseif (strcasecmp(substr($FrmStr,$i,2),'hm' )===0) { $FrmPHP .= 'h'; $FrmLOC .= '%I'; $i += 1; }
  2307. elseif (strcasecmp(substr($FrmStr,$i,4),'ampm')===0) { $FrmPHP .= substr($FrmStr,$i,1); $FrmLOC .= '%p'; $i += 3; } // $Fmp = 'A' or 'a'
  2308. elseif (strcasecmp(substr($FrmStr,$i,2),'nn' )===0) { $FrmPHP .= 'i'; $FrmLOC .= '%M'; $i += 1; }
  2309. elseif (strcasecmp(substr($FrmStr,$i,2),'ss' )===0) { $FrmPHP .= 's'; $FrmLOC .= '%S'; $i += 1; }
  2310. elseif (strcasecmp(substr($FrmStr,$i,2),'xx' )===0) { $FrmPHP .= 'S'; $FrmLOC .= '' ; $i += 1; }
  2311. elseif (strcasecmp(substr($FrmStr,$i,4),'yyyy')===0) { $FrmPHP .= 'Y'; $FrmLOC .= '%Y'; $i += 3; }
  2312. elseif (strcasecmp(substr($FrmStr,$i,2),'yy' )===0) { $FrmPHP .= 'y'; $FrmLOC .= '%y'; $i += 1; }
  2313. elseif (strcasecmp(substr($FrmStr,$i,4),'mmmm')===0) { $FrmPHP .= 'F'; $FrmLOC .= '%B'; $i += 3; }
  2314. elseif (strcasecmp(substr($FrmStr,$i,3),'mmm' )===0) { $FrmPHP .= 'M'; $FrmLOC .= '%b'; $i += 2; }
  2315. elseif (strcasecmp(substr($FrmStr,$i,2),'mm' )===0) { $FrmPHP .= 'm'; $FrmLOC .= '%m'; $i += 1; }
  2316. elseif (strcasecmp(substr($FrmStr,$i,1),'m' )===0) { $FrmPHP .= 'n'; $FrmLOC .= '%m'; }
  2317. elseif (strcasecmp(substr($FrmStr,$i,4),'wwww')===0) { $FrmPHP .= 'l'; $FrmLOC .= '%A'; $i += 3; }
  2318. elseif (strcasecmp(substr($FrmStr,$i,3),'www' )===0) { $FrmPHP .= 'D'; $FrmLOC .= '%a'; $i += 2; }
  2319. elseif (strcasecmp(substr($FrmStr,$i,1),'w' )===0) { $FrmPHP .= 'w'; $FrmLOC .= '%u'; }
  2320. elseif (strcasecmp(substr($FrmStr,$i,4),'dddd')===0) { $FrmPHP .= 'l'; $FrmLOC .= '%A'; $i += 3; }
  2321. elseif (strcasecmp(substr($FrmStr,$i,3),'ddd' )===0) { $FrmPHP .= 'D'; $FrmLOC .= '%a'; $i += 2; }
  2322. elseif (strcasecmp(substr($FrmStr,$i,2),'dd' )===0) { $FrmPHP .= 'd'; $FrmLOC .= '%d'; $i += 1; }
  2323. elseif (strcasecmp(substr($FrmStr,$i,1),'d' )===0) { $FrmPHP .= 'j'; $FrmLOC .= '%d'; }
  2324. else {
  2325. $FrmPHP .= '\\'.$FrmStr[$i]; // protected char
  2326. $FrmLOC .= $FrmStr[$i]; // protected char
  2327. $Cnt--;
  2328. }
  2329. }
  2330. } //-> if ($StrIn) {...} else
  2331. } //-> for ($i=0;$i<$iMax;$i++)
  2332. if ($Cnt>0) {
  2333. $this->_FrmSimpleLst[$FrmStr] = array('type'=>'date','str_us'=>$FrmPHP,'str_loc'=>$FrmLOC);
  2334. } else {
  2335. $this->_FrmSimpleLst[$FrmStr] = array('type'=>'else','string'=>$FrmStr);
  2336. }
  2337. } // if ($nPosEnd!==false) {...} else
  2338. }
  2339. } // class clsTinyButStrong
  2340. // *********************************************
  2341. function tbs_Misc_ConvSpe(&$Loc) {
  2342. if ($Loc->ConvMode!==2) {
  2343. $Loc->ConvMode = 2;
  2344. $Loc->ConvEsc = false;
  2345. $Loc->ConvWS = false;
  2346. $Loc->ConvJS = false;
  2347. }
  2348. }
  2349. function tbs_Misc_CheckArgLst(&$Str) {
  2350. $ArgLst = array();
  2351. if (substr($Str,-1,1)===')') {
  2352. $pos = strpos($Str,'(');
  2353. if ($pos!==false) {
  2354. $ArgLst = explode(',',substr($Str,$pos+1,strlen($Str)-$pos-2));
  2355. $Str = substr($Str,0,$pos);
  2356. }
  2357. }
  2358. return $ArgLst;
  2359. }
  2360. function tbs_Misc_CheckCondition($Str) {
  2361. // Check if an expression like "exrp1=expr2" is true or false.
  2362. // Find operator and position
  2363. $Ope = '=';
  2364. $Len = 1;
  2365. $Max = strlen($Str)-1;
  2366. $Pos = strpos($Str,$Ope);
  2367. if ($Pos===false) {
  2368. $Ope = '+';
  2369. $Pos = strpos($Str,$Ope);
  2370. if ($Pos===false) return false;
  2371. if (($Pos>0) and ($Str[$Pos-1]==='-')) {
  2372. $Ope = '-+'; $Pos--; $Len=2;
  2373. } elseif (($Pos<$Max) and ($Str[$Pos+1]==='-')) {
  2374. $Ope = '+-'; $Len=2;
  2375. } else {
  2376. return false;
  2377. }
  2378. } else {
  2379. if ($Pos>0) {
  2380. $x = $Str[$Pos-1];
  2381. if ($x==='!') {
  2382. $Ope = '!='; $Pos--; $Len=2;
  2383. } elseif ($x==='~') {
  2384. $Ope = '~='; $Pos--; $Len=2;
  2385. } elseif ($Pos<$Max) {
  2386. $y = $Str[$Pos+1];
  2387. if ($y==='=') {
  2388. $Len=2;
  2389. } elseif (($x==='+') and ($y==='-')) {
  2390. $Ope = '+=-'; $Pos--; $Len=3;
  2391. } elseif (($x==='-') and ($y==='+')) {
  2392. $Ope = '-=+'; $Pos--; $Len=3;
  2393. }
  2394. } else {
  2395. }
  2396. }
  2397. }
  2398. // Read values
  2399. $Val1 = trim(substr($Str,0,$Pos));
  2400. $Nude1 = tbs_Misc_DelDelimiter($Val1,'\'');
  2401. $Val2 = trim(substr($Str,$Pos+$Len));
  2402. $Nude2 = tbs_Misc_DelDelimiter($Val2,'\'');
  2403. // Compare values
  2404. if ($Ope==='=') {
  2405. return (strcasecmp($Val1,$Val2)==0);
  2406. } elseif ($Ope==='!=') {
  2407. return (strcasecmp($Val1,$Val2)!=0);
  2408. } elseif ($Ope==='~=') {
  2409. return (preg_match($Val2,$Val1)>0);
  2410. } else {
  2411. if ($Nude1) $Val1 = (float) $Val1;
  2412. if ($Nude2) $Val2 = (float) $Val2;
  2413. if ($Ope==='+-') {
  2414. return ($Val1>$Val2);
  2415. } elseif ($Ope==='-+') {
  2416. return ($Val1 < $Val2);
  2417. } elseif ($Ope==='+=-') {
  2418. return ($Val1 >= $Val2);
  2419. } elseif ($Ope==='-=+') {
  2420. return ($Val1<=$Val2);
  2421. } else {
  2422. return false;
  2423. }
  2424. }
  2425. }
  2426. function tbs_Misc_DelDelimiter(&$Txt,$Delim) {
  2427. // Delete the string delimiters
  2428. $len = strlen($Txt);
  2429. if (($len>1) and ($Txt[0]===$Delim)) {
  2430. if ($Txt[$len-1]===$Delim) $Txt = substr($Txt,1,$len-2);
  2431. return false;
  2432. } else {
  2433. return true;
  2434. }
  2435. }
  2436. function tbs_Misc_GetFile(&$Txt,$File) {
  2437. // Load the content of a file into the text variable.
  2438. $Txt = '';
  2439. $fd = @fopen($File, 'r'); // 'rb' if binary for some OS
  2440. if ($fd===false) return false;
  2441. $fs = @filesize($File); // return False for an URL
  2442. if ($fs===false) {
  2443. while (!feof($fd)) $Txt .= fread($fd,4096);
  2444. } else {
  2445. if ($fs>0) $Txt = fread($fd,$fs);
  2446. }
  2447. fclose($fd);
  2448. return true;
  2449. }
  2450. function tbs_Misc_GetFilePart($File,$Part) {
  2451. $Pos = strrpos($File,'/');
  2452. if ($Part===0) { // Path
  2453. if ($Pos===false) {
  2454. return '';
  2455. } else {
  2456. return substr($File,0,$Pos+1);
  2457. }
  2458. } else { // File
  2459. if ($Pos===false) {
  2460. return $File;
  2461. } else {
  2462. return substr($File,$Pos+1);
  2463. }
  2464. }
  2465. }
  2466. function tbs_Locator_SectionAddBlk(&$LocR,$BlockName,$Txt,$PrmLst) {
  2467. $i = ++$LocR->BlockNbr;
  2468. $LocR->BlockName[$i] = $BlockName;
  2469. $LocR->BlockSrc[$i] = $Txt;
  2470. $LocR->BlockLoc[$i] = array(0=>0);
  2471. $LocR->BlockChk[$i] = true;
  2472. $LocR->BlockPrm[$i] = $PrmLst;
  2473. return $i;
  2474. }
  2475. function tbs_Locator_SectionAddGrp(&$LocR,$Bid,$Type,$Field) {
  2476. if ($Type==='H') {
  2477. if ($LocR->HeaderFound===false) {
  2478. $LocR->HeaderFound = true;
  2479. $LocR->HeaderNbr = 0;
  2480. $LocR->HeaderBid = array(); // 1 to HeaderNbr
  2481. $LocR->HeaderPrevValue = array(); // 1 to HeaderNbr
  2482. $LocR->HeaderField = array(); // 1 to HeaderNbr
  2483. }
  2484. $i = ++$LocR->HeaderNbr;
  2485. $LocR->HeaderBid[$i] = $Bid;
  2486. $LocR->HeaderPrevValue[$i] = false;
  2487. $LocR->HeaderField[$i] = $Field;
  2488. } else {
  2489. if ($LocR->FooterFound===false) {
  2490. $LocR->FooterFound = true;
  2491. $LocR->FooterNbr = 0;
  2492. $LocR->FooterBid = array(); // 1 to FooterNbr
  2493. $LocR->FooterPrevValue = array(); // 1 to FooterNbr
  2494. $LocR->FooterField = array(); // 1 to FooterNbr
  2495. $LocR->FooterIsFooter = array(); // 1 to FooterNbr
  2496. }
  2497. $i = ++$LocR->FooterNbr;
  2498. $LocR->FooterBid[$i] = $Bid;
  2499. $LocR->FooterPrevValue[$i] = false;
  2500. if ($Type==='F') {
  2501. $LocR->FooterField[$i] = $Field;
  2502. $LocR->FooterIsFooter[$i] = true;
  2503. } else {
  2504. $LocR->FooterField[$i] = $Field;
  2505. $LocR->FooterIsFooter[$i] = false;
  2506. }
  2507. }
  2508. }
  2509. function tbs_Locator_PrmRead(&$Txt,$Pos,$HtmlTag,$DelimChrs,$BegStr,$EndStr,&$Loc,&$PosEnd) {
  2510. $BegLen = strlen($BegStr);
  2511. $BegChr = $BegStr[0];
  2512. $BegIs1 = ($BegLen===1);
  2513. $DelimIdx = false;
  2514. $DelimCnt = 0;
  2515. $DelimChr = '';
  2516. $BegCnt = 0;
  2517. $SubName = $Loc->SubOk;
  2518. $Status = 0; // 0: name not started, 1: name started, 2: name ended, 3: equal found, 4: value started
  2519. $PosName = 0;
  2520. $PosNend = 0;
  2521. $PosVal = 0;
  2522. // Variables for checking the loop
  2523. $PosEnd = strpos($Txt,$EndStr,$Pos);
  2524. if ($PosEnd===false) return;
  2525. $Continue = ($Pos<$PosEnd);
  2526. while ($Continue) {
  2527. $Chr = $Txt[$Pos];
  2528. if ($DelimIdx) { // Reading in the string
  2529. if ($Chr===$DelimChr) { // Quote found
  2530. if ($Chr===$Txt[$Pos+1]) { // Double Quote => the string continue with un-double the quote
  2531. $Pos++;
  2532. } else { // Simple Suote => end of string
  2533. $DelimIdx = false;
  2534. }
  2535. }
  2536. } else { // Reading outside the string
  2537. if ($BegCnt===0) {
  2538. // Analyzing parameters
  2539. $CheckChr = false;
  2540. if (($Chr===' ') or ($Chr==="\r") or ($Chr==="\n")) {
  2541. if ($Status===1) {
  2542. $Status = 2;
  2543. $PosNend = $Pos;
  2544. } elseif ($HtmlTag and ($Status===4)) {
  2545. tbs_Locator_PrmCompute($Txt,$Loc,$SubName,$Status,$HtmlTag,$DelimChr,$DelimCnt,$PosName,$PosNend,$PosVal,$Pos);
  2546. $Status = 0;
  2547. }
  2548. } elseif (($HtmlTag===false) and ($Chr===';')) {
  2549. tbs_Locator_PrmCompute($Txt,$Loc,$SubName,$Status,$HtmlTag,$DelimChr,$DelimCnt,$PosName,$PosNend,$PosVal,$Pos);
  2550. $Status = 0;
  2551. } elseif ($Status===4) {
  2552. $CheckChr = true;
  2553. } elseif ($Status===3) {
  2554. $Status = 4;
  2555. $DelimCnt = 0;
  2556. $PosVal = $Pos;
  2557. $CheckChr = true;
  2558. } elseif ($Status===2) {
  2559. if ($Chr==='=') {
  2560. $Status = 3;
  2561. } elseif ($HtmlTag) {
  2562. tbs_Locator_PrmCompute($Txt,$Loc,$SubName,$Status,$HtmlTag,$DelimChr,$DelimCnt,$PosName,$PosNend,$PosVal,$Pos);
  2563. $Status = 1;
  2564. $PosName = $Pos;
  2565. $CheckChr = true;
  2566. } else {
  2567. $Status = 4;
  2568. $DelimCnt = 0;
  2569. $PosVal = $Pos;
  2570. $CheckChr = true;
  2571. }
  2572. } elseif ($Status===1) {
  2573. if ($Chr==='=') {
  2574. $Status = 3;
  2575. $PosNend = $Pos;
  2576. } else {
  2577. $CheckChr = true;
  2578. }
  2579. } else {
  2580. $Status = 1;
  2581. $PosName = $Pos;
  2582. $CheckChr = true;
  2583. }
  2584. if ($CheckChr) {
  2585. $DelimIdx = strpos($DelimChrs,$Chr);
  2586. if ($DelimIdx===false) {
  2587. if ($Chr===$BegChr) {
  2588. if ($BegIs1) {
  2589. $BegCnt++;
  2590. } elseif(substr($Txt,$Pos,$BegLen)===$BegStr) {
  2591. $BegCnt++;
  2592. }
  2593. }
  2594. } else {
  2595. $DelimChr = $DelimChrs[$DelimIdx];
  2596. $DelimCnt++;
  2597. $DelimIdx = true;
  2598. }
  2599. }
  2600. } else {
  2601. if ($Chr===$BegChr) {
  2602. if ($BegIs1) {
  2603. $BegCnt++;
  2604. } elseif(substr($Txt,$Pos,$BegLen)===$BegStr) {
  2605. $BegCnt++;
  2606. }
  2607. }
  2608. }
  2609. }
  2610. // Next char
  2611. $Pos++;
  2612. // We check if it's the end
  2613. if ($Pos===$PosEnd) {
  2614. if ($DelimIdx===false) {
  2615. if ($BegCnt>0) {
  2616. $BegCnt--;
  2617. } else {
  2618. $Continue = false;
  2619. }
  2620. }
  2621. if ($Continue) {
  2622. $PosEnd = strpos($Txt,$EndStr,$PosEnd+1);
  2623. if ($PosEnd===false) return;
  2624. } else {
  2625. tbs_Locator_PrmCompute($Txt,$Loc,$SubName,$Status,$HtmlTag,$DelimChr,$DelimCnt,$PosName,$PosNend,$PosVal,$Pos);
  2626. }
  2627. }
  2628. }
  2629. $PosEnd = $PosEnd + (strlen($EndStr)-1);
  2630. }
  2631. function tbs_Locator_PrmCompute(&$Txt,&$Loc,&$SubName,$Status,$HtmlTag,$DelimChr,$DelimCnt,$PosName,$PosNend,$PosVal,$Pos) {
  2632. if ($Status===0) {
  2633. $SubName = false;
  2634. } else {
  2635. if ($Status===1) {
  2636. $x = substr($Txt,$PosName,$Pos-$PosName);
  2637. } else {
  2638. $x = substr($Txt,$PosName,$PosNend-$PosName);
  2639. }
  2640. if ($HtmlTag) $x = strtolower($x);
  2641. if ($SubName) {
  2642. $Loc->SubName = $x;
  2643. $SubName = false;
  2644. } else {
  2645. if ($Status===4) {
  2646. $v = trim(substr($Txt,$PosVal,$Pos-$PosVal));
  2647. if ($DelimCnt===1) { // Delete quotes inside the value
  2648. if ($v[0]===$DelimChr) {
  2649. $len = strlen($v);
  2650. if ($v[$len-1]===$DelimChr) {
  2651. $v = substr($v,1,$len-2);
  2652. $v = str_replace($DelimChr.$DelimChr,$DelimChr,$v);
  2653. }
  2654. }
  2655. }
  2656. } else {
  2657. $v = true;
  2658. }
  2659. if ($x==='if') {
  2660. tbs_Locator_PrmIfThen($Loc,true,$v);
  2661. } elseif ($x==='then') {
  2662. tbs_Locator_PrmIfThen($Loc,false,$v);
  2663. } else {
  2664. $Loc->PrmLst[$x] = $v;
  2665. }
  2666. }
  2667. }
  2668. }
  2669. function tbs_Locator_PrmIfThen(&$Loc,$IsIf,$Val) {
  2670. $nbr =& $Loc->PrmIfNbr;
  2671. if ($nbr===false) {
  2672. $nbr = 0;
  2673. $Loc->PrmIf = array();
  2674. $Loc->PrmIfVar = array();
  2675. $Loc->PrmThen = array();
  2676. $Loc->PrmThenVar = array();
  2677. $Loc->PrmElseVar = true;
  2678. }
  2679. if ($IsIf) {
  2680. $nbr++;
  2681. $Loc->PrmIf[$nbr] = $Val;
  2682. $Loc->PrmIfVar[$nbr] = true;
  2683. } else {
  2684. $nbr2 = $nbr;
  2685. if ($nbr2===false) $nbr2 = 1; // Only the first 'then' can be placed before its 'if'. This is for compatibility.
  2686. $Loc->PrmThen[$nbr2] = $Val;
  2687. $Loc->PrmThenVar[$nbr2] = true;
  2688. }
  2689. }
  2690. function tbs_Locator_EnlargeToStr(&$Txt,&$Loc,$StrBeg,$StrEnd) {
  2691. /*
  2692. This function enables to enlarge the pos limits of the Locator.
  2693. If the search result is not correct, $PosBeg must not change its value, and $PosEnd must be False.
  2694. This is because of the calling function.
  2695. */
  2696. // Search for the begining string
  2697. $Pos = $Loc->PosBeg;
  2698. $Ok = false;
  2699. do {
  2700. $Pos = strrpos(substr($Txt,0,$Pos),$StrBeg[0]);
  2701. if ($Pos!==false) {
  2702. if (substr($Txt,$Pos,strlen($StrBeg))===$StrBeg) $Ok = true;
  2703. }
  2704. } while ( (!$Ok) and ($Pos!==false) );
  2705. if ($Ok) {
  2706. $PosEnd = strpos($Txt,$StrEnd,$Loc->PosEnd + 1);
  2707. if ($PosEnd===false) {
  2708. $Ok = false;
  2709. } else {
  2710. $Loc->PosBeg = $Pos;
  2711. $Loc->PosEnd = $PosEnd + strlen($StrEnd) - 1;
  2712. }
  2713. }
  2714. return $Ok;
  2715. }
  2716. function tbs_Locator_EnlargeToTag(&$Txt,&$Loc,$TagLst,$RetInnerSrc) {
  2717. //Modify $Loc, return false if tags not found, returns the inner source of tag if $RetInnerSrc=true
  2718. // Analyze string
  2719. $Ref = 0;
  2720. $LevelStop = 0;
  2721. $TagLst = explode('+',$TagLst);
  2722. $TagIsSgl = array();
  2723. $TagMax = count($TagLst) - 1;
  2724. for ($i=0;$i<=$TagMax;$i++) {
  2725. do { // Check parentheses, relative position and single tag
  2726. $tag =& $TagLst[$i];
  2727. $tag = trim($tag);
  2728. $x = strlen($tag) - 1; // pos of last char
  2729. if (($x>1) and ($tag[0]==='(') and ($tag[$x]===')')) {
  2730. if ($Ref===0) $Ref = $i;
  2731. if ($Ref===$i) $LevelStop++;
  2732. $tag = substr($tag,1,$x-1);
  2733. } else {
  2734. if (($x>=0) and ($tag[$x]==='/')) {
  2735. $TagIsSgl[$i] = true;
  2736. $tag = substr($tag,0,$x);
  2737. } else {
  2738. $TagIsSgl[$i] = false;
  2739. }
  2740. $x = false;
  2741. }
  2742. } while ($x!==false);
  2743. }
  2744. // Find tags that embeds the locator
  2745. if ($LevelStop===0) $LevelStop = 1;
  2746. $TagO = tbs_Html_FindTag($Txt,$TagLst[$Ref],true,$Loc->PosBeg-1,false,$LevelStop,false);
  2747. if ($TagO===false) return false;
  2748. $PosBeg = $TagO->PosBeg;
  2749. if ($TagIsSgl[$Ref]) {
  2750. $PosEnd = max($Loc->PosEnd,$TagO->PosEnd);
  2751. $InnerLim = $PosEnd + 1;
  2752. } else {
  2753. $TagC = tbs_Html_FindTag($Txt,$TagLst[$Ref],false,$Loc->PosEnd+1,true,-$LevelStop,false);
  2754. if ($TagC==false) return false;
  2755. $PosEnd = $TagC->PosEnd;
  2756. $InnerLim = $TagC->PosBeg;
  2757. }
  2758. $RetVal = true;
  2759. if ($RetInnerSrc) {
  2760. $RetVal = '';
  2761. if ($Loc->PosBeg>$TagO->PosEnd) $RetVal .= substr($Txt,$TagO->PosEnd+1,min($Loc->PosBeg,$InnerLim)-$TagO->PosEnd-1);
  2762. if ($Loc->PosEnd<$InnerLim) $RetVal .= substr($Txt,max($Loc->PosEnd,$TagO->PosEnd)+1,$InnerLim-max($Loc->PosEnd,$TagO->PosEnd)-1);
  2763. }
  2764. // Forward
  2765. $TagC = true;
  2766. for ($i=$Ref+1;$i<=$TagMax;$i++) {
  2767. $x = $TagLst[$i];
  2768. if (($x!=='') and ($TagC!==false)) {
  2769. $level = ($TagIsSgl[$i]) ? 1 : 0;
  2770. $TagC = tbs_Html_FindTag($Txt,$x,$TagIsSgl[$i],$PosEnd+1,true,$level,false);
  2771. if ($TagC!==false) $PosEnd = $TagC->PosEnd;
  2772. }
  2773. }
  2774. // Backward
  2775. $TagO = true;
  2776. for ($i=$Ref-1;$i>=0;$i--) {
  2777. $x = $TagLst[$i];
  2778. if (($x!=='') and ($TagO!==false)) {
  2779. $level = ($TagIsSgl[$i]) ? 1 : 0;
  2780. $TagO = tbs_Html_FindTag($Txt,$x,true,$PosBeg-1,false,$level,false);
  2781. if ($TagO!==false) $PosBeg = $TagO->PosBeg;
  2782. }
  2783. }
  2784. $Loc->PosBeg = $PosBeg;
  2785. $Loc->PosEnd = $PosEnd;
  2786. return $RetVal;
  2787. }
  2788. function tbs_Html_Max(&$Txt,&$Nbr,$MaxEnd) {
  2789. // Limit the number of HTML chars
  2790. $pMax = strlen($Txt)-1;
  2791. $p=0;
  2792. $n=0;
  2793. $in = false;
  2794. $ok = true;
  2795. while ($ok) {
  2796. if ($in) {
  2797. if ($Txt[$p]===';') {
  2798. $in = false;
  2799. $n++;
  2800. }
  2801. } else {
  2802. if ($Txt[$p]==='&') {
  2803. $in = true;
  2804. } else {
  2805. $n++;
  2806. }
  2807. }
  2808. if (($n>=$Nbr) or ($p>=$pMax)) {
  2809. $ok = false;
  2810. } else {
  2811. $p++;
  2812. }
  2813. }
  2814. if (($n>=$Nbr) and ($p<$pMax)) $Txt = substr($Txt,0,$p).$MaxEnd;
  2815. }
  2816. function tbs_Html_GetPart(&$Txt,$Tag,$WithTags=false,$CancelIfEmpty=false) {
  2817. // This function returns a part of the HTML document (HEAD or BODY)
  2818. // The $CancelIfEmpty parameter enables to cancel the extraction when the part is not found.
  2819. if (($Tag===true) or ($Tag==='')) $Tag = 'BODY';
  2820. $x = false;
  2821. $LocOpen = tbs_Html_FindTag($Txt,$Tag,true,0,true,false,false);
  2822. if ($LocOpen!==false) {
  2823. $LocClose = tbs_Html_FindTag($Txt,$Tag,false,$LocOpen->PosEnd+1,true,false,false);
  2824. if ($LocClose!==false) {
  2825. if ($WithTags) {
  2826. $x = substr($Txt,$LocOpen->PosBeg,$LocClose->PosEnd - $LocOpen->PosBeg + 1);
  2827. } else {
  2828. $x = substr($Txt,$LocOpen->PosEnd+1,$LocClose->PosBeg - $LocOpen->PosEnd - 1);
  2829. }
  2830. }
  2831. }
  2832. if ($x===false) {
  2833. if ($CancelIfEmpty) {
  2834. $x = $Txt;
  2835. } else {
  2836. $x = '';
  2837. }
  2838. }
  2839. return $x;
  2840. }
  2841. function tbs_Html_FindTag(&$Txt,$Tag,$Opening,$PosBeg,$Forward,$LevelStop,$WithPrm) {
  2842. /* This function is a smarter issue to find an HTML tag.
  2843. It enables to ignore full opening/closing couple of tag that could be inserted before the searched tag.
  2844. It also enables to pass a number of encapsulations.
  2845. To ignore encapsulation and opengin/closing just set $LevelStop=false.
  2846. */
  2847. if ($Tag==='_') { // New line
  2848. $p = tbs_Html_FindNewLine($Txt,$PosBeg,$Forward);
  2849. $Loc = new clsTbsLocator;
  2850. $Loc->PosBeg = ($Forward) ? $PosBeg : $p;
  2851. $Loc->PosEnd = ($Forward) ? $p : $PosBeg;
  2852. return $Loc;
  2853. }
  2854. $Pos = $PosBeg + (($Forward) ? -1 : +1);
  2855. $TagIsOpening = false;
  2856. $TagClosing = '/'.$Tag;
  2857. $LevelNum = 0;
  2858. $TagOk = false;
  2859. do {
  2860. // Look for the next tag def
  2861. if ($Forward) {
  2862. $Pos = strpos($Txt,'<',$Pos+1);
  2863. } else {
  2864. if ($Pos<=0) {
  2865. $Pos = false;
  2866. } else {
  2867. $Pos = strrpos(substr($Txt,0,$Pos - 1),'<');
  2868. }
  2869. }
  2870. if ($Pos!==false) {
  2871. // Check the name of the tag
  2872. if (strcasecmp(substr($Txt,$Pos+1,strlen($Tag)),$Tag)==0) {
  2873. $PosX = $Pos + 1 + strlen($Tag); // The next char
  2874. $TagOk = true;
  2875. $TagIsOpening = true;
  2876. } elseif (strcasecmp(substr($Txt,$Pos+1,strlen($TagClosing)),$TagClosing)==0) {
  2877. $PosX = $Pos + 1 + strlen($TagClosing); // The next char
  2878. $TagOk = true;
  2879. $TagIsOpening = false;
  2880. }
  2881. if ($TagOk) {
  2882. // Check the next char
  2883. $x = $Txt[$PosX];
  2884. if (($x===' ') or ($x==="\r") or ($x==="\n") or ($x==='>')) {
  2885. // Check the encapsulation count
  2886. if ($LevelStop===false) { // No encaplusation check
  2887. if ($TagIsOpening!==$Opening) $TagOk = false;
  2888. } else { // Count the number of level
  2889. if ($TagIsOpening) {
  2890. $LevelNum++;
  2891. } else {
  2892. $LevelNum--;
  2893. }
  2894. // Check if it's the expected level
  2895. if ($LevelNum!=$LevelStop) $TagOk = false;
  2896. }
  2897. } else {
  2898. $TagOk = false;
  2899. }
  2900. } //--> if ($TagOk)
  2901. }
  2902. } while (($Pos!==false) and ($TagOk===false));
  2903. // Search for the end of the tag
  2904. if ($TagOk) {
  2905. $Loc = new clsTbsLocator;
  2906. if ($WithPrm) {
  2907. $PosEnd = 0;
  2908. tbs_Locator_PrmRead($Txt,$PosX,true,'\'"','<','>',$Loc,$PosEnd);
  2909. } else {
  2910. $PosEnd = strpos($Txt,'>',$PosX);
  2911. if ($PosEnd===false) {
  2912. $TagOk = false;
  2913. }
  2914. }
  2915. }
  2916. // Result
  2917. if ($TagOk) {
  2918. $Loc->PosBeg = $Pos;
  2919. $Loc->PosEnd = $PosEnd;
  2920. return $Loc;
  2921. } else {
  2922. return false;
  2923. }
  2924. }
  2925. function tbs_Html_FindNewLine(&$Txt,$PosBeg,$Forward) {
  2926. $p = $PosBeg;
  2927. if ($Forward) {
  2928. $Inc = 1;
  2929. $Inf =& $p;
  2930. $Sup = strlen($Txt)-1;
  2931. } else {
  2932. $Inc = -1;
  2933. $Inf = 0;
  2934. $Sup =& $p;
  2935. }
  2936. do {
  2937. if ($Inf>$Sup) return max($Sup,0);
  2938. $x = $Txt[$p];
  2939. if (($x==="\r") or ($x==="\n")) {
  2940. $x2 = ($x==="\n") ? "\r" : "\n";
  2941. $p0 = $p;
  2942. if (($Inf<$Sup) and ($Txt[$p+$Inc]===$x2)) $p += $Inc; // Newline char can have two chars.
  2943. if ($Forward) return $p; // Forward => return pos including newline char.
  2944. if ($p0!=$PosBeg) return $p0+1; // Backwars => return pos without newline char. Ignore newline if it is the very first char of the search.
  2945. }
  2946. $p += $Inc;
  2947. } while (true);
  2948. }
  2949. ?>