3 Standard functions for Wiki functionality
8 LinkExistingWikiWord($wikiword)
9 LinkUnknownWikiWord($wikiword)
13 CookSpaces($pagearray)
15 SetHTMLOutputMode($newmode, $depth)
16 UpdateRecentChanges($dbi, $pagename)
17 SaveCopyToArchive($pagename, $pagehash)
18 ParseAndLink($bracketlink)
21 // render the Wiki toolbar at bottom of page
22 function WikiToolBar() {
23 global $ScriptUrl, $pagename, $pagehash;
24 $enc_name = rawurlencode($pagename);
27 "<a href=\"$ScriptUrl?edit=$enc_name\">EditText</a>\n" .
29 if (is_array($pagehash)) {
30 $retval .= " (last edited " . $pagehash["date"] . ")\n";
33 "<a href=\"$ScriptUrl?FindPage\">" .
34 "FindPage</a> by browsing or searching\n";
39 function WikiHeader($pagename) {
40 global $LogoImage, $ScriptUrl;
41 return "<html>\n<head>\n<title>" . htmlspecialchars($pagename) .
42 "</title>\n</head>\n<body>\n";
45 function WikiFooter() {
46 return "</body>\n</html>\n";
49 function GetCurrentDate() {
50 // format is like December 13, 1999
51 return date("F j, Y");
55 function LinkExistingWikiWord($wikiword) {
57 $enc_word = rawurlencode($wikiword);
58 $wikiword = htmlspecialchars($wikiword);
59 return "<a href=\"$ScriptUrl?$enc_word\">$wikiword</a>";
62 function LinkUnknownWikiWord($wikiword) {
64 $enc_word = rawurlencode($wikiword);
65 $wikiword = htmlspecialchars($wikiword);
66 return "<u>$wikiword</u><a href=\"$ScriptUrl?edit=$enc_word\">?</a>";
69 function LinkURL($url) {
71 if(ereg("[<>\"]", $url)) {
72 return "<b><u>BAD URL -- remove all of <, >, "</u></b>";
74 $enc_url = htmlspecialchars($url);
75 return "<a href=\"$url\">$enc_url</a>";
79 function RenderQuickSearch() {
80 global $value, $ScriptUrl;
81 $formtext = "<form action='$ScriptUrl'>\n<input type='text' size='40' name='search' value='$value'>\n</form>\n";
85 function RenderFullSearch() {
86 global $value, $ScriptUrl;
87 $formtext = "<form action='$ScriptUrl'>\n<input type='text' size='40' name='full' value='$value'>\n</form>\n";
91 // converts spaces to tabs
92 function CookSpaces($pagearray) {
93 return preg_replace("/ {3,8}/", "\t", $pagearray);
101 function push($item) {
102 $this->items[$this->size] = $item;
108 if ($this->size == 0) {
109 return false; // stack is empty
112 return $this->items[$this->size];
120 return $this->items[$this->size - 1];
124 // end class definition
127 // I couldn't move this to wiki_config.php3 because it
128 // wasn't declared yet.
132 Wiki HTML output can, at any given time, be in only one mode.
133 It will be something like Unordered List, Preformatted Text,
134 plain text etc. When we change modes we have to issue close tags
135 for one mode and start tags for another.
138 function SetHTMLOutputMode($tag, $tagdepth, $tabcount) {
142 if ($tagdepth == SINGLE_DEPTH) {
144 if ($tabcount < $stack->cnt()) {
146 // there are fewer tabs than stack, reduce stack
147 // to one less than tab count; then push new tag
148 while ($stack->cnt() > ($tabcount - 1)) {
149 $closetag = $stack->pop();
150 if ($closetag == false) {
151 //echo "bounds error in tag stack";
155 #echo "</$closetag>\n";
156 $retvar .= "</$closetag>\n";
160 $retvar .= "<$tag>\n";
163 } elseif ($tabcount > $stack->cnt()) {
164 // we add the diff to the stack
165 // stack might be zero
166 while ($stack->cnt() < $tabcount) {
168 $retvar .= "<$tag>\n";
170 if ($stack->cnt() > 10) {
171 // arbitrarily limit tag nesting
172 echo "Stack bounds exceeded in SetHTMLOutputMode\n";
178 if ($tag == $stack->top()) {
181 $closetag = $stack->pop();
182 #echo "</$closetag>\n";
184 $retvar .= "</$closetag>\n";
185 $retvar .= "<$tag>\n";
190 } elseif ($tagdepth == ZERO_DEPTH) {
191 // empty the stack for $depth == 0;
192 // what if the stack is empty?
193 if ($tag == $stack->top()) {
196 while ($stack->cnt() > 0) {
197 $closetag = $stack->pop();
198 #echo "</$closetag>\n";
199 $retvar .= "</$closetag>\n";
204 $retvar .= "<$tag>\n";
210 echo "Passed bad tag depth value in SetHTMLOutputMode\n";
217 // end SetHTMLOutputMode
221 // The Recent Changes file is solely handled here
222 function UpdateRecentChanges($dbi, $pagename) {
224 global $remoteuser; // this is set in the config
226 $recentchanges = RetrievePage($dbi, "RecentChanges");
228 // this shouldn't be necessary, since PhpWiki loads
229 // default pages if this is a new baby Wiki
230 if ($recentchanges == -1) {
231 $recentchanges = array();
234 $currentdate = GetCurrentDate();
236 if ($recentchanges["date"] != $currentdate) {
238 $recentchanges["date"] = $currentdate;
243 $numlines = sizeof($recentchanges["text"]);
247 // scroll through the page to the first date and break
248 for ($i = 0; $i < ($numlines + 1); $i++) {
249 if (preg_match("/^\w\w\w+ \d\d?, \d\d\d\d\r$/",
250 $recentchanges["text"][$i])) {
253 $newpage[$k++] = $recentchanges["text"][$i];
257 // if it's a new date, insert it, else add the updated page's
261 $newpage[$k++] = "$currentdate\r";
263 $newpage[$k++] = $recentchanges["text"][$i++];
265 $newpage[$k++] = "\t* [$pagename] ..... $remoteuser\r";
267 // copy the rest of the page into the new array
268 $pagename = preg_quote($pagename);
269 for (; $i < ($numlines + 1); $i++) {
270 // skip previous entry for $pagename
271 if (preg_match("/\[$pagename\]/", $recentchanges["text"][$i])) {
274 $newpage[$k++] = $recentchanges["text"][$i];
278 $recentchanges["text"] = $newpage;
280 InsertPage($dbi, "RecentChanges", $recentchanges);
284 // for archiving pages to a seperate dbm
285 function SaveCopyToArchive($pagename, $pagehash) {
286 global $ArchiveDataBase;
288 $adbi = OpenDataBase($ArchiveDataBase);
289 $newpagename = $pagename;
290 InsertPage($adbi, $newpagename, $pagehash);
294 function ParseAndLink($bracketlink) {
295 global $dbi, $AllowedProtocols;
297 // $bracketlink will start and end with brackets; in between
298 // will be either a page name, a URL or both seperated by a pipe.
300 // strip brackets and leading space
301 preg_match("/(\[\s*)(.+?)(\s*\])/", $bracketlink, $match);
302 $linkdata = $match[2];
304 // send back links that are only numbers (they are references)
305 if (preg_match("/^\d+$/", $linkdata)) {
309 // send back escaped ([[) bracket sets
310 if (preg_match("/^\[/", $linkdata)) {
311 return htmlspecialchars($linkdata) . "]";
314 // match the contents
315 preg_match("/([^|]+)(\|)?([^|]+)?/", $linkdata, $matches);
317 if (isset($matches[3])) {
318 $URL = trim($matches[3]);
319 $linkname = htmlspecialchars(trim($matches[1]));
320 if (preg_match("#^($AllowedProtocols):#", $linkname)) {
321 return "<a href=\"$URL\">$linkname</a>";
323 return "<b><u>BAD URL -- links have to start with one of " . "$AllowedProtocols followed by ':'</u></b>";
327 if (isset($matches[1])) {
328 $linkname = trim($matches[1]);
329 if (IsWikiPage($dbi, $linkname)) {
330 return LinkExistingWikiWord($linkname);
331 } elseif (preg_match("#^($AllowedProtocols):#", $linkname)) {
332 return LinkURL($linkname);
333 } elseif ($linkname == 'Search') {
334 return RenderQuickSearch();
335 } elseif ($linkname == 'Fullsearch') {
336 return RenderFullSearch();
338 return LinkUnknownWikiWord($linkname);