Skip to content

Commit

Permalink
MDL-14589 new file not found general function
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 16, 2008
1 parent 43c60e8 commit 9e5fa33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
9 changes: 2 additions & 7 deletions file.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@
$fullpath .= '/';
}
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
not_found();
send_file_not_found();
}
}
// do not serve dirs
if ($file->get_filename() == '.') {
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
not_found();
send_file_not_found();
}
}
}
Expand All @@ -96,9 +96,4 @@
session_write_close(); // unlock session during fileserving
send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);

function not_found() {
global $CFG, $COURSE;
header('HTTP/1.0 404 not found');
print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$COURSE->id); //this is not displayed on IIS??
}

10 changes: 10 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ function get_mimetype_description($mimetype,$capitalise=false) {
return $result;
}

/**
* Reprot file is not found or not accessible
* @return does not return, terminates script
*/
function send_file_not_found() {
global $CFG, $COURSE;
header('HTTP/1.0 404 not found');
print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$COURSE->id); //this is not displayed on IIS??
}

/**
* Handles the sending of temporary file to user, download is forced.
* File is deleted after abort or succesful sending.
Expand Down
39 changes: 16 additions & 23 deletions pluginfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
}
if ($CFG->bloglevel == BLOG_USER_LEVEL) {
if ($USER->id != $entry->userid) {
not_found();
send_file_not_found();
}
}
}
$entryid = (int)array_shift($args);
if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
not_found();
send_file_not_found();
}
if ('publishstate' === 'public') {
if ($CFG->forcelogin) {
Expand All @@ -62,7 +62,7 @@
} else if ('publishstate' === 'draft') {
require_login();
if ($USER->id != $entry->userid) {
not_found();
send_file_not_found();
}
}

Expand All @@ -72,23 +72,23 @@
$fullpath = $context->id.'blog'.$entryid.$relativepath;

if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
not_found();
send_file_not_found();
}

send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!

} else {
not_found();
send_file_not_found();
}


} else if ($context->contextlevel == CONTEXT_USER) {
not_found();
send_file_not_found();


} else if ($context->contextlevel == CONTEXT_COURSECAT) {
if ($filearea !== 'intro') {
not_found();
send_file_not_found();
}

if ($CFG->forcelogin) {
Expand All @@ -100,7 +100,7 @@
$fullpath = $context->id.'intro0'.$relativepath;

if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
not_found();
send_file_not_found();
}

session_write_close(); // unlock session during fileserving
Expand All @@ -109,7 +109,7 @@

} else if ($context->contextlevel == CONTEXT_COURSE) {
if ($filearea !== 'intro' and $filearea !== 'backup') {
not_found();
send_file_not_found();
}

if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
Expand All @@ -129,7 +129,7 @@
$fullpath = $context->id.'intro0'.$relativepath;

if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
not_found();
send_file_not_found();
}

session_write_close(); // unlock session during fileserving
Expand All @@ -139,15 +139,15 @@
} else if ($context->contextlevel == CONTEXT_MODULE) {

if (!$coursecontext = get_context_instance_by_id(get_parent_contextid($context))) {
not_found();
send_file_not_found();
}

if (!$course = $DB->get_record('course', array('id'=>$coursecontext->instanceid))) {
not_found();
send_file_not_found();
}
$modinfo = get_fast_modinfo($course);
if (empty($modinfo->cms[$context->instanceid])) {
not_found();
send_file_not_found();
}

$cminfo = $modinfo->cms[$context->instanceid];
Expand All @@ -162,20 +162,13 @@
}
}
}
not_found();
send_file_not_found();

} else if ($context->contextlevel == CONTEXT_BLOCK) {
//not supported yet
not_found();
send_file_not_found();


} else {
not_found();
}


function not_found() {
global $CFG;
header('HTTP/1.0 404 not found');
print_error('filenotfound', 'error', $CFG->wwwroot.'/'); //this is not displayed on IIS??
send_file_not_found();
}

0 comments on commit 9e5fa33

Please sign in to comment.