@@ -1625,12 +1625,32 @@ public static function display_instances_list($context, $typename = null) {
16251625 * Prepare file reference information
16261626 *
16271627 * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
1628- * @return string file referece
1628+ * @return string file reference, ready to be stored
16291629 */
16301630 public function get_file_reference ($ source ) {
16311631 if ($ source && $ this ->has_moodle_files ()) {
1632- $ params = file_storage::unpack_reference ($ source );
1633- if (!is_array ($ params )) {
1632+ $ params = @json_decode (base64_decode ($ source ), true );
1633+ if (!$ params && !in_array ($ this ->get_typename (), array ('recent ' , 'user ' , 'local ' , 'coursefiles ' ))) {
1634+ // IMPORTANT! Since default format for moodle files was changed in the minor release as a security fix
1635+ // we maintain an old code here in order not to break 3rd party repositories that deal
1636+ // with moodle files. Repositories are strongly encouraged to be upgraded, see MDL-45616.
1637+ // In Moodle 2.8 this fallback will be removed.
1638+ $ params = file_storage::unpack_reference ($ source , true );
1639+ return file_storage::pack_reference ($ params );
1640+ }
1641+ if (!is_array ($ params ) || empty ($ params ['contextid ' ])) {
1642+ throw new repository_exception ('invalidparams ' , 'repository ' );
1643+ }
1644+ $ params = array (
1645+ 'component ' => empty ($ params ['component ' ]) ? '' : clean_param ($ params ['component ' ], PARAM_COMPONENT ),
1646+ 'filearea ' => empty ($ params ['filearea ' ]) ? '' : clean_param ($ params ['filearea ' ], PARAM_AREA ),
1647+ 'itemid ' => empty ($ params ['itemid ' ]) ? 0 : clean_param ($ params ['itemid ' ], PARAM_INT ),
1648+ 'filename ' => empty ($ params ['filename ' ]) ? null : clean_param ($ params ['filename ' ], PARAM_FILE ),
1649+ 'filepath ' => empty ($ params ['filepath ' ]) ? null : clean_param ($ params ['filepath ' ], PARAM_PATH ),
1650+ 'contextid ' => clean_param ($ params ['contextid ' ], PARAM_INT )
1651+ );
1652+ // Check if context exists.
1653+ if (!context::instance_by_id ($ params ['contextid ' ], IGNORE_MISSING )) {
16341654 throw new repository_exception ('invalidparams ' , 'repository ' );
16351655 }
16361656 return file_storage::pack_reference ($ params );
0 commit comments