@@ -1655,12 +1655,32 @@ public static function display_instances_list($context, $typename = null) {
16551655 * Prepare file reference information
16561656 *
16571657 * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
1658- * @return string file referece
1658+ * @return string file reference, ready to be stored
16591659 */
16601660 public function get_file_reference ($ source ) {
16611661 if ($ source && $ this ->has_moodle_files ()) {
1662- $ params = file_storage::unpack_reference ($ source );
1663- if (!is_array ($ params )) {
1662+ $ params = @json_decode (base64_decode ($ source ), true );
1663+ if (!$ params && !in_array ($ this ->get_typename (), array ('recent ' , 'user ' , 'local ' , 'coursefiles ' ))) {
1664+ // IMPORTANT! Since default format for moodle files was changed in the minor release as a security fix
1665+ // we maintain an old code here in order not to break 3rd party repositories that deal
1666+ // with moodle files. Repositories are strongly encouraged to be upgraded, see MDL-45616.
1667+ // In Moodle 2.8 this fallback will be removed.
1668+ $ params = file_storage::unpack_reference ($ source , true );
1669+ return file_storage::pack_reference ($ params );
1670+ }
1671+ if (!is_array ($ params ) || empty ($ params ['contextid ' ])) {
1672+ throw new repository_exception ('invalidparams ' , 'repository ' );
1673+ }
1674+ $ params = array (
1675+ 'component ' => empty ($ params ['component ' ]) ? '' : clean_param ($ params ['component ' ], PARAM_COMPONENT ),
1676+ 'filearea ' => empty ($ params ['filearea ' ]) ? '' : clean_param ($ params ['filearea ' ], PARAM_AREA ),
1677+ 'itemid ' => empty ($ params ['itemid ' ]) ? 0 : clean_param ($ params ['itemid ' ], PARAM_INT ),
1678+ 'filename ' => empty ($ params ['filename ' ]) ? null : clean_param ($ params ['filename ' ], PARAM_FILE ),
1679+ 'filepath ' => empty ($ params ['filepath ' ]) ? null : clean_param ($ params ['filepath ' ], PARAM_PATH ),
1680+ 'contextid ' => clean_param ($ params ['contextid ' ], PARAM_INT )
1681+ );
1682+ // Check if context exists.
1683+ if (!context::instance_by_id ($ params ['contextid ' ], IGNORE_MISSING )) {
16641684 throw new repository_exception ('invalidparams ' , 'repository ' );
16651685 }
16661686 return file_storage::pack_reference ($ params );
0 commit comments