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