]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/login.php
Opps, PEAR sucks. Need to call find() before fetch() ... :-(
[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   GNUsocial
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @author    Mikael Nordfeldth <mmn@hethane.se>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @copyright 2013 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://www.gnu.org/software/social/
31  */
32
33 if (!defined('GNUSOCIAL')) { exit(1); }
34
35 class LoginAction extends FormAction
36 {
37     protected $needLogin = false;
38
39     /**
40      * Prepare page to run
41      *
42      *
43      * @param $args
44      * @return string title
45      */
46     protected function prepare(array $args=array())
47     {
48         // @todo this check should really be in index.php for all sensitive actions
49         $ssl = common_config('site', 'ssl');
50         if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
51             common_redirect(common_local_url('login'));
52         }
53
54         return parent::prepare($args);
55     }
56
57     /**
58      * Handle input, produce output
59      *
60      * Switches on request method; either shows the form or handles its input.
61      *
62      * @return void
63      */
64     protected function handle()
65     {
66         if (common_is_real_login()) {
67             common_redirect(common_local_url('all', array('nickname' => $this->scoped->nickname)), 307);
68         }
69
70         return parent::handle();
71     }
72
73     /**
74      * Check the login data
75      *
76      * Determines if the login data is valid. If so, logs the user
77      * in, and redirects to the 'with friends' page, or to the stored
78      * return-to URL.
79      *
80      * @return void
81      */
82     protected function handlePost()
83     {
84         parent::handlePost();
85
86         // XXX: login throttle
87
88         $nickname = $this->trimmed('nickname');
89         $password = $this->arg('password');
90
91         $user = common_check_user($nickname, $password);
92
93         if (!$user instanceof User) {
94             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
95             throw new ServerException(_('Incorrect username or password.'));
96         }
97
98         // success!
99         if (!common_set_user($user)) {
100             // TRANS: Server error displayed when during login a server error occurs.
101             throw new ServerException(_('Error setting user. You are probably not authorized.'));
102         }
103
104         common_real_login(true);
105         $this->updateScopedProfile(); 
106
107         if ($this->boolean('rememberme')) {
108             common_rememberme($user);
109         }
110
111         $url = common_get_returnto();
112
113         if ($url) {
114             // We don't have to return to it again
115             common_set_returnto(null);
116             $url = common_inject_session($url);
117         } else {
118             $url = common_local_url('all',
119                                     array('nickname' => $this->scoped->nickname));
120         }
121
122         common_redirect($url, 303);
123     }
124
125     /**
126      * Store an error and show the page
127      *
128      * This used to show the whole page; now, it's just a wrapper
129      * that stores the error in an attribute.
130      *
131      * @param string $error error, if any.
132      *
133      * @return void
134      */
135     public function showForm($msg=null, $success=false)
136     {
137         common_ensure_session();
138         return parent::showForm($msg, $success);
139     }
140
141     function showScripts()
142     {
143         parent::showScripts();
144         $this->autofocus('nickname');
145     }
146
147     /**
148      * Title of the page
149      *
150      * @return string title of the page
151      */
152     function title()
153     {
154         // TRANS: Page title for login page.
155         return _('Login');
156     }
157
158     /**
159      * Core of the display code
160      *
161      * Shows the login form.
162      *
163      * @return void
164      */
165     function showContent()
166     {
167         $this->elementStart('form', array('method' => 'post',
168                                           'id' => 'form_login',
169                                           'class' => 'form_settings',
170                                           'action' => common_local_url('login')));
171         $this->elementStart('fieldset');
172         // TRANS: Form legend on login page.
173         $this->element('legend', null, _('Login to site'));
174         $this->elementStart('ul', 'form_data');
175         $this->elementStart('li');
176         // TRANS: Field label on login page.
177         $this->input('nickname', _('Username or email address'));
178         $this->elementEnd('li');
179         $this->elementStart('li');
180         // TRANS: Field label on login page.
181         $this->password('password', _('Password'));
182         $this->elementEnd('li');
183         $this->elementStart('li');
184         // TRANS: Checkbox label label on login page.
185         $this->checkbox('rememberme', _('Remember me'), false,
186                         // TRANS: Checkbox title on login page.
187                         _('Automatically login in the future; ' .
188                           'not for shared computers!'));
189         $this->elementEnd('li');
190         $this->elementEnd('ul');
191         // TRANS: Button text for log in on login page.
192         $this->submit('submit', _m('BUTTON','Login'));
193         $this->hidden('token', common_session_token());
194         $this->elementEnd('fieldset');
195         $this->elementEnd('form');
196         $this->elementStart('p');
197         $this->element('a', array('href' => common_local_url('recoverpassword')),
198                        // TRANS: Link text for link to "reset password" on login page.
199                        _('Lost or forgotten password?'));
200         $this->elementEnd('p');
201     }
202
203     /**
204      * Instructions for using the form
205      *
206      * For "remembered" logins, we make the user re-login when they
207      * try to change settings. Different instructions for this case.
208      *
209      * @return void
210      */
211     function getInstructions()
212     {
213         if (common_logged_in() && !common_is_real_login() &&
214             common_get_returnto()) {
215             // rememberme logins have to reauthenticate before
216             // changing any profile settings (cookie-stealing protection)
217             // TRANS: Form instructions on login page before being able to change user settings.
218             return _('For security reasons, please re-enter your ' .
219                      'user name and password ' .
220                      'before changing your settings.');
221         } else {
222             // TRANS: Form instructions on login page.
223             $prompt = _('Login with your username and password.');
224             if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
225                 $prompt .= ' ';
226                 // TRANS: Form instructions on login page. This message contains Markdown links in the form [Link text](Link).
227                 // TRANS: %%action.register%% is a link to the registration page.
228                 $prompt .= _('Don\'t have a username yet? ' .
229                              '[Register](%%action.register%%) a new account.');
230             }
231             return $prompt;
232         }
233     }
234
235     /**
236      * A local menu
237      *
238      * Shows different login/register actions.
239      *
240      * @return void
241      */
242     function showLocalNav()
243     {
244         $nav = new LoginGroupNav($this);
245         $nav->show();
246     }
247
248     function showNoticeForm()
249     {
250     }
251
252     function showProfileBlock()
253     {
254     }
255 }