]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merge commit 'refs/merge-requests/49' of https://gitorious.org/social/mainline into...
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index cf5e3f90edbdb86eb76523ef1c0b601d94c370dc..77b1e9bef5ae179a67d8ef24bdf132c11b2de088 100644 (file)
@@ -51,12 +51,12 @@ class Ostatus_profile extends Managed_DataObject
     {
         return array(
             'fields' => array(
-                'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
+                'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
                 'profile_id' => array('type' => 'integer'),
                 'group_id' => array('type' => 'integer'),
                 'peopletag_id' => array('type' => 'integer'),
-                'feeduri' => array('type' => 'varchar', 'length' => 255),
-                'salmonuri' => array('type' => 'varchar', 'length' => 255),
+                'feeduri' => array('type' => 'varchar', 'length' => 191),
+                'salmonuri' => array('type' => 'varchar', 'length' => 191),
                 'avatar' => array('type' => 'text'),
                 'created' => array('type' => 'datetime', 'not null' => true),
                 'modified' => array('type' => 'datetime', 'not null' => true),
@@ -81,6 +81,14 @@ class Ostatus_profile extends Managed_DataObject
         return $this->uri;
     }
 
+    public function fromProfile(Profile $profile)
+    {
+        $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
+        if (!$oprofile instanceof Ostatus_profile) {
+            throw new Exception('No Ostatus_profile for Profile ID: '.$profile->id);
+        }
+    }
+
     /**
      * Fetch the locally stored profile for this feed
      * @return Profile
@@ -93,10 +101,10 @@ class Ostatus_profile extends Managed_DataObject
         }
 
         $profile = Profile::getKV('id', $this->profile_id);
-        if ($profile instanceof Profile) {
-            return $profile;
+        if (!$profile instanceof Profile) {
+            throw new NoProfileException($this->profile_id);
         }
-        throw new NoProfileException($this->profile_id);
+        return $profile;
     }
 
     /**
@@ -105,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;
     }
 
     /**
@@ -610,7 +621,7 @@ class Ostatus_profile extends Managed_DataObject
 
         // Get (safe!) HTML and text versions of the content
 
-        $rendered = $this->purify($sourceContent);
+        $rendered = common_purify($sourceContent);
         $content = common_strip_html($rendered);
 
         $shortened = common_shorten_links($content);
@@ -777,7 +788,7 @@ class Ostatus_profile extends Managed_DataObject
 
         // Get (safe!) HTML and text versions of the content
 
-        $rendered = $this->purify($sourceContent);
+        $rendered = common_purify($sourceContent);
         $content = common_strip_html($rendered);
 
         $shortened = common_shorten_links($content);
@@ -903,17 +914,6 @@ class Ostatus_profile extends Managed_DataObject
         return $saved;
     }
 
-    /**
-     * Clean up HTML
-     */
-    protected function purify($html)
-    {
-        require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
-        $config = array('safe' => 1,
-                        'deny_attribute' => 'id,style,on*');
-        return htmLawed($html, $config);
-    }
-
     /**
      * Filters a list of recipient ID URIs to just those for local delivery.
      * @param Profile local profile of sender
@@ -1120,6 +1120,11 @@ class Ostatus_profile extends Managed_DataObject
      */
     public static function ensureFeedURL($feed_url, array $hints=array())
     {
+        $oprofile = Ostatus_profile::getKV('feeduri', $feed_url);
+        if ($oprofile instanceof Ostatus_profile) {
+            return $oprofile;
+        }
+
         $discover = new FeedDiscovery();
 
         $feeduri = $discover->discoverFromFeedURL($feed_url);
@@ -1133,8 +1138,9 @@ class Ostatus_profile extends Managed_DataObject
                         ?: $discover->getAtomLink(Salmon::NS_REPLIES);
         $hints['salmon'] = $salmonuri;
 
-        if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
+        if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
             // We can only deal with folks with a PuSH hub
+            // unless we have something similar available locally.
             throw new FeedSubNoHubException();
         }
 
@@ -1222,41 +1228,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(sys_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.'), $url));
+            $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;
@@ -1287,6 +1292,8 @@ class Ostatus_profile extends Managed_DataObject
         $orig = clone($this);
         $this->avatar = $url;
         $this->update($orig);
+
+        return Avatar::getUploaded($self);
     }
 
     /**
@@ -1526,7 +1533,7 @@ class Ostatus_profile extends Managed_DataObject
             $huburi = $discover->getHubLink();
         }
 
-        if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
+        if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
             // We can only deal with folks with a PuSH hub
             throw new FeedSubNoHubException();
         }
@@ -1793,8 +1800,8 @@ class Ostatus_profile extends Managed_DataObject
         }
 
         if (!empty($location)) {
-            if (mb_strlen($location) > 255) {
-                $location = mb_substr($note, 0, 255 - 3) . ' … ';
+            if (mb_strlen($location) > 191) {   // not 255 because utf8mb4 takes more space
+                $location = mb_substr($note, 0, 191 - 3) . ' … ';
             }
         }
 
@@ -2085,13 +2092,15 @@ class Ostatus_profile extends Managed_DataObject
                                    'text/html');
 
         $filepath = File::path($filename);
+        $fileurl = File::url($filename);
 
         file_put_contents($filepath, $final);
 
         $file = new File;
 
         $file->filename = $filename;
-        $file->url      = File::url($filename);
+        $file->urlhash  = File::hashurl($fileurl);
+        $file->url      = $fileurl;
         $file->size     = filesize($filepath);
         $file->date     = time();
         $file->mimetype = 'text/html';