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