From 952205e13222d79e62f640eed4a78db7e4dd9a8d Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 9 Oct 2025 14:48:50 +0000 Subject: [PATCH] herway --- assets/js/main.js | 54 +++++++-- db/migrate.php | 39 ++++++- db/migrations/002_create_trips_table.sql | 9 ++ index.php | 11 +- login.php | 103 +++++++++++++++- logout.php | 6 + my-trips.php | 142 +++++++++++++++++++++++ register.php | 8 +- sos.php | 40 +++++++ trip-setup.php | 65 +++++++++-- 10 files changed, 451 insertions(+), 26 deletions(-) create mode 100644 db/migrations/002_create_trips_table.sql create mode 100644 logout.php create mode 100644 my-trips.php create mode 100644 sos.php diff --git a/assets/js/main.js b/assets/js/main.js index 00dac9e..e074845 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,14 +1,52 @@ document.addEventListener('DOMContentLoaded', function () { - // Initialize Bootstrap components - var toastEl = document.getElementById('sosToast'); - var toast = new bootstrap.Toast(toastEl); + const sosButton = document.getElementById('sosButton'); + const sosToastEl = document.getElementById('sosToast'); + const sosToast = new bootstrap.Toast(sosToastEl); + const toastBody = sosToastEl.querySelector('.toast-body'); + const toastHeader = sosToastEl.querySelector('.toast-header .me-auto'); - // SOS Button functionality - var sosButton = document.getElementById('sosButton'); if (sosButton) { sosButton.addEventListener('click', function () { - // Show the toast notification - toast.show(); + // Disable button to prevent multiple clicks + sosButton.disabled = true; + sosButton.querySelector('.sos-text').textContent = 'Sending...'; + + fetch('sos.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + toastHeader.classList.remove('text-danger'); + toastHeader.classList.add('text-success'); + toastHeader.innerHTML = ' SOS Sent'; + toastBody.textContent = data.message; + } else { + toastHeader.classList.remove('text-success'); + toastHeader.classList.add('text-danger'); + toastHeader.innerHTML = ' Error'; + toastBody.textContent = data.message; + } + sosToast.show(); + }) + .catch(error => { + toastHeader.classList.remove('text-success'); + toastHeader.classList.add('text-danger'); + toastHeader.innerHTML = ' Network Error'; + toastBody.textContent = 'Could not connect to the server. Please check your connection.'; + sosToast.show(); + console.error('SOS fetch error:', error); + }) + .finally(() => { + // Re-enable the button after a delay + setTimeout(() => { + sosButton.disabled = false; + sosButton.querySelector('.sos-text').textContent = 'SOS'; + }, 5000); // 5-second cooldown + }); }); } -}); +}); \ No newline at end of file diff --git a/db/migrate.php b/db/migrate.php index f89fea6..0108e48 100644 --- a/db/migrate.php +++ b/db/migrate.php @@ -1,12 +1,41 @@ exec($sql); - echo "Migration successful: users table created.\n"; + + // 1. Create migrations table if it doesn't exist + $pdo->exec("CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); + + // 2. Get all migrations that have been run + $ran_migrations_stmt = $pdo->query("SELECT migration FROM migrations"); + $ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN); + + // 3. Get all migration files + $migration_files = glob(__DIR__ . '/migrations/*.sql'); + sort($migration_files); + + $migrations_to_run = array_filter($migration_files, function($file) use ($ran_migrations) { + return !in_array(basename($file), $ran_migrations); + }); + + if (empty($migrations_to_run)) { + echo "No new migrations to run.\n"; + } else { + foreach ($migrations_to_run as $file) { + echo "Running migration: " . basename($file) . "...\n"; + $sql = file_get_contents($file); + $pdo->exec($sql); + + // 4. Record the migration + $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); + $stmt->execute([basename($file)]); + echo "Migration successful: " . basename($file) . "\n"; + } + } + } catch (PDOException $e) { die("Migration failed: " . $e->getMessage() . "\n"); -} - +} \ No newline at end of file diff --git a/db/migrations/002_create_trips_table.sql b/db/migrations/002_create_trips_table.sql new file mode 100644 index 0000000..6496935 --- /dev/null +++ b/db/migrations/002_create_trips_table.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS trips ( + id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT(11) UNSIGNED NOT NULL, + destination VARCHAR(255) NOT NULL, + trip_time DATETIME NOT NULL, + status VARCHAR(50) DEFAULT 'planned', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +); diff --git a/index.php b/index.php index 1676ed7..3651669 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,4 @@ + @@ -18,7 +19,7 @@ - + @@ -34,7 +35,13 @@ - + + + + + + + diff --git a/login.php b/login.php index 9ed108c..5738636 100644 --- a/login.php +++ b/login.php @@ -1,3 +1,102 @@ prepare("SELECT * FROM users WHERE email = :email"); + $stmt->bindParam(':email', $email, PDO::PARAM_STR); + $stmt->execute(); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($user && password_verify($password, $user['password_hash'])) { + // Password is correct, start session + $_SESSION['user_id'] = $user['id']; + $_SESSION['user_name'] = $user['name']; + $_SESSION['user_email'] = $user['email']; + header("Location: trip-setup.php"); + exit(); + } else { + $error_message = 'Invalid email or password.'; + } + } catch (PDOException $e) { + $error_message = "Database error: " . $e->getMessage(); + } + } +} +?> + + + + + + Login - HerWay + + + + +
+
+ HerWay + +
+
+ +
+
+
+
+
+

Login

+ +
+ +
+
+ + +
+
+ + +
+
+ +
+
+
+ +
+
+
+
+ + + + + + \ No newline at end of file diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..1744dc5 --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ +prepare("SELECT destination, trip_time, status, created_at FROM trips WHERE user_id = :user_id ORDER BY trip_time DESC"); + $stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT); + $stmt->execute(); + $trips = $stmt->fetchAll(PDO::FETCH_ASSOC); +} catch (PDOException $e) { + $error_message = 'Database error: ' . $e->getMessage(); +} + +$success_message = ''; +if (isset($_GET['status']) && $_GET['status'] === 'success') { + $success_message = 'Your trip has been successfully saved!'; +} +?> + + + + + + My Trips - HerWay + + + + + + + + + + + + + + + + + + + + + +
+
+
+

