9d22f3e15361790b8c2fe89561b28cc88ebd0cf2
[Project_webapp.git] / index.php
1 <?php
2 require('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 $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL);
13 $isPage = true;
14 if (!isset($page)) {
15 $isPage = false;
16 $page = "";
17 }
18
19 /**
20 * form MUST have an hidden field named 'form' to enable proper routing
21 */
22 $form = filter_input(INPUT_POST, 'form', FILTER_SANITIZE_URL);
23 $isForm = true;
24 if (!isset($form)) {
25 $isForm = false;
26 $form = "";
27 }
28
29 function 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
43 function 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
57 /**
58 * [validate_url_action validate if the current page is a valid site actions]
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 */
63 function validate_url_action($action_name)
64 {
65 global $configs;
66
67 foreach ($configs['actions'] as $action) {
68 if (strcmp($action_name, $action) === 0) {
69 // The actions list can't have duplicated entries
70 return $action;
71 }
72 }
73 // Return the default action
74 return $configs['actions'][0];
75 }
76
77 function display_action($action_name)
78 {
79 global $configs, $isPage, $isForm;
80
81 $found_action = false;
82 foreach ($configs['actions'] as $action) {
83 if (strcmp($action_name, $action) === 0) {
84 // HTTP GET case
85 if (get_action_type() === "page" || get_action_type() === "empty") {
86 include($action . ".php");
87 // HTTP POST case
88 } elseif (get_action_type() === "form") {
89 include("form" . $action . ".php");
90 } else {
91 echo "Unknown error in action displaying";
92 }
93 $found_action = true;
94 break;
95 }
96 }
97 if (!$found_action) {
98 echo "Action to display do not exist";
99 }
100 }
101
102 $url_action = get_url_action();
103 $action = validate_url_action($url_action);
104 display_action($action);
105
106 session_destroy();
107
108 require('footer.html');
109 ?>