]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
Merge branch 'qna' into 1.0.x
[quix0rs-gnu-social.git] / actions / newapplication.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Register a new 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  * Add a new application
36  *
37  * This is the form for adding a new 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 NewApplicationAction extends OwnerDesignAction
46 {
47     var $msg;
48
49     function title()
50     {
51         // TRANS: This is the title of the form for adding a new application.
52         return _('New application');
53     }
54
55     /**
56      * Prepare to run
57      */
58     function prepare($args)
59     {
60         parent::prepare($args);
61
62         if (!common_logged_in()) {
63             // TRANS: Client error displayed trying to add a new application while not logged in.
64             $this->clientError(_('You must be logged in to register an application.'));
65             return false;
66         }
67
68         return true;
69     }
70
71     /**
72      * Handle the request
73      *
74      * On GET, show the form. On POST, try to save the app.
75      *
76      * @param array $args unused
77      *
78      * @return void
79      */
80     function handle($args)
81     {
82         parent::handle($args);
83
84         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85         $this->handlePost($args);
86         } else {
87             $this->showForm();
88         }
89     }
90
91     function handlePost($args)
92     {
93         // Workaround for PHP returning empty $_POST and $_FILES when POST
94         // length > post_max_size in php.ini
95
96         if (empty($_FILES)
97             && empty($_POST)
98             && ($_SERVER['CONTENT_LENGTH'] > 0)
99         ) {
100             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
101             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
102             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
103                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
104                       intval($_SERVER['CONTENT_LENGTH']));
105             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
106             return;
107         }
108
109         // CSRF protection
110         $token = $this->trimmed('token');
111         if (!$token || $token != common_session_token()) {
112             // TRANS: Client error displayed when the session token does not match or is not given.
113             $this->clientError(_('There was a problem with your session token.'));
114             return;
115         }
116
117         $cur = common_current_user();
118
119         if ($this->arg('cancel')) {
120             common_redirect(common_local_url('oauthappssettings'), 303);
121         } elseif ($this->arg('save')) {
122             $this->trySave();
123         } else {
124             // TRANS: Client error displayed when encountering an unexpected action on form submission.
125             $this->clientError(_('Unexpected form submission.'));
126         }
127     }
128
129     function showForm($msg=null)
130     {
131         $this->msg = $msg;
132         $this->showPage();
133     }
134
135     function showContent()
136     {
137         $form = new ApplicationEditForm($this);
138         $form->show();
139     }
140
141     function showPageNotice()
142     {
143         if ($this->msg) {
144             $this->element('p', 'error', $this->msg);
145         } else {
146             $this->element('p', 'instructions',
147                            // TRANS: Form instructions for registering a new application.
148                            _('Use this form to register a new application.'));
149         }
150     }
151
152     function trySave()
153     {
154         $name         = $this->trimmed('name');
155         $description  = $this->trimmed('description');
156         $source_url   = $this->trimmed('source_url');
157         $organization = $this->trimmed('organization');
158         $homepage     = $this->trimmed('homepage');
159         $callback_url = $this->trimmed('callback_url');
160         $type         = $this->arg('app_type');
161         $access_type  = $this->arg('default_access_type');
162
163         if (empty($name)) {
164             // TRANS: Validation error shown when not providing a name in the "New application" form.
165              $this->showForm(_('Name is required.'));
166              return;
167         } else if ($this->nameExists($name)) {
168             // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
169             $this->showForm(_('Name already in use. Try another one.'));
170             return;
171         } elseif (mb_strlen($name) > 255) {
172             // TRANS: Validation error shown when providing too long a name in the "New application" form.
173             $this->showForm(_('Name is too long (maximum 255 characters).'));
174             return;
175         } elseif (empty($description)) {
176             // TRANS: Validation error shown when not providing a description in the "New application" form.
177             $this->showForm(_('Description is required.'));
178             return;
179         } elseif (Oauth_application::descriptionTooLong($description)) {
180             $this->showForm(sprintf(
181                 // TRANS: Form validation error in New application form.
182                 // TRANS: %d is the maximum number of characters for the description.
183                 _m('Description is too long (maximum %d character).',
184                    'Description is too long (maximum %d characters).',
185                    Oauth_application::maxDesc()),
186                 Oauth_application::maxDesc()));
187             return;
188         } elseif (empty($source_url)) {
189             // TRANS: Validation error shown when not providing a source URL in the "New application" form.
190             $this->showForm(_('Source URL is required.'));
191             return;
192         } elseif ((strlen($source_url) > 0)
193             && !Validate::uri(
194                 $source_url,
195                 array('allowed_schemes' => array('http', 'https'))
196                 )
197             )
198         {
199             // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
200             $this->showForm(_('Source URL is not valid.'));
201             return;
202         } elseif (empty($organization)) {
203             // TRANS: Validation error shown when not providing an organisation in the "New application" form.
204             $this->showForm(_('Organization is required.'));
205             return;
206         } elseif (mb_strlen($organization) > 255) {
207             // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
208             $this->showForm(_('Organization is too long (maximum 255 characters).'));
209             return;
210         } elseif (empty($homepage)) {
211             // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
212             $this->showForm(_('Organization homepage is required.'));
213             return;
214         } elseif ((strlen($homepage) > 0)
215             && !Validate::uri(
216                 $homepage,
217                 array('allowed_schemes' => array('http', 'https'))
218                 )
219             )
220         {
221             // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
222             $this->showForm(_('Homepage is not a valid URL.'));
223             return;
224         } elseif (mb_strlen($callback_url) > 255) {
225             // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
226             $this->showForm(_('Callback is too long.'));
227             return;
228         } elseif (strlen($callback_url) > 0
229             && !Validate::uri(
230                 $source_url,
231                 array('allowed_schemes' => array('http', 'https'))
232                 )
233             )
234         {
235             // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
236             $this->showForm(_('Callback URL is not valid.'));
237             return;
238         }
239
240         $cur = common_current_user();
241
242         // Checked in prepare() above
243
244         assert(!is_null($cur));
245
246         $app = new Oauth_application();
247
248         $app->query('BEGIN');
249
250         $app->name         = $name;
251         $app->owner        = $cur->id;
252         $app->description  = $description;
253         $app->source_url   = $source_url;
254         $app->organization = $organization;
255         $app->homepage     = $homepage;
256         $app->callback_url = $callback_url;
257         $app->type         = $type;
258
259         // Yeah, I dunno why I chose bit flags. I guess so I could
260         // copy this value directly to Oauth_application_user
261         // access_type which I think does need bit flags -- Z
262
263         if ($access_type == 'r') {
264             $app->setAccessFlags(true, false);
265         } else {
266             $app->setAccessFlags(true, true);
267         }
268
269         $app->created = common_sql_now();
270
271         // generate consumer key and secret
272
273         $consumer = Consumer::generateNew();
274
275         $result = $consumer->insert();
276
277         if (!$result) {
278             common_log_db_error($consumer, 'INSERT', __FILE__);
279             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
280             $this->serverError(_('Could not create application.'));
281         }
282
283         $app->consumer_key = $consumer->consumer_key;
284
285         $this->app_id = $app->insert();
286
287         if (!$this->app_id) {
288             common_log_db_error($app, 'INSERT', __FILE__);
289             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
290             $this->serverError(_('Could not create application.'));
291             $app->query('ROLLBACK');
292         }
293
294         try {
295             $app->uploadLogo();
296         } catch (Exception $e) {
297             $app->query('ROLLBACK');
298             // TRANS: Form validation error on New application page when providing an invalid image upload.
299             $this->showForm(_('Invalid image.'));
300             return;
301         }
302
303         $app->query('COMMIT');
304
305         common_redirect(common_local_url('oauthappssettings'), 303);
306     }
307
308     /**
309      * Does the app name already exist?
310      *
311      * Checks the DB to see someone has already registered an app
312      * with the same name.
313      *
314      * @param string $name app name to check
315      *
316      * @return boolean true if the name already exists
317      */
318     function nameExists($name)
319     {
320         $app = Oauth_application::staticGet('name', $name);
321         return !empty($app);
322     }
323 }