Skip to content

Commit

Permalink
MDL-14589 file browsing refactoring - moving url encoding to filelib.…
Browse files Browse the repository at this point in the history
…php for now
  • Loading branch information
skodak committed May 20, 2009
1 parent c05e975 commit 4eef139
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 51 deletions.
2 changes: 1 addition & 1 deletion blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function blog_print_attachments($blogentry, $return=NULL) {
}

$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);
Expand Down
2 changes: 1 addition & 1 deletion files/draftfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<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>";;
Expand Down
26 changes: 0 additions & 26 deletions lib/file/file_browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,6 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
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
Expand Down
2 changes: 1 addition & 1 deletion lib/file/file_info_coursefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function get_url($forcedownload=false, $https=false) {

$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() {
Expand Down
2 changes: 1 addition & 1 deletion lib/file/file_info_stored.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function get_url($forcedownload=false, $https=false) {
} 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() {
Expand Down
36 changes: 36 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/form/filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function _get_draftfiles($draftid, $suffix) {
$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>";;
Expand Down
16 changes: 8 additions & 8 deletions lib/simpletest/testfilelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,18 @@ public function test_encodepath() {
$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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ function print_user_files($userid=0, $return=false) {
$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()));
Expand Down
6 changes: 3 additions & 3 deletions mod/assignment/type/upload/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function print_student_answer($userid, $return=false){
$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;';

}
Expand Down Expand Up @@ -347,7 +347,7 @@ function print_user_files($userid=0, $return=false) {
$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) {
Expand Down Expand Up @@ -409,7 +409,7 @@ function print_responsefiles($userid, $return=false) {
$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>';

Expand Down
2 changes: 1 addition & 1 deletion mod/assignment/type/uploadsingle/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function print_student_answer($userid, $return=false){
$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 />';
}
}
Expand Down
4 changes: 2 additions & 2 deletions mod/data/field/file/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function display_add_field($recordid=0) {
// 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>';
}
Expand Down Expand Up @@ -119,7 +119,7 @@ function display_browse_field($recordid, $template) {

$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).'" ':' ';

Expand Down
6 changes: 3 additions & 3 deletions mod/data/field/picture/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function display_add_field($recordid=0) {
//$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>';
Expand Down Expand Up @@ -121,12 +121,12 @@ function display_browse_field($recordid, $template) {
$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>';
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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> ";
Expand Down
2 changes: 1 addition & 1 deletion mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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> ";
Expand Down

0 comments on commit 4eef139

Please sign in to comment.