]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
ba1cca5c928ab6704a8b283f450b261972999574
[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-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  * 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
46 class NewApplicationAction extends OwnerDesignAction
47 {
48     var $msg;
49
50     function title()
51     {
52         return _('New Application');
53     }
54
55     /**
56      * Prepare to run
57      */
58
59     function prepare($args)
60     {
61         parent::prepare($args);
62
63         if (!common_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
81     function handle($args)
82     {
83         parent::handle($args);
84
85         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
86         $this->handlePost($args);
87         } else {
88             $this->showForm();
89         }
90     }
91
92     function handlePost($args)
93     {
94     // Workaround for PHP returning empty $_POST and $_FILES when POST
95         // length > post_max_size in php.ini
96
97         if (empty($_FILES)
98             && empty($_POST)
99             && ($_SERVER['CONTENT_LENGTH'] > 0)
100         ) {
101             $msg = _('The server was unable to handle that much POST ' .
102              'data (%s bytes) due to its current configuration.');
103             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
104             return;
105         }
106
107     // CSRF protection
108     $token = $this->trimmed('token');
109     if (!$token || $token != common_session_token()) {
110         $this->clientError(_('There was a problem with your session token.'));
111         return;
112     }
113
114     $cur = common_current_user();
115
116     if ($this->arg('cancel')) {
117         common_redirect(common_local_url('oauthappssettings'), 303);
118     } elseif ($this->arg('save')) {
119         $this->trySave();
120     } else {
121         $this->clientError(_('Unexpected form submission.'));
122     }
123     }
124
125     function showForm($msg=null)
126     {
127         $this->msg = $msg;
128         $this->showPage();
129     }
130
131     function showContent()
132     {
133         $form = new ApplicationEditForm($this);
134         $form->show();
135     }
136
137     function showPageNotice()
138     {
139         if ($this->msg) {
140             $this->element('p', 'error', $this->msg);
141         } else {
142             $this->element('p', 'instructions',
143                            _('Use this form to register a new application.'));
144         }
145     }
146
147     function trySave()
148     {
149         $name         = $this->trimmed('name');
150         $description  = $this->trimmed('description');
151         $source_url   = $this->trimmed('source_url');
152         $organization = $this->trimmed('organization');
153         $homepage     = $this->trimmed('homepage');
154         $callback_url = $this->trimmed('callback_url');
155         $type         = $this->arg('app_type');
156         $access_type  = $this->arg('default_access_type');
157
158         if (empty($name)) {
159              $this->showForm(_('Name is required.'));
160              return;
161         } else if ($this->nameExists($name)) {
162             $this->showForm(_('Name already in use. Try another one.'));
163             return;
164         } elseif (mb_strlen($name) > 255) {
165             $this->showForm(_('Name is too long (max 255 chars).'));
166             return;
167         } elseif (empty($description)) {
168             $this->showForm(_('Description is required.'));
169             return;
170         } elseif (Oauth_application::descriptionTooLong($description)) {
171             $this->showForm(sprintf(
172                 _('Description is too long (max %d chars).'),
173                 Oauth_application::maxDescription()));
174             return;
175         } elseif (empty($source_url)) {
176             $this->showForm(_('Source URL is required.'));
177             return;
178         } elseif ((strlen($source_url) > 0)
179             && !Validate::uri(
180                 $source_url,
181                 array('allowed_schemes' => array('http', 'https'))
182                 )
183             )
184         {
185             $this->showForm(_('Source URL is not valid.'));
186             return;
187         } elseif (empty($organization)) {
188             $this->showForm(_('Organization is required.'));
189             return;
190         } elseif (mb_strlen($organization) > 255) {
191             $this->showForm(_('Organization is too long (max 255 chars).'));
192             return;
193         } elseif (empty($homepage)) {
194             $this->showForm(_('Organization homepage is required.'));
195             return;
196         } elseif ((strlen($homepage) > 0)
197             && !Validate::uri(
198                 $homepage,
199                 array('allowed_schemes' => array('http', 'https'))
200                 )
201             )
202         {
203             $this->showForm(_('Homepage is not a valid URL.'));
204             return;
205         } elseif (mb_strlen($callback_url) > 255) {
206             $this->showForm(_('Callback is too long.'));
207             return;
208         } elseif (strlen($callback_url) > 0
209             && !Validate::uri(
210                 $source_url,
211                 array('allowed_schemes' => array('http', 'https'))
212                 )
213             )
214         {
215             $this->showForm(_('Callback URL is not valid.'));
216             return;
217         }
218
219         $cur = common_current_user();
220
221         // Checked in prepare() above
222
223         assert(!is_null($cur));
224
225         $app = new Oauth_application();
226
227         $app->query('BEGIN');
228
229         $app->name         = $name;
230         $app->owner        = $cur->id;
231         $app->description  = $description;
232         $app->source_url   = $source_url;
233         $app->organization = $organization;
234         $app->homepage     = $homepage;
235         $app->callback_url = $callback_url;
236         $app->type         = $type;
237
238         // Yeah, I dunno why I chose bit flags. I guess so I could
239         // copy this value directly to Oauth_application_user
240         // access_type which I think does need bit flags -- Z
241
242         if ($access_type == 'r') {
243             $app->setAccessFlags(true, false);
244         } else {
245             $app->setAccessFlags(true, true);
246         }
247
248         $app->created = common_sql_now();
249
250         // generate consumer key and secret
251
252         $consumer = Consumer::generateNew();
253
254         $result = $consumer->insert();
255
256         if (!$result) {
257             common_log_db_error($consumer, 'INSERT', __FILE__);
258             $this->serverError(_('Could not create application.'));
259         }
260
261         $app->consumer_key = $consumer->consumer_key;
262
263         $this->app_id = $app->insert();
264
265         if (!$this->app_id) {
266             common_log_db_error($app, 'INSERT', __FILE__);
267             $this->serverError(_('Could not create application.'));
268             $app->query('ROLLBACK');
269         }
270
271         $app->uploadLogo();
272
273         $app->query('COMMIT');
274
275         common_redirect(common_local_url('oauthappssettings'), 303);
276
277     }
278
279     /**
280      * Does the app name already exist?
281      *
282      * Checks the DB to see someone has already registered and app
283      * with the same name.
284      *
285      * @param string $name app name to check
286      *
287      * @return boolean true if the name already exists
288      */
289
290     function nameExists($name)
291     {
292         $app = Oauth_application::staticGet('name', $name);
293         return ($app !== false);
294     }
295
296 }
297