Change the color scheme to be more IHM rules compliant.
[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 here
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 /**
28 * [get_action_type description]
29 * @return [type] [description]
30 */
31 function get_action_type() {
32 global $is_page, $is_form;
33
34 if ($is_page && !$is_form && is_get_request()) {
35 return "page";
36 } elseif ($is_form && !$is_page && is_post_request()) {
37 return "form";
38 } elseif (!$is_page && !$is_form && is_get_request()){
39 return "empty";
40 } else {
41 return "unknown";
42 }
43 }
44
45 /**
46 * [get_url_action description]
47 * @param [type] $action_type [description]
48 * @return [type] [description]
49 */
50 function get_url_action($action_type) {
51 global $page, $form;
52
53 if ($action_type === "page") {
54 return $page;
55 } elseif ($action_type === "form") {
56 return $form;
57 } elseif ($action_type === "empty") {
58 return "";
59 } elseif ($action_type === "unknown") {
60 die('Cannot get a valid action from URL parameters or form fields.');
61 }
62 }
63
64 /**
65 * [validate_url_action validate if the current page is a valid site actions]
66 * @param [string] $page [the viewed page name]
67 * @return [string] [the valid matching action or the default action
68 * if the action name do not exist ]
69 */
70 function validate_url_action($action_name)
71 {
72 global $configs;
73
74 foreach ($configs['actions'] as $action) {
75 if (strcmp($action_name, $action) === 0) {
76 // The actions list can't have duplicated entries
77 return $action;
78 }
79 }
80 // Return the default action
81 return $configs['actions'][0];
82 }
83
84 /**
85 * [display_action description]
86 * @param [type] $action_name [description]
87 * @param [type] $action_type [description]
88 * @return [type] [description]
89 */
90 function display_action($action_name, $action_type)
91 {
92 global $configs;
93 $includes_rpath = 'includes';
94
95 $found_action = false;
96 foreach ($configs['actions'] as $action) {
97 if (strcmp($action_name, $action) === 0) {
98 // HTTP GET case
99 if ($action_type === "page" || $action_type === "empty") {
100 include($includes_rpath . "/" . $action . ".php");
101 // HTTP POST case
102 } elseif ($action_type === "form") {
103 include($includes_rpath . "/form" . $action . ".php");
104 } else {
105 echo "Unknown error in action displaying. <br>";
106 }
107 $found_action = true;
108 break;
109 }
110 }
111 if (!$found_action) {
112 echo "Action to display do not exist. <br>";
113 }
114 }
115
116 $action_type = get_action_type();
117 $url_action = get_url_action($action_type);
118 $action = validate_url_action($url_action);
119 display_action($action, $action_type);
120
121 $connection->close();
122
123 session_write_close();
124
125 require('includes/footer.html');
126 ?>