<?php
    // login.php
   
    switch ($action):
    case "g_default":
    drawMainTop();
    print "<p style='margin-left: 10'>
        <form method='post' action='index.php?shard=login&amp;action=proc_login'>
        <b>Login:</b><br />
        <font class='fine'>Username:<br />
        <input class='button' type='text' size='15' name='login_name'><br />
        Password:<br />
        <input class='button' type='password' size='15' name='login_pass'><br /><br />
        <input class='submit' type='submit' value='Login'></form><br /><br /></font>
        Don't have an account yet? <a href=\"index.php?shard=adduser\">Register</a> one!<br /><br />";
    drawMainBottom();
    break;
     
    case "g_error":
    $errormsg = $_REQUEST['errormsg'];
    ;
   
    drawMainTop();
    print "Error: " . $errormsg;
    drawMainBottom();
    break;
     
    case "proc_login":
     
    $login_name = $_REQUEST['login_name'];
    $login_pass = $_REQUEST['login_pass'];
     
    
    if (($login_name == "") || ($login_pass == ""))
    {
        
        header("Location: index.php?shard=login&action=g_error&errormsg=Missing username or password.");
        exit();
    }
     
    $result = mysql_query("select username, password from users where username='$login_name'") or DIE(mysql_error());
    $result = mysql_fetch_array($result);
    if (!$result)
    {
        
        header("Location: index.php?shard=login&action=g_error&errormsg=Bad username or password, please try again.");
        exit();
    }
    else
    {
        
        if ($result['password'] == sha1($login_pass))
        {
            
            setcookie("cookieusername", "$login_name", time()+864000000, "/");
            setcookie("cookiepassword", "$result[password]", time()+864000000, "/");
            header("Location: index.php?shard=login&action=g_login_success");
        }
        else
        {
            
            header("Location: index.php?shard=login&action=g_error&errormsg=Bad username or password, please try again.");
        }
    }
     
    break;
     
    case "proc_logout":
    setcookie("cookieusername", "", time()-3600, "/");
    header("Location: index.php?shard=login&action=g_logout_success");
    break;
     
    case "g_login_success":
    drawMainTop();
    print "Succesfully logged in as " . $_REQUEST['cookieusername'];
    drawMainBottom();
    break;
     
    case "g_logout_success":
    drawMainTop();
    print "Succesfully logged out.  Use the login screen to log back in.";
    drawMainBottom();
    break;
    endswitch;
     
    
     
?>
