<?
require('diag.php');

class PDF extends PDF_Diag
{
//This Function creates a table formated for the Inprotect Vulnerability Scanner.  It allows the last column to be multilined
//currently boarderType does nothing
	function InprotectTable($header,$data,$w,$headFillColor,$headTextColor, $fillColor, $textColor, $lineColor, $boarderType, $h)
	{

		$numRows = count($header);
		
		//Colors, line width and bold font
		
		$this->SetFillColor($headFillColor[0], $headFillColor[1], $headFillColor[2]);
		$this->SetTextColor($headTextColor);
		$this->SetDrawColor($lineColor[0], $lineColor[1], $lineColor[2]);
		$this->SetLineWidth(.3);
		$this->SetFont('','B');
		//Makes Header
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],$h,$header[$i],1,0,'C',1);
		$this->Ln();
		//Color and font restoration
		$this->SetFillColor($fillColor[0], $fillColor[1], $fillColor[2]);
		$this->SetTextColor($textColor);
		$this->SetFont('');
		//Makes Table
		$fill=0;
		$this->wPt = $this->fwPt;
		$this->hPt=$this->fhPt;
		$this->w=$this->fw;
		$this->h=$this->fh;
		foreach($data as $row)
		{
		//$i = 0;
			for($i = 0; $i < count($w); $i++)
			{
				if($i == count($w) - 1) //last column can have multiple lines
				{
					$this->MultiCellFill($w[$i],$h,$row[$i],'LR', 'L',$fill, $w);
				}
				else
				{
					$this->Cell($w[$i],$h,$row[$i],'LR',0,'L',$fill);
				}
			}
			$fill=!$fill;
			$this->Cell(array_sum($w), .3, '', 'T', 1);
		}
		
		$this->Cell(array_sum($w), $h, '', 'T');
		
		
	}

	//Writes cell as multiline and fills in other columns so table remains balanced
	//Much of the code taken from MultiCell included with FPDF	
	function MultiCellFill($w,$h,$txt,$border=0,$align='J',$fill, $wArray)
	{
		
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$b=0;
		if($border)
		{
			if($border==1)
			{
				$border='LTRB';
				$b='LRT';
				$b2='LR';
			}
			else
			{
				$b2='';
				if(is_int(strpos($border,'L')))
					$b2.='L';
				if(is_int(strpos($border,'R')))
					$b2.='R';
				$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
			}
		}
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$ns=0;
		$nl=1;
		while($i<$nb)
		{
			//Get next character
			$c=$s{$i};
			if($c=="\n")
			{
				//Explicit line break
				if($this->ws>0)
				{
					$this->ws=0;
					$this->_out('0 Tw');
				}
				$this->Cell($w,$h,substr($s,$j,$i-$j),$b,1,$align,$fill);
				$this->fixEmpty($wArray, $h, $boarder, $fill);
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$ns=0;
				$nl++;
				if($border and $nl==2)
					$b=$b2;
				continue;
			}
			if($c==' ')
			{
				$sep=$i;
				$ls=$l;
				$ns++;
			}
			$l+=$cw[$c];
			if($l>$wmax)
			{
				//Automatic line break
				if($sep==-1)
				{
					if($i==$j)
						$i++;
					if($this->ws>0)
					{
						$this->ws=0;
						$this->_out('0 Tw');
					}
					$this->Cell($w,$h,substr($s,$j,$i-$j),$b,1,$align,$fill);
					$this->fixEmpty($wArray, $h, $boarder, $fill);
				}
				else
				{
					if($align=='J')
					{
						$this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
						$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
					}
					$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,1,$align,$fill);
					$this->fixEmpty($wArray, $h, $boarder, $fill);
					$i=$sep+1;
				}
				$sep=-1;
				$j=$i;
				$l=0;
				$ns=0;
				$nl++;
				if($border and $nl==2)
					$b=$b2;
			}
			else
				$i++;
		}
		//Last chunk
		if($this->ws>0)
		{
			$this->ws=0;
			$this->_out('0 Tw');
		}
		if($border and is_int(strpos($border,'B')))
			$b.='B';
		$this->Cell($w,$h,substr($s,$j,$i-$j),$b,1,$align,$fill);
		//$this->fixEmpty($wArray, $h, $boarder, $fill);
		$this->x=$this->lMargin;
	}
	//fills in first lines so table remains balanced
	function fixEmpty($w, $h, $boarder, $fill)
	{
		for($i = 0; $i < count($w) - 1; $i++)
		{
			$this->Cell($w[$i], $h, "  ", 'LR', 0, 'L', $fill);
		}		
	}
}
?>