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