MDL-28043 user Fixed context hack in pluginfile for user pics leading to init of page, theme and output for redirect

This commit is contained in:
Sam Hemelryk 2011-09-06 11:29:05 +12:00
parent 9805fb9f17
commit 871a3ec5fb
3 changed files with 94 additions and 26 deletions

View File

@ -259,6 +259,78 @@ class user_picture implements renderable {
return $return; return $return;
} }
/**
* Works out the URL for the users picture.
*
* This method is recommended as it avoids costly redirects of user pictures
* if requests are made for non-existent files etc.
*
* @param renderer_base $renderer
* @return moodle_url
*/
public function get_url(moodle_page $page, renderer_base $renderer = null) {
global $CFG;
if (is_null($renderer)) {
$renderer = $page->get_renderer('core');
}
if (!empty($CFG->forcelogin) and !isloggedin()) {
// protect images if login required and not logged in;
// do not use require_login() because it is expensive and not suitable here anyway
return $renderer->pix_url('u/f1');
}
// Sort out the filename and size. Size is only required for the gravatar
// implementation presently.
if (empty($this->size)) {
$filename = 'f2';
$size = 35;
} else if ($this->size === true or $this->size == 1) {
$filename = 'f1';
$size = 100;
} else if ($this->size >= 50) {
$filename = 'f1';
$size = (int)$this->size;
} else {
$filename = 'f2';
$size = (int)$this->size;
}
if ($this->user->picture == 1) {
// The user has uploaded their own profile pic. In this case we will
// check that a profile pic file does actually exist
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_USER, $this->user->id);
if (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
if (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
return $renderer->pix_url('u/'.$filename);
}
}
$path = '/';
if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) {
// We append the theme name to the file path if we have it so that
// in the circumstance that the profile picture is not available
// when the user actually requests it they still get the profile
// picture for the correct theme.
$path .= $page->theme->name.'/';
}
return moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, $path, $filename);
} else if ($this->user->picture == 2) {
// This is just VERY basic support for gravatar to give the actual
// implementor a headstart in what to do.
if ($size < 1 || $size > 500) {
$size = 35;
}
$md5 = md5(strtolower(trim($this->user->email)));
$default = urlencode($this->pix_url('u/'.$filename)->out(false));
return "http://www.gravatar.com/avatar/{$md5}?s={$size}&d={$default}";
}
return $renderer->pix_url('u/'.$filename);
}
} }
/** /**

View File

@ -1801,33 +1801,21 @@ class core_renderer extends renderer_base {
} }
if (empty($userpicture->size)) { if (empty($userpicture->size)) {
$file = 'f2';
$size = 35; $size = 35;
} else if ($userpicture->size === true or $userpicture->size == 1) { } else if ($userpicture->size === true or $userpicture->size == 1) {
$file = 'f1';
$size = 100; $size = 100;
} else if ($userpicture->size >= 50) {
$file = 'f1';
$size = $userpicture->size;
} else { } else {
$file = 'f2';
$size = $userpicture->size; $size = $userpicture->size;
} }
$class = $userpicture->class; $class = $userpicture->class;
if ($user->picture == 1) { if ($user->picture != 1 && $user->picture != 2) {
$usercontext = get_context_instance(CONTEXT_USER, $user->id);
$src = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', $file);
} else if ($user->picture == 2) {
//TODO: gravatar user icon support
} else { // Print default user pictures (use theme version if available)
$class .= ' defaultuserpic'; $class .= ' defaultuserpic';
$src = $this->pix_url('u/' . $file);
} }
$src = $userpicture->get_url($this->page, $this);
$attributes = array('src'=>$src, 'alt'=>$alt, 'title'=>$alt, 'class'=>$class, 'width'=>$size, 'height'=>$size); $attributes = array('src'=>$src, 'alt'=>$alt, 'title'=>$alt, 'class'=>$class, 'width'=>$size, 'height'=>$size);
// get the image html output fisrt // get the image html output fisrt

View File

@ -279,24 +279,32 @@ if ($component === 'blog') {
// ======================================================================================================================== // ========================================================================================================================
} else if ($component === 'user') { } else if ($component === 'user') {
if ($filearea === 'icon' and $context->contextlevel == CONTEXT_USER) { if ($filearea === 'icon' and $context->contextlevel == CONTEXT_USER) {
// XXX: pix_url will initialize $PAGE, so we have to set up context here $redirect = false;
// this temp hack should be fixed by better solution if (count($args) == 1) {
$PAGE->set_context(get_system_context()); $themename = theme_config::DEFAULT_THEME;
if (!empty($CFG->forcelogin) and !isloggedin()) { $filename = array_shift($args);
} else {
$themename = array_shift($args);
$filename = array_shift($args);
}
if ((!empty($CFG->forcelogin) and !isloggedin())) {
// protect images if login required and not logged in; // protect images if login required and not logged in;
// do not use require_login() because it is expensive and not suitable here anyway // do not use require_login() because it is expensive and not suitable here anyway
redirect($OUTPUT->pix_url('u/f1')); $redirect = true;
} }
$filename = array_pop($args); if (!$redirect and ($filename !== 'f1' and $filename !== 'f2')) {
if ($filename !== 'f1' and $filename !== 'f2') { $filename = 'f1';
redirect($OUTPUT->pix_url('u/f1')); $redirect = true;
} }
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) { if (!$redirect && !$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) { if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
redirect($OUTPUT->pix_url('u/'.$filename)); $redirect = true;
} }
} }
if ($redirect) {
$theme = theme_config::load($themename);
redirect($theme->pix_url('u/'.$filename, 'moodle'));
}
send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page
} else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) { } else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) {