Quantcast
Channel: General developer forum
Viewing all articles
Browse latest Browse all 37679

Re: Simple proxy script workaround for Moodle's file API

$
0
0
by Matteo Scaramuccia.  

Hi Matt,
here is my proposal, based on both Moodle patterns and your logics:

require_once('../../config.php');
require_once('../../lib/filelib.php');

require_login();

global $CFG;

// (Optional) Clean the SWF content directory setting.
$CFG->swf_content_dir = clean_param($CFG->swf_content_dir, PARAM_PATH);
// Remove trailing slash(es).
$CFG->swf_content_dir = rtrim($CFG->swf_content_dir, '/');
// Get the relative path of the requested content.
$swf_relative_path = get_file_argument();
if (empty($CFG->swf_content_dir) || !$swf_relative_path) {
    print_error('invalidargorconf');
} else if ($swf_relative_path{0} != '/') {
// Relative path must start with '/'.
    print_error('pathdoesnotstartslash');
}

$swf_data_path = realpath($CFG->dataroot . $CFG->swf_content_dir . $swf_relative_path);
// (Paranoid) Content will be served just from the SWF content dir.
if (strpos($swf_data_path, realpath($CFG->dataroot . $CFG->swf_content_dir)) === 0) {
    $swf_data_info = pathinfo($swf_data_path);
    $swf_mime_type = mimeinfo('type', $swf_data_info['basename']);
    send_file($swf_data_path, $swf_data_info['basename'], 'default', 0, false, false, $swf_mime_type, false);
} else {
    send_file_not_found();
}

Regarding with MIME types, you can optionally change the Moodle ones using a switch to force some of them.

Besides, since you're developing a module keep care of providing $plugin->component (details here: http://docs.moodle.org/dev/version.php ) to let users receive an error in case they'll put your code in a different folder.

HTH,
Matteo


Viewing all articles
Browse latest Browse all 37679

Trending Articles