function HandleApprove($pagename) { global $ApproveUrlPattern, $WhiteUrlPatterns, $ApprovedUrlPagesFmt, $action; Lock(2); $page = ReadPage($pagename); $text = preg_replace('/[()]/', '', $page['text']); preg_match_all("/{$ApproveUrlPattern}/", $text, $match); ReadApprovedUrls($pagename); $addpat = array(); foreach ($match[0] as $a) { foreach ((array) $WhiteUrlPatterns as $pat) { if (preg_match("!^{$pat}(/|\$)!", $a)) { continue 2; } } if ($action == 'approvesites') { $a = preg_replace("!^([^:]+://[^/]+).*\$!", '$1', $a); } $addpat[] = $a; } if (count($addpat) > 0) { $aname = FmtPageName($ApprovedUrlPagesFmt[0], $pagename); $apage = ReadPage($aname, ''); $new = $apage; if (substr($new['text'], -1, 1) != "\n") { $new['text'] .= "\n"; } foreach ($addpat as $pat) { $new['text'] .= " {$pat}\n"; } $_REQUEST['post'] = 'y'; PostPage($aname, $apage, $new); } Redirect($pagename); }
function ReadTrail($pagename, $trailname) { global $SuffixPattern, $GroupPattern, $WikiWordPattern; $trailname = MakePageName($pagename, $trailname); $trailpage = ReadPage($trailname); if (!$trailpage) { return false; } $t = array(); $n = 0; foreach (explode("\n", @$trailpage['text']) as $x) { $x = preg_replace("/^([#*]+)\\s*(({$GroupPattern}([\\.]))?{$WikiWordPattern})/", '$1 [[$2]]', $x); $x = preg_replace("/\\[\\[([^\\]]*)->([^\\]]*)\\]\\]/", '[[$2|$1]]', $x); if (!preg_match("/^([#*]+)\\s*(\\[\\[([^|]*?)(\\|.*?)?\\]\\]({$SuffixPattern}))(.*)\$/", $x, $match)) { continue; } $tgt = MakePageName($trailname, $match[3]); $t[$n]['depth'] = $depth = strlen($match[1]); $t[$n]['pagename'] = $tgt; $t[$n]['markup'] = $match[2]; for ($i = $depth; $i < 10; $i++) { $d[$i] = $n; } if ($depth > 1) { $t[$n]['parent'] = @$d[$depth - 1]; } $n++; } return $t; }
function VarIndexLoad($pagename) { global $VarPagesFmt,$VarIndex,$WikiWordPattern; static $loaded; $VarIndex = (array)@$VarIndex; if ($loaded) return; foreach($VarPagesFmt as $vf) { $v = FmtPageName($vf, $pagename); if (@$loaded[$v]) continue; $vlist = array($v); $t = ReadTrail($pagename,$v); if ($t) for($i=0;$i<count($t);$i++) if (@!$loaded[$t[$i]['pagename']]) $vlist[]=$t[$i]['pagename']; foreach($vlist as $vname) { $vpage = ReadPage($vname, READPAGE_CURRENT); @$loaded[$vname]++; if (!$vpage) continue; if (!preg_match_all("/\n:\\$([[:upper:]]\\w+):/",@$vpage['text'],$match)) continue; foreach($match[1] as $n) { $VarIndex[$n]['pagename'] = $vname; $VarIndex[$n]['url'] = FmtPageName("{\$PageUrl}#$n",$vname); } } } }
function EditDraft(&$pagename, &$page, &$new) { global $WikiDir, $DraftSuffix, $DeleteKeyPattern, $EnableDraftAtomicDiff, $DraftRecentChangesFmt, $RecentChangesFmt, $Now; SDV($DeleteKeyPattern, "^\\s*delete\\s*\$"); $basename = preg_replace("/{$DraftSuffix}\$/", '', $pagename); $draftname = $basename . $DraftSuffix; if ($_POST['postdraft'] || $_POST['postedit']) { $pagename = $draftname; } else { if ($_POST['post'] && !preg_match("/{$DeleteKeyPattern}/", $new['text'])) { $pagename = $basename; if (IsEnabled($EnableDraftAtomicDiff, 0)) { $page = ReadPage($basename); foreach ($new as $k => $v) { # delete draft history if (preg_match('/:\\d+(:\\d+:)?$/', $k) && !preg_match("/:{$Now}(:\\d+:)?\$/", $k)) { unset($new[$k]); } } unset($new['rev']); SDVA($new, $page); } $WikiDir->delete($draftname); } else { if (PageExists($draftname) && $pagename != $draftname) { Redirect($draftname, '$PageUrl?action=edit'); exit; } } } if ($pagename == $draftname && isset($DraftRecentChangesFmt)) { $RecentChangesFmt = $DraftRecentChangesFmt; } }
function PrintDiff($pagename) { global $DiffHTMLFunction,$DiffShow,$DiffStartFmt,$TimeFmt, $DiffEndFmt,$DiffRestoreFmt,$FmtV, $LinkFunctions; $page = ReadPage($pagename); if (!$page) return; krsort($page); reset($page); $lf = $LinkFunctions; $LinkFunctions['http:'] = 'LinkSuppress'; $LinkFunctions['https:'] = 'LinkSuppress'; SDV($DiffHTMLFunction, 'DiffHTML'); foreach($page as $k=>$v) { if (!preg_match("/^diff:(\d+):(\d+):?([^:]*)/",$k,$match)) continue; $diffclass = $match[3]; if ($diffclass=='minor' && $DiffShow['minor']!='y') continue; $diffgmt = $FmtV['$DiffGMT'] = $match[1]; $FmtV['$DiffTime'] = strftime($TimeFmt,$diffgmt); $diffauthor = @$page["author:$diffgmt"]; if (!$diffauthor) @$diffauthor=$page["host:$diffgmt"]; if (!$diffauthor) $diffauthor="unknown"; $FmtV['$DiffChangeSum'] = htmlspecialchars(@$page["csum:$diffgmt"]); $FmtV['$DiffHost'] = @$page["host:$diffgmt"]; $FmtV['$DiffAuthor'] = $diffauthor; $FmtV['$DiffId'] = $k; $html = $DiffHTMLFunction($pagename, $v); if ($html===false) continue; echo FmtPageName($DiffStartFmt,$pagename); echo $html; echo FmtPageName($DiffEndFmt,$pagename); echo FmtPageName($DiffRestoreFmt,$pagename); } $LinkFunctions = $lf; }
function AuthUserId($pagename, $id, $pw = NULL) { global $AuthUser, $AuthUserPageFmt, $AuthUserFunctions, $AuthId, $AuthList, $MessagesFmt; $auth = $AuthUser; $authid = ''; # load information from Site.AuthUser (or page in $AuthUserPageFmt) SDV($AuthUserPageFmt, '$SiteGroup.AuthUser'); SDVA($AuthUserFunctions, array('htpasswd' => 'AuthUserHtPasswd', 'ldap' => 'AuthUserLDAP', $id => 'AuthUserConfig')); $pn = FmtPageName($AuthUserPageFmt, $pagename); $apage = ReadPage($pn, READPAGE_CURRENT); if ($apage && preg_match_all("/\n\\s*([@\\w][^\\s:]*):(.*)/", $apage['text'], $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { if (!preg_match_all('/\\bldap:\\S+|[^\\s,]+/', $m[2], $v)) { continue; } if ($m[1][0] == '@') { foreach ($v[0] as $g) { $auth[$g][] = $m[1]; } } else { $auth[$m[1]] = array_merge((array) @$auth[$m[1]], $v[0]); } } } if (is_null($pw)) { $authid = $id; } else { foreach ($AuthUserFunctions as $k => $fn) { if ($auth[$k] && $fn($pagename, $id, $pw, $auth[$k])) { $authid = $id; break; } } } if (!$authid) { $GLOBALS['InvalidLogin'] = 1; return; } if (!isset($AuthId)) { $AuthId = $authid; } @session_start(); $_SESSION['authid'] = $authid; $_SESSION['authlist']["id:{$authid}"] = 1; $_SESSION['authlist']["id:-{$authid}"] = -1; $_SESSION['authlist']["id:*"] = 1; foreach ((array) @$auth[$authid] as $g) { if ($g[0] == '@') { @($_SESSION['authlist'][$g] = 1); } } foreach ((array) @$auth['*'] as $g) { if ($g[0] == '@') { @($_SESSION['authlist'][$g] = 1); } } $AuthList = array_merge($AuthList, $_SESSION['authlist']); }
function HTTPBasicAuth($pagename, $level, $authprompt = true) { global $AuthRealmFmt, $AuthDeniedFmt, $DefaultPasswords, $AllowPassword, $GroupAttributesFmt; SDV($GroupAttributesFmt, '$Group/GroupAttributes'); SDV($AllowPassword, 'nopass'); SDV($AuthRealmFmt, $GLOBALS['WikiTitle']); SDV($AuthDeniedFmt, 'A valid password is required to access this feature.'); $page = ReadPage($pagename); if (!$page) { return false; } $passwd = @$page["passwd{$level}"]; if ($passwd == "") { $grouppg = ReadPage(FmtPageName($GroupAttributesFmt, $pagename)); $passwd = @$grouppg["passwd{$level}"]; if ($passwd == '') { $passwd = @$DefaultPasswords[$level]; } if ($passwd == '') { $passwd = @$page["passwdread"]; } if ($passwd == '') { $passwd = @$grouppg["passwdread"]; } if ($passwd == '') { $passwd = @$DefaultPasswords['read']; } } if ($passwd == '') { return $page; } if (crypt($AllowPassword, $passwd) == $passwd) { return $page; } @session_start(); if (@$_SERVER['PHP_AUTH_PW']) { @$_SESSION['authpw'][$_SERVER['PHP_AUTH_PW']]++; } $authpw = array_keys((array) @$_SESSION['authpw']); foreach (array_merge((array) $DefaultPasswords['admin'], (array) $passwd) as $pwchal) { foreach ($authpw as $pwresp) { if (@crypt($pwresp, $pwchal) == $pwchal) { return $page; } } } if (!$authprompt) { return false; } $realm = FmtPageName($AuthRealmFmt, $pagename); header("WWW-Authenticate: Basic realm=\"{$realm}\""); header("Status: 401 Unauthorized"); header("HTTP-Status: 401 Unauthorized"); PrintFmt($pagename, $AuthDeniedFmt); exit; }
function ExportActionHandler($pagename, $auth) { // read the page and sort keys chronologically $page = ReadPage($pagename); if (!$page || !$page['name']) { return; } krsort($page); reset($page); // start with the latest version $versions = []; $version['author'] = $page['author']; $version['timestamp'] = $page['time']; $version['source'] = utf8_encode($page['text']); array_push($versions, $version); // use the page's diff keys to restore markup for older version foreach ($page as $key => $value) { if (!preg_match("/^diff:(\\d+):(\\d+):?([^:]*)/", $key, $match)) { continue; } // ignore the original diff tag, which is is not a delta // ignore blank changes // ignore some rare dupes if ($match[1] === $match[2]) { continue; } if (@$page[$match[0]] === '') { continue; } if (array_search($match[2], array_column($versions, 'timestamp'))) { continue; } // metadata $diffgmt = $match[1]; $diffauthor = @$page["author:{$diffgmt}"]; if (!$diffauthor) { @($diffauthor = $page["host:{$diffgmt}"]); } if (!$diffauthor) { $diffauthor = "unknown"; } $diffchangesum = PHSC(@$page["csum:{$diffgmt}"]); // page version $version = []; $version['author'] = $diffauthor; $version['timestamp'] = $match[2]; $version['commit_message'] = $diffchangesum; $version['source'] = utf8_encode(RestorePage($pagename, $page, $new, $match[0])); array_push($versions, $version); } // finalize response $response['page_name'] = $pagename; $response['versions'] = $versions; $response = json_encode($response); echo $response; }
function AuthUserId($pagename, $id, $pw=NULL) { global $AuthUser, $AuthUserPageFmt, $AuthUserFunctions, $AuthId, $MessagesFmt, $AuthUserPat; $auth = array(); foreach((array)$AuthUser as $k=>$v) $auth[$k] = (array)$v; $authid = ''; # load information from SiteAdmin.AuthUser (or page in $AuthUserPageFmt) SDV($AuthUserPageFmt, '$SiteAdminGroup.AuthUser'); SDVA($AuthUserFunctions, array( 'htpasswd' => 'AuthUserHtPasswd', 'ldap' => 'AuthUserLDAP', # 'mysql' => 'AuthUserMySQL', $id => 'AuthUserConfig')); SDV($AuthUserPat, "/^\\s*([@\\w][^\\s:]*):(.*)/m"); $pn = FmtPageName($AuthUserPageFmt, $pagename); $apage = ReadPage($pn, READPAGE_CURRENT); if ($apage && preg_match_all($AuthUserPat, $apage['text'], $matches, PREG_SET_ORDER)) { foreach($matches as $m) { if (!preg_match_all('/\\bldaps?:\\S+|[^\\s,]+/', $m[2], $v)) continue; if ($m[1]{0} == '@') foreach($v[0] as $g) $auth[$g][] = $m[1]; else $auth[$m[1]] = array_merge((array)@$auth[$m[1]], $v[0]); } } if (func_num_args()==2) $authid = $id; else foreach($AuthUserFunctions as $k => $fn) if (@$auth[$k] && $fn($pagename, $id, $pw, $auth[$k], $authlist)) { $authid = $id; break; } if (!$authid) { $GLOBALS['InvalidLogin'] = 1; return; } if (!isset($AuthId)) $AuthId = $authid; $authlist["id:$authid"] = 1; $authlist["id:-$authid"] = -1; foreach(preg_grep('/^@/', (array)@$auth[$authid]) as $g) $authlist[$g] = 1; foreach(preg_grep('/^@/', (array)@$auth['*']) as $g) $authlist[$g] = 1; foreach(preg_grep('/^@/', array_keys($auth)) as $g) # useless? PITS:01201 if (in_array($authid, $auth[$g])) $authlist[$g] = 1; if ($auth['htgroup']) { foreach(AuthUserHtGroup($pagename, $id, $pw, $auth['htgroup']) as $g) $authlist["@$g"] = 1; } foreach(preg_grep('/^@/', (array)@$auth["-$authid"]) as $g) unset($authlist[$g]); SessionAuth($pagename, array('authid' => $authid, 'authlist' => $authlist)); }
function EditDraft(&$pagename, &$page, &$new) { global $WikiDir, $DraftSuffix, $DeleteKeyPattern; SDV($DeleteKeyPattern, "^\\s*delete\\s*\$"); $basename = preg_replace("/{$DraftSuffix}\$/", '', $pagename); $draftname = $basename . $DraftSuffix; if ($_POST['postdraft']) { $pagename = $draftname; return; } if ($_POST['post'] && !preg_match("/{$DeleteKeyPattern}/", $new['text'])) { $pagename = $basename; $page = ReadPage($basename); $WikiDir->delete($draftname); return; } if (PageExists($draftname) && $pagename != $draftname) { Redirect($draftname, '$PageUrl?action=edit'); exit; } }
function ReadTrail($pagename, $trailname) { global $SuffixPattern, $GroupPattern, $WikiWordPattern, $LinkWikiWords; if (preg_match('/^\\[\\[(.+?)(->|\\|)(.+?)\\]\\]$/', $trailname, $m)) { $trailname = $m[2] == '|' ? $m[1] : $m[3]; } $trailname = MakePageName($pagename, $trailname); $trailpage = ReadPage($trailname, READPAGE_CURRENT); if (!$trailpage) { return false; } $t = array(); $n = 0; foreach (explode("\n", @$trailpage['text']) as $x) { $x = preg_replace("/\\[\\[([^\\]]*)->([^\\]]*)\\]\\]/", '[[$2|$1]]', $x); if (!preg_match("/^([#*:]+) \\s* \n (\\[\\[([^:#!|][^|:]*?)(\\|.*?)?\\]\\]({$SuffixPattern})\n | (({$GroupPattern}([\\/.]))?{$WikiWordPattern})) (.*)/x", $x, $match)) { continue; } if (@$match[6]) { if (!$LinkWikiWords) { continue; } $tgt = MakePageName($trailname, $match[6]); } else { $tgt = MakePageName($trailname, preg_replace('/[#?].+/', '', $match[3])); } $t[$n]['depth'] = $depth = strlen($match[1]); $t[$n]['pagename'] = $tgt; $t[$n]['markup'] = $match[2]; $t[$n]['detail'] = $match[9]; for ($i = $depth; $i < 10; $i++) { $d[$i] = $n; } if ($depth > 1) { $t[$n]['parent'] = @$d[$depth - 1]; } $n++; } return $t; }
function VarIndexLoad($pagename) { global $VarPagesFmt, $VarIndex, $WikiWordPattern; static $loaded; if ($loaded) { return; } foreach ($VarPagesFmt as $vf) { $v = FmtPageName($vf, $pagename); if (@$loaded[$v]) { continue; } $vlist = array($v); $t = ReadTrail($pagename, $v); if ($t) { for ($i = 0; $i < count($t); $i++) { if (@(!$loaded[$t[$i]['pagename']])) { $vlist[] = $t[$i]['pagename']; } } } foreach ($vlist as $vname) { $vpage = ReadPage($vname); @$loaded[$vname]++; if (!$vpage) { continue; } if (!preg_match_all("/\n:\\\$([[:upper:]]\\w+):/", @$vpage['text'], $match)) { continue; } foreach ($match[1] as $n) { $VarIndex[$n]['pagename'] = $vname; $VarIndex[$n]['url'] = FmtPageName("\$PageUrl#{$n}", $vname); } } } }
function EditDraft(&$pagename, &$page, &$new) { global $WikiDir, $DraftSuffix, $DeleteKeyPattern, $DraftRecentChangesFmt, $RecentChangesFmt; SDV($DeleteKeyPattern, "^\\s*delete\\s*\$"); $basename = preg_replace("/{$DraftSuffix}\$/", '', $pagename); $draftname = $basename . $DraftSuffix; if ($_POST['postdraft'] || $_POST['postedit']) { $pagename = $draftname; } else { if ($_POST['post'] && !preg_match("/{$DeleteKeyPattern}/", $new['text'])) { $pagename = $basename; $page = ReadPage($basename); $WikiDir->delete($draftname); } else { if (PageExists($draftname) && $pagename != $draftname) { Redirect($draftname, '$PageUrl?action=edit'); exit; } } } if ($pagename == $draftname && isset($DraftRecentChangesFmt)) { $RecentChangesFmt = $DraftRecentChangesFmt; } }
function evaluateTrail () { $trailpage = ReadPage($this->pagename); if ($trailpage) { global $M2MDir, $MarkupTable, $SuffixPattern; $html = MarkupToHTML($this->pagename, $trailpage['text']); } }
function HandleUpgrade($pagename, $auth = 'ALWAYS') { global $SiteGroup, $SiteAdminGroup, $StatusPageName, $ScriptUrl, $AuthUserPageFmt, $VersionNum, $Version; StopWatch('HandleUpgrade: begin'); $message = ''; $done = ''; ## check for Site.* --> SiteAdmin.* foreach(array('AuthUser', 'NotifyList', 'Blocklist', 'ApprovedUrls') as $n) { $n0 = "$SiteGroup.$n"; $n1 = "$SiteAdminGroup.$n"; StopWatch("HandleUpgrade: checking $n0 -> $n1"); ## checking AuthUser is special, because Site.AuthUser comes with the ## distribution. if ($n == 'AuthUser') { ## if we already have a user-modified SiteAdmin.AuthUser, we can skip SDV($AuthUserPageFmt, '$SiteAdminGroup.AuthUser'); $n1 = FmtPageName($AuthUserPageFmt, $pagename); $page = ReadPage($n1, READPAGE_CURRENT); if (@$page['time'] > 1000000000) continue; ## if there's not a user-modified Site.AuthUser, we can skip $page = ReadPage($n0, READPAGE_CURRENT); if (@$page['time'] == 1000000000) continue; } else if (!PageExists($n0) || PageExists($n1)) continue; if (@$_REQUEST['migrate'] == 'yes') { ## if the admin wants PmWiki to migrate, do it. $page = RetrieveAuthPage($n0, 'admin', true); StopWatch("HandleUpgrade: copying $n0 -> $n1"); if ($page) { WritePage($n1, $page); $done .= "<li>Copied $n0 to $n1</li>"; continue; } } $message .= "<li>$n0 -> $n1</li>"; } if ($message) { $migrateurl = "$ScriptUrl?action=upgrade&migrate=yes"; $infourl = 'http://www.pmwiki.org/wiki/PmWiki/UpgradeToSiteAdmin'; $message = "<h2>Upgrade notice -- SiteAdmin group</h2> <p>This version of PmWiki expects several administrative pages from the <em>Site</em> group to be found in a new <em>SiteAdmin</em> group. On this site, the following pages appear to need to be relocated:</p> <ul>$message</ul> <p>For more information about this change, including the various options for proceeding, see</p> <blockquote><a target='_blank' href='$infourl'>$infourl</a></blockquote> <form action='$ScriptUrl' method='post'> <p>If you would like PmWiki to attempt to automatically copy these pages into their new <br /> locations for you, try <input type='hidden' name='action' value='upgrade' /> <input type='hidden' name='migrate' value='yes' /> <input type='submit' value='Relocate pages listed above' /> (admin password required) </p> </form> <p>If you want to configure PmWiki so that it continues to look for the above pages in <em>$SiteGroup</em>, add the following line near the top of <em>local/config.php</em>:</p> <blockquote><pre>\$SiteAdminGroup = \$SiteGroup;</pre></blockquote> $Version "; print $message; exit; } StopWatch("UpgradeCheck: writing $StatusPageName"); Lock(2); SDV($StatusPageName, "$SiteAdminGroup.Status"); $page = ReadPage($StatusPageName); $page['updatedto'] = $VersionNum; WritePage($StatusPageName, $page); if ($done) { $done .= "<li>Updated $StatusPageName</li>"; echo "<h2>Upgrade to $Version ... ok</h2><ul>$done</ul>"; $GLOBALS['EnableRedirect'] = 0; } Redirect($pagename); }
function PmWikiAuth($pagename, $level, $authprompt = true, $since = 0) { global $DefaultPasswords, $AllowPassword, $GroupAttributesFmt, $AuthCascade, $FmtV, $AuthPromptFmt, $PageStartFmt, $PageEndFmt, $AuthId, $AuthList; static $grouppasswd, $authpw; SDV($GroupAttributesFmt, '$Group/GroupAttributes'); SDV($AllowPassword, 'nopass'); $page = ReadPage($pagename, $since); if (!$page) { return false; } $groupattr = FmtPageName($GroupAttributesFmt, $pagename); if (!isset($grouppasswd[$groupattr])) { $grouppasswd[$groupattr] = array(); $gp = ReadPage($groupattr, READPAGE_CURRENT); foreach ($DefaultPasswords as $k => $v) { $grouppasswd[$groupattr][$k] = isset($gp["passwd{$k}"]) ? NormalizeAuth($gp["passwd{$k}"], 'group') : NormalizeAuth($v, 'site'); } } foreach ($DefaultPasswords as $k => $v) { $passwd[$k] = isset($page["passwd{$k}"]) ? NormalizeAuth($page["passwd{$k}"], 'page') : $grouppasswd[$groupattr][$k]; $page['=pwsource'][$k] = @$passwd[$k]['=pwsource']; unset($passwd[$k]['=pwsource']); } $page['=passwd'] = $passwd; foreach ($AuthCascade as $k => $t) { if (!$passwd[$k] && $passwd[$t]) { $passwd[$k] = $passwd[$t]; $page['=pwsource'][$k] = "cascade:{$t}"; } } if (!isset($authpw)) { if (@$_POST['authpw'] || @$_REQUEST[session_name()]) { $sid = session_id(); @session_start(); if (@$_POST['authpw']) { @$_SESSION['authpw'][$_POST['authpw']]++; } $authpw = array_keys((array) @$_SESSION['authpw']); if (!isset($AuthId)) { $AuthId = @$_SESSION['authid']; } $AuthList = array_merge($AuthList, (array) @$_SESSION['authlist']); if (!$sid) { session_write_close(); } } else { $authpw = array(); } if (@$AuthId) { $AuthList["id:{$AuthId}"] = 1; $AuthList["id:-{$AuthId}"] = -1; $AuthList["id:*"] = 1; } } foreach ($passwd as $lv => $a) { if (!$a) { @$page['=auth'][$lv]++; continue; } foreach ((array) $a as $pwchal) { if (preg_match('/^@|^\\w+:/', $pwchal)) { if (@$AuthList[$pwchal] > 0) { @$page['=auth'][$lv]++; continue 2; } if (@$AuthList[$pwchal] < 0) { continue 2; } continue; } if (crypt($AllowPassword, $pwchal) == $pwchal) { @$page['=auth'][$lv]++; continue 2; } foreach ($authpw as $pwresp) { if (crypt($pwresp, $pwchal) == $pwchal) { @$page['=auth'][$lv]++; continue 2; } } } } if (@$page['=auth']['admin']) { foreach ($passwd as $lv => $a) { @$page['=auth'][$lv]++; } } if (@$page['=auth'][$level]) { return $page; } if (!$authprompt) { return false; } $GLOBALS['AuthNeeded'] = @$_POST['authpw'] ? $page['=pwsource'][$level] . ' ' . $level : ''; PCache($pagename, $page); $postvars = ''; foreach ($_POST as $k => $v) { if ($k == 'authpw' || $k == 'authid') { continue; } $v = str_replace('$', '$', htmlspecialchars(stripmagic($v), ENT_COMPAT)); $postvars .= "<input type='hidden' name='{$k}' value=\"{$v}\" />\n"; } $FmtV['$PostVars'] = $postvars; SDV($AuthPromptFmt, array(&$PageStartFmt, "<p><b>\$[Password required]</b></p>\n <form name='authform' action='{$_SERVER['REQUEST_URI']}' method='post'>\n \$[Password]: <input tabindex='1' type='password' name='authpw' \n value='' />\n <input type='submit' value='OK' />\$PostVars</form>\n <script language='javascript' type='text/javascript'><!--\n document.authform.authpw.focus() //--></script>", &$PageEndFmt)); PrintFmt($pagename, $AuthPromptFmt); exit; }
function BasicAuth($pagename, $level, $authprompt = true) { global $DefaultPasswords, $AllowPassword, $GroupAttributesFmt, $SessionAuthFmt, $HTMLStartFmt, $HTMLEndFmt; SDV($GroupAttributesFmt, '$Group/GroupAttributes'); SDV($AllowPassword, 'nopass'); $page = ReadPage($pagename); if (!$page) { return false; } $passwd = @$page["passwd{$level}"]; if ($passwd == "") { $grouppg = ReadPage(FmtPageName($GroupAttributesFmt, $pagename)); $passwd = @$grouppg["passwd{$level}"]; if ($passwd == '') { $passwd = @$DefaultPasswords[$level]; } if ($passwd == '') { $passwd = @$page["passwdread"]; } if ($passwd == '') { $passwd = @$grouppg["passwdread"]; } if ($passwd == '') { $passwd = @$DefaultPasswords['read']; } } if ($passwd == '') { return $page; } if (crypt($AllowPassword, $passwd) == $passwd) { return $page; } @session_start(); if (@$_POST['authpw']) { @$_SESSION['authpw'][$_POST['authpw']]++; } $authpw = array_keys((array) @$_SESSION['authpw']); foreach (array_merge((array) $DefaultPasswords['admin'], (array) $passwd) as $pwchal) { foreach ($authpw as $pwresp) { if (@crypt($pwresp, $pwchal) == $pwchal) { return $page; } } } if (!$authprompt) { return false; } SDV($SessionAuthFmt, array(&$HTMLStartFmt, "<p><b>Password required</b></p>\n <form name='authform' action='{$_SERVER['REQUEST_URI']}' method='post'>\n Password: <input tabindex='1' type='password' name='authpw' value='' />\n <input type='submit' value='OK' /></form>", &$HTMLEndFmt)); PrintFmt($pagename, $SessionAuthFmt); exit; }
function HandleSearchA($pagename, $level = 'read') { global $PageSearchForm, $FmtV, $HandleSearchFmt, $PageStartFmt, $PageEndFmt; SDV($HandleSearchFmt, array(&$PageStartFmt, '$PageText', &$PageEndFmt)); SDV($PageSearchForm, '$[Site.Search]'); $form = ReadPage(FmtPageName($PageSearchForm, $pagename), READPAGE_CURRENT); $text = @$form['text']; if (!$text) { $text = '(:searchresults:)'; } $FmtV['$PageText'] = MarkupToHTML($pagename, $text); PrintFmt($pagename, $HandleSearchFmt); }
function PmWikiAuth($pagename, $level, $authprompt=true, $since=0) { global $DefaultPasswords, $GroupAttributesFmt, $AllowPassword, $AuthCascade, $FmtV, $AuthPromptFmt, $PageStartFmt, $PageEndFmt, $AuthId, $AuthList, $NoHTMLCache; static $acache; SDV($GroupAttributesFmt,'$Group/GroupAttributes'); SDV($AllowPassword,'nopass'); $page = ReadPage($pagename, $since); if (!$page) { return false; } if (!isset($acache)) SessionAuth($pagename, (@$_POST['authpw']) ? array('authpw' => array($_POST['authpw'] => 1)) : ''); if (@$AuthId) { $AuthList["id:$AuthId"] = 1; $AuthList["id:-$AuthId"] = -1; $AuthList["id:*"] = 1; } $gn = FmtPageName($GroupAttributesFmt, $pagename); if (!isset($acache[$gn])) { $gp = ReadPage($gn, READPAGE_CURRENT); foreach($DefaultPasswords as $k => $v) { $x = array(2, array(), ''); $acache['@site'][$k] = IsAuthorized($v, 'site', $x); $AuthList["@_site_$k"] = $acache['@site'][$k][0] ? 1 : 0; $acache[$gn][$k] = IsAuthorized($gp["passwd$k"], 'group', $acache['@site'][$k]); } } foreach($DefaultPasswords as $k => $v) list($page['=auth'][$k], $page['=passwd'][$k], $page['=pwsource'][$k]) = IsAuthorized($page["passwd$k"], 'page', $acache[$gn][$k]); foreach($AuthCascade as $k => $t) { if ($page['=auth'][$k]+0 == 2) { $page['=auth'][$k] = $page['=auth'][$t]; if ($page['=passwd'][$k] = $page['=passwd'][$t]) # assign $page['=pwsource'][$k] = "cascade:$t"; } } if (@$page['=auth']['admin']) foreach($page['=auth'] as $lv=>$a) @$page['=auth'][$lv] = 3; if (@$page['=passwd']['read']) $NoHTMLCache |= 2; if ($level=='ALWAYS' || @$page['=auth'][$level]) return $page; if (!$authprompt) return false; $GLOBALS['AuthNeeded'] = (@$_POST['authpw']) ? $page['=pwsource'][$level] . ' ' . $level : ''; PCache($pagename, $page); $postvars = ''; foreach($_POST as $k=>$v) { if ($k == 'authpw' || $k == 'authid') continue; $v = str_replace('$', '$', htmlspecialchars(stripmagic($v), ENT_COMPAT)); $postvars .= "<input type='hidden' name='$k' value=\"$v\" />\n"; } $FmtV['$PostVars'] = $postvars; SDV($AuthPromptFmt,array(&$PageStartFmt, "<p><b>$[Password required]</b></p> <form name='authform' action='{$_SERVER['REQUEST_URI']}' method='post'> $[Password]: <input tabindex='1' type='password' name='authpw' value='' /> <input type='submit' value='OK' />\$PostVars</form> <script language='javascript' type='text/javascript'><!-- document.authform.authpw.focus() //--></script>", &$PageEndFmt)); PrintFmt($pagename,$AuthPromptFmt); exit; }
function PrintAttrForm($pagename) { global $PageAttributes; echo FmtPageName("<form action='\$PageUrl' method='post'>\n <input type='hidden' name='action' value='postattr' />\n <input type='hidden' name='pagename' value='\$FullName' />\n <table>", $pagename); $page = ReadPage($pagename); foreach ($PageAttributes as $attr => $p) { $value = substr($attr, 0, 6) == 'passwd' ? '' : $page[$attr]; $prompt = FmtPageName($p, $pagename); echo "<tr><td>{$prompt}</td>\n <td><input type='text' name='{$attr}' value='{$value}' /></td></tr>"; } echo "</table><input type='submit' /></form>"; }
function FmtPageList($fmt, $pagename, $opt) { global $GroupPattern, $SearchPatterns, $FmtV, $FPLFunctions; $opt = array_merge(@$_REQUEST, $opt); if (!$opt['q']) { $opt['q'] = stripmagic(@$_REQUEST['q']); } if (!$opt['q']) { return; } $terms = preg_split('/((?<!\\S)[-+]?[\'"].*?[\'"](?!\\S)|\\S+)/', $opt['q'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if (preg_match("!^({$GroupPattern}(\\|{$GroupPattern})*)?/!i", @$terms[0], $match)) { $opt['group'] = @$match[1]; $terms[0] = str_replace(@$match[1] . '/', '', $terms[0]); } $excl = array(); $incl = array(); foreach ($terms as $t) { if (trim($t) == '') { continue; } if (preg_match('/^([^\'":=]*)[:=]([\'"]?)(.*?)\\2$/', $t, $match)) { $opt[$match[1]] = $match[3]; continue; } preg_match('/^([-+]?)([\'"]?)(.+?)\\2$/', $t, $match); if ($match[1] == '-') { $excl[] = $match[3]; } else { $incl[] = $match[3]; } } $show = isset($opt['list']) ? $opt['list'] : 'default'; $pats = (array) @$SearchPatterns[$show]; if (@$opt['group']) { array_unshift($pats, "/^({$opt['group']})\\./i"); } if (@$opt['trail']) { $t = ReadTrail($pagename, $opt['trail']); foreach ($t as $pagefile) { $pagelist[] = $pagefile['pagename']; } } else { $pagelist = ListPages($pats); } $matches = array(); $searchterms = count($excl) + count($incl); foreach ($pagelist as $pagefile) { $page = ReadPage($pagefile); Lock(0); if (!$page) { continue; } if ($searchterms) { $text = $pagefile . "\n" . @$page['text'] . "\n" . @$page['targets']; foreach ($excl as $t) { if (stristr($text, $t)) { continue 2; } } foreach ($incl as $t) { if (!stristr($text, $t)) { continue 2; } } } $matches[] = array('pagename' => $pagefile, 'size' => strlen(@$page['text']), 'author' => @$page['author'], 'time' => $page['time']); } sort($matches); $FmtV['$MatchCount'] = count($matches); $FmtV['$MatchSearched'] = count($pagelist); $FmtV['$Needle'] = $opt['q']; $fmtfn = @$FPLFunctions[$opt['fmt']]; if (!function_exists($fmtfn)) { $fmtfn = 'FPLByGroup'; } $FmtV['$MatchList'] = $fmtfn($pagename, $matches, $opt); return FmtPageName($fmt, $pagename); }
function NotifyUpdate($pagename, $dir = '') { global $NotifyList, $NotifyListPageFmt, $NotifyFile, $IsPagePosted, $FmtV, $NotifyTimeFmt, $NotifyItemFmt, $SearchPatterns, $NotifySquelch, $NotifyDelay, $Now, $NotifySubjectFmt, $NotifyBodyFmt, $NotifyHeaders, $NotifyParameters; $abort = ignore_user_abort(true); if ($dir) { flush(); chdir($dir); } $GLOBALS['EnableRedirect'] = 0; ## Read in the current notify configuration $pn = FmtPageName($NotifyListPageFmt, $pagename); $npage = ReadPage($pn, READPAGE_CURRENT); preg_match_all('/^[\\s*:#->]*(notify[:=].*)/m', $npage['text'], $nlist); $nlist = array_merge((array) @$NotifyList, (array) @$nlist[1]); if (!$nlist) { return; } ## make sure other processes are locked out Lock(2); ## let's load the current .notifylist table $nfile = FmtPageName($NotifyFile, $pagename); $nfp = @fopen($nfile, 'r'); if ($nfp) { ## get our current squelch and delay timestamps clearstatcache(); $sz = filesize($nfile); list($nextevent, $firstpost) = explode(' ', rtrim(fgets($nfp, $sz))); ## restore our notify array $notify = unserialize(fgets($nfp, $sz)); fclose($nfp); } if (!is_array($notify)) { $notify = array(); } ## if this is for a newly posted page, get its information if ($IsPagePosted) { $page = ReadPage($pagename, READPAGE_CURRENT); $FmtV['$PostTime'] = strftime($NotifyTimeFmt, $Now); $item = urlencode(FmtPageName($NotifyItemFmt, $pagename)); if ($firstpost < 1) { $firstpost = $Now; } } foreach ($nlist as $n) { $opt = ParseArgs($n); $mailto = preg_split('/[\\s,]+/', $opt['notify']); if (!$mailto) { continue; } if ($opt['squelch']) { foreach ($mailto as $m) { $squelch[$m] = $opt['squelch']; } } if (!$IsPagePosted) { continue; } if ($opt['link']) { $link = MakePageName($pagename, $opt['link']); if (!preg_match("/(^|,){$link}(,|\$)/i", $page['targets'])) { continue; } } $pats = @(array) $SearchPatterns[$opt['list']]; if ($opt['group']) { $pats[] = FixGlob($opt['group'], '$1$2.*'); } if ($opt['name']) { $pats[] = FixGlob($opt['name'], '$1*.$2'); } if ($pats && !MatchPageNames($pagename, $pats)) { continue; } if ($opt['trail']) { $trail = ReadTrail($pagename, $opt['trail']); for ($i = 0; $i < count($trail); $i++) { if ($trail[$i]['pagename'] == $pagename) { break; } } if ($i >= count($trail)) { continue; } } foreach ($mailto as $m) { $notify[$m][] = $item; } } $nnow = time(); if ($nnow < $firstpost + $NotifyDelay) { $nextevent = $firstpost + $NotifyDelay; } else { $firstpost = 0; $nextevent = $nnow + 86400; $mailto = array_keys($notify); $subject = FmtPageName($NotifySubjectFmt, $pagename); $body = FmtPageName($NotifyBodyFmt, $pagename); foreach ($mailto as $m) { $msquelch = @$notify[$m]['lastmail'] + (@$squelch[$m] ? $squelch[$m] : $NotifySquelch); if ($nnow < $msquelch) { if ($msquelch < $nextevent && count($notify[$m]) > 1) { $nextevent = $msquelch; } continue; } unset($notify[$m]['lastmail']); if (!$notify[$m]) { unset($notify[$m]); continue; } $mbody = str_replace('$NotifyItems', urldecode(implode("\n", $notify[$m])), $body); if ($NotifyParameters) { mail($m, $subject, $mbody, $NotifyHeaders, $NotifyParameters); } else { mail($m, $subject, $mbody, $NotifyHeaders); } $notify[$m] = array('lastmail' => $nnow); } } ## save the updated notify status $nfp = @fopen($nfile, "w"); if ($nfp) { fputs($nfp, "{$nextevent} {$firstpost}\n"); fputs($nfp, serialize($notify) . "\n"); fclose($nfp); } Lock(0); return true; }
function PrintRefCount($pagename) { global $GroupPattern, $NamePattern, $PageRefCountFmt, $RefCountTimeFmt; $pagelist = ListPages(); $grouplist = array(); foreach ($pagelist as $pname) { if (!preg_match("/^({$GroupPattern})[\\/.]({$NamePattern})\$/", $pname, $m)) { continue; } $grouplist[$m[1]] = $m[1]; } asort($grouplist); $grouplist = array_merge(array('all' => 'all groups'), $grouplist); $wlist = array('all', 'missing', 'existing', 'orphaned'); $tlist = isset($_REQUEST['tlist']) ? $_REQUEST['tlist'] : array('all'); $flist = isset($_REQUEST['flist']) ? $_REQUEST['flist'] : array('all'); $whichrefs = @$_REQUEST['whichrefs']; $showrefs = @$_REQUEST['showrefs']; $submit = @$_REQUEST['submit']; echo FmtPageName($PageRefCountFmt, $pagename); echo "<form method='post'><input type='hidden' action='refcount'>\n <table cellspacing='10'><tr><td valign='top'>Show\n <br><select name='whichrefs'>"; foreach ($wlist as $w) { echo "<option ", $whichrefs == $w ? 'selected' : '', " value='{$w}'>{$w}\n"; } echo "</select></td><td valign='top'> page names in group<br>\n <select name='tlist[]' multiple size='4'>"; foreach ($grouplist as $g => $t) { echo "<option ", in_array($g, $tlist) ? 'selected' : '', " value='{$g}'>{$t}\n"; } echo "</select></td><td valign='top'> referenced from pages in<br>\n <select name='flist[]' multiple size='4'>"; foreach ($grouplist as $g => $t) { echo "<option ", in_array($g, $flist) ? 'selected' : '', " value='{$g}'>{$t}\n"; } echo "</select></td></tr></table>\n <p><input type='checkbox' name='showrefs' value='checked' {$showrefs}>\n Display referencing pages\n <p><input type='submit' name='submit' value='Search'></form><p><hr>"; if ($submit) { foreach ($pagelist as $pname) { $ref = array(); $page = ReadPage($pname, READPAGE_CURRENT); if (!$page) { continue; } $tref[$pname]['time'] = $page['time']; if (!in_array('all', $flist) && !in_array(FmtPageName('$Group', $pname), $flist)) { continue; } $rc = preg_match('/RecentChanges$/', $pname); foreach (explode(',', @$page['targets']) as $r) { if ($r == '') { continue; } if ($rc) { @$tref[$r]['rc']++; } else { @$tref[$r]['page']++; @$pref[$r][$pname]++; } } } uasort($tref, 'RefCountCmp'); echo "<table >\n <tr><th></th><th colspan='2'>Referring pages</th></tr>\n <tr><th>Name / Time</th><th>All</th><th>R.C.</th></tr>"; reset($tref); foreach ($tref as $p => $c) { if (!in_array('all', $tlist) && !in_array(FmtPageName('$Group', $p), $tlist)) { continue; } if ($whichrefs == 'missing' && PageExists($p)) { continue; } elseif ($whichrefs == 'existing' && !PageExists($p)) { continue; } elseif ($whichrefs == 'orphaned' && (@$tref[$p]['page'] > 0 || !PageExists($p))) { continue; } echo "<tr><td valign='top'>", LinkPage($pagename, '', $p, '', $p); if (@$tref[$p]['time']) { echo strftime($RefCountTimeFmt, $tref[$p]['time']); } if ($showrefs && is_array(@$pref[$p])) { foreach ($pref[$p] as $pr => $pc) { echo "<dd>", LinkPage($pagename, '', $pr, '', $pr); } } echo "</td>"; echo "<td align='center' valign='top'>", @$tref[$p]['page'] + 0, "</td>"; echo "<td align='center' valign='top'>", @$tref[$p]['rc'] + 0, "</td>"; echo "</tr>"; } echo "</table>"; } }
function AuthUserId($pagename, $id, $pw = NULL) { global $AuthUser, $AuthUserPageFmt, $AuthUserFunctions, $AuthId, $MessagesFmt; foreach ((array) $AuthUser as $k => $v) { $auth[$k] = (array) $v; } $authid = ''; # load information from Site.AuthUser (or page in $AuthUserPageFmt) SDV($AuthUserPageFmt, '$SiteGroup.AuthUser'); SDVA($AuthUserFunctions, array('htpasswd' => 'AuthUserHtPasswd', 'ldap' => 'AuthUserLDAP', $id => 'AuthUserConfig')); $pn = FmtPageName($AuthUserPageFmt, $pagename); $apage = ReadPage($pn, READPAGE_CURRENT); if ($apage && preg_match_all("/^\\s*([@\\w][^\\s:]*):(.*)/m", $apage['text'], $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { if (!preg_match_all('/\\bldaps?:\\S+|[^\\s,]+/', $m[2], $v)) { continue; } if ($m[1][0] == '@') { foreach ($v[0] as $g) { $auth[$g][] = $m[1]; } } else { $auth[$m[1]] = array_merge((array) @$auth[$m[1]], $v[0]); } } } if (is_null($pw)) { $authid = $id; } else { foreach ($AuthUserFunctions as $k => $fn) { if ($auth[$k] && $fn($pagename, $id, $pw, $auth[$k])) { $authid = $id; break; } } } if (!$authid) { $GLOBALS['InvalidLogin'] = 1; return; } if (!isset($AuthId)) { $AuthId = $authid; } $authlist["id:{$authid}"] = 1; $authlist["id:-{$authid}"] = -1; foreach (preg_grep('/^@/', (array) @$auth[$authid]) as $g) { $authlist[$g] = 1; } foreach (preg_grep('/^@/', (array) @$auth['*']) as $g) { $authlist[$g] = 1; } foreach (preg_grep('/^@/', array_keys($auth)) as $g) { if (in_array($authid, $auth[$g])) { $authlist[$g] = 1; } } if ($auth['htgroup']) { foreach (AuthUserHtGroup($pagename, $id, $pw, $auth['htgroup']) as $g) { $authlist["@{$g}"] = 1; } } SessionAuth($pagename, array('authid' => $authid, 'authlist' => $authlist)); }
function BlocklistDownload($pagename, $dir = '') { global $BlocklistDownloadFmt, $BlocklistDownload, $FmtV; if ($dir) { flush(); chdir($dir); } SDV($BlocklistDownloadFmt, " [@ ## blocklist-note: NOTE: This page is automatically generated by blocklist.php ## blocklist-note: NOTE: Any edits to this page may be lost! ## blocklist-url: \$BlocklistDownloadUrl ## blocklist-when: \$CurrentTimeISO # blocklist-format: \$BlocklistFormat \$BlocklistData @] "); ## get the existing blocklist page $bd = &$BlocklistDownload[$pagename]; $page = ReadPage($pagename, READPAGE_CURRENT); ## try to retrieve the remote data $blocklistdata = @file($bd['url']); ## if we didn't get it, and we don't already have text, save a ## note in the page so we know what happened if (!$blocklistdata && !@$page['text']) { $auf = ini_get('allow_url_fopen'); $blocklistdata = "#### Unable to download blocklist (allow_url_fopen=$auf)"; } ## if we have some new text to save, let's format it and save it if ($blocklistdata) { $blocklistdata = implode('', (array)$blocklistdata); $blocklistdata = preg_replace('/^##blocklist.*/m', '', $blocklistdata); $FmtV['$BlocklistData'] = $blocklistdata; $FmtV['$BlocklistDownloadUrl'] = $bd['url']; $FmtV['$BlocklistFormat'] = $bd['format']; $page['text'] = FmtPageName($BlocklistDownloadFmt, $pagename); SDV($page['passwdread'], '@lock'); } ## save our updated(?) blocklist page WritePage($pagename, $page); }
function PageIndexUpdate($pagelist = NULL, $dir = '') { global $EnableReadOnly, $PageIndexUpdateList, $PageIndexFile, $PageIndexTime, $Now; if (IsEnabled($EnableReadOnly, 0)) return; $abort = ignore_user_abort(true); if ($dir) { flush(); chdir($dir); } if (is_null($pagelist)) { $pagelist = (array)$PageIndexUpdateList; $PageIndexUpdateList = array(); } if (!$pagelist || !$PageIndexFile) return; SDV($PageIndexTime, 10); $c = count($pagelist); $updatecount = 0; StopWatch("PageIndexUpdate begin ($c pages to update)"); $pagelist = (array)$pagelist; $timeout = time() + $PageIndexTime; $cmpfn = create_function('$a,$b', 'return strlen($b)-strlen($a);'); Lock(2); $ofp = fopen("$PageIndexFile,new", 'w'); foreach($pagelist as $pn) { if (@$updated[$pn]) continue; @$updated[$pn]++; if (time() > $timeout) continue; $page = ReadPage($pn, READPAGE_CURRENT); if ($page) { $targets = str_replace(',', ' ', @$page['targets']); $terms = PageIndexTerms(array(@$page['text'], $targets, $pn)); usort($terms, $cmpfn); $x = ''; foreach($terms as $t) { if (strpos($x, $t) === false) $x .= " $t"; } fputs($ofp, "$pn:$Now: $targets :$x\n"); } $updatecount++; } $ifp = @fopen($PageIndexFile, 'r'); if ($ifp) { while (!feof($ifp)) { $line = fgets($ifp, 4096); while (substr($line, -1, 1) != "\n" && !feof($ifp)) $line .= fgets($ifp, 4096); $i = strpos($line, ':'); if ($i === false) continue; $n = substr($line, 0, $i); if (@$updated[$n]) continue; fputs($ofp, $line); } fclose($ifp); } fclose($ofp); if (file_exists($PageIndexFile)) unlink($PageIndexFile); rename("$PageIndexFile,new", $PageIndexFile); fixperms($PageIndexFile); StopWatch("PageIndexUpdate end ($updatecount updated)"); ignore_user_abort($abort); }
function MakePageList($pagename, $opt) { global $MakePageListOpt, $SearchPatterns, $EnablePageListProtect, $PCache, $FmtV; StopWatch('MakePageList begin'); SDVA($MakePageListOpt, array('list' => 'default')); $opt = array_merge($MakePageListOpt, $opt); $readf = $opt['readf']; # we have to read the page if order= is anything but name $order = $opt['order']; $readf |= $order && $order != 'name' && $order != '-name'; $pats = (array) $SearchPatterns[$opt['list']]; if ($opt['group']) { array_unshift($pats, "/^({$opt['group']})\\./i"); } # inclp/exclp contain words to be included/excluded. $inclp = array(); $exclp = array(); foreach ((array) @$opt[''] as $i) { $inclp[] = '/' . preg_quote($i, '/') . '/i'; } foreach ((array) @$opt['+'] as $i) { $inclp[] = '/' . preg_quote($i, '/') . '/i'; } foreach ((array) @$opt['-'] as $i) { $exclp[] = '!' . preg_quote($i, '!') . '/i'; } $searchterms = count($inclp) + count($exclp); $readf += $searchterms; # forced read if incl/excl # link= (backlinks) if (@$opt['link']) { $linkpat = "/,{$opt['link']},/"; # find in target= attribute $readf = 1; # forced read } if (@$opt['trail']) { $trail = ReadTrail($pagename, $opt['trail']); foreach ($trail as $tstop) { $pn = $tstop['pagename']; $list[] = $pn; $tstop['parentnames'] = array(); PCache($pn, $tstop); } foreach ($trail as $tstop) { $PCache[$tstop['pagename']]['parentnames'][] = $trail[$tstop['parent']]['pagename']; } } else { $list = ListPages($pats); } if (IsEnabled($EnablePageListProtect, 0)) { $readf = 1000; } $matches = array(); $FmtV['$MatchSearched'] = count($list); foreach ((array) $list as $pn) { if ($readf) { $page = $readf == 1000 ? RetrieveAuthPage($pn, 'read', false, READPAGE_CURRENT) : ReadPage($pn, READPAGE_CURRENT); if (!$page) { continue; } if ($linkpat && !preg_match($linkpat, ",{$page['targets']},")) { continue; } if ($searchterms) { $text = $pn . "\n" . @$page['targets'] . "\n" . @$page['text']; foreach ($inclp as $i) { if (!preg_match($i, $text)) { continue 2; } } foreach ($exclp as $i) { if (preg_match($i, $text)) { continue 2; } } } $page['size'] = strlen(@$page['text']); } else { $page = array(); } $page['pagename'] = $page['name'] = $pn; PCache($pn, $page); $matches[] =& $PCache[$pn]; } SortPageList($matches, $order); StopWatch('MakePageList end'); return $matches; }
function PrintDiff($pagename) { global $DiffShow, $DiffStartFmt, $TimeFmt, $DiffDelFmt, $DiffAddFmt, $DiffEndDelAddFmt, $DiffEndFmt, $DiffRestoreFmt, $FmtV, $LinkFunctions; $page = ReadPage($pagename); if (!$page) { return; } krsort($page); reset($page); $lf = $LinkFunctions; $LinkFunctions['http:'] = 'LinkSuppress'; $LinkFunctions['https:'] = 'LinkSuppress'; foreach ($page as $k => $v) { if (!preg_match("/^diff:(\\d+):(\\d+):?([^:]*)/", $k, $match)) { continue; } $diffclass = $match[3]; if ($diffclass == 'minor' && $DiffShow['minor'] != 'y') { continue; } $diffgmt = $match[1]; $FmtV['$DiffTime'] = strftime($TimeFmt, $diffgmt); $diffauthor = @$page["author:{$diffgmt}"]; if (!$diffauthor) { @($diffauthor = $page["host:{$diffgmt}"]); } if (!$diffauthor) { $diffauthor = "unknown"; } $FmtV['$DiffChangeSum'] = htmlspecialchars(@$page["csum:{$diffgmt}"]); $FmtV['$DiffHost'] = @$page["host:{$diffgmt}"]; $FmtV['$DiffAuthor'] = $diffauthor; $FmtV['$DiffId'] = $k; echo FmtPageName($DiffStartFmt, $pagename); $difflines = explode("\n", $v . "\n"); $in = array(); $out = array(); $dtype = ''; 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; echo FmtPageName($txt, $pagename); if ($DiffShow['source'] == 'y') { echo "<div class='diffmarkup'>", str_replace("\n", "<br />", htmlspecialchars(join("\n", $in))), "</div>"; } else { echo MarkupToHTML($pagename, preg_replace('/\\(:(.*?):\\)/', '[@$1@]', join("\n", $in))); } } if ($match[4] == 'd' || $match[4] == 'c') { $txt = str_replace('line', $lines, $DiffAddFmt[$match[4]]); $FmtV['$DiffLines'] = $count; echo FmtPageName($txt, $pagename); if ($DiffShow['source'] == 'y') { echo "<div class='diffmarkup'>", str_replace("\n", "<br />", htmlspecialchars(join("\n", $out))), "</div>"; } else { echo MarkupToHTML($pagename, preg_replace('/(\\(:.*?:\\))/', '[@$1@]', join("\n", $out))); } } echo FmtPageName($DiffEndDelAddFmt, $pagename); } $in = array(); $out = array(); $dtype = $d; } echo FmtPageName($DiffEndFmt, $pagename); echo FmtPageName($DiffRestoreFmt, $pagename); } $LinkFunctions = $lf; }
function LinkIndexUpdate($pagelist) { global $LinkIndexFile, $PCache, $LinkIndexTime; SDV($LinkIndexTime, 10); if (!$pagelist || !$LinkIndexFile) { return; } StopWatch('LinkIndexUpdate begin'); $pagelist = (array) $pagelist; Lock(2); $ofp = fopen("{$LinkIndexFile},new", 'w'); $timeout = time() + $LinkIndexTime; foreach ($pagelist as $n) { if (time() > $timeout) { break; } if (isset($PCache[$n]['targets'])) { $targets = $PCache[$n]['targets']; } else { $page = ReadPage($n, READPAGE_CURRENT); if (!$page) { continue; } $targets = @$page['targets']; } fputs($ofp, "{$n}={$targets}\n"); } $ifp = @fopen($LinkIndexFile, 'r'); if ($ifp) { while (!feof($ifp)) { $line = fgets($ifp, 4096); while (substr($line, -1, 1) != "\n" && !feof($ifp)) { $line .= fgets($ifp, 4096); } $i = strpos($line, '='); if ($i === false) { continue; } $n = substr($line, 0, $i); if (in_array($n, $pagelist)) { continue; } fputs($ofp, $line); } fclose($ifp); } fclose($ofp); if (file_exists($LinkIndexFile)) { unlink($LinkIndexFile); } rename("{$LinkIndexFile},new", $LinkIndexFile); fixperms($LinkIndexFile); StopWatch('LinkIndexUpdate end'); }
function PmWikiAuth($pagename, $level, $authprompt = true, $since = 0) { global $DefaultPasswords, $AllowPassword, $GroupAttributesFmt, $FmtV, $AuthPromptFmt, $PageStartFmt, $PageEndFmt, $AuthId; static $grouppasswd, $authpw; SDV($GroupAttributesFmt, '$Group/GroupAttributes'); SDV($AllowPassword, 'nopass'); $page = ReadPage($pagename, $since); if (!$page) { return false; } $groupattr = FmtPageName($GroupAttributesFmt, $pagename); if (!isset($grouppasswd[$groupattr])) { $grouppasswd[$groupattr] = array(); $gp = ReadPage($groupattr, READPAGE_CURRENT); foreach ($DefaultPasswords as $k => $v) { if (isset($gp["passwd{$k}"])) { $grouppasswd[$groupattr][$k] = explode(' ', $gp["passwd{$k}"]); } } } foreach ($DefaultPasswords as $k => $v) { if (isset($page["passwd{$k}"])) { $passwd[$k] = explode(' ', $page["passwd{$k}"]); $page['=pwsource'][$k] = 'page'; } else { if (isset($grouppasswd[$groupattr][$k])) { $passwd[$k] = $grouppasswd[$groupattr][$k]; $page['=pwsource'][$k] = 'group'; } else { $passwd[$k] = $v; if ($v) { $page['=pwsource'][$k] = 'site'; } } } } $page['=passwd'] = $passwd; if (!isset($authpw)) { $sid = session_id(); @session_start(); if (@$_POST['authpw']) { @$_SESSION['authpw'][$_POST['authpw']]++; } $authpw = array_keys((array) @$_SESSION['authpw']); if (!isset($AuthId)) { $AuthId = @$_SESSION['authid']; } if (!$sid) { session_write_close(); } } foreach ($passwd as $lv => $a) { if (!$a) { $page['=auth'][$lv]++; continue; } foreach ((array) $a as $pwchal) { if ($AuthId && strncmp($pwchal, 'id:', 3) == 0) { $idlist = explode(',', substr($pwchal, 3)); foreach ($idlist as $id) { if ($id == $AuthId || $id == '*') { $page['=auth'][$lv]++; continue 3; } if ($id == "-{$AuthId}") { continue 3; } } } if ($pwchal == '' || crypt($AllowPassword, $pwchal) == $pwchal) { $page['=auth'][$lv]++; continue 2; } foreach ($authpw as $pwresp) { if (crypt($pwresp, $pwchal) == $pwchal) { $page['=auth'][$lv]++; continue 3; } } } } if ($page['=auth']['admin']) { foreach ($passwd as $lv => $a) { $page['=auth'][$lv]++; } } if ($page['=auth'][$level]) { return $page; } if (!$authprompt) { return false; } PCache($pagename, $page); $postvars = ''; foreach ($_POST as $k => $v) { if ($k == 'authpw') { continue; } $v = str_replace('$', '$', htmlspecialchars(stripmagic($v), ENT_COMPAT)); $postvars .= "<input type='hidden' name='{$k}' value=\"{$v}\" />\n"; } $FmtV['$PostVars'] = $postvars; SDV($AuthPromptFmt, array(&$PageStartFmt, "<p><b>Password required</b></p>\n <form name='authform' action='{$_SERVER['REQUEST_URI']}' method='post'>\n Password: <input tabindex='1' type='password' name='authpw' value='' />\n <input type='submit' value='OK' />\$PostVars</form>\n <script language='javascript'><!--\n document.authform.authpw.focus() //--></script>", &$PageEndFmt)); PrintFmt($pagename, $AuthPromptFmt); exit; }