]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 improved file handling operations & towards zipping support
authorskodak <skodak>
Sat, 2 Aug 2008 12:45:02 +0000 (12:45 +0000)
committerskodak <skodak>
Sat, 2 Aug 2008 12:45:02 +0000 (12:45 +0000)
files/index.php
lib/file/file_storage.php
lib/file/stored_file.php
lib/filelib.php
lib/formslib.php
lib/moodlelib.php

index b76d57ad58cfc0a62134c51ad69502e37c6f48a4..2573fe65cdc1d8a6dd103b2149fc1b8349274b24 100644 (file)
@@ -7,7 +7,7 @@
     $courseid   = optional_param('id', 0, PARAM_INT);
 
     $contextid  = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
-    $filearea   = optional_param('filearea', '', PARAM_SAFEDIR);
+    $filearea   = optional_param('filearea', '', PARAM_ALPHAEXT);
     $itemid     = optional_param('itemid', -1, PARAM_INT);
     $filepath   = optional_param('filepath', '', PARAM_PATH);
     $filename   = optional_param('filename', '', PARAM_FILE);
index f4e95abca7741675e5c0f860dd834224007c28b2..e1b293127fc065c6231d6d5768eb2290b94abe12 100644 (file)
@@ -23,10 +23,8 @@ class file_storage {
                 throw new file_exception('localfilecannotcreatefiledirs'); // permission trouble
             }
             // place warning file in file pool root
-            $fp = fopen($this->filedir.'/warning.txt', 'w');
-            fwrite($fp, 'This directory contains the content of uploaded files and is controlled by Moodle code. Do not manually move, change or rename any of the files and subdirectories here.');
-            fclose($fp);
-            unset($fp);
+            file_put_contents($this->filedir.'/warning.txt', 
+                              'This directory contains the content of uploaded files and is controlled by Moodle code. Do not manually move, change or rename any of the files and subdirectories here.');
         }
     }
 
@@ -199,7 +197,7 @@ class file_storage {
             throw new file_exception('localfileproblem', 'Invalid contextid');
         }
 
-        $filearea = clean_param($filearea, PARAM_SAFEDIR);
+        $filearea = clean_param($filearea, PARAM_ALPHAEXT);
         if ($filearea === '') {
             throw new file_exception('localfileproblem', 'Invalid filearea');
         }
@@ -289,7 +287,7 @@ class file_storage {
             }
 
             if ($key == 'filearea') {
-                $value = clean_param($value, PARAM_SAFEDIR);
+                $value = clean_param($value, PARAM_ALPHAEXT);
                 if ($value === '') {
                     throw new file_exception('localfileproblem', 'Invalid filearea');
                 }
@@ -353,7 +351,7 @@ class file_storage {
             throw new file_exception('localfileproblem', 'Invalid contextid');
         }
 
-        $file_record->filearea = clean_param($file_record->filearea, PARAM_SAFEDIR);
+        $file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT);
         if ($file_record->filearea === '') {
             throw new file_exception('localfileproblem', 'Invalid filearea');
         }
@@ -428,7 +426,7 @@ class file_storage {
             throw new file_exception('localfileproblem', 'Invalid contextid');
         }
 
-        $file_record->filearea = clean_param($file_record->filearea, PARAM_SAFEDIR);
+        $file_record->filearea = clean_param($file_record->filearea, PARAM_ALPHAEXT);
         if ($file_record->filearea === '') {
             throw new file_exception('localfileproblem', 'Invalid filearea');
         }
@@ -519,17 +517,9 @@ class file_storage {
             }
             $newfile = true;
 
