*/
public function localGroup()
{
- if ($this->group_id) {
- return User_group::getKV('id', $this->group_id);
+ $group = User_group::getKV('id', $this->group_id);
+
+ if (!$group instanceof User_group) {
+ throw new NoSuchGroupException(array('id'=>$this->group_id));
}
- return null;
+
+ return $group;
}
/**
* Download and update given avatar image
*
* @param string $url
+ * @return Avatar The Avatar we have on disk. (seldom used)
* @throws Exception in various failure cases
*/
- protected function updateAvatar($url)
+ public function updateAvatar($url, $force=false)
{
- if ($url == $this->avatar) {
- // We've already got this one.
- return;
+ try {
+ // If avatar URL differs: update. If URLs were identical but we're forced: update.
+ if ($url == $this->avatar && !$force) {
+ // If there's no locally stored avatar, throw an exception and continue fetching below.
+ $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar;
+ return $avatar;
+ }
+ } catch (NoAvatarException $e) {
+ // No avatar available, let's fetch it.
}
+
if (!common_valid_http_url($url)) {
// TRANS: Server exception. %s is a URL.
throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
}
- if ($this->isGroup()) {
- // FIXME: throw exception for localGroup
- $self = $this->localGroup();
- } else {
- // this throws an exception already
- $self = $this->localProfile();
- }
- if (!$self) {
- throw new ServerException(sprintf(
- // TRANS: Server exception. %s is a URI.
- _m('Tried to update avatar for unsaved remote profile %s.'),
- $this->getUri()));
- }
+ $self = $this->localProfile();
// @todo FIXME: This should be better encapsulated
// ripped from oauthstore.php (for old OMB client)
$temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
try {
- if (!copy($url, $temp_filename)) {
- // TRANS: Server exception. %s is a URL.
- throw new ServerException(sprintf(_m('Unable to fetch avatar from %s to %s.'), $url, $temp_filename));
+ $imgData = HTTPClient::quickGet($url);
+ // Make sure it's at least an image file. ImageFile can do the rest.
+ if (false === getimagesizefromstring($imgData)) {
+ throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
}
+ file_put_contents($temp_filename, $imgData);
+ unset($imgData); // No need to carry this in memory.
if ($this->isGroup()) {
$id = $this->group_id;
$orig = clone($this);
$this->avatar = $url;
$this->update($orig);
+
+ return Avatar::getUploaded($self);
}
/**