Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-40258

Filepicker "Invalid JSON String" error due to bug in how accepted_types is handled

    Details

    • Testing Instructions:
      Hide

      Not sure it can be reproduced anywhere in core at the moment but it surely can break 3rd party code

      To test

      1. hack /user/files.php (or any other filemanager options) and set
        'accepted_types' => array('.jpg', 'web_image')
      2. Open page with this filemanager
      3. Try to pick files from repositories, make sure no errors occur
      Show
      Not sure it can be reproduced anywhere in core at the moment but it surely can break 3rd party code To test hack /user/files.php (or any other filemanager options) and set 'accepted_types' => array('.jpg', 'web_image') Open page with this filemanager Try to pick files from repositories, make sure no errors occur
    • Affected Branches:
      MOODLE_23_STABLE, MOODLE_24_STABLE, MOODLE_25_STABLE, MOODLE_26_STABLE
    • Fixed Branches:
      MOODLE_23_STABLE, MOODLE_24_STABLE, MOODLE_25_STABLE
    • Pull Master Branch:
      MDL-40258_master

      Description

      I discovered a bug in the way that the "accepted_types" option of the file picker is handled. When passing in several different extensions and mime-types in the array file_get_grouptype() in lib/filelib.php returns an numerically indexed array of extensions. Those are passed in js_init_call(), which json_encodes it and includes in the page on output.

      When the array has consecutive indices starting at 0 (e.g. array(0 => 'jpg', 1 => 'png', 2 => 'svg') it gets encoded as an Array in JavaScript (e.g. ['jpg', 'png', 'svg']) and when the filepicker code turns that into the accepted_types parameter for the ajax call to the repository it works great there is no error.

      When the array has non-consecutive indices (e.g. array(0 => 'jpg', 1 => 'png', 9 => 'svg') it gets encoded as an JavaScript Object instead (e.g.

      {0: 'jpg', 1: 'png', 9: 'svg'}

      which in turn gets turned into the string "[object Object]" when the filepicker code trys to turn it into the accepted_types parameter for the ajax call to the repository.

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  8/Jul/13