]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Workflow for registering new OAuth apps pretty much done.
[quix0rs-gnu-social.git] / actions / showapplication.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show an 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  Application
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  * Show an OAuth application
36  *
37  * @category Application
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class ShowApplicationAction extends OwnerDesignAction
45 {
46     /**
47      * Application to show
48      */
49
50     var $application = null;
51
52     /**
53      * User who owns the app
54      */
55
56     var $owner = null;
57
58
59     var $msg = null;
60
61     var $success = null;
62
63     /**
64      * Load attributes based on database arguments
65      *
66      * Loads all the DB stuff
67      *
68      * @param array $args $_REQUEST array
69      *
70      * @return success flag
71      */
72
73     function prepare($args)
74     {
75         parent::prepare($args);
76
77         $id = (int)$this->arg('id');
78
79         $this->application  = Oauth_application::staticGet($id);
80         $this->owner        = User::staticGet($this->application->owner);
81
82         if (!common_logged_in()) {
83             $this->clientError(_('You must be logged in to view an application.'));
84             return false;
85         }
86
87         if (empty($this->application)) {
88             $this->clientError(_('No such application.'), 404);
89             return false;
90         }
91
92         $cur = common_current_user();
93
94         if ($cur->id != $this->owner->id) {
95             $this->clientError(_('You are not the owner of this application.'), 401);
96         }
97
98         return true;
99     }
100
101     /**
102      * Handle the request
103      *
104      * Shows info about the app
105      *
106      * @return void
107      */
108
109     function handle($args)
110     {
111         parent::handle($args);
112
113         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
114
115             // CSRF protection
116             $token = $this->trimmed('token');
117             if (!$token || $token != common_session_token()) {
118                 $this->clientError(_('There was a problem with your session token.'));
119                 return;
120             }
121
122             if ($this->arg('reset')) {
123                 $this->resetKey();
124             }
125         } else {
126             $this->showPage();
127         }
128     }
129
130     /**
131      * Title of the page
132      *
133      * @return string title of the page
134      */
135
136     function title()
137     {
138         if (!empty($this->application->name)) {
139             return 'Application: ' . $this->application->name;
140         }
141     }
142
143     function showPageNotice()
144     {
145         if (!empty($this->msg)) {
146             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
147         }
148     }
149
150     function showContent()
151     {
152
153         $cur = common_current_user();
154
155         $this->elementStart('div', 'entity_actions');
156
157         $this->element('a',
158             array('href' =>
159                 common_local_url(
160                     'editapplication',
161                     array(
162                         'nickname' => $this->owner->nickname,
163                         'id' => $this->application->id
164                     )
165                 )
166             ), 'Edit application');
167
168         $this->elementStart('form', array(
169             'id' => 'forma_reset_key',
170             'class' => 'form_reset_key',
171             'method' => 'POST',
172             'action' => common_local_url('showapplication',
173                 array('nickname' => $cur->nickname,
174                       'id' => $this->application->id))));
175
176         $this->elementStart('fieldset');
177         $this->hidden('token', common_session_token());
178         $this->submit('reset', _('Reset Consumer key/secret'));
179         $this->elementEnd('fieldset');
180         $this->elementEnd('form');
181
182         $this->elementEnd('div');
183
184         $consumer = $this->application->getConsumer();
185
186         $this->elementStart('div', 'entity-application');
187
188         $this->elementStart('ul', 'entity_application_details');
189
190         $this->elementStart('li', 'entity_application_name');
191         $this->element('span', array('class' => 'big'), $this->application->name);
192         $this->raw(sprintf(_(' by %1$s'), $this->application->organization));
193         $this->elementEnd('li');
194
195         $this->element('li', 'entity_application_description', $this->application->description);
196
197         $this->elementStart('li', 'entity_application_statistics');
198
199         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
200             ? 'read-write' : 'read-only';
201         $profile = Profile::staticGet($this->application->owner);
202         $userCnt = 0; // XXX: count how many users use the app
203
204         $this->raw(sprintf(
205             _('Created by %1$s - %2$s access by default - %3$d users.'),
206               $profile->getBestName(),
207               $defaultAccess,
208               $userCnt
209             ));
210
211         $this->elementEnd('li');
212
213         $this->elementEnd('ul');
214
215         $this->elementStart('dl', 'entity_consumer_key');
216         $this->element('dt', null, _('Consumer key'));
217         $this->element('dd', 'label', $consumer->consumer_key);
218         $this->elementEnd('dl');
219
220         $this->elementStart('dl', 'entity_consumer_secret');
221         $this->element('dt', null, _('Consumer secret'));
222         $this->element('dd', 'label', $consumer->consumer_secret);
223         $this->elementEnd('dl');
224
225         $this->elementStart('dl', 'entity_request_token_url');
226         $this->element('dt', null, _('Request token URL'));
227         $this->element('dd', 'label', common_local_url('oauthrequesttoken'));
228         $this->elementEnd('dl');
229
230         $this->elementStart('dl', 'entity_access_token_url');
231         $this->element('dt', null, _('Access token URL'));
232         $this->element('dd', 'label', common_local_url('oauthaccesstoken'));
233         $this->elementEnd('dl');
234
235         $this->elementStart('dl', 'entity_authorize_url');
236         $this->element('dt', null, _('Authorize URL'));
237         $this->element('dd', 'label', common_local_url('oauthauthorize'));
238         $this->elementEnd('dl');
239
240         $this->element('p', 'oauth-signature-note',
241             '*We support hmac-sha1 signatures. We do not support the plaintext signature method.');
242
243         $this->elementEnd('div');
244
245         $this->elementStart('div', 'entity-list-apps');
246         $this->element('a',
247             array(
248                 'href' => common_local_url(
249                     'apps',
250                     array('nickname' => $this->owner->nickname)
251                 )
252             ),
253             'View your applications');
254         $this->elementEnd('div');
255     }
256
257     function resetKey()
258     {
259         $this->application->query('BEGIN');
260
261         $consumer = $this->application->getConsumer();
262         $result = $consumer->delete();
263
264         if (!$result) {
265             common_log_db_error($consumer, 'DELETE', __FILE__);
266             $this->success = false;
267             $this->msg = ('Unable to reset consumer key and secret.');
268             $this->showPage();
269             return;
270         }
271
272         $consumer = Consumer::generateNew();
273
274         $result = $consumer->insert();
275
276         if (!$result) {
277             common_log_db_error($consumer, 'INSERT', __FILE__);
278             $this->application->query('ROLLBACK');
279             $this->success = false;
280             $this->msg = ('Unable to reset consumer key and secret.');
281             $this->showPage();
282             return;
283         }
284
285         $orig = clone($this->application);
286         $this->application->consumer_key = $consumer->consumer_key;
287         $result = $this->application->update($orig);
288
289         if (!$result) {
290             common_log_db_error($application, 'UPDATE', __FILE__);
291             $this->application->query('ROLLBACK');
292             $this->success = false;
293             $this->msg = ('Unable to reset consumer key and secret.');
294             $this->showPage();
295             return;
296         }
297
298         $this->application->query('COMMIT');
299
300         $this->success = true;
301         $this->msg = ('Consumer key and secret reset.');
302         $this->showPage();
303     }
304
305 }
306