1
0
mirror of https://github.com/mbirth/tcl_update_db.git synced 2024-12-25 12:04:07 +00:00
tcl_update_db/index.php

71 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2017-11-01 16:50:09 +00:00
<?php
2017-11-12 16:50:29 +00:00
require_once __DIR__ . '/lib/autoloader.php';
2017-11-12 15:35:28 +00:00
2017-12-16 22:08:23 +00:00
use \TclUpdates\GotuObject;
2017-12-16 22:09:58 +00:00
use \TclUpdates\SQLiteWriter;
2017-11-12 15:35:28 +00:00
use \TclUpdates\XmlParser;
2017-11-03 22:08:06 +00:00
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2017-11-12 15:35:28 +00:00
$input_xml = file_get_contents('php://input', false, null, -1, 8192); // read max 8 KiB
2017-11-05 00:36:18 +00:00
if (strlen($input_xml) >= 8192) {
2017-11-12 15:35:28 +00:00
// Max length, probably even longer, definitely no data we want
2017-11-05 00:36:18 +00:00
http_response_code(413); // "Payload too large"
exit;
}
2017-11-12 15:35:28 +00:00
$xp = new XmlParser();
$load_ok = $xp->loadXmlFromString($input_xml);
if (!$load_ok) {
2017-11-05 00:36:18 +00:00
// XML could not be parsed - invalid or no XML
http_response_code(406); // "Not acceptable"
exit;
}
2017-11-12 15:35:28 +00:00
if (!$xp->validateGOTU()) {
// No root node or root node isn't <GOTU>, so no update XML
2017-11-05 00:36:18 +00:00
http_response_code(412); // "Precondition failed"
exit;
}
// ### At this point we can be relatively sure to have the XML we want
2017-11-03 22:08:06 +00:00
echo "Input length is " . strlen($input_xml) . " Bytes." . PHP_EOL;
2017-11-12 15:35:28 +00:00
#echo $input_xml . PHP_EOL;
// Write backup copy (for maybe re-parsing later)
$bkup_dir = __DIR__ . '/data/';
if (!is_dir($bkup_dir)) {
mkdir($bkup_dir);
}
$bkup_filename = $bkup_dir . sprintf('%f-%04x.xml', microtime(true), rand(0, 65535));
file_put_contents($bkup_filename, $input_xml);
2017-12-16 22:06:19 +00:00
// Parse XML into database
$g = GotuObject::fromXmlParser($xp);
if ($g->tv) {
2017-12-16 22:09:58 +00:00
$sqlw = new SQLiteWriter();
2018-02-16 22:56:47 +00:00
$result = $sqlw->addGotu($g);
if ($result !== false) {
$config = parse_ini_file(__DIR__ . '/config.ini');
$type = 'FULL';
if ($g->fv) {
$type = 'OTA (from ' . $g->fv . ')';
}
$data = array(
2017-12-18 14:07:25 +00:00
'content' => 'New ' . $type . ' update for ' . $g->curef . ' found: ' . $g->tv,
);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$ctx = stream_context_create($options);
$notify_ok = file_get_contents($config['NOTIFY_WEBHOOK'], false, $ctx);
}
2017-12-16 22:06:19 +00:00
// I don't care if we can use the data or not. Maybe we can use it later (backup copy).
}
2017-11-12 15:35:28 +00:00
2017-11-03 22:08:06 +00:00
exit;
}
2017-12-16 17:17:57 +00:00
require_once 'index_main.php';