]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_display.php3
This commit was generated by cvs2svn to compensate for changes in r2,
[SourceForge/phpwiki.git] / wiki_display.php3
1 <?
2    /*
3       display.php3: render a page. This has all the display 
4       logic in it, except for the search boxes.
5    */
6  
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
9
10    if ($argv[0]) {
11       // match pagename in GET data, or fail
12       if (ereg("^(([A-Z][a-z]+){2,})", $argv[0], $regs)) {
13          $pagename = $regs[1];
14       } else {
15          // error, no valid page name passed in
16          echo "Error: invalid page name";
17          exit();
18       }
19    } else { 
20       $pagename = "FrontPage"; 
21
22       // if there is no FrontPage, create a basic set of Wiki pages
23       if (! IsWikiPage($dbi, $pagename)) {
24          include "wiki_setupwiki.php3";
25       }
26    }
27
28    WikiHeader($pagename);
29
30    echo "<h1>$LogoImage ";
31    echo "<a href=\"$ScriptUrl?full=$pagename\">$pagename</a></h1>\n";
32
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
36       // one.
37
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"])) {
42                // embed PNG images
43                $embedded[$i] = "<img src='" . $pagehash["r$i"] . "'>";
44             } else {
45                // ordinary embedded link
46                $embedded[$i] = "<a href='" . $pagehash["r$i"] . "'>[$i]</a>";
47             }
48          }
49       }
50
51       $numlines = count($pagehash["text"]);
52
53       // Loop over all lines of tha page and apply transformation rules
54       for ($index = 0; $index < $numlines; $index++) {
55          $tmpline = $pagehash["text"][$index];
56
57          // workaround for null array elements bug
58          // This was affecting RecentChanges but no more
59          if (strlen($tmpline) == 0) {
60             continue;
61          }
62
63          if (strlen($tmpline) == 1) {
64             // this is a blank line, send <p>
65             SetHTMLOutputMode("p", ZERO_DEPTH, 0);
66             continue;
67
68          }
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.
72
73          elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
74             // HTML mode
75             SetHTMLOutputMode("", ZERO_DEPTH, 0);
76             echo $matches[2];
77             continue;
78          }
79 */
80          // escape HTML metachars
81          $tmpline = ereg_replace("[&]", "&amp;", $tmpline);
82          $tmpline = ereg_replace("[>]", "&gt;", $tmpline);
83          $tmpline = ereg_replace("[<]", "&lt;", $tmpline);
84
85          // four or more dashes to <hr>
86          $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
87
88
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.
91
92          $hasURLs = preg_match_all("/\b((http)|(ftp)|(mailto)|(news)|(file)|(gopher)):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
93
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 
98          rsort($urls[0]);
99          reset($urls[0]);
100
101          for ($i = 0; $i < $hasURLs; $i++) {
102             $inplaceURL = preg_quote($urls[0][$i]);
103             $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
104             $tmpline = preg_replace("|$inplaceURL|",
105                                     $URLtoken,
106                                     $tmpline);
107          }
108
109          // bold italics
110          $tmpline = eregi_replace("(''''')(.*)(''''')",
111                                  "<strong><em>\\2</em></strong>",
112                                  $tmpline);
113
114          // bold
115          $tmpline = eregi_replace("(''')(.*)(''')",
116                                  "<strong>\\2</strong>",
117                                  $tmpline);
118
119          // italics
120          $tmpline = eregi_replace("('')(.*)('')",
121                                  "<em>\\2</em>",
122                                  $tmpline);
123
124          // Link Wiki words
125          if (preg_match_all("#\b(([A-Z][a-z]+){2,})\b#",
126                             $tmpline, 
127                             $link)) {
128
129             // uniq the list of matches
130             $hash = "";
131             for ($i = 0; $link[0][$i]; $i++) {
132                // $realfile = $link[0][$i];
133                $hash[$link[0][$i]]++;
134             }
135
136             reset($hash);
137             while (list($realfile, $val) = each($hash)) {
138                if (IsWikiPage($dbi, $realfile)) {
139                   $tmpline = preg_replace("|\b$realfile\b|",
140                               LinkExistingWikiWord($realfile),
141                               $tmpline);
142                } else {
143                   $tmpline = preg_replace("|\b$realfile\b|",
144                               LinkUnknownWikiWord($realfile),
145                               $tmpline);
146                }
147             }
148
149          }
150
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|", 
157                                     $inplaceURL,
158                                     $tmpline);
159          }
160
161
162          // Insert search boxes, if needed
163          $tmpline = ereg_replace("\[Search]", RenderQuickSearch(), $tmpline);
164          $tmpline = ereg_replace("\[Fullsearch]", RenderFullSearch(), $tmpline);
165
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]],
174                                           $tmpline);
175                }
176             }
177          }
178
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];
184
185          } elseif (preg_match("/(^\t+)(\*|\d|#)/", $tmpline, $matches)) {
186             // this is part of a list
187             $numtabs = strlen($matches[1]);
188             if ($matches[2] == "*") {
189                $listtag = "ul";
190             } else {
191                $listtag = "ol"; // a rather tacit assumption. oh well.
192             }
193             $tmpline = preg_replace("/^(\t+)(\*|\d|#)/", "", $tmpline);
194             SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
195             echo "<li>";
196
197          } elseif (preg_match("/^\s+/", $tmpline)) {
198             // this is preformatted text, i.e. <pre>
199             SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
200
201          } else {
202             // it's ordinary output if nothing else
203             SetHTMLOutputMode("", ZERO_DEPTH, 0);
204          }
205
206          echo "$tmpline"; // at last, emit the code
207       }
208
209    } else {
210       echo "Describe $pagename<a href='$ScriptUrl?edit=$pagename'>?</a> here.\n";
211    }
212
213    SetHTMLOutputMode("", ZERO_DEPTH, 0);
214    WikiToolBar();
215    WikiFooter();
216 ?>
217