]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Merge branch 'master' into social-master
[quix0rs-gnu-social.git] / actions / apigroupprofileupdate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update a group's profile
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * API analog to the group edit page
36  *
37  * @category API
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43 class ApiGroupProfileUpdateAction extends ApiAuthAction
44 {
45     protected $needPost = true;
46     /**
47      * Take arguments for running
48      *
49      * @param array $args $_REQUEST args
50      *
51      * @return boolean success flag
52      *
53      */
54     protected function prepare(array $args=array())
55     {
56         parent::prepare($args);
57
58         $this->nickname    = Nickname::normalize($this->trimmed('nickname'));
59
60         $this->fullname    = $this->trimmed('fullname');
61         $this->homepage    = $this->trimmed('homepage');
62         $this->description = $this->trimmed('description');
63         $this->location    = $this->trimmed('location');
64         $this->aliasstring = $this->trimmed('aliases');
65
66         $this->user  = $this->auth_user;
67         $this->group = $this->getTargetGroup($this->arg('id'));
68
69         return true;
70     }
71
72     /**
73      * Handle the request
74      *
75      * See which request params have been set, and update the profile
76      *
77      * @return void
78      */
79     protected function handle()
80     {
81         parent::handle();
82
83         if (!in_array($this->format, array('xml', 'json'))) {
84             // TRANS: Client error displayed when coming across a non-supported API method.
85             $this->clientError(_('API method not found.'), 404);
86         }
87
88         if (empty($this->user)) {
89             // TRANS: Client error displayed when not providing a user or an invalid user.
90             $this->clientError(_('No such user.'), 404);
91         }
92
93         if (empty($this->group)) {
94             // TRANS: Client error displayed when not providing a group or an invalid group.
95             $this->clientError(_('Group not found.'), 404);
96         }
97
98         if (!$this->user->isAdmin($this->group)) {
99             // TRANS: Client error displayed when trying to edit a group without being an admin.
100             $this->clientError(_('You must be an admin to edit the group.'), 403);
101         }
102
103         $this->group->query('BEGIN');
104
105         $orig = clone($this->group);
106
107         try {
108
109             if (common_config('profile', 'changenick') == true && $this->group->nickname !== $this->nickname) {
110                 try {
111                     $this->group->nickname = Nickname::normalize($this->nickname, true);
112                 } catch (NicknameException $e) {
113                     throw new ApiValidationException($e->getMessage());
114                 }
115                 $this->group->mainpage = common_local_url('showgroup',
116                                             array('nickname' => $this->group->nickname));
117             }
118
119             if (!empty($this->fullname)) {
120                 $this->validateFullname();
121                 $this->group->fullname = $this->fullname;
122             }
123
124             if (!empty($this->homepage)) {
125                 $this->validateHomepage();
126                 $this->group->homepage = $this->homepage;
127             }
128
129             if (!empty($this->description)) {
130                 $this->validateDescription();
131                 $this->group->description = $this->decription;
132             }
133
134             if (!empty($this->location)) {
135                 $this->validateLocation();
136                 $this->group->location = $this->location;
137             }
138
139         } catch (ApiValidationException $ave) {
140             $this->clientError($ave->getMessage(), 400);
141         }
142
143         $result = $this->group->update($orig);
144
145         if (!$result) {
146             common_log_db_error($this->group, 'UPDATE', __FILE__);
147             // TRANS: Server error displayed when group update fails.
148             $this->serverError(_('Could not update group.'));
149         }
150
151         $aliases = array();
152
153         try {
154             if (!empty($this->aliasstring)) {
155                 $aliases = $this->validateAliases();
156             }
157         } catch (ApiValidationException $ave) {
158             $this->clientError($ave->getMessage(), 403);
159         }
160
161         $result = $this->group->setAliases($aliases);
162
163         if (!$result) {
164             // TRANS: Server error displayed when adding group aliases fails.
165             $this->serverError(_('Could not create aliases.'));
166         }
167
168         $this->group->query('COMMIT');
169
170         switch($this->format) {
171         case 'xml':
172             $this->showSingleXmlGroup($this->group);
173             break;
174         case 'json':
175             $this->showSingleJsonGroup($this->group);
176             break;
177         default:
178             // TRANS: Client error displayed when coming across a non-supported API method.
179             $this->clientError(_('API method not found.'), 404);
180         }
181     }
182
183     function validateHomepage()
184     {
185         if (!is_null($this->homepage)
186                 && (strlen($this->homepage) > 0)
187                 && !common_valid_http_url($this->homepage)) {
188             throw new ApiValidationException(
189                 // TRANS: API validation exception thrown when homepage URL does not validate.
190                 _('Homepage is not a valid URL.')
191             );
192         }
193     }
194
195     function validateFullname()
196     {
197         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
198             throw new ApiValidationException(
199                 // TRANS: API validation exception thrown when full name does not validate.
200                 _('Full name is too long (maximum 255 characters).')
201             );
202         }
203     }
204
205     function validateDescription()
206     {
207         if (User_group::descriptionTooLong($this->description)) {
208             // TRANS: API validation exception thrown when description does not validate.
209             // TRANS: %d is the maximum description length and used for plural.
210             throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
211                                                         'Description is too long (maximum %d characters).',
212                                                         User_group::maxDescription()),
213                                                      User_group::maxDescription()));
214         }
215     }
216
217     function validateLocation()
218     {
219         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
220             throw new ApiValidationException(
221                 // TRANS: API validation exception thrown when location does not validate.
222                 _('Location is too long (maximum 255 characters).')
223             );
224         }
225     }
226
227     function validateAliases()
228     {
229         try {
230             $aliases = array_map(array('Nickname', 'normalize'),
231                             array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
232         } catch (NicknameException $e) {
233             throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
234         }
235
236         if (count($aliases) > common_config('group', 'maxaliases')) {
237             // TRANS: API validation exception thrown when aliases do not validate.
238             // TRANS: %d is the maximum number of aliases and used for plural.
239             throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
240                                                         'Too many aliases! Maximum %d allowed.',
241                                                         common_config('group', 'maxaliases')),
242                                                      common_config('group', 'maxaliases')));
243         }
244
245         return $aliases;
246     }
247 }