Refine the fligth search:
[Project_webapp.git] / index.php
index 125f62049964198da07139d78e5286c4990abb5b..9dc9e0bff683e93bc416467c952f29a9e88095af 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,33 +1,55 @@
 <?php
-require('header.php');
+require('includes/header.php');
 
 include('lib/db.php');
-/**
- * Let's use an array as the list of tunables.
- * Put in a variable the inclusion of this file:
- * $config_var = include('config.php');
- */
-$configs = include('config.php');
 
-/**
- * [session_start start a unique session for the current browser client]
- */
-session_start();
+$connection = new CustomDB($configs['host'], $configs['username'], $configs['password'], $configs['database']);
 
 $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL);
-$isPage = true;
+$is_page = true;
 if (!isset($page)) {
-    $isPage = false;
+    $is_page = false;
     $page = "";
 }
 
+/**
+ * form MUST have an hidden field named 'form' to enable proper routing
+ */
 $form = filter_input(INPUT_POST, 'form', FILTER_SANITIZE_URL);
-$isForm = true;
+$is_form = true;
 if (!isset($form)) {
-    $isForm = false;
+    $is_form = false;
     $form = "";
 }
 
+function get_action_type() {
+    global $is_page, $is_form;
+
+    if ($is_page && !$is_form && is_get_request()) {
+        return "page";
+    } elseif ($is_form && !$is_page && is_post_request()) {
+        return "form";
+    } elseif (!$is_page && !$is_form && is_get_request()){
+        return "empty";
+    } else {
+        return "unknown";
+    }
+}
+
+function get_url_action($action_type) {
+    global $page, $form;
+
+    if ($action_type === "page") {
+        return $page;
+    } elseif ($action_type === "form") {
+        return $form;
+    } elseif ($action_type === "empty") {
+        return "";
+    } elseif ($action_type === "unknown") {
+        die('Cannot get a valid action from URL parameters or form fields.');
+    }
+}
+
 /**
  * [validate_url_action validate if the current page is a valid site actions]
  * @param  [string] $page [the viewed page name]
@@ -48,62 +70,40 @@ function validate_url_action($action_name)
     return $configs['actions'][0];
 }
 
-function get_action_type() {
-    global $page, $form, $isPage, $isForm;
-
-    if ($isPage && !$isForm) {
-        return "page";
-    } elseif ($isForm && !$isPage) {
-        return "form";
-    } elseif (!$isPage && !$isForm){
-        return "empty";
-    } else {
-        return "unknown";
-    }
-}
-
-function display_action($action_name)
+function display_action($action_name, $action_type)
 {
-    global $configs, $isPage, $isForm;
+    global $configs;
+    $includes_rpath = 'includes';
 
     $found_action = false;
     foreach ($configs['actions'] as $action) {
         if (strcmp($action_name, $action) === 0) {
-            if (get_action_type() === "page" || get_action_type() === "empty") {
-                include($action . ".php");
-            } elseif (get_action_type() === "form") {
-                include("form" . $action . ".php");
+            // HTTP GET case
+            if ($action_type === "page" || $action_type === "empty") {
+                include($includes_rpath . "/" . $action . ".php");
+            // HTTP POST case
+            } elseif ($action_type === "form") {
+                include($includes_rpath . "/form" . $action . ".php");
             } else {
-                echo "Impossible error";
+                echo "Unknown error in action displaying. <br>";
             }
             $found_action = true;
             break;
         }
     }
     if (!$found_action) {
-        echo "Action to display do not exist";
-    }
-}
-
-function get_url_action() {
-    global $page, $form, $isPage, $isForm;
-
-    if (get_action_type() === "page") {
-        return $page;
-    } elseif (get_action_type() === "form") {
-        return $form;
-    } elseif (get_action_type() === "empty") {
-        return "";
-    } elseif (get_action_type() === "unknown") {
-        die('Cannot get a valid action from URL parameters');
+        echo "Action to display do not exist. <br>";
     }
 }
 
-$url_action = get_url_action();
+$action_type = get_action_type();
+$url_action = get_url_action($action_type);
 $action = validate_url_action($url_action);
-display_action($action);
+display_action($action, $action_type);
+
+$connection->close();
 
-session_destroy();
+session_write_close();
 
-require('footer.html');
+require('includes/footer.html');
 ?>