123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- <?php
- function xht_htmlwchars($s) // use only where ISO-8859-1 is not required!
- {
-
-
-
- global $charset;
- return api_ereg_replace('\[((/?(b|big|i|small|sub|sup|u))|br/)\]', '<\\1>',
- str_replace('@�@', '&',
- htmlspecialchars(api_ereg_replace('&#([0-9]+);', '@�@#\\1;', $s), ENT_QUOTES, $charset)));
-
-
- }
- function xht_is_assoclist($s) // ":key1:value1,, key2:value2,, ..."
- {
- return is_string($s) && strpos($s, ',,');
- }
- function xht_explode_assoclist($s)
- {
- $result = array(); if (!xht_is_assoclist($s)) return $result;
- foreach (explode(',,', api_substr($s, 1)) as $keyplusvalue)
- if ($cp = api_strpos($keyplusvalue, api_substr($s, 0, 1)))
- $result[trim(api_substr($keyplusvalue, 0, $cp))] =
- api_substr($keyplusvalue, $cp+1);
- return $result;
- }
- class xhtdoc
- {
- var $htt_array;
- var $htt_error;
- var $xht_xmldoc;
- var $xht_param;
- var $xht_get_lang;
- var $xht_resource;
- var $xht_dbgn;
- var $xht_dbgo;
- var $_prev_param;
- function xht_fill_template($template_name, $cur_elem = 0)
- {
- $template_name = trim($template_name);
- if (!$template_name || (api_strpos($template_name, ' ') !== FALSE)) return '';
- if (!is_string($httext = $this->htt_array[$template_name])) return '';
- if ($this->xht_dbgn) $this->xht_dbgo .= '<!-- ' . XHT_LP . $template_name .
- ' ' . XHT_RP . $this->_show_param() . " -->\n";
- $prev_lpp = 0; $prev_sub = ''; $scanpos = 0;
- while (($rpp = api_strpos($httext, XHT_RP, $scanpos)) !== FALSE)
- {
- if (($lpp = api_strpos($httext, XHT_LP)) === FALSE) break;
- if ($lpp > $rpp) break;
- while (($next_lpp = api_strpos($httext, XHT_LP, $lpp + XHT_PL)) !== FALSE)
- if ($next_lpp > $rpp) break;
- else $lpp = $next_lpp;
- $subtext = api_substr($httext, $lpp + XHT_PL, $rpp - $lpp - XHT_PL);
- $httext = api_substr($httext, 0, $lpp) .
- $this->xht_substitute($subtext, $cur_elem, XHT_LP, XHT_RP) .
- api_substr($httext, $rpp + XHT_PL);
- if ($lpp == $prev_lpp && $subtext == $prev_sub)
- {
- $scanpos = $rpp + 1; $prev_lpp = 0; $prev_sub = '';
- }
- else
- {
- $prev_lpp = $lpp; $prev_sub = $subtext;
- }
- }
- return $httext;
- }
- function xht_substitute($subtext, $cur_elem = 0, $pre = '', $post = '')
- {
- global $charset;
- $regs = array();
- if (!api_ereg(XHT_SUBS2, $subtext, $regs) && !api_ereg(XHT_SUBS1, $subtext, $regs))
- return $pre . $subtext . $post;
- $type = $regs[1]; $text = $regs[2]; $result = ''; $subnum = FALSE;
- $subtext = isset($regs[3]) ? $regs[3] : '';
- if ($this->xht_dbgn)
- {
- $subnum = ++ $this->xht_dbgn;
- $this->xht_dbgo .= '<!-- ' . XHT_LP . $type . $subnum . '+ ' .
- htmlspecialchars($text, ENT_QUOTES, $charset) . ' ' . XHT_RP .
- $this->_show_param() . " -->\n";
- }
- if ($type == 'D')
- {
-
- $this->xht_param[$text] = $subtext;
-
- }
- elseif ($type == 'E')
- {
- $result = call_user_func($text, FALSE);
- if ($result === FALSE)
- {
- $result = $this->xht_substitute($subtext, $cur_elem);
- $result = call_user_func($text, $result);
- }
- }
- elseif ($type == 'R')
- {
- $rdepthn = 'rdepth' . (++ $this->xht_param['rdepth']);
- $n = 0; $this->xht_param['number'] = '0';
- if (is_array($a = $this->_lang($text, $cur_elem)))
- {
- foreach ($a as $key => $value)
- {
- $this->xht_param['number'] = (string) (++ $n);
- $this->xht_param[$rdepthn] = (string) ($n);
- $this->xht_param['key'] = $key;
- $this->xht_param['value'] = $value;
- $result .= $this->xht_substitute($subtext, $cur_elem);
- }
- }
- elseif (xht_is_assoclist($a))
- {
- foreach (xht_explode_assoclist($a) as $key => $value)
- {
- $this->xht_param['number'] = (string) (++ $n);
- $this->xht_param[$rdepthn] = (string) ($n);
- $this->xht_param['key'] = $key;
- $this->xht_param['value'] = $value;
- $result .= $this->xht_substitute($subtext, $cur_elem);
- }
- }
- elseif (!is_object($this->xht_xmldoc))
- {
- $result = '? R Error: no XML doc has been assigned to xht_xmldoc';
- }
- else
- {
- $sub_elems =
- $this->xht_xmldoc->xmd_select_elements($text, $cur_elem);
- foreach ($sub_elems as $subElem)
- {
- $this->xht_param['number'] = (string) (++ $n);
- $this->xht_param[$rdepthn] = (string) ($n);
- $result .= $this->xht_substitute($subtext, $subElem);
- }
- }
-
-
- $this->xht_param['rdepth'] --;
-
-
- }
- elseif ($type == 'T')
- {
- if (api_ereg('^(=|==|<=|<|!=|<>|>|>=) +([^ ]+) +(.*)$', $subtext, $regs))
- {
-
- $cmp1 = isset($this->xht_param[$text]) ?
- $this->xht_param[$text] : $this->_lang($text, $cur_elem);
- $cmp3 = isset($this->xht_param[$cmp3 = $regs[2]]) ?
- $this->xht_param[$cmp3] : $this->_lang($cmp3, $cur_elem);
- $cmp = strcmp($cmp1, $cmp3); $op = ' ' . $regs[1] . ' ';
- if ($subnum) $this->xht_dbgo .= '<!-- ' . XHT_LP . $type . $subnum .
- ' ' . htmlspecialchars($cmp1.$op.$cmp3.' = '.$cmp, ENT_QUOTES, $charset) .
- ' ' . XHT_RP . " -->\n";
- if ( ($cmp < 0 && api_strpos(' <= < != <> ', $op)) ||
- ($cmp == 0 && api_strpos(' = == <= >= ', $op)) ||
- ($cmp > 0 && api_strpos(' != <> > >= ', $op)) )
- $result = $this->xht_substitute($regs[3], $cur_elem);
-
- }
- else
- {
- $result = $pre . $subtext . $post;
- }
- }
- else
- {
- if (api_strpos('CLPVX', $type) !== FALSE)
- $text = $this->xht_substitute($text, $cur_elem);
- if ($type == 'C')
- $result = $this->xht_fill_template($text, $cur_elem);
- elseif ($type == 'H') $result = htmlspecialchars($text, ENT_QUOTES, $charset);
- elseif ($type == 'L') $result = $this->_lang($text, $cur_elem);
- elseif ($type == 'P') $result = $this->xht_param[$text];
- elseif ($type == 'U') $result = urlencode($text);
- elseif ($type == 'W') $result = xht_htmlwchars($text);
- elseif (!is_object($this->xht_xmldoc))
- {
- $result = '? V/X Error: no XML doc has been assigned to xht_xmldoc';
- }
- else
- {
- if (api_ereg('^(.*)=/(.+)$', $text, $regs))
- {
- $path = $regs[1]; $text = $regs[2];
- if (api_substr($path, -1) == '/') $path = api_substr($path, 0, -1);
- if ($path) $cur_elem = $this->xht_xmldoc->
- xmd_select_single_element($path, $cur_elem);
- $cur_elem = call_user_func($this->xht_resource, $cur_elem);
- }
- $result = ($type == 'V') ?
- $this->xht_xmldoc->xmd_value($text, $cur_elem) :
- $this->xht_xmldoc->
- xmd_html_value($text, $cur_elem, 'xht_htmlwchars');
- }
- }
- if ($subnum) $this->xht_dbgo .= '<!-- ' . XHT_LP . $type . $subnum . '- ' .
- htmlspecialchars((api_strlen($result) <= 60) ?
- $result . ' ': api_substr($result, 0, 57).'...', ENT_QUOTES, $charset) . XHT_RP . " -->\n";
- return $result;
- }
- function xht_add_template($template_name, $httext)
- {
- $this->htt_array[$template_name] = $httext;
-
-
- if (!$this->htt_array[$template_name])
- {
- $this->htt_error = 'template ' . $template_name .
- ' is empty or invalid'; return;
- }
- }
- function xhtdoc($htt_file_contents)
- {
- $htt_file_contents =
- str_replace("\r", "\n", str_replace("\r\n", "\n", $htt_file_contents));
- while (api_substr($htt_file_contents, -1) == "\n")
- $htt_file_contents = api_substr($htt_file_contents, 0, -1);
- $last_line = api_strrchr($htt_file_contents, "\n"); $this->htt_error = '';
- if (api_strlen($last_line) < 12 || api_substr($last_line, 0, 6) != "\n<!-- "
- || api_strlen($last_line) % 2 != 0 || api_substr($last_line, -4) != " -->")
- {
- $this->htt_error = 'last line must be of the form <!-- {--} -->';
- return;
- }
- define('XHT_PL', (int) (api_strlen($last_line) - 10) / 2);
- define('XHT_LP', api_substr($last_line, 6, XHT_PL));
- define('XHT_RP', api_substr($last_line, 6 + XHT_PL, XHT_PL));
- if (XHT_LP == XHT_RP)
- {
- $this->htt_error =
- 'parentheses in last line <!-- {--} --> must not be identical';
- return;
- }
- $this->htt_array = array();
- foreach (explode("\n<!-- " . XHT_LP, "\n" . $htt_file_contents)
- as $name_and_text)
- {
- if (($name_length = api_strpos($name_and_text, XHT_RP . " -->\n")))
- {
- $template_name = trim(api_substr($name_and_text, 0, $name_length));
- if (api_strpos($template_name, ' ') !== FALSE) give_up('Template ' .
- $template_name . ' has a space in its name');
- $httext = api_substr($name_and_text, $name_length + XHT_PL + 5);
- while (api_substr($httext, 0, 1) == "\n") $httext = api_substr($httext, 1);
- while (api_substr($httext, -1) == "\n") $httext = api_substr($httext,0,-1);
- $this->xht_add_template($template_name, $httext);
- }
- }
- define('XHT_SUBS1', '^(C|H|L|P|U|V|W|X) +(.*)$');
-
- define('XHT_SUBS2', '^(D|E|R|T) +([^ ]+) +(.*)$');
-
- $this->xht_dbgo = '';
- $this->xht_param = array(0 => '0', 1 => '1',
- '' => '', 'empty' => '', 'rdepth' => 0);
- $this->_prev_param = $this->xht_param;
-
-
-
- }
- function _show_param() // for debugging info
- {
- global $charset;
- $result = '';
- foreach ($this->xht_param as $k => $v)
- if ($v !== $this->_prev_param[$k]) {
- $this->_prev_param[$k] = $v; $result .= ', ' . $k . ': ' .
- ((api_strlen($v) <= 20) ? $v : api_substr($v, 0, 17).'...');
- }
- return $result ? htmlspecialchars(api_substr($result, 1), ENT_QUOTES, $charset) : '';
- }
- function _lang($var, $cur_elem = 0)
- {
- $result = @call_user_func($this->xht_get_lang, $var, $cur_elem);
-
-
- if ($var == 'Langs')
- {
- global $langLangs;
- if (isset($langLangs))
- {
- $result = $langLangs;
- }
- }
- return isset($result) ? $result : $var;
- }
- }
- ?>
|