1 <?php //rcs_id('$Id$');
3 Copyright 1999-2008 $ThePhpWikiProgrammingTeam
4 Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 Standard functions for Wiki functionality
25 WikiURL ($pagename, $args, $get_abs_url)
27 IconForLink ($protocol_or_url)
28 PossiblyGlueIconToText($proto_or_url, $text)
30 LinkURL ($url, $linktext)
31 LinkImage ($url, $alt)
32 ImgObject ($img, $url)
34 SplitQueryArgs ($query_args)
35 LinkPhpwikiURL ($url, $text, $basepage)
36 ConvertOldMarkup ($content, $markup_type = "block")
37 MangleXmlIdentifier($str)
38 UnMangleXmlIdentifier($str)
40 class Stack { push($item), pop(), cnt(), top() }
41 class Alert { show() }
42 class WikiPageName {getParent(),isValid(),getWarnings() }
44 expand_tabs($str, $tab_width = 8)
46 NoSuchRevision ($request, $page, $version)
47 TimezoneOffset ($time, $no_colon)
48 Iso8601DateTime ($time)
49 Rfc2822DateTime ($time)
50 ParseRfc1123DateTime ($timestr)
52 ByteFormatter ($bytes = 0, $longformat = false)
55 __vsprintf ($fmt, $args)
57 file_mtime ($filename)
58 sort_file_mtime ($a, $b)
59 class fileSet {fileSet($directory, $filepattern = false),
60 getFiles($exclude='', $sortby='', $limit='') }
61 class ListRegexExpand { listMatchCallback($item, $key),
62 expandRegex ($index, &$pages) }
65 glob_match ($glob, $against, $case_sensitive = true)
66 explodeList ($input, $allnames, $glob_style = true, $case_sensitive = true)
67 explodePageList ($input, $perm = false)
69 can ($object, $method)
70 function_usable ($function_name)
72 better_srand ($seed = '')
75 subPageSlice ($pagename, $pos)
79 obj2hash ($obj, $exclude = false, $fields = false)
82 url_get_contents ($uri)
84 firstNWordsOfContent ($n, $content)
85 extractSection ($section, $content, $page, $quiet = false, $sectionhead = false)
88 charset_convert($from, $to, $data)
89 string_starts_with($string, $prefix)
90 string_ends_with($string, $suffix)
91 array_remove($arr,$value)
92 longer_timeout($secs=30)
95 binary_search($needle, $haystack)
97 javascript_quote_string($s)
99 parse_attributes($line)
102 compute_tablecell ($table, $i, $j, $imax, $jmax)
104 function: LinkInterWikiLink($link, $linktext)
105 moved to: lib/interwiki.php
106 function: linkExistingWikiWord($wikiword, $linktext, $version)
107 moved to: lib/WikiTheme.php
108 function: LinkUnknownWikiWord($wikiword, $linktext)
109 moved to: lib/WikiTheme.php
110 function: UpdateRecentChanges($dbi, $pagename, $isnewpage)
111 gone see: lib/plugin/RecentChanges.php
113 if (defined('_PHPWIKI_STDLIB_LOADED')) return;
114 else define('_PHPWIKI_STDLIB_LOADED', true);
116 define('MAX_PAGENAME_LENGTH', 100);
119 * Convert string to a valid XML identifier.
121 * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
123 * We would like to have, e.g. named anchors within wiki pages
124 * names like "Table of Contents" --- clearly not a valid XML
125 * fragment identifier.
127 * This function implements a one-to-one map from {any string}
128 * to {valid XML identifiers}.
131 * converting all bytes not in [A-Za-z0-9:_-],
132 * and any leading byte not in [A-Za-z] to 'xbb.',
133 * where 'bb' is the hexadecimal representation of the
136 * As a special case, the empty string is converted to 'empty.'
141 function MangleXmlIdentifier($str) {
145 return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
146 "'x' . sprintf('%02x', ord('\\0')) . '.'",
150 function UnMangleXmlIdentifier($str) {
151 if ($str == 'empty.')
153 return preg_replace('/x(\w\w)\./e',
154 "sprintf('%c', hex('\\0'))",
159 * Returns a name for the WIKI_ID cookie that should be unique on the host.
160 * But for it to be unique you must have set a unique WIKI_NAME in your
161 * configuration file.
162 * @return string The name of the WIKI_ID cookie to use for this wiki.
164 function GetCookieName() {
165 return preg_replace("/[^\d\w]/", "_", WIKI_NAME) . "_WIKI_ID";
169 * Generates a valid URL for a given Wiki pagename.
170 * @param mixed $pagename If a string this will be the name of the Wiki page to link to.
171 * If a WikiDB_Page object function will extract the name to link to.
172 * If a WikiDB_PageRevision object function will extract the name to link to.
174 * @param boolean $get_abs_url Default value is false.
175 * @return string The absolute URL to the page passed as $pagename.
177 function WikiURL($pagename, $args = '', $get_abs_url = false) {
178 global $request, $WikiTheme;
181 if (is_object($pagename)) {
182 if (isa($pagename, 'WikiDB_Page')) {
183 $pagename = $pagename->getName();
185 elseif (isa($pagename, 'WikiDB_PageRevision')) {
186 $page = $pagename->getPage();
187 $args['version'] = $pagename->getVersion();
188 $pagename = $page->getName();
190 elseif (isa($pagename, 'WikiPageName')) {
191 $anchor = $pagename->anchor;
192 $pagename = $pagename->name;
194 $anchor = $pagename->anchor;
195 $pagename = $pagename->name;
198 if (!$get_abs_url and DEBUG and $request->getArg('start_debug')) {
200 $args = 'start_debug=' . $request->getArg('start_debug');
201 elseif (is_array($args))
202 $args['start_debug'] = $request->getArg('start_debug');
204 $args .= '&start_debug=' . $request->getArg('start_debug');
206 if (is_array($args)) {
208 foreach ($args as $key => $val) {
209 // avoid default args
210 if (USE_PATH_INFO and $key == 'pagename')
212 elseif ($key == 'action' and $val == 'browse')
214 elseif (!is_array($val)) // ugly hack for getURLtoSelf() which also takes POST vars
215 $enc_args[] = urlencode($key) . '=' . urlencode($val);
217 $args = join('&', $enc_args);
220 if (USE_PATH_INFO or !empty($WikiTheme->HTML_DUMP_SUFFIX)) {
221 $url = $get_abs_url ? (SERVER_URL . VIRTUAL_PATH . "/") : "";
222 $base = preg_replace('/%2f/i', '/', rawurlencode($pagename));
224 if (!empty($WikiTheme->HTML_DUMP_SUFFIX)) {
225 if (!empty($WikiTheme->VALID_LINKS) and $request->getArg('action') == 'pdf') {
226 if (!in_array($pagename, $WikiTheme->VALID_LINKS))
229 $url = $base . $WikiTheme->HTML_DUMP_SUFFIX;
231 $url .= $WikiTheme->HTML_DUMP_SUFFIX;
241 $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
242 $url .= "?pagename=" . rawurlencode($pagename);
247 $url .= "#" . MangleXmlIdentifier($anchor);
251 /** Convert relative URL to absolute URL.
253 * This converts a relative URL to one of PhpWiki's support files
254 * to an absolute one.
257 * @return string Absolute URL
259 function AbsoluteURL ($url) {
260 if (preg_match('/^https?:/', $url))
262 if ($url[0] != '/') {
263 $base = USE_PATH_INFO ? VIRTUAL_PATH : dirname(SCRIPT_NAME);
264 while ($base != '/' and substr($url, 0, 3) == "../") {
265 $url = substr($url, 3);
266 $base = dirname($base);
272 return SERVER_URL . $url;
275 function DataURL ($url) {
276 if (preg_match('/^https?:/', $url))
278 $url = NormalizeWebFileName($url);
279 if (DEBUG and $GLOBALS['request']->getArg('start_debug') and substr($url,-4,4) == '.php')
280 $url .= "?start_debug=1"; // XMLRPC and SOAP debugging helper.
281 return AbsoluteURL($url);
285 * Generates icon in front of links.
287 * @param string $protocol_or_url URL or protocol to determine which icon to use.
289 * @return HtmlElement HtmlElement object that contains data to create img link to
290 * icon for use with url or protocol passed to the function. False if no img to be
293 function IconForLink($protocol_or_url) {
295 if (0 and $filename_suffix == false) {
296 // display apache style icon for file type instead of protocol icon
297 // - archive: unix:gz,bz2,tgz,tar,z; mac:dmg,dmgz,bin,img,cpt,sit; pc:zip;
298 // - document: html, htm, text, txt, rtf, pdf, doc
299 // - non-inlined image: jpg,jpeg,png,gif,tiff,tif,swf,pict,psd,eps,ps
300 // - audio: mp3,mp2,aiff,aif,au
301 // - multimedia: mpeg,mpg,mov,qt
303 list ($proto) = explode(':', $protocol_or_url, 2);
304 $src = $WikiTheme->getLinkIconURL($proto);
306 return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon', 'border' => 0));
313 * Glue icon in front of or after text.
314 * Pref: 'noLinkIcons' - ignore icon if set
315 * WikiTheme: 'LinkIcons' - 'yes' at front
316 * - 'no' display no icon
317 * - 'front' display at left
318 * - 'after' display at right
320 * @param string $protocol_or_url Protocol or URL. Used to determine the
322 * @param string $text The text.
323 * @return XmlContent.
325 function PossiblyGlueIconToText($proto_or_url, $text) {
326 global $request, $WikiTheme;
327 if ($request->getPref('noLinkIcons'))
329 $icon = IconForLink($proto_or_url);
332 if ($where = $WikiTheme->getLinkIconAttr()) {
333 if ($where == 'no') return $text;
334 if ($where != 'after') $where = 'front';
338 if ($where == 'after') {
339 // span the icon only to the last word (tie them together),
340 // to let the previous words wrap on line breaks.
341 if (!is_object($text)) {
342 preg_match('/^(\s*\S*)(\s*)$/', $text, $m);
343 list (, $prefix, $last_word) = $m;
349 $text = HTML::span(array('style' => 'white-space: nowrap'),
350 $last_word, HTML::Raw(' '), $icon);
352 $text = HTML($prefix, $text);
355 // span the icon only to the first word (tie them together),
356 // to let the next words wrap on line breaks
357 if (!is_object($text)) {
358 preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
359 list (, $first_word, $tail) = $m;
365 $text = HTML::span(array('style' => 'white-space: nowrap'),
368 $text = HTML($text, $tail);
373 * Determines if the url passed to function is safe, by detecting if the characters
374 * '<', '>', or '"' are present.
375 * Check against their urlencoded values also.
377 * @param string $url URL to check for unsafe characters.
378 * @return boolean True if same, false else.
380 function IsSafeURL($url) {
381 return !preg_match('/([<>"])|(%3C)|(%3E)|(%22)/', $url);
385 * Generates an HtmlElement object to store data for a link.
387 * @param string $url URL that the link will point to.
388 * @param string $linktext Text to be displayed as link.
389 * @return HtmlElement HtmlElement object that contains data to construct an html link.
391 function LinkURL($url, $linktext = '') {
392 // FIXME: Is this needed (or sufficient?)
393 if(! IsSafeURL($url)) {
394 $link = HTML::strong(HTML::u(array('class' => 'baduri'),
395 _("BAD URL -- remove all of <, >, \"")));
399 $linktext = preg_replace("/mailto:/A", "", $url);
400 $args = array('href' => $url);
401 if ( defined('EXTERNAL_LINK_TARGET') ) // can also be set in the css
402 $args['target'] = (is_string(EXTERNAL_LINK_TARGET) and (EXTERNAL_LINK_TARGET != "")) ? EXTERNAL_LINK_TARGET : "_blank";
403 $link = HTML::a($args, PossiblyGlueIconToText($url, $linktext));
405 $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
412 * Syntax: [image.png size=50% border=n align= hspace= vspace= width= height=]
413 * Disallows sizes which are too small.
414 * Spammers may use such (typically invisible) image attributes to raise their GoogleRank.
416 * Handle embeddable objects, like svg, class, vrml, swf, svgz, pdf, avi, wmv especially.
418 function LinkImage($url, $alt = "") {
419 $force_img = "png|jpg|gif|jpeg|bmp|pl|cgi";
420 // Disallow tags in img src urls. Typical CSS attacks.
421 // FIXME: Is this needed (or sufficient?)
422 // FIXED: This was broken for moniker:TP30 test/image.png => url="moniker:TP30" attr="test/image.png"
424 // support new syntax: [prefix/image.jpg size=50% border=n]
425 if (empty($alt)) $alt = "";
428 $arr = split(' ',$url);
429 if (!empty($arr)) $url = $arr[0];
430 if (! IsSafeURL($url)) {
431 $link = HTML::strong(HTML::u(array('class' => 'baduri'),
432 _("BAD URL -- remove all of <, >, \"")));
435 // spaces in inline images must be %20 encoded!
436 $link = HTML::img(array('src' => $url));
438 // Extract attributes
439 $arr = parse_attributes(strstr($ori_url, " "));
440 foreach ($arr as $attr => $value) {
441 // These attributes take strings: lang, id, title, alt
442 if (($attr == "lang")
444 || ($attr == "title")
445 || ($attr == "alt")) {
446 $link->setAttr($attr, $value);
448 // align = bottom|middle|top|left|right
449 elseif (($attr == "align")
450 && (($value == "bottom")
451 || ($value == "middle")
453 || ($value == "left")
454 || ($value == "right"))) {
455 $link->setAttr($attr, $value);
457 // These attributes take a number (pixels): border, hspace, vspace
458 elseif ((($attr == "border") || ($attr == "hspace") || ($attr == "vspace"))
459 && (is_int($value))) {
460 $link->setAttr($attr, $value);
462 // These attributes take a number (pixels) or a percentage: height, width
463 elseif ((($attr == "border") || ($attr == "hspace") || ($attr == "vspace"))
464 && (preg_match('/\d+[%p]?x?/', $value))) {
465 $link->setAttr($attr, $value);
467 // We allow size=50% and size=20x30
468 // We replace this with "width" and "height" HTML attributes
469 elseif ($attr == "size") {
470 if (preg_match('/(\d+)%/', $value, $m)) {
471 $link->setAttr('width',$m[1]);
472 $link->setAttr('height',$m[1]);
473 } elseif (preg_match('/(\d+)x(\d+)/', $value, $m)) {
474 $link->setAttr('width',$m[1]);
475 $link->setAttr('height',$m[2]);
479 trigger_error(sprintf(_("Invalid image attribute \"%s\" %s=%s"),
480 $url, $attr, $value), E_USER_WARNING);
483 // Correct silently the most common error
484 if ($url != $ori_url and empty($arr) and !preg_match("/^http/",$url)) {
485 // space belongs to the path
486 $file = NormalizeLocalFileName($ori_url);
487 if (file_exists($file)) {
488 $link = HTML::img(array('src' => $ori_url));
490 sprintf(_("Invalid image link fixed %s => %s. Spaces must be quoted with %%20."),
491 $url, $ori_url), E_USER_WARNING);
492 } elseif (string_starts_with($ori_url, getUploadDataPath())) {
493 $file = substr($file, strlen(getUploadDataPath()));
494 $path = getUploadFilePath().$file;
495 if (file_exists($path)) {
496 trigger_error(sprintf(_("Invalid image link fixed \"%s\" => \"%s\".\n Spaces must be quoted with %%20."),
497 $url, $ori_url), E_USER_WARNING);
498 $link->setAttr('src', getUploadDataPath() . $file);
503 if (!$link->getAttr('alt')) {
504 $link->setAttr('alt', $alt);
506 // Check width and height as spam countermeasure
507 if (($width = $link->getAttr('width')) and ($height = $link->getAttr('height'))) {
508 //$width = (int) $width; // px or % or other suffix
509 //$height = (int) $height;
510 if (($width < 3 and $height < 10) or
511 ($height < 3 and $width < 20) or
512 ($height < 7 and $width < 7))
514 trigger_error(_("Invalid image size"), E_USER_WARNING);
519 // Prepare for getimagesize($url)
520 // $url only valid for external urls, otherwise local path
521 // Older php versions crash here with certain png's:
522 // confirmed for 4.1.2, 4.1.3, 4.2.3; 4.3.2 and 4.3.7 are ok
523 // http://phpwiki.sourceforge.net/demo/themes/default/images/http.png
524 // See http://bugs.php.net/search.php?cmd=display&search_for=getimagesize
525 if (DISABLE_GETIMAGESIZE)
527 elseif (! preg_match("/\.$force_img$/i", $url))
528 ; // only valid image extensions or scripts assumed to generate images
529 elseif (!check_php_version(4,3) and preg_match("/^http.+\.png$/i",$url))
530 ; // it's safe to assume that this will fail.
531 elseif (preg_match("/^http/",$url)) { // external url
532 $size = @getimagesize($url);
533 } else { // local file
534 if (file_exists($file = NormalizeLocalFileName($url))) { // here
535 $size = @getimagesize($file);
536 } elseif (file_exists(NormalizeLocalFileName(urldecode($url)))) {
537 $size = @getimagesize($file);
538 $link->setAttr('src', rawurldecode($url));
539 } elseif (string_starts_with($url, getUploadDataPath())) { // there
540 $file = substr($file, strlen(getUploadDataPath()));
541 $path = getUploadFilePath().rawurldecode($file);
542 $size = @getimagesize($path);
543 $link->setAttr('src', getUploadDataPath() . rawurldecode($file));
544 } else { // elsewhere
545 $size = @getimagesize($request->get('DOCUMENT_ROOT').urldecode($url));
551 if (($width < 3 and $height < 10)
552 or ($height < 3 and $width < 20)
553 or ($height < 7 and $width < 7))
555 trigger_error(_("Invalid image size"), E_USER_WARNING);
560 $link->setAttr('class', 'inlineimage');
562 /* Check for inlined objects. Everything allowed in INLINE_IMAGES besides
563 * png|jpg|gif|jpeg|bmp|pl|cgi. If no image it is an object to embed.
564 * Note: Allow cgi's (pl,cgi) returning images.
566 if (!preg_match("/\.(".$force_img.")/i", $url)) {
567 // HTML::img(array('src' => $url, 'alt' => $alt, 'title' => $alt));
568 // => HTML::object(array('src' => $url)) ...;
569 return ImgObject($link, $ori_url);
575 * <object> / <embed> tags instead of <img> for all non-image extensions
577 * Called by LinkImage(), not directly.
578 * Syntax: [image.svg size=50% alt=image.gif border=n align= hspace= vspace= width= height=]
579 * Samples: [Upload:song.mp3 type=audio/mpeg width=200 height=10]
580 * $alt may be an alternate img
581 * TODO: Need to unify with WikiPluginCached::embedObject()
583 * Note that Safari 1.0 will crash with <object>, so use only <embed>
584 * http://www.alleged.org.uk/pdc/2002/svg-object.html
586 * Allowed object tags:
587 * DATA=URI (object data)
588 * CLASSID=URI (location of implementation)
589 * ARCHIVE=CDATA (archive files)
590 * CODEBASE=URI (base URI for CLASSID, DATA, ARCHIVE)
591 * WIDTH=Length (object width)
592 * HEIGHT=Length (object height)
593 * NAME=CDATA (name for form submission)
594 * USEMAP=URI (client-side image map)
595 * TYPE=ContentType (content-type of object)
596 * CODETYPE=ContentType (content-type of code)
597 * STANDBY=Text (message to show while loading)
598 * TABINDEX=NUMBER (position in tabbing order)
599 * DECLARE (do not instantiate object)
600 * The rest is added as <param name="" value="" /> tags
602 function ImgObject($img, $url) {
603 // get the url args: data="sample.svgz" type="image/svg+xml" width="400" height="300"
604 $args = split(' ', $url);
606 if (count($args) >= 1) {
607 $url = array_shift($args);
609 foreach ($args as $attr) {
610 foreach (explode(",","data,classid,archive,codebase,name,usemap,type,".
611 "codetype,standby,tabindex,declare") as $param)
613 if (preg_match("/^$param=(\S+)$/i",$attr,$m)) {
614 $img->setAttr($param, $m[1]);
620 // now all remaing args are added as <param> to the object
621 foreach ($args as $attr) {
622 if (!$found[$attr] and preg_match("/^(\S+)=(\S+)$/i",$attr,$m)) {
623 $params[] = HTML::param(array('name' => $m[1],
628 $type = $img->getAttr('type');
630 // TODO: map extension to mime-types if type is not given and php < 4.3
631 if (function_exists('mime_content_type'))
632 $type = mime_content_type($url);
634 $object = HTML::object(array_merge($img->_attr, array('src' => $url, 'type' => $type)),
636 $object->setAttr('class', 'inlineobject');
638 foreach ($params as $param) $object->pushContent($param);
640 if (isBrowserSafari()) {
641 return HTML::embed($object->_attr, $object->_content);
643 $object->pushContent(HTML::embed($object->_attr));
650 // var in php5 deprecated
652 $this->items = array();
655 function push($item) {
656 $this->items[$this->size] = $item;
662 if ($this->size == 0) {
663 return false; // stack is empty
666 return $this->items[$this->size];
675 return $this->items[$this->size - 1];
681 // end class definition
683 function SplitQueryArgs ($query_args = '')
685 // FIXME: use the arg-seperator which might not be &
686 $split_args = split('&', $query_args);
688 while (list($key, $val) = each($split_args))
689 if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
690 $args[$m[1]] = $m[2];
694 function LinkPhpwikiURL($url, $text = '', $basepage = false) {
697 if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m)) {
698 return HTML::strong(array('class' => 'rawurl'),
699 HTML::u(array('class' => 'baduri'),
700 _("BAD phpwiki: URL")));
704 $pagename = urldecode($m[1]);
707 if (empty($pagename) &&
708 preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)) {
709 // Convert old style links (to not break diff links in
711 $pagename = urldecode($m[2]);
712 $args = array("action" => $m[1]);
715 $args = SplitQueryArgs($qargs);
718 if (empty($pagename))
719 $pagename = $GLOBALS['request']->getArg('pagename');
721 if (isset($args['action']) && $args['action'] == 'browse')
722 unset($args['action']);
725 if (empty($args['action']))
727 else if (is_safe_action($args['action']))
728 $class = 'wikiaction';
730 if (empty($args['action']) || is_safe_action($args['action']))
731 $class = 'wikiaction';
733 // Don't allow administrative links on unlocked pages.
734 $dbi = $GLOBALS['request']->getDbh();
735 $page = $dbi->getPage($basepage ? $basepage : $pagename);
736 if (!$page->get('locked'))
737 return HTML::span(array('class' => 'wikiunsafe'),
738 HTML::u(_("Lock page to enable link")));
739 $class = 'wikiadmin';
743 $text = HTML::span(array('class' => 'rawurl'), $url);
745 $wikipage = new WikiPageName($pagename);
746 if (!$wikipage->isValid()) {
748 return $WikiTheme->linkBadWikiWord($wikipage, $url);
751 return HTML::a(array('href' => WikiURL($pagename, $args),
757 * A class to assist in parsing wiki pagenames.
759 * Now with subpages and anchors, parsing and passing around
760 * pagenames is more complicated. This should help.
764 /** Short name for page.
766 * This is the value of $name passed to the constructor.
767 * (For use, e.g. as a default label for links to the page.)
771 /** The full page name.
773 * This is the full name of the page (without anchor).
779 * This is the referenced anchor within the page, or the empty string.
785 * @param mixed $name Page name.
786 * WikiDB_Page, WikiDB_PageRevision, or string.
787 * This can be a relative subpage name (like '/SubPage'),
788 * or can be the empty string to refer to the $basename.
790 * @param string $anchor For links to anchors in page.
792 * @param mixed $basename Page name from which to interpret
793 * relative or other non-fully-specified page names.
795 function WikiPageName($name, $basename=false, $anchor=false) {
796 if (is_string($name)) {
797 $this->shortName = $name;
798 if (strstr($name, ':')) {
799 list($moniker, $shortName) = split (":", $name, 2);
800 $map = getInterwikiMap(); // allow overrides to custom maps
801 if (isset($map->_map[$moniker])) {
802 $url = $map->_map[$moniker];
803 if (strstr($url, '%s'))
804 $url = sprintf($url, $shortName);
808 // expand Talk or User, but not to absolute urls!
809 if (strstr($url, '//')) {
810 if ($moniker == 'Talk')
811 $name = $name . SUBPAGE_SEPARATOR . _("Discussion");
812 elseif ($moniker == 'User')
817 if (strstr($shortName, '?')) {
818 list($shortName, $dummy) = split("\?", $shortName, 2);
820 $this->shortName = $shortName;
823 // FIXME: We should really fix the cause for "/PageName" in the WikiDB
824 if ($name == '' or $name[0] == SUBPAGE_SEPARATOR) {
826 $name = $this->_pagename($basename) . $name;
828 $name = $this->_normalize_bad_pagename($name);
829 $this->shortName = $name;
834 $name = $this->_pagename($name);
835 $this->shortName = $name;
838 $this->name = $this->_check($name);
839 $this->anchor = (string)$anchor;
846 function getParent() {
848 if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
850 return substr($name, 0, -strlen($tail));
853 function isValid($strict = false) {
855 return !isset($this->_errors);
856 return (is_string($this->name) and $this->name != '');
859 function getWarnings() {
861 if (isset($this->_warnings))
862 $warnings = array_merge($warnings, $this->_warnings);
863 if (isset($this->_errors))
864 $warnings = array_merge($warnings, $this->_errors);
868 return sprintf(_("'%s': Bad page name: %s"),
869 $this->shortName, join(', ', $warnings));
872 function _pagename($page) {
873 if (isa($page, 'WikiDB_Page'))
874 return $page->getName();
875 elseif (isa($page, 'WikiDB_PageRevision'))
876 return $page->getPageName();
877 elseif (isa($page, 'WikiPageName'))
879 // '0' or e.g. '1984' should be allowed though
880 if (!is_string($page) and !is_integer($page)) {
881 trigger_error(sprintf("Non-string pagename '%s' (%s)(%s)",
882 $page, gettype($page), get_class($page)),
885 //assert(is_string($page));
889 function _normalize_bad_pagename($name) {
890 trigger_error("Bad pagename: " . $name, E_USER_WARNING);
892 // Punt... You really shouldn't get here.
895 return $request->getArg('pagename');
897 assert($name[0] == SUBPAGE_SEPARATOR);
898 $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
899 return substr($name, 1);
903 * Compress internal white-space to single space character.
905 * This leads to problems with loading a foreign charset pagename,
906 * which cannot be deleted anymore, because unknown chars are compressed.
907 * So BEFORE importing a file _check must be done !!!
909 function _check($pagename) {
910 // Compress internal white-space to single space character.
911 $pagename = preg_replace('/[\s\xa0]+/', ' ', $orig = $pagename);
912 if ($pagename != $orig)
913 $this->_warnings[] = _("White space converted to single space");
915 // Delete any control characters.
916 if (DATABASE_TYPE == 'cvs' or DATABASE_TYPE == 'file' or DATABASE_TYPE == 'flatfile') {
917 $pagename = preg_replace('/[\x00-\x1f\x7f\x80-\x9f]/', '', $orig = $pagename);
918 if ($pagename != $orig)
919 $this->_errors[] = _("Control characters not allowed");
922 // Strip leading and trailing white-space.
923 $pagename = trim($pagename);
926 while ($pagename and $pagename[0] == SUBPAGE_SEPARATOR)
927 $pagename = substr($pagename, 1);
928 if ($pagename != $orig)
929 $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
931 // ";" is urlencoded, so safe from php arg-delim problems
932 /*if (strstr($pagename, ';')) {
933 $this->_warnings[] = _("';' is deprecated");
934 $pagename = str_replace(';', '', $pagename);
937 // not only for SQL, also to restrict url length
938 if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
939 $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
940 $this->_errors[] = _("too long");
943 // disallow some chars only on file and cvs
944 if ((DATABASE_TYPE == 'cvs'
945 or DATABASE_TYPE == 'file'
946 or DATABASE_TYPE == 'flatfile')
947 and preg_match('/(:|\.\.)/', $pagename, $m))
949 $this->_warnings[] = sprintf(_("Illegal chars %s removed"), $m[1]);
950 $pagename = str_replace('..', '', $pagename);
951 $pagename = str_replace(':', '', $pagename);
959 * Convert old page markup to new-style markup.
961 * @param string $text Old-style wiki markup.
963 * @param string $markup_type
965 * <dt><code>"block"</code> <dd>Convert all markup.
966 * <dt><code>"inline"</code> <dd>Convert only inline markup.
967 * <dt><code>"links"</code> <dd>Convert only link markup.
970 * @return string New-style wiki markup.
972 * @bugs Footnotes don't work quite as before (esp if there are
973 * multiple references to the same footnote. But close enough,
974 * probably for now....
975 * @bugs Apache2 and IIS crash with OldTextFormattingRules or
976 * AnciennesR%E8glesDeFormatage. (at the 2nd attempt to do the anchored block regex)
977 * It only crashes with CreateToc so far, but other pages (not in pgsrc) are
978 * also known to crash, even with Apache1.
980 function ConvertOldMarkup ($text, $markup_type = "block") {
986 // Trying to detect why the 2nd paragraph of OldTextFormattingRules or
987 // AnciennesR%E8glesDeFormatage crashes.
988 // It only crashes with CreateToc so far, but other pages (not in pgsrc) are
989 // also known to crash, even with Apache1.
991 // I suspect this only to crash with Apache2 and IIS.
992 if (in_array(php_sapi_name(),array('apache2handler','apache2filter','isapi'))
993 and preg_match("/plugin CreateToc/", $text))
995 trigger_error(_("The CreateTocPlugin is not yet old markup compatible! ")
996 ._("Please remove the CreateToc line to be able to reformat this page to old markup. ")
997 ._("Skipped."), E_USER_WARNING);
999 //if (!DEBUG) return $text;
1004 /*****************************************************************
1005 * Conversions for inline markup:
1012 // escape escaped brackets
1016 // change ! escapes to ~'s.
1017 global $WikiNameRegexp, $request;
1018 $bang_esc[] = "(?:" . ALLOWED_PROTOCOLS . "):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]";
1019 // before 4.3.9 pcre had a memory release bug, which might hit us here. so be safe.
1020 if (check_php_version(4,3,9)) {
1021 $map = getInterwikiMap();
1022 if ($map_regex = $map->getRegexp())
1023 $bang_esc[] = $map_regex . ":[^\\s.,;?()]+"; // FIXME: is this really needed?
1025 $bang_esc[] = $WikiNameRegexp;
1026 $orig[] = '/!((?:' . join(')|(', $bang_esc) . '))/';
1029 $subs["links"] = array($orig, $repl);
1031 // Temporarily URL-encode pairs of underscores in links to hide
1032 // them from the re for bold markup.
1033 $orig[] = '/\[[^\[\]]*?__[^\[\]]*?\]/e';
1034 $repl[] = 'str_replace(\'__\', \'%5F%5F\', \'\\0\')';
1037 //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
1040 // Convert footnote references.
1041 $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
1042 $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
1044 // Convert old style emphases to HTML style emphasis.
1045 $orig[] = '/__(.*?)__/';
1046 $repl[] = '<strong>\\1</strong>';
1047 $orig[] = "/''(.*?)''/";
1048 $repl[] = '<em>\\1</em>';
1050 // Escape nestled markup.
1051 $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
1054 // in old markup headings only allowed at beginning of line
1058 // Convert URL-encoded pairs of underscores in links back to
1059 // real underscores after bold markup has been converted.
1060 $orig = '/\[[^\[\]]*?%5F%5F[^\[\]]*?\]/e';
1061 $repl = 'str_replace(\'%5F%5F\', \'__\', \'\\0\')';
1063 $subs["inline"] = array($orig, $repl);
1065 /*****************************************************************
1066 * Patterns which match block markup constructs which take
1067 * special handling...
1071 $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
1073 $blockpats[] = '\|(?:.*\n\|)*';
1076 $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
1078 // Footnote definitions
1079 $blockpats[] = '\[\s*(\d+)\s*\]';
1083 $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
1087 $blockpats[] = '!{1,3}[^!]';
1089 removed .|\n in the anchor not to crash on /m because with /m "." already includes \n
1090 this breaks headings but it doesn't crash anymore (crash on non-cgi, non-cli only)
1092 $block_re = ( '/\A((?:.|\n)*?)(^(?:'
1093 . join("|", $blockpats)
1098 if ($markup_type != "block") {
1099 list ($orig, $repl) = $subs[$markup_type];
1100 return preg_replace($orig, $repl, $text);
1103 list ($orig, $repl) = $subs['inline'];
1106 // php crashes here in the 2nd paragraph of OldTextFormattingRules,
1107 // AnciennesR%E8glesDeFormatage and more
1108 // See http://www.pcre.org/pcre.txt LIMITATIONS
1109 while (preg_match($block_re, $text, $m)) {
1110 $text = substr($text, strlen($m[0]));
1111 list (,$leading_text, $block) = $m;
1114 if (strchr(" \t", $block[0])) {
1116 $prefix = "<pre>\n";
1117 $suffix = "\n</pre>\n";
1119 elseif ($block[0] == '|') {
1121 $prefix = "<?plugin OldStyleTable\n";
1124 elseif (strchr("#*;", $block[0])) {
1125 // Old-style list item
1126 preg_match('/^([#*;]*)([*#]|;.*?:) */', $block, $m);
1127 list (,$ind,$bullet) = $m;
1128 $block = substr($block, strlen($m[0]));
1130 $indent = str_repeat(' ', strlen($ind));
1131 if ($bullet[0] == ';') {
1132 //$term = ltrim(substr($bullet, 1));
1133 //return $indent . $term . "\n" . $indent . ' ';
1134 $prefix = $ind . $bullet;
1137 $prefix = $indent . $bullet . ' ';
1139 elseif ($block[0] == '[') {
1140 // Footnote definition
1141 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
1143 $block = substr($block, strlen($m[0]));
1144 $prefix = "#[|ftnt_".${footnum}."]~[[".${footnum}."|#ftnt_ref_".${footnum}."]~] ";
1146 elseif ($block[0] == '<') {
1148 // HACK: no inline markup...
1152 elseif ($block[0] == '!') {
1154 preg_match('/^!{1,3}/', $block, $m);
1156 $block = substr($block, strlen($m[0]));
1162 if ($leading_text) $leading_text = preg_replace($orig, $repl, $leading_text);
1163 if ($block) $block = preg_replace($orig, $repl, $block);
1164 $out .= $leading_text;
1169 return $out . preg_replace($orig, $repl, $text);
1175 * Expand tabs in string.
1177 * Converts all tabs to (the appropriate number of) spaces.
1179 * @param string $str
1180 * @param integer $tab_width
1183 function expand_tabs($str, $tab_width = 8) {
1184 $split = split("\t", $str);
1185 $tail = array_pop($split);
1187 foreach ($split as $hunk) {
1189 $pos = strlen(strrchr($expanded, "\n")) - 1;
1190 $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
1192 return substr($expanded, 1) . $tail;
1196 * Split WikiWords in page names.
1198 * It has been deemed useful to split WikiWords (into "Wiki Words") in
1199 * places like page titles. This is rumored to help search engines
1202 * @param $page string The page name.
1204 * @return string The split name.
1206 function SplitPagename ($page) {
1208 if (preg_match("/\s/", $page))
1209 return $page; // Already split --- don't split any more.
1211 // This algorithm is specialized for several languages.
1212 // (Thanks to Pierrick MEIGNEN)
1213 // Improvements for other languages welcome.
1216 // This mess splits between a lower-case letter followed by
1217 // either an upper-case or a numeral; except that it wont
1218 // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
1219 switch ($GLOBALS['LANG']) {
1224 $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
1227 $RE[] = '/([[:lower:]])((?<!Mc|Di)[[:upper:]]|\d)/';
1230 $sep = preg_quote(SUBPAGE_SEPARATOR, '/');
1231 // This the single-letter words 'I' and 'A' from any following
1232 // capitalized words.
1233 switch ($GLOBALS['LANG']) {
1235 $RE[] = "/(?<= |${sep}|^)([AI])([[:upper:]][[:lower:]])/";
1238 $RE[] = "/(?<= |${sep}|^)([À])([[:upper:]][[:lower:]])/";
1241 // Split at underscore
1242 $RE[] = '/(_)([[:alpha:]])/';
1243 $RE[] = '/([[:alpha:]])(_)/';
1244 // Split numerals from following letters.
1245 $RE[] = '/(\d)([[:alpha:]])/';
1246 // Split at subpage seperators. TBD in WikiTheme.php
1247 $RE[] = "/([^${sep}]+)(${sep})/";
1248 $RE[] = "/(${sep})([^${sep}]+)/";
1250 foreach ($RE as $key)
1251 $RE[$key] = pcre_fix_posix_classes($key);
1254 foreach ($RE as $regexp) {
1255 $page = preg_replace($regexp, '\\1 \\2', $page);
1260 function NoSuchRevision (&$request, $page, $version) {
1261 $html = HTML(HTML::h2(_("Revision Not Found")),
1262 HTML::p(fmt("I'm sorry. Version %d of %s is not in the database.",
1263 $version, WikiLink($page, 'auto'))));
1264 include_once('lib/Template.php');
1265 GeneratePage($html, _("Bad Version"), $page->getCurrentRevision());
1271 * Get time offset for local time zone.
1273 * @param $time time_t Get offset for this time. Default: now.
1274 * @param $no_colon boolean Don't put colon between hours and minutes.
1275 * @return string Offset as a string in the format +HH:MM.
1277 function TimezoneOffset ($time = false, $no_colon = false) {
1278 if ($time === false)
1280 $secs = date('Z', $time);
1289 $colon = $no_colon ? '' : ':';
1290 $mins = intval(($secs + 30) / 60);
1291 return sprintf("%s%02d%s%02d",
1292 $sign, $mins / 60, $colon, $mins % 60);
1297 * Format time in ISO-8601 format.
1299 * @param $time time_t Time. Default: now.
1300 * @return string Date and time in ISO-8601 format.
1302 function Iso8601DateTime ($time = false) {
1303 if ($time === false)
1305 $tzoff = TimezoneOffset($time);
1306 $date = date('Y-m-d', $time);
1307 $time = date('H:i:s', $time);
1308 return $date . 'T' . $time . $tzoff;
1312 * Format time in RFC-2822 format.
1314 * @param $time time_t Time. Default: now.
1315 * @return string Date and time in RFC-2822 format.
1317 function Rfc2822DateTime ($time = false) {
1318 if ($time === false)
1320 return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
1324 * Format time in RFC-1123 format.
1326 * @param $time time_t Time. Default: now.
1327 * @return string Date and time in RFC-1123 format.
1329 function Rfc1123DateTime ($time = false) {
1330 if ($time === false)
1332 return gmdate('D, d M Y H:i:s \G\M\T', $time);
1335 /** Parse date in RFC-1123 format.
1337 * According to RFC 1123 we must accept dates in the following
1340 * Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
1341 * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
1342 * Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
1344 * (Though we're only allowed to generate dates in the first format.)
1346 function ParseRfc1123DateTime ($timestr) {
1347 $timestr = trim($timestr);
1348 if (preg_match('/^ \w{3},\s* (\d{1,2}) \s* (\w{3}) \s* (\d{4}) \s*'
1349 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1351 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1353 elseif (preg_match('/^ \w+,\s* (\d{1,2})-(\w{3})-(\d{2}|\d{4}) \s*'
1354 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1356 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1357 if ($year < 70) $year += 2000;
1358 elseif ($year < 100) $year += 1900;
1360 elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
1361 .'(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
1363 list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
1370 $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
1372 return false; // failed
1377 * Format time to standard 'ctime' format.
1379 * @param $time time_t Time. Default: now.
1380 * @return string Date and time.
1382 function CTime ($time = false)
1384 if ($time === false)
1386 return date("D M j H:i:s Y", $time);
1391 * Format number as kilobytes or bytes.
1392 * Short format is used for PageList
1393 * Long format is used in PageInfo
1395 * @param $bytes int. Default: 0.
1396 * @param $longformat bool. Default: false.
1397 * @return class FormattedText (XmlElement.php).
1399 function ByteFormatter ($bytes = 0, $longformat = false) {
1402 if ($bytes < 1024) {
1404 $size = fmt("%s b", $bytes);
1406 $size = fmt("%s bytes", $bytes);
1409 $kb = round($bytes / 1024, 1);
1411 $size = fmt("%s k", $kb);
1413 $size = fmt("%s Kb (%s bytes)", $kb, $bytes);
1419 * Internationalized printf.
1421 * This is essentially the same as PHP's built-in printf
1422 * with the following exceptions:
1424 * <li> It passes the format string through gettext().
1425 * <li> It supports the argument reordering extensions.
1432 * __printf("Differences between versions %s and %s of %s",
1433 * $new_link, $old_link, $page_link);
1436 * Then in locale/po/de.po, one can reorder the printf arguments:
1439 * msgid "Differences between %s and %s of %s."
1440 * msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
1443 * (Note that while PHP tries to expand $vars within double-quotes,
1444 * the values in msgstr undergo no such expansion, so the '$'s
1447 * One shouldn't use reordered arguments in the default format string.
1448 * Backslashes in the default string would be necessary to escape the
1449 * '$'s, and they'll cause all kinds of trouble....
1451 function __printf ($fmt) {
1452 $args = func_get_args();
1454 echo __vsprintf($fmt, $args);
1458 * Internationalized sprintf.
1460 * This is essentially the same as PHP's built-in printf with the
1461 * following exceptions:
1464 * <li> It passes the format string through gettext().
1465 * <li> It supports the argument reordering extensions.
1470 function __sprintf ($fmt) {
1471 $args = func_get_args();
1473 return __vsprintf($fmt, $args);
1477 * Internationalized vsprintf.
1479 * This is essentially the same as PHP's built-in printf with the
1480 * following exceptions:
1483 * <li> It passes the format string through gettext().
1484 * <li> It supports the argument reordering extensions.
1489 function __vsprintf ($fmt, $args) {
1490 $fmt = gettext($fmt);
1491 // PHP's sprintf doesn't support variable with specifiers,
1492 // like sprintf("%*s", 10, "x"); --- so we won't either.
1494 if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
1495 // Format string has '%2$s' style argument reordering.
1496 // PHP doesn't support this.
1497 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
1498 // literal variable name substitution only to keep locale
1499 // strings uncluttered
1500 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
1501 '%1\$s','%s'), E_USER_WARNING); //php+locale error
1503 $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
1506 // Reorder arguments appropriately.
1507 foreach($m[1] as $argnum) {
1508 if ($argnum < 1 || $argnum > count($args))
1509 trigger_error(sprintf(_("%s: argument index out of range"),
1510 $argnum), E_USER_WARNING);
1511 $newargs[] = $args[$argnum - 1];
1516 // Not all PHP's have vsprintf, so...
1517 array_unshift($args, $fmt);
1518 return call_user_func_array('sprintf', $args);
1521 function file_mtime ($filename) {
1522 if ($stat = @stat($filename))
1528 function sort_file_mtime ($a, $b) {
1529 $ma = file_mtime($a);
1530 $mb = file_mtime($b);
1531 if (!$ma or !$mb or $ma == $mb) return 0;
1532 return ($ma > $mb) ? -1 : 1;
1537 * Build an array in $this->_fileList of files from $dirname.
1538 * Subdirectories are not traversed.
1540 * (This was a function LoadDir in lib/loadsave.php)
1541 * See also http://www.php.net/manual/en/function.readdir.php
1543 function getFiles($exclude='', $sortby='', $limit='') {
1544 $list = $this->_fileList;
1547 require_once('lib/PageList.php');
1548 switch (Pagelist::sortby($sortby, 'db')) {
1549 case 'pagename ASC': break;
1550 case 'pagename DESC':
1551 $list = array_reverse($list);
1554 usort($list,'sort_file_mtime');
1557 usort($list,'sort_file_mtime');
1558 $list = array_reverse($list);
1563 return array_splice($list, 0, $limit);
1567 function _filenameSelector($filename) {
1568 if (! $this->_pattern )
1571 if (! $this->_pcre_pattern )
1572 $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1573 return preg_match('/' . $this->_pcre_pattern . ($this->_case ? '/' : '/i'),
1578 function fileSet($directory, $filepattern = false) {
1579 $this->_fileList = array();
1580 $this->_pattern = $filepattern;
1582 $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1584 $this->_case = !isWindows();
1585 $this->_pathsep = '/';
1587 if (empty($directory)) {
1588 trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1590 return; // early return
1593 @ $dir_handle = opendir($dir=$directory);
1594 if (empty($dir_handle)) {
1595 trigger_error(sprintf(_("Unable to open directory '%s' for reading"),
1596 $dir), E_USER_NOTICE);
1597 return; // early return
1600 while ($filename = readdir($dir_handle)) {
1601 if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1603 if ($this->_filenameSelector($filename)) {
1604 array_push($this->_fileList, "$filename");
1605 //trigger_error(sprintf(_("found file %s"), $filename),
1606 // E_USER_NOTICE); //debugging
1609 closedir($dir_handle);
1615 // expands a list containing regex's to its matching entries
1616 class ListRegexExpand {
1617 //var $match, $list, $index, $case_sensitive;
1618 function ListRegexExpand (&$list, $match, $case_sensitive = true) {
1619 $this->match = $match;
1620 $this->list = &$list;
1621 $this->case_sensitive = $case_sensitive;
1622 //$this->index = false;
1624 function listMatchCallback ($item, $key) {
1625 $quoted = str_replace('/','\/',$item);
1626 if (preg_match('/' . $this->match . ($this->case_sensitive ? '/' : '/i'),
1628 unset($this->list[$this->index]);
1629 $this->list[] = $item;
1632 function expandRegex ($index, &$pages) {
1633 $this->index = $index;
1634 array_walk($pages, array($this, 'listMatchCallback'));
1639 // Convert fileglob to regex style:
1640 // Convert some wildcards to pcre style, escape the rest
1641 // Escape . \\ + * ? [ ^ ] $ ( ) { } = ! < > | : /
1642 // Fixed bug #994994: "/" in $glob.
1643 function glob_to_pcre ($glob) {
1644 // check simple case: no need to escape
1645 $escape = '\[](){}=!<>|:/';
1646 if (strcspn($glob, $escape . ".+*?^$") == strlen($glob))
1648 // preg_replace cannot handle "\\\\\\2" so convert \\ to \xff
1649 $glob = strtr($glob, "\\", "\xff");
1650 $glob = str_replace("/", "\\/", $glob);
1651 // first convert some unescaped expressions to pcre style: . => \.
1653 $re = preg_replace('/([^\xff])?(['.preg_quote($special).'])/',
1654 "\\1\xff\\2", $glob);
1657 $re = preg_replace('/([^\xff])?\*/', '$1.*', $re);
1658 $re = preg_replace('/([^\xff])?\?/', '$1.', $re);
1659 if (!preg_match('/^[\?\*]/', $glob))
1661 if (!preg_match('/[\?\*]$/', $glob))
1664 // Fixes Bug 1182997
1665 // .*? handled above, now escape the rest
1666 //while (strcspn($re, $escape) != strlen($re)) // loop strangely needed
1667 $re = preg_replace('/([^\xff])(['.preg_quote($escape, "/").'])/',
1669 // Problem with 'Date/Time' => 'Date\/Time' => 'Date\xff\/Time' => 'Date\/Time'
1671 $re = preg_replace('/\xff/', '', $re);
1675 function glob_match ($glob, $against, $case_sensitive = true) {
1676 return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'),
1680 function explodeList($input, $allnames, $glob_style = true, $case_sensitive = true) {
1681 $list = explode(',',$input);
1682 // expand wildcards from list of $allnames
1683 if (preg_match('/[\?\*]/',$input)) {
1684 // Optimizing loop invariants:
1685 // http://phplens.com/lens/php-book/optimizing-debugging-php.php
1686 for ($i = 0, $max = sizeof($list); $i < $max; $i++) {
1688 if (preg_match('/[\?\*]/',$f)) {
1690 $expand = new ListRegexExpand($list,
1691 $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1692 $expand->expandRegex($i, $allnames);
1699 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1700 function explodePageList($input, $include_empty=false, $sortby='pagename',
1701 $limit='', $exclude='') {
1702 include_once("lib/PageList.php");
1703 return PageList::explodePageList($input, $include_empty, $sortby, $limit, $exclude);
1706 // Class introspections
1709 * Determine whether object is of a specified type.
1710 * In PHP builtin since 4.2.0 as is_a()
1711 * is_a() deprecated in PHP 5, in favor of instanceof operator
1713 * @param $object object An object.
1714 * @param $class string Class name.
1715 * @return bool True iff $object is a $class
1716 * or a sub-type of $class.
1718 function isa ($object, $class) {
1719 //if (check_php_version(5))
1720 // return $object instanceof $class;
1721 if (check_php_version(4,2) and !check_php_version(5))
1722 return is_a($object, $class);
1724 $lclass = check_php_version(5) ? $class : strtolower($class);
1725 return is_object($object)
1726 && ( strtolower(get_class($object)) == strtolower($class)
1727 || is_subclass_of($object, $lclass) );
1730 /** Determine whether (possible) object has method.
1732 * @param $object mixed Object
1733 * @param $method string Method name
1734 * @return bool True iff $object is an object with has method $method.
1736 function can ($object, $method) {
1737 return is_object($object) && method_exists($object, strtolower($method));
1740 /** Determine whether a function is okay to use.
1742 * Some providers (e.g. Lycos) disable some of PHP functions for
1743 * "security reasons." This makes those functions, of course,
1744 * unusable, despite the fact the function_exists() says they
1747 * This function test to see if a function exists and is not
1748 * disallowed by PHP's disable_functions config setting.
1750 * @param string $function_name Function name
1751 * @return bool True iff function can be used.
1753 function function_usable($function_name) {
1755 if (!is_array($disabled)) {
1756 $disabled = array();
1757 // Use get_cfg_var since ini_get() is one of the disabled functions
1758 // (on Lycos, at least.)
1759 $split = preg_split('/\s*,\s*/', trim(get_cfg_var('disable_functions')));
1760 foreach ($split as $f)
1761 $disabled[strtolower($f)] = true;
1764 return ( function_exists($function_name)
1765 and ! isset($disabled[strtolower($function_name)])
1772 * This is used for generating ETags.
1774 function wikihash ($x) {
1775 if (is_scalar($x)) {
1778 elseif (is_array($x)) {
1780 return md5(serialize($x));
1782 elseif (is_object($x)) {
1785 trigger_error("Can't hash $x", E_USER_ERROR);
1790 * Seed the random number generator.
1792 * better_srand() ensures the randomizer is seeded only once.
1794 * How random do you want it? See:
1795 * http://www.php.net/manual/en/function.srand.php
1796 * http://www.php.net/manual/en/function.mt-srand.php
1798 function better_srand($seed = '') {
1799 static $wascalled = FALSE;
1801 $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
1802 function_exists('mt_srand') ? mt_srand($seed) : srand($seed);
1804 //trigger_error("new random seed", E_USER_NOTICE); //debugging
1808 function rand_ascii($length = 1) {
1811 for ($i = 1; $i <= $length; $i++) {
1812 // return only typeable 7 bit ascii, avoid quotes
1813 if (function_exists('mt_rand'))
1814 $s .= chr(mt_rand(40, 126));
1816 // the usually bad glibc srand()
1817 $s .= chr(rand(40, 126));
1822 /* by Dan Frankowski.
1824 function rand_ascii_readable ($length = 6) {
1825 // Pick a few random letters or numbers
1828 // Don't use 1lI0O, because they're hard to read
1829 $letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
1830 $letter_len = strlen($letters);
1831 for ($i=0; $i < $length; $i++) {
1832 if (function_exists('mt_rand'))
1833 $word .= $letters[mt_rand(0, $letter_len-1)];
1835 $word .= $letters[rand(0, $letter_len-1)];
1841 * Recursively count all non-empty elements
1842 * in array of any dimension or mixed - i.e.
1843 * array('1' => 2, '2' => array('1' => 3, '2' => 4))
1844 * See http://www.php.net/manual/en/function.count.php
1846 function count_all($arg) {
1847 // skip if argument is empty
1849 //print_r($arg); //debugging
1851 // not an array, return 1 (base case)
1854 // else call recursively for all elements $arg
1855 foreach($arg as $key => $val)
1856 $count += count_all($val);
1861 function isSubPage($pagename) {
1862 return (strstr($pagename, SUBPAGE_SEPARATOR));
1865 function subPageSlice($pagename, $pos) {
1866 $pages = explode(SUBPAGE_SEPARATOR,$pagename);
1867 $pages = array_slice($pages,$pos,1);
1874 * Class for "popping up" and alert box. (Except that right now, it doesn't
1878 * This is a hackish and needs to be refactored. However it would be nice to
1879 * unify all the different methods we use for showing Alerts and Dialogs.
1880 * (E.g. "Page deleted", login form, ...)
1885 * @param object $request
1886 * @param mixed $head Header ("title") for alert box.
1887 * @param mixed $body The text in the alert box.
1888 * @param hash $buttons An array mapping button labels to URLs.
1889 * The default is a single "Okay" button pointing to $request->getURLtoSelf().
1891 function Alert($head, $body, $buttons=false) {
1892 if ($buttons === false)
1895 if (is_array($body)) {
1897 foreach ($body as $li) {
1898 $html->pushContent(HTML::li($li));
1902 $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1903 $this->_buttons = $buttons;
1907 * Show the alert box.
1912 $tokens = $this->_tokens;
1913 $tokens['BUTTONS'] = $this->_getButtons();
1915 $request->discardOutput();
1916 $tmpl = new Template('dialog', $request, $tokens);
1922 function _getButtons() {
1925 $buttons = $this->_buttons;
1927 $buttons = array(_("Okay") => $request->getURLtoSelf());
1930 foreach ($buttons as $label => $url)
1931 print "$label $url\n";
1932 $out[] = $WikiTheme->makeButton($label, $url, 'wikiaction');
1933 return new XmlContent($out);
1938 // 1.3.9-p1 => 1030.091
1939 // 1.3.10pre => 1030.099
1940 // 1.3.11pre-20041120 => 1030.1120041120
1941 // 1.3.12-rc1 => 1030.119
1942 function phpwiki_version() {
1943 static $PHPWIKI_VERSION;
1944 if (!isset($PHPWIKI_VERSION)) {
1945 $arr = explode('.',preg_replace('/\D+$/','', PHPWIKI_VERSION)); // remove the pre
1946 $arr[2] = preg_replace('/\.+/','.',preg_replace('/\D/','.',$arr[2]));
1947 $PHPWIKI_VERSION = $arr[0]*1000 + $arr[1]*10 + 0.01*$arr[2];
1948 if (strstr(PHPWIKI_VERSION, 'pre') or strstr(PHPWIKI_VERSION, 'rc'))
1949 $PHPWIKI_VERSION -= 0.01;
1951 return $PHPWIKI_VERSION;
1954 function phpwiki_gzhandler($ob) {
1955 if (function_exists('gzencode'))
1956 $ob = gzencode($ob);
1957 $GLOBALS['request']->_ob_get_length = strlen($ob);
1958 if (!headers_sent()) {
1959 header(sprintf("Content-Length: %d", $GLOBALS['request']->_ob_get_length));
1964 function isWikiWord($word) {
1965 global $WikiNameRegexp;
1966 //or preg_match('/\A' . $WikiNameRegexp . '\z/', $word) ??
1967 return preg_match("/^$WikiNameRegexp\$/",$word);
1970 // needed to store serialized objects-values only (perm, pref)
1971 function obj2hash ($obj, $exclude = false, $fields = false) {
1973 if (! $fields ) $fields = get_object_vars($obj);
1974 foreach ($fields as $key => $val) {
1975 if (is_array($exclude)) {
1976 if (in_array($key, $exclude)) continue;
1984 * isAsciiString($string)
1986 function isAsciiString($s) {
1987 $ptrASCII = '[\x00-\x7F]';
1988 return preg_match("/^($ptrASCII)*$/s", $s);
1992 * isUtf8String($string) - cheap utf-8 detection
1994 * segfaults for strings longer than 10kb!
1995 * Use http://www.phpdiscuss.com/article.php?id=565&group=php.i18n or
1996 * checkTitleEncoding() at http://cvs.sourceforge.net/viewcvs.py/wikipedia/phase3/languages/Language.php
1998 function isUtf8String( $s ) {
1999 $ptrASCII = '[\x00-\x7F]';
2000 $ptr2Octet = '[\xC2-\xDF][\x80-\xBF]';
2001 $ptr3Octet = '[\xE0-\xEF][\x80-\xBF]{2}';
2002 $ptr4Octet = '[\xF0-\xF4][\x80-\xBF]{3}';
2003 $ptr5Octet = '[\xF8-\xFB][\x80-\xBF]{4}';
2004 $ptr6Octet = '[\xFC-\xFD][\x80-\xBF]{5}';
2005 return preg_match("/^($ptrASCII|$ptr2Octet|$ptr3Octet|$ptr4Octet|$ptr5Octet|$ptr6Octet)*$/s", $s);
2009 * Check for UTF-8 URLs; Internet Explorer produces these if you
2010 * type non-ASCII chars in the URL bar or follow unescaped links.
2011 * Requires urldecoded pagename.
2012 * Fixes sf.net bug #953949
2014 * src: languages/Language.php:checkTitleEncoding() from mediawiki
2016 function fixTitleEncoding( $s ) {
2021 if (empty($s)) return $s;
2023 $ishigh = preg_match( '/[\x80-\xff]/', $s);
2025 $isutf = ($ishigh ? preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2026 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s ) : true );
2028 $isutf = ($ishigh ? isUtf8String($s) : true);
2029 $locharset = strtolower($charset);
2031 if( $locharset != "utf-8" and $ishigh and $isutf )
2032 $s = charset_convert('UTF-8', $locharset, $s);
2033 if ($locharset == "utf-8" and $ishigh and !$isutf )
2034 return utf8_encode( $s );
2036 // Other languages can safely leave this function, or replace
2037 // it with one to detect and convert another legacy encoding.
2042 * MySQL fulltext index doesn't grok utf-8, so we
2043 * need to fold cases and convert to hex.
2044 * src: languages/Language.php:stripForSearch() from mediawiki
2047 function stripForSearch( $string ) {
2048 global $wikiLowerChars;
2049 // '/(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])/' => "a-z\xdf-\xf6\xf8-\xff"
2050 return preg_replace(
2051 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
2052 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
2058 * Workaround for allow_url_fopen, to get the content of an external URI.
2059 * It returns the contents in one slurp. Parsers might want to check for allow_url_fopen
2060 * and use fopen, fread chunkwise. (see lib/XmlParser.php)
2062 function url_get_contents( $uri ) {
2063 if (get_cfg_var('allow_url_fopen')) { // was ini_get('allow_url_fopen'))
2064 return @file_get_contents($uri);
2066 require_once("lib/HttpClient.php");
2067 $bits = parse_url($uri);
2068 $host = $bits['host'];
2069 $port = isset($bits['port']) ? $bits['port'] : 80;
2070 $path = isset($bits['path']) ? $bits['path'] : '/';
2071 if (isset($bits['query'])) {
2072 $path .= '?'.$bits['query'];
2074 $client = new HttpClient($host, $port);
2075 $client->use_gzip = false;
2076 if (!$client->get($path)) {
2079 return $client->getContent();
2085 * Generate consecutively named strings:
2086 * Name, Name2, Name3, ...
2088 function GenerateId($name) {
2089 static $ids = array();
2090 if (empty($ids[$name])) {
2095 return $name . $ids[$name];
2099 // from IncludePage. To be of general use.
2100 // content: string or array of strings
2101 function firstNWordsOfContent( $n, $content ) {
2102 if ($content and $n > 0) {
2103 if (is_array($content)) {
2104 // fixme: return a list of lines then?
2105 //$content = join("\n", $content);
2106 //$return_array = true;
2108 foreach ($content as $line) {
2109 $words = explode(' ', $line);
2110 if ($wordcount + count($words) > $n) {
2111 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
2112 . sprintf(_("... (first %s words)"), $n);
2115 $wordcount += count($words);
2121 // fixme: use better whitespace/word seperators
2122 $words = explode(' ', $content);
2123 if (count($words) > $n) {
2124 return join(' ', array_slice($words, 0, $n))
2125 . sprintf(_("... (first %s words)"), $n);
2135 // moved from lib/plugin/IncludePage.php
2136 function extractSection ($section, $content, $page, $quiet = false, $sectionhead = false) {
2137 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
2139 if (preg_match("/ ^(!{1,}|={2,})\\s*$qsection" // section header
2140 . " \\s*$\\n?" // possible blank lines
2141 . " ( (?: ^.*\\n? )*? )" // some lines
2142 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
2143 implode("\n", $content),
2145 // Strip trailing blanks lines and ---- <hr>s
2146 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
2148 $text = $match[1] . $section ."\n". $text;
2149 return explode("\n", $text);
2152 $mesg = $page ." ". $section;
2155 return array(sprintf(_("<%s: no such section>"), $mesg));
2158 // Extract the first $sections sections of the page
2159 function extractSections ($sections, $content, $page, $quiet = false, $sectionhead = false) {
2161 $mycontent = $content;
2164 while ($sections > 0) {
2166 if (preg_match("/ ^(!{1,})\\s*(.*)\\n" // section header
2167 . " \\s*$\\n?" // possible blank lines
2168 . " ( (?: ^.*\\n? )*? )" // some lines
2169 . " ( ^\\1 (.|\\n)* | \\Z)/xm", // sec header (same or higher level) (or EOF)
2170 implode("\n", $mycontent),
2172 $section = $match[2];
2173 // Strip trailing blanks lines and ---- <hr>s
2174 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[3]);
2176 $text = $match[1] . $section ."\n". $text;
2179 $mycontent = explode("\n", $match[4]);
2181 if ($sections === 0) {
2182 return explode("\n", $result);
2188 // use this faster version: only load ExternalReferrer if we came from an external referrer
2189 function isExternalReferrer(&$request) {
2190 if ($referrer = $request->get('HTTP_REFERER')) {
2191 $home = SERVER_URL; // SERVER_URL or SCRIPT_NAME, if we want to check sister wiki's also
2192 if (string_starts_with(strtolower($referrer), strtolower($home))) return false;
2193 require_once("lib/ExternalReferrer.php");
2194 $se = new SearchEngines();
2195 return $se->parseSearchQuery($referrer);
2197 //if (DEBUG) return array('query' => 'wiki');
2202 * Useful for PECL overrides: cvsclient, ldap, soap, xmlrpc, pdo, pdo_<driver>
2204 function loadPhpExtension($extension) {
2205 if (!extension_loaded($extension)) {
2206 $isWindows = (substr(PHP_OS,0,3) == 'WIN');
2207 $soname = ($isWindows ? 'php_' : '')
2209 . ($isWindows ? '.dll' : '.so');
2213 return extension_loaded($extension);
2216 function charset_convert($from, $to, $data) {
2218 //$wikicharset = strtolower($CHARSET);
2219 //$systemcharset = strtolower(get_cfg_var('iconv.internal_encoding')); // 'iso-8859-1';
2220 if (strtolower($from) == 'utf-8' and strtolower($to) == 'iso-8859-1')
2221 return utf8_decode($data);
2222 if (strtolower($to) == 'utf-8' and strtolower($from) == 'iso-8859-1')
2223 return utf8_encode($data);
2225 if (loadPhpExtension("iconv")) {
2226 $tmpdata = iconv($from, $to, $data);
2228 trigger_error("charset conversion $from => $to failed. Wrong source charset?", E_USER_WARNING);
2232 trigger_error("The iconv extension cannot be loaded", E_USER_WARNING);
2237 function string_starts_with($string, $prefix) {
2238 return (substr($string, 0, strlen($prefix)) == $prefix);
2240 function string_ends_with($string, $suffix) {
2241 return (substr($string, -strlen($suffix)) == $suffix);
2243 function array_remove($arr,$value) {
2244 return array_values(array_diff($arr,array($value)));
2248 * Ensure that the script will have another $secs time left.
2249 * Works only if safe_mode is off.
2250 * For example not to timeout on waiting socket connections.
2251 * Use the socket timeout as arg.
2253 function longer_timeout($secs = 30) {
2254 $timeout = @ini_get("max_execution_time") ? ini_get("max_execution_time") : 30;
2255 $timeleft = $timeout - $GLOBALS['RUNTIMER']->getTime();
2256 if ($timeleft < $secs)
2257 @set_time_limit(max($timeout,(integer)($secs + $timeleft)));
2260 function printSimpleTrace($bt) {
2262 echo "\nTraceback:\n";
2263 if (function_exists('debug_print_backtrace')) { // >= 5
2264 debug_print_backtrace();
2266 foreach ($bt as $i => $elem) {
2267 if (!array_key_exists('file', $elem)) {
2270 //echo join(" ",array_values($elem)),"\n";
2271 echo " ",$elem['file'],':',$elem['line']," ",$elem['function'],"\n";
2277 * Return the used process memory, in bytes.
2278 * Enable the section which will work for you. They are very slow.
2279 * Special quirks for Windows: Requires cygwin.
2281 function getMemoryUsage() {
2282 //if (!(DEBUG & _DEBUG_VERBOSE)) return;
2283 if (function_exists('memory_get_usage') and memory_get_usage()) {
2284 return memory_get_usage();
2285 } elseif (function_exists('getrusage') and ($u = @getrusage()) and !empty($u['ru_maxrss'])) {
2286 $mem = $u['ru_maxrss'];
2287 } elseif (substr(PHP_OS,0,3) == 'WIN') { // may require a newer cygwin
2288 // what we want is the process memory only: apache or php (if CGI)
2291 // win32_ps_stat_proc, win32_ps_stat_mem
2292 if (function_exists('win32_ps_list_procs')) {
2293 $info = win32_ps_stat_proc($pid);
2294 $memstr = $info['mem']['working_set_size'];
2296 // This works only if it's a cygwin process (apache or php).
2297 // Requires a newer cygwin
2298 $memstr = exec("cat /proc/$pid/statm |cut -f1");
2300 // if it's native windows use something like this:
2301 // (requires pslist from sysinternals.com, grep, sed and perl)
2302 //$memstr = exec("pslist $pid|grep -A1 Mem|sed 1d|perl -ane\"print \$"."F[5]\"");
2304 return (integer) trim($memstr);
2307 //%MEM: Percentage of total memory in use by this process
2308 //VSZ: Total virtual memory size, in 1K blocks.
2309 //RSS: Real Set Size, the actual amount of physical memory allocated to this process.
2310 //CPU time used by process since it started.
2311 //echo "%",`ps -o%mem,vsz,rss,time -p $pid|sed 1d`,"\n";
2312 $memstr = exec("ps -orss -p $pid|sed 1d");
2313 return (integer) trim($memstr);
2318 * @param var $needle
2319 * @param array $haystack one-dimensional numeric array only, no hash
2321 * @desc Feed a sorted array to $haystack and a value to search for to $needle.
2322 It will return false if not found or the index where it was found.
2323 From dennis.decoene@moveit.be http://www.php.net/array_search
2325 function binary_search($needle, $haystack) {
2326 $high = count($haystack);
2329 while (($high - $low) > 1) {
2330 $probe = floor(($high + $low) / 2);
2331 if ($haystack[$probe] < $needle) {
2333 } elseif ($haystack[$probe] == $needle) {
2334 $high = $low = $probe;
2340 if ($high == count($haystack) || $haystack[$high] != $needle) {
2347 function is_localhost($url = false) {
2349 global $HTTP_SERVER_VARS;
2350 return $HTTP_SERVER_VARS['SERVER_ADDR'] == '127.0.0.1';
2355 * Take a string and quote it sufficiently to be passed as a Javascript
2356 * string between ''s
2358 function javascript_quote_string($s) {
2359 return str_replace("'", "\'", $s);
2362 function isSerialized($s) {
2363 return (!empty($s) and (strlen($s) > 3) and (substr($s,1,1) == ':'));
2367 * Take a string and return an array of pairs (attribute name, attribute value)
2369 * We allow attributes with or without double quotes (")
2370 * Attribute-value pairs may be separated by space or comma
2371 * Space is normal HTML attributes, comma is for RichTable compatibility
2372 * border=1, cellpadding="5"
2373 * border=1 cellpadding="5"
2374 * style="font-family: sans-serif; border-top:1px solid #dddddd;"
2375 * style="font-family: Verdana, Arial, Helvetica, sans-serif"
2377 function parse_attributes($line) {
2381 if (empty($line)) return $options;
2382 $line = trim($line);
2383 if (empty($line)) return $options;
2384 $line = trim($line, ",");
2385 if (empty($line)) return $options;
2387 // First we have an attribute name.
2392 while (($i < strlen($line)) && ($line[$i] != '=')) {
2395 $attribute = substr($line, 0, $i);
2396 $attribute = strtolower($attribute);
2398 $line = substr($line, $i+1);
2399 $line = trim ($line);
2400 $line = trim ($line, "=");
2401 $line = trim ($line);
2403 if (empty($line)) return $options;
2405 // Then we have the attribute value.
2408 // Attribute value might be between double quotes
2409 // In that case we have to find the closing double quote
2410 if ($line[0] == '"') {
2411 $i++; // skip first '"'
2412 while (($i < strlen($line)) && ($line[$i] != '"')) {
2415 $value = substr($line, 0, $i);
2416 $value = trim ($value, '"');
2417 $value = trim ($value);
2419 // If there are no double quotes, we have to find the next space or comma
2421 while (($i < strlen($line)) && (($line[$i] != ' ') && ($line[$i] != ','))) {
2424 $value = substr($line, 0, $i);
2425 $value = trim ($value);
2426 $value = trim ($value, ",");
2427 $value = trim ($value);
2430 $options[$attribute] = $value;
2432 $line = substr($line, $i+1);
2433 $line = trim ($line);
2434 $line = trim ($line, ",");
2435 $line = trim ($line);
2437 return $options + parse_attributes($line);
2441 * Returns true if the filename ends with an image suffix.
2442 * Uses INLINE_IMAGES if defined, else "png|jpg|jpeg|gif"
2444 function is_image ($filename) {
2446 if (defined('INLINE_IMAGES')) {
2447 $inline_images = INLINE_IMAGES;
2449 $inline_images = "png|jpg|jpeg|gif";
2452 foreach (explode("|", $inline_images) as $suffix) {
2453 if (string_ends_with(strtolower($filename), "." . $suffix)) {
2461 * Returns true if the filename ends with an video suffix.
2462 * Currently only FLV
2464 function is_video ($filename) {
2466 return string_ends_with(strtolower($filename), ".flv");
2471 * Compute cell in spreadsheet table
2472 * $table: two-dimensional table
2473 * $i and $j: indexes of cell to compute
2474 * $imax and $jmax: table dimensions
2476 function compute_tablecell ($table, $i, $j, $imax, $jmax) {
2478 // What is implemented:
2479 // @@=SUM(R)@@ : sum of cells in current row
2480 // @@=SUM(C)@@ : sum of cells in current column
2481 // @@=AVERAGE(R)@@ : average of cells in current row
2482 // @@=AVERAGE(C)@@ : average of cells in current column
2483 // @@=MAX(R)@@ : maximum value of cells in current row
2484 // @@=MAX(C)@@ : maximum value of cells in current column
2485 // @@=MIN(R)@@ : minimum value of cells in current row
2486 // @@=MIN(C)@@ : minimum value of cells in current column
2487 // @@=COUNT(R)@@ : number of cells in current row
2488 // (numeric or not, excluding headers and current cell)
2489 // @@=COUNT(C)@@ : number of cells in current column
2490 // (numeric or not, excluding headers and current cell)
2496 if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
2497 for ($index=0; $index<$imax; $index++) {
2498 if (is_numeric($table[$index][$j])) {
2499 $result += $table[$index][$j];
2502 return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
2504 } else if (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
2505 for ($index=0; $index<$jmax; $index++) {
2506 if (is_numeric($table[$i][$index])) {
2507 $result += $table[$i][$index];
2510 return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
2512 } else if (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
2513 for ($index=0; $index<$imax; $index++) {
2514 if (is_numeric($table[$index][$j])) {
2515 $result += $table[$index][$j];
2519 $result=$result/$counter;
2520 return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
2522 } else if (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
2523 for ($index=0; $index<$jmax; $index++) {
2524 if (is_numeric($table[$i][$index])) {
2525 $result += $table[$i][$index];
2529 $result=$result/$counter;
2530 return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
2532 } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
2533 for ($index=0; $index<$imax; $index++) {
2534 if (is_numeric($table[$index][$j])) {
2537 $result=$table[$index][$j];
2539 $result = max($result, $table[$index][$j]);
2546 return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
2548 } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
2549 for ($index=0; $index<$jmax; $index++) {
2550 if (is_numeric($table[$i][$index])) {
2553 $result=$table[$i][$index];
2555 $result = max($result, $table[$i][$index]);
2562 return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
2564 } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
2565 for ($index=0; $index<$imax; $index++) {
2566 if (is_numeric($table[$index][$j])) {
2569 $result=$table[$index][$j];
2571 $result = min($result, $table[$index][$j]);
2578 return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
2580 } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
2581 for ($index=0; $index<$jmax; $index++) {
2582 if (is_numeric($table[$i][$index])) {
2585 $result=$table[$i][$index];
2587 $result = min($result, $table[$i][$index]);
2594 return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
2596 } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
2597 for ($index=0; $index<$imax; $index++) {
2598 if (!string_starts_with(trim($table[$index][$j]), "=")) { // exclude header
2602 $result = $counter-1; // exclude self
2603 return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
2605 } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
2606 for ($index=0; $index<$jmax; $index++) {
2607 if (!string_starts_with(trim($table[$i][$index]), "=")) { // exclude header
2611 $result = $counter-1; // exclude self
2612 return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
2615 return $table[$i][$j];
2619 * Remove accents from given text.
2621 function strip_accents($text) {
2622 $res = utf8_decode($text);
2624 utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
2625 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
2626 return utf8_encode($res);
2629 // (c-file-style: "gnu")
2633 // c-basic-offset: 4
2634 // c-hanging-comment-ender-p: nil
2635 // indent-tabs-mode: nil