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