]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/apigroupprofileupdate.php
Don't accept non-objects before testing with "instanceof".
[quix0rs-gnu-social.git] / actions / apigroupprofileupdate.php
index 6ac4b5a4b56894aa9f2bcded6ed1d71c58c68633..1a489eeefb25e749cbfce5dedd94367797c2808d 100644 (file)
@@ -31,8 +31,6 @@ if (!defined('STATUSNET')) {
     exit(1);
 }
 
-require_once INSTALLDIR . '/lib/apiauth.php';
-
 /**
  * API analog to the group edit page
  *
@@ -42,10 +40,9 @@ require_once INSTALLDIR . '/lib/apiauth.php';
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-
 class ApiGroupProfileUpdateAction extends ApiAuthAction
 {
-
+    protected $needPost = true;
     /**
      * Take arguments for running
      *
@@ -54,12 +51,11 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
      * @return boolean success flag
      *
      */
-
-    function prepare($args)
+    protected function prepare(array $args=array())
     {
         parent::prepare($args);
 
-        $this->nickname    = common_canonical_nickname($this->trimmed('nickname'));
+        $this->nickname    = Nickname::normalize($this->trimmed('nickname'));
 
         $this->fullname    = $this->trimmed('fullname');
         $this->homepage    = $this->trimmed('homepage');
@@ -78,45 +74,30 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
      *
      * See which request params have been set, and update the profile
      *
-     * @param array $args $_REQUEST data (unused)
-     *
      * @return void
      */
-
-    function handle($args)
+    protected function handle()
     {
-        parent::handle($args);
-
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-            $this->clientError(
-                _('This method requires a POST.'),
-                400, $this->format
-            );
-            return;
-        }
+        parent::handle();
 
         if (!in_array($this->format, array('xml', 'json'))) {
-            $this->clientError(
-                _('API method not found.'),
-                404,
-                $this->format
-            );
-            return;
+            // TRANS: Client error displayed when coming across a non-supported API method.
+            $this->clientError(_('API method not found.'), 404);
         }
 
         if (empty($this->user)) {
-            $this->clientError(_('No such user.'), 404, $this->format);
-            return;
+            // TRANS: Client error displayed when not providing a user or an invalid user.
+            $this->clientError(_('No such user.'), 404);
         }
 
         if (empty($this->group)) {
-            $this->clientError(_('Group not found.'), 404, $this->format);
-            return false;
+            // TRANS: Client error displayed when not providing a group or an invalid group.
+            $this->clientError(_('Group not found.'), 404);
         }
 
         if (!$this->user->isAdmin($this->group)) {
+            // TRANS: Client error displayed when trying to edit a group without being an admin.
             $this->clientError(_('You must be an admin to edit the group.'), 403);
-            return false;
         }
 
         $this->group->query('BEGIN');
@@ -125,14 +106,14 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
 
         try {
 
-            if (!empty($this->nickname)) {
-                if ($this->validateNickname()) {
-                    $this->group->nickname = $this->nickname;
-                    $this->group->mainpage = common_local_url(
-                        'showgroup',
-                        array('nickname' => $this->nickname)
-                    );
+            if (common_config('profile', 'changenick') == true && $this->group->nickname !== $this->nickname) {
+                try {
+                    $this->group->nickname = Nickname::normalize($this->nickname, true);
+                } catch (NicknameException $e) {
+                    throw new ApiValidationException($e->getMessage());
                 }
+                $this->group->mainpage = common_local_url('showgroup',
+                                            array('nickname' => $this->group->nickname));
             }
 
             if (!empty($this->fullname)) {
@@ -142,7 +123,7 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
 
             if (!empty($this->homepage)) {
                 $this->validateHomepage();
-                $this->group->homepage = $this->hompage;
+                $this->group->homepage = $this->homepage;
             }
 
             if (!empty($this->description)) {
@@ -156,50 +137,34 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
             }
 
         } catch (ApiValidationException $ave) {
-            $this->clientError(
-                $ave->getMessage(),
-                403,
-                $this->format
-            );
-            return;
+            $this->clientError($ave->getMessage(), 400);
         }
 
         $result = $this->group->update($orig);
 
         if (!$result) {
             common_log_db_error($this->group, 'UPDATE', __FILE__);
+            // TRANS: Server error displayed when group update fails.
             $this->serverError(_('Could not update group.'));
         }
 
         $aliases = array();
 
         try {
-
-                       if (!empty($this->aliasstring)) {
-                               $aliases = $this->validateAliases();
+            if (!empty($this->aliasstring)) {
+                $aliases = $this->validateAliases();
             }
-
         } catch (ApiValidationException $ave) {
-            $this->clientError(
-                $ave->getMessage(),
-                403,
-                $this->format
-            );
-            return;
+            $this->clientError($ave->getMessage(), 403);
         }
 
         $result = $this->group->setAliases($aliases);
 
         if (!$result) {
+            // TRANS: Server error displayed when adding group aliases fails.
             $this->serverError(_('Could not create aliases.'));
         }
 
-        if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
-            common_log(LOG_INFO, "Saving local group info.");
-            $local = Local_group::staticGet('group_id', $this->group->id);
-            $local->setNickname($this->nickname);
-        }
-
         $this->group->query('COMMIT');
 
         switch($this->format) {
@@ -210,70 +175,18 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
             $this->showSingleJsonGroup($this->group);
             break;
         default:
-            $this->clientError(_('API method not found.'), 404, $this->format);
-            break;
-        }
-    }
-
-    function nicknameExists($nickname)
-    {
-        $group = Local_group::staticGet('nickname', $nickname);
-
-        if (!empty($group) &&
-            $group->group_id != $this->group->id) {
-            return true;
-        }
-
-        $alias = Group_alias::staticGet('alias', $nickname);
-
-        if (!empty($alias) &&
-            $alias->group_id != $this->group->id) {
-            return true;
-        }
-
-        return false;
-    }
-
-    function validateNickname()
-    {
-        if (!Validate::string(
-            $this->nickname, array(
-                'min_length' => 1,
-                'max_length' => 64,
-                'format' => NICKNAME_FMT
-                )
-            )
-        ) {
-            throw new ApiValidationException(
-                _(
-                    'Nickname must have only lowercase letters ' .
-                    'and numbers and no spaces.'
-                )
-            );
-        } else if ($this->nicknameExists($this->nickname)) {
-            throw new ApiValidationException(
-                _('Nickname already in use. Try another one.')
-            );
-        } else if (!User_group::allowedNickname($this->nickname)) {
-            throw new ApiValidationException(
-                _('Not a valid nickname.')
-            );
+            // TRANS: Client error displayed when coming across a non-supported API method.
+            $this->clientError(_('API method not found.'), 404);
         }
-
-               return true;
     }
 
     function validateHomepage()
     {
         if (!is_null($this->homepage)
-        && (strlen($this->homepage) > 0)
-        && !Validate::uri(
-                $this->homepage,
-                array('allowed_schemes' => array('http', 'https')
-                )
-            )
-        ) {
+                && (strlen($this->homepage) > 0)
+                && !common_valid_http_url($this->homepage)) {
             throw new ApiValidationException(
+                // TRANS: API validation exception thrown when homepage URL does not validate.
                 _('Homepage is not a valid URL.')
             );
         }
@@ -283,7 +196,8 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
     {
         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
             throw new ApiValidationException(
-                _('Full name is too long (max 255 chars).')
+                // TRANS: API validation exception thrown when full name does not validate.
+                _('Full name is too long (maximum 255 characters).')
             );
         }
     }
@@ -291,12 +205,12 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
     function validateDescription()
     {
         if (User_group::descriptionTooLong($this->description)) {
-            throw new ApiValidationException(
-                sprintf(
-                    _('description is too long (max %d chars).'),
-                    User_group::maxDescription()
-                )
-            );
+            // TRANS: API validation exception thrown when description does not validate.
+            // TRANS: %d is the maximum description length and used for plural.
+            throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
+                                                        'Description is too long (maximum %d characters).',
+                                                        User_group::maxDescription()),
+                                                     User_group::maxDescription()));
         }
     }
 
