<title>Schimba-ti adresa de email - <?php echo Config::$_SITE_NAME; ?></title>
<?php
if(!defined('panel'))
	die('Nope.');
if(!isset($_SESSION['user'])) { echo '<meta http-equiv="refresh" content="0;URL=\''.Config::$_PAGE_URL.'\'/>'; } else {
?>

</div>
	<div class="main-content">
			<div class="breadcrumbs" id="breadcrumbs">
				<script type="text/javascript">
					try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
				</script>

				<ul class="breadcrumb">
					<li>
						<i class="ace-icon fa fa-home home-icon"></i>
						<a href="<?php echo Config::$_PAGE_URL; ?>">Home</a>
					</li>
					<li class="active">Schimba-ti adresa de email</li>
				</ul><!-- /.breadcrumb -->

				<div class="nav-search" id="nav-search">
					<form method="POST" action="<?php echo Config::$_PAGE_URL; ?>search">
						<span class="input-icon">
							<input type="text" placeholder="Search ..." name="sname" id="username" type="submit" class="nav-search-input" autocomplete="on"> 
							<i class="ace-icon fa fa-search nav-search-icon"></i>
						</span>
					</form>
				</div> <!-- /.nav-search -->
			</div>	
<div class="page-content">
<div class="row-fluid">
<div class="span12">
<?php
if(isset($_POST['submit'])) {
	if(!$_POST['email'] || !$_POST['password']) {
		echo '<div class="alert alert-block alert-danger">Invalid email / Email invalid.</div>';
	} else {
		$q = Config::$g_con->prepare('SELECT * FROM `users` WHERE `name` = ? AND `password` = ?');
		$q->execute(array(Config::getData('users','name',$_SESSION['user']),$_POST['password']));
		while($row = $q->fetch(PDO::FETCH_OBJ))
		if($q->rowCount()) {
			$d = Config::$g_con->prepare('SELECT * FROM `changemail` WHERE `name` = ?');
			$d->execute(array(Config::getData('users','name',$_SESSION['user'])));
			if($d->rowCount()) {
				echo '<div class="alert alert-block alert-danger">Ai mai f&#259;cut deja o cerere de schimbare a emailului.</div>';
			}
			else {
				if($row->Email == "email@yahoo.com") {					
					$email = htmlspecialchars($_POST['email']);
				} else {
					$email = $row->Email;
				}
				$user = $row->name;

				$first = md5(uniqid());
				$final_key = $first . md5($first);
				
				$s = Config::$g_con->prepare("INSERT INTO changemail (ChangeMailKey, name, Email) VALUES (?, ?, ?)");
				$s->execute(array($final_key, $user, $_POST['email']));

require 'inc/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'link mail firma de webhost';
$mail->SMTPAuth = false;
$mail->Username = 'user mail webhost;
$mail->Password = 'pass mail webhost';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->setFrom('user mail webhost', 'site link');
$mail->addAddress($email);
$mail->Subject = 'Nume site - confirmare email / validate your email';
$mail->Body    = "Salutare $user,
[RO] Ai primit acest email pentru ca ai solicitat confirmarea emailului pe serverul pe serverul nume server
Pentru a avea emailul ". $_POST['email'] ." asociat cu contul tau, da click pe link-ul de mai jos:
".Config::$_PAGE_URL."checkmail/".$final_key."


[EN] You have received this email because you have requested to confirm your email on the server nume server
To have the email ". $_POST['email'] ." associated with your account, please click the link above.


Cu stima,
Echipa nume server
Contact: mailu tau";
if(!$mail->send()) {
    echo 'Email could not be sent.';
} else {
	$err = "<br />Un email a fost trimis catre ". $email ." pentru confirmarea emailului.<br />
	In acel email vei gasi un link pe care va trebui sa dai click pentru a confirma schimbarea adresei de email asociata cu contul tau.<br /><br />
	Emailul va ajunge la tine in cateva minute in casuta ta de email.<br />
	Daca nu ai primit emailul, verifica si folderul de spam sau foloseste optiunea de search din yahoo/gmail pentru a cauta WSter RPG.";
}
			}
			
		}
		else echo '<div class="alert alert-block alert-danger"><button type="button" class="close" data-dismiss="alert">Invalid email / Email invalid.</div>';
	}
} ?>
 
<div class="page-header">
<h1>Schimba-ti adresa de email</h1>
</div>
<p>
<b>Atentie!</b><br>E interzis sa vinzi contul sau sa-l dai la schimb.<br>O data ce ai schimbat emailul, nu vei mai putea recupera contul daca nu ai acces la noul email.<br>Nu seta emailul altcuiva daca nu vrei sa ramai fara cont.
<hr>
</p>
<?php if(isset($err)): ?>
<?php echo $err ?>
<?php else: ?>
<form method="POST" action="" accept-charset="UTF-8">
New email:<br />
<input placeholder="Scrie noua adresa de email aici..." name="email" type="email"><br />
SA:MP Password:<br />
<input name="password" type="password"><br/><br />
<input class="btn btn-inverse" type="submit" name="submit" value="Modifica!">
</form>
 <?php endif;
} ?>
