Skip to content

Commit 68170f0

Browse files
marinaglancyDamyon Wiese
authored and
Damyon Wiese
committed
MDL-45616 repositories: more clearly distinguish when we use source and when reference
Function repository::get_moodle_file() should always be called on packed reference and not on the source received from user. Also added phpdocs to some other methods that were confusing source and reference
1 parent 7d6e6ff commit 68170f0

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

repository/filepicker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
// note that in this case user may not have permission to access the source file directly
294294
// so no file_browser/file_info can be used below
295295
if ($repo->has_moodle_files()) {
296-
$file = repository::get_moodle_file($fileurl);
296+
$file = repository::get_moodle_file($reference);
297297
if ($file && $file->is_external_file()) {
298298
$sourcefield = $file->get_source(); // remember the original source
299299
$record->source = $repo::build_source_field($sourcefield);

repository/lib.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -828,13 +828,14 @@ public static function draftfile_exists($itemid, $filepath, $filename) {
828828
}
829829

830830
/**
831-
* Parses the 'source' returned by moodle repositories and returns an instance of stored_file
831+
* Parses the moodle file reference and returns an instance of stored_file
832832
*
833-
* @param string $source
833+
* @param string $reference reference to the moodle internal file as retruned by
834+
* {@link repository::get_file_reference()} or {@link file_storage::pack_reference()}
834835
* @return stored_file|null
835836
*/
836-
public static function get_moodle_file($source) {
837-
$params = file_storage::unpack_reference($source, true);
837+
public static function get_moodle_file($reference) {
838+
$params = file_storage::unpack_reference($reference, true);
838839
$fs = get_file_storage();
839840
return $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
840841
$params['itemid'], $params['filepath'], $params['filename']);
@@ -846,13 +847,14 @@ public static function get_moodle_file($source) {
846847
* This is checked when user tries to pick the file from repository to deal with
847848
* potential parameter substitutions is request
848849
*
849-
* @param string $source
850+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
850851
* @return bool whether the file is accessible by current user
851852
*/
852853
public function file_is_accessible($source) {
853854
if ($this->has_moodle_files()) {
855+
$reference = $this->get_file_reference($source);
854856
try {
855-
$params = file_storage::unpack_reference($source, true);
857+
$params = file_storage::unpack_reference($reference, true);
856858
} catch (file_reference_exception $e) {
857859
return false;
858860
}
@@ -1365,12 +1367,13 @@ public function cache_file_by_reference($reference, $storedfile) {
13651367
* again to another file area (also as a copy or as a reference), the value of
13661368
* files.source is copied.
13671369
*
1368-
* @param string $source the value that repository returned in listing as 'source'
1370+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
13691371
* @return string|null
13701372
*/
13711373
public function get_file_source_info($source) {
13721374
if ($this->has_moodle_files()) {
1373-
return $this->get_reference_details($source, 0);
1375+
$reference = $this->get_file_reference($source);
1376+
return $this->get_reference_details($reference, 0);
13741377
}
13751378
return $source;
13761379
}
@@ -1651,11 +1654,11 @@ public static function display_instances_list($context, $typename = null) {
16511654
/**
16521655
* Prepare file reference information
16531656
*
1654-
* @param string $source
1657+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
16551658
* @return string file referece
16561659
*/
16571660
public function get_file_reference($source) {
1658-
if ($this->has_moodle_files() && ($this->supported_returntypes() & FILE_REFERENCE)) {
1661+
if ($source && $this->has_moodle_files()) {
16591662
$params = file_storage::unpack_reference($source);
16601663
if (!is_array($params)) {
16611664
throw new repository_exception('invalidparams', 'repository');

repository/repository_ajax.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
// note that in this case user may not have permission to access the source file directly
209209
// so no file_browser/file_info can be used below
210210
if ($repo->has_moodle_files()) {
211-
$file = repository::get_moodle_file($source);
211+
$file = repository::get_moodle_file($reference);
212212
if ($file && $file->is_external_file()) {
213213
$sourcefield = $file->get_source(); // remember the original source
214214
$record->source = $repo::build_source_field($sourcefield);

0 commit comments

Comments
 (0)