#!/usr/bin/php
<?php
//error_reporting(0);

/***
 * Main program - Don't edit below
 */
echo " _          ______   _\n| |        (_____ \ (_)\n| | _  ____  ____) ) _  ___  ___  ____\n| || \|  _ \/_____/ | |/___)/ _ \|  _ \ \n| |_) ) | | |______ | |___ | |_| | | | |\n|____/| ||_(_______)| (___/ \___/|_| |_|\n      |_|         (__/             \n\n";

foreach (glob("classes/*.php") as $filename)
    include $filename;

$definitions = new \Clapp\CommandLineArgumentDefinition(
    array(
        "help|h"            => "Shows help message",
        "doc|d=s"           => "/path/to/burp.xml to use",
        "no-save|n"           => "Output only - Don't save JSON files",
    )
);

$filter = new \Clapp\CommandArgumentFilter($definitions, $argv);

if ($filter->getParam('h') === true || $argc < 2) {
	echo "Convert burp \"report issues\" XML output file to JSON files for repgen.php\n\n";
    fwrite(STDERR, $definitions->getUsage());
    exit(0);
} 

// see if doc exists 
if ($filter->getParam("doc") == false)
	die("[-] no doc set\n");

echo "[!] doc: ".$filter->getParam("doc")."\n";
if(!file_exists($filter->getParam("doc")))
	die("[-] no such file! \n"); 


//$xmlfile = file_get_contents($filter->getParam("doc"));
$burp= simplexml_load_file($filter->getParam("doc"));

$resultsFolder = substr($filter->getParam("doc"), 0, strrpos( $filter->getParam("doc"), '/') )."/";
$vulnarray = array();

foreach($burp as $issue => $details){
    $issue = (string)$details->name;
    if(!in_array($issue, $vulnarray)){
        array_push($vulnarray, $issue);
    }
}

echo "[=] ".count($vulnarray)." vulnerabilities identified\n";

foreach($vulnarray as $issue){
    $newfilename = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $issue) );
    if($filter->getParam('no-save') === true){
        echo "[+] issue: ".$issue."\n";
    }else{
        echo "[+] creating file: ".$resultsFolder.$newfilename.".json\n";
        touch($resultsFolder.$newfilename.".json");
    }
}

if($filter->getParam('no-save') !== true)
    echo "[!] These are empty JSON files, remember to floss!\n";


?>