@@ -304,64 +218,30 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
     {
         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
             throw new ApiValidationException(
-                _('Location is too long (max 255 chars).')
+                // TRANS: API validation exception thrown when location does not validate.
+                _('Location is too long (maximum 255 characters).')
             );
         }
     }
 
     function validateAliases()
     {
-        $aliases = array_map(
-            'common_canonical_nickname',
-            array_unique(
-                preg_split('/[\s,]+/',
-                $this->aliasstring
-                )
-            )
-        );
-
-        if (count($aliases) > common_config('group', 'maxaliases')) {
-            throw new ApiValidationException(
-                sprintf(
-                    _('Too many aliases! Maximum %d.'),
-                    common_config('group', 'maxaliases')
-                )
-            );
+        try {
+            $aliases = array_map(array('Nickname', 'normalize'),
+                            array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
+        } catch (NicknameException $e) {
+            throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
         }
 
-        foreach ($aliases as $alias) {
-            if (!Validate::string(
-                $alias, array(
-                    'min_length' => 1,
-                    'max_length' => 64,
-                    'format' => NICKNAME_FMT)
-                )
-            ) {
-                throw new ApiValidationException(
-                    sprintf(
-                        _('Invalid alias: "%s"'),
-                        $alias
-                    )
-                );
-            }
-
-            if ($this->nicknameExists($alias)) {
-                throw new ApiValidationException(
-                    sprintf(
-                        _('Alias "%s" already in use. Try another one.'),
-                        $alias)
-                );
-            }
-
-            // XXX assumes alphanum nicknames
-            if (strcmp($alias, $this->nickname) == 0) {
-                throw new ApiValidationException(
-                    _('Alias can\'t be the same as nickname.')
-                );
-            }
+        if (count($aliases) > common_config('group', 'maxaliases')) {
+            // TRANS: API validation exception thrown when aliases do not validate.
+            // TRANS: %d is the maximum number of aliases and used for plural.
+            throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
+                                                        'Too many aliases! Maximum %d allowed.',
+                                                        common_config('group', 'maxaliases')),
+                                                     common_config('group', 'maxaliases')));
         }
 
         return $aliases;
     }
-
-}
\ No newline at end of file
+}