Properly separate the HTTP GET requests from the POST requests
[Project_webapp.git] / index.php
CommitLineData
fb6aedc2
JB
1<?php
2require('header.php');
3
6405835a 4include('lib/db.php');
fb6aedc2
JB
5/**
6 * Let's use an array as the list of tunables.
7 * Put in a variable the inclusion of this file:
8 * $config_var = include('config.php');
9 */
10$configs = include('config.php');
11
12/**
13 * [session_start start a unique session for the current browser client]
14 */
15session_start();
16
6405835a
JB
17$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL);
18$isPage = true;
fb6aedc2 19if (!isset($page)) {
6405835a 20 $isPage = false;
fb6aedc2
JB
21 $page = "";
22}
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
fb6aedc2 31/**
6405835a 32 * [validate_url_action validate if the current page is a valid site actions]
fb6aedc2
JB
33 * @param [string] $page [the viewed page name]
34 * @return [string] [the valid matching action or the default action
35 * if the action name do not exist ]
36 */
6405835a 37function validate_url_action($action_name)
fb6aedc2
JB
38{
39 global $configs;
40
41 foreach ($configs['actions'] as $action) {
6405835a 42 if (strcmp($action_name, $action) === 0) {
fb6aedc2 43 // The actions list can't have duplicated entries
265d1374 44 return $action;
fb6aedc2
JB
45 }
46 }
265d1374
JB
47 // Return the default action
48 return $configs['actions'][0];
fb6aedc2
JB
49}
50
6405835a
JB
51function get_action_type() {
52 global $page, $form, $isPage, $isForm;
53
54 if ($isPage && !$isForm) {
55 return "page";
56 } elseif ($isForm && !$isPage) {
57 return "form";
58 } elseif (!$isPage && !$isForm){
59 return "empty";
60 } else {
61 return "unknown";
62 }
63}
64
fb6aedc2
JB
65function display_action($action_name)
66{
6405835a 67 global $configs, $isPage, $isForm;
fb6aedc2 68
265d1374 69 $found_action = false;
fb6aedc2 70 foreach ($configs['actions'] as $action) {
265d1374 71 if (strcmp($action_name, $action) === 0) {
6405835a
JB
72 if (get_action_type() === "page" || get_action_type() === "empty") {
73 include($action . ".php");
74 } elseif (get_action_type() === "form") {
75 include("form" . $action . ".php");
76 } else {
77 echo "Impossible error";
78 }
265d1374 79 $found_action = true;
fb6aedc2 80 break;
fb6aedc2
JB
81 }
82 }
265d1374
JB
83 if (!$found_action) {
84 echo "Action to display do not exist";
85 }
fb6aedc2
JB
86}
87
6405835a
JB
88function get_url_action() {
89 global $page, $form, $isPage, $isForm;
90
91 if (get_action_type() === "page") {
92 return $page;
93 } elseif (get_action_type() === "form") {
94 return $form;
95 } elseif (get_action_type() === "empty") {
96 return "";
97 } elseif (get_action_type() === "unknown") {
98 die('Cannot get a valid action from URL parameters');
99 }
100}
101
102$url_action = get_url_action();
103$action = validate_url_action($url_action);
265d1374 104display_action($action);
fb6aedc2
JB
105
106session_destroy();
107
108require('footer.html');
109?>