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