replace deprecated function calls
[dokuwiki.git] / inc / parser / xhtml.php
blob6874ee03ad284b8f29b5f559dcfde32ab02e2909
1 <?php
3 use dokuwiki\ChangeLog\MediaChangeLog;
4 use dokuwiki\Utils\MediaResolver;
5 use dokuwiki\Utils\PageResolver;
7 /**
8 * Renderer for XHTML output
10 * This is DokuWiki's main renderer used to display page content in the wiki
12 * @author Harry Fuecks <hfuecks@gmail.com>
13 * @author Andreas Gohr <andi@splitbrain.org>
16 class Doku_Renderer_xhtml extends Doku_Renderer {
17 /** @var array store the table of contents */
18 public $toc = array();
20 /** @var array A stack of section edit data */
21 protected $sectionedits = array();
23 /** @var string|int link pages and media against this revision */
24 public $date_at = '';
26 /** @var int last section edit id, used by startSectionEdit */
27 protected $lastsecid = 0;
29 /** @var array a list of footnotes, list starts at 1! */
30 protected $footnotes = array();
32 /** @var int current section level */
33 protected $lastlevel = 0;
34 /** @var array section node tracker */
35 protected $node = array(0, 0, 0, 0, 0);
37 /** @var string temporary $doc store */
38 protected $store = '';
40 /** @var array global counter, for table classes etc. */
41 protected $_counter = array(); //
43 /** @var int counts the code and file blocks, used to provide download links */
44 protected $_codeblock = 0;
46 /** @var array list of allowed URL schemes */
47 protected $schemes = null;
49 /**
50 * Register a new edit section range
52 * @param int $start The byte position for the edit start
53 * @param array $data Associative array with section data:
54 * Key 'name': the section name/title
55 * Key 'target': the target for the section edit,
56 * e.g. 'section' or 'table'
57 * Key 'hid': header id
58 * Key 'codeblockOffset': actual code block index
59 * Key 'start': set in startSectionEdit(),
60 * do not set yourself
61 * Key 'range': calculated from 'start' and
62 * $key in finishSectionEdit(),
63 * do not set yourself
64 * @return string A marker class for the starting HTML element
66 * @author Adrian Lang <lang@cosmocode.de>
68 public function startSectionEdit($start, $data) {
69 if (!is_array($data)) {
70 msg(
71 sprintf(
72 'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
73 hsc((string) $data)
74 ), -1
77 // @deprecated 2018-04-14, backward compatibility
78 $args = func_get_args();
79 $data = array();
80 if(isset($args[1])) $data['target'] = $args[1];
81 if(isset($args[2])) $data['name'] = $args[2];
82 if(isset($args[3])) $data['hid'] = $args[3];
84 $data['secid'] = ++$this->lastsecid;
85 $data['start'] = $start;
86 $this->sectionedits[] = $data;
87 return 'sectionedit'.$data['secid'];
90 /**
91 * Finish an edit section range
93 * @param int $end The byte position for the edit end; null for the rest of the page
95 * @author Adrian Lang <lang@cosmocode.de>
97 public function finishSectionEdit($end = null, $hid = null) {
98 $data = array_pop($this->sectionedits);
99 if(!is_null($end) && $end <= $data['start']) {
100 return;
102 if(!is_null($hid)) {
103 $data['hid'] .= $hid;
105 $data['range'] = $data['start'].'-'.(is_null($end) ? '' : $end);
106 unset($data['start']);
107 $this->doc .= '<!-- EDIT'.hsc(json_encode ($data)).' -->';
111 * Returns the format produced by this renderer.
113 * @return string always 'xhtml'
115 public function getFormat() {
116 return 'xhtml';
120 * Initialize the document
122 public function document_start() {
123 //reset some internals
124 $this->toc = array();
128 * Finalize the document
130 public function document_end() {
131 // Finish open section edits.
132 while(count($this->sectionedits) > 0) {
133 if($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
134 // If there is only one section, do not write a section edit
135 // marker.
136 array_pop($this->sectionedits);
137 } else {
138 $this->finishSectionEdit();
142 if(count($this->footnotes) > 0) {
143 $this->doc .= '<div class="footnotes">'.DOKU_LF;
145 foreach($this->footnotes as $id => $footnote) {
146 // check its not a placeholder that indicates actual footnote text is elsewhere
147 if(substr($footnote, 0, 5) != "@@FNT") {
149 // open the footnote and set the anchor and backlink
150 $this->doc .= '<div class="fn">';
151 $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
152 $this->doc .= $id.')</a></sup> '.DOKU_LF;
154 // get any other footnotes that use the same markup
155 $alt = array_keys($this->footnotes, "@@FNT$id");
157 if(count($alt)) {
158 foreach($alt as $ref) {
159 // set anchor and backlink for the other footnotes
160 $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
161 $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
165 // add footnote markup and close this footnote
166 $this->doc .= '<div class="content">'.$footnote.'</div>';
167 $this->doc .= '</div>'.DOKU_LF;
170 $this->doc .= '</div>'.DOKU_LF;
173 // Prepare the TOC
174 global $conf;
176 $this->info['toc'] &&
177 is_array($this->toc) &&
178 $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']
180 global $TOC;
181 $TOC = $this->toc;
184 // make sure there are no empty paragraphs
185 $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
189 * Add an item to the TOC
191 * @param string $id the hash link
192 * @param string $text the text to display
193 * @param int $level the nesting level
195 public function toc_additem($id, $text, $level) {
196 global $conf;
198 //handle TOC
199 if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
200 $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
205 * Render a heading
207 * @param string $text the text to display
208 * @param int $level header level
209 * @param int $pos byte position in the original source
211 public function header($text, $level, $pos) {
212 global $conf;
214 if(blank($text)) return; //skip empty headlines
216 $hid = $this->_headerToLink($text, true);
218 //only add items within configured levels
219 $this->toc_additem($hid, $text, $level);
221 // adjust $node to reflect hierarchy of levels
222 $this->node[$level - 1]++;
223 if($level < $this->lastlevel) {
224 for($i = 0; $i < $this->lastlevel - $level; $i++) {
225 $this->node[$this->lastlevel - $i - 1] = 0;
228 $this->lastlevel = $level;
230 if($level <= $conf['maxseclevel'] &&
231 count($this->sectionedits) > 0 &&
232 $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
234 $this->finishSectionEdit($pos - 1);
237 // write the header
238 $this->doc .= DOKU_LF.'<h'.$level;
239 if($level <= $conf['maxseclevel']) {
240 $data = array();
241 $data['target'] = 'section';
242 $data['name'] = $text;
243 $data['hid'] = $hid;
244 $data['codeblockOffset'] = $this->_codeblock;
245 $this->doc .= ' class="'.$this->startSectionEdit($pos, $data).'"';
247 $this->doc .= ' id="'.$hid.'">';
248 $this->doc .= $this->_xmlEntities($text);
249 $this->doc .= "</h$level>".DOKU_LF;
253 * Open a new section
255 * @param int $level section level (as determined by the previous header)
257 public function section_open($level) {
258 $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
262 * Close the current section
264 public function section_close() {
265 $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
269 * Render plain text data
271 * @param $text
273 public function cdata($text) {
274 $this->doc .= $this->_xmlEntities($text);
278 * Open a paragraph
280 public function p_open() {
281 $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
285 * Close a paragraph
287 public function p_close() {
288 $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
292 * Create a line break
294 public function linebreak() {
295 $this->doc .= '<br/>'.DOKU_LF;
299 * Create a horizontal line
301 public function hr() {
302 $this->doc .= '<hr />'.DOKU_LF;
306 * Start strong (bold) formatting
308 public function strong_open() {
309 $this->doc .= '<strong>';
313 * Stop strong (bold) formatting
315 public function strong_close() {
316 $this->doc .= '</strong>';
320 * Start emphasis (italics) formatting
322 public function emphasis_open() {
323 $this->doc .= '<em>';
327 * Stop emphasis (italics) formatting
329 public function emphasis_close() {
330 $this->doc .= '</em>';
334 * Start underline formatting
336 public function underline_open() {
337 $this->doc .= '<em class="u">';
341 * Stop underline formatting
343 public function underline_close() {
344 $this->doc .= '</em>';
348 * Start monospace formatting
350 public function monospace_open() {
351 $this->doc .= '<code>';
355 * Stop monospace formatting
357 public function monospace_close() {
358 $this->doc .= '</code>';
362 * Start a subscript
364 public function subscript_open() {
365 $this->doc .= '<sub>';
369 * Stop a subscript
371 public function subscript_close() {
372 $this->doc .= '</sub>';
376 * Start a superscript
378 public function superscript_open() {
379 $this->doc .= '<sup>';
383 * Stop a superscript
385 public function superscript_close() {
386 $this->doc .= '</sup>';
390 * Start deleted (strike-through) formatting
392 public function deleted_open() {
393 $this->doc .= '<del>';
397 * Stop deleted (strike-through) formatting
399 public function deleted_close() {
400 $this->doc .= '</del>';
404 * Callback for footnote start syntax
406 * All following content will go to the footnote instead of
407 * the document. To achieve this the previous rendered content
408 * is moved to $store and $doc is cleared
410 * @author Andreas Gohr <andi@splitbrain.org>
412 public function footnote_open() {
414 // move current content to store and record footnote
415 $this->store = $this->doc;
416 $this->doc = '';
420 * Callback for footnote end syntax
422 * All rendered content is moved to the $footnotes array and the old
423 * content is restored from $store again
425 * @author Andreas Gohr
427 public function footnote_close() {
428 /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
429 static $fnid = 0;
430 // assign new footnote id (we start at 1)
431 $fnid++;
433 // recover footnote into the stack and restore old content
434 $footnote = $this->doc;
435 $this->doc = $this->store;
436 $this->store = '';
438 // check to see if this footnote has been seen before
439 $i = array_search($footnote, $this->footnotes);
441 if($i === false) {
442 // its a new footnote, add it to the $footnotes array
443 $this->footnotes[$fnid] = $footnote;
444 } else {
445 // seen this one before, save a placeholder
446 $this->footnotes[$fnid] = "@@FNT".($i);
449 // output the footnote reference and link
450 $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
454 * Open an unordered list
456 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
458 public function listu_open($classes = null) {
459 $class = '';
460 if($classes !== null) {
461 if(is_array($classes)) $classes = join(' ', $classes);
462 $class = " class=\"$classes\"";
464 $this->doc .= "<ul$class>".DOKU_LF;
468 * Close an unordered list
470 public function listu_close() {
471 $this->doc .= '</ul>'.DOKU_LF;
475 * Open an ordered list
477 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
479 public function listo_open($classes = null) {
480 $class = '';
481 if($classes !== null) {
482 if(is_array($classes)) $classes = join(' ', $classes);
483 $class = " class=\"$classes\"";
485 $this->doc .= "<ol$class>".DOKU_LF;
489 * Close an ordered list
491 public function listo_close() {
492 $this->doc .= '</ol>'.DOKU_LF;
496 * Open a list item
498 * @param int $level the nesting level
499 * @param bool $node true when a node; false when a leaf
501 public function listitem_open($level, $node=false) {
502 $branching = $node ? ' node' : '';
503 $this->doc .= '<li class="level'.$level.$branching.'">';
507 * Close a list item
509 public function listitem_close() {
510 $this->doc .= '</li>'.DOKU_LF;
514 * Start the content of a list item
516 public function listcontent_open() {
517 $this->doc .= '<div class="li">';
521 * Stop the content of a list item
523 public function listcontent_close() {
524 $this->doc .= '</div>'.DOKU_LF;
528 * Output unformatted $text
530 * Defaults to $this->cdata()
532 * @param string $text
534 public function unformatted($text) {
535 $this->doc .= $this->_xmlEntities($text);
539 * Execute PHP code if allowed
541 * @param string $text PHP code that is either executed or printed
542 * @param string $wrapper html element to wrap result if $conf['phpok'] is okff
544 * @author Andreas Gohr <andi@splitbrain.org>
546 public function php($text, $wrapper = 'code') {
547 global $conf;
549 if($conf['phpok']) {
550 ob_start();
551 eval($text);
552 $this->doc .= ob_get_contents();
553 ob_end_clean();
554 } else {
555 $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
560 * Output block level PHP code
562 * If $conf['phpok'] is true this should evaluate the given code and append the result
563 * to $doc
565 * @param string $text The PHP code
567 public function phpblock($text) {
568 $this->php($text, 'pre');
572 * Insert HTML if allowed
574 * @param string $text html text
575 * @param string $wrapper html element to wrap result if $conf['htmlok'] is okff
577 * @author Andreas Gohr <andi@splitbrain.org>
579 public function html($text, $wrapper = 'code') {
580 global $conf;
582 if($conf['htmlok']) {
583 $this->doc .= $text;
584 } else {
585 $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
590 * Output raw block-level HTML
592 * If $conf['htmlok'] is true this should add the code as is to $doc
594 * @param string $text The HTML
596 public function htmlblock($text) {
597 $this->html($text, 'pre');
601 * Start a block quote
603 public function quote_open() {
604 $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
608 * Stop a block quote
610 public function quote_close() {
611 $this->doc .= '</div></blockquote>'.DOKU_LF;
615 * Output preformatted text
617 * @param string $text
619 public function preformatted($text) {
620 $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
624 * Display text as file content, optionally syntax highlighted
626 * @param string $text text to show
627 * @param string $language programming language to use for syntax highlighting
628 * @param string $filename file path label
629 * @param array $options assoziative array with additional geshi options
631 public function file($text, $language = null, $filename = null, $options=null) {
632 $this->_highlight('file', $text, $language, $filename, $options);
636 * Display text as code content, optionally syntax highlighted
638 * @param string $text text to show
639 * @param string $language programming language to use for syntax highlighting
640 * @param string $filename file path label
641 * @param array $options assoziative array with additional geshi options
643 public function code($text, $language = null, $filename = null, $options=null) {
644 $this->_highlight('code', $text, $language, $filename, $options);
648 * Use GeSHi to highlight language syntax in code and file blocks
650 * @author Andreas Gohr <andi@splitbrain.org>
651 * @param string $type code|file
652 * @param string $text text to show
653 * @param string $language programming language to use for syntax highlighting
654 * @param string $filename file path label
655 * @param array $options assoziative array with additional geshi options
657 public function _highlight($type, $text, $language = null, $filename = null, $options = null) {
658 global $ID;
659 global $lang;
660 global $INPUT;
662 $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);
664 if($filename) {
665 // add icon
666 list($ext) = mimetype($filename, false);
667 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
668 $class = 'mediafile mf_'.$class;
670 $offset = 0;
671 if ($INPUT->has('codeblockOffset')) {
672 $offset = $INPUT->str('codeblockOffset');
674 $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
675 $this->doc .= '<dt><a href="' .
676 exportlink(
677 $ID,
678 'code',
679 array('codeblock' => $offset + $this->_codeblock)
680 ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
681 $this->doc .= hsc($filename);
682 $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
685 if($text[0] == "\n") {
686 $text = substr($text, 1);
688 if(substr($text, -1) == "\n") {
689 $text = substr($text, 0, -1);
692 if(empty($language)) { // empty is faster than is_null and can prevent '' string
693 $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
694 } else {
695 $class = 'code'; //we always need the code class to make the syntax highlighting apply
696 if($type != 'code') $class .= ' '.$type;
698 $this->doc .= "<pre class=\"$class $language\">" .
699 p_xhtml_cached_geshi($text, $language, '', $options) .
700 '</pre>' . DOKU_LF;
703 if($filename) {
704 $this->doc .= '</dd></dl>'.DOKU_LF;
707 $this->_codeblock++;
711 * Format an acronym
713 * Uses $this->acronyms
715 * @param string $acronym
717 public function acronym($acronym) {
719 if(array_key_exists($acronym, $this->acronyms)) {
721 $title = $this->_xmlEntities($this->acronyms[$acronym]);
723 $this->doc .= '<abbr title="'.$title
724 .'">'.$this->_xmlEntities($acronym).'</abbr>';
726 } else {
727 $this->doc .= $this->_xmlEntities($acronym);
732 * Format a smiley
734 * Uses $this->smiley
736 * @param string $smiley
738 public function smiley($smiley) {
739 if(array_key_exists($smiley, $this->smileys)) {
740 $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
741 '" class="icon" alt="'.
742 $this->_xmlEntities($smiley).'" />';
743 } else {
744 $this->doc .= $this->_xmlEntities($smiley);
749 * Format an entity
751 * Entities are basically small text replacements
753 * Uses $this->entities
755 * @param string $entity
757 public function entity($entity) {
758 if(array_key_exists($entity, $this->entities)) {
759 $this->doc .= $this->entities[$entity];
760 } else {
761 $this->doc .= $this->_xmlEntities($entity);
766 * Typographically format a multiply sign
768 * Example: ($x=640, $y=480) should result in "640×480"
770 * @param string|int $x first value
771 * @param string|int $y second value
773 public function multiplyentity($x, $y) {
774 $this->doc .= "$x&times;$y";
778 * Render an opening single quote char (language specific)
780 public function singlequoteopening() {
781 global $lang;
782 $this->doc .= $lang['singlequoteopening'];
786 * Render a closing single quote char (language specific)
788 public function singlequoteclosing() {
789 global $lang;
790 $this->doc .= $lang['singlequoteclosing'];
794 * Render an apostrophe char (language specific)
796 public function apostrophe() {
797 global $lang;
798 $this->doc .= $lang['apostrophe'];
802 * Render an opening double quote char (language specific)
804 public function doublequoteopening() {
805 global $lang;
806 $this->doc .= $lang['doublequoteopening'];
810 * Render an closinging double quote char (language specific)
812 public function doublequoteclosing() {
813 global $lang;
814 $this->doc .= $lang['doublequoteclosing'];
818 * Render a CamelCase link
820 * @param string $link The link name
821 * @param bool $returnonly whether to return html or write to doc attribute
822 * @return void|string writes to doc attribute or returns html depends on $returnonly
824 * @see http://en.wikipedia.org/wiki/CamelCase
826 public function camelcaselink($link, $returnonly = false) {
827 if($returnonly) {
828 return $this->internallink($link, $link, null, true);
829 } else {
830 $this->internallink($link, $link);
835 * Render a page local link
837 * @param string $hash hash link identifier
838 * @param string $name name for the link
839 * @param bool $returnonly whether to return html or write to doc attribute
840 * @return void|string writes to doc attribute or returns html depends on $returnonly
842 public function locallink($hash, $name = null, $returnonly = false) {
843 global $ID;
844 $name = $this->_getLinkTitle($name, $hash, $isImage);
845 $hash = $this->_headerToLink($hash);
846 $title = $ID.' ↵';
848 $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
849 $doc .= $name;
850 $doc .= '</a>';
852 if($returnonly) {
853 return $doc;
854 } else {
855 $this->doc .= $doc;
860 * Render an internal Wiki Link
862 * $search,$returnonly & $linktype are not for the renderer but are used
863 * elsewhere - no need to implement them in other renderers
865 * @author Andreas Gohr <andi@splitbrain.org>
866 * @param string $id pageid
867 * @param string|null $name link name
868 * @param string|null $search adds search url param
869 * @param bool $returnonly whether to return html or write to doc attribute
870 * @param string $linktype type to set use of headings
871 * @return void|string writes to doc attribute or returns html depends on $returnonly
873 public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
874 global $conf;
875 global $ID;
876 global $INFO;
878 $params = '';
879 $parts = explode('?', $id, 2);
880 if(count($parts) === 2) {
881 $id = $parts[0];
882 $params = $parts[1];
885 // For empty $id we need to know the current $ID
886 // We need this check because _simpleTitle needs
887 // correct $id and resolve_pageid() use cleanID($id)
888 // (some things could be lost)
889 if($id === '') {
890 $id = $ID;
893 // default name is based on $id as given
894 $default = $this->_simpleTitle($id);
896 // now first resolve and clean up the $id
897 $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
898 $exists = page_exists($id, $this->date_at, false, true);
900 $link = array();
901 $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
902 if(!$isImage) {
903 if($exists) {
904 $class = 'wikilink1';
905 } else {
906 $class = 'wikilink2';
907 $link['rel'] = 'nofollow';
909 } else {
910 $class = 'media';
913 //keep hash anchor
914 @list($id, $hash) = explode('#', $id, 2);
915 if(!empty($hash)) $hash = $this->_headerToLink($hash);
917 //prepare for formating
918 $link['target'] = $conf['target']['wiki'];
919 $link['style'] = '';
920 $link['pre'] = '';
921 $link['suf'] = '';
922 $link['more'] = 'data-wiki-id="'.$id.'"'; // id is already cleaned
923 $link['class'] = $class;
924 if($this->date_at) {
925 $params = $params.'&at='.rawurlencode($this->date_at);
927 $link['url'] = wl($id, $params);
928 $link['name'] = $name;
929 $link['title'] = $id;
930 //add search string
931 if($search) {
932 ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
933 if(is_array($search)) {
934 $search = array_map('rawurlencode', $search);
935 $link['url'] .= 's[]='.join('&amp;s[]=', $search);
936 } else {
937 $link['url'] .= 's='.rawurlencode($search);
941 //keep hash
942 if($hash) $link['url'] .= '#'.$hash;
944 //output formatted
945 if($returnonly) {
946 return $this->_formatLink($link);
947 } else {
948 $this->doc .= $this->_formatLink($link);
953 * Render an external link
955 * @param string $url full URL with scheme
956 * @param string|array $name name for the link, array for media file
957 * @param bool $returnonly whether to return html or write to doc attribute
958 * @return void|string writes to doc attribute or returns html depends on $returnonly
960 public function externallink($url, $name = null, $returnonly = false) {
961 global $conf;
963 $name = $this->_getLinkTitle($name, $url, $isImage);
965 // url might be an attack vector, only allow registered protocols
966 if(is_null($this->schemes)) $this->schemes = getSchemes();
967 list($scheme) = explode('://', $url);
968 $scheme = strtolower($scheme);
969 if(!in_array($scheme, $this->schemes)) $url = '';
971 // is there still an URL?
972 if(!$url) {
973 if($returnonly) {
974 return $name;
975 } else {
976 $this->doc .= $name;
978 return;
981 // set class
982 if(!$isImage) {
983 $class = 'urlextern';
984 } else {
985 $class = 'media';
988 //prepare for formating
989 $link = array();
990 $link['target'] = $conf['target']['extern'];
991 $link['style'] = '';
992 $link['pre'] = '';
993 $link['suf'] = '';
994 $link['more'] = '';
995 $link['class'] = $class;
996 $link['url'] = $url;
997 $link['rel'] = '';
999 $link['name'] = $name;
1000 $link['title'] = $this->_xmlEntities($url);
1001 if($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
1002 if($conf['target']['extern']) $link['rel'] .= ' noopener';
1004 //output formatted
1005 if($returnonly) {
1006 return $this->_formatLink($link);
1007 } else {
1008 $this->doc .= $this->_formatLink($link);
1013 * Render an interwiki link
1015 * You may want to use $this->_resolveInterWiki() here
1017 * @param string $match original link - probably not much use
1018 * @param string|array $name name for the link, array for media file
1019 * @param string $wikiName indentifier (shortcut) for the remote wiki
1020 * @param string $wikiUri the fragment parsed from the original link
1021 * @param bool $returnonly whether to return html or write to doc attribute
1022 * @return void|string writes to doc attribute or returns html depends on $returnonly
1024 public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) {
1025 global $conf;
1027 $link = array();
1028 $link['target'] = $conf['target']['interwiki'];
1029 $link['pre'] = '';
1030 $link['suf'] = '';
1031 $link['more'] = '';
1032 $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage);
1033 $link['rel'] = '';
1035 //get interwiki URL
1036 $exists = null;
1037 $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
1039 if(!$isImage) {
1040 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
1041 $link['class'] = "interwiki iw_$class";
1042 } else {
1043 $link['class'] = 'media';
1046 //do we stay at the same server? Use local target
1047 if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
1048 $link['target'] = $conf['target']['wiki'];
1050 if($exists !== null && !$isImage) {
1051 if($exists) {
1052 $link['class'] .= ' wikilink1';
1053 } else {
1054 $link['class'] .= ' wikilink2';
1055 $link['rel'] .= ' nofollow';
1058 if($conf['target']['interwiki']) $link['rel'] .= ' noopener';
1060 $link['url'] = $url;
1061 $link['title'] = htmlspecialchars($link['url']);
1063 // output formatted
1064 if($returnonly) {
1065 if($url == '') return $link['name'];
1066 return $this->_formatLink($link);
1067 } else {
1068 if($url == '') $this->doc .= $link['name'];
1069 else $this->doc .= $this->_formatLink($link);
1074 * Link to windows share
1076 * @param string $url the link
1077 * @param string|array $name name for the link, array for media file
1078 * @param bool $returnonly whether to return html or write to doc attribute
1079 * @return void|string writes to doc attribute or returns html depends on $returnonly
1081 public function windowssharelink($url, $name = null, $returnonly = false) {
1082 global $conf;
1084 //simple setup
1085 $link = array();
1086 $link['target'] = $conf['target']['windows'];
1087 $link['pre'] = '';
1088 $link['suf'] = '';
1089 $link['style'] = '';
1091 $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
1092 if(!$isImage) {
1093 $link['class'] = 'windows';
1094 } else {
1095 $link['class'] = 'media';
1098 $link['title'] = $this->_xmlEntities($url);
1099 $url = str_replace('\\', '/', $url);
1100 $url = 'file:///'.$url;
1101 $link['url'] = $url;
1103 //output formatted
1104 if($returnonly) {
1105 return $this->_formatLink($link);
1106 } else {
1107 $this->doc .= $this->_formatLink($link);
1112 * Render a linked E-Mail Address
1114 * Honors $conf['mailguard'] setting
1116 * @param string $address Email-Address
1117 * @param string|array $name name for the link, array for media file
1118 * @param bool $returnonly whether to return html or write to doc attribute
1119 * @return void|string writes to doc attribute or returns html depends on $returnonly
1121 public function emaillink($address, $name = null, $returnonly = false) {
1122 global $conf;
1123 //simple setup
1124 $link = array();
1125 $link['target'] = '';
1126 $link['pre'] = '';
1127 $link['suf'] = '';
1128 $link['style'] = '';
1129 $link['more'] = '';
1131 $name = $this->_getLinkTitle($name, '', $isImage);
1132 if(!$isImage) {
1133 $link['class'] = 'mail';
1134 } else {
1135 $link['class'] = 'media';
1138 $address = $this->_xmlEntities($address);
1139 $address = obfuscate($address);
1140 $title = $address;
1142 if(empty($name)) {
1143 $name = $address;
1146 if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1148 $link['url'] = 'mailto:'.$address;
1149 $link['name'] = $name;
1150 $link['title'] = $title;
1152 //output formatted
1153 if($returnonly) {
1154 return $this->_formatLink($link);
1155 } else {
1156 $this->doc .= $this->_formatLink($link);
1161 * Render an internal media file
1163 * @param string $src media ID
1164 * @param string $title descriptive text
1165 * @param string $align left|center|right
1166 * @param int $width width of media in pixel
1167 * @param int $height height of media in pixel
1168 * @param string $cache cache|recache|nocache
1169 * @param string $linking linkonly|detail|nolink
1170 * @param bool $return return HTML instead of adding to $doc
1171 * @return void|string writes to doc attribute or returns html depends on $return
1173 public function internalmedia($src, $title = null, $align = null, $width = null,
1174 $height = null, $cache = null, $linking = null, $return = false) {
1175 global $ID;
1176 if (strpos($src, '#') !== false) {
1177 list($src, $hash) = explode('#', $src, 2);
1179 $src = (new MediaResolver($ID))->resolveId($src,$this->date_at,true);
1180 $exists = media_exists($src);
1182 $noLink = false;
1183 $render = ($linking == 'linkonly') ? false : true;
1184 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1186 list($ext, $mime) = mimetype($src, false);
1187 if(substr($mime, 0, 5) == 'image' && $render) {
1188 $link['url'] = ml(
1189 $src,
1190 array(
1191 'id' => $ID,
1192 'cache' => $cache,
1193 'rev' => $this->_getLastMediaRevisionAt($src)
1195 ($linking == 'direct')
1197 } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
1198 // don't link movies
1199 $noLink = true;
1200 } else {
1201 // add file icons
1202 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1203 $link['class'] .= ' mediafile mf_'.$class;
1204 $link['url'] = ml(
1205 $src,
1206 array(
1207 'id' => $ID,
1208 'cache' => $cache,
1209 'rev' => $this->_getLastMediaRevisionAt($src)
1211 true
1213 if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
1216 if (!empty($hash)) $link['url'] .= '#'.$hash;
1218 //markup non existing files
1219 if(!$exists) {
1220 $link['class'] .= ' wikilink2';
1223 //output formatted
1224 if($return) {
1225 if($linking == 'nolink' || $noLink) return $link['name'];
1226 else return $this->_formatLink($link);
1227 } else {
1228 if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
1229 else $this->doc .= $this->_formatLink($link);
1234 * Render an external media file
1236 * @param string $src full media URL
1237 * @param string $title descriptive text
1238 * @param string $align left|center|right
1239 * @param int $width width of media in pixel
1240 * @param int $height height of media in pixel
1241 * @param string $cache cache|recache|nocache
1242 * @param string $linking linkonly|detail|nolink
1243 * @param bool $return return HTML instead of adding to $doc
1244 * @return void|string writes to doc attribute or returns html depends on $return
1246 public function externalmedia($src, $title = null, $align = null, $width = null,
1247 $height = null, $cache = null, $linking = null, $return = false) {
1248 if(link_isinterwiki($src)){
1249 list($shortcut, $reference) = explode('>', $src, 2);
1250 $exists = null;
1251 $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
1252 if($src == '' && empty($title)){
1253 // make sure at least something will be shown in this case
1254 $title = $reference;
1257 list($src, $hash) = explode('#', $src, 2);
1258 $noLink = false;
1259 if($src == '') {
1260 // only output plaintext without link if there is no src
1261 $noLink = true;
1263 $render = ($linking == 'linkonly') ? false : true;
1264 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1266 $link['url'] = ml($src, array('cache' => $cache));
1268 list($ext, $mime) = mimetype($src, false);
1269 if(substr($mime, 0, 5) == 'image' && $render) {
1270 // link only jpeg images
1271 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1272 } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
1273 // don't link movies
1274 $noLink = true;
1275 } else {
1276 // add file icons
1277 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1278 $link['class'] .= ' mediafile mf_'.$class;
1281 if($hash) $link['url'] .= '#'.$hash;
1283 //output formatted
1284 if($return) {
1285 if($linking == 'nolink' || $noLink) return $link['name'];
1286 else return $this->_formatLink($link);
1287 } else {
1288 if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
1289 else $this->doc .= $this->_formatLink($link);
1294 * Renders an RSS feed
1296 * @param string $url URL of the feed
1297 * @param array $params Finetuning of the output
1299 * @author Andreas Gohr <andi@splitbrain.org>
1301 public function rss($url, $params) {
1302 global $lang;
1303 global $conf;
1305 require_once(DOKU_INC.'inc/FeedParser.php');
1306 $feed = new FeedParser();
1307 $feed->set_feed_url($url);
1309 //disable warning while fetching
1310 if(!defined('DOKU_E_LEVEL')) {
1311 $elvl = error_reporting(E_ERROR);
1313 $rc = $feed->init();
1314 if(isset($elvl)) {
1315 error_reporting($elvl);
1318 if($params['nosort']) $feed->enable_order_by_date(false);
1320 //decide on start and end
1321 if($params['reverse']) {
1322 $mod = -1;
1323 $start = $feed->get_item_quantity() - 1;
1324 $end = $start - ($params['max']);
1325 $end = ($end < -1) ? -1 : $end;
1326 } else {
1327 $mod = 1;
1328 $start = 0;
1329 $end = $feed->get_item_quantity();
1330 $end = ($end > $params['max']) ? $params['max'] : $end;
1333 $this->doc .= '<ul class="rss">';
1334 if($rc) {
1335 for($x = $start; $x != $end; $x += $mod) {
1336 $item = $feed->get_item($x);
1337 $this->doc .= '<li><div class="li">';
1338 // support feeds without links
1339 $lnkurl = $item->get_permalink();
1340 if($lnkurl) {
1341 // title is escaped by SimplePie, we unescape here because it
1342 // is escaped again in externallink() FS#1705
1343 $this->externallink(
1344 $item->get_permalink(),
1345 html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
1347 } else {
1348 $this->doc .= ' '.$item->get_title();
1350 if($params['author']) {
1351 $author = $item->get_author(0);
1352 if($author) {
1353 $name = $author->get_name();
1354 if(!$name) $name = $author->get_email();
1355 if($name) $this->doc .= ' '.$lang['by'].' '.hsc($name);
1358 if($params['date']) {
1359 $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
1361 if($params['details']) {
1362 $this->doc .= '<div class="detail">';
1363 if($conf['htmlok']) {
1364 $this->doc .= $item->get_description();
1365 } else {
1366 $this->doc .= strip_tags($item->get_description());
1368 $this->doc .= '</div>';
1371 $this->doc .= '</div></li>';
1373 } else {
1374 $this->doc .= '<li><div class="li">';
1375 $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1376 $this->externallink($url);
1377 if($conf['allowdebug']) {
1378 $this->doc .= '<!--'.hsc($feed->error).'-->';
1380 $this->doc .= '</div></li>';
1382 $this->doc .= '</ul>';
1386 * Start a table
1388 * @param int $maxcols maximum number of columns
1389 * @param int $numrows NOT IMPLEMENTED
1390 * @param int $pos byte position in the original source
1391 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1393 public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) {
1394 // initialize the row counter used for classes
1395 $this->_counter['row_counter'] = 0;
1396 $class = 'table';
1397 if($classes !== null) {
1398 if(is_array($classes)) $classes = join(' ', $classes);
1399 $class .= ' ' . $classes;
1401 if($pos !== null) {
1402 $hid = $this->_headerToLink($class, true);
1403 $data = array();
1404 $data['target'] = 'table';
1405 $data['name'] = '';
1406 $data['hid'] = $hid;
1407 $class .= ' '.$this->startSectionEdit($pos, $data);
1409 $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1410 DOKU_LF;
1414 * Close a table
1416 * @param int $pos byte position in the original source
1418 public function table_close($pos = null) {
1419 $this->doc .= '</table></div>'.DOKU_LF;
1420 if($pos !== null) {
1421 $this->finishSectionEdit($pos);
1426 * Open a table header
1428 public function tablethead_open() {
1429 $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1433 * Close a table header
1435 public function tablethead_close() {
1436 $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1440 * Open a table body
1442 public function tabletbody_open() {
1443 $this->doc .= DOKU_TAB.'<tbody>'.DOKU_LF;
1447 * Close a table body
1449 public function tabletbody_close() {
1450 $this->doc .= DOKU_TAB.'</tbody>'.DOKU_LF;
1454 * Open a table footer
1456 public function tabletfoot_open() {
1457 $this->doc .= DOKU_TAB.'<tfoot>'.DOKU_LF;
1461 * Close a table footer
1463 public function tabletfoot_close() {
1464 $this->doc .= DOKU_TAB.'</tfoot>'.DOKU_LF;
1468 * Open a table row
1470 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1472 public function tablerow_open($classes = null) {
1473 // initialize the cell counter used for classes
1474 $this->_counter['cell_counter'] = 0;
1475 $class = 'row'.$this->_counter['row_counter']++;
1476 if($classes !== null) {
1477 if(is_array($classes)) $classes = join(' ', $classes);
1478 $class .= ' ' . $classes;
1480 $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
1484 * Close a table row
1486 public function tablerow_close() {
1487 $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
1491 * Open a table header cell
1493 * @param int $colspan
1494 * @param string $align left|center|right
1495 * @param int $rowspan
1496 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1498 public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1499 $class = 'class="col'.$this->_counter['cell_counter']++;
1500 if(!is_null($align)) {
1501 $class .= ' '.$align.'align';
1503 if($classes !== null) {
1504 if(is_array($classes)) $classes = join(' ', $classes);
1505 $class .= ' ' . $classes;
1507 $class .= '"';
1508 $this->doc .= '<th '.$class;
1509 if($colspan > 1) {
1510 $this->_counter['cell_counter'] += $colspan - 1;
1511 $this->doc .= ' colspan="'.$colspan.'"';
1513 if($rowspan > 1) {
1514 $this->doc .= ' rowspan="'.$rowspan.'"';
1516 $this->doc .= '>';
1520 * Close a table header cell
1522 public function tableheader_close() {
1523 $this->doc .= '</th>';
1527 * Open a table cell
1529 * @param int $colspan
1530 * @param string $align left|center|right
1531 * @param int $rowspan
1532 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1534 public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1535 $class = 'class="col'.$this->_counter['cell_counter']++;
1536 if(!is_null($align)) {
1537 $class .= ' '.$align.'align';
1539 if($classes !== null) {
1540 if(is_array($classes)) $classes = join(' ', $classes);
1541 $class .= ' ' . $classes;
1543 $class .= '"';
1544 $this->doc .= '<td '.$class;
1545 if($colspan > 1) {
1546 $this->_counter['cell_counter'] += $colspan - 1;
1547 $this->doc .= ' colspan="'.$colspan.'"';
1549 if($rowspan > 1) {
1550 $this->doc .= ' rowspan="'.$rowspan.'"';
1552 $this->doc .= '>';
1556 * Close a table cell
1558 public function tablecell_close() {
1559 $this->doc .= '</td>';
1563 * Returns the current header level.
1564 * (required e.g. by the filelist plugin)
1566 * @return int The current header level
1568 public function getLastlevel() {
1569 return $this->lastlevel;
1572 #region Utility functions
1575 * Build a link
1577 * Assembles all parts defined in $link returns HTML for the link
1579 * @param array $link attributes of a link
1580 * @return string
1582 * @author Andreas Gohr <andi@splitbrain.org>
1584 public function _formatLink($link) {
1585 //make sure the url is XHTML compliant (skip mailto)
1586 if(substr($link['url'], 0, 7) != 'mailto:') {
1587 $link['url'] = str_replace('&', '&amp;', $link['url']);
1588 $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1590 //remove double encodings in titles
1591 $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1593 // be sure there are no bad chars in url or title
1594 // (we can't do this for name because it can contain an img tag)
1595 $link['url'] = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1596 $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1598 $ret = '';
1599 $ret .= $link['pre'];
1600 $ret .= '<a href="'.$link['url'].'"';
1601 if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1602 if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1603 if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1604 if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
1605 if(!empty($link['rel'])) $ret .= ' rel="'.trim($link['rel']).'"';
1606 if(!empty($link['more'])) $ret .= ' '.$link['more'];
1607 $ret .= '>';
1608 $ret .= $link['name'];
1609 $ret .= '</a>';
1610 $ret .= $link['suf'];
1611 return $ret;
1615 * Renders internal and external media
1617 * @author Andreas Gohr <andi@splitbrain.org>
1618 * @param string $src media ID
1619 * @param string $title descriptive text
1620 * @param string $align left|center|right
1621 * @param int $width width of media in pixel
1622 * @param int $height height of media in pixel
1623 * @param string $cache cache|recache|nocache
1624 * @param bool $render should the media be embedded inline or just linked
1625 * @return string
1627 public function _media($src, $title = null, $align = null, $width = null,
1628 $height = null, $cache = null, $render = true) {
1630 $ret = '';
1632 list($ext, $mime) = mimetype($src);
1633 if(substr($mime, 0, 5) == 'image') {
1634 // first get the $title
1635 if(!is_null($title)) {
1636 $title = $this->_xmlEntities($title);
1637 } elseif($ext == 'jpg' || $ext == 'jpeg') {
1638 //try to use the caption from IPTC/EXIF
1639 require_once(DOKU_INC.'inc/JpegMeta.php');
1640 $jpeg = new JpegMeta(mediaFN($src));
1641 if($jpeg !== false) $cap = $jpeg->getTitle();
1642 if(!empty($cap)) {
1643 $title = $this->_xmlEntities($cap);
1646 if(!$render) {
1647 // if the picture is not supposed to be rendered
1648 // return the title of the picture
1649 if($title === null || $title === "") {
1650 // just show the sourcename
1651 $title = $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
1653 return $title;
1655 //add image tag
1656 $ret .= '<img src="' . ml(
1657 $src,
1658 array(
1659 'w' => $width, 'h' => $height,
1660 'cache' => $cache,
1661 'rev' => $this->_getLastMediaRevisionAt($src)
1663 ) . '"';
1664 $ret .= ' class="media'.$align.'"';
1666 if($title) {
1667 $ret .= ' title="'.$title.'"';
1668 $ret .= ' alt="'.$title.'"';
1669 } else {
1670 $ret .= ' alt=""';
1673 if(!is_null($width))
1674 $ret .= ' width="'.$this->_xmlEntities($width).'"';
1676 if(!is_null($height))
1677 $ret .= ' height="'.$this->_xmlEntities($height).'"';
1679 $ret .= ' />';
1681 } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
1682 // first get the $title
1683 $title = !is_null($title) ? $title : false;
1684 if(!$render) {
1685 // if the file is not supposed to be rendered
1686 // return the title of the file (just the sourcename if there is no title)
1687 return $this->_xmlEntities($title ? $title : \dokuwiki\Utf8\PhpString::basename(noNS($src)));
1690 $att = array();
1691 $att['class'] = "media$align";
1692 if($title) {
1693 $att['title'] = $title;
1696 if(media_supportedav($mime, 'video')) {
1697 //add video
1698 $ret .= $this->_video($src, $width, $height, $att);
1700 if(media_supportedav($mime, 'audio')) {
1701 //add audio
1702 $ret .= $this->_audio($src, $att);
1705 } elseif($mime == 'application/x-shockwave-flash') {
1706 if(!$render) {
1707 // if the flash is not supposed to be rendered
1708 // return the title of the flash
1709 if(!$title) {
1710 // just show the sourcename
1711 $title = \dokuwiki\Utf8\PhpString::basename(noNS($src));
1713 return $this->_xmlEntities($title);
1716 $att = array();
1717 $att['class'] = "media$align";
1718 if($align == 'right') $att['align'] = 'right';
1719 if($align == 'left') $att['align'] = 'left';
1720 $ret .= html_flashobject(
1721 ml($src, array('cache' => $cache), true, '&'), $width, $height,
1722 array('quality' => 'high'),
1723 null,
1724 $att,
1725 $this->_xmlEntities($title)
1727 } elseif($title) {
1728 // well at least we have a title to display
1729 $ret .= $this->_xmlEntities($title);
1730 } else {
1731 // just show the sourcename
1732 $ret .= $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
1735 return $ret;
1739 * Escape string for output
1741 * @param $string
1742 * @return string
1744 public function _xmlEntities($string) {
1745 return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
1751 * Construct a title and handle images in titles
1753 * @author Harry Fuecks <hfuecks@gmail.com>
1754 * @param string|array $title either string title or media array
1755 * @param string $default default title if nothing else is found
1756 * @param bool $isImage will be set to true if it's a media file
1757 * @param null|string $id linked page id (used to extract title from first heading)
1758 * @param string $linktype content|navigation
1759 * @return string HTML of the title, might be full image tag or just escaped text
1761 public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
1762 $isImage = false;
1763 if(is_array($title)) {
1764 $isImage = true;
1765 return $this->_imageTitle($title);
1766 } elseif(is_null($title) || trim($title) == '') {
1767 if(useHeading($linktype) && $id) {
1768 $heading = p_get_first_heading($id);
1769 if(!blank($heading)) {
1770 return $this->_xmlEntities($heading);
1773 return $this->_xmlEntities($default);
1774 } else {
1775 return $this->_xmlEntities($title);
1780 * Returns HTML code for images used in link titles
1782 * @author Andreas Gohr <andi@splitbrain.org>
1783 * @param array $img
1784 * @return string HTML img tag or similar
1786 public function _imageTitle($img) {
1787 global $ID;
1789 // some fixes on $img['src']
1790 // see internalmedia() and externalmedia()
1791 list($img['src']) = explode('#', $img['src'], 2);
1792 if($img['type'] == 'internalmedia') {
1793 $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true);
1796 return $this->_media(
1797 $img['src'],
1798 $img['title'],
1799 $img['align'],
1800 $img['width'],
1801 $img['height'],
1802 $img['cache']
1807 * helperfunction to return a basic link to a media
1809 * used in internalmedia() and externalmedia()
1811 * @author Pierre Spring <pierre.spring@liip.ch>
1812 * @param string $src media ID
1813 * @param string $title descriptive text
1814 * @param string $align left|center|right
1815 * @param int $width width of media in pixel
1816 * @param int $height height of media in pixel
1817 * @param string $cache cache|recache|nocache
1818 * @param bool $render should the media be embedded inline or just linked
1819 * @return array associative array with link config
1821 public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1822 global $conf;
1824 $link = array();
1825 $link['class'] = 'media';
1826 $link['style'] = '';
1827 $link['pre'] = '';
1828 $link['suf'] = '';
1829 $link['more'] = '';
1830 $link['target'] = $conf['target']['media'];
1831 if($conf['target']['media']) $link['rel'] = 'noopener';
1832 $link['title'] = $this->_xmlEntities($src);
1833 $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1835 return $link;
1839 * Embed video(s) in HTML
1841 * @author Anika Henke <anika@selfthinker.org>
1842 * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
1844 * @param string $src - ID of video to embed
1845 * @param int $width - width of the video in pixels
1846 * @param int $height - height of the video in pixels
1847 * @param array $atts - additional attributes for the <video> tag
1848 * @return string
1850 public function _video($src, $width, $height, $atts = null) {
1851 // prepare width and height
1852 if(is_null($atts)) $atts = array();
1853 $atts['width'] = (int) $width;
1854 $atts['height'] = (int) $height;
1855 if(!$atts['width']) $atts['width'] = 320;
1856 if(!$atts['height']) $atts['height'] = 240;
1858 $posterUrl = '';
1859 $files = array();
1860 $tracks = array();
1861 $isExternal = media_isexternal($src);
1863 if ($isExternal) {
1864 // take direct source for external files
1865 list(/*ext*/, $srcMime) = mimetype($src);
1866 $files[$srcMime] = $src;
1867 } else {
1868 // prepare alternative formats
1869 $extensions = array('webm', 'ogv', 'mp4');
1870 $files = media_alternativefiles($src, $extensions);
1871 $poster = media_alternativefiles($src, array('jpg', 'png'));
1872 $tracks = media_trackfiles($src);
1873 if(!empty($poster)) {
1874 $posterUrl = ml(reset($poster), '', true, '&');
1878 $out = '';
1879 // open video tag
1880 $out .= '<video '.buildAttributes($atts).' controls="controls"';
1881 if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1882 $out .= '>'.NL;
1883 $fallback = '';
1885 // output source for each alternative video format
1886 foreach($files as $mime => $file) {
1887 if ($isExternal) {
1888 $url = $file;
1889 $linkType = 'externalmedia';
1890 } else {
1891 $url = ml($file, '', true, '&');
1892 $linkType = 'internalmedia';
1894 $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
1896 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1897 // alternative content (just a link to the file)
1898 $fallback .= $this->$linkType(
1899 $file,
1900 $title,
1901 null,
1902 null,
1903 null,
1904 $cache = null,
1905 $linking = 'linkonly',
1906 $return = true
1910 // output each track if any
1911 foreach( $tracks as $trackid => $info ) {
1912 list( $kind, $srclang ) = array_map( 'hsc', $info );
1913 $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
1914 $out .= "label=\"$srclang\" ";
1915 $out .= 'src="'.ml($trackid, '', true).'">'.NL;
1918 // finish
1919 $out .= $fallback;
1920 $out .= '</video>'.NL;
1921 return $out;
1925 * Embed audio in HTML
1927 * @author Anika Henke <anika@selfthinker.org>
1929 * @param string $src - ID of audio to embed
1930 * @param array $atts - additional attributes for the <audio> tag
1931 * @return string
1933 public function _audio($src, $atts = array()) {
1934 $files = array();
1935 $isExternal = media_isexternal($src);
1937 if ($isExternal) {
1938 // take direct source for external files
1939 list(/*ext*/, $srcMime) = mimetype($src);
1940 $files[$srcMime] = $src;
1941 } else {
1942 // prepare alternative formats
1943 $extensions = array('ogg', 'mp3', 'wav');
1944 $files = media_alternativefiles($src, $extensions);
1947 $out = '';
1948 // open audio tag
1949 $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
1950 $fallback = '';
1952 // output source for each alternative audio format
1953 foreach($files as $mime => $file) {
1954 if ($isExternal) {
1955 $url = $file;
1956 $linkType = 'externalmedia';
1957 } else {
1958 $url = ml($file, '', true, '&');
1959 $linkType = 'internalmedia';
1961 $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
1963 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1964 // alternative content (just a link to the file)
1965 $fallback .= $this->$linkType(
1966 $file,
1967 $title,
1968 null,
1969 null,
1970 null,
1971 $cache = null,
1972 $linking = 'linkonly',
1973 $return = true
1977 // finish
1978 $out .= $fallback;
1979 $out .= '</audio>'.NL;
1980 return $out;
1984 * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
1985 * which returns an existing media revision less or equal to rev or date_at
1987 * @author lisps
1988 * @param string $media_id
1989 * @access protected
1990 * @return string revision ('' for current)
1992 protected function _getLastMediaRevisionAt($media_id){
1993 if(!$this->date_at || media_isexternal($media_id)) return '';
1994 $pagelog = new MediaChangeLog($media_id);
1995 return $pagelog->getLastRevisionAt($this->date_at);
1998 #endregion
2001 //Setup VIM: ex: et ts=4 :