PageRenderTime 69ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/xkid_framework/template/tbs_class.php

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