Example #1
0
function ewiki_page_sitemap($id = 0, $data = 0, $action = 0)
{
    global $ewiki_config;
    //**code hijacked from page_pageindex.php**
    //creates a list of all of the valid wiki pages in the site
    $str_null = NULL;
    $a_validpages = ewiki_valid_pages(0, 1);
    //**end of hijacked code**
    //$time_end=getmicrotime();
    //creates the title bar on top of page
    if ($id == EWIKI_PAGE_SITEMAP) {
        $o = ewiki_make_title($id, ewiki_t($id), 2);
        foreach ($ewiki_config["SiteMap"]["RootList"] as $root) {
            if (isset($a_validpages[$root])) {
                $valid_root = TRUE;
                $str_rootid = $root;
                break;
            }
        }
    } else {
        $o = ewiki_make_title($id, ewiki_t("SMFOR") . " " . $id, 2);
        if (isset($a_validpages[$id])) {
            $valid_root = TRUE;
            $str_rootid = $id;
        }
    }
    $o .= "<p>" . ewiki_t("VIEWSMFOR") . " ";
    foreach ($ewiki_config["SiteMap"]["RootList"] as $root) {
        if (isset($a_validpages[$root])) {
            $o .= '<a href="' . ewiki_script('sitemap/', $root) . '">' . $root . '</a> ';
        }
    }
    $o .= "</p>";
    //checks to see if the user is allowed to view the root page
    if (!isset($a_validpages[$str_rootid])) {
        $o .= ewiki_t("INVALIDROOT");
        return $o;
    }
    //$timesitemap=getmicrotime();
    $a_sitemap = ewiki_sitemap_create($str_rootid, $a_validpages, EWIKI_SITEMAP_DEPTH);
    $timer = array();
    $level = -1;
    $fordump = 0;
    $str_formatted = "<ul>\n<li><a href=\"" . EWIKI_SCRIPT . $str_rootid . "\">" . $str_rootid . "</a></li>";
    $fin_level = format_sitemap($a_sitemap, $str_rootid, $str_formatted, $level, $timer, $fordump);
    $str_formatted .= "</ul>" . str_pad("", $fin_level * 6, "</ul>\n");
    $o .= $str_formatted;
    //$timesitemap_end=getmicrotime();
    //$o.="GetAll: ".($time_end-$time)."\n";
    //$o.="SiteMap: ".($timesitemap_end-$timesitemap)."\n";
    //$o.="Total: ".($timesitemap_end-$time);
    return $o;
}
function ewiki_page_wiki_dump_send($exportbinaries = 0, $exportformats = 0, $withvirtualpages = 0, $exportdestinations = 0)
{
    global $ewiki_config, $wiki, $ewiki_plugins, $wiki_entry, $course, $CFG, $ewiki_t, $userid, $groupid;
    $filestozip = array();
    #-- disable protected email
    if (is_array($ewiki_plugins["link_url"])) {
        foreach ($ewiki_plugins["link_url"] as $key => $linkplugin) {
            if ($linkplugin == "ewiki_email_protect_link") {
                unset($ewiki_plugins["link_url"][$key]);
            }
        }
    }
    /// HTML-Export
    if ($exportformats == 1) {
        #-- if exportformats is html
        $HTML_TEMPLATE = '<html>
      <head>' . $ewiki_t["c"]["EWIKIDUMPCSS"] . '
      <title>$title</title>
      </head>
      <body bgcolor="#ffffff";>
      <div id="PageText">
      <h2>$title</h2>
      $content
      </div>
      </body>
      </html>';
        #-- reconfigure ewiki_format() to generate offline pages and files
        $html_ext = ".html";
        $ewiki_config["script"] = "%s{$html_ext}";
        $ewiki_config["script_binary"] = "%s";
    }
    // Export Virtual pages special
    $a_virtual = array_keys($ewiki_plugins["page"]);
    #-- get all pages / binary files
    $a_validpages = ewiki_valid_pages(1, $withvirtualpages);
    $a_pagelist = ewiki_sitemap_create($wiki_entry->pagename, $a_validpages, 100, 1);
    # Add linked binary files to pagelist
    foreach ($a_pagelist as $key => $value) {
        if (is_array($a_validpages[$value]["refs"])) {
            foreach ($a_validpages[$value]["refs"] as $refs) {
                if ($a_validpages[$refs]["type"] == "image" || $a_validpages[$refs]["type"] == "file") {
                    $a_pagelist[] = $refs;
                }
            }
        }
    }
    # Adjust links to binary files
    foreach ($a_pagelist as $key => $value) {
        if ($a_validpages[$value]["type"] == "image") {
            $a_images[] = urlencode($value);
            $a_rimages[] = urlencode(preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $value));
            unset($a_validpages[$value]);
        }
        if ($a_validpages[$value]["type"] == "file") {
            $a_images[] = urlencode($value);
            $a_rimages[] = clean_filename(substr($value, strlen(EWIKI_IDF_INTERNAL)));
            $a_images[] = $value;
            $a_rimages[] = clean_filename(substr($value, strlen(EWIKI_IDF_INTERNAL)));
            unset($a_validpages[$value]);
        }
    }
    # Remove binaries from a_validpages and add to a_pagelist
    foreach ($a_validpages as $key => $value) {
        if ($a_validpages[$key]["type"] == "image" || $a_validpages[$key]["type"] == "file") {
            $a_pagelist[] = $key;
            unset($a_validpages[$key]);
        }
    }
    #print "<pre>"; print_r($a_validpages); print "</pre>";
    #print "<hr /><pre>"; print_r($a_pagelist); print "</pre>";
    $a_sitemap = ewiki_sitemap_create($wiki_entry->pagename, $a_validpages, 99, 0);
    if ($a_pagelist) {
        #-- create new zip file
        #if($arctype == "ZIP"){
        #  $archivename=EWIKI_WIKIDUMP_ARCNAME."$rootid.zip";
        #  $archive = new ewiki_virtual_zip();
        #} elseif ($arctype == "TAR") {
        #  $archivename=EWIKI_WIKIDUMP_ARCNAME."$rootid.tar";
        #  $archive = new ewiki_virtual_tarball();
        #} else {
        #  die();
        #}
        /// Create/Set Directory
        $wname = clean_filename(strip_tags(format_string($wiki->name, true)));
        if ($exportdestinations) {
            if (wiki_is_teacher($wiki)) {
                $exportdir = $CFG->dataroot . "/" . $course->id . "/" . $exportdestinations;
            } else {
                add_to_log($course->id, "wiki", "hack", "", format_string($wiki->name, true) . ": Tried to export a wiki as non-teacher into {$exportdestinations}.");
                error("You are not a teacher !");
            }
        } else {
            $exportbasedir = tempnam("/tmp", "WIKIEXPORT");
            @unlink($exportbasedir);
            @mkdir($exportbasedir);
            /// maybe we need to check the name here...?
            $exportdir = $exportbasedir . "/" . $wname;
            @mkdir($exportdir);
            if (!is_dir($exportdir)) {
                error("Cannot create temporary directory {$exportdir} !");
            }
        }
        $a_pagelist = array_unique($a_pagelist);
        #-- convert all pages
        foreach ($a_pagelist as $pagename) {
            if (!in_array($pagename, $a_virtual)) {
                $id = $pagename;
                #-- not a virtual page
                $row = ewiki_database("GET", array("id" => $pagename));
                $content = "";
            } elseif ($withvirtualpages) {
                $id = $pagename;
                #-- is a virtual page
                $pf = $ewiki_plugins["page"][$id];
                $content = $pf($id, $content, "view");
                if ($exportformats == 1) {
                    $content = str_replace('$content', $content, str_replace('$title', $id, $HTML_TEMPLATE));
                }
                $fn = urlencode($id);
                $fn = preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $fn);
                $fn = $fn . $html_ext;
            } else {
                continue;
            }
            if (empty($content)) {
                switch ($row["flags"] & EWIKI_DB_F_TYPE) {
                    // Text Page
                    case EWIKI_DB_F_TEXT:
                        #print "<pre>"; print_r($row[content]); print "\n-------------</pre>";
                        if ($exportformats == 1) {
                            /// HTML-Export
                            $content = ewiki_format($row["content"]);
                        } else {
                            $content = $row["content"];
                        }
                        # Binary files link adjustment when html
                        if ($exportformats == 1) {
                            $content = str_replace($a_images, $a_rimages, $content);
                        }
                        $fn = preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", urlencode($id));
                        $fn = $fn . $html_ext;
                        if ($exportformats == 1) {
                            /// HTML-Export
                            $content = str_replace('$content', $content, str_replace('$title', $id, $HTML_TEMPLATE));
                        }
                        break;
                    case EWIKI_DB_F_BINARY:
                        #print "Binary: $row[id]<br />";
                        if (($row["meta"]["class"] == "image" || $row["meta"]["class"] == "file") && $exportbinaries) {
                            # Copy files to the appropriate directory
                            $fn = moodle_binary_get_path($id, $row["meta"], $course, $wiki, $userid, $groupid);
                            $destfn = clean_filename(substr($id, strlen(EWIKI_IDF_INTERNAL)));
                            $dest = "{$exportdir}/" . $destfn;
                            if (!copy($fn, $dest)) {
                                notify("Cannot copy {$fn} to {$dest}.");
                            }
                            #$fn = urlencode(preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $id));
                            #$content = &$row["content"];
                            $filestozip[] = $exportdir . "/" . $destfn;
                            continue 2;
                        } else {
                            #-- php considers switch statements as loops so continue 2 is needed to
                            #-- hit the end of the for loop
                            continue 2;
                        }
                        break;
                    default:
                        # don't want it
                        continue 2;
                }
            }
            # Do not translate links when wiki already in pure html - mode
            if ($wiki->htmlmode != 2) {
                $content = preg_replace_callback('/(<a href=")(.*?)(\\.html">)/', create_function('$matches', 'return($matches[1].preg_replace(EWIKI_DUMP_FILENAME_REGEX,"",$matches[2]).$matches[3]);'), $content);
            }
            #-- add file
            // Let's make sure the file exists and is writable first.
            if (!($handle = fopen($exportdir . "/" . $fn, 'w'))) {
                error("Cannot open file ({$exportdir}/{$fn})");
            }
            // Write $content to our opened file.
            if (fwrite($handle, $content) === FALSE) {
                error("Cannot write to file ({$exportdir}/{$fn})");
            }
            fclose($handle);
            $filestozip[] = $exportdir . "/" . $fn;
            #$archive->add($content, $fn, array(
            #  "mtime" => $row["lastmodified"],
            #  "uname" => "ewiki",
            #  "mode" => 0664 | (($row["flags"]&EWIKI_DB_F_WRITEABLE)?0002:0000),
            #  ), $complevel);
        }
        #-- create index page
        /// HTML-Export
        if ($exportformats == 1) {
            $timer = array();
            $level = -1;
            $fordump = 1;
            $str_formatted = "<ul>\n<li><a href=\"" . $wiki_entry->pagename . $html_ext . "\">" . $wiki_entry->pagename . "</a></li>";
            $fin_level = format_sitemap($a_sitemap, $wiki_entry->pagename, $str_formatted, $level, $timer, $fordump);
            $str_formatted .= "</ul>" . str_pad("", $fin_level * 6, "</ul>\n");
            $str_formatted = preg_replace_callback('/(<a href=")(.*?)(\\.html">)/', create_function('$matches', 'return($matches[1].preg_replace(EWIKI_DUMP_FILENAME_REGEX,"",$matches[2]).$matches[3]);'), $str_formatted);
            $str_formatted = str_replace('$content', $str_formatted, str_replace('$title', get_string("index", "wiki"), $HTML_TEMPLATE));
            #-- add file
            // Let's make sure the file exists and is writable first.
            $indexname = "index" . $html_ext;
            if (!($handle = fopen($exportdir . "/" . $indexname, 'w'))) {
                error("Cannot open file ({$exportdir}/{$indexname})");
            }
            // Write $somecontent to our opened file.
            if (fwrite($handle, $str_formatted) === FALSE) {
                error("Cannot write to file ({$exportdir}/{$indexname})");
            }
            fclose($handle);
            $filestozip[] = $exportdir . "/" . $indexname;
            #-- add index page
            #    $archive->add($str_formatted, "Index_$rootid".$html_ext, array(
            #      "mtime" => $row["lastmodified"],
            #      "uname" => "ewiki",
            #      "mode" => 0664 | (($row["flags"]&EWIKI_DB_F_WRITEABLE)?0002:0000),
            #      ), $complevel);
        }
        if (!$exportdestinations) {
            $archivename = $wname . ".zip";
            zip_files($filestozip, "{$exportbasedir}/{$archivename}");
            #-- Headers
            Header("Content-type: application/zip");
            Header("Content-disposition: attachment; filename=\"{$archivename}\"");
            Header("Cache-control: private");
            Header("Original-Filename: {$archivename}");
            Header("X-Content-Type: application/zip");
            Header("Content-Location: {$archivename}");
            if (!@readfile("{$exportbasedir}/{$archivename}")) {
                error("Cannot read {$exportbasedir}/{$archivename}");
            }
            if (!deldir($exportbasedir)) {
                error("Cannot delete {$exportbasedir}");
            }
            #exit();
            return false;
        } else {
            return get_string("exportsuccessful", "wiki") . "<br />";
        }
    }
}
Example #3
0
function ewiki_page_wiki_dump_send($imgs = 1, $fullhtml = 0, $virtual = 0, $rootid, $depth = 1, $arctype = EWIKI_WIKIDUMP_DEFAULTTYPE, $complevel = 1)
{
    global $ewiki_config, $ewiki_plugins;
    #-- disable protected email
    foreach ($ewiki_plugins["link_url"] as $key => $linkplugin) {
        if ($linkplugin == "ewiki_email_protect_link") {
            unset($ewiki_plugins["link_url"][$key]);
        }
    }
    #-- set uservars
    $a_uservars = ewiki_get_uservar("WikiDump", array());
    if (!is_array($a_uservars)) {
        $a_uservars = unserialize($a_uservars);
    }
    $a_uservars[$rootid] = $depth;
    ewiki_set_uservar("WikiDump", $a_uservars);
    #-- if $fullhtml
    $HTML_TEMPLATE = '<html>
    <head>' . ewiki_t("EWIKIDUMPCSS") . '
    <title>$title</title>
    </head>
    <body bgcolor="#ffffff";>
    <div id="PageText">
    <h2>$title</h2>
    $content
    </div>
    </body>
    </html>
    ';
    #-- reconfigure ewiki_format() to generate offline pages and files
    $html_ext = ".htm";
    if ($fullhtml) {
        $html_ext = ".html";
    }
    $ewiki_config["script"] = "%s{$html_ext}";
    $ewiki_config["script_binary"] = "%s";
    #-- fetch also dynamic pages
    $a_virtual = array_keys($ewiki_plugins["page"]);
    #-- get all pages / binary files
    $a_validpages = ewiki_valid_pages(1, $virtual);
    $a_pagelist = ewiki_sitemap_create($rootid, $a_validpages, $depth, 1);
    foreach ($a_pagelist as $key => $value) {
        if (is_array($a_validpages[$value]["refs"])) {
            foreach ($a_validpages[$value]["refs"] as $refs) {
                if ($a_validpages[$refs]["type"] == "image") {
                    $a_pagelist[] = $refs;
                }
            }
        }
    }
    foreach ($a_pagelist as $key => $value) {
        if ($a_validpages[$value]["type"] == "image") {
            $a_images[] = urlencode($value);
            $a_rimages[] = urlencode(preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $value));
            unset($a_validpages[$value]);
        }
    }
    $a_sitemap = ewiki_sitemap_create($rootid, $a_validpages, $depth, 0);
    if ($a_pagelist) {
        #-- create new zip file
        if ($arctype == "ZIP") {
            $archivename = EWIKI_WIKIDUMP_ARCNAME . "{$rootid}.zip";
            $archive = new ewiki_virtual_zip();
        } elseif ($arctype == "TAR") {
            $archivename = EWIKI_WIKIDUMP_ARCNAME . "{$rootid}.tar";
            $archive = new ewiki_virtual_tarball();
        } else {
            die;
        }
        $a_pagelist = array_unique($a_pagelist);
        #-- convert all pages
        foreach ($a_pagelist as $pagename) {
            if (!in_array($pagename, $a_virtual)) {
                $id = $pagename;
                #-- not a virtual page
                $row = ewiki_db::GET($pagename);
                $content = "";
            } elseif ($virtual) {
                $id = $pagename;
                #-- is a virtual page
                $pf = $ewiki_plugins["page"][$id];
                $content = $pf($id, $content, "view");
                if ($fullhtml) {
                    $content = str_replace('$content', $content, str_replace('$title', $id, $HTML_TEMPLATE));
                }
                $fn = urlencode($id);
                $fn = preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $fn);
                $fn = $fn . $html_ext;
            } else {
                continue;
            }
            if (empty($content)) {
                switch ($row["flags"] & EWIKI_DB_F_TYPE) {
                    case EWIKI_DB_F_TEXT:
                        $content = ewiki_format($row["content"]);
                        $content = str_replace($a_images, $a_rimages, $content);
                        $fn = preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", urlencode($id));
                        $fn = $fn . $html_ext;
                        if ($fullhtml) {
                            $content = str_replace('$content', $content, str_replace('$title', $id, $HTML_TEMPLATE));
                        }
                        break;
                    case EWIKI_DB_F_BINARY:
                        if ($row["meta"]["class"] == "image" && $imgs) {
                            $fn = urlencode(preg_replace(EWIKI_DUMP_FILENAME_REGEX, "", $id));
                            $content =& $row["content"];
                        } else {
                            #-- php considers switch statements as loops so continue 2 is needed to
                            #-- hit the end of the for loop
                            continue 2;
                        }
                        break;
                    default:
                        # don't want it
                        continue 2;
                }
            }
            $content = preg_replace_callback('/(<a href=")(.*?)(\\.html">)/', create_function('$matches', 'return($matches[1].preg_replace(EWIKI_DUMP_FILENAME_REGEX,"",$matches[2]).$matches[3]);'), $content);
            #-- add file
            $archive->add($content, $fn, array("mtime" => $row["lastmodified"], "uname" => "ewiki", "mode" => 0664 | ($row["flags"] & EWIKI_DB_F_WRITEABLE ? 02 : 00)), $complevel);
        }
        #-- create index page
        $timer = array();
        $level = -1;
        $fordump = 1;
        $str_formatted = "<ul>\n<li><a href=\"" . $rootid . $html_ext . "\">" . $rootid . "</a></li>";
        $fin_level = format_sitemap($a_sitemap, $rootid, $str_formatted, $level, $timer, $fordump);
        $str_formatted .= "</ul>" . str_pad("", $fin_level * 6, "</ul>\n");
        $str_formatted = preg_replace_callback('/(<a href=")(.*?)(\\.html">)/', create_function('$matches', 'return($matches[1].preg_replace(EWIKI_DUMP_FILENAME_REGEX,"",$matches[2]).$matches[3]);'), $str_formatted);
        #-- add index page
        $archive->add($str_formatted, "Index_{$rootid}" . $html_ext, array("mtime" => $row["lastmodified"], "uname" => "ewiki", "mode" => 0664 | ($row["flags"] & EWIKI_DB_F_WRITEABLE ? 02 : 00)), $complevel);
        #-- Headers
        Header("Content-type: application/octet-stream");
        Header("Content-disposition: attachment; filename=\"{$archivename}\"");
        Header("Cache-control: private");
        Header("Original-Filename: {$archivename}");
        Header("X-Content-Type: application/octet-stream");
        Header("Content-Location: {$archivename}");
        #-- end output
        echo $archive->close();
    }
    #-- fin
    die;
}