]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showapplication.php
Give NoticeListItem its own class
[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 class ShowApplicationAction extends OwnerDesignAction
44 {
45     /**
46      * Application to show
47      */
48     var $application = null;
49
50     /**
51      * User who owns the app
52      */
53     var $owner = null;
54
55     var $msg = null;
56
57     var $success = null;
58
59     /**
60      * Load attributes based on database arguments
61      *
62      * Loads all the DB stuff
63      *
64      * @param array $args $_REQUEST array
65      *
66      * @return success flag
67      */
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $id = (int)$this->arg('id');
73
74         $this->application  = Oauth_application::staticGet($id);
75         $this->owner        = User::staticGet($this->application->owner);
76
77         if (!common_logged_in()) {
78             $this->clientError(_('You must be logged in to view an application.'));
79             return false;
80         }
81
82         if (empty($this->application)) {
83             $this->clientError(_('No such application.'), 404);
84             return false;
85         }
86
87         $cur = common_current_user();
88
89         if ($cur->id != $this->owner->id) {
90             $this->clientError(_('You are not the owner of this application.'), 401);
91             return false;
92         }
93
94         return true;
95     }
96
97     /**
98      * Handle the request
99      *
100      * Shows info about the app
101      *
102      * @return void
103      */
104     function handle($args)
105     {
106         parent::handle($args);
107
108         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
109
110             // CSRF protection
111             $token = $this->trimmed('token');
112             if (!$token || $token != common_session_token()) {
113                 $this->clientError(_('There was a problem with your session token.'));
114                 return;
115             }
116
117             if ($this->arg('reset')) {
118                 $this->resetKey();
119             }
120         } else {
121             $this->showPage();
122         }
123     }
124
125     /**
126      * Title of the page
127      *
128      * @return string title of the page
129      */
130     function title()
131     {
132         if (!empty($this->application->name)) {
133             return 'Application: ' . $this->application->name;
134         }
135     }
136
137     function showPageNotice()
138     {
139         if (!empty($this->msg)) {
140             $this->element('div', ($this->success) ? 'success' : 'error', $this->msg);
141         }
142     }
143
144     function showContent()
145     {
146         $cur = common_current_user();
147
148         $consumer = $this->application->getConsumer();
149
150         $this->elementStart('div', 'entity_profile vcard');
151         $this->element('h2', null, _('Application profile'));
152         if (!empty($this->application->icon)) {
153             $this->element('img', array('src' => $this->application->icon,
154                                         'class' => 'photo logo entity_depiction'));
155         }
156
157         $this->element('a', array('href' =>  $this->application->source_url,
158                                   'class' => 'url fn entity_fn'),
159                             $this->application->name);
160
161         $this->element('a', array('href' =>  $this->application->homepage,
162                                   'class' => 'url entity_org'),
163                             $this->application->organization);
164
165         $this->element('div',
166                        'note entity_note',
167                        $this->application->description);
168
169         $this->elementStart('div', 'entity_statistics');
170         $defaultAccess = ($this->application->access_type & Oauth_application::$writeAccess)
171             ? 'read-write' : 'read-only';
172         $profile = Profile::staticGet($this->application->owner);
173
174         $appUsers = new Oauth_application_user();
175         $appUsers->application_id = $this->application->id;
176         $userCnt = $appUsers->count();
177
178         $this->raw(sprintf(
179             _('Created by %1$s - %2$s access by default - %3$d users'),
180               $profile->getBestName(),
181               $defaultAccess,
182               $userCnt
183             ));
184         $this->elementEnd('div');
185
186         $this->elementEnd('div');
187
188         $this->elementStart('div', 'entity_actions');
189         $this->element('h2', null, _('Application actions'));
190         $this->elementStart('ul');
191         $this->elementStart('li', 'entity_edit');
192         $this->element('a',
193                        array('href' => common_local_url('editapplication',
194                                                         array('id' => $this->application->id))),
195                        'Edit');
196         $this->elementEnd('li');
197
198         $this->elementStart('li', 'entity_reset_keysecret');
199         $this->elementStart('form', array(
200             'id' => 'form_reset_key',
201             'class' => 'form_reset_key',
202             'method' => 'POST',
203             'action' => common_local_url('showapplication',
204                                          array('id' => $this->application->id))));
205         $this->elementStart('fieldset');
206         $this->hidden('token', common_session_token());
207
208         $this->element('input', array('type' => 'submit',
209                                       'id' => 'reset',
210                                       'name' => 'reset',
211                                       'class' => 'submit',
212                                       'value' => _('Reset key & secret'),
213                                       'onClick' => 'return confirmReset()'));
214         $this->elementEnd('fieldset');
215         $this->elementEnd('form');
216         $this->elementEnd('li');
217
218         $this->elementStart('li', 'entity_delete');
219         $this->elementStart('form', array(
220                                           'id' => 'form_delete_application',
221                                           'class' => 'form_delete_application',
222                                           'method' => 'POST',
223                                           'action' => common_local_url('deleteapplication',
224                                                                        array('id' => $this->application->id))));
225
226         $this->elementStart('fieldset');
227         $this->hidden('token', common_session_token());
228         $this->submit('delete', _('Delete'));
229         $this->elementEnd('fieldset');
230         $this->elementEnd('form');
231         $this->elementEnd('li');
232
233         $this->elementEnd('ul');
234         $this->elementEnd('div');
235
236         $this->elementStart('div', 'entity_data');
237         $this->element('h2', null, _('Application info'));
238         $this->element('div',
239                        'entity_consumer_key',
240                        $consumer->consumer_key);
241
242         $this->element('div',
243                        'entity_consumer_secret',
244                        $consumer->consumer_secret);
245
246         $this->element('div',
247                        'entity_request_token_url',
248                        common_local_url('ApiOauthRequestToken'));
249
250         $this->element('div', 'entity_access_token_url', common_local_url('ApiOauthAccessToken'));
251
252         $this->element('div', 'entity_authorize_url', common_local_url('ApiOauthAuthorize'));
253
254         $this->element('p', 'note',
255             _('Note: We support HMAC-SHA1 signatures. We do not support the plaintext signature method.'));
256         $this->elementEnd('div');
257
258         $this->elementStart('p', array('id' => 'application_action'));
259         $this->element('a',
260             array('href' => common_local_url('oauthappssettings'),
261                   'class' => 'more'),
262                   'View your applications');
263         $this->elementEnd('p');
264     }
265
266     /**
267      * Add a confirm script for Consumer key/secret reset
268      *
269      * @return void
270      */
271     function showScripts()
272     {
273         parent::showScripts();
274
275         $msg = _('Are you sure you want to reset your consumer key and secret?');
276
277         $js  = 'function confirmReset() { ';
278         $js .= '    var agree = confirm("' . $msg . '"); ';
279         $js .= '    return agree;';
280         $js .= '}';
281
282         $this->inlineScript($js);
283     }
284
285     /**
286      * Reset an application's Consumer key and secret
287      *
288      * XXX: Should this be moved to its own page with a confirm?
289      *
290      */
291     function resetKey()
292     {
293         $this->application->query('BEGIN');
294
295         $oauser = new Oauth_application_user();
296         $oauser->application_id = $this->application->id;
297         $result = $oauser->delete();
298
299         if ($result === false) {
300             common_log_db_error($oauser, 'DELETE', __FILE__);
301             $this->success = false;
302             $this->msg = ('Unable to reset consumer key and secret.');
303             $this->showPage();
304             return;
305         }
306
307         $consumer = $this->application->getConsumer();
308         $result = $consumer->delete();
309
310         if ($result === false) {
311             common_log_db_error($consumer, 'DELETE', __FILE__);
312             $this->success = false;
313             $this->msg = ('Unable to reset consumer key and secret.');
314             $this->showPage();
315             return;
316         }
317
318         $consumer = Consumer::generateNew();
319
320         $result = $consumer->insert();
321
322         if (empty($result)) {
323             common_log_db_error($consumer, 'INSERT', __FILE__);
324             $this->application->query('ROLLBACK');
325             $this->success = false;
326             $this->msg = ('Unable to reset consumer key and secret.');
327             $this->showPage();
328             return;
329         }
330
331         $orig = clone($this->application);
332         $this->application->consumer_key = $consumer->consumer_key;
333         $result = $this->application->update($orig);
334
335         if ($result === false) {
336             common_log_db_error($application, 'UPDATE', __FILE__);
337             $this->application->query('ROLLBACK');
338             $this->success = false;
339             $this->msg = ('Unable to reset consumer key and secret.');
340             $this->showPage();
341             return;
342         }
343
344         $this->application->query('COMMIT');
345
346         $this->success = true;
347         $this->msg = ('Consumer key and secret reset.');
348         $this->showPage();
349     }
350 }