<?php
	session_start();
	$err = "";
	if (isset($_POST["reg"])) {
		$username = htmlentities($_POST['user']);
		$name = htmlentities($_POST['name']);
		$password = htmlentities($_POST['password']);
		$success = FALSE;
		if ($password!=htmlentities($_POST["repassword"])||(strlen($password) < 5)) {
			$err = "Mật khẩu quá ngắn hoặc không trùng nhau.";
		}	
		else if (!preg_match("/^[a-zA-Z0-9]+$/", $username) || !preg_match("/^[a-zA-Z0-9]+$/", $password) || !preg_match("/^[a-zA-Z0-9\s\pL]+$/u", $name)) {
			if(!preg_match("/^[a-zA-Z0-9\s\pL]+$/u", $name)) echo $name;
			$err = "Thông tin chứa kí tự đặc biệt.";
		}
		else $success = TRUE;
	if ($success) {
		if (!is_file('data/tmp.xml')) {
			$fi = fopen('data/account.xml','r');
			$fo = fopen('data/tmp.xml','w');
			$id = 0;
			$setup = 0;
			while (!feof($fi)) {
				$str = fgets($fi);
				if(strpos($str, "/Row")) $id++;
				if(strpos($str, "s64") && strpos($str, ">".$username."<"))
				{
					$err = "Tài khoản đã tồn tại.";
					break;
				}
				if(strpos($str, "/Table") && $setup == 0)
				{
					$str = '   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s63"><Data ss:Type="Number">'.$id.'</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">'.$username.'</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">'.$password.'</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">'.$name.'</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="Number">0</Data></Cell>
   </Row>
  </Table>
  ';
					$setup = 1;
				}
				fwrite($fo,$str);
			}
			fclose($fi);
			fclose($fo);
			if(!$err) {
				copy('data/tmp.xml','data/account.xml');
				$_SESSION['tid'] = $id;
				$_SESSION['tuser'] = $username;
				$_SESSION['tpass'] = $password; 
				$_SESSION['tname'] = $name;
				$ok = 1;
			}
			unlink('data/tmp.xml');
		}
		else $err = "Đăng kí không thành công<br/>Vui lòng thử lại sau.";
	}
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Đăng kí &middot; Chấm bài trực tuyến</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    
    <!-- loading -->

    <script src="js/pace.min.js"></script>
    <link rel="stylesheet" href="/css/pace-theme-minimal.css">
    <!--end load-->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
      body {
        padding-top: 40px;
        padding-bottom: 40px;
        background-color: #f5f5f5;
      }

      .form-repass {
        max-width: 300px;
        padding: 19px 29px 29px;
        margin: 0 auto 20px;
        background-color: #fff;
        border: 1px solid #e5e5e5;
        -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
                border-radius: 5px;
        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
           -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                box-shadow: 0 1px 2px rgba(0,0,0,.05);
      }
      .form-repass .form-repass-heading,
      .form-repass .checkbox {
        margin-bottom: 10px;
      }
      .form-repass input[type="text"],
      .form-repass input[type="password"] {
        font-size: 16px;
        height: auto;
        margin-bottom: 15px;
        padding: 7px 9px;
      }
	  .form-repass .warning {
	    font-size: 14px;
		color: red; 
		margin-top: 8px;
		text-align: center;
	  }

    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <style> .modal-content {
    -webkit-border-radius: 0;
    -webkit-background-clip: padding-box;
    -moz-border-radius: 0;
    -moz-background-clip: padding;
    border-radius: 6px;
    background-clip: padding-box;
    -webkit-box-shadow: 0 0 40px rgba(0,0,0,.5);
    -moz-box-shadow: 0 0 40px rgba(0,0,0,.5);
    box-shadow: 0 0 40px rgba(0,0,0,.5);
    color: #000;
    background-color: #fff;
    border: rgba(0,0,0,0);
}
.modal-message .modal-dialog {
    width: 300px;
}
.modal-message .modal-body, .modal-message .modal-footer, .modal-message .modal-header, .modal-message .modal-title {
    background: 0 0;
    border: none;
    margin: 0;
    padding: 0 20px;
    text-align: center!important;
}

.modal-message .modal-title {
    font-size: 17px;
    color: #737373;
    margin-bottom: 3px;
}

.modal-message .modal-body {
    color: #737373;
}

.modal-message .modal-header {
    color: #fff;
    margin-bottom: 10px;
    padding: 15px 0 8px;
}
.modal-message .modal-header .fa, 
.modal-message .modal-header 
.glyphicon, .modal-message 
.modal-header .typcn, .modal-message .modal-header .wi {
    font-size: 30px;
}

.modal-message .modal-footer {
    margin: 25px 0 20px;
    padding-bottom: 10px;
}

.modal-backdrop.in {
    zoom: 1;
    filter: alpha(opacity=75);
    -webkit-opacity: .75;
    -moz-opacity: .75;
    opacity: .75;
}
.modal-backdrop {
    background-color: #fff;
}
.modal-message.modal-success .modal-header {
    color: #53a93f;
    border-bottom: 3px solid #a0d468;
}

.modal-message.modal-info .modal-header {
    color: #57b5e3;
    border-bottom: 3px solid #57b5e3;
}

.modal-message.modal-danger .modal-header {
    color: #d73d32;
    border-bottom: 3px solid #e46f61;
}

.modal-message.modal-warning .modal-header {
    color: #f4b400;
    border-bottom: 3px solid #ffce55;
}
</style>
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
    <![endif]-->

    <!-- Fav and touch icons -->
    <link rel="shortcut icon" href="ico/favicon.png">
  </head>

  <body>

    <div class="container">

      <form class="form-repass" method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
        <h2 class="form-repass-heading">Đăng kí tài khoản</h2>
        <input type="text" name="user" class="input-block-level" placeholder="Tên tài khoản">
        <input type="text" name="name" class="input-block-level" placeholder="Tên đầy đủ">
        <input type="password" name="password" class="input-block-level" placeholder="Mật khẩu (ít nhất 5 ký tự)">
        <input type="password" name="repassword" class="input-block-level" placeholder="Nhập lại mật khẩu mới">
        <div align="center">
		<button class="btn btn-large btn-primary" type="submit" name = "reg">Đăng kí</button> 
		<a class="btn btn-large btn-primary" href="index.php">Về trang chủ</a>
		</div>
      </form>
    </div>

 <?php
 if($err) {
 	?>
    <div class="modal modal-message modal-danger fade" id="warningModal" tabindex="-1" role="dialog"  aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header"><i class="glyphicon glyphicon-warning-sign"></i></div>
      <div class="modal-body">
        <h4><?php echo $err; ?></h4>  
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>
<?php
	}
	if($ok)
	{
		?>
		<div class="modal modal-message modal-success fade" id="successModal" tabindex="-1" role="dialog"  aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header"><i class="glyphicon glyphicon-check"></i></div>
      <div class="modal-body">
        <h4>ID: <?php echo $_SESSION['tid']; ?> </h4>
        <h4>Tài khoản: <?php echo $_SESSION['tuser']; ?> </h4>
        <h4>Mật khẩu: <?php echo $_SESSION['tpass']; ?> </h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-success" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>
		<?php
	}
 ?> <!-- /container -->
<script src=js/jquery-latest.js></script>
    <script src="js/bootstrap.min.js"></script>
<script>
  $("#successModal").modal("show");$("#successModal").on("hidden.bs.modal",function(){window.location="/index.php"});$(document).keyup(function(a){if(a.keyCode){$("#successModal").modal("hide")}});
  $("#warningModal").modal("show");$(document).keyup(function(a){if(a.keyCode){$("#warningModal").modal("hide")}});
</script>

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

  </body>
</html>
