Permalink
Browse files

feature(avatar): user avatars are now served by serve-file handler

Avatars are now served using elgg_get_inline_url() via serve-file handler.
Profile plugin no longer overwrites user avatar URLs and avatar handlers.
  • Loading branch information...
hypeJunction committed Mar 15, 2016
1 parent 769cb80 commit a55d746acd2b81c0bb36e32b147ebc97b6ef5cd6
Showing with 76 additions and 131 deletions.
  1. +22 −6 engine/lib/users.php
  2. +20 −43 mod/profile/icondirect.php
  3. +2 −32 mod/profile/start.php
  4. +32 −50 views/default/resources/avatar/view.php
View
@@ -445,14 +445,30 @@ function user_create_hook_add_site_relationship($event, $object_type, $object) {
* @access private
*/
function user_avatar_hook($hook, $entity_type, $returnvalue, $params) {
$user = $params['entity'];
$size = $params['size'];
$user = elgg_extract('entity', $params);
$size = elgg_extract('size', $params, 'medium');
if (isset($user->icontime)) {
if (!$user instanceof ElggUser) {
return;
}
$default_url = elgg_get_simplecache_url("icons/user/default{$size}.gif");
if (!isset($user->icontime)) {
return $default_url;
}
if (_elgg_view_may_be_altered('resources/avatar/view', 'resources/avatar/view.php')) {
// For BC with 2.0 if a plugin is suspected of using this view/page handler we need to use it.
// /avatar page handler will issue a deprecation notice.
return "avatar/view/$user->username/$size/$user->icontime";
} else {
return elgg_get_simplecache_url("icons/user/default{$size}.gif");
}
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->guid;
$filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
$avatar_url = elgg_get_inline_url($filehandler);
return $avatar_url ? : $default_url;
}
/**
@@ -644,7 +660,6 @@ function elgg_profile_fields_setup() {
* Avatar page handler
*
* /avatar/edit/<username>
* /avatar/view/<username>/<size>
*
* @param array $page
* @return bool
@@ -659,6 +674,7 @@ function elgg_avatar_page_handler($page) {
if ($page[0] == 'edit') {
echo elgg_view_resource("avatar/edit");
} else {
elgg_deprecated_notice("/avatar/view page handler has been deprecated and will be removed. Use elgg_get_inline_url() instead.", '2.2');
echo elgg_view_resource("avatar/view", [
'size' => elgg_extract(2, $page),
]);
View
@@ -4,58 +4,35 @@
*
*
* @package ElggProfile
* @deprecated 2.2
*/
// won't be able to serve anything if no guid
if (!isset($_GET['guid'])) {
header("HTTP/1.1 404 Not Found");
exit;
$autoload_root = dirname(dirname(__DIR__));
if (!is_file("$autoload_root/vendor/autoload.php")) {
$autoload_root = dirname(dirname(dirname($autoload_root)));
}
require_once "$autoload_root/vendor/autoload.php";
$last_cache = empty($_GET['lastcache']) ? 0 : (int)$_GET['lastcache']; // icontime
$guid = (int)$_GET['guid'];
\Elgg\Application::start();
// If is the same ETag, content didn't changed.
$etag = $last_cache . $guid;
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"$etag\"") {
header("HTTP/1.1 304 Not Modified");
exit;
}
elgg_deprecated_notice("icondirect.php is no longer used and will be removed, do not include and require it. Use elgg_get_inline_url() instead.", '2.2');
$size = "medium";
if (!empty($_GET['size'])) {
$size = strtolower($_GET['size']);
if (!in_array($size, array('large', 'medium', 'small', 'tiny', 'master', 'topbar'))) {
$size = "medium";
}
}
$guid = get_input('guid');
$size = get_input('size') ? : 'medium';
elgg_entity_gatekeeper($guid, 'user');
$autoload_root = dirname(dirname(__DIR__));
if (!is_file("$autoload_root/vendor/autoload.php")) {
$autoload_root = dirname(dirname(dirname($autoload_root)));
$user = get_entity($guid);
if ($user) {
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->guid;
$filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
$avatar_url = elgg_get_inline_url($filehandler);
}
require_once "$autoload_root/vendor/autoload.php";
$data_root = \Elgg\Application::getDataPath();
$locator = new \Elgg\EntityDirLocator($guid);
$user_path = $data_root . $locator->getPath();
$filename = $user_path . "profile/{$guid}{$size}.jpg";
$filesize = @filesize($filename);
if ($filesize) {
header("Content-type: image/jpeg");
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: $filesize");
header("ETag: \"$etag\"");
readfile($filename);
exit;
if (!$avatar_url) {
$avatar_url = elgg_get_simplecache_url("icons/user/default{$size}.gif");
}
// something went wrong so load engine and try to forward to default icon
\Elgg\Application::start();
elgg_log("Profile icon direct failed.", "WARNING");
forward(elgg_get_simplecache_url("icons/user/default{$size}.gif"));
forward($avatar_url);
View
@@ -19,10 +19,6 @@ function profile_init() {
// Register a URL handler for users
elgg_register_plugin_hook_handler('entity:url', 'user', 'profile_set_url');
elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'profile_set_icon_url');
elgg_unregister_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
elgg_register_simplecache_view('icon/user/default/tiny');
elgg_register_simplecache_view('icon/user/default/topbar');
elgg_register_simplecache_view('icon/user/default/small');
@@ -105,36 +101,10 @@ function profile_set_url($hook, $type, $url, $params) {
* @param string $url
* @param array $params
* @return string
* @deprecated 2.2
*/
function profile_set_icon_url($hook, $type, $url, $params) {
// if someone already set this, quit
if ($url) {
return;
}
$user = $params['entity'];
$size = $params['size'];
$user_guid = $user->getGUID();
$icon_time = $user->icontime;
if (!$icon_time) {
return elgg_get_simplecache_url("icons/user/default{$size}.gif");
}
$filehandler = new ElggFile();
$filehandler->owner_guid = $user_guid;
$filehandler->setFilename("profile/{$user_guid}{$size}.jpg");
try {
if ($filehandler->exists()) {
return "mod/profile/icondirect.php?lastcache=$icon_time&guid=$user_guid&size=$size";
}
} catch (InvalidParameterException $e) {
elgg_log("Unable to get profile icon for user with GUID $user_guid", 'ERROR');
return elgg_get_simplecache_url("icons/default/$size.png");
}
elgg_deprecated_notice("Profile plugin no longer customizes avatar url using 'entity:icon:url' hook", '2.2');
}
/**
@@ -1,50 +1,32 @@
<?php
/**
* View an avatar
*/
// page owner library sets this based on URL
$user = elgg_get_page_owner_entity();
// Get the size
$size = strtolower(elgg_extract('size', $vars));
if (!in_array($size, array('master', 'large', 'medium', 'small', 'tiny', 'topbar'))) {
$size = 'medium';
}
// If user doesn't exist, return default icon
if (!$user) {
forward(elgg_get_simplecache_url("icons/default/$size.png"));
}
$user_guid = $user->getGUID();
// Try and get the icon
$filehandler = new ElggFile();
$filehandler->owner_guid = $user_guid;
$filehandler->setFilename("profile/{$user_guid}{$size}.jpg");
$success = false;
try {
if ($filehandler->open("read")) {
$contents = $filehandler->read($filehandler->getSize());
if ($contents) {
$success = true;
}
}
} catch (InvalidParameterException $e) {
elgg_log("Unable to get avatar for user with GUID $user_guid", 'ERROR');
}
if (!$success) {
forward(elgg_get_simplecache_url("icons/user/default{$size}.gif"));
}
header("Content-type: image/jpeg", true);
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
header("Pragma: public", true);
header("Cache-Control: public", true);
header("Content-Length: " . strlen($contents));
echo $contents;
<?php
/**
* View an avatar
* @deprecated 2.2
*/
elgg_deprecated_notice("/avatar/view resource view has been deprecated and will be removed. Use elgg_get_inline_url() instead.", '2.2');
// page owner library sets this based on URL
$user = elgg_get_page_owner_entity();
// Get the size
$size = strtolower(elgg_extract('size', $vars));
if (!in_array($size, array('master', 'large', 'medium', 'small', 'tiny', 'topbar'))) {
$size = 'medium';
}
$avatar_url = false;
if ($user) {
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->guid;
$filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
$avatar_url = elgg_get_inline_url($filehandler);
}
if (!$avatar_url) {
$avatar_url = elgg_get_simplecache_url("icons/user/default{$size}.gif");
}
forward($avatar_url);

0 comments on commit a55d746

Please sign in to comment.