/** * Serves the resource files. * @param object $course * @param object $cminfo * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ function resource_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; require_once "{$CFG->libdir}/resourcelib.php"; if (!$cminfo->uservisible) { return false; } if ($filearea !== 'resource_content') { // intro is handled automatically in pluginfile.php return false; } if (!($cm = get_coursemodule_from_instance('resource', $cminfo->instance, $course->id))) { return false; } require_course_login($course, true, $cm); array_shift($args); // ignore revision - designed to prevent caching problems only $fs = get_file_storage(); $relativepath = '/' . implode('/', $args); $fullpath = $context->id . $filearea . '0' . $relativepath; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { $resource = $DB->get_record('resource', array('id' => $cminfo->instance), 'id, legacyfiles', MUST_EXIST); if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) { return false; } require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php"; if (!($file = resource_try_file_migration($relativepath, $cminfo->id, $cminfo->course, 'resource_content', 0))) { return false; } // file migrate - update flag $resource->legacyfileslast = time(); $DB->update_record('resource', $resource); } // should we apply filters? $mimetype = $file->get_mimetype(); if ($mimetype = 'text/html' or $mimetype = 'text/plain') { $filter = $DB->get_field('resource', 'filterfiles', array('id' => $cminfo->instance)); } else { $filter = 0; } // finally send the file send_stored_file($file, 86400, $filter, $forcedownload); }
/** * Migrate resource module data from 1.9 resource_old table to new resource table * @return void */ function resource_20_migrate() { global $CFG, $DB; require_once "{$CFG->libdir}/filelib.php"; require_once "{$CFG->libdir}/resourcelib.php"; require_once "{$CFG->dirroot}/course/lib.php"; $fs = get_file_storage(); $withrelativelinks = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/x-shockwave-flash'); // note: pdf doc and other types may contain links too, but we do not support relative links there if (!($candidates = $DB->get_recordset('resource_old', array('type' => 'file', 'migrated' => 0)))) { return; } foreach ($candidates as $candidate) { upgrade_set_timeout(); $path = $candidate->reference; $siteid = get_site()->id; $fs = get_file_storage(); if (strpos($path, 'LOCALPATH') === 0) { // ignore not maintained local files - sorry continue; } else { if (preg_match("|{$CFG->wwwroot}/file.php(\\?file=)?/{$siteid}(/[^\\s'\"&\\?#]+)|", $path, $matches)) { // public site files $path = $matches[2]; $resource = new object(); $resource->id = $candidate->oldid; $resource->tobemigrated = 0; $resource->mainfile = $path; $resource->filterfiles = $CFG->filteruploadedfiles; $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO; // on-demand-migration not possible for site files, sorry $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid); $file_record = array('contextid' => $context->id, 'filearea' => 'resource_content', 'itemid' => 0); if ($file = $fs->get_file_by_hash(sha1($sitecontext->id . 'course_content0' . $path))) { try { $fs->create_file_from_storedfile($file_record, $file); } catch (Exception $x) { } $resource->mainfile = $file->get_filepath() . $file->get_filename(); } } else { if (preg_match("|{$CFG->wwwroot}/file.php(\\?file=)?/{$candidate->course}(/[^\\s'\"&\\?#]+)|", $path, $matches)) { // current course files $path = $matches[2]; $resource = new object(); $resource->id = $candidate->oldid; $resource->tobemigrated = 0; $resource->mainfile = $path; $resource->filterfiles = $CFG->filteruploadedfiles; $mimetype = mimeinfo('type', $resource->mainfile); if (in_array($mimetype, $withrelativelinks)) { $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE; } else { $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO; } // try migration of main file - ignore if does not exist if ($file = resource_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'resource_content', 0)) { $resource->mainfile = $file->get_filepath() . $file->get_filename(); } } else { if (strpos($path, '://') or strpos($path, '/') === 0) { // http:// https:// ftp:// OR starts with slash - to be converted to link resource continue; } else { // current course files // cleanup old path first $path = '/' . trim(trim($path), '/'); if (strpos($path, '?forcedownload=1') !== false) { // eliminate old force download tricks $candidate->options = 'forcedownload'; $path = str_replace('?forcedownload=1', '', $path); } // get rid of any extra url parameters, sorry we can not support these preg_match("/^[^?#]+/", $path, $matches); $parts = $matches[0]; $resource = new object(); $resource->id = $candidate->oldid; $resource->tobemigrated = 0; $resource->mainfile = $path; $resource->filterfiles = $CFG->filteruploadedfiles; $mimetype = mimeinfo('type', $resource->mainfile); if (in_array($mimetype, $withrelativelinks)) { $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE; } else { $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO; } // try migration of main file - ignore if does not exist if ($file = resource_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'resource_content', 0)) { $resource->mainfile = $file->get_filepath() . $file->get_filename(); } } } } } $options = array('printheading' => 0, 'printintro' => 1); if ($candidate->options == 'frame') { $resource->display = RESOURCELIB_DISPLAY_FRAME; } else { if ($candidate->options == 'objectframe') { $resource->display = RESOURCELIB_DISPLAY_EMBED; } else { if ($candidate->options == 'forcedownload') { $resource->display = RESOURCELIB_DISPLAY_DOWNLOAD; } else { if ($candidate->popup) { $resource->display = RESOURCELIB_DISPLAY_POPUP; if ($candidate->popup) { $rawoptions = explode(',', $candidate->popup); foreach ($rawoptions as $rawoption) { list($name, $value) = explode('=', trim($rawoption), 2); if ($value > 0 and ($name == 'width' or $name == 'height')) { $options['popup' . $name] = $value; continue; } } } } else { $resource->display = RESOURCELIB_DISPLAY_AUTO; } } } } $resource->displayoptions = serialize($options); // update resource instance and mark as migrated $DB->update_record('resource', $resource); $candidate->newmodule = 'resource'; $candidate->newid = $candidate->oldid; $candidate->migrated = time(); $DB->update_record('resource_old', $candidate); } $candidates->close(); // clear all course modinfo caches rebuild_course_cache(0, true); }