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