PageRenderTime 63ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/template/tbs_class_php5.php

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