Tidy a bit the files structure.
[Project_webapp.git] / index.php
CommitLineData
fb6aedc2 1<?php
b5f60f89 2require('includes/header.php');
fb6aedc2 3
6405835a 4include('lib/db.php');
65fc0194 5include('lib/utils.php');
fb6aedc2
JB
6
7/**
8 * [session_start start a unique session for the current browser client]
9 */
10session_start();
11
b5f60f89
JB
12$connection = new CustomDB($configs['host'], $configs['username'], $configs['password'], $configs['database']);
13
6405835a
JB
14$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL);
15$isPage = true;
fb6aedc2 16if (!isset($page)) {
6405835a 17 $isPage = false;
fb6aedc2
JB
18 $page = "";
19}
20
65fc0194
JB
21/**
22 * form MUST have an hidden field named 'form' to enable proper routing
23 */
6405835a
JB
24$form = filter_input(INPUT_POST, 'form', FILTER_SANITIZE_URL);
25$isForm = true;
26if (!isset($form)) {
27 $isForm = false;
28 $form = "";
29}
30
65fc0194
JB
31function get_action_type() {
32 global $isPage, $isForm;
33
34 if ($isPage && !$isForm && is_get_request()) {
35 return "page";
36 } elseif ($isForm && !$isPage && is_post_request()) {
37 return "form";
38 } elseif (!$isPage && !$isForm && is_get_request()){
39 return "empty";
40 } else {
41 return "unknown";
42 }
43}
44
b5f60f89 45function get_url_action($action_type) {
65fc0194
JB
46 global $page, $form;
47
b5f60f89 48 if ($action_type === "page") {
65fc0194 49 return $page;
b5f60f89 50 } elseif ($action_type === "form") {
65fc0194 51 return $form;
b5f60f89 52 } elseif ($action_type === "empty") {
65fc0194 53 return "";
b5f60f89
JB
54 } elseif ($action_type === "unknown") {
55 die('Cannot get a valid action from URL parameters or form fields');
65fc0194
JB
56 }
57}
58
fb6aedc2 59/**
6405835a 60 * [validate_url_action validate if the current page is a valid site actions]
fb6aedc2
JB
61 * @param [string] $page [the viewed page name]
62 * @return [string] [the valid matching action or the default action
63 * if the action name do not exist ]
64 */
6405835a 65function validate_url_action($action_name)
fb6aedc2
JB
66{
67 global $configs;
68
69 foreach ($configs['actions'] as $action) {
6405835a 70 if (strcmp($action_name, $action) === 0) {
fb6aedc2 71 // The actions list can't have duplicated entries
265d1374 72 return $action;
fb6aedc2
JB
73 }
74 }
265d1374
JB
75 // Return the default action
76 return $configs['actions'][0];
fb6aedc2
JB
77}
78
b5f60f89 79function display_action($action_name, $action_type)
fb6aedc2 80{
b5f60f89
JB
81 global $configs;
82 $includes_rpath = 'includes';
fb6aedc2 83
265d1374 84 $found_action = false;
fb6aedc2 85 foreach ($configs['actions'] as $action) {
265d1374 86 if (strcmp($action_name, $action) === 0) {
65fc0194 87 // HTTP GET case
b5f60f89
JB
88 if ($action_type === "page" || $action_type === "empty") {
89 include($includes_rpath . "/" . $action . ".php");
65fc0194 90 // HTTP POST case
b5f60f89
JB
91 } elseif ($action_type === "form") {
92 include($includes_rpath . "/form" . $action . ".php");
6405835a 93 } else {
b5f60f89 94 echo "Unknown error in action displaying <br>";
6405835a 95 }
265d1374 96 $found_action = true;
fb6aedc2 97 break;
fb6aedc2
JB
98 }
99 }
265d1374 100 if (!$found_action) {
b5f60f89 101 echo "Action to display do not exist <br>";
265d1374 102 }
fb6aedc2
JB
103}
104
b5f60f89
JB
105//password_hash('12345678', PASSWORD_DEFAULT);
106
107$action_type = get_action_type();
108$url_action = get_url_action($action_type);
6405835a 109$action = validate_url_action($url_action);
b5f60f89
JB
110display_action($action, $action_type);
111
112$connection->close();
fb6aedc2
JB
113
114session_destroy();
115
b5f60f89 116require('includes/footer.html');
fb6aedc2 117?>