/** * Check if the given file is capable of being imported by this plugin. * As {@link file_storage::mimetype()} now uses finfo PHP extension if available, * the value returned by $file->get_mimetype for a .dat file is not the same on all servers. * So we must made 2 checks to verify if the plugin can import the file. * @param stored_file $file the file to check * @return bool whether this plugin can import the file */ public function can_import_file($file) { $mimetypes = array( mimeinfo('type', '.dat'), mimeinfo('type', '.zip') ); return in_array($file->get_mimetype(), $mimetypes) || in_array(mimeinfo('type', $file->get_filename()), $mimetypes); }
/** * Repository method to serve the referenced file * * @param stored_file $storedfile the file that contains the reference * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime) * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin * @param array $options additional options affecting the file serving */ public function send_file($storedfile, $lifetime = null, $filter = 0, $forcedownload = false, array $options = null) { $ref = unserialize(self::convert_to_valid_reference($storedfile->get_reference())); header('Location: ' . $ref->downloadurl); }
/** * Sets up SCORM 1.2/2004 packages using the manifest file. * Called whenever SCORM changes * @param object $scorm instance - fields are updated and changes saved into database * @param stored_file|string $manifest - path to manifest file or stored_file. * @return bool */ function scorm_parse_scorm(&$scorm, $manifest) { global $CFG, $DB; // load manifest into string if ($manifest instanceof stored_file) { $xmltext = $manifest->get_content(); } else { require_once "{$CFG->libdir}/filelib.php"; $xmltext = download_file_content($manifest); } $defaultorgid = 0; $firstinorg = 0; $pattern = '/&(?!\\w{2,6};)/'; $replacement = '&'; $xmltext = preg_replace($pattern, $replacement, $xmltext); $objXML = new xml2Array(); $manifests = $objXML->parse($xmltext); $scoes = new stdClass(); $scoes->version = ''; $scoes = scorm_get_manifest($manifests, $scoes); $newscoes = array(); $sortorder = 0; if (count($scoes->elements) > 0) { $olditems = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id)); foreach ($scoes->elements as $manifest => $organizations) { foreach ($organizations as $organization => $items) { foreach ($items as $identifier => $item) { $sortorder++; // This new db mngt will support all SCORM future extensions $newitem = new stdClass(); $newitem->scorm = $scorm->id; $newitem->manifest = $manifest; $newitem->organization = $organization; $newitem->sortorder = $sortorder; $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title'); foreach ($standarddatas as $standarddata) { if (isset($item->{$standarddata})) { $newitem->{$standarddata} = $item->{$standarddata}; } else { $newitem->{$standarddata} = ''; } } if (!empty($defaultorgid) && !empty($scoes->defaultorg) && empty($firstinorg) && $newitem->parent == $scoes->defaultorg) { $firstinorg = $sortorder; } if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) { $newitem->id = $olditemid; // Update the Sco sortorder but keep id so that user tracks are kept against the same ids. $DB->update_record('scorm_scoes', $newitem); $id = $olditemid; // Remove all old data so we don't duplicate it. $DB->delete_records('scorm_scoes_data', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_objective', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_mapinfo', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_ruleconds', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_rulecond', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_rolluprule', array('scoid' => $olditemid)); $DB->delete_records('scorm_seq_rolluprulecond', array('scoid' => $olditemid)); // Now remove this SCO from the olditems object as we have dealt with it. unset($olditems[$olditemid]); } else { // Insert the new SCO, and retain the link between the old and new for later adjustment $id = $DB->insert_record('scorm_scoes', $newitem); } $newscoes[$id] = $newitem; // Save this sco in memory so we can use it later. if ($optionaldatas = scorm_optionals_data($item, $standarddatas)) { $data = new stdClass(); $data->scoid = $id; foreach ($optionaldatas as $optionaldata) { if (isset($item->{$optionaldata})) { $data->name = $optionaldata; $data->value = $item->{$optionaldata}; $dataid = $DB->insert_record('scorm_scoes_data', $data); } } } if (isset($item->sequencingrules)) { foreach ($item->sequencingrules as $sequencingrule) { $rule = new stdClass(); $rule->scoid = $id; $rule->ruletype = $sequencingrule->type; $rule->conditioncombination = $sequencingrule->conditioncombination; $rule->action = $sequencingrule->action; $ruleid = $DB->insert_record('scorm_seq_ruleconds', $rule); if (isset($sequencingrule->ruleconditions)) { foreach ($sequencingrule->ruleconditions as $rulecondition) { $rulecond = new stdClass(); $rulecond->scoid = $id; $rulecond->ruleconditionsid = $ruleid; $rulecond->referencedobjective = $rulecondition->referencedobjective; $rulecond->measurethreshold = $rulecondition->measurethreshold; $rulecond->operator = $rulecondition->operator; $rulecond->cond = $rulecondition->cond; $rulecondid = $DB->insert_record('scorm_seq_rulecond', $rulecond); } } } } if (isset($item->rolluprules)) { foreach ($item->rolluprules as $rolluprule) { $rollup = new stdClass(); $rollup->scoid = $id; $rollup->childactivityset = $rolluprule->childactivityset; $rollup->minimumcount = $rolluprule->minimumcount; $rollup->minimumpercent = $rolluprule->minimumpercent; $rollup->rollupruleaction = $rolluprule->rollupruleaction; $rollup->conditioncombination = $rolluprule->conditioncombination; $rollupruleid = $DB->insert_record('scorm_seq_rolluprule', $rollup); if (isset($rollup->conditions)) { foreach ($rollup->conditions as $condition) { $cond = new stdClass(); $cond->scoid = $rollup->scoid; $cond->rollupruleid = $rollupruleid; $cond->operator = $condition->operator; $cond->cond = $condition->cond; $conditionid = $DB->insert_record('scorm_seq_rolluprulecond', $cond); } } } } if (isset($item->objectives)) { foreach ($item->objectives as $objective) { $obj = new stdClass(); $obj->scoid = $id; $obj->primaryobj = $objective->primaryobj; $obj->satisfiedbumeasure = $objective->satisfiedbymeasure; $obj->objectiveid = $objective->objectiveid; $obj->minnormalizedmeasure = trim($objective->minnormalizedmeasure); $objectiveid = $DB->insert_record('scorm_seq_objective', $obj); if (isset($objective->mapinfos)) { foreach ($objective->mapinfos as $objmapinfo) { $mapinfo = new stdClass(); $mapinfo->scoid = $id; $mapinfo->objectiveid = $objectiveid; $mapinfo->targetobjectiveid = $objmapinfo->targetobjectiveid; $mapinfo->readsatisfiedstatus = $objmapinfo->readsatisfiedstatus; $mapinfo->writesatisfiedstatus = $objmapinfo->writesatisfiedstatus; $mapinfo->readnormalizedmeasure = $objmapinfo->readnormalizedmeasure; $mapinfo->writenormalizedmeasure = $objmapinfo->writenormalizedmeasure; $mapinfoid = $DB->insert_record('scorm_seq_mapinfo', $mapinfo); } } } } if (empty($defaultorgid) && (empty($scoes->defaultorg) || $scoes->defaultorg == $identifier)) { $defaultorgid = $id; } } } } if (!empty($olditems)) { foreach ($olditems as $olditem) { $DB->delete_records('scorm_scoes', array('id' => $olditem->id)); $DB->delete_records('scorm_scoes_data', array('scoid' => $olditem->id)); $DB->delete_records('scorm_scoes_track', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_objective', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_mapinfo', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_ruleconds', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_rulecond', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_rolluprule', array('scoid' => $olditem->id)); $DB->delete_records('scorm_seq_rolluprulecond', array('scoid' => $olditem->id)); } } if (empty($scoes->version)) { $scoes->version = 'SCORM_1.2'; } $DB->set_field('scorm', 'version', $scoes->version, array('id' => $scorm->id)); $scorm->version = $scoes->version; } $scorm->launch = 0; // Check launch sco is valid. if (!empty($defaultorgid) && isset($newscoes[$defaultorgid]) && !empty($newscoes[$defaultorgid]->launch)) { // Launch param is valid - do nothing. $scorm->launch = $defaultorgid; } else { if (!empty($defaultorgid) && isset($newscoes[$defaultorgid]) && empty($newscoes[$defaultorgid]->launch)) { // The launch is probably the default org so we need to find the first launchable item inside this org. $sqlselect = 'scorm = ? AND sortorder >= ? AND ' . $DB->sql_isnotempty('scorm_scoes', 'launch', false, true); // We use get_records here as we need to pass a limit in the query that works cross db. $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id, $firstinorg), 'sortorder', 'id', 0, 1); if (!empty($scoes)) { $sco = reset($scoes); // We only care about the first record - the above query only returns one. $scorm->launch = $sco->id; } } } if (empty($scorm->launch)) { // No valid Launch is specified - find the first launchable sco instead. $sqlselect = 'scorm = ? AND ' . $DB->sql_isnotempty('scorm_scoes', 'launch', false, true); // We use get_records here as we need to pass a limit in the query that works cross db. $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1); if (!empty($scoes)) { $sco = reset($scoes); // We only care about the first record - the above query only returns one. $scorm->launch = $sco->id; } } return true; }
/** * Performs synchronisation of an external file if the previous one has expired. * * This function must be implemented for external repositories supporting * FILE_REFERENCE, it is called for existing aliases when their filesize, * contenthash or timemodified are requested. It is not called for internal * repositories (see {@link repository::has_moodle_files()}), references to * internal files are updated immediately when source is modified. * * Referenced files may optionally keep their content in Moodle filepool (for * thumbnail generation or to be able to serve cached copy). In this * case both contenthash and filesize need to be synchronized. Otherwise repositories * should use contenthash of empty file and correct filesize in bytes. * * Note that this function may be run for EACH file that needs to be synchronised at the * moment. If anything is being downloaded or requested from external sources there * should be a small timeout. The synchronisation is performed to update the size of * the file and/or to update image and re-generated image preview. There is nothing * fatal if syncronisation fails but it is fatal if syncronisation takes too long * and hangs the script generating a page. * * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or * $file->get_timemodified() make sure that recursion does not happen. * * Called from {@link stored_file::sync_external_file()} * * @uses stored_file::set_missingsource() * @uses stored_file::set_synchronized() * @param stored_file $file * @return bool false when file does not need synchronisation, true if it was synchronised */ public function sync_reference(stored_file $file) { if ($file->get_repository_id() != $this->id) { // This should not really happen because the function can be called from stored_file only. return false; } if ($this->has_moodle_files()) { // References to local files need to be synchronised only once. // Later they will be synchronised automatically when the source is changed. if ($file->get_referencelastsync()) { return false; } $fs = get_file_storage(); $params = file_storage::unpack_reference($file->get_reference(), true); if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']))) { $file->set_missingsource(); } else { $file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize(), 0, $storedfile->get_timemodified()); } return true; } return false; }
/** * Retreive the contents of a file. That file may either be in a conventional directory of the Moodle file storage * @param file_storage $filestorage. should be null if using a conventional directory * @param stored_file $fileobj the directory to look in. null if using a conventional directory * @param string $dir the directory to look in. null if using the Moodle file storage * @param string $filename the name of the file we want * @return string the contents of the file */ public function data_preset_get_file_contents(&$filestorage, &$fileobj, $dir, $filename) { if(empty($filestorage) || empty($fileobj)) { if (substr($dir, -1)!='/') { $dir .= '/'; } return file_get_contents($dir.$filename); } else { $file = $filestorage->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, $fileobj->get_filepath(), $filename); return $file->get_content(); } }
/** * Restore the original source field from draft files * * Do not use this function because it makes field files.source inconsistent * for draft area files. This function will be deprecated in 2.6 * * @param stored_file $storedfile This only works with draft files * @return stored_file */ function file_restore_source_field_from_draft_file($storedfile) { $source = @unserialize($storedfile->get_source()); if (!empty($source)) { if (is_object($source)) { $restoredsource = $source->source; $storedfile->set_source($restoredsource); } else { throw new moodle_exception('invalidsourcefield', 'error'); } } return $storedfile; }
/** * Get the URL of a file that belongs to a response variable of this * question_attempt. * @param stored_file $file the file to link to. * @return string the URL of that file. */ public function get_response_file_url(stored_file $file) { return file_encode_url(new moodle_url('/pluginfile.php'), '/' . implode('/', array($file->get_contextid(), $file->get_component(), $file->get_filearea(), $this->usageid, $this->slot, $file->get_itemid())) . $file->get_filepath() . $file->get_filename(), true); }
public static function get_pretected_pathname(stored_file $file) { return $file->get_pathname_by_contenthash(); }
/** * Replaces the fields that might have changed when file was overriden in filepicker: * reference, contenthash, filesize, userid * * Note that field 'source' must be updated separately because * it has different format for draft and non-draft areas and * this function will usually be used to replace non-draft area * file with draft area file. * * @param stored_file $newfile * @throws coding_exception */ public function replace_file_with(stored_file $newfile) { if ($newfile->get_referencefileid() && $this->fs->get_references_count_by_storedfile($this)) { // The new file is a reference. // The current file has other local files referencing to it. // Double reference is not allowed. throw new moodle_exception('errordoublereference', 'repository'); } $filerecord = new stdClass(); $contenthash = $newfile->get_contenthash(); if ($this->fs->content_exists($contenthash)) { $filerecord->contenthash = $contenthash; } else { throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash); } $filerecord->filesize = $newfile->get_filesize(); $filerecord->referencefileid = $newfile->get_referencefileid(); $filerecord->userid = $newfile->get_userid(); $this->update($filerecord); }
/** * Overwrite an existing file in a draft area. * * @param stored_file $newfile the new file with the new content and meta-data * @param stored_file $existingfile the file that will be overwritten * @throws moodle_exception * @since Moodle 3.2 */ function file_overwrite_existing_draftfile(stored_file $newfile, stored_file $existingfile) { if ($existingfile->get_component() != 'user' or $existingfile->get_filearea() != 'draft') { throw new coding_exception('The file to overwrite is not in a draft area.'); } $fs = get_file_storage(); // Remember original file source field. $source = @unserialize($existingfile->get_source()); // Remember the original sortorder. $sortorder = $existingfile->get_sortorder(); if ($newfile->is_external_file()) { // New file is a reference. Check that existing file does not have any other files referencing to it if (isset($source->original) && $fs->search_references_count($source->original)) { throw new moodle_exception('errordoublereference', 'repository'); } } // Delete existing file to release filename. $newfilerecord = array('contextid' => $existingfile->get_contextid(), 'component' => 'user', 'filearea' => 'draft', 'itemid' => $existingfile->get_itemid(), 'timemodified' => time()); $existingfile->delete(); // Create new file. $newfile = $fs->create_file_from_storedfile($newfilerecord, $newfile); // Preserve original file location (stored in source field) for handling references. if (isset($source->original)) { if (!($newfilesource = @unserialize($newfile->get_source()))) { $newfilesource = new stdClass(); } $newfilesource->original = $source->original; $newfile->set_source(serialize($newfilesource)); } $newfile->set_sortorder($sortorder); }
/** * Copies a file from somewhere else in moodle * to the portfolio temporary working directory * associated with this export * * @param stored_file $oldfile existing stored file object * @return stored_file|bool new file object */ public function copy_existing_file($oldfile) { if (array_key_exists($oldfile->get_contenthash(), $this->newfilehashes)) { return $this->newfilehashes[$oldfile->get_contenthash()]; } $fs = get_file_storage(); $file_record = $this->new_file_record_base($oldfile->get_filename()); if ($dir = $this->get('format')->get_file_directory()) { $file_record->filepath = '/' . $dir . '/'; } try { $newfile = $fs->create_file_from_storedfile($file_record, $oldfile->get_id()); $this->newfilehashes[$newfile->get_contenthash()] = $newfile; return $newfile; } catch (file_exception $e) { return false; } }
/** * Export the data for the given file in relation to this document. * * @param \stored_file $file The stored file we are talking about. * @return array */ public function export_file_for_engine($file) { $data = $this->export_for_engine(); // Content is index in the main document. unset($data['content']); unset($data['description1']); unset($data['description2']); // Going to append the fileid to give it a unique id. $data['id'] = $data['id'] . '-solrfile' . $file->get_id(); $data['type'] = \core_search\manager::TYPE_FILE; $data['solr_fileid'] = $file->get_id(); $data['solr_filecontenthash'] = $file->get_contenthash(); $data['solr_fileindexstatus'] = self::INDEXED_FILE_TRUE; $data['title'] = $file->get_filename(); $data['modified'] = self::format_time_for_engine($file->get_timemodified()); return $data; }
/** * Repository method to serve the referenced file * * @param stored_file $storedfile the file that contains the reference * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours) * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin * @param array $options additional options affecting the file serving */ public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) { $ref = $storedfile->get_reference(); // Let box.net serve the file. It will return 'no such file' content if file not found // also if file has the different name than alias, it will be returned with the box.net filename header('Location: ' . $ref); }
/** * Returns the number of aliases that link to the given stored_file * * Aliases in user draft areas are not counted. * * @param stored_file $storedfile * @return int */ public function get_references_count_by_storedfile(stored_file $storedfile) { global $DB; $params = array(); $params['contextid'] = $storedfile->get_contextid(); $params['component'] = $storedfile->get_component(); $params['filearea'] = $storedfile->get_filearea(); $params['itemid'] = $storedfile->get_itemid(); $params['filename'] = $storedfile->get_filename(); $params['filepath'] = $storedfile->get_filepath(); return $this->search_references_count(self::pack_reference($params)); }
/** * Repository method to serve the referenced file * * @see send_stored_file * * @param stored_file $storedfile the file that contains the reference * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours) * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin * @param array $options additional options affecting the file serving */ public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) { $reference = $storedfile->get_reference(); if ($reference{0} == '/') { $file = $this->root_path.substr($reference, 1, strlen($reference)-1); } else { $file = $this->root_path.$reference; } if (is_readable($file)) { $filename = $storedfile->get_filename(); if ($options && isset($options['filename'])) { $filename = $options['filename']; } $dontdie = ($options && isset($options['dontdie'])); send_file($file, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie); } else { send_file_not_found(); } }
/** * Check if the given file is capable of being imported by this plugin. * * Note that expensive or detailed integrity checks on the file should * not be performed by this method. Simple file type or magic-number tests * would be suitable. * * @param stored_file $file the file to check * @return bool whether this plugin can import the file */ public function can_import_file($file) { return $file->get_mimetype() == $this->mime_type(); }
/** * Convert file alias to local file * * @throws moodle_exception if file could not be downloaded * * @param stored_file $storedfile a stored_file instances * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit) * @return stored_file stored_file */ public function import_external_file(stored_file $storedfile, $maxbytes = 0) { global $CFG; $storedfile->import_external_file_contents($maxbytes); $storedfile->delete_reference(); return $storedfile; }
/** * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image * @param stored_file $file the image file to process * @param int $maxwidth the maximum width allowed for the image * @param int $maxheight the maximum height allowed for the image * @return string HTML fragment to add to the label */ function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) { global $CFG; $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); $link = null; $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl); if ($imginfo = $file->get_imageinfo()) { // Work out the new width / height, bounded by maxwidth / maxheight $width = $imginfo['width']; $height = $imginfo['height']; if (!empty($maxwidth) && $width > $maxwidth) { $height *= (double) $maxwidth / $width; $width = $maxwidth; } if (!empty($maxheight) && $height > $maxheight) { $width *= (double) $maxheight / $height; $height = $maxheight; } $attrib['width'] = $width; $attrib['height'] = $height; // If the size has changed and the image is of a suitable mime type, generate a smaller version if ($width != $imginfo['width']) { $mimetype = $file->get_mimetype(); if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') { require_once $CFG->libdir . '/gdlib.php'; $tmproot = make_temp_directory('mod_label'); $tmpfilepath = $tmproot . '/' . $file->get_contenthash(); $file->copy_content_to($tmpfilepath); $data = generate_image_thumbnail($tmpfilepath, $width, $height); unlink($tmpfilepath); if (!empty($data)) { $fs = get_file_storage(); $record = array('contextid' => $file->get_contextid(), 'component' => $file->get_component(), 'filearea' => $file->get_filearea(), 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => 's_' . $file->get_filename()); $smallfile = $fs->create_file_from_string($record, $data); // Replace the image 'src' with the resized file and link to the original $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(), $smallfile->get_filename()); $link = $fullurl; } } } } else { // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG) $attrib['width'] = $maxwidth; } $img = html_writer::empty_tag('img', $attrib); if ($link) { return html_writer::link($link, $img); } else { return $img; } }
/** * Performs synchronisation of reference to an external file if the previous one has expired. * * @param stored_file $file * @param bool $resetsynchistory whether to reset all history of sync (used by phpunit) * @return bool success */ public static function sync_external_file($file, $resetsynchistory = false) { global $DB; // TODO MDL-25290 static should be replaced with MUC code. static $synchronized = array(); if ($resetsynchistory) { $synchronized = array(); } $fs = get_file_storage(); if (!$file || !$file->get_referencefileid()) { return false; } if (array_key_exists($file->get_id(), $synchronized)) { return $synchronized[$file->get_id()]; } // remember that we already cached in current request to prevent from querying again $synchronized[$file->get_id()] = false; if (!($reference = $DB->get_record('files_reference', array('id' => $file->get_referencefileid())))) { return false; } if (!empty($reference->lastsync) and $reference->lastsync + $reference->lifetime > time()) { $synchronized[$file->get_id()] = true; return true; } if (!($repository = self::get_repository_by_id($reference->repositoryid, SYSCONTEXTID))) { return false; } if (!$repository->sync_individual_file($file)) { return false; } $lifetime = $repository->get_reference_file_lifetime($reference); $fileinfo = $repository->get_file_by_reference($reference); if ($fileinfo === null) { // does not exist any more - set status to missing $file->set_missingsource($lifetime); $synchronized[$file->get_id()] = true; return true; } $contenthash = null; $filesize = null; if (!empty($fileinfo->filesize)) { // filesize returned if (!empty($fileinfo->contenthash) && $fs->content_exists($fileinfo->contenthash)) { // contenthash is specified and valid $contenthash = $fileinfo->contenthash; } else { if ($fileinfo->filesize == $file->get_filesize()) { // we don't know the new contenthash but the filesize did not change, // assume the contenthash did not change either $contenthash = $file->get_contenthash(); } else { // we can't save empty contenthash so generate contenthash from empty string $fs->add_string_to_pool(''); $contenthash = sha1(''); } } $filesize = $fileinfo->filesize; } else { if (!empty($fileinfo->filepath)) { // File path returned list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo->filepath); } else { if (!empty($fileinfo->handle) && is_resource($fileinfo->handle)) { // File handle returned $contents = ''; while (!feof($fileinfo->handle)) { $contents .= fread($handle, 8192); } fclose($fileinfo->handle); list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($content); } else { if (isset($fileinfo->content)) { // File content returned list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($fileinfo->content); } } } } if (!isset($contenthash) or !isset($filesize)) { return false; } // update files table $file->set_synchronized($contenthash, $filesize, 0, $lifetime); $synchronized[$file->get_id()] = true; return true; }
/** * Print resource info and workaround link when JS not available. * @param object $resource * @param object $cm * @param object $course * @param stored_file $file main file * @return does not return */ function resource_print_workaround($resource, $cm, $course, $file) { global $CFG, $OUTPUT; resource_print_header($resource, $cm, $course); resource_print_heading($resource, $cm, $course, true); resource_print_intro($resource, $cm, $course, true); $resource->mainfile = $file->get_filename(); echo '<div class="resourceworkaround">'; switch (resource_get_final_display_type($resource)) { case RESOURCELIB_DISPLAY_POPUP: $path = '/' . $file->get_contextid() . '/mod_resource/content/' . $resource->revision . $file->get_filepath() . $file->get_filename(); $fullurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $path, false); $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions); $width = empty($options['popupwidth']) ? 620 : $options['popupwidth']; $height = empty($options['popupheight']) ? 450 : $options['popupheight']; $wh = "width={$width},height={$height},toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes"; $extra = "onclick=\"window.open('{$fullurl}', '', '{$wh}'); return false;\""; echo resource_get_clicktoopen($file, $resource->revision, $extra); break; case RESOURCELIB_DISPLAY_NEW: $extra = 'onclick="this.target=\'_blank\'"'; echo resource_get_clicktoopen($file, $resource->revision, $extra); break; case RESOURCELIB_DISPLAY_DOWNLOAD: echo resource_get_clicktodownload($file, $resource->revision); break; case RESOURCELIB_DISPLAY_OPEN: default: echo resource_get_clicktoopen($file, $resource->revision); break; } echo '</div>'; echo $OUTPUT->footer(); die; }
/** * Gets the file data * * @param stored_file $file * @param int $entryid Attachment entry id */ public function __construct($file, $entryid) { global $CFG; $this->file = $file; $this->filename = $file->get_filename(); $this->url = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . SYSCONTEXTID . '/blog/attachment/' . $entryid . '/' . $this->filename); }
/** * Replace the content by providing another stored_file instance * * @param stored_file $storedfile */ public function replace_content_with(stored_file $storedfile) { $contenthash = $storedfile->get_contenthash(); $this->set_contenthash($contenthash); }
/** * Perform archiving file from stored file. * * @param zip_archive $ziparch zip archive instance * @param string $archivepath file path to archive * @param stored_file $file stored_file object * @param file_progress $progress Progress indicator callback or null if not required * @return bool success */ private function archive_stored($ziparch, $archivepath, $file, file_progress $progress = null) { $result = $file->archive_file($ziparch, $archivepath); if (!$result) { return false; } if (!$file->is_directory()) { return true; } $baselength = strlen($file->get_filepath()); $fs = get_file_storage(); $files = $fs->get_directory_files($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), true, true); foreach ($files as $file) { // Record progress for each file. if ($progress) { $progress->progress(); } $path = $file->get_filepath(); $path = substr($path, $baselength); $path = $archivepath . '/' . $path; if (!$file->is_directory()) { $path = $path . $file->get_filename(); } // Ignore result here, partial zipping is ok for now. $file->archive_file($ziparch, $path); } return true; }
/** * Checks to see if a passed file is indexable. * * @param \stored_file $file The file to check * @return bool True if the file can be indexed */ protected function file_is_indexable($file) { if (!empty($this->config->maxindexfilekb) && $file->get_filesize() > $this->config->maxindexfilekb * 1024) { // The file is too big to index. return false; } $mime = $file->get_mimetype(); if ($mime == 'application/vnd.moodle.backup') { // We don't index Moodle backup files. There is nothing usefully indexable in them. return false; } return true; }
/** * Performs synchronisation of an external file if the previous one has expired. * * This function must be implemented for external repositories supporting * FILE_REFERENCE, it is called for existing aliases when their filesize, * contenthash or timemodified are requested. It is not called for internal * repositories (see {@link repository::has_moodle_files()}), references to * internal files are updated immediately when source is modified. * * Referenced files may optionally keep their content in Moodle filepool (for * thumbnail generation or to be able to serve cached copy). In this * case both contenthash and filesize need to be synchronized. Otherwise repositories * should use contenthash of empty file and correct filesize in bytes. * * Note that this function may be run for EACH file that needs to be synchronised at the * moment. If anything is being downloaded or requested from external sources there * should be a small timeout. The synchronisation is performed to update the size of * the file and/or to update image and re-generated image preview. There is nothing * fatal if syncronisation fails but it is fatal if syncronisation takes too long * and hangs the script generating a page. * * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or * $file->get_timemodified() make sure that recursion does not happen. * * Called from {@link stored_file::sync_external_file()} * * @uses stored_file::set_missingsource() * @uses stored_file::set_synchronized() * @param stored_file $file * @return bool false when file does not need synchronisation, true if it was synchronised */ public function sync_reference(stored_file $file) { if ($file->get_repository_id() != $this->id) { // This should not really happen because the function can be called from stored_file only. return false; } if ($this->has_moodle_files()) { // References to local files need to be synchronised only once. // Later they will be synchronised automatically when the source is changed. if ($file->get_referencelastsync()) { return false; } $fs = get_file_storage(); $params = file_storage::unpack_reference($file->get_reference(), true); if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']))) { $file->set_missingsource(); } else { $file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize()); } return true; } // Backward compatibility (Moodle 2.3-2.5) implementation that calls // methods repository::get_reference_file_lifetime(), repository::sync_individual_file() // and repository::get_file_by_reference(). These methods are removed from the // base repository class but may still be implemented by the child classes. // THIS IS NOT A GOOD EXAMPLE of implementation. For good examples see the overwriting methods. if (!method_exists($this, 'get_file_by_reference')) { // Function get_file_by_reference() is not implemented. No synchronisation. return false; } // Check if the previous sync result is still valid. if (method_exists($this, 'get_reference_file_lifetime')) { $lifetime = $this->get_reference_file_lifetime($file->get_reference()); } else { // Default value that was hardcoded in Moodle 2.3 - 2.5. $lifetime = 60 * 60 * 24; } if (($lastsynced = $file->get_referencelastsync()) && $lastsynced + $lifetime >= time()) { return false; } $cache = cache::make('core', 'repositories'); if (($lastsyncresult = $cache->get('sync:'.$file->get_referencefileid())) !== false) { if ($lastsyncresult === true) { // We are in the process of synchronizing this reference. // Avoid recursion when calling $file->get_filesize() and $file->get_contenthash(). return false; } else { // We have synchronised the same reference inside this request already. // It looks like the object $file was created before the synchronisation and contains old data. if (!empty($lastsyncresult['missing'])) { $file->set_missingsource(); } else { $cache->set('sync:'.$file->get_referencefileid(), true); if ($file->get_contenthash() != $lastsyncresult['contenthash'] || $file->get_filesize() != $lastsyncresult['filesize']) { $file->set_synchronized($lastsyncresult['contenthash'], $lastsyncresult['filesize']); } $cache->set('sync:'.$file->get_referencefileid(), $lastsyncresult); } return true; } } // Weird function sync_individual_file() that was present in API in 2.3 - 2.5, default value was true. if (method_exists($this, 'sync_individual_file') && !$this->sync_individual_file($file)) { return false; } // Set 'true' into the cache to indicate that file is in the process of synchronisation. $cache->set('sync:'.$file->get_referencefileid(), true); // Create object with the structure that repository::get_file_by_reference() expects. $reference = new stdClass(); $reference->id = $file->get_referencefileid(); $reference->reference = $file->get_reference(); $reference->referencehash = sha1($file->get_reference()); $reference->lastsync = $file->get_referencelastsync(); $reference->lifetime = $lifetime; $fileinfo = $this->get_file_by_reference($reference); $contenthash = null; $filesize = null; $fs = get_file_storage(); if (!empty($fileinfo->filesize)) { // filesize returned if (!empty($fileinfo->contenthash) && $fs->content_exists($fileinfo->contenthash)) { // contenthash is specified and valid $contenthash = $fileinfo->contenthash; } else if ($fileinfo->filesize == $file->get_filesize()) { // we don't know the new contenthash but the filesize did not change, // assume the contenthash did not change either $contenthash = $file->get_contenthash(); } else { // we can't save empty contenthash so generate contenthash from empty string list($contenthash, $unused1, $unused2) = $fs->add_string_to_pool(''); } $filesize = $fileinfo->filesize; } else if (!empty($fileinfo->filepath)) { // File path returned list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo->filepath); } else if (!empty($fileinfo->handle) && is_resource($fileinfo->handle)) { // File handle returned $contents = ''; while (!feof($fileinfo->handle)) { $contents .= fread($fileinfo->handle, 8192); } fclose($fileinfo->handle); list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($contents); } else if (isset($fileinfo->content)) { // File content returned list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($fileinfo->content); } if (!isset($contenthash) or !isset($filesize)) { $file->set_missingsource(null); $cache->set('sync:'.$file->get_referencefileid(), array('missing' => true)); } else { // update files table $file->set_synchronized($contenthash, $filesize); $cache->set('sync:'.$file->get_referencefileid(), array('contenthash' => $contenthash, 'filesize' => $filesize)); } return true; }
/** * Deduce export format from file mimetype * * This function returns the revelant portfolio export format * which is used to determine which portfolio plugins can be used * for exporting this content * according to the mime type of the given file * this only works when exporting exactly <b>one</b> file * * @param stored_file $file file to check mime type for * * @return string the format constant (see PORTFOLIO_FORMAT_XXX constants) */ function portfolio_format_from_file(stored_file $file) { static $alreadymatched; if (empty($alreadymatched)) { $alreadymatched = array(); } if (!$file instanceof stored_file) { throw new portfolio_exception('invalidfileargument', 'portfolio'); } $mimetype = $file->get_mimetype(); if (array_key_exists($mimetype, $alreadymatched)) { return $alreadymatched[$mimetype]; } $allformats = portfolio_supported_formats(); foreach ($allformats as $format => $classname) { $supportedmimetypes = call_user_func(array($classname, 'mimetypes')); if (!is_array($supportedmimetypes)) { debugging("one of the portfolio format classes, {$classname}, said it supported something funny for mimetypes, should have been array..."); debugging(print_r($supportedmimetypes, true)); continue; } if (in_array($mimetype, $supportedmimetypes)) { $alreadymatched[$mimetype] = $format; return $format; } } return PORTFOLIO_FORMAT_FILE; // base case for files... }
/** * Remove all role assignments in the specified course for the specified account * id for the user whose id information is passed in the line data. * * @access public * @static * @param stdClass $course Course in which to remove the role assignment * @param string $ident_field The field (column) name in Moodle user rec against which to query using the imported data * @param stored_file $import_file File in local repository from which to get enrollment and group data * @return string String message with results * * @uses $DB */ public static function unenroll_file(stdClass $course, $ident_field, stored_file $import_file) { global $DB; // Default return value $result = ''; // Need one of these in the loop $course_context = context_course::instance($course->id); // Choose the regex pattern based on the $ident_field switch ($ident_field) { case 'email': $regex_pattern = '/^"?\\s*([a-z0-9][\\w.%-]*@[a-z0-9][a-z0-9.-]{0,61}[a-z0-9]\\.[a-z]{2,6})\\s*"?(?:\\s*[;,\\t]\\s*"?\\s*([a-z0-9][\\w\' .,&-]*))?\\s*"?$/Ui'; break; case 'idnumber': $regex_pattern = '/^"?\\s*(\\d{1,32})\\s*"?(?:\\s*[;,\\t]\\s*"?\\s*([a-z0-9][\\w\' .,&-]*))?\\s*"?$/Ui'; break; default: $regex_pattern = '/^"?\\s*([a-z0-9][\\w@.-]*)\\s*"?(?:\\s*[;,\\t]\\s*"?\\s*([a-z0-9][\\w\' .,&-]*))?\\s*"?$/Ui'; break; } $user_rec = null; // Open and fetch the file contents $fh = $import_file->get_content_file_handle(); $line_num = 0; while (false !== ($line = fgets($fh))) { $line_num++; // Clean these up for each iteration unset($user_rec); if (!($line = trim($line))) { continue; } // Parse the line, from which we may get one or two // matches since the group name is an optional item // on a line by line basis if (!preg_match($regex_pattern, $line, $matches)) { $result .= sprintf(get_string('ERR_PATTERN_MATCH', self::PLUGIN_NAME), $line_num, $line); continue; } $ident_value = $matches[1]; // User must already exist, we import enrollments // into courses, not users into the system. Exclude // records marked as deleted. Because idnumber is // not enforced unique, possible multiple records // returned when using that identifying field, so // use ->get_records method to make that detection // and inform user $user_rec_array = $DB->get_records('user', array($ident_field => addslashes($ident_value), 'deleted' => 0)); // Should have one and only one record, otherwise // report it and move on to the next $user_rec_count = count($user_rec_array); if ($user_rec_count == 0) { // No record found $result .= sprintf(get_string('ERR_USERID_INVALID', self::PLUGIN_NAME), $line_num, $ident_value); continue; } elseif ($user_rec_count > 1) { // Too many records $result .= sprintf(get_string('ERR_USER_MULTIPLE_RECS', self::PLUGIN_NAME), $line_num, $ident_value); continue; } $user_rec = array_shift($user_rec_array); // Fetch all the role assignments this user might have for this course's context. $sql = 'SELECT ue.id, ue.status, ue.enrolid FROM {user_enrolments} ue JOIN {enrol} e ON e.id = ue.enrolid AND e.courseid=:courseid WHERE ue.userid=:userid'; $params = array("courseid" => $course->id, "userid" => $user_rec->id); $user_enrollments = $DB->get_records_sql($sql, $params); if ($user_enrollments) { foreach ($user_enrollments as $ue) { $instance = $DB->get_record('enrol', array('id' => $ue->enrolid), '*', MUST_EXIST); if ($instance->enrol == "meta") { $result .= sprintf(get_string('ERR_UNENROLL_META', self::PLUGIN_NAME), $line_num, $ident_value); } else { try { if (!enrol_is_enabled($instance->enrol)) { print_error('erroreditenrolment', 'enrol'); } $plugin = enrol_get_plugin($instance->enrol); if (!$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/{$instance->enrol}:unenrol", $course_context)) { print_error('erroreditenrolment', 'enrol'); } $plugin->unenrol_user($instance, $user_rec->id); } catch (Exception $exc) { $result .= sprintf(get_string('ERR_UNENROLL_FAILED', self::PLUGIN_NAME), $line_num, $ident_value); $result .= $exc->getMessage(); continue; } } } } } // while fgets fclose($fh); return empty($result) ? get_string('INF_UNENROLL_SUCCESS', self::PLUGIN_NAME) : $result; }
/** * if we already know we have exactly one file, * bypass set_formats and just pass the file * so we can detect the formats by mimetype. * * @param stored_file $file file to set the format from * @param mixed $extraformats any additional formats other than by mimetype * eg leap2a etc */ public function set_format_by_file(stored_file $file, $extraformats = null) { $this->file = $file; $fileformat = portfolio_format_from_mimetype($file->get_mimetype()); if (is_string($extraformats)) { $extraformats = array($extraformats); } else { if (!is_array($extraformats)) { $extraformats = array(); } } $this->set_formats(array_merge(array($fileformat), $extraformats)); }
/** * Repository method to serve the referenced file * * This method is ivoked from {@link send_stored_file()}. * Dropbox repository first caches the file by reading it into temporary folder and then * serves from there. * * @param stored_file $storedfile the file that contains the reference * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime) * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin * @param array $options additional options affecting the file serving */ public function send_file($storedfile, $lifetime = null, $filter = 0, $forcedownload = false, array $options = null) { $ref = unserialize($storedfile->get_reference()); if ($storedfile->get_filesize() > $this->max_cache_bytes()) { header('Location: ' . $this->get_file_download_link($ref->url)); die; } try { $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); if (!is_array($options)) { $options = array(); } $options['sendcachedexternalfile'] = true; send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options); } catch (moodle_exception $e) { // redirect to Dropbox, it will show the error. // We redirect to Dropbox shared link, not to download link here! header('Location: ' . $ref->url); die; } }
/** * Make url based on file for theme_snap components only. * * @param stored_file $file * @return \moodle_url | bool */ private static function snap_pluginfile_url($file) { if (!$file) { return false; } else { return \moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_timemodified(), $file->get_filepath(), $file->get_filename()); } }