]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index 402d1967f9a3b612935507860bc5fa78795d1369..1089f6a21fd1b42ac797b262d0a2b9718a09fb49 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+if (!defined('STATUSNET')) {
+    exit(1);
+}
+
 /**
  * @package OStatusPlugin
  * @maintainer Brion Vibber <brion@status.net>
  */
-class Ostatus_profile extends Memcached_DataObject
+
+class Ostatus_profile extends Managed_DataObject
 {
     public $__table = 'ostatus_profile';
 
@@ -43,74 +48,35 @@ class Ostatus_profile extends Memcached_DataObject
     }
 
     /**
-     * return table definition for DB_DataObject
-     *
-     * DB_DataObject needs to know something about the table to manipulate
-     * instances. This method provides all the DB_DataObject needs to know.
+     * Return table definition for Schema setup and DB_DataObject usage.
      *
      * @return array array of column definitions
      */
-    function table()
-    {
-        return array('uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
-                     'profile_id' => DB_DATAOBJECT_INT,
-                     'group_id' => DB_DATAOBJECT_INT,
-                     'feeduri' => DB_DATAOBJECT_STR,
-                     'salmonuri' =>  DB_DATAOBJECT_STR,
-                     'avatar' =>  DB_DATAOBJECT_STR,
-                     'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
-                     'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
-    }
 
     static function schemaDef()
     {
-        return array(new ColumnDef('uri', 'varchar',
-                                   255, false, 'PRI'),
-                     new ColumnDef('profile_id', 'integer',
-                                   null, true, 'UNI'),
-                     new ColumnDef('group_id', 'integer',
-                                   null, true, 'UNI'),
-                     new ColumnDef('feeduri', 'varchar',
-                                   255, true, 'UNI'),
-                     new ColumnDef('salmonuri', 'text',
-                                   null, true),
-                     new ColumnDef('avatar', 'text',
-                                   null, true),
-                     new ColumnDef('created', 'datetime',
-                                   null, false),
-                     new ColumnDef('modified', 'datetime',
-                                   null, false));
-    }
-
-    /**
-     * return key definitions for DB_DataObject
-     *
-     * DB_DataObject needs to know about keys that the table has; this function
-     * defines them.
-     *
-     * @return array key definitions
-     */
-    function keys()
-    {
-        return array_keys($this->keyTypes());
-    }
-
-    /**
-     * return key definitions for Memcached_DataObject
-     *
-     * Our caching system uses the same key definitions, but uses a different
-     * method to get them.
-     *
-     * @return array key definitions
-     */
-    function keyTypes()
-    {
-        return array('uri' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
-    }
-
-    function sequenceKey()
-    {
-        return array(false, false, false);
+        return array(
+            'fields' => array(
+                'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
+                'profile_id' => array('type' => 'integer'),
+                'group_id' => array('type' => 'integer'),
+                'feeduri' => array('type' => 'varchar', 'length' => 255),
+                'salmonuri' => array('type' => 'varchar', 'length' => 255),
+                'avatar' => array('type' => 'text'),
+                'created' => array('type' => 'datetime', 'not null' => true),
+                'modified' => array('type' => 'datetime', 'not null' => true),
+            ),
+            'primary key' => array('uri'),
+            'unique keys' => array(
+                'ostatus_profile_profile_id_idx' => array('profile_id'),
+                'ostatus_profile_group_id_idx' => array('group_id'),
+                'ostatus_profile_feeduri_idx' => array('feeduri'),
+            ),
+            'foreign keys' => array(
+                'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
+                'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')),
+            ),
+        );
     }
 
     /**
@@ -184,10 +150,10 @@ class Ostatus_profile extends Memcached_DataObject
         } else if ($this->group_id && !$this->profile_id) {
             return true;
         } else if ($this->group_id && $this->profile_id) {
-            // TRANS: Server exception.
+            // TRANS: Server exception. %s is a URI.
             throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs set for %s.'),$this->uri));
         } else {
-            // TRANS: Server exception.
+            // TRANS: Server exception. %s is a URI.
             throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs empty for %s.'),$this->uri));
         }
     }
@@ -401,6 +367,7 @@ class Ostatus_profile extends Memcached_DataObject
         } else if ($feed->localName == 'rss') { // @fixme check namespace
             $this->processRssFeed($feed, $source);
         } else {
+            // TRANS: Exception.
             throw new Exception(_m('Unknown feed format.'));
         }
     }
@@ -424,6 +391,7 @@ class Ostatus_profile extends Memcached_DataObject
         $channels = $rss->getElementsByTagName('channel');
 
         if ($channels->length == 0) {
+            // TRANS: Exception.
             throw new Exception(_m('RSS feed without a channel.'));
         } else if ($channels->length > 1) {
             common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed");
@@ -551,14 +519,14 @@ class Ostatus_profile extends Memcached_DataObject
             $sourceContent = $note->title;
         } else {
             // @fixme fetch from $sourceUrl?
-            // TRANS: Client exception. %s is a source URL.
+            // TRANS: Client exception. %s is a source URI.
             throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri));
         }
 
         // Get (safe!) HTML and text versions of the content
 
         $rendered = $this->purify($sourceContent);
-        $content = html_entity_decode(strip_tags($rendered));
+        $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
 
         $shortened = common_shorten_links($content);
 
@@ -569,7 +537,7 @@ class Ostatus_profile extends Memcached_DataObject
 
         if (Notice::contentTooLong($shortened)) {
             $attachment = $this->saveHTMLFile($note->title, $rendered);
-            $summary = html_entity_decode(strip_tags($note->summary));
+            $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8');
             if (empty($summary)) {
                 $summary = $content;
             }
@@ -584,7 +552,9 @@ class Ostatus_profile extends Memcached_DataObject
                 // We mark up the attachment link specially for the HTML output
                 // so we can fold-out the full version inline.
 
-                // TRANS: Shown when a notice is longer than supported and/or when attachments are present.
+                // @fixme I18N this tooltip will be saved with the site's default language
+                // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
+                // TRANS: this will usually be replaced with localised text from StatusNet core messages.
                 $showMoreText = _m('Show more');
                 $attachUrl = common_local_url('attachment',
                                               array('attachment' => $attachment->id));
@@ -835,7 +805,7 @@ class Ostatus_profile extends Memcached_DataObject
             return self::ensureFeedURL($feedurl, $hints);
         }
 
-        // TRANS: Exception.
+        // TRANS: Exception. %s is a URL.
         throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
     }
 
@@ -973,6 +943,7 @@ class Ostatus_profile extends Memcached_DataObject
         }
 
         // XXX: make some educated guesses here
+        // TRANS: Feed sub exception.
         throw new FeedSubException(_m('Can\'t find enough profile information to make a feed.'));
     }
 
@@ -1032,6 +1003,7 @@ class Ostatus_profile extends Memcached_DataObject
             return;
         }
         if (!common_valid_http_url($url)) {
+            // TRANS: Server exception. %s is a URL.
             throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url));
         }
 
@@ -1042,6 +1014,7 @@ class Ostatus_profile extends Memcached_DataObject
         }
         if (!$self) {
             throw new ServerException(sprintf(
+                // TRANS: Server exception. %s is a URI.
                 _m("Tried to update avatar for unsaved remote profile %s."),
                 $this->uri));
         }
@@ -1049,22 +1022,28 @@ class Ostatus_profile extends Memcached_DataObject
         // @fixme this should be better encapsulated
         // ripped from oauthstore.php (for old OMB client)
         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
-        if (!copy($url, $temp_filename)) {
-            throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
-        }
+        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));
+            }
 
-        if ($this->isGroup()) {
-            $id = $this->group_id;
-        } else {
-            $id = $this->profile_id;
-        }
-        // @fixme should we be using different ids?
-        $imagefile = new ImageFile($id, $temp_filename);
-        $filename = Avatar::filename($id,
-                                     image_type_to_extension($imagefile->type),
-                                     null,
-                                     common_timestamp());
-        rename($temp_filename, Avatar::path($filename));
+            if ($this->isGroup()) {
+                $id = $this->group_id;
+            } else {
+                $id = $this->profile_id;
+            }
+            // @fixme should we be using different ids?
+            $imagefile = new ImageFile($id, $temp_filename);
+            $filename = Avatar::filename($id,
+                                         image_type_to_extension($imagefile->type),
+                                         null,
+                                         common_timestamp());
+            rename($temp_filename, Avatar::path($filename));
+        } catch (Exception $e) {
+            unlink($temp_filename);
+            throw $e;
+        }
         // @fixme hardcoded chmod is lame, but seems to be necessary to
         // keep from accidentally saving images from command-line (queues)
         // that can't be read from web server, which causes hard-to-notice
@@ -1088,7 +1067,7 @@ class Ostatus_profile extends Memcached_DataObject
      * @return mixed URL string or false
      */
 
