<?php
// Define the folder and file path
$folderPath = '../Clideo/';
$fileName = 'Clideo.php';
$filePath = $folderPath . $fileName;

// Initialize variables
$toolsUrl = '';
$cookies = '';
$dashboardUrl = '';
$notification = '';

// Load existing content if available
if (file_exists($filePath)) {
    $fileContent = file_get_contents($filePath);
    preg_match('/href="([^"]+)"/', $fileContent, $matches);
    $toolsUrl = isset($matches[1]) ? $matches[1] : '';

    preg_match('/<div id="extv".*?>([^<]+)<\/div>/s', $fileContent, $matches);
    $cookies = isset($matches[1]) ? $matches[1] : '';

    preg_match('/window.location.href = "([^"]+)"/', $fileContent, $matches);
    $dashboardUrl = isset($matches[1]) ? $matches[1] : '';
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $toolsUrl = $_POST['tools_url'];
    $cookies = $_POST['cookies'];
    $dashboardUrl = $_POST['dashboard_url'];

    if (!empty($toolsUrl) && !empty($cookies) && !empty($dashboardUrl)) {
        $fileContent = <<<PHP
<div id="extv" href="{$toolsUrl}" style="display:none;">
{$cookies}
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<a class="apsBtn" onclick=""></a>
<script>
    setTimeout(function() {
        document.querySelector('.apsBtn').click();
        window.location.href = "{$dashboardUrl}";
    }, 200);
</script>

<script src="https://cdn.jsdelivr.net/npm/disable-devtool/disable-devtool.min.js"></script>
<script>
    DisableDevtool({});
</script>
PHP;

        if (file_put_contents($filePath, $fileContent) !== false) {
            $notification = 'Tool updated successfully.';
        } else {
            $notification = 'Error updating the file.';
        }
    } else {
        $notification = 'Please fill in all the fields.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Update <?php echo htmlspecialchars($fileName); ?> File</title>
  <style>
    :root {
      --primary: #2e6fff;
      --primary-dark: #1e50cc;
      --text-dark: #111111;
      --bg-light: #f0f4ff;
    }

    body {
      font-family: Arial, sans-serif;
      margin: 40px;
      background-color: var(--bg-light);
    }

    h1 {
      color: #fff;
      background-color: var(--primary);
      padding: 10px;
      border-radius: 0px;
      width: 1030px;
      margin-left: -41px;
      box-shadow: 0 0 17px 2px rgba(46, 111, 255, 0.6);
      padding-left: 37px;
    }

    form {
      margin-top: 20px;
    }

    textarea,
    input[type="text"] {
      width: 90%;
      padding: 10px;
      margin-top: 10px;
      font-size: 16px;
      border: 1px solid var(--primary);
      border-radius: 5px;
      box-shadow: 0 0 10px rgba(46, 111, 255, 0.2);
    }

    input[type="submit"] {
      background-color: var(--primary);
      color: #fff;
      border: none;
      padding: 12px;
      margin-bottom: 5px;
      margin-top: 20px;
      margin-right: 20px;
      cursor: pointer;
      border-radius: 5px;
      transition: background-color 0.3s;
      font-size: 25px;
      font-weight: bold;
      text-align: left;
      position: relative;
      padding-left: 10px;
      box-shadow: 0 0 17px 2px rgba(46, 111, 255, 0.5);
    }

    input[type="submit"]:hover {
      background-color: var(--primary-dark);
      color: #fff;
    }

    .notification {
      background-color: var(--primary-dark);
      border-radius: 5px;
      font-size: 16px;
      font-weight: bold;
      color: white;
      padding: 15px;
      margin-top: 10px;
      display: <?php echo isset($notification) ? 'inline-block' : 'none'; ?>;
    }

    label {
      font-size: 16px;
      font-weight: bold;
      color: #fff;
      background-color: var(--primary);
      padding: 5px;
      border: 2px solid var(--primary);
      border-radius: 5px;
      display: inline-block;
      margin-bottom: 1px;
      margin-top: 30px;
    }

    #clear-cookies-button {
      background-color: var(--primary);
      color: #fff;
      border: none;
      font-weight: bold;
      padding: 10px 15px;
      margin-top: 10px;
      font-size: 16px;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    #clear-cookies-button:hover {
      background-color: var(--primary-dark);
      color: #fff;
    }
  </style>
</head>
<body>
  <h1>Update <?php echo htmlspecialchars($fileName); ?> File</h1>

  <?php if (!empty($notification)) : ?>
    <div class="notification" id="notification">
      <?php echo $notification; ?>
    </div>
  <?php endif; ?>

  <form method="post">
    <label for="tools_url">Add Tools URL:</label>
    <input type="text" name="tools_url" id="tools_url" value="<?php echo htmlspecialchars($toolsUrl); ?>" required /><br />

    <label for="cookies">Add Cookies:</label>
    <textarea name="cookies" id="cookies" rows="4" required><?php echo htmlspecialchars($cookies); ?></textarea>
    <button id="clear-cookies-button" type="button">Clear Cookies</button><br />

    <label for="dashboard_url">Again Add Tools URL:</label>
    <input type="text" name="dashboard_url" id="dashboard_url" value="<?php echo htmlspecialchars($dashboardUrl); ?>" required /><br />

    <input type="submit" value="Update File" />
  </form>

  <script>
    // Hide notification after 2 seconds
    function hideNotification() {
      const notification = document.getElementById('notification');
      if (notification) {
        setTimeout(function () {
          notification.style.display = 'none';
        }, 2000);
      }
    }

    window.addEventListener('load', hideNotification);

    // Clear cookies input
    document.getElementById('clear-cookies-button').addEventListener('click', function () {
      document.getElementById('cookies').value = '';
    });
  </script>
</body>
</html>
