3 display.php3: render a page. This has all the display
4 logic in it, except for the search boxes.
7 // if we got GET data, the first item is always a page name
8 // if it wasn't this file would not have been included
11 // match pagename in GET data, or fail
12 if (ereg("^(([A-Z][a-z]+){2,})", $argv[0], $regs)) {
15 // error, no valid page name passed in
16 echo "Error: invalid page name";
20 $pagename = "FrontPage";
22 // if there is no FrontPage, create a basic set of Wiki pages
23 if (! IsWikiPage($dbi, $pagename)) {
24 include "wiki_setupwiki.php3";
28 WikiHeader($pagename);
30 echo "<h1>$LogoImage ";
31 echo "<a href=\"$ScriptUrl?full=$pagename\">$pagename</a></h1>\n";
33 $pagehash = RetrievePage($dbi, $pagename);
34 if (is_array($pagehash)) {
35 // we render the page if it's a hash, else ask the user to write
38 // Set up inline links and images
39 for ($i = 1; $i < (NUM_LINKS + 1); $i++) {
40 if (! empty($pagehash["r$i"])) {
41 if (preg_match("/png$/i", $pagehash["r$i"])) {
43 $embedded[$i] = "<img src='" . $pagehash["r$i"] . "'>";
45 // ordinary embedded link
46 $embedded[$i] = "<a href='" . $pagehash["r$i"] . "'>[$i]</a>";
51 $numlines = count($pagehash["text"]);
53 // Loop over all lines of tha page and apply transformation rules
54 for ($index = 0; $index < $numlines; $index++) {
55 $tmpline = $pagehash["text"][$index];
57 // workaround for null array elements bug
58 // This was affecting RecentChanges but no more
59 if (strlen($tmpline) == 0) {
63 if (strlen($tmpline) == 1) {
64 // this is a blank line, send <p>
65 SetHTMLOutputMode("p", ZERO_DEPTH, 0);
69 /* If your web server is not accessble to the general public, you may allow this code below,
70 which allows embedded HTML. If anyone can reach your web server it is highly advised that
71 you do not allow this.
73 elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
75 SetHTMLOutputMode("", ZERO_DEPTH, 0);
80 // escape HTML metachars
81 $tmpline = ereg_replace("[&]", "&", $tmpline);
82 $tmpline = ereg_replace("[>]", ">", $tmpline);
83 $tmpline = ereg_replace("[<]", "<", $tmpline);
85 // four or more dashes to <hr>
86 $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
89 // replace all URL's with tokens, so we don't confuse them
90 // with Wiki words later. Wiki words in URL's break things.
92 $hasURLs = preg_match_all("/\b((http)|(ftp)|(mailto)|(news)|(file)|(gopher)):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
94 // workaround: php can only do global search and replace which
95 // renders wrong when the domain appears in two consecutive URL's
96 // on the same line, but the second is longer i.e.
97 // http://c2.com followed by http://c2.com/wiki
101 for ($i = 0; $i < $hasURLs; $i++) {
102 $inplaceURL = preg_quote($urls[0][$i]);
103 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
104 $tmpline = preg_replace("|$inplaceURL|",
110 $tmpline = eregi_replace("(''''')(.*)(''''')",
111 "<strong><em>\\2</em></strong>",
115 $tmpline = eregi_replace("(''')(.*)(''')",
116 "<strong>\\2</strong>",
120 $tmpline = eregi_replace("('')(.*)('')",
125 if (preg_match_all("#\b(([A-Z][a-z]+){2,})\b#",
129 // uniq the list of matches
131 for ($i = 0; $link[0][$i]; $i++) {
132 // $realfile = $link[0][$i];
133 $hash[$link[0][$i]]++;
137 while (list($realfile, $val) = each($hash)) {
138 if (IsWikiPage($dbi, $realfile)) {
139 $tmpline = preg_replace("|\b$realfile\b|",
140 LinkExistingWikiWord($realfile),
143 $tmpline = preg_replace("|\b$realfile\b|",
144 LinkUnknownWikiWord($realfile),
151 // put URLs back, linked
152 for ($i = 0; $i < $hasURLs; $i++) {
153 $inplaceURL = "<a href='" . $urls[0][$i] . "'>";
154 $inplaceURL .= $urls[0][$i] . "</a>";
155 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
156 $tmpline = preg_replace("|$URLtoken|",
162 // Insert search boxes, if needed
163 $tmpline = ereg_replace("\[Search]", RenderQuickSearch(), $tmpline);
164 $tmpline = ereg_replace("\[Fullsearch]", RenderFullSearch(), $tmpline);
166 // match and replace all user-defined links ([1], [2], [3]...)
167 preg_match_all("|\[(\d)\]|", $tmpline, $match);
168 if (count($match[0])) {
169 for ($k = 0; $k < count($match[0]); $k++) {
170 if (! empty($embedded[$match[1][$k]])) {
171 $linkpattern = preg_quote($match[0][$k]);
172 $tmpline = preg_replace("|$linkpattern|",
173 $embedded[$match[1][$k]],
179 // HTML modes: pre, unordered/ordered lists, term/def
180 if (preg_match("/(^\t)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
181 // this is a dictionary list item
182 SetHTMLOutputMode("dl", SINGLE_DEPTH, 1);
183 $tmpline = "<dt>" . $matches[2] . "<dd>" . $matches[4];
185 } elseif (preg_match("/(^\t+)(\*|\d|#)/", $tmpline, $matches)) {
186 // this is part of a list
187 $numtabs = strlen($matches[1]);
188 if ($matches[2] == "*") {
191 $listtag = "ol"; // a rather tacit assumption. oh well.
193 $tmpline = preg_replace("/^(\t+)(\*|\d|#)/", "", $tmpline);
194 SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
197 } elseif (preg_match("/^\s+/", $tmpline)) {
198 // this is preformatted text, i.e. <pre>
199 SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
202 // it's ordinary output if nothing else
203 SetHTMLOutputMode("", ZERO_DEPTH, 0);
206 echo "$tmpline"; // at last, emit the code
210 echo "Describe $pagename<a href='$ScriptUrl?edit=$pagename'>?</a> here.\n";
213 SetHTMLOutputMode("", ZERO_DEPTH, 0);