<?php
    // adduser.php
    $user = "";
    $password = "";
    $vpassword = "";
    $realname = "";
    $birthdate = "";
    $location = "";
    $email = "";
    $website = "";
    $IM = "";
    $profile = "";
    $errormsg = "";
     
     
    if (array_key_exists("user" , $_REQUEST ) == TRUE )
        $user = $_REQUEST["user"];
    if (array_key_exists("password" , $_REQUEST ) == TRUE )
        $password = $_REQUEST["password"];
    if (array_key_exists("vpassword" , $_REQUEST ) == TRUE )
        $vpassword = $_REQUEST["vpassword"];
    if (array_key_exists("realname" , $_REQUEST ) == TRUE )
        $realname = $_REQUEST["realname"];
    if (array_key_exists("birthdate" , $_REQUEST ) == TRUE )
        $birthdate = $_REQUEST["birthdate"];
    if (array_key_exists("location" , $_REQUEST ) == TRUE )
        $location = $_REQUEST["location"];
    if (array_key_exists("email" , $_REQUEST ) == TRUE )
        $email = $_REQUEST["email"];
    if (array_key_exists("website" , $_REQUEST ) == TRUE )
        $website = $_REQUEST["website"];
    if (array_key_exists("IM" , $_REQUEST ) == TRUE )
        $IM = $_REQUEST["IM"];
    if (array_key_exists("profile" , $_REQUEST ) == TRUE )
        $profile = $_REQUEST["profile"];
    if (array_key_exists("errormsg" , $_REQUEST ) == TRUE )
        $errormsg = $_REQUEST["errormsg"];
     
     
     
    switch ($action):
    case "g_error":
		drawMainTop();
    print "Error: " . $errormsg;
		drawMainBottom();
    case "g_default":
		drawMainTop();
		print "Create New User<br><br>
         
        Please take a moment to register yourself a username. Items with an * are required; all other fields are optional and will be part of your user profile.<br><br>
         
        <form action='index.php?shard=adduser&amp;action=add_user_db' method=POST>
        <font class='fineC2'>* Desired username:<br>
        <input class='text' type=text name=user size=20 maxlength=20 value='$user'><br><br>
        * Password (Must be between 6 and 10 characters):<br>
        <input class='text' type=password name=password size=20 maxlength=10><br><br>
        * Verify Password:</font><font class='fineC2'><br>
        <input class='text' type=password name=vpassword size=20 maxlength=10><br><br>
        'Real' Name:<br>
        <input class='text' type=text name=realname size=20 maxlength=20 value='$realname'><br><br>
        Birth date (mm/dd/yyyy format):<br>
        <input class='text' type=text name=birthdate size=10 maxlength=10 value='$birthdate'><br><br>
        Location:<br>
        <input class='text' type=text name=location size=20 maxlength=20 value='$location'><br><br>
        Email (dont worry, no spam from me!):<br>
        <input class='text' type=text name=email size=35 value='$email'><br><br>
        Website:<br>
        <input class='text' type=text name=website size=35 value='$website'><br><br>
        AIM / ICQ / IM name:<br>
        <input class='text' type=text name=IM size=20 maxlength=20 value='$IM'><br><br>
        Profile:<br>
        <textarea class='text' name=profile cols=45 rows=10>$profile</textarea><br><br>
        <input class='button' type=submit value='Submit'>&nbsp;&nbsp;<input class='button' type=reset value=Cancel>
        ";
		drawMainBottom();
    break;
    case "add_user_db":
     
    // Checking if all required fields were entered
   
    if (($user == "") || ($password == "") || ($vpassword == ""))
    {
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=A required field is missing&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        else
            header("Location: index.php?shard=adduser&action=g_error&errormsg=A required field is missing&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
         
         
         
        exit();
    }
     
    // Checking for password verification
    if ($password != $vpassword)
    {
                 
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=Passwords do not match&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        else
            header("Location: index.php?shard=adduser&action=g_error&errormsg=Passwords do not match&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        exit();
    }
     
    // Checking for password length
    if (strlen($password) < 6)
    {
       
         
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=Passwords do not match&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        else
            header("Location: index.php?shard=adduser&action=g_error&errormsg=Password is too short&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        exit();
    }
     
    // Check for existing username
    $result = mysql_query("select count(*) from users where username='$user'");
    $result = mysql_fetch_row($result);
    if ($result[0] > 0)
    {
		if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=Username aleady exists, please choose a different one.&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        else
            header("Location: index.php?shard=adduser&action=g_error&errormsg=Username aleady exists, please choose a different one.&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        exit();
    }
   
     
    //check for date format correctness
    if ($birthdate != "")
    {
        if (ereg("([0-9]{2})/([0-9]{2})/([0-9]{4})", $birthdate) == false)
        {
            if ($siteSettings['slickurls'] == TRUE )
                header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=Birthdate is not in MM/DD/YYYY format&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
            else
                header("Location: index.php?shard=adduser&action=g_error&errormsg=Birthdate is not in MM/DD/YYYY format&user=$user&password=$password&vpassword=$vpassword&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
            exit();
        }
    }
     
    // check for username validity
    if (stristr($user, "'"))
    {
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_error&errormsg=Usernames may only contain alphanumeric characters.&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        else
            header("Location: index.php?shard=adduser&action=g_error&errormsg=Usernames may only contain alphanumeric characters.&realname=$realname&birthdate=$birthdate&location=$location&email=$email&website=$website&IM=$IM&profile=$profile");
        exit();
    }
     
    // add entry into database
   
     
    $datejoined = time();
    $password = sha1($password);
    //
    // make a directory in /users for this user.  Also copy the default.jpg from the anonymous user to the user's folder
    //
    // -- for now leave this out as it assumes certain permissions and is not even really used--it was for the forums
    //mkdir( "users/" . $user );
    //copy( "users/anonymous/default.jpg" , "users/" . $user . "/default.jpg");
    // set the path to the user's default avatar
    $avatar_path = "users/" . $user . "/default.jpg";
     
    $addStr = "insert into users (username, password, realname, birthdate, location, email, website, IM, profile, rating, datejoined, userstatus, avatar) values ('$user', '$password', '$realname', '$birthdate', '$location', '$email', '$website', '$IM', '$profile', 0, $datejoined, NULL, '$avatar_path' )";
    $setPermission = "insert into permissionGroups (username, pGroup) values ('$user', 'everyone')";
   
     
    $result = mysql_query($addStr) or DIE(mysql_error());
    $result = mysql_query($setPermission) or DIE(mysql_error());
     
    if ($siteSettings['slickurls'] == TRUE )
        header("Location: " . $siteSettings['siteurl'] . "/index.php?shard=adduser&action=g_success&user=$user");
    else
        header("Location: index.php?shard=adduser&action=g_success&user=$user");
    exit();
     
    break;
     
    case "g_success":
    drawMainTop();
    print "Successfully created user: " . $user . "!";
    drawMainBottom();
    break;
     
    endswitch;
   
?>
