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

Started parser class.

This commit is contained in:
Markus Birth 2017-11-12 16:35:28 +01:00
parent ddb5c35ccd
commit 509cf528d7
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 51 additions and 9 deletions

View File

@ -1,28 +1,33 @@
<?php
require_once 'lib/autoloader.php';
use \TclUpdates\XmlParser;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$input_xml = file_get_contents('php://input', false, NULL, -1, 8192); // read max 8 KiB
$input_xml = file_get_contents('php://input', false, null, -1, 8192); // read max 8 KiB
if (strlen($input_xml) >= 8192) {
// Max length, probably even longer, definitely no XML
// Max length, probably even longer, definitely no data we want
http_response_code(413); // "Payload too large"
exit;
}
$dom = new DOMDocument();
$load_ok = $dom->loadXML($input_xml, LIBXML_NOENT);
if (!$load_ok || $dom->childNodes->length < 1) {
$xp = new XmlParser();
$load_ok = $xp->loadXmlFromString($input_xml);
if (!$load_ok) {
// XML could not be parsed - invalid or no XML
http_response_code(406); // "Not acceptable"
exit;
}
$root_node = $dom->childNodes->item(0);
if ($root_node->nodeName != 'GOTU') {
// Root node isn't <GOTU>, so no update XML
if (!$xp->validateGOTU()) {
// No root node or root node isn't <GOTU>, so no update XML
http_response_code(412); // "Precondition failed"
exit;
}
// ### At this point we can be relatively sure to have the XML we want
echo "Input length is " . strlen($input_xml) . " Bytes." . PHP_EOL;
echo $input_xml . PHP_EOL;
#echo $input_xml . PHP_EOL;
// TODO: Check if it's XML
// If so: Store a copy for re-parsing (or to print out and hang up on a wall)
// Then parse XML into database

31
lib/TclUpdates/XmlParser.php Executable file
View File

@ -0,0 +1,31 @@
<?php
namespace TclUpdates;
class XmlParser
{
private $dom;
public function __construct()
{
$this->dom = new \DOMDocument();
}
public function loadXMLFromString($xml)
{
$xml_ok = $this->dom->loadXML($xml, LIBXML_NOENT);
return $xml_ok;
}
public function validateGOTU()
{
if ($this->dom->childNodes->length < 1) {
return false;
}
$root_node = $this->dom->childNodes->item(0);
if ($root_node->nodeName != 'GOTU') {
return false;
}
return true;
}
}

6
lib/autoloader.php Executable file
View File

@ -0,0 +1,6 @@
<?php
set_include_path(__DIR__ . PATH_SEPARATOR . get_include_path());
// PSR-4
spl_autoload_register();