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