xhtml.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. <?php
  2. use dokuwiki\ChangeLog\MediaChangeLog;
  3. use dokuwiki\File\MediaResolver;
  4. use dokuwiki\File\PageResolver;
  5. /**
  6. * Renderer for XHTML output
  7. *
  8. * This is DokuWiki's main renderer used to display page content in the wiki
  9. *
  10. * @author Harry Fuecks <hfuecks@gmail.com>
  11. * @author Andreas Gohr <andi@splitbrain.org>
  12. *
  13. */
  14. class Doku_Renderer_xhtml extends Doku_Renderer {
  15. /** @var array store the table of contents */
  16. public $toc = array();
  17. /** @var array A stack of section edit data */
  18. protected $sectionedits = array();
  19. /** @var int last section edit id, used by startSectionEdit */
  20. protected $lastsecid = 0;
  21. /** @var array a list of footnotes, list starts at 1! */
  22. protected $footnotes = array();
  23. /** @var int current section level */
  24. protected $lastlevel = 0;
  25. /** @var array section node tracker */
  26. protected $node = array(0, 0, 0, 0, 0);
  27. /** @var string temporary $doc store */
  28. protected $store = '';
  29. /** @var array global counter, for table classes etc. */
  30. protected $_counter = array(); //
  31. /** @var int counts the code and file blocks, used to provide download links */
  32. protected $_codeblock = 0;
  33. /** @var array list of allowed URL schemes */
  34. protected $schemes = null;
  35. /**
  36. * Register a new edit section range
  37. *
  38. * @param int $start The byte position for the edit start
  39. * @param array $data Associative array with section data:
  40. * Key 'name': the section name/title
  41. * Key 'target': the target for the section edit,
  42. * e.g. 'section' or 'table'
  43. * Key 'hid': header id
  44. * Key 'codeblockOffset': actual code block index
  45. * Key 'start': set in startSectionEdit(),
  46. * do not set yourself
  47. * Key 'range': calculated from 'start' and
  48. * $key in finishSectionEdit(),
  49. * do not set yourself
  50. * @return string A marker class for the starting HTML element
  51. *
  52. * @author Adrian Lang <lang@cosmocode.de>
  53. */
  54. public function startSectionEdit($start, $data) {
  55. if (!is_array($data)) {
  56. msg(
  57. sprintf(
  58. 'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
  59. hsc((string) $data)
  60. ), -1
  61. );
  62. // @deprecated 2018-04-14, backward compatibility
  63. $args = func_get_args();
  64. $data = array();
  65. if(isset($args[1])) $data['target'] = $args[1];
  66. if(isset($args[2])) $data['name'] = $args[2];
  67. if(isset($args[3])) $data['hid'] = $args[3];
  68. }
  69. $data['secid'] = ++$this->lastsecid;
  70. $data['start'] = $start;
  71. $this->sectionedits[] = $data;
  72. return 'sectionedit'.$data['secid'];
  73. }
  74. /**
  75. * Finish an edit section range
  76. *
  77. * @param int $end The byte position for the edit end; null for the rest of the page
  78. *
  79. * @author Adrian Lang <lang@cosmocode.de>
  80. */
  81. public function finishSectionEdit($end = null, $hid = null) {
  82. if(count($this->sectionedits) == 0) {
  83. return;
  84. }
  85. $data = array_pop($this->sectionedits);
  86. if(!is_null($end) && $end <= $data['start']) {
  87. return;
  88. }
  89. if(!is_null($hid)) {
  90. $data['hid'] .= $hid;
  91. }
  92. $data['range'] = $data['start'].'-'.(is_null($end) ? '' : $end);
  93. unset($data['start']);
  94. $this->doc .= '<!-- EDIT'.hsc(json_encode ($data)).' -->';
  95. }
  96. /**
  97. * Returns the format produced by this renderer.
  98. *
  99. * @return string always 'xhtml'
  100. */
  101. public function getFormat() {
  102. return 'xhtml';
  103. }
  104. /**
  105. * Initialize the document
  106. */
  107. public function document_start() {
  108. //reset some internals
  109. $this->toc = array();
  110. }
  111. /**
  112. * Finalize the document
  113. */
  114. public function document_end() {
  115. // Finish open section edits.
  116. while(count($this->sectionedits) > 0) {
  117. if($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
  118. // If there is only one section, do not write a section edit
  119. // marker.
  120. array_pop($this->sectionedits);
  121. } else {
  122. $this->finishSectionEdit();
  123. }
  124. }
  125. if(count($this->footnotes) > 0) {
  126. $this->doc .= '<div class="footnotes">'.DOKU_LF;
  127. foreach($this->footnotes as $id => $footnote) {
  128. // check its not a placeholder that indicates actual footnote text is elsewhere
  129. if(substr($footnote, 0, 5) != "@@FNT") {
  130. // open the footnote and set the anchor and backlink
  131. $this->doc .= '<div class="fn">';
  132. $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
  133. $this->doc .= $id.')</a></sup> '.DOKU_LF;
  134. // get any other footnotes that use the same markup
  135. $alt = array_keys($this->footnotes, "@@FNT$id");
  136. if(count($alt)) {
  137. foreach($alt as $ref) {
  138. // set anchor and backlink for the other footnotes
  139. $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
  140. $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
  141. }
  142. }
  143. // add footnote markup and close this footnote
  144. $this->doc .= '<div class="content">'.$footnote.'</div>';
  145. $this->doc .= '</div>'.DOKU_LF;
  146. }
  147. }
  148. $this->doc .= '</div>'.DOKU_LF;
  149. }
  150. // Prepare the TOC
  151. global $conf;
  152. if(
  153. $this->info['toc'] &&
  154. is_array($this->toc) &&
  155. $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']
  156. ) {
  157. global $TOC;
  158. $TOC = $this->toc;
  159. }
  160. // make sure there are no empty paragraphs
  161. $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
  162. }
  163. /**
  164. * Add an item to the TOC
  165. *
  166. * @param string $id the hash link
  167. * @param string $text the text to display
  168. * @param int $level the nesting level
  169. */
  170. public function toc_additem($id, $text, $level) {
  171. global $conf;
  172. //handle TOC
  173. if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
  174. $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
  175. }
  176. }
  177. /**
  178. * Render a heading
  179. *
  180. * @param string $text the text to display
  181. * @param int $level header level
  182. * @param int $pos byte position in the original source
  183. * @param bool $returnonly whether to return html or write to doc attribute
  184. * @return void|string writes to doc attribute or returns html depends on $returnonly
  185. */
  186. public function header($text, $level, $pos, $returnonly = false) {
  187. global $conf;
  188. if(blank($text)) return; //skip empty headlines
  189. $hid = $this->_headerToLink($text, true);
  190. //only add items within configured levels
  191. $this->toc_additem($hid, $text, $level);
  192. // adjust $node to reflect hierarchy of levels
  193. $this->node[$level - 1]++;
  194. if($level < $this->lastlevel) {
  195. for($i = 0; $i < $this->lastlevel - $level; $i++) {
  196. $this->node[$this->lastlevel - $i - 1] = 0;
  197. }
  198. }
  199. $this->lastlevel = $level;
  200. if($level <= $conf['maxseclevel'] &&
  201. count($this->sectionedits) > 0 &&
  202. $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
  203. ) {
  204. $this->finishSectionEdit($pos - 1);
  205. }
  206. // build the header
  207. $header = DOKU_LF.'<h'.$level;
  208. if($level <= $conf['maxseclevel']) {
  209. $data = array();
  210. $data['target'] = 'section';
  211. $data['name'] = $text;
  212. $data['hid'] = $hid;
  213. $data['codeblockOffset'] = $this->_codeblock;
  214. $header .= ' class="'.$this->startSectionEdit($pos, $data).'"';
  215. }
  216. $header .= ' id="'.$hid.'">';
  217. $header .= $this->_xmlEntities($text);
  218. $header .= "</h$level>".DOKU_LF;
  219. if ($returnonly) {
  220. return $header;
  221. } else {
  222. $this->doc .= $header;
  223. }
  224. }
  225. /**
  226. * Open a new section
  227. *
  228. * @param int $level section level (as determined by the previous header)
  229. */
  230. public function section_open($level) {
  231. $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
  232. }
  233. /**
  234. * Close the current section
  235. */
  236. public function section_close() {
  237. $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
  238. }
  239. /**
  240. * Render plain text data
  241. *
  242. * @param $text
  243. */
  244. public function cdata($text) {
  245. $this->doc .= $this->_xmlEntities($text);
  246. }
  247. /**
  248. * Open a paragraph
  249. */
  250. public function p_open() {
  251. $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
  252. }
  253. /**
  254. * Close a paragraph
  255. */
  256. public function p_close() {
  257. $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
  258. }
  259. /**
  260. * Create a line break
  261. */
  262. public function linebreak() {
  263. $this->doc .= '<br/>'.DOKU_LF;
  264. }
  265. /**
  266. * Create a horizontal line
  267. */
  268. public function hr() {
  269. $this->doc .= '<hr />'.DOKU_LF;
  270. }
  271. /**
  272. * Start strong (bold) formatting
  273. */
  274. public function strong_open() {
  275. $this->doc .= '<strong>';
  276. }
  277. /**
  278. * Stop strong (bold) formatting
  279. */
  280. public function strong_close() {
  281. $this->doc .= '</strong>';
  282. }
  283. /**
  284. * Start emphasis (italics) formatting
  285. */
  286. public function emphasis_open() {
  287. $this->doc .= '<em>';
  288. }
  289. /**
  290. * Stop emphasis (italics) formatting
  291. */
  292. public function emphasis_close() {
  293. $this->doc .= '</em>';
  294. }
  295. /**
  296. * Start underline formatting
  297. */
  298. public function underline_open() {
  299. $this->doc .= '<em class="u">';
  300. }
  301. /**
  302. * Stop underline formatting
  303. */
  304. public function underline_close() {
  305. $this->doc .= '</em>';
  306. }
  307. /**
  308. * Start monospace formatting
  309. */
  310. public function monospace_open() {
  311. $this->doc .= '<code>';
  312. }
  313. /**
  314. * Stop monospace formatting
  315. */
  316. public function monospace_close() {
  317. $this->doc .= '</code>';
  318. }
  319. /**
  320. * Start a subscript
  321. */
  322. public function subscript_open() {
  323. $this->doc .= '<sub>';
  324. }
  325. /**
  326. * Stop a subscript
  327. */
  328. public function subscript_close() {
  329. $this->doc .= '</sub>';
  330. }
  331. /**
  332. * Start a superscript
  333. */
  334. public function superscript_open() {
  335. $this->doc .= '<sup>';
  336. }
  337. /**
  338. * Stop a superscript
  339. */
  340. public function superscript_close() {
  341. $this->doc .= '</sup>';
  342. }
  343. /**
  344. * Start deleted (strike-through) formatting
  345. */
  346. public function deleted_open() {
  347. $this->doc .= '<del>';
  348. }
  349. /**
  350. * Stop deleted (strike-through) formatting
  351. */
  352. public function deleted_close() {
  353. $this->doc .= '</del>';
  354. }
  355. /**
  356. * Callback for footnote start syntax
  357. *
  358. * All following content will go to the footnote instead of
  359. * the document. To achieve this the previous rendered content
  360. * is moved to $store and $doc is cleared
  361. *
  362. * @author Andreas Gohr <andi@splitbrain.org>
  363. */
  364. public function footnote_open() {
  365. // move current content to store and record footnote
  366. $this->store = $this->doc;
  367. $this->doc = '';
  368. }
  369. /**
  370. * Callback for footnote end syntax
  371. *
  372. * All rendered content is moved to the $footnotes array and the old
  373. * content is restored from $store again
  374. *
  375. * @author Andreas Gohr
  376. */
  377. public function footnote_close() {
  378. /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
  379. static $fnid = 0;
  380. // assign new footnote id (we start at 1)
  381. $fnid++;
  382. // recover footnote into the stack and restore old content
  383. $footnote = $this->doc;
  384. $this->doc = $this->store;
  385. $this->store = '';
  386. // check to see if this footnote has been seen before
  387. $i = array_search($footnote, $this->footnotes);
  388. if($i === false) {
  389. // its a new footnote, add it to the $footnotes array
  390. $this->footnotes[$fnid] = $footnote;
  391. } else {
  392. // seen this one before, save a placeholder
  393. $this->footnotes[$fnid] = "@@FNT".($i);
  394. }
  395. // output the footnote reference and link
  396. $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
  397. }
  398. /**
  399. * Open an unordered list
  400. *
  401. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  402. */
  403. public function listu_open($classes = null) {
  404. $class = '';
  405. if($classes !== null) {
  406. if(is_array($classes)) $classes = join(' ', $classes);
  407. $class = " class=\"$classes\"";
  408. }
  409. $this->doc .= "<ul$class>".DOKU_LF;
  410. }
  411. /**
  412. * Close an unordered list
  413. */
  414. public function listu_close() {
  415. $this->doc .= '</ul>'.DOKU_LF;
  416. }
  417. /**
  418. * Open an ordered list
  419. *
  420. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  421. */
  422. public function listo_open($classes = null) {
  423. $class = '';
  424. if($classes !== null) {
  425. if(is_array($classes)) $classes = join(' ', $classes);
  426. $class = " class=\"$classes\"";
  427. }
  428. $this->doc .= "<ol$class>".DOKU_LF;
  429. }
  430. /**
  431. * Close an ordered list
  432. */
  433. public function listo_close() {
  434. $this->doc .= '</ol>'.DOKU_LF;
  435. }
  436. /**
  437. * Open a list item
  438. *
  439. * @param int $level the nesting level
  440. * @param bool $node true when a node; false when a leaf
  441. */
  442. public function listitem_open($level, $node=false) {
  443. $branching = $node ? ' node' : '';
  444. $this->doc .= '<li class="level'.$level.$branching.'">';
  445. }
  446. /**
  447. * Close a list item
  448. */
  449. public function listitem_close() {
  450. $this->doc .= '</li>'.DOKU_LF;
  451. }
  452. /**
  453. * Start the content of a list item
  454. */
  455. public function listcontent_open() {
  456. $this->doc .= '<div class="li">';
  457. }
  458. /**
  459. * Stop the content of a list item
  460. */
  461. public function listcontent_close() {
  462. $this->doc .= '</div>'.DOKU_LF;
  463. }
  464. /**
  465. * Output unformatted $text
  466. *
  467. * Defaults to $this->cdata()
  468. *
  469. * @param string $text
  470. */
  471. public function unformatted($text) {
  472. $this->doc .= $this->_xmlEntities($text);
  473. }
  474. /**
  475. * Start a block quote
  476. */
  477. public function quote_open() {
  478. $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
  479. }
  480. /**
  481. * Stop a block quote
  482. */
  483. public function quote_close() {
  484. $this->doc .= '</div></blockquote>'.DOKU_LF;
  485. }
  486. /**
  487. * Output preformatted text
  488. *
  489. * @param string $text
  490. */
  491. public function preformatted($text) {
  492. $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
  493. }
  494. /**
  495. * Display text as file content, optionally syntax highlighted
  496. *
  497. * @param string $text text to show
  498. * @param string $language programming language to use for syntax highlighting
  499. * @param string $filename file path label
  500. * @param array $options assoziative array with additional geshi options
  501. */
  502. public function file($text, $language = null, $filename = null, $options=null) {
  503. $this->_highlight('file', $text, $language, $filename, $options);
  504. }
  505. /**
  506. * Display text as code content, optionally syntax highlighted
  507. *
  508. * @param string $text text to show
  509. * @param string $language programming language to use for syntax highlighting
  510. * @param string $filename file path label
  511. * @param array $options assoziative array with additional geshi options
  512. */
  513. public function code($text, $language = null, $filename = null, $options=null) {
  514. $this->_highlight('code', $text, $language, $filename, $options);
  515. }
  516. /**
  517. * Use GeSHi to highlight language syntax in code and file blocks
  518. *
  519. * @author Andreas Gohr <andi@splitbrain.org>
  520. * @param string $type code|file
  521. * @param string $text text to show
  522. * @param string $language programming language to use for syntax highlighting
  523. * @param string $filename file path label
  524. * @param array $options assoziative array with additional geshi options
  525. */
  526. public function _highlight($type, $text, $language = null, $filename = null, $options = null) {
  527. global $ID;
  528. global $lang;
  529. global $INPUT;
  530. $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? '');
  531. if($filename) {
  532. // add icon
  533. list($ext) = mimetype($filename, false);
  534. $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
  535. $class = 'mediafile mf_'.$class;
  536. $offset = 0;
  537. if ($INPUT->has('codeblockOffset')) {
  538. $offset = $INPUT->str('codeblockOffset');
  539. }
  540. $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
  541. $this->doc .= '<dt><a href="' .
  542. exportlink(
  543. $ID,
  544. 'code',
  545. array('codeblock' => $offset + $this->_codeblock)
  546. ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
  547. $this->doc .= hsc($filename);
  548. $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
  549. }
  550. if($text[0] == "\n") {
  551. $text = substr($text, 1);
  552. }
  553. if(substr($text, -1) == "\n") {
  554. $text = substr($text, 0, -1);
  555. }
  556. if(empty($language)) { // empty is faster than is_null and can prevent '' string
  557. $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
  558. } else {
  559. $class = 'code'; //we always need the code class to make the syntax highlighting apply
  560. if($type != 'code') $class .= ' '.$type;
  561. $this->doc .= "<pre class=\"$class $language\">" .
  562. p_xhtml_cached_geshi($text, $language, '', $options) .
  563. '</pre>' . DOKU_LF;
  564. }
  565. if($filename) {
  566. $this->doc .= '</dd></dl>'.DOKU_LF;
  567. }
  568. $this->_codeblock++;
  569. }
  570. /**
  571. * Format an acronym
  572. *
  573. * Uses $this->acronyms
  574. *
  575. * @param string $acronym
  576. */
  577. public function acronym($acronym) {
  578. if(array_key_exists($acronym, $this->acronyms)) {
  579. $title = $this->_xmlEntities($this->acronyms[$acronym]);
  580. $this->doc .= '<abbr title="'.$title
  581. .'">'.$this->_xmlEntities($acronym).'</abbr>';
  582. } else {
  583. $this->doc .= $this->_xmlEntities($acronym);
  584. }
  585. }
  586. /**
  587. * Format a smiley
  588. *
  589. * Uses $this->smiley
  590. *
  591. * @param string $smiley
  592. */
  593. public function smiley($smiley) {
  594. if (isset($this->smileys[$smiley])) {
  595. $this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] .
  596. '" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />';
  597. } else {
  598. $this->doc .= $this->_xmlEntities($smiley);
  599. }
  600. }
  601. /**
  602. * Format an entity
  603. *
  604. * Entities are basically small text replacements
  605. *
  606. * Uses $this->entities
  607. *
  608. * @param string $entity
  609. */
  610. public function entity($entity) {
  611. if(array_key_exists($entity, $this->entities)) {
  612. $this->doc .= $this->entities[$entity];
  613. } else {
  614. $this->doc .= $this->_xmlEntities($entity);
  615. }
  616. }
  617. /**
  618. * Typographically format a multiply sign
  619. *
  620. * Example: ($x=640, $y=480) should result in "640×480"
  621. *
  622. * @param string|int $x first value
  623. * @param string|int $y second value
  624. */
  625. public function multiplyentity($x, $y) {
  626. $this->doc .= "$x&times;$y";
  627. }
  628. /**
  629. * Render an opening single quote char (language specific)
  630. */
  631. public function singlequoteopening() {
  632. global $lang;
  633. $this->doc .= $lang['singlequoteopening'];
  634. }
  635. /**
  636. * Render a closing single quote char (language specific)
  637. */
  638. public function singlequoteclosing() {
  639. global $lang;
  640. $this->doc .= $lang['singlequoteclosing'];
  641. }
  642. /**
  643. * Render an apostrophe char (language specific)
  644. */
  645. public function apostrophe() {
  646. global $lang;
  647. $this->doc .= $lang['apostrophe'];
  648. }
  649. /**
  650. * Render an opening double quote char (language specific)
  651. */
  652. public function doublequoteopening() {
  653. global $lang;
  654. $this->doc .= $lang['doublequoteopening'];
  655. }
  656. /**
  657. * Render an closinging double quote char (language specific)
  658. */
  659. public function doublequoteclosing() {
  660. global $lang;
  661. $this->doc .= $lang['doublequoteclosing'];
  662. }
  663. /**
  664. * Render a CamelCase link
  665. *
  666. * @param string $link The link name
  667. * @param bool $returnonly whether to return html or write to doc attribute
  668. * @return void|string writes to doc attribute or returns html depends on $returnonly
  669. *
  670. * @see http://en.wikipedia.org/wiki/CamelCase
  671. */
  672. public function camelcaselink($link, $returnonly = false) {
  673. if($returnonly) {
  674. return $this->internallink($link, $link, null, true);
  675. } else {
  676. $this->internallink($link, $link);
  677. }
  678. }
  679. /**
  680. * Render a page local link
  681. *
  682. * @param string $hash hash link identifier
  683. * @param string $name name for the link
  684. * @param bool $returnonly whether to return html or write to doc attribute
  685. * @return void|string writes to doc attribute or returns html depends on $returnonly
  686. */
  687. public function locallink($hash, $name = null, $returnonly = false) {
  688. global $ID;
  689. $name = $this->_getLinkTitle($name, $hash, $isImage);
  690. $hash = $this->_headerToLink($hash);
  691. $title = $ID.' ↵';
  692. $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
  693. $doc .= $name;
  694. $doc .= '</a>';
  695. if($returnonly) {
  696. return $doc;
  697. } else {
  698. $this->doc .= $doc;
  699. }
  700. }
  701. /**
  702. * Render an internal Wiki Link
  703. *
  704. * $search,$returnonly & $linktype are not for the renderer but are used
  705. * elsewhere - no need to implement them in other renderers
  706. *
  707. * @author Andreas Gohr <andi@splitbrain.org>
  708. * @param string $id pageid
  709. * @param string|null $name link name
  710. * @param string|null $search adds search url param
  711. * @param bool $returnonly whether to return html or write to doc attribute
  712. * @param string $linktype type to set use of headings
  713. * @return void|string writes to doc attribute or returns html depends on $returnonly
  714. */
  715. public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
  716. global $conf;
  717. global $ID;
  718. global $INFO;
  719. $params = '';
  720. $parts = explode('?', $id, 2);
  721. if(count($parts) === 2) {
  722. $id = $parts[0];
  723. $params = $parts[1];
  724. }
  725. // For empty $id we need to know the current $ID
  726. // We need this check because _simpleTitle needs
  727. // correct $id and resolve_pageid() use cleanID($id)
  728. // (some things could be lost)
  729. if($id === '') {
  730. $id = $ID;
  731. }
  732. // default name is based on $id as given
  733. $default = $this->_simpleTitle($id);
  734. // now first resolve and clean up the $id
  735. $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
  736. $exists = page_exists($id, $this->date_at, false, true);
  737. $link = array();
  738. $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
  739. if(!$isImage) {
  740. if($exists) {
  741. $class = 'wikilink1';
  742. } else {
  743. $class = 'wikilink2';
  744. $link['rel'] = 'nofollow';
  745. }
  746. } else {
  747. $class = 'media';
  748. }
  749. //keep hash anchor
  750. list($id, $hash) = sexplode('#', $id, 2);
  751. if(!empty($hash)) $hash = $this->_headerToLink($hash);
  752. //prepare for formating
  753. $link['target'] = $conf['target']['wiki'];
  754. $link['style'] = '';
  755. $link['pre'] = '';
  756. $link['suf'] = '';
  757. $link['more'] = 'data-wiki-id="'.$id.'"'; // id is already cleaned
  758. $link['class'] = $class;
  759. if($this->date_at) {
  760. $params = $params.'&at='.rawurlencode($this->date_at);
  761. }
  762. $link['url'] = wl($id, $params);
  763. $link['name'] = $name;
  764. $link['title'] = $id;
  765. //add search string
  766. if($search) {
  767. ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
  768. if(is_array($search)) {
  769. $search = array_map('rawurlencode', $search);
  770. $link['url'] .= 's[]='.join('&amp;s[]=', $search);
  771. } else {
  772. $link['url'] .= 's='.rawurlencode($search);
  773. }
  774. }
  775. //keep hash
  776. if($hash) $link['url'] .= '#'.$hash;
  777. //output formatted
  778. if($returnonly) {
  779. return $this->_formatLink($link);
  780. } else {
  781. $this->doc .= $this->_formatLink($link);
  782. }
  783. }
  784. /**
  785. * Render an external link
  786. *
  787. * @param string $url full URL with scheme
  788. * @param string|array $name name for the link, array for media file
  789. * @param bool $returnonly whether to return html or write to doc attribute
  790. * @return void|string writes to doc attribute or returns html depends on $returnonly
  791. */
  792. public function externallink($url, $name = null, $returnonly = false) {
  793. global $conf;
  794. $name = $this->_getLinkTitle($name, $url, $isImage);
  795. // url might be an attack vector, only allow registered protocols
  796. if(is_null($this->schemes)) $this->schemes = getSchemes();
  797. list($scheme) = explode('://', $url);
  798. $scheme = strtolower($scheme);
  799. if(!in_array($scheme, $this->schemes)) $url = '';
  800. // is there still an URL?
  801. if(!$url) {
  802. if($returnonly) {
  803. return $name;
  804. } else {
  805. $this->doc .= $name;
  806. }
  807. return;
  808. }
  809. // set class
  810. if(!$isImage) {
  811. $class = 'urlextern';
  812. } else {
  813. $class = 'media';
  814. }
  815. //prepare for formating
  816. $link = array();
  817. $link['target'] = $conf['target']['extern'];
  818. $link['style'] = '';
  819. $link['pre'] = '';
  820. $link['suf'] = '';
  821. $link['more'] = '';
  822. $link['class'] = $class;
  823. $link['url'] = $url;
  824. $link['rel'] = '';
  825. $link['name'] = $name;
  826. $link['title'] = $this->_xmlEntities($url);
  827. if($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
  828. if($conf['target']['extern']) $link['rel'] .= ' noopener';
  829. //output formatted
  830. if($returnonly) {
  831. return $this->_formatLink($link);
  832. } else {
  833. $this->doc .= $this->_formatLink($link);
  834. }
  835. }
  836. /**
  837. * Render an interwiki link
  838. *
  839. * You may want to use $this->_resolveInterWiki() here
  840. *
  841. * @param string $match original link - probably not much use
  842. * @param string|array $name name for the link, array for media file
  843. * @param string $wikiName indentifier (shortcut) for the remote wiki
  844. * @param string $wikiUri the fragment parsed from the original link
  845. * @param bool $returnonly whether to return html or write to doc attribute
  846. * @return void|string writes to doc attribute or returns html depends on $returnonly
  847. */
  848. public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) {
  849. global $conf;
  850. $link = array();
  851. $link['target'] = $conf['target']['interwiki'];
  852. $link['pre'] = '';
  853. $link['suf'] = '';
  854. $link['more'] = '';
  855. $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage);
  856. $link['rel'] = '';
  857. //get interwiki URL
  858. $exists = null;
  859. $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
  860. if(!$isImage) {
  861. $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
  862. $link['class'] = "interwiki iw_$class";
  863. } else {
  864. $link['class'] = 'media';
  865. }
  866. //do we stay at the same server? Use local target
  867. if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
  868. $link['target'] = $conf['target']['wiki'];
  869. }
  870. if($exists !== null && !$isImage) {
  871. if($exists) {
  872. $link['class'] .= ' wikilink1';
  873. } else {
  874. $link['class'] .= ' wikilink2';
  875. $link['rel'] .= ' nofollow';
  876. }
  877. }
  878. if($conf['target']['interwiki']) $link['rel'] .= ' noopener';
  879. $link['url'] = $url;
  880. $link['title'] = $this->_xmlEntities($link['url']);
  881. // output formatted
  882. if($returnonly) {
  883. if($url == '') return $link['name'];
  884. return $this->_formatLink($link);
  885. } else {
  886. if($url == '') $this->doc .= $link['name'];
  887. else $this->doc .= $this->_formatLink($link);
  888. }
  889. }
  890. /**
  891. * Link to windows share
  892. *
  893. * @param string $url the link
  894. * @param string|array $name name for the link, array for media file
  895. * @param bool $returnonly whether to return html or write to doc attribute
  896. * @return void|string writes to doc attribute or returns html depends on $returnonly
  897. */
  898. public function windowssharelink($url, $name = null, $returnonly = false) {
  899. global $conf;
  900. //simple setup
  901. $link = array();
  902. $link['target'] = $conf['target']['windows'];
  903. $link['pre'] = '';
  904. $link['suf'] = '';
  905. $link['style'] = '';
  906. $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
  907. if(!$isImage) {
  908. $link['class'] = 'windows';
  909. } else {
  910. $link['class'] = 'media';
  911. }
  912. $link['title'] = $this->_xmlEntities($url);
  913. $url = str_replace('\\', '/', $url);
  914. $url = 'file:///'.$url;
  915. $link['url'] = $url;
  916. //output formatted
  917. if($returnonly) {
  918. return $this->_formatLink($link);
  919. } else {
  920. $this->doc .= $this->_formatLink($link);
  921. }
  922. }
  923. /**
  924. * Render a linked E-Mail Address
  925. *
  926. * Honors $conf['mailguard'] setting
  927. *
  928. * @param string $address Email-Address
  929. * @param string|array $name name for the link, array for media file
  930. * @param bool $returnonly whether to return html or write to doc attribute
  931. * @return void|string writes to doc attribute or returns html depends on $returnonly
  932. */
  933. public function emaillink($address, $name = null, $returnonly = false) {
  934. global $conf;
  935. //simple setup
  936. $link = array();
  937. $link['target'] = '';
  938. $link['pre'] = '';
  939. $link['suf'] = '';
  940. $link['style'] = '';
  941. $link['more'] = '';
  942. $name = $this->_getLinkTitle($name, '', $isImage);
  943. if(!$isImage) {
  944. $link['class'] = 'mail';
  945. } else {
  946. $link['class'] = 'media';
  947. }
  948. $address = $this->_xmlEntities($address);
  949. $address = obfuscate($address);
  950. $title = $address;
  951. if(empty($name)) {
  952. $name = $address;
  953. }
  954. if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
  955. $link['url'] = 'mailto:'.$address;
  956. $link['name'] = $name;
  957. $link['title'] = $title;
  958. //output formatted
  959. if($returnonly) {
  960. return $this->_formatLink($link);
  961. } else {
  962. $this->doc .= $this->_formatLink($link);
  963. }
  964. }
  965. /**
  966. * Render an internal media file
  967. *
  968. * @param string $src media ID
  969. * @param string $title descriptive text
  970. * @param string $align left|center|right
  971. * @param int $width width of media in pixel
  972. * @param int $height height of media in pixel
  973. * @param string $cache cache|recache|nocache
  974. * @param string $linking linkonly|detail|nolink
  975. * @param bool $return return HTML instead of adding to $doc
  976. * @return void|string writes to doc attribute or returns html depends on $return
  977. */
  978. public function internalmedia($src, $title = null, $align = null, $width = null,
  979. $height = null, $cache = null, $linking = null, $return = false) {
  980. global $ID;
  981. if (strpos($src, '#') !== false) {
  982. list($src, $hash) = sexplode('#', $src, 2);
  983. }
  984. $src = (new MediaResolver($ID))->resolveId($src,$this->date_at,true);
  985. $exists = media_exists($src);
  986. $noLink = false;
  987. $render = ($linking == 'linkonly') ? false : true;
  988. $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
  989. list($ext, $mime) = mimetype($src, false);
  990. if(substr($mime, 0, 5) == 'image' && $render) {
  991. $link['url'] = ml(
  992. $src,
  993. array(
  994. 'id' => $ID,
  995. 'cache' => $cache,
  996. 'rev' => $this->_getLastMediaRevisionAt($src)
  997. ),
  998. ($linking == 'direct')
  999. );
  1000. } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
  1001. // don't link movies
  1002. $noLink = true;
  1003. } else {
  1004. // add file icons
  1005. $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
  1006. $link['class'] .= ' mediafile mf_'.$class;
  1007. $link['url'] = ml(
  1008. $src,
  1009. array(
  1010. 'id' => $ID,
  1011. 'cache' => $cache,
  1012. 'rev' => $this->_getLastMediaRevisionAt($src)
  1013. ),
  1014. true
  1015. );
  1016. if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
  1017. }
  1018. if (!empty($hash)) $link['url'] .= '#'.$hash;
  1019. //markup non existing files
  1020. if(!$exists) {
  1021. $link['class'] .= ' wikilink2';
  1022. }
  1023. //output formatted
  1024. if($return) {
  1025. if($linking == 'nolink' || $noLink) return $link['name'];
  1026. else return $this->_formatLink($link);
  1027. } else {
  1028. if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
  1029. else $this->doc .= $this->_formatLink($link);
  1030. }
  1031. }
  1032. /**
  1033. * Render an external media file
  1034. *
  1035. * @param string $src full media URL
  1036. * @param string $title descriptive text
  1037. * @param string $align left|center|right
  1038. * @param int $width width of media in pixel
  1039. * @param int $height height of media in pixel
  1040. * @param string $cache cache|recache|nocache
  1041. * @param string $linking linkonly|detail|nolink
  1042. * @param bool $return return HTML instead of adding to $doc
  1043. * @return void|string writes to doc attribute or returns html depends on $return
  1044. */
  1045. public function externalmedia($src, $title = null, $align = null, $width = null,
  1046. $height = null, $cache = null, $linking = null, $return = false) {
  1047. if(link_isinterwiki($src)){
  1048. list($shortcut, $reference) = sexplode('>', $src, 2, '');
  1049. $exists = null;
  1050. $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
  1051. if($src == '' && empty($title)){
  1052. // make sure at least something will be shown in this case
  1053. $title = $reference;
  1054. }
  1055. }
  1056. list($src, $hash) = sexplode('#', $src, 2);
  1057. $noLink = false;
  1058. if($src == '') {
  1059. // only output plaintext without link if there is no src
  1060. $noLink = true;
  1061. }
  1062. $render = ($linking == 'linkonly') ? false : true;
  1063. $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
  1064. $link['url'] = ml($src, array('cache' => $cache));
  1065. list($ext, $mime) = mimetype($src, false);
  1066. if(substr($mime, 0, 5) == 'image' && $render) {
  1067. // link only jpeg images
  1068. // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
  1069. } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
  1070. // don't link movies
  1071. $noLink = true;
  1072. } else {
  1073. // add file icons
  1074. $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
  1075. $link['class'] .= ' mediafile mf_'.$class;
  1076. }
  1077. if($hash) $link['url'] .= '#'.$hash;
  1078. //output formatted
  1079. if($return) {
  1080. if($linking == 'nolink' || $noLink) return $link['name'];
  1081. else return $this->_formatLink($link);
  1082. } else {
  1083. if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
  1084. else $this->doc .= $this->_formatLink($link);
  1085. }
  1086. }
  1087. /**
  1088. * Renders an RSS feed
  1089. *
  1090. * @param string $url URL of the feed
  1091. * @param array $params Finetuning of the output
  1092. *
  1093. * @author Andreas Gohr <andi@splitbrain.org>
  1094. */
  1095. public function rss($url, $params) {
  1096. global $lang;
  1097. global $conf;
  1098. require_once(DOKU_INC.'inc/FeedParser.php');
  1099. $feed = new FeedParser();
  1100. $feed->set_feed_url($url);
  1101. //disable warning while fetching
  1102. if(!defined('DOKU_E_LEVEL')) {
  1103. $elvl = error_reporting(E_ERROR);
  1104. }
  1105. $rc = $feed->init();
  1106. if(isset($elvl)) {
  1107. error_reporting($elvl);
  1108. }
  1109. if($params['nosort']) $feed->enable_order_by_date(false);
  1110. //decide on start and end
  1111. if($params['reverse']) {
  1112. $mod = -1;
  1113. $start = $feed->get_item_quantity() - 1;
  1114. $end = $start - ($params['max']);
  1115. $end = ($end < -1) ? -1 : $end;
  1116. } else {
  1117. $mod = 1;
  1118. $start = 0;
  1119. $end = $feed->get_item_quantity();
  1120. $end = ($end > $params['max']) ? $params['max'] : $end;
  1121. }
  1122. $this->doc .= '<ul class="rss">';
  1123. if($rc) {
  1124. for($x = $start; $x != $end; $x += $mod) {
  1125. $item = $feed->get_item($x);
  1126. $this->doc .= '<li><div class="li">';
  1127. $lnkurl = $item->get_permalink();
  1128. $title = html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8');
  1129. // support feeds without links
  1130. if($lnkurl) {
  1131. $this->externallink($item->get_permalink(), $title);
  1132. } else {
  1133. $this->doc .= ' '.hsc($item->get_title());
  1134. }
  1135. if($params['author']) {
  1136. $author = $item->get_author(0);
  1137. if($author) {
  1138. $name = $author->get_name();
  1139. if(!$name) $name = $author->get_email();
  1140. if($name) $this->doc .= ' '.$lang['by'].' '.hsc($name);
  1141. }
  1142. }
  1143. if($params['date']) {
  1144. $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
  1145. }
  1146. if($params['details']) {
  1147. $desc = $item->get_description();
  1148. $desc = strip_tags($desc);
  1149. $desc = html_entity_decode($desc, ENT_QUOTES, 'UTF-8');
  1150. $this->doc .= '<div class="detail">';
  1151. $this->doc .= hsc($desc);
  1152. $this->doc .= '</div>';
  1153. }
  1154. $this->doc .= '</div></li>';
  1155. }
  1156. } else {
  1157. $this->doc .= '<li><div class="li">';
  1158. $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
  1159. $this->externallink($url);
  1160. if($conf['allowdebug']) {
  1161. $this->doc .= '<!--'.hsc($feed->error).'-->';
  1162. }
  1163. $this->doc .= '</div></li>';
  1164. }
  1165. $this->doc .= '</ul>';
  1166. }
  1167. /**
  1168. * Start a table
  1169. *
  1170. * @param int $maxcols maximum number of columns
  1171. * @param int $numrows NOT IMPLEMENTED
  1172. * @param int $pos byte position in the original source
  1173. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  1174. */
  1175. public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) {
  1176. // initialize the row counter used for classes
  1177. $this->_counter['row_counter'] = 0;
  1178. $class = 'table';
  1179. if($classes !== null) {
  1180. if(is_array($classes)) $classes = join(' ', $classes);
  1181. $class .= ' ' . $classes;
  1182. }
  1183. if($pos !== null) {
  1184. $hid = $this->_headerToLink($class, true);
  1185. $data = array();
  1186. $data['target'] = 'table';
  1187. $data['name'] = '';
  1188. $data['hid'] = $hid;
  1189. $class .= ' '.$this->startSectionEdit($pos, $data);
  1190. }
  1191. $this->doc .= '<div class="'.$class.'"><table class="inline">'.
  1192. DOKU_LF;
  1193. }
  1194. /**
  1195. * Close a table
  1196. *
  1197. * @param int $pos byte position in the original source
  1198. */
  1199. public function table_close($pos = null) {
  1200. $this->doc .= '</table></div>'.DOKU_LF;
  1201. if($pos !== null) {
  1202. $this->finishSectionEdit($pos);
  1203. }
  1204. }
  1205. /**
  1206. * Open a table header
  1207. */
  1208. public function tablethead_open() {
  1209. $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
  1210. }
  1211. /**
  1212. * Close a table header
  1213. */
  1214. public function tablethead_close() {
  1215. $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
  1216. }
  1217. /**
  1218. * Open a table body
  1219. */
  1220. public function tabletbody_open() {
  1221. $this->doc .= DOKU_TAB.'<tbody>'.DOKU_LF;
  1222. }
  1223. /**
  1224. * Close a table body
  1225. */
  1226. public function tabletbody_close() {
  1227. $this->doc .= DOKU_TAB.'</tbody>'.DOKU_LF;
  1228. }
  1229. /**
  1230. * Open a table footer
  1231. */
  1232. public function tabletfoot_open() {
  1233. $this->doc .= DOKU_TAB.'<tfoot>'.DOKU_LF;
  1234. }
  1235. /**
  1236. * Close a table footer
  1237. */
  1238. public function tabletfoot_close() {
  1239. $this->doc .= DOKU_TAB.'</tfoot>'.DOKU_LF;
  1240. }
  1241. /**
  1242. * Open a table row
  1243. *
  1244. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  1245. */
  1246. public function tablerow_open($classes = null) {
  1247. // initialize the cell counter used for classes
  1248. $this->_counter['cell_counter'] = 0;
  1249. $class = 'row'.$this->_counter['row_counter']++;
  1250. if($classes !== null) {
  1251. if(is_array($classes)) $classes = join(' ', $classes);
  1252. $class .= ' ' . $classes;
  1253. }
  1254. $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
  1255. }
  1256. /**
  1257. * Close a table row
  1258. */
  1259. public function tablerow_close() {
  1260. $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
  1261. }
  1262. /**
  1263. * Open a table header cell
  1264. *
  1265. * @param int $colspan
  1266. * @param string $align left|center|right
  1267. * @param int $rowspan
  1268. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  1269. */
  1270. public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
  1271. $class = 'class="col'.$this->_counter['cell_counter']++;
  1272. if(!is_null($align)) {
  1273. $class .= ' '.$align.'align';
  1274. }
  1275. if($classes !== null) {
  1276. if(is_array($classes)) $classes = join(' ', $classes);
  1277. $class .= ' ' . $classes;
  1278. }
  1279. $class .= '"';
  1280. $this->doc .= '<th '.$class;
  1281. if($colspan > 1) {
  1282. $this->_counter['cell_counter'] += $colspan - 1;
  1283. $this->doc .= ' colspan="'.$colspan.'"';
  1284. }
  1285. if($rowspan > 1) {
  1286. $this->doc .= ' rowspan="'.$rowspan.'"';
  1287. }
  1288. $this->doc .= '>';
  1289. }
  1290. /**
  1291. * Close a table header cell
  1292. */
  1293. public function tableheader_close() {
  1294. $this->doc .= '</th>';
  1295. }
  1296. /**
  1297. * Open a table cell
  1298. *
  1299. * @param int $colspan
  1300. * @param string $align left|center|right
  1301. * @param int $rowspan
  1302. * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
  1303. */
  1304. public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
  1305. $class = 'class="col'.$this->_counter['cell_counter']++;
  1306. if(!is_null($align)) {
  1307. $class .= ' '.$align.'align';
  1308. }
  1309. if($classes !== null) {
  1310. if(is_array($classes)) $classes = join(' ', $classes);
  1311. $class .= ' ' . $classes;
  1312. }
  1313. $class .= '"';
  1314. $this->doc .= '<td '.$class;
  1315. if($colspan > 1) {
  1316. $this->_counter['cell_counter'] += $colspan - 1;
  1317. $this->doc .= ' colspan="'.$colspan.'"';
  1318. }
  1319. if($rowspan > 1) {
  1320. $this->doc .= ' rowspan="'.$rowspan.'"';
  1321. }
  1322. $this->doc .= '>';
  1323. }
  1324. /**
  1325. * Close a table cell
  1326. */
  1327. public function tablecell_close() {
  1328. $this->doc .= '</td>';
  1329. }
  1330. /**
  1331. * Returns the current header level.
  1332. * (required e.g. by the filelist plugin)
  1333. *
  1334. * @return int The current header level
  1335. */
  1336. public function getLastlevel() {
  1337. return $this->lastlevel;
  1338. }
  1339. #region Utility functions
  1340. /**
  1341. * Build a link
  1342. *
  1343. * Assembles all parts defined in $link returns HTML for the link
  1344. *
  1345. * @param array $link attributes of a link
  1346. * @return string
  1347. *
  1348. * @author Andreas Gohr <andi@splitbrain.org>
  1349. */
  1350. public function _formatLink($link) {
  1351. //make sure the url is XHTML compliant (skip mailto)
  1352. if(substr($link['url'], 0, 7) != 'mailto:') {
  1353. $link['url'] = str_replace('&', '&amp;', $link['url']);
  1354. $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
  1355. }
  1356. //remove double encodings in titles
  1357. $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
  1358. // be sure there are no bad chars in url or title
  1359. // (we can't do this for name because it can contain an img tag)
  1360. $link['url'] = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
  1361. $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
  1362. $ret = '';
  1363. $ret .= $link['pre'];
  1364. $ret .= '<a href="'.$link['url'].'"';
  1365. if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
  1366. if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
  1367. if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
  1368. if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
  1369. if(!empty($link['rel'])) $ret .= ' rel="'.trim($link['rel']).'"';
  1370. if(!empty($link['more'])) $ret .= ' '.$link['more'];
  1371. $ret .= '>';
  1372. $ret .= $link['name'];
  1373. $ret .= '</a>';
  1374. $ret .= $link['suf'];
  1375. return $ret;
  1376. }
  1377. /**
  1378. * Renders internal and external media
  1379. *
  1380. * @author Andreas Gohr <andi@splitbrain.org>
  1381. * @param string $src media ID
  1382. * @param string $title descriptive text
  1383. * @param string $align left|center|right
  1384. * @param int $width width of media in pixel
  1385. * @param int $height height of media in pixel
  1386. * @param string $cache cache|recache|nocache
  1387. * @param bool $render should the media be embedded inline or just linked
  1388. * @return string
  1389. */
  1390. public function _media($src, $title = null, $align = null, $width = null,
  1391. $height = null, $cache = null, $render = true) {
  1392. $ret = '';
  1393. list($ext, $mime) = mimetype($src);
  1394. if(substr($mime, 0, 5) == 'image') {
  1395. // first get the $title
  1396. if(!is_null($title)) {
  1397. $title = $this->_xmlEntities($title);
  1398. } elseif($ext == 'jpg' || $ext == 'jpeg') {
  1399. //try to use the caption from IPTC/EXIF
  1400. require_once(DOKU_INC.'inc/JpegMeta.php');
  1401. $jpeg = new JpegMeta(mediaFN($src));
  1402. if($jpeg !== false) $cap = $jpeg->getTitle();
  1403. if(!empty($cap)) {
  1404. $title = $this->_xmlEntities($cap);
  1405. }
  1406. }
  1407. if(!$render) {
  1408. // if the picture is not supposed to be rendered
  1409. // return the title of the picture
  1410. if($title === null || $title === "") {
  1411. // just show the sourcename
  1412. $title = $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
  1413. }
  1414. return $title;
  1415. }
  1416. //add image tag
  1417. $ret .= '<img src="' . ml(
  1418. $src,
  1419. array(
  1420. 'w' => $width, 'h' => $height,
  1421. 'cache' => $cache,
  1422. 'rev' => $this->_getLastMediaRevisionAt($src)
  1423. )
  1424. ) . '"';
  1425. $ret .= ' class="media'.$align.'"';
  1426. $ret .= ' loading="lazy"';
  1427. if($title) {
  1428. $ret .= ' title="'.$title.'"';
  1429. $ret .= ' alt="'.$title.'"';
  1430. } else {
  1431. $ret .= ' alt=""';
  1432. }
  1433. if(!is_null($width))
  1434. $ret .= ' width="'.$this->_xmlEntities($width).'"';
  1435. if(!is_null($height))
  1436. $ret .= ' height="'.$this->_xmlEntities($height).'"';
  1437. $ret .= ' />';
  1438. } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
  1439. // first get the $title
  1440. $title = !is_null($title) ? $title : false;
  1441. if(!$render) {
  1442. // if the file is not supposed to be rendered
  1443. // return the title of the file (just the sourcename if there is no title)
  1444. return $this->_xmlEntities($title ? $title : \dokuwiki\Utf8\PhpString::basename(noNS($src)));
  1445. }
  1446. $att = array();
  1447. $att['class'] = "media$align";
  1448. if($title) {
  1449. $att['title'] = $title;
  1450. }
  1451. if(media_supportedav($mime, 'video')) {
  1452. //add video
  1453. $ret .= $this->_video($src, $width, $height, $att);
  1454. }
  1455. if(media_supportedav($mime, 'audio')) {
  1456. //add audio
  1457. $ret .= $this->_audio($src, $att);
  1458. }
  1459. } elseif($mime == 'application/x-shockwave-flash') {
  1460. if(!$render) {
  1461. // if the flash is not supposed to be rendered
  1462. // return the title of the flash
  1463. if(!$title) {
  1464. // just show the sourcename
  1465. $title = \dokuwiki\Utf8\PhpString::basename(noNS($src));
  1466. }
  1467. return $this->_xmlEntities($title);
  1468. }
  1469. $att = array();
  1470. $att['class'] = "media$align";
  1471. if($align == 'right') $att['align'] = 'right';
  1472. if($align == 'left') $att['align'] = 'left';
  1473. $ret .= html_flashobject(
  1474. ml($src, array('cache' => $cache), true, '&'), $width, $height,
  1475. array('quality' => 'high'),
  1476. null,
  1477. $att,
  1478. $this->_xmlEntities($title)
  1479. );
  1480. } elseif($title) {
  1481. // well at least we have a title to display
  1482. $ret .= $this->_xmlEntities($title);
  1483. } else {
  1484. // just show the sourcename
  1485. $ret .= $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
  1486. }
  1487. return $ret;
  1488. }
  1489. /**
  1490. * Escape string for output
  1491. *
  1492. * @param $string
  1493. * @return string
  1494. */
  1495. public function _xmlEntities($string) {
  1496. return hsc($string);
  1497. }
  1498. /**
  1499. * Construct a title and handle images in titles
  1500. *
  1501. * @author Harry Fuecks <hfuecks@gmail.com>
  1502. * @param string|array $title either string title or media array
  1503. * @param string $default default title if nothing else is found
  1504. * @param bool $isImage will be set to true if it's a media file
  1505. * @param null|string $id linked page id (used to extract title from first heading)
  1506. * @param string $linktype content|navigation
  1507. * @return string HTML of the title, might be full image tag or just escaped text
  1508. */
  1509. public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
  1510. $isImage = false;
  1511. if(is_array($title)) {
  1512. $isImage = true;
  1513. return $this->_imageTitle($title);
  1514. } elseif(is_null($title) || trim($title) == '') {
  1515. if(useHeading($linktype) && $id) {
  1516. $heading = p_get_first_heading($id);
  1517. if(!blank($heading)) {
  1518. return $this->_xmlEntities($heading);
  1519. }
  1520. }
  1521. return $this->_xmlEntities($default);
  1522. } else {
  1523. return $this->_xmlEntities($title);
  1524. }
  1525. }
  1526. /**
  1527. * Returns HTML code for images used in link titles
  1528. *
  1529. * @author Andreas Gohr <andi@splitbrain.org>
  1530. * @param array $img
  1531. * @return string HTML img tag or similar
  1532. */
  1533. public function _imageTitle($img) {
  1534. global $ID;
  1535. // some fixes on $img['src']
  1536. // see internalmedia() and externalmedia()
  1537. list($img['src']) = explode('#', $img['src'], 2);
  1538. if($img['type'] == 'internalmedia') {
  1539. $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true);
  1540. }
  1541. return $this->_media(
  1542. $img['src'],
  1543. $img['title'],
  1544. $img['align'],
  1545. $img['width'],
  1546. $img['height'],
  1547. $img['cache']
  1548. );
  1549. }
  1550. /**
  1551. * helperfunction to return a basic link to a media
  1552. *
  1553. * used in internalmedia() and externalmedia()
  1554. *
  1555. * @author Pierre Spring <pierre.spring@liip.ch>
  1556. * @param string $src media ID
  1557. * @param string $title descriptive text
  1558. * @param string $align left|center|right
  1559. * @param int $width width of media in pixel
  1560. * @param int $height height of media in pixel
  1561. * @param string $cache cache|recache|nocache
  1562. * @param bool $render should the media be embedded inline or just linked
  1563. * @return array associative array with link config
  1564. */
  1565. public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
  1566. global $conf;
  1567. $link = array();
  1568. $link['class'] = 'media';
  1569. $link['style'] = '';
  1570. $link['pre'] = '';
  1571. $link['suf'] = '';
  1572. $link['more'] = '';
  1573. $link['target'] = $conf['target']['media'];
  1574. if($conf['target']['media']) $link['rel'] = 'noopener';
  1575. $link['title'] = $this->_xmlEntities($src);
  1576. $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render);
  1577. return $link;
  1578. }
  1579. /**
  1580. * Embed video(s) in HTML
  1581. *
  1582. * @author Anika Henke <anika@selfthinker.org>
  1583. * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
  1584. *
  1585. * @param string $src - ID of video to embed
  1586. * @param int $width - width of the video in pixels
  1587. * @param int $height - height of the video in pixels
  1588. * @param array $atts - additional attributes for the <video> tag
  1589. * @return string
  1590. */
  1591. public function _video($src, $width, $height, $atts = null) {
  1592. // prepare width and height
  1593. if(is_null($atts)) $atts = array();
  1594. $atts['width'] = (int) $width;
  1595. $atts['height'] = (int) $height;
  1596. if(!$atts['width']) $atts['width'] = 320;
  1597. if(!$atts['height']) $atts['height'] = 240;
  1598. $posterUrl = '';
  1599. $files = array();
  1600. $tracks = array();
  1601. $isExternal = media_isexternal($src);
  1602. if ($isExternal) {
  1603. // take direct source for external files
  1604. list(/*ext*/, $srcMime) = mimetype($src);
  1605. $files[$srcMime] = $src;
  1606. } else {
  1607. // prepare alternative formats
  1608. $extensions = array('webm', 'ogv', 'mp4');
  1609. $files = media_alternativefiles($src, $extensions);
  1610. $poster = media_alternativefiles($src, array('jpg', 'png'));
  1611. $tracks = media_trackfiles($src);
  1612. if(!empty($poster)) {
  1613. $posterUrl = ml(reset($poster), '', true, '&');
  1614. }
  1615. }
  1616. $out = '';
  1617. // open video tag
  1618. $out .= '<video '.buildAttributes($atts).' controls="controls"';
  1619. if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
  1620. $out .= '>'.NL;
  1621. $fallback = '';
  1622. // output source for each alternative video format
  1623. foreach($files as $mime => $file) {
  1624. if ($isExternal) {
  1625. $url = $file;
  1626. $linkType = 'externalmedia';
  1627. } else {
  1628. $url = ml($file, '', true, '&');
  1629. $linkType = 'internalmedia';
  1630. }
  1631. $title = !empty($atts['title'])
  1632. ? $atts['title']
  1633. : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
  1634. $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
  1635. // alternative content (just a link to the file)
  1636. $fallback .= $this->$linkType(
  1637. $file,
  1638. $title,
  1639. null,
  1640. null,
  1641. null,
  1642. $cache = null,
  1643. $linking = 'linkonly',
  1644. $return = true
  1645. );
  1646. }
  1647. // output each track if any
  1648. foreach( $tracks as $trackid => $info ) {
  1649. list( $kind, $srclang ) = array_map( 'hsc', $info );
  1650. $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
  1651. $out .= "label=\"$srclang\" ";
  1652. $out .= 'src="'.ml($trackid, '', true).'">'.NL;
  1653. }
  1654. // finish
  1655. $out .= $fallback;
  1656. $out .= '</video>'.NL;
  1657. return $out;
  1658. }
  1659. /**
  1660. * Embed audio in HTML
  1661. *
  1662. * @author Anika Henke <anika@selfthinker.org>
  1663. *
  1664. * @param string $src - ID of audio to embed
  1665. * @param array $atts - additional attributes for the <audio> tag
  1666. * @return string
  1667. */
  1668. public function _audio($src, $atts = array()) {
  1669. $files = array();
  1670. $isExternal = media_isexternal($src);
  1671. if ($isExternal) {
  1672. // take direct source for external files
  1673. list(/*ext*/, $srcMime) = mimetype($src);
  1674. $files[$srcMime] = $src;
  1675. } else {
  1676. // prepare alternative formats
  1677. $extensions = array('ogg', 'mp3', 'wav');
  1678. $files = media_alternativefiles($src, $extensions);
  1679. }
  1680. $out = '';
  1681. // open audio tag
  1682. $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
  1683. $fallback = '';
  1684. // output source for each alternative audio format
  1685. foreach($files as $mime => $file) {
  1686. if ($isExternal) {
  1687. $url = $file;
  1688. $linkType = 'externalmedia';
  1689. } else {
  1690. $url = ml($file, '', true, '&');
  1691. $linkType = 'internalmedia';
  1692. }
  1693. $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
  1694. $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
  1695. // alternative content (just a link to the file)
  1696. $fallback .= $this->$linkType(
  1697. $file,
  1698. $title,
  1699. null,
  1700. null,
  1701. null,
  1702. $cache = null,
  1703. $linking = 'linkonly',
  1704. $return = true
  1705. );
  1706. }
  1707. // finish
  1708. $out .= $fallback;
  1709. $out .= '</audio>'.NL;
  1710. return $out;
  1711. }
  1712. /**
  1713. * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
  1714. * which returns an existing media revision less or equal to rev or date_at
  1715. *
  1716. * @author lisps
  1717. * @param string $media_id
  1718. * @access protected
  1719. * @return string revision ('' for current)
  1720. */
  1721. protected function _getLastMediaRevisionAt($media_id) {
  1722. if (!$this->date_at || media_isexternal($media_id)) return '';
  1723. $changelog = new MediaChangeLog($media_id);
  1724. return $changelog->getLastRevisionAt($this->date_at);
  1725. }
  1726. #endregion
  1727. }
  1728. //Setup VIM: ex: et ts=4 :