Esempio n. 1
0
function MakeWikiZipHtml(&$request)
{
    $request->_TemplatesProcessed = array();
    $zipname = "wikihtml.zip";
    $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $zipname);
    $dbi =& $request->_dbi;
    $thispage = $request->getArg('pagename');
    // for "Return to ..."
    if ($exclude = $request->getArg('exclude')) {
        // exclude which pagenames
        $excludeList = explodePageList($exclude);
    } else {
        $excludeList = array();
    }
    if ($pages = $request->getArg('pages')) {
        // which pagenames
        if ($pages == '[]') {
            // current page
            $pages = $thispage;
        }
        $page_iter = new WikiDB_Array_PageIterator(explodePageList($pages));
    } else {
        $page_iter = $dbi->getAllPages(false, false, false, $excludeList);
    }
    global $WikiTheme;
    if (defined('HTML_DUMP_SUFFIX')) {
        $WikiTheme->HTML_DUMP_SUFFIX = HTML_DUMP_SUFFIX;
    }
    $WikiTheme->DUMP_MODE = 'ZIPHTML';
    $_bodyAttr = @$WikiTheme->_MoreAttr['body'];
    unset($WikiTheme->_MoreAttr['body']);
    /* ignore fatals in plugins */
    if (check_php_version(4, 1)) {
        global $ErrorManager;
        $ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
    }
    $request_args = $request->args;
    $timeout = !$request->getArg('start_debug') ? 20 : 240;
    while ($page = $page_iter->next()) {
        $request->args = $request_args;
        // some plugins might change them (esp. on POST)
        longer_timeout($timeout);
        // Reset watchdog
        $current = $page->getCurrentRevision();
        if ($current->getVersion() == 0) {
            continue;
        }
        $pagename = $page->getName();
        if (in_array($pagename, $excludeList)) {
            continue;
        }
        $attrib = array('mtime' => $current->get('mtime'), 'is_ascii' => 1);
        if ($page->get('locked')) {
            $attrib['write_protected'] = 1;
        }
        $request->setArg('pagename', $pagename);
        // Template::_basepage fix
        $filename = FilenameForPage($pagename) . $WikiTheme->HTML_DUMP_SUFFIX;
        $revision = $page->getCurrentRevision();
        $transformedContent = $revision->getTransformedContent();
        $template = new Template('browse', $request, array('revision' => $revision, 'CONTENT' => $transformedContent));
        $data = GeneratePageasXML($template, $pagename);
        $zip->addRegularFile($filename, $data, $attrib);
        if (USECACHE) {
            $request->_dbi->_cache->invalidate_cache($pagename);
            unset($request->_dbi->_cache->_pagedata_cache);
            unset($request->_dbi->_cache->_versiondata_cache);
            unset($request->_dbi->_cache->_glv_cache);
        }
        unset($request->_dbi->_cache->_backend->_page_data);
        unset($revision->_transformedContent);
        unset($revision);
        unset($template->_request);
        unset($template);
        unset($data);
    }
    $page_iter->free();
    $attrib = false;
    // Deal with css and images here.
    if (!empty($WikiTheme->dumped_images) and is_array($WikiTheme->dumped_images)) {
        // dirs are created automatically
        //if ($WikiTheme->dumped_images) $zip->addRegularFile("images", "", $attrib);
        foreach ($WikiTheme->dumped_images as $img_file) {
            if ($from = $WikiTheme->_findFile($img_file, true) and basename($from)) {
                $target = "images/" . basename($img_file);
                if (check_php_version(4, 3)) {
                    $zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
                } else {
                    $zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
                }
            }
        }
    }
    if (!empty($WikiTheme->dumped_buttons) and is_array($WikiTheme->dumped_buttons)) {
        //if ($WikiTheme->dumped_buttons) $zip->addRegularFile("images/buttons", "", $attrib);
        foreach ($WikiTheme->dumped_buttons as $text => $img_file) {
            if ($from = $WikiTheme->_findFile($img_file, true) and basename($from)) {
                $target = "images/buttons/" . basename($img_file);
                if (check_php_version(4, 3)) {
                    $zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
                } else {
                    $zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
                }
            }
        }
    }
    if (!empty($WikiTheme->dumped_css) and is_array($WikiTheme->dumped_css)) {
        foreach ($WikiTheme->dumped_css as $css_file) {
            if ($from = $WikiTheme->_findFile(basename($css_file), true) and basename($from)) {
                $target = basename($css_file);
                if (check_php_version(4, 3)) {
                    $zip->addRegularFile($target, file_get_contents($WikiTheme->_path . $from), $attrib);
                } else {
                    $zip->addRegularFile($target, join('', file($WikiTheme->_path . $from)), $attrib);
                }
            }
        }
    }
    $zip->finish();
    if (check_php_version(4, 1)) {
        global $ErrorManager;
        $ErrorManager->popErrorHandler();
    }
    $WikiTheme->HTML_DUMP_SUFFIX = '';
    $WikiTheme->DUMP_MODE = false;
    $WikiTheme->_MoreAttr['body'] = $_bodyAttr;
}
Esempio n. 2
0
function _DumpHtmlToDir($target, $page_iter, $exclude = false)
{
    global $WikiTheme, $request, $ErrorManager;
    $silent = true;
    $zip = false;
    $directory = false;
    if ($WikiTheme->DUMP_MODE == 'HTML') {
        $directory = $target;
        $silent = false;
    } elseif ($WikiTheme->DUMP_MODE == 'PDFHTML') {
        $directory = $target;
    } elseif (is_object($target)) {
        // $WikiTheme->DUMP_MODE == 'ZIPHTML'
        $zip = $target;
    }
    $request->_TemplatesProcessed = array();
    if ($exclude) {
        // exclude which pagenames
        $excludeList = explodePageList($exclude);
    } else {
        $excludeList = array('DebugAuthInfo', 'DebugGroupInfo', 'AuthInfo');
    }
    $WikiTheme->VALID_LINKS = array();
    if ($request->getArg('format')) {
        // pagelist
        $page_iter_sav = $page_iter;
        foreach ($page_iter_sav->asArray() as $handle) {
            $WikiTheme->VALID_LINKS[] = is_string($handle) ? $handle : $handle->getName();
        }
        $page_iter_sav->reset();
    }
    if (defined('HTML_DUMP_SUFFIX')) {
        $WikiTheme->HTML_DUMP_SUFFIX = HTML_DUMP_SUFFIX;
    }
    $_bodyAttr = @$WikiTheme->_MoreAttr['body'];
    unset($WikiTheme->_MoreAttr['body']);
    $ErrorManager->pushErrorHandler(new WikiFunctionCb('_dump_error_handler'));
    // check if the dumped file will be accessible from outside
    $doc_root = $request->get("DOCUMENT_ROOT");
    if ($WikiTheme->DUMP_MODE == 'HTML') {
        $ldir = NormalizeLocalFileName($directory);
        $wikiroot = NormalizeLocalFileName('');
        if (string_starts_with($ldir, $doc_root)) {
            $link_prefix = substr($directory, strlen($doc_root)) . "/";
        } elseif (string_starts_with($ldir, $wikiroot)) {
            $link_prefix = NormalizeWebFileName(substr($directory, strlen($wikiroot))) . "/";
        } else {
            $prefix = '';
            if (isWindows()) {
                $prefix = '/';
                // . substr($doc_root,0,2); // add drive where apache is installed
            }
            $link_prefix = "file://" . $prefix . $directory . "/";
        }
    } else {
        $link_prefix = "";
    }
    $request_args = $request->args;
    $timeout = !$request->getArg('start_debug') ? 60 : 240;
    if ($directory) {
        if (isWindows()) {
            $directory = str_replace("\\", "/", $directory);
        }
        // no Win95 support.
        @mkdir("{$directory}/images");
    }
    $already = array();
    $outfiles = array();
    $already_images = array();
    while ($page = $page_iter->next()) {
        if (is_string($page)) {
            $pagename = $page;
            $page = $request->_dbi->getPage($pagename);
        } else {
            $pagename = $page->getName();
        }
        if (empty($firstpage)) {
            $firstpage = $pagename;
        }
        if (array_key_exists($pagename, $already)) {
            continue;
        }
        $already[$pagename] = 1;
        $current = $page->getCurrentRevision();
        //if ($current->getVersion() == 0)
        //    continue;
        $request->args = $request_args;
        // some plugins might change them (esp. on POST)
        longer_timeout($timeout);
        // Reset watchdog
        if ($zip) {
            $attrib = array('mtime' => $current->get('mtime'), 'is_ascii' => 1);
            if ($page->get('locked')) {
                $attrib['write_protected'] = 1;
            }
        } elseif (!$silent) {
            if (!isa($request, 'MockRequest')) {
                PrintXML(HTML::br(), $pagename, ' ... ');
                flush();
            }
        }
        if (in_array($pagename, $excludeList)) {
            if (!$silent and !isa($request, 'MockRequest')) {
                PrintXML(_("Skipped."));
                flush();
            }
            continue;
        }
        $relative_base = '';
        if ($WikiTheme->DUMP_MODE == 'PDFHTML') {
            $request->setArg('action', 'pdf');
        }
        // to omit cache headers
        $request->setArg('pagename', $pagename);
        // Template::_basepage fix
        $filename = FilenameForPage($pagename) . $WikiTheme->HTML_DUMP_SUFFIX;
        $args = array('revision' => $current, 'CONTENT' => $current->getTransformedContent(), 'relative_base' => $relative_base);
        // For every %2F will need to mkdir -p dirname($pagename)
        if (preg_match("/(%2F|\\/)/", $filename)) {
            // mkdir -p and set relative base for subdir pages
            $filename = preg_replace("/%2F/", "/", $filename);
            $count = substr_count($filename, "/");
            $dirname = dirname($filename);
            if ($directory) {
                mkdir_p($directory . "/" . $dirname);
            }
            // Fails with "XX / YY", "XX" is created, "XX / YY" cannot be written
            // if (isWindows()) // interesting Windows bug: cannot mkdir "bla "
            // Since dumps needs to be copied, we have to disallow this for all platforms.
            $filename = preg_replace("/ \\//", "/", $filename);
            $relative_base = "../";
            while ($count > 1) {
                $relative_base .= "../";
                $count--;
            }
            $args['relative_base'] = $relative_base;
        }
        $msg = HTML();
        $DUMP_MODE = $WikiTheme->DUMP_MODE;
        $data = GeneratePageasXML(new Template('browse', $request, $args), $pagename, $current, $args);
        $WikiTheme->DUMP_MODE = $DUMP_MODE;
        if (preg_match_all("/<img .*?src=\"(\\/.+?)\"/", $data, $m)) {
            // fix to local relative path for uploaded images, so that pdf will work
            foreach ($m[1] as $img_file) {
                $base = basename($img_file);
                $data = str_replace('src="' . $img_file . '"', 'src="images/' . $base . '"', $data);
                if (array_key_exists($img_file, $already_images)) {
                    continue;
                }
                $already_images[$img_file] = 1;
                // resolve src from webdata to file
                $src = $doc_root . $img_file;
                if (file_exists($src) and $base) {
                    if ($directory) {
                        $target = "{$directory}/images/{$base}";
                        if (copy($src, $target)) {
                            if (!$silent) {
                                _copyMsg($img_file, fmt("... copied to %s", $target));
                            }
                        } else {
                            if (!$silent) {
                                _copyMsg($img_file, fmt("... not copied to %s", $target));
                            }
                        }
                    } else {
                        $target = "images/{$base}";
                        $zip->addSrcFile($target, $src);
                    }
                }
            }
        }
        if ($directory) {
            $outfile = $directory . "/" . $filename;
            if (!($fd = fopen($outfile, "wb"))) {
                $msg->pushContent(HTML::strong(fmt("couldn't open file '%s' for writing", $outfile)));
                $request->finish($msg);
            }
            $len = strlen($data);
            $num = fwrite($fd, $data, $len);
            if ($pagename != $filename) {
                $link = LinkURL($link_prefix . $filename, $filename);
                $msg->pushContent(HTML::small(_("saved as "), $link, " ... "));
            }
            $msg->pushContent(HTML::small(fmt("%s bytes written", $num), "\n"));
            if (!$silent) {
                if (!isa($request, 'MockRequest')) {
                    PrintXML($msg);
                }
                flush();
                $request->chunkOutput();
            }
            assert($num == $len);
            fclose($fd);
            $outfiles[] = $outfile;
        } else {
            $zip->addRegularFile($filename, $data, $attrib);
        }
        if (USECACHE) {
            $request->_dbi->_cache->invalidate_cache($pagename);
            unset($request->_dbi->_cache->_pagedata_cache);
            unset($request->_dbi->_cache->_versiondata_cache);
            unset($request->_dbi->_cache->_glv_cache);
        }
        unset($request->_dbi->_cache->_backend->_page_data);
        unset($msg);
        unset($current->_transformedContent);
        unset($current);
        if (!empty($template)) {
            unset($template->_request);
            unset($template);
        }
        unset($data);
    }
    $page_iter->free();
    $attrib = false;
    //array('is_ascii' => 0);
    if (!empty($WikiTheme->dumped_images) and is_array($WikiTheme->dumped_images)) {
        // @mkdir("$directory/images");
        foreach ($WikiTheme->dumped_images as $img_file) {
            if (array_key_exists($img_file, $already_images)) {
                continue;
            }
            $already_images[$img_file] = 1;
            if ($img_file and $from = $WikiTheme->_findFile($img_file, true) and basename($from)) {
                if ($directory) {
                    $target = "{$directory}/images/" . basename($from);
                    if ($silent) {
                        copy($WikiTheme->_path . $from, $target);
                    } else {
                        if (copy($WikiTheme->_path . $from, $target)) {
                            _copyMsg($from, fmt("... copied to %s", $target));
                        } else {
                            _copyMsg($from, fmt("... not copied to %s", $target));
                        }
                    }
                } else {
                    $target = "images/" . basename($from);
                    $zip->addSrcFile($target, $WikiTheme->_path . $from);
                }
            } elseif (!$silent) {
                _copyMsg($from, _("... not found"));
            }
        }
    }
    if (!empty($WikiTheme->dumped_buttons) and is_array($WikiTheme->dumped_buttons)) {
        // Buttons also
        if ($directory) {
            @mkdir("{$directory}/images/buttons");
        }
        foreach ($WikiTheme->dumped_buttons as $text => $img_file) {
            if (array_key_exists($img_file, $already_images)) {
                continue;
            }
            $already_images[$img_file] = 1;
            if ($img_file and $from = $WikiTheme->_findFile($img_file, true) and basename($from)) {
                if ($directory) {
                    $target = "{$directory}/images/buttons/" . basename($from);
                    if ($silent) {
                        copy($WikiTheme->_path . $from, $target);
                    } else {
                        if (copy($WikiTheme->_path . $from, $target)) {
                            _copyMsg($from, fmt("... copied to %s", $target));
                        } else {
                            _copyMsg($from, fmt("... not copied to %s", $target));
                        }
                    }
                } else {
                    $target = "images/buttons/" . basename($from);
                    $zip->addSrcFile($target, $WikiTheme->_path . $from);
                }
            } elseif (!$silent) {
                _copyMsg($from, _("... not found"));
            }
        }
    }
    if (!empty($WikiTheme->dumped_css) and is_array($WikiTheme->dumped_css)) {
        foreach ($WikiTheme->dumped_css as $css_file) {
            if (array_key_exists($css_file, $already_images)) {
                continue;
            }
            $already_images[$css_file] = 1;
            if ($css_file and $from = $WikiTheme->_findFile(basename($css_file), true) and basename($from)) {
                // TODO: fix @import url(main.css);
                if ($directory) {
                    $target = "{$directory}/" . basename($css_file);
                    if ($silent) {
                        copy($WikiTheme->_path . $from, $target);
                    } else {
                        if (copy($WikiTheme->_path . $from, $target)) {
                            _copyMsg($from, fmt("... copied to %s", $target));
                        } else {
                            _copyMsg($from, fmt("... not copied to %s", $target));
                        }
                    }
                } else {
                    //$attrib = array('is_ascii' => 0);
                    $target = basename($css_file);
                    $zip->addSrcFile($target, $WikiTheme->_path . $from);
                }
            } elseif (!$silent) {
                _copyMsg($from, _("... not found"));
            }
        }
    }
    if ($zip) {
        $zip->finish();
    }
    if ($WikiTheme->DUMP_MODE == 'PDFHTML') {
        if (USE_EXTERNAL_HTML2PDF and $outfiles) {
            $cmd = EXTERNAL_HTML2PDF_PAGELIST . ' "' . join('" "', $outfiles) . '"';
            $filename = FilenameForPage($firstpage);
            if (DEBUG) {
                $tmpfile = $directory . "/createpdf.bat";
                $fp = fopen($tmpfile, "wb");
                fwrite($fp, $cmd . " > {$filename}.pdf");
                fclose($fp);
            }
            if (!headers_sent()) {
                Header('Content-Type: application/pdf');
                passthru($cmd);
            } else {
                $tmpdir = getUploadFilePath();
                $s = passthru($cmd . " > {$tmpdir}/{$filename}.pdf");
                $errormsg = "<br />\nGenerated <a href=\"" . getUploadDataPath() . "{$filename}.pdf\">Upload:{$filename}.pdf</a>\n";
                $errormsg .= $s;
                echo $errormsg;
            }
            if (!DEBUG) {
                foreach ($outfiles as $f) {
                    unlink($f);
                }
            }
        }
        if (!empty($errormsg)) {
            $request->discardOutput();
            $GLOBALS['ErrorManager']->_postponed_errors = array();
        }
    }
    $ErrorManager->popErrorHandler();
    $WikiTheme->HTML_DUMP_SUFFIX = '';
    $WikiTheme->DUMP_MODE = false;
    $WikiTheme->_MoreAttr['body'] = $_bodyAttr;
}