]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/loadsave.php
Hack to get valid XHTML code in Monobook (but the result is still missing the menus)
[SourceForge/phpwiki.git] / lib / loadsave.php
1 <?php //-*-php-*-
2 rcs_id('$Id$');
3
4 /*
5  Copyright 1999,2000,2001,2002,2004,2005,2006,2007 $ThePhpWikiProgrammingTeam
6  Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7
8  This file is part of PhpWiki.
9
10  PhpWiki is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  PhpWiki is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with PhpWiki; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 require_once("lib/ziplib.php");
26 require_once("lib/Template.php");
27
28 /**
29  * ignore fatal errors during dump
30  */
31 function _dump_error_handler(&$error) {
32     if ($error->isFatal()) {
33         $error->errno = E_USER_WARNING;
34         return true;
35     }
36     return true;         // Ignore error
37     /*
38     if (preg_match('/Plugin/', $error->errstr))
39         return true;
40     */
41     // let the message come through: call the remaining handlers:
42     // return false; 
43 }
44
45 function StartLoadDump(&$request, $title, $html = '')
46 {
47     // MockRequest is from the unit testsuite, a faked request. (may be cmd-line)
48     // We are silent on unittests.
49     if (isa($request,'MockRequest'))
50         return;
51     // FIXME: This is a hack. This really is the worst overall hack in phpwiki.
52     if ($html)
53         $html->pushContent('%BODY%');
54     $tmpl = Template('html', array('TITLE' => $title,
55                                    'HEADER' => $title,
56                                    'CONTENT' => $html ? $html : '%BODY%'));
57     echo ereg_replace('%BODY%.*', '', $tmpl->getExpansion($html));
58     $request->chunkOutput();
59     
60     // set marker for sendPageChangeNotification()
61     $request->_deferredPageChangeNotification = array();
62 }
63
64 function EndLoadDump(&$request)
65 {
66     global $WikiTheme;
67
68     if (isa($request,'MockRequest'))
69         return;
70     $action = $request->getArg('action');
71     $label = '';
72     switch ($action) {
73     case 'zip':        $label = _("ZIP files of database"); break;
74     case 'dumpserial': $label = _("Dump to directory"); break;
75     case 'upload':     $label = _("Upload File"); break;
76     case 'loadfile':   $label = _("Load File"); break;
77     case 'upgrade':    $label = _("Upgrade"); break;
78     case 'dumphtml': 
79     case 'ziphtml':    $label = _("Dump pages as XHTML"); break;
80     }
81     if ($label) $label = str_replace(" ","_",$label);
82     if ($action == 'browse') // loading virgin 
83         $pagelink = WikiLink(HOME_PAGE);
84     else
85         $pagelink = WikiLink(new WikiPageName(_("PhpWikiAdministration"),false,$label));
86
87     // do deferred sendPageChangeNotification()
88     if (!empty($request->_deferredPageChangeNotification)) {
89         $pages = $all_emails = $all_users = array();
90         foreach ($request->_deferredPageChangeNotification as $p) {
91             list($pagename, $emails, $userids) = $p;
92             $pages[] = $pagename;
93             $all_emails = array_unique(array_merge($all_emails, $emails));
94             $all_users = array_unique(array_merge($all_users, $userids));
95         }
96         $editedby = sprintf(_("Edited by: %s"), $request->_user->getId());
97         $content = "Loaded the following pages:\n" . join("\n", $pages);
98         if (mail(join(',',$all_emails),"[".WIKI_NAME."] "._("LoadDump"), 
99                  _("LoadDump")."\n".
100                  $editedby."\n\n".
101                  $content))
102             trigger_error(sprintf(_("PageChange Notification of %s sent to %s"),
103                                   join("\n",$pages), join(',',$all_users)), E_USER_NOTICE);
104         else
105             trigger_error(sprintf(_("PageChange Notification Error: Couldn't send %s to %s"),
106                                   join("\n",$pages), join(',',$all_users)), E_USER_WARNING);
107         unset($pages);
108         unset($all_emails);
109         unset($all_users);
110     }
111     unset($request->_deferredPageChangeNotification);
112
113     PrintXML(HTML::p(HTML::strong(_("Complete."))),
114              HTML::p(fmt("Return to %s", $pagelink)));
115     // Ugly hack to get valid XHTML code
116     if (isa($WikiTheme, 'WikiTheme_gforge')) {
117         echo "</div>\n";
118         echo "</td></tr>\n";
119         echo "</table>\n";
120         echo "</td></tr>\n";
121         echo "</table>\n";
122     }
123     if (isa($WikiTheme, 'WikiTheme_MonoBook')) {
124         echo "</div>\n";
125         echo "</div>\n";
126         echo "</div>\n";
127         echo "</div>\n";
128     }
129     echo "</body></html>\n";
130 }
131
132
133 ////////////////////////////////////////////////////////////////
134 //
135 //  Functions for dumping.
136 //
137 ////////////////////////////////////////////////////////////////
138
139 /**
140  * For reference see:
141  * http://www.nacs.uci.edu/indiv/ehood/MIME/2045/rfc2045.html
142  * http://www.faqs.org/rfcs/rfc2045.html
143  * (RFC 1521 has been superceeded by RFC 2045 & others).
144  *
145  * Also see http://www.faqs.org/rfcs/rfc2822.html
146  */
147 function MailifyPage ($page, $nversions = 1)
148 {
149     $current = $page->getCurrentRevision(false);
150     $head = '';
151
152     if (STRICT_MAILABLE_PAGEDUMPS) {
153         $from = defined('SERVER_ADMIN') ? SERVER_ADMIN : 'foo@bar';
154         //This is for unix mailbox format: (not RFC (2)822)
155         // $head .= "From $from  " . CTime(time()) . "\r\n";
156         $head .= "Subject: " . rawurlencode($page->getName()) . "\r\n";
157         $head .= "From: $from (PhpWiki)\r\n";
158         // RFC 2822 requires only a Date: and originator (From:)
159         // field, however the obsolete standard RFC 822 also
160         // requires a destination field.
161         $head .= "To: $from (PhpWiki)\r\n";
162     }
163     $head .= "Date: " . Rfc2822DateTime($current->get('mtime')) . "\r\n";
164     $head .= sprintf("Mime-Version: 1.0 (Produced by PhpWiki %s)\r\n",
165                      PHPWIKI_VERSION);
166
167     // This should just be entered by hand (or by script?)
168     // in the actual pgsrc files, since only they should have
169     // RCS ids.
170     //$head .= "X-Rcs-Id: \$Id\$\r\n";
171
172     $iter = $page->getAllRevisions();
173     $parts = array();
174     while ($revision = $iter->next()) {
175         $parts[] = MimeifyPageRevision($page, $revision);
176         if ($nversions > 0 && count($parts) >= $nversions)
177             break;
178     }
179     if (count($parts) > 1)
180         return $head . MimeMultipart($parts);
181     assert($parts);
182     return $head . $parts[0];
183 }
184
185 /***
186  * Compute filename to used for storing contents of a wiki page.
187  *
188  * Basically we do a rawurlencode() which encodes everything except
189  * ASCII alphanumerics and '.', '-', and '_'.
190  *
191  * But we also want to encode leading dots to avoid filenames like
192  * '.', and '..'. (Also, there's no point in generating "hidden" file
193  * names, like '.foo'.)
194  *
195  * We have to apply a different "/" logic for dumpserial, htmldump and zipdump.
196  * dirs are allowed for zipdump and htmldump, not for dumpserial
197  * 
198  *
199  * @param $pagename string Pagename.
200  * @return string Filename for page.
201  */
202 function FilenameForPage ($pagename, $action = false)
203 {
204     $enc = rawurlencode($pagename);
205     if (!$action) {
206         global $request;
207         $action = $request->getArg('action');
208     }
209     if ($action != 'dumpserial') { // zip, ziphtml, dumphtml
210         // For every %2F we will need to mkdir -p dirname($pagename)
211         $enc = preg_replace('/%2F/', '/', $enc);
212     }
213     $enc = preg_replace('/^\./', '%2E', $enc);
214     $enc = preg_replace('/%20/', ' ',   $enc);
215     $enc = preg_replace('/\.$/', '%2E', $enc);
216     return $enc;
217 }
218
219 /**
220  * The main() function which generates a zip archive of a PhpWiki.
221  *
222  * If $include_archive is false, only the current version of each page
223  * is included in the zip file; otherwise all archived versions are
224  * included as well.
225  */
226 function MakeWikiZip (&$request)
227 {
228     if ($request->getArg('include') == 'all') {
229         $zipname         = WIKI_NAME . _("FullDump") . date('Ymd-Hi') . '.zip';
230         $include_archive = true;
231     }
232     else {
233         $zipname         = WIKI_NAME . _("LatestSnapshot") . date('Ymd-Hi') . '.zip';
234         $include_archive = false;
235     }
236     $include_empty = false;
237     if ($request->getArg('include') == 'empty') {
238         $include_empty = true;
239     }
240
241     $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
242
243     /* ignore fatals in plugins */
244     if (check_php_version(4,1)) {
245         global $ErrorManager;
246         $ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
247     }
248
249     $dbi =& $request->_dbi;
250     $thispage = $request->getArg('pagename'); // for "Return to ..."
251     if ($exclude = $request->getArg('exclude')) {   // exclude which pagenames
252         $excludeList = explodePageList($exclude); 
253     } else {
254         $excludeList = array();
255     }
256     if ($pages = $request->getArg('pages')) {  // which pagenames
257         if ($pages == '[]') // current page
258             $pages = $thispage;
259         $page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
260     } else {
261         $page_iter = $dbi->getAllPages(false,false,false,$excludeList);
262     }
263     $request_args = $request->args;
264     $timeout = (! $request->getArg('start_debug')) ? 30 : 240;
265     
266     while ($page = $page_iter->next()) {
267         $request->args = $request_args; // some plugins might change them (esp. on POST)
268         longer_timeout($timeout);       // Reset watchdog
269
270         $current = $page->getCurrentRevision();
271         if ($current->getVersion() == 0)
272             continue;
273
274         $pagename = $page->getName();
275         $wpn = new WikiPageName($pagename);
276         if (!$wpn->isValid())
277             continue;
278         if (in_array($page->getName(), $excludeList)) {
279             continue;
280         }
281
282         $attrib = array('mtime'    => $current->get('mtime'),
283                         'is_ascii' => 1);
284         if ($page->get('locked'))
285             $attrib['write_protected'] = 1;
286
287         if ($include_archive)
288             $content = MailifyPage($page, 0);
289         else
290             $content = MailifyPage($page);
291
292         $zip->addRegularFile( FilenameForPage($pagename),
293                               $content, $attrib);
294     }
295     $zip->finish();
296     if (check_php_version(4,1)) {
297         global $ErrorManager;
298         $ErrorManager->popErrorHandler();
299     }
300 }
301
302 function DumpToDir (&$request)
303 {
304     $directory = $request->getArg('directory');
305     if (empty($directory))
306         $directory = DEFAULT_DUMP_DIR; // See lib/plugin/WikiForm.php:87
307     if (empty($directory))
308         $request->finish(_("You must specify a directory to dump to"));
309
310     // see if we can access the directory the user wants us to use
311     if (! file_exists($directory)) {
312         if (! mkdir($directory, 0755))
313             $request->finish(fmt("Cannot create directory '%s'", $directory));
314         else
315             $html = HTML::p(fmt("Created directory '%s' for the page dump...",
316                                 $directory));
317     } else {
318         $html = HTML::p(fmt("Using directory '%s'", $directory));
319     }
320
321     StartLoadDump($request, _("Dumping Pages"), $html);
322
323     $dbi =& $request->_dbi;
324     $thispage = $request->getArg('pagename'); // for "Return to ..."
325     if ($exclude = $request->getArg('exclude')) {   // exclude which pagenames
326         $excludeList = explodePageList($exclude); 
327     } else {
328         $excludeList = array();
329     }
330     $include_empty = false;
331     if ($request->getArg('include') == 'empty') {
332         $include_empty = true;
333     }
334     if ($pages = $request->getArg('pages')) {  // which pagenames
335         if ($pages == '[]') // current page
336             $pages = $thispage;
337         $page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
338     } else {
339         $page_iter = $dbi->getAllPages($include_empty,false,false,$excludeList);
340     }
341
342     $request_args = $request->args;
343     $timeout = (! $request->getArg('start_debug')) ? 30 : 240;
344
345     while ($page = $page_iter->next()) {
346         $request->args = $request_args; // some plugins might change them (esp. on POST)
347         longer_timeout($timeout);       // Reset watchdog
348
349         $pagename = $page->getName();
350         if (!isa($request,'MockRequest')) {
351             PrintXML(HTML::br(), $pagename, ' ... ');
352             flush();
353         }
354
355         if (in_array($pagename, $excludeList)) {
356             if (!isa($request, 'MockRequest')) {
357                 PrintXML(_("Skipped."));
358                 flush();
359             }
360             continue;
361         }
362         $filename = FilenameForPage($pagename);
363         $msg = HTML();
364         if($page->getName() != $filename) {
365             $msg->pushContent(HTML::small(fmt("saved as %s", $filename)),
366                               " ... ");
367         }
368
369         if ($request->getArg('include') == 'all')
370             $data = MailifyPage($page, 0);
371         else
372             $data = MailifyPage($page);
373
374         if ( !($fd = fopen($directory."/".$filename, "wb")) ) {
375             $msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
376                                                "$directory/$filename")));
377             $request->finish($msg);
378         }
379
380         $num = fwrite($fd, $data, strlen($data));
381         $msg->pushContent(HTML::small(fmt("%s bytes written", $num)));
382         if (!isa($request, 'MockRequest')) {
383             PrintXML($msg);
384             flush();
385         }
386         assert($num == strlen($data));
387         fclose($fd);
388     }
389
390     EndLoadDump($request);
391 }
392
393 function _copyMsg($page, $smallmsg) {
394     if (!isa($GLOBALS['request'], 'MockRequest')) {
395         if ($page) $msg = HTML(HTML::br(), HTML($page), HTML::small($smallmsg));
396         else $msg = HTML::small($smallmsg);
397         PrintXML($msg);
398         flush();
399     }
400 }
401
402 function mkdir_p($pathname, $permission = 0777) {
403     $arr = explode("/", $pathname);
404     if (empty($arr)) {
405         return mkdir($pathname, $permission);
406     }
407     $s = array_shift($arr);
408     $ok = TRUE;
409     foreach ($arr as $p) {
410         $curr = "$s/$p";
411         if (!is_dir($curr))
412             $ok = mkdir($curr, $permission);
413         $s = $curr;
414         if (!$ok) return FALSE;
415     }
416     return TRUE;
417 }
418
419 /**
420  * Dump all pages as XHTML to a directory, as pagename.html.
421  * Copies all used css files to the directory, all used images to a 
422  * "images" subdirectory, and all used buttons to a "images/buttons" subdirectory.
423  * The webserver must have write permissions to these directories. 
424  *   chown httpd HTML_DUMP_DIR; chmod u+rwx HTML_DUMP_DIR 
425  * should be enough.
426  *
427  * @param string directory (optional) path to dump to. Default: HTML_DUMP_DIR
428  * @param string pages     (optional) Comma-seperated of glob-style pagenames to dump.
429  *                                    Also array of pagenames allowed.
430  * @param string exclude   (optional) Comma-seperated of glob-style pagenames to exclude
431  */
432 function DumpHtmlToDir (&$request)
433 {
434     global $WikiTheme;
435     $directory = $request->getArg('directory');
436     if (empty($directory))
437         $directory = HTML_DUMP_DIR; // See lib/plugin/WikiForm.php:87
438     if (empty($directory))
439         $request->finish(_("You must specify a directory to dump to"));
440
441     // See if we can access the directory the user wants us to use
442     if (! file_exists($directory)) {
443         if (! mkdir($directory, 0755))
444             $request->finish(fmt("Cannot create directory '%s'", $directory));
445         else
446             $html = HTML::p(fmt("Created directory '%s' for the page dump...",
447                                 $directory));
448     } else {
449         $html = HTML::p(fmt("Using directory '%s'", $directory));
450     }
451     StartLoadDump($request, _("Dumping Pages"), $html);
452     $thispage = $request->getArg('pagename'); // for "Return to ..."
453
454     $dbi =& $request->_dbi;
455     if ($exclude = $request->getArg('exclude')) {   // exclude which pagenames
456         $excludeList = explodePageList($exclude);
457     } else {
458         $excludeList = array('DebugAuthInfo', 'DebugGroupInfo', 'AuthInfo');
459     }
460     if ($pages = $request->getArg('pages')) {  // which pagenames
461         if ($pages == '[]') // current page
462             $pages = $thispage;
463         $page_iter = new WikiDB_Array_generic_iter(explodePageList($pages));
464     // not at admin page: dump only the current page
465     } elseif ($thispage != _("PhpWikiAdministration")) { 
466         $page_iter = new WikiDB_Array_generic_iter(array($thispage));
467     } else {
468         $page_iter = $dbi->getAllPages(false,false,false,$excludeList);
469     }
470
471     $WikiTheme->DUMP_MODE = 'HTML';
472     _DumpHtmlToDir($directory, $page_iter, $request->getArg('exclude'));
473     $WikiTheme->DUMP_MODE = false;
474
475     $request->setArg('pagename',$thispage); // Template::_basepage fix
476     EndLoadDump($request);
477 }
478
479 /* Known problem: any plugins or other code which echo()s text will
480  * lead to a corrupted html zip file which may produce the following
481  * errors upon unzipping:
482  *
483  * warning [wikihtml.zip]:  2401 extra bytes at beginning or within zipfile
484  * file #58:  bad zipfile offset (local header sig):  177561
485  *  (attempting to re-compensate)
486  *
487  * However, the actual wiki page data should be unaffected.
488  */
489 function MakeWikiZipHtml (&$request)
490 {
491     global $WikiTheme;
492     if ($request->getArg('zipname')) {
493         $zipname = basename($request->getArg('zipname'));
494         if (!preg_match("/\.zip$/i", $zipname))
495             $zipname .= ".zip";
496         $request->setArg('zipname', false);
497     } else {
498         $zipname = "wikihtml.zip";
499     }
500     $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
501     $dbi =& $request->_dbi;
502     $thispage = $request->getArg('pagename'); // for "Return to ..."
503     if ($pages = $request->getArg('pages')) {  // which pagenames
504         if ($pages == '[]') // current page
505             $pages = $thispage;
506         $page_iter = new WikiDB_Array_generic_iter(explodePageList($pages));
507     } else {
508         $page_iter = $dbi->getAllPages(false,false,false,$request->getArg('exclude'));
509     }
510
511     $WikiTheme->DUMP_MODE = 'ZIPHTML';
512     _DumpHtmlToDir($zip, $page_iter, $request->getArg('exclude'));
513     $WikiTheme->DUMP_MODE = false;
514 }
515
516 /*
517  * Internal html dumper. Used for dumphtml, ziphtml and pdf
518  */
519 function _DumpHtmlToDir ($target, $page_iter, $exclude = false)
520 {
521     global $WikiTheme, $request;
522     $silent = true; $zip = false; $directory = false;
523     if ($WikiTheme->DUMP_MODE == 'HTML') {
524         $directory = $target;
525         $silent = false;
526     } elseif ($WikiTheme->DUMP_MODE == 'PDFHTML') {
527         $directory = $target;
528     } elseif (is_object($target)) { // $WikiTheme->DUMP_MODE == 'ZIPHTML'
529         $zip = $target;
530     }
531         
532     $request->_TemplatesProcessed = array();
533     if ($exclude) {   // exclude which pagenames
534         $excludeList = explodePageList($exclude);
535     } else {
536         $excludeList = array('DebugAuthInfo', 'DebugGroupInfo', 'AuthInfo');
537     }
538     $WikiTheme->VALID_LINKS = array();
539     if ($request->getArg('format')) { // pagelist
540         $page_iter_sav = $page_iter;
541         foreach ($page_iter_sav->asArray() as $handle) {
542             $WikiTheme->VALID_LINKS[] = is_string($handle) ? $handle : $handle->getName();
543         }
544         $page_iter_sav->reset();
545     }
546
547     if (defined('HTML_DUMP_SUFFIX'))
548         $WikiTheme->HTML_DUMP_SUFFIX = HTML_DUMP_SUFFIX;
549     $_bodyAttr = @$WikiTheme->_MoreAttr['body'];
550     unset($WikiTheme->_MoreAttr['body']);
551
552     if (check_php_version(4,1)) {
553         global $ErrorManager;
554         $ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
555     }
556
557     // check if the dumped file will be accessible from outside
558     $doc_root = $request->get("DOCUMENT_ROOT");
559     if ($WikiTheme->DUMP_MODE == 'HTML') {
560         $ldir = NormalizeLocalFileName($directory);
561         $wikiroot = NormalizeLocalFileName('');
562         if (string_starts_with($ldir, $doc_root)) {
563             $link_prefix = substr($directory, strlen($doc_root))."/";
564         } elseif (string_starts_with($ldir, $wikiroot)) {
565             $link_prefix = NormalizeWebFileName(substr($directory, strlen($wikiroot)))."/";
566         } else {
567             $prefix = '';
568             if (isWindows()) {
569                 $prefix = '/'; // . substr($doc_root,0,2); // add drive where apache is installed
570             }
571             $link_prefix = "file://".$prefix.$directory."/";
572         }
573     } else {
574         $link_prefix = "";
575     }
576
577     $request_args = $request->args;
578     $timeout = (! $request->getArg('start_debug')) ? 60 : 240;
579     $SAVE_RCS_IDS = $GLOBALS['RCS_IDS'];
580     if ($directory) {
581         if (isWindows())
582             $directory = str_replace("\\", "/", $directory); // no Win95 support.
583         @mkdir("$directory/images");
584     }
585     $already = array();
586     $outfiles = array();
587     $already_images = array();
588     
589     while ($page = $page_iter->next()) {
590         if (is_string($page)) {
591             $pagename = $page;
592             $page = $request->_dbi->getPage($pagename);
593         } else {
594             $pagename = $page->getName();
595         }
596         if (empty($firstpage)) $firstpage = $pagename;
597         if (array_key_exists($pagename, $already))
598             continue;
599         $already[$pagename] = 1;
600         $current = $page->getCurrentRevision();
601         //if ($current->getVersion() == 0)
602         //    continue;
603
604         $request->args = $request_args; // some plugins might change them (esp. on POST)
605         longer_timeout($timeout);       // Reset watchdog
606
607         if ($zip) {
608             $attrib = array('mtime'    => $current->get('mtime'),
609                             'is_ascii' => 1);
610             if ($page->get('locked'))
611                 $attrib['write_protected'] = 1;
612         } elseif (!$silent) {
613             if (!isa($request,'MockRequest')) {
614                 PrintXML(HTML::br(), $pagename, ' ... ');
615                 flush();
616             }
617         }
618         if (in_array($pagename, $excludeList)) {
619             if (!$silent and !isa($request,'MockRequest')) {
620                 PrintXML(_("Skipped."));
621                 flush();
622             }
623             continue;
624         }
625         $relative_base = '';
626         if ($WikiTheme->DUMP_MODE == 'PDFHTML') 
627             $request->setArg('action', 'pdf');   // to omit cache headers
628         $request->setArg('pagename', $pagename); // Template::_basepage fix
629         $filename = FilenameForPage($pagename) . $WikiTheme->HTML_DUMP_SUFFIX;
630         $args = array('revision'      => $current,
631                       'CONTENT'       => $current->getTransformedContent(),
632                       'relative_base' => $relative_base);
633         // For every %2F will need to mkdir -p dirname($pagename)
634         if (preg_match("/(%2F|\/)/", $filename)) {
635             // mkdir -p and set relative base for subdir pages
636             $filename = preg_replace("/%2F/", "/", $filename);
637             $count = substr_count($filename, "/");
638             $dirname = dirname($filename);
639             if ($directory)
640                 mkdir_p($directory."/".$dirname);
641             // Fails with "XX / YY", "XX" is created, "XX / YY" cannot be written
642             // if (isWindows()) // interesting Windows bug: cannot mkdir "bla "
643             // Since dumps needs to be copied, we have to disallow this for all platforms.
644             $filename = preg_replace("/ \//", "/", $filename);
645             $relative_base = "../";
646             while ($count > 1) {
647                 $relative_base .= "../";
648                 $count--;
649             }
650             $args['relative_base'] = $relative_base;
651         }
652         $msg = HTML();
653
654         $DUMP_MODE = $WikiTheme->DUMP_MODE;
655         $data = GeneratePageasXML(new Template('browse', $request, $args),
656                                  $pagename, $current, $args);
657         $WikiTheme->DUMP_MODE = $DUMP_MODE;                      
658
659         if (preg_match_all("/<img .*?src=\"(\/.+?)\"/", $data, $m)) {
660             // fix to local relative path for uploaded images, so that pdf will work
661             foreach ($m[1] as $img_file) {
662                 $base = basename($img_file);
663                 $data = str_replace('src="'.$img_file.'"','src="images/'.$base.'"', $data);
664                 if (array_key_exists($img_file, $already_images))
665                     continue;
666                 $already_images[$img_file] = 1;
667                 // resolve src from webdata to file
668                 $src = $doc_root . $img_file;
669                 if (file_exists($src) and $base) {
670                     if ($directory) {
671                         $target = "$directory/images/$base";
672                         if (copy($src, $target)) {
673                             if (!$silent)
674                                 _copyMsg($img_file, fmt("... copied to %s", $target));
675                         } else {
676                             if (!$silent)
677                                 _copyMsg($img_file, fmt("... not copied to %s", $target));
678                         }
679                     } else {
680                         $target = "images/$base";
681                         $zip->addSrcFile($target, $src);
682                     }
683                 }
684             }
685         }
686         
687         if ($directory) {
688             $outfile = $directory."/".$filename;
689             if ( !($fd = fopen($outfile, "wb")) ) {
690                 $msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
691                                                    $outfile)));
692                 $request->finish($msg);
693             }
694             $len = strlen($data);
695             $num = fwrite($fd, $data, $len);
696             if ($pagename != $filename) {
697                 $link = LinkURL($link_prefix.$filename, $filename);
698                 $msg->pushContent(HTML::small(_("saved as "), $link, " ... "));
699             }
700             $msg->pushContent(HTML::small(fmt("%s bytes written", $num), "\n"));
701             if (!$silent) {
702                 if (!isa($request, 'MockRequest')) {
703                     PrintXML($msg);
704                 }
705                 flush();
706                 $request->chunkOutput();
707             }
708             assert($num == $len);
709             fclose($fd);
710             $outfiles[] = $outfile;
711         } else {
712             $zip->addRegularFile($filename, $data, $attrib);
713         }
714
715         if (USECACHE) {
716             $request->_dbi->_cache->invalidate_cache($pagename);
717             unset ($request->_dbi->_cache->_pagedata_cache);
718             unset ($request->_dbi->_cache->_versiondata_cache);
719             unset ($request->_dbi->_cache->_glv_cache);
720         }
721         unset ($request->_dbi->_cache->_backend->_page_data);
722
723         unset($msg);
724         unset($current->_transformedContent);
725         unset($current);
726         if (!empty($template)) {
727             unset($template->_request);
728             unset($template);
729         }
730         unset($data);
731         if (DEBUG)
732             $GLOBALS['RCS_IDS'] = $SAVE_RCS_IDS;
733     }
734     $page_iter->free();
735
736     $attrib = false; //array('is_ascii' => 0);
737     if (!empty($WikiTheme->dumped_images) and is_array($WikiTheme->dumped_images)) {
738         // @mkdir("$directory/images");
739         foreach ($WikiTheme->dumped_images as $img_file) {
740             if (array_key_exists($img_file, $already_images))
741                 continue;
742             $already_images[$img_file] = 1;
743             if ($img_file 
744                 and ($from = $WikiTheme->_findFile($img_file, true)) 
745                 and basename($from)) 
746             {
747                 if ($directory) {
748                     $target = "$directory/images/".basename($from);
749                     if ($silent)
750                         copy($WikiTheme->_path . $from, $target);
751                     else {
752                         if (copy($WikiTheme->_path . $from, $target)) {
753                             _copyMsg($from, fmt("... copied to %s", $target));
754                         } else {
755                             _copyMsg($from, fmt("... not copied to %s", $target));
756                         }
757                     }
758                 } else {
759                     $target = "images/".basename($from);
760                     $zip->addSrcFile($target, $WikiTheme->_path . $from);
761                 }
762             } elseif (!$silent) {
763                 _copyMsg($from, _("... not found"));
764             }
765         }
766     }
767
768     if (!empty($WikiTheme->dumped_buttons) 
769          and is_array($WikiTheme->dumped_buttons)) 
770     {
771         // Buttons also
772         if ($directory)
773             @mkdir("$directory/images/buttons");
774         foreach ($WikiTheme->dumped_buttons as $text => $img_file) {
775             if (array_key_exists($img_file, $already_images))
776                 continue;
777             $already_images[$img_file] = 1;
778             if ($img_file 
779                 and ($from = $WikiTheme->_findFile($img_file, true)) 
780                 and basename($from)) 
781             {
782                 if ($directory) {
783                     $target = "$directory/images/buttons/".basename($from);
784                     if ($silent)
785                         copy($WikiTheme->_path . $from, $target);
786                     else {
787                         if (copy($WikiTheme->_path . $from, $target)) {
788                             _copyMsg($from, fmt("... copied to %s", $target));
789                         } else {
790                             _copyMsg($from, fmt("... not copied to %s", $target));
791                         }
792                     }
793                 } else {
794                     $target = "images/buttons/".basename($from);
795                     $zip->addSrcFile($target, $WikiTheme->_path . $from);
796                 }
797             } elseif (!$silent) {
798                 _copyMsg($from, _("... not found"));
799             }
800         }
801     }
802     if (!empty($WikiTheme->dumped_css) and is_array($WikiTheme->dumped_css)) {
803         foreach ($WikiTheme->dumped_css as $css_file) {
804             if (array_key_exists($css_file, $already_images))
805                 continue;
806             $already_images[$css_file] = 1;
807             if ($css_file 
808                 and ($from = $WikiTheme->_findFile(basename($css_file), true)) 
809                 and basename($from)) 
810             {
811                 // TODO: fix @import url(main.css);
812                 if ($directory) {
813                     $target = "$directory/" . basename($css_file);
814                     if ($silent)
815                         copy($WikiTheme->_path . $from, $target);
816                     else {
817                         if (copy($WikiTheme->_path . $from, $target)) {
818                             _copyMsg($from, fmt("... copied to %s", $target));
819                         } else {
820                             _copyMsg($from, fmt("... not copied to %s", $target));
821                         }
822                     }
823                 } else {
824                     //$attrib = array('is_ascii' => 0);
825                     $target = basename($css_file);
826                     $zip->addSrcFile($target, $WikiTheme->_path . $from);
827                 }
828             } elseif (!$silent) {
829                 _copyMsg($from, _("... not found"));
830             }
831         }
832     }
833
834     if ($zip) 
835         $zip->finish();
836
837     if ($WikiTheme->DUMP_MODE == 'PDFHTML') {
838         if (USE_EXTERNAL_HTML2PDF and $outfiles) {
839             $cmd = EXTERNAL_HTML2PDF_PAGELIST.' "'.join('" "', $outfiles).'"';
840             $filename = FilenameForPage($firstpage);
841             if (DEBUG) {
842                 $tmpfile = $directory . "/createpdf.bat";
843                 $fp = fopen($tmpfile, "wb");
844                 fwrite($fp, $cmd . " > $filename.pdf");
845                 fclose($fp);
846             }
847             if (!headers_sent()) {
848                 Header('Content-Type: application/pdf');
849                 passthru($cmd);
850             }
851             else {
852                 $tmpdir = getUploadFilePath();
853                 $s = passthru($cmd . " > $tmpdir/$filename.pdf");
854                 $errormsg = "<br />\nGenerated <a href=\"".getUploadDataPath()."$filename.pdf\">Upload:$filename.pdf</a>\n";
855                 $errormsg .= $s;
856                 echo $errormsg;
857             }
858             if (!DEBUG) {
859                 foreach($outfiles as $f) unlink($f);
860             }
861         }
862         if (!empty($errormsg)) {
863             $request->discardOutput();
864             $GLOBALS['ErrorManager']->_postponed_errors = array();
865         }
866     }
867
868     if (check_php_version(4,1)) {
869         global $ErrorManager;
870         $ErrorManager->popErrorHandler();
871     }
872
873     $WikiTheme->HTML_DUMP_SUFFIX = '';
874     $WikiTheme->DUMP_MODE = false;
875     $WikiTheme->_MoreAttr['body'] = $_bodyAttr;
876 }
877
878
879 ////////////////////////////////////////////////////////////////
880 //
881 //  Functions for restoring.
882 //
883 ////////////////////////////////////////////////////////////////
884
885 function SavePage (&$request, &$pageinfo, $source, $filename)
886 {
887     static $overwite_all = false;
888     $pagedata    = $pageinfo['pagedata'];    // Page level meta-data.
889     $versiondata = $pageinfo['versiondata']; // Revision level meta-data.
890
891     if (empty($pageinfo['pagename'])) {
892         PrintXML(HTML::p(HTML::strong(_("Empty pagename!"))));
893         return;
894     }
895
896     if (empty($versiondata['author_id']))
897         $versiondata['author_id'] = $versiondata['author'];
898
899     // remove invalid backend specific chars. utf8 issues mostly
900     $pagename_check = new WikiPagename($pageinfo['pagename']);
901     if (!$pagename_check->isValid()) {
902         PrintXML(HTML::p(HTML::strong(_("Invalid pagename!")." ".$pageinfo['pagename'])));
903         return;
904     }
905     $pagename = $pagename_check->getName();
906     $content  = $pageinfo['content'];
907
908     if ($pagename == _("InterWikiMap"))
909         $content = _tryinsertInterWikiMap($content);
910
911     $dbi =& $request->_dbi;
912     $page = $dbi->getPage($pagename);
913
914     // Try to merge if updated pgsrc contents are different. This
915     // whole thing is hackish
916     //
917     // TODO: try merge unless:
918     // if (current contents = default contents && pgsrc_version >=
919     // pgsrc_version) then just upgrade this pgsrc
920     $needs_merge = false;
921     $merging = false;
922     $overwrite = false;
923
924     if ($request->getArg('merge')) {
925         $merging = true;
926     }
927     else if ($request->getArg('overwrite')) {
928         $overwrite = true;
929     }
930
931     $current = $page->getCurrentRevision();
932     $skip = false;
933     $edit = $request->getArg('edit');
934     if ($merging) { 
935         if (isset($edit['keep_old'])) {
936             $merging = false;
937             $skip = true;
938         }
939         elseif (isset($edit['overwrite'])) {
940             $merging = false;
941             $overwrite = true;
942         }
943         elseif ( $current and (! $current->hasDefaultContents())
944          && ($current->getPackedContent() != $content) ) 
945         {
946             include_once('lib/editpage.php');
947             $request->setArg('pagename', $pagename);
948             $v = $current->getVersion();
949             $request->setArg('revision', $current->getVersion());
950             $p = new LoadFileConflictPageEditor($request);
951             $p->_content = $content;
952             $p->_currentVersion = $v - 1;
953             $p->editPage($saveFailed = true);
954             return; //early return
955        }
956     }
957     if (!$skip)
958       foreach ($pagedata as $key => $value) {
959         if (!empty($value))
960             $page->set($key, $value);
961       }
962
963     $mesg = HTML::p(array('style' => 'text-indent: 3em;'));
964     if ($source)
965         $mesg->pushContent(' ', fmt("from %s", $source));
966
967     if (!$current) {
968         //FIXME: This should not happen! (empty vdata, corrupt cache or db)
969         $current = $page->getCurrentRevision();
970     }
971     if ($current->getVersion() == 0) {
972         $mesg->pushContent(' - ', _("New page"));
973         $isnew = true;
974     }
975     else {
976         if ( (! $current->hasDefaultContents())
977              && ($current->getPackedContent() != $content) ) {
978             if ($overwrite) {
979                 $mesg->pushContent(' ',
980                                    fmt("has edit conflicts - overwriting anyway"));
981                 $skip = false;
982                 if (substr_count($source, 'pgsrc')) {
983                     $versiondata['author'] = ADMIN_USER;
984                     // but leave authorid as userid who loaded the file
985                 }
986             }
987             else {
988                 if (isset($edit['keep_old'])) {
989                     $mesg->pushContent(' ', fmt("keep old"));
990                 } else {
991                     $mesg->pushContent(' ', fmt("has edit conflicts - skipped"));
992                     $needs_merge = true; // hackish, to display the buttons
993                 }
994                 $skip = true;
995             }
996         }
997         else if ($current->getPackedContent() == $content
998                  && $current->get('author') == $versiondata['author']) {
999             // The page metadata is already changed, we don't need a new revision.
1000             // This was called previously "is identical to current version %d - skipped"
1001             // which is wrong, since the pagedata was stored, not skipped.
1002             $mesg->pushContent(' ',
1003                                fmt("content is identical to current version %d - no new revision created",
1004                                    $current->getVersion()));
1005             $skip = true;
1006         }
1007         $isnew = false;
1008     }
1009
1010     if (! $skip ) {
1011         // in case of failures print the culprit:
1012         if (!isa($request,'MockRequest')) {
1013             PrintXML(HTML::p(WikiLink($pagename))); flush();
1014         }
1015         $new = $page->save($content, WIKIDB_FORCE_CREATE, $versiondata);
1016         $dbi->touch();
1017         $mesg->pushContent(' ', fmt("- saved to database as version %d",
1018                                     $new->getVersion()));
1019     }
1020     if ($needs_merge) {
1021         $f = $source;
1022         // hackish, $source contains needed path+filename
1023         $f = str_replace(sprintf(_("MIME file %s"), ''), '', $f);
1024         $f = str_replace(sprintf(_("Serialized file %s"), ''), '', $f);
1025         $f = str_replace(sprintf(_("plain file %s"), ''), '', $f);
1026         //check if uploaded file? they pass just the content, but the file is gone
1027         if (@stat($f)) {
1028             global $WikiTheme;
1029             $meb = Button(array('action' => 'loadfile',
1030                                 'merge'=> true,
1031                                 'source'=> $f),
1032                           _("Merge Edit"),
1033                           _("PhpWikiAdministration"),
1034                           'wikiadmin');
1035             $owb = Button(array('action' => 'loadfile',
1036                                 'overwrite'=> true,
1037                                 'source'=> $f),
1038                           _("Restore Anyway"),
1039                           _("PhpWikiAdministration"),
1040                           'wikiunsafe');
1041             $mesg->pushContent(' ', $meb, " ", $owb);
1042             if (!$overwite_all) {
1043                 $args = $request->getArgs();
1044                 $args['overwrite'] = 1;
1045                 $owb = Button($args,
1046                               _("Overwrite All"),
1047                               _("PhpWikiAdministration"),
1048                               'wikiunsafe');
1049                 $mesg->pushContent(HTML::span(array('class' => 'hint'), $owb));
1050                 $overwite_all = true;
1051             }
1052         } else {
1053             $mesg->pushContent(HTML::em(_(" Sorry, cannot merge.")));
1054         }
1055     }
1056
1057     if (!isa($request,'MockRequest')) {
1058       if ($skip)
1059         PrintXML(HTML::p(HTML::em(WikiLink($pagename))), $mesg);
1060       else
1061         PrintXML($mesg);
1062       flush();
1063     }
1064 }
1065
1066 // action=revert (by diff)
1067 function RevertPage (&$request)
1068 {
1069     $mesg = HTML::div();
1070     $pagename = $request->getArg('pagename');
1071     $version = $request->getArg('version');
1072     if (!$version) {
1073         PrintXML(HTML::p(fmt("Revert")," ",WikiLink($pagename)),
1074                  HTML::p(_("missing required version argument")));
1075         return;
1076     }
1077     $dbi =& $request->_dbi;
1078     $page = $dbi->getPage($pagename);
1079     $current = $page->getCurrentRevision();
1080     $currversion = $current->getVersion();
1081     if ($currversion == 0) {
1082         $mesg->pushContent(' ', _("no page content"));
1083         PrintXML(HTML::p(fmt("Revert")," ",WikiLink($pagename)),
1084                  $mesg);
1085         flush();
1086         return;
1087     }
1088     if ($currversion == $version) {
1089         $mesg->pushContent(' ', _("same version page"));
1090         PrintXML(HTML::p(fmt("Revert")," ",WikiLink($pagename)),
1091                  $mesg);
1092         flush();
1093         return;
1094     }
1095     if ($request->getArg('cancel')) {
1096         $mesg->pushContent(' ', _("Cancelled"));
1097         PrintXML(HTML::p(fmt("Revert")," ",WikiLink($pagename)),
1098                  $mesg);
1099         flush();
1100         return;
1101     }
1102     if (!$request->getArg('verify')) {
1103         $mesg->pushContent(HTML::p(fmt("Are you sure to revert %s to version $version?", WikiLink($pagename))),
1104                            HTML::form(array('action' => $request->getPostURL(),
1105                                             'method' => 'post'),
1106                                       HiddenInputs($request->getArgs(), false, array('verify')),
1107                                       HiddenInputs(array('verify' => 1)),
1108                                       Button('submit:verify', _("Yes"), 'button'),
1109                                       HTML::Raw('&nbsp;'),
1110                                       Button('submit:cancel', _("Cancel"), 'button'))
1111                            );
1112         $rev = $page->getRevision($version);
1113         $html = HTML(HTML::fieldset($mesg), HTML::hr(), $rev->getTransformedContent()); 
1114         $template = Template('browse', 
1115                              array('CONTENT' => $html));
1116         GeneratePage($template, $pagename, $rev);
1117         $request->checkValidators();
1118         flush();
1119         return;
1120     }
1121     $rev = $page->getRevision($version);
1122     $content = $rev->getPackedContent();
1123     $versiondata = $rev->_data;
1124     $versiondata['summary'] = sprintf(_("revert to version %d"), $version);
1125     $new = $page->save($content, $currversion + 1, $versiondata);
1126     $dbi->touch();
1127     
1128     $mesg = HTML::span();
1129     $pagelink = WikiLink($pagename);
1130     $mesg->pushContent(fmt("Revert: %s", $pagelink), 
1131                        fmt("- version %d saved to database as version %d",
1132                            $version, $new->getVersion()));
1133     // Force browse of current page version.
1134     $request->setArg('version', false);
1135     $template = Template('savepage', array());
1136     $template->replace('CONTENT', $new->getTransformedContent());
1137     
1138     GeneratePage($template, $mesg, $new);
1139     flush();
1140 }
1141
1142 function _tryinsertInterWikiMap($content) {
1143     $goback = false;
1144     if (strpos($content, "<verbatim>")) {
1145         //$error_html = " The newly loaded pgsrc already contains a verbatim block.";
1146         $goback = true;
1147     }
1148     if (!$goback && !defined('INTERWIKI_MAP_FILE')) {
1149         $error_html = sprintf(" "._("%s: not defined"), "INTERWIKI_MAP_FILE");
1150         $goback = true;
1151     }
1152     $mapfile = FindFile(INTERWIKI_MAP_FILE,1);
1153     if (!$goback && !file_exists($mapfile)) {
1154         $error_html = sprintf(" "._("%s: file not found"), INTERWIKI_MAP_FILE);
1155         $goback = true;
1156     }
1157
1158     if (!empty($error_html))
1159         trigger_error(_("Default InterWiki map file not loaded.")
1160                       . $error_html, E_USER_NOTICE);
1161     if ($goback)
1162         return $content;
1163
1164     // if loading from virgin setup do echo, otherwise trigger_error E_USER_NOTICE
1165     if (!isa($GLOBALS['request'], 'MockRequest'))
1166         echo sprintf(_("Loading InterWikiMap from external file %s."), $mapfile),"<br />";
1167
1168     $fd = fopen ($mapfile, "rb");
1169     $data = fread ($fd, filesize($mapfile));
1170     fclose ($fd);
1171     $content = $content . "\n<verbatim>\n$data</verbatim>\n";
1172     return $content;
1173 }
1174
1175 function ParseSerializedPage($text, $default_pagename, $user)
1176 {
1177     if (!preg_match('/^a:\d+:{[si]:\d+/', $text))
1178         return false;
1179
1180     $pagehash = unserialize($text);
1181
1182     // Split up pagehash into four parts:
1183     //   pagename
1184     //   content
1185     //   page-level meta-data
1186     //   revision-level meta-data
1187
1188     if (!defined('FLAG_PAGE_LOCKED'))
1189         define('FLAG_PAGE_LOCKED', 1);
1190     if (!defined('FLAG_PAGE_EXTERNAL'))
1191         define('FLAG_PAGE_EXTERNAL', 1);
1192     $pageinfo = array('pagedata'    => array(),
1193                       'versiondata' => array());
1194
1195     $pagedata = &$pageinfo['pagedata'];
1196     $versiondata = &$pageinfo['versiondata'];
1197
1198     // Fill in defaults.
1199     if (empty($pagehash['pagename']))
1200         $pagehash['pagename'] = $default_pagename;
1201     if (empty($pagehash['author'])) {
1202         $pagehash['author'] = $user->getId();
1203     }
1204
1205     foreach ($pagehash as $key => $value) {
1206         switch($key) {
1207             case 'pagename':
1208             case 'version':
1209             case 'hits':
1210                 $pageinfo[$key] = $value;
1211                 break;
1212             case 'content':
1213                 $pageinfo[$key] = join("\n", $value);
1214                 break;
1215             case 'flags':
1216                 if (($value & FLAG_PAGE_LOCKED) != 0)
1217                     $pagedata['locked'] = 'yes';
1218                 if (($value & FLAG_PAGE_EXTERNAL) != 0)
1219                     $pagedata['external'] = 'yes';
1220                 break;
1221             case 'owner':
1222             case 'created':
1223                 $pagedata[$key] = $value;
1224                 break;
1225             case 'acl':
1226             case 'perm':
1227                 $pagedata['perm'] = ParseMimeifiedPerm($value);
1228                 break;
1229             case 'lastmodified':
1230                 $versiondata['mtime'] = $value;
1231                 break;
1232             case 'author':
1233             case 'author_id':
1234             case 'summary':
1235                 $versiondata[$key] = $value;
1236                 break;
1237         }
1238     }
1239     if (empty($pagehash['charset']))
1240         $pagehash['charset'] = 'utf-8';
1241     // compare to target charset
1242     if (strtolower($pagehash['charset']) != strtolower($GLOBALS['charset'])) {
1243         $pageinfo['content'] = charset_convert($params['charset'], $GLOBALS['charset'], $pageinfo['content']);
1244         $pageinfo['pagename'] = charset_convert($params['charset'], $GLOBALS['charset'], $pageinfo['pagename']);
1245     }
1246     return $pageinfo;
1247 }
1248
1249 function SortByPageVersion ($a, $b) {
1250     return $a['version'] - $b['version'];
1251 }
1252
1253 /**
1254  * Security alert! We should not allow to import config.ini into our wiki (or from a sister wiki?)
1255  * because the sql passwords are in plaintext there. And the webserver must be able to read it.
1256  * Detected by Santtu Jarvi.
1257  */
1258 function LoadFile (&$request, $filename, $text = false, $mtime = false)
1259 {
1260     if (preg_match("/config$/", dirname($filename))             // our or other config
1261         and preg_match("/config.*\.ini/", basename($filename))) // backups and other versions also
1262     {
1263         trigger_error(sprintf("Refused to load %s", $filename), E_USER_WARNING);
1264         return;
1265     }
1266     if (!is_string($text)) {
1267         // Read the file.
1268         $stat  = stat($filename);
1269         $mtime = $stat[9];
1270         $text  = implode("", file($filename));
1271     }
1272
1273     if (! $request->getArg('start_debug')) @set_time_limit(30); // Reset watchdog
1274     else @set_time_limit(240);
1275
1276     // FIXME: basename("filewithnoslashes") seems to return garbage sometimes.
1277     $basename = basename("/dummy/" . $filename);
1278
1279     if (!$mtime)
1280         $mtime = time();    // Last resort.
1281
1282     // DONE: check source - target charset for content and pagename
1283     // but only for pgsrc'ed content, not from the browser.
1284
1285     $default_pagename = rawurldecode($basename);
1286     if ( ($parts = ParseMimeifiedPages($text)) ) {
1287         if (count($parts) > 1)
1288             $overwrite = $request->getArg('overwrite');
1289         usort($parts, 'SortByPageVersion');
1290         foreach ($parts as $pageinfo) {
1291             // force overwrite
1292             if (count($parts) > 1)
1293                 $request->setArg('overwrite', 1);
1294             SavePage($request, $pageinfo, sprintf(_("MIME file %s"),
1295                                                   $filename), $basename);
1296     }
1297         if (count($parts) > 1)
1298             if ($overwrite) 
1299                 $request->setArg('overwrite', $overwrite);
1300             else     
1301                 unset($request->_args['overwrite']);
1302     }
1303     else if ( ($pageinfo = ParseSerializedPage($text, $default_pagename,
1304                                                $request->getUser())) ) {
1305         SavePage($request, $pageinfo, sprintf(_("Serialized file %s"),
1306                                               $filename), $basename);
1307     }
1308     else {
1309         // plain old file
1310         $user = $request->getUser();
1311
1312         $file_charset = 'utf-8';
1313         // compare to target charset
1314         if ($file_charset != strtolower($GLOBALS['charset'])) {
1315             $text = charset_convert($file_charset, $GLOBALS['charset'], $text);
1316             $default_pagename = charset_convert($file_charset, $GLOBALS['charset'], $default_pagename);
1317         }
1318
1319         // Assume plain text file.
1320         $pageinfo = array('pagename' => $default_pagename,
1321                           'pagedata' => array(),
1322                           'versiondata'
1323                           => array('author' => $user->getId()),
1324                           'content'  => preg_replace('/[ \t\r]*\n/', "\n",
1325                                                      chop($text))
1326                           );
1327         SavePage($request, $pageinfo, sprintf(_("plain file %s"), $filename),
1328                  $basename);
1329     }
1330 }
1331
1332 function LoadZip (&$request, $zipfile, $files = false, $exclude = false) {
1333     $zip = new ZipReader($zipfile);
1334     $timeout = (! $request->getArg('start_debug')) ? 20 : 120;
1335     while (list ($fn, $data, $attrib) = $zip->readFile()) {
1336         // FIXME: basename("filewithnoslashes") seems to return
1337         // garbage sometimes.
1338         $fn = basename("/dummy/" . $fn);
1339         if ( ($files && !in_array($fn, $files))
1340              || ($exclude && in_array($fn, $exclude)) ) {
1341             PrintXML(HTML::p(WikiLink($fn)),
1342                      HTML::p(_("Skipping")));
1343             flush();
1344             continue;
1345         }
1346         longer_timeout($timeout);       // longer timeout per page
1347         LoadFile($request, $fn, $data, $attrib['mtime']);
1348     }
1349 }
1350
1351 function LoadDir (&$request, $dirname, $files = false, $exclude = false) {
1352     $fileset = new LimitedFileSet($dirname, $files, $exclude);
1353
1354     if (!$files and ($skiplist = $fileset->getSkippedFiles())) {
1355         PrintXML(HTML::p(HTML::strong(_("Skipping"))));
1356         $list = HTML::ul();
1357         foreach ($skiplist as $file)
1358             $list->pushContent(HTML::li(WikiLink($file)));
1359         PrintXML(HTML::p($list));
1360     }
1361
1362     // Defer HomePage loading until the end. If anything goes wrong
1363     // the pages can still be loaded again.
1364     $files = $fileset->getFiles();
1365     if (in_array(HOME_PAGE, $files)) {
1366         $files = array_diff($files, array(HOME_PAGE));
1367         $files[] = HOME_PAGE;
1368     }
1369     $timeout = (! $request->getArg('start_debug')) ? 20 : 120;
1370     foreach ($files as $file) {
1371         longer_timeout($timeout);       // longer timeout per page
1372         if (substr($file,-1,1) != '~')  // refuse to load backup files
1373             LoadFile($request, "$dirname/$file");
1374     }
1375 }
1376
1377 class LimitedFileSet extends FileSet {
1378     function LimitedFileSet($dirname, $_include, $exclude) {
1379         $this->_includefiles = $_include;
1380         $this->_exclude = $exclude;
1381         $this->_skiplist = array();
1382         parent::FileSet($dirname);
1383     }
1384
1385     function _filenameSelector($fn) {
1386         $incl = &$this->_includefiles;
1387         $excl = &$this->_exclude;
1388
1389         if ( ($incl && !in_array($fn, $incl))
1390              || ($excl && in_array($fn, $excl)) ) {
1391             $this->_skiplist[] = $fn;
1392             return false;
1393         } else {
1394             return true;
1395         }
1396     }
1397
1398     function getSkippedFiles () {
1399         return $this->_skiplist;
1400     }
1401 }
1402
1403
1404 function IsZipFile ($filename_or_fd)
1405 {
1406     // See if it looks like zip file
1407     if (is_string($filename_or_fd))
1408     {
1409         $fd    = fopen($filename_or_fd, "rb");
1410         $magic = fread($fd, 4);
1411         fclose($fd);
1412     }
1413     else
1414     {
1415         $fpos  = ftell($filename_or_fd);
1416         $magic = fread($filename_or_fd, 4);
1417         fseek($filename_or_fd, $fpos);
1418     }
1419
1420     return $magic == ZIP_LOCHEAD_MAGIC || $magic == ZIP_CENTHEAD_MAGIC;
1421 }
1422
1423
1424 function LoadAny (&$request, $file_or_dir, $files = false, $exclude = false)
1425 {
1426     // Try urlencoded filename for accented characters.
1427     if (!file_exists($file_or_dir)) {
1428         // Make sure there are slashes first to avoid confusing phps
1429         // with broken dirname or basename functions.
1430         // FIXME: windows uses \ and :
1431         if (is_integer(strpos($file_or_dir, "/"))) {
1432             $newfile = FindFile($file_or_dir, true);
1433             // Panic. urlencoded by the browser (e.g. San%20Diego => San Diego)
1434             if (!$newfile)
1435                 $file_or_dir = dirname($file_or_dir) . "/"
1436                     . rawurlencode(basename($file_or_dir));
1437         } else {
1438             // This is probably just a file.
1439             $file_or_dir = rawurlencode($file_or_dir);
1440         }
1441     }
1442
1443     $type = filetype($file_or_dir);
1444     if ($type == 'link') {
1445         // For symbolic links, use stat() to determine
1446         // the type of the underlying file.
1447         list(,,$mode) = stat($file_or_dir);
1448         $type = ($mode >> 12) & 017;
1449         if ($type == 010)
1450             $type = 'file';
1451         elseif ($type == 004)
1452             $type = 'dir';
1453     }
1454
1455     if (! $type) {
1456         $request->finish(fmt("Empty or not existing source. Unable to load: %s", $file_or_dir));
1457     }
1458     else if ($type == 'dir') {
1459         LoadDir($request, $file_or_dir, $files, $exclude);
1460     }
1461     else if ($type != 'file' && !preg_match('/^(http|ftp):/', $file_or_dir))
1462     {
1463         $request->finish(fmt("Bad file type: %s", $type));
1464     }
1465     else if (IsZipFile($file_or_dir)) {
1466         LoadZip($request, $file_or_dir, $files, $exclude);
1467     }
1468     else /* if (!$files || in_array(basename($file_or_dir), $files)) */
1469     {
1470         LoadFile($request, $file_or_dir);
1471     }
1472 }
1473
1474 function LoadFileOrDir (&$request)
1475 {
1476     $source = $request->getArg('source');
1477     $finder = new FileFinder;
1478     $source = $finder->slashifyPath($source);
1479     $page = rawurldecode(basename($source));
1480     StartLoadDump($request, fmt("Loading '%s'", 
1481         HTML(dirname($source),
1482              dirname($source) ? "/" : "",
1483              WikiLink($page,'auto'))));
1484     LoadAny($request, $source);
1485     EndLoadDump($request);
1486 }
1487
1488 /**
1489  * HomePage was not found so first-time install is supposed to run.
1490  * - import all pgsrc pages.
1491  * - Todo: installer interface to edit config/config.ini settings
1492  * - Todo: ask for existing old index.php to convert to config/config.ini
1493  * - Todo: theme-specific pages: 
1494  *   blog - HomePage, ADMIN_USER/Blogs
1495  */
1496 function SetupWiki (&$request)
1497 {
1498     global $GenericPages, $LANG;
1499
1500     //FIXME: This is a hack (err, "interim solution")
1501     // This is a bogo-bogo-login:  Login without
1502     // saving login information in session state.
1503     // This avoids logging in the unsuspecting
1504     // visitor as ADMIN_USER
1505     //
1506     // This really needs to be cleaned up...
1507     // (I'm working on it.)
1508     $real_user = $request->_user;
1509     if (ENABLE_USER_NEW)
1510         $request->_user = new _BogoUser(ADMIN_USER);
1511
1512     else
1513         $request->_user = new WikiUser($request, ADMIN_USER, WIKIAUTH_BOGO);
1514
1515     StartLoadDump($request, _("Loading up virgin wiki"));
1516
1517     $pgsrc = FindLocalizedFile(WIKI_PGSRC);
1518     $default_pgsrc = FindFile(DEFAULT_WIKI_PGSRC);
1519
1520     $request->setArg('overwrite', true);
1521     if ($default_pgsrc != $pgsrc) {
1522         LoadAny($request, $default_pgsrc, $GenericPages);
1523     }
1524     $request->setArg('overwrite', false);
1525     LoadAny($request, $pgsrc);
1526     $dbi =& $request->_dbi;
1527
1528     // Ensure that all mandatory pages are loaded
1529     $finder = new FileFinder;
1530
1531     if (! defined('GFORGE') or !GFORGE) {
1532         $mandatory = explode(':','SandBox:Template/Category:TemplateTalk:SpecialPages:CategoryCategory:CategoryActionPage:Help/OldTextFormattingRules:Help/TextFormattingRules:PhpWikiAdministration');
1533     } else if (WIKI_NAME == "help") {
1534         $mandatory = explode(':','SandBox:Template/Category:TemplateTalk:SpecialPages:CategoryCategory:CategoryActionPage:Help/TextFormattingRules:PhpWikiAdministration');
1535     } else {
1536         $mandatory = explode(':','SandBox:Template/Category:TemplateTalk:SpecialPages:CategoryCategory:CategoryActionPage:TextFormattingRules:PhpWikiAdministration');
1537     }
1538     foreach (array_merge($mandatory,
1539                          $GLOBALS['AllActionPages'],
1540                          array(constant('HOME_PAGE'))) as $f) 
1541     {
1542         $page = gettext($f);
1543         $epage = urlencode($page);
1544         if (! $dbi->isWikiPage($page) ) {
1545             // translated version provided?
1546             if ($lf = FindLocalizedFile($pgsrc . $finder->_pathsep . $epage, 1)) {
1547                 LoadAny($request, $lf);
1548             } else { // load english version of required action page
1549                 LoadAny($request, FindFile(DEFAULT_WIKI_PGSRC . $finder->_pathsep . urlencode($f)));
1550                 $page = $f;
1551             }
1552         }
1553         if (! $dbi->isWikiPage($page)) {
1554             trigger_error(sprintf("Mandatory file %s couldn't be loaded!", $page),
1555                           E_USER_WARNING);
1556         }
1557     }
1558     
1559     $pagename = _("InterWikiMap");
1560     $map = $dbi->getPage($pagename);
1561     $map->set('locked', true);
1562     PrintXML(HTML::p(HTML::em(WikiLink($pagename)), HTML::strong(" locked")));
1563     EndLoadDump($request);
1564 }
1565
1566 function LoadPostFile (&$request)
1567 {
1568     $upload = $request->getUploadedFile('file');
1569
1570     if (!$upload)
1571         $request->finish(_("No uploaded file to upload?")); // FIXME: more concise message
1572
1573     // Dump http headers.
1574     StartLoadDump($request, sprintf(_("Uploading %s"), $upload->getName()));
1575
1576     $fd = $upload->open();
1577     if (IsZipFile($fd))
1578         LoadZip($request, $fd, false, array(_("RecentChanges")));
1579     else
1580         LoadFile($request, $upload->getName(), $upload->getContents());
1581
1582     EndLoadDump($request);
1583 }
1584
1585 // For emacs users
1586 // Local Variables:
1587 // mode: php
1588 // tab-width: 8
1589 // c-basic-offset: 4
1590 // c-hanging-comment-ender-p: nil
1591 // indent-tabs-mode: nil
1592 // End:
1593 ?>