<?php require 'ust.php'; ?>
<main id="main">
<?php if($_GET["sifremiunuttum"] == "ok"){ ?>

  <section class="breadcrumbs">
    <div class="container">
      <div class="d-flex justify-content-between align-items-center">
        <h2>Şifremi Unuttum</h2>
        <ol>
          <li><a href="index.php">Ana Sayfa</a></li>
          <li> Şifremi Unuttum </li>
        </ol>
      </div>
    </div>
  </section>
  <section>
    <div class="col-lg-3" style="margin: auto;">
    <div class="card">
    <div class="card-body">
      <form method="post">
  <div class="form-group my-3">
    <label for="exampleInputEmail1">E-Mail Adresiniz</label>
    <input name="unuttumemail" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
  </div>
  <button name="unuttumsifre" type="submit" class="btn btn-primary my-1">Şifremi Sıfırla</button>
</form>
      </div>
      </div>
      </div>
    </div>
  </section>

<?php }else {?>
  <section class="breadcrumbs">
    <div class="container">
      <div class="d-flex justify-content-between align-items-center">
        <h2>Giriş Yap</h2>
        <ol>
          <li><a href="index.php">Ana Sayfa</a></li>
          <li>Giriş Yap</li>
        </ol>
      </div>
    </div>
  </section>
  <section>
    <div class="col-lg-3" style="margin: auto;">
    <div class="card">
    <div class="card-body">
      <p class="text-center">Üye Girişi</p>
      <form method="post">
  <div class="form-group my-3">
    <label for="exampleInputEmail1">E-Mail Adresiniz</label>
    <input name="email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
  </div>
  <div class="form-group my-3">
    <label for="exampleInputPassword1">Şifreniz</label>
    <input name="sifre" type="password" class="form-control" id="exampleInputPassword1">
  </div>
  <div class="form-group my-3">
    <a href="?sifremiunuttum=ok"><label>Şifremi Unuttum?</label></a> <a href="kayitol.php" style="float: right;"><label>Kayıt Ol!</label></a>
  </div>
  <button name="giris" type="submit" class="btn btn-primary my-1">Giriş Yap</button>
</form>
<form method="post">
<div class="form-group my-3">
  
</div>
</form>
      </div>
      </div>
      </div>
    </div>
  </section>
<?php } ?>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<?php require 'footer.php'; ?>
<?php
if ( isset($_POST['giris']) ){
  $email = $_POST["email"];
  $sifre = md5(md5(md5(hash('sha512',md5(md5(md5($_POST["sifre"])))))));
  $sorgu = mysqli_query($db, "SELECT * FROM uyeler WHERE email = '$email' AND sifre = '$sifre'");
  $sorgudetay = mysqli_fetch_array($sorgu);
  $sorgusay = mysqli_num_rows($sorgu);
  if ($sorgusay > 0) {
    $_SESSION['uyeid'] = $sorgudetay["id"];
    echo '<script>swal("Başarılı", "Giriş Başarılı Panele Yönlendiriliyorsunuz", "success");</script>';
    header("Refresh:2;url=panel.php");
  }else {
    echo '<script>swal("Hata!", "E-Mail veya Şifre Hatalı !", "warning");</script>';
  }
}
if ( isset($_POST['unuttumsifre']) ){

$eposta = $_POST["unuttumemail"];
$mailsor = mysqli_query($db, "SELECT * FROM uyeler WHERE email = '$eposta'");
$mailsay = mysqli_num_rows($mailsor);
if ($mailsay > 0) {
  $smtpbilgi = mysqli_fetch_array(mysqli_query($db, "SELECT * FROM site_smtp WHERE id = '1'"));
  $ysifre = rand(111111,999999);
  $sifregs = md5(md5(md5(hash('sha512',md5(md5(md5($ysifre)))))));
  $mesaj = "Şifrenizi Unuttuğunuzu Gördük Sizin İçin 6 Haneli Bir Yeni Şifre Oluşturduk Bu Şifre Sayesinde Sisteme Giriş Yapabilirsiniz.";
  require 'entegrasyonlar/phpmailer/class.phpmailer.php';
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->Host = $smtpbilgi["smtp_host"];
  $mail->Port = $smtpbilgi["smtp_port"];
  $mail->SMTPSecure = $smtpbilgi["smtp_gvnlk"];
  $mail->Username = $smtpbilgi["smtp_mail"];
  $mail->Password = $smtpbilgi["smtp_sifre"];
  $mail->SetFrom($mail->Username, $site_adi);
  $mail->AddAddress($eposta);
  $mail->CharSet = 'UTF-8';
  $mail->Subject = 'Yeni Şifreniz Başarılı Bir Şekilde Oluşturuldu!';
  $content = '<div style="background: #00ffff; padding: 10px; font-size: 30px"><center>'.$site_adi.'</center><hr><br>'.$mesaj.'<br>Yeni Şifreniz : <b><u>'.$ysifre.'</b></u></div>';
  $mail->MsgHTML($content);
  if($mail->Send()) {
    echo '<script>swal("Başarılı", "Yeni Şifreniz Mail Adresinize Gönderildi", "success");</script>';
    $gunce = mysqli_query($db, "UPDATE uyeler SET sifre = '$sifregs' WHERE email = '$eposta'");
    echo '<script>setTimeout(function(){ window.location.assign("giris.php");}, 1500);</script>';
  } else {
  echo "Mailer Error: " . $mail->ErrorInfo;
 echo '<script>swal("Hata!", "Mail Gönderilemedi Lütfen Tekrar Deneyin", "warning");</script>';
  }
}else {
  echo '<script>swal("Hata!", "Mail Adresi Hatalı", "error");</script>';
}
}
 ?>
