]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Merged stuff from upstream/master
[quix0rs-gnu-social.git] / classes / Notice.php
index 6781918be4ae410e2b654466673ff0bbee31f859..2d3ed9942a4950c253c49829c9097d602f4cb57d 100644 (file)
@@ -330,7 +330,7 @@ class Notice extends Managed_DataObject
      * Record the given set of hash tags in the db for this notice.
      * Given tag strings will be normalized and checked for dupes.
      */
-    function saveKnownTags($hashtags)
+    function saveKnownTags(array $hashtags)
     {
         //turn each into their canonical tag
         //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
@@ -414,7 +414,7 @@ class Notice extends Managed_DataObject
      * @return Notice
      * @throws ClientException
      */
-    static function saveNew($profile_id, $content, $source, array $options=null) {
+    static function saveNew($profile_id, $content, $source, array $options=array()) {
         $defaults = array('uri' => null,
                           'url' => null,
                           'conversation' => null,   // URI of conversation
@@ -425,13 +425,16 @@ class Notice extends Managed_DataObject
                           'object_type' => null,
                           'verb' => null);
 
-        if (!empty($options) && is_array($options)) {
+        /*
+         * Above type-hint is already array, so simply count it, this saves
+         * "some" CPU cycles.
+         */
+        if (count($options) > 0) {
             $options = array_merge($defaults, $options);
-            extract($options);
-        } else {
-            extract($defaults);
         }
 
+        extract($options);
+
         if (!isset($is_local)) {
             $is_local = Notice::LOCAL_PUBLIC;
         }
@@ -532,8 +535,7 @@ class Notice extends Managed_DataObject
                 throw new ClientException(_('You cannot repeat your own notice.'));
             }
 
-            if ($repeat->scope != Notice::SITE_SCOPE &&
-                $repeat->scope != Notice::PUBLIC_SCOPE) {
+            if ($repeat->isPrivateScope()) {
                 // TRANS: Client error displayed when trying to repeat a non-public notice.
                 throw new ClientException(_('Cannot repeat a private notice.'), 403);
             }
@@ -974,8 +976,6 @@ class Notice extends Managed_DataObject
         // reasonably handle notifications themselves.
         if (ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST))) {
 
-            $stored->saveAttentions($act->context->attention);
-
             if (!empty($tags)) {
                 $stored->saveKnownTags($tags);
             } else {
@@ -984,7 +984,7 @@ class Notice extends Managed_DataObject
 
             // Note: groups may save tags, so must be run after tags are saved
             // to avoid errors on duplicates.
-            // Note: groups should always be set.
+            $stored->saveAttentions($act->context->attention);
 
             if (!empty($urls)) {
                 $stored->saveKnownUrls($urls);
@@ -1153,7 +1153,7 @@ class Notice extends Managed_DataObject
      *
      * @return void
      */
-    function saveKnownUrls($urls)
+    function saveKnownUrls(array $urls)
     {
         if (common_config('attachments', 'process_links')) {
             // @fixme validation?
@@ -1238,7 +1238,7 @@ class Notice extends Managed_DataObject
         return $this->_attachments[$this->id];
     }
 
-       function _setAttachments($attachments)
+       function _setAttachments(array $attachments)
        {
            $this->_attachments[$this->id] = $attachments;
        }
@@ -1581,14 +1581,27 @@ class Notice extends Managed_DataObject
                 continue;
             }
 
-            $this->saveAttention($target);
+            try {
+                $this->saveAttention($target);
+            } catch (AlreadyFulfilledException $e) {
+                common_debug('Attention already exists: '.var_export($e->getMessage(),true));
+            } catch (Exception $e) {
+                common_log(LOG_ERR, "Could not save notice id=={$this->getID()} attention for profile id=={$target->getID()}: {$e->getMessage()}");
+            }
         }
     }
 
+    /**
+     * Saves an attention for a profile (user or group) which means
+     * it shows up in their home feed and such.
+     */
     function saveAttention(Profile $target, $reason=null)
     {
         if ($target->isGroup()) {
-            // FIXME: Make sure we check that users are in the groups they send to!
+            // FIXME: Make sure we check (for both local and remote) users are in the groups they send to!
+
+            // legacy notification method, will still be in use for quite a while I think
+            $this->addToGroupInbox($target->getGroup());
         } else {
             if ($target->hasBlocked($this->getProfile())) {
                 common_log(LOG_INFO, "Not saving reply to profile {$target->id} ($uri) from sender {$sender->id} because of a block.");
@@ -1597,17 +1610,11 @@ class Notice extends Managed_DataObject
         }
 
         if ($target->isLocal()) {
-            // is local user
-            $this->saveReply($target->getID());   // since we still have the Reply table which some apparently use!
+            // legacy notification method, will still be in use for quite a while I think
+            $this->saveReply($target->getID());
         }
 
-        try {
-            $att = Attention::saveNew($this, $target, $reason);
-        } catch (AlreadyFulfilledException $e) {
-            common_debug('Could not save Attention: '.$e->getMessage());
-        } catch (Exception $e) {
-            common_log(LOG_ERR, 'Could not save Attention: '.$e->getMessage());
-        }
+        $att = Attention::saveNew($this, $target, $reason);
 
         self::blow('reply:stream:%d', $target->getID());
         return true;
@@ -1758,7 +1765,7 @@ class Notice extends Managed_DataObject
         return $this->_replies[$this->getID()];
     }
 
-    function _setReplies($replies)
+    function _setReplies(array $replies)
     {
         $this->_replies[$this->getID()] = $replies;
     }
@@ -1853,11 +1860,11 @@ class Notice extends Managed_DataObject
                }
 
                $groups = User_group::multiGet('id', $ids);
-               $this->_groups[$this->id] = $groups->fetchAll();
+               $this->_setGroups($groups->fetchAll());
                return $this->_groups[$this->id];
     }
 
-    function _setGroups($groups)
+    function _setGroups(array $groups)
     {
         $this->_groups[$this->id] = $groups;
     }
@@ -1954,19 +1961,11 @@ class Notice extends Managed_DataObject
                 }
             }
 
