From c431ea66844128fa17a93c7bd5a500fc9ab13ac1 Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Sun, 1 Oct 2023 00:48:41 +0700
Subject: [PATCH] fix: htaccess

---
 .htaccess                    |  6 ++++++
 Dockerfile                   |  2 ++
 api/auth/login.php           |  6 +++---
 api/auth/register.php        |  4 ++--
 app/controllers/Learn.php    |  2 +-
 app/core/App.php             | 20 ++++++++++++--------
 app/views/home/index.php     |  2 +-
 app/views/learn/index.php    |  8 ++++----
 app/views/login/index.php    |  2 +-
 app/views/navbar/index.php   | 16 ++++++++--------
 app/views/register/index.php |  2 +-
 11 files changed, 41 insertions(+), 29 deletions(-)
 create mode 100644 .htaccess

diff --git a/.htaccess b/.htaccess
new file mode 100644
index 0000000..35063d8
--- /dev/null
+++ b/.htaccess
@@ -0,0 +1,6 @@
+Options -Multiviews
+
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-d 
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^(.*)$ index.php?url=$1 [L]
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index eb4e8e5..287fc7a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,6 +1,8 @@
 FROM php:8.0-apache
 COPY . /var/www/html
 
+RUN a2enmod rewrite
+
 RUN apt-get update \
     && apt-get install -y libpq-dev \
     && docker-php-ext-install pdo pdo_pgsql
diff --git a/api/auth/login.php b/api/auth/login.php
index 7695f9b..361f9da 100644
--- a/api/auth/login.php
+++ b/api/auth/login.php
@@ -17,15 +17,15 @@ if (isset($_POST['username'])) {
 
   if ($result == null) {
     $_SESSION['error'] = "Invalid username or password";
-    header('location: ../../?login');
+    header('location: ../../login');
   } else {
     if (password_verify($password, $result['password'])) {
       $_SESSION['username'] = $username;
       $_SESSION['is_admin'] = $result['is_admin'] ? 1 : 0;
-      header('Location: ../../?learn');
+      header('Location: ../../learn');
     } else {
       $_SESSION['error'] = "Invalid username or password";
-      header('location: ../../?login');
+      header('location: ../../login');
     }
   }
 }
\ No newline at end of file
diff --git a/api/auth/register.php b/api/auth/register.php
index db1184a..7c3941a 100644
--- a/api/auth/register.php
+++ b/api/auth/register.php
@@ -40,10 +40,10 @@ if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['emai
   $rows = $user_model->register(array('username' => $username, 'password' => $password, 'email' => $email));
 
   if ($rows) {
-    header('Location: ../../?login');
+    header('Location: ../../login');
     echo json_encode(array('status' => 'success', 'message' => 'User created'));
   } else {
     $_SESSION['error'] = "Something went wrong";
-    header('Location: ../../?register');
+    header('Location: ../../register');
   }
 }
