]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
Merge branch 'fixes/private_scope_on_tags' into social-master
[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  * @copyright 2013 Free Software Foundation, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Add a new application
37  *
38  * This is the form for adding a new application
39  *
40  * @category Application
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class NewApplicationAction extends FormAction
47 {
48     function title()
49     {
50         // TRANS: This is the title of the form for adding a new application.
51         return _('New application');
52     }
53
54     protected function handlePost()
55     {
56         parent::handlePost();
57
58         if ($this->arg('cancel')) {
59             common_redirect(common_local_url('oauthappssettings'), 303);
60         } elseif ($this->arg('save')) {
61             $this->trySave();
62         }
63
64         // TRANS: Client error displayed when encountering an unexpected action on form submission.
65         $this->clientError(_('Unexpected form submission.'));
66     }
67
68     function showForm($msg=null)
69     {
70         $this->msg = $msg;
71         $this->showPage();
72     }
73
74     function showContent()
75     {
76         $form = new ApplicationEditForm($this);
77         $form->show();
78     }
79
80     function showPageNotice()
81     {
82         if ($this->msg) {
83             $this->element('p', 'error', $this->msg);
84         } else {
85             $this->element('p', 'instructions',
86                            // TRANS: Form instructions for registering a new application.
87                            _('Use this form to register a new application.'));
88         }
89     }
90
91     private function trySave()
92     {
93         $name         = $this->trimmed('name');
94         $description  = $this->trimmed('description');
95         $source_url   = $this->trimmed('source_url');
96         $organization = $this->trimmed('organization');
97         $homepage     = $this->trimmed('homepage');
98         $callback_url = $this->trimmed('callback_url');
99         $type         = $this->arg('app_type');
100         $access_type  = $this->arg('default_access_type');
101
102         if (empty($name)) {
103             // TRANS: Validation error shown when not providing a name in the "New application" form.
104             $this->clientError(_('Name is required.'));
105         } else if ($this->nameExists($name)) {
106             // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
107             $this->clientError(_('Name already in use. Try another one.'));
108         } elseif (mb_strlen($name) > 255) {
109             // TRANS: Validation error shown when providing too long a name in the "New application" form.
110             $this->clientError(_('Name is too long (maximum 255 characters).'));
111         } elseif (empty($description)) {
112             // TRANS: Validation error shown when not providing a description in the "New application" form.
113             $this->clientError(_('Description is required.'));
114         } elseif (Oauth_application::descriptionTooLong($description)) {
115             $this->clientError(sprintf(
116                 // TRANS: Form validation error in New application form.
117                 // TRANS: %d is the maximum number of characters for the description.
118                 _m('Description is too long (maximum %d character).',
119                    'Description is too long (maximum %d characters).',
120                    Oauth_application::maxDesc()),
121                 Oauth_application::maxDesc()));
122         } elseif (empty($source_url)) {
123             // TRANS: Validation error shown when not providing a source URL in the "New application" form.
124             $this->clientError(_('Source URL is required.'));
125         } elseif ((strlen($source_url) > 0) && !common_valid_http_url($source_url)) {
126             // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
127             $this->clientError(_('Source URL is not valid.'));
128         } elseif (empty($organization)) {
129             // TRANS: Validation error shown when not providing an organisation in the "New application" form.
130             $this->clientError(_('Organization is required.'));
131         } elseif (mb_strlen($organization) > 255) {
132             // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
133             $this->clientError(_('Organization is too long (maximum 255 characters).'));
134         } elseif (empty($homepage)) {
135             // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
136             $this->clientError(_('Organization homepage is required.'));
137         } elseif ((strlen($homepage) > 0) && !common_valid_http_url($homepage)) {
138             // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
139             $this->clientError(_('Homepage is not a valid URL.'));
140         } elseif (mb_strlen($callback_url) > 255) {
141             // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
142             $this->clientError(_('Callback is too long.'));
143         } elseif (strlen($callback_url) > 0 && !common_valid_http_url($callback_url)) {
144             // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
145             $this->clientError(_('Callback URL is not valid.'));
146         }
147
148         // Login is checked in parent::prepare()
149         assert(!is_null($this->scoped));
150
151         $app = new Oauth_application();
152
153         $app->query('BEGIN');
154
155         $app->name         = $name;
156         $app->owner        = $this->scoped->id;
157         $app->description  = $description;
158         $app->source_url   = $source_url;
159         $app->organization = $organization;
160         $app->homepage     = $homepage;
161         $app->callback_url = $callback_url;
162         $app->type         = $type;
163
164         // Yeah, I dunno why I chose bit flags. I guess so I could
165         // copy this value directly to Oauth_application_user
166         // access_type which I think does need bit flags -- Z
167
168         if ($access_type == 'r') {
169             $app->setAccessFlags(true, false);
170         } else {
171             $app->setAccessFlags(true, true);
172         }
173
174         $app->created = common_sql_now();
175
176         // generate consumer key and secret
177
178         $consumer = Consumer::generateNew();
179
180         $result = $consumer->insert();
181
182         if (!$result) {
183             common_log_db_error($consumer, 'INSERT', __FILE__);
184             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
185             $this->serverError(_('Could not create application.'));
186         }
187
188         $app->consumer_key = $consumer->consumer_key;
189
190         $this->app_id = $app->insert();
191
192         if (!$this->app_id) {
193             common_log_db_error($app, 'INSERT', __FILE__);
194             // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
195             $this->serverError(_('Could not create application.'));
196             $app->query('ROLLBACK');
197         }
198
199         try {
200             $app->uploadLogo();
201         } catch (Exception $e) {
202             $app->query('ROLLBACK');
203             // TRANS: Form validation error messages displayed when uploading an invalid application logo.
204             $this->clientError(_('Invalid image.'));
205         }
206
207         $app->query('COMMIT');
208
209         common_redirect(common_local_url('oauthappssettings'), 303);
210     }
211
212     /**
213      * Does the app name already exist?
214      *
215      * Checks the DB to see someone has already registered an app
216      * with the same name.
217      *
218      * @param string $name app name to check
219      *
220      * @return boolean true if the name already exists
221      */
222     function nameExists($name)
223     {
224         $app = Oauth_application::getKV('name', $name);
225         return !empty($app);
226     }
227 }