]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editapplication.php
Can now edit/change application 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
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         $this->app = Oauth_application::staticGet($id);
72
73         if (!$this->app) {
74             $this->clientError(_('No such application.'));
75             return false;
76         }
77
78         return true;
79     }
80
81     /**
82      * Handle the request
83      *
84      * On GET, show the form. On POST, try to save the app.
85      *
86      * @param array $args unused
87      *
88      * @return void
89      */
90
91     function handle($args)
92     {
93         parent::handle($args);
94
95         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
96             $this->handlePost($args);
97         } else {
98             $this->showForm();
99         }
100     }
101
102     function handlePost($args)
103     {
104         // Workaround for PHP returning empty $_POST and $_FILES when POST
105         // length > post_max_size in php.ini
106
107         if (empty($_FILES)
108             && empty($_POST)
109             && ($_SERVER['CONTENT_LENGTH'] > 0)
110             ) {
111             $msg = _('The server was unable to handle that much POST ' .
112                      'data (%s bytes) due to its current configuration.');
113             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
114             return;
115         }
116
117         // CSRF protection
118         $token = $this->trimmed('token');
119         if (!$token || $token != common_session_token()) {
120             $this->clientError(_('There was a problem with your session token.'));
121             return;
122         }
123
124         $cur = common_current_user();
125
126         if ($this->arg('cancel')) {
127             common_redirect(common_local_url('showapplication',
128                                              array(
129                                                    'nickname' => $cur->nickname,
130                                                    'id' => $this->app->id)
131                                              ), 303);
132         } elseif ($this->arg('save')) {
133             $this->trySave();
134         } else {
135             $this->clientError(_('Unexpected form submission.'));
136         }
137     }
138
139     function showForm($msg=null)
140     {
141         $this->msg = $msg;
142         $this->showPage();
143     }
144
145     function showContent()
146     {
147         $form = new ApplicationEditForm($this, $this->app);
148         $form->show();
149     }
150
151     function showPageNotice()
152     {
153         if (!empty($this->msg)) {
154             $this->element('p', 'error', $this->msg);
155         } else {
156             $this->element('p', 'instructions',
157                            _('Use this form to edit your application.'));
158         }
159     }
160
161     function trySave()
162     {
163         $name         = $this->trimmed('name');
164         $description  = $this->trimmed('description');
165         $source_url   = $this->trimmed('source_url');
166         $organization = $this->trimmed('organization');
167         $homepage     = $this->trimmed('homepage');
168         $callback_url = $this->trimmed('callback_url');
169         $type         = $this->arg('app_type');
170         $access_type  = $this->arg('default_access_type');
171
172         if (empty($name)) {
173             $this->showForm(_('Name is required.'));
174             return;
175         } elseif (mb_strlen($name) > 255) {
176             $this->showForm(_('Name is too long (max 255 chars).'));
177             return;
178         } elseif (empty($description)) {
179             $this->showForm(_('Description is required.'));
180             return;
181         } elseif (Oauth_application::descriptionTooLong($description)) {
182             $this->showForm(sprintf(
183                 _('Description is too long (max %d chars).'),
184                                     Oauth_application::maxDescription()));
185             return;
186         } elseif (mb_strlen($source_url) > 255) {
187             $this->showForm(_('Source URL is too long.'));
188             return;
189         } elseif ((mb_strlen($source_url) > 0)
190                   && !Validate::uri($source_url,
191                                     array('allowed_schemes' => array('http', 'https'))))
192             {
193                 $this->showForm(_('Source URL is not valid.'));
194                 return;
195         } elseif (empty($organization)) {
196             $this->showForm(_('Organization is required.'));
197             return;
198         } elseif (mb_strlen($organization) > 255) {
199             $this->showForm(_('Organization is too long (max 255 chars).'));
200             return;
201         } elseif (empty($homepage)) {
202             $this->showForm(_('Organization homepage is required.'));
203             return;
204         } elseif ((mb_strlen($homepage) > 0)
205                   && !Validate::uri($homepage,
206                                     array('allowed_schemes' => array('http', 'https'))))
207             {
208                 $this->showForm(_('Homepage is not a valid URL.'));
209                 return;
210             } elseif (mb_strlen($callback_url) > 255) {
211                 $this->showForm(_('Callback is too long.'));
212                 return;
213             } elseif (mb_strlen($callback_url) > 0
214                       && !Validate::uri($source_url,
215                                         array('allowed_schemes' => array('http', 'https'))
216                                         ))
217                 {
218                     $this->showForm(_('Callback URL is not valid.'));
219                     return;
220                 }
221
222         $cur = common_current_user();
223
224         // Checked in prepare() above
225
226         assert(!is_null($cur));
227         assert(!is_null($this->app));
228
229         $orig = clone($this->app);
230
231         $this->app->name         = $name;
232         $this->app->description  = $description;
233         $this->app->source_url   = $source_url;
234         $this->app->organization = $organization;
235         $this->app->homepage     = $homepage;
236         $this->app->callback_url = $callback_url;
237         $this->app->type         = $type;
238
239         common_debug("access_type = $access_type");
240
241         if ($access_type == 'r') {
242             $this->app->access_type = 1;
243         } else {
244             $this->app->access_type = 3;
245         }
246
247         $result = $this->app->update($orig);
248
249         if (!$result) {
250             common_log_db_error($this->app, 'UPDATE', __FILE__);
251             $this->serverError(_('Could not update application.'));
252         }
253
254         $this->app->uploadLogo();
255
256         common_redirect(common_local_url('apps',
257             array('nickname' => $cur->nickname)), 303);
258     }
259
260 }
261