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