template.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. /**
  3. * This is a library of functions to act on the template HTML file.
  4. * The functions declared here mainly read the template and write the
  5. * result output using this template.
  6. * Modified version - the PHP extension must be higher than 3.2.32 and PHP must be higher than 4.2
  7. */
  8. /**
  9. * Initialise arrays
  10. */
  11. $templates=array();
  12. /**
  13. * Reads the template file given and returns a string.
  14. * Similar to the file_get_contents() function but using a loop to read by chunks
  15. * of 1024 bytes.
  16. * @param string Filename
  17. * @return string File contents
  18. */
  19. function load_file($file){
  20. if (!($fp=fopen($file, 'r'))){
  21. echo "Can't open template file $template_file";
  22. exit_local(1);
  23. }
  24. while(!feof($fp)) {
  25. $str.=fgets($fp, 1024);
  26. }
  27. fclose($fp);
  28. return $str;
  29. }
  30. /**
  31. * Build the navigation bar
  32. * @param string Original query ? - not used anyway
  33. * @return string Navigation bar string
  34. */
  35. function make_nav($query_orig=''){
  36. global $found,$np,$isnext,$ps,$tag,$ul,$ul_arr,$self,$o,$m,$cat,$cat_arr;
  37. global $dt, $dp, $dx, $dm, $dy, $dd, $db, $de, $lang, $wm, $wf, $site, $sp, $sy;
  38. global $q_local,$db_local,$de_local,$lang_arr, $tag, $tag_arr, $type, $type_arr;
  39. $temp_ul='';
  40. for($i=0; $i<count($ul_arr); $i++) {
  41. $temp_ul.="&ul=".urlencode($ul_arr[$i]);
  42. }
  43. $temp_tag='';
  44. for($i=0; $i<count($tag_arr); $i++) {
  45. $temp_tag.="&tag=".urlencode($tag_arr[$i]);
  46. }
  47. $temp_lang='';
  48. for($i=0; $i<count($lang_arr); $i++) {
  49. $temp_lang.="&lang=".urlencode($lang_arr[$i]);
  50. }
  51. $temp_cat='';
  52. for($i=0; $i<count($cat_arr); $i++) {
  53. $temp_cat.="&cat=".urlencode($cat_arr[$i]);
  54. }
  55. $temp_type='';
  56. for($i=0; $i<count($type_arr); $i++) {
  57. $temp_type.="&type=".urlencode($type_arr[$i]);
  58. }
  59. if($np>0){
  60. $prevp=$np-1;
  61. $prev_href="$self?q=$q_local&np=$prevp&m=$m".
  62. ($ps==20?'':"&ps=$ps").
  63. $temp_tag.
  64. $temp_ul.
  65. $temp_cat.
  66. $temp_lang.
  67. ($site==''?'':"&site=$site").
  68. ($wm==''?'':"&wm=$wm").
  69. ($wf==''?'':"&wf=$wf").
  70. (!$o?'':"&o=$o").
  71. ($dt=='back'?'':"&dt=$dt").
  72. (!$dp?'':"&dp=$dp").
  73. (!$dx?'':"&dx=$dx").
  74. ($dd=='01'?'':"&dd=$dd").
  75. (!$dm?'':"&dm=$dm").
  76. ($dy=='1970'?'':"&dy=$dy").
  77. ($db=='01/01/1970'?'':"&db=$db_local").
  78. ($de=='31/12/2020'?'':"&de=$de_local").
  79. ($sp==''?'':"&sp=$sp").
  80. ($sy==''?'':"&sy=$sy");
  81. $nav_left=print_template('navleft',0);
  82. $nav_left=ereg_replace('\$NH',"$prev_href",$nav_left);
  83. } elseif ($np==0) {
  84. $nav_left=print_template('navleft_nop',0);
  85. }
  86. if($isnext==1) {
  87. $nextp=$np+1;
  88. $next_href="$self?q=$q_local&np=$nextp&m=$m".
  89. ($ps==20?'':"&ps=$ps").
  90. $temp_tag.
  91. $temp_ul.
  92. $temp_cat.
  93. $temp_lang.
  94. ($site==''?'':"&site=$site").
  95. ($wm==''?'':"&wm=$wm").
  96. ($wf==''?'':"&wf=$wf").
  97. (!$o?'':"&o=$o").
  98. ($dt=='back'?'':"&dt=$dt").
  99. (!$dp?'':"&dp=$dp").
  100. (!$dx?'':"&dx=$dx").
  101. ($dd=='01'?'':"&dd=$dd").
  102. (!$dm?'':"&dm=$dm").
  103. ($dy=='1970'?'':"&dy=$dy").
  104. ($db=='01/01/1970'?'':"&db=$db_local").
  105. ($de=='31/12/2020'?'':"&de=$de_local").
  106. ($sp==''?'':"&sp=$sp").
  107. ($sy==''?'':"&sy=$sy");
  108. $nav_right=print_template('navright',0);
  109. $nav_right=ereg_replace('\$NH',"$next_href",$nav_right);
  110. } else {
  111. $nav_right=print_template('navright_nop',0);
  112. }
  113. $nav_bar0=print_template('navbar0',0);
  114. $nav_bar1=print_template('navbar1',0);
  115. $tp=ceil($found/$ps);
  116. $cp=$np+1;
  117. if ($cp>5) {
  118. $lp=$cp-5;
  119. } else {
  120. $lp=1;
  121. }
  122. $rp=$lp+10-1;
  123. if ($rp>$tp) {
  124. $rp=$tp;
  125. $lp=$rp-10+1;
  126. if ($lp<1) $lp=1;
  127. }
  128. if ($lp!=$rp) {
  129. for ($i=$lp; $i<=$rp;$i++) {
  130. $realp=$i-1;
  131. if ($i==$cp) {
  132. $nav_bar=$nav_bar.$nav_bar0;
  133. } else {
  134. $nav_bar=$nav_bar.$nav_bar1;
  135. }
  136. $href="$self?q=$q_local&np=$realp&m=$m".
  137. ($ps==20?'':"&ps=$ps").
  138. $temp_tag.
  139. $temp_ul.
  140. $temp_cat.
  141. $temp_lang.
  142. ($site==''?'':"&site=$site").
  143. ($wm==''?'':"&wm=$wm").
  144. ($wf==''?'':"&wf=$wf").
  145. (!$o?'':"&o=$o").
  146. ($dt=='back'?'':"&dt=$dt").
  147. (!$dp?'':"&dp=$dp").
  148. (!$dx?'':"&dx=$dx").
  149. ($dd=='01'?'':"&dd=$dd").
  150. (!$dm?'':"&dm=$dm").
  151. ($dy=='1970'?'':"&dy=$dy").
  152. ($db=='01/01/1970'?'':"&db=$db_local").
  153. ($de=='31/12/2020'?'':"&de=$de_local").
  154. ($sp==''?'':"&sp=$sp").
  155. ($sy==''?'':"&sy=$sy");
  156. $nav_bar=ereg_replace('\$NP',"$i",$nav_bar);
  157. $nav_bar=ereg_replace('\$NH',"$href",$nav_bar);
  158. }
  159. $nav=print_template('navigator',0);
  160. $nav=ereg_replace('\$NL',"$nav_left",$nav);
  161. $nav=ereg_replace('\$NR',"$nav_right",$nav);
  162. $nav=ereg_replace('\$NB',"$nav_bar",$nav);
  163. } elseif ($found) {
  164. $nav=print_template('navigator',0);
  165. $nav=ereg_replace('\$NL',"$nav_left",$nav);
  166. $nav=ereg_replace('\$NR',"$nav_right",$nav);
  167. $nav=ereg_replace('\$NB',"",$nav);
  168. }
  169. return $nav;
  170. }
  171. /**
  172. * Prints the template on screen after substituting a lot of variables
  173. * from the global scope
  174. * @param string Index of the part of the template we want to use (storedoc_top, storedoc, ...)
  175. * @param boolean Whether to echo the results on screen or just return the string
  176. * @return mixed Nothing if $echo=1, the resulting string if $echo=false
  177. */
  178. function print_template($t,$echo=1){
  179. global $templates, $udm_agent;
  180. global $first_doc, $last_doc, $found, $query_orig, $error, $self;
  181. global $nav, $wordinfo, $ws, $doclang, $doccharset, $storedocurl;
  182. global $url, $ue, $o, $cat, $storedstr, $stored_href;
  183. global $clonestr, $searchtime, $persite;
  184. global $title, $rating, $desc, $contype, $lastmod, $docsize, $ndoc;
  185. global $keyw, $text, $category, $pop_rank;
  186. global $crc, $Randoms, $rec_id, $DEBUG, $id;
  187. global $lang_url_translation, $phpver, $sitelimit_href, $sitelimitstr;
  188. global $global_doc_res, $global_res_position;
  189. global $last_modified, $content, $charset, $length, $document, $url;
  190. $str=$templates["$t"][$o];
  191. if ($str == '') $str=$templates["$t"][0];
  192. //variables as returned by the udm_search() function and defined in search.php
  193. $str=ereg_replace('\$f', "$first_doc", $str);
  194. $str=ereg_replace('\$l', "$last_doc", $str);
  195. $str=ereg_replace('\$t', "$found", $str);
  196. $str=ereg_replace('\$A', $self, $str);
  197. $str=ereg_replace('\$Q', urlencode(stripslashes($query_orig)), $str);
  198. $str=ereg_replace('\$q', urlencode($query_orig), $str);
  199. $str=eregi_replace('\$UE', $ue, $str);
  200. $str=ereg_replace('\$E', $error, $str);
  201. $str=ereg_replace('\$W', htmlspecialchars($wordinfo), $str);
  202. $str=ereg_replace('\$\(WS\)', htmlspecialchars($ws), $str);
  203. $str=ereg_replace('\$V', $nav, $str);
  204. if ($lang_url_translation == 'yes') {
  205. $nolangurl = ereg_replace("\.[a-z]{2}\.[a-z]{2,4}$", "", $url);
  206. $str=ereg_replace('\$DU', htmlspecialchars($nolangurl), $str);
  207. } else {
  208. $str=ereg_replace('\$DU', htmlspecialchars($url), $str);
  209. }
  210. $str=ereg_replace('\$\(stored_href\)', htmlspecialchars($stored_href), $str);
  211. $str=ereg_replace('\$\(sitelimit_href\)', htmlspecialchars($sitelimit_href), $str);
  212. //require_once('filter_user.functions.php');
  213. //$title = subst_course_code(urldecode(html_entity_decode($title)));
  214. $str=ereg_replace('\$DT', htmlspecialchars($title), $str);
  215. $str=ereg_replace('\$DR', htmlspecialchars("$rating"), $str);
  216. //$text = subst_course_code(html_entity_decode($text));
  217. //Comment out temporarily because breaking XML file (excerpt of Word document with unknown character)
  218. $str=ereg_replace('\$DX', htmlspecialchars(urlencode($text)), $str);
  219. $str=ereg_replace('\$DE', ($desc != '')?htmlspecialchars(urlencode($desc)):htmlspecialchars(urlencode($text)), $str);
  220. $str=ereg_replace('\$DC', $contype, $str);
  221. $str=ereg_replace('\$DM', $lastmod, $str);
  222. $str=ereg_replace('\$DS', "$docsize", $str);
  223. $str=ereg_replace('\$DN', "$ndoc", $str);
  224. $str=ereg_replace('\$DD', htmlspecialchars($desc), $str);
  225. $str=ereg_replace('\$DK', htmlspecialchars($keyw), $str);
  226. $str=ereg_replace('\$SearchTime', "$searchtime", $str);
  227. $str=ereg_replace('\$\(STORED\)', htmlspecialchars("$storedstr"), $str);
  228. $str=ereg_replace('\$\(SITELIMIT\)', htmlspecialchars("$sitelimitstr"), $str);
  229. $str=ereg_replace('\$\(Pop_Rank\)', "$pop_rank", $str);
  230. $str=ereg_replace('\$CL', "$clonestr", $str);
  231. $str=ereg_replace('\$\(PerSite\)', "$persite", $str);
  232. //information on each link (header for one item)
  233. if ($t == 'storedoc_top') {
  234. $str=ereg_replace('\$\(ID\)', htmlspecialchars("$id"), $str);
  235. $str=ereg_replace('\$\(Last-Modified\)', htmlspecialchars("$last_modified"), $str);
  236. $str=ereg_replace('\$\(Content-Language\)', htmlspecialchars("$content"), $str);
  237. $str=ereg_replace('\$\(Charset\)', htmlspecialchars("$charset"), $str);
  238. $str=ereg_replace('\$\(Content-Length\)', "$length", $str);
  239. $str=ereg_replace('\$\(URL\)', htmlspecialchars("$url"), $str);
  240. }
  241. //link to the document
  242. if ($t == 'storedoc') {
  243. $str=ereg_replace('\$\(document\)', htmlspecialchars("$document"), $str);
  244. }
  245. //Categories
  246. if (ereg('\$CP',$str)) {
  247. if ($temp_cp_arr=udm_cat_path($udm_agent,$cat)) {
  248. reset($temp_cp_arr);
  249. $temp_cp='';
  250. for ($i=0; $i<count($temp_cp_arr); $i+=2) {
  251. $cp_path=$temp_cp_arr[$i];
  252. $cp_name=$temp_cp_arr[$i+1];
  253. $temp_cp .= " &gt; <a href=\"$PHP_SELF?cat=$cp_path\">$cp_name</a> ";
  254. }
  255. $str=ereg_replace('\$CP', $temp_cp, $str);
  256. } else $str=ereg_replace('\$CP', '', $str);
  257. }
  258. //Categories
  259. if (ereg('\$CS',$str)) {
  260. if ($temp_cp_arr=udm_cat_list($udm_agent,$cat)) {
  261. reset($temp_cp_arr);
  262. $temp_cp='';
  263. for ($i=0; $i<count($temp_cp_arr); $i+=2) {
  264. $cp_path=$temp_cp_arr[$i];
  265. $cp_name=$temp_cp_arr[$i+1];
  266. $temp_cp .= "<a href=\"$PHP_SELF?cat=$cp_path\">$cp_name</a><br>";
  267. }
  268. $str=ereg_replace('\$CS', $temp_cp, $str);
  269. } else $str=ereg_replace('\$CS', '', $str);
  270. }
  271. //Categories
  272. if (ereg('\$DY',$str)) {
  273. if ($temp_cp_arr=udm_cat_path($udm_agent,$category)) {
  274. reset($temp_cp_arr);
  275. $temp_cp='';
  276. for ($i=0; $i<count($temp_cp_arr); $i+=2) {
  277. $cp_path=$temp_cp_arr[$i];
  278. $cp_name=$temp_cp_arr[$i+1];
  279. $temp_cp .= " &gt; <a href=\"$PHP_SELF?cat=$cp_path\">$cp_name</a> ";
  280. }
  281. $str=ereg_replace('\$DY', $temp_cp, $str);
  282. } else $str=ereg_replace('\$DY', '', $str);
  283. }
  284. //Number of documents
  285. if (eregi('\$ndocs*',$str)) {
  286. $str=preg_replace('/\$ndocs?/', get_doc_count(), $str);
  287. }
  288. //document link?
  289. if ($global_doc_res) {
  290. $str=preg_replace_callback('/\$\(([\w\.\_\-\:]+)\)/', 'res_field_callback', $str);
  291. }
  292. //Random?
  293. $sub=$str;
  294. while($sub=strstr($sub,'$r')){
  295. $sub=substr($sub,2);
  296. $pos=0;
  297. $num='';
  298. while(($sub[$pos]>='0')&&($sub[$pos]<='9')){
  299. $num.=$sub[$pos++];
  300. }
  301. $str=ereg_replace('\$r'.$num, ''.$Randoms[$num], $str);
  302. }
  303. if($echo) echo $str; else return $str;
  304. }
  305. /**
  306. * Reads the template file.
  307. * @param none All the parameters are taken as globals, which is very bad.
  308. * @return void No return value everything is left in global scope
  309. */
  310. function read_template(){
  311. global $Randoms;
  312. global $template_file; //uri of the file to open
  313. global $templates,$dbaddr;
  314. global $ps, $np, $ul, $ul_arr, $o, $tag, $tag_arr, $m, $cat, $cat_arr, $wm, $wf;
  315. global $dt, $dp, $dx, $dm, $dy, $dd, $db, $de;
  316. global $clones,$lang,$lang_arr, $sp, $sy, $s, $resultslimit;
  317. global $affix_file, $spell_file, $spell_host;
  318. global $stopwordtable_arr, $stopwordfile_arr;
  319. global $synonym_arr, $searchd_arr;
  320. global $alias_arr,$type, $type_arr;
  321. $affix_file=array();
  322. $spell_file=array();
  323. $alias_arr=array();
  324. $dbaddr=array();
  325. $names=array('top', 'bottom', 'restop', 'resbot', 'navigator',
  326. 'res', 'ftpres', 'httpres', 'notfound', 'error', 'variables',
  327. 'clone', 'navleft_nop', 'navright_nop', 'navleft',
  328. 'navbar1', 'navbar0', 'navright', 'noquery', 'site_limit',
  329. 'storedoc_top','storedoc_bottom','storedoc','stored');
  330. $where='';
  331. //First check that the template file is available
  332. if (!($fp=fopen($template_file, 'r'))){
  333. echo "Can't open template file $template_file";
  334. exit_local(1);
  335. } do {
  336. //The template file is there, read it
  337. $str=fgets($fp, 1024);
  338. $param = array();
  339. if (preg_match('/value="(.*)"[\s\t]+selected="\$ps"/i',$str,$param)) {
  340. if (($param[1]) == $ps) {
  341. $str=preg_replace('/selected="\$ps"/i',"selected",$str);
  342. } else {
  343. $str=preg_replace('/selected="\$ps"/i',"",$str);
  344. }
  345. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$ul"/i',$str,$param)) {
  346. $found_param=0;
  347. for($i=0; $i<count($ul_arr); $i++) {
  348. $temp_ul=$ul_arr[$i];
  349. if (($param[1]) == $temp_ul) {
  350. $str=preg_replace('/selected="\$ul"/i',"selected",$str);
  351. $found_param=1;
  352. break;
  353. }
  354. }
  355. if (!$found_param) $str=preg_replace('/selected="\$ul"/i',"",$str);
  356. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$type"/i',$str,$param)) {
  357. $found_param=0;
  358. for($i=0; $i<count($type_arr); $i++) {
  359. $temp_type=$type_arr[$i];
  360. if (($param[1]) == $temp_type) {
  361. $str=preg_replace('/selected="\$type"/i',"selected",$str);
  362. $found_param=1;
  363. break;
  364. }
  365. }
  366. if (!$found_param) $str=preg_replace('/selected="\$type"/i',"",$str);
  367. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$lang"/i',$str,$param)) {
  368. $found_param=0;
  369. for($i=0; $i<count($lang_arr); $i++) {
  370. $temp_lang=$lang_arr[$i];
  371. if (($param[1]) == $temp_lang) {
  372. $str=preg_replace('/selected="\$lang"/i',"selected",$str);
  373. $found_param=1;
  374. break;
  375. }
  376. }
  377. if (!$found_param) $str=preg_replace('/selected="\$lang"/i',"",$str);
  378. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$o"/i',$str,$param)) {
  379. if (($param[1]) == $o) {
  380. $str=preg_replace('/selected="\$o"/i',"selected",$str);
  381. } else {
  382. $str=preg_replace('/selected="\$o"/i',"",$str);
  383. }
  384. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$m"/i',$str,$param)) {
  385. if (($param[1]) == $m) {
  386. $str=preg_replace('/selected="\$m"/i',"selected",$str);
  387. } else {
  388. $str=preg_replace('/selected="\$m"/i',"",$str);
  389. }
  390. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$wm"/i',$str,$param)) {
  391. if (($param[1]) == $wm) {
  392. $str=preg_replace('/selected="\$wm"/i',"selected",$str);
  393. } else {
  394. $str=preg_replace('/selected="\$wm"/i',"",$str);
  395. }
  396. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$s"/i',$str,$param)) {
  397. if ($param[1] == $s) {
  398. $str=preg_replace('/selected="\$s"/i',"selected",$str);
  399. } else {
  400. $str=preg_replace('/selected="\$s"/i',"",$str);
  401. }
  402. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$sp"/i',$str,$param)) {
  403. if ($param[1] == $sp) {
  404. $str=preg_replace('/selected="\$sp"/i',"selected",$str);
  405. } else {
  406. $str=preg_replace('/selected="\$sp"/i',"",$str);
  407. }
  408. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$sy"/i',$str,$param)) {
  409. if ($param[1] == $sy) {
  410. $str=preg_replace('/selected="\$sy"/i',"selected",$str);
  411. } else {
  412. $str=preg_replace('/selected="\$sy"/i',"",$str);
  413. }
  414. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$wf"/i',$str,$param)) {
  415. if (($param[1]) == $wf) {
  416. $str=preg_replace('/selected="\$wf"/i',"selected",$str);
  417. } else {
  418. $str=preg_replace('/selected="\$wf"/i',"",$str);
  419. }
  420. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$tag"/i',$str,$param)) {
  421. $found_param=0;
  422. for($i=0; $i<count($tag_arr); $i++) {
  423. $temp_tag=$tag_arr[$i];
  424. if (($param[1]) == $temp_tag) {
  425. $str=preg_replace('/selected="\$tag"/i',"selected",$str);
  426. $found_param=1;
  427. break;
  428. }
  429. }
  430. if (!$found_param) $str=preg_replace('/selected="\$tag"/i',"",$str);
  431. } elseif (preg_match('/type=radio[\s\t]+name="dt"[\s\t]+value="(.*)"/i',$str,$param)) {
  432. if (($param[1]) == $dt) {
  433. $str=preg_replace("/value=\"$dt\"/i","value=\"$dt\" checked",$str);
  434. } else {
  435. $str=preg_replace('/checked/i',"",$str);
  436. }
  437. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$dp"/i',$str,$param)) {
  438. if (($param[1]) == $dp) {
  439. $str=preg_replace('/selected="\$dp"/i',"selected",$str);
  440. } else {
  441. $str=preg_replace('/selected="\$dp"/i',"",$str);
  442. }
  443. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$dx"/i',$str,$param)) {
  444. if (($param[1]) == $dx) {
  445. $str=preg_replace('/selected="\$dx"/i',"selected",$str);
  446. } else {
  447. $str=preg_replace('/selected="\$dx"/i',"",$str);
  448. }
  449. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$dm"/i',$str,$param)) {
  450. if (($param[1]) == $dm) {
  451. $str=preg_replace('/selected="\$dm"/i',"selected",$str);
  452. } else {
  453. $str=preg_replace('/selected="\$dm"/i',"",$str);
  454. }
  455. } elseif (preg_match('/value="(.*)"[\s\t]+selected="\$dy"/i',$str,$param)) {
  456. if (($param[1]) == $dy) {
  457. $str=preg_replace('/selected="\$dy"/i',"selected",$str);
  458. } else {
  459. $str=preg_replace('/selected="\$dy"/i',"",$str);
  460. }
  461. }
  462. if (preg_match('/\$g\((\d*)\)/i',$str,$param)) {
  463. $str=str_replace("\$g($param[1])",substr($tag,0,strlen($tag)-$param[1]),$str);
  464. }
  465. //Replace date attributes
  466. $str=str_replace("\$dd",$dd,$str);
  467. $str=str_replace("\$dy",$dy,$str);
  468. $str=str_replace("\$dm",$dm,$str);
  469. $str=str_replace("\$db",$db,$str);
  470. $str=str_replace("\$de",$de,$str);
  471. //Replace page numbering
  472. $str=str_replace("\$ps",$ps,$str);
  473. $str=str_replace("\$pn",$ps*$np,$str);
  474. $str=str_replace("\$cat",$cat,$str);
  475. if (preg_match('/\$IF\s?\((.+)\)/i',$str,$param)) {
  476. $param[1]=trim($param[1]);
  477. $str=preg_replace('/\$IF\s?\((.+)\)/i',load_file($param[1]),$str);
  478. }
  479. $comment_flag=0;
  480. //If found comment tag, add empty field to the $templates array for the corresponding tag
  481. if (substr($str, 0, 4) == "<!--") {
  482. reset($names);
  483. while ($name=current($names)){
  484. if (strstr($str, "<!--$name")){
  485. $where=$name;
  486. $templates["$where"][] = '';
  487. $comment_flag=1;
  488. break;
  489. } elseif (strstr($str, "/$name-->")) {
  490. $where='';
  491. $comment_flag=1;
  492. break;
  493. }
  494. next($names);
  495. }
  496. }
  497. //if found a comment on this line, skip to the next line (do{...})
  498. if ($comment_flag) continue;
  499. //if the previous line was a comment and contained 'variables'
  500. //'variables' is the section of the template where the configuration is
  501. //Once this section is over, the HTML template really begins
  502. if ($where=='variables') {
  503. //if the line starts with a #, it's a comment so skip to next line
  504. if (ereg("^#",$str)) continue;
  505. //look for some characters not space or tab starting a line,
  506. //followed by spaces or tabs, followed by any chars (basically any var..value couple)
  507. if (preg_match("/^([^\s\t]+)[\s\t]+(.+)/",$str,$param)) {
  508. $var=$param[1];
  509. $value=$param[2];
  510. $var=strtolower($var);
  511. $var=trim($var);
  512. $value=trim($value);
  513. //assign each value to the corresponding array, for reuse later
  514. if ($var=='affix') {
  515. if ($sp=='') $sp=1;
  516. $GLOBALS["ispellmode"]="text";
  517. if (preg_match("/([^\s\t]+)[\s\t]+([^\s\t]+)[\s\t]+(.+)/",$value,$param)) {
  518. $t_lang=trim($param[1]);
  519. $t_charset=trim($param[2]);
  520. $file=trim($param[3]);
  521. $affix_file[$t_lang]="$t_charset;$file";
  522. } else {
  523. print "Unsupported Affix command: Affix $value\n<br>";
  524. exit;
  525. }
  526. } elseif ($var=='spell') {
  527. if ($sp=='') $sp=1;
  528. $GLOBALS["ispellmode"]="text";
  529. if (preg_match("/([^\s\t]+)[\s\t]+([^\s\t]+)[\s\t]+(.+)/",$value,$param)) {
  530. $t_lang=trim($param[1]);
  531. $t_charset=trim($param[2]);
  532. $file=trim($param[3]);
  533. $spell_file[$t_lang][]="$t_charset;$file";
  534. } else {
  535. print "Unsupported Spell command: Spell $value\n<br>";
  536. exit;
  537. }
  538. } elseif ($var=='ispellmode') {
  539. if (preg_match("/^server[\s\t]+(.+)/i",$value,$param)) {
  540. $spell_host=trim($param[1]);
  541. $GLOBALS["ispellmode"]="server";
  542. } else {
  543. $GLOBALS["$var"]="$value";
  544. }
  545. } elseif ($var=='alias') {
  546. if (preg_match("/^([^\s]+)\s+(.+)$/i",$value,$param)) {
  547. $alias_arr[trim($param[1])]=trim($param[2]);
  548. } else {
  549. $GLOBALS["$var"]="$value";
  550. }
  551. } elseif ($var=='stopwordtable') {
  552. $stopwordtable_arr[]=$value;
  553. } elseif ($var=='stopwordfile') {
  554. $stopwordfile_arr[]=$value;
  555. } elseif ($var=='synonym') {
  556. if ($sy=='') $sy=1;
  557. $synonym_arr[]=$value;
  558. } elseif ($var=='searchdaddr') {
  559. $searchd_arr[]=$value;
  560. } elseif ($var=='r') {
  561. $rand=0+substr($var,1);
  562. $frand=rand();
  563. $frand=$frand/getrandmax()*$value;
  564. $Randoms[$rand]=floor($frand);
  565. } elseif ($var=='dbaddr') {
  566. $dbaddr[]=$value;
  567. } else {
  568. $GLOBALS["$var"]="$value";
  569. }
  570. }
  571. }
  572. else
  573. if ($where!=''){
  574. $templates["$where"][count($templates["$where"])-1] .= $str;
  575. }
  576. } while (!feof($fp));//do this big loop until we reach the end of the file
  577. fclose($fp);
  578. }
  579. /**
  580. * Calls back the udm_get_res_field_ex() function whith the second element of
  581. * the given array...!?
  582. * @param array Array which second element is sent to udm_get_res_field_ex()
  583. * @return string A string
  584. */
  585. function res_field_callback($expr) {
  586. global $global_doc_res, $global_res_position;
  587. return ParseDocText(htmlspecialchars(udm_get_res_field_ex($global_doc_res,$global_res_position,$expr[1])));
  588. }
  589. ?>