]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index d698ba81a36339c56c315ef9bbb1310d8f157f11..d5ad33a91e8c258582d0f7cc33d473af55a602e6 100644 (file)
@@ -113,10 +113,13 @@ class Ostatus_profile extends Managed_DataObject
      */
     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;
     }
 
     /**
@@ -1232,41 +1235,40 @@ class Ostatus_profile extends Managed_DataObject
      * 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;
@@ -1297,6 +1299,8 @@ class Ostatus_profile extends Managed_DataObject
         $orig = clone($this);
         $this->avatar = $url;
         $this->update($orig);
+
+        return Avatar::getUploaded($self);
     }
 
     /**