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
|
<?php
/**
* Common DokuWiki functions
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
use dokuwiki\Cache\CacheInstructions;
use dokuwiki\Cache\CacheRenderer;
use dokuwiki\ChangeLog\PageChangeLog;
use dokuwiki\File\PageFile;
use dokuwiki\Logger;
use dokuwiki\Subscriptions\PageSubscriptionSender;
use dokuwiki\Subscriptions\SubscriberManager;
use dokuwiki\Extension\AuthPlugin;
use dokuwiki\Extension\Event;
/**
* Wrapper around htmlspecialchars()
*
* @author Andreas Gohr <andi@splitbrain.org>
* @see htmlspecialchars()
*
* @param string $string the string being converted
* @return string converted string
*/
function hsc($string) {
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
}
/**
* Checks if the given input is blank
*
* This is similar to empty() but will return false for "0".
*
* Please note: when you pass uninitialized variables, they will implicitly be created
* with a NULL value without warning.
*
* To avoid this it's recommended to guard the call with isset like this:
*
* (isset($foo) && !blank($foo))
* (!isset($foo) || blank($foo))
*
* @param $in
* @param bool $trim Consider a string of whitespace to be blank
* @return bool
*/
function blank(&$in, $trim = false) {
if(is_null($in)) return true;
if(is_array($in)) return empty($in);
if($in === "\0") return true;
if($trim && trim($in) === '') return true;
if(strlen($in) > 0) return false;
return empty($in);
}
/**
* print a newline terminated string
*
* You can give an indention as optional parameter
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $string line of text
* @param int $indent number of spaces indention
*/
function ptln($string, $indent = 0) {
echo str_repeat(' ', $indent)."$string\n";
}
/**
* strips control characters (<32) from the given string
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $string being stripped
* @return string
*/
function stripctl($string) {
return preg_replace('/[\x00-\x1F]+/s', '', $string);
}
/**
* Return a secret token to be used for CSRF attack prevention
*
* @author Andreas Gohr <andi@splitbrain.org>
* @link http://en.wikipedia.org/wiki/Cross-site_request_forgery
* @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html
*
* @return string
*/
function getSecurityToken() {
/** @var Input $INPUT */
global $INPUT;
$user = $INPUT->server->str('REMOTE_USER');
$session = session_id();
// CSRF checks are only for logged in users - do not generate for anonymous
if(trim($user) == '' || trim($session) == '') return '';
return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt());
}
/**
* Check the secret CSRF token
*
* @param null|string $token security token or null to read it from request variable
* @return bool success if the token matched
*/
function checkSecurityToken($token = null) {
/** @var Input $INPUT */
global $INPUT;
if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check
if(is_null($token)) $token = $INPUT->str('sectok');
if(getSecurityToken() != $token) {
msg('Security Token did not match. Possible CSRF attack.', -1);
return false;
}
return true;
}
/**
* Print a hidden form field with a secret CSRF token
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param bool $print if true print the field, otherwise html of the field is returned
* @return string html of hidden form field
*/
function formSecurityToken($print = true) {
$ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n";
if($print) echo $ret;
return $ret;
}
/**
* Determine basic information for a request of $id
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Chris Smith <chris@jalakai.co.uk>
*
* @param string $id pageid
* @param bool $htmlClient add info about whether is mobile browser
* @return array with info for a request of $id
*
*/
function basicinfo($id, $htmlClient=true){
global $USERINFO;
/* @var Input $INPUT */
global $INPUT;
// set info about manager/admin status.
$info = array();
$info['isadmin'] = false;
$info['ismanager'] = false;
if($INPUT->server->has('REMOTE_USER')) {
$info['userinfo'] = $USERINFO;
$info['perm'] = auth_quickaclcheck($id);
$info['client'] = $INPUT->server->str('REMOTE_USER');
if($info['perm'] == AUTH_ADMIN) {
$info['isadmin'] = true;
$info['ismanager'] = true;
} elseif(auth_ismanager()) {
$info['ismanager'] = true;
}
// if some outside auth were used only REMOTE_USER is set
if(empty($info['userinfo']['name'])) {
$info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER');
}
} else {
$info['perm'] = auth_aclcheck($id, '', null);
$info['client'] = clientIP(true);
}
$info['namespace'] = getNS($id);
// mobile detection
if ($htmlClient) {
$info['ismobile'] = clientismobile();
}
return $info;
}
/**
* Return info about the current document as associative
* array.
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @return array with info about current document
*/
function pageinfo() {
global $ID;
global $REV;
global $RANGE;
global $lang;
/* @var Input $INPUT */
global $INPUT;
$info = basicinfo($ID);
// include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml
// FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary
$info['id'] = $ID;
$info['rev'] = $REV;
$subManager = new SubscriberManager();
$info['subscribed'] = $subManager->userSubscription();
$info['locked'] = checklock($ID);
$info['filepath'] = wikiFN($ID);
$info['exists'] = file_exists($info['filepath']);
$info['currentrev'] = @filemtime($info['filepath']);
if ($REV) {
//check if current revision was meant
if ($info['exists'] && ($info['currentrev'] == $REV)) {
$REV = '';
} elseif ($RANGE) {
//section editing does not work with old revisions!
$REV = '';
$RANGE = '';
msg($lang['nosecedit'], 0);
} else {
//really use old revision
$info['filepath'] = wikiFN($ID, $REV);
$info['exists'] = file_exists($info['filepath']);
}
}
$info['rev'] = $REV;
if ($info['exists']) {
$info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT);
} else {
$info['writable'] = ($info['perm'] >= AUTH_CREATE);
}
$info['editable'] = ($info['writable'] && empty($info['locked']));
$info['lastmod'] = @filemtime($info['filepath']);
//load page meta data
$info['meta'] = p_get_metadata($ID);
//who's the editor
$pagelog = new PageChangeLog($ID, 1024);
if ($REV) {
$revinfo = $pagelog->getRevisionInfo($REV);
} else {
if (!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) {
$revinfo = $info['meta']['last_change'];
} else {
$revinfo = $pagelog->getRevisionInfo($info['lastmod']);
// cache most recent changelog line in metadata if missing and still valid
if ($revinfo !== false) {
$info['meta']['last_change'] = $revinfo;
p_set_metadata($ID, array('last_change' => $revinfo));
}
}
}
//and check for an external edit
if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) {
// cached changelog line no longer valid
$revinfo = false;
$info['meta']['last_change'] = $revinfo;
p_set_metadata($ID, array('last_change' => $revinfo));
}
if ($revinfo !== false) {
$info['ip'] = $revinfo['ip'];
$info['user'] = $revinfo['user'];
$info['sum'] = $revinfo['sum'];
// See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
// Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor'].
$info['editor'] = $revinfo['user'] ?: $revinfo['ip'];
} else {
$info['ip'] = null;
$info['user'] = null;
$info['sum'] = null;
$info['editor'] = null;
}
// draft
$draft = new \dokuwiki\Draft($ID, $info['client']);
if ($draft->isDraftAvailable()) {
$info['draft'] = $draft->getDraftFilename();
}
return $info;
}
/**
* Initialize and/or fill global $JSINFO with some basic info to be given to javascript
*/
function jsinfo() {
global $JSINFO, $ID, $INFO, $ACT;
if (!is_array($JSINFO)) {
$JSINFO = [];
}
//export minimal info to JS, plugins can add more
$JSINFO['id'] = $ID;
$JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : '';
$JSINFO['ACT'] = act_clean($ACT);
$JSINFO['useHeadingNavigation'] = (int) useHeading('navigation');
$JSINFO['useHeadingContent'] = (int) useHeading('content');
}
/**
* Return information about the current media item as an associative array.
*
* @return array with info about current media item
*/
function mediainfo() {
global $NS;
global $IMG;
$info = basicinfo("$NS:*");
$info['image'] = $IMG;
return $info;
}
/**
* Build an string of URL parameters
*
* @author Andreas Gohr
*
* @param array $params array with key-value pairs
* @param string $sep series of pairs are separated by this character
* @return string query string
*/
function buildURLparams($params, $sep = '&') {
$url = '';
$amp = false;
foreach($params as $key => $val) {
if($amp) $url .= $sep;
$url .= rawurlencode($key).'=';
$url .= rawurlencode((string) $val);
$amp = true;
}
return $url;
}
/**
* Build an string of html tag attributes
*
* Skips keys starting with '_', values get HTML encoded
*
* @author Andreas Gohr
*
* @param array $params array with (attribute name-attribute value) pairs
* @param bool $skipEmptyStrings skip empty string values?
* @return string
*/
function buildAttributes($params, $skipEmptyStrings = false) {
$url = '';
$white = false;
foreach($params as $key => $val) {
if($key[0] == '_') continue;
if($val === '' && $skipEmptyStrings) continue;
if($white) $url .= ' ';
$url .= $key.'="';
$url .= hsc($val);
$url .= '"';
$white = true;
}
return $url;
}
/**
* This builds the breadcrumb trail and returns it as array
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @return string[] with the data: array(pageid=>name, ... )
*/
function breadcrumbs() {
// we prepare the breadcrumbs early for quick session closing
static $crumbs = null;
if($crumbs != null) return $crumbs;
global $ID;
global $ACT;
global $conf;
global $INFO;
//first visit?
$crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array();
//we only save on show and existing visible readable wiki documents
$file = wikiFN($ID);
if($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) {
$_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
return $crumbs;
}
// page names
$name = noNSorNS($ID);
if(useHeading('navigation')) {
// get page title
$title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE);
if($title) {
$name = $title;
}
}
//remove ID from array
if(isset($crumbs[$ID])) {
unset($crumbs[$ID]);
}
//add to array
$crumbs[$ID] = $name;
//reduce size
while(count($crumbs) > $conf['breadcrumbs']) {
array_shift($crumbs);
}
//save to session
$_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
return $crumbs;
}
/**
* Filter for page IDs
*
* This is run on a ID before it is outputted somewhere
* currently used to replace the colon with something else
* on Windows (non-IIS) systems and to have proper URL encoding
*
* See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and
* https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of
* unaffected servers instead of blacklisting affected servers here.
*
* Urlencoding is ommitted when the second parameter is false
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id pageid being filtered
* @param bool $ue apply urlencoding?
* @return string
*/
function idfilter($id, $ue = true) {
global $conf;
/* @var Input $INPUT */
global $INPUT;
$id = (string) $id;
if($conf['useslash'] && $conf['userewrite']) {
$id = strtr($id, ':', '/');
} elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
$conf['userewrite'] &&
strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false
) {
$id = strtr($id, ':', ';');
}
if($ue) {
$id = rawurlencode($id);
$id = str_replace('%3A', ':', $id); //keep as colon
$id = str_replace('%3B', ';', $id); //keep as semicolon
$id = str_replace('%2F', '/', $id); //keep as slash
}
return $id;
}
/**
* This builds a link to a wikipage
*
* It handles URL rewriting and adds additional parameters
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id, defaults to start page
* @param string|array $urlParameters URL parameters, associative array recommended
* @param bool $absolute request an absolute URL instead of relative
* @param string $separator parameter separator
* @return string
*/
function wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') {
global $conf;
if(is_array($urlParameters)) {
if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']);
if(isset($urlParameters['at']) && $conf['date_at_format']) {
$urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']);
}
$urlParameters = buildURLparams($urlParameters, $separator);
} else {
$urlParameters = str_replace(',', $separator, $urlParameters);
}
if($id === '') {
$id = $conf['start'];
}
$id = idfilter($id);
if($absolute) {
$xlink = DOKU_URL;
} else {
$xlink = DOKU_BASE;
}
if($conf['userewrite'] == 2) {
$xlink .= DOKU_SCRIPT.'/'.$id;
if($urlParameters) $xlink .= '?'.$urlParameters;
} elseif($conf['userewrite']) {
$xlink .= $id;
if($urlParameters) $xlink .= '?'.$urlParameters;
} elseif($id !== '') {
$xlink .= DOKU_SCRIPT.'?id='.$id;
if($urlParameters) $xlink .= $separator.$urlParameters;
} else {
$xlink .= DOKU_SCRIPT;
if($urlParameters) $xlink .= '?'.$urlParameters;
}
return $xlink;
}
/**
* This builds a link to an alternate page format
*
* Handles URL rewriting if enabled. Follows the style of wl().
*
* @author Ben Coburn <btcoburn@silicodon.net>
* @param string $id page id, defaults to start page
* @param string $format the export renderer to use
* @param string|array $urlParameters URL parameters, associative array recommended
* @param bool $abs request an absolute URL instead of relative
* @param string $sep parameter separator
* @return string
*/
function exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') {
global $conf;
if(is_array($urlParameters)) {
$urlParameters = buildURLparams($urlParameters, $sep);
} else {
$urlParameters = str_replace(',', $sep, $urlParameters);
}
$format = rawurlencode($format);
$id = idfilter($id);
if($abs) {
$xlink = DOKU_URL;
} else {
$xlink = DOKU_BASE;
}
if($conf['userewrite'] == 2) {
$xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
if($urlParameters) $xlink .= $sep.$urlParameters;
} elseif($conf['userewrite'] == 1) {
$xlink .= '_export/'.$format.'/'.$id;
if($urlParameters) $xlink .= '?'.$urlParameters;
} else {
$xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
if($urlParameters) $xlink .= $sep.$urlParameters;
}
return $xlink;
}
/**
* Build a link to a media file
*
* Will return a link to the detail page if $direct is false
*
* The $more parameter should always be given as array, the function then
* will strip default parameters to produce even cleaner URLs
*
* @param string $id the media file id or URL
* @param mixed $more string or array with additional parameters
* @param bool $direct link to detail page if false
* @param string $sep URL parameter separator
* @param bool $abs Create an absolute URL
* @return string
*/
function ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) {
global $conf;
$isexternalimage = media_isexternal($id);
if(!$isexternalimage) {
$id = cleanID($id);
}
if(is_array($more)) {
// add token for resized images
$w = isset($more['w']) ? $more['w'] : null;
$h = isset($more['h']) ? $more['h'] : null;
if($w || $h || $isexternalimage){
$more['tok'] = media_get_token($id, $w, $h);
}
// strip defaults for shorter URLs
if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']);
if(empty($more['w'])) unset($more['w']);
if(empty($more['h'])) unset($more['h']);
if(isset($more['id']) && $direct) unset($more['id']);
if(isset($more['rev']) && !$more['rev']) unset($more['rev']);
$more = buildURLparams($more, $sep);
} else {
$matches = array();
if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){
$resize = array('w'=>0, 'h'=>0);
foreach ($matches as $match){
$resize[$match[1]] = $match[2];
}
$more .= $more === '' ? '' : $sep;
$more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']);
}
$more = str_replace('cache=cache', '', $more); //skip default
$more = str_replace(',,', ',', $more);
$more = str_replace(',', $sep, $more);
}
if($abs) {
$xlink = DOKU_URL;
} else {
$xlink = DOKU_BASE;
}
// external URLs are always direct without rewriting
if($isexternalimage) {
$xlink .= 'lib/exe/fetch.php';
$xlink .= '?'.$more;
$xlink .= $sep.'media='.rawurlencode($id);
return $xlink;
}
$id = idfilter($id);
// decide on scriptname
if($direct) {
if($conf['userewrite'] == 1) {
$script = '_media';
} else {
$script = 'lib/exe/fetch.php';
}
} else {
if($conf['userewrite'] == 1) {
$script = '_detail';
} else {
$script = 'lib/exe/detail.php';
}
}
// build URL based on rewrite mode
if($conf['userewrite']) {
$xlink .= $script.'/'.$id;
if($more) $xlink .= '?'.$more;
} else {
if($more) {
$xlink .= $script.'?'.$more;
$xlink .= $sep.'media='.$id;
} else {
$xlink .= $script.'?media='.$id;
}
}
return $xlink;
}
/**
* Returns the URL to the DokuWiki base script
*
* Consider using wl() instead, unless you absoutely need the doku.php endpoint
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @return string
*/
function script() {
return DOKU_BASE.DOKU_SCRIPT;
}
/**
* Spamcheck against wordlist
*
* Checks the wikitext against a list of blocked expressions
* returns true if the text contains any bad words
*
* Triggers COMMON_WORDBLOCK_BLOCKED
*
* Action Plugins can use this event to inspect the blocked data
* and gain information about the user who was blocked.
*
* Event data:
* data['matches'] - array of matches
* data['userinfo'] - information about the blocked user
* [ip] - ip address
* [user] - username (if logged in)
* [mail] - mail address (if logged in)
* [name] - real name (if logged in)
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Michael Klier <chi@chimeric.de>
*
* @param string $text - optional text to check, if not given the globals are used
* @return bool - true if a spam word was found
*/
function checkwordblock($text = '') {
global $TEXT;
global $PRE;
global $SUF;
global $SUM;
global $conf;
global $INFO;
/* @var Input $INPUT */
global $INPUT;
if(!$conf['usewordblock']) return false;
if(!$text) $text = "$PRE $TEXT $SUF $SUM";
// we prepare the text a tiny bit to prevent spammers circumventing URL checks
// phpcs:disable Generic.Files.LineLength.TooLong
$text = preg_replace(
'!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i',
'\1http://\2 \2\3',
$text
);
// phpcs:enable
$wordblocks = getWordblocks();
// how many lines to read at once (to work around some PCRE limits)
if(version_compare(phpversion(), '4.3.0', '<')) {
// old versions of PCRE define a maximum of parenthesises even if no
// backreferences are used - the maximum is 99
// this is very bad performancewise and may even be too high still
$chunksize = 40;
} else {
// read file in chunks of 200 - this should work around the
// MAX_PATTERN_SIZE in modern PCRE
$chunksize = 200;
}
while($blocks = array_splice($wordblocks, 0, $chunksize)) {
$re = array();
// build regexp from blocks
foreach($blocks as $block) {
$block = preg_replace('/#.*$/', '', $block);
$block = trim($block);
if(empty($block)) continue;
$re[] = $block;
}
if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) {
// prepare event data
$data = array();
$data['matches'] = $matches;
$data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR');
if($INPUT->server->str('REMOTE_USER')) {
$data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER');
$data['userinfo']['name'] = $INFO['userinfo']['name'];
$data['userinfo']['mail'] = $INFO['userinfo']['mail'];
}
$callback = function () {
return true;
};
return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true);
}
}
return false;
}
/**
* Return the IP of the client
*
* Honours X-Forwarded-For and X-Real-IP Proxy Headers
*
* It returns a comma separated list of IPs if the above mentioned
* headers are set. If the single parameter is set, it tries to return
* a routable public address, prefering the ones suplied in the X
* headers
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param boolean $single If set only a single IP is returned
* @return string
*/
function clientIP($single = false) {
/* @var Input $INPUT */
global $INPUT, $conf;
$ip = array();
$ip[] = $INPUT->server->str('REMOTE_ADDR');
if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) {
$ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR'))));
}
if($INPUT->server->str('HTTP_X_REAL_IP')) {
$ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP'))));
}
// remove any non-IP stuff
$cnt = count($ip);
for($i = 0; $i < $cnt; $i++) {
if(filter_var($ip[$i], FILTER_VALIDATE_IP) === false) {
unset($ip[$i]);
}
}
$ip = array_values(array_unique($ip));
if(empty($ip) || !$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
if(!$single) return join(',', $ip);
// skip trusted local addresses
foreach($ip as $i) {
if(!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) {
continue;
} else {
return $i;
}
}
// still here? just use the last address
// this case all ips in the list are trusted
return $ip[count($ip)-1];
}
/**
* Check if the browser is on a mobile device
*
* Adapted from the example code at url below
*
* @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code
*
* @deprecated 2018-04-27 you probably want media queries instead anyway
* @return bool if true, client is mobile browser; otherwise false
*/
function clientismobile() {
/* @var Input $INPUT */
global $INPUT;
if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true;
if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true;
if(!$INPUT->server->has('HTTP_USER_AGENT')) return false;
$uamatches = join(
'|',
[
'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv',
'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia',
'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-',
'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx',
'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox',
'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb',
'\d\d\di', 'moto'
]
);
if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true;
return false;
}
/**
* check if a given link is interwiki link
*
* @param string $link the link, e.g. "wiki>page"
* @return bool
*/
function link_isinterwiki($link){
if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true;
return false;
}
/**
* Convert one or more comma separated IPs to hostnames
*
* If $conf['dnslookups'] is disabled it simply returns the input string
*
* @author Glen Harris <astfgl@iamnota.org>
*
* @param string $ips comma separated list of IP addresses
* @return string a comma separated list of hostnames
*/
function gethostsbyaddrs($ips) {
global $conf;
if(!$conf['dnslookups']) return $ips;
$hosts = array();
$ips = explode(',', $ips);
if(is_array($ips)) {
foreach($ips as $ip) {
$hosts[] = gethostbyaddr(trim($ip));
}
return join(',', $hosts);
} else {
return gethostbyaddr(trim($ips));
}
}
/**
* Checks if a given page is currently locked.
*
* removes stale lockfiles
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id
* @return bool page is locked?
*/
function checklock($id) {
global $conf;
/* @var Input $INPUT */
global $INPUT;
$lock = wikiLockFN($id);
//no lockfile
if(!file_exists($lock)) return false;
//lockfile expired
if((time() - filemtime($lock)) > $conf['locktime']) {
@unlink($lock);
return false;
}
//my own lock
@list($ip, $session) = explode("\n", io_readFile($lock));
if($ip == $INPUT->server->str('REMOTE_USER') || (session_id() && $session == session_id())) {
return false;
}
return $ip;
}
/**
* Lock a page for editing
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id to lock
*/
function lock($id) {
global $conf;
/* @var Input $INPUT */
global $INPUT;
if($conf['locktime'] == 0) {
return;
}
$lock = wikiLockFN($id);
if($INPUT->server->str('REMOTE_USER')) {
io_saveFile($lock, $INPUT->server->str('REMOTE_USER'));
} else {
io_saveFile($lock, clientIP()."\n".session_id());
}
}
/**
* Unlock a page if it was locked by the user
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id to unlock
* @return bool true if a lock was removed
*/
function unlock($id) {
/* @var Input $INPUT */
global $INPUT;
$lock = wikiLockFN($id);
if(file_exists($lock)) {
@list($ip, $session) = explode("\n", io_readFile($lock));
if($ip == $INPUT->server->str('REMOTE_USER') || $session == session_id()) {
@unlink($lock);
return true;
}
}
return false;
}
/**
* convert line ending to unix format
*
* also makes sure the given text is valid UTF-8
*
* @see formText() for 2crlf conversion
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $text
* @return string
*/
function cleanText($text) {
$text = preg_replace("/(\015\012)|(\015)/", "\012", $text);
// if the text is not valid UTF-8 we simply assume latin1
// this won't break any worse than it breaks with the wrong encoding
// but might actually fix the problem in many cases
if(!\dokuwiki\Utf8\Clean::isUtf8($text)) $text = utf8_encode($text);
return $text;
}
/**
* Prepares text for print in Webforms by encoding special chars.
* It also converts line endings to Windows format which is
* pseudo standard for webforms.
*
* @see cleanText() for 2unix conversion
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $text
* @return string
*/
function formText($text) {
$text = str_replace("\012", "\015\012", $text);
return htmlspecialchars($text);
}
/**
* Returns the specified local text in raw format
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id
* @param string $ext extension of file being read, default 'txt'
* @return string
*/
function rawLocale($id, $ext = 'txt') {
return io_readFile(localeFN($id, $ext));
}
/**
* Returns the raw WikiText
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id
* @param string|int $rev timestamp when a revision of wikitext is desired
* @return string
*/
function rawWiki($id, $rev = '') {
return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
}
/**
* Returns the pagetemplate contents for the ID's namespace
*
* @triggers COMMON_PAGETPL_LOAD
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id the id of the page to be created
* @return string parsed pagetemplate content
*/
function pageTemplate($id) {
global $conf;
if(is_array($id)) $id = $id[0];
// prepare initial event data
$data = array(
'id' => $id, // the id of the page to be created
'tpl' => '', // the text used as template
'tplfile' => '', // the file above text was/should be loaded from
'doreplace' => true // should wildcard replacements be done on the text?
);
$evt = new Event('COMMON_PAGETPL_LOAD', $data);
if($evt->advise_before(true)) {
// the before event might have loaded the content already
if(empty($data['tpl'])) {
// if the before event did not set a template file, try to find one
if(empty($data['tplfile'])) {
$path = dirname(wikiFN($id));
if(file_exists($path.'/_template.txt')) {
$data['tplfile'] = $path.'/_template.txt';
} else {
// search upper namespaces for templates
$len = strlen(rtrim($conf['datadir'], '/'));
while(strlen($path) >= $len) {
if(file_exists($path.'/__template.txt')) {
$data['tplfile'] = $path.'/__template.txt';
break;
}
$path = substr($path, 0, strrpos($path, '/'));
}
}
}
// load the content
$data['tpl'] = io_readFile($data['tplfile']);
}
if($data['doreplace']) parsePageTemplate($data);
}
$evt->advise_after();
unset($evt);
return $data['tpl'];
}
/**
* Performs common page template replacements
* This works on data from COMMON_PAGETPL_LOAD
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param array $data array with event data
* @return string
*/
function parsePageTemplate(&$data) {
/**
* @var string $id the id of the page to be created
* @var string $tpl the text used as template
* @var string $tplfile the file above text was/should be loaded from
* @var bool $doreplace should wildcard replacements be done on the text?
*/
extract($data);
global $USERINFO;
global $conf;
/* @var Input $INPUT */
global $INPUT;
// replace placeholders
$file = noNS($id);
$page = strtr($file, $conf['sepchar'], ' ');
$tpl = str_replace(
array(
'@ID@',
'@NS@',
'@CURNS@',
'@!CURNS@',
'@!!CURNS@',
'@!CURNS!@',
'@FILE@',
'@!FILE@',
'@!FILE!@',
'@PAGE@',
'@!PAGE@',
'@!!PAGE@',
'@!PAGE!@',
'@USER@',
'@NAME@',
'@MAIL@',
'@DATE@',
),
array(
$id,
getNS($id),
curNS($id),
\dokuwiki\Utf8\PhpString::ucfirst(curNS($id)),
\dokuwiki\Utf8\PhpString::ucwords(curNS($id)),
\dokuwiki\Utf8\PhpString::strtoupper(curNS($id)),
$file,
\dokuwiki\Utf8\PhpString::ucfirst($file),
\dokuwiki\Utf8\PhpString::strtoupper($file),
$page,
\dokuwiki\Utf8\PhpString::ucfirst($page),
\dokuwiki\Utf8\PhpString::ucwords($page),
\dokuwiki\Utf8\PhpString::strtoupper($page),
$INPUT->server->str('REMOTE_USER'),
$USERINFO ? $USERINFO['name'] : '',
$USERINFO ? $USERINFO['mail'] : '',
$conf['dformat'],
), $tpl
);
// we need the callback to work around strftime's char limit
$tpl = preg_replace_callback(
'/%./',
function ($m) {
return dformat(null, $m[0]);
},
$tpl
);
$data['tpl'] = $tpl;
return $tpl;
}
/**
* Returns the raw Wiki Text in three slices.
*
* The range parameter needs to have the form "from-to"
* and gives the range of the section in bytes - no
* UTF-8 awareness is needed.
* The returned order is prefix, section and suffix.
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $range in form "from-to"
* @param string $id page id
* @param string $rev optional, the revision timestamp
* @return string[] with three slices
*/
function rawWikiSlices($range, $id, $rev = '') {
$text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
// Parse range
list($from, $to) = explode('-', $range, 2);
// Make range zero-based, use defaults if marker is missing
$from = !$from ? 0 : ($from - 1);
$to = !$to ? strlen($text) : ($to - 1);
$slices = array();
$slices[0] = substr($text, 0, $from);
$slices[1] = substr($text, $from, $to - $from);
$slices[2] = substr($text, $to);
return $slices;
}
/**
* Joins wiki text slices
*
* function to join the text slices.
* When the pretty parameter is set to true it adds additional empty
* lines between sections if needed (used on saving).
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $pre prefix
* @param string $text text in the middle
* @param string $suf suffix
* @param bool $pretty add additional empty lines between sections
* @return string
*/
function con($pre, $text, $suf, $pretty = false) {
if($pretty) {
if($pre !== '' && substr($pre, -1) !== "\n" &&
substr($text, 0, 1) !== "\n"
) {
$pre .= "\n";
}
if($suf !== '' && substr($text, -1) !== "\n" &&
substr($suf, 0, 1) !== "\n"
) {
$text .= "\n";
}
}
return $pre.$text.$suf;
}
/**
* Checks if the current page version is newer than the last entry in the page's
* changelog. If so, we assume it has been an external edit and we create an
* attic copy and add a proper changelog line.
*
* This check is only executed when the page is about to be saved again from the
* wiki, triggered in @see saveWikiText()
*
* @param string $id the page ID
* @deprecated 2021-11-28
*/
function detectExternalEdit($id) {
dbg_deprecated(PageFile::class .'::detectExternalEdit()');
(new PageFile($id))->detectExternalEdit();
}
/**
* Saves a wikitext by calling io_writeWikiPage.
* Also directs changelog and attic updates.
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Ben Coburn <btcoburn@silicodon.net>
*
* @param string $id page id
* @param string $text wikitext being saved
* @param string $summary summary of text update
* @param bool $minor mark this saved version as minor update
*/
function saveWikiText($id, $text, $summary, $minor = false) {
// get COMMON_WIKIPAGE_SAVE event data
$data = (new PageFile($id))->saveWikiText($text, $summary, $minor);
// send notify mails
list('oldRevision' => $rev, 'newRevision' => $new_rev, 'summary' => $summary) = $data;
notify($id, 'admin', $rev, $summary, $minor, $new_rev);
notify($id, 'subscribers', $rev, $summary, $minor, $new_rev);
// if useheading is enabled, purge the cache of all linking pages
if (useHeading('content')) {
$pages = ft_backlinks($id, true);
foreach ($pages as $page) {
$cache = new CacheRenderer($page, wikiFN($page), 'xhtml');
$cache->removeCache();
}
}
}
/**
* moves the current version to the attic and returns its revision date
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $id page id
* @return int|string revision timestamp
* @deprecated 2021-11-28
*/
function saveOldRevision($id) {
dbg_deprecated(PageFile::class .'::saveOldRevision()');
return (new PageFile($id))->saveOldRevision();
}
/**
* Sends a notify mail on page change or registration
*
* @param string $id The changed page
* @param string $who Who to notify (admin|subscribers|register)
* @param int|string $rev Old page revision
* @param string $summary What changed
* @param boolean $minor Is this a minor edit?
* @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value
* @param int|string $current_rev New page revision
* @return bool
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) {
global $conf;
/* @var Input $INPUT */
global $INPUT;
// decide if there is something to do, eg. whom to mail
if ($who == 'admin') {
if (empty($conf['notify'])) return false; //notify enabled?
$tpl = 'mailtext';
$to = $conf['notify'];
} elseif ($who == 'subscribers') {
if (!actionOK('subscribe')) return false; //subscribers enabled?
if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors
$data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace);
Event::createAndTrigger(
'COMMON_NOTIFY_ADDRESSLIST', $data,
array(new SubscriberManager(), 'notifyAddresses')
);
$to = $data['addresslist'];
if (empty($to)) return false;
$tpl = 'subscr_single';
} else {
return false; //just to be safe
}
// prepare content
$subscription = new PageSubscriptionSender();
return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev);
}
/**
* extracts the query from a search engine referrer
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Todd Augsburger <todd@rollerorgans.com>
*
* @return array|string
*/
function getGoogleQuery() {
/* @var Input $INPUT */
global $INPUT;
if(!$INPUT->server->has('HTTP_REFERER')) {
return '';
}
$url = parse_url($INPUT->server->str('HTTP_REFERER'));
// only handle common SEs
if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return '';
$query = array();
parse_str($url['query'], $query);
$q = '';
if(isset($query['q'])){
$q = $query['q'];
}elseif(isset($query['p'])){
$q = $query['p'];
}elseif(isset($query['query'])){
$q = $query['query'];
}
$q = trim($q);
if(!$q) return '';
// ignore if query includes a full URL
if(strpos($q, '//') !== false) return '';
$q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY);
return $q;
}
/**
* Return the human readable size of a file
*
* @param int $size A file size
* @param int $dec A number of decimal places
* @return string human readable size
*
* @author Martin Benjamin <b.martin@cybernet.ch>
* @author Aidan Lister <aidan@php.net>
* @version 1.0.0
*/
function filesize_h($size, $dec = 1) {
$sizes = array('B', 'KB', 'MB', 'GB');
$count = count($sizes);
$i = 0;
while($size >= 1024 && ($i < $count - 1)) {
$size /= 1024;
$i++;
}
return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space
}
/**
* Return the given timestamp as human readable, fuzzy age
*
* @author Andreas Gohr <gohr@cosmocode.de>
*
* @param int $dt timestamp
* @return string
*/
function datetime_h($dt) {
global $lang;
$ago = time() - $dt;
if($ago > 24 * 60 * 60 * 30 * 12 * 2) {
return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12)));
}
if($ago > 24 * 60 * 60 * 30 * 2) {
return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30)));
}
if($ago > 24 * 60 * 60 * 7 * 2) {
return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7)));
}
if($ago > 24 * 60 * 60 * 2) {
return sprintf($lang['days'], round($ago / (24 * 60 * 60)));
}
if($ago > 60 * 60 * 2) {
return sprintf($lang['hours'], round($ago / (60 * 60)));
}
if($ago > 60 * 2) {
return sprintf($lang['minutes'], round($ago / (60)));
}
return sprintf($lang['seconds'], $ago);
}
/**
* Wraps around strftime but provides support for fuzzy dates
*
* The format default to $conf['dformat']. It is passed to
* strftime - %f can be used to get the value from datetime_h()
*
* @see datetime_h
* @author Andreas Gohr <gohr@cosmocode.de>
*
* @param int|null $dt timestamp when given, null will take current timestamp
* @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime()
* @return string
*/
function dformat($dt = null, $format = '') {
global $conf;
if(is_null($dt)) $dt = time();
$dt = (int) $dt;
if(!$format) $format = $conf['dformat'];
$format = str_replace('%f', datetime_h($dt), $format);
return strftime($format, $dt);
}
/**
* Formats a timestamp as ISO 8601 date
*
* @author <ungu at terong dot com>
* @link http://php.net/manual/en/function.date.php#54072
*
* @param int $int_date current date in UNIX timestamp
* @return string
*/
function date_iso8601($int_date) {
$date_mod = date('Y-m-d\TH:i:s', $int_date);
$pre_timezone = date('O', $int_date);
$time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2);
$date_mod .= $time_zone;
return $date_mod;
}
/**
* return an obfuscated email address in line with $conf['mailguard'] setting
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @author Christopher Smith <chris@jalakai.co.uk>
*
* @param string $email email address
* @return string
*/
function obfuscate($email) {
global $conf;
switch($conf['mailguard']) {
case 'visible' :
$obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
return strtr($email, $obfuscate);
case 'hex' :
return \dokuwiki\Utf8\Conversion::toHtml($email, true);
case 'none' :
default :
return $email;
}
}
/**
* Removes quoting backslashes
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $string
* @param string $char backslashed character
* @return string
*/
function unslash($string, $char = "'") {
return str_replace('\\'.$char, $char, $string);
}
/**
* Convert php.ini shorthands to byte
*
* On 32 bit systems values >= 2GB will fail!
*
* -1 (infinite size) will be reported as -1
*
* @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
* @param string $value PHP size shorthand
* @return int
*/
function php_to_byte($value) {
switch (strtoupper(substr($value,-1))) {
case 'G':
$ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024;
break;
case 'M':
$ret = intval(substr($value, 0, -1)) * 1024 * 1024;
break;
case 'K':
$ret = intval(substr($value, 0, -1)) * 1024;
break;
default:
$ret = intval($value);
break;
}
return $ret;
}
/**
* Wrapper around preg_quote adding the default delimiter
*
* @param string $string
* @return string
*/
function preg_quote_cb($string) {
return preg_quote($string, '/');
}
/**
* Shorten a given string by removing data from the middle
*
* You can give the string in two parts, the first part $keep
* will never be shortened. The second part $short will be cut
* in the middle to shorten but only if at least $min chars are
* left to display it. Otherwise it will be left off.
*
* @param string $keep the part to keep
* @param string $short the part to shorten
* @param int $max maximum chars you want for the whole string
* @param int $min minimum number of chars to have left for middle shortening
* @param string $char the shortening character to use
* @return string
*/
function shorten($keep, $short, $max, $min = 9, $char = '…') {
$max = $max - \dokuwiki\Utf8\PhpString::strlen($keep);
if($max < $min) return $keep;
$len = \dokuwiki\Utf8\PhpString::strlen($short);
if($len <= $max) return $keep.$short;
$half = floor($max / 2);
return $keep .
\dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) .
$char .
\dokuwiki\Utf8\PhpString::substr($short, $len - $half);
}
/**
* Return the users real name or e-mail address for use
* in page footer and recent changes pages
*
* @param string|null $username or null when currently logged-in user should be used
* @param bool $textonly true returns only plain text, true allows returning html
* @return string html or plain text(not escaped) of formatted user name
*
* @author Andy Webber <dokuwiki AT andywebber DOT com>
*/
function editorinfo($username, $textonly = false) {
return userlink($username, $textonly);
}
/**
* Returns users realname w/o link
*
* @param string|null $username or null when currently logged-in user should be used
* @param bool $textonly true returns only plain text, true allows returning html
* @return string html or plain text(not escaped) of formatted user name
*
* @triggers COMMON_USER_LINK
*/
function userlink($username = null, $textonly = false) {
global $conf, $INFO;
/** @var AuthPlugin $auth */
global $auth;
/** @var Input $INPUT */
global $INPUT;
// prepare initial event data
$data = array(
'username' => $username, // the unique user name
'name' => '',
'link' => array( //setting 'link' to false disables linking
'target' => '',
'pre' => '',
'suf' => '',
'style' => '',
'more' => '',
'url' => '',
'title' => '',
'class' => ''
),
'userlink' => '', // formatted user name as will be returned
'textonly' => $textonly
);
if($username === null) {
$data['username'] = $username = $INPUT->server->str('REMOTE_USER');
if($textonly){
$data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')';
}else {
$data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '.
'(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)';
}
}
$evt = new Event('COMMON_USER_LINK', $data);
if($evt->advise_before(true)) {
if(empty($data['name'])) {
if($auth) $info = $auth->getUserData($username);
if($conf['showuseras'] != 'loginname' && isset($info) && $info) {
switch($conf['showuseras']) {
case 'username':
case 'username_link':
$data['name'] = $textonly ? $info['name'] : hsc($info['name']);
break;
case 'email':
case 'email_link':
$data['name'] = obfuscate($info['mail']);
break;
}
} else {
$data['name'] = $textonly ? $data['username'] : hsc($data['username']);
}
}
/** @var Doku_Renderer_xhtml $xhtml_renderer */
static $xhtml_renderer = null;
if(!$data['textonly'] && empty($data['link']['url'])) {
if(in_array($conf['showuseras'], array('email_link', 'username_link'))) {
if(!isset($info)) {
if($auth) $info = $auth->getUserData($username);
}
if(isset($info) && $info) {
if($conf['showuseras'] == 'email_link') {
$data['link']['url'] = 'mailto:' . obfuscate($info['mail']);
} else {
if(is_null($xhtml_renderer)) {
$xhtml_renderer = p_get_renderer('xhtml');
}
if(empty($xhtml_renderer->interwiki)) {
$xhtml_renderer->interwiki = getInterwiki();
}
$shortcut = 'user';
$exists = null;
$data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists);
$data['link']['class'] .= ' interwiki iw_user';
if($exists !== null) {
if($exists) {
$data['link']['class'] .= ' wikilink1';
} else {
$data['link']['class'] .= ' wikilink2';
$data['link']['rel'] = 'nofollow';
}
}
}
} else {
$data['textonly'] = true;
}
} else {
$data['textonly'] = true;
}
}
if($data['textonly']) {
$data['userlink'] = $data['name'];
} else {
$data['link']['name'] = $data['name'];
if(is_null($xhtml_renderer)) {
$xhtml_renderer = p_get_renderer('xhtml');
}
$data['userlink'] = $xhtml_renderer->_formatLink($data['link']);
}
}
$evt->advise_after();
unset($evt);
return $data['userlink'];
}
/**
* Returns the path to a image file for the currently chosen license.
* When no image exists, returns an empty string
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $type - type of image 'badge' or 'button'
* @return string
*/
function license_img($type) {
global $license;
global $conf;
if(!$conf['license']) return '';
if(!is_array($license[$conf['license']])) return '';
$try = array();
$try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png';
$try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif';
if(substr($conf['license'], 0, 3) == 'cc-') {
$try[] = 'lib/images/license/'.$type.'/cc.png';
}
foreach($try as $src) {
if(file_exists(DOKU_INC.$src)) return $src;
}
return '';
}
/**
* Checks if the given amount of memory is available
*
* If the memory_get_usage() function is not available the
* function just assumes $bytes of already allocated memory
*
* @author Filip Oscadal <webmaster@illusionsoftworks.cz>
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param int $mem Size of memory you want to allocate in bytes
* @param int $bytes already allocated memory (see above)
* @return bool
*/
function is_mem_available($mem, $bytes = 1048576) {
$limit = trim(ini_get('memory_limit'));
if(empty($limit)) return true; // no limit set!
if($limit == -1) return true; // unlimited
// parse limit to bytes
$limit = php_to_byte($limit);
// get used memory if possible
if(function_exists('memory_get_usage')) {
$used = memory_get_usage();
} else {
$used = $bytes;
}
if($used + $mem > $limit) {
return false;
}
return true;
}
/**
* Send a HTTP redirect to the browser
*
* Works arround Microsoft IIS cookie sending bug. Exits the script.
*
* @link http://support.microsoft.com/kb/q176113/
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $url url being directed to
*/
function send_redirect($url) {
$url = stripctl($url); // defend against HTTP Response Splitting
/* @var Input $INPUT */
global $INPUT;
//are there any undisplayed messages? keep them in session for display
global $MSG;
if(isset($MSG) && count($MSG) && !defined('NOSESSION')) {
//reopen session, store data and close session again
@session_start();
$_SESSION[DOKU_COOKIE]['msg'] = $MSG;
}
// always close the session
session_write_close();
// check if running on IIS < 6 with CGI-PHP
if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') &&
(strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) &&
(preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) &&
$matches[1] < 6
) {
header('Refresh: 0;url='.$url);
} else {
header('Location: '.$url);
}
// no exits during unit tests
if(defined('DOKU_UNITTEST')) {
// pass info about the redirect back to the test suite
$testRequest = TestRequest::getRunning();
if($testRequest !== null) {
$testRequest->addData('send_redirect', $url);
}
return;
}
exit;
}
/**
* Validate a value using a set of valid values
*
* This function checks whether a specified value is set and in the array
* $valid_values. If not, the function returns a default value or, if no
* default is specified, throws an exception.
*
* @param string $param The name of the parameter
* @param array $valid_values A set of valid values; Optionally a default may
* be marked by the key “default”.
* @param array $array The array containing the value (typically $_POST
* or $_GET)
* @param string $exc The text of the raised exception
*
* @throws Exception
* @return mixed
* @author Adrian Lang <lang@cosmocode.de>
*/
function valid_input_set($param, $valid_values, $array, $exc = '') {
if(isset($array[$param]) && in_array($array[$param], $valid_values)) {
return $array[$param];
} elseif(isset($valid_values['default'])) {
return $valid_values['default'];
} else {
throw new Exception($exc);
}
}
/**
* Read a preference from the DokuWiki cookie
* (remembering both keys & values are urlencoded)
*
* @param string $pref preference key
* @param mixed $default value returned when preference not found
* @return string preference value
*/
function get_doku_pref($pref, $default) {
$enc_pref = urlencode($pref);
if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) {
$parts = explode('#', $_COOKIE['DOKU_PREFS']);
$cnt = count($parts);
// due to #2721 there might be duplicate entries,
// so we read from the end
for($i = $cnt-2; $i >= 0; $i -= 2) {
if($parts[$i] == $enc_pref) {
return urldecode($parts[$i + 1]);
}
}
}
return $default;
}
/**
* Add a preference to the DokuWiki cookie
* (remembering $_COOKIE['DOKU_PREFS'] is urlencoded)
* Remove it by setting $val to false
*
* @param string $pref preference key
* @param string $val preference value
*/
function set_doku_pref($pref, $val) {
global $conf;
$orig = get_doku_pref($pref, false);
$cookieVal = '';
if($orig !== false && ($orig !== $val)) {
$parts = explode('#', $_COOKIE['DOKU_PREFS']);
$cnt = count($parts);
// urlencode $pref for the comparison
$enc_pref = rawurlencode($pref);
$seen = false;
for ($i = 0; $i < $cnt; $i += 2) {
if ($parts[$i] == $enc_pref) {
if (!$seen){
if ($val !== false) {
$parts[$i + 1] = rawurlencode($val ?? '');
} else {
unset($parts[$i]);
unset($parts[$i + 1]);
}
$seen = true;
} else {
// no break because we want to remove duplicate entries
unset($parts[$i]);
unset($parts[$i + 1]);
}
}
}
$cookieVal = implode('#', $parts);
} else if ($orig === false && $val !== false) {
$cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') .
rawurlencode($pref) . '#' . rawurlencode($val);
}
$cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
if(defined('DOKU_UNITTEST')) {
$_COOKIE['DOKU_PREFS'] = $cookieVal;
}else{
setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl()));
}
}
/**
* Strips source mapping declarations from given text #601
*
* @param string &$text reference to the CSS or JavaScript code to clean
*/
function stripsourcemaps(&$text){
$text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text);
}
/**
* Returns the contents of a given SVG file for embedding
*
* Inlining SVGs saves on HTTP requests and more importantly allows for styling them through
* CSS. However it should used with small SVGs only. The $maxsize setting ensures only small
* files are embedded.
*
* This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG!
*
* @param string $file full path to the SVG file
* @param int $maxsize maximum allowed size for the SVG to be embedded
* @return string|false the SVG content, false if the file couldn't be loaded
*/
function inlineSVG($file, $maxsize = 2048) {
$file = trim($file);
if($file === '') return false;
if(!file_exists($file)) return false;
if(filesize($file) > $maxsize) return false;
if(!is_readable($file)) return false;
$content = file_get_contents($file);
$content = preg_replace('/<!--.*?(-->)/s','', $content); // comments
$content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header
$content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type
$content = preg_replace('/>\s+</s', '><', $content); // newlines between tags
$content = trim($content);
if(substr($content, 0, 5) !== '<svg ') return false;
return $content;
}
//Setup VIM: ex: et ts=2 :
|