]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidlogin.php
Regression fix for Recaptcha on SSL registration page; their API is served on a diffe...
[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             $this->clientError(_m('Already logged in.'));
31         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32             $provider = common_config('openid', 'trusted_provider');
33             if ($provider) {
34                 $openid_url = $provider;
35                 if (common_config('openid', 'append_username')) {
36                     $openid_url .= $this->trimmed('openid_username');
37                 }
38             } else {
39                 $openid_url = $this->trimmed('openid_url');
40             }
41
42             oid_assert_allowed($openid_url);
43
44             # CSRF protection
45             $token = $this->trimmed('token');
46             if (!$token || $token != common_session_token()) {
47                 $this->showForm(_m('There was a problem with your session token. Try again, please.'), $openid_url);
48                 return;
49             }
50
51             $rememberme = $this->boolean('rememberme');
52
53             common_ensure_session();
54
55             $_SESSION['openid_rememberme'] = $rememberme;
56
57             $result = oid_authenticate($openid_url,
58                                        'finishopenidlogin');
59
60             if (is_string($result)) { # error message
61                 unset($_SESSION['openid_rememberme']);
62                 $this->showForm($result, $openid_url);
63             }
64         } else {
65             $openid_url = oid_get_last();
66             $this->showForm(null, $openid_url);
67         }
68     }
69
70     function getInstructions()
71     {
72         if (common_logged_in() && !common_is_real_login() &&
73             common_get_returnto()) {
74             // rememberme logins have to reauthenticate before
75             // changing any profile settings (cookie-stealing protection)
76             return _m('For security reasons, please re-login with your ' .
77                      '[OpenID](%%doc.openid%%) ' .
78                      'before changing your settings.');
79         } else {
80             return _m('Login with an [OpenID](%%doc.openid%%) account.');
81         }
82     }
83
84     function showPageNotice()
85     {
86         if ($this->error) {
87             $this->element('div', array('class' => 'error'), $this->error);
88         } else {
89             $instr = $this->getInstructions();
90             $output = common_markup_to_html($instr);
91             $this->elementStart('div', 'instructions');
92             $this->raw($output);
93             $this->elementEnd('div');
94         }
95     }
96
97     function showScripts()
98     {
99         parent::showScripts();
100         if (common_config('openid', 'trusted_provider')) {
101             if (common_config('openid', 'append_username')) {
102                 $this->autofocus('openid_username');
103             } else {
104                 $this->autofocus('rememberme');
105             }
106         } else {
107             $this->autofocus('openid_url');
108         }
109     }
110
111     function title()
112     {
113         return _m('OpenID Login');
114     }
115
116     function showForm($error=null, $openid_url)
117     {
118         $this->error = $error;
119         $this->openid_url = $openid_url;
120         $this->showPage();
121     }
122
123     function showContent() {
124         $formaction = common_local_url('openidlogin');
125         $this->elementStart('form', array('method' => 'post',
126                                            'id' => 'form_openid_login',
127                                            'class' => 'form_settings',
128                                            'action' => $formaction));
129         $this->elementStart('fieldset');
130         $this->element('legend', null, _m('OpenID login'));
131         $this->hidden('token', common_session_token());
132
133         $this->elementStart('ul', 'form_data');
134         $this->elementStart('li');
135         $provider = common_config('openid', 'trusted_provider');
136         $appendUsername = common_config('openid', 'append_username');
137         if ($provider) {
138             $this->element('label', array(), _m('OpenID provider'));
139             $this->element('span', array(), $provider);
140             if ($appendUsername) {
141                 $this->element('input', array('id' => 'openid_username',
142                                               'name' => 'openid_username',
143                                               'style' => 'float: none'));
144             }
145             $this->element('p', 'form_guide',
146                            ($appendUsername ? _m('Enter your username.') . ' ' : '') .
147                            _m('You will be sent to the provider\'s site for authentication.'));
148             $this->hidden('openid_url', $provider);
149         } else {
150             $this->input('openid_url', _m('OpenID URL'),
151                          $this->openid_url,
152                          _m('Your OpenID URL'));
153         }
154         $this->elementEnd('li');
155         $this->elementStart('li', array('id' => 'settings_rememberme'));
156         $this->checkbox('rememberme', _m('Remember me'), false,
157                         _m('Automatically login in the future; ' .
158                            'not for shared computers!'));
159         $this->elementEnd('li');
160         $this->elementEnd('ul');
161         $this->submit('submit', _m('Login'));
162         $this->elementEnd('fieldset');
163         $this->elementEnd('form');
164     }
165
166     function showLocalNav()
167     {
168         $nav = new LoginGroupNav($this);
169         $nav->show();
170     }
171 }