Skip to content

Commit 867f409

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 78ba7d9 commit 867f409

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

Diff for: 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);

Diff for: repository/lib.php

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

841841
/**
842-
* Parses the 'source' returned by moodle repositories and returns an instance of stored_file
842+
* Parses the moodle file reference and returns an instance of stored_file
843843
*
844-
* @param string $source
844+
* @param string $reference reference to the moodle internal file as retruned by
845+
* {@link repository::get_file_reference()} or {@link file_storage::pack_reference()}
845846
* @return stored_file|null
846847
*/
847-
public static function get_moodle_file($source) {
848-
$params = file_storage::unpack_reference($source, true);
848+
public static function get_moodle_file($reference) {
849+
$params = file_storage::unpack_reference($reference, true);
849850
$fs = get_file_storage();
850851
return $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
851852
$params['itemid'], $params['filepath'], $params['filename']);
@@ -857,13 +858,14 @@ public static function get_moodle_file($source) {
857858
* This is checked when user tries to pick the file from repository to deal with
858859
* potential parameter substitutions is request
859860
*
860-
* @param string $source
861+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
861862
* @return bool whether the file is accessible by current user
862863
*/
863864
public function file_is_accessible($source) {
864865
if ($this->has_moodle_files()) {
866+
$reference = $this->get_file_reference($source);
865867
try {
866-
$params = file_storage::unpack_reference($source, true);
868+
$params = file_storage::unpack_reference($reference, true);
867869
} catch (file_reference_exception $e) {
868870
return false;
869871
}
@@ -1376,12 +1378,13 @@ public function cache_file_by_reference($reference, $storedfile) {
13761378
* again to another file area (also as a copy or as a reference), the value of
13771379
* files.source is copied.
13781380
*
1379-
* @param string $source the value that repository returned in listing as 'source'
1381+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
13801382
* @return string|null
13811383
*/
13821384
public function get_file_source_info($source) {
13831385
if ($this->has_moodle_files()) {
1384-
return $this->get_reference_details($source, 0);
1386+
$reference = $this->get_file_reference($source);
1387+
return $this->get_reference_details($reference, 0);
13851388
}
13861389
return $source;
13871390
}
@@ -1662,11 +1665,11 @@ public static function display_instances_list($context, $typename = null) {
16621665
/**
16631666
* Prepare file reference information
16641667
*
1665-
* @param string $source
1668+
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
16661669
* @return string file referece
16671670
*/
16681671
public function get_file_reference($source) {
1669-
if ($this->has_moodle_files() && ($this->supported_returntypes() & FILE_REFERENCE)) {
1672+
if ($source && $this->has_moodle_files()) {
16701673
$params = file_storage::unpack_reference($source);
16711674
if (!is_array($params)) {
16721675
throw new repository_exception('invalidparams', 'repository');

Diff for: 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)