<?php
session_start();
require("../../mainconfig.php");
$msg_type = "nothing";

function verif_email($kode, $email){
    $url = "https://api.smtp2go.com/v3/email/send";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
{
    "api_key": "api-4B7D84D4E9F34B75929089754FD5A97F",
    "to": ["$email"],
    "sender": "admin@borneo-media.com",
    "subject": "Borneo Media SMM Verification",
    "text_body": "Kode Verifikasi anda adalah: $kode"
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
$hasil1 = json_decode($resp, true)["data"]["succeeded"];
return $hasil1;
}

function dapetin($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        $data = curl_exec($ch);
        curl_close($ch);
                return json_decode($data, true);
}

if (isset($_SESSION['user'])) {
	header("Location: ".$cfg_baseurl);
} else {
	if (isset($_POST['reset'])) {
		$post_username = mysqli_real_escape_string($db, trim($_POST['username']));
		$post_email = mysqli_real_escape_string($db, trim($_POST['email']));		
		$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$post_username' AND email = '$post_email'");

		$secret_key = '6LftOK4nAAAAAEShHyri4uvG3mE1hh0IRvPu4vci'; //masukkan secret key-nya berdasarkan secret key masig-masing saat create api key nya
        $captcha=$_POST['g-recaptcha-response'];
        $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secret_key) . '&response=' . $captcha;
        $recaptcha = dapetin($url);
        
	    $data_user = mysqli_fetch_assoc($check_user);
		$check_email = mysqli_query($db, "SELECT * FROM users WHERE email = '$post_email' AND username = '$post_username'");
	    $data_email = mysqli_fetch_assoc($check_email); 
			
		if (empty($post_username) || empty($post_email)) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Mohon mengisi input.', 'error');</script><b>Gagal:</b> Mohon mengisi input.";
		} else if ($recaptcha['success'] == false) {
            $msg_type = "error";
			$msg_content = "<b>Gagal:</b> Mohon mengisi captcha.";
		} else if (mysqli_num_rows($check_user) == 0) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'User tidak ditemukan.', 'error');</script><b>Gagal:</b> User tidak ditemukan.";
		} else if (mysqli_num_rows($check_email) == 0) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Email tidak ditemukan.', 'error');</script><b>Gagal:</b> Email tidak ditemukan.";			
		} else if (strlen($post_username) > 999) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Username Maksimal 8 karakter.', 'error');</script><b>Gagal:</b> Username Maksimal 8 karakter.";
		} else if ($recaptcha['success'] == false) {
            $msg_type = "error";
			$msg_content = "<b>Gagal:</b> Mohon mengisi captcha.";
		} else {
			
			    $new_password = random(7);
			    $code_verification = random_number(7);
                $insert_user = verif_email($code_verification, $post_email);	
                $send = mysqli_query($db, "UPDATE users SET password = '$new_password', status = 'Waiting', verif_code = '$code_verification' WHERE username = '$post_username'");
                if ($send == true) {
					if ($insert_user == TRUE) {
        				$data_user['status'] = 'Waiting';
        			} else {
        				$msg_type = "error";
        				$msg_content = '<b>Gagal:</b> Error system.';
        		   	}
                } else {
                    $msg_type = "error";
					$msg_content = "<script>swal('Error!', 'Error system (1).', 'error');</script><b>Gagal:</b> Error system (1).";
                }	
			}
		} else if (isset($_POST['verif'])) {
		$post_code = mysqli_real_escape_string($db, trim($_POST['code']));
		$check_code = mysqli_query($db, "SELECT * FROM users WHERE verif_code = '$post_code' AND  status = 'Waiting'");
	    $data_user = mysqli_fetch_assoc($check_code);
			
		if (empty($post_code)) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Mohon mengisi input.', 'error');</script><b>Gagal:</b> Mohon mengisi input.";
			$data_user['status'] = 'Waiting';
		} else if (mysqli_num_rows($check_code) == 0) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Kode verifikasi tidak ditemukan.', 'error');</script><b>Gagal:</b> Kode verifikasi tidak ditemukan.";
			$data_user['status'] = 'Waiting';
		} else {

        $update_user = mysqli_query($db, "UPDATE users SET status = 'Suspended' WHERE verif_code = '$post_code' AND  status = 'Waiting'");
        if ($update_user == TRUE) {
        				$data_user['status'] = 'Suspended';
        			} else {
        				$msg_type = "error";
        				$msg_content = '<b>Gagal:</b> Error system.';
        				$data_user['status'] = 'Waiting';
        		   	} 	   
		    }
	    } else if (isset($_POST['restore_password'])) {
	    $post_username = mysqli_real_escape_string($db, trim($_POST['username']));
		$post_password = mysqli_real_escape_string($db, trim($_POST['password']));
		$post_repeat_password = mysqli_real_escape_string($db, trim($_POST['repassword']));
		$check_usernya = mysqli_query($db, "SELECT * FROM users WHERE verif_code = '$post_username' AND status = 'Suspended'");
		if (strlen($post_password) > 15) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Password Maksimal 15 karakter.', 'error');</script><b>Gagal:</b> Password Maksimal 15 karakter.";
			$data_user['status'] = 'Suspended';
		} else if (strlen($post_password) < 5) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Password Minimal 5 karakter.', 'error');</script><b>Gagal:</b> Password Minimal 5 karakter.";
			$data_user['status'] = 'Suspended';
		} else if ($post_password <> $post_repeat_password) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'Password tidak sesuai.', 'error');</script><b>Gagal:</b> Password tidak sesuai.";
			$data_user['status'] = 'Suspended';
		} else if (mysqli_num_rows($check_usernya) == 0) {
			$msg_type = "error";
			$msg_content = "<script>swal('Error!', 'User tidak ditemukan.', 'error');</script><b>Gagal:</b> User tidak ditemukan.";
			$data_user['status'] = 'Suspended';
		} else {
        $update_user = mysqli_query($db, "UPDATE users SET password = '$post_password', status = 'Active' WHERE verif_code = '$post_username' AND status = 'Suspended'");
        if ($update_user == TRUE) {
                        $msg_type = "success";
        				$msg_content = "<b>Berhasil:</b> Reset password berhasil. Anda akan dialihkan ke halaman login.<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=login\">";
        			} else {
        				$msg_type = "error";
        				$msg_content = '<b>Gagal:</b> Error system.';    
                    }	   
		        }
	        }
	    }
