Tidy a bit the files structure.
[Project_webapp.git] / index.php
index 01b3ce32fed45e398ceee1032a2c0798f187065f..8bceb64bb6702c58fa45b66bb7f4ea744d6415d5 100644 (file)
--- a/index.php
+++ b/index.php
 <?php
-require('header.php');
+require('includes/header.php');
 
-include('libs/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');
+include('lib/db.php');
+include('lib/utils.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;
 if (!isset($page)) {
+    $isPage = false;
     $page = "";
 }
 
 /**
- * [valid_page validate if the current page is a valid site actions]
+ * form MUST have an hidden field named 'form' to enable proper routing
+ */
+$form = filter_input(INPUT_POST, 'form', FILTER_SANITIZE_URL);
+$isForm = true;
+if (!isset($form)) {
+    $isForm = false;
+    $form = "";
+}
+
+function get_action_type() {
+    global $isPage, $isForm;
+
+    if ($isPage && !$isForm && is_get_request()) {
+        return "page";
+    } elseif ($isForm && !$isPage && is_post_request()) {
+        return "form";
+    } elseif (!$isPage && !$isForm && 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]
  * @return [string]       [the valid matching action or the default action
  *                         if the action name do not exist ]
  */
-function valid_page($page_name)
+function validate_url_action($action_name)
 {
     global $configs;
 
     foreach ($configs['actions'] as $action) {
-        if ($page_name === $action) {
-            return $action;
+        if (strcmp($action_name, $action) === 0) {
             // The actions list can't have duplicated entries
-            break;
-        } else {
-            return $configs['actions'][0];
+            return $action;
         }
     }
+    // Return the default action
+    return $configs['actions'][0];
 }
 
-function display_action($action_name)
+function display_action($action_name, $action_type)
 {
     global $configs;
+    $includes_rpath = 'includes';
 
+    $found_action = false;
     foreach ($configs['actions'] as $action) {
-        if ($action_name === $action) {
-            include("$action.php");
+        if (strcmp($action_name, $action) === 0) {
+            // 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 "Unknown error in action displaying <br>";
+            }
+            $found_action = true;
             break;
-        } else {
-            echo "Action to display do not exist";
         }
     }
+    if (!$found_action) {
+        echo "Action to display do not exist <br>";
+    }
 }
 
-display_action(valid_page($page));
+//password_hash('12345678', PASSWORD_DEFAULT);
+
+$action_type = get_action_type();
+$url_action = get_url_action($action_type);
+$action = validate_url_action($url_action);
+display_action($action, $action_type);
+
+$connection->close();
 
 session_destroy();
 
-require('footer.html');
+require('includes/footer.html');
 ?>