]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editapplication.php
* Add/update translator documentation.
[quix0rs-gnu-social.git] / actions / editapplication.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Edit an OAuth Application
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  Applications
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-2009 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') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Edit the details of an OAuth application
36  *
37  * This is the form for editing an application
38  *
39  * @category Application
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class EditApplicationAction extends OwnerDesignAction
46 {
47     var $msg   = null;
48     var $owner = null;
49     var $app   = null;
50
51     function title()
52     {
53         // TRANS: Title for "Edit application" form.
54         return _('Edit application');
55     }
56
57     /**
58      * Prepare to run
59      */
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if (!common_logged_in()) {
65             // TRANS: Client error displayed trying to edit an application while not logged in.
66             $this->clientError(_('You must be logged in to edit an application.'));
67             return false;
68         }
69
70         $id = (int)$this->arg('id');
71
72         $this->app   = Oauth_application::staticGet($id);
73         $this->owner = User::staticGet($this->app->owner);
74         $cur         = common_current_user();
75
76         if ($cur->id != $this->owner->id) {
77             // TRANS: Client error displayed trying to edit an application while not being its owner.
78             $this->clientError(_('You are not the owner of this application.'), 401);
79         }
80
81         if (!$this->app) {
82             // TRANS: Client error displayed trying to edit an application that does not exist.
83             $this->clientError(_('No such application.'));
84             return false;
85         }
86
87         return true;
88     }
89
90     /**
91      * Handle the request
92      *
93      * On GET, show the form. On POST, try to save the app.
94      *
95      * @param array $args unused
96      *
97      * @return void
98      */
99     function handle($args)
100     {
101         parent::handle($args);
102
103         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
104             $this->handlePost($args);
105         } else {
106             $this->showForm();
107         }
108     }
109
110     function handlePost($args)
111     {
112         // Workaround for PHP returning empty $_POST and $_FILES when POST
113         // length > post_max_size in php.ini
114
115         if (empty($_FILES)
116             && empty($_POST)
117             && ($_SERVER['CONTENT_LENGTH'] > 0)
118             ) {
119             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
120             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
121             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
122                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
123                       intval($_SERVER['CONTENT_LENGTH']));
124             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
125             return;
126         }
127
128         // CSRF protection
129         $token = $this->trimmed('token');
130         if (!$token || $token != common_session_token()) {
131             $this->clientError(_('There was a problem with your session token.'));
132             return;
133         }
134
135         $cur = common_current_user();
136
137         if ($this->arg('cancel')) {
138             common_redirect(common_local_url('showapplication',
139                                              array('id' => $this->app->id)), 303);
140         } elseif ($this->arg('save')) {
141             $this->trySave();
142         } else {
143             // TRANS: Client error displayed submitting invalid form data for edit application.
144             $this->clientError(_('Unexpected form submission.'));
145         }
146     }
147
148     function showForm($msg=null)
149     {
150         $this->msg = $msg;
151         $this->showPage();
152     }
153
154     function showContent()
155     {
156         $form = new ApplicationEditForm($this, $this->app);
157         $form->show();
158     }
159
160     function showPageNotice()
161     {
162         if (!empty($this->msg)) {
163             $this->element('p', 'error', $this->msg);
164         } else {
165             $this->element('p', 'instructions',
166                            // TRANS: Instructions for "Edit application" form.
167                            _('Use this form to edit your application.'));
168         }
169     }
170
171     function trySave()
172     {
173         $name         = $this->trimmed('name');
174         $description  = $this->trimmed('description');
175         $source_url   = $this->trimmed('source_url');
176         $organization = $this->trimmed('organization');
177         $homepage     = $this->trimmed('homepage');
178         $callback_url = $this->trimmed('callback_url');
179         $type         = $this->arg('app_type');
180         $access_type  = $this->arg('default_access_type');
181
182         if (empty($name)) {
183             // TRANS: Validation error shown when not providing a name in the "Edit application" form.
184             $this->showForm(_('Name is required.'));
185             return;
186         } elseif (mb_strlen($name) > 255) {
187             // TRANS: Validation error shown when providing too long a name in the "Edit application" form.
188             $this->showForm(_('Name is too long (maximum 255 characters).'));
189             return;
190         } else if ($this->nameExists($name)) {
191             // TRANS: Validation error shown when providing a name for an application that already exists in the "Edit application" form.
192             $this->showForm(_('Name already in use. Try another one.'));
193             return;
194         } elseif (empty($description)) {
195             // TRANS: Validation error shown when not providing a description in the "Edit application" form.
196             $this->showForm(_('Description is required.'));
197             return;
198         } elseif (Oauth_application::descriptionTooLong($description)) {
199             $this->showForm(sprintf(
200                 // TRANS: Validation error shown when providing too long a description in the "Edit application" form.
201                 // TRANS: %d is the maximum number of allowed characters.
202                 _m('Description is too long (maximum %d character).',
203                   'Description is too long (maximum %d characters).',
204                   Oauth_application::maxDesc()),
205                                     Oauth_application::maxDesc()));
206             return;
207         } elseif (mb_strlen($source_url) > 255) {
208             // TRANS: Validation error shown when providing too long a source URL in the "Edit application" form.
209             $this->showForm(_('Source URL is too long.'));
210             return;
211         } elseif ((mb_strlen($source_url) > 0)
212                   && !Validate::uri($source_url,
213                                     array('allowed_schemes' => array('http', 'https'))))
214             {
215                 // TRANS: Validation error shown when providing an invalid source URL in the "Edit application" form.
216                 $this->showForm(_('Source URL is not valid.'));
217                 return;
218         } elseif (empty($organization)) {
219             // TRANS: Validation error shown when not providing an organisation in the "Edit application" form.
220             $this->showForm(_('Organization is required.'));
221             return;
222         } elseif (mb_strlen($organization) > 255) {
223             // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
224             $this->showForm(_('Organization is too long (maximum 255 characters).'));
225             return;
226         } elseif (empty($homepage)) {
227             // TRANS: Form validation error show when an organisation name has not been provided in the edit application form.
228             $this->showForm(_('Organization homepage is required.'));
229             return;
230         } elseif ((mb_strlen($homepage) > 0)
231                   && !Validate::uri($homepage,
232                                     array('allowed_schemes' => array('http', 'https'))))
233             {
234                 // TRANS: Validation error shown when providing an invalid homepage URL in the "Edit application" form.
235                 $this->showForm(_('Homepage is not a valid URL.'));
236                 return;
237             } elseif (mb_strlen($callback_url) > 255) {
238                 // TRANS: Validation error shown when providing too long a callback URL in the "Edit application" form.
239                 $this->showForm(_('Callback is too long.'));
240                 return;
241             } elseif (mb_strlen($callback_url) > 0
242                       && !Validate::uri($source_url,
243                                         array('allowed_schemes' => array('http', 'https'))
244                                         ))
245                 {
246                     // TRANS: Validation error shown when providing an invalid callback URL in the "Edit application" form.
247                     $this->showForm(_('Callback URL is not valid.'));
248                     return;
249                 }
250
251         $cur = common_current_user();
252
253         // Checked in prepare() above
254
255         assert(!is_null($cur));
256         assert(!is_null($this->app));
257
258         $orig = clone($this->app);
259
260         $this->app->name         = $name;
261         $this->app->description  = $description;
262         $this->app->source_url   = $source_url;
263         $this->app->organization = $organization;
264         $this->app->homepage     = $homepage;
265         $this->app->callback_url = $callback_url;
266         $this->app->type         = $type;
267
268         common_debug("access_type = $access_type");
269
270         if ($access_type == 'r') {
271             $this->app->access_type = 1;
272         } else {
273             $this->app->access_type = 3;
274         }
275
276         $result = $this->app->update($orig);
277
278         // Note: 0 means no rows changed, which can happen if the only
279         // thing we changed was the icon, since it's not altered until
280         // the next step.
281         if ($result === false) {
282             common_log_db_error($this->app, 'UPDATE', __FILE__);
283             // TRANS: Server error occuring when an application could not be updated from the "Edit application" form.
284             $this->serverError(_('Could not update application.'));
285         }
286
287         $this->app->uploadLogo();
288
289         common_redirect(common_local_url('oauthappssettings'), 303);
290     }
291
292     /**
293      * Does the app name already exist?
294      *
295      * Checks the DB to see someone has already registered an app
296      * with the same name.
297      *
298      * @param string $name app name to check
299      *
300      * @return boolean true if the name already exists
301      */
302     function nameExists($name)
303     {
304         $newapp = Oauth_application::staticGet('name', $name);
305         if (empty($newapp)) {
306             return false;
307         } else {
308             return $newapp->id != $this->app->id;
309         }
310     }
311 }