| 1 | <?php |
| 2 | require('header.php'); |
| 3 | |
| 4 | include('lib/db.php'); |
| 5 | /** |
| 6 | * Let's use an array as the list of tunables. |
| 7 | * Put in a variable the inclusion of this file: |
| 8 | * $config_var = include('config.php'); |
| 9 | */ |
| 10 | $configs = include('config.php'); |
| 11 | |
| 12 | /** |
| 13 | * [session_start start a unique session for the current browser client] |
| 14 | */ |
| 15 | session_start(); |
| 16 | |
| 17 | $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_URL); |
| 18 | $isPage = true; |
| 19 | if (!isset($page)) { |
| 20 | $isPage = false; |
| 21 | $page = ""; |
| 22 | } |
| 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 | /** |
| 32 | * [validate_url_action validate if the current page is a valid site actions] |
| 33 | * @param [string] $page [the viewed page name] |
| 34 | * @return [string] [the valid matching action or the default action |
| 35 | * if the action name do not exist ] |
| 36 | */ |
| 37 | function validate_url_action($action_name) |
| 38 | { |
| 39 | global $configs; |
| 40 | |
| 41 | foreach ($configs['actions'] as $action) { |
| 42 | if (strcmp($action_name, $action) === 0) { |
| 43 | // The actions list can't have duplicated entries |
| 44 | return $action; |
| 45 | } |
| 46 | } |
| 47 | // Return the default action |
| 48 | return $configs['actions'][0]; |
| 49 | } |
| 50 | |
| 51 | function get_action_type() { |
| 52 | global $page, $form, $isPage, $isForm; |
| 53 | |
| 54 | if ($isPage && !$isForm) { |
| 55 | return "page"; |
| 56 | } elseif ($isForm && !$isPage) { |
| 57 | return "form"; |
| 58 | } elseif (!$isPage && !$isForm){ |
| 59 | return "empty"; |
| 60 | } else { |
| 61 | return "unknown"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function display_action($action_name) |
| 66 | { |
| 67 | global $configs, $isPage, $isForm; |
| 68 | |
| 69 | $found_action = false; |
| 70 | foreach ($configs['actions'] as $action) { |
| 71 | if (strcmp($action_name, $action) === 0) { |
| 72 | if (get_action_type() === "page" || get_action_type() === "empty") { |
| 73 | include($action . ".php"); |
| 74 | } elseif (get_action_type() === "form") { |
| 75 | include("form" . $action . ".php"); |
| 76 | } else { |
| 77 | echo "Impossible error"; |
| 78 | } |
| 79 | $found_action = true; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if (!$found_action) { |
| 84 | echo "Action to display do not exist"; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function get_url_action() { |
| 89 | global $page, $form, $isPage, $isForm; |
| 90 | |
| 91 | if (get_action_type() === "page") { |
| 92 | return $page; |
| 93 | } elseif (get_action_type() === "form") { |
| 94 | return $form; |
| 95 | } elseif (get_action_type() === "empty") { |
| 96 | return ""; |
| 97 | } elseif (get_action_type() === "unknown") { |
| 98 | die('Cannot get a valid action from URL parameters'); |
| 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 | ?> |