index.php
<?php
// Function to delete credentials
function deleteCredentials($conn, $id) {
$sql = "DELETE FROM credentials WHERE id = $id";
if ($conn->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Delete credential if delete button is clicked
if (isset($_POST['delete'])) {
$deleteId = $_POST['delete'];
if (deleteCredentials($conn, $deleteId)) {
$success_message = "Credential deleted successfully!";
} else {
$error_message = "Error deleting credential.";
}
}
// Collect form data for saving credentials
else {
$oauthName = $_POST['oauth_name'];
$clientId = $_POST['client_id'];
$clientSecret = $_POST['client_secret'];
// Save credentials to database
$sql = "INSERT INTO credentials (oauth_name, client_id, client_secret)
VALUES ('$oauthName', '$clientId', '$clientSecret')";
if ($conn->query($sql) === TRUE) {
$success_message = "OAuth 2.0 credentials saved successfully!";
} else {
$error_message = "Error saving credentials.";
}
}
}
// Retrieve credentials from database
$sql = "SELECT * FROM credentials";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Office 365 OAuth 2.0 Setup</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.1.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4">Office 365 OAuth 2.0 Setup</h1>
<!-- OAuth 2.0 Credentials Form -->
<div class="mb-5">
<h2>OAuth 2.0 Credentials</h2>
<form method="post">
<div class="mb-3">
<label for="oauth_name" class="form-label">OAuth Name</label>
<input type="text" class="form-control" id="oauth_name" name="oauth_name">
</div>
<div class="mb-3">
<label for="client_id" class="form-label">Client ID</label>
<input type="text" class="form-control" id="client_id" name="client_id">
</div>
<div class="mb-3">
<label for="client_secret" class="form-label">Client Secret</label>
<input type="text" class="form-control" id="client_secret" name="client_secret">
</div>
<button type="submit" class="btn btn-primary">Save OAuth Credentials</button>
</form>
</div>
<?php if (isset($success_message)) : ?>
<div class="alert alert-success" role="alert">
<?php echo $success_message; ?>
</div>
<?php endif; ?>
<?php if (isset($error_message)) : ?>
<div class="alert alert-danger" role="alert">
<?php echo $error_message; ?>
</div>
<?php endif; ?>
<?php if ($result->num_rows > 0) : ?>
<!-- Display saved credentials -->
<div class="mb-5">
<h2>Saved OAuth 2.0 Credentials</h2>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>OAuth Name</th>
<th>Client ID</th>
<th>Client Secret</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['oauth_name']; ?></td>
<td><?php echo str_repeat("*", strlen($row['client_id'])); ?></td>
<td><?php echo str_repeat("*", strlen($row['client_secret'])); ?></td>
<td>
<form method="post">
<input type="hidden" name="delete" value="<?php echo $row['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<!-- Test Email Form -->
<div>
<hr>
<h2 class="mt-4">Test Email with Saved Credentials</h2>
<form method="post" action="send_email.php">
<div class="mb-3">
<label for="credentials_id" class="form-label">Select OAuth Credentials</label>
<select class="form-select" id="credentials_id" name="credentials_id" required>
<option value="">Select OAuth Credentials</option>
<?php
$result->data_seek(0); // Reset result set to the beginning
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['id'] . '">' . $row['oauth_name'] . '</option>';
}
?>
</select>
</div>
<div class="mb-3">
<label for="test_email" class="form-label">Test Email Address</label>
<input type="email" class="form-control" id="test_email" name="test_email" required>
</div>
<button type="submit" class="btn btn-primary">Send Test Email</button>
</form>
</div>
<?php endif; ?>
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
</body>
</html>