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