123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class ApiGroupProfileUpdateAction extends ApiAuthAction
- {
- protected $needPost = true;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- $this->nickname = Nickname::normalize($this->trimmed('nickname'));
- $this->fullname = $this->trimmed('fullname');
- $this->homepage = $this->trimmed('homepage');
- $this->description = $this->trimmed('description');
- $this->location = $this->trimmed('location');
- $this->aliasstring = $this->trimmed('aliases');
- $this->user = $this->auth_user;
- $this->group = $this->getTargetGroup($this->arg('id'));
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- if (!in_array($this->format, array('xml', 'json'))) {
-
- $this->clientError(_('API method not found.'), 404);
- }
- if (empty($this->user)) {
-
- $this->clientError(_('No such user.'), 404);
- }
- if (empty($this->group)) {
-
- $this->clientError(_('Group not found.'), 404);
- }
- if (!$this->user->isAdmin($this->group)) {
-
- $this->clientError(_('You must be an admin to edit the group.'), 403);
- }
- $this->group->query('BEGIN');
- $orig = clone($this->group);
- try {
- 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)) {
- $this->validateFullname();
- $this->group->fullname = $this->fullname;
- }
- if (!empty($this->homepage)) {
- $this->validateHomepage();
- $this->group->homepage = $this->homepage;
- }
- if (!empty($this->description)) {
- $this->validateDescription();
- $this->group->description = $this->decription;
- }
- if (!empty($this->location)) {
- $this->validateLocation();
- $this->group->location = $this->location;
- }
- } catch (ApiValidationException $ave) {
- $this->clientError($ave->getMessage(), 400);
- }
- $result = $this->group->update($orig);
- if (!$result) {
- common_log_db_error($this->group, 'UPDATE', __FILE__);
-
- $this->serverError(_('Could not update group.'));
- }
- $aliases = array();
- try {
- if (!empty($this->aliasstring)) {
- $aliases = $this->validateAliases();
- }
- } catch (ApiValidationException $ave) {
- $this->clientError($ave->getMessage(), 403);
- }
- $result = $this->group->setAliases($aliases);
- if (!$result) {
-
- $this->serverError(_('Could not create aliases.'));
- }
- $this->group->query('COMMIT');
- switch($this->format) {
- case 'xml':
- $this->showSingleXmlGroup($this->group);
- break;
- case 'json':
- $this->showSingleJsonGroup($this->group);
- break;
- default:
-
- $this->clientError(_('API method not found.'), 404);
- }
- }
- function validateHomepage()
- {
- if (!is_null($this->homepage)
- && (strlen($this->homepage) > 0)
- && !common_valid_http_url($this->homepage)) {
- throw new ApiValidationException(
-
- _('Homepage is not a valid URL.')
- );
- }
- }
- function validateFullname()
- {
- if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
- throw new ApiValidationException(
-
- _('Full name is too long (maximum 255 characters).')
- );
- }
- }
- function validateDescription()
- {
- if (User_group::descriptionTooLong($this->description)) {
-
-
- 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()));
- }
- }
- function validateLocation()
- {
- if (!is_null($this->location) && mb_strlen($this->location) > 255) {
- throw new ApiValidationException(
-
- _('Location is too long (maximum 255 characters).')
- );
- }
- }
- function validateAliases()
- {
- 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()));
- }
- if (count($aliases) > common_config('group', 'maxaliases')) {
-
-
- 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;
- }
- }
|