<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
$vars = json_decode(file_get_contents("php://input"));
if (!isset($vars->email)) {
  die(json_encode(array("status" => false, "message" => "Email Not Found")));
}
if (!filter_var($vars->email, FILTER_VALIDATE_EMAIL)) {
  die(json_encode(array("status" => false, "message" => "Email is invalid")));
}
include "konnection.php";
include "func.php";
$email = filter_var($vars->email, FILTER_VALIDATE_EMAIL);
$email = mysqli_real_escape_string($conn, $email);
$check = mysqli_query($conn, "SELECT * FROM users WHERE email = '$email'");
$site = getSite();
if (mysqli_num_rows($check) == 1) {
  $t = substr(str_shuffle("00987654321ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
  $token = md5($t);
  $uid = md5($email);
  $expDate = date("Y-m-d H:i:s");
  $message = "<!DOCTYPE html>
        <html lang='en' xmlns='http://www.w3.org/1999/xhtml' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #f1f1f1; margin: 0 auto; padding: 0; height: 100%; width: 100%;'>
        <head>
            <meta charset='utf-8'> <!-- utf-8 works for most cases -->
            <meta name='viewport' content='width=device-width'> <!-- Forcing initial-scale shouldn't be necessary -->
            <meta http-equiv='X-UA-Compatible' content='IE=edge'> <!-- Use the latest (edge) version of IE rendering engine -->
            <meta name='x-apple-disable-message-reformatting'>  <!-- Disable auto-scale in iOS 10 Mail entirely -->
            <title></title> <!-- The title tag shows in email notifications, like Android 4.4. -->
        
            <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet'>
        
            <!-- CSS Reset : BEGIN -->
            <style>
        @media only screen and (min-device-width: 320px) and (max-device-width: 374px) {
          u ~ div .email-container {
            min-width: 320px !important;
          }
        }
        @media only screen and (min-device-width: 375px) and (max-device-width: 413px) {
          u ~ div .email-container {
            min-width: 375px !important;
          }
        }
        @media only screen and (min-device-width: 414px) {
          u ~ div .email-container {
            min-width: 414px !important;
          }
        }
        </style>
        
            <!-- CSS Reset : END -->
        
            <!-- Progressive Enhancements : BEGIN -->
            <style>
        @media screen and (max-width: 500px) {}
        </style>
        
        
        </head>
        
        <body width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #f1f1f1; font-family: railway, sans-serif; font-weight: 400; font-size: 15px; line-height: 1.8; color: rgba(0,0,0,.4); mso-line-height-rule: exactly; background-color: #f1f1f1; margin: 0 auto; height: 100%; width: 100%; padding: 0;'>
        	<center style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; width: 100%; background-color: #f1f1f1;'>
            <div style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; display: none; font-size: 1px; max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden; mso-hide: all; font-family: sans-serif;'>
              &zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;
            </div>
            <div style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; max-width: 600px; margin: 0 auto;' class='email-container'>
            	<!-- BEGIN BODY -->
              <table align='center' role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
              	<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                  <td valign='top' class='bg_white' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #ffffff; padding: 1em 2.5em 0 2.5em; mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                  	<table role='presentation' border='0' cellpadding='0' cellspacing='0' width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
                  		<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                  			<td class='logo' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; text-align: center; mso-table-lspace: 0pt; mso-table-rspace: 0pt;' align='center'>
        			            <h1 style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: Lato, sans-serif; color: #000000; margin-top: 0; font-weight: 400; margin: 0;'><a href='#' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; text-decoration: none; color: #01858A; font-size: 24px; font-weight: 700; font-family: Lato, sans-serif;'>" . strtoupper($site["site_name"]) . "</a></h1>
        			          </td>
                  		</tr>
                  	</table>
                  </td>
        	      </tr><!-- end tr -->
        	      
        				<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                  <td valign='middle' class='hero bg_white' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #ffffff; position: relative; z-index: 0; padding: 2em 0 4em 0; mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                    <table style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
                    	<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                    		<td style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                    			<div class='text' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: black; padding: 0 2.5em; text-align: center;'>
                    				<h3 style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: Lato, sans-serif; margin-top: 0; color: black; font-size: 20px; margin-bottom: 0; font-weight: 400; line-height: 1.4;'>Password Reset Request</h3>
                    				<h4 style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: Lato, sans-serif; color: black; margin-top: 0; font-size: 22px; font-weight: 300;'>Please neglect this message if you didnt make any request. Your Reset Pin Is </h4>
                    				<a href='" . BASE . '/reset/?token=' . $token . "' class='btn btn-primary' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; padding: 10px 15px; display: inline-block; border-radius: 5px; background: #01858A; color: #ffffff;'>Reset Link</p>
                    			</div>
                    		</td>
                    	</tr>
                    </table>
                  </td>
        	      </tr><!-- end tr -->
              <!-- 1 Column Text + Button : END -->
              </table>
              <table align='center' role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
              	<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                  <td valign='middle' class='bg_light footer email-section' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #fafafa; padding: 2.5em; border-top: 1px solid rgba(0,0,0,.05); color: rgba(0,0,0,.5); mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                    <table style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
                    	<tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                        <td valign='top' width='50%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; padding-top: 20px; mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                          <table role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
                            <tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                              <td style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; text-align: left; padding-right: 10px; mso-table-lspace: 0pt; mso-table-rspace: 0pt;' align='left'>
                              	<h3 class='heading' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: Lato, sans-serif; margin-top: 0; font-weight: 400; color: #000; font-size: 20px;'>About</h3>
                              	<p style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>" . strtoupper($site["site_name"]) . " is a platform to pay bills and make subscription.</p>
                              </td>
                            </tr>
                          </table>
                        </td>
                        <td valign='top' width='50%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; padding-top: 20px; mso-table-lspace: 0pt; mso-table-rspace: 0pt;'>
                          <table role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin: 0 auto;'>
                            <tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                              <td style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; text-align: left; padding-left: 5px; padding-right: 5px; mso-table-lspace: 0pt; mso-table-rspace: 0pt;' align='left'>
                              	<h3 class='heading' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: Lato, sans-serif; margin-top: 0; font-weight: 400; color: #000; font-size: 20px;'>Contact Info</h3>
                              	<ul style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; margin: 0; padding: 0;'>
        					                <li style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; list-style: none; margin-bottom: 10px;'><span class='text' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>" . $site["site_address"] . "</span></li>
        					                <li style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; list-style: none; margin-bottom: 10px;'><span class='text' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>" . $site["site_phone"] . "</span></li>
        					                <li style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; list-style: none; margin-bottom: 10px;'><span class='text' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>" . $site["site_email"] . "</span></li>
        					              </ul>
                              </td>
                            </tr>
                          </table>
                        </td>
                        
                      </tr>
                    </table>
                  </td>
                </tr><!-- end: tr -->
                <tr style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>
                  <td class='bg_light' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background: #fafafa; text-align: center; mso-table-lspace: 0pt; mso-table-rspace: 0pt;' align='center'>
                  	<p style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;'>Regards from CEO - <a href='#' style='-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; text-decoration: none; color: rgba(0,0,0,.8);'>" . strtoupper($site["site_name"]) . "</a> - " . strtoupper($site["site_name"]) . "&copy; " . date('Y') . "</p>
                  </td>
                </tr>
              </table>
        
            </div>
          </center>
        </body>
        </html>";
  mysqli_query($conn, "INSERT INTO reset_tokens (user, reset, expiry) VALUES ('$uid', '$token', '$expDate')");
  $r = mysqli_fetch_array($check);
  $sendMail = sendMail(MAILER_EMAIL, MAILER_SENDER, "PASSWORD RESET ON " . MAILER_SENDER, $message);
  $sendMail->addAddress($email);
  $sendMail->send();
  die(json_encode(["status" => true, "message" => "We sent a reset password email"]));
} else {
  die(json_encode(["status" => true, "message" => "We sent a reset password email"]));
}
