function ewiki_powersearch($q, $where = 'content') { $q = ewiki_lowercase(preg_replace('/\\s*[\\000-\\040]+\\s*/', ' ', $q)); $found = array(); $scored = array(); #-- initial scan foreach (explode(" ", $q) as $search) { if (empty($search)) { continue; } $result = ewiki_db::SEARCH($where, $search); while ($row = $result->get()) { if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT) { $id = $row["id"]; $content = strtolower($row[$where]); unset($row); #-- have a closer look $len1 = strlen($content) + 1; if (!isset($scored[$id])) { $scored[$id] = 1; } $scored[$id] += 800 * (strlen($search) / $len1); $scored[$id] += 65 * (count(explode($search, $content)) - 2); $p = -1; while (($p = strpos($content, $search, $p + 1)) !== false) { $scored[$id] += 80 * (1 - $p / $len1); } } #if-TXT } } #-- output results arsort($scored); $o = "<ol>\n"; $n = 0; foreach ($scored as $id => $score) { #-- refetch page for top 10 entries (still cached by OS or DB) $row = $n < 10 ? ewiki_db::GET($id) : NULL; #-- check access rights in protected mode if (EWIKI_PROTECTED_MODE && !ewiki_auth($id, $row, "view", $ring = false, $force = 0)) { if (EWIKI_PROTECTED_MODE_HIDING) { continue; } else { $row["content"] = ewiki_t("FORBIDDEN"); } } $o .= "<li>\n"; $o .= '<div class="search-result ' . ($oe ^= 1 ? "odd" : "even") . '">' . '<a href="' . ewiki_script("", $id) . '">' . $id . "</a> " . "\n"; #-- top 10 results are printed more verbosely if ($n++ < 10) { preg_match_all('/([_-\\w]+)/', $row["content"], $uu); $text = htmlentities(substr(implode(" ", $uu[1]), 0, 200)); $o .= "<br />\n<small>{$text}\n" . "<br />" . strftime(ewiki_t("LASTCHANGED"), $row["lastmodified"]) . "<br /><br /></small>\n"; } $o .= "</div>\n"; $o .= "</li>\n"; } $o .= "</ol>\n"; return $o; }
function ewiki_tcn_find_variants($id) { list($base_id, $id_lang) = ewiki_tcn_which_variant($id); $variants = array("{$id}" => "{$id_lang}"); $result = ewiki_db::SEARCH("id", $base_id); while ($result && ($row = $result->get())) { if (substr($row["id"], 0, strlen($base_id)) == $base_id) { list($i, $l) = ewiki_tcn_which_variant($row["id"]); if ($i == $base_id) { $variants[$row["id"]] = $l; } } } return $variants; }
function ewiki_subpage_list($id, $postfix = "") { $_hiding = EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING; $result = ewiki_db::SEARCH("id", $id . $postfix); while ($row = $result->get()) { #-- retrieve and check rights if running in protected mode if ($_hiding) { if (!ewiki_auth($row["id"], $uu, 'view', $ring = false, $force = 0)) { continue; } } $pages[$row["id"]] = ""; } return $pages; }
function calendar_exists($always = false) { if ($always) { return true; } $result = ($id = $GLOBALS["ewiki_id"]) && ($Qresult = ewiki_db::SEARCH("id", $id . CALENDAR_NAME_SEP)) && $Qresult->count(); if (!$result || $id == EWIKI_PAGE_CALENDAR || $id == EWIKI_PAGE_YEAR_CALENDAR || !empty($_REQUEST["year"])) { return false; } while ($row = $Qresult->get(0, 0x1037)) { if (ewiki_isCalendarId($row["id"])) { return true; } } return false; }
function ewiki_page_like($id, $data, $action) { preg_match_all("/([" . EWIKI_CHARS_U . "][" . EWIKI_CHARS_L . "]+)/", $id, $words); $pages = array(); foreach ($words[1] as $find) { $result = ewiki_db::SEARCH("id", $find); while ($row = $result->get()) { if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && !ewiki_auth($row["id"], $row, "view")) { continue; } $pages[$row["id"]] = ""; } } $o = ewiki_make_title($id, ewiki_t(strtoupper($action) . "_TITLE"), 3); $o .= ewiki_list_pages($pages, 0); return $o; }
function ewiki_view_append_threads($id, $data, $action) { if (isThread($id)) { return ""; } $result = ewiki_db::SEARCH("id", $id . EWIKI_THREAD_SEPARATOR); while ($row = $result->get()) { if (!isPost($row["id"])) { $pages[$row["id"]] = ""; } } if (0 != count($pages)) { $o = "<div class='wiki_threads'><small>" . ewiki_t('THREADS') . ":</small><br />"; $o .= ewiki_list_pages($pages) . "</div>\n"; } $o .= "<form action='" . ewiki_script('addthread', $id) . "' method='POST'>" . ewiki_t("NEWTHREAD") . ": <input type='text' name='threadname'>" . "<input type='submit' value='Add Thread'>" . "</form>"; return "<div class='wiki aview threads'>" . $o . '</div>'; }
function ewiki_view_append_attachments($id, $data, $action) { $o = '<hr><h4><a href="' . ewiki_script(EWIKI_ACTION_ATTACHMENTS, $id) . '">' . ewiki_t("ATTACHMENTS") . '</a></h4>'; $scan = 's:7:"section";' . serialize($id); $result = ewiki_db::SEARCH("meta", $scan); $ord = array(); while ($row = $result->get()) { $ord[$row["id"]] = $row["created"]; } arsort($ord); foreach ($ord as $id => $uu) { $row = ewiki_db::GET($id); if (EWIKI_PROTECTED_MODE && EWIKI_PROTECTED_MODE_HIDING && !ewiki_auth($row["id"], $row, "view")) { continue; } $o .= ewiki_entry_downloads($row, "*"); } return $o; }
function ewiki_linking_findany($moniker, $page) { global $ewiki_config; #-- lists $page_i = strtolower($page); $search = array_merge($ewiki_config["instant"], $ewiki_config["interwiki"]); foreach ($search as $pn => $url) { if (strtolower($pn) == $page_i) { return $url; } } #-- find a page $result = ewiki_db::SEARCH("id", $page_i); while ($row = $result->get()) { if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT) { return ewiki_script("", $row["id"]); } } #-- Google saves the day! return "http://www.google.com/search?q=" . urlencode($page); }
function ewiki_aedit_templates(&$html, $id, &$data, $action) { if (!$data["content"]) { #-- search template pages $list = array(); $result = ewiki_db::SEARCH("id", "Template"); while ($row = $result->get(0, 0x21, EWIKI_DB_F_TEXT)) { if (preg_match('/Template$/', $row["id"])) { $list[] = $row["id"]; } } #-- add list if ($list) { $o = '<form action="' . ewiki_script("", $id) . '" method="POST" entype="multipart/form-data">' . '<input type="hidden" name="id" value="' . $action . '/' . $id . '">' . '<input type="submit" value="load"> ' . '<select name="load_template">'; foreach ($list as $i) { $o .= '<option value="' . $i . '">' . $i . '</option>'; } $o .= '</select></form>'; #-- output $html = $o . $html; } } }
function ewiki_action_control_page($id, &$data, $action) { global $ewiki_ring, $ewiki_config, $ewiki_plugins; $a_flagnames = array("_TEXT", "_BINARY", "_DISABLED", "_HTML", "_READONLY", "_WRITEABLE", "_APPENDONLY", "_SYSTEM", "_PART", "_MINOR", "_HIDDEN", "_ARCHIVE", "_UU12", "_UU13", "_UU14", "_UU15", "_UU16", "_EXEC", "_UU18", "_UU19"); $o = ewiki_make_title($id, "control {$id}", 2); #-- admin requ. --------------------------------------------------------- if (!ewiki_auth($id, $data, $action, $ring = 0, "_FORCE_LOGIN=1") || !isset($ewiki_ring) || $ewiki_ring > 1) { if (is_array($data)) { $data = "You'll need to be admin. See ewiki_auth() and _PROTECTED_MODE in the README."; } $o .= $data; } elseif (@$_REQUEST["pgc_setflags"]) { #-- setted new flags $new_f = 0; foreach ($_REQUEST["sflag"] as $n => $b) { if ($b) { $new_f |= 1 << $n; } } #-- administrator may change all flags if ($ewiki_ring == 0) { $data["flags"] = $new_f; } else { $data["flags"] = $data["flags"] & ~EWIKI_DB_F_MODERATORFLAGS | $new_f & EWIKI_DB_F_MODERATORFLAGS; } $data["lastmodified"] = time(); $data["version"]++; if (ewiki_db::WRITE($data)) { $o .= "Page flags were updated correctly."; ewiki_log("page flags of '{$id}' were set to {$data['flags']}"); } else { $o .= "A database error occoured."; } } elseif (@$_REQUEST["pgc_rename"] && strlen($new_id = $_REQUEST["mv_to"])) { $old_id = $id; $report = ""; $preg_id = "/" . addcslashes($old_id, ".+*?|/\\()\$[]^#") . "/" . ($_REQUEST["mv_cr1"] ? "i" : ""); #-- check if new name does not already exist in database $exists = ewiki_db::GET($new_id); if ($exists || !empty($exists)) { return $o .= "Cannot overwrite an existing database entry."; } #-- copy from old name to new name $max_ver = $data["version"]; $data = array(); for ($v = 1; $v <= $max_ver; $v++) { $row = ewiki_db::GET($old_id, $v); $row["id"] = $new_id; $row["lastmodified"] = time(); $row["content"] = preg_replace($preg_id, $new_id, $row["content"]); ewiki_scan_wikiwords($row["content"], $links, "_STRIP_EMAIL=1"); $row["refs"] = "\n\n" . implode("\n", array_keys($links)) . "\n\n"; $row["author"] = ewiki_author("control/"); if (!ewiki_db::WRITE($row)) { $report .= "error while copying version {$v},<br />\n"; } } #-- proceed if previous actions error_free if (empty($report)) { #-- deleting old versions for ($v = 1; $v <= $max_ver; $v++) { ewiki_db::DELETE($old_id, $v); } #-- adjust links/references to old page name if ($_REQUEST["mv_cr0"]) { $result = ewiki_db::SEARCH("refs", $old_id); while ($result && ($row = $result->get())) { $row = ewiki_db::GET($row["id"]); if (preg_match($preg_id, $row["content"], $uu)) { $row["content"] = preg_replace($preg_id, $new_id, $row["content"]); $row["lastmodified"] = time(); $row["version"]++; ewiki_scan_wikiwords($row["content"], $links, "_STRIP_EMAIL=1"); $row["refs"] = "\n\n" . implode("\n", array_keys($links)) . "\n\n"; $row["author"] = ewiki_author("control/"); if (!ewiki_db::WRITE($row)) { $report .= "could not update references in " . $row['id'] . ",<br />\n"; } else { $report .= "updated references in " . $row['id'] . ",<br />\n"; } } } } $o .= "This page was correctly renamed from '{$old_id}' to '{$new_id}'.<br /><br />\n{$report}"; ewiki_log("page renamed from '{$old_id}' to '{$new_id}'", 2); } else { $o .= "Some problems occoured while processing your request, therefor the old page still exists:<br />\n" . $report; } } elseif (@$_REQUEST["pgc_setmeta"] && $ewiki_ring == 0 && ($set = explode("\n", $_REQUEST["pgc_meta"]))) { $new_meta = array(); foreach ($set as $line) { if (($line = trim($line)) && ($key = trim(strtok($line, ":"))) && ($value = trim(strtok("")))) { $new_meta[$key] = $value; } } $data["meta"] = $new_meta; $data["lastmodified"] = time(); $data["version"]++; if (ewiki_db::WRITE($data)) { $o .= "The {meta} field was updated."; } else { $o .= "A database error occoured."; } } elseif (@$_REQUEST["pgc_purge"] && $_REQUEST["pgc_purge1"]) { $loop = 3; do { $verZ = $data["version"]; while ($verZ > 0) { ewiki_db::DELETE($id, $verZ); $verZ--; } } while ($loop-- && ($data = ewiki_db::GET($id))); if (empty($data)) { $o .= "Page completely removed from database."; ewiki_log("page '{$id}' was deleted from db", 2); } else { $o .= "Page still here."; } } else { $o .= '<form action="' . ewiki_script("{$action}", $id) . '" method="POST" enctype="text/html">' . '<input type="hidden" name="id" value="' . "{$action}/{$id}" . '">'; #-- flags $o .= '<div class="flags">'; $o .= "<h4>page flags</h4>\n"; foreach ($a_flagnames as $n => $s) { $disabled = $ewiki_ring == 1 && !(1 << $n & EWIKI_DB_F_MODERATORFLAGS) ? ' disabled="disabled"' : ""; $checked = $data["flags"] & 1 << $n ? ' checked="checked"' : ""; $a[$n] = '<input type="checkbox" name="sflag[' . $n . ']" value="1"' . $checked . $disabled . '> ' . $s; } $o .= '<table border="0" class="list">' . "\n"; for ($n = 0; $n < count($a_flagnames); $n++) { $y = $n >> 2; $x = $n & 0x3; if ($x == 0) { $o .= "<tr>"; } $o .= "<td>" . $a[4 * $y + $x] . "</td>"; if ($x == 3) { $o .= "</tr>\n"; } } $o .= '</table>'; $o .= '<input type="submit" name="pgc_setflags" value="chmod">'; $o .= "\n<br /><br /><hr></div>\n"; #-- rename $o .= '<div class="rename">'; $o .= "<h4>rename page</h4>\n"; $o .= 'new page name: <input type="text" size="30" name="mv_to" value="' . htmlentities($id) . '">' . '<br />' . '<input type="checkbox" name="mv_cr0" value="1" checked> also try to change all references from other pages accordingly ' . '(<input type="checkbox" name="mv_cr1" value="1" checked> and act case-insensitive when doing so) '; $o .= '<br /><input type="submit" name="pgc_rename" value="mv">'; $o .= "\n<br /><br /><hr></div>\n"; #-- meta if (isset($ewiki_ring) && $ewiki_ring == 0) { $o .= '<div class="meta">'; $o .= "<h4>meta data</h4>\n"; $o .= '<textarea cols="40" rows="6" name="pgc_meta">'; if (($uu = @$data["meta"]) && is_array($uu)) { foreach ($uu as $key => $value) { if (is_array($value)) { $value = serialize($array); } $o .= htmlentities($key . ": " . trim($value)) . "\n"; } } $o .= "</textarea>\n"; $o .= '<br /><input type="submit" name="pgc_setmeta" value="set">'; $o .= "\n<br /><br /><hr></div>\n"; } #-- delete $o .= '<div class="delete">'; $o .= "<h4>delete page</h4>\n"; $o .= '<input type="checkbox" name="pgc_purge1" value="1"> I\'m sure'; $o .= '<br /><input type="submit" name="pgc_purge" value="rm">'; $o .= "\n<br /><br /><hr></div>\n"; $o .= '</form>'; } return $o; }
function ewiki_uservars_pages_search($varname, $value = NULL) { $result = ewiki_db::SEARCH("content", $varname); //Get a list of all pages containing our varname while ($row = $result->get()) { if (!preg_match("/" . EWIKI_USERVARS_PAGENAME_PREFIX . "(.*)/", $row["id"], $matches)) { continue; } //Page name is properly formed as a UserVars page name $username = $matches[1]; //echo("Checking $username for $varname"); if (!($data = ewiki_uservars_pages_retrieve($username))) { //echo(" rejected, no data retrieved"); continue; } //data was retrieved from the page. if (!isset($data[$varname])) { //echo(" rejected, variable not set $varname"); continue; } //The varname we are searching for was there if (isset($value) && $value != $data[$varname]) { //echo("rejected $value != ".$data[$varname]); continue; } //if we were looking for a value, it matches //echo("passed, setting $data[$varname] to $values[$varname]"); $values[$username] = $data[$varname]; } return $values; }
function posts_exist($always = false) { $id = $GLOBALS["ewiki_id"]; $result = $always || $id && ($result = ewiki_db::SEARCH("id", $id . '_POST')) && $result->count(); return $id && $id != EWIKI_PAGE_CALENDAR && $id != EWIKI_PAGE_YEAR_CALENDAR && !isPost($id) && empty($_REQUEST["year"]) && $result; }
if (@$_REQUEST["listall"]) { while ($row = $result->get()) { $selected[$row["id"]] = "listall <br>"; } } else { while ($page = $result->get()) { $id = $page["id"]; if (!strlen($id)) { continue; } $page = ewiki_db::GET($id); $flags = $page["flags"]; if (!strlen(trim($page["content"]))) { @($selected[$id] .= "EMPTY <br>"); } $res2 = ewiki_db::SEARCH("content", $id); if ($res2 && $res2->count()) { $check2 = 1; while ($row = $res2->get()) { $check = ewiki_db::GET($row["id"]); $check = strtolower($check["content"]); $check2 &= strpos($check, strtolower($id)) !== false; #echo "rc({$row['id']})==>($id): $check2 <br>"; } $check = $check2; } if (empty($check)) { @($selected[$id] .= "UNREFerenced <br>"); } if ($flags & EWIKI_DB_F_DISABLED) { @($selected[$id] .= "disabled_page <br>");
function ewiki_page_searchandreplace($id, $data, $action) { global $ewiki_ring, $ewiki_plugins; $o = ewiki_make_title($id, $id, 2); #-- admin requ. --------------------------------------------------------- if (!ewiki_auth($id, $data, $action, $ring = 1, "_FORCE_LOGIN=1") || !isset($ewiki_ring) || $ewiki_ring > 1) { if (is_array($data)) { $data = "You'll need moderator/administrator privileges to use this."; } return $o .= $data; } #-- form ---------------------------------------------------------------- if (empty($_REQUEST["snr_go"]) && empty($_REQUEST["snr_test"])) { $url = ewiki_script("", $id); $o .= ewiki_t(<<<END Use this form to replace all occourences of a phrase in all WikiPages. <br /><br /> <form action="{$url}" method="POST" enctype="multipart/form-data"> search for string<br /> <input type="text" name="snr_search_string" value="" size="30"><br /> <small>this text snippet always matches case-insensitive, used as <b>first-stage</b> search string; leave it empty to use only the regular expression matching (slower)</small><br /> look this string up only in <select name="snr_search_string_where"><option selected="selected" value="content">page content / body</option> <option value="id">page name / title</option></select><br /> <br /> <i>and/or</i> with <tt>/Perl/i</tt> regular expression<br /> <input type="text" name="snr_search_regex" value="" size="30"><br /> <small>this is <b>optional</b>, and is anyhow only used as second-stage search pattern; if used allows to use regex backreferences in the replacement string field</small><br /> <br /> then replace with string<br /> <input type="text" name="snr_replace" value="" size="30"><br /> <small>can contain backreferences and \$1 if the regex search field was used</small><br /> <br /> <input type="submit" name="snr_test" value="dry run / test regex"> <input type="submit" name="snr_go" value="Replace All"> </form> <br /> <br /> The regular expression matching is optional, you'll often only need the simple string search field and another simple string in the replacement field. <br /> <br /> Please note, that this form allows to initially search for a simple string, but you can leave this empty and only use a regex search. And as it is a two stage searching, both patterns can be completely different. <br /> <br /> Text replacement always happens in the WikiPages body, even if the simple search string can be used to search for page names - if you do so, you certainly need a second regular expression pattern for content replacement. <br /> END ); } else { $do = $_REQUEST["snr_go"]; #-- prepare vars $search_where = $_REQUEST["snr_search_string_where"]; $search_string = $_REQUEST["snr_search_string"]; $search_regex = $_REQUEST["snr_search_regex"]; $replacement = $_REQUEST["snr_replace"]; if ($search_string == "*") { $search_string = ""; } $search_string2 = preg_quote($search_string, "/"); $replacement2 = addcslashes($replacement, "\$"); #-- security check in search_regex if (preg_match('/([\\w\\s]+)$/', $search_regex, $uu) && strstr($uu[0], "e")) { ewiki_log("use of regex '{$search_regex}' could be security circumvention attempt", 1); return $o . "wrong regex delimiter"; } #-- complain if (empty($search_string) && empty($search_regex) || empty($replacement)) { return $o . "too few parameters, needs at least one search and a replacement string"; } #-- initial database string search if (empty($search_string)) { $result = ewiki_db::GETALL(array("id", "version", "flags")); } else { $result = ewiki_db::SEARCH($search_where, $search_string); } #-- walk through pages while ($row = $result->get()) { #-- skip binary entries if (EWIKI_DB_F_TEXT != ($row["flags"] & EWIKI_DB_F_TYPE)) { continue; } $id = $row["id"]; $save = false; $row = ewiki_db::GET($id); /* if (!ewiki_auth($id, $row, "edit", ... ... */ if ($search_regex) { if (preg_match($search_regex, $row[$search_where], $uu)) { $save = true; $row["content"] = preg_replace($search_regex, $replacement, $row["content"]); } } elseif ($search_string) { if (stristr($row[$search_where], $search_string)) { $save = true; $row["content"] = preg_replace("/{$search_string2}/i", $replacement, $row["content"]); } } if ($save) { $o .= "· <a href=\"" . ewiki_script("", $id) . "\">" . htmlentities($id) . "</a> matched given search pattern<br />\n"; if ($do) { $row["lastmodified"] = time(); $row["author"] = ewiki_author("SearchAndReplace"); $row["version"]++; if (ewiki_db::WRITE($row)) { $o .= " changed.<br />\n"; } else { $o .= " database store error<br />\n"; $o .= " " . mysql_error() . "<br />\n"; } } } } #-- while $result if ($do) { ewiki_log("SearchAndReplace for '{$search_strinmg}' and '{$search_regex}' to replace with '{$replacement}'"); } } return $o; }