@@ -222,16 +222,74 @@ function file_postupdate_standard_editor($data, $field, array $options, $context $editor = $data->{$field.'_editor'}; if ($options['maxfiles'] == 0 or is_null($filearea) or is_null($itemid) or empty($editor['itemid'])) { + // Remove inline images as no files should be stored + $editor['text'] = preg_replace('/data:image\/\w+;base64,[^"]*/','',$editor['text']); $data->{$field} = $editor['text']; } else { + // MDL-28506 Convert pasted images into html editor from inline to repository + // Need to remove inline images and replace with files + $editor_item_id = $editor['itemid']; + $preg_callback = function ( $matches ) use ( $editor_item_id ) { + return save_inline_images ( $matches, $editor_item_id ); + }; + $editor['text'] = preg_replace_callback('/data:image\/(\w+);base64,([^"]*)/',$preg_callback,$editor['text']); + + // Now process normal linked images $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $component, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']); } $data->{$field.'format'} = $editor['format']; - + return $data; } /** + * Store the inline image as a user uploaded file + * + * MDL-28506 Convert pasted images into html editor from inline to repository + * @category files + * @global stdClass $CFG + * @global stdClass $USER + * @param string $data base64 encoded image + * @param string $editor_item_id id of editor currently in use + * @return string URL of draft file + */ + +function save_inline_images ( $data, $editor_item_id ) { + global $CFG, $USER; + + # decode the inline image as a binary string + $file_data = base64_decode($data[2]); + + $wwwroot = $CFG->wwwroot; + if ($forcehttps) { + $wwwroot = str_replace('http://', 'https://', $wwwroot); + } + + if (isguestuser() or !isloggedin()) { + // guests and not-logged-in users can not be allowed to upload anything!!!!!! + print_error('noguest'); + } + + $contextid = context_user::instance($USER->id)->id; + + # Store the binary file as if it was user uploaded + $fs = get_file_storage(); + $draftfiles = $fs->get_area_files($contextid, 'user', 'draft', $editor_item_id); + + $filerecord = new stdClass; + $filerecord->contextid = $contextid; + $filerecord->component = 'user'; + $filerecord->filearea = 'draft'; + $filerecord->itemid = $editor_item_id; + $filerecord->filepath = '/'; + $filerecord->filename = 'inline-image-'.rand(1, 999999999).'.'.$data[1]; + $file = $fs->create_file_from_string($filerecord, $file_data ); + + return "$wwwroot/draftfile.php/$contextid/user/draft/$editor_item_id/".$filerecord->filename; +} + + +/** * Saves text and files modified by Editor formslib element * * @category files