Skip to content

Commit

Permalink
add unpack script
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Greppi committed Oct 8, 2018
1 parent b1de567 commit 202d86c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions bin/unpack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/php
<?php
// unpacks the invoice and extracts some data
//
// Copyright (c) 2018, Paolo Greppi <[email protected]>
// License: BSD 3-Clause

require_once './vendor/autoload.php';

if (count($argv) <= 1) {
echo "Usage: uppack.php file.xml\n";
exit(-1);
}

$filename = $argv[1];

// defend against XML External Entity Injection
libxml_disable_entity_loader(true);
if (!file_exists($filename)) {
throw new \InvalidArgumentException('File not found');
}
$xml_string = file_get_contents($filename);
$collapsed_xml_string = preg_replace("/\s+/", "", $xml_string);
$collapsed_xml_string = $collapsed_xml_string ? $collapsed_xml_string : $xml_string;
if (preg_match("/\<!DOCTYPE/i", $collapsed_xml_string)) {
throw new \InvalidArgumentException('Invalid XML: Detected use of illegal DOCTYPE');
}

libxml_use_internal_errors(true);
$xml = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOWARNING);
if ($xml === false) {
throw new \InvalidArgumentException("Cannot load XML\n");
}

echo $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->IdFiscaleIVA->IdPaese . '-' . $xml->FatturaElettronicaHeader->CedentePrestatore->DatiAnagrafici->IdFiscaleIVA->IdCodice;
var_dump($xml->FatturaElettronicaBody[0]->DatiGenerali->DatiGeneraliDocumento->Data);
2 changes: 1 addition & 1 deletion www/Xml2Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(string $filename)
libxml_use_internal_errors(true);
$this->xml = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOWARNING);
if ($this->xml === false) {
throw new \InvalidArgumentException("Cannot load xml file.\n");
throw new \InvalidArgumentException("Cannot load XML\n");
}
// convert back and forth to transform SimpleXMLElement Objects to associative arrays
$json = json_encode($this->xml);
Expand Down

0 comments on commit 202d86c

Please sign in to comment.