My Planned Trips

+ Plan a New Trip +
+ + +
+ + + +
+ + +
+
+ +

You have no planned trips yet.

+ +
+ + + + + + + + + + + + + + + + + + + +
DestinationDate & TimeStatusBooked On
+
+ +
+
+
+
+ + + + + + + + + + + diff --git a/register.php b/register.php index b202287..c0f06df 100644 --- a/register.php +++ b/register.php @@ -87,7 +87,13 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { - + + + + + + + diff --git a/sos.php b/sos.php new file mode 100644 index 0000000..b736091 --- /dev/null +++ b/sos.php @@ -0,0 +1,40 @@ + false, 'message' => 'An unexpected error occurred.']; + +$userName = 'An anonymous user'; +$userEmail = 'Not provided'; + +if (isset($_SESSION['user_id'])) { + $userName = $_SESSION['user_name']; + $userEmail = $_SESSION['user_email']; +} + +$subject = 'SOS Alert Triggered by ' . $userName; +$htmlBody = "

An SOS alert was triggered by:

" + . "" + . "

Please take appropriate action immediately.

"; +$textBody = "SOS Alert from " . $userName . " (" . $userEmail . ") at " . date('Y-m-d H:i:s') . " UTC."; + +// Send the email. If no recipient is set in .env, it will use the default. +$result = MailService::sendMail(null, $subject, $htmlBody, $textBody); + +if (!empty($result['success'])) { + $response['success'] = true; + $response['message'] = 'SOS alert sent successfully. Help is on the way.'; +} else { + // In a real app, you would log this error in more detail. + $response['message'] = 'Failed to send SOS alert. Please try again or contact support.'; + // For debugging, you might want to include the error, but not in production. + // $response['error'] = $result['error']; +} + +echo json_encode($response); diff --git a/trip-setup.php b/trip-setup.php index eed919f..7781319 100644 --- a/trip-setup.php +++ b/trip-setup.php @@ -1,3 +1,42 @@ +prepare("INSERT INTO trips (user_id, destination, trip_time) VALUES (:user_id, :destination, :trip_time)"); + $stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT); + $stmt->bindParam(':destination', $destination, PDO::PARAM_STR); + $stmt->bindParam(':trip_time', $trip_time, PDO::PARAM_STR); + + if ($stmt->execute()) { + header('Location: my-trips.php?status=success'); + exit(); + } else { + $error_message = 'Failed to save the trip. Please try again.'; + } + } catch (PDOException $e) { + $error_message = 'Database error: ' . $e->getMessage(); + } + } +} +?> @@ -18,7 +57,7 @@ - + @@ -34,7 +73,13 @@ - + + + + + + + @@ -48,19 +93,23 @@

Plan Your Trip

Enter your destination and travel time to find verified travel companions.

-
+ +
+ + +
- +
- - + +
- +
@@ -86,4 +135,4 @@ - \ No newline at end of file +