hanze/iwa-panda2

index.php in main
Repositories | Summary | Log | Files | README.md

index.php (5539B) download


  1<?php
  2
  3require_once "utils/autoloader.php";
  4
  5require_once "routing/login.php";
  6require_once "routing/user_add.php";
  7require_once "routing/user.php";
  8$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda");
  9
 10session_start();
 11
 12$templater = new Lollipop\Template(new Lollipop\TemplateMethods());
 13$router = new Lollipop\Router($templater);
 14
 15//login
 16$router->addRoute(["GET"], "/", "views/login.html");
 17$router->addRoute(["POST"], "/login", $login);
 18
 19//css
 20$router->addRoute(["GET"], "css/dashboard.css", "css/dashboard.css");
 21$router->addRoute(["GET"], "css/hdata.css", "css/hdata.css");
 22$router->addRoute(["GET"], "css/login.css", "css/login.css");
 23$router->addRoute(["GET"], "css/map.css", "css/map.css");
 24//logout
 25$router->addRoute(["POST", "GET"], "/logout", $logout);
 26
 27$router->addRoute(["GET"], "/api/:data", function ($key) {
 28    include 'Controller/api/api.php';
 29    $weather_data = [];
 30    if($key['data'] == ':3jvl7yb5sRr80s6lTdeOyxV9VTQZkCPRp7bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk') {
 31        get_windchill();
 32        $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
 33        // we halen data op van 2023-06-28 zodat er data word getoond op de homepage en op de map,
 34        // bij de echte applicatie waar de data elke dag wordt geupdatet zou hier CURDATE() komen te staan.
 35        $query = "SELECT city, min(wind_chill) as wind_chill, country
 36                  FROM retrieve_zoos
 37                  WHERE date = '2023-06-28'
 38                  group by city, country
 39                  ";
 40
 41        $stmt = mysqli_prepare($db, $query);
 42        $stmt->execute();
 43        $data = $stmt->get_result();
 44
 45        while ($row = $data->fetch_assoc()) {
 46            $weather_data[] = [
 47                'windchill' => $row['wind_chill'],
 48                'location' => $row['city'],
 49                'country' => $row['country']
 50            ];
 51        }
 52        header('Content-Type: application/json');
 53        echo json_encode($weather_data);
 54    } elseif($key['data'] == ':ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684') {
 55        get_graph();
 56        $weather_data = [];
 57        $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "panda");
 58        $query = "SELECT distinct humidity, city, date_time
 59                FROM retrieve_facilities
 60                WHERE date(date_time) BETWEEN DATE_SUB(CURDATE(), INTERVAL 28 DAY) AND CURDATE()
 61              ";
 62        $stmt = mysqli_prepare($db, $query);
 63        $stmt->execute();
 64        $data = $stmt->get_result();
 65
 66        while ($row = $data->fetch_assoc()) {
 67            $weather_data[] = [
 68                'humidity' => $row['humidity'],
 69                'location' => $row['city'],
 70                'datetime' => $row['date_time']
 71            ];
 72        }
 73
 74        header('Content-Type: application/json');
 75        echo json_encode($weather_data);
 76    }
 77});
 78
 79if(isset($_SESSION['user_permissions']) && (in_array(1, $_SESSION['user_permissions']) || in_array(2, $_SESSION['user_permissions']))) {
 80    //user
 81    //add
 82    $router->addRoute(["GET"], "/user/add", "views/add_user.html");
 83    $router->addRoute(["POST"], "/user/add", $add_user);
 84
 85    //dashboard
 86    $router->addRoute(["GET"], "/dashboard", function (&$vars) {
 87        $vars += $_SESSION;
 88        return "views/dashboard.html";
 89    });
 90
 91    $router->addRoute(["GET"], "/map", "views/map.html");
 92    $router->addRoute(["GET"], "/hdata", function (&$vars) {
 93        $key = new Key();
 94        if (isset($_GET['downloadXml'])) {
 95            $filename = 'weather_data.xml'; // Specify the filename for the downloaded XML file
 96
 97            // Set the appropriate headers for file download
 98            header('Content-Type: application/xml');
 99            header('Content-Disposition: attachment; filename="' . $filename . '"');
100
101            // Retrieve the XML data
102            $humidityXml = $key->retrieveHumidityTable('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684', true);
103            $hDataXml = $key->retrieveHData('ae9c50dc5cd58c538a0d6aedb17fffedcaffd568d22381dab3ae72baaeb24684', true);
104
105            // Output the XML data directly to the browser
106            echo '<weather>' . $humidityXml . $hDataXml . '</weather>';
107            exit();
108        } else {
109            $vars["table"] = $key->retrieveHData('3jvl/yb?sRr80s6lTdeOyxV9VTQZkCPRp/bKOWKFWxfL2vhsU4Hhpgcmz9qe0zEk');
110        }
111
112        return "views/hdata.html";
113    });
114    //user management for admins
115    if(in_array(1, $_SESSION['user_permissions'])) {
116        $router->addRoute(["GET"], "/user", $user_get);
117
118        $router->addRoute(["POST"], "/user", $user_post);
119
120        $router->addRoute(["GET"], "/user/search/:search_query", $user_query);
121
122        $router->addRoute(["POST"], "/user/search", $user_search);
123
124        $router->addRoute(["GET"], "/user/:primary_key/edit", $user_edit);
125
126        $router->addRoute(["GET"], "/user/:primary_key/delete", $user_delete);
127    }
128}
129
130// static files //
131
132$router->addRoute(['GET'], '/css/dashboard.css', "css/dashboard.css");
133$router->addRoute(['GET'], '/css/login.css', "css/login.css");
134$router->addRoute(['GET'], '/css/hdata.css', "css/hdata.css");
135$router->addRoute(['GET'], '/css/map.css', "css/map.css");
136$router->addRoute(['GET'], '/css/user.css', "css/user.css");
137
138$router->addRoute(['GET'], '/img/big-logo.png', "img/big-logo.png");
139$router->addRoute(['GET'], '/img/small_logo.png', "img/small_logo.png");
140
141$router->addRoute(['GET'], '/js/panda.js', "js/panda.js");
142$router->addRoute(['GET'], '/js/map.js', "js/map.js");
143
144$router->route();