]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Merge branch '0.9.x' into 1.0.x
[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
46 class LoginAction extends Action
47 {
48     /**
49      * Has there been an error?
50      */
51
52     var $error = null;
53
54     /**
55      * Is this a read-only action?
56      *
57      * @return boolean false
58      */
59
60     function isReadOnly($args)
61     {
62         return false;
63     }
64
65     /**
66      * Handle input, produce output
67      *
68      * Switches on request method; either shows the form or handles its input.
69      *
70      * @param array $args $_REQUEST data
71      *
72      * @return void
73      */
74
75     function handle($args)
76     {
77         parent::handle($args);
78
79         if (common_is_real_login()) {
80             $this->clientError(_('Already logged in.'));
81         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
82             $this->checkLogin();
83         } else {
84             common_ensure_session();
85             $this->showForm();
86         }
87     }
88
89     /**
90      * Check the login data
91      *
92      * Determines if the login data is valid. If so, logs the user
93      * in, and redirects to the 'with friends' page, or to the stored
94      * return-to URL.
95      *
96      * @return void
97      */
98
99     function checkLogin($user_id=null)
100     {
101         // XXX: login throttle
102
103         $nickname = $this->trimmed('nickname');
104         $password = $this->arg('password');
105
106         $user = common_check_user($nickname, $password);
107
108         if (!$user) {
109             $this->showForm(_('Incorrect username or password.'));
110             return;
111         }
112
113         // success!
114         if (!common_set_user($user)) {
115             $this->serverError(_('Error setting user. You are probably not authorized.'));
116             return;
117         }
118
119         common_real_login(true);
120
121         if ($this->boolean('rememberme')) {
122             common_rememberme($user);
123         }
124
125         $url = common_get_returnto();
126
127         if ($url) {
128             // We don't have to return to it again
129             common_set_returnto(null);
130             $url = common_inject_session($url);
131         } else {
132             $url = common_local_url('all',
133                                     array('nickname' =>
134                                           $user->nickname));
135         }
136
137         common_redirect($url, 303);
138     }
139
140     /**
141      * Store an error and show the page
142      *
143      * This used to show the whole page; now, it's just a wrapper
144      * that stores the error in an attribute.
145      *
146      * @param string $error error, if any.
147      *
148      * @return void
149      */
150
151     function showForm($error=null)
152     {
153         $this->error = $error;
154         $this->showPage();
155     }
156
157     function showScripts()
158     {
159         parent::showScripts();
160         $this->autofocus('nickname');
161     }
162
163     /**
164      * Title of the page
165      *
166      * @return string title of the page
167      */
168
169     function title()
170     {
171         return _('Login');
172     }
173
174     /**
175      * Show page notice
176      *
177      * Display a notice for how to use the page, or the
178      * error if it exists.
179      *
180      * @return void
181      */
182
183     function showPageNotice()
184     {
185         if ($this->error) {
186             $this->element('p', 'error', $this->error);
187         } else {
188             $instr  = $this->getInstructions();
189             $output = common_markup_to_html($instr);
190
191             $this->raw($output);
192         }
193     }
194
195     /**
196      * Core of the display code
197      *
198      * Shows the login form.
199      *
200      * @return void
201      */
202
203     function showContent()
204     {
205         $this->elementStart('form', array('method' => 'post',
206                                           'id' => 'form_login',
207                                           'class' => 'form_settings',
208                                           'action' => common_local_url('login')));
209         $this->elementStart('fieldset');
210         $this->element('legend', null, _('Login to site'));
211         $this->elementStart('ul', 'form_data');
212         $this->elementStart('li');
213         $this->input('nickname', _('Nickname'));
214         $this->elementEnd('li');
215         $this->elementStart('li');
216         $this->password('password', _('Password'));
217         $this->elementEnd('li');
218         $this->elementStart('li');
219         $this->checkbox('rememberme', _('Remember me'), false,
220                         _('Automatically login in the future; ' .
221                           'not for shared computers!'));
222         $this->elementEnd('li');
223         $this->elementEnd('ul');
224         $this->submit('submit', _('Login'));
225         $this->elementEnd('fieldset');
226         $this->elementEnd('form');
227         $this->elementStart('p');
228         $this->element('a', array('href' => common_local_url('recoverpassword')),
229                        _('Lost or forgotten password?'));
230         $this->elementEnd('p');
231     }
232
233     /**
234      * Instructions for using the form
235      *
236      * For "remembered" logins, we make the user re-login when they
237      * try to change settings. Different instructions for this case.
238      *
239      * @return void
240      */
241
242     function getInstructions()
243     {
244         if (common_logged_in() && !common_is_real_login() &&
245             common_get_returnto()) {
246             // rememberme logins have to reauthenticate before
247             // changing any profile settings (cookie-stealing protection)
248             return _('For security reasons, please re-enter your ' .
249                      'user name and password ' .
250                      'before changing your settings.');
251         } else {
252             $prompt = _('Login with your username and password.');
253             if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
254                 $prompt .= ' ';
255                 $prompt .= _('Don\'t have a username yet? ' .
256                              '[Register](%%action.register%%) a new account.');
257             }
258             return $prompt;
259         }
260     }
261
262     /**
263      * A local menu
264      *
265      * Shows different login/register actions.
266      *
267      * @return void
268      */
269
270     function showLocalNav()
271     {
272         $nav = new LoginGroupNav($this);
273         $nav->show();
274     }
275 }