From: skodak Date: Wed, 20 May 2009 22:32:02 +0000 (+0000) Subject: MDL-14589 file browsing refactoring - moving url encoding to filelib.php for now X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4eef139962ed53063bd74d5d317bb5d3a22e16f1;p=moodle.git MDL-14589 file browsing refactoring - moving url encoding to filelib.php for now --- diff --git a/blog/lib.php b/blog/lib.php index b94635e6e6..0541d191b0 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -279,7 +279,7 @@ } $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); diff --git a/files/draftfiles.php b/files/draftfiles.php index bc584cff7b..c6371d2f21 100644 --- a/files/draftfiles.php +++ b/files/draftfiles.php @@ -133,7 +133,7 @@ } 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 '
'; echo "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize) "; echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />";; diff --git a/lib/file/file_browser.php b/lib/file/file_browser.php index e446e41165..c873cd1691 100644 --- a/lib/file/file_browser.php +++ b/lib/file/file_browser.php @@ -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 .= '&forcedownload=1'; - } - } - - if ($https) { - $return = str_replace('http://', 'https://', $return); - } - - return $return; - } - /** * Returns info about the files at System context * @param object $context diff --git a/lib/file/file_info_coursefile.php b/lib/file/file_info_coursefile.php index a903a15f12..d8c9593289 100644 --- a/lib/file/file_info_coursefile.php +++ b/lib/file/file_info_coursefile.php @@ -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() { diff --git a/lib/file/file_info_stored.php b/lib/file/file_info_stored.php index ce1d2db81a..d8e12fbb09 100644 --- a/lib/file/file_info_stored.php +++ b/lib/file/file_info_stored.php @@ -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() { diff --git a/lib/filelib.php b/lib/filelib.php index cfc395a95e..4089f10a59 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -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 .= '&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 diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index eef7bf044a..3e25c5220e 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -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 .= '
  • '; $html .= "pixpath/f/$icon\" class=\"icon\" /> ".s($filename)." ($filesize) "; $html .= "pixpath/t/delete.gif\" class=\"iconsmall\" />";; diff --git a/lib/simpletest/testfilelib.php b/lib/simpletest/testfilelib.php index c6ec9834fc..b0416ffd3e 100644 --- a/lib/simpletest/testfilelib.php +++ b/lib/simpletest/testfilelib.php @@ -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&forcedownload=1', $fb->encodepath('http://test.url.com', '/path/to/page.php', true)); - $this->assertEqual('https://test.url.com?file=%2Fpath%2Fto%2Fpage.php&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&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&forcedownload=1', file_encode_url('http://test.url.com', '/path/to/page.php', true, true)); } } diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index ac34749d7b..c9e953b802 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -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 .= ''.$icon.''.s($filename).''; 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())); diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php index ea3d7ebeb9..bea3647740 100644 --- a/mod/assignment/type/upload/assignment.class.php +++ b/mod/assignment/type/upload/assignment.class.php @@ -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 .= ''.$icon.''.s($filename).' '; } @@ -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 .= ''.$icon.''.s($filename).''; 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 .= ''.$icon.''.$filename.''; diff --git a/mod/assignment/type/uploadsingle/assignment.class.php b/mod/assignment/type/uploadsingle/assignment.class.php index 6fc81ef3a1..70b8669ab0 100644 --- a/mod/assignment/type/uploadsingle/assignment.class.php +++ b/mod/assignment/type/uploadsingle/assignment.class.php @@ -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 .= ''.$icon.''.s($filename).'
    '; } } diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php index bb58c0c799..3e49966121 100755 --- a/mod/data/field/file/field.class.php +++ b/mod/data/field/file/field.class.php @@ -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 .= ''.$icon.''. ''.s($file->get_filename()).''; } @@ -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).'" ':' '; diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php index d40387352c..6e81556289 100755 --- a/mod/data/field/picture/field.class.php +++ b/mod/data/field/picture/field.class.php @@ -59,7 +59,7 @@ class data_field_picture extends data_field_base { //$str .= ''; 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 .= ''; } $str .= ''; @@ -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 = ''.s($alt).''; } 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 = ''.s($alt).''; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 25c14a5d10..05169b93f1 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4005,7 +4005,7 @@ function forum_print_attachments($post, $cm, $type) { $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $iconimage = ''.$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 .= "$iconimage "; diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index cf4b31ebe4..c6cb39ccbb 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -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 = ''.$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 .= "$iconimage ";