From 927ce887595a69e7d7f86fdf843403d01de14b63 Mon Sep 17 00:00:00 2001 From: scyrma Date: Thu, 4 Sep 2008 01:56:50 +0000 Subject: [PATCH] MDL-15405: Add support for relinking of inline media --- lib/filelib.php | 34 ++++++++++++++++++++++++++++------ mod/forum/lib.php | 28 ++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index e178f27304..08e6bd4d76 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -66,6 +66,7 @@ function get_file_url($path, $options=null, $type='coursefile') { * must be called once for each file. * * @uses $CFG + * @see file_storage::move_draft_to_final() * * @param $text string text to modify * @param $contextid int context that the file should be assigned to @@ -75,17 +76,38 @@ function get_file_url($path, $options=null, $type='coursefile') { * @param $currentcontextid int the current contextid of the file * @param $currentfilearea string the current filearea of the file (defaults * to "user_draft") + * @param $overwrite bool wether the files should overwrite existing files * @return string modified $text, or null if an error occured. */ -function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'userdraft') { +function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'user_draft', $overwrite = false) { global $CFG; - //TODO: complete, test and adjust if the filearea is removed from the url. + $context = get_context_instance_by_id($contextid); + $fs = get_file_storage(); - $re = $CFG->wwwroot .'\/draftfile.php\/'. $currentcontextid .'\/'. $currentfilearea .'\/'. $itemid .'\/(?[A-Fa-f0-9]+)\/([.]+)/'; - $newurl = $CFG->wwwroot .'/userfile.php/'. $contextid .'/'. $filearea .'/'. $itemid .'/'. $filepath .'/\0'; - - return preg_replace($re, $newurl, $text); + $re = '|'. preg_quote($CFG->wwwroot) .'/draftfile.php/'. $currentcontextid .'/'. $currentfilearea .'/([0-9]+)/(?:[A-Fa-f0-9]+)/([^"]+)|'; + $matches = array(); + if (!preg_match_all($re, $text, $matches, PREG_SET_ORDER)) { + return $text; // no draftfile url in text. + } + + foreach($matches as $file) { + if ($newfiles = $fs->move_draft_to_final($file[1], $contextid, $filearea, $itemid, $filepath, $overwrite)) { + foreach($newfiles as $newfile) { + var_dump($newfile); + if ($context->contextlevel == 'CONTEXT_USER') { + //TODO: get the good url for userfile.php + //$newurl = $CFG->wwwroot .'/userfile.php/'. $contextid .'/'. $filearea .'/'. $itemid; + } else { + $newurl = $CFG->wwwroot .'/pluginfile.php/'. $contextid .'/'. $filearea .'/'. $itemid . $newfile->get_filepath() . $newfile->get_filename(); + } + if ($newurl) { // can be removed whenever userfile.php newurl is properly built + $text = str_replace('"'. $file[0] .'"', '"'. $newurl .'"', $text); + } + } + } // else file not found, wrong file, or string is just not a file so we leave it alone. + } + return $text; } /** diff --git a/mod/forum/lib.php b/mod/forum/lib.php index de39837629..45fadf3bbc 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3969,7 +3969,8 @@ function forum_print_attachments($post, $cm, $type) { function forum_pluginfile($course, $cminfo, $context, $filearea, $args) { global $CFG, $DB; - if ($filearea !== 'forum_attachment') { + $fileareas = array('forum_attachment', 'forum_post'); + if (!in_array($filearea, $fileareas)) { return false; } @@ -4053,7 +4054,24 @@ function forum_add_attachment($post, $cm, $mform=null, &$message=null, $remove_p } /** - * + * Relinks urls linked to draftfile.php in $post->message. + */ +function forum_relink_inline_attachments($post, $cm){ + global $DB; + + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $authorcontext = get_context_instance(CONTEXT_USER, $post->userid); + + if ($post->message = file_rewrite_urls($post->message, $context->id, '/', 'forum_post', $post->id, $authorcontext->id)) { + $DB->set_field("forum_posts", "message", $post->message, array("id" => $post->id)); + return true; + } else { + return false; + } +} + +/** + * Add a new post in an existing discussion. */ function forum_add_new_post($post, $mform, &$message) { global $USER, $CFG, $DB; @@ -4071,6 +4089,7 @@ function forum_add_new_post($post, $mform, &$message) { return false; } + forum_relink_inline_attachments($post, $cm); forum_add_attachment($post, $cm, $mform, $message, false); // Update discussion modified date @@ -4085,7 +4104,7 @@ function forum_add_new_post($post, $mform, &$message) { } /** - * + * Update a post */ function forum_update_post($post, $mform, &$message) { global $USER, $CFG, $DB; @@ -4113,6 +4132,7 @@ function forum_update_post($post, $mform, &$message) { return false; } + forum_relink_inline_attachments($post, $cm); forum_add_attachment($post, $cm, $mform, $message, true); if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { @@ -4175,7 +4195,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) { return 0; } - // save attachment if present + forum_relink_inline_attachments($post, $cm); forum_add_attachment($post, $cm, $mform, $message, false); if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { -- 2.39.5