<?php
/*================================================================+\
|| # PHPRetro - An extendable virtual hotel site and management
|+==================================================================
|| # Copyright (C) 2009 Yifan Lu. All rights reserved.
|| # http://www.yifanlu.com
|| # Parts Copyright (C) 2009 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|| # All images, scripts, and layouts
|| # Copyright (C) 2009 Sulake Ltd. All rights reserved.
|+==================================================================
|| # PHPRetro is provided "as is" and comes without
|| # warrenty of any kind. PHPRetro is free software!
|| # License: GNU Public License 3.0
|| # http://opensource.org/licenses/gpl-license.php
\+================================================================*/

$page['allow_guests'] = true;
require_once('./includes/core.php');
require_once('./includes/session.php');
$lang->addLocale("credits.credits");
?>

<style type="text/css">
body {
  background-color: #00152C;
  font-family: Verdana;
  font-size: 10px;
}
#box {
  width: 340px;
  margin: auto;
  background-color: #FFFFFF;
  padding: 10px;
}
#box_header {
  width: 320px;
  margin: auto;
  background-color: #000000;
  padding: 10px;
  color: #FFFFFF;
  font-weight: bold;
}
.alert_red_box {
  background-color: #FF0000;
  padding: 7px;
  margin-bottom: 10px;
  border-style: solid;
  border-color: #CC3333;
  color: #FFFFFF;
}
input[type="text"], input[type="password"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  width: 339px;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
input[type="submit"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
  width: 200px;
}
select {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
</style>

<?php
if($user->name == "Guest"){
  header("Location: ".PATH);
}

$checkIfFlaggedQuery = mysql_query("SELECT * FROM users WHERE id = '".$user->id."'");
$checkIfFlagged = mysql_fetch_array($checkIfFlaggedQuery);
if($checkIfFlagged['flagged'] == 0){
  header("Location: ".PATH);
}

if(isset($_POST['submit_name'])){
  if($_POST['new_name'] == NULL || $_POST['password'] == NULL){
    $error_message = '<div class="alert_red_box">You have left a field blank.</div>';
  }else{
    $getOldPassword = mysql_query("SELECT password FROM users WHERE id = '".$user->id."'");
    $oldPasswordDB = mysql_fetch_assoc($getOldPassword);
    $checkPassword = $input->HoloHash($_POST['password'], $user->name);
    if($checkPassword != $oldPasswordDB['password']){
      $error_message = '<div class="alert_red_box">Invalid password.</div>';
    }else{
      $newUsername = $input->FilterText($_POST['new_name']);
      $newPassword = $input->HoloHash($_POST['password'], $newUsername);
      $newUsernameFilter =  preg_replace("/([^a-z\d]+[-])/i", "", $_POST['new_name']);
      if($newUsername == $user->name){
        $error_message = '<div class="alert_red_box">Nice try, but you cannot use the same username.</div>';
      }else{
        if($newUsername != $newUsernameFilter){
          echo '<div class="alert_red_box">This username is invalid or contains invalid characters.</div>';
        }else{
          $checkUsername = mysql_query("SELECT * FROM users WHERE name = '".$newUsername."'");
          if(mysql_num_rows($checkUsername) > 0){
            $error_message = '<div class="alert_red_box">This username is already in use.</div>';
          }else{
            if(strlen($newUsername) > 30 || strlen($newUsername) < 5){
              $error_message = '<div class="alert_red_box">Your username must be between three to thirty characters long.</div>';
            }else{
              mysql_query("UPDATE users SET name = '".$newUsername."', password = '".$newPassword."', flagged = '0' WHERE id = '".$user->id."'");
              mysql_query("UPDATE cms_users SET name = '".$newUsername."' WHERE id = '".$user->id."'");
              mysql_query("UPDATE rooms SET owner = '".$newUsername."' WHERE owner = '".$user->name."'");
              header("Location: ".PATH."/logout");
            }
          }
        }
      }
    }
  }
}

?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

<div id="column1" class="column">
				<div class="habblet-container ">
						<div class="cbb clearfix white ">
<div class="box-content clearfix">

<div id="box">
<div id="box_header">Create A New Name</div><br />
<?php echo $error_message; ?>
<form action="" method="post">
New Name:<br />
<input type="text" name="new_name" /><br /><br />
Password Check:<br />
<input type="password" name="password" /><br /><br />
<center><input type="submit" name="submit_name" value="Submit" /></center>
</form>
* You were flagged because your name was not RP fashion.<br />
* All names <u>MUST</u> be in <strong>Firstname-Lastname</strong> format.
</div>