-            $reply_ids = $this->getReplies();
-
-            foreach ($reply_ids as $id) {
-                $rprofile = Profile::getKV('id', $id);
-                if ($rprofile instanceof Profile) {
-                    $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
-                }
-            }
-
-            $groups = $this->getGroups();
-
-            foreach ($groups as $group) {
-                $ctx->attention[$group->getUri()] = ActivityObject::GROUP;
+            // This covers the legacy getReplies and getGroups too which get their data
+            // from entries stored via Notice::saveNew (which we want to move away from)...
+            foreach ($this->getAttentionProfiles() as $target) {
+                // User and group profiles which get the attention of this notice
+                $ctx->attention[$target->getUri()] = $target->getObjectType();
             }
 
             switch ($this->scope) {
@@ -2580,6 +2579,37 @@ class Notice extends Managed_DataObject
      */
     public function getTags()
     {
+        // Check default scope (non-private notices)
+        $inScope = (!$this->isPrivateScope());
+
+        // Get current profile
+        $profile = Profile::current();
+
+        // Is the general scope check okay and the user in logged in?
+        //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . ']: inScope=' . intval($inScope) . ',profile[]=' . gettype($profile));
+        if (($inScope === TRUE) && ($profile instanceof Profile)) {
+            /*
+             * Check scope, else a privacy leaks happens this way:
+             *
+             * 1) Bob and Alice follow each other and write private notices
+             *    (this->scope=2) to each other.
+             * 2) Bob uses tags in his private notice to alice (which she can
+             *    read from him).
+             * 3) Alice adds that notice (with tags) to her favorites
+             *    ("faving") it.
+             * 4) The tags from Bob's private notice becomes visible in Alice's
+             *    profile.
+             *
+             * This has the simple background that the scope is not being
+             * re-checked. This has to be done here at this point because given
+             * above scenario is a privacy leak as the tags may be *really*
+             * private (nobody else shall see them) such as initmate words or
+             * very political words.
+             */
+            $inScope = $this->inScope($profile);
+            //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . ']: inScope=' . intval($inScope) . ' - After inScope() has been called.');
+        }
+
         $tags = array();
 
         $keypart = sprintf('notice:tags:%d', $this->id);
@@ -2591,7 +2621,9 @@ class Notice extends Managed_DataObject
         } else {
             $tag = new Notice_tag();
             $tag->notice_id = $this->id;
-            if ($tag->find()) {
+
+            // Check scope for privacy-leak protection (see some lines above why)
+            if (($inScope === TRUE) && ($tag->find())) {
                 while ($tag->fetch()) {
                     $tags[] = $tag->tag;
                 }
@@ -2719,6 +2751,11 @@ class Notice extends Managed_DataObject
                 ($this->is_local != Notice::GATEWAY));
     }
 
+    public function isPrivateScope () {
+        return ($this->scope != Notice::SITE_SCOPE &&
+                $this->scope != Notice::PUBLIC_SCOPE);
+    }
+
     /**
      * Check that the given profile is allowed to read, respond to, or otherwise
      * act on this notice.
@@ -2733,7 +2770,7 @@ class Notice extends Managed_DataObject
      *
      * @return boolean whether the profile is in the notice's scope
      */
-    function inScope($profile)
+    function inScope(Profile $profile=null)
     {
         if (is_null($profile)) {
             $keypart = sprintf('notice:in-scope-for:%d:null', $this->id);
@@ -2756,7 +2793,7 @@ class Notice extends Managed_DataObject
         return ($result == 1) ? true : false;
     }
 
-    protected function _inScope($profile)
+    protected function _inScope(Profile $profile=null)
     {
         $scope = is_null($this->scope) ? self::defaultScope() : $this->getScope();
 
@@ -2821,7 +2858,7 @@ class Notice extends Managed_DataObject
         return !$this->isHiddenSpam($profile);
     }
 
-    function isHiddenSpam($profile) {
+    function isHiddenSpam(Profile $profile=null) {
 
         // Hide posts by silenced users from everyone but moderators.
 
@@ -2906,7 +2943,7 @@ class Notice extends Managed_DataObject
        return $scope;
     }
 
-       static function fillProfiles($notices)
+       static function fillProfiles(array $notices)
        {
                $map = self::getProfiles($notices);
                foreach ($notices as $entry=>$notice) {
@@ -2923,7 +2960,7 @@ class Notice extends Managed_DataObject
                return array_values($map);
        }
 
-       static function getProfiles(&$notices)
+       static function getProfiles(array &$notices)
        {
                $ids = array();
                foreach ($notices as $notice) {
@@ -2933,7 +2970,7 @@ class Notice extends Managed_DataObject
                return Profile::pivotGet('id', $ids);
        }
 
-       static function fillGroups(&$notices)
+       static function fillGroups(array &$notices)
        {
         $ids = self::_idsOf($notices);
         $gis = Group_inbox::listGet('notice_id', $ids);
@@ -2968,7 +3005,7 @@ class Notice extends Managed_DataObject
                return array_keys($ids);
     }
 
-    static function fillAttachments(&$notices)
+    static function fillAttachments(array &$notices)
     {
         $ids = self::_idsOf($notices);
         $f2pMap = File_to_post::listGet('post_id', $ids);
@@ -2992,7 +3029,7 @@ class Notice extends Managed_DataObject
                }
     }
 
-    static function fillReplies(&$notices)
+    static function fillReplies(array &$notices)
     {
         $ids = self::_idsOf($notices);
         $replyMap = Reply::listGet('notice_id', $ids);
@@ -3006,6 +3043,39 @@ class Notice extends Managed_DataObject
         }
     }
 
+    /**
+     * Checks whether the current profile is allowed (in scope) to see this notice.
+     *
+     * @return $inScope Whether the current profile is allowed to see this notice
+     */
+    function isCurrentProfileInScope () {
+        // Check scope, default is allowed
+        $inScope = TRUE;
+
+        //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] this->tag=' . $this->tag . ',this->id=' . $this->id . ',this->scope=' . $this->scope);
+
+        // Is it private scope?
+        if ($this->isPrivateScope()) {
+            // 2) Get current profile
+            $profile = Profile::current();
+
+            // Is the profile not set?
+            if (!$profile instanceof Profile) {
+                // Public viewer shall not see a tag from a private dent (privacy leak)
+                //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] Not logged in (public view).');
+                $inScope = FALSE;
+            } elseif (!$this->inScope($profile)) {
+                // Current profile is not in scope (not allowed to see) of notice
+                //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] profile->id=' . $profile->id . ' is not allowed to see this notice.');
+                $inScope = FALSE;
+            }
+        }
+
+        // Return result
+        //* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . '] this->tag=' . $this->tag . ',this->weight=' . $this->weight . ',inScope=' . intval($inScope) . ' - EXIT!');
+        return $inScope;
+    }
+
     static public function beforeSchemaUpdate()
     {
         $table = strtolower(get_called_class());
@@ -3051,5 +3121,15 @@ class Notice extends Managed_DataObject
             print ".";
         }
         print "\n";
+
+    /**
+     * Checks whether this notice is in "private scope" (non-public notice)
+     *
+     * @return $isPrivate Whether this notice is private
+     */
+    public function isPrivateScope ()
+    {
+        return ($this->scope != Notice::SITE_SCOPE &&
+                $this->scope != Notice::PUBLIC_SCOPE);
     }
 }