]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/stdlib.php
Fix to ensure absolute URL for logo in RSS recent changes.
[SourceForge/phpwiki.git] / lib / stdlib.php
1 <?php //rcs_id('$Id: stdlib.php,v 1.145 2003-03-04 01:55:05 dairiki Exp $');
2
3 /*
4   Standard functions for Wiki functionality
5     WikiURL($pagename, $args, $get_abs_url)
6     IconForLink($protocol_or_url)
7     LinkURL($url, $linktext)
8     LinkImage($url, $alt)
9
10     SplitQueryArgs ($query_args)
11     LinkPhpwikiURL($url, $text)
12     ConvertOldMarkup($content)
13     
14     class Stack { push($item), pop(), cnt(), top() }
15
16     split_pagename ($page)
17     NoSuchRevision ($request, $page, $version)
18     TimezoneOffset ($time, $no_colon)
19     Iso8601DateTime ($time)
20     Rfc2822DateTime ($time)
21     CTime ($time)
22     __printf ($fmt)
23     __sprintf ($fmt)
24     __vsprintf ($fmt, $args)
25     better_srand($seed = '')
26     count_all($arg)
27     isSubPage($pagename)
28     subPageSlice($pagename, $pos)
29     explodePageList($input, $perm = false)
30
31   function: LinkInterWikiLink($link, $linktext)
32   moved to: lib/interwiki.php
33   function: linkExistingWikiWord($wikiword, $linktext, $version)
34   moved to: lib/Theme.php
35   function: LinkUnknownWikiWord($wikiword, $linktext)
36   moved to: lib/Theme.php
37   function: UpdateRecentChanges($dbi, $pagename, $isnewpage) 
38   gone see: lib/plugin/RecentChanges.php
39 */
40
41 define('MAX_PAGENAME_LENGTH', 100);
42
43             
44 /**
45  * Convert string to a valid XML identifier.
46  *
47  * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
48  *
49  * We would like to have, e.g. named anchors within wiki pages
50  * names like "Table of Contents" --- clearly not a valid XML
51  * fragment identifier.
52  *
53  * This function implements a one-to-one map from {any string}
54  * to {valid XML identifiers}.
55  *
56  * It does this by
57  * converting all bytes not in [A-Za-z0-9:_-],
58  * and any leading byte not in [A-Za-z] to 'xbb.',
59  * where 'bb' is the hexadecimal representation of the
60  * character.
61  *
62  * As a special case, the empty string is converted to 'empty.'
63  *
64  * @param string $str
65  * @return string
66  */
67 function MangleXmlIdentifier($str) 
68 {
69     if (!$str)
70         return 'empty.';
71     
72     return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
73                         "'x' . sprintf('%02x', ord('\\0')) . '.'",
74                         $str);
75 }
76     
77
78 /**
79  * Generates a valid URL for a given Wiki pagename.
80  * @param mixed $pagename If a string this will be the name of the Wiki page to link to.
81  *                        If a WikiDB_Page object function will extract the name to link to.
82  *                        If a WikiDB_PageRevision object function will extract the name to link to.
83  * @param array $args 
84  * @param boolean $get_abs_url Default value is false.
85  * @return string The absolute URL to the page passed as $pagename.
86  */
87 function WikiURL($pagename, $args = '', $get_abs_url = false) {
88     $anchor = false;
89     
90     if (is_object($pagename)) {
91         if (isa($pagename, 'WikiDB_Page')) {
92             $pagename = $pagename->getName();
93         }
94         elseif (isa($pagename, 'WikiDB_PageRevision')) {
95             $page = $pagename->getPage();
96             $args['version'] = $pagename->getVersion();
97             $pagename = $page->getName();
98         }
99         elseif (isa($pagename, 'WikiPageName')) {
100             $anchor = $pagename->anchor;
101             $pagename = $pagename->name;
102         }
103     }
104     
105     if (is_array($args)) {
106         $enc_args = array();
107         foreach  ($args as $key => $val) {
108             if (!is_array($val)) // ugly hack for getURLtoSelf() which also takes POST vars
109               $enc_args[] = urlencode($key) . '=' . urlencode($val);
110         }
111         $args = join('&', $enc_args);
112     }
113
114     if (USE_PATH_INFO) {
115         $url = $get_abs_url ? SERVER_URL . VIRTUAL_PATH . "/" : "";
116         $url .= preg_replace('/%2f/i', '/', rawurlencode($pagename));
117         if ($args)
118             $url .= "?$args";
119     }
120     else {
121         $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
122         $url .= "?pagename=" . rawurlencode($pagename);
123         if ($args)
124             $url .= "&$args";
125     }
126     if ($anchor)
127         $url .= "#" . MangleXmlIdentifier($anchor);
128     return $url;
129 }
130
131 /** Convert relative URL to absolute URL.
132  *
133  * This converts a relative URL to one of PhpWiki's support files
134  * to an absolute one.
135  *
136  * @param string $url
137  * @return string Absolute URL
138  */
139 function AbsoluteURL ($url) {
140     if (preg_match('/^https?:/', $url))
141         return $url;
142     if ($url[0] != '/') {
143         $base = USE_PATH_INFO ? VIRTUAL_PATH : dirname(SCRIPT_NAME);
144         while ($base != '/' and substr($url, 0, 3) == "../") {
145             $url = substr($url, 3);
146             $base = dirname($base);
147         }
148         if ($base != '/')
149             $base .= '/';
150         $url = $base . $url;
151     }
152     return SERVER_URL . $url;
153 }
154
155 /**
156  * Generates icon in front of links.
157  *
158  * @param string $protocol_or_url URL or protocol to determine which icon to use.
159  *
160  * @return HtmlElement HtmlElement object that contains data to create img link to
161  * icon for use with url or protocol passed to the function. False if no img to be
162  * displayed.
163  */
164 function IconForLink($protocol_or_url) {
165     global $Theme;
166     if ($filename_suffix = false) {
167         // display apache style icon for file type instead of protocol icon
168         // - archive: unix:gz,bz2,tgz,tar,z; mac:dmg,dmgz,bin,img,cpt,sit; pc:zip;
169         // - document: html, htm, text, txt, rtf, pdf, doc
170         // - non-inlined image: jpg,jpeg,png,gif,tiff,tif,swf,pict,psd,eps,ps
171         // - audio: mp3,mp2,aiff,aif,au
172         // - multimedia: mpeg,mpg,mov,qt
173     } else {
174         list ($proto) = explode(':', $protocol_or_url, 2);
175         $src = $Theme->getLinkIconURL($proto);
176         if ($src)
177             return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon', 'border' => 0));
178         else
179             return false;
180     }
181 }
182
183 /**
184  * Glue icon in front of text.
185  *
186  * @param string $protocol_or_url Protocol or URL.  Used to determine the
187  * proper icon.
188  * @param string $text The text.
189  * @return XmlContent.
190  */
191 function PossiblyGlueIconToText($proto_or_url, $text) {
192     $icon = IconForLink($proto_or_url);
193     if ($icon) {
194         preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
195         list (, $first_word, $tail) = $m;
196         $text = HTML::span(array('style' => 'white-space: nowrap'),
197                            $icon, $first_word);
198         if ($tail)
199             $text = HTML($text, $tail);
200     }
201     return $text;
202 }
203
204 /**
205  * Determines if the url passed to function is safe, by detecting if the characters
206  * '<', '>', or '"' are present.
207  *
208  * @param string $url URL to check for unsafe characters.
209  * @return boolean True if same, false else.
210  */
211 function IsSafeURL($url) {
212     return !ereg('[<>"]', $url);
213 }
214
215 /**
216  * Generates an HtmlElement object to store data for a link.
217  *
218  * @param string $url URL that the link will point to.
219  * @param string $linktext Text to be displayed as link.
220  * @return HtmlElement HtmlElement object that contains data to construct an html link.
221  */
222 function LinkURL($url, $linktext = '') {
223     // FIXME: Is this needed (or sufficient?)
224     if(! IsSafeURL($url)) {
225         $link = HTML::strong(HTML::u(array('class' => 'baduri'),
226                                      _("BAD URL -- remove all of <, >, \"")));
227     }
228     else {
229         if (!$linktext)
230             $linktext = preg_replace("/mailto:/A", "", $url);
231         
232         $link = HTML::a(array('href' => $url),
233                         PossiblyGlueIconToText($url, $linktext));
234         
235     }
236     $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
237     return $link;
238 }
239
240
241 function LinkImage($url, $alt = false) {
242     // FIXME: Is this needed (or sufficient?)
243     if(! IsSafeURL($url)) {
244         $link = HTML::strong(HTML::u(array('class' => 'baduri'),
245                                      _("BAD URL -- remove all of <, >, \"")));
246     }
247     else {
248         if (empty($alt))
249             $alt = $url;
250         $link = HTML::img(array('src' => $url, 'alt' => $alt));
251     }
252     $link->setAttr('class', 'inlineimage');
253     return $link;
254 }
255
256
257
258 class Stack {
259     var $items = array();
260     var $size = 0;
261     
262     function push($item) {
263         $this->items[$this->size] = $item;
264         $this->size++;
265         return true;
266     }  
267     
268     function pop() {
269         if ($this->size == 0) {
270             return false; // stack is empty
271         }  
272         $this->size--;
273         return $this->items[$this->size];
274     }  
275     
276     function cnt() {
277         return $this->size;
278     }  
279     
280     function top() {
281         if($this->size)
282             return $this->items[$this->size - 1];
283         else
284             return '';
285     }
286     
287 }  
288 // end class definition
289
290 function SplitQueryArgs ($query_args = '') 
291 {
292     $split_args = split('&', $query_args);
293     $args = array();
294     while (list($key, $val) = each($split_args))
295         if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
296             $args[$m[1]] = $m[2];
297     return $args;
298 }
299
300 function LinkPhpwikiURL($url, $text = '', $basepage) {
301     $args = array();
302     
303     if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m)) {
304         return HTML::strong(array('class' => 'rawurl'),
305                             HTML::u(array('class' => 'baduri'),
306                                     _("BAD phpwiki: URL")));
307     }
308
309     if ($m[1])
310         $pagename = urldecode($m[1]);
311     $qargs = $m[2];
312     
313     if (empty($pagename) &&
314         preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)) {
315         // Convert old style links (to not break diff links in
316         // RecentChanges).
317         $pagename = urldecode($m[2]);
318         $args = array("action" => $m[1]);
319     }
320     else {
321         $args = SplitQueryArgs($qargs);
322     }
323
324     if (empty($pagename))
325         $pagename = $GLOBALS['request']->getArg('pagename');
326
327     if (isset($args['action']) && $args['action'] == 'browse')
328         unset($args['action']);
329     
330     /*FIXME:
331       if (empty($args['action']))
332       $class = 'wikilink';
333       else if (is_safe_action($args['action']))
334       $class = 'wikiaction';
335     */
336     if (empty($args['action']) || is_safe_action($args['action']))
337         $class = 'wikiaction';
338     else {
339         // Don't allow administrative links on unlocked pages.
340         $dbi = $GLOBALS['request']->getDbh();
341         $page = $dbi->getPage($basepage);
342         if (!$page->get('locked'))
343             return HTML::span(array('class' => 'wikiunsafe'),
344                               HTML::u(_("Lock page to enable link")));
345         $class = 'wikiadmin';
346     }
347     
348     if (!$text)
349         $text = HTML::span(array('class' => 'rawurl'), $url);
350
351     $wikipage = new WikiPageName($pagename);
352     if (!$wikipage->isValid()) {
353         global $Theme;
354         return $Theme->linkBadWikiWord($wikipage, $url);
355     }
356     
357     return HTML::a(array('href'  => WikiURL($pagename, $args),
358                          'class' => $class),
359                    $text);
360 }
361
362 /**
363  * A class to assist in parsing wiki pagenames.
364  *
365  * Now with subpages and anchors, parsing and passing around
366  * pagenames is more complicated.  This should help.
367  */
368 class WikiPagename
369 {
370     /** Short name for page.
371      *
372      * This is the value of $name passed to the constructor.
373      * (For use, e.g. as a default label for links to the page.)
374      */
375     var $shortName;
376
377     /** The full page name.
378      *
379      * This is the full name of the page (without anchor).
380      */
381     var $name;
382     
383     /** The anchor.
384      *
385      * This is the referenced anchor within the page, or the empty string.
386      */
387     var $anchor;
388     
389     /** Constructor
390      *
391      * @param mixed $name Page name.
392      * WikiDB_Page, WikiDB_PageRevision, or string.
393      * This can be a relative subpage name (like '/SubPage'),
394      * or can be the empty string to refer to the $basename.
395      *
396      * @param string $anchor For links to anchors in page.
397      *
398      * @param mixed $basename Page name from which to interpret
399      * relative or other non-fully-specified page names.
400      */
401     function WikiPageName($name, $basename=false, $anchor=false) {
402         if (is_string($name)) {
403             $this->shortName = $name;
404         
405             if ($anchor === false and preg_match('/\A(.*)#(.*?)?\Z/', $name, $m))
406                 list(, $name, $anchor) = $m;
407             
408             if (empty($name) or $name[0] == SUBPAGE_SEPARATOR) {
409                 if ($basename)
410                     $name = $this->_pagename($basename) . $name;
411                 else
412                     $name = $this->_normalize_bad_pagename($name);
413             }
414         }
415         else {
416             $name = $this->_pagename($name);
417             $this->shortName = $name;
418         }
419
420         $this->name = $this->_check($name);
421         $this->anchor = (string)$anchor;
422     }
423
424     function getParent() {
425         $name = $this->name;
426         if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
427             return false;
428         return substr($name, 0, -strlen($tail));
429     }
430
431     function isValid($strict = false) {
432         if ($strict)
433             return !isset($this->_errors);
434         return !empty($this->name);
435     }
436
437     function getWarnings() {
438         $warnings = array();
439         if (isset($this->_warnings))
440             $warnings = array_merge($warnings, $this->_warnings);
441         if (isset($this->_errors))
442             $warnings = array_merge($warnings, $this->_errors);
443         if (!$warnings)
444             return false;
445         
446         return sprintf(_("'%s': Bad page name: %s"),
447                        $this->shortName, join(', ', $warnings));
448     }
449     
450     function _pagename($page) {
451         if (isa($page, 'WikiDB_Page'))
452             return $page->getName();
453         elseif (isa($page, 'WikiDB_PageRevision'))
454             return $page->getPageName();
455         elseif (isa($page, 'WikiPageName'))
456             return $page->name;
457         if (!is_string($page)) {
458             print "PAGE: " . gettype($page) . " " . get_class($page) . "<br>\n";
459         }
460         //assert(is_string($page));
461         return $page;
462     }
463
464     function _normalize_bad_pagename($name) {
465         trigger_error("Bad pagename: " . $name, E_USER_WARNING);
466
467         // Punt...  You really shouldn't get here.
468         if (empty($name)) {
469             global $request;
470             return $request->getArg('pagename');
471         }
472         assert($name[0] == SUBPAGE_SEPARATOR);
473         return substr($name, 1);
474     }
475
476
477     function _check($pagename) {
478         // Compress internal white-space to single space character.
479         $pagename = preg_replace('/[\s\xa0]+/', ' ', $orig = $pagename);
480         if ($pagename != $orig)
481             $this->_warnings[] = _("White space converted to single space");
482     
483         // Delete any control characters.
484         $pagename = preg_replace('/[\x00-\x1f\x7f\x80-\x9f]/', '', $orig = $pagename);
485         if ($pagename != $orig)
486             $this->_errors[] = _("Control characters not allowed");
487
488         // Strip leading and trailing white-space.
489         $pagename = trim($pagename);
490
491         $orig = $pagename;
492         while ($pagename and $pagename[0] == SUBPAGE_SEPARATOR)
493             $pagename = substr($pagename, 1);
494         if ($pagename != $orig)
495             $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
496
497         if (preg_match('/[:;]/', $pagename))
498             $this->_warnings[] = _("';' and ':' in pagenames are deprecated");
499         
500         if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
501             $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
502             $this->_errors[] = _("too long");
503         }
504         
505
506         if ($pagename == '.' or $pagename == '..') {
507             $this->_errors[] = sprintf(_("illegal pagename"), $pagename);
508             $pagename = '';
509         }
510         
511         return $pagename;
512     }
513 }
514
515 /**
516  * Convert old page markup to new-style markup.
517  *
518  * @param string $text Old-style wiki markup.
519  *
520  * @param string $markup_type
521  * One of: <dl>
522  * <dt><code>"block"</code>  <dd>Convert all markup.
523  * <dt><code>"inline"</code> <dd>Convert only inline markup.
524  * <dt><code>"links"</code>  <dd>Convert only link markup.
525  * </dl>
526  *
527  * @return string New-style wiki markup.
528  *
529  * @bugs Footnotes don't work quite as before (esp if there are
530  *   multiple references to the same footnote.  But close enough,
531  *   probably for now....
532  */
533 function ConvertOldMarkup ($text, $markup_type = "block") {
534
535     static $subs;
536     static $block_re;
537     
538     if (empty($subs)) {
539         /*****************************************************************
540          * Conversions for inline markup:
541          */
542
543         // escape tilde's
544         $orig[] = '/~/';
545         $repl[] = '~~';
546
547         // escape escaped brackets
548         $orig[] = '/\[\[/';
549         $repl[] = '~[';
550
551         // change ! escapes to ~'s.
552         global $AllowedProtocols, $WikiNameRegexp, $request;
553         include_once('lib/interwiki.php');
554         $map = InterWikiMap::GetMap($request);
555         $bang_esc[] = "(?:$AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]";
556         $bang_esc[] = $map->getRegexp() . ":[^\\s.,;?()]+"; // FIXME: is this really needed?
557         $bang_esc[] = $WikiNameRegexp;
558         $orig[] = '/!((?:' . join(')|(', $bang_esc) . '))/';
559         $repl[] = '~\\1';
560
561         $subs["links"] = array($orig, $repl);
562
563         // Escape '<'s
564         //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
565         //$repl[] = '~<';
566         
567         // Convert footnote references.
568         $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
569         $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
570
571         // Convert old style emphases to HTML style emphasis.
572         $orig[] = '/__(.*?)__/';
573         $repl[] = '<strong>\\1</strong>';
574         $orig[] = "/''(.*?)''/";
575         $repl[] = '<em>\\1</em>';
576
577         // Escape nestled markup.
578         $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
579         $repl[] = '~\\0';
580         
581         // in old markup headings only allowed at beginning of line
582         $orig[] = '/!/';
583         $repl[] = '~!';
584
585         $subs["inline"] = array($orig, $repl);
586
587         /*****************************************************************
588          * Patterns which match block markup constructs which take
589          * special handling...
590          */
591
592         // Indented blocks
593         $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
594
595         // Tables
596         $blockpats[] = '\|(?:.*\n\|)*';
597
598         // List items
599         $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
600
601         // Footnote definitions
602         $blockpats[] = '\[\s*(\d+)\s*\]';
603
604         // Plugins
605         $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
606
607         // Section Title
608         $blockpats[] = '!{1,3}[^!]';
609
610         $block_re = ( '/\A((?:.|\n)*?)(^(?:'
611                       . join("|", $blockpats)
612                       . ').*$)\n?/m' );
613         
614     }
615     
616     if ($markup_type != "block") {
617         list ($orig, $repl) = $subs[$markup_type];
618         return preg_replace($orig, $repl, $text);
619     }
620     else {
621         list ($orig, $repl) = $subs['inline'];
622         $out = '';
623         while (preg_match($block_re, $text, $m)) {
624             $text = substr($text, strlen($m[0]));
625             list (,$leading_text, $block) = $m;
626             $suffix = "\n";
627             
628             if (strchr(" \t", $block[0])) {
629                 // Indented block
630                 $prefix = "<pre>\n";
631                 $suffix = "\n</pre>\n";
632             }
633             elseif ($block[0] == '|') {
634                 // Old-style table
635                 $prefix = "<?plugin OldStyleTable\n";
636                 $suffix = "\n?>\n";
637             }
638             elseif (strchr("#*;", $block[0])) {
639                 // Old-style list item
640                 preg_match('/^([#*;]*)([*#]|;.*?:) */', $block, $m);
641                 list (,$ind,$bullet) = $m;
642                 $block = substr($block, strlen($m[0]));
643                 
644                 $indent = str_repeat('     ', strlen($ind));
645                 if ($bullet[0] == ';') {
646                     //$term = ltrim(substr($bullet, 1));
647                     //return $indent . $term . "\n" . $indent . '     ';
648                     $prefix = $ind . $bullet;
649                 }
650                 else
651                     $prefix = $indent . $bullet . ' ';
652             }
653             elseif ($block[0] == '[') {
654                 // Footnote definition
655                 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
656                 $footnum = $m[1];
657                 $block = substr($block, strlen($m[0]));
658                 $prefix = "#[|ftnt_${footnum}]~[[${footnum}|#ftnt_ref_${footnum}]~] ";
659             }
660             elseif ($block[0] == '<') {
661                 // Plugin.
662                 // HACK: no inline markup...
663                 $prefix = $block;
664                 $block = '';
665             }
666             elseif ($block[0] == '!') {
667                 // Section heading
668                 preg_match('/^!{1,3}/', $block, $m);
669                 $prefix = $m[0];
670                 $block = substr($block, strlen($m[0]));
671             }
672             else {
673                 // AAck!
674                 assert(0);
675             }
676
677             $out .= ( preg_replace($orig, $repl, $leading_text)
678                       . $prefix
679                       . preg_replace($orig, $repl, $block)
680                       . $suffix );
681         }
682         return $out . preg_replace($orig, $repl, $text);
683     }
684 }
685
686
687 /**
688  * Expand tabs in string.
689  *
690  * Converts all tabs to (the appropriate number of) spaces.
691  *
692  * @param string $str
693  * @param integer $tab_width
694  * @return string
695  */
696 function expand_tabs($str, $tab_width = 8) {
697     $split = split("\t", $str);
698     $tail = array_pop($split);
699     $expanded = "\n";
700     foreach ($split as $hunk) {
701         $expanded .= $hunk;
702         $pos = strlen(strrchr($expanded, "\n")) - 1;
703         $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
704     }
705     return substr($expanded, 1) . $tail;
706 }
707
708 /**
709  * Split WikiWords in page names.
710  *
711  * It has been deemed useful to split WikiWords (into "Wiki Words") in
712  * places like page titles. This is rumored to help search engines
713  * quite a bit.
714  *
715  * @param $page string The page name.
716  *
717  * @return string The split name.
718  */
719 function split_pagename ($page) {
720     
721     if (preg_match("/\s/", $page))
722         return $page;           // Already split --- don't split any more.
723     
724     // FIXME: this algorithm is Anglo-centric.
725     static $RE;
726     if (!isset($RE)) {
727         // This mess splits between a lower-case letter followed by
728         // either an upper-case or a numeral; except that it wont
729         // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
730         $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
731         // This the single-letter words 'I' and 'A' from any following
732         // capitalized words.
733         $sep = preg_quote(SUBPAGE_SEPARATOR, '/');
734         $RE[] = "/(?<= |${sep}|^)([AI])([[:upper:]][[:lower:]])/";
735         // Split numerals from following letters.
736         $RE[] = '/(\d)([[:alpha:]])/';
737         
738         foreach ($RE as $key => $val)
739             $RE[$key] = pcre_fix_posix_classes($val);
740     }
741
742     foreach ($RE as $regexp) {
743         $page = preg_replace($regexp, '\\1 \\2', $page);
744     }
745     return $page;
746 }
747
748 function NoSuchRevision (&$request, $page, $version) {
749     $html = HTML(HTML::h2(_("Revision Not Found")),
750                  HTML::p(fmt("I'm sorry.  Version %d of %s is not in the database.",
751                              $version, WikiLink($page, 'auto'))));
752     include_once('lib/Template.php');
753     GeneratePage($html, _("Bad Version"), $page->getCurrentRevision());
754     $request->finish();
755 }
756
757
758 /**
759  * Get time offset for local time zone.
760  *
761  * @param $time time_t Get offset for this time. Default: now.
762  * @param $no_colon boolean Don't put colon between hours and minutes.
763  * @return string Offset as a string in the format +HH:MM.
764  */
765 function TimezoneOffset ($time = false, $no_colon = false) {
766     if ($time === false)
767         $time = time();
768     $secs = date('Z', $time);
769
770     if ($secs < 0) {
771         $sign = '-';
772         $secs = -$secs;
773     }
774     else {
775         $sign = '+';
776     }
777     $colon = $no_colon ? '' : ':';
778     $mins = intval(($secs + 30) / 60);
779     return sprintf("%s%02d%s%02d",
780                    $sign, $mins / 60, $colon, $mins % 60);
781 }
782
783
784 /**
785  * Format time in ISO-8601 format.
786  *
787  * @param $time time_t Time.  Default: now.
788  * @return string Date and time in ISO-8601 format.
789  */
790 function Iso8601DateTime ($time = false) {
791     if ($time === false)
792         $time = time();
793     $tzoff = TimezoneOffset($time);
794     $date  = date('Y-m-d', $time);
795     $time  = date('H:i:s', $time);
796     return $date . 'T' . $time . $tzoff;
797 }
798
799 /**
800  * Format time in RFC-2822 format.
801  *
802  * @param $time time_t Time.  Default: now.
803  * @return string Date and time in RFC-2822 format.
804  */
805 function Rfc2822DateTime ($time = false) {
806     if ($time === false)
807         $time = time();
808     return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
809 }
810
811 /**
812  * Format time in RFC-1123 format.
813  *
814  * @param $time time_t Time.  Default: now.
815  * @return string Date and time in RFC-1123 format.
816  */
817 function Rfc1123DateTime ($time = false) {
818     if ($time === false)
819         $time = time();
820     return gmdate('D, d M Y H:i:s \G\M\T', $time);
821 }
822
823 /** Parse date in RFC-1123 format.
824  *
825  * According to RFC 1123 we must accept dates in the following
826  * formats:
827  *
828  *   Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
829  *   Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
830  *   Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
831  *
832  * (Though we're only allowed to generate dates in the first format.)
833  */
834 function ParseRfc1123DateTime ($timestr) {
835     $timestr = trim($timestr);
836     if (preg_match('/^ \w{3},\s* (\d{1,2}) \s* (\w{3}) \s* (\d{4}) \s*'
837                    .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
838                    $timestr, $m)) {
839         list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
840     }
841     elseif (preg_match('/^ \w+,\s* (\d{1,2})-(\w{3})-(\d{2}|\d{4}) \s*'
842                        .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
843                        $timestr, $m)) {
844         list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
845         if ($year < 70) $year += 2000;
846         elseif ($year < 100) $year += 1900;
847     }
848     elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
849                        .'(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
850                        $timestr, $m)) {
851         list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
852     }
853     else {
854         // Parse failed.
855         return false;
856     }
857
858     $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
859     if ($time == -1)
860         return false;           // failed
861     return $time;
862 }
863
864 /**
865  * Format time to standard 'ctime' format.
866  *
867  * @param $time time_t Time.  Default: now.
868  * @return string Date and time.
869  */
870 function CTime ($time = false)
871 {
872     if ($time === false)
873         $time = time();
874     return date("D M j H:i:s Y", $time);
875 }
876
877
878
879 /**
880  * Internationalized printf.
881  *
882  * This is essentially the same as PHP's built-in printf
883  * with the following exceptions:
884  * <ol>
885  * <li> It passes the format string through gettext().
886  * <li> It supports the argument reordering extensions.
887  * </ol>
888  *
889  * Example:
890  *
891  * In php code, use:
892  * <pre>
893  *    __printf("Differences between versions %s and %s of %s",
894  *             $new_link, $old_link, $page_link);
895  * </pre>
896  *
897  * Then in locale/po/de.po, one can reorder the printf arguments:
898  *
899  * <pre>
900  *    msgid "Differences between %s and %s of %s."
901  *    msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
902  * </pre>
903  *
904  * (Note that while PHP tries to expand $vars within double-quotes,
905  * the values in msgstr undergo no such expansion, so the '$'s
906  * okay...)
907  *
908  * One shouldn't use reordered arguments in the default format string.
909  * Backslashes in the default string would be necessary to escape the
910  * '$'s, and they'll cause all kinds of trouble....
911  */ 
912 function __printf ($fmt) {
913     $args = func_get_args();
914     array_shift($args);
915     echo __vsprintf($fmt, $args);
916 }
917
918 /**
919  * Internationalized sprintf.
920  *
921  * This is essentially the same as PHP's built-in printf with the
922  * following exceptions:
923  *
924  * <ol>
925  * <li> It passes the format string through gettext().
926  * <li> It supports the argument reordering extensions.
927  * </ol>
928  *
929  * @see __printf
930  */ 
931 function __sprintf ($fmt) {
932     $args = func_get_args();
933     array_shift($args);
934     return __vsprintf($fmt, $args);
935 }
936
937 /**
938  * Internationalized vsprintf.
939  *
940  * This is essentially the same as PHP's built-in printf with the
941  * following exceptions:
942  *
943  * <ol>
944  * <li> It passes the format string through gettext().
945  * <li> It supports the argument reordering extensions.
946  * </ol>
947  *
948  * @see __printf
949  */ 
950 function __vsprintf ($fmt, $args) {
951     $fmt = gettext($fmt);
952     // PHP's sprintf doesn't support variable with specifiers,
953     // like sprintf("%*s", 10, "x"); --- so we won't either.
954     
955     if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
956         // Format string has '%2$s' style argument reordering.
957         // PHP doesn't support this.
958         if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
959             // literal variable name substitution only to keep locale
960             // strings uncluttered
961             trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
962                                   '%1\$s','%s'), E_USER_WARNING); //php+locale error
963         
964         $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
965         $newargs = array();
966         
967         // Reorder arguments appropriately.
968         foreach($m[1] as $argnum) {
969             if ($argnum < 1 || $argnum > count($args))
970                 trigger_error(sprintf(_("%s: argument index out of range"), 
971                                       $argnum), E_USER_WARNING);
972             $newargs[] = $args[$argnum - 1];
973         }
974         $args = $newargs;
975     }
976     
977     // Not all PHP's have vsprintf, so...
978     array_unshift($args, $fmt);
979     return call_user_func_array('sprintf', $args);
980 }
981
982
983 class fileSet {
984     /**
985      * Build an array in $this->_fileList of files from $dirname.
986      * Subdirectories are not traversed.
987      *
988      * (This was a function LoadDir in lib/loadsave.php)
989      * See also http://www.php.net/manual/en/function.readdir.php
990      */
991     function getFiles() {
992         return $this->_fileList;
993     }
994
995     function _filenameSelector($filename) {
996         if (! $this->_pattern)
997             return true;
998         else {
999             return glob_match ($this->_pattern, $filename, $this->_case);
1000         }
1001     }
1002
1003     function fileSet($directory, $filepattern = false) {
1004         $this->_fileList = array();
1005         $this->_pattern = $filepattern;
1006         $this->_case = !isWindows();
1007         $this->_pathsep = '/';
1008
1009         if (empty($directory)) {
1010             trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1011                           E_USER_NOTICE);
1012             return; // early return
1013         }
1014
1015         @ $dir_handle = opendir($dir=$directory);
1016         if (empty($dir_handle)) {
1017             trigger_error(sprintf(_("Unable to open directory '%s' for reading"),
1018                                   $dir), E_USER_NOTICE);
1019             return; // early return
1020         }
1021
1022         while ($filename = readdir($dir_handle)) {
1023             if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1024                 continue;
1025             if ($this->_filenameSelector($filename)) {
1026                 array_push($this->_fileList, "$filename");
1027                 //trigger_error(sprintf(_("found file %s"), $filename),
1028                 //                      E_USER_NOTICE); //debugging
1029             }
1030         }
1031         closedir($dir_handle);
1032     }
1033 };
1034
1035 // File globbing
1036
1037 // expands a list containing regex's to its matching entries
1038 class ListRegexExpand {
1039     var $match, $list, $index, $case_sensitive;
1040     function ListRegexExpand (&$list, $match, $case_sensitive = true) {
1041         $this->match = str_replace('/','\/',$match);
1042         $this->list = &$list;
1043         $this->case_sensitive = $case_sensitive;        
1044     }
1045     function listMatchCallback ($item, $key) {
1046         if (preg_match('/' . $this->match . ($this->case_sensitive ? '/' : '/i'), $item)) {
1047             unset($this->list[$this->index]);
1048             $this->list[] = $item;
1049         }
1050     }
1051     function expandRegex ($index, &$pages) {
1052         $this->index = $index;
1053         array_walk($pages, array($this, 'listMatchCallback'));
1054         return $this->list;
1055     }
1056 }
1057
1058 // convert fileglob to regex style
1059 function glob_to_pcre ($glob) {
1060     $re = preg_replace('/\./', '\\.', $glob);
1061     $re = preg_replace(array('/\*/','/\?/'), array('.*','.'), $glob);
1062     if (!preg_match('/^[\?\*]/',$glob))
1063         $re = '^' . $re;
1064     if (!preg_match('/[\?\*]$/',$glob))
1065         $re = $re . '$';
1066     return $re;
1067 }
1068
1069 function glob_match ($glob, $against, $case_sensitive = true) {
1070     return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'), $against);
1071 }
1072
1073 function explodeList($input, $allnames, $glob_style = true, $case_sensitive = true) {
1074     $list = explode(',',$input);
1075     // expand wildcards from list of $allnames
1076     if (preg_match('/[\?\*]/',$input)) {
1077         for ($i = 0; $i < sizeof($list); $i++) {
1078             $f = $list[$i];
1079             if (preg_match('/[\?\*]/',$f)) {
1080                 reset($allnames);
1081                 $expand = new ListRegexExpand($list, $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1082                 $expand->expandRegex($i, $allnames);
1083             }
1084         }
1085     }
1086     return $list;
1087 }
1088
1089 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1090
1091 function explodePageList($input, $perm = false) {
1092     // expand wildcards from list of all pages
1093     if (preg_match('/[\?\*]/',$input)) {
1094         $dbi = $GLOBALS['request']->_dbi;
1095         $allPagehandles = $dbi->getAllPages($perm);
1096         while ($pagehandle = $allPagehandles->next()) {
1097             $allPages[] = $pagehandle->getName();
1098         }
1099         return explodeList($input, $allPages);
1100     } else {
1101         return explode(',',$input);
1102     }
1103 }
1104
1105 // Class introspections
1106
1107 /** Determine whether object is of a specified type.
1108  *
1109  * @param $object object An object.
1110  * @param $class string Class name.
1111  * @return bool True iff $object is a $class
1112  * or a sub-type of $class. 
1113  */
1114 function isa ($object, $class) 
1115 {
1116     $lclass = strtolower($class);
1117
1118     return is_object($object)
1119         && ( get_class($object) == strtolower($lclass)
1120              || is_subclass_of($object, $lclass) );
1121 }
1122
1123 /** Determine whether (possible) object has method.
1124  *
1125  * @param $object mixed Object
1126  * @param $method string Method name
1127  * @return bool True iff $object is an object with has method $method.
1128  */
1129 function can ($object, $method) 
1130 {
1131     return is_object($object) && method_exists($object, strtolower($method));
1132 }
1133
1134 /** Hash a value.
1135  *
1136  * This is used for generating ETags.
1137  */
1138 function hash ($x) {
1139     if (is_scalar($x)) {
1140         return $x;
1141     }
1142     elseif (is_array($x)) {            
1143         ksort($x);
1144         return md5(serialize($x));
1145     }
1146     elseif (is_object($x)) {
1147         return $x->hash();
1148     }
1149     trigger_error("Can't hash $x", E_USER_ERROR);
1150 }
1151
1152     
1153 /**
1154  * Seed the random number generator.
1155  *
1156  * better_srand() ensures the randomizer is seeded only once.
1157  * 
1158  * How random do you want it? See:
1159  * http://www.php.net/manual/en/function.srand.php
1160  * http://www.php.net/manual/en/function.mt-srand.php
1161  */
1162 function better_srand($seed = '') {
1163     static $wascalled = FALSE;
1164     if (!$wascalled) {
1165         $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
1166         srand($seed);
1167         $wascalled = TRUE;
1168         //trigger_error("new random seed", E_USER_NOTICE); //debugging
1169     }
1170 }
1171
1172 /**
1173  * Recursively count all non-empty elements 
1174  * in array of any dimension or mixed - i.e. 
1175  * array('1' => 2, '2' => array('1' => 3, '2' => 4))
1176  * See http://www.php.net/manual/en/function.count.php
1177  */
1178 function count_all($arg) {
1179     // skip if argument is empty
1180     if ($arg) {
1181         //print_r($arg); //debugging
1182         $count = 0;
1183         // not an array, return 1 (base case) 
1184         if(!is_array($arg))
1185             return 1;
1186         // else call recursively for all elements $arg
1187         foreach($arg as $key => $val)
1188             $count += count_all($val);
1189         return $count;
1190     }
1191 }
1192
1193 function isSubPage($pagename) {
1194     return (strstr($pagename, SUBPAGE_SEPARATOR));
1195 }
1196
1197 function subPageSlice($pagename, $pos) {
1198     $pages = explode(SUBPAGE_SEPARATOR,$pagename);
1199     $pages = array_slice($pages,$pos,1);
1200     return $pages[0];
1201 }
1202
1203 /**
1204  * Alert
1205  *
1206  * Class for "popping up" and alert box.  (Except that right now, it doesn't
1207  * pop up...)
1208  *
1209  * FIXME:
1210  * This is a hackish and needs to be refactored.  However it would be nice to
1211  * unify all the different methods we use for showing Alerts and Dialogs.
1212  * (E.g. "Page deleted", login form, ...)
1213  */
1214 class Alert {
1215     /** Constructor
1216      *
1217      * @param object $request
1218      * @param mixed $head  Header ("title") for alert box.
1219      * @param mixed $body  The text in the alert box.
1220      * @param hash $buttons  An array mapping button labels to URLs.
1221      *    The default is a single "Okay" button pointing to $request->getURLtoSelf().
1222      */
1223     function Alert($head, $body, $buttons=false) {
1224         if ($buttons === false)
1225             $buttons = array();
1226
1227         $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1228         $this->_buttons = $buttons;
1229     }
1230
1231     /**
1232      * Show the alert box.
1233      */
1234     function show(&$request) {
1235         global $request;
1236
1237         $tokens = $this->_tokens;
1238         $tokens['BUTTONS'] = $this->_getButtons();
1239         
1240         $request->discardOutput();
1241         $tmpl = new Template('dialog', $request, $tokens);
1242         $tmpl->printXML();
1243         $request->finish();
1244     }
1245
1246
1247     function _getButtons() {
1248         global $request;
1249
1250         $buttons = $this->_buttons;
1251         if (!$buttons)
1252             $buttons = array(_("Okay") => $request->getURLtoSelf());
1253         
1254         global $Theme;
1255         foreach ($buttons as $label => $url)
1256             print "$label $url\n";
1257             $out[] = $Theme->makeButton($label, $url, 'wikiaction');
1258         return new XmlContent($out);
1259     }
1260 }
1261
1262                       
1263         
1264 // $Log: not supported by cvs2svn $
1265 // Revision 1.144  2003/02/26 00:39:30  dairiki
1266 // Bug fix: for magic PhpWiki URLs, "lock page to enable link" message was
1267 // being displayed at incorrect times.
1268 //
1269 // Revision 1.143  2003/02/26 00:10:26  dairiki
1270 // More/better/different checks for bad page names.
1271 //
1272 // Revision 1.142  2003/02/25 22:19:46  dairiki
1273 // Add some sanity checking for pagenames.
1274 //
1275 // Revision 1.141  2003/02/22 20:49:55  dairiki
1276 // Fixes for "Call-time pass by reference has been deprecated" errors.
1277 //
1278 // Revision 1.140  2003/02/21 23:33:29  dairiki
1279 // Set alt="" on the link icon image tags.
1280 // (See SF bug #675141.)
1281 //
1282 // Revision 1.139  2003/02/21 22:16:27  dairiki
1283 // Get rid of MakeWikiForm, and form-style MagicPhpWikiURLs.
1284 // These have been obsolete for quite awhile (I hope).
1285 //
1286 // Revision 1.138  2003/02/21 04:12:36  dairiki
1287 // WikiPageName: fixes for new cached links.
1288 //
1289 // Alert: new class for displaying alerts.
1290 //
1291 // ExtractWikiPageLinks and friends are now gone.
1292 //
1293 // LinkBracketLink moved to InlineParser.php
1294 //
1295 // Revision 1.137  2003/02/18 23:13:40  dairiki
1296 // Wups again.  Typo fix.
1297 //
1298 // Revision 1.136  2003/02/18 21:52:07  dairiki
1299 // Fix so that one can still link to wiki pages with # in their names.
1300 // (This was made difficult by the introduction of named tags, since
1301 // '[Page #1]' is now a link to anchor '1' in page 'Page'.
1302 //
1303 // Now the ~ escape for page names should work: [Page ~#1].
1304 //
1305 // Revision 1.135  2003/02/18 19:17:04  dairiki
1306 // split_pagename():
1307 //     Bug fix. 'ThisIsABug' was being split to 'This IsA Bug'.
1308 //     Cleanup up subpage splitting code.
1309 //
1310 // Revision 1.134  2003/02/16 19:44:20  dairiki
1311 // New function hash().  This is a helper, primarily for generating
1312 // HTTP ETags.
1313 //
1314 // Revision 1.133  2003/02/16 04:50:09  dairiki
1315 // New functions:
1316 // Rfc1123DateTime(), ParseRfc1123DateTime()
1317 // for converting unix timestamps to and from strings.
1318 //
1319 // These functions produce and grok the time strings
1320 // in the format specified by RFC 2616 for use in HTTP headers
1321 // (like Last-Modified).
1322 //
1323 // Revision 1.132  2003/01/04 22:19:43  carstenklapp
1324 // Bugfix UnfoldSubpages: "Undefined offset: 1" error when plugin invoked
1325 // on a page with no subpages (explodeList(): array 0-based, sizeof 1-based).
1326 //
1327
1328 // (c-file-style: "gnu")
1329 // Local Variables:
1330 // mode: php
1331 // tab-width: 8
1332 // c-basic-offset: 4
1333 // c-hanging-comment-ender-p: nil
1334 // indent-tabs-mode: nil
1335 // End:   
1336 ?>