]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
ec0f2e7af28aeb17ae52dd3e920ff8cf903d760d
[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 group.
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
87             // CSRF protection
88             $token = $this->trimmed('token');
89             if (!$token || $token != common_session_token()) {
90                 $this->clientError(_('There was a problem with your session token.'));
91                 return;
92             }
93
94             $cur = common_current_user();
95
96             if ($this->arg('cancel')) {
97                 common_redirect(common_local_url('apps',
98                     array('nickname' => $cur->nickname)), 303);
99             } elseif ($this->arg('save')) {
100                 $this->trySave();
101             } else {
102                 $this->clientError(_('Unexpected form submission.'));
103             }
104         } else {
105             $this->showForm();
106         }
107     }
108
109     function showForm($msg=null)
110     {
111         $this->msg = $msg;
112         $this->showPage();
113     }
114
115     function showContent()
116     {
117         $form = new ApplicationEditForm($this);
118         $form->show();
119     }
120
121     function showPageNotice()
122     {
123         if ($this->msg) {
124             $this->element('p', 'error', $this->msg);
125         } else {
126             $this->element('p', 'instructions',
127                            _('Use this form to register a new application.'));
128         }
129     }
130
131     function trySave()
132     {
133         $name         = $this->trimmed('name');
134         $description  = $this->trimmed('description');
135         $source_url   = $this->trimmed('source_url');
136         $organization = $this->trimmed('organization');
137         $homepage     = $this->trimmed('homepage');
138         $callback_url = $this->trimmed('callback_url');
139         $type         = $this->arg('app_type');
140         $access_type  = $this->arg('access_type');
141
142         if (empty($name)) {
143              $this->showForm(_('Name is required.'));
144              return;
145         } elseif (mb_strlen($name) > 255) {
146             $this->showForm(_('Name is too long (max 255 chars).'));
147             return;
148         } elseif (empty($description)) {
149             $this->showForm(_('Description is required.'));
150             return;
151         } elseif (Oauth_application::descriptionTooLong($description)) {
152             $this->showForm(sprintf(
153                 _('Description is too long (max %d chars).'),
154                 Oauth_application::maxDescription()));
155             return;
156         } elseif (empty($source_url)) {
157             $this->showForm(_('Source URL is required.'));
158             return;
159         } elseif ((strlen($source_url) > 0)
160             && !Validate::uri(
161                 $source_url,
162                 array('allowed_schemes' => array('http', 'https'))
163                 )
164             )
165         {
166             $this->showForm(_('Source URL is not valid.'));
167             return;
168         } elseif (empty($organization)) {
169             $this->showForm(_('Organization is required.'));
170             return;
171         } elseif (mb_strlen($organization) > 255) {
172             $this->showForm(_('Organization is too long (max 255 chars).'));
173             return;
174         } elseif (empty($homepage)) {
175             $this->showForm(_('Organization homepage is required.'));
176             return;
177         } elseif ((strlen($homepage) > 0)
178             && !Validate::uri(
179                 $homepage,
180                 array('allowed_schemes' => array('http', 'https'))
181                 )
182             )
183         {
184             $this->showForm(_('Homepage is not a valid URL.'));
185             return;
186         } elseif (empty($callback_url)) {
187             $this->showForm(_('Callback is required.'));
188             return;
189         } elseif (strlen($callback_url) > 0
190             && !Validate::uri(
191                 $source_url,
192                 array('allowed_schemes' => array('http', 'https'))
193                 )
194             )
195         {
196             $this->showForm(_('Callback URL is not valid.'));
197             return;
198         }
199
200         $cur = common_current_user();
201
202         // Checked in prepare() above
203
204         assert(!is_null($cur));
205
206         $app = new Oauth_application();
207
208         $app->query('BEGIN');
209
210         $app->name         = $name;
211         $app->owner        = $cur->id;
212         $app->description  = $description;
213         $app->source_url   = $source_url;
214         $app->organization = $organization;
215         $app->homepage     = $homepage;
216         $app->callback_url = $callback_url;
217         $app->type         = $type;
218
219         // Yeah, I dunno why I chose bit flags. I guess so I could
220         // copy this value directly to Oauth_application_user
221         // access_type which I think does need bit flags -- Z
222
223         if ($access_type == 'r') {
224             $app->setAccessFlags(true, false);
225         } else {
226             $app->setAccessFlags(true, true);
227         }
228
229         $app->created = common_sql_now();
230
231         // generate consumer key and secret
232
233         $consumer = Consumer::generateNew();
234
235         $result = $consumer->insert();
236
237         if (!$result) {
238             common_log_db_error($consumer, 'INSERT', __FILE__);
239             $this->serverError(_('Could not create application.'));
240         }
241
242         $app->consumer_key = $consumer->consumer_key;
243
244         $result = $app->insert();
245
246         if (!$result) {
247             common_log_db_error($app, 'INSERT', __FILE__);
248             $this->serverError(_('Could not create application.'));
249             $app->query('ROLLBACK');
250         }
251
252         $app->query('COMMIT');
253
254         common_redirect(common_local_url('apps',
255             array('nickname' => $cur->nickname)), 303);
256
257     }
258
259 }
260