]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Verify that authenticated API calls are made from our domain name.
[quix0rs-gnu-social.git] / classes / Profile.php
index 00701bc7482b5043cf6b50b9409a81c6d06d51f0..7aae98fb5f171e698ac73cb0dba13c8eab51e99d 100644 (file)
@@ -122,6 +122,12 @@ class Profile extends Managed_DataObject
         }
     }
 
+    public function isPerson()
+    {
+        // Maybe other things than PERSON and GROUP can have Profiles in the future?
+        return !$this->isGroup();
+    }
+
     public function isLocal()
     {
         try {
@@ -175,7 +181,6 @@ class Profile extends Managed_DataObject
         $avatar->mediatype = image_type_to_mime_type($imagefile->type);
         $avatar->filename = $filename;
         $avatar->original = true;
-        $avatar->url = Avatar::url($filename);
         $avatar->created = common_sql_now();
 
         // XXX: start a transaction here
@@ -1169,6 +1174,22 @@ class Profile extends Managed_DataObject
         }
     }
 
+    function silenceAs(Profile $actor)
+    {
+        if (!$actor->hasRight(Right::SILENCEUSER)) {
+            throw new AuthorizationException(_('You cannot silence users on this site.'));
+        }
+        // Only administrators can silence other privileged users (such as others who have the right to silence).
+        if ($this->isPrivileged() && !$actor->hasRole(Profile_role::ADMINISTRATOR)) {
+            throw new AuthorizationException(_('You cannot silence other privileged users.'));
+        }
+        if ($this->isSilenced()) {
+            // TRANS: Client error displayed trying to silence an already silenced user.
+            throw new AlreadyFulfilledException(_('User is already silenced.'));
+        }
+        return $this->silence();
+    }
+
     function unsilence()
     {
         $this->revokeRole(Profile_role::SILENCED);
@@ -1177,6 +1198,19 @@ class Profile extends Managed_DataObject
         }
     }
 
+    function unsilenceAs(Profile $actor)
+    {
+        if (!$actor->hasRight(Right::SILENCEUSER)) {
+            // TRANS: Client error displayed trying to unsilence a user when the user does not have the right.
+            throw new AuthorizationException(_('You cannot unsilence users on this site.'));
+        }
+        if (!$this->isSilenced()) {
+            // TRANS: Client error displayed trying to unsilence a user when the target user has not been silenced.
+            throw new AlreadyFulfilledException(_('User is not silenced.'));
+        }
+        return $this->unsilence();
+    }
+
     function flushVisibility()
     {
         // Get all notices
@@ -1187,6 +1221,22 @@ class Profile extends Managed_DataObject
         }
     }
 
+    public function isPrivileged()
+    {
+        // TODO: An Event::handle so plugins can report if users are privileged.
+        // The ModHelper is the only one I care about when coding this, and that
+        // can be tested with Right::SILENCEUSER which I do below:
+        switch (true) {
+        case $this->hasRight(Right::SILENCEUSER):
+        case $this->hasRole(Profile_role::MODERATOR):
+        case $this->hasRole(Profile_role::ADMINISTRATOR):
+        case $this->hasRole(Profile_role::OWNER):
+            return true;
+        }
+
+        return false;
+    }
+
     /**
      * Does this user have the right to do X?
      *
@@ -1423,11 +1473,25 @@ class Profile extends Managed_DataObject
      */
     public function getUrl()
     {
-        if (empty($this->profileurl) ||
-                !filter_var($this->profileurl, FILTER_VALIDATE_URL)) {
-            throw new InvalidUrlException($this->profileurl);
+        $url = null;
+        if ($this->isGroup()) {
+            // FIXME: Get rid of this event, it fills no real purpose, data should be in Profile->profileurl (replaces User_group->mainpage)
+            if (Event::handle('StartUserGroupHomeUrl', array($this->getGroup(), &$url))) {
+                $url = $this->getGroup()->isLocal()
+                        ? common_local_url('showgroup', array('nickname' => $this->getNickname()))
+                        : $this->profileurl;
+            }
+            Event::handle('EndUserGroupHomeUrl', array($this->getGroup(), $url));
+        } elseif ($this->isLocal()) {
+            $url = common_local_url('showstream', array('nickname' => $this->getNickname()));
+        } else {
+            $url = $this->profileurl;
         }
-        return $this->profileurl;
+        if (empty($url) ||
+                !filter_var($url, FILTER_VALIDATE_URL)) {
+            throw new InvalidUrlException($url);
+        }
+        return $url;
     }
 
     public function getNickname()
@@ -1542,6 +1606,11 @@ class Profile extends Managed_DataObject
             $user = User::getKV('uri', $uri);
             if ($user instanceof User) {
                 $profile = $user->getProfile();
+            } else {
+                $group = User_group::getKV('uri', $uri);
+                if ($group instanceof User_group) {
+                    $profile = $group->getProfile();
+                }
             }
             Event::handle('EndGetProfileFromURI', array($uri, $profile));
         }
@@ -1604,6 +1673,15 @@ class Profile extends Managed_DataObject
         return $profile;
     }
 
+    static function ensureCurrent()
+    {
+        $profile = self::current();
+        if (!$profile instanceof Profile) {
+            throw new AuthorizationException('A currently scoped profile is required.');
+        }
+        return $profile;
+    }
+
     /**
      * Magic function called at serialize() time.
      *