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