]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/loadsave.php
Minor enhancement: Provide more meaningful filenames for WikiDB zip
[SourceForge/phpwiki.git] / lib / loadsave.php
1 <?php //-*-php-*-
2 rcs_id('$Id: loadsave.php,v 1.86 2003-12-02 16:18:26 carstenklapp Exp $');
3
4 /*
5  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24
25 require_once("lib/ziplib.php");
26 require_once("lib/Template.php");
27
28 function StartLoadDump(&$request, $title, $html = '')
29 {
30     // FIXME: This is a hack
31     $tmpl = Template('html', array('TITLE' => $title,
32                                   'HEADER' => $title,
33                                   'CONTENT' => '%BODY%'));
34     echo ereg_replace('%BODY%.*', '', $tmpl->getExpansion($html));
35 }
36
37 function EndLoadDump(&$request)
38 {
39     // FIXME: This is a hack
40     $pagelink = WikiLink($request->getPage());
41
42     PrintXML(HTML::p(HTML::strong(_("Complete."))),
43              HTML::p(fmt("Return to %s", $pagelink)));
44     echo "</body></html>\n";
45 }
46
47
48 ////////////////////////////////////////////////////////////////
49 //
50 //  Functions for dumping.
51 //
52 ////////////////////////////////////////////////////////////////
53
54 /**
55  * For reference see:
56  * http://www.nacs.uci.edu/indiv/ehood/MIME/2045/rfc2045.html
57  * http://www.faqs.org/rfcs/rfc2045.html
58  * (RFC 1521 has been superceeded by RFC 2045 & others).
59  *
60  * Also see http://www.faqs.org/rfcs/rfc2822.html
61  */
62 function MailifyPage ($page, $nversions = 1)
63 {
64     $current = $page->getCurrentRevision();
65     $head = '';
66
67     if (STRICT_MAILABLE_PAGEDUMPS) {
68         $from = defined('SERVER_ADMIN') ? SERVER_ADMIN : 'foo@bar';
69         //This is for unix mailbox format: (not RFC (2)822)
70         // $head .= "From $from  " . CTime(time()) . "\r\n";
71         $head .= "Subject: " . rawurlencode($page->getName()) . "\r\n";
72         $head .= "From: $from (PhpWiki)\r\n";
73         // RFC 2822 requires only a Date: and originator (From:)
74         // field, however the obsolete standard RFC 822 also
75         // requires a destination field.
76         $head .= "To: $from (PhpWiki)\r\n";
77     }
78     $head .= "Date: " . Rfc2822DateTime($current->get('mtime')) . "\r\n";
79     $head .= sprintf("Mime-Version: 1.0 (Produced by PhpWiki %s)\r\n",
80                      PHPWIKI_VERSION);
81
82     // This should just be entered by hand (or by script?)
83     // in the actual pgsrc files, since only they should have
84     // RCS ids.
85     //$head .= "X-Rcs-Id: \$Id\$\r\n";
86
87     $iter = $page->getAllRevisions();
88     $parts = array();
89     while ($revision = $iter->next()) {
90         $parts[] = MimeifyPageRevision($revision);
91         if ($nversions > 0 && count($parts) >= $nversions)
92             break;
93     }
94     if (count($parts) > 1)
95         return $head . MimeMultipart($parts);
96     assert($parts);
97     return $head . $parts[0];
98 }
99
100 /***
101  * Compute filename to used for storing contents of a wiki page.
102  *
103  * Basically we do a rawurlencode() which encodes everything except
104  * ASCII alphanumerics and '.', '-', and '_'.
105  *
106  * But we also want to encode leading dots to avoid filenames like
107  * '.', and '..'. (Also, there's no point in generating "hidden" file
108  * names, like '.foo'.)
109  *
110  * @param $pagename string Pagename.
111  * @return string Filename for page.
112  */
113 function FilenameForPage ($pagename)
114 {
115     $enc = rawurlencode($pagename);
116     return preg_replace('/^\./', '%2e', $enc);
117 }
118
119 /**
120  * The main() function which generates a zip archive of a PhpWiki.
121  *
122  * If $include_archive is false, only the current version of each page
123  * is included in the zip file; otherwise all archived versions are
124  * included as well.
125  */
126 function MakeWikiZip (&$request)
127 {
128     if ($request->getArg('include') == 'all') {
129         $zipname         = WIKI_NAME . _("FullDump") . date('Ymd-Hi') . '.zip';
130         $include_archive = true;
131     }
132     else {
133         $zipname         = WIKI_NAME . _("LatestSnapshot") . date('Ymd-Hi') . '.zip';
134         $include_archive = false;
135     }
136
137
138
139     $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
140
141     $dbi = $request->getDbh();
142     $pages = $dbi->getAllPages();
143     while ($page = $pages->next()) {
144         @set_time_limit(30); // Reset watchdog
145
146         $current = $page->getCurrentRevision();
147         if ($current->getVersion() == 0)
148             continue;
149
150         $wpn = new WikiPageName($page->getName());
151         if (!$wpn->isValid())
152             continue;
153
154         $attrib = array('mtime'    => $current->get('mtime'),
155                         'is_ascii' => 1);
156         if ($page->get('locked'))
157             $attrib['write_protected'] = 1;
158
159         if ($include_archive)
160             $content = MailifyPage($page, 0);
161         else
162             $content = MailifyPage($page);
163
164         $zip->addRegularFile( FilenameForPage($page->getName()),
165                               $content, $attrib);
166     }
167     $zip->finish();
168 }
169
170 function DumpToDir (&$request)
171 {
172     $directory = $request->getArg('directory');
173     if (empty($directory))
174         $request->finish(_("You must specify a directory to dump to"));
175
176     // see if we can access the directory the user wants us to use
177     if (! file_exists($directory)) {
178         if (! mkdir($directory, 0755))
179             $request->finish(fmt("Cannot create directory '%s'", $directory));
180         else
181             $html = HTML::p(fmt("Created directory '%s' for the page dump...",
182                                 $directory));
183     } else {
184         $html = HTML::p(fmt("Using directory '%s'", $directory));
185     }
186
187     StartLoadDump($request, _("Dumping Pages"), $html);
188
189     $dbi = $request->getDbh();
190     $pages = $dbi->getAllPages();
191
192     while ($page = $pages->next()) {
193         @set_time_limit(30); // Reset watchdog.
194
195         $filename = FilenameForPage($page->getName());
196
197         $msg = HTML(HTML::br(), $page->getName(), ' ... ');
198
199         if($page->getName() != $filename) {
200             $msg->pushContent(HTML::small(fmt("saved as %s", $filename)),
201                               " ... ");
202         }
203
204         if ($request->getArg('include') == 'all')
205             $data = MailifyPage($page, 0);
206         else
207             $data = MailifyPage($page);
208
209         if ( !($fd = fopen("$directory/$filename", "w")) ) {
210             $msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
211                                                "$directory/$filename")));
212             $request->finish($msg);
213         }
214
215         $num = fwrite($fd, $data, strlen($data));
216         $msg->pushContent(HTML::small(fmt("%s bytes written", $num)));
217         PrintXML($msg);
218
219         flush();
220         assert($num == strlen($data));
221         fclose($fd);
222     }
223
224     EndLoadDump($request);
225 }
226
227
228 function DumpHtmlToDir (&$request)
229 {
230     $directory = $request->getArg('directory');
231     if (empty($directory))
232         $request->finish(_("You must specify a directory to dump to"));
233
234     // see if we can access the directory the user wants us to use
235     if (! file_exists($directory)) {
236         if (! mkdir($directory, 0755))
237             $request->finish(fmt("Cannot create directory '%s'", $directory));
238         else
239             $html = HTML::p(fmt("Created directory '%s' for the page dump...",
240                                 $directory));
241     } else {
242         $html = HTML::p(fmt("Using directory '%s'", $directory));
243     }
244
245     StartLoadDump($request, _("Dumping Pages"), $html);
246
247     $dbi = $request->getDbh();
248     $pages = $dbi->getAllPages();
249
250     global $HTML_DUMP_SUFFIX, $Theme;
251     if ($HTML_DUMP_SUFFIX)
252         $Theme->HTML_DUMP_SUFFIX = $HTML_DUMP_SUFFIX;
253
254     while ($page = $pages->next()) {
255         @set_time_limit(30); // Reset watchdog.
256
257         $pagename = $page->getName();
258         $filename = FilenameForPage($pagename) . $Theme->HTML_DUMP_SUFFIX;
259
260         $msg = HTML(HTML::br(), $pagename, ' ... ');
261
262         if($page->getName() != $filename) {
263             $msg->pushContent(HTML::small(fmt("saved as %s", $filename)),
264                               " ... ");
265         }
266
267         $revision = $page->getCurrentRevision();
268
269         $transformedContent = $revision->getTransformedContent();
270
271         $template = new Template('browse', $request,
272                                  array('revision' => $revision,
273                                        'CONTENT' => $transformedContent));
274
275         $data = GeneratePageasXML($template, $pagename);
276
277         if ( !($fd = fopen("$directory/$filename", "w")) ) {
278             $msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing",
279                                                "$directory/$filename")));
280             $request->finish($msg);
281         }
282
283         $num = fwrite($fd, $data, strlen($data));
284         $msg->pushContent(HTML::small(fmt("%s bytes written", $num), "\n"));
285         PrintXML($msg);
286
287         flush();
288         assert($num == strlen($data));
289         fclose($fd);
290     }
291
292     //CopyImageFiles() will go here;
293     $Theme->$HTML_DUMP_SUFFIX = '';
294
295     EndLoadDump($request);
296 }
297
298 /* Known problem: any plugins or other code which echo()s text will
299  * lead to a corrupted html zip file which may produce the following
300  * errors upon unzipping:
301  *
302  * warning [wikihtml.zip]:  2401 extra bytes at beginning or within zipfile
303  * file #58:  bad zipfile offset (local header sig):  177561
304  *  (attempting to re-compensate)
305  *
306  * However, the actual wiki page data should be unaffected.
307  */
308 function MakeWikiZipHtml (&$request)
309 {
310     $zipname = "wikihtml.zip";
311     $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
312     $dbi = $request->getDbh();
313     $pages = $dbi->getAllPages();
314
315     global $HTML_DUMP_SUFFIX, $Theme;
316     if ($HTML_DUMP_SUFFIX)
317         $Theme->HTML_DUMP_SUFFIX = $HTML_DUMP_SUFFIX;
318
319     while ($page = $pages->next()) {
320         @set_time_limit(30); // Reset watchdog.
321
322         $current = $page->getCurrentRevision();
323         if ($current->getVersion() == 0)
324             continue;
325
326         $attrib = array('mtime'    => $current->get('mtime'),
327                         'is_ascii' => 1);
328         if ($page->get('locked'))
329             $attrib['write_protected'] = 1;
330
331         $pagename = $page->getName();
332         $filename = FilenameForPage($pagename) . $Theme->HTML_DUMP_SUFFIX;
333         $revision = $page->getCurrentRevision();
334
335         $transformedContent = $revision->getTransformedContent();
336
337         $template = new Template('browse', $request,
338                                  array('revision' => $revision,
339                                        'CONTENT' => $transformedContent));
340
341         $data = GeneratePageasXML($template, $pagename);
342
343         $zip->addRegularFile( $filename, $data, $attrib);
344     }
345     // FIXME: Deal with images here.
346     $zip->finish();
347     $Theme->$HTML_DUMP_SUFFIX = '';
348 }
349
350
351 ////////////////////////////////////////////////////////////////
352 //
353 //  Functions for restoring.
354 //
355 ////////////////////////////////////////////////////////////////
356
357 function SavePage (&$request, $pageinfo, $source, $filename)
358 {
359     $pagedata    = $pageinfo['pagedata'];    // Page level meta-data.
360     $versiondata = $pageinfo['versiondata']; // Revision level meta-data.
361
362     if (empty($pageinfo['pagename'])) {
363         PrintXML(HTML::dt(HTML::strong(_("Empty pagename!"))));
364         return;
365     }
366
367     if (empty($versiondata['author_id']))
368         $versiondata['author_id'] = $versiondata['author'];
369
370     $pagename = $pageinfo['pagename'];
371     $content  = $pageinfo['content'];
372
373     if ($pagename ==_("InterWikiMap"))
374         $content = _tryinsertInterWikiMap($content);
375
376     $dbi = $request->getDbh();
377     $page = $dbi->getPage($pagename);
378
379     $current = $page->getCurrentRevision();
380     // Try to merge if updated pgsrc contents are different. This
381     // whole thing is hackish
382     //
383     // TODO: try merge unless:
384     // if (current contents = default contents && pgsrc_version >=
385     // pgsrc_version) then just upgrade this pgsrc
386     $needs_merge = false;
387     $merging = false;
388     $overwrite = false;
389
390     if ($request->getArg('merge')) {
391         $merging = true;
392     }
393     else if ($request->getArg('overwrite')) {
394         $overwrite = true;
395     }
396
397     if ( (! $current->hasDefaultContents())
398          && ($current->getPackedContent() != $content)
399          && ($merging == true) ) {
400         include_once('lib/editpage.php');
401         $request->setArg('pagename', $pagename);
402         $r = $current->getVersion();
403         $request->setArg('revision', $current->getVersion());
404         $p = new LoadFileConflictPageEditor($request);
405         $p->_content = $content;
406         $p->_currentVersion = $r - 1;
407         $p->editPage($saveFailed = true);
408         return; //early return
409     }
410
411     foreach ($pagedata as $key => $value) {
412         if (!empty($value))
413             $page->set($key, $value);
414     }
415
416     $mesg = HTML::dd();
417     $skip = false;
418     if ($source)
419         $mesg->pushContent(' ', fmt("from %s", $source));
420
421
422     $current = $page->getCurrentRevision();
423     if ($current->getVersion() == 0) {
424         $mesg->pushContent(' ', _("new page"));
425         $isnew = true;
426     }
427     else {
428         if ( (! $current->hasDefaultContents())
429              && ($current->getPackedContent() != $content) ) {
430             if ($overwrite) {
431                 $mesg->pushContent(' ',
432                                    fmt("has edit conflicts - overwriting anyway"));
433                 $skip = false;
434                 if (substr_count($source, 'pgsrc')) {
435                     $versiondata['author'] = _("The PhpWiki programming team");
436                     // but leave authorid as userid who loaded the file
437                 }
438             }
439             else {
440                 $mesg->pushContent(' ', fmt("has edit conflicts - skipped"));
441                 $needs_merge = true; // hackish
442                 $skip = true;
443             }
444         }
445         else if ($current->getPackedContent() == $content
446                  && $current->get('author') == $versiondata['author']) {
447             $mesg->pushContent(' ',
448                                fmt("is identical to current version %d - skipped",
449                                    $current->getVersion()));
450             $skip = true;
451         }
452         $isnew = false;
453     }
454
455     if (! $skip) {
456         $new = $page->save($content, WIKIDB_FORCE_CREATE, $versiondata);
457         $dbi->touch();
458         $mesg->pushContent(' ', fmt("- saved to database as version %d",
459                                     $new->getVersion()));
460     }
461     if ($needs_merge) {
462         $f = $source;
463         // hackish, $source contains needed path+filename
464         $f = str_replace(sprintf(_("MIME file %s"), ''), '', $f);
465         $f = str_replace(sprintf(_("Serialized file %s"), ''), '', $f);
466         $f = str_replace(sprintf(_("plain file %s"), ''), '', $f);
467         global $Theme;
468         $meb = Button(array('action' => 'loadfile',
469                             'merge'=> true,
470                             'source'=> dirname($f) . "/"
471                                        . basename($f)),
472                       _("Merge Edit"),
473                       _("PhpWikiAdministration"),
474                       'wikiadmin');
475         $owb = Button(array('action' => 'loadfile',
476                             'overwrite'=> true,
477                             'source'=> dirname($f) . "/"
478                                        . basename($f)),
479                       _("Restore Anyway"),
480                       _("PhpWikiAdministration"),
481                       'wikiunsafe');
482         $mesg->pushContent(' ', $meb, " ", $owb);
483     }
484
485     if ($skip)
486         PrintXML(HTML::dt(HTML::em(WikiLink($pagename))), $mesg);
487     else
488         PrintXML(HTML::dt(WikiLink($pagename)), $mesg);
489     flush();
490 }
491
492 function _tryinsertInterWikiMap($content) {
493     $goback = false;
494     if (strpos($content, "<verbatim>")) {
495         //$error_html = " The newly loaded pgsrc already contains a verbatim block.";
496         $goback = true;
497     }
498     if (!$goback && !defined('INTERWIKI_MAP_FILE')) {
499         $error_html = sprintf(" "._("%s: not defined"), "INTERWIKI_MAP_FILE");
500         $goback = true;
501     }
502     if (!$goback && !file_exists(INTERWIKI_MAP_FILE)) {
503         $error_html = sprintf(" "._("%s: file not found"), INTERWIKI_MAP_FILE);
504         $goback = true;
505     }
506
507     if (!empty($error_html))
508         trigger_error(_("Default InterWiki map file not loaded.")
509                       . $error_html, E_USER_NOTICE);
510
511     if ($goback)
512         return $content;
513
514     $filename = INTERWIKI_MAP_FILE;
515     trigger_error(sprintf(_("Loading InterWikiMap from external file %s."),
516                           $filename), E_USER_NOTICE);
517
518     $fd = fopen ($filename, "rb");
519     $data = fread ($fd, filesize($filename));
520     fclose ($fd);
521     $content = $content . "\n<verbatim>\n$data</verbatim>\n";
522     return $content;
523 }
524
525 function ParseSerializedPage($text, $default_pagename, $user)
526 {
527     if (!preg_match('/^a:\d+:{[si]:\d+/', $text))
528         return false;
529
530     $pagehash = unserialize($text);
531
532     // Split up pagehash into four parts:
533     //   pagename
534     //   content
535     //   page-level meta-data
536     //   revision-level meta-data
537
538     if (!defined('FLAG_PAGE_LOCKED'))
539         define('FLAG_PAGE_LOCKED', 1);
540     $pageinfo = array('pagedata'    => array(),
541                       'versiondata' => array());
542
543     $pagedata = &$pageinfo['pagedata'];
544     $versiondata = &$pageinfo['versiondata'];
545
546     // Fill in defaults.
547     if (empty($pagehash['pagename']))
548         $pagehash['pagename'] = $default_pagename;
549     if (empty($pagehash['author'])) {
550         $pagehash['author'] = $user->getId();
551     }
552
553     foreach ($pagehash as $key => $value) {
554         switch($key) {
555             case 'pagename':
556             case 'version':
557             case 'hits':
558                 $pageinfo[$key] = $value;
559                 break;
560             case 'content':
561                 $pageinfo[$key] = join("\n", $value);
562                 break;
563             case 'flags':
564                 if (($value & FLAG_PAGE_LOCKED) != 0)
565                     $pagedata['locked'] = 'yes';
566                 break;
567             case 'created':
568                 $pagedata[$key] = $value;
569                 break;
570             case 'lastmodified':
571                 $versiondata['mtime'] = $value;
572                 break;
573             case 'author':
574             case 'author_id':
575             case 'summary':
576                 $versiondata[$key] = $value;
577                 break;
578         }
579     }
580     return $pageinfo;
581 }
582
583 function SortByPageVersion ($a, $b) {
584     return $a['version'] - $b['version'];
585 }
586
587 function LoadFile (&$request, $filename, $text = false, $mtime = false)
588 {
589     if (!is_string($text)) {
590         // Read the file.
591         $stat  = stat($filename);
592         $mtime = $stat[9];
593         $text  = implode("", file($filename));
594     }
595
596     @set_time_limit(30); // Reset watchdog.
597
598     // FIXME: basename("filewithnoslashes") seems to return garbage sometimes.
599     $basename = basename("/dummy/" . $filename);
600
601     if (!$mtime)
602         $mtime = time();    // Last resort.
603
604     $default_pagename = rawurldecode($basename);
605
606     if ( ($parts = ParseMimeifiedPages($text)) ) {
607         usort($parts, 'SortByPageVersion');
608         foreach ($parts as $pageinfo)
609             SavePage($request, $pageinfo, sprintf(_("MIME file %s"),
610                                                   $filename), $basename);
611     }
612     else if ( ($pageinfo = ParseSerializedPage($text, $default_pagename,
613                                                $request->getUser())) ) {
614         SavePage($request, $pageinfo, sprintf(_("Serialized file %s"),
615                                               $filename), $basename);
616     }
617     else {
618         $user = $request->getUser();
619
620         // Assume plain text file.
621         $pageinfo = array('pagename' => $default_pagename,
622                           'pagedata' => array(),
623                           'versiondata'
624                           => array('author' => $user->getId()),
625                           'content'  => preg_replace('/[ \t\r]*\n/', "\n",
626                                                      chop($text))
627                           );
628         SavePage($request, $pageinfo, sprintf(_("plain file %s"), $filename),
629                  $basename);
630     }
631 }
632
633 function LoadZip (&$request, $zipfile, $files = false, $exclude = false) {
634     $zip = new ZipReader($zipfile);
635     while (list ($fn, $data, $attrib) = $zip->readFile()) {
636         // FIXME: basename("filewithnoslashes") seems to return
637         // garbage sometimes.
638         $fn = basename("/dummy/" . $fn);
639         if ( ($files && !in_array($fn, $files))
640              || ($exclude && in_array($fn, $exclude)) ) {
641             PrintXML(HTML::dt(WikiLink($fn)),
642                      HTML::dd(_("Skipping")));
643             continue;
644         }
645
646         LoadFile($request, $fn, $data, $attrib['mtime']);
647     }
648 }
649
650 function LoadDir (&$request, $dirname, $files = false, $exclude = false) {
651     $fileset = new LimitedFileSet($dirname, $files, $exclude);
652
653     if (($skiplist = $fileset->getSkippedFiles())) {
654         PrintXML(HTML::dt(HTML::strong(_("Skipping"))));
655         $list = HTML::ul();
656         foreach ($skiplist as $file)
657             $list->pushContent(HTML::li(WikiLink($file)));
658         PrintXML(HTML::dd($list));
659     }
660
661     // Defer HomePage loading until the end. If anything goes wrong
662     // the pages can still be loaded again.
663     $files = $fileset->getFiles();
664     if (in_array(HOME_PAGE, $files)) {
665         $files = array_diff($files, array(HOME_PAGE));
666         $files[] = HOME_PAGE;
667     }
668     foreach ($files as $file)
669         LoadFile($request, "$dirname/$file");
670 }
671
672 class LimitedFileSet extends FileSet {
673     function LimitedFileSet($dirname, $_include, $exclude) {
674         $this->_includefiles = $_include;
675         $this->_exclude = $exclude;
676         $this->_skiplist = array();
677         parent::FileSet($dirname);
678     }
679
680     function _filenameSelector($fn) {
681         $incl = &$this->_includefiles;
682         $excl = &$this->_exclude;
683
684         if ( ($incl && !in_array($fn, $incl))
685              || ($excl && in_array($fn, $excl)) ) {
686             $this->_skiplist[] = $fn;
687             return false;
688         } else {
689             return true;
690         }
691     }
692
693     function getSkippedFiles () {
694         return $this->_skiplist;
695     }
696 }
697
698
699 function IsZipFile ($filename_or_fd)
700 {
701     // See if it looks like zip file
702     if (is_string($filename_or_fd))
703     {
704         $fd    = fopen($filename_or_fd, "rb");
705         $magic = fread($fd, 4);
706         fclose($fd);
707     }
708     else
709     {
710         $fpos  = ftell($filename_or_fd);
711         $magic = fread($filename_or_fd, 4);
712         fseek($filename_or_fd, $fpos);
713     }
714
715     return $magic == ZIP_LOCHEAD_MAGIC || $magic == ZIP_CENTHEAD_MAGIC;
716 }
717
718
719 function LoadAny (&$request, $file_or_dir, $files = false, $exclude = false)
720 {
721     // Try urlencoded filename for accented characters.
722     if (!file_exists($file_or_dir)) {
723         // Make sure there are slashes first to avoid confusing phps
724         // with broken dirname or basename functions.
725         // FIXME: windows uses \ and :
726         if (is_integer(strpos($file_or_dir, "/"))) {
727             $file_or_dir = FindFile($file_or_dir);
728             // Panic
729             if (!file_exists($file_or_dir))
730                 $file_or_dir = dirname($file_or_dir) . "/"
731                     . urlencode(basename($file_or_dir));
732         } else {
733             // This is probably just a file.
734             $file_or_dir = urlencode($file_or_dir);
735         }
736     }
737
738     $type = filetype($file_or_dir);
739     if ($type == 'link') {
740         // For symbolic links, use stat() to determine
741         // the type of the underlying file.
742         list(,,$mode) = stat($file_or_dir);
743         $type = ($mode >> 12) & 017;
744         if ($type == 010)
745             $type = 'file';
746         elseif ($type == 004)
747             $type = 'dir';
748     }
749
750     if (! $type) {
751         $request->finish(fmt("Unable to load: %s", $file_or_dir));
752     }
753     else if ($type == 'dir') {
754         LoadDir($request, $file_or_dir, $files, $exclude);
755     }
756     else if ($type != 'file' && !preg_match('/^(http|ftp):/', $file_or_dir))
757     {
758         $request->finish(fmt("Bad file type: %s", $type));
759     }
760     else if (IsZipFile($file_or_dir)) {
761         LoadZip($request, $file_or_dir, $files, $exclude);
762     }
763     else /* if (!$files || in_array(basename($file_or_dir), $files)) */
764     {
765         LoadFile($request, $file_or_dir);
766     }
767 }
768
769 function LoadFileOrDir (&$request)
770 {
771     $source = $request->getArg('source');
772     StartLoadDump($request, fmt("Loading '%s'", HTML(dirname($source),
773                                                      "/",
774                                                      WikiLink(basename($source),
775                                                               'auto'))));
776     echo "<dl>\n";
777     LoadAny($request, $source);
778     echo "</dl>\n";
779     EndLoadDump($request);
780 }
781
782 function SetupWiki (&$request)
783 {
784     global $GenericPages, $LANG;
785
786
787     //FIXME: This is a hack (err, "interim solution")
788     // This is a bogo-bogo-login:  Login without
789     // saving login information in session state.
790     // This avoids logging in the unsuspecting
791     // visitor as "The PhpWiki programming team".
792     //
793     // This really needs to be cleaned up...
794     // (I'm working on it.)
795     $real_user = $request->_user;
796     $request->_user = new WikiUser($request, _("The PhpWiki programming team"),
797                                    WIKIAUTH_BOGO);
798
799     StartLoadDump($request, _("Loading up virgin wiki"));
800     echo "<dl>\n";
801
802     $pgsrc = FindLocalizedFile(WIKI_PGSRC);
803     $default_pgsrc = FindFile(DEFAULT_WIKI_PGSRC);
804     
805     if ($default_pgsrc != $pgsrc)
806         LoadAny($request, $default_pgsrc, $GenericPages);
807
808     LoadAny($request, $pgsrc);
809
810     echo "</dl>\n";
811     EndLoadDump($request);
812 }
813
814 function LoadPostFile (&$request)
815 {
816     $upload = $request->getUploadedFile('file');
817
818     if (!$upload)
819         $request->finish(_("No uploaded file to upload?")); // FIXME: more concise message
820
821
822     // Dump http headers.
823     StartLoadDump($request, sprintf(_("Uploading %s"), $upload->getName()));
824     echo "<dl>\n";
825
826     $fd = $upload->open();
827     if (IsZipFile($fd))
828         LoadZip($request, $fd, false, array(_("RecentChanges")));
829     else
830         LoadFile($request, $upload->getName(), $upload->getContents());
831
832     echo "</dl>\n";
833     EndLoadDump($request);
834 }
835
836 /**
837  $Log: not supported by cvs2svn $
838  Revision 1.85  2003/11/30 18:18:13  carstenklapp
839  Minor code optimization: use include_once instead of require_once
840  inside functions that might not always called.
841
842  Revision 1.84  2003/11/26 20:47:47  carstenklapp
843  Redo bugfix: My last refactoring broke merge-edit & overwrite
844  functionality again, should be fixed now. Sorry.
845
846  Revision 1.83  2003/11/20 22:18:54  carstenklapp
847  New feature: h1 during merge-edit displays WikiLink to original page.
848  Internal changes: Replaced some hackish url-generation code in
849  function SavePage (for pgsrc merge-edit) with appropriate Button()
850  calls.
851
852  Revision 1.82  2003/11/18 19:48:01  carstenklapp
853  Fixed missing gettext _() for button name.
854
855  Revision 1.81  2003/11/18 18:28:35  carstenklapp
856  Bugfix: In the Load File function of PhpWikiAdministration: When doing
857  a "Merge Edit" or "Restore Anyway", page names containing accented
858  letters (such as locale/de/pgsrc/G%E4steBuch) would produce a file not
859  found error (Use FilenameForPage funtion to urlencode page names).
860
861  Revision 1.80  2003/03/07 02:46:57  dairiki
862  Omit checks for safe_mode before set_time_limit().  Just prefix the
863  set_time_limit() calls with @ so that they fail silently if not
864  supported.
865
866  Revision 1.79  2003/02/26 01:56:05  dairiki
867  Only zip pages with legal pagenames.
868
869  Revision 1.78  2003/02/24 02:05:43  dairiki
870  Fix "n bytes written" message when dumping HTML.
871
872  Revision 1.77  2003/02/21 04:12:05  dairiki
873  Minor fixes for new cached markup.
874
875  Revision 1.76  2003/02/16 19:47:17  dairiki
876  Update WikiDB timestamp when editing or deleting pages.
877
878  Revision 1.75  2003/02/15 03:04:30  dairiki
879  Fix for WikiUser constructor API change.
880
881  Revision 1.74  2003/02/15 02:18:04  dairiki
882  When default language was English (at least), pgsrc was being
883  loaded twice.
884
885  LimitedFileSet: Fix typo/bug. ($include was being ignored.)
886
887  SetupWiki(): Fix bugs in loading of $GenericPages.
888
889  Revision 1.73  2003/01/28 21:09:17  zorloc
890  The get_cfg_var() function should only be used when one is
891  interested in the value from php.ini or similar. Use ini_get()
892  instead to get the effective value of a configuration variable.
893  -- Martin Geisler
894
895  Revision 1.72  2003/01/03 22:25:53  carstenklapp
896  Cosmetic fix to "Merge Edit" & "Overwrite" buttons. Added "The PhpWiki
897  programming team" as author when loading from pgsrc. Source
898  reformatting.
899
900  Revision 1.71  2003/01/03 02:48:05  carstenklapp
901  function SavePage: Added loadfile options for overwriting or merge &
902  compare a loaded pgsrc file with an existing page.
903
904  function LoadAny: Added a general error message when unable to load a
905  file instead of defaulting to "Bad file type".
906
907  */
908
909 // For emacs users
910 // Local Variables:
911 // mode: php
912 // tab-width: 8
913 // c-basic-offset: 4
914 // c-hanging-comment-ender-p: nil
915 // indent-tabs-mode: nil
916 // End:
917 ?>