Only allow GET and POST HTTP request and die otherwise.
[Project_webapp.git] / index.php
CommitLineData
fb6aedc2
JB
1<?php
2require('header.php');
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
6405835a
JB
12$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL);
13$isPage = true;
fb6aedc2 14if (!isset($page)) {
6405835a 15 $isPage = false;
fb6aedc2
JB
16 $page = "";
17}
18
65fc0194
JB
19/**
20 * form MUST have an hidden field named 'form' to enable proper routing
21 */
6405835a
JB
22$form = filter_input(INPUT_POST, 'form', FILTER_SANITIZE_URL);
23$isForm = true;
24if (!isset($form)) {
25 $isForm = false;
26 $form = "";
27}
28
65fc0194
JB
29function get_action_type() {
30 global $isPage, $isForm;
31
32 if ($isPage && !$isForm && is_get_request()) {
33 return "page";
34 } elseif ($isForm && !$isPage && is_post_request()) {
35 return "form";
36 } elseif (!$isPage && !$isForm && is_get_request()){
37 return "empty";
38 } else {
39 return "unknown";
40 }
41}
42
43function get_url_action() {
44 global $page, $form;
45
46 if (get_action_type() === "page") {
47 return $page;
48 } elseif (get_action_type() === "form") {
49 return $form;
50 } elseif (get_action_type() === "empty") {
51 return "";
52 } elseif (get_action_type() === "unknown") {
53 die('Cannot get a valid action from URL parameters');
54 }
55}
56
fb6aedc2 57/**
6405835a 58 * [validate_url_action validate if the current page is a valid site actions]
fb6aedc2
JB
59 * @param [string] $page [the viewed page name]
60 * @return [string] [the valid matching action or the default action
61 * if the action name do not exist ]
62 */
6405835a 63function validate_url_action($action_name)
fb6aedc2
JB
64{
65 global $configs;
66
67 foreach ($configs['actions'] as $action) {
6405835a 68 if (strcmp($action_name, $action) === 0) {
fb6aedc2 69 // The actions list can't have duplicated entries
265d1374 70 return $action;
fb6aedc2
JB
71 }
72 }
265d1374
JB
73 // Return the default action
74 return $configs['actions'][0];
fb6aedc2
JB
75}
76
77function display_action($action_name)
78{
6405835a 79 global $configs, $isPage, $isForm;
fb6aedc2 80
265d1374 81 $found_action = false;
fb6aedc2 82 foreach ($configs['actions'] as $action) {
265d1374 83 if (strcmp($action_name, $action) === 0) {
65fc0194 84 // HTTP GET case
6405835a
JB
85 if (get_action_type() === "page" || get_action_type() === "empty") {
86 include($action . ".php");
65fc0194 87 // HTTP POST case
6405835a
JB
88 } elseif (get_action_type() === "form") {
89 include("form" . $action . ".php");
90 } else {
65fc0194 91 echo "Unknown error in action displaying";
6405835a 92 }
265d1374 93 $found_action = true;
fb6aedc2 94 break;
fb6aedc2
JB
95 }
96 }
265d1374
JB
97 if (!$found_action) {
98 echo "Action to display do not exist";
99 }
fb6aedc2
JB
100}
101
6405835a
JB
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?>