-    protected static function getActivityObjectAvatar($object, $hints=array())
+    public static function getActivityObjectAvatar($object, $hints=array())
     {
         if ($object->avatarLinks) {
             $best = false;
@@ -1328,7 +1307,7 @@ class Ostatus_profile extends Memcached_DataObject
 
             $oprofile->profile_id = $profile->insert();
             if (!$oprofile->profile_id) {
-            // TRANS: Exception.
+            // TRANS: Server exception.
                 throw new ServerException(_m('Can\'t save local profile.'));
             }
         } else {
@@ -1339,7 +1318,7 @@ class Ostatus_profile extends Memcached_DataObject
 
             $oprofile->group_id = $group->insert();
             if (!$oprofile->group_id) {
-                // TRANS: Exception.
+                // TRANS: Server exception.
                 throw new ServerException(_m('Can\'t save local profile.'));
             }
         }
@@ -1347,7 +1326,7 @@ class Ostatus_profile extends Memcached_DataObject
         $ok = $oprofile->insert();
 
         if (!$ok) {
-            // TRANS: Exception.
+            // TRANS: Server exception.
             throw new ServerException(_m('Can\'t save OStatus profile.'));
         }
 
@@ -1390,7 +1369,7 @@ class Ostatus_profile extends Memcached_DataObject
         }
     }
 
