PageRenderTime 70ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/template/tbs_class.php

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