X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=inc%2F3rdparty%2FSession.class.php;h=b56e4c545b23fa815d53c63b7c8e2a66e33cd0b7;hb=90a1a78b1e2f4d40e1d9b8e6f46aca129a9d7bcf;hp=eff924ccdb06dddf8c68bcfab1a15fc967bfc000;hpb=a4565e88edbc8e3bd092a475469769c86a4c350c;p=github%2Fwallabag%2Fwallabag.git diff --git a/inc/3rdparty/Session.class.php b/inc/3rdparty/Session.class.php index eff924cc..b56e4c54 100644 --- a/inc/3rdparty/Session.class.php +++ b/inc/3rdparty/Session.class.php @@ -1,136 +1,346 @@ $value) { - $_SESSION[$key] = $value; - } - if ($login==$login_test && $password==$password_test){ - // generate unique random number to sign forms (HMAC) - $_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); - $_SESSION['info']=Session::_allInfos(); - $_SESSION['username']=$login; - // Set session expiration. - $_SESSION['expires_on']=time()+Session::$inactivity_timeout; - return true; + self::banInit(); + if (self::banCanLogin()) { + if ($login === $loginTest && $password === $passwordTest) { + self::banLoginOk(); + + self::init($longlastingsession); + + // Generate unique random number to sign forms (HMAC) + $_SESSION['uid'] = sha1(uniqid('', true).'_'.mt_rand()); + $_SESSION['ip'] = self::_allIPs(); + $_SESSION['username'] = $login; + // Set session expiration. + $_SESSION['expires_on'] = time() + self::$inactivityTimeout; + if ($longlastingsession) { + $_SESSION['longlastingsession'] = self::$longSessionTimeout; + $_SESSION['expires_on'] += $_SESSION['longlastingsession']; + } + + foreach ($pValues as $key => $value) { + $_SESSION[$key] = $value; + } + + return true; + } + self::banLoginFailed(); } + + self::init(); return false; } - // Force logout + /** + * Unset SESSION variable to force logout + */ public static function logout() { - unset($_SESSION['uid'],$_SESSION['info'],$_SESSION['expires_on'],$_SESSION['tokens'], $_SESSION['login'], $_SESSION['pass']); + // unset($_SESSION['uid'],$_SESSION['ip'],$_SESSION['expires_on'],$_SESSION['tokens'], $_SESSION['login'], $_SESSION['pass'], $_SESSION['longlastingsession'], $_SESSION['poche_user']); + + // Destruction du cookie (le code peut paraître complexe mais c'est pour être certain de reprendre les mêmes paramètres) + $args = array_merge(array(session_name(), ''), array_values(session_get_cookie_params())); + $args[2] = time() - 3600; + call_user_func_array('setcookie', $args); + // Suppression physique de la session + session_destroy(); } - // Make sure user is logged in. + /** + * Make sure user is logged in. + * + * @return true|false True if user is logged in, false otherwise + */ public static function isLogged() { if (!isset ($_SESSION['uid']) - || $_SESSION['info']!=Session::_allInfos() - || time()>=$_SESSION['expires_on']){ - Session::logout(); + || (self::$disableSessionProtection === false + && $_SESSION['ip'] !== self::_allIPs()) + || time() >= $_SESSION['expires_on']) { + //self::logout(); + return false; } // User accessed a page : Update his/her session expiration date. - $_SESSION['expires_on']=time()+Session::$inactivity_timeout; + $_SESSION['expires_on'] = time() + self::$inactivityTimeout; + if (!empty($_SESSION['longlastingsession'])) { + $_SESSION['expires_on'] += $_SESSION['longlastingsession']; + } + return true; } - // Returns a token. - public static function getToken() + /** + * Create a token, store it in SESSION and return it + * + * @param string $salt to prevent birthday attack + * + * @return string Token created + */ + public static function getToken($salt = '') { - if (!isset($_SESSION['tokens'])){ + if (!isset($_SESSION['tokens'])) { $_SESSION['tokens']=array(); } // We generate a random string and store it on the server side. - $rnd = sha1(uniqid('',true).'_'.mt_rand()); + $rnd = sha1(uniqid('', true).'_'.mt_rand().$salt); $_SESSION['tokens'][$rnd]=1; + return $rnd; } - // Tells if a token is ok. Using this function will destroy the token. - // return true if token is ok. + /** + * Tells if a token is ok. Using this function will destroy the token. + * + * @param string $token Token to test + * + * @return true|false True if token is correct, false otherwise + */ public static function isToken($token) { - if (isset($_SESSION['tokens'][$token])) - { + if (isset($_SESSION['tokens'][$token])) { unset($_SESSION['tokens'][$token]); // Token is used: destroy it. + return true; // Token is ok. } + return false; // Wrong token, or already used. } -} \ No newline at end of file + + /** + * Signal a failed login. Will ban the IP if too many failures: + */ + public static function banLoginFailed() + { + if (self::$banFile !== '') { + $ip = $_SERVER["REMOTE_ADDR"]; + $gb = $GLOBALS['IPBANS']; + + if (!isset($gb['FAILURES'][$ip])) { + $gb['FAILURES'][$ip] = 0; + } + $gb['FAILURES'][$ip]++; + if ($gb['FAILURES'][$ip] > (self::$banAfter - 1)) { + $gb['BANS'][$ip]= time() + self::$banDuration; + } + + $GLOBALS['IPBANS'] = $gb; + file_put_contents(self::$banFile, ""); + } + } + + /** + * Signals a successful login. Resets failed login counter. + */ + public static function banLoginOk() + { + if (self::$banFile !== '') { + $ip = $_SERVER["REMOTE_ADDR"]; + $gb = $GLOBALS['IPBANS']; + unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); + $GLOBALS['IPBANS'] = $gb; + file_put_contents(self::$banFile, ""); + } + } + + /** + * Ban init + */ + public static function banInit() + { + if (self::$banFile !== '') { + if (!is_file(self::$banFile)) { + file_put_contents(self::$banFile, "array(), 'BANS'=>array()), true).";\n?>"); + } + include self::$banFile; + } + } + + /** + * Checks if the user CAN login. If 'true', the user can try to login. + * + * @return boolean true if user is banned, false otherwise + */ + public static function banCanLogin() + { + if (self::$banFile !== '') { + $ip = $_SERVER["REMOTE_ADDR"]; + $gb = $GLOBALS['IPBANS']; + if (isset($gb['BANS'][$ip])) { + // User is banned. Check if the ban has expired: + if ($gb['BANS'][$ip] <= time()) { + // Ban expired, user can try to login again. + unset($gb['FAILURES'][$ip]); + unset($gb['BANS'][$ip]); + file_put_contents(self::$banFile, ""); + + return true; // Ban has expired, user can login. + } + + return false; // User is banned. + } + } + + return true; // User is not banned. + } + + + /** + * Tells if a param exists in session + * + * @param $name name of the param to test + * @return bool + */ + public static function isInSession($name) + { + return (isset($_SESSION[$name]) ? : FALSE); + } + + /** + * Returns param in session + * + * @param $name name of the param to return + * @return mixed param or null + */ + public static function getParam($name) + { + return (self::isInSession($name) ? $_SESSION[$name] : NULL); + } + + /** + * Store value in session + * + * @param $name name of the variable to store + * @param $value value to store + */ + public static function setParam($name, $value) + { + $_SESSION[$name] = $value; + } +}