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