]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
Merge remote branch 'origin/1.0.x' 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             $this->clientError(_('There was a problem with your session token.'));
113             return;
114         }
115
116         $cur = common_current_user();
117
118         if ($this->arg('cancel')) {
119             common_redirect(common_local_url('oauthappssettings'), 303);
120         } elseif ($this->arg('save')) {
121             $this->trySave();
122         } else {
123             // TRANS: Client error displayed when encountering an unexpected action on form submission.
124             $this->clientError(_('Unexpected form submission.'));
125         }
126     }
127
128     function showForm($msg=null)
129     {
130         $this->msg = $msg;
131         $this->showPage();
132     }
133
134     function showContent()
135     {
136         $form = new ApplicationEditForm($this);
137         $form->show();
138     }
139
140     function showPageNotice()
141     {
142         if ($this->msg) {
143             $this->element('p', 'error', $this->msg);
144         } else {
145             $this->element('p', 'instructions',
146                            // TRANS: Form instructions for registering a new application.
147                            _('Use this form to register a new application.'));
148         }
149     }
150
151     function trySave()
152     {
153         $name         = $this->trimmed('name');
154         $description  = $this->trimmed('description');
155         $source_url   = $this->trimmed('source_url');
156         $organization = $this->trimmed('organization');
157         $homepage     = $this->trimmed('homepage');
158         $callback_url = $this->trimmed('callback_url');
159         $type         = $this->arg('app_type');
160         $access_type  = $this->arg('default_access_type');
161
162         if (empty($name)) {
163             // TRANS: Validation error shown when not providing a name in the "New application" form.
164              $this->showForm(_('Name is required.'));
165              return;
166         } else if ($this->nameExists($name)) {
167             // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
168             $this->showForm(_('Name already in use. Try another one.'));
169             return;
170         } elseif (mb_strlen($name) > 255) {
171             // TRANS: Validation error shown when providing too long a name in the "New application" form.
172             $this->showForm(_('Name is too long (maximum 255 characters).'));
173             return;
174         } elseif (empty($description)) {
175             // TRANS: Validation error shown when not providing a description in the "New application" form.
176             $this->showForm(_('Description is required.'));
177             return;
178         } elseif (Oauth_application::descriptionTooLong($description)) {
179             $this->showForm(sprintf(
180                 // TRANS: Form validation error in New application form.
181                 // TRANS: %d is the maximum number of characters for the description.
182                 _m('Description is too long (maximum %d character).',
183                    'Description is too long (maximum %d characters).',
184                    Oauth_application::maxDesc()),
185                 Oauth_application::maxDesc()));
186             return;
187         } elseif (empty($source_url)) {
188             // TRANS: Validation error shown when not providing a source URL in the "New application" form.
189             $this->showForm(_('Source URL is required.'));
190             return;
191         } elseif ((strlen($source_url) > 0)
192             && !Validate::uri(
193                 $source_url,
194                 array('allowed_schemes' => array('http', 'https'))
195                 )
196             )
197         {
198             // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
199             $this->showForm(_('Source URL is not valid.'));
200             return;
201         } elseif (empty($organization)) {
202             // TRANS: Validation error shown when not providing an organisation in the "New application" form.
203             $this->showForm(_('Organization is required.'));
204             return;
205         } elseif (mb_strlen($organization) > 255) {
206             // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
207             $this->showForm(_('Organization is too long (maximum 255 characters).'));
208             return;
209         } elseif (empty($homepage)) {
210             // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
211             $this->showForm(_('Organization homepage is required.'));
212             return;
213         } elseif ((strlen($homepage) > 0)
214             && !Validate::uri(
215                 $homepage,
216                 array('allowed_schemes' => array('http', 'https'))
217                 )
218             )
219         {
220             // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
221             $this->showForm(_('Homepage is not a valid URL.'));
222             return;
223         } elseif (mb_strlen($callback_url) > 255) {
224             // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
225             $this->showForm(_('Callback is too long.'));
226             return;
227         } elseif (strlen($callback_url) > 0
228             && !Validate::uri(
229                 $source_url,
230                 array('allowed_schemes' => array('http', 'https'))
231                 )
232             )
233         {
234             // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
235             $this->showForm(_('Callback URL is not valid.'));
236             return;
237         }
238
239         $cur = common_current_user();
240
241         // Checked in prepare() above
242
243         assert(!is_null($cur));
244
245         $app = new Oauth_application();
246
247         $app->query('BEGIN');
248
249         $app->name         = $name;
250         $app->owner        = $cur->id;
251         $app->description  = $description;
252         $app->source_url   = $source_url;
253         $app->organization = $organization;
254         $app->homepage     = $homepage;
255         $app->callback_url = $callback_url;
256         $app->type         = $type;
257
258         // Yeah, I dunno why I chose bit flags. I guess so I could
259         // copy this value directly to Oauth_application_user
260         // access_type which I think does need bit flags -- Z
261
262         if ($access_type == 'r') {
263             $app->setAccessFlags(true, false);
264         } else {
265             $app->setAccessFlags(true, true);
266         }
267
268         $app->created = common_sql_now();
269
270         // generate consumer key and secret
271
272         $consumer = Consumer::generateNew();
273
274         $result = $consumer->insert();
275
276         if (!$result) {
277             common_log_db_error($consumer, 'INSERT', __FILE__);
278             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
279             $this->serverError(_('Could not create application.'));
280         }
281
282         $app->consumer_key = $consumer->consumer_key;
283
284         $this->app_id = $app->insert();
285
286         if (!$this->app_id) {
287             common_log_db_error($app, 'INSERT', __FILE__);
288             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
289             $this->serverError(_('Could not create application.'));
290             $app->query('ROLLBACK');
291         }
292
293         try {
294             $app->uploadLogo();
295         } catch (Exception $e) {
296             $app->query('ROLLBACK');
297             $this->showForm(_('Invalid image.'));
298             return;      
299         }
300
301         $app->query('COMMIT');
302
303         common_redirect(common_local_url('oauthappssettings'), 303);
304     }
305
306     /**
307      * Does the app name already exist?
308      *
309      * Checks the DB to see someone has already registered an app
310      * with the same name.
311      *
312      * @param string $name app name to check
313      *
314      * @return boolean true if the name already exists
315      */
316     function nameExists($name)
317     {
318         $app = Oauth_application::staticGet('name', $name);
319         return !empty($app);
320     }
321 }