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
|
<?php //-*-php-*-
rcs_id('$Id: loadsave.php,v 1.153 2007/05/28 20:54:40 rurban Exp $');
/*
Copyright 1999,2000,2001,2002,2004,2005,2006,2007 $ThePhpWikiProgrammingTeam
This file is part of PhpWiki.
PhpWiki is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
PhpWiki is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PhpWiki; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once("lib/ziplib.php");
require_once("lib/Template.php");
/**
* ignore fatal errors during dump
*/
function _dump_error_handler(&$error) {
if ($error->isFatal()) {
$error->errno = E_USER_WARNING;
return true;
}
return true; // Ignore error
/*
if (preg_match('/Plugin/', $error->errstr))
return true;
*/
// let the message come through: call the remaining handlers:
// return false;
}
function StartLoadDump(&$request, $title, $html = '')
{
// MockRequest is from the unit testsuite, a faked request. (may be cmd-line)
// We are silent on unittests.
if (isa($request,'MockRequest'))
return;
// FIXME: This is a hack. This really is the worst overall hack in phpwiki.
if ($html)
$html->pushContent('%BODY%');
$tmpl = Template('html', array('TITLE' => $title,
'HEADER' => $title,
'CONTENT' => $html ? $html : '%BODY%'));
echo ereg_replace('%BODY%.*', '', $tmpl->getExpansion($html));
$request->chunkOutput();
// set marker for sendPageChangeNotification()
$request->_deferredPageChangeNotification = array();
}
function EndLoadDump(&$request)
{
if (isa($request,'MockRequest'))
return;
$action = $request->getArg('action');
$label = '';
switch ($action) {
case 'zip': $label = _("ZIP files of database"); break;
case 'dumpserial': $label = _("Dump to directory"); break;
case 'upload': $label = _("Upload File"); break;
case 'loadfile': $label = _("Load File"); break;
case 'upgrade': $label = _("Upgrade"); break;
case 'dumphtml':
case 'ziphtml': $label = _("Dump pages as XHTML"); break;
}
if ($label) $label = str_replace(" ","_",$label);
if ($action == 'browse') // loading virgin
$pagelink = WikiLink(HOME_PAGE);
else
$pagelink = WikiLink(new WikiPageName(_("PhpWikiAdministration"),false,$label));
// do deferred sendPageChangeNotification()
if (!empty($request->_deferredPageChangeNotification)) {
$pages = $all_emails = $all_users = array();
foreach ($request->_deferredPageChangeNotification as $p) {
list($pagename, $emails, $userids) = $p;
$pages[] = $pagename;
$all_emails = array_unique(array_merge($all_emails, $emails));
$all_users = array_unique(array_merge($all_users, $userids));
}
$editedby = sprintf(_("Edited by: %s"), $request->_user->getId());
$content = "Loaded the following pages:\n" . join("\n", $pages);
if (mail(join(',',$all_emails),"[".WIKI_NAME."] "._("LoadDump"),
_("LoadDump")."\n".
$editedby."\n\n".
$content))
trigger_error(sprintf(_("PageChange Notification of %s sent to %s"),
join("\n",$pages), join(',',$all_users)), E_USER_NOTICE);
else
trigger_error(sprintf(_("PageChange Notification Error: Couldn't send %s to %s"),
join("\n",$pages), join(',',$all_users)), E_USER_WARNING);
unset($pages);
unset($all_emails);
unset($all_users);
}
unset($request->_deferredPageChangeNotification);
PrintXML(HTML::p(HTML::strong(_("Complete."))),
HTML::p(fmt("Return to %s", $pagelink)));
echo "</body></html>\n";
}
////////////////////////////////////////////////////////////////
//
// Functions for dumping.
//
////////////////////////////////////////////////////////////////
/**
* For reference see:
* http://www.nacs.uci.edu/indiv/ehood/MIME/2045/rfc2045.html
* http://www.faqs.org/rfcs/rfc2045.html
* (RFC 1521 has been superceeded by RFC 2045 & others).
*
* Also see http://www.faqs.org/rfcs/rfc2822.html
*/
function MailifyPage ($page, $nversions = 1)
{
$current = $page->getCurrentRevision(false);
$head = '';
if (STRICT_MAILABLE_PAGEDUMPS) {
$from = defined('SERVER_ADMIN') ? SERVER_ADMIN : 'foo@bar';
//This is for unix mailbox format: (not RFC (2)822)
// $head .= "From $from " . CTime(time()) . "\r\n";
$head .= "Subject: " . rawurlencode($page->getName()) . "\r\n";
$head .= "From: $from (PhpWiki)\r\n";
// RFC 2822 requires only a Date: and originator (From:)
// field, however the obsolete standard RFC 822 also
// requires a destination field.
$head .= "To: $from (PhpWiki)\r\n";
}
$head .= "Date: " . Rfc2822DateTime($current->get('mtime')) . "\r\n";
$head .= sprintf("Mime-Version: 1.0 (Produced by PhpWiki %s)\r\n",
PHPWIKI_VERSION);
// This should just be entered by hand (or by script?)
// in the actual pgsrc files, since only they should have
// RCS ids.
//$head .= "X-Rcs-Id: \$Id\$\r\n";
$iter = $page->getAllRevisions();
$parts = array();
while ($revision = $iter->next()) {
$parts[] = MimeifyPageRevision($page, $revision);
if ($nversions > 0 && count($parts) >= $nversions)
break;
}
if (count($parts) > 1)
return $head . MimeMultipart($parts);
assert($parts);
return $head . $parts[0];
}
/***
* Compute filename to used for storing contents of a wiki page.
*
* Basically we do a rawurlencode() which encodes everything except
* ASCII alphanumerics and '.', '-', and '_'.
*
* But we also want to encode leading dots to avoid filenames like
* '.', and '..'. (Also, there's no point in generating "hidden" file
* names, like '.foo'.)
*
* @param $pagename string Pagename.
* @return string Filename for page.
*/
function FilenameForPage ($pagename)
{
//$enc = rawurlencode($pagename);
$enc = preg_replace('/:/', '%3A', $pagename);
// For every %2F will need to mkdir -p dirname($pagename)
return preg_replace('/^\./', '%2E', $enc);
// return preg_replace(array('/^\./','%20'), array('%2E',' '), $enc);
}
/**
* The main() function which generates a zip archive of a PhpWiki.
*
* If $include_archive is false, only the current version of each page
* is included in the zip file; otherwise all archived versions are
* included as well.
*/
function MakeWikiZip (&$request)
{
if ($request->getArg('include') == 'all') {
$zipname = WIKI_NAME . _("FullDump") . date('Ymd-Hi') . '.zip';
$include_archive = true;
}
else {
$zipname = WIKI_NAME . _("LatestSnapshot") . date('Ymd-Hi') . '.zip';
$include_archive = false;
}
$zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
/* ignore fatals in plugins */
if (check_php_version(4,1)) {
global $ErrorManager;
$ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
}
$dbi =& $request->_dbi;
$thispage = $request->getArg('pagename'); // for "Return to ..."
if ($exclude = $request->getArg('exclude')) { // exclude which pagenames
$excludeList = explodePageList($exclude);
} else {
$excludeList = array();
}
if ($pages = $request->getArg('pages')) { // which pagenames
if ($pages == '[]') // current page
$pages = $thispage;
$page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
} else {
$page_iter = $dbi->getAllPages(false,false,false,$excludeList);
}
$request_args = $request->args;
$timeout = (! $request->getArg('start_debug')) ? 30 : 240;
while ($page = $page_iter->next()) {
$request->args = $request_args; // some plugins might change them (esp. on POST)
longer_timeout($timeout); // Reset watchdog
$current = $page->getCurrentRevision();
if ($current->getVersion() == 0)
continue;
$pagename = $page->getName();
$wpn = new WikiPageName($pagename);
if (!$wpn->isValid())
continue;
if (in_array($page->getName(), $excludeList)) {
continue;
}
$attrib = array('mtime' => $current->get('mtime'),
'is_ascii' => 1);
if ($page->get('locked'))
$attrib['write_protected'] = 1;
if ($include_archive)
$content = MailifyPage($page, 0);
else
$content = MailifyPage($page);
$zip->addRegularFile( FilenameForPage($pagename),
$content, $attrib);
}
$zip->finish();
if (check_php_version(4,1)) {
global $ErrorManager;
$ErrorManager->popErrorHandler();
}
}
function DumpToDir (&$request)
{
$directory = $request->getArg('directory');
if (empty($directory))
$directory = DEFAULT_DUMP_DIR; // See lib/plugin/WikiForm.php:87
if (empty($directory))
$request->finish(_("You must specify a directory to dump to"));
// see if we can access the directory the user wants us to use
if (! file_exists($directory)) {
if (! mkdir($directory, 0755))
$request->finish(fmt("Cannot create directory '%s'", $directory));
else
$html = HTML::p(fmt("Created directory '%s' for the page dump...",
$directory));
} else {
$html = HTML::p(fmt("Using directory '%s'", $directory));
}
StartLoadDump($request, _("Dumping Pages"), $html);
$dbi =& $request->_dbi;
$thispage = $request->getArg('pagename'); // for "Return to ..."
if ($exclude = $request->getArg('exclude')) { // exclude which pagenames
$excludeList = explodePageList($exclude);
} else {
$excludeList = array();
}
if ($pages = $request->getArg('pages')) { // which pagenames
if ($pages == '[]') // current page
$pages = $thispage;
$page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
} else {
$page_iter = $dbi->getAllPages(false,false,false,$excludeList);
}
$request_args = $request->args;
$timeout = (! $request->getArg('start_debug')) ? 30 : 240;
while ($page = $page_iter->next()) {
$request->args = $request_args; // some plugins might change them (esp. on POST)
longer_timeout($timeout); // Reset watchdog
$pagename = $page->getName();
if (!isa($request,'MockRequest')) {
PrintXML(HTML::br(), $pagename, ' ... ');
flush();
}
if (in_array($pagename, $excludeList)) {
if (!isa($request, 'MockRequest')) {
PrintXML(_("Skipped."));
flush();
}
continue;
}
$filename = FilenameForPage($pagename);
$msg = HTML();
if($page->getName() != $filename) {
$msg->pushContent(HTML::small(fmt("saved as %s", $filename)),
" ... ");
}
if ($request->getArg('include') == 'all')
$data = MailifyPage($page, 0);
else
$data = MailifyPage($page);
if ( !($fd = fopen($directory."/".$filename, "wb")) ) {
$msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
"$directory/$filename")));
$request->finish($msg);
}
$num = fwrite($fd, $data, strlen($data));
$msg->pushContent(HTML::small(fmt("%s bytes written", $num)));
if (!isa($request, 'MockRequest')) {
PrintXML($msg);
flush();
}
assert($num == strlen($data));
fclose($fd);
}
EndLoadDump($request);
}
function _copyMsg($page, $smallmsg) {
if (!isa($GLOBALS['request'], 'MockRequest')) {
if ($page) $msg = HTML(HTML::br(), HTML($page), HTML::small($smallmsg));
else $msg = HTML::small($smallmsg);
PrintXML($msg);
flush();
}
}
function mkdir_p($pathname, $permission = 0777) {
$arr = explode("/", $pathname);
if (empty($arr)) {
return mkdir($pathname, $permission);
}
$s = array_shift($arr);
$ok = TRUE;
foreach ($arr as $p) {
$curr = "$s/$p";
if (!is_dir($curr))
$ok = mkdir($curr, $permission);
$s = $curr;
if (!$ok) return FALSE;
}
return TRUE;
}
/**
* Dump all pages as XHTML to a directory, as pagename.html.
* Copies all used css files to the directory, all used images to a
* "images" subdirectory, and all used buttons to a "images/buttons" subdirectory.
* The webserver must have write permissions to these directories.
* chown httpd HTML_DUMP_DIR; chmod u+rwx HTML_DUMP_DIR
* should be enough.
*
* @param string directory (optional) path to dump to. Default: HTML_DUMP_DIR
* @param string pages (optional) Comma-seperated of glob-style pagenames to dump
* @param string exclude (optional) Comma-seperated of glob-style pagenames to exclude
*/
function DumpHtmlToDir (&$request)
{
$directory = $request->getArg('directory');
if (empty($directory))
$directory = HTML_DUMP_DIR; // See lib/plugin/WikiForm.php:87
if (empty($directory))
$request->finish(_("You must specify a directory to dump to"));
// see if we can access the directory the user wants us to use
if (! file_exists($directory)) {
if (! mkdir($directory, 0755))
$request->finish(fmt("Cannot create directory '%s'", $directory));
else
$html = HTML::p(fmt("Created directory '%s' for the page dump...",
$directory));
} else {
$html = HTML::p(fmt("Using directory '%s'", $directory));
}
$request->_TemplatesProcessed = array();
StartLoadDump($request, _("Dumping Pages"), $html);
$thispage = $request->getArg('pagename'); // for "Return to ..."
$dbi =& $request->_dbi;
if ($exclude = $request->getArg('exclude')) { // exclude which pagenames
$excludeList = explodePageList($exclude);
} else {
$excludeList = array();
}
if ($pages = $request->getArg('pages')) { // which pagenames
if ($pages == '[]') // current page
$pages = $thispage;
$page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
// not at admin page: dump only the current page
} elseif ($thispage != _("PhpWikiAdministration")) {
$page_iter = new WikiDB_Array_PageIterator(array($thispage));
} else {
$page_iter = $dbi->getAllPages(false,false,false,$excludeList);
}
global $WikiTheme;
if (defined('HTML_DUMP_SUFFIX'))
$WikiTheme->HTML_DUMP_SUFFIX = HTML_DUMP_SUFFIX;
$WikiTheme->DUMP_MODE = 'HTML';
$_bodyAttr = @$WikiTheme->_MoreAttr['body'];
unset($WikiTheme->_MoreAttr['body']);
// check if the dumped file will be accessible from outside
$doc_root = $request->get("DOCUMENT_ROOT");
$ldir = NormalizeLocalFileName($directory);
$wikiroot = NormalizeLocalFileName('');
if (string_starts_with($ldir, $doc_root)) {
$link_prefix = substr($directory, strlen($doc_root))."/";
} elseif (string_starts_with($ldir, $wikiroot)) {
$link_prefix = NormalizeWebFileName(substr($directory, strlen($wikiroot)))."/";
} else {
$prefix = '';
if (isWindows()) {
$prefix = '/'; // . substr($doc_root,0,2); // add drive where apache is installed
}
$link_prefix = "file://".$prefix.$directory."/";
}
$request_args = $request->args;
$timeout = (! $request->getArg('start_debug')) ? 20 : 240;
while ($page = $page_iter->next()) {
$request->args = $request_args; // some plugins might change them (esp. on POST)
longer_timeout($timeout); // Reset watchdog
$pagename = $page->getName();
if (!isa($request,'MockRequest')) {
PrintXML(HTML::br(), $pagename, ' ... ');
flush();
}
if (in_array($pagename, $excludeList)) {
if (!isa($request,'MockRequest')) {
PrintXML(_("Skipped."));
flush();
}
continue;
}
$relative_base = '';
$request->setArg('pagename', $pagename); // Template::_basepage fix
$filename = FilenameForPage($pagename) . $WikiTheme->HTML_DUMP_SUFFIX;
$revision = $page->getCurrentRevision();
$args = array('revision' => $revision,
'CONTENT' => $revision->getTransformedContent(),
'relative_base' => $relative_base);
// For every %2F will need to mkdir -p dirname($pagename)
if (preg_match("/(%2F|\/)/", $filename)) {
// mkdir -p and set relative base for subdir pages
$count = substr_count($filename, "%2F");
$filename = preg_replace("/%2F/", "/", $filename);
$dirname = dirname($filename);
mkdir_p($directory."/".$dirname);
$relative_base = "../";
while ($count > 1) {
$relative_base .= "../";
$count--;
}
$args['relative_base'] = $relative_base;
}
$msg = HTML();
$template = new Template('browse', $request, $args);
$data = GeneratePageasXML($template, $pagename, $revision, $args);
if ( !($fd = fopen($directory."/".$filename, "wb")) ) {
$msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
"$directory/$filename")));
$request->finish($msg);
}
$len = strlen($data);
$num = fwrite($fd, $data, $len);
if ($page->getName() != $filename) {
$link = LinkURL($link_prefix.$filename, $filename);
$msg->pushContent(HTML::small(_("saved as "), $link, " ... "));
}
$msg->pushContent(HTML::small(fmt("%s bytes written", $num), "\n"));
if (!isa($request, 'MockRequest')) {
PrintXML($msg);
}
flush();
$request->chunkOutput();
assert($num == $len);
fclose($fd);
if (USECACHE) {
$request->_dbi->_cache->invalidate_cache($pagename);
unset ($request->_dbi->_cache->_pagedata_cache);
unset ($request->_dbi->_cache->_versiondata_cache);
unset ($request->_dbi->_cache->_glv_cache);
}
unset ($request->_dbi->_cache->_backend->_page_data);
unset($msg);
unset($revision->_transformedContent);
unset($revision);
unset($template->_request);
unset($template);
unset($data);
}
$page_iter->free();
if (!empty($WikiTheme->dumped_images) and is_array($WikiTheme->dumped_images)) {
@mkdir("$directory/images");
foreach ($WikiTheme->dumped_images as $img_file) {
if ($img_file
and ($from = $WikiTheme->_findFile($img_file, true))
and basename($from))
{
$target = "$directory/images/".basename($img_file);
if (copy($WikiTheme->_path . $from, $target)) {
_copyMsg($from, fmt("... copied to %s", $target));
} else {
_copyMsg($from, fmt("... not copied to %s", $target));
}
//TODO: fix to local path for uploaded images, so that pdf will work
} else {
_copyMsg($from, _("... not found"));
}
}
}
if (!empty($WikiTheme->dumped_buttons) and is_array($WikiTheme->dumped_buttons)) {
// Buttons also
@mkdir("$directory/images/buttons");
foreach ($WikiTheme->dumped_buttons as $text => $img_file) {
if ($img_file
and ($from = $WikiTheme->_findFile($img_file, true))
and basename($from))
{
$target = "$directory/images/buttons/".basename($img_file);
if (copy($WikiTheme->_path . $from, $target)) {
_copyMsg($from, fmt("... copied to %s", $target));
} else {
_copyMsg($from, fmt("... not copied to %s", $target));
}
} else {
_copyMsg($from, _("... not found"));
}
}
}
if (!empty($WikiTheme->dumped_css) and is_array($WikiTheme->dumped_css)) {
foreach ($WikiTheme->dumped_css as $css_file) {
if ($css_file
and ($from = $WikiTheme->_findFile(basename($css_file), true))
and basename($from))
{
$target = "$directory/" . basename($css_file);
if (copy($WikiTheme->_path . $from, $target)) {
_copyMsg($from, fmt("... copied to %s", $target));
} else {
_copyMsg($from, fmt("... not copied to %s", $target));
}
// TODO: fix @import url(main.css);
} else {
_copyMsg($from, _("... not found"));
}
}
}
$WikiTheme->HTML_DUMP_SUFFIX = '';
$WikiTheme->DUMP_MODE = false;
$WikiTheme->_MoreAttr['body'] = $_bodyAttr;
$request->setArg('pagename',$thispage); // Template::_basepage fix
EndLoadDump($request);
}
/* Known problem: any plugins or other code which echo()s text will
* lead to a corrupted html zip file which may produce the following
* errors upon unzipping:
*
* warning [wikihtml.zip]: 2401 extra bytes at beginning or within zipfile
* file #58: bad zipfile offset (local header sig): 177561
* (attempting to re-compensate)
*
* However, the actual wiki page data should be unaffected.
*/
function MakeWikiZipHtml (&$request)
{
$request->_TemplatesProcessed = array();
$zipname = "wikihtml.zip";
$zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
$dbi =& $request->_dbi;
$thispage = $request->getArg('pagename'); // for "Return to ..."
if ($exclude = $request->getArg('exclude')) { // exclude which pagenames
$excludeList = explodePageList($exclude);
} else {
$excludeList = array();
}
if ($pages = $request->getArg('pages')) { // which pagenames
if ($pages == '[]') // current page
$pages = $thispage;
$page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
} else {
$page_iter = $dbi->getAllPages(false,false,false,$excludeList);
}
global $WikiTheme;
if (defined('HTML_DUMP_SUFFIX'))
$WikiTheme->HTML_DUMP_SUFFIX = HTML_DUMP_SUFFIX;
$WikiTheme->DUMP_MODE = 'ZIPHTML';
$_bodyAttr = @$WikiTheme->_MoreAttr['body'];
unset($WikiTheme->_MoreAttr['body']);
/* ignore fatals in plugins */
if (check_php_version(4,1)) {
global $ErrorManager;
$ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
}
$request_args = $request->args;
$timeout = (! $request->getArg('start_debug')) ? 20 : 240;
while ($page = $page_iter->next()) {
$request->args = $request_args; // some plugins might change them (esp. on POST)
longer_timeout($timeout); // Reset watchdog
$current = $page->getCurrentRevision();
if ($current->getVersion() == 0)
continue;
$pagename = $page->getName();
if (in_array($pagename, $excludeList)) {
continue;
}
$attrib = array('mtime' => $current->get('mtime'),
'is_ascii' => 1);
if ($page->get('locked'))
$attrib['write_protected'] = 1;
$request->setArg('pagename', $pagename); // Template::_basepage fix
$filename = FilenameForPage($pagename) . $WikiTheme->HTML_DUMP_SUFFIX;
$revision = $page->getCurrentRevision();
$transformedContent = $revision->getTransformedContent();
$template = new Template('browse', $request,
array('revision' => $revision,
'CONTENT' => $transformedContent));
$data = GeneratePageasXML($template, $pagename);
$zip->addRegularFile( $filename, $data, $attrib );
if (USECACHE) {
$request->_dbi->_cache->invalidate_cache($pagename);
unset ($request->_dbi->_cache->_pagedata_cache);
unset ($request->_dbi->_cache->_versiondata_cache);
unset ($request->_dbi->_cache->_glv_cache);
}
unset ($request->_dbi->_cache->_backend->_page_data);
unset($revision->_transformedContent);
unset($revision);
unset($template->_request);
unset($template);
unset($data);
}
$page_iter->free();
$attrib = false;
// Deal with css and images here.
if (!empty($WikiTheme->dumped_images) and is_array($WikiTheme->dumped_images)) {
// dirs are created automatically
//if ($WikiTheme->dumped_images) $zip->addRegularFile("images", "", $attrib);
foreach ($WikiTheme->dumped_images as $img_file) {
if (($from = $WikiTheme->_findFile($img_file, true)) and basename($from)) {
$target = "images/".basename($img_file);
if (check_php_version(4,3))
$zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
else
$zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
}
}
}
if (!empty($WikiTheme->dumped_buttons) and is_array($WikiTheme->dumped_buttons)) {
//if ($WikiTheme->dumped_buttons) $zip->addRegularFile("images/buttons", "", $attrib);
foreach ($WikiTheme->dumped_buttons as $text => $img_file) {
if (($from = $WikiTheme->_findFile($img_file, true)) and basename($from)) {
$target = "images/buttons/".basename($img_file);
if (check_php_version(4,3))
$zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
else
$zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
}
}
}
if (!empty($WikiTheme->dumped_css) and is_array($WikiTheme->dumped_css)) {
foreach ($WikiTheme->dumped_css as $css_file) {
if (($from = $WikiTheme->_findFile(basename($css_file), true)) and basename($from)) {
$target = basename($css_file);
if (check_php_version(4,3))
$zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
else
$zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
}
}
}
$zip->finish();
if (check_php_version(4,1)) {
global $ErrorManager;
$ErrorManager->popErrorHandler();
}
$WikiTheme->HTML_DUMP_SUFFIX = '';
$WikiTheme->DUMP_MODE = false;
$WikiTheme->_MoreAttr['body'] = $_bodyAttr;
}
////////////////////////////////////////////////////////////////
//
// Functions for restoring.
//
////////////////////////////////////////////////////////////////
function SavePage (&$request, &$pageinfo, $source, $filename)
{
static $overwite_all = false;
$pagedata = $pageinfo['pagedata']; // Page level meta-data.
$versiondata = $pageinfo['versiondata']; // Revision level meta-data.
if (empty($pageinfo['pagename'])) {
PrintXML(HTML::dt(HTML::strong(_("Empty pagename!"))));
return;
}
if (empty($versiondata['author_id']))
$versiondata['author_id'] = $versiondata['author'];
// remove invalid backend specific chars. utf8 issues mostly
$pagename_check = new WikiPagename($pageinfo['pagename']);
if (!$pagename_check->isValid()) {
PrintXML(HTML::dt(HTML::strong(_("Invalid pagename!")." ".$pageinfo['pagename'])));
return;
}
$pagename = $pagename_check->getName();
$content = $pageinfo['content'];
if ($pagename == _("InterWikiMap"))
$content = _tryinsertInterWikiMap($content);
$dbi =& $request->_dbi;
$page = $dbi->getPage($pagename);
// Try to merge if updated pgsrc contents are different. This
// whole thing is hackish
//
// TODO: try merge unless:
// if (current contents = default contents && pgsrc_version >=
// pgsrc_version) then just upgrade this pgsrc
$needs_merge = false;
$merging = false;
$overwrite = false;
if ($request->getArg('merge')) {
$merging = true;
}
else if ($request->getArg('overwrite')) {
$overwrite = true;
}
$current = $page->getCurrentRevision();
$skip = false;
$edit = $request->getArg('edit');
if ($merging) {
if (isset($edit['keep_old'])) {
$merging = false;
$skip = true;
}
elseif (isset($edit['overwrite'])) {
$merging = false;
$overwrite = true;
}
elseif ( $current and (! $current->hasDefaultContents())
&& ($current->getPackedContent() != $content) )
{
include_once('lib/editpage.php');
$request->setArg('pagename', $pagename);
$v = $current->getVersion();
$request->setArg('revision', $current->getVersion());
$p = new LoadFileConflictPageEditor($request);
$p->_content = $content;
$p->_currentVersion = $v - 1;
$p->editPage($saveFailed = true);
return; //early return
}
}
if (!$skip)
foreach ($pagedata as $key => $value) {
if (!empty($value))
$page->set($key, $value);
}
$mesg = HTML::dd();
if ($source)
$mesg->pushContent(' ', fmt("from %s", $source));
if (!$current) {
//FIXME: This should not happen! (empty vdata, corrupt cache or db)
$current = $page->getCurrentRevision();
}
if ($current->getVersion() == 0) {
$mesg->pushContent(' - ', _("New page"));
$isnew = true;
}
else {
if ( (! $current->hasDefaultContents())
&& ($current->getPackedContent() != $content) ) {
if ($overwrite) {
$mesg->pushContent(' ',
fmt("has edit conflicts - overwriting anyway"));
$skip = false;
if (substr_count($source, 'pgsrc')) {
$versiondata['author'] = _("The PhpWiki programming team");
// but leave authorid as userid who loaded the file
}
}
else {
if (isset($edit['keep_old'])) {
$mesg->pushContent(' ', fmt("keep old"));
} else {
$mesg->pushContent(' ', fmt("has edit conflicts - skipped"));
$needs_merge = true; // hackish, to display the buttons
}
$skip = true;
}
}
else if ($current->getPackedContent() == $content
&& $current->get('author') == $versiondata['author']) {
// The page metadata is already changed, we don't need a new revision.
// This was called previously "is identical to current version %d - skipped"
// which is wrong, since the pagedata was stored, not skipped.
$mesg->pushContent(' ',
fmt("content is identical to current version %d - no new revision created",
$current->getVersion()));
$skip = true;
}
$isnew = false;
}
if (! $skip ) {
// in case of failures print the culprit:
if (!isa($request,'MockRequest')) {
PrintXML(HTML::dt(WikiLink($pagename))); flush();
}
$new = $page->save($content, WIKIDB_FORCE_CREATE, $versiondata);
$dbi->touch();
$mesg->pushContent(' ', fmt("- saved to database as version %d",
$new->getVersion()));
}
if ($needs_merge) {
$f = $source;
// hackish, $source contains needed path+filename
$f = str_replace(sprintf(_("MIME file %s"), ''), '', $f);
$f = str_replace(sprintf(_("Serialized file %s"), ''), '', $f);
$f = str_replace(sprintf(_("plain file %s"), ''), '', $f);
//check if uploaded file? they pass just the content, but the file is gone
if (@stat($f)) {
global $WikiTheme;
$meb = Button(array('action' => 'loadfile',
'merge'=> true,
'source'=> $f),
_("Merge Edit"),
_("PhpWikiAdministration"),
'wikiadmin');
$owb = Button(array('action' => 'loadfile',
'overwrite'=> true,
'source'=> $f),
_("Restore Anyway"),
_("PhpWikiAdministration"),
'wikiunsafe');
$mesg->pushContent(' ', $meb, " ", $owb);
if (!$overwite_all) {
$args = $request->getArgs();
$args['overwrite'] = 1;
$owb = Button($args,
_("Overwrite All"),
_("PhpWikiAdministration"),
'wikiunsafe');
$mesg->pushContent(HTML::div(array('class' => 'hint'), $owb));
$overwite_all = true;
}
} else {
$mesg->pushContent(HTML::em(_(" Sorry, cannot merge.")));
}
}
if (!isa($request,'MockRequest')) {
if ($skip)
PrintXML(HTML::dt(HTML::em(WikiLink($pagename))), $mesg);
else
PrintXML($mesg);
flush();
}
}
// action=revert (by diff)
function RevertPage (&$request)
{
$mesg = HTML::dd();
$pagename = $request->getArg('pagename');
$version = $request->getArg('version');
if (!$version) {
PrintXML(HTML::dt(fmt("Revert")," ",WikiLink($pagename)),
HTML::dd(_("missing required version argument")));
return;
}
$dbi =& $request->_dbi;
$page = $dbi->getPage($pagename);
$current = $page->getCurrentRevision();
$currversion = $current->getVersion();
if ($currversion == 0) {
$mesg->pushContent(' ', _("no page content"));
PrintXML(HTML::dt(fmt("Revert")," ",WikiLink($pagename)),
$mesg);
flush();
return;
}
if ($currversion == $version) {
$mesg->pushContent(' ', _("same version page"));
PrintXML(HTML::dt(fmt("Revert")," ",WikiLink($pagename)),
$mesg);
flush();
return;
}
if ($request->getArg('cancel')) {
$mesg->pushContent(' ', _("Cancelled"));
PrintXML(HTML::dt(fmt("Revert")," ",WikiLink($pagename)),
$mesg);
flush();
return;
}
if (!$request->getArg('verify')) {
$mesg->pushContent(HTML::br(),
_("Are you sure?"),
HTML::br(),
HTML::form(array('action' => $request->getPostURL(),
'method' => 'post'),
HiddenInputs($request->getArgs(), false, array('verify')),
HiddenInputs(array('verify' => 1)),
Button('submit:verify', _("Yes"), 'button'),
HTML::Raw(' '),
Button('submit:cancel', _("Cancel"), 'button')),
HTML::hr());
$rev = $page->getRevision($version);
$html = HTML(HTML::dt(fmt("Revert %s to version $version", WikiLink($pagename))),
$mesg,
$rev->getTransformedContent());
$template = Template('browse',
array('CONTENT' => $html));
GeneratePage($template, $pagename, $rev);
$request->checkValidators();
flush();
return;
}
$rev = $page->getRevision($version);
$content = $rev->getPackedContent();
$versiondata = $rev->_data;
$versiondata['summary'] = sprintf(_("revert to version %d"), $version);
$new = $page->save($content, $currversion + 1, $versiondata);
$dbi->touch();
$pagelink = WikiLink($pagename);
$mesg->pushContent(fmt("Revert: %s", $pagelink),
fmt("- version %d saved to database as version %d",
$version, $new->getVersion()));
// Force browse of current page version.
$request->setArg('version', false);
$template = Template('savepage', array());
$template->replace('CONTENT', $new->getTransformedContent());
GeneratePage($template, $mesg, $new);
flush();
}
function _tryinsertInterWikiMap($content) {
$goback = false;
if (strpos($content, "<verbatim>")) {
//$error_html = " The newly loaded pgsrc already contains a verbatim block.";
$goback = true;
}
if (!$goback && !defined('INTERWIKI_MAP_FILE')) {
$error_html = sprintf(" "._("%s: not defined"), "INTERWIKI_MAP_FILE");
$goback = true;
}
$mapfile = FindFile(INTERWIKI_MAP_FILE,1);
if (!$goback && !file_exists($mapfile)) {
$error_html = sprintf(" "._("%s: file not found"), INTERWIKI_MAP_FILE);
$goback = true;
}
if (!empty($error_html))
trigger_error(_("Default InterWiki map file not loaded.")
. $error_html, E_USER_NOTICE);
if ($goback)
return $content;
// if loading from virgin setup do echo, otherwise trigger_error E_USER_NOTICE
if (!isa($GLOBALS['request'], 'MockRequest'))
echo sprintf(_("Loading InterWikiMap from external file %s."), $mapfile),"<br />";
$fd = fopen ($mapfile, "rb");
$data = fread ($fd, filesize($mapfile));
fclose ($fd);
$content = $content . "\n<verbatim>\n$data</verbatim>\n";
return $content;
}
function ParseSerializedPage($text, $default_pagename, $user)
{
if (!preg_match('/^a:\d+:{[si]:\d+/', $text))
return false;
$pagehash = unserialize($text);
// Split up pagehash into four parts:
// pagename
// content
// page-level meta-data
// revision-level meta-data
if (!defined('FLAG_PAGE_LOCKED'))
define('FLAG_PAGE_LOCKED', 1);
$pageinfo = array('pagedata' => array(),
'versiondata' => array());
$pagedata = &$pageinfo['pagedata'];
$versiondata = &$pageinfo['versiondata'];
// Fill in defaults.
if (empty($pagehash['pagename']))
$pagehash['pagename'] = $default_pagename;
if (empty($pagehash['author'])) {
$pagehash['author'] = $user->getId();
}
foreach ($pagehash as $key => $value) {
switch($key) {
case 'pagename':
case 'version':
case 'hits':
$pageinfo[$key] = $value;
break;
case 'content':
$pageinfo[$key] = join("\n", $value);
break;
case 'flags':
if (($value & FLAG_PAGE_LOCKED) != 0)
$pagedata['locked'] = 'yes';
break;
case 'owner':
case 'created':
$pagedata[$key] = $value;
break;
case 'acl':
case 'perm':
$pagedata['perm'] = ParseMimeifiedPerm($value);
break;
case 'lastmodified':
$versiondata['mtime'] = $value;
break;
case 'author':
case 'author_id':
case 'summary':
$versiondata[$key] = $value;
break;
}
}
if (empty($pagehash['charset']))
$pagehash['charset'] = 'iso-8859-1';
// compare to target charset
if (strtolower($pagehash['charset']) != strtolower($GLOBALS['charset'])) {
$pageinfo['content'] = charset_convert($params['charset'], $GLOBALS['charset'], $pageinfo['content']);
$pageinfo['pagename'] = charset_convert($params['charset'], $GLOBALS['charset'], $pageinfo['pagename']);
}
return $pageinfo;
}
function SortByPageVersion ($a, $b) {
return $a['version'] - $b['version'];
}
/**
* Security alert! We should not allow to import config.ini into our wiki (or from a sister wiki?)
* because the sql passwords are in plaintext there. And the webserver must be able to read it.
* Detected by Santtu Jarvi.
*/
function LoadFile (&$request, $filename, $text = false, $mtime = false)
{
if (preg_match("/config$/", dirname($filename)) // our or other config
and preg_match("/config.*\.ini/", basename($filename))) // backups and other versions also
{
trigger_error(sprintf("Refused to load %s", $filename), E_USER_WARNING);
return;
}
if (!is_string($text)) {
// Read the file.
$stat = stat($filename);
$mtime = $stat[9];
$text = implode("", file($filename));
}
if (! $request->getArg('start_debug')) @set_time_limit(30); // Reset watchdog
else @set_time_limit(240);
// FIXME: basename("filewithnoslashes") seems to return garbage sometimes.
$basename = basename("/dummy/" . $filename);
if (!$mtime)
$mtime = time(); // Last resort.
// DONE: check source - target charset for content and pagename
// but only for pgsrc'ed content, not from the browser.
$default_pagename = rawurldecode($basename);
if ( ($parts = ParseMimeifiedPages($text)) ) {
if (count($parts) > 1)
$overwrite = $request->getArg('overwrite');
usort($parts, 'SortByPageVersion');
foreach ($parts as $pageinfo) {
// force overwrite
if (count($parts) > 1)
$request->setArg('overwrite', 1);
SavePage($request, $pageinfo, sprintf(_("MIME file %s"),
$filename), $basename);
}
if (count($parts) > 1)
if ($overwrite)
$request->setArg('overwrite', $overwrite);
else
unset($request->_args['overwrite']);
}
else if ( ($pageinfo = ParseSerializedPage($text, $default_pagename,
$request->getUser())) ) {
SavePage($request, $pageinfo, sprintf(_("Serialized file %s"),
$filename), $basename);
}
else {
// plain old file
$user = $request->getUser();
$file_charset = 'iso-8859-1';
// compare to target charset
if ($file_charset != strtolower($GLOBALS['charset'])) {
$text = charset_convert($file_charset, $GLOBALS['charset'], $text);
$default_pagename = charset_convert($file_charset, $GLOBALS['charset'], $default_pagename);
}
// Assume plain text file.
$pageinfo = array('pagename' => $default_pagename,
'pagedata' => array(),
'versiondata'
=> array('author' => $user->getId()),
'content' => preg_replace('/[ \t\r]*\n/', "\n",
chop($text))
);
SavePage($request, $pageinfo, sprintf(_("plain file %s"), $filename),
$basename);
}
}
function LoadZip (&$request, $zipfile, $files = false, $exclude = false) {
$zip = new ZipReader($zipfile);
$timeout = (! $request->getArg('start_debug')) ? 20 : 120;
while (list ($fn, $data, $attrib) = $zip->readFile()) {
// FIXME: basename("filewithnoslashes") seems to return
// garbage sometimes.
$fn = basename("/dummy/" . $fn);
if ( ($files && !in_array($fn, $files))
|| ($exclude && in_array($fn, $exclude)) ) {
PrintXML(HTML::dt(WikiLink($fn)),
HTML::dd(_("Skipping")));
flush();
continue;
}
longer_timeout($timeout); // longer timeout per page
LoadFile($request, $fn, $data, $attrib['mtime']);
}
}
function LoadDir (&$request, $dirname, $files = false, $exclude = false) {
$fileset = new LimitedFileSet($dirname, $files, $exclude);
if (!$files and ($skiplist = $fileset->getSkippedFiles())) {
PrintXML(HTML::dt(HTML::strong(_("Skipping"))));
$list = HTML::ul();
foreach ($skiplist as $file)
$list->pushContent(HTML::li(WikiLink($file)));
PrintXML(HTML::dd($list));
}
// Defer HomePage loading until the end. If anything goes wrong
// the pages can still be loaded again.
$files = $fileset->getFiles();
if (in_array(HOME_PAGE, $files)) {
$files = array_diff($files, array(HOME_PAGE));
$files[] = HOME_PAGE;
}
$timeout = (! $request->getArg('start_debug')) ? 20 : 120;
foreach ($files as $file) {
longer_timeout($timeout); // longer timeout per page
if (substr($file,-1,1) != '~') // refuse to load backup files
LoadFile($request, "$dirname/$file");
}
}
class LimitedFileSet extends FileSet {
function LimitedFileSet($dirname, $_include, $exclude) {
$this->_includefiles = $_include;
$this->_exclude = $exclude;
$this->_skiplist = array();
parent::FileSet($dirname);
}
function _filenameSelector($fn) {
$incl = &$this->_includefiles;
$excl = &$this->_exclude;
if ( ($incl && !in_array($fn, $incl))
|| ($excl && in_array($fn, $excl)) ) {
$this->_skiplist[] = $fn;
return false;
} else {
return true;
}
}
function getSkippedFiles () {
return $this->_skiplist;
}
}
function IsZipFile ($filename_or_fd)
{
// See if it looks like zip file
if (is_string($filename_or_fd))
{
$fd = fopen($filename_or_fd, "rb");
$magic = fread($fd, 4);
fclose($fd);
}
else
{
$fpos = ftell($filename_or_fd);
$magic = fread($filename_or_fd, 4);
fseek($filename_or_fd, $fpos);
}
return $magic == ZIP_LOCHEAD_MAGIC || $magic == ZIP_CENTHEAD_MAGIC;
}
function LoadAny (&$request, $file_or_dir, $files = false, $exclude = false)
{
// Try urlencoded filename for accented characters.
if (!file_exists($file_or_dir)) {
// Make sure there are slashes first to avoid confusing phps
// with broken dirname or basename functions.
// FIXME: windows uses \ and :
if (is_integer(strpos($file_or_dir, "/"))) {
$newfile = FindFile($file_or_dir, true);
// Panic. urlencoded by the browser (e.g. San%20Diego => San Diego)
if (!$newfile)
$file_or_dir = dirname($file_or_dir) . "/"
. rawurlencode(basename($file_or_dir));
} else {
// This is probably just a file.
$file_or_dir = rawurlencode($file_or_dir);
}
}
$type = filetype($file_or_dir);
if ($type == 'link') {
// For symbolic links, use stat() to determine
// the type of the underlying file.
list(,,$mode) = stat($file_or_dir);
$type = ($mode >> 12) & 017;
if ($type == 010)
$type = 'file';
elseif ($type == 004)
$type = 'dir';
}
if (! $type) {
$request->finish(fmt("Empty or not existing source. Unable to load: %s", $file_or_dir));
}
else if ($type == 'dir') {
LoadDir($request, $file_or_dir, $files, $exclude);
}
else if ($type != 'file' && !preg_match('/^(http|ftp):/', $file_or_dir))
{
$request->finish(fmt("Bad file type: %s", $type));
}
else if (IsZipFile($file_or_dir)) {
LoadZip($request, $file_or_dir, $files, $exclude);
}
else /* if (!$files || in_array(basename($file_or_dir), $files)) */
{
LoadFile($request, $file_or_dir);
}
}
function LoadFileOrDir (&$request)
{
$source = $request->getArg('source');
$finder = new FileFinder;
$source = $finder->slashifyPath($source);
$page = rawurldecode(basename($source));
StartLoadDump($request, fmt("Loading '%s'",
HTML(dirname($source),
dirname($source) ? "/" : "",
WikiLink($page,'auto'))));
echo "<dl>\n";
LoadAny($request, $source);
echo "</dl>\n";
EndLoadDump($request);
}
/**
* HomePage was not found so first-time install is supposed to run.
* - import all pgsrc pages.
* - Todo: installer interface to edit config/config.ini settings
* - Todo: ask for existing old index.php to convert to config/config.ini
* - Todo: theme-specific pages:
* blog - HomePage, ADMIN_USER/Blogs
*/
function SetupWiki (&$request)
{
global $GenericPages, $LANG;
//FIXME: This is a hack (err, "interim solution")
// This is a bogo-bogo-login: Login without
// saving login information in session state.
// This avoids logging in the unsuspecting
// visitor as "The PhpWiki programming team".
//
// This really needs to be cleaned up...
// (I'm working on it.)
$real_user = $request->_user;
if (ENABLE_USER_NEW)
$request->_user = new _BogoUser(_("The PhpWiki programming team"));
else
$request->_user = new WikiUser($request, _("The PhpWiki programming team"),
WIKIAUTH_BOGO);
StartLoadDump($request, _("Loading up virgin wiki"));
echo "<dl>\n";
$pgsrc = FindLocalizedFile(WIKI_PGSRC);
$default_pgsrc = FindFile(DEFAULT_WIKI_PGSRC);
$request->setArg('overwrite', true);
if ($default_pgsrc != $pgsrc) {
LoadAny($request, $default_pgsrc, $GenericPages);
}
$request->setArg('overwrite', false);
LoadAny($request, $pgsrc);
$dbi =& $request->_dbi;
// Ensure that all mandatory pages are loaded
$finder = new FileFinder;
foreach (array_merge(explode(':','Help/OldTextFormattingRules:Help/TextFormattingRules:PhpWikiAdministration'),
$GLOBALS['AllActionPages'],
array(constant('HOME_PAGE'))) as $f)
{
$page = gettext($f);
$epage = urlencode($page);
if (! $dbi->isWikiPage($page) ) {
// translated version provided?
if ($lf = FindLocalizedFile($pgsrc . $finder->_pathsep . $epage, 1)) {
LoadAny($request, $lf);
} else { // load english version of required action page
LoadAny($request, FindFile(DEFAULT_WIKI_PGSRC . $finder->_pathsep . urlencode($f)));
$page = $f;
}
}
if (! $dbi->isWikiPage($page)) {
trigger_error(sprintf("Mandatory file %s couldn't be loaded!", $page),
E_USER_WARNING);
}
}
echo "</dl>\n";
$pagename = _("InterWikiMap");
$map = $dbi->getPage($pagename);
$map->set('locked', true);
PrintXML(HTML::dt(HTML::em(WikiLink($pagename))), HTML::dd("locked"));
EndLoadDump($request);
}
function LoadPostFile (&$request)
{
$upload = $request->getUploadedFile('file');
if (!$upload)
$request->finish(_("No uploaded file to upload?")); // FIXME: more concise message
// Dump http headers.
StartLoadDump($request, sprintf(_("Uploading %s"), $upload->getName()));
echo "<dl>\n";
$fd = $upload->open();
if (IsZipFile($fd))
LoadZip($request, $fd, false, array(_("RecentChanges")));
else
LoadFile($request, $upload->getName(), $upload->getContents());
echo "</dl>\n";
EndLoadDump($request);
}
/**
$Log: loadsave.php,v $
Revision 1.153 2007/05/28 20:54:40 rurban
fix DumpToHtml creating dirs
Revision 1.152 2007/05/01 16:22:41 rurban
lock InterWikiMap on init
Revision 1.151 2007/02/17 14:17:34 rurban
only media=print css for htmldump and pdf
Revision 1.150 2007/01/20 15:53:42 rurban
Use WikiPagename treatment for imported pagenames
Revision 1.149 2007/01/03 21:25:10 rurban
Use convert_charset()
Revision 1.148 2007/01/02 13:21:57 rurban
omit want_content if not necessary. support keep_old and overwrite buttons
Revision 1.147 2006/12/22 17:44:15 rurban
support importing foreign charsets. e.g latin1 => utf8
Revision 1.146 2006/12/17 18:35:23 rurban
Create the right subdirectory name, urlencoded.
Revision 1.145 2006/09/06 06:01:18 rurban
support loadfile multipart archives automatically
Revision 1.144 2006/08/25 22:06:13 rurban
args fix to pass $args to the template
Revision 1.143 2006/08/25 21:48:39 rurban
dumphtml subpages
Revision 1.142 2006/03/19 17:16:32 rurban
remove remaining cruft
Revision 1.141 2006/03/19 17:11:32 rurban
add verify to RevertPage, display reverted page as template
Revision 1.140 2006/03/07 20:45:43 rurban
wikihash for php-5.1
Revision 1.139 2005/08/27 18:02:43 rurban
fix and expand pages
Revision 1.138 2005/08/27 09:39:10 rurban
dumphtml when not at admin page: dump the current or given page
Revision 1.137 2005/01/30 23:14:38 rurban
simplify page names
Revision 1.136 2005/01/25 07:07:24 rurban
remove body tags in html dumps, add css and images to zipdumps, simplify printing
Revision 1.135 2004/12/26 17:17:25 rurban
announce dumps - mult.requests to avoid request::finish, e.g. LinkDatabase, PdfOut, ...
Revision 1.134 2004/12/20 16:05:01 rurban
gettext msg unification
Revision 1.133 2004/12/08 12:57:41 rurban
page-specific timeouts for long multi-page requests
Revision 1.132 2004/12/08 01:18:33 rurban
Disallow loading config*.ini files. Detected by Santtu Jarvi.
Revision 1.131 2004/11/30 17:48:38 rurban
just comments
Revision 1.130 2004/11/25 08:28:12 rurban
dont fatal on missing css or imgfiles and actually print the miss
Revision 1.129 2004/11/25 08:11:40 rurban
pass exclude to the get_all_pages backend
Revision 1.128 2004/11/16 16:16:44 rurban
enable Overwrite All for upgrade
Revision 1.127 2004/11/01 10:43:57 rurban
seperate PassUser methods into seperate dir (memory usage)
fix WikiUser (old) overlarge data session
remove wikidb arg from various page class methods, use global ->_dbi instead
...
Revision 1.126 2004/10/16 15:13:39 rurban
new [Overwrite All] button
Revision 1.125 2004/10/14 19:19:33 rurban
loadsave: check if the dumped file will be accessible from outside.
and some other minor fixes. (cvsclient native not yet ready)
Revision 1.124 2004/10/04 23:44:28 rurban
for older or CGI phps
Revision 1.123 2004/09/25 16:26:54 rurban
deferr notifies (to be improved)
Revision 1.122 2004/09/17 14:25:45 rurban
update comments
Revision 1.121 2004/09/08 13:38:00 rurban
improve loadfile stability by using markup=2 as default for undefined markup-style.
use more refs for huge objects.
fix debug=static issue in WikiPluginCached
Revision 1.120 2004/07/08 19:04:42 rurban
more unittest fixes (file backend, metadata RatingsDb)
Revision 1.119 2004/07/08 15:23:59 rurban
less verbose for tests
Revision 1.118 2004/07/08 13:50:32 rurban
various unit test fixes: print error backtrace on _DEBUG_TRACE; allusers fix; new PHPWIKI_NOMAIN constant for omitting the mainloop
Revision 1.117 2004/07/02 09:55:58 rurban
more stability fixes: new DISABLE_GETIMAGESIZE if your php crashes when loading LinkIcons: failing getimagesize in old phps; blockparser stabilized
Revision 1.116 2004/07/01 09:05:41 rurban
support pages and exclude arguments for all 4 dump methods
Revision 1.115 2004/07/01 08:51:22 rurban
dumphtml: added exclude, print pagename before processing
Revision 1.114 2004/06/28 12:51:41 rurban
improved dumphtml and virgin setup
Revision 1.113 2004/06/27 10:26:02 rurban
oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
Revision 1.112 2004/06/25 14:29:20 rurban
WikiGroup refactoring:
global group attached to user, code for not_current user.
improved helpers for special groups (avoid double invocations)
new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
fixed a XHTML validation error on userprefs.tmpl
Revision 1.111 2004/06/21 16:38:55 rurban
fixed the StartLoadDump html argument hack.
Revision 1.110 2004/06/21 16:22:30 rurban
add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
fixed dumping buttons locally (images/buttons/),
support pages arg for dumphtml,
optional directory arg for dumpserial + dumphtml,
fix a AllPages warning,
show dump warnings/errors on DEBUG,
don't warn just ignore on wikilens pagelist columns, if not loaded.
RateIt pagelist column is called "rating", not "ratingwidget" (Dan?)
Revision 1.109 2004/06/17 11:31:05 rurban
jump back to label after dump/upgrade
Revision 1.108 2004/06/16 12:43:01 rurban
4.0.6 cannot use this errorhandler (not found)
Revision 1.107 2004/06/14 11:31:37 rurban
renamed global $Theme to $WikiTheme (gforge nameclash)
inherit PageList default options from PageList
default sortby=pagename
use options in PageList_Selectable (limit, sortby, ...)
added action revert, with button at action=diff
added option regex to WikiAdminSearchReplace
Revision 1.106 2004/06/13 13:54:25 rurban
Catch fatals on the four dump calls (as file and zip, as html and mimified)
FoafViewer: Check against external requirements, instead of fatal.
Change output for xhtmldumps: using file:// urls to the local fs.
Catch SOAP fatal by checking for GOOGLE_LICENSE_KEY
Import GOOGLE_LICENSE_KEY and FORTUNE_DIR from config.ini.
Revision 1.105 2004/06/08 19:48:16 rurban
fixed foreign setup: no ugly skipped msg for the GenericPages, load english actionpages if translated not found
Revision 1.104 2004/06/08 13:51:57 rurban
some comments only
Revision 1.103 2004/06/08 10:54:46 rurban
better acl dump representation, read back acl and owner
Revision 1.102 2004/06/06 16:58:51 rurban
added more required ActionPages for foreign languages
install now english ActionPages if no localized are found. (again)
fixed default anon user level to be 0, instead of -1
(wrong "required administrator to view this page"...)
Revision 1.101 2004/06/04 20:32:53 rurban
Several locale related improvements suggested by Pierrick Meignen
LDAP fix by John Cole
reenable admin check without ENABLE_PAGEPERM in the admin plugins
Revision 1.100 2004/05/02 21:26:38 rurban
limit user session data (HomePageHandle and auth_dbi have to invalidated anyway)
because they will not survive db sessions, if too large.
extended action=upgrade
some WikiTranslation button work
revert WIKIAUTH_UNOBTAINABLE (need it for main.php)
some temp. session debug statements
Revision 1.99 2004/05/02 15:10:07 rurban
new finally reliable way to detect if /index.php is called directly
and if to include lib/main.php
new global AllActionPages
SetupWiki now loads all mandatory pages: HOME_PAGE, action pages, and warns if not.
WikiTranslation what=buttons for Carsten to create the missing MacOSX buttons
PageGroupTestOne => subpages
renamed PhpWikiRss to PhpWikiRecentChanges
more docs, default configs, ...
Revision 1.98 2004/04/29 23:25:12 rurban
re-ordered locale init (as in 1.3.9)
fixed loadfile with subpages, and merge/restore anyway
(sf.net bug #844188)
Revision 1.96 2004/04/19 23:13:03 zorloc
Connect the rest of PhpWiki to the IniConfig system. Also the keyword regular expression is not a config setting
Revision 1.95 2004/04/18 01:11:52 rurban
more numeric pagename fixes.
fixed action=upload with merge conflict warnings.
charset changed from constant to global (dynamic utf-8 switching)
Revision 1.94 2004/03/14 16:36:37 rurban
dont load backup files
Revision 1.93 2004/02/26 03:22:05 rurban
also copy css and images with XHTML Dump
Revision 1.92 2004/02/26 02:25:54 rurban
fix empty and #-anchored links in XHTML Dumps
Revision 1.91 2004/02/24 17:19:37 rurban
debugging helpers only
Revision 1.90 2004/02/24 17:09:24 rurban
fixed \r\r\n with dumping on windows
Revision 1.88 2004/02/22 23:20:31 rurban
fixed DumpHtmlToDir,
enhanced sortby handling in PageList
new button_heading th style (enabled),
added sortby and limit support to the db backends and plugins
for paging support (<<prev, next>> links on long lists)
Revision 1.87 2004/01/26 09:17:49 rurban
* changed stored pref representation as before.
the array of objects is 1) bigger and 2)
less portable. If we would import packed pref
objects and the object definition was changed, PHP would fail.
This doesn't happen with an simple array of non-default values.
* use $prefs->retrieve and $prefs->store methods, where retrieve
understands the interim format of array of objects also.
* simplified $prefs->get() and fixed $prefs->set()
* added $user->_userid and class '_WikiUser' portability functions
* fixed $user object ->_level upgrading, mostly using sessions.
this fixes yesterdays problems with loosing authorization level.
* fixed WikiUserNew::checkPass to return the _level
* fixed WikiUserNew::isSignedIn
* added explodePageList to class PageList, support sortby arg
* fixed UserPreferences for WikiUserNew
* fixed WikiPlugin for empty defaults array
* UnfoldSubpages: added pagename arg, renamed pages arg,
removed sort arg, support sortby arg
Revision 1.86 2003/12/02 16:18:26 carstenklapp
Minor enhancement: Provide more meaningful filenames for WikiDB zip
dumps & snapshots.
Revision 1.85 2003/11/30 18:18:13 carstenklapp
Minor code optimization: use include_once instead of require_once
inside functions that might not always called.
Revision 1.84 2003/11/26 20:47:47 carstenklapp
Redo bugfix: My last refactoring broke merge-edit & overwrite
functionality again, should be fixed now. Sorry.
Revision 1.83 2003/11/20 22:18:54 carstenklapp
New feature: h1 during merge-edit displays WikiLink to original page.
Internal changes: Replaced some hackish url-generation code in
function SavePage (for pgsrc merge-edit) with appropriate Button()
calls.
Revision 1.82 2003/11/18 19:48:01 carstenklapp
Fixed missing gettext _() for button name.
Revision 1.81 2003/11/18 18:28:35 carstenklapp
Bugfix: In the Load File function of PhpWikiAdministration: When doing
a "Merge Edit" or "Restore Anyway", page names containing accented
letters (such as locale/de/pgsrc/G%E4steBuch) would produce a file not
found error (Use FilenameForPage funtion to urlencode page names).
Revision 1.80 2003/03/07 02:46:57 dairiki
Omit checks for safe_mode before set_time_limit(). Just prefix the
set_time_limit() calls with @ so that they fail silently if not
supported.
Revision 1.79 2003/02/26 01:56:05 dairiki
Only zip pages with legal pagenames.
Revision 1.78 2003/02/24 02:05:43 dairiki
Fix "n bytes written" message when dumping HTML.
Revision 1.77 2003/02/21 04:12:05 dairiki
Minor fixes for new cached markup.
Revision 1.76 2003/02/16 19:47:17 dairiki
Update WikiDB timestamp when editing or deleting pages.
Revision 1.75 2003/02/15 03:04:30 dairiki
Fix for WikiUser constructor API change.
Revision 1.74 2003/02/15 02:18:04 dairiki
When default language was English (at least), pgsrc was being
loaded twice.
LimitedFileSet: Fix typo/bug. ($include was being ignored.)
SetupWiki(): Fix bugs in loading of $GenericPages.
Revision 1.73 2003/01/28 21:09:17 zorloc
The get_cfg_var() function should only be used when one is
interested in the value from php.ini or similar. Use ini_get()
instead to get the effective value of a configuration variable.
-- Martin Geisler
Revision 1.72 2003/01/03 22:25:53 carstenklapp
Cosmetic fix to "Merge Edit" & "Overwrite" buttons. Added "The PhpWiki
programming team" as author when loading from pgsrc. Source
reformatting.
Revision 1.71 2003/01/03 02:48:05 carstenklapp
function SavePage: Added loadfile options for overwriting or merge &
compare a loaded pgsrc file with an existing page.
function LoadAny: Added a general error message when unable to load a
file instead of defaulting to "Bad file type".
*/
// For emacs users
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
|