]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editapplication.php
Fix error when editing OAuth app registration and only changing the icon.
[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
46 class EditApplicationAction extends OwnerDesignAction
47 {
48     var $msg   = null;
49     var $owner = null;
50     var $app   = null;
51
52     function title()
53     {
54         return _('Edit Application');
55     }
56
57     /**
58      * Prepare to run
59      */
60
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         if (!common_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             $this->clientError(_('You are not the owner of this application.'), 401);
78         }
79
80         if (!$this->app) {
81             $this->clientError(_('No such application.'));
82             return false;
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
98     function handle($args)
99     {
100         parent::handle($args);
101
102         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
103             $this->handlePost($args);
104         } else {
105             $this->showForm();
106         }
107     }
108
109     function handlePost($args)
110     {
111         // Workaround for PHP returning empty $_POST and $_FILES when POST
112         // length > post_max_size in php.ini
113
114         if (empty($_FILES)
115             && empty($_POST)
116             && ($_SERVER['CONTENT_LENGTH'] > 0)
117             ) {
118             $msg = _('The server was unable to handle that much POST ' .
119                      'data (%s bytes) due to its current configuration.');
120             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
121             return;
122         }
123
124         // CSRF protection
125         $token = $this->trimmed('token');
126         if (!$token || $token != common_session_token()) {
127             $this->clientError(_('There was a problem with your session token.'));
128             return;
129         }
130
131         $cur = common_current_user();
132
133         if ($this->arg('cancel')) {
134             common_redirect(common_local_url('showapplication',
135                                              array('id' => $this->app->id)), 303);
136         } elseif ($this->arg('save')) {
137             $this->trySave();
138         } else {
139             $this->clientError(_('Unexpected form submission.'));
140         }
141     }
142
143     function showForm($msg=null)
144     {
145         $this->msg = $msg;
146         $this->showPage();
147     }
148
149     function showContent()
150     {
151         $form = new ApplicationEditForm($this, $this->app);
152         $form->show();
153     }
154
155     function showPageNotice()
156     {
157         if (!empty($this->msg)) {
158             $this->element('p', 'error', $this->msg);
159         } else {
160             $this->element('p', 'instructions',
161                            _('Use this form to edit your application.'));
162         }
163     }
164
165     function trySave()
166     {
167         $name         = $this->trimmed('name');
168         $description  = $this->trimmed('description');
169         $source_url   = $this->trimmed('source_url');
170         $organization = $this->trimmed('organization');
171         $homepage     = $this->trimmed('homepage');
172         $callback_url = $this->trimmed('callback_url');
173         $type         = $this->arg('app_type');
174         $access_type  = $this->arg('default_access_type');
175
176         if (empty($name)) {
177             $this->showForm(_('Name is required.'));
178             return;
179         } elseif (mb_strlen($name) > 255) {
180             $this->showForm(_('Name is too long (max 255 chars).'));
181             return;
182         } else if ($this->nameExists($name)) {
183             $this->showForm(_('Name already in use. Try another one.'));
184             return;
185         } elseif (empty($description)) {
186             $this->showForm(_('Description is required.'));
187             return;
188         } elseif (Oauth_application::descriptionTooLong($description)) {
189             $this->showForm(sprintf(
190                 _('Description is too long (max %d chars).'),
191                                     Oauth_application::maxDesc()));
192             return;
193         } elseif (mb_strlen($source_url) > 255) {
194             $this->showForm(_('Source URL is too long.'));
195             return;
196         } elseif ((mb_strlen($source_url) > 0)
197                   && !Validate::uri($source_url,
198                                     array('allowed_schemes' => array('http', 'https'))))
199             {
200                 $this->showForm(_('Source URL is not valid.'));
201                 return;
202         } elseif (empty($organization)) {
203             $this->showForm(_('Organization is required.'));
204             return;
205         } elseif (mb_strlen($organization) > 255) {
206             $this->showForm(_('Organization is too long (max 255 chars).'));
207             return;
208         } elseif (empty($homepage)) {
209             $this->showForm(_('Organization homepage is required.'));
210             return;
211         } elseif ((mb_strlen($homepage) > 0)
212                   && !Validate::uri($homepage,
213                                     array('allowed_schemes' => array('http', 'https'))))
214             {
215                 $this->showForm(_('Homepage is not a valid URL.'));
216                 return;
217             } elseif (mb_strlen($callback_url) > 255) {
218                 $this->showForm(_('Callback is too long.'));
219                 return;
220             } elseif (mb_strlen($callback_url) > 0
221                       && !Validate::uri($source_url,
222                                         array('allowed_schemes' => array('http', 'https'))
223                                         ))
224                 {
225                     $this->showForm(_('Callback URL is not valid.'));
226                     return;
227                 }
228
229         $cur = common_current_user();
230
231         // Checked in prepare() above
232
233         assert(!is_null($cur));
234         assert(!is_null($this->app));
235
236         $orig = clone($this->app);
237
238         $this->app->name         = $name;
239         $this->app->description  = $description;
240         $this->app->source_url   = $source_url;
241         $this->app->organization = $organization;
242         $this->app->homepage     = $homepage;
243         $this->app->callback_url = $callback_url;
244         $this->app->type         = $type;
245
246         common_debug("access_type = $access_type");
247
248         if ($access_type == 'r') {
249             $this->app->access_type = 1;
250         } else {
251             $this->app->access_type = 3;
252         }
253
254         $result = $this->app->update($orig);
255
256         // Note: 0 means no rows changed, which can happen if the only
257         // thing we changed was the icon, since it's not altered until
258         // the next step.
259         if ($result === false) {
260             common_log_db_error($this->app, 'UPDATE', __FILE__);
261             $this->serverError(_('Could not update application.'));
262         }
263
264         $this->app->uploadLogo();
265
266         common_redirect(common_local_url('oauthappssettings'), 303);
267     }
268
269     /**
270      * Does the app name already exist?
271      *
272      * Checks the DB to see someone has already registered an app
273      * with the same name.
274      *
275      * @param string $name app name to check
276      *
277      * @return boolean true if the name already exists
278      */
279
280     function nameExists($name)
281     {
282         $newapp = Oauth_application::staticGet('name', $name);
283         if (empty($newapp)) {
284             return false;
285         } else {
286             return $newapp->id != $this->app->id;
287         }
288     }
289
290 }
291