<?php
//
// $Id: respdf.php,v 1.3 2004/11/18 23:22:43 greg_k Exp $
//

/***********************************************************/
/* last update: 11.17.2004                                 */
/* --------------------------------------------------------*/
/*                    Inprotect                            */
/* --------------------------------------------------------*/
/* Copyright (C) 2002 Inprotect.com                        */
/*                                                         */
/* This program is free software; you can redistribute it  */
/* and/or modify it under the terms of version 2 of the    */
/* GNU General Public License as published by the Free     */
/* Software Foundation.                                    */
/* This program is distributed in the hope that it will be */
/* useful, but WITHOUT ANY WARRANTY; without even the      */
/* implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/* PARTICULAR PURPOSE. See the GNU General Public License  */
/* for more details.                                       */
/*                                                         */
/* You should have received a copy of the GNU General      */
/* Public License along with this program; if not, write   */
/* to the Free Software Foundation, Inc., 59 Temple Place, */
/* Suite 330, Boston, MA 02111-1307 USA                    */
/*                                                         */
/* Contact Information:                                    */
/* Alex Zimin (alex@inprotect.com)                         */
/* http://www.inprotect.com                                */
/***********************************************************/
/* See the README.txt and/or help files for more           */
/* information on how to use & config.                     */
/* See the LICENSE.txt file for more information on the    */
/* License this software is distributed under.             */
/*                                                         */
/* This program is intended for use in an authorized       */
/* manner only, and the author can not be held liable for  */
/* anything done with this program, code, or items         */
/* discovered with this program's use.                     */
/***********************************************************/
#error_reporting(E_ALL);
include ('auth.php');

if (!is_numeric($schedid)) {die("Can not access this page");}
$query="select count(id) from nessus_schedule where username='$username' and id=$schedid";
$result=$dbconn->execute($query);

list($allowaccess)=$result->fields;
if ($allowaccess<>1) {die("Can not access this page");}


$scantime=htmlspecialchars($scantime);
$scantime=mysql_escape_string($scantime);

//Seperates the parts of the date so it doesn't just display it as one big number
$scanyear = substr($scantime, 0, 4);
$scanmonth = substr($scantime, 4, 2);
$scanday = substr($scantime, 6, 2);
$scanhour = substr($scantime, 8, 2);
$scanmin = substr($scantime, 10, 2);
$scansec = substr($scantime, 12);

//here you can set the preference for the table colors
$head_fill_color = array(255,0,0);
$head_text_color = 255;
$fill_color = array(224,235,255);
$text_color = 0;
$line_color = array(128,0,0);
$boarder_type = 1; #doesn't work yet, there's always a full boarder for now

//row height for the table
$row_height = 5;


$query="select count(scantime) from nessus_results where scantime='$scantime' and username='$username' and sched_id=$schedid";
$result=$dbconn->execute($query);

list($allowaccess)=$result->fields;
if ($allowaccess<1) {die("Can not access this page");}

//include pdf libraries
define('FPDF_FONTPATH','inc/font/');
require('inc/InproTable.php');

set_time_limit(300);
//start pdf file, add page, set font
$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica', 'B', 15);
$pdf->Cell(0, 6, 'Vulnerability test report', 0, 1, 'C', 0);

$pdf->SetFont('Helvetica', '', 10);

$pdf->Cell(0,10,"Scan time: $scanmonth/$scanday/$scanyear $scanhour:$scanmin:$scansec", 0, 1, 'C', 0);


if ($scantime<>"" and $scanip<>"" and $scantype=="M") {
    $valX = $pdf->GetX();
    $valY = $pdf->GetY();
    $riskArray = array();
    $colorarray=array(); //array of colors for pie chart
    $result=$dbconn->Execute("SELECT count(risk) as count, risk FROM `nessus_results` where scantime='$scantime' and username='$username' and scantype='$scantype' group by risk");
    while (list($riskcount, $risk)=$result->fields) {
        $pdf->Cell(0, 6, "".getrisk($risk)." : $riskcount");
        $riskArray [getrisk($risk)] = $riskcount;
        $pdf->Ln();
        $colorarray[$index] = getriskcolor($risk);
        $index++;
        $result->MoveNext();
    }

    $pdf->Ln();
    $pdf->SetXY(80, $valY);
    $pdf->PieChart(140, 40, $riskArray, '%l', $colorarray);
    $pdf->SetXY($valX, $valY + 40);

    $pdf->SetFont('Helvetica', '', 10);

    $result=$dbconn->Execute("select distinct service, risk, msg from nessus_results where scantime='$scantime' and scantype='$scantype' and username='$username' and host='$scanip'");

    while ($row = $result->FetchRow()){
        $row[1]=getrisk($row[1]);
        $all_results[] = $row;
    }
    $cols = array('Service','Risk','Description');

    $w=array(50,35,65);

    $pdf->InprotectTable($cols, $all_results, $width_array, $head_fill_color, $head_text_color, $fill_color, $text_color, $line_color, $boarder_type, $row_height);

    $pdf->Ln();
    $pdf->Cell(0,6, 'Inprotect.com - web interface for Nessus and Nmap scanners',0, 1, 'C', 0);

}

else if ($scantime<>"" and $scantype=="A" and $schedid<>"") {

    //get current possition so we can put the pie chart to the side
    $valX = $pdf->GetX();
    $valY = $pdf->GetY();
    $riskarray = array(); //array of risks for pie chart
    $colorarray=array(); //array of colors for pie chart

    $result=$dbconn->Execute("SELECT count(risk) as count, risk FROM `nessus_results` where scantime='$scantime' and username='$username' and scantype='$scantype' and sched_id=$schedid group by risk");
    $index = 0;
    while (list($riskcount, $risk)=$result->fields) {

        $pdf->Cell(0, 6, "".getrisk($risk)." : $riskcount");
        $riskArray [getrisk($risk)] = $riskcount;
        $colorarray[$index] = getriskcolor($risk);
        $index++;
        $pdf->Ln();
        $result->MoveNext();
    }

    //make pie chart and put current possition below
    $pdf->Ln();
    $pdf->SetXY(80, $valY);
    $pdf->PieChart(140, 40, $riskArray, '%l', $colorarray);
    $pdf->SetXY($valX, $valY + 40);

    //build array of risks
    $pdf->SetFont('Helvetica', '', 10);
    $result=$dbconn->Execute("select distinct host, service, risk, msg from nessus_results where scantime='$scantime' and scantype='$scantype' and username='$username' and sched_id=$schedid order by host ASC, result_id ASC");


    while ($row = $result->FetchRow()){
        $row[2]=getrisk($row[2]);
        $row[3]=trim($row[3]);
        $all_results[] = $row;
    }
    $cols = array('Host','Service','Risk','Description');
    //widths for columns
    $width_array=array(40,50,35,65);
    //build table
    $pdf->InprotectTable($cols, $all_results, $width_array, $head_fill_color, $head_text_color, $fill_color, $text_color, $line_color, $boarder_type, $row_height);
    $pdf->Ln();
    $pdf->Cell(0,6, 'Inprotect.com - web interface for Nessus and Nmap scanners',0, 1, 'C', 0);


}

//output the pdf, now we're done
$pdf->Output();

//matches risks numbers with names
function getrisk($risk) {

    switch ($risk) {
    case 1:
            $risk="Serious";
        break;
    case 2:
        $risk="High";
        break;
    case 3:
        $risk="Medium";
        break;
    case 4:
        $risk="Medium/Low";
        break;
    case 5:
        $risk="Low/Medium";
        break;
    case 6:
        $risk="Low";
        break;
    case 7:
        $risk="Info";
        break;
    }

    return $risk;
}

//matches risks number with colors
function getriskcolor($risk)
{
    switch ($risk)
    {
    case 1:
        $risk=array(255,0,0);
        break;
    case 2:
        $risk=array(255,255,0);
        break;
    case 3:
        $risk=array(255,0,255);
        break;
    case 4:
        $risk=array(255,165,0);
        break;
    case 5:
        $risk=array(0,0,128);
        break;
    case 6:
        $risk=array(0,139,69);
        break;
    case 7:
        $risk=array(100,149,237);
        break;
    }
    return $risk;
}


?>
