Skip to content

Commit

Permalink
MDL-14279: use get_file_url instead of checking $CFG->slashargument m…
Browse files Browse the repository at this point in the history
…anually (merge from 1.9)
  • Loading branch information
scyrma committed Jul 10, 2008
1 parent 83e6cf2 commit 5a254a2
Show file tree
Hide file tree
Showing 30 changed files with 104 additions and 209 deletions.
13 changes: 2 additions & 11 deletions backup/restorelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3636,6 +3636,7 @@ function restore_create_events($restore,$xml_file) {
//finished to know all the oldid, newid equivaleces
function restore_decode_absolute_links($content) {
global $CFG, $restore;
require_once($CFG->libdir.'/filelib.php');

/// MDL-14072: Prevent NULLs, empties and numbers to be processed by the
/// heavy interlinking. Just a few cpu cycles saved.
Expand All @@ -3649,17 +3650,7 @@ function restore_decode_absolute_links($content) {

//Now decode wwwroot and file.php calls
$search = array ("$@FILEPHP@$");

//Check for the status of the slasharguments config variable
$slash = $CFG->slasharguments;

//Build the replace string as needed
if ($slash == 1) {
$replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
} else {
$replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
}

$replace = array(get_file_url($restore->course_id));
$result = str_replace($search,$replace,$content);

if ($result != $content && debugging()) { //Debug
Expand Down
6 changes: 1 addition & 5 deletions blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,7 @@ function blog_print_attachments($blogentry, $return=NULL) {
include_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo("icon", $file);
$type = mimeinfo("type", $file);
if ($CFG->slasharguments) {
$ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
} else {
$ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
}
$ffurl = get_file_url("$filearea/$file");
$image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";

if ($return == "html") {
Expand Down
7 changes: 2 additions & 5 deletions blog/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext=
$path .= '/rss.xml';
$rsspix = $CFG->pixpath .'/i/rss.gif';

if ($CFG->slasharguments) {
$path = $CFG->wwwroot.'/rss/file.php/'.$path;
} else {
$path = $CFG->wwwroot.'/rss/file.php?file='.$path;
}
require_once($CFG->libdir.'/filelib.php');
$path = get_file_url($path, null, 'rssfile');
print '<div align="right"><a href="'. $path .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';

}
Expand Down
4 changes: 4 additions & 0 deletions file.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php // $Id$
// This script fetches files from the dataroot directory
//
// You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
// This ensures proper formatting and offers useful options.
//
// Syntax: file.php/courseid/dir/dir/dir/filename.ext
// file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
// file.php/courseid/dir (returns index.html from dir)
Expand Down
7 changes: 2 additions & 5 deletions files/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,8 @@ function displaydir ($wdir) {

print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
echo "<td align=\"left\" style=\"white-space:nowrap\" class=\"name\">";
if ($CFG->slasharguments) {
$ffurl = str_replace('//', '/', "/file.php/$id/$fileurl");
} else {
$ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
}

$ffurl = str_replace('//', '/', get_file_url("$id/$fileurl"));
link_to_popup_window ($ffurl, "display",
"<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
480, 640);
Expand Down
8 changes: 2 additions & 6 deletions lib/editor/htmlarea/coursefiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,13 @@ function displaydir ($wdir) {
print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" onclick=\";return set_rename('$file');\" />");
}
echo "<td align=\"left\" nowrap=\"nowrap\">";
if ($CFG->slasharguments) {
$ffurl = "/file.php/$id$fileurl";
} else {
$ffurl = "/file.php?file=/$id$fileurl";
}
$ffurl = get_file_url($id$fileurl);
link_to_popup_window ($ffurl, "display",
"<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />",
480, 640);
$file_size = filesize($filename);

echo "<a onclick=\"return set_value(info = {url: '".$CFG->wwwroot.$ffurl."',";
echo "<a onclick=\"return set_value(info = {url: '".$ffurl."',";
echo " isize: '".$file_size."', itype: '".$imgtype."', iwidth: '".$imgwidth."',";
echo " iheight: '".$imgheight."', imodified: '".$filedate."' })\" href=\"#\">$file</a>";
echo "</td>\n";
Expand Down
25 changes: 20 additions & 5 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@ function get_file_url($path, $options=null, $type='coursefile') {

// type of file
switch ($type) {
case 'coursefile':
case 'questionfile':
$url = $CFG->wwwroot."/question/exportfile.php";
break;
case 'rssfile':
$url = $CFG->wwwroot."/rss/file.php";
break;
case 'user':
$url = $CFG->wwwroot."/user/pix.php";
break;
case 'usergroup':
$url = $CFG->wwwroot."/user/pixgroup.php";
break;
case 'httpscoursefile':
$url = $CFG->httpswwwroot."/file.php";
break;
case 'coursefile':
default:
$url = "$CFG->wwwroot/file.php";
$url = $CFG->wwwroot."/file.php";
}

if ($CFG->slasharguments) {
$parts = explode('/', $path);
$parts = array_map('rawurlencode', $parts);
$path = implode('/', $parts);
$ffurl = "$CFG->wwwroot/file.php/$path";
$ffurl = $url.'/'.$path;
$separator = '?';
} else {
$path = rawurlencode("/$path");
$ffurl = "$CFG->wwwroot/file.php?file=$path";
$path = rawurlencode('/'.$path);
$ffurl = $url.'?file='.$path;
$separator = '&amp;';
}

Expand Down
16 changes: 5 additions & 11 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1558,12 +1558,10 @@ function get_question_image($question) {
if (substr(strtolower($question->image), 0, 7) == 'http://') {
$img .= $question->image;

} else if ($CFG->slasharguments) { // Use this method if possible for better caching
$img .= "$CFG->wwwroot/file.php/$coursefilesdir/$question->image";

} else {
$img .= "$CFG->wwwroot/file.php?file=/$coursefilesdir/$question->image";
}
require_once($CFG->libdir .'/filelib.php');
$img = get_file_url("$courseid/{$question->image}");
}
}
return $img;
}
Expand Down Expand Up @@ -2363,12 +2361,8 @@ function question_url_check($url){
*/
function question_replace_file_links_in_html($html, $fromcourseid, $tocourseid, $url, $destination, &$changed){
global $CFG;
if ($CFG->slasharguments) { // Use this method if possible for better caching
$tourl = "$CFG->wwwroot/file.php/$tocourseid/$destination";

} else {
$tourl = "$CFG->wwwroot/file.php?file=/$tocourseid/$destination";
}
require_once($CFG->libdir .'/filelib.php');
$tourl = get_file_url("$tocourseid/$destination");
$fromurl = question_file_links_base_url($fromcourseid).preg_quote($url, '!');
$searchfor = array('!(<\s*(a|img)\s[^>]*(href|src)\s*=\s*")'.$fromurl.'(")!i',
'!(<\s*(a|img)\s[^>]*(href|src)\s*=\s*\')'.$fromurl.'(\')!i');
Expand Down
8 changes: 2 additions & 6 deletions lib/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ function rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext='') {
*/
function rss_get_url($courseid, $userid, $modulename, $id) {
global $CFG;
if ($CFG->slasharguments) {
$rsspath = "$CFG->wwwroot/rss/file.php/$courseid/$userid/$modulename/$id/rss.xml";
} else {
$rsspath = "$CFG->wwwroot/rss/file.php?file=/$courseid/$userid/$modulename/$id/rss.xml";
}
return $rsspath;
require_once($CFG->libdir.'/filelib.php');
return get_file_url($courseid.'/'.$userid.'/'.$modulename.'/'.$id.'/rss.xml', null, 'rssfile');
}

/**
Expand Down
25 changes: 8 additions & 17 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4226,11 +4226,8 @@ function print_file_picture($path, $courseid=0, $height='', $width='', $link='',

} else if ($courseid) {
$output .= '<img style="height:'.$height.'px;width:'.$width.'px;" src="';
if ($CFG->slasharguments) { // Use this method if possible for better caching
$output .= $CFG->wwwroot .'/file.php/'. $courseid .'/'. $path;
} else {
$output .= $CFG->wwwroot .'/file.php?file=/'. $courseid .'/'. $path;
}
require_once($CFG->libdir.'/filelib.php');
$output .= get_file_url("$courseid/$path");
$output .= '" />';
} else {
$output .= 'Error: must pass URL or course';
Expand Down Expand Up @@ -4330,11 +4327,8 @@ function print_user_picture($user, $courseid, $picture=NULL, $size=0, $return=fa
}

if ($picture) { // Print custom user picture
if ($CFG->slasharguments) { // Use this method if possible for better caching
$src = $wwwroot .'/user/pix.php/'. $user->id .'/'. $file .'.jpg';
} else {
$src = $wwwroot .'/user/pix.php?file=/'. $user->id .'/'. $file .'.jpg';
}
require_once($CFG->libdir.'/filelib.php');
$src = get_file_url($user->id.'/'.$file.'.jpg', null, 'user');
} else { // Print default user pictures (use theme version if available)
$class .= " defaultuserpic";
$src = "$CFG->pixpath/u/$file.png";
Expand Down Expand Up @@ -4536,13 +4530,10 @@ function print_group_picture($group, $courseid, $large=false, $return=false, $li
$size = 35;
}
if ($group->picture) { // Print custom group picture
if ($CFG->slasharguments) { // Use this method if possible for better caching
$output .= '<img class="grouppicture" src="'.$CFG->wwwroot.'/user/pixgroup.php/'.$group->id.'/'.$file.'.jpg"'.
' style="width:'.$size.'px;height:'.$size.'px;" alt="'.s(get_string('group').' '.$group->name).'" title="'.s($group->name).'"/>';
} else {
$output .= '<img class="grouppicture" src="'.$CFG->wwwroot.'/user/pixgroup.php?file=/'.$group->id.'/'.$file.'.jpg"'.
' style="width:'.$size.'px;height:'.$size.'px;" alt="'.s(get_string('group').' '.$group->name).'" title="'.s($group->name).'"/>';
}
require_once($CFG->libdir.'/filelib.php');
$grouppictureurl = get_file_url($group->id.'/'.$file.'.jpg', null, 'usergroup');
$output .= '<img class="grouppicture" src="'.$grouppictureurl.'"'.
' style="width:'.$size.'px;height:'.$size.'px;" alt="'.s(get_string('group').' '.$group->name).'" title="'.s($group->name).'"/>';
}
if ($link or has_capability('moodle/site:accessallgroups', $context)) {
$output .= '</a>';
Expand Down
22 changes: 7 additions & 15 deletions lib/wiki_to_markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,16 @@ function line_replace( $line ) {
$line = eregi_replace( " ([a-zA-Z]+):([0-9]+)\(([^)]+)\)",
" [\\3](".$CFG->wwwroot."/mod/\\1/view.php?id=\\2) ", $line );

require_once($CFG->libdir.'/filelib.php');
$coursefileurl = get_file_url($this->courseid);

// Replace picture resource link
if ($CFG->slasharguments) {
$line = eregi_replace( "/([a-zA-Z0-9./_-]+)(png|gif|jpg)\(([^)]+)\)",
"![\\3]($CFG->wwwroot/file.php/$this->courseid/\\1\\2)", $line );
} else {
$line = eregi_replace( "/([a-zA-Z0-9./_-]+)(png|gif|jpg)\(([^)]+)\)",
"![\\3]($CFG->wwwroot/file.php?file=/$this->courseid/\\1\\2)", $line );
}
$line = eregi_replace( "/([a-zA-Z0-9./_-]+)(png|gif|jpg)\(([^)]+)\)",
"![\\3](".$coursefileurl."/\\1\\2)", $line );

// Replace file resource link
if ($CFG->slasharguments) {
$line = eregi_replace( "file:/([[:alnum:]/._-]+)\(([^)]+)\)",
"[\\2]($CFG->wwwroot/file.php/$this->courseid/\\1)", $line );
} else {
$line = eregi_replace( "file:/([[:alnum:]/._-]+)\(([^)]+)\)",
"[\\2]($CFG->wwwroot/file.php?file=/$this->courseid/\\1)", $line );
}

$line = eregi_replace( "file:/([[:alnum:]/._-]+)\(([^)]+)\)",
"[\\2](".$coursefileurl."/\\1)", $line );

return $line;
}
Expand Down
8 changes: 2 additions & 6 deletions mod/data/field/file/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ function display_add_field($recordid=0) {
$src = empty($contents[0]) ? '' : $contents[0];
$name = empty($contents[1]) ? $src : $contents[1];
$displayname = empty($contents[1]) ? '' : $contents[1];
$path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
if ($CFG->slasharguments) {
$source = $CFG->wwwroot.'/file.php/'.$path;
} else {
$source = $CFG->wwwroot.'/file.php?file=/'.$path;
}
require_once($CFG->libdir.'/filelib.php');
$source = get_file_url($this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid);
} else {
$src = '';
$name = '';
Expand Down
20 changes: 7 additions & 13 deletions mod/data/field/picture/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ function display_add_field($recordid=0) {
$description = $content->content1;
}
$path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
if ($CFG->slasharguments) {
$filepath = $CFG->wwwroot.'/file.php/'.$path.'/'.$filename;
} else {
$filepath = $CFG->wwwroot.'/file.php?file=/'.$path.'/'.$filename;
}
require_once($CFG->libdir.'/filelib.php');
$filepath = get_file_url("$path/$filename");
}
$str = '<div title="'.s($this->field->description).'">';
$str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
Expand Down Expand Up @@ -102,14 +99,11 @@ function display_browse_field($recordid, $template) {
$title = empty($contents[1])? '':$contents[1];
$src = $contents[0];
$path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
$thumbnaillocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid.'/thumb/'.$src;
if ($CFG->slasharguments) {
$source = $CFG->wwwroot.'/file.php/'.$path.'/'.$src;
$thumbnailsource = file_exists($thumbnaillocation) ? $CFG->wwwroot.'/file.php/'.$path.'/thumb/'.$src : $source;
} else {
$source = $CFG->wwwroot.'/file.php?file=/'.$path.'/'.$src;
$thumbnailsource = file_exists($thumbnaillocation) ? $CFG->wwwroot.'/file.php?file=/'.$path.'/thumb/'.$src : $source;
}

$thumbnaillocation = $CFG->dataroot .'/'. $path .'/thumb/'.$src;
require_once($CFG->libdir.'/filelib.php');
$source = get_file_url("$path/$src");
$thumbnailsource = file_exists($thumbnaillocation) ? get_file_url("$path/thumb/$src") : $source;

if ($template == 'listtemplate') {
$width = $this->field->param4 ? ' width="'.s($this->field->param4).'" ' : ' ';
Expand Down
3 changes: 2 additions & 1 deletion mod/data/preset_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ function action_export($params) {
print_error('movezipfailed', 'data');
}

$html .= '<a href="' . $CFG->wwwroot . '/file.php/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip">'.get_string('download', 'data')."</a>";
require_once($CFG->libdir.'/filelib.php');
$html .= '<a href="'. get_file_url($course->id .'/moddata/data/'. $this->data->id .'/preset.zip') .'">'. get_string('download', 'data') .'</a>';
$html .= '</div>';
return $html;
}
Expand Down
6 changes: 1 addition & 5 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3716,11 +3716,7 @@ function forum_print_attachments($post, $return=NULL) {
foreach ($files as $file) {
$icon = mimeinfo("icon", $file);
$type = mimeinfo("type", $file);
if ($CFG->slasharguments) {
$ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
} else {
$ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
}
$ffurl = get_file_url("$filearea/$file");
$image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";

if ($return == "html") {
Expand Down
8 changes: 3 additions & 5 deletions mod/forum/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ function forum_rss_feed_posts($forum, $newsince=0) {
$formatoptions = new object;
$formatoptions->trusttext = true;

require_once($CFG->libdir.'/filelib.php');

foreach ($recs as $rec) {
unset($item);
unset($user);
Expand All @@ -257,11 +259,7 @@ function forum_rss_feed_posts($forum, $newsince=0) {
$item->attachments = array();
foreach ($post_files as $file) {
$attachment = new stdClass;
if ($CFG->slasharguments) {
$attachment->url = "{$CFG->wwwroot}/file.php/$post_file_area_name/$file";
} else {
$attachment->url = "{$CFG->wwwroot}/file.php?file=/$post_file_area_name/$file";
}
$attachment->url = get_file_url($post_file_area_name.'/'.$file);
$attachment->length = filesize("$CFG->dataroot/$post_file_area_name/$file");
$item->attachments[] = $attachment;
}
Expand Down
6 changes: 1 addition & 5 deletions mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,7 @@ function glossary_print_attachments($entry, $return=NULL, $align="left") {
$strattachment = get_string("attachment", "glossary");
foreach ($files as $file) {
$icon = mimeinfo("icon", $file);
if ($CFG->slasharguments) {
$ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
} else {
$ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
}
$ffurl = get_file_url("$filearea/$file");
$image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";

if ($return == "html") {
Expand Down
7 changes: 2 additions & 5 deletions mod/hotpot/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1803,11 +1803,8 @@ function get_baseurl() {
// set the url base (first time only)
if (!isset($this->baseurl)) {
global $CFG;
if ($CFG->slasharguments) {
$this->baseurl = "$CFG->wwwroot/file.php/$this->filedir/";
} else {
$this->baseurl = "$CFG->wwwroot/file.php?file=/$this->filedir/";
}
require_once($CFG->libdir.'/filelib.php');
$this->baseurl = get_file_url($this->filedir .'/');
}
return $this->baseurl;
}
Expand Down
7 changes: 2 additions & 5 deletions mod/lesson/importppt.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,8 @@ function extract_data($pages, $courseid, $lessonname, $modname) {

$imagedir = $CFG->dataroot.'/'.$courseid.'/moddata/'.$modname;

if ($CFG->slasharguments) {
$imagelink = $CFG->wwwroot.'/file.php/'.$courseid.'/moddata/'.$modname;
} else {
$imagelink = $CFG->wwwroot.'/file.php?file=/'.$courseid.'/moddata/'.$modname;
}
require_once($CFG->libdir .'/filelib.php');
$imagelink = get_file_url($courseid.'/moddata/'.$modname);

// try to make a unique subfolder to store the images
$lessonname = str_replace(' ', '_', $lessonname); // get rid of spaces
Expand Down
Loading

0 comments on commit 5a254a2

Please sign in to comment.