Skip to content

Commit ae00595

Browse files
committed
A real "Stay signed in": keep the connection
Instead of trusting the php session, it uses a cookie. The php session sooner or later is distroyed if not used. It depends upon the server settings. Using a cookie ensures that one really stays signed in. Dev notes: I wanted to avoid merge conflicts, stay with the main developper standards and keep the "index.php" in one file. That's why the code may not be that nice. My own dev level my also explain.
1 parent 067e66a commit ae00595

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

index.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
define('shaarli_version','0.0.41 beta');
3838
define('PHPPREFIX','<?php /* '); // Prefix to encapsulate data in php code.
3939
define('PHPSUFFIX',' */ ?>'); // Suffix to encapsulate data in php code.
40+
// http://server.com/x/shaarli --> /shaarli/
41+
define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0)));
4042

4143
// Force cookie path (but do not change lifetime)
4244
$cookie=session_get_cookie_params();
@@ -110,6 +112,8 @@ function stripslashes_deep($value) { $value = is_array($value) ? array_map('stri
110112

111113
require $GLOBALS['config']['CONFIG_FILE']; // Read login/password hash into $GLOBALS.
112114

115+
// a token depending of deployment salt, user password, and the current ip
116+
define('STAY_SIGNED_IN_TOKEN', sha1($GLOBALS['hash'].$_SERVER["REMOTE_ADDR"].$GLOBALS['salt']));
113117

114118
autoLocale(); // Sniff browser language and set date format accordingly.
115119
header('Content-Type: text/html; charset=utf-8'); // We use UTF-8 for proper international characters handling.
@@ -294,16 +298,20 @@ function allIPs()
294298
return $ip;
295299
}
296300

301+
function fillSessionInfo() {
302+
$_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); // generate unique random number (different than phpsessionid)
303+
$_SESSION['ip']=allIPs(); // We store IP address(es) of the client to make sure session is not hijacked.
304+
$_SESSION['username']=$GLOBALS['login'];
305+
$_SESSION['expires_on']=time()+INACTIVITY_TIMEOUT; // Set session expiration.
306+
}
307+
297308
// Check that user/password is correct.
298309
function check_auth($login,$password)
299310
{
300311
$hash = sha1($password.$login.$GLOBALS['salt']);
301312
if ($login==$GLOBALS['login'] && $hash==$GLOBALS['hash'])
302313
{ // Login/password is correct.
303-
$_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); // generate unique random number (different than phpsessionid)
304-
$_SESSION['ip']=allIPs(); // We store IP address(es) of the client to make sure session is not hijacked.
305-
$_SESSION['username']=$login;
306-
$_SESSION['expires_on']=time()+INACTIVITY_TIMEOUT; // Set session expiration.
314+
fillSessionInfo();
307315
logm('Login successful');
308316
return True;
309317
}
@@ -318,6 +326,11 @@ function isLoggedIn()
318326

319327
if (!isset($GLOBALS['login'])) return false; // Shaarli is not configured yet.
320328

329+
if (@$_COOKIE['shaarli_staySignedIn']===STAY_SIGNED_IN_TOKEN)
330+
{
331+
fillSessionInfo();
332+
return true;
333+
}
321334
// If session does not exist on server side, or IP address has changed, or session has expired, logout.
322335
if (empty($_SESSION['uid']) || ($GLOBALS['disablesessionprotection']==false && $_SESSION['ip']!=allIPs()) || time()>=$_SESSION['expires_on'])
323336
{
@@ -331,7 +344,9 @@ function isLoggedIn()
331344
}
332345

333346
// Force logout.
334-
function logout() { if (isset($_SESSION)) { unset($_SESSION['uid']); unset($_SESSION['ip']); unset($_SESSION['username']); unset($_SESSION['privateonly']); } }
347+
function logout() { if (isset($_SESSION)) { unset($_SESSION['uid']); unset($_SESSION['ip']); unset($_SESSION['username']); unset($_SESSION['privateonly']); }
348+
setcookie('shaarli_staySignedIn', FALSE, 0, WEB_PATH);
349+
}
335350

336351

337352
// ------------------------------------------------------------------------------------------
@@ -393,6 +408,7 @@ function ban_canLogin()
393408
// If user wants to keep the session cookie even after the browser closes:
394409
if (!empty($_POST['longlastingsession']))
395410
{
411+
setcookie('shaarli_staySignedIn', STAY_SIGNED_IN_TOKEN, time()+31536000, WEB_PATH);
396412
$_SESSION['longlastingsession']=31536000; // (31536000 seconds = 1 year)
397413
$_SESSION['expires_on']=time()+$_SESSION['longlastingsession']; // Set session expiration on server-side.
398414

0 commit comments

Comments
 (0)