function moodle_binary_store_get_file($id, &$meta)
{
    # READ-Only
    global $CFG, $cm, $course, $wiki, $groupid, $userid;
    #-- check for file
    if (!$wiki->ewikiacceptbinary) {
        print_error('cannotacceptbin', 'wiki');
        return 0;
    }
    $filepath = moodle_binary_get_path($id, $meta, $course, $wiki, $userid, $groupid);
    if (file_exists($filepath)) {
        readfile($filepath);
        return true;
    } else {
        return false;
    }
    //$dbfname = EWIKI_DB_STORE_DIRECTORY."/".rawurlencode($id);
    //if (file_exists($dbfname)) {
    //   readfile($dbfname);
    //   return(true);
    //}
    //else {
    //   return(false);
    //}
}
Exemplo n.º 2
0
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 />";
        }
    }
}
Exemplo n.º 3
0
function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid)
{
    $ret = "";
    foreach ($pagestodelete as $id) {
        $id = rawurldecode($id);
        $data = ewiki_database("GET", array("id" => $id));
        for ($version = 1; $version <= $data["version"]; $version++) {
            ewiki_database("DELETE", array("id" => $id, "version" => $version));
            if ($data["flags"] & EWIKI_DB_F_BINARY) {
                $filepath = moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
                @unlink("{$filepath}");
            }
        }
    }
    return $ret;
}