]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Merge commit 'refs/merge-requests/157' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / actions / login.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Login form
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Login
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Login form
37  *
38  * @category Personal
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Sarven Capadisli <csarven@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class LoginAction extends Action
46 {
47     /**
48      * Has there been an error?
49      */
50     var $error = null;
51
52     /**
53      * Is this a read-only action?
54      *
55      * @return boolean false
56      */
57     function isReadOnly($args)
58     {
59         return false;
60     }
61
62     /**
63      * Prepare page to run
64      *
65      *
66      * @param $args
67      * @return string title
68      */
69     function prepare($args)
70     {
71         parent::prepare($args);
72
73         // @todo this check should really be in index.php for all sensitive actions
74         $ssl = common_config('site', 'ssl');
75         if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
76             common_redirect(common_local_url('login'));
77             // exit
78         }
79
80         return true;
81     }
82
83     /**
84      * Handle input, produce output
85      *
86      * Switches on request method; either shows the form or handles its input.
87      *
88      * @param array $args $_REQUEST data
89      *
90      * @return void
91      */
92     function handle($args)
93     {
94         parent::handle($args);
95
96         if (common_is_real_login()) {
97             // TRANS: Client error displayed when trying to log in while already logged in.
98             $this->clientError(_('Already logged in.'));
99         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
100             $this->checkLogin();
101         } else {
102             common_ensure_session();
103             $this->showForm();
104         }
105     }
106
107     /**
108      * Check the login data
109      *
110      * Determines if the login data is valid. If so, logs the user
111      * in, and redirects to the 'with friends' page, or to the stored
112      * return-to URL.
113      *
114      * @return void
115      */
116     function checkLogin($user_id=null, $token=null)
117     {
118         // XXX: login throttle
119
120         $nickname = $this->trimmed('nickname');
121         $password = $this->arg('password');
122
123         $user = common_check_user($nickname, $password);
124
125         if (!$user) {
126             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
127             $this->showForm(_('Incorrect username or password.'));
128             return;
129         }
130
131         // success!
132         if (!common_set_user($user)) {
133             // TRANS: Server error displayed when during login a server error occurs.
134             $this->serverError(_('Error setting user. You are probably not authorized.'));
135             return;
136         }
137
138         common_real_login(true);
139
140         if ($this->boolean('rememberme')) {
141             common_rememberme($user);
142         }
143
144         $url = common_get_returnto();
145
146         if ($url) {
147             // We don't have to return to it again
148             common_set_returnto(null);
149             $url = common_inject_session($url);
150         } else {
151             $url = common_local_url('all',
152                                     array('nickname' =>
153                                           $user->nickname));
154         }
155
156         common_redirect($url, 303);
157     }
158
159     /**
160      * Store an error and show the page
161      *
162      * This used to show the whole page; now, it's just a wrapper
163      * that stores the error in an attribute.
164      *
165      * @param string $error error, if any.
166      *
167      * @return void
168      */
169     function showForm($error=null)
170     {
171         $this->error = $error;
172         $this->showPage();
173     }
174
175     function showScripts()
176     {
177         parent::showScripts();
178         $this->autofocus('nickname');
179     }
180
181     /**
182      * Title of the page
183      *
184      * @return string title of the page
185      */
186     function title()
187     {
188         // TRANS: Page title for login page.
189         return _('Login');
190     }
191
192     /**
193      * Show page notice
194      *
195      * Display a notice for how to use the page, or the
196      * error if it exists.
197      *
198      * @return void
199      */
200     function showPageNotice()
201     {
202         if ($this->error) {
203             $this->element('p', 'error', $this->error);
204         } else {
205             $instr  = $this->getInstructions();
206             $output = common_markup_to_html($instr);
207
208             $this->raw($output);
209         }
210     }
211
212     /**
213      * Core of the display code
214      *
215      * Shows the login form.
216      *
217      * @return void
218      */
219     function showContent()
220     {
221         $this->elementStart('form', array('method' => 'post',
222                                           'id' => 'form_login',
223                                           'class' => 'form_settings',
224                                           'action' => common_local_url('login')));
225         $this->elementStart('fieldset');
226         // TRANS: Form legend on login page.
227         $this->element('legend', null, _('Login to site'));
228         $this->elementStart('ul', 'form_data');
229         $this->elementStart('li');
230         // TRANS: Field label on login page.
231         $this->input('nickname', _('Username or email address'));
232         $this->elementEnd('li');
233         $this->elementStart('li');
234         // TRANS: Field label on login page.
235         $this->password('password', _('Password'));
236         $this->elementEnd('li');
237         $this->elementStart('li');
238         // TRANS: Checkbox label label on login page.
239         $this->checkbox('rememberme', _('Remember me'), false,
240                         // TRANS: Checkbox title on login page.
241                         _('Automatically login in the future; ' .
242                           'not for shared computers!'));
243         $this->elementEnd('li');
244         $this->elementEnd('ul');
245         // TRANS: Button text for log in on login page.
246         $this->submit('submit', _m('BUTTON','Login'));
247         $this->elementEnd('fieldset');
248         $this->elementEnd('form');
249         $this->elementStart('p');
250         $this->element('a', array('href' => common_local_url('recoverpassword')),
251                        // TRANS: Link text for link to "reset password" on login page.
252                        _('Lost or forgotten password?'));
253         $this->elementEnd('p');
254     }
255
256     /**
257      * Instructions for using the form
258      *
259      * For "remembered" logins, we make the user re-login when they
260      * try to change settings. Different instructions for this case.
261      *
262      * @return void
263      */
264     function getInstructions()
265     {
266         if (common_logged_in() && !common_is_real_login() &&
267             common_get_returnto()) {
268             // rememberme logins have to reauthenticate before
269             // changing any profile settings (cookie-stealing protection)
270             // TRANS: Form instructions on login page before being able to change user settings.
271             return _('For security reasons, please re-enter your ' .
272                      'user name and password ' .
273                      'before changing your settings.');
274         } else {
275             // TRANS: Form instructions on login page.
276             $prompt = _('Login with your username and password.');
277             if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
278                 $prompt .= ' ';
279                 // TRANS: Form instructions on login page. This message contains Markdown links in the form [Link text](Link).
280                 // TRANS: %%action.register%% is a link to the registration page.
281                 $prompt .= _('Don\'t have a username yet? ' .
282                              '[Register](%%action.register%%) a new account.');
283             }
284             return $prompt;
285         }
286     }
287
288     /**
289      * A local menu
290      *
291      * Shows different login/register actions.
292      *
293      * @return void
294      */
295     function showLocalNav()
296     {
297         $nav = new LoginGroupNav($this);
298         $nav->show();
299     }
300
301     function showNoticeForm()
302     {
303     }
304
305     function showProfileBlock()
306     {
307     }
308 }