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