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