<?php
/*
* SWG Resource Tracker
* Copyright (C) 2004 Enigma
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
function validateSession()
{
global $baseurl, $DB;
$login = "";
if (isset($_SESSION['rtusername']) && isset($_SESSION['rtpassword']))
{
$username = $_SESSION['rtusername'];
$password = $_SESSION['rtpassword'];
$query = "SELECT `id`, `enabled` FROM `users` WHERE `name`='{$username}' && `password`='{$password}'";
$DB->query($query);
if ($DB->rowCount() != 1)
{
$login = "Unable to validate session <a href=\"{$baseurl}?act=login\">(Login)</a>";
setcookie("rtusername", "", time() - 3600, "/");
setcookie("rtpassword", "", time() - 3600, "/");
}
else
{
$result = $DB->fetchAssoc();
if ($result['enabled'] == 1)
{
define("USERID", $result['id']);
$query = "UPDATE `users` SET `lastvisit`=NOW(), `register`=`register` WHERE `id`=" . USERID;
$DB->query($query);
}
else
{
$login = "Account has been disabled";
setcookie("rtusername", "", time() - 3600, "/");
setcookie("rtpassword", "", time() - 3600, "/");
}
}
}
else
{
$login = "Session has expired";
}
return $login;
}
function doLogin()
{
global $baseurl, $DB;
$login = "";
if (isset($_REQUEST['rtusername']) && isset($_REQUEST['rtpassword']))
{
$username = $_REQUEST['rtusername'];
$password = $_REQUEST['rtpassword'];
$username = ereg_replace("[^a-zA-Z0-9!@#\$%\^&*\(\)\-\_\+\=]", "", $username);
$password = ereg_replace("[^a-zA-Z0-9!@#\$%\^&*\(\)\-\_\+\=]", "", $password);
$query = "SELECT `id`, `enabled` FROM `users` WHERE `name`='{$username}' && `password`='{$password}'";
$DB->query($query);
if ($DB->rowCount() != 1)
{
$login = "Unable to validate account <a href=\"{$baseurl}?act=login\">(Login)</a>";
setcookie("rtusername", "", time() - 3600, "/");
setcookie("rtpassword", "", time() - 3600, "/");
}
else
{
$result = $DB->fetchAssoc();
if ($result['enabled'] == 1)
{
$_SESSION['rtusername'] = $username;
$_SESSION['rtpassword'] = $password;
define("USERID", $result['id']);
$query = "UPDATE `users` SET `lastvisit`=NOW(), `register`=`register` WHERE `id`=" . USERID;
$DB->query($query);
}
else
{
$login = "Account has been disabled";
setcookie("rtusername", "", time() - 3600, "/");
setcookie("rtpassword", "", time() - 3600, "/");
}
}
}
else
{
$login = "Not Logged In <a href=\"{$baseurl}?act=login\">(Login)</a>";
}
return $login;
}
function doLogout()
{
global $baseurl;
$_SESSION['rtusername'] = "";
$_SESSION['rtpassword'] = "";
unset($_SESSION['rtusername']);
unset($_SESSION['rtpassword']);
setcookie("rtusername", "", time() - 3600, "/");
setcookie("rtpassword", "", time() - 3600, "/");
header("Location: {$baseurl}");
die();
}
function sessionUsername()
{
return $_SESSION['rtusername'];
}
?>