<?php
session_start();

if (@$_GET['action'] == "reset") {
	$_SESSION["page"] = 4;
	$_SESSION["reset"] = 1;
	$url = "../e-shop.php";
	echo "<script language='javascript'
	type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

if (!isset($_POST['submit'])) {
	exit('Fail!');
}

include '../../conf/conn.php';

@$username = mysqlSecurity($conn, $_POST['username']);
@$email = mysqlSecurity($conn, $_POST['email']);

if (@$_SESSION["reset"] == 1) {
	$password = base64_encode($_POST['Cpassword']);
	$id = $_SESSION['id'];
	$query = "UPDATE clients SET password = '$password' WHERE clients.id = '$id'";

	if ($result = mysqli_query($conn, $query)) {
		$_SESSION["reset"] = 0;
		$query = "select * from clients where clients.id = '$id'";
		$result = mysqli_query($conn, $query);
		$row = mysqli_fetch_assoc($result);
		$_SESSION['username'] = $row['username'];
		$_SESSION['id'] = $row['id'];
		$_SESSION['resetpassword'] = "0";
		$url = "../e-shop.php";
		echo "<script language='javascript'
			type='text/javascript'>";
		echo "window.location.href='$url'";
		echo "</script>";
	} else {
		exit('fail to update password. click <a href="javascript:history.go(-1);">here</a> back.');
	}
} else {
	$query = "select * from clients where email='$email' limit 1";
	$result = mysqli_query($conn, $query);

	if ($row = mysqli_fetch_assoc($result)) {
		require_once '../../phpmailer/class.phpmailer.php';
		$mail = new PHPMailer(); // create a new object
		// $mail->IsSMTP(); // enable SMTP
		// $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
		//$mail->SMTPAuth = true; // authentication enabled
		//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
		//$mail->Host = "localhost";
		//$mail->Host = "smtp.gmail.com";
		$mail->Host = "localhost";
		//$mail->Port = 465; // or 587

		$mail->AddBCC("daniel@at-creative.com");
		//$mail->AddBCC("edmondcheng@at-creative.com");
		$mail->AddBCC("info@sayvour.com.hk");
		$mail->AddBCC("catherine@sayvour.com.hk");
		$mail->AddBCC("sayvourhongkong@gmail.com");
		$mail->IsHTML(true);
		//$mail->Username = "kk@at-creative.com";
		//$mail->Password = "artech0088";
		$mail->SetFrom("info@sayvour.com.hk", "Sayvour");
		$mail->Subject = "Password notice from Sayvour";
		$mail->Body = "                                 <!-- top bar -->

                <div id=':lt' class='ii gt adP adO'>
                    <div id=':ls' class='a3s aXjCH m156312919a8bb8e9'><u></u>
                        <div style='margin:0;padding:0'>
													<div style='min-width:320px;background-color:#c2e3e3' lang='x-wrapper'>
														<div style='Margin:0 auto;max-width:560px;min-width:280px;'>
                                    <div style='border-collapse:collapse;display:table;width:100%'>
                                        <div style='Float:left;font-size:12px;line-height:19px;max-width:280px;min-width:140px;padding:10px 0 5px 0;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif'></div>
                                        <div style='Float:left;font-size:12px;line-height:19px;max-width:280px;min-width:139px;padding:10px 0 5px 0;text-align:right;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif'></div>
                                    </div>
                                </div>
                                <div style='Margin:0 auto;max-width:600px;min-width:320px;'>
                                    <div style='font-size:26px;line-height:32px;Margin-top:6px;Margin-bottom:20px;color:#ffffff;font-family:&quot;PT Serif&quot;,Georgia,serif;Margin-left:20px;Margin-right:20px' align='center'>
                                        <div style='font-size:0px!important;line-height:0!important' align='center'><img style='min-height:auto;width:100%;border:0;max-width:217px' src='https://ci4.googleusercontent.com/proxy/vUMxJkR_Kw9qDMrAUmEEtS4_hy8v4TztsG8zNDbLJxG0zizdPUmHdqEAaVAuRWqrT0fA9zde7TizwG9QGRXUNgPmnlhNJWJ_gzMCbqpsW3bSMaNbueUnoTh1=s0-d-e1-ft#http://i1.cmail2.com/ei/d/F9/F24/F62/203623/csfinal/sayvour-logo.png' alt='' width='217' class='CToWUd'></div>
                                    </div>
                                </div>

                <!-- end top bar -->

                <!-- Dear -->
										<div style='background-color:#FFFFFF'>
					<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
						<div style='border-collapse:collapse;display:table;width:100%'>
							<div style='max-width:600px;min-width:320px;text-align:left;FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
								<div style='Margin-left:20px;Margin-right:20px'>
									<div style='line-height:40px;font-size:1px'>&nbsp;</div>
								</div>
								<div style='Margin-left:20px;Margin-right:20px'>
									<h1 style='Margin-top:0;Margin-bottom:20px;font-style:normal;font-weight:normal;color:#499f9e;font-size:24px;line-height:32px;font-family:&quot;PT Serif&quot;,Georgia,serif;text-align:center' lang='x-size-28'>Reset yor sayvour password!</h1>
								</div>

                 <div style='Margin-left:20px;Margin-right:20px'>
                    <div style='line-height:20px;font-size:1px'>&nbsp;</div>
                </div>


								<div style='Margin-left:20px;Margin-right:20px'>
									<p style='Margin-top:0;Margin-bottom:0;text-transform: capitalize'>Dear&nbsp;" . $row['first name'] . ",</p><p style='Margin-top:20px;Margin-bottom:20px'>Your sayvour account password is :<p style='color:red'>'" . base64_decode($row["password"]) . "'</p><br><br>
									You can reset your password in your profile page.
</p>
								</div>
								<div style='Margin-left:20px;Margin-right:20px'>
									<div style='line-height:20px;font-size:1px'>&nbsp;</div>
				</div>

                <!-- End Dear -->


                <div style='Margin-left:20px;Margin-right:20px'>
                    <div style='line-height:5px;font-size:1px'>&nbsp;</div>
                </div>

                <!-- Best regards -->


				<div style='Margin-left:20px;Margin-right:25px'>
									<p style='Margin-top:0;Margin-bottom:20px;font-size:14px;line-height:21px;text-align:left' lang='x-size-14'>
									Best regards,</p>
								</div>
								<div style='font-size:12px;font-style:normal;font-weight:normal' align='left'>
									<a href='http://www.sayvour.com.hk'><img style='border:0;display:block;min-height:auto;width:100%;max-width:150px; padding-left:20px' alt='' width='249' src='https://ci4.googleusercontent.com/proxy/WhjL_vvSpLarh-zQTnIIub3yktIgmMYyzgWhl_XTAMbJQ9LIpv2A32hCNW_U3MQg3wHDHewv_AnV0ysjc7_dtTBbxdltT6OvngagFLBQ44sF8Vmw8QWrq9G8jgY=s0-d-e1-ft#http://i1.cmail2.com/ei/d/F9/F24/F62/203624/csfinal/Sayvour-logo-b.png' class='CToWUd'></a>
								</div>
								<div style='Margin-left:20px;Margin-right:20px;Margin-top:20px'>
									<p style='Margin-top:0;Margin-bottom:20px'>Sayvour | Say Your Flavour</p>
								</div>
								<div style='Margin-left:20px;Margin-right:20px'>
									<div style='line-height:5px;font-size:1px'>&nbsp;</div>
								</div>
								<div style='Margin-left:20px;Margin-right:20px'>
									<div style='line-height:5px;font-size:1px'>&nbsp;</div>
								</div>
								<div style='Margin-left:20px;Margin-right:20px'>
									<div style='line-height:5px;font-size:1px'>&nbsp;</div>
								</div>
							</div>
						</div>
					</div>
				</div>

               	<!-- End Best regards -->


                <!-- footer  -->

				<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
					<div style='border-collapse:collapse;display:table;width:100%'>
						<div style='text-align:left;font-size:12px;line-height:19px;color:#7A7A7A;font-family:Cabin,Avenir,sans-serif;Float:left;max-width:400px;min-width:320px;'>
							<div style='Margin-left:20px;Margin-right:20px;Margin-top:10px;Margin-bottom:10px'>
								<table style='border-collapse:collapse;table-layout:fixed'>
									<tbody>
										<tr>
											<td style='padding:0;width:26px'><a style='text-decoration:underline;color:#9dc4c4' href='http://kk.cmail2.com/t/d-i-ijlhttd-l-r/' target='_blank'><img style='border:0' src='https://ci4.googleusercontent.com/proxy/cdaOVwx01pydUCeWanZ5I0WsxDjvph7JzEiKY3zimkrOp-LjXcGnE1GadRLWYfIKzlmiYSOtzr-WjjHJdYebPiyRLHmWcDY2Zz_HAol546DebSYvB0BcePndD26qkP8w-AE=s0-d-e1-ft#http://i8.cmail2.com/static/eb/master/13-the-blueprint-3/images/facebook.png' width='26' height='26' class='CToWUd'></a></td>
											<td style='padding:0 0 0 3px;width:26px'><a style='text-decoration:underline;color:#9dc4c4' href='http://kk.cmail2.com/t/d-i-ijlhttd-l-y/' target='_blank'><img style='border:0' src='https://ci5.googleusercontent.com/proxy/H7Yt85uks7gPb4DmaC8HgjAqYMQhhSna9kYBtoUZr5D3lFKE-IJtZdN1MqyIopBh8YJYDX-igm7ScYklACfkd0Y1wvJ0BbBd3m1bcejcougYGa1dWg76ozQ-vG28HEqFDCbI=s0-d-e1-ft#http://i1.cmail2.com/static/eb/master/13-the-blueprint-3/images/instagram.png' width='26' height='26' class='CToWUd'></a></td>
										</tr>
									</tbody>
								</table>
								<div style='Margin-top:20px'>
									<div>Patisserie Sayvour Limited<br></div>

									<div>Sayvour is an artisan confectionery specialising in handmade chocolates and pastries by using high quality ingredients from around the world.</div>
								</div>
							</div>
						</div>
						<div style='text-align:left;font-size:12px;line-height:19px;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif;Float:left;max-width:320px;min-width:200px;'>
							<div style='Margin-left:20px;Margin-right:20px;Margin-top:10px;Margin-bottom:10px'></div>
						</div>
					</div>
				</div>
				<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
					<div style='border-collapse:collapse;display:table;width:100%'>
						<div style='text-align:left;font-size:12px;line-height:19px;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif;max-width:600px;min-width:320px;'>
						</div>
					</div>
				</div>
				<div style='line-height:40px;font-size:40px'>&nbsp;</div>
			</div>
			<img style='overflow:hidden;display:block!important;min-height:1px!important;width:1px!important;border:0!important;margin:0!important;padding:0!important' src='https://ci4.googleusercontent.com/proxy/-HQ08vlGM2ltqJyXUtWXZlorkMV10CQ0pH8IJ2TQo0JNoLOLepN2GqRqjNvhCsxelytv0dohcw2kweEKYfPeyqw=s0-d-e1-ft#https://kk.cmail2.com/t/d-o-ijlhttd-l/o.gif' width='1' height='1' border='0' alt='' class='CToWUd'>
			<div class='yj6qo'></div>
			<div class='adL'></div>
		</div>
		<div class='adL'></div>
	</div>
</div>

                <!--End  footer  -->




		";

		$mail->AddAddress($email);

		$url = "../e-shop.php";
		echo "<script language='javascript'
		type='text/javascript'>";
		echo "window.location.href='$url';";

		if (!$mail->Send()) {
			echo 'alert("Error");</script>';
		} else {
			echo 'alert("Please check your email address and retrieve the new password.");</script>';
		}
	} else {
		$url = "../e-shop.php";
		echo "<script language='javascript'
		type='text/javascript'>";
		echo "window.location.href='$url';";
		echo 'alert("Invalid email address.");</script>';
	}
}
?>