]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 file browsing refactoring - moving url encoding to filelib.php for now
authorskodak <skodak>
Wed, 20 May 2009 22:32:02 +0000 (22:32 +0000)
committerskodak <skodak>
Wed, 20 May 2009 22:32:02 +0000 (22:32 +0000)
15 files changed:
blog/lib.php
files/draftfiles.php
lib/file/file_browser.php
lib/file/file_info_coursefile.php
lib/file/file_info_stored.php
lib/filelib.php
lib/form/filemanager.php
lib/simpletest/testfilelib.php
mod/assignment/lib.php
mod/assignment/type/upload/assignment.class.php
mod/assignment/type/uploadsingle/assignment.class.php
mod/data/field/file/field.class.php
mod/data/field/picture/field.class.php
mod/forum/lib.php
mod/glossary/lib.php

index b94635e6e63eed8017475538eecf2a7f76e3845f..0541d191b0e66446fed1626d2e412f23106c463d 100755 (executable)
             }
 
             $filename = $file->get_filename();
-            $ffurl    = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/'.$blogentry->id.'/'.$filename);
+            $ffurl    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/'.$blogentry->id.'/'.$filename);
             $type     = $file->get_mimetype();
             $icon     = mimeinfo_from_type("icon", $type);
             $type     = mimeinfo_from_type("type", $type);
