Permalink
Browse files

MDL-33452 It must not possible to create an alias to another alias

- if the downloaded file is an alias we pretend that we operate with its source
  • Loading branch information...
1 parent f8dfdb5 commit c0e8f211d31924f5b6bf230d3552594250e83f68 @marinaglancy marinaglancy committed Jun 6, 2012
Showing with 36 additions and 0 deletions.
  1. +26 −0 repository/lib.php
  2. +10 −0 repository/repository_ajax.php
View
26 repository/lib.php
@@ -645,6 +645,32 @@ public static function draftfile_exists($itemid, $filepath, $filename) {
}
/**
+ * Parses the 'source' returned by moodle repositories and returns an instance of stored_file
+ *
+ * @param string $source
+ * @return stored_file|null
+ */
+ public static function get_moodle_file($source) {
+ $params = unserialize(base64_decode($source));
+ if (empty($params) || !is_array($params)) {
+ return null;
+ }
+ foreach (array('contextid', 'itemid', 'filename', 'filepath', 'component') as $key) {
+ if (!array_key_exists($key, $params)) {
+ return null;
+ }
+ }
+ $contextid = clean_param($params['contextid'], PARAM_INT);
+ $component = clean_param($params['component'], PARAM_COMPONENT);
+ $filearea = clean_param($params['filearea'], PARAM_AREA);
+ $itemid = clean_param($params['itemid'], PARAM_INT);
+ $filepath = clean_param($params['filepath'], PARAM_PATH);
+ $filename = clean_param($params['filename'], PARAM_FILE);
+ $fs = get_file_storage();
+ return $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
+ }
+
+ /**
* This function is used to copy a moodle file to draft area
*
* @param string $encoded The metainfo of file, it is base64 encoded php serialized data
View
10 repository/repository_ajax.php
@@ -231,6 +231,16 @@
$record->timemodified = $now;
$record->userid = $USER->id;
+ // If file is already a reference, set $source = file source, $repo = file repository
+ if ($repo->has_moodle_files()) {
+ $file = repository::get_moodle_file($source);
+ if ($file && $file->is_external_file()) {
+ $source = $file->get_reference();
+ $repo_id = $file->get_repository_id();
+ $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
+ }
+ }
+
if ($usefilereference) {
$reference = $repo->get_file_reference($source);
// get reference life time from repo

0 comments on commit c0e8f21

Please sign in to comment.