<?php

  require('db.php');

  // Form Processors

  if (isset($_REQUEST['update_site'])) {

    //  Sanitize Fields

    $logoA = filter_var($_REQUEST['colouredLogo'], FILTER_SANITIZE_SPECIAL_CHARS);

    $logoB = filter_var($_REQUEST['plainLogo'], FILTER_SANITIZE_SPECIAL_CHARS);

    $webName = filter_var($_REQUEST['wName'], FILTER_SANITIZE_SPECIAL_CHARS);

    $webUrl = filter_var($_REQUEST['wUrl'], FILTER_SANITIZE_SPECIAL_CHARS);

    $webMail = filter_var($_REQUEST['wEmail'], FILTER_SANITIZE_SPECIAL_CHARS);

    $webIcon = !empty($_FILES['wIcon']) && !empty($_FILES['wIcon']['name']) ? $_FILES['wIcon'] : null;

    $webBTC = filter_var($_REQUEST['wBtc'], FILTER_SANITIZE_SPECIAL_CHARS);

    $webETH = filter_var($_REQUEST['wEth'], FILTER_SANITIZE_SPECIAL_CHARS);

    // Update Website Settings

    if ($webIcon != null) {

      $storage = "../uploads/";

      $iconExt = strtolower(pathinfo($webIcon['name'], PATHINFO_EXTENSION));

      // Rename File

      $icon = $storage . 'favicon.' . $iconExt;

      // Upload Icon

      if (move_uploaded_file($webIcon['tmp_name'], $icon)) {

        $query = mysqli_query($db, "UPDATE `settings` SET `logoA` = '$logoA', `logoB` = '$logoB', `site_name` = '$webName', `site_email` = '$webMail', `site_url` = '$webUrl', `wBTC` = '$webBTC', `wETH` = '$webETH', `site_icon` = '$icon'"); 

        if ($query) {

          echo'<script>
          
            window.location.href = "status";

          </script>';

          exit();

        }

      }


    } else {

      $query = mysqli_query($db, "UPDATE `settings` SET `logoA` = '$logoA', `logoB` = '$logoB', `site_name` = '$webName', `site_email` = '$webMail', `site_url` = '$webUrl', `wBTC` = '$webBTC', `wETH` = '$webETH'"); 

      if ($query) {

        echo'<script>
        
          window.location.href = "status";

        </script>';

        exit();

      }

    }

  } else if (isset($_REQUEST['update_admin'])) {

    //  Sanitize Fields

    $user = filter_var($_REQUEST['adminName'], FILTER_SANITIZE_SPECIAL_CHARS);

    $key = $_REQUEST['adminKey'];

    // Update Website Settings

    $query = mysqli_query($db, "UPDATE `admin` SET `username` = '$user', `password` = '$key'"); 

    if ($query) {

      echo'<script>
      
        window.location.href = "status";

      </script>';

      exit();

    }

  }

?>

<html>
  <head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.9">
    <link rel="icon" href="<?= htmlspecialchars($siteUrl . str_replace('..', '', $siteIcon)) ?>">
    <title><?= $siteName ?> | Admin - Settings</title>
    <style type="text/css">
      <!--
      .style1 {
        color: #F58635
      }

      .style2 {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 24px;
      }

      .style5 {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
      }
      -->
    </style>

  </head>

<body style="background:#f4edf5">
  <div align="center"><br />
    <h2>
      <font color="#f39c12" face="verdana,arial,sans-serif"><?= $siteName; ?></font>
    </h2><br />
    <br />
  </div>
  <hr>
  <center>
    <h1 class="style1 style2">Update Website Settings</h1>
    <strong>
      <font color="red">Please only edit the record field you wish to update</font>
    </strong>
  </center>
  <hr>
  <center>
    <form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
      <table>
        <tr>
          <td><span class="style5">Colured Logo:</span></td>
          <td><input type="text" name="colouredLogo" value="<?php echo $siteLogoA; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Plain Logo:</span></td>
          <td><input type="text" name="plainLogo" value="<?php echo $siteLogoB; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Website Name:</span></td>
          <td><input type="text" name="wName" value="<?php echo $siteName; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Website URL:</span></td>
          <td><input type="text" name="wUrl" value="<?php echo $siteUrl; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Website Email:</span></td>
          <td><input type="text" name="wEmail" value="<?php echo $siteEmail; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Website Favicon:</span></td>
          <td><input type="file" name="wIcon" value="">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Wallet BTC:</span></td>
          <td><input type="text" name="wBtc" value="<?php echo $walletBTC; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Wallet ETH:</span></td>
          <td><input type="text" name="wEth" value="<?php echo $walletETH; ?>">&nbsp;</td>
        </tr>
      </table>
      <input name="update_site" type="submit" value="Update Website Settings">
    </form><br />
    <hr>
  </center>
  <hr>
  <center>
    <h1 class="style1 style2">Update Administrator Data</h1>
    <strong>
      <font color="red">Please only edit the record field you wish to update</font>
    </strong>
  </center>
  <hr>
  <center>
    <form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
      <table>
        <tr>
          <td><span class="style5">Username:</span></td>
          <td><input type="text" name="adminName" value="<?php echo $adminName; ?>">&nbsp;</td>
        </tr>
        <tr>
          <td><span class="style5">Password:</span></td>
          <td><input type="text" name="adminKey" value="<?php echo $adminPass; ?>">&nbsp;</td>
        </tr>
      </table>
      <input name="update_admin" type="submit" value="Update Admin Data">
    </form><br />
    <hr>
  </center>  
</body>

</html>