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