]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/openidsettings.php
dd575bb737837c97b4f1426d38950223ebd037e1
[quix0rs-gnu-social.git] / _darcs / pristine / actions / openidsettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/openid.php');
24
25 class OpenidsettingsAction extends SettingsAction {
26
27     function get_instructions() {
28         return _('[OpenID](%%doc.openid%%) lets you log into many sites ' .
29                   ' with the same user account. '.
30                   ' Manage your associated OpenIDs from here.');
31     }
32
33     function show_form($msg=NULL, $success=false) {
34
35         $user = common_current_user();
36
37         $this->form_header(_('OpenID settings'), $msg, $success);
38
39         common_element_start('form', array('method' => 'post',
40                                            'id' => 'openidadd',
41                                            'action' =>
42                                            common_local_url('openidsettings')));
43         common_hidden('token', common_session_token());
44         common_element('h2', NULL, _('Add OpenID'));
45         common_element('p', NULL,
46                        _('If you want to add an OpenID to your account, ' .
47                           'enter it in the box below and click "Add".'));
48         common_element_start('p');
49         common_element('label', array('for' => 'openid_url'),
50                        _('OpenID URL'));
51         common_element('input', array('name' => 'openid_url',
52                                       'type' => 'text',
53                                       'id' => 'openid_url'));
54         common_element('input', array('type' => 'submit',
55                                       'id' => 'add',
56                                       'name' => 'add',
57                                       'class' => 'submit',
58                                       'value' => _('Add')));
59         common_element_end('p');
60         common_element_end('form');
61
62         $oid = new User_openid();
63         $oid->user_id = $user->id;
64
65         $cnt = $oid->find();
66
67         if ($cnt > 0) {
68
69             common_element('h2', NULL, _('Remove OpenID'));
70
71             if ($cnt == 1 && !$user->password) {
72
73                 common_element('p', NULL,
74                                _('Removing your only OpenID would make it impossible to log in! ' .
75                                   'If you need to remove it, add another OpenID first.'));
76
77                 if ($oid->fetch()) {
78                     common_element_start('p');
79                     common_element('a', array('href' => $oid->canonical),
80                                    $oid->display);
81                     common_element_end('p');
82                 }
83
84             } else {
85
86                 common_element('p', NULL,
87                                _('You can remove an OpenID from your account '.
88                                   'by clicking the button marked "Remove".'));
89                 $idx = 0;
90
91                 while ($oid->fetch()) {
92                     common_element_start('form', array('method' => 'POST',
93                                                        'id' => 'openiddelete' . $idx,
94                                                        'action' =>
95                                                        common_local_url('openidsettings')));
96                     common_element_start('p');
97                     common_hidden('token', common_session_token());
98                     common_element('a', array('href' => $oid->canonical),
99                                    $oid->display);
100                     common_element('input', array('type' => 'hidden',
101                                                   'id' => 'openid_url'.$idx,
102                                                   'name' => 'openid_url',
103                                                   'value' => $oid->canonical));
104                     common_element('input', array('type' => 'submit',
105                                                   'id' => 'remove'.$idx,
106                                                   'name' => 'remove',
107                                                   'class' => 'submit',
108                                                   'value' => _('Remove')));
109                     common_element_end('p');
110                     common_element_end('form');
111                     $idx++;
112                 }
113             }
114         }
115
116         common_show_footer();
117     }
118
119     function handle_post() {
120         # CSRF protection
121         $token = $this->trimmed('token');
122         if (!$token || $token != common_session_token()) {
123             $this->show_form(_('There was a problem with your session token. Try again, please.'));
124             return;
125         }
126
127         if ($this->arg('add')) {
128             $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
129             if (is_string($result)) { # error message
130                 $this->show_form($result);
131             }
132         } else if ($this->arg('remove')) {
133             $this->remove_openid();
134         } else {
135             $this->show_form(_('Something weird happened.'));
136         }
137     }
138
139     function remove_openid() {
140
141         $openid_url = $this->trimmed('openid_url');
142         $oid = User_openid::staticGet('canonical', $openid_url);
143         if (!$oid) {
144             $this->show_form(_('No such OpenID.'));
145             return;
146         }
147         $cur = common_current_user();
148         if (!$cur || $oid->user_id != $cur->id) {
149             $this->show_form(_('That OpenID does not belong to you.'));
150             return;
151         }
152         $oid->delete();
153         $this->show_form(_('OpenID removed.'), true);
154         return;
155     }
156 }