index bc584cff7bfc86ee64f8b19fc9773242663a7d4a..c6371d2f21139f9990466cd49688340b2f1d6ce5 100644 (file)
 
         } else {
             $icon    = mimeinfo_from_type('icon', $mimetype);
-            $viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false);
+            $viewurl = file_encode_url("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false);
             echo '<div class="file">';
             echo "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".s($filename)." ($filesize)</a> ";
             echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;delete=$filenameurl&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
index e446e411656367ea6362e9c5fde175fae6d41f5a..c873cd1691f492812c1a9623f122b28791438af7 100644 (file)
@@ -79,32 +79,6 @@ class file_browser {
         return null;
     }
 
-    public function encodepath($urlbase, $path, $forcedownload=false, $https=false) {
-        global $CFG;
-
-        if ($CFG->slasharguments) {
-            $parts = explode('/', $path);
-            $parts = array_map('rawurlencode', $parts);
-            $path  = implode('/', $parts);
-            $return = $urlbase.$path;
-            if ($forcedownload) {
-                $return .= '?forcedownload=1';
-            }
-        } else {
-            $path = rawurlencode($path);
-            $return = $urlbase.'?file='.$path;
-            if ($forcedownload) {
-                $return .= '&amp;forcedownload=1';
-            }
-        }
-
-        if ($https) {
-            $return = str_replace('http://', 'https://', $return);
-        }
-
-        return $return;
-    }
-
     /**
      * Returns info about the files at System context
      * @param object $context
index a903a15f12d97d46815cf1cc0042235763a58290..d8c9593289a7e9b6863690618463d2fd72c09ec7 100644 (file)
@@ -51,7 +51,7 @@ class file_info_coursefile extends file_info_stored {
 
         $path = '/'.$courseid.$filepath.$filename;
 
-        return $this->browser->encodepath($this->urlbase, $path, $forcedownload, $https);
+        return file_encode_url($this->urlbase, $path, $forcedownload, $https);
     }
 
     public function get_children() {
index ce1d2db81aeacd563c7f80ac8308e139df09edfa..d8e12fbb09c4a72041d1cdbc5b64b52bd13e3bd4 100644 (file)
@@ -99,7 +99,7 @@ class file_info_stored extends file_info {
         } else {
             $path = '/'.$contextid.'/'.$filearea.$filepath.$filename;
         }
-        return $this->browser->encodepath($this->urlbase, $path, $forcedownload, $https);
+        return file_encode_url($this->urlbase, $path, $forcedownload, $https);
     }
 
     public function is_readable() {
index cfc395a95e46bf4c1ba72cf01fed2d38c1d8b01f..4089f10a594888d6684e26df37e2d8f9002912d0 100644 (file)
@@ -107,6 +107,42 @@ function get_file_url($path, $options=null, $type='coursefile') {
     return $ffurl;
 }
 
+
+/**
+ * Encodes file serving url
+ * TODO: decide if we really need this
+ * @param string $urlbase
+ * @param string $path /filearea/itemid/dir/dir/file.exe
+ * @param bool $forcedownload
+ * @param bool $https https url required
+ * @return string encoded file url
+ */
+function file_encode_url($urlbase, $path, $forcedownload=false, $https=false) {
+    global $CFG;
+
+    if ($CFG->slasharguments) {
+        $parts = explode('/', $path);
+        $parts = array_map('rawurlencode', $parts);
+        $path  = implode('/', $parts);
+        $return = $urlbase.$path;
+        if ($forcedownload) {
+            $return .= '?forcedownload=1';
+        }
+    } else {
+        $path = rawurlencode($path);
+        $return = $urlbase.'?file='.$path;
+        if ($forcedownload) {
+            $return .= '&amp;forcedownload=1';
+        }
+    }
+
+    if ($https) {
+        $return = str_replace('http://', 'https://', $return);
+    }
+
+    return $return;
+}
+
 /**
  * Prepares standardised text field fro editing with Editor formslib element
  * @param object $data $database entry field
index eef7bf044a56f15dc16f24a80acfebaab83ff8f2..3e25c5220eb5bde742dc46d7ea2641fc134c83bc 100644 (file)
@@ -116,7 +116,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
             $filesize    = $filesize ? display_size($filesize) : '';
             $mimetype = $file->get_mimetype();
             $icon    = mimeinfo_from_type('icon', $mimetype);
-            $viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$draftid".$filepath.$filename, false, false);
+            $viewurl = file_encode_url("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$draftid".$filepath.$filename, false, false);
             $html .= '<li>';
             $html .= "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" />&nbsp;".s($filename)." ($filesize)</a> ";
             $html .= "<a href=\"###\" onclick='rm_$suffix(".$file->get_itemid().", \"".$filename."\", this)'><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";;
index c6ec9834fcda87ae57d401b1008ecb98afda4444..b0416ffd3e27f544e4d936a0d7b0ad917884ae40 100644 (file)
@@ -251,18 +251,18 @@ class file_browser_test extends filelib_test {
         $fb = new file_browser();
 
         $CFG->slasharguments = true;
-        $this->assertEqual('http://test.url.com/path/to/page.php', $fb->encodepath('http://test.url.com', '/path/to/page.php'));
-        $this->assertEqual('http://test.url.com/path/to/page.php?forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php', true));
-        $this->assertEqual('https://test.url.com/path/to/page.php?forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php', true, true));
+        $this->assertEqual('http://test.url.com/path/to/page.php', file_encode_url('http://test.url.com', '/path/to/page.php'));
+        $this->assertEqual('http://test.url.com/path/to/page.php?forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true));
+        $this->assertEqual('https://test.url.com/path/to/page.php?forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true, true));
 
         // TODO add error checking for malformed path (does method support get variables?)
-        $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2', $fb->encodepath('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2'));
-        $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2&forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2', true));
+        $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2', file_encode_url('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2'));
+        $this->assertEqual('http://test.url.com/path/to/page.php?var1=value1&var2=value2&forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php?var1=value1&var2=value2', true));
 
         $CFG->slasharguments = false;
-        $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php', $fb->encodepath('http://test.url.com', '/path/to/page.php'));
-        $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php&amp;forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php', true));
-        $this->assertEqual('https://test.url.com?file=%2Fpath%2Fto%2Fpage.php&amp;forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php', true, true));
+        $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php', file_encode_url('http://test.url.com', '/path/to/page.php'));
+        $this->assertEqual('http://test.url.com?file=%2Fpath%2Fto%2Fpage.php&amp;forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true));
+        $this->assertEqual('https://test.url.com?file=%2Fpath%2Fto%2Fpage.php&amp;forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true, true));
     }
 }
 
index ac34749d7be183deef3b8ecaf552744f2de516c5..c9e953b8020f9e79f744ded8e417f0a29d3ccaf4 100644 (file)
@@ -1705,7 +1705,7 @@ class assignment_base {
                 $found = true;
                 $mimetype = $file->get_mimetype();
                 $icon = mimeinfo_from_type('icon', $mimetype);
-                $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.s($filename).'</a>';
                 if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
                     $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()));
index ea3d7ebeb9bb9774b31ae900ef000f83867c8d78..bea36477409d86e36f9a4c18dbfbdfee21ab4185 100644 (file)
@@ -287,7 +287,7 @@ class assignment_upload extends assignment_base {
                 $found = true;
                 $mimetype = $file->get_mimetype();
                 $icon = mimeinfo_from_type('icon', $mimetype);
-                $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.s($filename).'</a>&nbsp;';
 
             }
@@ -347,7 +347,7 @@ class assignment_upload extends assignment_base {
                 $filename = $file->get_filename();
                 $mimetype = $file->get_mimetype();
                 $icon = mimeinfo_from_type('icon', $mimetype);
-                $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.s($filename).'</a>';
 
                 if ($candelete) {
@@ -409,7 +409,7 @@ class assignment_upload extends assignment_base {
                 $found = true;
                 $mimetype = $file->get_mimetype();
                 $icon = mimeinfo_from_type('icon', $mimetype);
-                $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_response/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_response/'.$userid.'/'.$filename);
 
                 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$filename.'</a>';
 
index 6fc81ef3a18fa5de52eab8a6edf51377dc5c7afc..70b8669ab058b66792dc119e5eedee6ec1fe4dba 100644 (file)
@@ -22,7 +22,7 @@ class assignment_uploadsingle extends assignment_base {
                 $found = true;
                 $mimetype = $file->get_mimetype();
                 $icon = mimeinfo_from_type('icon', $mimetype);
-                $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.s($filename).'</a><br />';
             }
         }
index bb58c0c79914255b67e9af5ad2d26147cc72177f..3e49966121e0df2d5abaf0a66a886a8f82eea5b3 100755 (executable)
@@ -60,7 +60,7 @@ class data_field_file extends data_field_base {
             // Print icon if file already exists
             $browser = get_file_browser();
             $icon    = mimeinfo_from_type('icon', $file->get_mimetype());
-            $src     = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
+            $src     = file_encode_url($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
             $str .= '<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
                     '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
         }
@@ -119,7 +119,7 @@ class data_field_file extends data_field_base {
 
         $name   = empty($content->content1) ? $file->get_filename() : $content->content1;
         $icon   = mimeinfo_from_type('icon', $file->get_mimetype());
-        $src    = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
+        $src    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
         $width  = $this->field->param1 ? ' width  = "'.s($this->field->param1).'" ':' ';
         $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
 
index d40387352cc09b17c095d3ad6ba91379daac88bc..6e815562897910dff70b63087e3c8d64496ef823 100755 (executable)
@@ -59,7 +59,7 @@ class data_field_picture extends data_field_base {
         //$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />';
         if ($file) {
             $browser = get_file_browser();
-            $src     = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
+            $src     = file_encode_url($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename());
             $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />';
         }
         $str .= '</fieldset>';
@@ -121,12 +121,12 @@ class data_field_picture extends data_field_base {
         $title = $alt;
 
         if ($template == 'listtemplate') {
-            $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.'thumb_'.$content->content);
+            $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.'thumb_'.$content->content);
             // no need to add width/height, because the thumb is resized properly
             $str = '<a href="view.php?d='.$this->field->dataid.'&amp;rid='.$recordid.'"><img src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>';
 
         } else {
-            $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$content->content);
+            $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$content->content);
             $width  = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' ';
             $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' ';
             $str = '<a href="'.$src.'"><img '.$width.$height.' src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>';
index 25c14a5d10b353324cde17ea5e05cc0a682e98d1..05169b93f17c2d596c760953d3aace51dace974e 100644 (file)
@@ -4005,7 +4005,7 @@ function forum_print_attachments($post, $cm, $type) {
             $mimetype = $file->get_mimetype();
             $icon = mimeinfo_from_type('icon', $mimetype);
             $iconimage = '<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />';
-            $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/forum_attachment/'.$post->id.'/'.$filename);
+            $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/forum_attachment/'.$post->id.'/'.$filename);
 
             if ($type == 'html') {
                 $output .= "<a href=\"$path\">$iconimage</a> ";
index cf4b31ebe407da69c17978042bdbf939a9ee6786..c6cb39ccbbaa70ac6fa679dfae90f803c137ac55 100644 (file)
@@ -1032,7 +1032,7 @@ function glossary_print_attachments($entry, $cm, $type=NULL, $align="left") {
             $mimetype = $file->get_mimetype();
             $icon = mimeinfo_from_type('icon', $mimetype);
             $iconimage = '<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />';
-            $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/glossary_attachment/'.$entry->id.'/'.$filename);
+            $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/glossary_attachment/'.$entry->id.'/'.$filename);
 
             if ($type == 'html') {
                 $output .= "<a href=\"$path\">$iconimage</a> ";