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