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)
76 isActionPage ($filename)
80 obj2hash ($obj, $exclude = false, $fields = false)
83 url_get_contents ($uri)
85 firstNWordsOfContent ($n, $content)
86 extractSection ($section, $content, $page, $quiet = false, $sectionhead = false)
89 charset_convert($from, $to, $data)
90 string_starts_with($string, $prefix)
91 string_ends_with($string, $suffix)
92 array_remove($arr,$value)
93 longer_timeout($secs=30)
96 binary_search($needle, $haystack)
98 javascript_quote_string($s)
100 parse_attributes($line)
104 function: linkExistingWikiWord($wikiword, $linktext, $version)
105 moved to: lib/WikiTheme.php
107 if (defined('_PHPWIKI_STDLIB_LOADED')) return;
108 else define('_PHPWIKI_STDLIB_LOADED', true);
110 define('MAX_PAGENAME_LENGTH', 100);
113 * Convert string to a valid XML identifier.
115 * XML 1.0 identifiers are of the form: [A-Za-z][A-Za-z0-9:_.-]*
117 * We would like to have, e.g. named anchors within wiki pages
118 * names like "Table of Contents" --- clearly not a valid XML
119 * fragment identifier.
121 * This function implements a one-to-one map from {any string}
122 * to {valid XML identifiers}.
125 * converting all bytes not in [A-Za-z0-9:_-],
126 * and any leading byte not in [A-Za-z] to 'xbb.',
127 * where 'bb' is the hexadecimal representation of the
130 * As a special case, the empty string is converted to 'empty.'
135 function MangleXmlIdentifier($str) {
139 return preg_replace('/[^-_:A-Za-z0-9]|(?<=^)[^A-Za-z]/e',
140 "'x' . sprintf('%02x', ord('\\0')) . '.'",
144 function UnMangleXmlIdentifier($str) {
145 if ($str == 'empty.')
147 return preg_replace('/x(\w\w)\./e',
148 "sprintf('%c', hex('\\0'))",
153 * Generates a valid URL for a given Wiki pagename.
154 * @param mixed $pagename If a string this will be the name of the Wiki page to link to.
155 * If a WikiDB_Page object function will extract the name to link to.
156 * If a WikiDB_PageRevision object function will extract the name to link to.
158 * @param boolean $get_abs_url Default value is false.
159 * @return string The absolute URL to the page passed as $pagename.
161 function WikiURL($pagename, $args = '', $get_abs_url = false) {
162 global $request, $WikiTheme;
165 if (is_object($pagename)) {
166 if (isa($pagename, 'WikiDB_Page')) {
167 $pagename = $pagename->getName();
169 elseif (isa($pagename, 'WikiDB_PageRevision')) {
170 $page = $pagename->getPage();
171 $args['version'] = $pagename->getVersion();
172 $pagename = $page->getName();
174 elseif (isa($pagename, 'WikiPageName')) {
175 $anchor = $pagename->anchor;
176 $pagename = $pagename->name;
178 $anchor = $pagename->anchor;
179 $pagename = $pagename->name;
182 if (!$get_abs_url and DEBUG and $request->getArg('start_debug')) {
184 $args = 'start_debug=' . $request->getArg('start_debug');
185 elseif (is_array($args))
186 $args['start_debug'] = $request->getArg('start_debug');
188 $args .= '&start_debug=' . $request->getArg('start_debug');
190 if (is_array($args)) {
192 foreach ($args as $key => $val) {
193 // avoid default args
194 if (USE_PATH_INFO and $key == 'pagename')
196 elseif ($key == 'action' and $val == 'browse')
198 elseif (!is_array($val)) // ugly hack for getURLtoSelf() which also takes POST vars
199 $enc_args[] = urlencode($key) . '=' . urlencode($val);
201 $args = join('&', $enc_args);
204 if (USE_PATH_INFO or !empty($WikiTheme->HTML_DUMP_SUFFIX)) {
205 $url = $get_abs_url ? (SERVER_URL . VIRTUAL_PATH . "/") : "";
206 $base = preg_replace('/%2f/i', '/', rawurlencode($pagename));
208 if (!empty($WikiTheme->HTML_DUMP_SUFFIX)) {
209 if (!empty($WikiTheme->VALID_LINKS) and $request->getArg('action') == 'pdf') {
210 if (!in_array($pagename, $WikiTheme->VALID_LINKS))
213 $url = $base . $WikiTheme->HTML_DUMP_SUFFIX;
215 $url .= $WikiTheme->HTML_DUMP_SUFFIX;
225 $url = $get_abs_url ? SERVER_URL . SCRIPT_NAME : basename(SCRIPT_NAME);
226 $url .= "?pagename=" . rawurlencode($pagename);
231 $url .= "#" . MangleXmlIdentifier($anchor);
235 /** Convert relative URL to absolute URL.
237 * This converts a relative URL to one of PhpWiki's support files
238 * to an absolute one.
241 * @return string Absolute URL
243 function AbsoluteURL ($url) {
244 if (preg_match('/^https?:/', $url))
246 if ($url[0] != '/') {
247 $base = USE_PATH_INFO ? VIRTUAL_PATH : dirname(SCRIPT_NAME);
248 while ($base != '/' and substr($url, 0, 3) == "../") {
249 $url = substr($url, 3);
250 $base = dirname($base);
256 return SERVER_URL . $url;
259 function DataURL ($url) {
260 if (preg_match('/^https?:/', $url))
262 $url = NormalizeWebFileName($url);
263 if (DEBUG and $GLOBALS['request']->getArg('start_debug') and substr($url,-4,4) == '.php')
264 $url .= "?start_debug=1"; // XMLRPC and SOAP debugging helper.
265 return AbsoluteURL($url);
269 * Generates icon in front of links.
271 * @param string $protocol_or_url URL or protocol to determine which icon to use.
273 * @return HtmlElement HtmlElement object that contains data to create img link to
274 * icon for use with url or protocol passed to the function. False if no img to be
277 function IconForLink($protocol_or_url) {
279 if (0 and $filename_suffix == false) {
280 // display apache style icon for file type instead of protocol icon
281 // - archive: unix:gz,bz2,tgz,tar,z; mac:dmg,dmgz,bin,img,cpt,sit; pc:zip;
282 // - document: html, htm, text, txt, rtf, pdf, doc
283 // - non-inlined image: jpg,jpeg,png,gif,tiff,tif,swf,pict,psd,eps,ps
284 // - audio: mp3,mp2,aiff,aif,au
285 // - multimedia: mpeg,mpg,mov,qt
287 list ($proto) = explode(':', $protocol_or_url, 2);
288 $src = $WikiTheme->getLinkIconURL($proto);
290 return HTML::img(array('src' => $src, 'alt' => "", 'class' => 'linkicon'));
297 * Glue icon in front of or after text.
298 * Pref: 'noLinkIcons' - ignore icon if set
299 * WikiTheme: 'LinkIcons' - 'yes' at front
300 * - 'no' display no icon
301 * - 'front' display at left
302 * - 'after' display at right
304 * @param string $protocol_or_url Protocol or URL. Used to determine the
306 * @param string $text The text.
307 * @return XmlContent.
309 function PossiblyGlueIconToText($proto_or_url, $text) {
310 global $request, $WikiTheme;
311 if ($request->getPref('noLinkIcons'))
313 $icon = IconForLink($proto_or_url);
316 if ($where = $WikiTheme->getLinkIconAttr()) {
317 if ($where == 'no') return $text;
318 if ($where != 'after') $where = 'front';
322 if ($where == 'after') {
323 // span the icon only to the last word (tie them together),
324 // to let the previous words wrap on line breaks.
325 if (!is_object($text)) {
326 preg_match('/^(\s*\S*)(\s*)$/', $text, $m);
327 list (, $prefix, $last_word) = $m;
333 $text = HTML::span(array('style' => 'white-space: nowrap'),
334 $last_word, HTML::Raw(' '), $icon);
336 $text = HTML($prefix, $text);
339 // span the icon only to the first word (tie them together),
340 // to let the next words wrap on line breaks
341 if (!is_object($text)) {
342 preg_match('/^\s*(\S*)(.*?)\s*$/', $text, $m);
343 list (, $first_word, $tail) = $m;
349 $text = HTML::span(array('style' => 'white-space: nowrap'),
352 $text = HTML($text, $tail);
357 * Determines if the url passed to function is safe, by detecting if the characters
358 * '<', '>', or '"' are present.
359 * Check against their urlencoded values also.
361 * @param string $url URL to check for unsafe characters.
362 * @return boolean True if same, false else.
364 function IsSafeURL($url) {
365 return !preg_match('/([<>"])|(%3C)|(%3E)|(%22)/', $url);
369 * Generates an HtmlElement object to store data for a link.
371 * @param string $url URL that the link will point to.
372 * @param string $linktext Text to be displayed as link.
373 * @return HtmlElement HtmlElement object that contains data to construct an html link.
375 function LinkURL($url, $linktext = '') {
376 // FIXME: Is this needed (or sufficient?)
377 if(! IsSafeURL($url)) {
378 $link = HTML::span(array('class' => 'error'), _("BAD URL -- remove all of <, >, \""));
383 $linktext = preg_replace("/mailto:/A", "", $url);
384 $args = array('href' => $url);
385 if ( defined('EXTERNAL_LINK_TARGET') ) // can also be set in the css
386 $args['target'] = (is_string(EXTERNAL_LINK_TARGET) and (EXTERNAL_LINK_TARGET != "")) ? EXTERNAL_LINK_TARGET : "_blank";
387 $link = HTML::a($args, PossiblyGlueIconToText($url, $linktext));
389 $link->setAttr('class', $linktext ? 'namedurl' : 'rawurl');
396 * Syntax: [image.png size=50% border=n align= hspace= vspace= width= height=]
397 * Disallows sizes which are too small.
398 * Spammers may use such (typically invisible) image attributes to raise their GoogleRank.
400 * Handle embeddable objects, like svg, class, vrml, swf, svgz, pdf, avi, wmv especially.
402 function LinkImage($url, $alt = "") {
403 $force_img = "png|jpg|gif|jpeg|bmp|pl|cgi";
404 // Disallow tags in img src urls. Typical CSS attacks.
405 // FIXME: Is this needed (or sufficient?)
406 // FIXED: This was broken for moniker:TP30 test/image.png => url="moniker:TP30" attr="test/image.png"
408 // support new syntax: [prefix/image.jpg size=50% border=n]
409 if (empty($alt)) $alt = "";
412 $arr = explode(' ',$url);
413 if (!empty($arr)) $url = $arr[0];
414 if (! IsSafeURL($url)) {
415 $link = HTML::span(array('class' => 'error'), _("BAD URL -- remove all of <, >, \""));
418 // spaces in inline images must be %20 encoded!
419 $link = HTML::img(array('src' => $url));
421 // Extract attributes
422 $arr = parse_attributes(strstr($ori_url, " "));
423 foreach ($arr as $attr => $value) {
424 // These attributes take strings: lang, id, title, alt
425 if (($attr == "lang")
427 || ($attr == "title")
428 || ($attr == "alt")) {
429 $link->setAttr($attr, $value);
431 // align = bottom|middle|top|left|right
432 elseif (($attr == "align")
433 && (($value == "bottom")
434 || ($value == "middle")
436 || ($value == "left")
437 || ($value == "right"))) {
438 $link->setAttr($attr, $value);
440 // These attributes take a number (pixels): border, hspace, vspace
441 elseif ((($attr == "border") || ($attr == "hspace") || ($attr == "vspace"))
442 && (is_numeric($value))) {
443 $link->setAttr($attr, (int)$value);
445 // These attributes take a number (pixels) or a percentage: height, width
446 elseif ((($attr == "height") || ($attr == "width"))
447 && (preg_match('/\d+[%p]?x?/', $value))) {
448 $link->setAttr($attr, $value);
450 // We allow size=50% and size=20x30
451 // We replace this with "width" and "height" HTML attributes
452 elseif ($attr == "size") {
453 if (preg_match('/(\d+%)/', $value, $m)) {
454 $link->setAttr('width',$m[1]);
455 $link->setAttr('height',$m[1]);
456 } elseif (preg_match('/(\d+)x(\d+)/', $value, $m)) {
457 $link->setAttr('width',$m[1]);
458 $link->setAttr('height',$m[2]);
462 $link = HTML::span(array('class' => 'error'),
463 sprintf(_("Invalid image attribute \"%s\" %s=%s"),
464 $url, $attr, $value));
468 // Correct silently the most common error
469 if ($url != $ori_url and empty($arr) and !preg_match("/^http/",$url)) {
470 // space belongs to the path
471 $file = NormalizeLocalFileName($ori_url);
472 if (file_exists($file)) {
473 $link = HTML::img(array('src' => $ori_url));
475 sprintf(_("Invalid image link fixed %s => %s. Spaces must be quoted with %%20."),
476 $url, $ori_url), E_USER_WARNING);
477 } elseif (string_starts_with($ori_url, getUploadDataPath())) {
478 $file = substr($file, strlen(getUploadDataPath()));
479 $path = getUploadFilePath().$file;
480 if (file_exists($path)) {
481 trigger_error(sprintf(_("Invalid image link fixed \"%s\" => \"%s\".\n Spaces must be quoted with %%20."),
482 $url, $ori_url), E_USER_WARNING);
483 $link->setAttr('src', getUploadDataPath() . $file);
488 if (!$link->getAttr('alt')) {
489 $link->setAttr('alt', $alt);
491 // Check width and height as spam countermeasure
492 if (($width = $link->getAttr('width')) and ($height = $link->getAttr('height'))) {
493 //$width = (int) $width; // px or % or other suffix
494 //$height = (int) $height;
495 if (($width < 3 and $height < 10) or
496 ($height < 3 and $width < 20) or
497 ($height < 7 and $width < 7))
499 $link = HTML::span(array('class' => 'error'),
500 _("Invalid image size"));
505 // Prepare for getimagesize($url)
506 // $url only valid for external urls, otherwise local path
507 if (DISABLE_GETIMAGESIZE)
509 elseif (! preg_match("/\.$force_img$/i", $url))
510 ; // only valid image extensions or scripts assumed to generate images
511 elseif (preg_match("/^http/",$url)) { // external url
512 $size = @getimagesize($url);
513 } else { // local file
514 if (file_exists($file = NormalizeLocalFileName($url))) { // here
515 $size = @getimagesize($file);
516 } elseif (file_exists(NormalizeLocalFileName(urldecode($url)))) {
517 $size = @getimagesize($file);
518 $link->setAttr('src', rawurldecode($url));
519 } elseif (string_starts_with($url, getUploadDataPath())) { // there
520 $file = substr($file, strlen(getUploadDataPath()));
521 $path = getUploadFilePath().rawurldecode($file);
522 $size = @getimagesize($path);
523 $link->setAttr('src', getUploadDataPath() . rawurldecode($file));
524 } else { // elsewhere
526 $size = @getimagesize($request->get('DOCUMENT_ROOT').urldecode($url));
532 if (($width < 3 and $height < 10)
533 or ($height < 3 and $width < 20)
534 or ($height < 7 and $width < 7))
536 $link = HTML::span(array('class' => 'error'),
537 _("Invalid image size"));
542 $link->setAttr('class', 'inlineimage');
544 /* Check for inlined objects. Everything allowed in INLINE_IMAGES besides
545 * png|jpg|gif|jpeg|bmp|pl|cgi. If no image it is an object to embed.
546 * Note: Allow cgi's (pl,cgi) returning images.
548 if (!preg_match("/\.(".$force_img.")/i", $url)) {
549 // HTML::img(array('src' => $url, 'alt' => $alt, 'title' => $alt));
550 // => HTML::object(array('src' => $url)) ...;
551 return ImgObject($link, $ori_url);
557 * <object> / <embed> tags instead of <img> for all non-image extensions
559 * Called by LinkImage(), not directly.
560 * Syntax: [image.svg size=50% alt=image.gif border=n align= hspace= vspace= width= height=]
561 * Samples: [Upload:song.mp3 type=audio/mpeg width=200 height=10]
562 * $alt may be an alternate img
563 * TODO: Need to unify with WikiPluginCached::embedObject()
565 * Note that Safari 1.0 will crash with <object>, so use only <embed>
566 * http://www.alleged.org.uk/pdc/2002/svg-object.html
568 * Allowed object tags:
569 * DATA=URI (object data)
570 * CLASSID=URI (location of implementation)
571 * ARCHIVE=CDATA (archive files)
572 * CODEBASE=URI (base URI for CLASSID, DATA, ARCHIVE)
573 * WIDTH=Length (object width)
574 * HEIGHT=Length (object height)
575 * NAME=CDATA (name for form submission)
576 * USEMAP=URI (client-side image map)
577 * TYPE=ContentType (content-type of object)
578 * CODETYPE=ContentType (content-type of code)
579 * STANDBY=Text (message to show while loading)
580 * TABINDEX=NUMBER (position in tabbing order)
581 * DECLARE (do not instantiate object)
582 * The rest is added as <param name="" value="" /> tags
584 function ImgObject($img, $url) {
585 // get the url args: data="sample.svgz" type="image/svg+xml" width="400" height="300"
586 $params = explode(",","data,classid,archive,codebase,name,usemap,type,".
587 "codetype,standby,tabindex,declare");
588 if (is_array($url)) {
591 foreach ($args as $attr => $value) {
592 foreach ($params as $param) {
593 if ($param == $attr) {
594 $img->setAttr($param, $value);
595 if (isset($found[$param])) $found[$param]++;
596 else $found[$param] = 1;
601 // now all remaining args are added as <param> to the object
603 foreach ($args as $attr => $value) {
604 if (!isset($found[$attr])) {
605 $params[] = HTML::param(array('name' => $attr,
609 $url = $img->getAttr('src');
610 $force_img = "png|jpg|gif|jpeg|bmp";
611 if (!preg_match("/\.(".$force_img.")/i", $url)) {
612 $img->setAttr('src', false);
615 $args = explode(' ', $url);
616 if (count($args) >= 1) {
617 $url = array_shift($args);
619 foreach ($args as $attr) {
620 foreach ($params as $param) {
621 if (preg_match("/^$param=(\S+)$/i",$attr,$m)) {
622 $img->setAttr($param, $m[1]);
623 if (isset($found[$param])) $found[$param]++;
624 else $found[$param] = 1;
629 // now all remaining args are added as <param> to the object
631 foreach ($args as $attr) {
632 if (!isset($found[$attr]) and preg_match("/^(\S+)=(\S+)$/i",$attr,$m)) {
633 $params[] = HTML::param(array('name' => $m[1],
639 $type = $img->getAttr('type');
641 if (function_exists('mime_content_type') && file_exists($url)) {
642 $type = mime_content_type($url);
645 $object = HTML::object(array_merge($img->_attr,
646 array('type' => $type)), //'src' => $url
648 $object->setAttr('class', 'inlineobject');
650 foreach ($params as $param) $object->pushContent($param);
652 if (isBrowserSafari() and !isBrowserSafari(532)) { // recent chrome can do OBJECT
653 return HTML::embed($object->_attr, $object->_content);
655 $object->pushContent(HTML::embed($object->_attr));
662 // var in php5 deprecated
664 $this->items = array();
667 function push($item) {
668 $this->items[$this->size] = $item;
674 if ($this->size == 0) {
675 return false; // stack is empty
678 return $this->items[$this->size];
687 return $this->items[$this->size - 1];
693 // end class definition
695 function SplitQueryArgs ($query_args = '')
697 // FIXME: use the arg-seperator which might not be &
698 $split_args = explode('&', $query_args);
700 while (list($key, $val) = each($split_args))
701 if (preg_match('/^ ([^=]+) =? (.*) /x', $val, $m))
702 $args[$m[1]] = $m[2];
706 function LinkPhpwikiURL($url, $text = '', $basepage = false) {
709 if (!preg_match('/^ phpwiki: ([^?]*) [?]? (.*) $/x', $url, $m)) {
710 return HTML::span(array('class' => 'error'), _("BAD phpwiki: URL"));
714 $pagename = urldecode($m[1]);
717 if (empty($pagename) &&
718 preg_match('/^(diff|edit|links|info)=([^&]+)$/', $qargs, $m)) {
719 // Convert old style links (to not break diff links in
721 $pagename = urldecode($m[2]);
722 $args = array("action" => $m[1]);
725 $args = SplitQueryArgs($qargs);
728 if (empty($pagename))
729 $pagename = $GLOBALS['request']->getArg('pagename');
731 if (isset($args['action']) && $args['action'] == 'browse')
732 unset($args['action']);
735 if (empty($args['action']))
737 else if (is_safe_action($args['action']))
738 $class = 'wikiaction';
740 if (empty($args['action']) || is_safe_action($args['action']))
741 $class = 'wikiaction';
743 // Don't allow administrative links on unlocked pages.
744 $dbi = $GLOBALS['request']->getDbh();
745 $page = $dbi->getPage($basepage ? $basepage : $pagename);
746 if (!$page->get('locked'))
747 return HTML::span(array('class' => 'wikiunsafe'),
748 HTML::u(_("Lock page to enable link")));
749 $class = 'wikiadmin';
753 $text = HTML::span(array('class' => 'rawurl'), $url);
755 $wikipage = new WikiPageName($pagename);
756 if (!$wikipage->isValid()) {
758 return $WikiTheme->linkBadWikiWord($wikipage, $url);
761 return HTML::a(array('href' => WikiURL($pagename, $args),
767 * A class to assist in parsing wiki pagenames.
769 * Now with subpages and anchors, parsing and passing around
770 * pagenames is more complicated. This should help.
774 /** Short name for page.
776 * This is the value of $name passed to the constructor.
777 * (For use, e.g. as a default label for links to the page.)
781 /** The full page name.
783 * This is the full name of the page (without anchor).
789 * This is the referenced anchor within the page, or the empty string.
795 * @param mixed $name Page name.
796 * WikiDB_Page, WikiDB_PageRevision, or string.
797 * This can be a relative subpage name (like '/SubPage'),
798 * or can be the empty string to refer to the $basename.
800 * @param string $anchor For links to anchors in page.
802 * @param mixed $basename Page name from which to interpret
803 * relative or other non-fully-specified page names.
805 function WikiPageName($name, $basename=false, $anchor=false) {
806 if (is_string($name)) {
807 $this->shortName = $name;
808 if (strstr($name, ':')) {
809 list($moniker, $shortName) = explode (":", $name, 2);
810 $map = getInterwikiMap(); // allow overrides to custom maps
811 if (isset($map->_map[$moniker])) {
812 $url = $map->_map[$moniker];
813 if (strstr($url, '%s'))
814 $url = sprintf($url, $shortName);
818 // expand Talk or User, but not to absolute urls!
819 if (strstr($url, '//')) {
820 if ($moniker == 'Talk')
821 $name = $name . SUBPAGE_SEPARATOR . _("Discussion");
822 elseif ($moniker == 'User')
827 if (strstr($shortName, '?')) {
828 list($shortName, $dummy) = explode("\?", $shortName, 2);
830 $this->shortName = $shortName;
833 // FIXME: We should really fix the cause for "/PageName" in the WikiDB
834 if ($name == '' or $name[0] == SUBPAGE_SEPARATOR) {
836 $name = $this->_pagename($basename) . $name;
838 $name = $this->_normalize_bad_pagename($name);
839 $this->shortName = $name;
844 $name = $this->_pagename($name);
845 $this->shortName = $name;
848 $this->name = $this->_check($name);
849 $this->anchor = (string)$anchor;
856 function getParent() {
858 if (!($tail = strrchr($name, SUBPAGE_SEPARATOR)))
860 return substr($name, 0, -strlen($tail));
863 function isValid($strict = false) {
865 return !isset($this->_errors);
866 return (is_string($this->name) and $this->name != '');
869 function getWarnings() {
871 if (isset($this->_warnings))
872 $warnings = array_merge($warnings, $this->_warnings);
873 if (isset($this->_errors))
874 $warnings = array_merge($warnings, $this->_errors);
878 return sprintf(_("'%s': Bad page name: %s"),
879 $this->shortName, join(', ', $warnings));
882 function _pagename($page) {
883 if (isa($page, 'WikiDB_Page'))
884 return $page->getName();
885 elseif (isa($page, 'WikiDB_PageRevision'))
886 return $page->getPageName();
887 elseif (isa($page, 'WikiPageName'))
889 // '0' or e.g. '1984' should be allowed though
890 if (!is_string($page) and !is_integer($page)) {
891 trigger_error(sprintf("Non-string pagename '%s' (%s)(%s)",
892 $page, gettype($page), get_class($page)),
895 //assert(is_string($page));
899 function _normalize_bad_pagename($name) {
900 trigger_error("Bad pagename: " . $name, E_USER_WARNING);
902 // Punt... You really shouldn't get here.
905 return $request->getArg('pagename');
907 assert($name[0] == SUBPAGE_SEPARATOR);
908 $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
909 return substr($name, 1);
913 * Compress internal white-space to single space character.
915 * This leads to problems with loading a foreign charset pagename,
916 * which cannot be deleted anymore, because unknown chars are compressed.
917 * So BEFORE importing a file _check must be done !!!
919 function _check($pagename) {
920 // Compress internal white-space to single space character.
921 $pagename = preg_replace('/[\s\xa0]+/', ' ', $orig = $pagename);
922 if ($pagename != $orig)
923 $this->_warnings[] = _("White space converted to single space");
925 // Delete any control characters.
926 if (DATABASE_TYPE == 'cvs' or DATABASE_TYPE == 'file' or DATABASE_TYPE == 'flatfile') {
927 $pagename = preg_replace('/[\x00-\x1f\x7f\x80-\x9f]/', '', $orig = $pagename);
928 if ($pagename != $orig)
929 $this->_errors[] = _("Control characters not allowed");
932 // Strip leading and trailing white-space.
933 $pagename = trim($pagename);
936 while ($pagename and $pagename[0] == SUBPAGE_SEPARATOR)
937 $pagename = substr($pagename, 1);
938 if ($pagename != $orig)
939 $this->_errors[] = sprintf(_("Leading %s not allowed"), SUBPAGE_SEPARATOR);
941 // ";" is urlencoded, so safe from php arg-delim problems
942 /*if (strstr($pagename, ';')) {
943 $this->_warnings[] = _("';' is deprecated");
944 $pagename = str_replace(';', '', $pagename);
947 // not only for SQL, also to restrict url length
948 if (strlen($pagename) > MAX_PAGENAME_LENGTH) {
949 $pagename = substr($pagename, 0, MAX_PAGENAME_LENGTH);
950 $this->_errors[] = _("too long");
953 // disallow some chars only on file and cvs
954 if ((DATABASE_TYPE == 'cvs'
955 or DATABASE_TYPE == 'file'
956 or DATABASE_TYPE == 'flatfile')
957 and preg_match('/(:|\.\.)/', $pagename, $m))
959 $this->_warnings[] = sprintf(_("Illegal chars %s removed"), $m[1]);
960 $pagename = str_replace('..', '', $pagename);
961 $pagename = str_replace(':', '', $pagename);
969 * Convert old page markup to new-style markup.
971 * @param string $text Old-style wiki markup.
973 * @param string $markup_type
975 * <dt><code>"block"</code> <dd>Convert all markup.
976 * <dt><code>"inline"</code> <dd>Convert only inline markup.
977 * <dt><code>"links"</code> <dd>Convert only link markup.
980 * @return string New-style wiki markup.
982 * @bugs Footnotes don't work quite as before (esp if there are
983 * multiple references to the same footnote. But close enough,
984 * probably for now....
985 * @bugs Apache2 and IIS crash with OldTextFormattingRules or
986 * AnciennesR%E8glesDeFormatage. (at the 2nd attempt to do the anchored block regex)
987 * It only crashes with CreateToc so far, but other pages (not in pgsrc) are
988 * also known to crash, even with Apache1.
990 function ConvertOldMarkup ($text, $markup_type = "block") {
996 // Trying to detect why the 2nd paragraph of OldTextFormattingRules or
997 // AnciennesR%E8glesDeFormatage crashes.
998 // It only crashes with CreateToc so far, but other pages (not in pgsrc) are
999 // also known to crash, even with Apache1.
1000 $debug_skip = false;
1001 // I suspect this only to crash with Apache2 and IIS.
1002 if (in_array(php_sapi_name(),array('apache2handler','apache2filter','isapi'))
1003 and preg_match("/plugin CreateToc/", $text))
1005 trigger_error(_("The CreateTocPlugin is not yet old markup compatible! ")
1006 ._("Please remove the CreateToc line to be able to reformat this page to old markup. ")
1007 ._("Skipped."), E_USER_WARNING);
1009 //if (!DEBUG) return $text;
1014 /*****************************************************************
1015 * Conversions for inline markup:
1022 // escape escaped brackets
1026 // change ! escapes to ~'s.
1027 global $WikiNameRegexp, $request;
1028 $bang_esc[] = "(?:" . ALLOWED_PROTOCOLS . "):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]";
1029 // before 4.3.9 pcre had a memory release bug, which might hit us here. so be safe.
1030 $map = getInterwikiMap();
1031 if ($map_regex = $map->getRegexp())
1032 $bang_esc[] = $map_regex . ":[^\\s.,;?()]+"; // FIXME: is this really needed?
1033 $bang_esc[] = $WikiNameRegexp;
1034 $orig[] = '/!((?:' . join(')|(', $bang_esc) . '))/';
1037 $subs["links"] = array($orig, $repl);
1039 // Temporarily URL-encode pairs of underscores in links to hide
1040 // them from the re for bold markup.
1041 $orig[] = '/\[[^\[\]]*?__[^\[\]]*?\]/e';
1042 $repl[] = 'str_replace(\'__\', \'%5F%5F\', \'\\0\')';
1045 //$orig[] = '/<(?!\?plugin)|(?<!^)</m';
1048 // Convert footnote references.
1049 $orig[] = '/(?<=.)(?<!~)\[\s*(\d+)\s*\]/m';
1050 $repl[] = '#[|ftnt_ref_\\1]<sup>~[[\\1|#ftnt_\\1]~]</sup>';
1052 // Convert old style emphases to HTML style emphasis.
1053 $orig[] = '/__(.*?)__/';
1054 $repl[] = '<strong>\\1</strong>';
1055 $orig[] = "/''(.*?)''/";
1056 $repl[] = '<em>\\1</em>';
1058 // Escape nestled markup.
1059 $orig[] = '/^(?<=^|\s)[=_](?=\S)|(?<=\S)[=_*](?=\s|$)/m';
1062 // in old markup headings only allowed at beginning of line
1066 // Convert URL-encoded pairs of underscores in links back to
1067 // real underscores after bold markup has been converted.
1068 $orig = '/\[[^\[\]]*?%5F%5F[^\[\]]*?\]/e';
1069 $repl = 'str_replace(\'%5F%5F\', \'__\', \'\\0\')';
1071 $subs["inline"] = array($orig, $repl);
1073 /*****************************************************************
1074 * Patterns which match block markup constructs which take
1075 * special handling...
1079 $blockpats[] = '[ \t]+\S(?:.*\s*\n[ \t]+\S)*';
1081 $blockpats[] = '\|(?:.*\n\|)*';
1084 $blockpats[] = '[#*;]*(?:[*#]|;.*?:)';
1086 // Footnote definitions
1087 $blockpats[] = '\[\s*(\d+)\s*\]';
1091 $blockpats[] = '<\?plugin(?:-form)?\b.*\?>\s*$';
1095 $blockpats[] = '!{1,3}[^!]';
1097 removed .|\n in the anchor not to crash on /m because with /m "." already includes \n
1098 this breaks headings but it doesn't crash anymore (crash on non-cgi, non-cli only)
1100 $block_re = ( '/\A((?:.|\n)*?)(^(?:'
1101 . join("|", $blockpats)
1106 if ($markup_type != "block") {
1107 list ($orig, $repl) = $subs[$markup_type];
1108 return preg_replace($orig, $repl, $text);
1111 list ($orig, $repl) = $subs['inline'];
1114 // php crashes here in the 2nd paragraph of OldTextFormattingRules,
1115 // AnciennesR%E8glesDeFormatage and more
1116 // See http://www.pcre.org/pcre.txt LIMITATIONS
1117 while (preg_match($block_re, $text, $m)) {
1118 $text = substr($text, strlen($m[0]));
1119 list (,$leading_text, $block) = $m;
1122 if (strchr(" \t", $block[0])) {
1124 $prefix = "<pre>\n";
1125 $suffix = "\n</pre>\n";
1127 elseif ($block[0] == '|') {
1129 $prefix = "<?plugin OldStyleTable\n";
1132 elseif (strchr("#*;", $block[0])) {
1133 // Old-style list item
1134 preg_match('/^([#*;]*)([*#]|;.*?:) */', $block, $m);
1135 list (,$ind,$bullet) = $m;
1136 $block = substr($block, strlen($m[0]));
1138 $indent = str_repeat(' ', strlen($ind));
1139 if ($bullet[0] == ';') {
1140 //$term = ltrim(substr($bullet, 1));
1141 //return $indent . $term . "\n" . $indent . ' ';
1142 $prefix = $ind . $bullet;
1145 $prefix = $indent . $bullet . ' ';
1147 elseif ($block[0] == '[') {
1148 // Footnote definition
1149 preg_match('/^\[\s*(\d+)\s*\]/', $block, $m);
1151 $block = substr($block, strlen($m[0]));
1152 $prefix = "#[|ftnt_".${footnum}."]~[[".${footnum}."|#ftnt_ref_".${footnum}."]~] ";
1154 elseif ($block[0] == '<') {
1156 // HACK: no inline markup...
1160 elseif ($block[0] == '!') {
1162 preg_match('/^!{1,3}/', $block, $m);
1164 $block = substr($block, strlen($m[0]));
1170 if ($leading_text) $leading_text = preg_replace($orig, $repl, $leading_text);
1171 if ($block) $block = preg_replace($orig, $repl, $block);
1172 $out .= $leading_text;
1177 return $out . preg_replace($orig, $repl, $text);
1183 * Expand tabs in string.
1185 * Converts all tabs to (the appropriate number of) spaces.
1187 * @param string $str
1188 * @param integer $tab_width
1191 function expand_tabs($str, $tab_width = 8) {
1192 $split = explode("\t", $str);
1193 $tail = array_pop($split);
1195 foreach ($split as $hunk) {
1197 $pos = strlen(strrchr($expanded, "\n")) - 1;
1198 $expanded .= str_repeat(" ", ($tab_width - $pos % $tab_width));
1200 return substr($expanded, 1) . $tail;
1204 * Split WikiWords in page names.
1206 * It has been deemed useful to split WikiWords (into "Wiki Words") in
1207 * places like page titles. This is rumored to help search engines
1210 * @param $page string The page name.
1212 * @return string The split name.
1214 function SplitPagename ($page) {
1216 if (preg_match("/\s/", $page))
1217 return $page; // Already split --- don't split any more.
1219 // This algorithm is specialized for several languages.
1220 // (Thanks to Pierrick MEIGNEN)
1221 // Improvements for other languages welcome.
1224 // This mess splits between a lower-case letter followed by
1225 // either an upper-case or a numeral; except that it wont
1226 // split the prefixes 'Mc', 'De', or 'Di' off of their tails.
1227 switch ($GLOBALS['LANG']) {
1232 $RE[] = '/([[:lower:]])((?<!Mc|De|Di)[[:upper:]]|\d)/';
1235 $RE[] = '/([[:lower:]])((?<!Mc|Di)[[:upper:]]|\d)/';
1238 $sep = preg_quote(SUBPAGE_SEPARATOR, '/');
1239 // This the single-letter words 'I' and 'A' from any following
1240 // capitalized words.
1241 switch ($GLOBALS['LANG']) {
1243 $RE[] = "/(?<= |${sep}|^)([AI])([[:upper:]][[:lower:]])/";
1246 $RE[] = "/(?<= |${sep}|^)([À])([[:upper:]][[:lower:]])/";
1249 // Split at underscore
1250 $RE[] = '/(_)([[:alpha:]])/';
1251 $RE[] = '/([[:alpha:]])(_)/';
1252 // Split numerals from following letters.
1253 $RE[] = '/(\d)([[:alpha:]])/';
1254 // Split at subpage seperators. TBD in WikiTheme.php
1255 $RE[] = "/([^${sep}]+)(${sep})/";
1256 $RE[] = "/(${sep})([^${sep}]+)/";
1258 foreach ($RE as $key)
1262 foreach ($RE as $regexp) {
1263 $page = preg_replace($regexp, '\\1 \\2', $page);
1268 function NoSuchRevision (&$request, $page, $version) {
1269 $html = HTML(HTML::h2(_("Revision Not Found")),
1270 HTML::p(fmt("I'm sorry. Version %d of %s is not in the database.",
1271 $version, WikiLink($page, 'auto'))));
1272 include_once('lib/Template.php');
1273 GeneratePage($html, _("Bad Version"), $page->getCurrentRevision());
1279 * Get time offset for local time zone.
1281 * @param $time time_t Get offset for this time. Default: now.
1282 * @param $no_colon boolean Don't put colon between hours and minutes.
1283 * @return string Offset as a string in the format +HH:MM.
1285 function TimezoneOffset ($time = false, $no_colon = false) {
1286 if ($time === false)
1288 $secs = date('Z', $time);
1297 $colon = $no_colon ? '' : ':';
1298 $mins = intval(($secs + 30) / 60);
1299 return sprintf("%s%02d%s%02d",
1300 $sign, $mins / 60, $colon, $mins % 60);
1305 * Format time in ISO-8601 format.
1307 * @param $time time_t Time. Default: now.
1308 * @return string Date and time in ISO-8601 format.
1310 function Iso8601DateTime ($time = false) {
1311 if ($time === false)
1313 $tzoff = TimezoneOffset($time);
1314 $date = date('Y-m-d', $time);
1315 $time = date('H:i:s', $time);
1316 return $date . 'T' . $time . $tzoff;
1320 * Format time in RFC-2822 format.
1322 * @param $time time_t Time. Default: now.
1323 * @return string Date and time in RFC-2822 format.
1325 function Rfc2822DateTime ($time = false) {
1326 if ($time === false)
1328 return date('D, j M Y H:i:s ', $time) . TimezoneOffset($time, 'no colon');
1332 * Format time in RFC-1123 format.
1334 * @param $time time_t Time. Default: now.
1335 * @return string Date and time in RFC-1123 format.
1337 function Rfc1123DateTime ($time = false) {
1338 if ($time === false)
1340 return gmdate('D, d M Y H:i:s \G\M\T', $time);
1343 /** Parse date in RFC-1123 format.
1345 * According to RFC 1123 we must accept dates in the following
1348 * Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
1349 * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
1350 * Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
1352 * (Though we're only allowed to generate dates in the first format.)
1354 function ParseRfc1123DateTime ($timestr) {
1355 $timestr = trim($timestr);
1356 if (preg_match('/^ \w{3},\s* (\d{1,2}) \s* (\w{3}) \s* (\d{4}) \s*'
1357 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1359 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1361 elseif (preg_match('/^ \w+,\s* (\d{1,2})-(\w{3})-(\d{2}|\d{4}) \s*'
1362 .'(\d\d):(\d\d):(\d\d) \s* GMT $/ix',
1364 list(, $mday, $mon, $year, $hh, $mm, $ss) = $m;
1365 if ($year < 70) $year += 2000;
1366 elseif ($year < 100) $year += 1900;
1368 elseif (preg_match('/^\w+\s* (\w{3}) \s* (\d{1,2}) \s*'
1369 .'(\d\d):(\d\d):(\d\d) \s* (\d{4})$/ix',
1371 list(, $mon, $mday, $hh, $mm, $ss, $year) = $m;
1378 $time = strtotime("$mday $mon $year ${hh}:${mm}:${ss} GMT");
1380 return false; // failed
1385 * Format time to standard 'ctime' format.
1387 * @param $time time_t Time. Default: now.
1388 * @return string Date and time.
1390 function CTime ($time = false)
1392 if ($time === false)
1394 return date("D M j H:i:s Y", $time);
1399 * Format number as kibibytes or bytes.
1400 * Short format is used for PageList
1401 * Long format is used in PageInfo
1403 * @param $bytes int. Default: 0.
1404 * @param $longformat bool. Default: false.
1405 * @return class FormattedText (XmlElement.php).
1407 function ByteFormatter ($bytes = 0, $longformat = false) {
1410 if ($bytes < 1024) {
1412 $size = fmt("%s B", $bytes);
1414 $size = fmt("%s bytes", $bytes);
1417 $kb = round($bytes / 1024, 1);
1419 $size = fmt("%s KiB", $kb);
1421 $size = fmt("%s KiB (%s bytes)", $kb, $bytes);
1427 * Internationalized printf.
1429 * This is essentially the same as PHP's built-in printf
1430 * with the following exceptions:
1432 * <li> It passes the format string through gettext().
1433 * <li> It supports the argument reordering extensions.
1440 * __printf("Differences between versions %s and %s of %s",
1441 * $new_link, $old_link, $page_link);
1444 * Then in locale/po/de.po, one can reorder the printf arguments:
1447 * msgid "Differences between %s and %s of %s."
1448 * msgstr "Der Unterschiedsergebnis von %3$s, zwischen %1$s und %2$s."
1451 * (Note that while PHP tries to expand $vars within double-quotes,
1452 * the values in msgstr undergo no such expansion, so the '$'s
1455 * One shouldn't use reordered arguments in the default format string.
1456 * Backslashes in the default string would be necessary to escape the
1457 * '$'s, and they'll cause all kinds of trouble....
1459 function __printf ($fmt) {
1460 $args = func_get_args();
1462 echo __vsprintf($fmt, $args);
1466 * Internationalized sprintf.
1468 * This is essentially the same as PHP's built-in printf with the
1469 * following exceptions:
1472 * <li> It passes the format string through gettext().
1473 * <li> It supports the argument reordering extensions.
1478 function __sprintf ($fmt) {
1479 $args = func_get_args();
1481 return __vsprintf($fmt, $args);
1485 * Internationalized vsprintf.
1487 * This is essentially the same as PHP's built-in printf with the
1488 * following exceptions:
1491 * <li> It passes the format string through gettext().
1492 * <li> It supports the argument reordering extensions.
1497 function __vsprintf ($fmt, $args) {
1498 $fmt = gettext($fmt);
1499 // PHP's sprintf doesn't support variable with specifiers,
1500 // like sprintf("%*s", 10, "x"); --- so we won't either.
1502 if (preg_match_all('/(?<!%)%(\d+)\$/x', $fmt, $m)) {
1503 // Format string has '%2$s' style argument reordering.
1504 // PHP doesn't support this.
1505 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
1506 // literal variable name substitution only to keep locale
1507 // strings uncluttered
1508 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
1509 '%1\$s','%s'), E_USER_WARNING); //php+locale error
1511 $fmt = preg_replace('/(?<!%)%\d+\$/x', '%', $fmt);
1514 // Reorder arguments appropriately.
1515 foreach($m[1] as $argnum) {
1516 if ($argnum < 1 || $argnum > count($args))
1517 trigger_error(sprintf(_("%s: argument index out of range"),
1518 $argnum), E_USER_WARNING);
1519 $newargs[] = $args[$argnum - 1];
1524 // Not all PHP's have vsprintf, so...
1525 array_unshift($args, $fmt);
1526 return call_user_func_array('sprintf', $args);
1529 function file_mtime ($filename) {
1530 if ($stat = @stat($filename))
1536 function sort_file_mtime ($a, $b) {
1537 $ma = file_mtime($a);
1538 $mb = file_mtime($b);
1539 if (!$ma or !$mb or $ma == $mb) return 0;
1540 return ($ma > $mb) ? -1 : 1;
1545 * Build an array in $this->_fileList of files from $dirname.
1546 * Subdirectories are not traversed.
1548 * (This was a function LoadDir in lib/loadsave.php)
1549 * See also http://www.php.net/manual/en/function.readdir.php
1551 function getFiles($exclude='', $sortby='', $limit='') {
1552 $list = $this->_fileList;
1555 require_once('lib/PageList.php');
1556 switch (Pagelist::sortby($sortby, 'db')) {
1557 case 'pagename ASC': break;
1558 case 'pagename DESC':
1559 $list = array_reverse($list);
1562 usort($list,'sort_file_mtime');
1565 usort($list,'sort_file_mtime');
1566 $list = array_reverse($list);
1571 return array_splice($list, 0, $limit);
1575 function _filenameSelector($filename) {
1576 if (! $this->_pattern )
1579 if (! $this->_pcre_pattern )
1580 $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1581 return preg_match('/' . $this->_pcre_pattern . ($this->_case ? '/' : '/i'),
1586 function fileSet($directory, $filepattern = false) {
1587 $this->_fileList = array();
1588 $this->_pattern = $filepattern;
1590 $this->_pcre_pattern = glob_to_pcre($this->_pattern);
1592 $this->_case = !isWindows();
1593 $this->_pathsep = '/';
1595 if (empty($directory)) {
1596 trigger_error(sprintf(_("%s is empty."), 'directoryname'),
1598 return; // early return
1601 @ $dir_handle = opendir($dir=$directory);
1602 if (empty($dir_handle)) {
1603 trigger_error(sprintf(_("Unable to open directory '%s' for reading"),
1604 $dir), E_USER_NOTICE);
1605 return; // early return
1608 while ($filename = readdir($dir_handle)) {
1609 if ($filename[0] == '.' || filetype($dir . $this->_pathsep . $filename) != 'file')
1611 if ($this->_filenameSelector($filename)) {
1612 array_push($this->_fileList, "$filename");
1613 //trigger_error(sprintf(_("found file %s"), $filename),
1614 // E_USER_NOTICE); //debugging
1617 closedir($dir_handle);
1623 // expands a list containing regex's to its matching entries
1624 class ListRegexExpand {
1625 //var $match, $list, $index, $case_sensitive;
1626 function ListRegexExpand (&$list, $match, $case_sensitive = true) {
1627 $this->match = $match;
1628 $this->list = &$list;
1629 $this->case_sensitive = $case_sensitive;
1630 //$this->index = false;
1632 function listMatchCallback ($item, $key) {
1633 $quoted = str_replace('/','\/',$item);
1634 if (preg_match('/' . $this->match . ($this->case_sensitive ? '/' : '/i'),
1636 unset($this->list[$this->index]);
1637 $this->list[] = $item;
1640 function expandRegex ($index, &$pages) {
1641 $this->index = $index;
1642 array_walk($pages, array($this, 'listMatchCallback'));
1647 // Convert fileglob to regex style:
1648 // Convert some wildcards to pcre style, escape the rest
1649 // Escape . \\ + * ? [ ^ ] $ ( ) { } = ! < > | : /
1650 // Fixed bug #994994: "/" in $glob.
1651 function glob_to_pcre ($glob) {
1652 // check simple case: no need to escape
1653 $escape = '\[](){}=!<>|:/';
1654 if (strcspn($glob, $escape . ".+*?^$") == strlen($glob))
1656 // preg_replace cannot handle "\\\\\\2" so convert \\ to \xff
1657 $glob = strtr($glob, "\\", "\xff");
1658 $glob = str_replace("/", "\\/", $glob);
1659 // first convert some unescaped expressions to pcre style: . => \.
1661 $re = preg_replace('/([^\xff])?(['.preg_quote($special).'])/',
1662 "\\1\xff\\2", $glob);
1665 $re = preg_replace('/([^\xff])?\*/', '$1.*', $re);
1666 $re = preg_replace('/([^\xff])?\?/', '$1.', $re);
1667 if (!preg_match('/^[\?\*]/', $glob))
1669 if (!preg_match('/[\?\*]$/', $glob))
1672 // Fixes Bug 1182997
1673 // .*? handled above, now escape the rest
1674 //while (strcspn($re, $escape) != strlen($re)) // loop strangely needed
1675 $re = preg_replace('/([^\xff])(['.preg_quote($escape, "/").'])/',
1677 // Problem with 'Date/Time' => 'Date\/Time' => 'Date\xff\/Time' => 'Date\/Time'
1679 $re = preg_replace('/\xff/', '', $re);
1683 function glob_match ($glob, $against, $case_sensitive = true) {
1684 return preg_match('/' . glob_to_pcre($glob) . ($case_sensitive ? '/' : '/i'),
1688 function explodeList($input, $allnames, $glob_style = true, $case_sensitive = true) {
1689 $list = explode(',',$input);
1690 // expand wildcards from list of $allnames
1691 if (preg_match('/[\?\*]/',$input)) {
1692 // Optimizing loop invariants:
1693 // http://phplens.com/lens/php-book/optimizing-debugging-php.php
1694 for ($i = 0, $max = sizeof($list); $i < $max; $i++) {
1696 if (preg_match('/[\?\*]/',$f)) {
1698 $expand = new ListRegexExpand($list,
1699 $glob_style ? glob_to_pcre($f) : $f, $case_sensitive);
1700 $expand->expandRegex($i, $allnames);
1707 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1708 function explodePageList($input, $include_empty=false, $sortby='pagename',
1709 $limit='', $exclude='') {
1710 include_once("lib/PageList.php");
1711 return PageList::explodePageList($input, $include_empty, $sortby, $limit, $exclude);
1714 // Class introspections
1717 * Determine whether object is of a specified type.
1718 * In PHP builtin since 4.2.0 as is_a()
1719 * is_a() deprecated in PHP 5, in favor of instanceof operator
1721 * @param $object object An object.
1722 * @param $class string Class name.
1723 * @return bool True iff $object is a $class
1724 * or a sub-type of $class.
1726 function isa ($object, $class) {
1727 //if (check_php_version(5))
1728 // return $object instanceof $class;
1729 if (!check_php_version(5))
1730 return is_a($object, $class);
1732 $lclass = check_php_version(5) ? $class : strtolower($class);
1733 return is_object($object)
1734 && ( strtolower(get_class($object)) == strtolower($class)
1735 || is_subclass_of($object, $lclass) );
1738 /** Determine whether a function is okay to use.
1740 * Some providers (e.g. Lycos) disable some of PHP functions for
1741 * "security reasons." This makes those functions, of course,
1742 * unusable, despite the fact the function_exists() says they
1745 * This function test to see if a function exists and is not
1746 * disallowed by PHP's disable_functions config setting.
1748 * @param string $function_name Function name
1749 * @return bool True iff function can be used.
1751 function function_usable($function_name) {
1753 if (!is_array($disabled)) {
1754 $disabled = array();
1755 // Use get_cfg_var since ini_get() is one of the disabled functions
1756 // (on Lycos, at least.)
1757 $split = preg_split('/\s*,\s*/', trim(get_cfg_var('disable_functions')));
1758 foreach ($split as $f)
1759 $disabled[strtolower($f)] = true;
1762 return ( function_exists($function_name)
1763 and ! isset($disabled[strtolower($function_name)])
1770 * This is used for generating ETags.
1772 function wikihash ($x) {
1773 if (is_scalar($x)) {
1776 elseif (is_array($x)) {
1778 return md5(serialize($x));
1780 elseif (is_object($x)) {
1783 trigger_error("Can't hash $x", E_USER_ERROR);
1788 * Seed the random number generator.
1790 * better_srand() ensures the randomizer is seeded only once.
1792 * How random do you want it? See:
1793 * http://www.php.net/manual/en/function.srand.php
1794 * http://www.php.net/manual/en/function.mt-srand.php
1796 function better_srand($seed = '') {
1797 static $wascalled = FALSE;
1799 $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
1800 function_exists('mt_srand') ? mt_srand($seed) : srand($seed);
1802 //trigger_error("new random seed", E_USER_NOTICE); //debugging
1806 function rand_ascii($length = 1) {
1809 for ($i = 1; $i <= $length; $i++) {
1810 // return only typeable 7 bit ascii, avoid quotes
1811 if (function_exists('mt_rand'))
1812 $s .= chr(mt_rand(40, 126));
1814 // the usually bad glibc srand()
1815 $s .= chr(rand(40, 126));
1820 /* by Dan Frankowski.
1822 function rand_ascii_readable ($length = 6) {
1823 // Pick a few random letters or numbers
1826 // Don't use 1lI0O, because they're hard to read
1827 $letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
1828 $letter_len = strlen($letters);
1829 for ($i=0; $i < $length; $i++) {
1830 if (function_exists('mt_rand'))
1831 $word .= $letters[mt_rand(0, $letter_len-1)];
1833 $word .= $letters[rand(0, $letter_len-1)];
1839 * Recursively count all non-empty elements
1840 * in array of any dimension or mixed - i.e.
1841 * array('1' => 2, '2' => array('1' => 3, '2' => 4))
1842 * See http://www.php.net/manual/en/function.count.php
1844 function count_all($arg) {
1845 // skip if argument is empty
1847 //print_r($arg); //debugging
1849 // not an array, return 1 (base case)
1852 // else call recursively for all elements $arg
1853 foreach($arg as $key => $val)
1854 $count += count_all($val);
1859 function isSubPage($pagename) {
1860 return (strstr($pagename, SUBPAGE_SEPARATOR));
1863 function subPageSlice($pagename, $pos) {
1864 $pages = explode(SUBPAGE_SEPARATOR,$pagename);
1865 $pages = array_slice($pages,$pos,1);
1869 function isActionPage($filename) {
1871 global $AllActionPages;
1873 $localizedAllActionPages = array_map("gettext", $AllActionPages);
1875 return (in_array($filename, $localizedAllActionPages));
1881 * Class for "popping up" and alert box. (Except that right now, it doesn't
1885 * This is a hackish and needs to be refactored. However it would be nice to
1886 * unify all the different methods we use for showing Alerts and Dialogs.
1887 * (E.g. "Page deleted", login form, ...)
1892 * @param object $request
1893 * @param mixed $head Header ("title") for alert box.
1894 * @param mixed $body The text in the alert box.
1895 * @param hash $buttons An array mapping button labels to URLs.
1896 * The default is a single "Okay" button pointing to $request->getURLtoSelf().
1898 function Alert($head, $body, $buttons=false) {
1899 if ($buttons === false)
1902 if (is_array($body)) {
1904 foreach ($body as $li) {
1905 $html->pushContent(HTML::li($li));
1909 $this->_tokens = array('HEADER' => $head, 'CONTENT' => $body);
1910 $this->_buttons = $buttons;
1914 * Show the alert box.
1919 $tokens = $this->_tokens;
1920 $tokens['BUTTONS'] = $this->_getButtons();
1922 $request->discardOutput();
1923 $tmpl = new Template('dialog', $request, $tokens);
1929 function _getButtons() {
1932 $buttons = $this->_buttons;
1934 $buttons = array(_("Okay") => $request->getURLtoSelf());
1937 foreach ($buttons as $label => $url)
1938 print "$label $url\n";
1939 $out[] = $WikiTheme->makeButton($label, $url, 'wikiaction');
1940 return new XmlContent($out);
1945 // 1.3.9-p1 => 1030.091
1946 // 1.3.10pre => 1030.099
1947 // 1.3.11pre-20041120 => 1030.1120041120
1948 // 1.3.12-rc1 => 1030.119
1949 function phpwiki_version() {
1950 static $PHPWIKI_VERSION;
1951 if (!isset($PHPWIKI_VERSION)) {
1952 $arr = explode('.',preg_replace('/\D+$/','', PHPWIKI_VERSION)); // remove the pre
1953 $arr[2] = preg_replace('/\.+/','.',preg_replace('/\D/','.',$arr[2]));
1954 $PHPWIKI_VERSION = $arr[0]*1000 + $arr[1]*10 + 0.01*$arr[2];
1955 if (strstr(PHPWIKI_VERSION, 'pre') or strstr(PHPWIKI_VERSION, 'rc'))
1956 $PHPWIKI_VERSION -= 0.01;
1958 return $PHPWIKI_VERSION;
1961 function phpwiki_gzhandler($ob) {
1962 if (function_exists('gzencode'))
1963 $ob = gzencode($ob);
1964 $GLOBALS['request']->_ob_get_length = strlen($ob);
1965 if (!headers_sent()) {
1966 header(sprintf("Content-Length: %d", $GLOBALS['request']->_ob_get_length));
1971 function isWikiWord($word) {
1972 global $WikiNameRegexp;
1973 //or preg_match('/\A' . $WikiNameRegexp . '\z/', $word) ??
1974 return preg_match("/^$WikiNameRegexp\$/",$word);
1977 // needed to store serialized objects-values only (perm, pref)
1978 function obj2hash ($obj, $exclude = false, $fields = false) {
1980 if (! $fields ) $fields = get_object_vars($obj);
1981 foreach ($fields as $key => $val) {
1982 if (is_array($exclude)) {
1983 if (in_array($key, $exclude)) continue;
1991 * isAsciiString($string)
1993 function isAsciiString($s) {
1994 $ptrASCII = '[\x00-\x7F]';
1995 return preg_match("/^($ptrASCII)*$/s", $s);
1999 * isUtf8String($string) - cheap utf-8 detection
2001 * segfaults for strings longer than 10kb!
2002 * Use http://www.phpdiscuss.com/article.php?id=565&group=php.i18n or
2003 * checkTitleEncoding() at http://cvs.sourceforge.net/viewcvs.py/wikipedia/phase3/languages/Language.php
2005 function isUtf8String( $s ) {
2006 $ptrASCII = '[\x00-\x7F]';
2007 $ptr2Octet = '[\xC2-\xDF][\x80-\xBF]';
2008 $ptr3Octet = '[\xE0-\xEF][\x80-\xBF]{2}';
2009 $ptr4Octet = '[\xF0-\xF4][\x80-\xBF]{3}';
2010 $ptr5Octet = '[\xF8-\xFB][\x80-\xBF]{4}';
2011 $ptr6Octet = '[\xFC-\xFD][\x80-\xBF]{5}';
2012 return preg_match("/^($ptrASCII|$ptr2Octet|$ptr3Octet|$ptr4Octet|$ptr5Octet|$ptr6Octet)*$/s", $s);
2016 * Check for UTF-8 URLs; Internet Explorer produces these if you
2017 * type non-ASCII chars in the URL bar or follow unescaped links.
2018 * Requires urldecoded pagename.
2019 * Fixes sf.net bug #953949
2021 * src: languages/Language.php:checkTitleEncoding() from mediawiki
2023 function fixTitleEncoding( $s ) {
2028 if (empty($s)) return $s;
2030 $ishigh = preg_match( '/[\x80-\xff]/', $s);
2032 $isutf = ($ishigh ? preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2033 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s ) : true );
2035 $isutf = ($ishigh ? isUtf8String($s) : true);
2036 $locharset = strtolower($charset);
2038 if( $locharset != "utf-8" and $ishigh and $isutf )
2039 $s = charset_convert('UTF-8', $locharset, $s);
2040 if ($locharset == "utf-8" and $ishigh and !$isutf )
2041 return utf8_encode( $s );
2043 // Other languages can safely leave this function, or replace
2044 // it with one to detect and convert another legacy encoding.
2049 * MySQL fulltext index doesn't grok utf-8, so we
2050 * need to fold cases and convert to hex.
2051 * src: languages/Language.php:stripForSearch() from mediawiki
2054 function stripForSearch( $string ) {
2055 global $wikiLowerChars;
2056 // '/(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])/' => "a-z\xdf-\xf6\xf8-\xff"
2057 return preg_replace(
2058 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
2059 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
2065 * Workaround for allow_url_fopen, to get the content of an external URI.
2066 * It returns the contents in one slurp. Parsers might want to check for allow_url_fopen
2067 * and use fopen, fread chunkwise. (see lib/XmlParser.php)
2069 function url_get_contents( $uri ) {
2070 if (get_cfg_var('allow_url_fopen')) { // was ini_get('allow_url_fopen'))
2071 return @file_get_contents($uri);
2073 require_once("lib/HttpClient.php");
2074 $bits = parse_url($uri);
2075 $host = $bits['host'];
2076 $port = isset($bits['port']) ? $bits['port'] : 80;
2077 $path = isset($bits['path']) ? $bits['path'] : '/';
2078 if (isset($bits['query'])) {
2079 $path .= '?'.$bits['query'];
2081 $client = new HttpClient($host, $port);
2082 $client->use_gzip = false;
2083 if (!$client->get($path)) {
2086 return $client->getContent();
2092 * Generate consecutively named strings:
2093 * Name, Name2, Name3, ...
2095 function GenerateId($name) {
2096 static $ids = array();
2097 if (empty($ids[$name])) {
2102 return $name . $ids[$name];
2106 // from IncludePage. To be of general use.
2107 // content: string or array of strings
2108 function firstNWordsOfContent( $n, $content ) {
2109 if ($content and $n > 0) {
2110 if (is_array($content)) {
2111 // fixme: return a list of lines then?
2112 //$content = join("\n", $content);
2113 //$return_array = true;
2115 foreach ($content as $line) {
2116 $words = explode(' ', $line);
2117 if ($wordcount + count($words) > $n) {
2118 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
2119 . sprintf(_("... (first %s words)"), $n);
2122 $wordcount += count($words);
2128 // fixme: use better whitespace/word seperators
2129 $words = explode(' ', $content);
2130 if (count($words) > $n) {
2131 return join(' ', array_slice($words, 0, $n))
2132 . sprintf(_("... (first %s words)"), $n);
2142 // moved from lib/plugin/IncludePage.php
2143 function extractSection ($section, $content, $page, $quiet = false, $sectionhead = false) {
2144 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
2146 if (preg_match("/ ^(!{1,}|={2,})\\s*$qsection\s*=*" // section header
2147 . " \\s*$\\n?" // possible blank lines
2148 . " ( (?: ^.*\\n? )*? )" // some lines
2149 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
2150 implode("\n", $content),
2152 // Strip trailing blanks lines and ---- <hr>s
2153 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
2155 $text = $match[1] . $section ."\n". $text;
2156 return explode("\n", $text);
2159 $mesg = $page ." ". $section;
2162 return array(sprintf(_("<%s: no such section>"), $mesg));
2165 // Extract the first $sections sections of the page
2166 function extractSections ($sections, $content, $page, $quiet = false, $sectionhead = false) {
2168 $mycontent = $content;
2171 while ($sections > 0) {
2173 if (preg_match("/ ^(!{1,}|={2,})\\s*(.*)\\n" // section header
2174 . " \\s*$\\n?" // possible blank lines
2175 . " ( (?: ^.*\\n? )*? )" // some lines
2176 . " ( ^\\1 (.|\\n)* | \\Z)/xm", // sec header (same or higher level) (or EOF)
2177 implode("\n", $mycontent),
2179 $section = $match[2];
2180 // Strip trailing blanks lines and ---- <hr>s
2181 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[3]);
2183 $text = $match[1] . $section ."\n". $text;
2186 $mycontent = explode("\n", $match[4]);
2188 if ($sections === 0) {
2189 return explode("\n", $result);
2195 // use this faster version: only load ExternalReferrer if we came from an external referrer
2196 function isExternalReferrer(&$request) {
2197 if ($referrer = $request->get('HTTP_REFERER')) {
2198 $home = SERVER_URL; // SERVER_URL or SCRIPT_NAME, if we want to check sister wiki's also
2199 if (string_starts_with(strtolower($referrer), strtolower($home))) return false;
2200 require_once("lib/ExternalReferrer.php");
2201 $se = new SearchEngines();
2202 return $se->parseSearchQuery($referrer);
2204 //if (DEBUG) return array('query' => 'wiki');
2209 * Useful for PECL overrides: cvsclient, ldap, soap, xmlrpc, pdo, pdo_<driver>
2211 function loadPhpExtension($extension) {
2212 if (!extension_loaded($extension)) {
2213 $isWindows = (substr(PHP_OS,0,3) == 'WIN');
2214 $soname = ($isWindows ? 'php_' : '')
2216 . ($isWindows ? '.dll' : '.so');
2220 return extension_loaded($extension);
2223 function charset_convert($from, $to, $data) {
2225 //$wikicharset = strtolower($CHARSET);
2226 //$systemcharset = strtolower(get_cfg_var('iconv.internal_encoding')); // 'iso-8859-1';
2227 if (strtolower($from) == 'utf-8' and strtolower($to) == 'iso-8859-1')
2228 return utf8_decode($data);
2229 if (strtolower($to) == 'utf-8' and strtolower($from) == 'iso-8859-1')
2230 return utf8_encode($data);
2232 if (loadPhpExtension("iconv")) {
2233 $tmpdata = iconv($from, $to, $data);
2235 trigger_error("charset conversion $from => $to failed. Wrong source charset?", E_USER_WARNING);
2239 trigger_error("The iconv extension cannot be loaded", E_USER_WARNING);
2244 function string_starts_with($string, $prefix) {
2245 return (substr($string, 0, strlen($prefix)) == $prefix);
2247 function string_ends_with($string, $suffix) {
2248 return (substr($string, -strlen($suffix)) == $suffix);
2250 function array_remove($arr,$value) {
2251 return array_values(array_diff($arr,array($value)));
2255 * Ensure that the script will have another $secs time left.
2256 * Works only if safe_mode is off.
2257 * For example not to timeout on waiting socket connections.
2258 * Use the socket timeout as arg.
2260 function longer_timeout($secs = 30) {
2261 $timeout = @ini_get("max_execution_time") ? ini_get("max_execution_time") : 30;
2262 $timeleft = $timeout - $GLOBALS['RUNTIMER']->getTime();
2263 if ($timeleft < $secs)
2264 @set_time_limit(max($timeout,(integer)($secs + $timeleft)));
2267 function printSimpleTrace($bt) {
2269 echo "\nTraceback:\n";
2270 if (function_exists('debug_print_backtrace')) { // >= 5
2271 debug_print_backtrace();
2273 foreach ($bt as $i => $elem) {
2274 if (!array_key_exists('file', $elem)) {
2277 //echo join(" ",array_values($elem)),"\n";
2278 echo " ",$elem['file'],':',$elem['line']," ",$elem['function'],"\n";
2284 * Return the used process memory, in bytes.
2285 * Enable the section which will work for you. They are very slow.
2286 * Special quirks for Windows: Requires cygwin.
2288 function getMemoryUsage() {
2289 //if (!(DEBUG & _DEBUG_VERBOSE)) return;
2290 if (function_exists('memory_get_usage') and memory_get_usage()) {
2291 return memory_get_usage();
2292 } elseif (function_exists('getrusage') and ($u = @getrusage()) and !empty($u['ru_maxrss'])) {
2293 $mem = $u['ru_maxrss'];
2294 } elseif (substr(PHP_OS,0,3) == 'WIN') { // may require a newer cygwin
2295 // what we want is the process memory only: apache or php (if CGI)
2298 // win32_ps_stat_proc, win32_ps_stat_mem
2299 if (function_exists('win32_ps_list_procs')) {
2300 $info = win32_ps_stat_proc($pid);
2301 $memstr = $info['mem']['working_set_size'];
2303 // This works only if it's a cygwin process (apache or php).
2304 // Requires a newer cygwin
2305 $memstr = exec("cat /proc/$pid/statm |cut -f1");
2307 // if it's native windows use something like this:
2308 // (requires pslist from sysinternals.com, grep, sed and perl)
2309 //$memstr = exec("pslist $pid|grep -A1 Mem|sed 1d|perl -ane\"print \$"."F[5]\"");
2311 return (integer) trim($memstr);
2314 //%MEM: Percentage of total memory in use by this process
2315 //VSZ: Total virtual memory size, in 1K blocks.
2316 //RSS: Real Set Size, the actual amount of physical memory allocated to this process.
2317 //CPU time used by process since it started.
2318 //echo "%",`ps -o%mem,vsz,rss,time -p $pid|sed 1d`,"\n";
2319 $memstr = exec("ps -orss -p $pid|sed 1d");
2320 return (integer) trim($memstr);
2325 * @param var $needle
2326 * @param array $haystack one-dimensional numeric array only, no hash
2328 * @desc Feed a sorted array to $haystack and a value to search for to $needle.
2329 It will return false if not found or the index where it was found.
2330 From dennis.decoene@moveit.be http://www.php.net/array_search
2332 function binary_search($needle, $haystack) {
2333 $high = count($haystack);
2336 while (($high - $low) > 1) {
2337 $probe = floor(($high + $low) / 2);
2338 if ($haystack[$probe] < $needle) {
2340 } elseif ($haystack[$probe] == $needle) {
2341 $high = $low = $probe;
2347 if ($high == count($haystack) || $haystack[$high] != $needle) {
2354 function is_localhost($url = false) {
2356 global $HTTP_SERVER_VARS;
2357 return $HTTP_SERVER_VARS['SERVER_ADDR'] == '127.0.0.1';
2362 * Take a string and quote it sufficiently to be passed as a Javascript
2363 * string between ''s
2365 function javascript_quote_string($s) {
2366 return str_replace("'", "\'", $s);
2369 function isSerialized($s) {
2370 return (!empty($s) and (strlen($s) > 3) and (substr($s,1,1) == ':'));
2374 * Take a string and return an array of pairs (attribute name, attribute value)
2376 * We allow attributes with or without double quotes (")
2377 * Attribute-value pairs may be separated by space or comma
2378 * Space is normal HTML attributes, comma is for RichTable compatibility
2379 * border=1, cellpadding="5"
2380 * border=1 cellpadding="5"
2381 * style="font-family: sans-serif; border-top:1px solid #dddddd;"
2382 * style="font-family: Verdana, Arial, Helvetica, sans-serif"
2384 function parse_attributes($line) {
2388 if (empty($line)) return $options;
2389 $line = trim($line);
2390 if (empty($line)) return $options;
2391 $line = trim($line, ",");
2392 if (empty($line)) return $options;
2394 // First we have an attribute name.
2399 while (($i < strlen($line)) && ($line[$i] != '=')) {
2402 $attribute = substr($line, 0, $i);
2403 $attribute = strtolower($attribute);
2405 $line = substr($line, $i+1);
2406 $line = trim ($line);
2407 $line = trim ($line, "=");
2408 $line = trim ($line);
2410 if (empty($line)) return $options;
2412 // Then we have the attribute value.
2415 // Attribute value might be between double quotes
2416 // In that case we have to find the closing double quote
2417 if ($line[0] == '"') {
2418 $i++; // skip first '"'
2419 while (($i < strlen($line)) && ($line[$i] != '"')) {
2422 $value = substr($line, 0, $i);
2423 $value = trim ($value, '"');
2424 $value = trim ($value);
2426 // If there are no double quotes, we have to find the next space or comma
2428 while (($i < strlen($line)) && (($line[$i] != ' ') && ($line[$i] != ','))) {
2431 $value = substr($line, 0, $i);
2432 $value = trim ($value);
2433 $value = trim ($value, ",");
2434 $value = trim ($value);
2437 $options[$attribute] = $value;
2439 $line = substr($line, $i+1);
2440 $line = trim ($line);
2441 $line = trim ($line, ",");
2442 $line = trim ($line);
2444 return $options + parse_attributes($line);
2448 * Returns true if the filename ends with an image suffix.
2449 * Uses INLINE_IMAGES if defined, else "png|jpg|jpeg|gif"
2451 function is_image ($filename) {
2453 if (defined('INLINE_IMAGES')) {
2454 $inline_images = INLINE_IMAGES;
2456 $inline_images = "png|jpg|jpeg|gif";
2459 foreach (explode("|", $inline_images) as $suffix) {
2460 if (string_ends_with(strtolower($filename), "." . $suffix)) {
2468 * Returns true if the filename ends with an video suffix.
2469 * Currently only FLV and OGG
2471 function is_video ($filename) {
2473 return string_ends_with(strtolower($filename), ".flv")
2474 or string_ends_with(strtolower($filename), ".ogg");
2478 * Remove accents from given text.
2480 function strip_accents($text) {
2481 $res = utf8_decode($text);
2483 utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
2484 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
2485 return utf8_encode($res);
2491 // c-basic-offset: 4
2492 // c-hanging-comment-ender-p: nil
2493 // indent-tabs-mode: nil