We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

login.php 8.93 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5
 * Login form
6 7 8 9
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
10 11 12
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Evan Prodromou's avatar
Evan Prodromou committed
13
 *
Evan Prodromou's avatar
Evan Prodromou committed
14 15 16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
18
 *
Evan Prodromou's avatar
Evan Prodromou committed
19 20
 * You should have received a copy of the GNU Affero General Public License
 * 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/
Evan Prodromou's avatar
Evan Prodromou committed
29 30
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34 35
    exit(1);
}

/**
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?
     */
Evan Prodromou's avatar
Evan Prodromou committed
50
    var $error = null;
51 52 53 54

    /**
     * Is this a read-only action?
     *
Evan Prodromou's avatar
Evan Prodromou committed
55
     * @return boolean false
56
     */
57
    function isReadOnly($args)
58
    {
Evan Prodromou's avatar
Evan Prodromou committed
59
        return false;
60
    }
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    /**
     * Prepare page to run
     *
     *
     * @param $args
     * @return string title
     */
    function prepare($args)
    {
        parent::prepare($args);

        // @todo this check should really be in index.php for all sensitive actions
        $ssl = common_config('site', 'ssl');
        if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
            common_redirect(common_local_url('login'));
            // exit
        }

        return true;
    }

83 84 85 86 87 88 89 90 91
    /**
     * Handle input, produce output
     *
     * Switches on request method; either shows the form or handles its input.
     *
     * @param array $args $_REQUEST data
     *
     * @return void
     */
92 93
    function handle($args)
    {
94
        parent::handle($args);
95

96
        if (common_is_real_login()) {
97 98
            $user = common_current_user();
            common_redirect(common_local_url('all', array('nickname' => $user->nickname)), 307);
99
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
100
            $this->checkLogin();
101
        } else {
102
            common_ensure_session();
103
            $this->showForm();
104 105
        }
    }
106

107 108 109 110 111 112 113 114 115
    /**
     * Check the login data
     *
     * Determines if the login data is valid. If so, logs the user
     * in, and redirects to the 'with friends' page, or to the stored
     * return-to URL.
     *
     * @return void
     */
116
    function checkLogin($user_id=null, $token=null)
117
    {
118 119 120 121 122 123 124
        // XXX: login throttle

        $nickname = $this->trimmed('nickname');
        $password = $this->arg('password');

        $user = common_check_user($nickname, $password);

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 129
            return;
        }
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 136
            return;
        }
137

138
        common_real_login(true);
139

140 141 142
        if ($this->boolean('rememberme')) {
            common_rememberme($user);
        }
143

144
        $url = common_get_returnto();
145

146
        if ($url) {
147
            // We don't have to return to it again
Evan Prodromou's avatar
Evan Prodromou committed
148
            common_set_returnto(null);
149
	    $url = common_inject_session($url);
150 151 152
        } else {
            $url = common_local_url('all',
                                    array('nickname' =>
153
                                          $user->nickname));
154
        }
155

156
        common_redirect($url, 303);
157
    }
Evan Prodromou's avatar
Evan Prodromou committed
158

159 160 161 162 163 164 165 166 167 168
    /**
     * Store an error and show the page
     *
     * This used to show the whole page; now, it's just a wrapper
     * that stores the error in an attribute.
     *
     * @param string $error error, if any.
     *
     * @return void
     */
169
    function showForm($error=null)
170
    {
sarven's avatar
sarven committed
171 172
        $this->error = $error;
        $this->showPage();
173 174
    }

175 176 177 178 179 180
    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('nickname');
    }

181 182 183 184 185
    /**
     * Title of the page
     *
     * @return string title of the page
     */
186 187
    function title()
    {
188
        // TRANS: Page title for login page.
sarven's avatar
sarven committed
189
        return _('Login');
190 191
    }

192 193 194 195 196 197 198 199
    /**
     * Show page notice
     *
     * Display a notice for how to use the page, or the
     * error if it exists.
     *
     * @return void
     */
200 201 202 203 204
    function showPageNotice()
    {
        if ($this->error) {
            $this->element('p', 'error', $this->error);
        } else {
205
            $instr  = $this->getInstructions();
206
            $output = common_markup_to_html($instr);
207

208 209 210
            $this->raw($output);
        }
    }
211 212 213 214 215 216 217 218

    /**
     * Core of the display code
     *
     * Shows the login form.
     *
     * @return void
     */
219
    function showContent()
220
    {
221
        $this->elementStart('form', array('method' => 'post',
222 223 224
                                          'id' => 'form_login',
                                          'class' => 'form_settings',
                                          'action' => common_local_url('login')));
sarven's avatar
sarven committed
225
        $this->elementStart('fieldset');
226
        // TRANS: Form legend on login page.
sarven's avatar
sarven committed
227
        $this->element('legend', null, _('Login to site'));
sarven's avatar
sarven committed
228
        $this->elementStart('ul', 'form_data');
sarven's avatar
sarven committed
229
        $this->elementStart('li');
230
        // TRANS: Field label on login page.
231
        $this->input('nickname', _('Username or email address'));
sarven's avatar
sarven committed
232 233
        $this->elementEnd('li');
        $this->elementStart('li');
234
        // TRANS: Field label on login page.
235
        $this->password('password', _('Password'));
sarven's avatar
sarven committed
236 237
        $this->elementEnd('li');
        $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!'));
sarven's avatar
sarven committed
243 244
        $this->elementEnd('li');
        $this->elementEnd('ul');
245 246
        // TRANS: Button text for log in on login page.
        $this->submit('submit', _m('BUTTON','Login'));
sarven's avatar
sarven committed
247
        $this->elementEnd('fieldset');
248 249 250
        $this->elementEnd('form');
        $this->elementStart('p');
        $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 258 259 260 261 262 263
    /**
     * Instructions for using the form
     *
     * For "remembered" logins, we make the user re-login when they
     * try to change settings. Different instructions for this case.
     *
     * @return void
     */
264
    function getInstructions()
265
    {
266 267 268 269
        if (common_logged_in() && !common_is_real_login() &&
            common_get_returnto()) {
            // rememberme logins have to reauthenticate before
            // changing any profile settings (cookie-stealing protection)
270
            // TRANS: Form instructions on login page before being able to change user settings.
271 272 273
            return _('For security reasons, please re-enter your ' .
                     'user name and password ' .
                     'before changing your settings.');
274
        } else {
275
            // TRANS: Form instructions on login page.
276 277 278
            $prompt = _('Login with your username and password.');
            if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
                $prompt .= ' ';
279 280
                // TRANS: Form instructions on login page. This message contains Markdown links in the form [Link text](Link).
                // TRANS: %%action.register%% is a link to the registration page.
281 282 283 284
                $prompt .= _('Don\'t have a username yet? ' .
                             '[Register](%%action.register%%) a new account.');
            }
            return $prompt;
285 286
        }
    }
287 288 289 290 291 292 293 294 295 296 297 298 299

    /**
     * A local menu
     *
     * Shows different login/register actions.
     *
     * @return void
     */
    function showLocalNav()
    {
        $nav = new LoginGroupNav($this);
        $nav->show();
    }
300 301 302 303

    function showNoticeForm()
    {
    }
304 305 306 307

    function showProfileBlock()
    {
    }
Evan Prodromou's avatar
Evan Prodromou committed
308
}