-            $fs = fopen($pathname, 'rb');
-            $fp = fopen($hashfile, 'wb');
-            while(!feof($fs)) {
-                $buf = fread($fs, 65536);
-                if ($buf === false) {
-                    throw new file_exception('localfilecannotread');
-                }
-                fwrite($fp, $buf);
+            if (!copy($pathname, $hashfile)) {
+                throw new file_exception('localfilecannotread');
             }
-            fclose($fp);
-            fclose($fs);
 
             if (filesize($hashfile) !== $filesize) {
                 @unlink($hashfile);
@@ -566,10 +556,7 @@ class file_storage {
             }
             $newfile = true;
 
-            $fp = fopen($hashfile, 'wb');
-
-            fwrite($fp, $content);
-            fclose($fp);
+            file_put_contents($hashfile, $content);
 
             if (filesize($hashfile) !== $filesize) {
                 @unlink($hashfile);
index 375809cc74a76b0fa246b5b062ff777fef571509..c8371df55e09ea34ded4c8f250a52b6453c43d76 100644 (file)
@@ -36,7 +36,7 @@ class stored_file {
     }
 
     /**
-     * Protected - devs must not gain direct access to this function
+     * Protected - developers must not gain direct access to this function
      **/
     protected function get_content_file_location() {
         // NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
@@ -80,6 +80,19 @@ class stored_file {
         return file_get_contents($this->get_content_file_location());
     }
 
+    /**
+     * Copy content of file to give npathname
+     * @param string $pathnema rela path to new file 
+     * @return bool success
+     */
+    public function copy_content_to($pathname) {
+        $path = $this->get_content_file_location();
+        if (!is_readable($path)) {
+            throw new file_exception('localfilecannotread');
+        }
+        return copy($path, $pathname);
+    }
+
     public function get_contextid() {
         return $this->file_record->contextid;
     }
@@ -119,4 +132,8 @@ class stored_file {
     public function get_timemodified() {
         return $this->file_record->timemodified;
     }
+
+    public function get_status() {
+        return $this->file_record->status;
+    }
 }
index 8d64540ee477eff7d576d746585138d520ac9615..0600604beb34a8d2cae5783e779e5f2903c7dd24 100644 (file)
@@ -5,6 +5,7 @@ define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
 require_once("$CFG->libdir/file/file_exceptions.php");
 require_once("$CFG->libdir/file/file_storage.php");
 require_once("$CFG->libdir/file/file_browser.php");
+require_once("$CFG->libdir/file/file_packer.php");
 
 function get_file_url($path, $options=null, $type='coursefile') {
     global $CFG;
index 6d5c8ecf2208d10a97326ac8e36ff7b9724bcf34..84523254e997b7a9006a331079bcf6a1cd923670 100644 (file)
@@ -530,20 +530,10 @@ class moodleform {
                 return false;
             }
         }
-        if (!$temp = @fopen($_FILES[$elname]['tmp_name'], "rb")) {
-            return false;
-        }
-        if (!$file = @fopen($pathname, "wb")) {
+        if (!copy($_FILES[$elname]['tmp_name'], $pathname)) {
             return false;
         }
 
-        while (!feof($temp)) {
-            $data = fread($temp, 65536);
-            fwrite($file, $data);
-        }
-        fclose($file);
-        fclose($temp);
-
         return true;
     }
 
@@ -604,17 +594,7 @@ class moodleform {
             return false;
         }
 
-        if (!$file = @fopen($_FILES[$elname]['tmp_name'], "rb")) {
-            return false;
-        }
-
-        $data = '';
-        while (!feof($file)) {
-            $data .= fread($file, 4048);
-        }
-        fclose($file);
-
-        return $data;
+        return file_get_contents($_FILES[$elname]['tmp_name']);
     }
 
     /**
index 3c8fe40f0cb5e94eba9b67aa4ac2b4653eca9d19..ed8664bb94f056782162f6fdd1e62eddb49955b2 100644 (file)
@@ -4504,6 +4504,26 @@ function get_file_browser() {
     return $fb;
 }
 
+/**
+ * Returns local file storage instance
+ * @return object file_storage
+ */
+function get_file_packer() {
+    global $CFG;
+
+    static $fp = null;
+
+    if ($fp) {
+        return $fp;
+    }
+
+    require_once("$CFG->libdir/filelib.php");
+
+    $fp = new file_packer();
+
+    return $fp;
+}
+
 /**
  * Makes an upload directory for a particular module.
  *