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