1 <?php //rcs_id('$Id: stdlib.php,v 1.233 2005-02-02 20:40:12 rurban Exp $');
3 Copyright 1999,2000,2001,2002,2004,2005 $ThePhpWikiProgrammingTeam
5 This file is part of PhpWiki.
7 PhpWiki is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 PhpWiki is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with PhpWiki; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 Standard functions for Wiki functionality
24 WikiURL ($pagename, $args, $get_abs_url)
26 IconForLink ($protocol_or_url)
27 PossiblyGlueIconToText($proto_or_url, $text)
29 LinkURL ($url, $linktext)
30 LinkImage ($url, $alt)
32 SplitQueryArgs ($query_args)
33 LinkPhpwikiURL ($url, $text, $basepage)
34 ConvertOldMarkup ($content, $markup_type = "block")
35 MangleXmlIdentifier($str)
36 UnMangleXmlIdentifier($str)
38 class Stack { push($item), pop(), cnt(), top() }
39 class Alert { show() }
40 class WikiPageName {getParent(),isValid(),getWarnings() }
42 expand_tabs($str, $tab_width = 8)
44 NoSuchRevision ($request, $page, $version)
45 TimezoneOffset ($time, $no_colon)
46 Iso8601DateTime ($time)
47 Rfc2822DateTime ($time)
48 ParseRfc1123DateTime ($timestr)
50 ByteFormatter ($bytes = 0, $longformat = false)
53 __vsprintf ($fmt, $args)
55 file_mtime ($filename)
56 sort_file_mtime ($a, $b)
57 class fileSet {fileSet($directory, $filepattern = false), getFiles($exclude=false, $sortby=false, $limit=false) }
58 class ListRegexExpand { listMatchCallback($item, $key), expandRegex ($index, &$pages) }
61 glob_match ($glob, $against, $case_sensitive = true)
62 explodeList ($input, $allnames, $glob_style = true, $case_sensitive = true)
63 explodePageList ($input, $perm = false)
65 can ($object, $method)
66 function_usable ($function_name)
68 better_srand ($seed = '')
71 subPageSlice ($pagename, $pos)
75 obj2hash ($obj, $exclude = false, $fields = false)
78 url_get_contents ($uri)
80 firstNWordsOfContent ($n, $content)
81 extractSection ($section, $content, $page, $quiet = false, $sectionhead = false)
84 function: LinkInterWikiLink($link, $linktext)
85 moved to: lib/interwiki.php
86 function: linkExistingWikiWord($wikiword, $linktext, $version)
87 moved to: lib/Theme.php
88 function: LinkUnknownWikiWord($wikiword, $linktext)
89 moved to: lib/Theme.php
90 function: UpdateRecentChanges($dbi, $pagename, $isnewpage)
91 gone see: lib/plugin/RecentChanges.php
93 if (defined('_PHPWIKI_STDLIB_LOADED')) return;
94 else define('_PHPWIKI_STDLIB_LOADED', true);
96 define('MAX_PAGENAME_LENGTH', 100);
99 * Convert string to a valid XML identifier.
101 * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
103 * We would like to have, e.g. named anchors within wiki pages
104 * names like "Table of Contents" --- clearly not a valid XML
105 * fragment identifier.
107 * This function implements a one-to-one map from {any string}
108 * to {valid XML identifiers}.
111 * converting all bytes not in [A-Za-z0-9:_-],
112 * and any leading byte not in [A-Za-z] to 'xbb.',
113 * where 'bb' is the hexadecimal representation of the
116 * As a special case, the empty string is converted to 'empty.'
121 function MangleXmlIdentifier($str) {
125 return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
126 "'x' . sprintf('%02x', ord('\\0')) . '.'",
130 function UnMangleXmlIdentifier($str) {
131 if ($str == 'empty.')
133 return preg_replace('/x(\w\w)\./e',
134 "sprintf('%c', hex('\\0'))",
139 * Generates a valid URL for a given Wiki pagename.
140 * @param mixed $pagename If a string this will be the name of the Wiki page to link to.
141 * If a WikiDB_Page object function will extract the name to link to.
142 * If a WikiDB_PageRevision object function will extract the name to link to.
144 * @param boolean $get_abs_url Default value is false.
145 * @return string The absolute URL to the page passed as $pagename.
147 function WikiURL($pagename, $args = '', $get_abs_url = false) {
150 if (is_object($pagename)) {
151 if (isa($pagename, 'WikiDB_Page')) {
152 $pagename = $pagename->getName();
154 elseif (isa($pagename, 'WikiDB_PageRevision')) {
155 $page = $pagename->getPage();
156 $args['version'] = $pagename->getVersion();
157 $pagename = $page->getName();
159 elseif (isa($pagename, 'WikiPageName')) {
160 $anchor = $pagename->anchor;
161 $pagename = $pagename->name;
163 $anchor = $pagename->anchor;
164 $pagename = $pagename->name;
167 if (!$get_abs_url and DEBUG and $GLOBALS['request']->getArg('start_debug')) {
169 $args = 'start_debug=' . $GLOBALS['request']->getArg('start_debug');
170 elseif (is_array($args))
171 $args['start_debug'] = $GLOBALS['request']->getArg('start_debug');
173 $args .= '&start_debug=' . $GLOBALS['request']->getArg('start_debug');
175 if (is_array($args)) {
177 foreach ($args as $key => $val) {
178 // avoid default args
179 if (USE_PATH_INFO and $key == 'pagename')
181 elseif ($key == 'action' and $val == 'browse')
183 elseif (!is_array($val)) // ugly hack for getURLtoSelf() which also takes POST vars
184 $enc_args[] = urlencode($key) . '=' . urlencode($val);
186 $args = join('&', $enc_args);
189 if (USE_PATH_INFO or !empty($GLOBALS['WikiTheme']->HTML_DUMP_SUFFIX)) {
190 $url = $get_abs_url ? (SERVER_URL . VIRTUAL_PATH . "/") : "";
191 $url = $url . preg_replace('/%2f/i', '/', rawurlencode($pagename));
192 if (!empty($GLOBALS['WikiTheme']->HTML_DUMP_SUFFIX))
193 $url .= $GLOBALS['WikiTheme']->HTML_DUMP_SUFFIX;
198 $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
199 $url .= "?pagename=" . rawurlencode($pagename);
204 $url .= "#" . MangleXmlIdentifier($anchor);
208 /** Convert relative URL to absolute URL.
210 * This converts a relative URL to one of PhpWiki's support files
211 * to an absolute one.
214 * @return string Absolute URL
216 function AbsoluteURL ($url) {
217 if (preg_match('/^https?:/', $url))
219 if ($url[0] != '/') {
220 $base = USE_PATH_INFO ? VIRTUAL_PATH : dirname(SCRIPT_NAME);
221 while ($base != '/' and substr($url, 0, 3) == "../") {
222 $url = substr($url, 3);
223 $base = dirname($base);
229 return SERVER_URL . $url;
232 function DataURL ($url) {
233 if (preg_match('/^https?:/', $url))
235 $url = NormalizeWebFileName($url);
236 if (DEBUG and $GLOBALS['request']->getArg('start_debug') and substr($url,-4,4) == '.php')
237 $url .= "?start_debug=1"; // XMLRPC and SOAP debugging helper.
238 return AbsoluteURL($url);
242 * Generates icon in front of links.
244 * @param string $protocol_or_url URL or protocol to determine which icon to use.
246 * @return HtmlElement HtmlElement object that contains data to create img link to
247 * icon for use with url or protocol passed to the function. False if no img to be
250 function IconForLink($protocol_or_url) {
252 if (0 and $filename_suffix == false) {
253 // display apache style icon for file type instead of protocol icon
254 // - archive: unix:gz,bz2,tgz,tar,z; mac:dmg,dmgz,bin,img,cpt,sit; pc:zip;
255 // - document: html, htm, text, txt, rtf, pdf, doc
256 // - non-inlined image: jpg,jpeg,png,gif,tiff,tif,swf,pict,psd,eps,ps
257 // - audio: mp3,mp2,aiff,aif,au
258 // - multimedia: mpeg,mpg,mov,qt
260 list ($proto) = explode(':', $protocol_or_url, 2);
261 $src = $WikiTheme->getLinkIconURL($proto);
263 return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon', 'border' => 0));
270 * Glue icon in front of or after text.
271 * Pref: 'noLinkIcons' - ignore icon if set
272 * Theme: 'LinkIcons' - 'yes' at front
273 * - 'no' display no icon
274 * - 'front' display at left
275 * - 'after' display at right
277 * @param string $protocol_or_url Protocol or URL. Used to determine the
279 * @param string $text The text.
280 * @return XmlContent.
282 function PossiblyGlueIconToText($proto_or_url, $text) {
283 global $request, $WikiTheme;
284 if ($request->getPref('noLinkIcons'))
286 $icon = IconForLink($proto_or_url);
289 if ($where = $WikiTheme->getLinkIconAttr()) {
290 if ($where == 'no') return $text;
291 if ($where != 'after') $where = 'front';
295 if ($where == 'after') {
296 // span the icon only to the last word (tie them together),
297 // to let the previous words wrap on line breaks.
298 if (!is_object($text)) {
299 preg_match('/^(\s*\S*)(\s*)$/', $text, $m);
300 list (, $prefix, $last_word) = $m;
306 $text = HTML::span(array('style' => 'white-space: nowrap'),
307 $last_word, HTML::Raw(' '), $icon);
309 $text = HTML($prefix, $text);
312 // span the icon only to the first word (tie them together),
313 // to let the next words wrap on line breaks
314 if (!is_object($text)) {
315 preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
316 list (, $first_word, $tail) = $m;
322 $text = HTML::span(array('style' => 'white-space: nowrap'),
325 $text = HTML($text, $tail);
330 * Determines if the url passed to function is safe, by detecting if the characters
331 * '<', '>', or '"' are present.
333 * @param string $url URL to check for unsafe characters.
334 * @return boolean True if same, false else.
336 function IsSafeURL($url) {
337 return !preg_match('/[<>"]/', $url);
341 * Generates an HtmlElement object to store data for a link.
343 * @param string $url URL that the link will point to.
344 * @param string $linktext Text to be displayed as link.
345 * @return HtmlElement HtmlElement object that contains data to construct an html link.
347 function LinkURL($url, $linktext = '') {
348 // FIXME: Is this needed (or sufficient?)
349 if(! IsSafeURL($url)) {
350 $link = HTML::strong(HTML::u(array('class' => 'baduri'),
351 _("BAD URL -- remove all of <, >, \"")));
355 $linktext = preg_replace("/mailto:/A", "", $url);
357 $link = HTML::a(array('href' => $url),
358 PossiblyGlueIconToText($url, $linktext));
361 $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
366 * FIXME: disallow sizes which are too small.
367 * Spammers may use such (typically invisible) image attributes to higher their GoogleRank.
369 function LinkImage($url, $alt = false) {
370 // FIXME: Is this needed (or sufficient?)
371 if(! IsSafeURL($url)) {
372 $link = HTML::strong(HTML::u(array('class' => 'baduri'),
373 _("BAD URL -- remove all of <, >, \"")));
375 // support new syntax: [image.jpg size=50% border=n]
376 $arr = split(' ',$url);
377 if (count($arr) > 1) {
380 if (empty($alt)) $alt = basename($url);
381 $link = HTML::img(array('src' => $url, 'alt' => $alt));
382 if (count($arr) > 1) {
384 foreach ($arr as $attr) {
385 if (preg_match('/^size=(\d+%)$/',$attr,$m)) {
386 $link->setAttr('width',$m[1]);
387 $link->setAttr('height',$m[1]);
389 if (preg_match('/^size=(\d+)x(\d+)$/',$attr,$m)) {
390 $link->setAttr('width',$m[1]);
391 $link->setAttr('height',$m[2]);
393 if (preg_match('/^border=(\d+)$/',$attr,$m))
394 $link->setAttr('border',$m[1]);
395 if (preg_match('/^align=(\w+)$/',$attr,$m))
396 $link->setAttr('align',$m[1]);
397 if (preg_match('/^hspace=(\d+)$/',$attr,$m))
398 $link->setAttr('hspace',$m[1]);
399 if (preg_match('/^vspace=(\d+)$/',$attr,$m))
400 $link->setAttr('vspace',$m[1]);
403 // check width and height as spam countermeasure
404 if (($width = $link->getAttr('width')) and ($height = $link->getAttr('height'))) {
405 //$width = (int) $width; // px or % or other suffix
406 //$height = (int) $height;
407 if (($width < 3 and $height < 10) or
408 ($height < 3 and $width < 20) or
409 ($height < 7 and $width < 7))
411 trigger_error(_("Invalid image size"), E_USER_NOTICE);
415 // Older php versions crash here with certain png's:
416 // confirmed for 4.1.2, 4.1.3, 4.2.3; 4.3.2 and 4.3.7 are ok
417 // http://phpwiki.sourceforge.net/demo/themes/default/images/http.png
418 // See http://bugs.php.net/search.php?cmd=display&search_for=getimagesize
419 if (!check_php_version(4,3) and preg_match("/^http.+\.png$/i",$url))
420 ; // it's safe to assume that this will fail.
421 elseif (!DISABLE_GETIMAGESIZE and ($size = @getimagesize($url))) {
424 if (($width < 3 and $height < 10)
425 or ($height < 3 and $width < 20)
426 or ($height < 7 and $width < 7))
428 trigger_error(_("Invalid image size"), E_USER_NOTICE);
434 $link->setAttr('class', 'inlineimage');
442 // var in php5 deprecated
444 $this->items = array();
447 function push($item) {
448 $this->items[$this->size] = $item;
454 if ($this->size == 0) {
455 return false; // stack is empty
458 return $this->items[$this->size];
467 return $this->items[$this->size - 1];
473 // end class definition
475 function SplitQueryArgs ($query_args = '')
477 $split_args = split('&', $query_args);
479 while (list($key, $val) = each($split_args))
480 if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
481 $args[$m[1]] = $m[2];
485 function LinkPhpwikiURL($url, $text = '', $basepage = false) {
488 if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m)) {
489 return HTML::strong(array('class' => 'rawurl'),
490 HTML::u(array('class' => 'baduri'),
491 _("BAD phpwiki: URL")));
495 $pagename = urldecode($m[1]);
498 if (empty($pagename) &&
499 preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)) {
500 // Convert old style links (to not break diff links in
502 $pagename = urldecode($m[2]);
503 $args = array("action" => $m[1]);
506 $args = SplitQueryArgs($qargs);
509 if (empty($pagename))
510 $pagename = $GLOBALS['request']->getArg('pagename');
512 if (isset($args['action']) && $args['action'] == 'browse')
513 unset($args['action']);
516 if (empty($args['action']))
518 else if (is_safe_action($args['action']))
519 $class = 'wikiaction';
521 if (empty($args['action']) || is_safe_action($args['action']))
522 $class = 'wikiaction';
524 // Don't allow administrative links on unlocked pages.
525 $dbi = $GLOBALS['request']->getDbh();
526 $page = $dbi->getPage($basepage ? $basepage : $pagename);
527 if (!$page->get('locked'))
528 return HTML::span(array('class' => 'wikiunsafe'),
529 HTML::u(_("Lock page to enable link")));
530 $class = 'wikiadmin';
534 $text = HTML::span(array('class' => 'rawurl'), $url);
536 $wikipage = new WikiPageName($pagename);
537 if (!$wikipage->isValid()) {
539 return $WikiTheme->linkBadWikiWord($wikipage, $url);
542 return HTML::a(array('href' => WikiURL($pagename, $args),
548 * A class to assist in parsing wiki pagenames.
550 * Now with subpages and anchors, parsing and passing around
551 * pagenames is more complicated. This should help.
555 /** Short name for page.
557 * This is the value of $name passed to the constructor.
558 * (For use, e.g. as a default label for links to the page.)
562 /** The full page name.
564 * This is the full name of the page (without anchor).
570 * This is the referenced anchor within the page, or the empty string.
576 * @param mixed $name Page name.
577 * WikiDB_Page, WikiDB_PageRevision, or string.
578 * This can be a relative subpage name (like '/SubPage'),
579 * or can be the empty string to refer to the $basename.
581 * @param string $anchor For links to anchors in page.
583 * @param mixed $basename Page name from which to interpret
584 * relative or other non-fully-specified page names.
586 function WikiPageName($name, $basename=false, $anchor=false) {
587 if (is_string($name)) {
588 $this->shortName = $name;
589 if (strstr($name, ':')) {
590 $this->shortName = substr(strstr($name, ':'), 1);
591 $map = getInterwikiMap(); // allow overrides to custom maps
592 $link = $map->link($name);
593 $url = $link->getAttr('href');
594 if (strstr($url, '?'))
595 list($name,) = explode("?", $url);
596 // expand Talk or User, but not to absolute urls!
597 if (strstr($url, '//')) {
599 $name = $name . SUBPAGE_SEPARATOR . _("Discussion");
600 elseif ($m[1] == 'User')
607 if ($name == '' or $name[0] == SUBPAGE_SEPARATOR) {
609 $name = $this->_pagename($basename) . $name;
611 $name = $this->_normalize_bad_pagename($name);
615 $name = $this->_pagename($name);
616 $this->shortName = $name;
619 $this->name = $this->_check($name);
620 $this->anchor = (string)$anchor;
627 function getParent() {
629 if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
631 return substr($name, 0, -strlen($tail));
634 function isValid($strict = false) {
636 return !isset($this->_errors);
637 return (is_string($this->name) and $this->name != '');
640 function getWarnings() {
642 if (isset($this->_warnings))
643 $warnings = array_merge($warnings, $this->_warnings);
644 if (isset($this->_errors))
645 $warnings = array_merge($warnings, $this->_errors);
649 return sprintf(_("'%s': Bad page name: %s"),
650 $this->shortName, join(', ', $warnings));
653 function _pagename($page) {
654 if (isa($page, 'WikiDB_Page'))
655 return $page->getName();
656 elseif (isa($page, 'WikiDB_PageRevision'))
657 return $page->getPageName();
658 elseif (isa($page, 'WikiPageName'))
660 if (!is_string($page)) {
661 trigger_error(sprintf("Non-string pagename '%s' (%s)(%s)",
662 $page, gettype($page), get_class($page)),
665 //assert(is_string($page));
669 function _normalize_bad_pagename($name) {
670 trigger_error("Bad pagename: " . $name, E_USER_WARNING);
672 // Punt... You really shouldn't get here.
675 return $request->getArg('pagename');
677 assert($name[0] == SUBPAGE_SEPARATOR);
678 return substr($name, 1);
682 function _check($pagename) {
683 // Compress internal white-space to single space character.
684 $pagename = preg_replace('/[\s\xa0]+/', ' ', $orig = $pagename);
685 if ($pagename != $orig)
686 $this->_warnings[] = _("White space converted to single space");
688 // Delete any control characters.
689 if (DATABASE_TYPE == 'cvs' or DATABASE_TYPE == 'file') {
690 $pagename = preg_replace('/[\x00-\x1f\x7f\x80-\x9f]/', '', $orig = $pagename);
691 if ($pagename != $orig)
692 $this->_errors[] = _("Control characters not allowed");
695 // Strip leading and trailing white-space.
696 $pagename = trim($pagename);
699 while ($pagename and $pagename[0] == SUBPAGE_SEPARATOR)
700 $pagename = substr($pagename, 1);
701 if ($pagename != $orig)
702 $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
704 // ";" is urlencoded, so safe from php arg-delim problems
705 /*if (strstr($pagename, ';')) {
706 $this->_warnings[] = _("';' is deprecated");
707 $pagename = str_replace(';', '', $pagename);
710 // not only for the db backend, also to restrict url length
711 if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
712 $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
713 $this->_errors[] = _("too long");
716 // disallow some chars only on file and cvs
717 if ((DATABASE_TYPE == 'cvs' or DATABASE_TYPE == 'file')
718 and preg_match('/(:|\.\.)/', $pagename, $m)) {
719 $this->_warnings[] = sprintf(_("Illegal chars %s removed"), $m[1]);
720 $pagename = str_replace('..', '', $pagename);
721 $pagename = str_replace(':', '', $pagename);
729 * Convert old page markup to new-style markup.
731 * @param string $text Old-style wiki markup.
733 * @param string $markup_type
735 * <dt><code>"block"</code> <dd>Convert all markup.
736 * <dt><code>"inline"</code> <dd>Convert only inline markup.
737 * <dt><code>"links"</code> <dd>Convert only link markup.
740 * @return string New-style wiki markup.
742 * @bugs Footnotes don't work quite as before (esp if there are
743 * multiple references to the same footnote. But close enough,
744 * probably for now....
745 * @bugs Apache2 and IIS crash with OldTextFormattingRules or
746 * AnciennesR%E8glesDeFormatage. (at the 2nd attempt to do the anchored block regex)
747 * It only crashes with CreateToc so far, but other pages (not in pgsrc) are
748 * also known to crash, even with Apache1.
750 function ConvertOldMarkup ($text, $markup_type = "block") {
756 // Trying to detect why the 2nd paragraph of OldTextFormattingRules or
757 // AnciennesR%E8glesDeFormatage crashes.
758 // It only crashes with CreateToc so far, but other pages (not in pgsrc) are
759 // also known to crash, even with Apache1.
761 // I suspect this only to crash with Apache2 and IIS.
762 if (in_array(php_sapi_name(),array('apache2handler','apache2filter','isapi'))
763 and preg_match("/plugin CreateToc/", $text))
765 trigger_error(_("The CreateTocPlugin is not yet old markup compatible! ")
766 ._("Please remove the CreateToc line to be able to reformat this page to old markup. ")
767 ._("Skipped."), E_USER_WARNING);
769 //if (!DEBUG) return $text;
774 /*****************************************************************
775 * Conversions for inline markup:
782 // escape escaped brackets
786 // change ! escapes to ~'s.
787 global $WikiNameRegexp, $request;
788 $bang_esc[] = "(?:" . ALLOWED_PROTOCOLS . "):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]";
789 // before 4.3.9 pcre had a memory release bug, which might hit us here. so be safe.
790 if (check_php_version(4,3,9)) {
791 $map = getInterwikiMap();
792 if ($map_regex = $map->getRegexp())
793 $bang_esc[] = $map_regex . ":[^\\s.,;?()]+"; // FIXME: is this really needed?
795 $bang_esc[] = $WikiNameRegexp;
796 $orig[] = '/!((?:' . join(')|(', $bang_esc) . '))/';
799 $subs["links"] = array($orig, $repl);
801 // Temporarily URL-encode pairs of underscores in links to hide
802 // them from the re for bold markup.
803 $orig[] = '/\[[^\[\]]*?__[^\[\]]*?\]/e';
804 $repl[] = 'str_replace(\'__\', \'%5F%5F\', \'\\0\')';
807 //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
810 // Convert footnote references.
811 $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
812 $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
814 // Convert old style emphases to HTML style emphasis.
815 $orig[] = '/__(.*?)__/';
816 $repl[] = '<strong>\\1</strong>';
817 $orig[] = "/''(.*?)''/";
818 $repl[] = '<em>\\1</em>';
820 // Escape nestled markup.
821 $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
824 // in old markup headings only allowed at beginning of line
828 // Convert URL-encoded pairs of underscores in links back to
829 // real underscores after bold markup has been converted.
830 $orig = '/\[[^\[\]]*?%5F%5F[^\[\]]*?\]/e';
831 $repl = 'str_replace(\'%5F%5F\', \'__\', \'\\0\')';
833 $subs["inline"] = array($orig, $repl);
835 /*****************************************************************
836 * Patterns which match block markup constructs which take
837 * special handling...
841 $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
843 $blockpats[] = '\|(?:.*\n\|)*';
846 $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
848 // Footnote definitions
849 $blockpats[] = '\[\s*(\d+)\s*\]';
853 $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
857 $blockpats[] = '!{1,3}[^!]';
859 removed .|\n in the anchor not to crash on /m because with /m "." already includes \n
860 this breaks headings but it doesn't crash anymore (crash on non-cgi, non-cli only)
862 $block_re = ( '/\A((?:.|\n)*?)(^(?:'
863 . join("|", $blockpats)
868 if ($markup_type != "block") {
869 list ($orig, $repl) = $subs[$markup_type];
870 return preg_replace($orig, $repl, $text);
873 list ($orig, $repl) = $subs['inline'];
876 // php crashes here in the 2nd paragraph of OldTextFormattingRules,
877 // AnciennesR%E8glesDeFormatage and more
878 // See http://www.pcre.org/pcre.txt LIMITATIONS
879 while (preg_match($block_re, $text, $m)) {
880 $text = substr($text, strlen($m[0]));
881 list (,$leading_text, $block) = $m;
884 if (strchr(" \t", $block[0])) {
887 $suffix = "\n</pre>\n";
889 elseif ($block[0] == '|') {
891 $prefix = "<?plugin OldStyleTable\n";
894 elseif (strchr("#*;", $block[0])) {
895 // Old-style list item
896 preg_match('/^([#*;]*)([*#]|;.*?:) */', $block, $m);
897 list (,$ind,$bullet) = $m;
898 $block = substr($block, strlen($m[0]));
900 $indent = str_repeat(' ', strlen($ind));
901 if ($bullet[0] == ';') {
902 //$term = ltrim(substr($bullet, 1));
903 //return $indent . $term . "\n" . $indent . ' ';
904 $prefix = $ind . $bullet;
907 $prefix = $indent . $bullet . ' ';
909 elseif ($block[0] == '[') {
910 // Footnote definition
911 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
913 $block = substr($block, strlen($m[0]));
914 $prefix = "#[|ftnt_${footnum}]~[[${footnum}|#ftnt_ref_${footnum}]~] ";
916 elseif ($block[0] == '<') {
918 // HACK: no inline markup...
922 elseif ($block[0] == '!') {
924 preg_match('/^!{1,3}/', $block, $m);
926 $block = substr($block, strlen($m[0]));
932 if ($leading_text) $leading_text = preg_replace($orig, $repl, $leading_text);
933 if ($block) $block = preg_replace($orig, $repl, $block);
934 $out .= $leading_text;
939 return $out . preg_replace($orig, $repl, $text);
945 * Expand tabs in string.
947 * Converts all tabs to (the appropriate number of) spaces.
950 * @param integer $tab_width
953 function expand_tabs($str, $tab_width = 8) {
954 $split = split("\t", $str);
955 $tail = array_pop($split);
957 foreach ($split as $hunk) {
959 $pos = strlen(strrchr($expanded, "\n")) - 1;
960 $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
962 return substr($expanded, 1) . $tail;
966 * Split WikiWords in page names.
968 * It has been deemed useful to split WikiWords (into "Wiki Words") in
969 * places like page titles. This is rumored to help search engines
972 * @param $page string The page name.
974 * @return string The split name.
976 function SplitPagename ($page) {
978 if (preg_match("/\s/", $page))
979 return $page; // Already split --- don't split any more.
981 // This algorithm is specialized for several languages.
982 // (Thanks to Pierrick MEIGNEN)
983 // Improvements for other languages welcome.
986 // This mess splits between a lower-case letter followed by
987 // either an upper-case or a numeral; except that it wont
988 // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
989 switch ($GLOBALS['LANG']) {
994 $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
997 $RE[] = '/([[:lower:]])((?<!Mc|Di)[[:upper:]]|\d)/';
1000 $sep = preg_quote(SUBPAGE_SEPARATOR, '/');
1001 // This the single-letter words 'I' and 'A' from any following
1002 // capitalized words.
1003 switch ($GLOBALS['LANG']) {
1005 $RE[] = "/(?<= |${sep}|^)([AI])([[:upper:]][[:lower:]])/";
1008 $RE[] = "/(?<= |${sep}|^)([À])([[:upper:]][[:lower:]])/";
1011 // Split numerals from following letters.
1012 $RE[] = '/(\d)([[:alpha:]])/';
1013 // Split at subpage seperators. TBD in Theme.php
1014 $RE[] = "/([^${sep}]+)(${sep})/";
1016 foreach ($RE as $key)
1017 $RE[$key] = pcre_fix_posix_classes($key);
1020 foreach ($RE as $regexp) {
1021 $page = preg_replace($regexp, '\\1 \\2', $page);
1026 function NoSuchRevision (&$request, $page, $version) {
1027 $html = HTML(HTML::h2(_("Revision Not Found")),
1028 HTML::p(fmt("I'm sorry. Version %d of %s is not in the database.",
1029 $version, WikiLink($page, 'auto'))));
1030 include_once('lib/Template.php');
1031 GeneratePage($html, _("Bad Version"), $page->getCurrentRevision());
1037 * Get time offset for local time zone.
1039 * @param $time time_t Get offset for this time. Default: now.
1040 * @param $no_colon boolean Don't put colon between hours and minutes.
1041 * @return string Offset as a string in the format +HH:MM.
1043 function TimezoneOffset ($time = false, $no_colon = false) {
1044 if ($time === false)
1046 $secs = date('Z', $time);
1055 $colon = $no_colon ? '' : ':';
1056 $mins = intval(($secs + 30) / 60);
1057 return sprintf("%s%02d%s%02d",
1058 $sign, $mins / 60, $colon, $mins % 60);
1063 * Format time in ISO-8601 format.
1065 * @param $time time_t Time. Default: now.
1066 * @return string Date and time in ISO-8601 format.
1068 function Iso8601DateTime ($time = false) {
1069 if ($time === false)
1071 $tzoff = TimezoneOffset($time);
1072 $date = date('Y-m-d', $time);
1073 $time = date('H:i:s', $time);
1074 return $date . 'T' . $time . $tzoff;
1078 * Format time in RFC-2822 format.
1080 * @param $time time_t Time. Default: now.
1081 * @return string Date and time in RFC-2822 format.
1083 function Rfc2822DateTime ($time = false) {
1084 if ($time === false)
1086 return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
1090 * Format time in RFC-1123 format.
1092 * @param $time time_t Time. Default: now.
1093 * @return string Date and time in RFC-1123 format.
1095 function Rfc1123DateTime ($time = false) {
1096 if ($time === false)
1098 return gmdate('D, d M Y H:i:s \G\M\T', $time);
1101 /** Parse date in RFC-1123 format.
1103 * According to RFC 1123 we must accept dates in the following
1106 * Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
1107 * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
1108 * Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
1110 * (Though we're only allowed to generate dates in the first format.)
1112 function ParseRfc1123DateTime ($timestr) {
1113 $timestr = trim($timestr);
1114 if (preg_match('/^ \w{3},\s* (\d{1,2}) \s* (\w{3}) \s* (\d{4}) \s*'
1115 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1117 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1119 elseif (preg_match('/^ \w+,\s* (\d{1,2})-(\w{3})-(\d{2}|\d{4}) \s*'
1120 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1122 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1123 if ($year < 70) $year += 2000;
1124 elseif ($year < 100) $year += 1900;
1126 elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
1127 .'(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
1129 list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
1136 $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
1138 return false; // failed
1143 * Format time to standard 'ctime' format.
1145 * @param $time time_t Time. Default: now.
1146 * @return string Date and time.
1148 function CTime ($time = false)
1150 if ($time === false)
1152 return date("D M j H:i:s Y", $time);
1157 * Format number as kilobytes or bytes.
1158 * Short format is used for PageList
1159 * Long format is used in PageInfo
1161 * @param $bytes int. Default: 0.
1162 * @param $longformat bool. Default: false.
1163 * @return class FormattedText (XmlElement.php).
1165 function ByteFormatter ($bytes = 0, $longformat = false) {
1168 if ($bytes < 1024) {
1170 $size = fmt("%s b", $bytes);
1172 $size = fmt("%s bytes", $bytes);
1175 $kb = round($bytes / 1024, 1);
1177 $size = fmt("%s k", $kb);
1179 $size = fmt("%s Kb (%s bytes)", $kb, $bytes);
1185 * Internationalized printf.
1187 * This is essentially the same as PHP's built-in printf
1188 * with the following exceptions:
1190 * <li> It passes the format string through gettext().
1191 * <li> It supports the argument reordering extensions.
1198 * __printf("Differences between versions %s and %s of %s",
1199 * $new_link, $old_link, $page_link);
1202 * Then in locale/po/de.po, one can reorder the printf arguments:
1205 * msgid "Differences between %s and %s of %s."
1206 * msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
1209 * (Note that while PHP tries to expand $vars within double-quotes,
1210 * the values in msgstr undergo no such expansion, so the '$'s
1213 * One shouldn't use reordered arguments in the default format string.
1214 * Backslashes in the default string would be necessary to escape the
1215 * '$'s, and they'll cause all kinds of trouble....
1217 function __printf ($fmt) {
1218 $args = func_get_args();
1220 echo __vsprintf($fmt, $args);
1224 * Internationalized sprintf.
1226 * This is essentially the same as PHP's built-in printf with the
1227 * following exceptions:
1230 * <li> It passes the format string through gettext().
1231 * <li> It supports the argument reordering extensions.
1236 function __sprintf ($fmt) {
1237 $args = func_get_args();
1239 return __vsprintf($fmt, $args);
1243 * Internationalized vsprintf.
1245 * This is essentially the same as PHP's built-in printf with the
1246 * following exceptions:
1249 * <li> It passes the format string through gettext().
1250 * <li> It supports the argument reordering extensions.
1255 function __vsprintf ($fmt, $args) {
1256 $fmt = gettext($fmt);
1257 // PHP's sprintf doesn't support variable with specifiers,
1258 // like sprintf("%*s", 10, "x"); --- so we won't either.
1260 if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
1261 // Format string has '%2$s' style argument reordering.
1262 // PHP doesn't support this.
1263 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
1264 // literal variable name substitution only to keep locale
1265 // strings uncluttered
1266 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
1267 '%1\$s','%s'), E_USER_WARNING); //php+locale error
1269 $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
1272 // Reorder arguments appropriately.
1273 foreach($m[1] as $argnum) {
1274 if ($argnum < 1 || $argnum > count($args))
1275 trigger_error(sprintf(_("%s: argument index out of range"),
1276 $argnum), E_USER_WARNING);
1277 $newargs[] = $args[$argnum - 1];
1282 // Not all PHP's have vsprintf, so...
1283 array_unshift($args, $fmt);
1284 return call_user_func_array('sprintf', $args);
1287 function file_mtime ($filename) {
1288 if ($stat = @stat($filename))
1294 function sort_file_mtime ($a, $b) {
1295 $ma = file_mtime($a);
1296 $mb = file_mtime($b);
1297 if (!$ma or !$mb or $ma == $mb) return 0;
1298 return ($ma > $mb) ? -1 : 1;
1303 * Build an array in $this->_fileList of files from $dirname.
1304 * Subdirectories are not traversed.
1306 * (This was a function LoadDir in lib/loadsave.php)
1307 * See also http://www.php.net/manual/en/function.readdir.php
1309 function getFiles($exclude=false, $sortby=false, $limit=false) {
1310 $list = $this->_fileList;
1313 require_once('lib/PageList.php');
1314 switch (Pagelist::sortby($sortby, 'db')) {
1315 case 'pagename ASC': break;
1316 case 'pagename DESC':
1317 $list = array_reverse($list);
1320 usort($list,'sort_file_mtime');
1323 usort($list,'sort_file_mtime');
1324 $list = array_reverse($list);
1329 return array_splice($list, 0, $limit);
1333 function _filenameSelector($filename) {
1334 if (! $this->_pattern)
1337 return glob_match ($this->_pattern, $filename, $this->_case);
1341 function fileSet($directory, $filepattern = false) {
1342 $this->_fileList = array();
1343 $this->_pattern = $filepattern;
1344 $this->_case = !isWindows();
1345 $this->_pathsep = '/';
1347 if (empty($directory)) {
1348 trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1350 return; // early return
1353 @ $dir_handle = opendir($dir=$directory);
1354 if (empty($dir_handle)) {
1355 trigger_error(sprintf(_("Unable to open directory '%s' for reading"),
1356 $dir), E_USER_NOTICE);
1357 return; // early return
1360 while ($filename = readdir($dir_handle)) {
1361 if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1363 if ($this->_filenameSelector($filename)) {
1364 array_push($this->_fileList, "$filename");
1365 //trigger_error(sprintf(_("found file %s"), $filename),
1366 // E_USER_NOTICE); //debugging
1369 closedir($dir_handle);
1375 // expands a list containing regex's to its matching entries
1376 class ListRegexExpand {
1377 //var $match, $list, $index, $case_sensitive;
1378 function ListRegexExpand (&$list, $match, $case_sensitive = true) {
1379 $this->match = str_replace('/','\/',$match);
1380 $this->list = &$list;
1381 $this->case_sensitive = $case_sensitive;
1382 //$this->index = false;
1384 function listMatchCallback ($item, $key) {
1385 if (preg_match('/' . $this->match . ($this->case_sensitive ? '/' : '/i'), $item)) {
1386 unset($this->list[$this->index]);
1387 $this->list[] = $item;
1390 function expandRegex ($index, &$pages) {
1391 $this->index = $index;
1392 array_walk($pages, array($this, 'listMatchCallback'));
1397 // convert fileglob to regex style:
1398 // convert some wildcards to pcre style, escape the rest
1399 // escape . \\ + * ? [ ^ ] $ ( ) { } = ! < > | :
1400 function glob_to_pcre ($glob) {
1401 // check simple case: no need to escape
1402 if (strcspn($glob, ".\\+*?[^]$(){}=!<>|:") == strlen($glob))
1404 // preg_replace cannot handle "\\\\\\2" so convert \\ to \xff
1405 $glob = strtr($glob, "\\", "\xff");
1406 // first convert some unescaped expressions to pcre style: . => \.
1408 $re = preg_replace('/([^\xff])?(['.preg_quote($escape).'])/', "\\1\xff\\2", $glob);
1411 $re = preg_replace('/([^\xff])?\*/', '$1.*', $re);
1412 $re = preg_replace('/([^\xff])?\?/', '$1.', $re);
1413 if (!preg_match('/^[\?\*]/',$glob))
1415 if (!preg_match('/[\?\*]$/',$glob))
1418 // .*? handled above, now escape the rest
1419 $escape = '\[](){}=!<>|:';
1420 while (strcspn($re, $escape) != strlen($re)) // loop strangely needed
1421 $re = preg_replace('/([^\xff])(['.preg_quote($escape).'])/', "\\1\xff\\2", $re);
1422 return strtr($re, "\xff", "\\");
1425 function glob_match ($glob, $against, $case_sensitive = true) {
1426 return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'), $against);
1429 function explodeList($input, $allnames, $glob_style = true, $case_sensitive = true) {
1430 $list = explode(',',$input);
1431 // expand wildcards from list of $allnames
1432 if (preg_match('/[\?\*]/',$input)) {
1433 // Optimizing loop invariants:
1434 // http://phplens.com/lens/php-book/optimizing-debugging-php.php
1435 for ($i = 0, $max = sizeof($list); $i < $max; $i++) {
1437 if (preg_match('/[\?\*]/',$f)) {
1439 $expand = new ListRegexExpand($list, $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1440 $expand->expandRegex($i, $allnames);
1447 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1448 function explodePageList($input, $include_empty=false, $sortby='pagename', $limit=false, $exclude=false) {
1449 include_once("lib/PageList.php");
1450 return PageList::explodePageList($input, $include_empty, $sortby, $limit, $exclude);
1453 // Class introspections
1456 * Determine whether object is of a specified type.
1457 * In PHP builtin since 4.2.0 as is_a()
1459 * @param $object object An object.
1460 * @param $class string Class name.
1461 * @return bool True iff $object is a $class
1462 * or a sub-type of $class.
1464 function isa ($object, $class) {
1465 //if (check_php_version(5))
1466 // return $object instanceof $class;
1467 if (check_php_version(4,2) and !check_php_version(5))
1468 return is_a($object, $class);
1470 $lclass = check_php_version(5) ? $class : strtolower($class);
1471 return is_object($object)
1472 && ( strtolower(get_class($object)) == strtolower($class)
1473 || is_subclass_of($object, $lclass) );
1476 /** Determine whether (possible) object has method.
1478 * @param $object mixed Object
1479 * @param $method string Method name
1480 * @return bool True iff $object is an object with has method $method.
1482 function can ($object, $method) {
1483 return is_object($object) && method_exists($object, strtolower($method));
1486 /** Determine whether a function is okay to use.
1488 * Some providers (e.g. Lycos) disable some of PHP functions for
1489 * "security reasons." This makes those functions, of course,
1490 * unusable, despite the fact the function_exists() says they
1493 * This function test to see if a function exists and is not
1494 * disallowed by PHP's disable_functions config setting.
1496 * @param string $function_name Function name
1497 * @return bool True iff function can be used.
1499 function function_usable($function_name) {
1501 if (!is_array($disabled)) {
1502 $disabled = array();
1503 // Use get_cfg_var since ini_get() is one of the disabled functions
1504 // (on Lycos, at least.)
1505 $split = preg_split('/\s*,\s*/', trim(get_cfg_var('disable_functions')));
1506 foreach ($split as $f)
1507 $disabled[strtolower($f)] = true;
1510 return ( function_exists($function_name)
1511 and ! isset($disabled[strtolower($function_name)])
1518 * This is used for generating ETags.
1520 function hash ($x) {
1521 if (is_scalar($x)) {
1524 elseif (is_array($x)) {
1526 return md5(serialize($x));
1528 elseif (is_object($x)) {
1531 trigger_error("Can't hash $x", E_USER_ERROR);
1536 * Seed the random number generator.
1538 * better_srand() ensures the randomizer is seeded only once.
1540 * How random do you want it? See:
1541 * http://www.php.net/manual/en/function.srand.php
1542 * http://www.php.net/manual/en/function.mt-srand.php
1544 function better_srand($seed = '') {
1545 static $wascalled = FALSE;
1547 $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
1548 function_exists('mt_srand') ? mt_srand($seed) : srand($seed);
1550 //trigger_error("new random seed", E_USER_NOTICE); //debugging
1555 * Recursively count all non-empty elements
1556 * in array of any dimension or mixed - i.e.
1557 * array('1' => 2, '2' => array('1' => 3, '2' => 4))
1558 * See http://www.php.net/manual/en/function.count.php
1560 function count_all($arg) {
1561 // skip if argument is empty
1563 //print_r($arg); //debugging
1565 // not an array, return 1 (base case)
1568 // else call recursively for all elements $arg
1569 foreach($arg as $key => $val)
1570 $count += count_all($val);
1575 function isSubPage($pagename) {
1576 return (strstr($pagename, SUBPAGE_SEPARATOR));
1579 function subPageSlice($pagename, $pos) {
1580 $pages = explode(SUBPAGE_SEPARATOR,$pagename);
1581 $pages = array_slice($pages,$pos,1);
1588 * Class for "popping up" and alert box. (Except that right now, it doesn't
1592 * This is a hackish and needs to be refactored. However it would be nice to
1593 * unify all the different methods we use for showing Alerts and Dialogs.
1594 * (E.g. "Page deleted", login form, ...)
1599 * @param object $request
1600 * @param mixed $head Header ("title") for alert box.
1601 * @param mixed $body The text in the alert box.
1602 * @param hash $buttons An array mapping button labels to URLs.
1603 * The default is a single "Okay" button pointing to $request->getURLtoSelf().
1605 function Alert($head, $body, $buttons=false) {
1606 if ($buttons === false)
1609 $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1610 $this->_buttons = $buttons;
1614 * Show the alert box.
1619 $tokens = $this->_tokens;
1620 $tokens['BUTTONS'] = $this->_getButtons();
1622 $request->discardOutput();
1623 $tmpl = new Template('dialog', $request, $tokens);
1629 function _getButtons() {
1632 $buttons = $this->_buttons;
1634 $buttons = array(_("Okay") => $request->getURLtoSelf());
1637 foreach ($buttons as $label => $url)
1638 print "$label $url\n";
1639 $out[] = $WikiTheme->makeButton($label, $url, 'wikiaction');
1640 return new XmlContent($out);
1645 // 1.3.9-p1 => 1030.091
1646 // 1.3.10pre => 1030.099
1647 // 1.3.11pre-20041120 => 1030.1120041120
1648 function phpwiki_version() {
1649 static $PHPWIKI_VERSION;
1650 if (!isset($PHPWIKI_VERSION)) {
1651 $arr = explode('.',preg_replace('/\D+$/','', PHPWIKI_VERSION)); // remove the pre
1652 $arr[2] = preg_replace('/\.+/','.',preg_replace('/\D/','.',$arr[2]));
1653 $PHPWIKI_VERSION = $arr[0]*1000 + $arr[1]*10 + 0.01*$arr[2];
1654 if (strstr(PHPWIKI_VERSION, 'pre'))
1655 $PHPWIKI_VERSION -= 0.01;
1657 return $PHPWIKI_VERSION;
1660 function isWikiWord($word) {
1661 global $WikiNameRegexp;
1662 //or preg_match('/\A' . $WikiNameRegexp . '\z/', $word) ??
1663 return preg_match("/^$WikiNameRegexp\$/",$word);
1666 // needed to store serialized objects-values only (perm, pref)
1667 function obj2hash ($obj, $exclude = false, $fields = false) {
1669 if (! $fields ) $fields = get_object_vars($obj);
1670 foreach ($fields as $key => $val) {
1671 if (is_array($exclude)) {
1672 if (in_array($key,$exclude)) continue;
1680 * isUtf8String($string) - cheap utf-8 detection
1682 * segfaults for strings longer than 10kb!
1683 * Use http://www.phpdiscuss.com/article.php?id=565&group=php.i18n or
1684 * checkTitleEncoding() at http://cvs.sourceforge.net/viewcvs.py/wikipedia/phase3/languages/Language.php
1686 function isUtf8String( $s ) {
1687 $ptrASCII = '[\x00-\x7F]';
1688 $ptr2Octet = '[\xC2-\xDF][\x80-\xBF]';
1689 $ptr3Octet = '[\xE0-\xEF][\x80-\xBF]{2}';
1690 $ptr4Octet = '[\xF0-\xF4][\x80-\xBF]{3}';
1691 $ptr5Octet = '[\xF8-\xFB][\x80-\xBF]{4}';
1692 $ptr6Octet = '[\xFC-\xFD][\x80-\xBF]{5}';
1693 return preg_match("/^($ptrASCII|$ptr2Octet|$ptr3Octet|$ptr4Octet|$ptr5Octet|$ptr6Octet)*$/s", $s);
1697 * Check for UTF-8 URLs; Internet Explorer produces these if you
1698 * type non-ASCII chars in the URL bar or follow unescaped links.
1699 * Requires urldecoded pagename.
1700 * Fixes sf.net bug #953949
1702 * src: languages/Language.php:checkTitleEncoding() from mediawiki
1704 function fixTitleEncoding( $s ) {
1709 if (empty($s)) return $s;
1711 $ishigh = preg_match( '/[\x80-\xff]/', $s);
1713 $isutf = ($ishigh ? preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1714 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s ) : true );
1716 $isutf = ($ishigh ? isUtf8String($s) : true);
1717 $locharset = strtolower($charset);
1719 if( $locharset != "utf-8" and $ishigh and $isutf )
1720 // if charset == 'iso-8859-1' then simply use utf8_decode()
1721 if ($locharset == 'iso-8859-1')
1722 return utf8_decode( $s );
1724 // TODO: check for iconv support
1725 return iconv( "UTF-8", $charset, $s );
1727 if ($locharset == "utf-8" and $ishigh and !$isutf )
1728 return utf8_encode( $s );
1730 // Other languages can safely leave this function, or replace
1731 // it with one to detect and convert another legacy encoding.
1736 * MySQL fulltext index doesn't grok utf-8, so we
1737 * need to fold cases and convert to hex.
1738 * src: languages/Language.php:stripForSearch() from mediawiki
1741 function stripForSearch( $string ) {
1742 global $wikiLowerChars;
1743 // '/(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])/' => "a-z\xdf-\xf6\xf8-\xff"
1744 return preg_replace(
1745 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1746 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1752 * Workaround for allow_url_fopen, to get the content of an external URI.
1753 * It returns the contents in one slurp. Parsers might want to check for allow_url_fopen
1754 * and use fopen, fread chunkwise. (see lib/XmlParser.php)
1756 function url_get_contents( $uri ) {
1757 if (get_cfg_var('allow_url_fopen')) { // was ini_get('allow_url_fopen'))
1758 return @file_get_contents($uri);
1760 require_once("lib/HttpClient.php");
1761 $bits = parse_url($uri);
1762 $host = $bits['host'];
1763 $port = isset($bits['port']) ? $bits['port'] : 80;
1764 $path = isset($bits['path']) ? $bits['path'] : '/';
1765 if (isset($bits['query'])) {
1766 $path .= '?'.$bits['query'];
1768 $client = new HttpClient($host, $port);
1769 $client->use_gzip = false;
1770 if (!$client->get($path)) {
1773 return $client->getContent();
1779 * Generate consecutively named strings:
1780 * Name, Name2, Name3, ...
1782 function GenerateId($name) {
1783 static $ids = array();
1784 if (empty($ids[$name])) {
1789 return $name . $ids[$name];
1793 // from IncludePage. To be of general use.
1794 // content: string or array of strings
1795 function firstNWordsOfContent( $n, $content ) {
1796 if ($content and $n > 0) {
1797 if (is_array($content)) {
1798 // fixme: return a list of lines then?
1799 //$content = join("\n", $content);
1800 //$return_array = true;
1802 foreach ($content as $line) {
1803 $words = explode(' ', $line);
1804 if ($wordcount + count($words) > $n) {
1805 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
1806 . sprintf(_("... (first %s words)"), $n);
1809 $wordcount += count($words);
1815 // fixme: use better whitespace/word seperators
1816 $words = explode(' ', $content);
1817 if (count($words) > $n) {
1818 return join(' ', array_slice($words, 0, $n))
1819 . sprintf(_("... (first %s words)"), $n);
1829 // moved from lib/plugin/IncludePage.php
1830 function extractSection ($section, $content, $page, $quiet = false, $sectionhead = false) {
1831 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
1833 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
1834 . " \\s*$\\n?" // possible blank lines
1835 . " ( (?: ^.*\\n? )*? )" // some lines
1836 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
1837 implode("\n", $content),
1839 // Strip trailing blanks lines and ---- <hr>s
1840 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
1842 $text = $match[1] . $section ."\n". $text;
1843 return explode("\n", $text);
1846 $mesg = $page ." ". $section;
1849 return array(sprintf(_("<%s: no such section>"), $mesg));
1852 // use this faster version: only load ExternalReferrer if we came from an external referrer
1853 function isExternalReferrer(&$request) {
1854 if ($referrer = $request->get('HTTP_REFERER')) {
1855 $home = SERVER_URL; // SERVER_URL or SCRIPT_NAME, if we want to check sister wiki's also
1856 if (string_starts_with(strtolower($referrer), strtolower($home))) return false;
1857 require_once("lib/ExternalReferrer.php");
1858 $se = new SearchEngines();
1859 return $se->parseSearchQuery($referrer);
1865 * useful for PECL overrides: cvsclient, ldap, soap.
1867 function loadPhpExtension($extension) {
1868 if (!extension_loaded($extension)) {
1869 $soname = (isWindows() ? 'php_' : '') . $extension . (isWindows() ? '.dll' : '.so');
1873 return extension_loaded($extension);
1876 function string_starts_with($string, $prefix) {
1877 return (substr($string, 0, strlen($prefix)) == $prefix);
1879 function string_ends_with($string, $suffix) {
1880 return (substr($string, -strlen($suffix)) == $suffix);
1884 * Ensure that the script will have another $secs time left.
1885 * Works only if safe_mode is off.
1886 * For example not to timeout on waiting socket connections.
1887 * Use the socket timeout as arg.
1889 function longer_timeout($secs = 30) {
1890 $timeout = @ini_get("max_execution_time") ? ini_get("max_execution_time") : 30;
1891 $timeleft = $timeout - $GLOBALS['RUNTIMER']->getTime();
1892 if ($timeleft < $secs)
1893 @set_time_limit(max($timeout,(integer)($secs + $timeleft)));
1896 function printSimpleTrace($bt) {
1898 echo "Traceback:\n";
1899 foreach ($bt as $i => $elem) {
1900 if (!array_key_exists('file', $elem)) {
1903 echo join(" ",array_values($elem)),"\n";
1904 //print " " . $elem['file'] . ':' . $elem['line'] . " " .$elem['function']"\n";
1909 * Return the used process memory (in byte?)
1910 * Enable the section which will work for you. (They are very slow)
1911 * Special quirks for Windows: Requires cygwin.
1913 function getMemoryUsage() {
1914 if (function_exists('memory_get_usage') and memory_get_usage()) {
1915 return memory_get_usage();
1916 // } elseif (function_exists('getrusage') and ($u = getrusage()) and !empty($u['ru_maxrss'])) {
1917 // $mem = $u['ru_maxrss'];
1918 } elseif (1 and substr(PHP_OS,0,3) == 'WIN') { // requires a newer cygwin
1919 // what we want is the process memory only: apache or php
1921 // This works only if it's a cygwin process (apache or php)
1922 //$mem = (integer) trim(exec("cat /proc/$pid/statm |cut -f1"));
1923 // if it's native windows use something like this:
1924 // (requires pslist from sysinternals.com)
1925 $memstr = exec("pslist $pid|grep -A1 Mem|sed 1d|perl -ane\"print \$"."F[5]\"");
1926 return (integer) trim($memstr);
1929 //%MEM: Percentage of total memory in use by this process
1930 //VSZ: Total virtual memory size, in 1K blocks.
1931 //RSS: Real Set Size, the actual amount of physical memory allocated to this process.
1932 //CPU time used by process since it started.
1933 //echo "%",`ps -o%mem,vsz,rss,time -p $pid|sed 1d`,"\n";
1934 $memstr = exec("ps -orss -p $pid|sed 1d");
1935 return (integer) trim($memstr);
1939 // $Log: not supported by cvs2svn $
1940 // Revision 1.232 2005/02/02 19:34:09 rurban
1941 // more maps: Talk, User
1943 // Revision 1.231 2005/01/30 19:48:52 rurban
1944 // enable ps memory on unix
1946 // Revision 1.230 2005/01/25 07:10:51 rurban
1947 // add getMemoryUsage to stdlib
1949 // Revision 1.229 2005/01/21 11:51:22 rurban
1952 // Revision 1.228 2005/01/17 20:28:30 rurban
1953 // Allow more pagename chars: Limit only on certain backends.
1954 // Re-Allow : and ; and control chars on non-file backends.
1956 // Revision 1.227 2005/01/14 18:32:08 uckelman
1957 // ConvertOldMarkup did not properly handle links containing pairs of pairs
1958 // of underscores. (E.g., [http://example.com/foo__bar__.html] would be
1959 // munged by the regex for bold text.) Now '__' in links are hidden prior to
1960 // conversion of '__' into '<strong>', and then unhidden afterwards.
1962 // Revision 1.226 2004/12/26 17:12:06 rurban
1963 // avoid stdargs in url, php5 fixes
1965 // Revision 1.225 2004/12/22 19:02:29 rurban
1966 // fix glob for starting * or ?
1968 // Revision 1.224 2004/12/20 12:11:50 rurban
1969 // fix "lib/stdlib.php:1348: Warning[2]: Compilation failed: unmatched parentheses at offset 2"
1970 // not reproducable other than on sf.net, but this seems to fix it.
1972 // Revision 1.223 2004/12/18 16:49:29 rurban
1973 // fix RPC for !USE_PATH_INFO, add debugging helper
1975 // Revision 1.222 2004/12/17 16:40:45 rurban
1976 // add not yet used url helper
1978 // Revision 1.221 2004/12/06 19:49:58 rurban
1979 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
1980 // renamed delete_page to purge_page.
1981 // enable action=edit&version=-1 to force creation of a new version.
1982 // added BABYCART_PATH config
1983 // fixed magiqc in adodb.inc.php
1984 // and some more docs
1986 // Revision 1.220 2004/11/30 17:47:41 rurban
1987 // added mt_srand, check for native isa
1989 // Revision 1.219 2004/11/26 18:39:02 rurban
1990 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
1992 // Revision 1.218 2004/11/25 08:28:48 rurban
1995 // Revision 1.217 2004/11/16 17:31:03 rurban
1996 // re-enable old block markup conversion
1998 // Revision 1.216 2004/11/11 18:31:26 rurban
1999 // add simple backtrace on such general failures to get at least an idea where
2001 // Revision 1.215 2004/11/11 14:34:12 rurban
2002 // minor clarifications
2004 // Revision 1.214 2004/11/11 11:01:20 rurban
2005 // fix loadPhpExtension
2007 // Revision 1.213 2004/11/01 10:43:57 rurban
2008 // seperate PassUser methods into seperate dir (memory usage)
2009 // fix WikiUser (old) overlarge data session
2010 // remove wikidb arg from various page class methods, use global ->_dbi instead
2013 // Revision 1.212 2004/10/22 09:15:39 rurban
2014 // Alert::show has no arg anymore
2016 // Revision 1.211 2004/10/22 09:05:11 rurban
2017 // added longer_timeout (HttpClient)
2020 // Revision 1.210 2004/10/14 21:06:02 rurban
2021 // fix dumphtml with USE_PATH_INFO (again). fix some PageList refs
2023 // Revision 1.209 2004/10/14 19:19:34 rurban
2024 // loadsave: check if the dumped file will be accessible from outside.
2025 // and some other minor fixes. (cvsclient native not yet ready)
2027 // Revision 1.208 2004/10/12 13:13:20 rurban
2028 // php5 compatibility (5.0.1 ok)
2030 // Revision 1.207 2004/09/26 12:21:40 rurban
2031 // removed old log entries.
2032 // added persistent start_debug on internal links and DEBUG
2033 // added isExternalReferrer (not yet used)
2035 // Revision 1.206 2004/09/25 16:28:36 rurban
2036 // added to TOC, firstNWordsOfContent is now plugin compatible, added extractSection
2038 // Revision 1.205 2004/09/23 13:59:35 rurban
2039 // Before removing a page display a sample of 100 words.
2041 // Revision 1.204 2004/09/17 13:19:15 rurban
2042 // fix LinkPhpwikiURL bug reported in http://phpwiki.sourceforge.net/phpwiki/KnownBugs
2045 // Revision 1.203 2004/09/16 08:00:52 rurban
2046 // just some comments
2048 // Revision 1.202 2004/09/14 10:11:44 rurban
2049 // start 2nd Id with ...Plugin2
2051 // Revision 1.201 2004/09/14 10:06:42 rurban
2052 // generate iterated plugin ids, set plugin span id also
2054 // Revision 1.200 2004/08/05 17:34:26 rurban
2055 // move require to sortby branch
2057 // Revision 1.199 2004/08/05 10:38:15 rurban
2058 // fix Bug #993692: Making Snapshots or Backups doesn't work anymore
2061 // Revision 1.198 2004/07/02 10:30:36 rurban
2062 // always disable getimagesize for < php-4.3 with external png's
2064 // Revision 1.197 2004/07/02 09:55:58 rurban
2065 // more stability fixes: new DISABLE_GETIMAGESIZE if your php crashes when loading LinkIcons: failing getimagesize in old phps; blockparser stabilized
2067 // Revision 1.196 2004/07/01 08:51:22 rurban
2068 // dumphtml: added exclude, print pagename before processing
2070 // Revision 1.195 2004/06/29 08:52:22 rurban
2071 // Use ...version() $need_content argument in WikiDB also:
2072 // To reduce the memory footprint for larger sets of pagelists,
2073 // we don't cache the content (only true or false) and
2074 // we purge the pagedata (_cached_html) also.
2075 // _cached_html is only cached for the current pagename.
2076 // => Vastly improved page existance check, ACL check, ...
2078 // Now only PagedList info=content or size needs the whole content, esp. if sortable.
2080 // Revision 1.194 2004/06/29 06:48:04 rurban
2081 // Improve LDAP auth and GROUP_LDAP membership:
2082 // no error message on false password,
2083 // added two new config vars: LDAP_OU_USERS and LDAP_OU_GROUP with GROUP_METHOD=LDAP
2084 // fixed two group queries (this -> user)
2085 // stdlib: ConvertOldMarkup still flawed
2087 // Revision 1.193 2004/06/28 13:27:03 rurban
2088 // CreateToc disabled for old markup and Apache2 only
2090 // Revision 1.192 2004/06/28 12:47:43 rurban
2091 // skip if non-DEBUG and old markup with CreateToc
2093 // Revision 1.191 2004/06/25 14:31:56 rurban
2094 // avoid debug_skip warning
2096 // Revision 1.190 2004/06/25 14:29:20 rurban
2097 // WikiGroup refactoring:
2098 // global group attached to user, code for not_current user.
2099 // improved helpers for special groups (avoid double invocations)
2100 // new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
2101 // fixed a XHTML validation error on userprefs.tmpl
2103 // Revision 1.189 2004/06/20 09:45:35 rurban
2104 // php5 isa fix (wrong strtolower)
2106 // Revision 1.188 2004/06/16 10:38:58 rurban
2107 // Disallow refernces in calls if the declaration is a reference
2108 // ("allow_call_time_pass_reference clean").
2109 // PhpWiki is now allow_call_time_pass_reference = Off clean,
2110 // but several external libraries may not.
2111 // In detail these libs look to be affected (not tested):
2115 // Revision 1.187 2004/06/14 11:31:37 rurban
2116 // renamed global $Theme to $WikiTheme (gforge nameclash)
2117 // inherit PageList default options from PageList
2118 // default sortby=pagename
2119 // use options in PageList_Selectable (limit, sortby, ...)
2120 // added action revert, with button at action=diff
2121 // added option regex to WikiAdminSearchReplace
2123 // Revision 1.186 2004/06/13 13:54:25 rurban
2124 // Catch fatals on the four dump calls (as file and zip, as html and mimified)
2125 // FoafViewer: Check against external requirements, instead of fatal.
2126 // Change output for xhtmldumps: using file:// urls to the local fs.
2127 // Catch SOAP fatal by checking for GOOGLE_LICENSE_KEY
2128 // Import GOOGLE_LICENSE_KEY and FORTUNE_DIR from config.ini.
2130 // Revision 1.185 2004/06/11 09:07:30 rurban
2131 // support theme-specific LinkIconAttr: front or after or none
2133 // Revision 1.184 2004/06/04 20:32:53 rurban
2134 // Several locale related improvements suggested by Pierrick Meignen
2135 // LDAP fix by John Cole
2136 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
2138 // Revision 1.183 2004/06/01 10:22:56 rurban
2139 // added url_get_contents() used in XmlParser and elsewhere
2141 // Revision 1.182 2004/05/25 12:40:48 rurban
2142 // trim the pagename
2144 // Revision 1.181 2004/05/25 10:18:44 rurban
2145 // Check for UTF-8 URLs; Internet Explorer produces these if you
2146 // type non-ASCII chars in the URL bar or follow unescaped links.
2147 // Fixes sf.net bug #953949
2148 // src: languages/Language.php:checkTitleEncoding() from mediawiki
2150 // Revision 1.180 2004/05/18 16:23:39 rurban
2151 // rename split_pagename to SplitPagename
2153 // Revision 1.179 2004/05/18 16:18:37 rurban
2154 // AutoSplit at subpage seperators
2155 // RssFeed stability fix for empty feeds or broken connections
2157 // Revision 1.178 2004/05/12 10:49:55 rurban
2158 // require_once fix for those libs which are loaded before FileFinder and
2159 // its automatic include_path fix, and where require_once doesn't grok
2160 // dirname(__FILE__) != './lib'
2161 // upgrade fix with PearDB
2162 // navbar.tmpl: remove spaces for IE button alignment
2164 // Revision 1.177 2004/05/08 14:06:12 rurban
2165 // new support for inlined image attributes: [image.jpg size=50x30 align=right]
2166 // minor stability and portability fixes
2168 // Revision 1.176 2004/05/08 11:25:15 rurban
2171 // Revision 1.175 2004/05/06 17:30:38 rurban
2172 // CategoryGroup: oops, dos2unix eol
2173 // improved phpwiki_version:
2174 // pre -= .0001 (1.3.10pre: 1030.099)
2175 // -p1 += .001 (1.3.9-p1: 1030.091)
2176 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
2177 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
2178 // backend->backendType(), backend->database(),
2179 // backend->listOfFields(),
2180 // backend->listOfTables(),
2182 // Revision 1.174 2004/05/06 12:02:05 rurban
2183 // fix sf.net bug#949002: [ Link | ] assertion
2185 // Revision 1.173 2004/05/03 15:00:31 rurban
2186 // added more database upgrading: session.sess_ip, page.id autp_increment
2188 // Revision 1.172 2004/04/26 20:44:34 rurban
2189 // locking table specific for better databases
2191 // Revision 1.171 2004/04/19 23:13:03 zorloc
2192 // Connect the rest of PhpWiki to the IniConfig system. Also the keyword regular expression is not a config setting
2194 // Revision 1.170 2004/04/19 18:27:45 rurban
2195 // Prevent from some PHP5 warnings (ref args, no :: object init)
2196 // php5 runs now through, just one wrong XmlElement object init missing
2197 // Removed unneccesary UpgradeUser lines
2198 // Changed WikiLink to omit version if current (RecentChanges)
2200 // Revision 1.169 2004/04/15 21:29:48 rurban
2201 // allow [0] with new markup: link to page "0"
2203 // Revision 1.168 2004/04/10 02:30:49 rurban
2204 // Fixed gettext problem with VIRTUAL_PATH scripts (Windows only probably)
2205 // Fixed "cannot setlocale..." (sf.net problem)
2207 // Revision 1.167 2004/04/02 15:06:55 rurban
2208 // fixed a nasty ADODB_mysql session update bug
2209 // improved UserPreferences layout (tabled hints)
2210 // fixed UserPreferences auth handling
2211 // improved auth stability
2212 // improved old cookie handling: fixed deletion of old cookies with paths
2214 // Revision 1.166 2004/04/01 15:57:10 rurban
2215 // simplified Sidebar theme: table, not absolute css positioning
2216 // added the new box methods.
2217 // remaining problems: large left margin, how to override _autosplitWikiWords in Template only
2219 // Revision 1.165 2004/03/24 19:39:03 rurban
2220 // php5 workaround code (plus some interim debugging code in XmlElement)
2221 // php5 doesn't work yet with the current XmlElement class constructors,
2222 // WikiUserNew does work better than php4.
2223 // rewrote WikiUserNew user upgrading to ease php5 update
2224 // fixed pref handling in WikiUserNew
2225 // added Email Notification
2226 // added simple Email verification
2227 // removed emailVerify userpref subclass: just a email property
2228 // changed pref binary storage layout: numarray => hash of non default values
2229 // print optimize message only if really done.
2230 // forced new cookie policy: delete pref cookies, use only WIKI_ID as plain string.
2231 // prefs should be stored in db or homepage, besides the current session.
2233 // Revision 1.164 2004/03/18 21:41:09 rurban
2234 // fixed sqlite support
2235 // WikiUserNew: PHP5 fixes: don't assign $this (untested)
2237 // Revision 1.163 2004/03/17 18:41:49 rurban
2238 // just reformatting
2240 // Revision 1.162 2004/03/16 15:43:08 rurban
2241 // make fileSet sortable to please PageList
2243 // Revision 1.161 2004/03/12 15:48:07 rurban
2244 // fixed explodePageList: wrong sortby argument order in UnfoldSubpages
2245 // simplified lib/stdlib.php:explodePageList
2247 // Revision 1.160 2004/02/28 21:14:08 rurban
2248 // generally more PHPDOC docs
2249 // see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
2250 // fxied WikiUserNew pref handling: empty theme not stored, save only
2251 // changed prefs, sql prefs improved, fixed password update,
2252 // removed REPLACE sql (dangerous)
2253 // moved gettext init after the locale was guessed
2254 // + some minor changes
2257 // (c-file-style: "gnu")
2261 // c-basic-offset: 4
2262 // c-hanging-comment-ender-p: nil
2263 // indent-tabs-mode: nil