diff --git a/app/controllers/Learn.php b/app/controllers/Learn.php
index c4453b6..968c0b9 100644
--- a/app/controllers/Learn.php
+++ b/app/controllers/Learn.php
@@ -3,7 +3,7 @@
 class Learn extends Controller {
   public function index() {
     if (!$this->isLoggedIn()) {
-        header('Location: /?login');
+        header('Location: /login');
         exit();
     }
 
diff --git a/app/core/App.php b/app/core/App.php
index dd7f5a3..73b24d8 100644
--- a/app/core/App.php
+++ b/app/core/App.php
@@ -39,15 +39,19 @@ class App {
     }
   
     public function parse_url()
-    {
-      if (isset($_SERVER['REQUEST_URI'])) {
-        $url = rtrim($_SERVER['REQUEST_URI'], '/');
-        $url = ltrim($url, 'public/');
-        $url = filter_var($url, FILTER_SANITIZE_URL);
-        $url = ltrim($url, '?');
-        $url = explode('/', $url);
-        return $url;
+  {
+    if (isset($_SERVER['REQUEST_URI'])) {
+      if ($_SERVER['REQUEST_URI'] == '/') {
+        return [''];
       }
+      $url = $_SERVER['REQUEST_URI'];
+      $url = preg_replace('#^/public/#', '/', $url);
+      $url = rtrim($url, '/');
+      $url = explode('/', $url);
+      $url = array_values(array_filter($url));
+
+      return $url;
     }
   }
+  }
   
\ No newline at end of file
diff --git a/app/views/home/index.php b/app/views/home/index.php
index 2135f39..cf71c6e 100644
--- a/app/views/home/index.php
+++ b/app/views/home/index.php
@@ -15,7 +15,7 @@ $username = isset($_SESSION['username']) ? $_SESSION['username'] : null;
                         <h1 class="text-xl font-bold text-blue-purple-gradient">
                             Your Path to <br> Multilingual Excellence
                         </h1>                        
-                        <a class="distinct-button get-started-btn font-reg text-sm" href="<?php echo $username ? '/?learn' : '/?login'; ?>">
+                        <a class="distinct-button get-started-btn font-reg text-sm" href="<?php echo $username ? '/learn' : '/login'; ?>">
                             Get Started
                         </a>
                     </div>
diff --git a/app/views/learn/index.php b/app/views/learn/index.php
index 382b254..56f6c8d 100644
--- a/app/views/learn/index.php
+++ b/app/views/learn/index.php
@@ -17,7 +17,7 @@
             </h1>
             <div class="learn-container">
                 <div class="learn-card">
-                    <a href="/?preview">
+                    <a href="/preview">
                         <div>
                             <img class="learn-card-image" src="../../../public/icons/uk-flag.svg" alt="English" height="130px">
                             <h2 class="learn-card-header">
@@ -27,7 +27,7 @@
                     </a>
                 </div>
                 <div class="learn-card">
-                    <a href="/?preview">
+                    <a href="/preview">
                         <div>
                             <img class="learn-card-image" src="../../../public/icons/id-flag.svg" alt="Indonesian" height="130px">
                             <h2 class="learn-card-header">
@@ -37,7 +37,7 @@
                     </a>
                 </div>
                 <div class="learn-card">
-                    <a href="/?preview">
+                    <a href="/preview">
                         <div>
                             <img class="learn-card-image" src="../../../public/icons/fr-flag.svg" alt="French" height="130px">
                             <h2 class="learn-card-header">
@@ -47,7 +47,7 @@
                     </a>
                 </div>
                 <div class="learn-card">
-                    <a href="/?preview">
+                    <a href="?preview">
                         <div>
                             <img class="learn-card-image" src="../../../public/icons/gr-flag.svg" alt="Germany" height="130px">
                             <h2 class="learn-card-header">
diff --git a/app/views/login/index.php b/app/views/login/index.php
index 942b2cf..4f47140 100644
--- a/app/views/login/index.php
+++ b/app/views/login/index.php
@@ -36,7 +36,7 @@
                     </div>
                 </form>
                 <h3 class="login-desc1">
-                    <a class="login-desc1-click" href="/?register">
+                    <a class="login-desc1-click" href="/register">
                         Don't have an account yet? Register here
                     </a>
                 </h3>
diff --git a/app/views/navbar/index.php b/app/views/navbar/index.php
index b199534..ba30423 100644
--- a/app/views/navbar/index.php
+++ b/app/views/navbar/index.php
@@ -12,7 +12,7 @@ $is_admin = isset($_SESSION['is_admin']) ? $_SESSION['is_admin'] : null;
             </a>
 
             <ul class="nav-menu">
-                <li><a href="<?php echo $username ? '/?learn' : '/?login'; ?>" class="text-sm text-black">Learn</a></li>
+                <li><a href="<?php echo $username ? '/learn' : '/login'; ?>" class="text-sm text-black">Learn</a></li>
                 <li><a href="/#" class="text-sm text-black">Articles</a></li>
                 <li><a href="/#" class="text-sm text-black">Bootcamp</a></li>
             </ul>
@@ -32,12 +32,12 @@ $is_admin = isset($_SESSION['is_admin']) ? $_SESSION['is_admin'] : null;
                         <span class="text-sm"><?php echo $username; ?>'s learning</span>
                     <?php endif; ?>
                     <div>
-                        <a href="/?profile">
+                        <a href="/profile">
                             <img id="profile-picture" src="/public/icons/profile.webp" alt="Profile picture" height="40px" draggable="false">
                         </a>
                     </div>
                 <?php else : ?>
-                    <a href="/?login">
+                    <a href="/login">
                         <button class="login-btn text-sm primary-button font-reg">
                             Login
                         </button>
@@ -49,19 +49,19 @@ $is_admin = isset($_SESSION['is_admin']) ? $_SESSION['is_admin'] : null;
     </div>
     <div class="dropdown-menu">
         <ul>
-            <li><a href="<?php echo $username ? '/?learn' : '/?login'; ?>" class="text-sm text-black">Learn</a></li>
+            <li><a href="<?php echo $username ? '/learn' : '/login'; ?>" class="text-sm text-black">Learn</a></li>
             <li><a href="/#" class="text-sm text-black">Articles</a></li>
             <li><a href="/#" class="text-sm text-black">Bootcamp</a></li>
             <?php if ($username) : ?>
-                <li><a href="/?logout" class="text-sm text-black"><?php echo $username; ?>'s learning</a></li>
+                <li><a href="/logout" class="text-sm text-black"><?php echo $username; ?>'s learning</a></li>
                 <?php if ($is_admin) : ?>
-                    <li><a href="/?cms" class="text-sm text-black">CMS</a></li>
+                    <li><a href="/cms" class="text-sm text-black">CMS</a></li>
                 <?php else : ?>
-                    <li><a href="/?profile" class="text-sm text-black">Profile</a></li>
+                    <li><a href="/profile" class="text-sm text-black">Profile</a></li>
                 <?php endif; ?>
                 <li><a href="../../../api/auth/logout.php" class="text-sm text-black">Logout</a></li>
             <?php else : ?>
-                <li><a href="/?login" class="login-btn text-sm primary-button font-reg">Login</a></li>
+                <li><a href="/login" class="login-btn text-sm primary-button font-reg">Login</a></li>
             <?php endif; ?>
         </ul>
     </div>
diff --git a/app/views/register/index.php b/app/views/register/index.php
index 4882959..5f5eb6c 100644
--- a/app/views/register/index.php
+++ b/app/views/register/index.php
@@ -40,7 +40,7 @@
                     </div>
                 </form>
                 <h3 class="register-desc1">
-                    <a href="/?login" class="register-desc1-click">
+                    <a href="/login" class="register-desc1-click">
                         Already have an account? Login here
                     </a>
                 </h3>
-- 
GitLab