]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newapplication.php
Reorganized the OAuth app URLs and more work on the register app workflow
[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 Action
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 create a group.'));
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         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85             $this->trySave();
86         } else {
87             $this->showForm();
88         }
89     }
90
91     function showForm($msg=null)
92     {
93         $this->msg = $msg;
94         $this->showPage();
95     }
96
97     function showContent()
98     {
99         $form = new ApplicationEditForm($this);
100         $form->show();
101     }
102
103     function showPageNotice()
104     {
105         if ($this->msg) {
106             $this->element('p', 'error', $this->msg);
107         } else {
108             $this->element('p', 'instructions',
109                            _('Use this form to register a new application.'));
110         }
111     }
112
113     function trySave()
114     {
115         $name              = $this->trimmed('name');
116         $description       = $this->trimmed('description'); 
117         $source_url        = $this->trimmed('source_url');
118         $organization      = $this->trimmed('organization');
119         $homepage          = $this->trimmed('application');
120         $callback_url      = $this->trimmed('callback_url');
121         $this->type        = $this->trimmed('type');
122         $this->access_type = $this->trimmed('access_type');
123          
124         if (!is_null($name) && mb_strlen($name) > 255) {
125             $this->showForm(_('Name is too long (max 255 chars).'));
126             return;
127         } else if (User_group::descriptionTooLong($description)) {
128             $this->showForm(sprintf(
129                 _('description is too long (max %d chars).'), 
130                 Oauth_application::maxDescription()));
131             return;
132         } elseif (!is_null($source_url) 
133             && (strlen($source_url) > 0) 
134             && !Validate::uri(
135                 $source_url,
136                 array('allowed_schemes' => array('http', 'https'))
137                 )
138             ) 
139         {
140             $this->showForm(_('Source URL is not valid.'));
141             return;
142         } elseif (!is_null($homepage) 
143             && (strlen($homepage) > 0) 
144             && !Validate::uri(
145                 $homepage,
146                 array('allowed_schemes' => array('http', 'https'))
147                 )
148             ) 
149         {
150             $this->showForm(_('Homepage is not a valid URL.'));
151             return; 
152         } elseif (!is_null($callback_url) 
153             && (strlen($callback_url) > 0) 
154             && !Validate::uri(
155                 $source_url,
156                 array('allowed_schemes' => array('http', 'https'))
157                 )
158             ) 
159         {
160             $this->showForm(_('Callback URL is not valid.'));
161             return;
162         }
163         
164         $cur = common_current_user();
165
166         // Checked in prepare() above
167
168         assert(!is_null($cur));
169
170         $app = new Oauth_application();
171
172         $app->query('BEGIN');
173
174         $app->name    = $name;
175         $app->owner  = $cur->id;
176         $app->description = $description;
177         $app->source_url = $souce_url;
178         $app->organization = $organization;
179         $app->homepage = $homepage;
180         $app->callback_url = $callback_url;
181         $app->type = $type;
182         $app->access_type = $access_type;
183         
184         // generate consumer key and secret
185    
186         $app->created     = common_sql_now();
187
188         $result = $app->insert();
189
190         if (!$result) {
191             common_log_db_error($group, 'INSERT', __FILE__);
192             $this->serverError(_('Could not create application.'));
193         }
194        
195         $group->query('COMMIT');
196
197         common_redirect($group->homeUrl(), 303);
198         
199     }
200
201 }
202