]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/openidsettings.php
change function headers to K&R style
[quix0rs-gnu-social.git] / 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     {
29         return _('[OpenID](%%doc.openid%%) lets you log into many sites ' .
30                   ' with the same user account. '.
31                   ' Manage your associated OpenIDs from here.');
32     }
33
34     function show_form($msg=null, $success=false)
35     {
36
37         $user = common_current_user();
38
39         $this->form_header(_('OpenID settings'), $msg, $success);
40
41         common_element_start('form', array('method' => 'post',
42                                            'id' => 'openidadd',
43                                            'action' =>
44                                            common_local_url('openidsettings')));
45         common_hidden('token', common_session_token());
46         common_element('h2', null, _('Add OpenID'));
47         common_element('p', null,
48                        _('If you want to add an OpenID to your account, ' .
49                           'enter it in the box below and click "Add".'));
50         common_element_start('p');
51         common_element('label', array('for' => 'openid_url'),
52                        _('OpenID URL'));
53         common_element('input', array('name' => 'openid_url',
54                                       'type' => 'text',
55                                       'id' => 'openid_url'));
56         common_element('input', array('type' => 'submit',
57                                       'id' => 'add',
58                                       'name' => 'add',
59                                       'class' => 'submit',
60                                       'value' => _('Add')));
61         common_element_end('p');
62         common_element_end('form');
63
64         $oid = new User_openid();
65         $oid->user_id = $user->id;
66
67         $cnt = $oid->find();
68
69         if ($cnt > 0) {
70
71             common_element('h2', null, _('Remove OpenID'));
72
73             if ($cnt == 1 && !$user->password) {
74
75                 common_element('p', null,
76                                _('Removing your only OpenID would make it impossible to log in! ' .
77                                   'If you need to remove it, add another OpenID first.'));
78
79                 if ($oid->fetch()) {
80                     common_element_start('p');
81                     common_element('a', array('href' => $oid->canonical),
82                                    $oid->display);
83                     common_element_end('p');
84                 }
85
86             } else {
87
88                 common_element('p', null,
89                                _('You can remove an OpenID from your account '.
90                                   'by clicking the button marked "Remove".'));
91                 $idx = 0;
92
93                 while ($oid->fetch()) {
94                     common_element_start('form', array('method' => 'POST',
95                                                        'id' => 'openiddelete' . $idx,
96                                                        'action' =>
97                                                        common_local_url('openidsettings')));
98                     common_element_start('p');
99                     common_hidden('token', common_session_token());
100                     common_element('a', array('href' => $oid->canonical),
101                                    $oid->display);
102                     common_element('input', array('type' => 'hidden',
103                                                   'id' => 'openid_url'.$idx,
104                                                   'name' => 'openid_url',
105                                                   'value' => $oid->canonical));
106                     common_element('input', array('type' => 'submit',
107                                                   'id' => 'remove'.$idx,
108                                                   'name' => 'remove',
109                                                   'class' => 'submit',
110                                                   'value' => _('Remove')));
111                     common_element_end('p');
112                     common_element_end('form');
113                     $idx++;
114                 }
115             }
116         }
117
118         common_show_footer();
119     }
120
121     function handle_post()
122     {
123         # CSRF protection
124         $token = $this->trimmed('token');
125         if (!$token || $token != common_session_token()) {
126             $this->show_form(_('There was a problem with your session token. Try again, please.'));
127             return;
128         }
129
130         if ($this->arg('add')) {
131             $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
132             if (is_string($result)) { # error message
133                 $this->show_form($result);
134             }
135         } else if ($this->arg('remove')) {
136             $this->remove_openid();
137         } else {
138             $this->show_form(_('Something weird happened.'));
139         }
140     }
141
142     function remove_openid()
143     {
144
145         $openid_url = $this->trimmed('openid_url');
146         $oid = User_openid::staticGet('canonical', $openid_url);
147         if (!$oid) {
148             $this->show_form(_('No such OpenID.'));
149             return;
150         }
151         $cur = common_current_user();
152         if (!$cur || $oid->user_id != $cur->id) {
153             $this->show_form(_('That OpenID does not belong to you.'));
154             return;
155         }
156         $oid->delete();
157         $this->show_form(_('OpenID removed.'), true);
158         return;
159     }
160 }