Sixtens hemsida Uppgifter Blogg Om

VED House bokningssystem

Gå till sida

Källkod

api/

database.php

api.php

ved/

monthBookingData.php

openingHoursException.php

bookingData.php

dayTimeframes.php

bookings.php

admin/

revokeToken.php

authorization.php

login.php

modules/

vedDatabase.php

utility.php

gyar/

log.php

auth/

revokeToken.php

authorization.php

login.php

station/

settings.php

log.php

modules/

gyarDatabase.php

databaseConnection.php

database.php

utility.php

credentials.json

weather/

latest.php

old.php

admin/

index.html

changeTimetable.html

login.html

js/

login.js

modules.js

admin.js

header.js

changeTimetable.js

cron/

vedClearExpiredTokens.php

api/gyar/log.php

1 lines
<?php
# latest.php
# API endpoint
# Fil för att hämta den senaste väderdatan från en viss mätpunkt.

require_once "gyar/modules/utility.php";

# Hämtar den senaste tillgängliga väderdatan för dagen.
$functionPOST = function () {
    
$headers getallheaders();
    if (!isset(
$headers["Authorization"]))
        throw new 
APIError(401"Invalid authorization.");
    
$authData explode(" "$headers["Authorization"]);
    if (
sizeof($authData) != 2)
        throw new 
APIError(401"Invalid authorization.");
    
$authType $authData[0];
    
$authToken $authData[1];
    
$hashed hash("sha256"$authToken);

    require 
"gyar/modules/gyarDatabase.php";
    
$station $db->query("SELECT stationId FROM weatherStation WHERE apiKeyHash=:apiKeyHash", array("apiKeyHash" => $hashed));
    if (!
$station)
        throw new 
APIError(401"Invalid authorization.");

    
$pointInstance $db->query("SELECT stationPointInstanceId FROM stationPointInstance WHERE stationId=:stationId"$station);
    if (!
$pointInstance)
        throw new 
APIError(403"Station has not been assigned a point.");

    
$db->pdo->beginTransaction();
    try {
        
$created $db->query("INSERT INTO stationPointTimeData(stationPointInstanceId, timestamp) VALUES (:stationPointInstanceId, CURRENT_TIMESTAMP())"$pointInstance);
    } catch (
Exception $ex) {
        
# Om någonting misslyckas backas allting tillbaka..
        
$db->pdo->rollBack();
        throw 
$ex;
    }
    
$db->pdo->commit();
    
http_response_code(201);

    return 
$created;
};

$api = new APIEndpoint();
$auth = (require "gyar/auth/authorization.php")(AuthLevel::STATION);

$httpPOST = new APIMethod("POST"$functionPOST);
$httpPOST->setAuthorization($auth);
$httpPOST->setRequiredBodyParameters(array("temperature"));
$api->addMethod($httpPOST);

return 
$api;
?>