?>
          <!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="SMM Panel Indonesia yang menyediakan Followers, Likes, Views, Subscriber, untuk beragam social media: Instagram, Youtube, Facebook, Twitter dengan harga Termurah." name="description"/>
    <title>Borneo Media - SMM</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="SMM Panel Indonesia, Followers, Likes, Views, Subscriber, untuk beragam social media: Instagram, Youtube, Facebook, Twitter, harga Termurah">
    <meta name="author" content="</CODINGINAN>">
    <link rel="icon" href="<?= $cfg_baseurl ?>assets/images/fav.png" type="image/x-icon">
    <link rel="shortcut icon" href="<?= $cfg_baseurl ?>assets/images/favicon.png" type="image/x-icon">
    <!-- Google font-->
    <link href="https://fonts.googleapis.com/css?family=Rubik:400,400i,500,500i,700,700i&amp;display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i,900&amp;display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../../assets/css/font-awesome.css">
    <!-- ico-font-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/icofont.css">
    <!-- Themify icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/themify.css">
    <!-- Flag icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/flag-icon.css">
    <!-- Feather icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/feather-icon.css">
    <!-- Plugins css start-->
    <!-- Plugins css Ends-->
    <!-- Bootstrap css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/bootstrap.css">
    <!-- App css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/style.css">
    <link id="color" rel="stylesheet" href="../../assets/css/color-1.css" media="screen">
    <!-- Responsive css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/responsive.css">
  </head>
  <body>
    <!-- login page start-->
    <div class="container-fluid p-0">
        <? if (($data_user['status'] != 'Waiting') && ($data_user['status'] != 'Suspended')){ ?>
      <div class="row m-0">
        <div class="col-12 p-0">    
          <div class="login-card login-dark">
            <div>
              <div><a class="logo" href="index.html"><img class="img-fluid for-light" src="../../assets/images/logo/BMSMM.png" alt="looginpage"><img class="img-fluid for-dark" src="../../assets/images/logo/BMSMM.png" alt="looginpage"></a></div>
              <div class="login-main"> 
              <?php if ($msg_type == "success") {
											?>
											<div class="alert alert-success dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                          <META HTTP-EQUIV=Refresh CONTENT=\"2; URL=login\">
                        </div>
											<?php } ?>
                <?php if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php
											}
											?>
                <form class="theme-form" method="POST">
                  <h4>Reset Password</h4>
                  <p>Masukkan Email & Username untuk Reset Password</p>
                  <div class="form-group">
                    <label class="col-form-label">Email</label>
                    <input class="form-control" type="email" name="email" required placeholder="Your Email">
                  </div>
                  <div class="form-group">
                    <label class="col-form-label">username</label>
                    <input class="form-control" type="text" name="username" required placeholder="Your Username">
                  </div>
                  <div class="form-group">
                    <label class="col-form-label">SAYA BUKAN ROBOT!</label>
                    <div class="form-input position-relative">
                      <div class="g-recaptcha" data-sitekey="6LftOK4nAAAAAD9UwoeBkEYW8YY1pJaa8m4GQqMC"></div>
                    </div>
                  </div>
                  <div class="form-group mb-0">
                    <div class="text-end mt-3">
                      <button class="btn btn-primary btn-block w-100" type="submit" name="reset">Reset Password</button>
                    </div>
                  </div>
                  <p class="mt-4 mb-0 text-center">Belum memiliki akun?<a class="ms-2" href="register">Daftar Sekarang</a></p>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      <?php } else if ($data_user['status'] == 'Waiting') { ?>
      <div class="row m-0">
        <div class="col-12 p-0">    
          <div class="login-card login-dark">
            <div>
              <div><a class="logo" href="index.html"><img class="img-fluid for-light" src="../../assets/images/logo/logo.png" alt="looginpage"><img class="img-fluid for-dark" src="../../assets/images/logo/logo_dark.png" alt="looginpage"></a></div>
              <div class="login-main"> 
                <?php if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php
											}
											?>
                <form class="theme-form" method="POST">
                  <h4>Verify your code</h4>
                  <p>Enter your verification code</p>
                  <div class="form-group">
                    <label class="col-form-label">Verification Code</label>
                    <input class="form-control" type="number" name="code" required placeholder="Kode Verifikasi">
                  </div>
                  <div class="form-group mb-0">
                    <div class="text-end mt-3">
                      <button class="btn btn-primary btn-block w-100" type="submit" name="verif">Verif Sekarang!</button>
                    </div>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      <? }  else if ($data_user['status'] == 'Suspended') { ?>
      <div class="row m-0">
        <div class="col-12 p-0">    
          <div class="login-card login-dark">
            <div>
              <div><a class="logo" href="index.html"><img class="img-fluid for-light" src="../../assets/images/logo/logo.png" alt="looginpage"><img class="img-fluid for-dark" src="../../assets/images/logo/logo_dark.png" alt="looginpage"></a></div>
              <div class="login-main"> 
              <?php if ($msg_type == "success") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                          <META HTTP-EQUIV=Refresh CONTENT=\"2; URL=login\">
                        </div>
											<?php } ?>
                <?php if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php } ?>
                <form class="theme-form" method="POST">
                  <h4>Verify your code</h4>
                  <p>Enter your verification code</p>
                  <div class="form-group">
                    <label class="col-form-label">Verification Code</label>
                    <div class="form-input position-relative">
                      <input class="form-control" type="text" name="username" value="<?=$data_user['verif_code']?>" readonly>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-form-label">Password</label>
                    <div class="form-input position-relative">
                      <input class="form-control" type="password" name="password" required placeholder="*********" id="myInput">
                      <div onclick="myFunction()" class="show-hide"><span class="show"></span></div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-form-label">Ulangi Password</label>
                    <div class="form-input position-relative">
                      <input class="form-control" type="password" name="repassword" required placeholder="*********" id="myInput2">
                      <div onclick="myFunction2()" class="show-hide"><span class="show"></span></div>
                    </div>
                  </div>
                  <div class="form-group mb-0">
                    <div class="text-end mt-3">
                      <button class="btn btn-primary btn-block w-100" type="submit" name="restore_password">Reset Password!</button>
                    </div>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      <?php } ?>
      <!-- latest jquery-->
      <script src="../../assets/js/jquery.min.js"></script>
      <!-- Bootstrap js-->
      <script src="../../assets/js/bootstrap/bootstrap.bundle.min.js"></script>
      <!-- feather icon js-->
      <script src="../../assets/js/icons/feather-icon/feather.min.js"></script>
      <script src="../../assets/js/icons/feather-icon/feather-icon.js"></script>
      <!-- scrollbar js-->
      <!-- Sidebar jquery-->
      <script src="../../assets/js/config.js"></script>
      <!-- Plugins JS start-->
      <!-- Plugins JS Ends-->
      <!-- Theme js-->
      <script src="../../assets/js/script.js"></script>
      <script src='https://www.google.com/recaptcha/api.js'></script>
      <script>
    function myFunction() {
      var x = document.getElementById("myInput");
      if (x.type === "password") {
        x.type = "text";
      } else {
        x.type = "password";
      }
    }
    
    function myFunction2() {
      var x = document.getElementById("myInput2");
      if (x.type === "password") {
        x.type = "text";
      } else {
        x.type = "password";
      }
    }
    </script>
    </div>
  </body>
</html>