Session management

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1173 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2009-10-06 15:33:48 +00:00
parent 12d2c6db84
commit 5f83619201
3 changed files with 11 additions and 5 deletions

View file

@ -1,14 +1,13 @@
<?php
$ignore = array("server", "username", "password");
$session_name = session_name();
if (isset($_POST["server"])) {
session_regenerate_id(); // defense against session fixation
$_SESSION["usernames"][$_POST["server"]] = $_POST["username"];
$_SESSION["passwords"][$_POST["server"]] = $_POST["password"];
$_SESSION["tokens"][$_POST["server"]] = rand(1, 1e6); // defense against cross-site request forgery
if (count($_POST) == count($ignore)) {
$location = ((string) $_GET["server"] === $_POST["server"] ? remove_from_uri() : preg_replace('~^[^?]*/([^?]*).*~', '\\1', $_SERVER["REQUEST_URI"]) . (strlen($_POST["server"]) ? '?server=' . urlencode($_POST["server"]) : ''));
if (!isset($_COOKIE[$session_name])) {
$location = ((string) $_GET["server"] === $_POST["server"] ? remove_from_uri() : preg_replace('~^([^?]*).*~', '\\1', $_SERVER["REQUEST_URI"]) . (strlen($_POST["server"]) ? '?server=' . urlencode($_POST["server"]) : ''));
if (!isset($_COOKIE[session_name()])) {
$location .= (strpos($location, "?") === false ? "?" : "&") . SID;
}
redirect($location);
@ -32,9 +31,13 @@ if (isset($_POST["server"])) {
function auth_error($exception = null) {
global $ignore, $connection, $adminer;
$session_name = session_name();
$username = $_SESSION["usernames"][$_GET["server"]];
unset($_SESSION["usernames"][$_GET["server"]]);
page_header(lang('Login'), (isset($username) ? h($exception ? $exception->getMessage() : (is_string($connection) ? $connection : lang('Invalid credentials.'))) : (isset($_POST["server"]) ? lang('Session support must be enabled.') : ($_POST ? lang('Session expired, please login again.') : ""))), null);
page_header(lang('Login'), (isset($username) ? h($exception ? $exception->getMessage() : (is_string($connection) ? $connection : lang('Invalid credentials.')))
: (!$_COOKIE[$session_name] && $_GET[$session_name] && ini_get("session.use_only_cookies") ? lang('Session support must be enabled.')
: (($_COOKIE[$session_name] || $_GET[$session_name]) && !isset($_SESSION["passwords"]) ? lang('Session expired, please login again.')
: ""))), null);
echo "<form action='' method='post'>\n";
$adminer->loginForm($username);
echo "<p>\n";

View file

@ -44,6 +44,9 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") {
echo "<div class='message'>" . implode("</div>\n<div class='message'>", $_SESSION["messages"]) . "</div>\n";
$_SESSION["messages"] = array();
}
if (!$_POST && !isset($_SESSION["passwords"])) { // used in auth
$_SESSION["passwords"] = array();
}
$databases = &$_SESSION["databases"][$_GET["server"]];
if (strlen(DB) && $databases && !in_array(DB, $databases, true)) {
$databases = null;

View file

@ -266,7 +266,7 @@ function queries($query = null) {
*/
function remove_from_uri($param = "") {
$param = "($param|" . session_name() . ")";
return preg_replace("~\\?$param=[^&]*&~", '?', preg_replace("~\\?$param=[^&]*\$|&$param=[^&]*~", '', $_SERVER["REQUEST_URI"]));
return substr(preg_replace("~([?&])$param=[^&]*&~", '\\1', "$_SERVER[REQUEST_URI]&"), 0, -1);
}
/** Generate page number for pagination