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