oebb/index.php
2017-08-31 13:21:59 +02:00

128 lines
4.8 KiB
PHP

<?php
require_once("config.php");
require_once($apipath);
require_once($pqpath);
$api = new oebbApi();
function error($string, $code = 400) {
http_response_code($code);
die($string);
}
function prepare() {
global $output;
header("Content-type: application/" . $output . "; charset=utf-8");
if ($output == "xml") {
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
}
}
if (isset($_GET["debug"])) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
$output = (isset($_GET["output"]) ? strtolower($_GET["output"]) :
(isset($_POST["output"]) ? strtolower($_POST["output"]) :
$output = "xml"));
if ($output != "xml") {
error("Unknown output type provided.");
}
if (!isset($_GET["type"]) && !isset($_POST["type"])) {
error("No request type provided.");
}
$type = strtolower((isset($_GET["type"]) ? $_GET["type"] : $_POST["type"]));
if ($type == "validate") {
if (!isset($_GET["name"]) && !isset($_POST["name"])) {
error("No station name provided for verification.");
}
prepare();
print("<stations>");
foreach($api->validate(isset($_GET["name"]) ? $_GET["name"] : $_POST["name"]) as $station) {
print("<station><id>" . $station["id"] . "</id><name>" . $station["value"] . "</name></station>\n");
}
print("</stations>");
} else if ($type == "conn" || $type == "connection" || $type == "connections") {
if ((isset($_GET["from"]) && isset($_GET["to"])) || (isset($_POST["from"]) && isset($_POST["to"]))) {
prepare();
$from = (isset($_GET["from"]) && isset($_GET["to"]) ? $_GET["from"] : $_POST["from"]);
$to = (isset($_GET["from"]) && isset($_GET["to"]) ? $_GET["to"] : $_POST["to"]);
$count = (isset($_GET["count"]) ? $_GET["count"] : (isset($_POST["count"]) ? $_POST["count"] : 3));
$date = (isset($_GET["date"]) ? $_GET["date"] : (isset($_POST["date"]) ? $_POST["date"] : null));
$time = (isset($_GET["time"]) ? $_GET["time"] : (isset($_POST["time"]) ? $_POST["time"] : null));
$mode = (isset($_GET["mode"]) ? $_GET["mode"] : (isset($_POST["mode"]) ? $_POST["mode"] : null));
print("<connections>\n");
/* foreach ($api->get($from, $to, $count, $date, $time, $mode) as $conn) {
print("Connection");
var_dump($conn);
print("Details");
var_dump($api->getDetails($conn["id"]));
print("Coords");
var_dump($api->getCoords());
} */
foreach ($api->get($from, $to, $count, $date, $time, $mode) as $conn) {
print(" <connection id='" . $conn["id"] . "'>\n");
print(" <from><name>" . trim($conn["startStation"]) . "</name><id>" . $api->validate($conn["startStation"])[0]["id"] . "</id></from>\n");
print(" <to><name>" . trim($conn["endStation"]) . "</name><id>" . $api->validate($conn["endStation"])[0]["id"] . "</id></to>\n");
print(" <details>\n");
print(" <departure><date>" . $conn["startDate"] . "</date><time>" . trim($conn["startTime"]) . "</time></departure>\n");
print(" <arrival><date>" . $conn["endDate"] . "</date><time>" . trim($conn["endTime"]) . "</time></arrival>\n");
print(" <duration>" . $conn["duration"] . "</duration>\n");
print(" <changes>" . $conn["changes"] . "</changes>\n");
print(" <current>" . htmlspecialchars($conn["prognose"], ENT_XML1, 'UTF-8') . "</current>\n");
print(" </details>\n");
print(" <services>\n");
$order = 0;
foreach ($api->getDetails($conn["id"]) as $service) {
print(" <service id='" . $order . "'>\n");
print(" <name>" . $service["data"]["productName"] . "</name>\n");
print(" <departure>\n");
print(" <station><name>" . trim($service["start"]["station"]) . "</name><id>" . $api->validate($service["start"]["station"])[0]["id"] . "</id></station>\n");
print(" <date>" . $service["start"]["date"] . "</date><time>" . trim($service["start"]["time"]) . "</time>\n");
print(" <platform>" . $service["start"]["plattform"] . "</platform>\n");
print(" <current>" . htmlspecialchars($service["start"]["prognose"], ENT_XML1, 'UTF-8') . "</current>\n");
print(" </departure>\n");
print(" <arrival>\n");
print(" <station><name>" . trim($service["end"]["station"]) . "</name><id>" . $api->validate($service["end"]["station"])[0]["id"] . "</id></station>\n");
print(" <date>" . $service["end"]["date"] . "</date><time>" . trim($service["end"]["time"]) . "</time>\n");
print(" <platform>" . $service["end"]["plattform"] . "</platform>\n");
print(" <current>" . htmlspecialchars($service["end"]["prognose"], ENT_XML1, 'UTF-8') . "</current>\n");
print(" </arrival>\n");
print(" </service>\n");
$order++;
}
print(" </services>\n");
print(" </connection>\n");
}
print("</connections>\n");
} else {
error("Required 'from' and/or 'to' arguments missing.");
}
} else {
error("Unknown request type provided.");
}