function CondText2($pagename, $text, $code = '') { global $Conditions, $CondTextPattern, $CondTextReplacement; $if = "if{$code}"; $repl = str_replace('$pagename', "'{$pagename}'", $CondTextReplacement); $parts = preg_split("/\\(:(?:{$if}end|{$if}|else *{$if}|else{$code})\\b\\s*(.*?)\\s*:\\)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE); $x = array_shift($parts); while ($parts) { list($condspec, $condtext) = array_splice($parts, 0, 2); if (!preg_match("/^\\s*(!?)\\s*(\\S*)\\s*(.*?)\\s*\$/", $condspec, $match)) { continue; } list($x, $not, $condname, $condparm) = $match; if (!isset($Conditions[$condname])) { return PPRE($CondTextPattern, $repl, $condtext); } $tf = @eval("return ({$Conditions[$condname]});"); if ($tf xor $not) { return PPRE($CondTextPattern, $repl, $condtext); } } return ''; }
function MarkupRestore($text) { global $KeepToken, $KPV; return PPRE("/$KeepToken(\\d.*?)$KeepToken/", "\$GLOBALS['KPV'][\$m[1]]", $text); }
function MarkupExpression($pagename, $expr) { global $KeepToken, $KPV, $MarkupExpr; $rpat = "/$KeepToken(\\d+P)$KeepToken/"; $rrep = '$GLOBALS["KPV"][$m[1]]'; $expr = PPRE('/([\'"])(.*?)\\1/', "Keep(\$m[2],'P')", $expr); $expr = PPRE('/\\(\\W/', "Keep(\$m[0],'P')", $expr); while (preg_match('/\\((\\w+)(\\s[^()]*)?\\)/', $expr, $match)) { list($repl, $func, $params) = $match; $code = @$MarkupExpr[$func]; ## if not a valid function, save this string as-is and exit if (!$code) break; ## if the code uses '$params', we just evaluate directly if (strpos($code, '$params') !== false) { $out = eval("return ({$code});"); if ($expr == $repl) { $expr = $out; break; } $expr = str_replace($repl, $out, $expr); continue; } ## otherwise, we parse arguments into $args before evaluating $argp = ParseArgs($params); $x = $argp['#']; $args = array(); while ($x) { list($k, $v) = array_splice($x, 0, 2); if ($k == '' || $k == '+' || $k == '-') $args[] = $k.PPRE($rpat, $rrep, $v); } ## fix any quoted arguments foreach ($argp as $k => $v) if (!is_array($v)) $argp[$k] = PPRE($rpat, $rrep, $v); $out = eval("return ({$code});"); if ($expr == $repl) { $expr = $out; break; } $expr = str_replace($repl, Keep($out, 'P'), $expr); } return PPRE($rpat, $rrep, $expr); }
function ApplyStyles($x) { global $UrlExcludeChars, $WikiStylePattern, $WikiStyleRepl, $WikiStyle, $WikiStyleAttr, $WikiStyleCSS, $WikiStyleApply, $BlockPattern, $WikiStyleTag, $imgTag, $aTag, $spanTag, $WikiStyleAttrPrefix; $wt = @$WikiStyleTag; $ns = $WikiStyleAttrPrefix; $ws = ''; $x = PPRE("/\\b(href|src)=(['\"]?)[^{$UrlExcludeChars}]+\\2/", "Keep(\$m[0])", $x); $x = PPRE("/\\bhttps?:[^{$UrlExcludeChars}]+/", "Keep(\$m[0])", $x); $parts = preg_split("/({$WikiStylePattern})/", $x, -1, PREG_SPLIT_DELIM_CAPTURE); $parts[] = NULL; $out = ''; $style = array(); $wikicsspat = '/^(' . implode('|', (array) $WikiStyleCSS) . ')$/'; while ($parts) { $p = array_shift($parts); if (preg_match("/^{$WikiStylePattern}\$/", $p)) { $WikiStyle['curr'] = $style; $style = array(); foreach ((array) $WikiStyleRepl as $pat => $rep) { $p = preg_replace($pat, $rep, $p); } preg_match_all('/\\b([a-zA-Z][-\\w]*)([:=]([-#,\\w.()%]+|([\'"]).*?\\4))?/', $p, $match, PREG_SET_ORDER); while ($match) { $m = array_shift($match); if (@$m[2]) { $style[$m[1]] = preg_replace('/^([\'"])(.*?)\\1$/', '$2', $m[3]); } else { if (!isset($WikiStyle[$m[1]])) { @($style['class'] .= ' ' . $m[1]); } else { $c = @$style['class']; $style = array_merge($style, (array) $WikiStyle[$m[1]]); if ($c && !preg_match("/(^| ){$c}( |\$)/", $style['class'])) { $style['class'] = $c . ' ' . $style['class']; } } } } if (@$style['define']) { $d = $style['define']; unset($style['define']); $WikiStyle[$d] = $style; } if (@$WikiStyleApply[$style['apply']]) { $apply[$style['apply']] = array_merge((array) @$apply[$style['apply']], $style); $style = array(); } continue; } if (is_null($p)) { $alist = @$apply; unset($alist['']); $p = $out; $out = ''; } elseif ($p == '') { continue; } else { $alist = array('' => $style); } foreach ((array) $alist as $a => $s) { $spanattr = ''; $stylev = array(); $id = ''; foreach ((array) $s as $k => $v) { $v = trim($v); if ($wt) { $ws = str_replace('$1', "{$ns}{$k}='{$v}'", $wt); } if ($k == 'class' && $v) { $spanattr = "{$ns}class='{$v}'"; } elseif ($k == 'id') { $id = preg_replace('/[^-A-Za-z0-9:_.]+/', '_', $v); } elseif (($k == 'width' || $k == 'height') && !@$WikiStyleApply[$a] && preg_match("/\\s*<{$imgTag}\\b/", $p)) { $p = preg_replace("/<({$imgTag})\\b(?![^>]*\\s{$k}=)/", "{$ws}<\$1 {$ns}{$k}='{$v}'", $p); } elseif (@$WikiStyleAttr[$k]) { $p = preg_replace("/<({$WikiStyleAttr[$k]}(?![^>]*\\s(?:{$ns})?{$k}=))([^>]*)>/s", "{$ws}<\$1 {$ns}{$k}='{$v}' \$2>", $p); } elseif (preg_match($wikicsspat, $k)) { $stylev[] = "{$k}: {$v};"; } } if ($stylev) { $spanattr .= " {$ns}style='" . implode(' ', $stylev) . "'"; } if ($id) { $spanattr .= " {$ns}id='{$id}'"; } if ($spanattr) { if ($wt) { $ws = str_replace('$1', $spanattr, $wt); } if (!@$WikiStyleApply[$a]) { $p = preg_replace("!^(.*?)(\$|</?({$BlockPattern}))!s", "{$ws}<{$spanTag} {$spanattr}>\$1</{$spanTag}>\$2", $p, 1); } elseif (!preg_match('/^(\\s*<[^>]+>)*$/s', $p) || preg_match("/<{$imgTag}\\b/", $p)) { $p = preg_replace("/<({$WikiStyleApply[$a]})\\b/", "{$ws}<\$1 {$spanattr}", $p); } } if (@$s['color']) { $colorattr = "{$ns}style='color: {$s['color']}'"; if ($wt) { $ws = str_replace('$1', $colorattr, $wt); } $p = preg_replace("/<{$aTag}\\b/", "{$ws}<{$aTag} {$colorattr}", $p); } } $out .= $p; } return $out; }
function DiffHTML($pagename, $diff) { if (@$_REQUEST['nodiff']>'') return ''; global $FmtV, $DiffShow, $DiffAddFmt, $DiffDelFmt, $DiffEndDelAddFmt, $DiffRenderSourceFunction; SDV($DiffRenderSourceFunction, 'DiffRenderSource'); $difflines = explode("\n",$diff."\n"); $in=array(); $out=array(); $dtype=''; $html = ''; foreach($difflines as $d) { if ($d>'') { if ($d[0]=='-' || $d[0]=='\\') continue; if ($d[0]=='<') { $out[]=substr($d,2); continue; } if ($d[0]=='>') { $in[]=substr($d,2); continue; } } if (preg_match("/^(\\d+)(,(\\d+))?([adc])(\\d+)(,(\\d+))?/", $dtype,$match)) { if (@$match[7]>'') { $lines='lines'; $count=$match[1].'-'.($match[1]+$match[7]-$match[5]); } elseif ($match[3]>'') { $lines='lines'; $count=$match[1].'-'.$match[3]; } else { $lines='line'; $count=$match[1]; } if ($match[4]=='a' || $match[4]=='c') { $txt = str_replace('line',$lines,$DiffDelFmt[$match[4]]); $FmtV['$DiffLines'] = $count; $html .= FmtPageName($txt,$pagename); if ($DiffShow['source']=='y') $html .= "<div class='diffmarkup'>" .$DiffRenderSourceFunction($in, $out, 0) ."</div>"; else $html .= MarkupToHTML($pagename, PPRE('/\\(:.*?:\\)/',"Keep(PHSC(\$m[0]))", join("\n",$in))); } if ($match[4]=='d' || $match[4]=='c') { $txt = str_replace('line',$lines,$DiffAddFmt[$match[4]]); $FmtV['$DiffLines'] = $count; $html .= FmtPageName($txt,$pagename); if ($DiffShow['source']=='y') $html .= "<div class='diffmarkup'>" .$DiffRenderSourceFunction($in, $out, 1) ."</div>"; else $html .= MarkupToHTML($pagename, PPRE('/\\(:.*?:\\)/',"Keep(PHSC(\$m[0]))",join("\n",$out))); } $html .= FmtPageName($DiffEndDelAddFmt,$pagename); } $in=array(); $out=array(); $dtype=$d; } return $html; }
function FPLExpandItemVars($item, $matches, $idx, $psvars) { global $Cursor, $EnableUndefinedTemplateVars; $Cursor['<'] = $Cursor['<'] = (string)@$matches[$idx-1]; $Cursor['='] = $pn = (string)@$matches[$idx]; $Cursor['>'] = $Cursor['>'] = (string)@$matches[$idx+1]; $item = str_replace(array_keys($psvars), array_values($psvars), $item); $item = PPRE('/\\{(=|&[lg]t;)(\\$:?\\w[-\\w]*)\\}/', "PVSE(PageVar('$pn', \$m[2], \$m[1]))", $item); if(! IsEnabled($EnableUndefinedTemplateVars, 0)) $item = preg_replace("/\\{\\$\\$\\w+\\}/", '', $item); return $item; }
function utf8string($str, $start = false, $len = false) { # strlen+substr++ combo for UTF-8 global $CaseConversions; static $lower; if (!@$lower) { $lower = implode('|', array_keys($CaseConversions)); } $ascii = preg_match('/[\\x80-\\xFF]/', $str) ? 0 : 1; switch ((string) $start) { case 'ucfirst': return $ascii ? ucfirst($str) : PPRE("/^({$lower})/", '$GLOBALS["CaseConversions"][$m[1]]', $str); case 'ucwords': return $ascii ? ucwords($str) : PPRE("/(^|\\s+)({$lower})/", '$m[1].$GLOBALS["CaseConversions"][$m[2]]', $str); case 'tolower': return $ascii ? strtolower($str) : utf8fold($str); case 'toupper': return $ascii ? strtoupper($str) : utf8toupper($str); } if ($ascii) { if ($start === 'strlen') { return strlen($str); } if ($len === false) { return substr($str, $start); } return substr($str, $start, $len); } $letters = preg_split("/([\\x00-\\x7f]|[\\xc2-\\xdf].|[\\xe0-\\xef]..|[\\xf0-\\xf4]...)/", $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if ($start === 'strlen') { return count($letters); } if ($len === false) { return implode('', array_slice($letters, $start)); } return implode('', array_slice($letters, $start, $len)); }
function CondText2($pagename, $text, $code = '') { global $Conditions, $CondTextPattern, $CondTextReplacement; $if = "if$code"; $parts = preg_split("/\\(:(?:{$if}end|$if|else *$if|else$code)\\b\\s*(.*?)\\s*:\\)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE); $x = array_shift($parts); while ($parts) { list($condspec, $condtext) = array_splice($parts, 0, 2); if (!preg_match("/^\\s*(!?)\\s*(\\S*)\\s*(.*?)\\s*$/", $condspec, $match)) continue; list($x, $not, $condname, $condparm) = $match; if (!isset($Conditions[$condname])) return PPRE($CondTextPattern, $CondTextReplacement, $condtext); $tf = @eval("return ({$Conditions[$condname]});"); if ($tf xor $not) return PPRE($CondTextPattern, $CondTextReplacement, $condtext); } return ''; }