]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidlogin.php
Merge remote branch 'gitorious/1.0.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / OpenID / openidlogin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
23
24 class OpenidloginAction extends Action
25 {
26     function handle($args)
27     {
28         parent::handle($args);
29         if (common_is_real_login()) {
30             // TRANS: Client error message trying to log on with OpenID while already logged on.
31             $this->clientError(_m('Already logged in.'));
32         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
33             $provider = common_config('openid', 'trusted_provider');
34             if ($provider) {
35                 $openid_url = $provider;
36                 if (common_config('openid', 'append_username')) {
37                     $openid_url .= $this->trimmed('openid_username');
38                 }
39             } else {
40                 $openid_url = $this->trimmed('openid_url');
41             }
42
43             oid_assert_allowed($openid_url);
44
45             # CSRF protection
46             $token = $this->trimmed('token');
47             if (!$token || $token != common_session_token()) {
48                 // TRANS: Message given when there is a problem with the user's session token.
49                 $this->showForm(_m('There was a problem with your session token. Try again, please.'), $openid_url);
50                 return;
51             }
52
53             $rememberme = $this->boolean('rememberme');
54
55             common_ensure_session();
56
57             $_SESSION['openid_rememberme'] = $rememberme;
58
59             $result = oid_authenticate($openid_url,
60                                        'finishopenidlogin');
61
62             if (is_string($result)) { # error message
63                 unset($_SESSION['openid_rememberme']);
64                 $this->showForm($result, $openid_url);
65             }
66         } else {
67             $openid_url = oid_get_last();
68             $this->showForm(null, $openid_url);
69         }
70     }
71
72     function getInstructions()
73     {
74         if (common_logged_in() && !common_is_real_login() &&
75             common_get_returnto()) {
76             // rememberme logins have to reauthenticate before
77             // changing any profile settings (cookie-stealing protection)
78             // TRANS: OpenID plugin message. Rememberme logins have to reauthenticate before changing any profile settings.
79             // TRANS: "OpenID" is the display text for a link with URL "(%%doc.openid%%)".
80             return _m('For security reasons, please re-login with your ' .
81                      '[OpenID](%%doc.openid%%) ' .
82                      'before changing your settings.');
83         } else {
84             // TRANS: OpenID plugin message.
85             // TRANS: "OpenID" is the display text for a link with URL "(%%doc.openid%%)".
86             return _m('Login with an [OpenID](%%doc.openid%%) account.');
87         }
88     }
89
90     function showPageNotice()
91     {
92         if ($this->error) {
93             $this->element('div', array('class' => 'error'), $this->error);
94         } else {
95             $instr = $this->getInstructions();
96             $output = common_markup_to_html($instr);
97             $this->elementStart('div', 'instructions');
98             $this->raw($output);
99             $this->elementEnd('div');
100         }
101     }
102
103     function showScripts()
104     {
105         parent::showScripts();
106         if (common_config('openid', 'trusted_provider')) {
107             if (common_config('openid', 'append_username')) {
108                 $this->autofocus('openid_username');
109             } else {
110                 $this->autofocus('rememberme');
111             }
112         } else {
113             $this->autofocus('openid_url');
114         }
115     }
116
117     function title()
118     {
119         // TRANS: OpenID plugin message. Title.
120         return _m('OpenID Login');
121     }
122
123     function showForm($error=null, $openid_url)
124     {
125         $this->error = $error;
126         $this->openid_url = $openid_url;
127         $this->showPage();
128     }
129
130     function showContent() {
131         $formaction = common_local_url('openidlogin');
132         $this->elementStart('form', array('method' => 'post',
133                                            'id' => 'form_openid_login',
134                                            'class' => 'form_settings',
135                                            'action' => $formaction));
136         $this->elementStart('fieldset');
137         // TRANS: OpenID plugin logon form legend.
138         $this->element('legend', null, _m('OpenID login'));
139         $this->hidden('token', common_session_token());
140
141         $this->elementStart('ul', 'form_data');
142         $this->elementStart('li');
143         $provider = common_config('openid', 'trusted_provider');
144         $appendUsername = common_config('openid', 'append_username');
145         if ($provider) {
146             $this->element('label', array(), _m('OpenID provider'));
147             $this->element('span', array(), $provider);
148             if ($appendUsername) {
149                 $this->element('input', array('id' => 'openid_username',
150                                               'name' => 'openid_username',
151                                               'style' => 'float: none'));
152             }
153             $this->element('p', 'form_guide',
154                            ($appendUsername ? _m('Enter your username.') . ' ' : '') .
155                            _m('You will be sent to the provider\'s site for authentication.'));
156             $this->hidden('openid_url', $provider);
157         } else {
158             // TRANS: OpenID plugin logon form field label.
159             $this->input('openid_url', _m('OpenID URL'),
160                          $this->openid_url,
161                         // TRANS: OpenID plugin logon form field instructions.
162                          _m('Your OpenID URL'));
163         }
164         $this->elementEnd('li');
165         $this->elementStart('li', array('id' => 'settings_rememberme'));
166         // TRANS: OpenID plugin logon form checkbox label for setting to put the OpenID information in a cookie.
167         $this->checkbox('rememberme', _m('Remember me'), false,
168                         // TRANS: OpenID plugin logon form field instructions.
169                         _m('Automatically login in the future; ' .
170                            'not for shared computers!'));
171         $this->elementEnd('li');
172         $this->elementEnd('ul');
173         // TRANS: OpenID plugin logon form button label to start logon with the data provided in the logon form.
174         $this->submit('submit', _m('BUTTON', 'Login'));
175         $this->elementEnd('fieldset');
176         $this->elementEnd('form');
177     }
178
179     function showLocalNav()
180     {
181         $nav = new LoginGroupNav($this);
182         $nav->show();
183     }
184 }