PageRenderTime 70ms CodeModel.GetById 28ms 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

Large files files are truncated, but you can click here to view the full 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)

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