]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15405: first version of post-upload content rewriting function
authorscyrma <scyrma>
Mon, 1 Sep 2008 06:15:08 +0000 (06:15 +0000)
committerscyrma <scyrma>
Mon, 1 Sep 2008 06:15:08 +0000 (06:15 +0000)
lib/filelib.php

index adfcf0dbcb458b194faa1405b87e64dfbf63f55e..e178f273046426701c6eb016b5d810e3c197b227 100644 (file)
@@ -58,6 +58,36 @@ function get_file_url($path, $options=null, $type='coursefile') {
     return $ffurl;
 }
 
+/**
+ * Finds occurences of a link to "draftfile.php" in text and replaces the 
+ * address based on passed information. Matching is performed using the given 
+ * current itemid, contextid and filearea and $CFG->wwwroot. This function 
+ * replaces all the urls for one file. If more than one files were sent, it 
+ * must be called once for each file.
+ *
+ * @uses $CFG
+ *
+ * @param $text string text to modify
+ * @param $contextid int context that the file should be assigned to
+ * @param $filepath string filepath under which the file should be saved
+ * @param $filearea string filearea into which the file should be saved
+ * @param $itemid int the itemid of the file
+ * @param $currentcontextid int the current contextid of the file
+ * @param $currentfilearea string the current filearea of the file (defaults
+ *   to "user_draft")
+ * @return string modified $text, or null if an error occured.
+ */
+function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'userdraft') {
+    global $CFG;
+
+    //TODO: complete, test and adjust if the filearea is removed from the url.
+
+    $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);
+}
+
 /**
  * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
  * Due to security concerns only downloads from http(s) sources are supported.