/** * Serves the resource files. * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; require_once("$CFG->libdir/resourcelib.php"); if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_course_login($course, true, $cm); if (!has_capability('mod/resource:view', $context)) { return false; } if ($filearea !== 'content') { // intro is handled automatically in pluginfile.php return false; } array_shift($args); // ignore revision - designed to prevent caching problems only $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/'); do { if (!$file = $fs->get_file_by_hash(sha1($fullpath))) { if ($fs->get_file_by_hash(sha1("$fullpath/."))) { if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) { break; } if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) { break; } if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) { break; } } $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST); if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) { return false; } if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) { return false; } // file migrate - update flag $resource->legacyfileslast = time(); $DB->update_record('resource', $resource); } } while (false); // should we apply filters? $mimetype = $file->get_mimetype(); if ($mimetype === 'text/html' or $mimetype === 'text/plain') { $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance)); $CFG->embeddedsoforcelinktarget = true; } 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 $candidates = $DB->get_recordset('resource_old', array('type' => 'file', 'migrated' => 0)); if (!$candidates->valid()) { $candidates->close(); // Not going to iterate (but exit), close rs return; } foreach ($candidates as $candidate) { upgrade_set_timeout(); $path = $candidate->reference; $siteid = get_site()->id; $fs = get_file_storage(); if (empty($candidate->cmid)) { // skip borked records continue; } else { 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 stdClass(); $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, 'component' => 'mod_resourse', 'filearea' => 'content', 'itemid' => 0); if ($file = $fs->get_file_by_hash(sha1("/{$sitecontext->id}/course/legacy/content/0" . $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 stdClass(); $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 = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_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 stdClass(); $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 = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_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); }
/** * Serves the page files. * * @package mod_page * @category files * @param stdClass $course course object * @param stdClass $cm course module object * @param stdClass $context context object * @param string $filearea file area * @param array $args extra arguments * @param bool $forcedownload whether or not force download * @param array $options additional options affecting the file serving * @return bool false if file not found, does not return if found - just send the file */ function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) { global $CFG, $DB; require_once("$CFG->libdir/resourcelib.php"); if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_course_login($course, true, $cm); if (!has_capability('mod/page:view', $context)) { return false; } if ($filearea !== 'content') { // intro is handled automatically in pluginfile.php return false; } // $arg could be revision number or index.html $arg = array_shift($args); if ($arg == 'index.html' || $arg == 'index.htm') { // serve page content $filename = $arg; if (!$page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST)) { return false; } // remove @@PLUGINFILE@@/ $content = str_replace('@@PLUGINFILE@@/', '', $page->content); $formatoptions = new stdClass; $formatoptions->noclean = true; $formatoptions->overflowdiv = true; $formatoptions->context = $context; $content = format_text($content, $page->contentformat, $formatoptions); send_file($content, $filename, 0, 0, true, true); } else { $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST); if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) { return false; } if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) { return false; } //file migrate - update flag $page->legacyfileslast = time(); $DB->update_record('page', $page); } // finally send the file send_stored_file($file, 86400, 0, $forcedownload, $options); } }
/** * Serves the page files. * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $CFG, $DB; require_once("$CFG->libdir/resourcelib.php"); if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_course_login($course, true, $cm); if (!has_capability('mod/page:view', $context)) { return false; } if ($filearea !== 'content') { // intro is handled automatically in pluginfile.php return false; } array_shift($args); // ignore revision - designed to prevent caching problems only $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath"; if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST); if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) { return false; } if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) { return false; } //file migrate - update flag $page->legacyfileslast = time(); $DB->update_record('page', $page); } // finally send the file send_stored_file($file, 86400, 0, $forcedownload); }