]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/openidsettings.php
do some commits
[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 show_top($arr) {
28                 $msg = $arr[0];
29                 $success = $arr[1];
30                 
31                 if ($msg) {
32                         $this->message($msg, $success);
33                 } else {
34                         common_element('div', 'instructions',
35                                                    _t('Manage your associated OpenIDs from here.'));
36                 }
37                 
38                 $this->settings_menu();
39         }
40         
41         function show_form($msg=NULL, $success=false) {
42                 
43                 $user = common_current_user();
44                 
45                 common_show_header(_t('OpenID settings'), NULL, array($msg, $success),
46                                                    array($this, 'show_top'));
47
48                 common_element_start('form', array('method' => 'POST',
49                                                                                    'id' => 'openidadd',
50                                                                                    'action' =>
51                                                                                    common_local_url('openidsettings')));
52                 common_element('h2', NULL, _t('Add OpenID'));
53                 common_element('p', NULL,
54                                            _t('If you want to add an OpenID to your account, ' .
55                                                   'enter it in the box below and click "Add".'));
56                 common_element_start('p');
57                 common_element('label', array('for' => 'openid_url'),
58                                            _t('OpenID URL'));
59                 common_element('input', array('name' => 'openid_url',
60                                                                           'type' => 'text',
61                                                                           'id' => 'openid_url'));
62                 common_element('input', array('type' => 'submit',
63                                                                           'id' => 'add',
64                                                                           'name' => 'add',
65                                                                           'class' => 'submit',
66                                                                           'value' => _t('Add')));
67                 common_element_end('p');
68                 common_element_end('form');
69
70                 $oid = new User_openid();
71                 $oid->user_id = $user->id;
72                 
73                 if ($oid->find()) {
74                         
75                         common_element('h2', NULL, _t('OpenID'));
76                         common_element('p', NULL,
77                                                    _t('You can remove an OpenID from your account '.
78                                                           'by clicking the button marked "Delete" next to it.'));
79                         $idx = 0;
80                         
81                         while ($oid->fetch()) {
82                                 common_element_start('form', array('method' => 'POST',
83                                                                                                    'id' => 'openiddelete' . $idx,
84                                                                                                    'action' =>
85                                                                                                    common_local_url('openidsettings')));
86                                 common_element_start('p');
87                                 common_element('a', array('href' => $oid->canonical),
88                                                            $oid->display);
89                                 common_element('input', array('type' => 'hidden',
90                                                                                           'id' => 'openid_url'.$idx,
91                                                                                           'name' => 'openid_url',
92                                                                                           'value' => $oid->canonical));
93                                 common_element('input', array('type' => 'submit',
94                                                                                           'id' => 'remove'.$idx,
95                                                                                           'name' => 'remove',
96                                                                                           'class' => 'submit',
97                                                                                           'value' => _t('Remove')));
98                                 common_element_end('p');
99                                 common_element_end('form');
100                                 $idx++;
101                         }
102                 }
103                 
104                 common_show_footer();
105         }
106
107         function handle_post() {
108                 if ($this->arg('add')) {
109                         $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
110                         if (is_string($result)) { # error message
111                                 $this->show_form($result);
112                         }
113                 } else if ($this->arg('remove')) {
114                         $this->remove_openid();
115                 } else {
116                         $this->show_form(_t('Something weird happened.'));
117                 }
118         }
119
120         function remove_openid() {
121                 
122                 $openid_url = $this->trimmed('openid_url');
123                 $oid = User_openid::staticGet('canonical', $openid_url);
124                 if (!$oid) {
125                         $this->show_form(_t('No such OpenID.'));
126                         return;
127                 }
128                 $cur = common_current_user();
129                 if (!$cur || $oid->user_id != $cur->id) {
130                         $this->show_form(_t('That OpenID does not belong to you.'));
131                         return;
132                 }
133                 $oid->delete();
134                 $this->show_form(_t('OpenID removed.'), true);
135                 return;
136         }
137 }