3
# WikiWare - Standard MoinMoin Transclusion Function
4
# Dmytri Kleiner -- dmytrik@trickmedia.com
5
# Copyleft 2003 Idiosyntactix. All rights detourned.
6
# License: GNU General Public License (GPL)
9
# print MoinMoin("FrontPage");
10
# print MoinMoin("FrontPage","costomregs.php");
11
# print MoinMoin("FrontPage","costomregs.php",$cachesuffix);
13
# General Configuration
14
# ... moved into trick.php
16
#############################################################
17
#############################################################
18
#############################################################
21
* All of these get set when a user presses shift-reload in mozilla
23
* HTTP_CACHE_CONTROL: "no-cache"
24
* HTTP_PRAGMA: "no-cache"
25
* PHP Variables: _SERVER["HTTP_CACHE_CONTROL"]: "no-cache"
26
* HTTP request headers: Cache-Control "no-cache"
29
* All of these get set when a user presses reload in mozilla
30
* HTTP_CACHE_CONTROL: "max-age=0"
31
* PHP Variables: _SERVER["HTTP_CACHE_CONTROL"]: "max-age=0"
32
* HTTP request headers: Cache-Control "max-age=0"
34
* None of these are available when a "normal" page load is performed.
36
* For now, we only pay attention to the shift-reload sequence.
40
function MoinMoinNoCache($cachefile)
42
global $MOINMOINCacheLimit, $PageTitle, $MOINMOINurl, $MOINMOINfetched;
44
if (isset($MOINMOINfetched[$cachefile])) {
47
if (isset($MOINMOINCacheLimit[$PageTitle])) {
48
$cachelimit = $MOINMOINCacheLimit[$PageTitle];
49
}elseif (isset($MOINMOINCacheLimit['*'])) {
50
$cachelimit = $MOINMOINCacheLimit['*'];
54
if ($cachelimit >= 0) {
55
$now = intval(date("U"));
56
if (file_exists($cachefile)) {
58
$mtime = filemtime($cachefile);
62
if (($now-$mtime) >= $cachelimit) {
66
if (isset($_SERVER["HTTP_CACHE_CONTROL"])) {
67
return (0 == strcasecmp($_SERVER["HTTP_CACHE_CONTROL"], "no-cache"));
72
function MoinMoinLang ($ptitle, $INCLUDEPHP = false, $CACHESUFFIX = "")
75
if ($MOINMOINlang == "" || $MOINMOINlang == "en" || $MOINMOINlang == "us_en") {
76
return "<!-- lang: en $MOINMOINlang --> " . MoinMoin($ptitle, $INCLUDEPHP, $CACHESUFFIX);
78
return "<!-- lang: $MOINMOINlang --> " .
79
MoinMoin($MOINMOINlang . "/" . $ptitle . "_" . $MOINMOINlang, $INCLUDEPHP, $CACHESUFFIX);
83
$MOINMOIN404string = "<b>Page not found.</b> <!-- 404 -->";
85
function MoinMoin($ptitle, $INCLUDEPHP = false, $CACHESUFFIX = "")
87
global $MOINMOINurl, $MOINMOINalias, $MOINMOINcachedir, $MOINMOINfilemod, $MOINMOINstandardsearch;
88
global $MOINMOINstandardreplace, $current_cache_prefix, $current_cache_relprefix, $PageTitle;
89
global $MOINMOINfetched, $MOINMOINpagename;
91
#$PageTitle = str_replace("/","_", $ptitle);
93
$filename = "$MOINMOINurl/$PageTitle";
94
# $cachefile = "$MOINMOINcachedir/$MOINMOINalias$PageTitle$CACHESUFFIX.html";
95
$cachefile = sprintf("%s/%s%s%s.html", $MOINMOINcachedir, $MOINMOINalias, str_replace("/","_2f", $ptitle), $CACHESUFFIX);
96
# for attachments and the like
97
$PageTitleNoSlash = str_replace("/","_",$ptitle);
98
$current_cache_prefix = "$MOINMOINcachedir/${MOINMOINalias}${PageTitleNoSlash}__";
99
$current_cache_relprefix = "${MOINMOINalias}${PageTitle}__";
103
if (MoinMoinNoCache($cachefile) && file_exists($cachefile)) {
106
if (!file_exists($cachefile))
108
$content = wget_text($filename);
111
/* OOPS! ptitle URL is 404! */
112
global $MOINMOIN404string, $MOINMOINlang;
113
/* Try the English version of the page (if any) */
114
if (preg_match("/^$MOINMOINlang\/(.*)_$MOINMOINlang/"
115
, $ptitle, $match)) {
116
/* Better than nothing, eh? */
117
LogIt("SUBSTITUTED $match[1] for $ptitle");
118
return MoinMoin($match[1], $INCLUDEPHP, $CACHESUFFIX);
120
LogIt("GOT404 on $ptitle");
121
return $MOINMOIN404string;
124
if (!$GLOBALS["MOINMOINstandardregsloaded"])
126
MOINMOINloadstandardregs();
131
include($INCLUDEPHP);
133
$MOINMOINallsearch = array_merge($MOINMOINstandardsearch, $MOINMOINsearch);
134
$MOINMOINallreplace = array_merge($MOINMOINstandardreplace, $MOINMOINreplace);
136
unset($MOINMOINsearch);
137
unset($MOINMOINreplace);
139
$MOINMOINallsearch = $MOINMOINstandardsearch;
140
$MOINMOINallreplace = $MOINMOINstandardreplace;
142
$body = preg_replace ($MOINMOINallsearch, $MOINMOINallreplace, $content);
143
# Because we include things, these line numbers can easily wind up conflicting...
144
$body = preg_replace("'<span id=\"line-[0-9]+\"></span>'si", "", $body);
146
$fd = fopen($cachefile, "w");
149
chmod($cachefile, $MOINMOINfilemod);
150
$msg=sprintf("EXPANDED %d bytes into %s"
151
, filesize($cachefile), $cachefile);
152
$MOINMOINfetched[$cachefile] = true;
155
$body = implode("",file($cachefile));
156
$msg=sprintf("READ %d bytes from %s"
157
, filesize($cachefile), $cachefile);
165
function MOINMOINloadstandardregs()
167
global $MOINMOINstandardsearch, $MOINMOINstandardreplace, $MOINMOINalias
168
, $local_cache_url_prefix, $MOINMOINExtraneousImages;
169
global $MOINMOIN404string;
171
if (!isset($MOINMOINstandardsearch)) { $MOINMOINstandardsearch = array(); }
172
if (!isset($MOINMOINstandardreplace)) { $MOINMOINstandardreplace = array(); }
175
# Trap Pages Not In Wiki
177
$MOINMOINstandardsearch[] = "'^.*<a href=\"[^\">]*?\?action=edit\">Create this page</a>.*$'s";
178
$MOINMOINstandardreplace[] = $MOINMOIN404string;
180
# Eliminate Goto Link from Include Macro
181
$MOINMOINstandardsearch[] = "'<div class=\"include-link\"><a [^>]*>.*?</a></div>'s";
182
$MOINMOINstandardreplace[] = "";
185
$MOINMOINstandardsearch[] = "'.*<span class=\"anchor\" id=\"top\"></span>'s";
186
$MOINMOINstandardreplace[] = "";
187
$MOINMOINstandardsearch[] = "'<span class=\"anchor\" id=\"bottom\"></span></div>.*$'s";
188
$MOINMOINstandardreplace[] = "";
190
# Strip Wiki Class Tags
191
$MOINMOINstandardsearch[] = "'(<[^>]*) class=\"[^\"]*\"([^>]*>)'U";
192
$MOINMOINstandardreplace[] = "\\1\\2";
195
$MOINMOINstandardsearch[] = "'(<table\s)[^>]*(>)'Ui";
196
$MOINMOINstandardreplace[] = "\\1\\2";
199
$MOINMOINstandardsearch[] = "'(<a\s[^>]*href=\")/$MOINMOINalias([^\">]*\">)'iU";
200
$MOINMOINstandardreplace[] = "\\1$local_cache_url_prefix\\2";
202
# Fix "illegal" id fields
203
# ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
204
# followed by any number of letters, digits ([0-9]), hyphens ("-"),
205
# underscores ("_"), colons (":"), and periods (".").
206
$MOINMOINstandardsearch[] = "'id=\"([^\"]*)\"'ie";
207
$MOINMOINstandardreplace[] = "'id=\"' . str_replace('/','_2f','\\1') . '\"'";
209
# Strip out [WWW] [FTP] images, etc.
210
foreach ($MOINMOINExtraneousImages as $im) {
211
$MOINMOINstandardsearch[] = "'< *img +src=\"${im}\"[^>]*> 'i";
212
$MOINMOINstandardreplace[] = '';
215
# Cache MoinMoin Images Locally
216
$MOINMOINstandardsearch[] = "'src=\"(/[^\"]*wiki[^\"]*img/([^\"]*))\"'ie";
217
$MOINMOINstandardreplace[] = "MOINMOINcacheimages('\\1','\\2')";
219
# Cache inline images (Attachments) Locally
220
$MOINMOINstandardsearch[] = "'(<\s*img\s[^>]*src=\")/$MOINMOINalias([^\">]*?action=AttachFile&[^\">]*target=([^\">]*))(\"[^>]*>)'iUe";
221
$MOINMOINstandardreplace[] = "stripslashes('\\1') . MOINMOINcacheattachments('\\2','\\3') . stripslashes('\\4')";
223
# Cache MoinMoin Attachments Localy
224
$MOINMOINstandardsearch[] = "'(<a\s*[^>]*href=\")/$MOINMOINalias([^\">]*?action=AttachFile&[^\">]*target=([^\">]*))(\"[^>]*>)'iUe";
225
$MOINMOINstandardreplace[] = "stripslashes('\\1') . MOINMOINcacheattachments('\\2','\\3') . stripslashes('\\4')";
226
# Cache MoinMoin EMBED/OBJECT Attachments Localy
227
$MOINMOINstandardsearch[] = "'(<EMBED\s*[^>]*SRC=\")/$MOINMOINalias([^\">]*?action=AttachFile&[^\">]*target=([^\">]*))(\"[^>]*>)'iUe";
228
$MOINMOINstandardreplace[] = "stripslashes('\\1') . MOINMOINcacheattachments('\\2','\\3') . stripslashes('\\4')";
230
$GLOBALS["MOINMOINstandardregsloaded"] = true;
233
function browser_type() {
237
if (isset($_SERVER["HTTP_USER_AGENT"])) {
238
$ua = $_SERVER["HTTP_USER_AGENT"];
240
$BrowserPats = array('%(MSIE|Opera) +(([1-9][0-9]*)\.[0-9.]+)%'
241
, '%; +(Konqueror|Netscape)/(([1-9][0-9]*)\.[0-9.]+)%i'
242
, '%(Mozilla)/(([1-9][0-9]*)\.[0-9.]+)%i');
244
foreach ($BrowserPats as $pat) {
245
if (preg_match($pat, $ua, $match)) {
248
$MajorVers=$match[3];
253
return array($Browser, $Version, intval($MajorVers));
256
function browser_compatibility() {
259
if (strcasecmp($T[0], "Mozilla") == 0 && $T[2] >= 5) {
262
if (strcasecmp($T[0], "MSIE") == 0) {
269
if (strcasecmp($T[0], "Konqueror") == 0) {
276
if (strcasecmp($T[0], "Netscape") == 0) {
279
}elseif ($T[2] >= 5) {
283
if (strcasecmp($T[0], "Opera") == 0) {
286
}elseif ($T[2] == 5) {
293
function browser_compatibility_messages() {
294
$c = browser_compatibility();
295
$ffurl="http://www.mozilla.org/products/firefox/";
296
$ff="<a href=\"$ffurl\">Firefox </a>";
297
$imgdir="http://sfx-images.mozilla.org/affiliates/Buttons";
298
$ffbut1="<img border=\"0\" alt=\"Get Firefox!\" src=\"$imgdir/80x15/white_1.gif\"/>";
299
$ffbut2="<img border=\"0\" alt=\"Get Firefox!\" height=\"24\" WIDTH=\"83\" src=\"$imgdir/110x32/trust.gif\"/>";
300
$ff1="<a href=\"$ffurl\">Firefox $ffbut1</a>";
301
$ff2="<a href=\"$ffurl\">Firefox $ffbut2</a>";
306
echo '<font size="-2">This site best when viewed with a CSS-compatible browser. '
307
. "We recommend $ff1.</font>\n";
309
echo '<font size="-2">This site best when viewed with a modern standards-compliant browser. '
310
. "We recommend $ff2.</font>\n";
314
function search_box_html($ncols)
316
echo "<TABLE><TR ALIGN=CENTER><TD>
317
<FORM method=GET action=\"http://www.google.com/search\">
318
<input type=hidden name=ie value=UTF-8><input type=hidden name=oe value=UTF-8>
319
<INPUT TYPE=text name=q size=$ncols maxlength=255 value=\"Enter search here.\"><BR>
320
<INPUT type=submit name=btnG VALUE=\"Site Search\"><BR>
321
<input type=hidden name=domains value=\"http://linux-ha.org\">
322
<input type=hidden name=sitesearch value=\"linux-ha.org\">
326
# return array($Browser, $Version, intval($MajorVers));
327
function stylesheet_link()
330
if (strcasecmp($T[0], "MSIE") == 0 && $T[2] < 7) {
331
$ss="/linuxhaIE6.css";
335
echo "<link rel=\"stylesheet\" href=\"$ss\" type=\"text/css\">\n";
338
function URLtoCacheFile($urlsuffix, $cacheprefix)
340
global $MOINMOINcachedir;
341
/* FIXME: Clean up $urlsuffix to make sure it's safe */
343
# need to clean up sanitize $argTar, in a specially crafted wiki page
344
# may be a special file name
345
# hope this is enough, just in case:
346
$urlsuffix = str_replace("/","_", "${cacheprefix}${urlsuffix}");
347
return "${MOINMOINcachedir}/${urlsuffix}";
350
function CleanURL($urlprefix, $urlsuffix)
352
/* FIXME: Clean up $url to make sure it's safe */
353
return $urlprefix . $urlsuffix;
356
function CacheURL($urlprefix, $urlsuffix, $cacheprefix)
358
$cachefile = URLtoCacheFile($urlsuffix, $cacheprefix);
359
$url = CleanURL($urlprefix , $urlsuffix);
360
return CacheURL_ll($url, $cachefile);
363
function LogIt($message)
365
$logfile="/tmp/linux-ha.web";
366
$datestamp=date("Y/m/d_H:i:s");
367
if (file_exists($logfile) && filesize($logfile) > 1000000) {
368
rename($logfile, "$logfile.OLD");
370
chmod($logfile, 0644);
372
error_log("$datestamp $message\n", 3, $logfile);
373
chmod($logfile, 0644);
375
function ReportError($errorcode, $descr, $file, $line, $symtab)
384
echo "ERROR: $errorcode $descr [$file:$line]";
385
LogIt("ERROR: $errorcode $descr [$file:$line]");
386
error_log("ERROR: $errorcode $descr [$file:$line]", 0);
387
if (isset($cachetmp) && file_exists($cachetmp)) {
393
function wget($url, $file) {
395
if (file_exists($file)) {
398
$WGET='/usr/bin/wget';
399
$url = preg_replace('/\\\\/', '\\\\', $url);
400
$url = preg_replace('/\'/', '\\\'', $url);
401
$CMD="$WGET -q -U 'Mozilla/5.0' -S -nd -O '$file' '" . $url . '\'';
404
/* LogIt("WGETFAIL: [$CMD] failed with $rc"); */
405
if (file_exists($file)) {
409
/*LogIt(sprintf("wget %s got %d bytes", $url, filesize($file)))*/;
413
function wget_text($url) {
414
$tmpname = tempnam("/var/tmp", "wget");
415
if (wget($url, $tmpname) == 0) {
416
$ret = implode("",file($tmpname));
420
/* LogIt("wget_text failed: [$url] failed"); */
424
# Inputs are the results from microtime()
425
function subtimes($lhs, $rhs)
427
$lhsa = explode(" ", $lhs, 2);
428
$lhsusec=intval(substr($lhsa[0], 2, 6));
429
$lhssec=intval($lhsa[1]);
431
$rhsa = explode(" ", $rhs, 2);
432
$rhsusec=intval(substr($rhsa[0], 2, 6));
433
$rhssec=intval($rhsa[1]);
435
if ($lhsusec < $rhsusec) {
439
$usecs = ($lhssec - $rhssec) * 1000000;
440
$usecs += ($lhsusec - $rhsusec);
441
return (doubleval($usecs)/ 1000000.0);
444
function CacheURL_ll($url, $cachefile)
446
global $MOINMOINfilemod, $MOINMOINcachedir, $MOINMOINurl;
447
global $cachetmp, $MOINMOINfetched;
448
$cachetmp = tempnam($MOINMOINcachedir, 'TEMP_');
450
set_error_handler("ReportError");
451
if (substr($url, 0, 1) == "/") {
452
$url = "${MOINMOINurl}/${url}";
456
if (wget($url, $cachetmp) != 0) {
460
$elapsed = subtimes($end, $start);
461
chmod($cachetmp, $MOINMOINfilemod);
463
$msg=sprintf("CACHED %d bytes in %.3f secs into %s"
464
, filesize($cachetmp), $elapsed, $cachefile);
467
if (file_exists($cachefile)) {
470
rename($cachetmp, $cachefile);
471
if (file_exists($cachetmp)) {
475
$MOINMOINfetched[$cachefile] = true;
476
return file_exists($cachefile);
480
function MOINMOINcacheattachments($argSrc, $argTar)
482
global $MOINMOINurl, $current_cache_prefix, $local_cache_url_prefix;
485
# need to clean up sanitize $argTar, in a specially crafted wiki page
486
# may be a special file name
487
# hope this is enough, just in case:
488
$argTar = str_replace("/","_", $argTar);
490
$cachefile = "$current_cache_prefix$argTar";
491
if (MoinMoinNoCache($cachefile) || !file_exists($cachefile)) {
492
CacheURL_ll("$MOINMOINurl/$argSrc", $cachefile);
494
return $local_cache_url_prefix . $cachefile;
497
function MOINMOINcacheimages($argSrc, $argTar)
499
global $MOINMOINserver, $MOINMOINcachedir, $MOINMOINfilemod, $local_cache_url_prefix, $MOINMOINurl;
502
# need to clean up sanitize $argTar, in a specially crafted wiki page
503
# may be a special file name
504
# hope this is enough, just in case:
506
$argTar = str_replace("/","_", $argTar);
508
$cachefile = URLtoCacheFile($argSrc, "");
509
if (MoinMoinNoCache($cachefile) || !file_exists($cachefile)) {
510
CacheURL($MOINMOINurl, $argSrc, "");
512
return "src=\"$local_cache_url_prefix$cachefile\"";
515
* This is what google says we ought to do..
516
* Index the site from only one name - not from all possible names.
517
* (in our case, we own about 6 domain names)
518
* AND... tell robots to ignore any pages with ? in the name
520
function robots_metadata()
522
global $MOINMOINSitesToIndex;
523
global $MOINMOINCacheLimit, $PageTitle, $MOINMOINpagename;
526
if (!isset($_SERVER["HTTP_HOST"])
527
|| !isset($MOINMOINSitesToIndex[$_SERVER["HTTP_HOST"]])
528
|| (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")) {
532
if (isset($MOINMOINCacheLimit[$MOINMOINpagename])) {
536
$policy = '<META NAME="ROBOTS" CONTENT="INDEX';
538
$policy = '<META NAME="ROBOTS" CONTENT="NOINDEX';
541
$policy .= ', FOLLOW">';
543
$policy .= ', NOFOLLOW">';