PageRenderTime 74ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/xkid_framework/template/tbs_class_php5.php

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