From 6c0e2d08a98f4fe91e435afc82ce78a8b974be28 Mon Sep 17 00:00:00 2001 From: skodak Date: Sat, 2 Aug 2008 12:45:02 +0000 Subject: [PATCH] MDL-14589 improved file handling operations & towards zipping support --- files/index.php | 2 +- lib/file/file_storage.php | 31 +++++++++---------------------- lib/file/stored_file.php | 19 ++++++++++++++++++- lib/filelib.php | 1 + lib/formslib.php | 24 ++---------------------- lib/moodlelib.php | 20 ++++++++++++++++++++ 6 files changed, 51 insertions(+), 46 deletions(-) diff --git a/files/index.php b/files/index.php index b76d57ad58..2573fe65cd 100644 --- a/files/index.php +++ b/files/index.php @@ -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); diff --git a/lib/file/file_storage.php b/lib/file/file_storage.php index f4e95abca7..e1b293127f 100644 --- a/lib/file/file_storage.php +++ b/lib/file/file_storage.php @@ -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); diff --git a/lib/file/stored_file.php b/lib/file/stored_file.php index 375809cc74..c8371df55e 100644 --- a/lib/file/stored_file.php +++ b/lib/file/stored_file.php @@ -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; + } } diff --git a/lib/filelib.php b/lib/filelib.php index 8d64540ee4..0600604beb 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -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; diff --git a/lib/formslib.php b/lib/formslib.php index 6d5c8ecf22..84523254e9 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -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']); } /** diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 3c8fe40f0c..ed8664bb94 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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. * -- 2.39.5