-    protected static function updateProfile($profile, $object, $hints=array())
+    public static function updateProfile($profile, $object, $hints=array())
     {
         $orig = clone($profile);
 
@@ -1518,7 +1497,7 @@ class Ostatus_profile extends Memcached_DataObject
         return $bio;
     }
 
-    protected static function getActivityObjectNickname($object, $hints=array())
+    public static function getActivityObjectNickname($object, $hints=array())
     {
         if ($object->poco) {
             if (!empty($object->poco->preferredUsername)) {
@@ -1535,8 +1514,11 @@ class Ostatus_profile extends Memcached_DataObject
         }
 
         // Try the profile url (like foo.example.com or example.com/user/foo)
-
-        $profileUrl = ($object->link) ? $object->link : $hints['profileurl'];
+        if (!empty($object->link)) {
+            $profileUrl = $object->link;
+        } else if (!empty($hints['profileurl'])) {
+            $profileUrl = $hints['profileurl'];
+        }
 
         if (!empty($profileUrl)) {
             $nickname = self::nicknameFromURI($profileUrl);
@@ -1567,9 +1549,11 @@ class Ostatus_profile extends Memcached_DataObject
 
     protected static function nicknameFromURI($uri)
     {
-        preg_match('/(\w+):/', $uri, $matches);
-
-        $protocol = $matches[1];
+        if (preg_match('/(\w+):/', $uri, $matches)) {
+            $protocol = $matches[1];
+        } else {
+            return null;
+        }
 
         switch ($protocol) {
         case 'acct':
@@ -1786,6 +1770,7 @@ class Ostatus_profile extends Memcached_DataObject
 
         if ($file_id === false) {
             common_log_db_error($file, "INSERT", __FILE__);
+            // TRANS: Server exception.
             throw new ServerException(_m('Could not store HTML content of long post as file.'));
         }