]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/stdlib.php
Function UnMangleXmlIdentifier unused
[SourceForge/phpwiki.git] / lib / stdlib.php
1 <?php
2 /*
3  * Copyright 1999-2008 $ThePhpWikiProgrammingTeam
4  * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /*
24   Standard functions for Wiki functionality
25     WikiURL ($pagename, $args, $get_abs_url)
26     AbsoluteURL ($url)
27     IconForLink ($protocol_or_url)
28     PossiblyGlueIconToText($proto_or_url, $text)
29     IsSafeURL($url)
30     LinkURL ($url, $linktext)
31     LinkImage ($url, $alt)
32     ImgObject ($img, $url)
33
34     SplitQueryArgs ($query_args)
35     LinkPhpwikiURL ($url, $text, $basepage)
36     ConvertOldMarkup ($content, $markup_type = "block")
37     MangleXmlIdentifier($str)
38
39     class Stack { push($item), pop(), cnt(), top() }
40     class Alert { show() }
41     class WikiPageName {getParent(),isValid(),getWarnings() }
42
43     expand_tabs($str, $tab_width = 8)
44     SplitPagename ($page)
45     NoSuchRevision ($request, $page, $version)
46     TimezoneOffset ($time, $no_colon)
47     Iso8601DateTime ($time)
48     Rfc2822DateTime ($time)
49     ParseRfc1123DateTime ($timestr)
50     CTime ($time)
51     ByteFormatter ($bytes = 0, $longformat = false)
52     __printf ($fmt)
53     __sprintf ($fmt)
54     __vsprintf ($fmt, $args)
55
56     file_mtime ($filename)
57     sort_file_mtime ($a, $b)
58     class fileSet {fileSet($directory, $filepattern = false),
59                    getFiles($exclude='', $sortby='', $limit='') }
60     class ListRegexExpand { listMatchCallback($item, $key),
61                             expandRegex ($index, &$pages) }
62
63     glob_to_pcre ($glob)
64     glob_match ($glob, $against, $case_sensitive = true)
65     explodeList ($input, $allnames, $glob_style = true, $case_sensitive = true)
66     explodePageList ($input, $perm = false)
67     isa ($object, $class)
68     can ($object, $method)
69     function_usable ($function_name)
70     wikihash ($x)
71     better_srand ($seed = '')
72     count_all ($arg)
73     isSubPage ($pagename)
74     subPageSlice ($pagename, $pos)
75     isActionPage ($filename)
76
77     phpwiki_version ()
78     isWikiWord ($word)
79     obj2hash ($obj, $exclude = false, $fields = false)
80     isUtf8String ($s)
81     fixTitleEncoding ($s)
82     url_get_contents ($uri)
83     GenerateId ($name)
84     firstNWordsOfContent ($n, $content)
85     extractSection ($section, $content, $page, $quiet = false, $sectionhead = false)
86     isExternalReferrer()
87
88     charset_convert($from, $to, $data)
89     string_starts_with($string, $prefix)
90     string_ends_with($string, $suffix)
91     array_remove($arr,$value)
92     longer_timeout($secs=30)
93     printSimpleTrace($bt)
94     getMemoryUsage()
95     binary_search($needle, $haystack)
96     is_localhost()
97     javascript_quote_string($s)
98     isSerialized($s)
99     is_whole_number($var)
100     parse_attributes($line)
101     is_image ($filename)
102     is_video ($filename)
103
104   function: linkExistingWikiWord($wikiword, $linktext, $version)
105   moved to: lib/WikiTheme.php
106 */
107 if (defined('_PHPWIKI_STDLIB_LOADED')) return;
108 else define('_PHPWIKI_STDLIB_LOADED', true);
109
110 if (!defined('MAX_PAGENAME_LENGTH')) {
111     define('MAX_PAGENAME_LENGTH', 100);
112 }
113
114 /**
115  * Convert string to a valid XML identifier.
116  *
117  * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
118  *
119  * We would like to have, e.g. named anchors within wiki pages
120  * names like "Table of Contents" --- clearly not a valid XML
121  * fragment identifier.
122  *
123  * This function implements a one-to-one map from {any string}
124  * to {valid XML identifiers}.
125  *
126  * It does this by
127  * converting all bytes not in [A-Za-z0-9:_-],
128  * and any leading byte not in [A-Za-z] to 'xbb.',
129  * where 'bb' is the hexadecimal representation of the
130  * character.
131  *
132  * As a special case, the empty string is converted to 'empty.'
133  *
134  * @param string $str
135  * @return string
136  */
137 function MangleXmlIdentifier($str)
138 {
139     if (!$str)
140         return 'empty.';
141
142     return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
143         "'x' . sprintf('%02x', ord('\\0')) . '.'",
144         $str);
145 }
146
147 /**
148  * Returns a name for the WIKI_ID cookie that should be unique on the host.
149  * But for it to be unique you must have set a unique WIKI_NAME in your
150  * configuration file.
151  * @return string The name of the WIKI_ID cookie to use for this wiki.
152  */
153 function getCookieName()
154 {
155     return preg_replace("/[^\d\w]/", "_", WIKI_NAME) . "_WIKI_ID";
156 }
157
158 /**
159  * Generates a valid URL for a given Wiki pagename.
160  * @param mixed $pagename If a string this will be the name of the Wiki page to link to.
161  *               If a WikiDB_Page object function will extract the name to link to.
162  *               If a WikiDB_PageRevision object function will extract the name to link to.
163  * @param array $args
164  * @param boolean $get_abs_url Default value is false.
165  * @return string The absolute URL to the page passed as $pagename.
166  */
167 function WikiURL($pagename, $args = '', $get_abs_url = false)
168 {
169     global $request, $WikiTheme;
170     $anchor = false;
171
172     if (is_object($pagename)) {
173         if (isa($pagename, 'WikiDB_Page')) {
174             $pagename = $pagename->getName();
175         } elseif (isa($pagename, 'WikiDB_PageRevision')) {
176             $page = $pagename->getPage();
177             $args['version'] = $pagename->getVersion();
178             $pagename = $page->getName();
179         } elseif (isa($pagename, 'WikiPageName')) {
180             $anchor = $pagename->anchor;
181             $pagename = $pagename->name;
182         } else { // php5
183             $anchor = $pagename->anchor;
184             $pagename = $pagename->name;
185         }
186     }
187     if (!$get_abs_url and DEBUG and $request->getArg('start_debug')) {
188         if (!$args)
189             $args = 'start_debug=' . $request->getArg('start_debug');
190         elseif (is_array($args))
191             $args['start_debug'] = $request->getArg('start_debug'); else
192             $args .= '&start_debug=' . $request->getArg('start_debug');
193     }
194     if (is_array($args)) {
195         $enc_args = array();
196         foreach ($args as $key => $val) {
197             // avoid default args
198             if (USE_PATH_INFO and $key == 'pagename')
199                 ;
200             elseif ($key == 'action' and $val == 'browse')
201                 ; elseif (!is_array($val)) // ugly hack for getURLtoSelf() which also takes POST vars
202                 $enc_args[] = urlencode($key) . '=' . urlencode($val);
203         }
204         $args = join('&', $enc_args);
205     }
206
207     if (USE_PATH_INFO or !empty($WikiTheme->HTML_DUMP_SUFFIX)) {
208         $url = $get_abs_url ? (SERVER_URL . VIRTUAL_PATH . "/") : "";
209         $base = preg_replace('/%2f/i', '/', rawurlencode($pagename));
210         $url .= $base;
211         if (!empty($WikiTheme->HTML_DUMP_SUFFIX)) {
212             if (!empty($WikiTheme->VALID_LINKS) and $request->getArg('action') == 'pdf') {
213                 if (!in_array($pagename, $WikiTheme->VALID_LINKS))
214                     $url = '';
215                 else
216                     $url = $base . $WikiTheme->HTML_DUMP_SUFFIX;
217             } else {
218                 $url .= $WikiTheme->HTML_DUMP_SUFFIX;
219                 if ($args)
220                     $url .= "?$args";
221             }
222         } else {
223             if ($args)
224                 $url .= "?$args";
225         }
226     } else {
227         $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
228         $url .= "?pagename=" . rawurlencode($pagename);
229         if ($args)
230             $url .= "&$args";
231     }
232     if ($anchor)
233         $url .= "#" . MangleXmlIdentifier($anchor);
234     return $url;
235 }
236
237 /** Convert relative URL to absolute URL.
238  *
239  * This converts a relative URL to one of PhpWiki's support files
240  * to an absolute one.
241  *
242  * @param string $url
243  * @return string Absolute URL
244  */
245 function AbsoluteURL($url)
246 {
247     if (preg_match('/^https?:/', $url))
248         return $url;
249     if ($url[0] != '/') {
250         $base = USE_PATH_INFO ? VIRTUAL_PATH : dirname(SCRIPT_NAME);
251         while ($base != '/' and substr($url, 0, 3) == "../") {
252             $url = substr($url, 3);
253             $base = dirname($base);
254         }
255         if ($base != '/')
256             $base .= '/';
257         $url = $base . $url;
258     }
259     return SERVER_URL . $url;
260 }
261
262 function DataURL($url)
263 {
264     if (preg_match('/^https?:/', $url))
265         return $url;
266     $url = NormalizeWebFileName($url);
267     if (DEBUG and $GLOBALS['request']->getArg('start_debug') and substr($url, -4, 4) == '.php')
268         $url .= "?start_debug=1"; // XMLRPC and SOAP debugging helper.
269     return AbsoluteURL($url);
270 }
271
272 /**
273  * Generates icon in front of links.
274  *
275  * @param string $protocol_or_url URL or protocol to determine which icon to use.
276  *
277  * @return HtmlElement HtmlElement object that contains data to create img link to
278  * icon for use with url or protocol passed to the function. False if no img to be
279  * displayed.
280  */
281 function IconForLink($protocol_or_url)
282 {
283     global $WikiTheme;
284     if (0 and $filename_suffix == false) {
285         // display apache style icon for file type instead of protocol icon
286         // - archive: unix:gz,bz2,tgz,tar,z; mac:dmg,dmgz,bin,img,cpt,sit; pc:zip;
287         // - document: html, htm, text, txt, rtf, pdf, doc
288         // - non-inlined image: jpg,jpeg,png,gif,tiff,tif,swf,pict,psd,eps,ps
289         // - audio: mp3,mp2,aiff,aif,au
290         // - multimedia: mpeg,mpg,mov,qt
291     } else {
292         list ($proto) = explode(':', $protocol_or_url, 2);
293         $src = $WikiTheme->getLinkIconURL($proto);
294         if ($src)
295             return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon'));
296         else
297             return false;
298     }
299 }
300
301 /**
302  * Glue icon in front of or after text.
303  * Pref: 'noLinkIcons'      - ignore icon if set
304  * WikiTheme: 'LinkIcons'   - 'yes'   at front
305  *                          - 'no'    display no icon
306  *                          - 'front' display at left
307  *                          - 'after' display at right
308  *
309  * @param string $protocol_or_url Protocol or URL.  Used to determine the
310  * proper icon.
311  * @param string $text The text.
312  * @return XmlContent.
313  */
314 function PossiblyGlueIconToText($proto_or_url, $text)
315 {
316     global $request, $WikiTheme;
317     if ($request->getPref('noLinkIcons'))
318         return $text;
319     $icon = IconForLink($proto_or_url);
320     if (!$icon)
321         return $text;
322     if ($where = $WikiTheme->getLinkIconAttr()) {
323         if ($where == 'no') return $text;
324         if ($where != 'after') $where = 'front';
325     } else {
326         $where = 'front';
327     }
328     if ($where == 'after') {
329         // span the icon only to the last word (tie them together),
330         // to let the previous words wrap on line breaks.
331         if (!is_object($text)) {
332             preg_match('/^(\s*\S*)(\s*)$/', $text, $m);
333             list (, $prefix, $last_word) = $m;
334         } else {
335             $last_word = $text;
336             $prefix = false;
337         }
338         $text = HTML::span(array('style' => 'white-space: nowrap'),
339             $last_word, HTML::Raw('&nbsp;'), $icon);
340         if ($prefix)
341             $text = HTML($prefix, $text);
342         return $text;
343     }
344     // span the icon only to the first word (tie them together),
345     // to let the next words wrap on line breaks
346     if (!is_object($text)) {
347         preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
348         list (, $first_word, $tail) = $m;
349     } else {
350         $first_word = $text;
351         $tail = false;
352     }
353     $text = HTML::span(array('style' => 'white-space: nowrap'),
354         $icon, $first_word);
355     if ($tail)
356         $text = HTML($text, $tail);
357     return $text;
358 }
359
360 /**
361  * Determines if the url passed to function is safe, by detecting if the characters
362  * '<', '>', or '"' are present.
363  * Check against their urlencoded values also.
364  *
365  * @param string $url URL to check for unsafe characters.
366  * @return boolean True if same, false else.
367  */
368 function IsSafeURL($url)
369 {
370     return !preg_match('/([<>"])|(%3C)|(%3E)|(%22)/', $url);
371 }
372
373 /**
374  * Generates an HtmlElement object to store data for a link.
375  *
376  * @param string $url URL that the link will point to.
377  * @param string $linktext Text to be displayed as link.
378  * @return HtmlElement HtmlElement object that contains data to construct an html link.
379  */
380 function LinkURL($url, $linktext = '')
381 {
382     // FIXME: Is this needed (or sufficient?)
383     if (!IsSafeURL($url)) {
384         $link = HTML::span(array('class' => 'error'), _('Bad URL -- remove all of <, >, "'));
385         return $link;
386     } else {
387         if (!$linktext)
388             $linktext = preg_replace("/mailto:/A", "", $url);
389         $args = array('href' => $url);
390         if (defined('EXTERNAL_LINK_TARGET')) // can also be set in the css
391             $args['target'] = (is_string(EXTERNAL_LINK_TARGET) and (EXTERNAL_LINK_TARGET != "")) ? EXTERNAL_LINK_TARGET : "_blank";
392         $link = HTML::a($args, PossiblyGlueIconToText($url, $linktext));
393     }
394     $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
395     return $link;
396 }
397
398 /**
399  * Inline Images
400  *
401  * Syntax: [image.png size=50% border=n align= hspace= vspace= width= height=]
402  * Disallows sizes which are too small.
403  * Spammers may use such (typically invisible) image attributes to raise their GoogleRank.
404  *
405  * Handle embeddable objects, like svg, class, vrml, swf, svgz, pdf, avi, wmv especially.
406  */
407 function LinkImage($url, $alt = "")
408 {
409     $force_img = "png|jpg|gif|jpeg|bmp|pl|cgi";
410     // Disallow tags in img src urls. Typical CSS attacks.
411     // FIXME: Is this needed (or sufficient?)
412     // FIXED: This was broken for moniker:TP30 test/image.png => url="moniker:TP30" attr="test/image.png"
413     $ori_url = $url;
414     // support new syntax: [prefix/image.jpg size=50% border=n]
415     if (empty($alt)) {
416         $alt = "";
417     }
418     // Extract URL
419     $arr = explode(' ', $url);
420     if (!empty($arr)) $url = $arr[0];
421     if (!IsSafeURL($url)) {
422         $link = HTML::span(array('class' => 'error'), _('Bad URL for image -- remove all of <, >, "'));
423         return $link;
424     }
425     // spaces in inline images must be %20 encoded!
426     $link = HTML::img(array('src' => $url));
427
428     // Extract attributes
429     $arr = parse_attributes(strstr($ori_url, " "));
430     foreach ($arr as $attr => $value) {
431         // These attributes take strings: lang, id, title, alt
432         if (($attr == "lang")
433             || ($attr == "id")
434             || ($attr == "title")
435             || ($attr == "alt")
436         ) {
437             $link->setAttr($attr, $value);
438         } // align = bottom|middle|top|left|right
439         // we allow "center" as synonym for "middle"
440         elseif (($attr == "align")
441             && (($value == "bottom")
442                 || ($value == "middle")
443                 || ($value == "center")
444                 || ($value == "top")
445                 || ($value == "left")
446                 || ($value == "right"))
447         ) {
448             if ($value == "center") {
449                 $value = "middle";
450             }
451             $link->setAttr($attr, $value);
452         } // These attributes take a number (pixels): border, hspace, vspace
453         elseif ((($attr == "border") || ($attr == "hspace") || ($attr == "vspace"))
454             && (is_numeric($value))
455         ) {
456             $link->setAttr($attr, (int)$value);
457         } // These attributes take a number (pixels) or a percentage: height, width
458         elseif ((($attr == "height") || ($attr == "width"))
459             && (preg_match('/\d+[%p]?x?/', $value))
460         ) {
461             $link->setAttr($attr, $value);
462         } // We allow size=50% and size=20x30
463         // We replace this with "width" and "height" HTML attributes
464         elseif ($attr == "size") {
465             if (preg_match('/(\d+%)/', $value, $m)) {
466                 $link->setAttr('width', $m[1]);
467                 $link->setAttr('height', $m[1]);
468             } elseif (preg_match('/(\d+)x(\d+)/', $value, $m)) {
469                 $link->setAttr('width', $m[1]);
470                 $link->setAttr('height', $m[2]);
471             }
472         } else {
473             $url = substr(strrchr($ori_url, "/"), 1);
474             $link = HTML::span(array('class' => 'error'),
475                 sprintf(_("Invalid attribute %s=%s for image %s"),
476                     $attr, $value, $url));
477             return $link;
478         }
479     }
480     // Correct silently the most common error
481     if ($url != $ori_url and empty($arr) and !preg_match("/^http/", $url)) {
482         // space belongs to the path
483         $file = NormalizeLocalFileName($ori_url);
484         if (file_exists($file)) {
485             $link = HTML::img(array('src' => $ori_url));
486             trigger_error(
487                 sprintf(_("Invalid image link fixed %s => %s. Spaces must be quoted with %%20."),
488                     $url, $ori_url), E_USER_WARNING);
489         } elseif (string_starts_with($ori_url, getUploadDataPath())) {
490             $file = substr($file, strlen(getUploadDataPath()));
491             $path = getUploadFilePath() . $file;
492             if (file_exists($path)) {
493                 trigger_error(sprintf(_("Invalid image link fixed \"%s\" => \"%s\".\n Spaces must be quoted with %%20."),
494                     $url, $ori_url), E_USER_WARNING);
495                 $link->setAttr('src', getUploadDataPath() . $file);
496                 $url = $ori_url;
497             }
498         }
499     }
500     if (!$link->getAttr('alt')) {
501         $link->setAttr('alt', $alt);
502     }
503     // Check width and height as spam countermeasure
504     if (($width = $link->getAttr('width')) and ($height = $link->getAttr('height'))) {
505         //$width  = (int) $width; // px or % or other suffix
506         //$height = (int) $height;
507         if (($width < 3 and $height < 10) or
508             ($height < 3 and $width < 20) or
509             ($height < 7 and $width < 7)
510         ) {
511             $link = HTML::span(array('class' => 'error'),
512                 _("Invalid image size"));
513             return $link;
514         }
515     } else {
516         $size = 0;
517         // Prepare for getimagesize($url)
518         // $url only valid for external urls, otherwise local path
519         if (DISABLE_GETIMAGESIZE)
520             ;
521         elseif (!preg_match("/\.$force_img$/i", $url))
522             ; // only valid image extensions or scripts assumed to generate images
523         elseif (preg_match("/^http/", $url)) { // external url
524             $size = @getimagesize($url);
525         } else { // local file
526             if (file_exists($file = NormalizeLocalFileName($url))) { // here
527                 $size = @getimagesize($file);
528             } elseif (file_exists(NormalizeLocalFileName(urldecode($url)))) {
529                 $size = @getimagesize($file);
530                 $link->setAttr('src', rawurldecode($url));
531             } elseif (string_starts_with($url, getUploadDataPath())) { // there
532                 $file = substr($file, strlen(getUploadDataPath()));
533                 $path = getUploadFilePath() . rawurldecode($file);
534                 $size = @getimagesize($path);
535                 $link->setAttr('src', getUploadDataPath() . rawurldecode($file));
536             } else { // elsewhere
537                 global $request;
538                 $size = @getimagesize($request->get('DOCUMENT_ROOT') . urldecode($url));
539             }
540         }
541         if ($size) {
542             $width = $size[0];
543             $height = $size[1];
544             if (($width < 3 and $height < 10)
545                 or ($height < 3 and $width < 20)
546                 or ($height < 7 and $width < 7)
547             ) {
548                 $link = HTML::span(array('class' => 'error'),
549                     _("Invalid image size"));
550                 return $link;
551             }
552         }
553     }
554     $link->setAttr('class', 'inlineimage');
555
556     /* Check for inlined objects. Everything allowed in INLINE_IMAGES besides
557      * png|jpg|gif|jpeg|bmp|pl|cgi.  If no image it is an object to embed.
558      * Note: Allow cgi's (pl,cgi) returning images.
559      */
560     if (!preg_match("/\.(" . $force_img . ")/i", $ori_url)) {
561         // HTML::img(array('src' => $url, 'alt' => $alt, 'title' => $alt));
562         // => HTML::object(array('src' => $url)) ...;
563         return ImgObject($link, $ori_url);
564     }
565     return $link;
566 }
567
568 /**
569  * <object> / <embed> tags instead of <img> for all non-image extensions
570  * in INLINE_IMAGES.
571  * Called by LinkImage(), not directly.
572  * Syntax:  [image.svg size=50% alt=image.gif border=n align= hspace= vspace= width= height=]
573  * Samples: [Upload:song.mp3 type=audio/mpeg width=200 height=10]
574  *   $alt may be an alternate img
575  * TODO: Need to unify with WikiPluginCached::embedObject()
576  *
577  * Note that Safari 1.0 will crash with <object>, so use only <embed>
578  *   http://www.alleged.org.uk/pdc/2002/svg-object.html
579  *
580  * Allowed object tags:
581  *   ID
582  *   DATA=URI (object data)
583  *   CLASSID=URI (location of implementation)
584  *   ARCHIVE=CDATA (archive files)
585  *   CODEBASE=URI (base URI for CLASSID, DATA, ARCHIVE)
586  *   WIDTH=Length (object width)
587  *   HEIGHT=Length (object height)
588  *   NAME=CDATA (name for form submission)
589  *   USEMAP=URI (client-side image map)
590  *   TYPE=ContentType (content-type of object)
591  *   CODETYPE=ContentType (content-type of code)
592  *   STANDBY=Text (message to show while loading)
593  *   TABINDEX=NUMBER (position in tabbing order)
594  *   DECLARE (do not instantiate object)
595  * The rest is added as <param name="" value="" /> tags
596  */
597 function ImgObject($img, $url)
598 {
599     // get the url args: data="sample.svgz" type="image/svg+xml" width="400" height="300"
600     $params = explode(",", "id,width,height,data,classid,archive,codebase,name,usemap,type," .
601         "codetype,standby,tabindex,declare");
602     if (is_array($url)) {
603         $args = $url;
604         $found = array();
605         foreach ($args as $attr => $value) {
606             foreach ($params as $param) {
607                 if ($param == $attr) {
608                     $img->setAttr($param, $value);
609                     if (isset($found[$param])) $found[$param]++;
610                     else $found[$param] = 1;
611                     break;
612                 }
613             }
614         }
615         // now all remaining args are added as <param> to the object
616         $params = array();
617         foreach ($args as $attr => $value) {
618             if (!isset($found[$attr])) {
619                 $params[] = HTML::param(array('name' => $attr,
620                     'value' => $value));
621             }
622         }
623         $url = $img->getAttr('src');
624         $force_img = "png|jpg|gif|jpeg|bmp";
625         if (!preg_match("/\.(" . $force_img . ")/i", $url)) {
626             $img->setAttr('src', false);
627         }
628     } else {
629         $args = explode(' ', $url);
630         if (count($args) >= 1) {
631             $url = array_shift($args);
632             $found = array();
633             foreach ($args as $attr) {
634                 foreach ($params as $param) {
635                     if (preg_match("/^$param=(\S+)$/i", $attr, $m)) {
636                         $img->setAttr($param, $m[1]);
637                         if (isset($found[$param])) $found[$param]++;
638                         else $found[$param] = 1;
639                         break;
640                     }
641                 }
642             }
643             // now all remaining args are added as <param> to the object
644             $params = array();
645             foreach ($args as $attr) {
646                 if (!isset($found[$attr]) and preg_match("/^(\S+)=(\S+)$/i", $attr, $m)) {
647                     $params[] = HTML::param(array('name' => $m[1],
648                         'value' => $m[2]));
649                 }
650             }
651         }
652     }
653     $type = $img->getAttr('type');
654     if (!$type) {
655         if (function_exists('mime_content_type') && file_exists($url)) {
656             $type = mime_content_type($url);
657         }
658     }
659     $object = HTML::object(array_merge($img->_attr,
660             array('type' => $type)), //'src' => $url
661         $img->_content);
662     $object->setAttr('class', 'inlineobject');
663     if ($params) {
664         foreach ($params as $param) $object->pushContent($param);
665     }
666     if (isBrowserSafari() and !isBrowserSafari(532)) { // recent chrome can do OBJECT
667         return HTML::embed($object->_attr, $object->_content);
668     }
669     $object->pushContent(HTML::embed($object->_attr));
670     return $object;
671 }
672
673 class Stack
674 {
675     function Stack()
676     {
677         $this->items = array();
678         $this->size = 0;
679     }
680
681     function push($item)
682     {
683         $this->items[$this->size] = $item;
684         $this->size++;
685         return true;
686     }
687
688     function pop()
689     {
690         if ($this->size == 0) {
691             return false; // stack is empty
692         }
693         $this->size--;
694         return $this->items[$this->size];
695     }
696
697     function cnt()
698     {
699         return $this->size;
700     }
701
702     function top()
703     {
704         if ($this->size)
705             return $this->items[$this->size - 1];
706         else
707             return '';
708     }
709
710 }
711
712 // end class definition
713
714 function SplitQueryArgs($query_args = '')
715 {
716     // FIXME: use the arg-seperator which might not be &
717     $split_args = explode('&', $query_args);
718     $args = array();
719     while (list($key, $val) = each($split_args))
720         if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
721             $args[$m[1]] = $m[2];
722     return $args;
723 }
724
725 function LinkPhpwikiURL($url, $text = '', $basepage = false)
726 {
727     $args = array();
728
729     if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m)) {
730         return HTML::span(array('class' => 'error'), _("BAD phpwiki: URL"));
731     }
732
733     if ($m[1])
734         $pagename = urldecode($m[1]);
735     $qargs = $m[2];
736
737     if (empty($pagename) &&
738         preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)
739     ) {
740         // Convert old style links (to not break diff links in
741         // RecentChanges).
742         $pagename = urldecode($m[2]);
743         $args = array("action" => $m[1]);
744     } else {
745         $args = SplitQueryArgs($qargs);
746     }
747
748     if (empty($pagename))
749         $pagename = $GLOBALS['request']->getArg('pagename');
750
751     if (isset($args['action']) && $args['action'] == 'browse')
752         unset($args['action']);
753
754     /*FIXME:
755       if (empty($args['action']))
756       $class = 'wikilink';
757       else if (is_safe_action($args['action']))
758       $class = 'wikiaction';
759     */
760     if (empty($args['action']) || is_safe_action($args['action']))
761         $class = 'wikiaction';
762     else {
763         // Don't allow administrative links on unlocked pages.
764         $dbi = $GLOBALS['request']->getDbh();
765         $page = $dbi->getPage($basepage ? $basepage : $pagename);
766         if (!$page->get('locked'))
767             return HTML::span(array('class' => 'wikiunsafe'),
768                 HTML::u(_("Lock page to enable link")));
769         $class = 'wikiadmin';
770     }
771
772     if (!$text)
773         $text = HTML::span(array('class' => 'rawurl'), $url);
774
775     $wikipage = new WikiPageName($pagename);
776     if (!$wikipage->isValid()) {
777         global $WikiTheme;
778         return $WikiTheme->linkBadWikiWord($wikipage, $url);
779     }
780
781     return HTML::a(array('href' => WikiURL($pagename, $args),
782             'class' => $class),
783         $text);
784 }
785
786 /**
787  * A class to assist in parsing wiki pagenames.
788  *
789  * Now with subpages and anchors, parsing and passing around
790  * pagenames is more complicated.  This should help.
791  */
792 class WikiPageName
793 {
794     /** Short name for page.
795      *
796      * This is the value of $name passed to the constructor.
797      * (For use, e.g. as a default label for links to the page.)
798      */
799     public $shortName;
800
801     /** The full page name.
802      *
803      * This is the full name of the page (without anchor).
804      */
805     public $name;
806
807     /** The anchor.
808      *
809      * This is the referenced anchor within the page, or the empty string.
810      */
811     public $anchor;
812
813     /** Constructor
814      *
815      * @param mixed $name Page name.
816      * WikiDB_Page, WikiDB_PageRevision, or string.
817      * This can be a relative subpage name (like '/SubPage'),
818      * or can be the empty string to refer to the $basename.
819      *
820      * @param string $anchor For links to anchors in page.
821      *
822      * @param mixed $basename Page name from which to interpret
823      * relative or other non-fully-specified page names.
824      */
825     function WikiPageName($name, $basename = false, $anchor = false)
826     {
827         if (is_string($name)) {
828             $this->shortName = $name;
829             if (strstr($name, ':')) {
830                 list($moniker, $shortName) = explode(":", $name, 2);
831                 $map = getInterwikiMap(); // allow overrides to custom maps
832                 if (isset($map->_map[$moniker])) {
833                     $url = $map->_map[$moniker];
834                     if (strstr($url, '%s'))
835                         $url = sprintf($url, $shortName);
836                     else
837                         $url .= $shortName;
838                     $this->url = $url;
839                     // expand Talk or User, but not to absolute urls!
840                     if (strstr($url, '//')) {
841                         if ($moniker == 'Talk')
842                             $name = $name . SUBPAGE_SEPARATOR . _("Discussion");
843                         elseif ($moniker == 'User')
844                             $name = $name;
845                     } else {
846                         $name = $url;
847                     }
848                     if (strstr($shortName, '?')) {
849                         list($shortName, $dummy) = explode("\?", $shortName, 2);
850                     }
851                     $this->shortName = $shortName;
852                 }
853             }
854             // FIXME: We should really fix the cause for "/PageName" in the WikiDB
855             if ($name == '' or $name[0] == SUBPAGE_SEPARATOR) {
856                 if ($basename)
857                     $name = $this->_pagename($basename) . $name;
858                 else {
859                     $name = $this->_normalize_bad_pagename($name);
860                     $this->shortName = $name;
861                 }
862             }
863         } else {
864             $name = $this->_pagename($name);
865             $this->shortName = $name;
866         }
867
868         $this->name = $this->_check($name);
869         $this->anchor = (string)$anchor;
870     }
871
872     function getName()
873     {
874         return $this->name;
875     }
876
877     function getParent()
878     {
879         $name = $this->name;
880         if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
881             return false;
882         return substr($name, 0, -strlen($tail));
883     }
884
885     function isValid($strict = false)
886     {
887         if ($strict)
888             return !isset($this->_errors);
889         return (is_string($this->name) and $this->name != '');
890     }
891
892     function getWarnings()
893     {
894         $warnings = array();
895         if (isset($this->_warnings))
896             $warnings = array_merge($warnings, $this->_warnings);
897         if (isset($this->_errors))
898             $warnings = array_merge($warnings, $this->_errors);
899         if (!$warnings)
900             return false;
901
902         return sprintf(_("“%s”: Bad page name: %s"),
903             $this->shortName, join(', ', $warnings));
904     }
905
906     function _pagename($page)
907     {
908         if (isa($page, 'WikiDB_Page'))
909             return $page->getName();
910         elseif (isa($page, 'WikiDB_PageRevision'))
911             return $page->getPageName(); elseif (isa($page, 'WikiPageName'))
912             return $page->name;
913         // '0' or e.g. '1984' should be allowed though
914         if (!is_string($page) and !is_integer($page)) {
915             trigger_error(sprintf("Non-string pagename “%s” (%s)(%s)",
916                     $page, gettype($page), get_class($page)),
917                 E_USER_NOTICE);
918         }
919         //assert(is_string($page));
920         return $page;
921     }
922
923     function _normalize_bad_pagename($name)
924     {
925         trigger_error("Bad pagename: " . $name, E_USER_WARNING);
926
927         // Punt...  You really shouldn't get here.
928         if (empty($name)) {
929             global $request;
930             return $request->getArg('pagename');
931         }
932         assert($name[0] == SUBPAGE_SEPARATOR);
933         $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
934         return substr($name, 1);
935     }
936
937     /**
938      * Compress internal white-space to single space character.
939      *
940      * This leads to problems with loading a foreign charset pagename,
941      * which cannot be deleted anymore, because unknown chars are compressed.
942      * So BEFORE importing a file _check must be done !!!
943      */
944     function _check($pagename)
945     {
946         // Compress internal white-space to single space character.
947         $pagename = preg_replace('/[\s\xa0]+/', ' ', $orig = $pagename);
948         if ($pagename != $orig)
949             $this->_warnings[] = _("White space converted to single space");
950
951         // Delete any control characters.
952         if (DATABASE_TYPE == 'cvs' or DATABASE_TYPE == 'file' or DATABASE_TYPE == 'flatfile') {
953             $pagename = preg_replace('/[\x00-\x1f\x7f\x80-\x9f]/', '', $orig = $pagename);
954             if ($pagename != $orig)
955                 $this->_errors[] = _("Control characters not allowed");
956         }
957
958         // Strip leading and trailing white-space.
959         $pagename = trim($pagename);
960
961         $orig = $pagename;
962         while ($pagename and $pagename[0] == SUBPAGE_SEPARATOR)
963             $pagename = substr($pagename, 1);
964         if ($pagename != $orig)
965             $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
966
967         // ";" is urlencoded, so safe from php arg-delim problems
968         /*if (strstr($pagename, ';')) {
969             $this->_warnings[] = _("';' is deprecated");
970             $pagename = str_replace(';', '', $pagename);
971         }*/
972
973         // not only for SQL, also to restrict url length
974         if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
975             $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
976             $this->_errors[] = _("Page name too long");
977         }
978
979         // disallow some chars only on file and cvs
980         if ((DATABASE_TYPE == 'cvs'
981             or DATABASE_TYPE == 'file'
982             or DATABASE_TYPE == 'flatfile')
983             and preg_match('/(:|\.\.)/', $pagename, $m)
984         ) {
985             $this->_warnings[] = sprintf(_("Illegal chars %s removed"), $m[1]);
986             $pagename = str_replace('..', '', $pagename);
987             $pagename = str_replace(':', '', $pagename);
988         }
989
990         return $pagename;
991     }
992 }
993
994 /**
995  * Convert old page markup to new-style markup.
996  *
997  * @param string $text Old-style wiki markup.
998  *
999  * @param string $markup_type
1000  * One of: <dl>
1001  * <dt><code>"block"</code>  <dd>Convert all markup.
1002  * <dt><code>"inline"</code> <dd>Convert only inline markup.
1003  * <dt><code>"links"</code>  <dd>Convert only link markup.
1004  * </dl>
1005  *
1006  * @return string New-style wiki markup.
1007  *
1008  * @bugs Footnotes don't work quite as before (esp if there are
1009  *   multiple references to the same footnote.  But close enough,
1010  *   probably for now....
1011  * @bugs  Apache2 and IIS crash with OldTextFormattingRules or
1012  *   AnciennesR%E8glesDeFormatage. (at the 2nd attempt to do the anchored block regex)
1013  *   It only crashes with CreateToc so far, but other pages (not in pgsrc) are
1014  *   also known to crash, even with Apache1.
1015  */
1016 function ConvertOldMarkup($text, $markup_type = "block")
1017 {
1018
1019     static $subs;
1020     static $block_re;
1021
1022     // FIXME:
1023     // Trying to detect why the 2nd paragraph of OldTextFormattingRules or
1024     // AnciennesR%E8glesDeFormatage crashes.
1025     // It only crashes with CreateToc so far, but other pages (not in pgsrc) are
1026     // also known to crash, even with Apache1.
1027     $debug_skip = false;
1028     // I suspect this only to crash with Apache2 and IIS.
1029     if (in_array(php_sapi_name(), array('apache2handler', 'apache2filter', 'isapi'))
1030         and preg_match("/plugin CreateToc/", $text)
1031     ) {
1032         trigger_error(_("The CreateTocPlugin is not yet old markup compatible! ")
1033             . _("Please remove the CreateToc line to be able to reformat this page to old markup. ")
1034             . _("Skipped."), E_USER_WARNING);
1035         $debug_skip = true;
1036         //if (!DEBUG) return $text;
1037         return $text;
1038     }
1039
1040     if (empty($subs)) {
1041         /*****************************************************************
1042          * Conversions for inline markup:
1043          */
1044
1045         // escape tilde's
1046         $orig[] = '/~/';
1047         $repl[] = '~~';
1048
1049         // escape escaped brackets
1050         $orig[] = '/\[\[/';
1051         $repl[] = '~[';
1052
1053         // change ! escapes to ~'s.
1054         global $WikiNameRegexp, $request;
1055         $bang_esc[] = "(?:" . ALLOWED_PROTOCOLS . "):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]";
1056         // before 4.3.9 pcre had a memory release bug, which might hit us here. so be safe.
1057         $map = getInterwikiMap();
1058         if ($map_regex = $map->getRegexp())
1059             $bang_esc[] = $map_regex . ":[^\\s.,;?()]+"; // FIXME: is this really needed?
1060         $bang_esc[] = $WikiNameRegexp;
1061         $orig[] = '/!((?:' . join(')|(', $bang_esc) . '))/';
1062         $repl[] = '~\\1';
1063
1064         $subs["links"] = array($orig, $repl);
1065
1066         // Temporarily URL-encode pairs of underscores in links to hide
1067         // them from the re for bold markup.
1068         $orig[] = '/\[[^\[\]]*?__[^\[\]]*?\]/e';
1069         $repl[] = 'str_replace(\'__\', \'%5F%5F\', \'\\0\')';
1070
1071         // Escape '<'s
1072         //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
1073         //$repl[] = '~<';
1074
1075         // Convert footnote references.
1076         $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
1077         $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
1078
1079         // Convert old style emphases to HTML style emphasis.
1080         $orig[] = '/__(.*?)__/';
1081         $repl[] = '<strong>\\1</strong>';
1082         $orig[] = "/''(.*?)''/";
1083         $repl[] = '<em>\\1</em>';
1084
1085         // Escape nestled markup.
1086         $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
1087         $repl[] = '~\\0';
1088
1089         // in old markup headings only allowed at beginning of line
1090         $orig[] = '/!/';
1091         $repl[] = '~!';
1092
1093         // Convert URL-encoded pairs of underscores in links back to
1094         // real underscores after bold markup has been converted.
1095         $orig = '/\[[^\[\]]*?%5F%5F[^\[\]]*?\]/e';
1096         $repl = 'str_replace(\'%5F%5F\', \'__\', \'\\0\')';
1097
1098         $subs["inline"] = array($orig, $repl);
1099
1100         /*****************************************************************
1101          * Patterns which match block markup constructs which take
1102          * special handling...
1103          */
1104
1105         // Indented blocks
1106         $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
1107         // Tables
1108         $blockpats[] = '\|(?:.*\n\|)*';
1109
1110         // List items
1111         $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
1112
1113         // Footnote definitions
1114         $blockpats[] = '\[\s*(\d+)\s*\]';
1115
1116         if (!$debug_skip) {
1117             // Plugins
1118             $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
1119         }
1120
1121         // Section Title
1122         $blockpats[] = '!{1,3}[^!]';
1123         /*
1124     removed .|\n in the anchor not to crash on /m because with /m "." already includes \n
1125     this breaks headings but it doesn't crash anymore (crash on non-cgi, non-cli only)
1126     */
1127         $block_re = ('/\A((?:.|\n)*?)(^(?:'
1128             . join("|", $blockpats)
1129             . ').*$)\n?/m');
1130
1131     }
1132
1133     if ($markup_type != "block") {
1134         list ($orig, $repl) = $subs[$markup_type];
1135         return preg_replace($orig, $repl, $text);
1136     } else {
1137         list ($orig, $repl) = $subs['inline'];
1138         $out = '';
1139         //FIXME:
1140         // php crashes here in the 2nd paragraph of OldTextFormattingRules,
1141         // AnciennesR%E8glesDeFormatage and more
1142         // See http://www.pcre.org/pcre.txt LIMITATIONS
1143         while (preg_match($block_re, $text, $m)) {
1144             $text = substr($text, strlen($m[0]));
1145             list (, $leading_text, $block) = $m;
1146             $suffix = "\n";
1147
1148             if (strchr(" \t", $block[0])) {
1149                 // Indented block
1150                 $prefix = "<pre>\n";
1151                 $suffix = "\n</pre>\n";
1152             } elseif ($block[0] == '|') {
1153                 // Old-style table
1154                 $prefix = "<?plugin OldStyleTable\n";
1155                 $suffix = "\n?>\n";
1156             } elseif (strchr("#*;", $block[0])) {
1157                 // Old-style list item
1158                 preg_match('/^([#*;]*)([*#]|;.*?:) */', $block, $m);
1159                 list (, $ind, $bullet) = $m;
1160                 $block = substr($block, strlen($m[0]));
1161
1162                 $indent = str_repeat('     ', strlen($ind));
1163                 if ($bullet[0] == ';') {
1164                     //$term = ltrim(substr($bullet, 1));
1165                     //return $indent . $term . "\n" . $indent . '     ';
1166                     $prefix = $ind . $bullet;
1167                 } else
1168                     $prefix = $indent . $bullet . ' ';
1169             } elseif ($block[0] == '[') {
1170                 // Footnote definition
1171                 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
1172                 $footnum = $m[1];
1173                 $block = substr($block, strlen($m[0]));
1174                 $prefix = "#[|ftnt_" . ${footnum} . "]~[[" . ${footnum} . "|#ftnt_ref_" . ${footnum} . "]~] ";
1175             } elseif ($block[0] == '<') {
1176                 // Plugin.
1177                 // HACK: no inline markup...
1178                 $prefix = $block;
1179                 $block = '';
1180             } elseif ($block[0] == '!') {
1181                 // Section heading
1182                 preg_match('/^!{1,3}/', $block, $m);
1183                 $prefix = $m[0];
1184                 $block = substr($block, strlen($m[0]));
1185             } else {
1186                 // AAck!
1187                 assert(0);
1188             }
1189             if ($leading_text) $leading_text = preg_replace($orig, $repl, $leading_text);
1190             if ($block) $block = preg_replace($orig, $repl, $block);
1191             $out .= $leading_text;
1192             $out .= $prefix;
1193             $out .= $block;
1194             $out .= $suffix;
1195         }
1196         return $out . preg_replace($orig, $repl, $text);
1197     }
1198 }
1199
1200 /**
1201  * Expand tabs in string.
1202  *
1203  * Converts all tabs to (the appropriate number of) spaces.
1204  *
1205  * @param string $str
1206  * @param integer $tab_width
1207  * @return string
1208  */
1209 function expand_tabs($str, $tab_width = 8)
1210 {
1211     $split = explode("\t", $str);
1212     $tail = array_pop($split);
1213     $expanded = "\n";
1214     foreach ($split as $hunk) {
1215         $expanded .= $hunk;
1216         $pos = strlen(strrchr($expanded, "\n")) - 1;
1217         $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
1218     }
1219     return substr($expanded, 1) . $tail;
1220 }
1221
1222 /**
1223  * Split WikiWords in page names.
1224  *
1225  * It has been deemed useful to split WikiWords (into "Wiki Words") in
1226  * places like page titles. This is rumored to help search engines
1227  * quite a bit.
1228  *
1229  * @param $page string The page name.
1230  *
1231  * @return string The split name.
1232  */
1233 function SplitPagename($page)
1234 {
1235
1236     if (preg_match("/\s/", $page))
1237         return $page; // Already split --- don't split any more.
1238
1239     // This algorithm is specialized for several languages.
1240     // (Thanks to Pierrick MEIGNEN)
1241     // Improvements for other languages welcome.
1242     static $RE;
1243     if (!isset($RE)) {
1244         // This mess splits between a lower-case letter followed by
1245         // either an upper-case or a numeral; except that it wont
1246         // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
1247         switch ($GLOBALS['LANG']) {
1248             case 'en':
1249             case 'it':
1250             case 'es':
1251             case 'de':
1252                 $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
1253                 break;
1254             case 'fr':
1255                 $RE[] = '/([[:lower:]])((?<!Mc|Di)[[:upper:]]|\d)/';
1256                 break;
1257         }
1258         $sep = preg_quote(SUBPAGE_SEPARATOR, '/');
1259         // This the single-letter words 'I' and 'A' from any following
1260         // capitalized words.
1261         switch ($GLOBALS['LANG']) {
1262             case 'en':
1263                 $RE[] = "/(?<= |${sep}|^)([AI])([[:upper:]][[:lower:]])/";
1264                 break;
1265             case 'fr':
1266                 $RE[] = "/(?<= |${sep}|^)([À])([[:upper:]][[:lower:]])/";
1267                 break;
1268         }
1269         // Split at underscore
1270         $RE[] = '/(_)([[:alpha:]])/';
1271         $RE[] = '/([[:alpha:]])(_)/';
1272         // Split numerals from following letters.
1273         $RE[] = '/(\d)([[:alpha:]])/';
1274         // Split at subpage seperators. TBD in WikiTheme.php
1275         $RE[] = "/([^${sep}]+)(${sep})/";
1276         $RE[] = "/(${sep})([^${sep}]+)/";
1277
1278         foreach ($RE as $key)
1279             $RE[$key] = $key;
1280     }
1281
1282     foreach ($RE as $regexp) {
1283         $page = preg_replace($regexp, '\\1 \\2', $page);
1284     }
1285     return $page;
1286 }
1287
1288 function NoSuchRevision(&$request, $page, $version)
1289 {
1290     $html = HTML(HTML::h2(_("Revision Not Found")),
1291         HTML::p(fmt("I'm sorry.  Version %d of %s is not in the database.",
1292             $version, WikiLink($page, 'auto'))));
1293     include_once 'lib/Template.php';
1294     GeneratePage($html, _("Bad Version"), $page->getCurrentRevision());
1295     $request->finish();
1296 }
1297
1298 /**
1299  * Get time offset for local time zone.
1300  *
1301  * @param $time time_t Get offset for this time. Default: now.
1302  * @param $no_colon boolean Don't put colon between hours and minutes.
1303  * @return string Offset as a string in the format +HH:MM.
1304  */
1305 function TimezoneOffset($time = false, $no_colon = false)
1306 {
1307     if ($time === false)
1308         $time = time();
1309     $secs = date('Z', $time);
1310
1311     if ($secs < 0) {
1312         $sign = '-';
1313         $secs = -$secs;
1314     } else {
1315         $sign = '+';
1316     }
1317     $colon = $no_colon ? '' : ':';
1318     $mins = intval(($secs + 30) / 60);
1319     return sprintf("%s%02d%s%02d",
1320         $sign, $mins / 60, $colon, $mins % 60);
1321 }
1322
1323 /**
1324  * Format time in ISO-8601 format.
1325  *
1326  * @param $time time_t Time.  Default: now.
1327  * @return string Date and time in ISO-8601 format.
1328  */
1329 function Iso8601DateTime($time = false)
1330 {
1331     if ($time === false)
1332         $time = time();
1333     $tzoff = TimezoneOffset($time);
1334     $date = date('Y-m-d', $time);
1335     $time = date('H:i:s', $time);
1336     return $date . 'T' . $time . $tzoff;
1337 }
1338
1339 /**
1340  * Format time in RFC-2822 format.
1341  *
1342  * @param $time time_t Time.  Default: now.
1343  * @return string Date and time in RFC-2822 format.
1344  */
1345 function Rfc2822DateTime($time = false)
1346 {
1347     if ($time === false)
1348         $time = time();
1349     return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
1350 }
1351
1352 /**
1353  * Format time in RFC-1123 format.
1354  *
1355  * @param $time time_t Time.  Default: now.
1356  * @return string Date and time in RFC-1123 format.
1357  */
1358 function Rfc1123DateTime($time = false)
1359 {
1360     if ($time === false)
1361         $time = time();
1362     return gmdate('D, d M Y H:i:s \G\M\T', $time);
1363 }
1364
1365 /** Parse date in RFC-1123 format.
1366  *
1367  * According to RFC 1123 we must accept dates in the following
1368  * formats:
1369  *
1370  *   Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
1371  *   Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
1372  *   Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
1373  *
1374  * (Though we're only allowed to generate dates in the first format.)
1375  */
1376 function ParseRfc1123DateTime($timestr)
1377 {
1378     $timestr = trim($timestr);
1379     if (preg_match('/^ \w{3},\s* (\d{1,2}) \s* (\w{3}) \s* (\d{4}) \s*'
1380             . '(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1381         $timestr, $m)
1382     ) {
1383         list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1384     } elseif (preg_match('/^ \w+,\s* (\d{1,2})-(\w{3})-(\d{2}|\d{4}) \s*'
1385             . '(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1386         $timestr, $m)
1387     ) {
1388         list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1389         if ($year < 70) $year += 2000;
1390         elseif ($year < 100) $year += 1900;
1391     } elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
1392             . '(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
1393         $timestr, $m)
1394     ) {
1395         list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
1396     } else {
1397         // Parse failed.
1398         return false;
1399     }
1400
1401     $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
1402     if ($time == -1)
1403         return false; // failed
1404     return $time;
1405 }
1406
1407 /**
1408  * Format time to standard 'ctime' format.
1409  *
1410  * @param $time time_t Time.  Default: now.
1411  * @return string Date and time.
1412  */
1413 function CTime($time = false)
1414 {
1415     if ($time === false)
1416         $time = time();
1417     return date("D M j H:i:s Y", $time);
1418 }
1419
1420 /**
1421  * Format number as kibibytes or bytes.
1422  * Short format is used for PageList
1423  * Long format is used in PageInfo
1424  *
1425  * @param $bytes       int.  Default: 0.
1426  * @param $longformat  bool. Default: false.
1427  * @return class FormattedText (XmlElement.php).
1428  */
1429 function ByteFormatter($bytes = 0, $longformat = false)
1430 {
1431     if ($bytes < 0)
1432         return fmt("-???");
1433     if ($bytes < 1024) {
1434         if (!$longformat)
1435             $size = fmt("%s B", $bytes);
1436         else
1437             $size = fmt("%s bytes", $bytes);
1438     } else {
1439         $kb = round($bytes / 1024, 1);
1440         if (!$longformat)
1441             $size = fmt("%s KiB", $kb);
1442         else
1443             $size = fmt("%s KiB (%s bytes)", $kb, $bytes);
1444     }
1445     return $size;
1446 }
1447
1448 /**
1449  * Internationalized printf.
1450  *
1451  * This is essentially the same as PHP's built-in printf
1452  * with the following exceptions:
1453  * <ol>
1454  * <li> It passes the format string through gettext().
1455  * <li> It supports the argument reordering extensions.
1456  * </ol>
1457  *
1458  * Example:
1459  *
1460  * In php code, use:
1461  * <pre>
1462  *    __printf("Differences between versions %s and %s of %s",
1463  *             $new_link, $old_link, $page_link);
1464  * </pre>
1465  *
1466  * Then in locale/po/de.po, one can reorder the printf arguments:
1467  *
1468  * <pre>
1469  *    msgid "Differences between %s and %s of %s."
1470  *    msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
1471  * </pre>
1472  *
1473  * (Note that while PHP tries to expand $vars within double-quotes,
1474  * the values in msgstr undergo no such expansion, so the '$'s
1475  * okay...)
1476  *
1477  * One shouldn't use reordered arguments in the default format string.
1478  * Backslashes in the default string would be necessary to escape the
1479  * '$'s, and they'll cause all kinds of trouble....
1480  */
1481 function __printf($fmt)
1482 {
1483     $args = func_get_args();
1484     array_shift($args);
1485     echo __vsprintf($fmt, $args);
1486 }
1487
1488 /**
1489  * Internationalized sprintf.
1490  *
1491  * This is essentially the same as PHP's built-in printf with the
1492  * following exceptions:
1493  *
1494  * <ol>
1495  * <li> It passes the format string through gettext().
1496  * <li> It supports the argument reordering extensions.
1497  * </ol>
1498  *
1499  * @see __printf
1500  */
1501 function __sprintf($fmt)
1502 {
1503     $args = func_get_args();
1504     array_shift($args);
1505     return __vsprintf($fmt, $args);
1506 }
1507
1508 /**
1509  * Internationalized vsprintf.
1510  *
1511  * This is essentially the same as PHP's built-in printf with the
1512  * following exceptions:
1513  *
1514  * <ol>
1515  * <li> It passes the format string through gettext().
1516  * <li> It supports the argument reordering extensions.
1517  * </ol>
1518  *
1519  * @see __printf
1520  */
1521 function __vsprintf($fmt, $args)
1522 {
1523     $fmt = gettext($fmt);
1524     // PHP's sprintf doesn't support variable with specifiers,
1525     // like sprintf("%*s", 10, "x"); --- so we won't either.
1526
1527     if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
1528         // Format string has '%2$s' style argument reordering.
1529         // PHP doesn't support this.
1530         if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
1531             // literal variable name substitution only to keep locale
1532             // strings uncluttered
1533             trigger_error(sprintf(_("Can't mix “%s” with “%s” type format strings"),
1534                 '%1\$s', '%s'), E_USER_WARNING); //php+locale error
1535
1536         $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
1537         $newargs = array();
1538
1539         // Reorder arguments appropriately.
1540         foreach ($m[1] as $argnum) {
1541             if ($argnum < 1 || $argnum > count($args))
1542                 trigger_error(sprintf(_("%s: argument index out of range"),
1543                     $argnum), E_USER_WARNING);
1544             $newargs[] = $args[$argnum - 1];
1545         }
1546         $args = $newargs;
1547     }
1548
1549     // Not all PHP's have vsprintf, so...
1550     array_unshift($args, $fmt);
1551     return call_user_func_array('sprintf', $args);
1552 }
1553
1554 function file_mtime($filename)
1555 {
1556     if ($stat = @stat($filename))
1557         return $stat[9];
1558     else
1559         return false;
1560 }
1561
1562 function sort_file_mtime($a, $b)
1563 {
1564     $ma = file_mtime($a);
1565     $mb = file_mtime($b);
1566     if (!$ma or !$mb or $ma == $mb) return 0;
1567     return ($ma > $mb) ? -1 : 1;
1568 }
1569
1570 class fileSet
1571 {
1572     /**
1573      * Build an array in $this->_fileList of files from $dirname.
1574      * Subdirectories are not traversed.
1575      *
1576      * (This was a function LoadDir in lib/loadsave.php)
1577      * See also http://www.php.net/manual/en/function.readdir.php
1578      */
1579     function getFiles($exclude = '', $sortby = '', $limit = '')
1580     {
1581         $list = $this->_fileList;
1582
1583         if ($sortby) {
1584             require_once 'lib/PageList.php';
1585             switch (Pagelist::sortby($sortby, 'db')) {
1586                 case 'pagename ASC':
1587                     break;
1588                 case 'pagename DESC':
1589                     $list = array_reverse($list);
1590                     break;
1591                 case 'mtime ASC':
1592                     usort($list, 'sort_file_mtime');
1593                     break;
1594                 case 'mtime DESC':
1595                     usort($list, 'sort_file_mtime');
1596                     $list = array_reverse($list);
1597                     break;
1598             }
1599         }
1600         if ($limit)
1601             return array_splice($list, 0, $limit);
1602         return $list;
1603     }
1604
1605     function _filenameSelector($filename)
1606     {
1607         if (!$this->_pattern)
1608             return true;
1609         else {
1610             if (!$this->_pcre_pattern)
1611                 $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1612             return preg_match('/' . $this->_pcre_pattern . ($this->_case ? '/' : '/i'),
1613                 $filename);
1614         }
1615     }
1616
1617     function fileSet($directory, $filepattern = false)
1618     {
1619         $this->_fileList = array();
1620         $this->_pattern = $filepattern;
1621         if ($filepattern) {
1622             $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1623         }
1624         $this->_case = !isWindows();
1625         $this->_pathsep = '/';
1626
1627         if (empty($directory)) {
1628             trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1629                 E_USER_NOTICE);
1630             return; // early return
1631         }
1632
1633         @ $dir_handle = opendir($dir = $directory);
1634         if (empty($dir_handle)) {
1635             trigger_error(sprintf(_("Unable to open directory “%s” for reading"),
1636                 $dir), E_USER_NOTICE);
1637             return; // early return
1638         }
1639
1640         while ($filename = readdir($dir_handle)) {
1641             if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1642                 continue;
1643             if ($this->_filenameSelector($filename)) {
1644                 array_push($this->_fileList, "$filename");
1645                 //trigger_error(sprintf(_("found file %s"), $filename),
1646                 //                      E_USER_NOTICE); //debugging
1647             }
1648         }
1649         closedir($dir_handle);
1650     }
1651 }
1652
1653 // File globbing
1654
1655 // expands a list containing regex's to its matching entries
1656 class ListRegexExpand
1657 {
1658     public $match, $list, $index, $case_sensitive;
1659     function ListRegexExpand(&$list, $match, $case_sensitive = true)
1660     {
1661         $this->match = $match;
1662         $this->list = &$list;
1663         $this->case_sensitive = $case_sensitive;
1664         //$this->index = false;
1665     }
1666
1667     function listMatchCallback($item, $key)
1668     {
1669         $quoted = str_replace('/', '\/', $item);
1670         if (preg_match('/' . $this->match . ($this->case_sensitive ? '/' : '/i'),
1671             $quoted)
1672         ) {
1673             unset($this->list[$this->index]);
1674             $this->list[] = $item;
1675         }
1676     }
1677
1678     function expandRegex($index, &$pages)
1679     {
1680         $this->index = $index;
1681         array_walk($pages, array($this, 'listMatchCallback'));
1682         return $this->list;
1683     }
1684 }
1685
1686 // Convert fileglob to regex style:
1687 // Convert some wildcards to pcre style, escape the rest
1688 // Escape . \\ + * ? [ ^ ] $ ( ) { } = ! < > | : /
1689 // Fixed bug #994994: "/" in $glob.
1690 function glob_to_pcre($glob)
1691 {
1692     // check simple case: no need to escape
1693     $escape = '\[](){}=!<>|:/';
1694     if (strcspn($glob, $escape . ".+*?^$") == strlen($glob))
1695         return $glob;
1696     // preg_replace cannot handle "\\\\\\2" so convert \\ to \xff
1697     $glob = strtr($glob, "\\", "\xff");
1698     $glob = str_replace("/", "\\/", $glob);
1699     // first convert some unescaped expressions to pcre style: . => \.
1700     $special = '.^$';
1701     $re = preg_replace('/([^\xff])?([' . preg_quote($special) . '])/',
1702         "\\1\xff\\2", $glob);
1703
1704     // * => .*, ? => .
1705     $re = preg_replace('/([^\xff])?\*/', '$1.*', $re);
1706     $re = preg_replace('/([^\xff])?\?/', '$1.', $re);
1707     if (!preg_match('/^[\?\*]/', $glob))
1708         $re = '^' . $re;
1709     if (!preg_match('/[\?\*]$/', $glob))
1710         $re = $re . '$';
1711
1712     // Fixes Bug 1182997
1713     // .*? handled above, now escape the rest
1714     //while (strcspn($re, $escape) != strlen($re)) // loop strangely needed
1715     $re = preg_replace('/([^\xff])([' . preg_quote($escape, "/") . '])/',
1716         "\\1\xff\\2", $re);
1717     // Problem with 'Date/Time' => 'Date\/Time' => 'Date\xff\/Time' => 'Date\/Time'
1718     // 'plugin/*.php'
1719     $re = preg_replace('/\xff/', '', $re);
1720     return $re;
1721 }
1722
1723 function glob_match($glob, $against, $case_sensitive = true)
1724 {
1725     return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'),
1726         $against);
1727 }
1728
1729 function explodeList($input, $allnames, $glob_style = true, $case_sensitive = true)
1730 {
1731     $list = explode(',', $input);
1732     // expand wildcards from list of $allnames
1733     if (preg_match('/[\?\*]/', $input)) {
1734         // Optimizing loop invariants:
1735         // http://phplens.com/lens/php-book/optimizing-debugging-php.php
1736         for ($i = 0, $max = sizeof($list); $i < $max; $i++) {
1737             $f = $list[$i];
1738             if (preg_match('/[\?\*]/', $f)) {
1739                 reset($allnames);
1740                 $expand = new ListRegexExpand($list,
1741                     $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1742                 $expand->expandRegex($i, $allnames);
1743             }
1744         }
1745     }
1746     return $list;
1747 }
1748
1749 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1750 function explodePageList($input, $include_empty = false, $sortby = 'pagename',
1751                          $limit = '', $exclude = '')
1752 {
1753     include_once 'lib/PageList.php';
1754     return PageList::explodePageList($input, $include_empty, $sortby, $limit, $exclude);
1755 }
1756
1757 // Class introspections
1758
1759 /**
1760  * Determine whether object is of a specified type.
1761  * In PHP builtin since 4.2.0 as is_a()
1762  * is_a() deprecated in PHP 5, in favor of instanceof operator
1763  * @param $object object An object.
1764  * @param $class string Class name.
1765  * @return bool True iff $object is a $class
1766  * or a sub-type of $class.
1767  */
1768 function isa($object, $class)
1769 {
1770     $lclass = $class;
1771     return is_object($object)
1772         && (strtolower(get_class($object)) == strtolower($class)
1773             || is_subclass_of($object, $lclass));
1774 }
1775
1776 /** Determine whether a function is okay to use.
1777  *
1778  * Some providers (e.g. Lycos) disable some of PHP functions for
1779  * "security reasons."  This makes those functions, of course,
1780  * unusable, despite the fact the function_exists() says they
1781  * exist.
1782  *
1783  * This function test to see if a function exists and is not
1784  * disallowed by PHP's disable_functions config setting.
1785  *
1786  * @param string $function_name  Function name
1787  * @return bool  True iff function can be used.
1788  */
1789 function function_usable($function_name)
1790 {
1791     static $disabled;
1792     if (!is_array($disabled)) {
1793         $disabled = array();
1794         // Use get_cfg_var since ini_get() is one of the disabled functions
1795         // (on Lycos, at least.)
1796         $split = preg_split('/\s*,\s*/', trim(get_cfg_var('disable_functions')));
1797         foreach ($split as $f)
1798             $disabled[strtolower($f)] = true;
1799     }
1800
1801     return (function_exists($function_name)
1802         and !isset($disabled[strtolower($function_name)])
1803     );
1804 }
1805
1806 /** Hash a value.
1807  *
1808  * This is used for generating ETags.
1809  */
1810 function wikihash($x)
1811 {
1812     if (is_scalar($x)) {
1813         return $x;
1814     } elseif (is_array($x)) {
1815         ksort($x);
1816         return md5(serialize($x));
1817     } elseif (is_object($x)) {
1818         return $x->hash();
1819     }
1820     trigger_error("Can't hash $x", E_USER_ERROR);
1821 }
1822
1823 /**
1824  * Seed the random number generator.
1825  *
1826  * better_srand() ensures the randomizer is seeded only once.
1827  *
1828  * How random do you want it? See:
1829  * http://www.php.net/manual/en/function.srand.php
1830  * http://www.php.net/manual/en/function.mt-srand.php
1831  */
1832 function better_srand($seed = '')
1833 {
1834     static $wascalled = FALSE;
1835     if (!$wascalled) {
1836         $seed = $seed === '' ? (double)microtime() * 1000000 : $seed;
1837         function_exists('mt_srand') ? mt_srand($seed) : srand($seed);
1838         $wascalled = TRUE;
1839         //trigger_error("new random seed", E_USER_NOTICE); //debugging
1840     }
1841 }
1842
1843 function rand_ascii($length = 1)
1844 {
1845     better_srand();
1846     $s = "";
1847     for ($i = 1; $i <= $length; $i++) {
1848         // return only typeable 7 bit ascii, avoid quotes
1849         if (function_exists('mt_rand'))
1850             $s .= chr(mt_rand(40, 126));
1851         else
1852             // the usually bad glibc srand()
1853             $s .= chr(rand(40, 126));
1854     }
1855     return $s;
1856 }
1857
1858 /* by Dan Frankowski.
1859  */
1860 function rand_ascii_readable($length = 6)
1861 {
1862     // Pick a few random letters or numbers
1863     $word = "";
1864     better_srand();
1865     // Don't use 1lI0O, because they're hard to read
1866     $letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
1867     $letter_len = strlen($letters);
1868     for ($i = 0; $i < $length; $i++) {
1869         if (function_exists('mt_rand'))
1870             $word .= $letters[mt_rand(0, $letter_len - 1)];
1871         else
1872             $word .= $letters[rand(0, $letter_len - 1)];
1873     }
1874     return $word;
1875 }
1876
1877 /**
1878  * Recursively count all non-empty elements
1879  * in array of any dimension or mixed - i.e.
1880  * array('1' => 2, '2' => array('1' => 3, '2' => 4))
1881  * See http://www.php.net/manual/en/function.count.php
1882  */
1883 function count_all($arg)
1884 {
1885     // skip if argument is empty
1886     if ($arg) {
1887         //print_r($arg); //debugging
1888         $count = 0;
1889         // not an array, return 1 (base case)
1890         if (!is_array($arg))
1891             return 1;
1892         // else call recursively for all elements $arg
1893         foreach ($arg as $key => $val)
1894             $count += count_all($val);
1895         return $count;
1896     }
1897 }
1898
1899 function isSubPage($pagename)
1900 {
1901     return (strstr($pagename, SUBPAGE_SEPARATOR));
1902 }
1903
1904 function subPageSlice($pagename, $pos)
1905 {
1906     $pages = explode(SUBPAGE_SEPARATOR, $pagename);
1907     $pages = array_slice($pages, $pos, 1);
1908     return $pages[0];
1909 }
1910
1911 function isActionPage($filename)
1912 {
1913
1914     global $AllActionPages;
1915
1916     $localizedAllActionPages = array_map("__", $AllActionPages);
1917
1918     return (in_array($filename, $localizedAllActionPages));
1919 }
1920
1921 /**
1922  * Alert
1923  *
1924  * Class for "popping up" and alert box.  (Except that right now, it doesn't
1925  * pop up...)
1926  *
1927  * FIXME:
1928  * This is a hackish and needs to be refactored.  However it would be nice to
1929  * unify all the different methods we use for showing Alerts and Dialogs.
1930  * (E.g. "Page deleted", login form, ...)
1931  */
1932 class Alert
1933 {
1934     /** Constructor
1935      *
1936      * @param object $request
1937      * @param mixed  $head    Header ("title") for alert box.
1938      * @param mixed  $body    The text in the alert box.
1939      * @param hash   $buttons An array mapping button labels to URLs.
1940      *    The default is a single "Okay" button pointing to $request->getURLtoSelf().
1941      */
1942     function Alert($head, $body, $buttons = false)
1943     {
1944         if ($buttons === false)
1945             $buttons = array();
1946
1947         if (is_array($body)) {
1948             $html = HTML::ol();
1949             foreach ($body as $li) {
1950                 $html->pushContent(HTML::li($li));
1951             }
1952             $body = $html;
1953         }
1954         $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1955         $this->_buttons = $buttons;
1956     }
1957
1958     /**
1959      * Show the alert box.
1960      */
1961     function show()
1962     {
1963         global $request;
1964
1965         $tokens = $this->_tokens;
1966         $tokens['BUTTONS'] = $this->_getButtons();
1967
1968         $request->discardOutput();
1969         $tmpl = new Template('dialog', $request, $tokens);
1970         $tmpl->printXML();
1971         $request->finish();
1972     }
1973
1974     function _getButtons()
1975     {
1976         global $request;
1977
1978         $buttons = $this->_buttons;
1979         if (!$buttons)
1980             $buttons = array(_("OK") => $request->getURLtoSelf());
1981
1982         global $WikiTheme;
1983         foreach ($buttons as $label => $url)
1984             print "$label $url\n";
1985         $out[] = $WikiTheme->makeButton($label, $url, 'wikiaction');
1986         return new XmlContent($out);
1987     }
1988 }
1989
1990 // 1.3.8     => 1030.08
1991 // 1.3.9-p1  => 1030.091
1992 // 1.3.10pre => 1030.099
1993 // 1.3.11pre-20041120 => 1030.1120041120
1994 // 1.3.12-rc1 => 1030.119
1995 function phpwiki_version()
1996 {
1997     static $PHPWIKI_VERSION;
1998     if (!isset($PHPWIKI_VERSION)) {
1999         $arr = explode('.', preg_replace('/\D+$/', '', PHPWIKI_VERSION)); // remove the pre
2000         $arr[2] = preg_replace('/\.+/', '.', preg_replace('/\D/', '.', $arr[2]));
2001         $PHPWIKI_VERSION = $arr[0] * 1000 + $arr[1] * 10 + 0.01 * $arr[2];
2002         if (strstr(PHPWIKI_VERSION, 'pre') or strstr(PHPWIKI_VERSION, 'rc'))
2003             $PHPWIKI_VERSION -= 0.01;
2004     }
2005     return $PHPWIKI_VERSION;
2006 }
2007
2008 function phpwiki_gzhandler($ob)
2009 {
2010     if (function_exists('gzencode'))
2011         $ob = gzencode($ob);
2012     $GLOBALS['request']->_ob_get_length = strlen($ob);
2013     if (!headers_sent()) {
2014         header(sprintf("Content-Length: %d", $GLOBALS['request']->_ob_get_length));
2015     }
2016     return $ob;
2017 }
2018
2019 function isWikiWord($word)
2020 {
2021     global $WikiNameRegexp;
2022     //or preg_match('/\A' . $WikiNameRegexp . '\z/', $word) ??
2023     return preg_match("/^$WikiNameRegexp\$/", $word);
2024 }
2025
2026 // needed to store serialized objects-values only (perm, pref)
2027 function obj2hash($obj, $exclude = false, $fields = false)
2028 {
2029     $a = array();
2030     if (!$fields) $fields = get_object_vars($obj);
2031     foreach ($fields as $key => $val) {
2032         if (is_array($exclude)) {
2033             if (in_array($key, $exclude)) continue;
2034         }
2035         $a[$key] = $val;
2036     }
2037     return $a;
2038 }
2039
2040 /**
2041  * isAsciiString($string)
2042  */
2043 function isAsciiString($s)
2044 {
2045     $ptrASCII = '[\x00-\x7F]';
2046     return preg_match("/^($ptrASCII)*$/s", $s);
2047 }
2048
2049 /**
2050  * isUtf8String($string) - cheap utf-8 detection
2051  *
2052  * segfaults for strings longer than 10kb!
2053  * Use http://www.phpdiscuss.com/article.php?id=565&group=php.i18n or
2054  * checkTitleEncoding() at http://cvs.sourceforge.net/viewcvs.py/wikipedia/phase3/languages/Language.php
2055  */
2056 function isUtf8String($s)
2057 {
2058     $ptrASCII = '[\x00-\x7F]';
2059     $ptr2Octet = '[\xC2-\xDF][\x80-\xBF]';
2060     $ptr3Octet = '[\xE0-\xEF][\x80-\xBF]{2}';
2061     $ptr4Octet = '[\xF0-\xF4][\x80-\xBF]{3}';
2062     $ptr5Octet = '[\xF8-\xFB][\x80-\xBF]{4}';
2063     $ptr6Octet = '[\xFC-\xFD][\x80-\xBF]{5}';
2064     return preg_match("/^($ptrASCII|$ptr2Octet|$ptr3Octet|$ptr4Octet|$ptr5Octet|$ptr6Octet)*$/s", $s);
2065 }
2066
2067 /**
2068  * Check for UTF-8 URLs; Internet Explorer produces these if you
2069  * type non-ASCII chars in the URL bar or follow unescaped links.
2070  * Requires urldecoded pagename.
2071  * Fixes sf.net bug #953949
2072  *
2073  * src: languages/Language.php:checkTitleEncoding() from mediawiki
2074  */
2075 function fixTitleEncoding($s)
2076 {
2077     return $s;
2078 }
2079
2080 /**
2081  * Workaround for allow_url_fopen, to get the content of an external URI.
2082  * It returns the contents in one slurp. Parsers might want to check for allow_url_fopen
2083  * and use fopen, fread chunkwise. (see lib/XmlParser.php)
2084  */
2085 function url_get_contents($uri)
2086 {
2087     if (get_cfg_var('allow_url_fopen')) { // was ini_get('allow_url_fopen'))
2088         return @file_get_contents($uri);
2089     } else {
2090         require_once 'lib/HttpClient.php';
2091         $bits = parse_url($uri);
2092         $host = $bits['host'];
2093         $port = isset($bits['port']) ? $bits['port'] : 80;
2094         $path = isset($bits['path']) ? $bits['path'] : '/';
2095         if (isset($bits['query'])) {
2096             $path .= '?' . $bits['query'];
2097         }
2098         $client = new HttpClient($host, $port);
2099         $client->use_gzip = false;
2100         if (!$client->get($path)) {
2101             return false;
2102         } else {
2103             return $client->getContent();
2104         }
2105     }
2106 }
2107
2108 /**
2109  * Generate consecutively named strings:
2110  *   Name, Name2, Name3, ...
2111  */
2112 function GenerateId($name)
2113 {
2114     static $ids = array();
2115     if (empty($ids[$name])) {
2116         $ids[$name] = 1;
2117         return $name;
2118     } else {
2119         $ids[$name]++;
2120         return $name . $ids[$name];
2121     }
2122 }
2123
2124 // from IncludePage. To be of general use.
2125 // content: string or array of strings
2126 function firstNWordsOfContent($n, $content)
2127 {
2128     if ($content and $n > 0) {
2129         if (is_array($content)) {
2130             // fixme: return a list of lines then?
2131             //$content = join("\n", $content);
2132             //$return_array = true;
2133             $wordcount = 0;
2134             foreach ($content as $line) {
2135                 $words = explode(' ', $line);
2136                 if ($wordcount + count($words) > $n) {
2137                     $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
2138                         . sprintf(_("... (first %s words)"), $n);
2139                     return $new;
2140                 } else {
2141                     $wordcount += count($words);
2142                     $new[] = $line;
2143                 }
2144             }
2145             return $new;
2146         } else {
2147             // fixme: use better whitespace/word seperators
2148             $words = explode(' ', $content);
2149             if (count($words) > $n) {
2150                 return join(' ', array_slice($words, 0, $n))
2151                     . sprintf(_("... (first %s words)"), $n);
2152             } else {
2153                 return $content;
2154             }
2155         }
2156     } else {
2157         return '';
2158     }
2159 }
2160
2161 // moved from lib/plugin/IncludePage.php
2162 function extractSection($section, $content, $page, $quiet = false, $sectionhead = false)
2163 {
2164     $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
2165
2166     if (preg_match("/ ^(!{1,}|={2,})\\s*$qsection\s*=*" // section header
2167             . "  \\s*$\\n?" // possible blank lines
2168             . "  ( (?: ^.*\\n? )*? )" // some lines
2169             . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
2170         implode("\n", $content),
2171         $match)
2172     ) {
2173         // Strip trailing blanks lines and ---- <hr>s
2174         $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
2175         if ($sectionhead)
2176             $text = $match[1] . $section . "\n" . $text;
2177         return explode("\n", $text);
2178     }
2179     if ($quiet)
2180         $mesg = $page . " " . $section;
2181     else
2182         $mesg = $section;
2183     return array(sprintf(_("<%s: no such section>"), $mesg));
2184 }
2185
2186 // Extract the first $sections sections of the page
2187 function extractSections($sections, $content, $page, $quiet = false, $sectionhead = false)
2188 {
2189
2190     $mycontent = $content;
2191     $result = "";
2192
2193     while ($sections > 0) {
2194
2195         if (preg_match("/ ^(!{1,}|={2,})\\s*(.*)\\n" // section header
2196                 . "  \\s*$\\n?" // possible blank lines
2197                 . "  ( (?: ^.*\\n? )*? )" // some lines
2198                 . "  ( ^\\1 (.|\\n)* | \\Z)/xm", // sec header (same or higher level) (or EOF)
2199             implode("\n", $mycontent),
2200             $match)
2201         ) {
2202             $section = $match[2];
2203             // Strip trailing blanks lines and ---- <hr>s
2204             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[3]);
2205             if ($sectionhead)
2206                 $text = $match[1] . $section . "\n" . $text;
2207             $result .= $text;
2208
2209             $mycontent = explode("\n", $match[4]);
2210             $sections--;
2211             if ($sections === 0) {
2212                 return explode("\n", $result);
2213             }
2214         }
2215     }
2216 }
2217
2218 // use this faster version: only load ExternalReferrer if we came from an external referrer
2219 function isExternalReferrer(&$request)
2220 {
2221     if ($referrer = $request->get('HTTP_REFERER')) {
2222         $home = SERVER_URL; // SERVER_URL or SCRIPT_NAME, if we want to check sister wiki's also
2223         if (string_starts_with(strtolower($referrer), strtolower($home))) return false;
2224         require_once 'lib/ExternalReferrer.php';
2225         $se = new SearchEngines();
2226         return $se->parseSearchQuery($referrer);
2227     }
2228     //if (DEBUG) return array('query' => 'wiki');
2229     return false;
2230 }
2231
2232 /**
2233  * Useful for PECL overrides: cvsclient, ldap, soap, xmlrpc, pdo, pdo_<driver>
2234  */
2235 function loadPhpExtension($extension)
2236 {
2237     if (!extension_loaded($extension)) {
2238         $isWindows = (substr(PHP_OS, 0, 3) == 'WIN');
2239         $soname = ($isWindows ? 'php_' : '')
2240             . $extension
2241             . ($isWindows ? '.dll' : '.so');
2242         if (!@dl($soname))
2243             return false;
2244     }
2245     return extension_loaded($extension);
2246 }
2247
2248 function charset_convert($from, $to, $data)
2249 {
2250     if (strtolower($from) == 'utf-8' and strtolower($to) == 'iso-8859-1')
2251         return utf8_decode($data);
2252     if (strtolower($to) == 'utf-8' and strtolower($from) == 'iso-8859-1')
2253         return utf8_encode($data);
2254
2255     if (loadPhpExtension("iconv")) {
2256         $tmpdata = iconv($from, $to, $data);
2257         if (!$tmpdata)
2258             trigger_error("charset conversion $from => $to failed. Wrong source charset?", E_USER_WARNING);
2259         else
2260             $data = $tmpdata;
2261     } else {
2262         trigger_error("The iconv extension cannot be loaded", E_USER_WARNING);
2263     }
2264     return $data;
2265 }
2266
2267 function string_starts_with($string, $prefix)
2268 {
2269     return (substr($string, 0, strlen($prefix)) == $prefix);
2270 }
2271
2272 function string_ends_with($string, $suffix)
2273 {
2274     return (substr($string, -strlen($suffix)) == $suffix);
2275 }
2276
2277 function array_remove($arr, $value)
2278 {
2279     return array_values(array_diff($arr, array($value)));
2280 }
2281
2282 /**
2283  * Ensure that the script will have another $secs time left.
2284  * Works only if safe_mode is off.
2285  * For example not to timeout on waiting socket connections.
2286  *   Use the socket timeout as arg.
2287  */
2288 function longer_timeout($secs = 30)
2289 {
2290     $timeout = @ini_get("max_execution_time") ? ini_get("max_execution_time") : 30;
2291     $timeleft = $timeout - $GLOBALS['RUNTIMER']->getTime();
2292     if ($timeleft < $secs)
2293         @set_time_limit(max($timeout, (integer)($secs + $timeleft)));
2294 }
2295
2296 function printSimpleTrace($bt)
2297 {
2298     //print_r($bt);
2299     echo "\nTraceback:\n";
2300     if (function_exists('debug_print_backtrace')) { // >= 5
2301         debug_print_backtrace();
2302     } else {
2303         foreach ($bt as $i => $elem) {
2304             if (!array_key_exists('file', $elem)) {
2305                 continue;
2306             }
2307             //echo join(" ",array_values($elem)),"\n";
2308             echo "  ", $elem['file'], ':', $elem['line'], " ", $elem['function'], "\n";
2309         }
2310     }
2311 }
2312
2313 /**
2314  * Return the used process memory, in bytes.
2315  * Enable the section which will work for you. They are very slow.
2316  * Special quirks for Windows: Requires cygwin.
2317  */
2318 function getMemoryUsage()
2319 {
2320     //if (!(DEBUG & _DEBUG_VERBOSE)) return;
2321     if (function_exists('memory_get_usage') and memory_get_usage()) {
2322         return memory_get_usage();
2323     } elseif (function_exists('getrusage') and ($u = @getrusage()) and !empty($u['ru_maxrss'])) {
2324         $mem = $u['ru_maxrss'];
2325     } elseif (substr(PHP_OS, 0, 3) == 'WIN') { // may require a newer cygwin
2326         // what we want is the process memory only: apache or php (if CGI)
2327         $pid = getmypid();
2328         $memstr = '';
2329         // win32_ps_stat_proc, win32_ps_stat_mem
2330         if (function_exists('win32_ps_list_procs')) {
2331             $info = win32_ps_stat_proc($pid);
2332             $memstr = $info['mem']['working_set_size'];
2333         } elseif (0) {
2334             // This works only if it's a cygwin process (apache or php).
2335             // Requires a newer cygwin
2336             $memstr = exec("cat /proc/$pid/statm |cut -f1");
2337
2338             // if it's native windows use something like this:
2339             //   (requires pslist from sysinternals.com, grep, sed and perl)
2340             //$memstr = exec("pslist $pid|grep -A1 Mem|sed 1d|perl -ane\"print \$"."F[5]\"");
2341         }
2342         return (integer)trim($memstr);
2343     } elseif (1) {
2344         $pid = getmypid();
2345         //%MEM: Percentage of total memory in use by this process
2346         //VSZ: Total virtual memory size, in 1K blocks.
2347         //RSS: Real Set Size, the actual amount of physical memory allocated to this process.
2348         //CPU time used by process since it started.
2349         //echo "%",`ps -o%mem,vsz,rss,time -p $pid|sed 1d`,"\n";
2350         $memstr = exec("ps -orss -p $pid|sed 1d");
2351         return (integer)trim($memstr);
2352     }
2353 }
2354
2355 /**
2356  * @param var $needle
2357  * @param array $haystack one-dimensional numeric array only, no hash
2358  * @return integer
2359  * @desc Feed a sorted array to $haystack and a value to search for to $needle.
2360 It will return false if not found or the index where it was found.
2361 From dennis.decoene@moveit.be http://www.php.net/array_search
2362  */
2363 function binary_search($needle, $haystack)
2364 {
2365     $high = count($haystack);
2366     $low = 0;
2367
2368     while (($high - $low) > 1) {
2369         $probe = floor(($high + $low) / 2);
2370         if ($haystack[$probe] < $needle) {
2371             $low = $probe;
2372         } elseif ($haystack[$probe] == $needle) {
2373             $high = $low = $probe;
2374         } else {
2375             $high = $probe;
2376         }
2377     }
2378
2379     if ($high == count($haystack) || $haystack[$high] != $needle) {
2380         return false;
2381     } else {
2382         return $high;
2383     }
2384 }
2385
2386 function is_localhost()
2387 {
2388     return $_SERVER['SERVER_ADDR'] == '127.0.0.1';
2389 }
2390
2391 /**
2392  * Take a string and quote it sufficiently to be passed as a Javascript
2393  * string between ''s
2394  */
2395 function javascript_quote_string($s)
2396 {
2397     return str_replace("'", "\'", $s);
2398 }
2399
2400 function isSerialized($s)
2401 {
2402     return (!empty($s) and (strlen($s) > 3) and (substr($s, 1, 1) == ':'));
2403 }
2404
2405 /**
2406  * Determine if a variable represents a whole number
2407  */
2408
2409 function is_whole_number($var)
2410 {
2411     return (is_numeric($var) && (intval($var) == floatval($var)));
2412 }
2413
2414 /**
2415  * Take a string and return an array of pairs (attribute name, attribute value)
2416  *
2417  * We allow attributes with or without double quotes (")
2418  * Attribute-value pairs may be separated by space or comma
2419  * Space is normal HTML attributes, comma is for RichTable compatibility
2420  * border=1, cellpadding="5"
2421  * border=1 cellpadding="5"
2422  * style="font-family: sans-serif; border-top:1px solid #dddddd;"
2423  * style="font-family: Verdana, Arial, Helvetica, sans-serif"
2424  */
2425 function parse_attributes($line)
2426 {
2427
2428     $options = array();
2429
2430     if (empty($line)) return $options;
2431     $line = trim($line);
2432     if (empty($line)) return $options;
2433     $line = trim($line, ",");
2434     if (empty($line)) return $options;
2435
2436     // First we have an attribute name.
2437     $attribute = "";
2438     $value = "";
2439
2440     $i = 0;
2441     while (($i < strlen($line)) && ($line[$i] != '=')) {
2442         $i++;
2443     }
2444     $attribute = substr($line, 0, $i);
2445     $attribute = strtolower($attribute);
2446
2447     $line = substr($line, $i + 1);
2448     $line = trim($line);
2449     $line = trim($line, "=");
2450     $line = trim($line);
2451
2452     if (empty($line)) return $options;
2453
2454     // Then we have the attribute value.
2455
2456     $i = 0;
2457     // Attribute value might be between double quotes
2458     // In that case we have to find the closing double quote
2459     if ($line[0] == '"') {
2460         $i++; // skip first '"'
2461         while (($i < strlen($line)) && ($line[$i] != '"')) {
2462             $i++;
2463         }
2464         $value = substr($line, 0, $i);
2465         $value = trim($value, '"');
2466         $value = trim($value);
2467
2468         // If there are no double quotes, we have to find the next space or comma
2469     } else {
2470         while (($i < strlen($line)) && (($line[$i] != ' ') && ($line[$i] != ','))) {
2471             $i++;
2472         }
2473         $value = substr($line, 0, $i);
2474         $value = trim($value);
2475         $value = trim($value, ",");
2476         $value = trim($value);
2477     }
2478
2479     $options[$attribute] = $value;
2480
2481     $line = substr($line, $i + 1);
2482     $line = trim($line);
2483     $line = trim($line, ",");
2484     $line = trim($line);
2485
2486     return $options + parse_attributes($line);
2487 }
2488
2489 /**
2490  * Returns true if the filename ends with an image suffix.
2491  * Uses INLINE_IMAGES if defined, else "png|jpg|jpeg|gif|swf"
2492  */
2493 function is_image($filename)
2494 {
2495
2496     if (defined('INLINE_IMAGES')) {
2497         $inline_images = INLINE_IMAGES;
2498     } else {
2499         $inline_images = "png|jpg|jpeg|gif|swf";
2500     }
2501
2502     foreach (explode("|", $inline_images) as $suffix) {
2503         if (string_ends_with(strtolower($filename), "." . $suffix)) {
2504             return true;
2505         }
2506     }
2507     return false;
2508 }
2509
2510 /**
2511  * Returns true if the filename ends with an video suffix.
2512  * Currently only FLV and OGG
2513  */
2514 function is_video($filename)
2515 {
2516
2517     return string_ends_with(strtolower($filename), ".flv")
2518         or string_ends_with(strtolower($filename), ".ogg");
2519 }
2520
2521 /**
2522  * Remove accents from given text.
2523  */
2524 function strip_accents($text)
2525 {
2526     $res = utf8_decode($text);
2527     $res = strtr($res,
2528         utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
2529         'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
2530     return utf8_encode($res);
2531 }
2532
2533 // Local Variables:
2534 // mode: php
2535 // tab-width: 8
2536 // c-basic-offset: 4
2537 // c-hanging-comment-ender-p: nil
2538 // indent-tabs-mode: nil
2539 // End: