<?php

include('auth.php');
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php");

if (isset($schedule) ) {
                if(!is_numeric($schedule)) { JpGraphError::Raise(" Incorrect parameter. "); }
                $query="select count(id) from nessus_schedule where username='$username' and id=$schedule";
                $result=$dbconn->execute($query);
                list($allowaccess)=$result->fields;
                if ($allowaccess<>1) { JpGraphError::Raise(" Incorrect parameter. "); }
                $img.="&amp;schedule=$schedule";
                $msg2="$schedule";
        }
        if (isset($sid) ) {
                if(!is_numeric($sid)) { JpGraphError::Raise(" Incorrect parameter. "); }
                $query="select count(nessus_settings.id) from nessus_settings inner join nessus_settings_users on nessus_settings.id=nessus_settings_users.sid where nessus_settings_users.username='$username' and nessus_settings.id=$sid";
                $result=$dbconn->execute($query);
                list($allowaccess)=$result->fields;
                if ($allowaccess<>1) { JpGraphError::Raise(" Incorrect parameter. "); }
        }
        if (isset($days) ) {
                if(!is_numeric($days) or $days<0) { JpGraphError::Raise(" Incorrect parameter. "); }
        }
        if (isset($rrisk) ) {
                if(!is_numeric($rrisk)) { JpGraphError::Raise(" Incorrect parameter. "); }
                if($rrisk<1 or $rrisk>7) { JpGraphError::Raise(" Incorrect parameter. "); }
        }
        if (isset($ip) ) {
                $ip=htmlspecialchars($ip);
                $ip=mysql_escape_string($ip);
                include ('validateip.inc.php');
                if (!inrange($ip, $dbconn)) { JpGraphError::Raise(" Incorrect parameter. "); }
        }


$datax=array();
$risk1=array();
$risk2=array();
$risk3=array();
$risk4=array();
$risk5=array();
$risk6=array();
$risk7=array();



$query = "select distinct scantime as vdate from nessus_results where username='$username'";


if (isset($days) ) {

	$query.=" and TO_DAYS(now())-TO_DAYS(scantime)<=$days";

}


if (isset($schedule) ) {

        $query.=" and sched_id=$schedule";

}

if (isset($ip) ) {

        $query.=" and host='$ip'";


}

if (isset($sid) ) {

        $query.=" and sid=$sid";

}

if (isset($rrisk) ) {

        $query.=" and risk=$rrisk";
}

$query.=" order by vdate";



$result=$dbconn->execute($query);

$totalrows=0;
while(!$result->EOF) {
	$totalrows = $totalrows + 1;
	list($date)=$result->fields;
	array_push($datax, $date);
	$prevrisk=0;

//	echo "<br>Date - $date<br>";

$query1="select risk, count(scantime) as vcount  from nessus_results where scantime='$date' and username='$username' and falsepositive<>'Y' and scriptid <> 10180 and msg<>''";


if (isset($schedule) ) {

        $query1.=" and sched_id=$schedule";

}

if (isset($ip) ) {

        $query1.=" and host='$ip'";


}

if (isset($sid) ) {

        $query1.=" and sid=$sid";

}

if (isset($rrisk) ) {

	$query1.=" and risk=$rrisk";
}
$query1.=" group by risk order by risk";


$result1=$dbconn->execute($query1);

while (list($risk, $riskcount)=$result1->fields) {

                for ($i=0;$i<$risk-$prevrisk-1;$i++) {

//			echo "i=$prevrisk+$i+1, ";


			switch ($i+$prevrisk+1) {
			    case 1:
			        array_push($risk1,0);
			        break;
			    case 2:
				array_push($risk2,0);
			        break;
			    case 3:
			        array_push($risk3,0);
			        break;
                            case 4:
                                array_push($risk4,0);
                                break;
                            case 5:
                                array_push($risk5,0);
                                break;
                            case 6:
                                array_push($risk6,0);
                                break;
                            case 7:
                                array_push($risk7,0);
                                break;
				} #end of switch

                        } #end of if
			switch ($risk) {
                            case 1:
                                array_push($risk1,$riskcount);
                                break;
                            case 2:
                                array_push($risk2,$riskcount);
                                break;
                            case 3:
                                array_push($risk3,$riskcount);
                                break;
                            case 4:
                                array_push($risk4,$riskcount);
                                break;
                            case 5:
                                array_push($risk5,$riskcount);
                                break;
                            case 6:
                                array_push($risk6,$riskcount);
                                break;
                            case 7:
                                array_push($risk7,$riskcount);
                                break;
                                } #end of switch


			$prevrisk=$risk;

                $result1->MoveNext();
        }

	for ($i=$prevrisk+1; $i<=7; $i++) {
		switch ($i) {
                            case 1:
                                array_push($risk1,0);
                                break;
                            case 2:
                                array_push($risk2,0);
                                break;
                            case 3:
                                array_push($risk3,0);
                                break;
                            case 4:
                                array_push($risk4,0);
                                break;
                            case 5:
                                array_push($risk5,0);
                                break;
                            case 6:
                                array_push($risk6,0);
                                break;
                            case 7:
                                array_push($risk7,0);
                                break;
                                } #end of switch




	}



	$result->MoveNext();
} #End of while;
// Create the graph. These two calls are always required
$graph = new Graph(640,380,"auto");	
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->img->SetMargin(70,150,20,100);

// Create the linear plots for each category
$dplot[0] = new LinePLot($risk7);
$dplot[0]->SetLegend("Info");
$dplot[1] = new LinePLot($risk6);
$dplot[1]->SetLegend("Low");
$dplot[2] = new LinePLot($risk5);
$dplot[2]->SetLegend("Low/Medium");
$dplot[3] = new LinePLot($risk4);
$dplot[3]->SetLegend("Medium/Low");
$dplot[4] = new LinePLot($risk3);
$dplot[4]->SetLegend("Medium");
$dplot[5] = new LinePLot($risk2);
$dplot[5]->SetLegend("High");
$dplot[6] = new LinePLot($risk1);
$dplot[6]->SetLegend("Serious");

$dplot[0]->SetFillColor("blue");
$dplot[1]->SetFillColor("green");
$dplot[2]->SetFillColor("navy");
$dplot[3]->SetFillColor("orange");
$dplot[4]->SetFillColor("magenta");
$dplot[5]->SetFillColor("yellow");
$dplot[6]->SetFillColor("red");


// Create the accumulated graph
$accplot = new AccLinePlot($dplot);

//$legend=array("Serious","High","Medium","Medium/Low","Low/Medium","Low","Info");


//$accplot->SetLegend($legend); 


// Add the plot to the graph
$graph->Add($accplot);
$graph->xaxis->SetTickLabels($datax);
//$graph->xaxis->SetTextLabelInterval(2);
$graph->xaxis->SetLabelAngle(90);



$graph->xaxis->SetTextTickInterval(1);
$graph->title->Set("$days Day(s) Vulnerability Trend");
$graph->xaxis->title->Set("Dates");
$graph->yaxis->title->Set("Vulnerabilities");

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);


// Display the graph
if ($totalrows > 1) {
	$graph->Stroke();
}
else {
	JpGraphError::Raise("Only 1 value returned - 2 or more values required to produce this graph.");
}
	
?>
