Skip to content

Commit

Permalink
feature(groups): group avatars now use serve-file handler
Browse files Browse the repository at this point in the history
Group avatars are now served with elgg_get_inline_url()
  • Loading branch information
hypeJunction committed Mar 15, 2016
1 parent c2b2f7f commit ac57e99
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 53 deletions.
59 changes: 16 additions & 43 deletions mod/groups/icon.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,30 @@
<?php

/**
* Icon display
*
* @package ElggGroups
* @deprecated 2.2
*/
elgg_deprecated_notice('icon.php has been depreated and should not be included. Use elgg_get_inline_url() instead.', '2.2');

if (!isset($page)) {
die('This file cannot be called directly.');
}

$group_guid = get_input('group_guid');

/* @var ElggGroup $group */
$group = get_entity($group_guid);
if (!($group instanceof ElggGroup)) {
header("HTTP/1.1 404 Not Found");
exit;
$guid = get_input('group_guid');
$size = get_input('size');
if (!isset($size) || $size === '') {
$size = 'medium';
}

// If is the same ETag, content didn't changed.
$etag = $group->icontime . $group_guid;
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"$etag\"") {
header("HTTP/1.1 304 Not Modified");
exit;
}

$size = strtolower(get_input('size'));
if (!in_array($size, array('large', 'medium', 'small', 'tiny', 'master', 'topbar')))
$size = "medium";
elgg_entity_gatekeeper($guid, 'group');

$success = false;
$group = get_entity($guid);

$filehandler = new ElggFile();
$filehandler->owner_guid = $group->owner_guid;
$filehandler->setFilename("groups/" . $group->guid . $size . ".jpg");

$success = false;
if ($filehandler->open("read")) {
if ($contents = $filehandler->read($filehandler->getSize())) {
$success = true;
}
}
$icon = new ElggFile();
$icon->owner_guid = $group->owner_guid;
$icon->setFilename("groups/{$group->guid}{$size}.jpg");

if (!$success) {
$contents = elgg_view("groups/default{$size}.gif");
header("Content-type: image/gif");
} else {
header("Content-type: image/jpeg");
$url = elgg_get_inline_url($icon, true);
if (!$url) {
$url = elgg_get_simplecache_url("groups/default{$size}.gif");
}

header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+10 days")), true);
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
header("ETag: \"$etag\"");
echo $contents;
forward($url);
36 changes: 26 additions & 10 deletions mod/groups/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,29 @@ function groups_page_handler($page) {
*
* @param array $page
* @return bool
* @deprecated 2.2
*/
function groups_icon_handler($page) {

// The username should be the file we're getting
if (isset($page[0])) {
set_input('group_guid', $page[0]);
}
if (isset($page[1])) {
set_input('size', $page[1]);
elgg_deprecated_notice('/groupicon page handler has been deprecated. Use elgg_get_inline_url() instead.', '2.2');

$guid = array_shift($page);
elgg_entity_gatekeeper($guid, 'group');

$size = array_shift($page) ? : 'medium';

$group = get_entity($guid);

$icon = new ElggFile();
$icon->owner_guid = $group->owner_guid;
$icon->setFilename("groups/{$group->guid}{$size}.jpg");

$url = elgg_get_inline_url($icon, true);
if (!$url) {
$url = elgg_get_simplecache_url("groups/default{$size}.gif");
}
// Include the standard profile index
include __DIR__ . "/icon.php";
return true;

forward($url);
}

/**
Expand Down Expand Up @@ -354,7 +364,13 @@ function groups_set_icon_url($hook, $type, $url, $params) {
}
if ($icontime) {
// return thumbnail
return "groupicon/$group->guid/$size/$icontime.jpg";
$icon = new ElggFile();
$icon->owner_guid = $group->owner_guid;
$icon->setFilename("groups/{$group->guid}{$size}.jpg");
$url = elgg_get_inline_url($icon, true); // binding to session due to complexity in group access controls
if ($url) {
return $url;
}
}

return elgg_get_simplecache_url("groups/default{$size}.gif");
Expand Down

0 comments on commit ac57e99

Please sign in to comment.