1 <?php //rcs_id('$Id: stdlib.php,v 1.145 2003-03-04 01:55:05 dairiki Exp $');
4 Standard functions for Wiki functionality
5 WikiURL($pagename, $args, $get_abs_url)
6 IconForLink($protocol_or_url)
7 LinkURL($url, $linktext)
10 SplitQueryArgs ($query_args)
11 LinkPhpwikiURL($url, $text)
12 ConvertOldMarkup($content)
14 class Stack { push($item), pop(), cnt(), top() }
16 split_pagename ($page)
17 NoSuchRevision ($request, $page, $version)
18 TimezoneOffset ($time, $no_colon)
19 Iso8601DateTime ($time)
20 Rfc2822DateTime ($time)
24 __vsprintf ($fmt, $args)
25 better_srand($seed = '')
28 subPageSlice($pagename, $pos)
29 explodePageList($input, $perm = false)
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
41 define('MAX_PAGENAME_LENGTH', 100);
45 * Convert string to a valid XML identifier.
47 * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
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.
53 * This function implements a one-to-one map from {any string}
54 * to {valid XML identifiers}.
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
62 * As a special case, the empty string is converted to 'empty.'
67 function MangleXmlIdentifier($str)
72 return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
73 "'x' . sprintf('%02x', ord('\\0')) . '.'",
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.
84 * @param boolean $get_abs_url Default value is false.
85 * @return string The absolute URL to the page passed as $pagename.
87 function WikiURL($pagename, $args = '', $get_abs_url = false) {
90 if (is_object($pagename)) {
91 if (isa($pagename, 'WikiDB_Page')) {
92 $pagename = $pagename->getName();
94 elseif (isa($pagename, 'WikiDB_PageRevision')) {
95 $page = $pagename->getPage();
96 $args['version'] = $pagename->getVersion();
97 $pagename = $page->getName();
99 elseif (isa($pagename, 'WikiPageName')) {
100 $anchor = $pagename->anchor;
101 $pagename = $pagename->name;
105 if (is_array($args)) {
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);
111 $args = join('&', $enc_args);
115 $url = $get_abs_url ? SERVER_URL . VIRTUAL_PATH . "/" : "";
116 $url .= preg_replace('/%2f/i', '/', rawurlencode($pagename));
121 $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
122 $url .= "?pagename=" . rawurlencode($pagename);
127 $url .= "#" . MangleXmlIdentifier($anchor);
131 /** Convert relative URL to absolute URL.
133 * This converts a relative URL to one of PhpWiki's support files
134 * to an absolute one.
137 * @return string Absolute URL
139 function AbsoluteURL ($url) {
140 if (preg_match('/^https?:/', $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);
152 return SERVER_URL . $url;
156 * Generates icon in front of links.
158 * @param string $protocol_or_url URL or protocol to determine which icon to use.
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
164 function IconForLink($protocol_or_url) {
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
174 list ($proto) = explode(':', $protocol_or_url, 2);
175 $src = $Theme->getLinkIconURL($proto);
177 return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon', 'border' => 0));
184 * Glue icon in front of text.
186 * @param string $protocol_or_url Protocol or URL. Used to determine the
188 * @param string $text The text.
189 * @return XmlContent.
191 function PossiblyGlueIconToText($proto_or_url, $text) {
192 $icon = IconForLink($proto_or_url);
194 preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
195 list (, $first_word, $tail) = $m;
196 $text = HTML::span(array('style' => 'white-space: nowrap'),
199 $text = HTML($text, $tail);
205 * Determines if the url passed to function is safe, by detecting if the characters
206 * '<', '>', or '"' are present.
208 * @param string $url URL to check for unsafe characters.
209 * @return boolean True if same, false else.
211 function IsSafeURL($url) {
212 return !ereg('[<>"]', $url);
216 * Generates an HtmlElement object to store data for a link.
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.
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 <, >, \"")));
230 $linktext = preg_replace("/mailto:/A", "", $url);
232 $link = HTML::a(array('href' => $url),
233 PossiblyGlueIconToText($url, $linktext));
236 $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
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 <, >, \"")));
250 $link = HTML::img(array('src' => $url, 'alt' => $alt));
252 $link->setAttr('class', 'inlineimage');
259 var $items = array();
262 function push($item) {
263 $this->items[$this->size] = $item;
269 if ($this->size == 0) {
270 return false; // stack is empty
273 return $this->items[$this->size];
282 return $this->items[$this->size - 1];
288 // end class definition
290 function SplitQueryArgs ($query_args = '')
292 $split_args = split('&', $query_args);
294 while (list($key, $val) = each($split_args))
295 if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
296 $args[$m[1]] = $m[2];
300 function LinkPhpwikiURL($url, $text = '', $basepage) {
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")));
310 $pagename = urldecode($m[1]);
313 if (empty($pagename) &&
314 preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)) {
315 // Convert old style links (to not break diff links in
317 $pagename = urldecode($m[2]);
318 $args = array("action" => $m[1]);
321 $args = SplitQueryArgs($qargs);
324 if (empty($pagename))
325 $pagename = $GLOBALS['request']->getArg('pagename');
327 if (isset($args['action']) && $args['action'] == 'browse')
328 unset($args['action']);
331 if (empty($args['action']))
333 else if (is_safe_action($args['action']))
334 $class = 'wikiaction';
336 if (empty($args['action']) || is_safe_action($args['action']))
337 $class = 'wikiaction';
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';
349 $text = HTML::span(array('class' => 'rawurl'), $url);
351 $wikipage = new WikiPageName($pagename);
352 if (!$wikipage->isValid()) {
354 return $Theme->linkBadWikiWord($wikipage, $url);
357 return HTML::a(array('href' => WikiURL($pagename, $args),
363 * A class to assist in parsing wiki pagenames.
365 * Now with subpages and anchors, parsing and passing around
366 * pagenames is more complicated. This should help.
370 /** Short name for page.
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.)
377 /** The full page name.
379 * This is the full name of the page (without anchor).
385 * This is the referenced anchor within the page, or the empty string.
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.
396 * @param string $anchor For links to anchors in page.
398 * @param mixed $basename Page name from which to interpret
399 * relative or other non-fully-specified page names.
401 function WikiPageName($name, $basename=false, $anchor=false) {
402 if (is_string($name)) {
403 $this->shortName = $name;
405 if ($anchor === false and preg_match('/\A(.*)#(.*?)?\Z/', $name, $m))
406 list(, $name, $anchor) = $m;
408 if (empty($name) or $name[0] == SUBPAGE_SEPARATOR) {
410 $name = $this->_pagename($basename) . $name;
412 $name = $this->_normalize_bad_pagename($name);
416 $name = $this->_pagename($name);
417 $this->shortName = $name;
420 $this->name = $this->_check($name);
421 $this->anchor = (string)$anchor;
424 function getParent() {
426 if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
428 return substr($name, 0, -strlen($tail));
431 function isValid($strict = false) {
433 return !isset($this->_errors);
434 return !empty($this->name);
437 function getWarnings() {
439 if (isset($this->_warnings))
440 $warnings = array_merge($warnings, $this->_warnings);
441 if (isset($this->_errors))
442 $warnings = array_merge($warnings, $this->_errors);
446 return sprintf(_("'%s': Bad page name: %s"),
447 $this->shortName, join(', ', $warnings));
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'))
457 if (!is_string($page)) {
458 print "PAGE: " . gettype($page) . " " . get_class($page) . "<br>\n";
460 //assert(is_string($page));
464 function _normalize_bad_pagename($name) {
465 trigger_error("Bad pagename: " . $name, E_USER_WARNING);
467 // Punt... You really shouldn't get here.
470 return $request->getArg('pagename');
472 assert($name[0] == SUBPAGE_SEPARATOR);
473 return substr($name, 1);
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");
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");
488 // Strip leading and trailing white-space.
489 $pagename = trim($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);
497 if (preg_match('/[:;]/', $pagename))
498 $this->_warnings[] = _("';' and ':' in pagenames are deprecated");
500 if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
501 $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
502 $this->_errors[] = _("too long");
506 if ($pagename == '.' or $pagename == '..') {
507 $this->_errors[] = sprintf(_("illegal pagename"), $pagename);
516 * Convert old page markup to new-style markup.
518 * @param string $text Old-style wiki markup.
520 * @param string $markup_type
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.
527 * @return string New-style wiki markup.
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....
533 function ConvertOldMarkup ($text, $markup_type = "block") {
539 /*****************************************************************
540 * Conversions for inline markup:
547 // escape escaped brackets
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) . '))/';
561 $subs["links"] = array($orig, $repl);
564 //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
567 // Convert footnote references.
568 $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
569 $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
571 // Convert old style emphases to HTML style emphasis.
572 $orig[] = '/__(.*?)__/';
573 $repl[] = '<strong>\\1</strong>';
574 $orig[] = "/''(.*?)''/";
575 $repl[] = '<em>\\1</em>';
577 // Escape nestled markup.
578 $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
581 // in old markup headings only allowed at beginning of line
585 $subs["inline"] = array($orig, $repl);
587 /*****************************************************************
588 * Patterns which match block markup constructs which take
589 * special handling...
593 $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
596 $blockpats[] = '\|(?:.*\n\|)*';
599 $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
601 // Footnote definitions
602 $blockpats[] = '\[\s*(\d+)\s*\]';
605 $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
608 $blockpats[] = '!{1,3}[^!]';
610 $block_re = ( '/\A((?:.|\n)*?)(^(?:'
611 . join("|", $blockpats)
616 if ($markup_type != "block") {
617 list ($orig, $repl) = $subs[$markup_type];
618 return preg_replace($orig, $repl, $text);
621 list ($orig, $repl) = $subs['inline'];
623 while (preg_match($block_re, $text, $m)) {
624 $text = substr($text, strlen($m[0]));
625 list (,$leading_text, $block) = $m;
628 if (strchr(" \t", $block[0])) {
631 $suffix = "\n</pre>\n";
633 elseif ($block[0] == '|') {
635 $prefix = "<?plugin OldStyleTable\n";
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]));
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;
651 $prefix = $indent . $bullet . ' ';
653 elseif ($block[0] == '[') {
654 // Footnote definition
655 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
657 $block = substr($block, strlen($m[0]));
658 $prefix = "#[|ftnt_${footnum}]~[[${footnum}|#ftnt_ref_${footnum}]~] ";
660 elseif ($block[0] == '<') {
662 // HACK: no inline markup...
666 elseif ($block[0] == '!') {
668 preg_match('/^!{1,3}/', $block, $m);
670 $block = substr($block, strlen($m[0]));
677 $out .= ( preg_replace($orig, $repl, $leading_text)
679 . preg_replace($orig, $repl, $block)
682 return $out . preg_replace($orig, $repl, $text);
688 * Expand tabs in string.
690 * Converts all tabs to (the appropriate number of) spaces.
693 * @param integer $tab_width
696 function expand_tabs($str, $tab_width = 8) {
697 $split = split("\t", $str);
698 $tail = array_pop($split);
700 foreach ($split as $hunk) {
702 $pos = strlen(strrchr($expanded, "\n")) - 1;
703 $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
705 return substr($expanded, 1) . $tail;
709 * Split WikiWords in page names.
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
715 * @param $page string The page name.
717 * @return string The split name.
719 function split_pagename ($page) {
721 if (preg_match("/\s/", $page))
722 return $page; // Already split --- don't split any more.
724 // FIXME: this algorithm is Anglo-centric.
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:]])/';
738 foreach ($RE as $key => $val)
739 $RE[$key] = pcre_fix_posix_classes($val);
742 foreach ($RE as $regexp) {
743 $page = preg_replace($regexp, '\\1 \\2', $page);
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());
759 * Get time offset for local time zone.
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.
765 function TimezoneOffset ($time = false, $no_colon = false) {
768 $secs = date('Z', $time);
777 $colon = $no_colon ? '' : ':';
778 $mins = intval(($secs + 30) / 60);
779 return sprintf("%s%02d%s%02d",
780 $sign, $mins / 60, $colon, $mins % 60);
785 * Format time in ISO-8601 format.
787 * @param $time time_t Time. Default: now.
788 * @return string Date and time in ISO-8601 format.
790 function Iso8601DateTime ($time = false) {
793 $tzoff = TimezoneOffset($time);
794 $date = date('Y-m-d', $time);
795 $time = date('H:i:s', $time);
796 return $date . 'T' . $time . $tzoff;
800 * Format time in RFC-2822 format.
802 * @param $time time_t Time. Default: now.
803 * @return string Date and time in RFC-2822 format.
805 function Rfc2822DateTime ($time = false) {
808 return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
812 * Format time in RFC-1123 format.
814 * @param $time time_t Time. Default: now.
815 * @return string Date and time in RFC-1123 format.
817 function Rfc1123DateTime ($time = false) {
820 return gmdate('D, d M Y H:i:s \G\M\T', $time);
823 /** Parse date in RFC-1123 format.
825 * According to RFC 1123 we must accept dates in the following
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
832 * (Though we're only allowed to generate dates in the first format.)
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',
839 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
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',
844 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
845 if ($year < 70) $year += 2000;
846 elseif ($year < 100) $year += 1900;
848 elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
849 .'(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
851 list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
858 $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
860 return false; // failed
865 * Format time to standard 'ctime' format.
867 * @param $time time_t Time. Default: now.
868 * @return string Date and time.
870 function CTime ($time = false)
874 return date("D M j H:i:s Y", $time);
880 * Internationalized printf.
882 * This is essentially the same as PHP's built-in printf
883 * with the following exceptions:
885 * <li> It passes the format string through gettext().
886 * <li> It supports the argument reordering extensions.
893 * __printf("Differences between versions %s and %s of %s",
894 * $new_link, $old_link, $page_link);
897 * Then in locale/po/de.po, one can reorder the printf arguments:
900 * msgid "Differences between %s and %s of %s."
901 * msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
904 * (Note that while PHP tries to expand $vars within double-quotes,
905 * the values in msgstr undergo no such expansion, so the '$'s
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....
912 function __printf ($fmt) {
913 $args = func_get_args();
915 echo __vsprintf($fmt, $args);
919 * Internationalized sprintf.
921 * This is essentially the same as PHP's built-in printf with the
922 * following exceptions:
925 * <li> It passes the format string through gettext().
926 * <li> It supports the argument reordering extensions.
931 function __sprintf ($fmt) {
932 $args = func_get_args();
934 return __vsprintf($fmt, $args);
938 * Internationalized vsprintf.
940 * This is essentially the same as PHP's built-in printf with the
941 * following exceptions:
944 * <li> It passes the format string through gettext().
945 * <li> It supports the argument reordering extensions.
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.
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
964 $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
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];
977 // Not all PHP's have vsprintf, so...
978 array_unshift($args, $fmt);
979 return call_user_func_array('sprintf', $args);
985 * Build an array in $this->_fileList of files from $dirname.
986 * Subdirectories are not traversed.
988 * (This was a function LoadDir in lib/loadsave.php)
989 * See also http://www.php.net/manual/en/function.readdir.php
991 function getFiles() {
992 return $this->_fileList;
995 function _filenameSelector($filename) {
996 if (! $this->_pattern)
999 return glob_match ($this->_pattern, $filename, $this->_case);
1003 function fileSet($directory, $filepattern = false) {
1004 $this->_fileList = array();
1005 $this->_pattern = $filepattern;
1006 $this->_case = !isWindows();
1007 $this->_pathsep = '/';
1009 if (empty($directory)) {
1010 trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1012 return; // early return
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
1022 while ($filename = readdir($dir_handle)) {
1023 if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1025 if ($this->_filenameSelector($filename)) {
1026 array_push($this->_fileList, "$filename");
1027 //trigger_error(sprintf(_("found file %s"), $filename),
1028 // E_USER_NOTICE); //debugging
1031 closedir($dir_handle);
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;
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;
1051 function expandRegex ($index, &$pages) {
1052 $this->index = $index;
1053 array_walk($pages, array($this, 'listMatchCallback'));
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))
1064 if (!preg_match('/[\?\*]$/',$glob))
1069 function glob_match ($glob, $against, $case_sensitive = true) {
1070 return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'), $against);
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++) {
1079 if (preg_match('/[\?\*]/',$f)) {
1081 $expand = new ListRegexExpand($list, $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1082 $expand->expandRegex($i, $allnames);
1089 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
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();
1099 return explodeList($input, $allPages);
1101 return explode(',',$input);
1105 // Class introspections
1107 /** Determine whether object is of a specified type.
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.
1114 function isa ($object, $class)
1116 $lclass = strtolower($class);
1118 return is_object($object)
1119 && ( get_class($object) == strtolower($lclass)
1120 || is_subclass_of($object, $lclass) );
1123 /** Determine whether (possible) object has method.
1125 * @param $object mixed Object
1126 * @param $method string Method name
1127 * @return bool True iff $object is an object with has method $method.
1129 function can ($object, $method)
1131 return is_object($object) && method_exists($object, strtolower($method));
1136 * This is used for generating ETags.
1138 function hash ($x) {
1139 if (is_scalar($x)) {
1142 elseif (is_array($x)) {
1144 return md5(serialize($x));
1146 elseif (is_object($x)) {
1149 trigger_error("Can't hash $x", E_USER_ERROR);
1154 * Seed the random number generator.
1156 * better_srand() ensures the randomizer is seeded only once.
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
1162 function better_srand($seed = '') {
1163 static $wascalled = FALSE;
1165 $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
1168 //trigger_error("new random seed", E_USER_NOTICE); //debugging
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
1178 function count_all($arg) {
1179 // skip if argument is empty
1181 //print_r($arg); //debugging
1183 // not an array, return 1 (base case)
1186 // else call recursively for all elements $arg
1187 foreach($arg as $key => $val)
1188 $count += count_all($val);
1193 function isSubPage($pagename) {
1194 return (strstr($pagename, SUBPAGE_SEPARATOR));
1197 function subPageSlice($pagename, $pos) {
1198 $pages = explode(SUBPAGE_SEPARATOR,$pagename);
1199 $pages = array_slice($pages,$pos,1);
1206 * Class for "popping up" and alert box. (Except that right now, it doesn't
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, ...)
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().
1223 function Alert($head, $body, $buttons=false) {
1224 if ($buttons === false)
1227 $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1228 $this->_buttons = $buttons;
1232 * Show the alert box.
1234 function show(&$request) {
1237 $tokens = $this->_tokens;
1238 $tokens['BUTTONS'] = $this->_getButtons();
1240 $request->discardOutput();
1241 $tmpl = new Template('dialog', $request, $tokens);
1247 function _getButtons() {
1250 $buttons = $this->_buttons;
1252 $buttons = array(_("Okay") => $request->getURLtoSelf());
1255 foreach ($buttons as $label => $url)
1256 print "$label $url\n";
1257 $out[] = $Theme->makeButton($label, $url, 'wikiaction');
1258 return new XmlContent($out);
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.
1269 // Revision 1.143 2003/02/26 00:10:26 dairiki
1270 // More/better/different checks for bad page names.
1272 // Revision 1.142 2003/02/25 22:19:46 dairiki
1273 // Add some sanity checking for pagenames.
1275 // Revision 1.141 2003/02/22 20:49:55 dairiki
1276 // Fixes for "Call-time pass by reference has been deprecated" errors.
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.)
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).
1286 // Revision 1.138 2003/02/21 04:12:36 dairiki
1287 // WikiPageName: fixes for new cached links.
1289 // Alert: new class for displaying alerts.
1291 // ExtractWikiPageLinks and friends are now gone.
1293 // LinkBracketLink moved to InlineParser.php
1295 // Revision 1.137 2003/02/18 23:13:40 dairiki
1296 // Wups again. Typo fix.
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'.
1303 // Now the ~ escape for page names should work: [Page ~#1].
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.
1310 // Revision 1.134 2003/02/16 19:44:20 dairiki
1311 // New function hash(). This is a helper, primarily for generating
1314 // Revision 1.133 2003/02/16 04:50:09 dairiki
1316 // Rfc1123DateTime(), ParseRfc1123DateTime()
1317 // for converting unix timestamps to and from strings.
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).
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).
1328 // (c-file-style: "gnu")
1332 // c-basic-offset: 4
1333 // c-hanging-comment-ender-p: nil
1334 // indent-tabs-mode: nil