@@ -1666,12 +1666,32 @@ public static function display_instances_list($context, $typename = null) {
16661666 * Prepare file reference information
16671667 *
16681668 * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
1669- * @return string file referece
1669+ * @return string file reference, ready to be stored
16701670 */
16711671 public function get_file_reference ($ source ) {
16721672 if ($ source && $ this ->has_moodle_files ()) {
1673- $ params = file_storage::unpack_reference ($ source );
1674- if (!is_array ($ params )) {
1673+ $ params = @json_decode (base64_decode ($ source ), true );
1674+ if (!$ params && !in_array ($ this ->get_typename (), array ('recent ' , 'user ' , 'local ' , 'coursefiles ' ))) {
1675+ // IMPORTANT! Since default format for moodle files was changed in the minor release as a security fix
1676+ // we maintain an old code here in order not to break 3rd party repositories that deal
1677+ // with moodle files. Repositories are strongly encouraged to be upgraded, see MDL-45616.
1678+ // In Moodle 2.8 this fallback will be removed.
1679+ $ params = file_storage::unpack_reference ($ source , true );
1680+ return file_storage::pack_reference ($ params );
1681+ }
1682+ if (!is_array ($ params ) || empty ($ params ['contextid ' ])) {
1683+ throw new repository_exception ('invalidparams ' , 'repository ' );
1684+ }
1685+ $ params = array (
1686+ 'component ' => empty ($ params ['component ' ]) ? '' : clean_param ($ params ['component ' ], PARAM_COMPONENT ),
1687+ 'filearea ' => empty ($ params ['filearea ' ]) ? '' : clean_param ($ params ['filearea ' ], PARAM_AREA ),
1688+ 'itemid ' => empty ($ params ['itemid ' ]) ? 0 : clean_param ($ params ['itemid ' ], PARAM_INT ),
1689+ 'filename ' => empty ($ params ['filename ' ]) ? null : clean_param ($ params ['filename ' ], PARAM_FILE ),
1690+ 'filepath ' => empty ($ params ['filepath ' ]) ? null : clean_param ($ params ['filepath ' ], PARAM_PATH ),
1691+ 'contextid ' => clean_param ($ params ['contextid ' ], PARAM_INT )
1692+ );
1693+ // Check if context exists.
1694+ if (!context::instance_by_id ($ params ['contextid ' ], IGNORE_MISSING )) {
16751695 throw new repository_exception ('invalidparams ' , 'repository ' );
16761696 }
16771697 return file_storage::pack_reference ($ params );
0 commit comments