]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/stdlib.php
Cleanup: updated function list, reindending, rewrapping in emacs.
[SourceForge/phpwiki.git] / lib / stdlib.php
1 <?php rcs_id('$Id: stdlib.php,v 1.70 2002-01-08 03:17:41 carstenklapp Exp $');
2
3 /*
4   Standard functions for Wiki functionality
5     DataURL($url)
6     BaseURL()
7     WikiURL($pagename, $args, $get_abs_url)
8     CSS_URL($CSS_URLS, $CSS_DEFAULT)
9     StartTag($tag, $args)
10     Element($tag, $args, $content)
11     QElement($tag, $args, $content)
12     IconForLink($protocol_or_url)
13     LinkURL($url, $linktext)
14     LinkWikiWord($wikiword, $linktext)
15     LinkExistingWikiWord($wikiword, $linktext)
16     LinkUnknownWikiWord($wikiword, $linktext)
17     LinkImage($url, $alt)
18     class Stack { push($item), pop(), cnt(), top() }
19     CookSpaces($pagearray)
20     MakeWikiForm ($pagename, $args, $class, $button_text)
21     SplitQueryArgs ($query_args)
22     LinkPhpwikiURL($url, $text)
23     ParseAndLink($bracketlink)
24     ExtractWikiPageLinks($content)
25     LinkRelatedPages($dbi, $pagename)
26     split_pagename ($page)
27     NoSuchRevision ($page, $version)
28     TimezoneOffset ($time, $no_colon)
29     Iso8601DateTime ($time)
30     Rfc2822DateTime ($time)
31     CTime ($time)
32     __printf ($fmt)
33     __sprintf ($fmt)
34     __vsprintf ($fmt, $args)
35
36   function moved => LinkInterWikiLink($link, $linktext)
37     (see /lib/interwiki.php)
38   function gone  => UpdateRecentChanges($dbi, $pagename, $isnewpage) 
39     (see /lib/plugin/RecentChanges.php)
40
41 */
42
43
44 function DataURL($url) {
45     if (preg_match('@^(\w+:|/)@', $url))
46         return $url;
47     return SERVER_URL . DATA_PATH . "/$url";
48 }
49
50 function BaseURL() {
51     return SERVER_URL . DATA_PATH . "/";
52 }
53
54 function WikiURL($pagename, $args = '', $get_abs_url = false) {
55     if (is_array($args)) {
56         $enc_args = array();
57         foreach  ($args as $key => $val) {
58             $enc_args[] = urlencode($key) . '=' . urlencode($val);
59         }
60         $args = join('&', $enc_args);
61     }
62     
63     if (USE_PATH_INFO) {
64         $url = $get_abs_url ?
65             SERVER_URL . VIRTUAL_PATH . "/" : VIRTUAL_PATH . "/";
66         $url .= rawurlencode($pagename);
67         if ($args)
68             $url .= "?$args";
69     }
70     else {
71         $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
72         $url .= "?pagename=" . rawurlencode($pagename);
73         if ($args)
74             $url .= "&$args";
75     }
76     return $url;
77 }
78
79 function CSS_URL($CSS_URLS, $CSS_DEFAULT) {
80     // FIXME: This is a kludge to allow xgettext to localize the title
81     // of the printer stylesheet. Think up a more elegant way which
82     // will also work with n stylesheets...
83     $PrinterStylesheetTitle = _("Printer");
84     
85     $html = "";
86     foreach  ($CSS_URLS as $key => $val) {
87         $html .= QElement('link',
88                           array('rel'   => (($CSS_DEFAULT == $key) ?
89                                             'stylesheet' : 'alternate stylesheet'),
90                                 'title' => htmlspecialchars(_($key)),
91                                 'href'  => DataURL(htmlspecialchars($val)),
92                                 'type'  => 'text/css'))."\n";
93     }
94     return $html;
95 }
96
97 define('NO_END_TAG_PAT',
98        '/^' . join('|', array('area', 'base', 'basefont',
99                               'br', 'col', 'frame',
100                               'hr', 'img', 'input',
101                               'isindex', 'link', 'meta',
102                               'param')) . '$/i');
103
104 function StartTag($tag, $args = '') {
105     $s = "<$tag";
106     if (is_array($args)) {
107         while (list($key, $val) = each($args))
108             {
109                 if (is_string($val) || is_numeric($val))
110                     $s .= sprintf(' %s="%s"', $key, htmlspecialchars($val));
111                 else if ($val)
112                     $s .= " $key=\"$key\"";
113             }
114     }
115     return "$s>";
116 }
117
118
119 function Element($tag, $args = '', $content = '') {
120     $html = "<$tag";
121     if (!is_array($args)) {
122         $content = $args;
123         $args = false;
124     }
125     $html = StartTag($tag, $args);
126     if (preg_match(NO_END_TAG_PAT, $tag)) {
127         assert(! $content);
128         return preg_replace('/>$/', " />", $html);
129     } else {
130         $html .= $content;
131         $html .= "</$tag>";//FIXME: newline might not always be desired.
132     }
133     return $html;
134 }
135
136 function QElement($tag, $args = '', $content = '')
137 {
138     if (is_array($args))
139         return Element($tag, $args, htmlspecialchars($content));
140     else {
141         $content = $args;
142         return Element($tag, htmlspecialchars($content));
143     }
144 }
145
146 function IconForLink($protocol_or_url) {
147     global $URL_LINK_ICONS;
148     
149     list ($proto) = explode(':', $protocol_or_url, 2);
150     
151     if (isset($URL_LINK_ICONS[$proto])) {
152         $linkimg = $URL_LINK_ICONS[$proto];
153     }
154     elseif (isset($URL_LINK_ICONS['*'])) {
155         $linkimg = $URL_LINK_ICONS['*'];
156     }
157     
158     if (empty($linkimg))
159         return '';
160     
161     return Element('img', array('src'   => DataURL($linkimg),
162                                 'alt'   => $proto,
163                                 'class' => 'linkicon'));
164 }
165
166
167 function LinkURL($url, $linktext = '') {
168     // FIXME: Is this needed (or sufficient?)
169     if(ereg("[<>\"]", $url)) {
170         return Element('strong',
171                        QElement('u', array('class' => 'baduri'),
172                                 _("BAD URL -- remove all of <, >, \"")));
173     }
174     
175     $attr['href'] = $url;
176     
177     if (empty($linktext)) {
178         $linktext = $url;
179         $attr['class'] = 'rawurl';
180     } else
181         $attr['class'] = 'namedurl';
182     
183     return Element('a', $attr,
184                    IconForLink($url) . htmlspecialchars($linktext));
185 }
186
187 function LinkWikiWord($wikiword, $linktext = '') {
188     global $dbi;
189     if ($dbi->isWikiPage($wikiword))
190         return LinkExistingWikiWord($wikiword, $linktext);
191     else
192         return LinkUnknownWikiWord($wikiword, $linktext);
193 }
194
195 function LinkExistingWikiWord($wikiword, $linktext = '') {
196     if (empty($linktext)) {
197         $linktext = $wikiword;
198     if (defined("autosplit_wikiwords"))
199             $linktext=split_pagename($linktext);
200         $class = 'wiki';
201     } else
202         $class = 'named-wiki';
203     
204     return QElement('a', array('href'  => WikiURL($wikiword),
205                                'class' => $class),
206                     $linktext);
207 }
208
209 function LinkUnknownWikiWord($wikiword, $linktext = '') {
210     if (empty($linktext)) {
211         $linktext = $wikiword;
212         if (defined("autosplit_wikiwords"))
213             $linktext=split_pagename($linktext);
214         $class = 'wikiunknown';
215     } else
216         $class = 'named-wikiunknown';
217     
218     return Element('span', array('class' => $class),
219                    QElement('a',
220                             array('href' => WikiURL($wikiword,
221                                                     array('action' => 'edit'))),
222                             '?') . Element('u', $linktext));
223 }
224
225 function LinkImage($url, $alt = '[External Image]') {
226     // FIXME: Is this needed (or sufficient?)
227     //  As long as the src in htmlspecialchars()ed I think it's safe.
228     if(ereg("[<>\"]", $url)) {
229         return Element('strong',
230                        QElement('u', array('class' => 'baduri'),
231                                 _("BAD URL -- remove all of <, >, \"")));
232     }
233     return Element('img', array('src' => $url, 'alt' => $alt));
234 }
235
236 // converts spaces to tabs
237 function CookSpaces($pagearray) {
238     return preg_replace("/ {3,8}/", "\t", $pagearray);
239 }
240
241
242 class Stack {
243     var $items = array();
244     var $size = 0;
245     
246     function push($item) {
247         $this->items[$this->size] = $item;
248         $this->size++;
249         return true;
250     }  
251     
252     function pop() {
253         if ($this->size == 0) {
254             return false; // stack is empty
255         }  
256         $this->size--;
257         return $this->items[$this->size];
258     }  
259     
260     function cnt() {
261         return $this->size;
262     }  
263     
264     function top() {
265         if($this->size)
266             return $this->items[$this->size - 1];
267         else
268             return '';
269     }
270     
271 }  
272 // end class definition
273
274
275 function MakeWikiForm ($pagename, $args, $class, $button_text = '') {
276     $formargs['action'] = USE_PATH_INFO ? WikiURL($pagename) : SCRIPT_NAME;
277     $formargs['method'] = 'get';
278     $formargs['class']  = $class;
279     
280     $contents = '';
281     $input_seen = 0;
282     
283     while (list($key, $val) = each($args)) {
284         $a = array('name' => $key, 'value' => $val, 'type' => 'hidden');
285         
286         if (preg_match('/^ (\d*) \( (.*) \) ((upload)?) $/xi', $val, $m)) {
287             $input_seen++;
288             $a['type'] = 'text';
289             $a['size'] = $m[1] ? $m[1] : 30;
290             $a['value'] = $m[2];
291             if ($m[3])
292                 {
293                     $a['type'] = 'file';
294                     $formargs['enctype'] = 'multipart/form-data';
295                     $contents .= Element('input',
296                                          array('name'  => 'MAX_FILE_SIZE',
297                                                'value' =>  MAX_UPLOAD_SIZE,
298                                                'type'  => 'hidden'));
299                     $formargs['method'] = 'post';
300                 }
301         }
302         
303         $contents .= Element('input', $a);
304     }
305     
306     $row = Element('td', $contents);
307     
308     if (!empty($button_text)) {
309         $row .= Element('td',
310                         Element('input', array('type'  => 'submit',
311                                                'class' => 'button',
312                                                'value' => $button_text)));
313     }
314     
315     return Element('form', $formargs,
316                    Element('table', array('cellspacing' => 0,
317                                           'cellpadding' => 2,
318                                           'border'      => 0),
319                            Element('tr', $row)));
320 }
321
322 function SplitQueryArgs ($query_args = '') 
323 {
324     $split_args = split('&', $query_args);
325     $args = array();
326     while (list($key, $val) = each($split_args))
327         if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
328             $args[$m[1]] = $m[2];
329     return $args;
330 }
331
332 function LinkPhpwikiURL($url, $text = '') {
333     $args = array();
334     
335     if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m))
336         return Element('strong',
337                        QElement('u', array('class' => 'baduri'),
338                                 'BAD phpwiki: URL'));
339     if ($m[1])
340         $pagename = urldecode($m[1]);
341     $qargs = $m[2];
342     
343     if (empty($pagename) &&
344         preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m))
345         {
346             // Convert old style links (to not break diff links in
347             // RecentChanges).
348             $pagename = urldecode($m[2]);
349             $args = array("action" => $m[1]);
350         }
351     else {
352         $args = SplitQueryArgs($qargs);
353     }
354     
355     if (empty($pagename))
356         $pagename = $GLOBALS['pagename'];
357     
358     if (isset($args['action']) && $args['action'] == 'browse')
359         unset($args['action']);
360     /*FIXME:
361       if (empty($args['action']))
362       $class = 'wikilink';
363       else if (is_safe_action($args['action']))
364       $class = 'wikiaction';
365     */
366     if (empty($args['action']) || is_safe_action($args['action']))
367         $class = 'wikiaction';
368     else {
369         // Don't allow administrative links on unlocked pages.
370         // FIXME: Ugh: don't like this...
371         global $dbi;
372         $page = $dbi->getPage($GLOBALS['pagename']);
373         if (!$page->get('locked'))
374             return QElement('u', array('class' => 'wikiunsafe'),
375                             _("Lock page to enable link"));
376         
377         $class = 'wikiadmin';
378     }
379     
380     // FIXME: ug, don't like this
381     if (preg_match('/=\d*\(/', $qargs))
382         return MakeWikiForm($pagename, $args, $class, $text);
383     if ($text)
384         $text = htmlspecialchars($text);
385     else
386         $text = QElement('span', array('class' => 'rawurl'), $url);
387     
388     return Element('a', array('href'  => WikiURL($pagename, $args),
389                               'class' => $class),
390                    $text);
391 }
392
393 function ParseAndLink($bracketlink) {
394     global $dbi, $AllowedProtocols, $InlineImages;
395     global $InterWikiLinkRegexp;
396     
397     // $bracketlink will start and end with brackets; in between will
398     // be either a page name, a URL or both separated by a pipe.
399     
400     // strip brackets and leading space
401     preg_match("/(\[\s*)(.+?)(\s*\])/", $bracketlink, $match);
402     // match the contents 
403     preg_match("/([^|]+)(\|)?([^|]+)?/", $match[2], $matches);
404     
405     if (isset($matches[3])) {
406         // named link of the form  "[some link name | http://blippy.com/]"
407         $URL = trim($matches[3]);
408         $linkname = trim($matches[1]);
409           $linktype = 'named';
410     } else {
411         // unnamed link of the form "[http://blippy.com/] or [wiki page]"
412         $URL = trim($matches[1]);
413         $linkname = '';
414         $linktype = 'simple';
415     }
416     
417     if ($dbi->isWikiPage($URL)) {
418         $link['type'] = "wiki-$linktype";
419         $link['link'] = LinkExistingWikiWord($URL, $linkname);
420     } elseif (preg_match("#^($AllowedProtocols):#", $URL)) {
421         // if it's an image, embed it; otherwise, it's a regular link
422         if (preg_match("/($InlineImages)$/i", $URL)) {
423             $link['type'] = "image-$linktype";
424               $link['link'] = LinkImage($URL, $linkname);
425         } else {
426             $link['type'] = "url-$linktype";
427             $link['link'] = LinkURL($URL, $linkname);
428         }
429     } elseif (preg_match("#^phpwiki:(.*)#", $URL, $match)) {
430         $link['type'] = "url-wiki-$linktype";
431         $link['link'] = LinkPhpwikiURL($URL, $linkname);
432     } elseif (preg_match("#^\d+$#", $URL)) {
433         $link['type'] = "footnote-$linktype";
434         $link['link'] = $URL;
435     } elseif (function_exists('LinkInterWikiLink') &&
436               preg_match("#^$InterWikiLinkRegexp:#", $URL)) {
437           $link['type'] = "interwiki-$linktype";
438           $link['link'] = LinkInterWikiLink($URL, $linkname);
439       } else {
440           $link['type'] = "wiki-unknown-$linktype";
441           $link['link'] = LinkUnknownWikiWord($URL, $linkname);
442       }
443     
444     return $link;
445 }
446
447
448 function ExtractWikiPageLinks($content) {
449     global $WikiNameRegexp;
450     
451     if (is_string($content))
452         $content = explode("\n", $content);
453     
454     $wikilinks = array();
455     foreach ($content as $line) {
456         // remove plugin code
457         $line = preg_replace('/<\?plugin\s+\w.*?\?>/', '', $line);
458         // remove escaped '['
459         $line = str_replace('[[', ' ', $line);
460         
461         // bracket links (only type wiki-* is of interest)
462         $numBracketLinks = preg_match_all("/\[\s*([^\]|]+\|)?\s*(\S.*?)\s*\]/",
463                                           $line, $brktlinks);
464         for ($i = 0; $i < $numBracketLinks; $i++) {
465             $link = ParseAndLink($brktlinks[0][$i]);
466             if (preg_match("#^wiki#", $link['type']))
467                 $wikilinks[$brktlinks[2][$i]] = 1;
468             
469             $brktlink = preg_quote($brktlinks[0][$i]);
470             $line = preg_replace("|$brktlink|", '', $line);
471         }
472         
473         // BumpyText old-style wiki links
474         if (preg_match_all("/!?$WikiNameRegexp/", $line, $link)) {
475             for ($i = 0; isset($link[0][$i]); $i++) {
476                 if($link[0][$i][0] <> '!')
477                     $wikilinks[$link[0][$i]] = 1;
478             }
479         }
480     }
481     return array_keys($wikilinks);
482 }      
483
484 function LinkRelatedPages($dbi, $pagename)
485 {
486     // currently not supported everywhere
487     if(!function_exists('GetWikiPageLinks'))
488         return '';
489     
490     //FIXME: fix or toss?
491     $links = GetWikiPageLinks($dbi, $pagename);
492     
493     $txt = QElement('strong',
494                     sprintf (_("%d best incoming links:"), NUM_RELATED_PAGES));
495     for($i = 0; $i < NUM_RELATED_PAGES; $i++) {
496         if(isset($links['in'][$i])) {
497             list($name, $score) = $links['in'][$i];
498             $txt .= LinkExistingWikiWord($name) . " ($score), ";
499         }
500     }
501     
502     $txt .= "\n" . Element('br');
503     $txt .= Element('strong',
504                     sprintf (_("%d best outgoing links:"), NUM_RELATED_PAGES));
505     for($i = 0; $i < NUM_RELATED_PAGES; $i++) {
506         if(isset($links['out'][$i])) {
507             list($name, $score) = $links['out'][$i];
508         if($dbi->isWikiPage($name))
509                 $txt .= LinkExistingWikiWord($name) . " ($score), ";
510         }
511     }
512     
513     $txt .= "\n" . Element('br');
514     $txt .= Element('strong',
515                     sprintf (_("%d most popular nearby:"), NUM_RELATED_PAGES));
516     for($i = 0; $i < NUM_RELATED_PAGES; $i++) {
517         if(isset($links['popular'][$i])) {
518             list($name, $score) = $links['popular'][$i];
519         $txt .= LinkExistingWikiWord($name) . " ($score), ";
520         }
521     }
522     
523     return $txt;
524 }
525
526
527 /**
528  * Split WikiWords in page names.
529  *
530  * It has been deemed useful to split WikiWords (into "Wiki Words") in
531  * places like page titles. This is rumored to help search engines
532  * quite a bit.
533  *
534  * @param $page string The page name.
535  *
536  * @return string The split name.
537  */
538 function split_pagename ($page) {
539     
540     if (preg_match("/\s/", $page))
541         return $page;           // Already split --- don't split any more.
542     
543     // FIXME: this algorithm is Anglo-centric.
544     static $RE;
545     if (!isset($RE)) {
546         // This mess splits between a lower-case letter followed by
547         // either an upper-case or a numeral; except that it wont
548         // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
549         $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
550         // This the single-letter words 'I' and 'A' from any following
551         // capitalized words.
552         $RE[] = '/(?: |^)([AI])([[:upper:]])/';
553         // Split numerals from following letters.
554         $RE[] = '/(\d)([[:alpha:]])/';
555         
556         foreach ($RE as $key => $val)
557             $RE[$key] = pcre_fix_posix_classes($val);
558     }
559     
560     foreach ($RE as $regexp)
561         $page = preg_replace($regexp, '\\1 \\2', $page);
562     return $page;
563 }
564
565 function NoSuchRevision ($page, $version) {
566     $html = Element('p', QElement('strong', gettext("Bad Version"))) . "\n";
567     $html .= QElement('p',
568                       sprintf(_("I'm sorry.  Version %d of %s is not in my database."),
569                               $version, $page->getName())) . "\n";
570     
571     include_once('lib/Template.php');
572     echo GeneratePage('MESSAGE', $html, _("Bad Version"));
573     ExitWiki ("");
574 }
575
576
577 /**
578  * Get time offset for local time zone.
579  *
580  * @param $time time_t Get offset for this time. Default: now.
581  * @param $no_colon boolean Don't put colon between hours and minutes.
582  * @return string Offset as a string in the format +HH:MM.
583  */
584 function TimezoneOffset ($time = false, $no_colon = false) {
585     if ($time === false)
586         $time = time();
587     $secs = date('Z', $time);
588     if ($secs < 0) {
589         $sign = '-';
590         $secs = -$secs;
591     }
592     else {
593         $sign = '+';
594     }
595     $colon = $no_colon ? '' : ':';
596     $mins = intval(($secs + 30) / 60);
597     return sprintf("%s%02d%s%02d",
598                    $sign, $mins / 60, $colon, $mins % 60);
599 }
600
601 /**
602  * Format time in ISO-8601 format.
603  *
604  * @param $time time_t Time.  Default: now.
605  * @return string Date and time in ISO-8601 format.
606  */
607 function Iso8601DateTime ($time = false) {
608     if ($time === false)
609         $time = time();
610     $tzoff = TimezoneOffset($time);
611     $date  = date('Y-m-d', $time);
612     $time  = date('H:i:s', $time);
613     return $date . 'T' . $time . $tzoff;
614 }
615
616 /**
617  * Format time in RFC-2822 format.
618  *
619  * @param $time time_t Time.  Default: now.
620  * @return string Date and time in RFC-2822 format.
621  */
622 function Rfc2822DateTime ($time = false) {
623     if ($time === false)
624         $time = time();
625     return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
626 }
627
628 /**
629  * Format time to standard 'ctime' format.
630  *
631  * @param $time time_t Time.  Default: now.
632  * @return string Date and time in RFC-2822 format.
633  */
634 function CTime ($time = false)
635 {
636     if ($time === false)
637         $time = time();
638     return date("D M j H:i:s Y", $time);
639 }
640
641
642
643 /**
644  * Internationalized printf.
645  *
646  * This is essentially the same as PHP's built-in printf
647  * with the following exceptions:
648  * <ol>
649  * <li> It passes the format string through gettext().
650  * <li> It supports the argument reordering extensions.
651  * </ol>
652  *
653  * Example:
654  *
655  * In php code, use:
656  * <pre>
657  *    __printf("Differences between versions %s and %s of %s",
658  *             $new_link, $old_link, $page_link);
659  * </pre>
660  *
661  * Then in locale/po/de.po, one can reorder the printf arguments:
662  *
663  * <pre>
664  *    msgid "Differences between %s and %s of %s."
665  *    msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
666  * </pre>
667  *
668  * (Note that while PHP tries to expand $vars within double-quotes,
669  * the values in msgstr undergo no such expansion, so the '$'s
670  * okay...)
671  *
672  * One shouldn't use reordered arguments in the default format string.
673  * Backslashes in the default string would be necessary to escape the
674  * '$'s, and they'll cause all kinds of trouble....
675  */ 
676 function __printf ($fmt) {
677     $args = func_get_args();
678     array_shift($args);
679     echo __vsprintf($fmt, $args);
680 }
681
682 /**
683  * Internationalized sprintf.
684  *
685  * This is essentially the same as PHP's built-in printf with the
686  * following exceptions:
687  *
688  * <ol>
689  * <li> It passes the format string through gettext().
690  * <li> It supports the argument reordering extensions.
691  * </ol>
692  *
693  * @see __printf
694  */ 
695 function __sprintf ($fmt) {
696     $args = func_get_args();
697     array_shift($args);
698     return __vsprintf($fmt, $args);
699 }
700
701 /**
702  * Internationalized vsprintf.
703  *
704  * This is essentially the same as PHP's built-in printf with the
705  * following exceptions:
706  *
707  * <ol>
708  * <li> It passes the format string through gettext().
709  * <li> It supports the argument reordering extensions.
710  * </ol>
711  *
712  * @see __printf
713  */ 
714 function __vsprintf ($fmt, $args) {
715     $fmt = gettext($fmt);
716     // PHP's sprintf doesn't support variable with specifiers,
717     // like sprintf("%*s", 10, "x"); --- so we won't either.
718     
719     if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
720         // Format string has '%2$s' style argument reordering.
721         // PHP doesn't support this.
722         if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
723             // literal variable name substitution only to keep locale
724             // strings uncluttered
725             trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
726                                   '%1\$s','%s'), E_USER_WARNING);
727         
728         $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
729         $newargs = array();
730         
731         // Reorder arguments appropriately.
732         foreach($m[1] as $argnum) {
733             if ($argnum < 1 || $argnum > count($args))
734                 trigger_error(sprintf(_("%s: argument index out of range"), 
735                                       $argnum), E_USER_WARNING);
736             $newargs[] = $args[$argnum - 1];
737         }
738         $args = $newargs;
739     }
740     
741     // Not all PHP's have vsprintf, so...
742     array_unshift($args, $fmt);
743     return call_user_func_array('sprintf', $args);
744 }
745
746
747 // (c-file-style: "gnu")
748 // Local Variables:
749 // mode: php
750 // tab-width: 8
751 // c-basic-offset: 4
752 // c-hanging-comment-ender-p: nil
753 // indent-tabs-mode: nil
754 // End:   
755 ?>