Refine the fligth search:
[Project_webapp.git] / index.php
... / ...
CommitLineData
1<?php
2require('includes/header.php');
3
4include('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;
10if (!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;
20if (!isset($form)) {
21 $is_form = false;
22 $form = "";
23}
24
25function get_action_type() {
26 global $is_page, $is_form;
27
28 if ($is_page && !$is_form && is_get_request()) {
29 return "page";
30 } elseif ($is_form && !$is_page && is_post_request()) {
31 return "form";
32 } elseif (!$is_page && !$is_form && is_get_request()){
33 return "empty";
34 } else {
35 return "unknown";
36 }
37}
38
39function get_url_action($action_type) {
40 global $page, $form;
41
42 if ($action_type === "page") {
43 return $page;
44 } elseif ($action_type === "form") {
45 return $form;
46 } elseif ($action_type === "empty") {
47 return "";
48 } elseif ($action_type === "unknown") {
49 die('Cannot get a valid action from URL parameters or form fields.');
50 }
51}
52
53/**
54 * [validate_url_action validate if the current page is a valid site actions]
55 * @param [string] $page [the viewed page name]
56 * @return [string] [the valid matching action or the default action
57 * if the action name do not exist ]
58 */
59function validate_url_action($action_name)
60{
61 global $configs;
62
63 foreach ($configs['actions'] as $action) {
64 if (strcmp($action_name, $action) === 0) {
65 // The actions list can't have duplicated entries
66 return $action;
67 }
68 }
69 // Return the default action
70 return $configs['actions'][0];
71}
72
73function display_action($action_name, $action_type)
74{
75 global $configs;
76 $includes_rpath = 'includes';
77
78 $found_action = false;
79 foreach ($configs['actions'] as $action) {
80 if (strcmp($action_name, $action) === 0) {
81 // HTTP GET case
82 if ($action_type === "page" || $action_type === "empty") {
83 include($includes_rpath . "/" . $action . ".php");
84 // HTTP POST case
85 } elseif ($action_type === "form") {
86 include($includes_rpath . "/form" . $action . ".php");
87 } else {
88 echo "Unknown error in action displaying. <br>";
89 }
90 $found_action = true;
91 break;
92 }
93 }
94 if (!$found_action) {
95 echo "Action to display do not exist. <br>";
96 }
97}
98
99$action_type = get_action_type();
100$url_action = get_url_action($action_type);
101$action = validate_url_action($url_action);
102display_action($action, $action_type);
103
104$connection->close();
105
106session_write_close();
107
108require('includes/footer.html');
109?>