Location: PHPKode > projects > WaveWatcher > wavewatcher3/html/graph/signoi.php
<?
require_once("../../classes/dbconnection.php");

// Input Variables
$nSizeX = empty($xsize) ? 450 : $xsize;					// Total width of the picture
$nSizeY = empty($ysize) ? 225 : $ysize;					// Total height of the picture
$font = empty($font) ? 2 : $font;						// Font to all the text in the diagram
$dateStartTime = empty($starttime) ? null : $starttime;	// The start of the time interval to draw
$dateStopTime = empty($stoptime) ? null : $stoptime;	// The stop of the time interval to draw
$nDrawMode = isset($drawmode) ? $drawmode : 4;			// How the data should be drawn
$nHorGridMode = empty($xgrid) ? 3 : $xgrid;				// Decides how many horizontal gridlines to use
$nVerGridMode = empty($ygrid) ? 3 : $ygrid;				// Decides how many vertical gridlines to use
$nFitWidth =  empty($fitwidth) ? 1 : $fitwidth;			// Decides what the highest value for the x-scale shall be for a histogram
$nDownTimeLevel = isset($downtime) ? $downtime : 5;		// Help to decide the time that must go before a client is estimated as down
$nHorPos = empty($xpos) ? 24 : $xpos;					// Number of steps on the x-scale 
$nVerPos = empty($ypos) ? 5 : $ypos;					// Number of steps on the y-scale. 5 = (-50 - -100)
$nMode = empty($mode) ? 1 : $mode;						// Text that explain what time (or %) it is between two delimitationlines on the x-scale


// Internal variables
$strError = "";		// Error message
$bError = FALSE;	// Tells if an error has occured or not
$bHistogram = ($nDrawMode == 1 || $nDrawMode == 2 || $nDrawMode == 3) ? TRUE : FALSE; // Tells if the diagram is a histogram or not

// Check if valid values. if not, set to default or set an error
$nMode = $nMode != 1 &&  $nMode != 2 && $nMode != 3 ? 1 : $nMode;
$nSizeX = $nSizeX < 1 ? 450 : $nSizeX;							
$nSizeY = $nSizeY < 1 ? 225 : $nSizeY;							
$font = $font < 1 || $font > 5 ? 2 : $font;	
$nDrawMode = $nDrawMode < 1 || $nDrawMode > 12 ? 4 : $nDrawMode;					
$nHorGridMode = $nHorGridMode < 1 || $nHorGridMode > 3 ? 3 : $nHorGridMode;	
$nVerGridMode = $nVerGridMode < 1 || $nVerGridMode > 3 ? 3 : $nVerGridMode;	
$nFitWidth = $nFitWidth < 1 || $nFitWidth > 3 ? 3 : $nFitWidth;	
$nDownTimeLevel = $nDownTimeLevel < 0 ? 10 : $nDownTimeLevel;			
$nHorPos = $nHorPos < 1 ? 24 : $nHorPos;							
$nVerPos = $nVerPos < 1 ? 5 : $nVerPos;			

$bGetConnectionID = TRUE;

// connection_id has higher priority than node_id, if_name and mac.
// So if connection_id is valid, use it to get node_id, if_name and mac from the database. 
if (isset($connection_id))
{
	if ($connection_id > 0)
	{
		// Database access
		$dbcn = new DBConnection();
		$dbcn->Connect();
		$query = "SELECT node_id, if_name, mac FROM connections WHERE connection_id = '$connection_id'";
		$result = $dbcn->Query($query); 
		$dbcn->Disconnect();

		if ($result) 
		{
			List($node_id, $if_name, $mac) = $dbcn->FetchRow($result);
			$bGetConnectionID = FALSE;
		}
		else
		{
			$strError .= "Not a valid query to the database. Check connection_id. ";
			$bError = TRUE;
		}
	}
	else
	{
		$strError .= "Not a valid connection_id. ";
		$bError = TRUE;
	}
}

// Check if an MAC address was sent and if it´s valid
if (!$bError && (empty($mac) || !ereg("^([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}$", $mac)))
{
#	$strError .= "Not a valid mac. ";
#	$bError = TRUE;
}	

// Check if node_id was sent and if it´s valid
if (!$bError && (empty($node_id) || $node_id < 0))
{
	$strError .= "Not a valid node_id. ";
	$bError = TRUE;
}	

// Check if interface name was sent
//if (!$bError && empty($if_name))
//{
//	$strError .= "Not a valid if_name. ";
//	$bError = TRUE;
//}

if (!$bError && $bGetConnectionID)
{
	// Database access
	$dbcn = new DBConnection();
	$dbcn->Connect();
	$query = "SELECT connection_id FROM connections WHERE node_id = '$node_id' AND if_name = '$if_name' AND mac = '$mac'";
	$result = $dbcn->Query($query); 
	$dbcn->Disconnect();

	if ($result) 
		List($connection_id) = $dbcn->FetchRow($result);
	else
	{
		$strError .= "Could not get connection_id from the database. Check node_id, if_name and mac. ";
		$bError = TRUE;
	}
}

//if (!empty($dateStopTime) && !empty($dateStartTime))
//{
	// If $dateStartTime and $dateStopTime have been sent as parameters to the program
	// we calculate a valid $nStartTime && $nStopTime and finally we find a suitable $nHorPos also!

//	$nStopTime = mktime(substr($dateStopTime,6,2), 0, 0, substr($dateStopTime,2,2), substr($dateStopTime,4,2), substr($dateStopTime,0,2));
//	$nStartTime = mktime(substr($dateStartTime,6,2), 0, 0, substr($dateStartTime,2,2), substr($dateStartTime,4,2), substr($dateStartTime,0,2));

//	$nHorPos = ($nStopTime - $nStartTime) / 3600;								// Presentation type: Hours
//	$nHorPos = $nMode == 2 ? ($nStopTime - $nStartTime) / 3600 / 24 : $nHorPos;	// Presentation type: Days
//	$nHorPos = $nMode == 3 ? ($nStopTime - $nStartTime) / 3600 / 24 : $nHorPos;	// Presentation type: Weeks

//	if ($nStartTime >= $nStopTime)
//	{
//		$bError = TRUE;
//		$strError .= "Not a valid time interval. ";
//	}
//}
//else
//{
	// If not $dateStartTime and $dateStopTime have been sent as parameters to the program
	// we set $nStopTime to the time just now and calculate a valid $nStartTime with help of 
	// $nHorPos that was sent as a parameter.

	$ntime = time();
	$nStopTime = $ntime + $scroll;
	//	$nStopTime = 1015580663; // last value in the test database

	switch($nMode)
	{
		case 1:		$nStartTime = $nStopTime - $nHorPos * 3600; // 3600 = 60 * 60
					break;

		case 2:		$nStartTime = $nStopTime - $nHorPos * 86400; // 86400 = 60 * 60 * 24
					break;

		case 3:		$nStartTime = $nStopTime - $nHorPos * 86400;
					break;

		default:	$nStartTime = $nStopTime - $nHorPos * 3600;
					break;
	}
//}

$nFontHeight = ImageFontHeight($font);		// Selected fonts height
$nFontWidth = ImageFontWidth($font);		// Selected fonts width	
$nLeft = 4 * 2 + $nFontWidth * 4;			// the space to the left of drawn data
$nRight = 15;								// the space to the right of drawn data
$nTop = 2 + $nFontHeight + 6;				// the space over the drawn data
$nBottom = 4 + ($nFontHeight * 2) + 5;		// the space bellow the drawn data
$nHeight = $nSizeY - $nTop - $nBottom;		// Height to use when drawing the data
$nWidth = $nSizeX - $nLeft - $nRight;		// Width to use when drawing the data
$dStepY = $nHeight / $nVerPos;				// Size of each step on the y-scale
$dStepX = $nWidth / $nHorPos;				// Size of each step on the x-scale

// Define header type
Header("Content-type: image/png");

// Create image
$im = ImageCreate($nSizeX, $nSizeY);

// Define colors
$clrWhite = ImageColorAllocate($im, 255, 255, 255);	// background color
$clrBlack = ImageColorAllocate($im, 0, 0, 0);
$clrBlue = ImageColorAllocate($im, 0, 0, 224);
$clrRed = ImageColorAllocate($im, 224, 0, 0);
$clrLBlue = ImageColorAllocate($im, 150, 150, 224);
$clrLRed = ImageColorAllocate($im, 224, 150, 150);
$clrTitle = ImageColorAllocate($im, 0, 0, 0); 
$clrGrid = ImageColorAllocate($im, 192, 192, 192);

//Sets style to gridlines and separatorlines
$arrGridStyle = array($clrGrid, $clrGrid, $clrGrid, $clrWhite, $clrWhite, $clrWhite);
ImageSetStyle($im, $arrGridStyle);
$arrSeparator = array($clrRed, $clrRed, $clrWhite, $clrWhite);

// Draw outer border
ImageRectangle($im, 0, 0, $nSizeX - 1, $nSizeY - 1, $clrBlack);

// Write title 
ImageString($im, $font, 4, 2, "$if_name $mac", $clrTitle);

if ($nDrawMode == 1)
	ImageString($im, $font, $nSizeX - ($nFontWidth * 6) - 4, 2, "signal", $clrBlue);
else if ($nDrawMode == 2)
	ImageString($im, $font, $nSizeX - ($nFontWidth * 5) - 4, 2, "noise", $clrRed);
else
{
	ImageString($im, $font, $nSizeX - ($nFontWidth * 27) - 4, 2, "locsig", $clrBlue);
	ImageString($im, $font, $nSizeX - ($nFontWidth * 21) - 4, 2, "/", $clrTitle);
	ImageString($im, $font, $nSizeX - ($nFontWidth * 20) - 4, 2, "locnoi", $clrRed);
	ImageString($im, $font, $nSizeX - ($nFontWidth * 14) - 4, 2, "/", $clrTitle);
	ImageString($im, $font, $nSizeX - ($nFontWidth * 13) - 4, 2, "remsig", $clrLBlue);
        ImageString($im, $font, $nSizeX - ($nFontWidth * 7) - 4, 2, "/", $clrTitle);
        ImageString($im, $font, $nSizeX - ($nFontWidth * 6) - 4, 2, "remnoi", $clrLRed);
}

if ((!$bError) &&(($locsig_active==1)||($remnoi_active==1)))
{
	$nCount = 0;

	// Database access
	$dbcn = new DBConnection();
	$dbcn->Connect();
	$query = "SELECT utime, locsig, remnoi FROM signal_levels_log WHERE connection_id = '$connection_id' AND utime >= '$nStartTime' AND utime <= '$nStopTime' ORDER BY utime";
	$result = $dbcn->Query($query); 
	$dbcn->Disconnect();

	// evaluate result
	if ($result)
	{
		// Zero arrays
		for ($i=50; $i < $nStop; $i++)
		{
			$arrSigValues[$i] = 0;
			$arrNoiValues[$i] = 0;
		}

		while (List($time, $sig, $noi) = $dbcn->FetchRow($result))
		{	
			if ($nVerPos == 5)
			{
				$sig = $sig < -99 ? -99 : $sig;
				$noi = $noi < -99 ? -99 : $noi;
			}
			if ($locsig_active==0)$sig =0;
			if ($remnoi_active==0)$noi =0;
			$arrSig[$nCount] = $sig;
			$arrNoi[$nCount] = $noi;
			$arrTime[$nCount] = $time;
			$arrSigValues[-$sig]++;
			$arrNoiValues[-$noi]++;

			$nCount++;
		}

		if ($nCount > 0)
		{
			$nDataInterval = $nStopTime - $nStartTime;
			$nStop = $nCount >= 30 ? 30 : $nCount;

			// Check the minimum time between data
			for ($i = 1; $i < $nStop; $i++)
			{
				if (($arrTime[$nCount - $i] - $arrTime[$nCount - $i - 1]) < $nDataInterval)
					$nDataInterval = $arrTime[$nCount - $i] - $arrTime[$nCount - $i - 1];
			}

			// Calculate the time that must go before a client is estimated as down
			$nTimeBeforeDown = $nDataInterval * $nDownTimeLevel;

			// Draw horizontal gridlines and vertical numbers
			$nDBM = 50;
			$dPosY = $nTop;
			for($pos = 0; $pos <= $nVerPos; $pos++)
			{
				if ($nHorGridMode == 2 || $nHorGridMode == 3)
					ImageLine($im, $nLeft, $dPosY, $nSizeX - $nRight, $dPosY, IMG_COLOR_STYLED);
				
				if ($nHorGridMode == 3 && ($dPosY - $dStepY / 2) > $nTop) 
					ImageLine($im, $nLeft, $dPosY - $dStepY / 2, $nSizeX - $nRight, $dPosY - $dStepY / 2, IMG_COLOR_STYLED);

				ImageString($im, $font, 4, $dPosY - ($nFontHeight / 2), "-$nDBM", $clrBlack);	
				ImageLine($im, $nLeft-3, $dPosY, $nLeft+3, $dPosY, $clrBlack);
				
				$nDBM += 10;
				$dPosY += $dStepY;
			}
			if ($bHistogram)
			{
				$nHorPos = 20;
				$strMode = "%";
				$nStart = 50;
				$nStop = ($nVerPos * 10) + $nStart;

				if ($nFitWidth != 1) // make the step on the x-scale fit the largest value (%) in the arrays 
				{
					$nLargestValue = 0;

					for ($i = $nStart; $i < $nStop; $i++)
					{
						if (($nDrawMode == 2 || $nDrawMode == 3) && $arrNoiValues[$i] > $nLargestValue)
							$nLargestValue = $arrNoiValues[$i];

						if (($nDrawMode == 1 || $nDrawMode == 3) && $arrSigValues[$i] > $nLargestValue)
							$nLargestValue = $arrSigValues[$i];
					}

					// Recalculate $nHorPos so that it fit to the largest value
					$nHorPos = ceil($nLargestValue / $nCount * 20);
					
					if ($nFitWidth == 2 && $nHorPos % 2 > 0)
						$nHorPos++;
				}

				// Draw vertical gridlines and horizontal numbers
				$nPosProc = $nLeft;
				$dStepX = ($nWidth / $nHorPos);

				for ($pos = 0; $pos <= $nHorPos; $pos++)
				{
					$nProc = $pos * 5;

					if ($nPosProc >= $nLeft)
					{
						// Draw gridline if the gridmode is correct
						if ($nVerGridMode != 1 && ($nVerGridMode == 3 || $pos % 2 == 0)) 
							ImageLine($im, $nPosProc, $nTop, $nPosProc, $nTop + $nHeight, IMG_COLOR_STYLED);	

						if ($pos % 2 == 0)
							ImageString($im, $font, $nPosProc - $nFontWidth, $nTop + $nHeight + 4, "$nProc", $clrBlack);

						ImageLine($im, $nPosProc, $nTop + $nHeight - 3, $nPosProc, $nTop + $nHeight + 3, $clrBlack);
					}
					
					$nPosProc += $dStepX;	
				}

				$dPixelPerProc = $nWidth / ($nHorPos * 5);
				$dPixelPerDBM = $nHeight / ($nVerPos * 10);

				// Draw the stacks for each dBm 
				for ($i = $nStart; $i < $nStop; $i++)
				{
					$nSigX = $nLeft + ($arrSigValues[$i] / $nCount * 100 * $dPixelPerProc);
					$nNoiX = $nLeft + ($arrNoiValues[$i] / $nCount * 100 * $dPixelPerProc);
					$nY = $nTop + ($i - $nStart) * $dPixelPerDBM;

					if ($nNoiX > $nSigX) // Draw signal on top of noise
					{
						if ($nDrawMode == 2 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nNoiX, $nY + $dPixelPerDBM, $clrLRed);

						if ($nDrawMode == 1 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nSigX, $nY + $dPixelPerDBM, $clrBlue);
					}
					else if ($nNoiX == $nSigX && $nSigX != $nLeft) // Make the signal one pixel larger and draw the noise on top 
					{
						if ($nDrawMode == 2 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nNoiX, $nY + $dPixelPerDBM, $clrLRed);

						if ($nDrawMode == 1 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nSigX + 1, $nY + $dPixelPerDBM, $clrBlue);
					}
					else // draw noise on top of signal
					{
						if ($nDrawMode == 1 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nSigX, $nY + $dPixelPerDBM, $clrBlue);

						if ($nDrawMode == 2 || $nDrawMode == 3)
							ImageFilledRectangle($im, $nLeft, $nY, $nNoiX, $nY + $dPixelPerDBM, $clrLRed);
					}
				}
			}
			else // graph
			{
				$dPixelPerDBM = $nHeight / ($nVerPos * 10);
				$dPixelPerSec = $nWidth / ($nStopTime - $nStartTime);

				$nMinutes = strftime("%M", $nStopTime);
				$nHours = strftime("%H", $nStopTime);

				switch ($nMode)
				{
					// $strMode			Text that explain what time it is between two delimitationlines on the x-scale
					// $nExtraPos		Is to loop some extra times in the next for-loop when $nMode = 3. Otherwise the text to the left on the x-scale is missing

					// hour
					case 1:		$dStepX = $dPixelPerSec * 3600; // (3600 = 60 * 60)
								$nMinutesOffset = $dStepX / 60 * $nMinutes;
								$nPosTime = $nLeft + $nHorPos * $dStepX - $nMinutesOffset;
								$strMode = "hour";
								$nExtraPos = 0;
								break;

					// day
					case 2:		$dStepX = $dPixelPerSec * 86400; // (86400 = 60 * 60 * 24)
								$nHoursOffset = $dStepX / 24 * $nHours;
								$nMinutesOffset = $dStepX / 1440 * $nMinutes; // (1440 = 24 * 60)
								$nPosTime = $nLeft + ($nHorPos * $dStepX) - $nHoursOffset - $nMinutesOffset;
								$strMode = "day";
								$nExtraPos = 0;
								break;

					// week
					case 3:		$dStepX = $dPixelPerSec * 86400; // (86400 = 60 * 60 * 24)
								$nHoursOffset = $dStepX / 24 * $nHours;
								$nMinutesOffset = $dStepX / 1440 * $nMinutes; // (1440 = 24 * 60)
								$nPosTime = $nLeft + ($nHorPos * $dStepX) - $nHoursOffset - $nMinutesOffset;
								$strMode = "week";
								$nExtraPos = 7; // Make sure all the text for the x-scale is printed
								break;

					default:	break;
				}

				$bSeperator = FALSE;
				$bDelimitationLine = TRUE;

				// Draw horizontal numbers or text
				for ($pos = 0; $pos <= $nHorPos + $nExtraPos; $pos++)
				{
					switch($nMode)
					{
						// $strTextX		Text or number for x-scale
						// $nTextOffset		Dislocate $strTextX to the right place in relation to $nMode and the delimitationlines

						// Hour
						case 1:		$strTextX = strftime("%H", $nStopTime - (3600 * $pos)); // 3600 = 60 * 60
									$nTextOffset = $nFontWidth;

									// Only print every other hour
									if ((($strTextX % 2) == 0) && (($nPosTime + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime - $nTextOffset) >= $nLeft))
									{
										ImageString($im, $font, $nPosTime - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
									}

									// Should we draw separatorline?
									$bSeperator = $strTextX == "0" ? TRUE : FALSE;
									break;

						// Day
						case 2:		// Check if both name and number of the day fits between delimitationlines
									if ($dStepX < (7 * $nFontWidth)) // Only number
									{
										$strTextX = strftime("%d", $nStopTime - (86400 * $pos)); // 86400 = 24 * 60 * 60
										$nTextOffset = $nFontWidth;
									}
									else // Both name and number
									{
										$strTextX = strftime("%a %d", $nStopTime - (86400 * $pos)); 
										$nTextOffset =  3 * $nFontWidth;
									}
									
									$nTextCenter = $dStepX / 2;

									if ((($nPosTime + $nTextCenter + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime + $nTextCenter - $nTextOffset) >= $nLeft))
									{
										ImageString($im, $font, $nPosTime + $nTextCenter - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
									}

									// Should we draw separatorline?
									$bSeperator = strftime("%w", $nStopTime - 3600 * 24 * $pos) == "1" ? true : false;
									break;

						// Week
						case 3:		$strTextX = strftime("%V", $nStopTime - (86400 * $pos)); // 86400 = 24 * 60 * 60
									$nTextCenter = $dStepX * 7 / 2;
									$nTextOffset = $nFontWidth;

									if ($strTextX != strftime("%V", $nStopTime - 86400 * ($pos + 1)))
									{
										if (($dStepX * 7) >= (5 * $nFontWidth))
										{
											$strTextX = "w $strTextX";
											$nTextOffset = 2 * $nFontWidth;
										}

										if ((($nPosTime + $nTextCenter + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime + $nTextCenter - $nTextOffset) >= $nLeft))
										{
											ImageString($im, $font, $nPosTime + $nTextCenter - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
										}
										
										$bDelimitationLine = true;
									}
									else
									{
										$bDelimitationLine = false;
									}

									// Should we draw separatorline?
									$bSeperator = strftime("%d", $nStopTime - 3600 * 24 * $pos) == "01" ? true : false;
									break;

						default:	break;
					}
				
					if ($nPosTime >= $nLeft)
					{					
						// Draw delimitationline?
						if ($bDelimitationLine)
						{
							// Draw gridline if the gridmode is correct
							if ($nVerGridMode != 1 && ($nVerGridMode == 3 || $pos % 2 == 0)) 
								ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop + $nHeight, IMG_COLOR_STYLED);	

							ImageLine($im, $nPosTime, $nTop + $nHeight - 3, $nPosTime, $nTop + $nHeight + 3, $clrBlack);	
						}

						// Draw separatorline?
						if ($bSeperator)
						{
							ImageSetStyle($im, $arrSeparator);
							ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop + $nHeight, IMG_COLOR_STYLED);
							ImageSetStyle($im, $arrGridStyle);
						}
					}
					
					$nPosTime -= $dStepX;	
				}
				// Draw data
				for ($i = 1; $i < $nCount; $i++)
				{	
					$nTime = $arrTime[$i];
					$nX = ($nTime - $nStartTime) * $dPixelPerSec + $nLeft;
					$nSigY = (-$arrSig[$i] - 50) * $dPixelPerDBM + $nTop;
					$nNoiY = (-$arrNoi[$i] - 50) * $dPixelPerDBM + $nTop;
					
					$nTimeOld = $arrTime[$i - 1];
					$nXOld = ($nTimeOld - $nStartTime) * $dPixelPerSec + $nLeft;
					$nSigYOld = (-$arrSig[$i - 1] - 50) * $dPixelPerDBM + $nTop;
					$nNoiYOld = (-$arrNoi[$i - 1] - 50) * $dPixelPerDBM + $nTop;

					// Check if the unit has been up or down
					$bHasBeenUp = ($nTime - $nTimeOld) < $nTimeBeforeDown ? TRUE : FALSE;

					if ($nDrawMode == 5) // Small line and small + 
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrLRed);
						}

						ImageLine($im, $nX - 1, $nSigY, $nX + 1, $nSigY, $clrBlue);
						ImageLine($im, $nX, $nSigY - 1, $nX, $nSigY + 1, $clrBlue);

						ImageLine($im, $nX - 1, $nNoiY, $nX + 1, $nNoiY, $clrLRed);
						ImageLine($im, $nX, $nNoiY - 1, $nX, $nNoiY + 1, $clrLRed);
					}
					else if ($nDrawMode == 6) // Small line and small X 
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrLRed);
						}
							
						ImageLine($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrBlue);
						ImageLine($im, $nX + 1, $nSigY - 1, $nX - 1, $nSigY + 1, $clrBlue);

						ImageLine($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrLRed);
						ImageLine($im, $nX + 1, $nNoiY - 1, $nX - 1, $nNoiY + 1, $clrLRed);
					}
					else if ($nDrawMode == 7) // Small dot
					{
						ImageSetPixel($im, $nX, $nSigY, $clrBlue);
						ImageSetPixel($im, $nX, $nNoiY, $clrLRed);
					}
					else if ($nDrawMode == 8) // Medium dot
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX, $nSigY, $clrBlue);
						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX, $nNoiY, $clrLRed);
					}
					else if ($nDrawMode == 9) // Large dot
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrBlue);
						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrLRed);
					}
					else if ($nDrawMode == 10) // Small + 
					{
						ImageLine($im, $nX - 1, $nSigY, $nX + 1, $nSigY, $clrBlue);
						ImageLine($im, $nX, $nSigY - 1, $nX, $nSigY + 1, $clrBlue);

						ImageLine($im, $nX - 1, $nNoiY, $nX + 1, $nNoiY, $clrLRed);
						ImageLine($im, $nX, $nNoiY - 1, $nX, $nNoiY + 1, $clrLRed);
					}
					else if ($nDrawMode == 11) // Medium + 
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY, $clrBlue);
						ImageLine($im, $nX, $nSigY - 2, $nX, $nSigY + 1, $clrBlue);

						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY, $clrLRed);
						ImageLine($im, $nX, $nNoiY - 2, $nX, $nNoiY + 1, $clrLRed);
					}
					else if ($nDrawMode == 12) // Small X 
					{
						ImageLine($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrBlue);
						ImageLine($im, $nX + 1, $nSigY - 1, $nX - 1, $nSigY + 1, $clrBlue);

						ImageLine($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrLRed);
						ImageLine($im, $nX + 1, $nNoiY - 1, $nX - 1, $nNoiY + 1, $clrLRed);
					}
					else // Default = small line
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrLRed);
						}
						else
						{
							ImageSetPixel($im, $nX, $nSigY, $clrBlue);
							ImageSetPixel($im, $nX, $nNoiY, $clrLRed);
						}
					}
				}
			}
		}
		else
		{
			$bError = TRUE;
			$strError .= "There are no data in the database for the reqested time. ";
		}
	}
	else
	{
		$bError = TRUE;
		$strError .= "Not a valid query to the database. Check connection_id, mac, if_name and node_id. ";
	}
}

if ((!$bError) &&(($remsig_active==1)||($locnoi_active==1)))
{
	$nCount = 0;

	// Database access
	$dbcn = new DBConnection();
	$dbcn->Connect();
	$query = "SELECT utime, remsig, locnoi FROM signal_levels_log WHERE connection_id = '$connection_id' AND utime >= '$nStartTime' AND utime <= '$nStopTime' ORDER BY utime";
	$result = $dbcn->Query($query); 
	$dbcn->Disconnect();

	// evaluate result
	if ($result)
	{
		// Zero arrays
		for ($i=50; $i < $nStop; $i++)
		{
			$arrSigValues[$i] = 0;
			$arrNoiValues[$i] = 0;
		}

		while (List($time, $sig, $noi) = $dbcn->FetchRow($result))
		{	
			if ($nVerPos == 5)
			{
				$sig = $sig < -99 ? -99 : $sig;
				$noi = $noi < -99 ? -99 : $noi;
			}
			if ($remsig_active==0)$sig =0;
			if ($locnoi_active==0)$noi =0;
			$arrSig[$nCount] = $sig;
			$arrNoi[$nCount] = $noi;
			$arrTime[$nCount] = $time;
			$arrSigValues[-$sig]++;
			$arrNoiValues[-$noi]++;

			$nCount++;
		}

		if ($nCount > 0)
		{
			$nDataInterval = $nStopTime - $nStartTime;
			$nStop = $nCount >= 30 ? 30 : $nCount;

			// Check the minimum time between data
			for ($i = 1; $i < $nStop; $i++)
			{
				if (($arrTime[$nCount - $i] - $arrTime[$nCount - $i - 1]) < $nDataInterval)
					$nDataInterval = $arrTime[$nCount - $i] - $arrTime[$nCount - $i - 1];
			}

			// Calculate the time that must go before a client is estimated as down
			$nTimeBeforeDown = $nDataInterval * $nDownTimeLevel;

			// Draw horizontal gridlines and vertical numbers
			
			$nDBM = 50;
			$dPosY = $nTop;
			for($pos = 0; $pos <= $nVerPos; $pos++)
			{
				$nDBM += 10;
				$dPosY += $dStepY;
			}

				$dPixelPerDBM = $nHeight / ($nVerPos * 10);
				$dPixelPerSec = $nWidth / ($nStopTime - $nStartTime);

				$nMinutes = strftime("%M", $nStopTime);
				$nHours = strftime("%H", $nStopTime);

				switch ($nMode)
				{
					// $strMode			Text that explain what time it is between two delimitationlines on the x-scale
					// $nExtraPos		Is to loop some extra times in the next for-loop when $nMode = 3. Otherwise the text to the left on the x-scale is missing

					// hour
					case 1:		$dStepX = $dPixelPerSec * 3600; // (3600 = 60 * 60)
								$nMinutesOffset = $dStepX / 60 * $nMinutes;
								$nPosTime = $nLeft + $nHorPos * $dStepX - $nMinutesOffset;
								$strMode = "hour";
								$nExtraPos = 0;
								break;

					// day
					case 2:		$dStepX = $dPixelPerSec * 86400; // (86400 = 60 * 60 * 24)
								$nHoursOffset = $dStepX / 24 * $nHours;
								$nMinutesOffset = $dStepX / 1440 * $nMinutes; // (1440 = 24 * 60)
								$nPosTime = $nLeft + ($nHorPos * $dStepX) - $nHoursOffset - $nMinutesOffset;
								$strMode = "day";
								$nExtraPos = 0;
								break;

					// week
					case 3:		$dStepX = $dPixelPerSec * 86400; // (86400 = 60 * 60 * 24)
								$nHoursOffset = $dStepX / 24 * $nHours;
								$nMinutesOffset = $dStepX / 1440 * $nMinutes; // (1440 = 24 * 60)
								$nPosTime = $nLeft + ($nHorPos * $dStepX) - $nHoursOffset - $nMinutesOffset;
								$strMode = "week";
								$nExtraPos = 7; // Make sure all the text for the x-scale is printed
								break;

					default:	break;
				}

				$bSeperator = FALSE;
				$bDelimitationLine = TRUE;

				// Draw horizontal numbers or text
				for ($pos = 0; $pos <= $nHorPos + $nExtraPos; $pos++)
				{
					switch($nMode)
					{
						// $strTextX		Text or number for x-scale
						// $nTextOffset		Dislocate $strTextX to the right place in relation to $nMode and the delimitationlines

						// Hour
						case 1:		$strTextX = strftime("%H", $nStopTime - (3600 * $pos)); // 3600 = 60 * 60
									$nTextOffset = $nFontWidth;

									// Only print every other hour
									if ((($strTextX % 2) == 0) && (($nPosTime + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime - $nTextOffset) >= $nLeft))
									{
										ImageString($im, $font, $nPosTime - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
									}

									// Should we draw separatorline?
									$bSeperator = $strTextX == "0" ? TRUE : FALSE;
									break;

						// Day
						case 2:		// Check if both name and number of the day fits between delimitationlines
									if ($dStepX < (7 * $nFontWidth)) // Only number
									{
										$strTextX = strftime("%d", $nStopTime - (86400 * $pos)); // 86400 = 24 * 60 * 60
										$nTextOffset = $nFontWidth;
									}
									else // Both name and number
									{
										$strTextX = strftime("%a %d", $nStopTime - (86400 * $pos)); 
										$nTextOffset =  3 * $nFontWidth;
									}
									
									$nTextCenter = $dStepX / 2;

									if ((($nPosTime + $nTextCenter + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime + $nTextCenter - $nTextOffset) >= $nLeft))
									{
										ImageString($im, $font, $nPosTime + $nTextCenter - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
									}

									// Should we draw separatorline?
									$bSeperator = strftime("%w", $nStopTime - 3600 * 24 * $pos) == "1" ? true : false;
									break;

						// Week
						case 3:		$strTextX = strftime("%V", $nStopTime - (86400 * $pos)); // 86400 = 24 * 60 * 60
									$nTextCenter = $dStepX * 7 / 2;
									$nTextOffset = $nFontWidth;

									if ($strTextX != strftime("%V", $nStopTime - 86400 * ($pos + 1)))
									{
										if (($dStepX * 7) >= (5 * $nFontWidth))
										{
											$strTextX = "w $strTextX";
											$nTextOffset = 2 * $nFontWidth;
										}

										if ((($nPosTime + $nTextCenter + $nTextOffset) <= ($nLeft + $nWidth)) && (($nPosTime + $nTextCenter - $nTextOffset) >= $nLeft))
										{
											ImageString($im, $font, $nPosTime + $nTextCenter - $nTextOffset, $nTop + $nHeight + 4, $strTextX, $clrBlack);
										}
										
										$bDelimitationLine = true;
									}
									else
									{
										$bDelimitationLine = false;
									}

									// Should we draw separatorline?
									$bSeperator = strftime("%d", $nStopTime - 3600 * 24 * $pos) == "01" ? true : false;
									break;

						default:	break;
					}
				
					if ($nPosTime >= $nLeft)
					{					
						// Draw delimitationline?
						if ($bDelimitationLine)
						{
							// Draw gridline if the gridmode is correct
							//if ($nVerGridMode != 1 && ($nVerGridMode == 3 || $pos % 2 == 0)) 
								//ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop + $nHeight, IMG_COLOR_STYLED);	

							//ImageLine($im, $nPosTime, $nTop + $nHeight - 3, $nPosTime, $nTop + $nHeight + 3, $clrBlack);	
						}

						// Draw separatorline?
						if ($bSeperator)
						{
							//ImageSetStyle($im, $arrSeparator);
							//ImageLine($im, $nPosTime, $nTop, $nPosTime, $nTop + $nHeight, IMG_COLOR_STYLED);
							//ImageSetStyle($im, $arrGridStyle);
						}
					}
					
					$nPosTime -= $dStepX;	
				// Draw data
				for ($i = 1; $i < $nCount; $i++)
				{	
					$nTime = $arrTime[$i];
					$nX = ($nTime - $nStartTime) * $dPixelPerSec + $nLeft;
					$nSigY = (-$arrSig[$i] - 50) * $dPixelPerDBM + $nTop;
					$nNoiY = (-$arrNoi[$i] - 50) * $dPixelPerDBM + $nTop;
					
					$nTimeOld = $arrTime[$i - 1];
					$nXOld = ($nTimeOld - $nStartTime) * $dPixelPerSec + $nLeft;
					$nSigYOld = (-$arrSig[$i - 1] - 50) * $dPixelPerDBM + $nTop;
					$nNoiYOld = (-$arrNoi[$i - 1] - 50) * $dPixelPerDBM + $nTop;

					// Check if the unit has been up or down
					$bHasBeenUp = ($nTime - $nTimeOld) < $nTimeBeforeDown ? TRUE : FALSE;

					if ($nDrawMode == 5) // Small line and small + 
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrLBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrRed);
						}

						ImageLine($im, $nX - 1, $nSigY, $nX + 1, $nSigY, $clrLBlue);
						ImageLine($im, $nX, $nSigY - 1, $nX, $nSigY + 1, $clrLBlue);

						ImageLine($im, $nX - 1, $nNoiY, $nX + 1, $nNoiY, $clrRed);
						ImageLine($im, $nX, $nNoiY - 1, $nX, $nNoiY + 1, $clrRed);
					}
					else if ($nDrawMode == 6) // Small line and small X 
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrLBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrRed);
						}
							
						ImageLine($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrLBlue);
						ImageLine($im, $nX + 1, $nSigY - 1, $nX - 1, $nSigY + 1, $clrLBlue);

						ImageLine($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrRed);
						ImageLine($im, $nX + 1, $nNoiY - 1, $nX - 1, $nNoiY + 1, $clrRed);
					}
					else if ($nDrawMode == 7) // Small dot
					{
						ImageSetPixel($im, $nX, $nSigY, $clrLBlue);
						ImageSetPixel($im, $nX, $nNoiY, $clrRed);
					}
					else if ($nDrawMode == 8) // Medium dot
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX, $nSigY, $clrLBlue);
						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX, $nNoiY, $clrRed);
					}
					else if ($nDrawMode == 9) // Large dot
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrLBlue);
						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrRed);
					}
					else if ($nDrawMode == 10) // Small + 
					{
						ImageLine($im, $nX - 1, $nSigY, $nX + 1, $nSigY, $clrLBlue);
						ImageLine($im, $nX, $nSigY - 1, $nX, $nSigY + 1, $clrLBlue);

						ImageLine($im, $nX - 1, $nNoiY, $nX + 1, $nNoiY, $clrRed);
						ImageLine($im, $nX, $nNoiY - 1, $nX, $nNoiY + 1, $clrRed);
					}
					else if ($nDrawMode == 11) // Medium + 
					{
						ImageFilledRectangle($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY, $clrLBlue);
						ImageLine($im, $nX, $nSigY - 2, $nX, $nSigY + 1, $clrLBlue);

						ImageFilledRectangle($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY, $clrRed);
						ImageLine($im, $nX, $nNoiY - 2, $nX, $nNoiY + 1, $clrRed);
					}
					else if ($nDrawMode == 12) // Small X 
					{
						ImageLine($im, $nX - 1, $nSigY - 1, $nX + 1, $nSigY + 1, $clrLBlue);
						ImageLine($im, $nX + 1, $nSigY - 1, $nX - 1, $nSigY + 1, $clrLBlue);

						ImageLine($im, $nX - 1, $nNoiY - 1, $nX + 1, $nNoiY + 1, $clrRed);
						ImageLine($im, $nX + 1, $nNoiY - 1, $nX - 1, $nNoiY + 1, $clrRed);
					}
					else // Default = small line
					{
						if ($bHasBeenUp)
						{
							ImageLine($im, $nXOld, $nSigYOld, $nX, $nSigY, $clrLBlue);
							ImageLine($im, $nXOld, $nNoiYOld, $nX, $nNoiY, $clrRed);
						}
						else
						{
							ImageSetPixel($im, $nX, $nSigY, $clrLBlue);
							ImageSetPixel($im, $nX, $nNoiY, $clrRed);
						}
					}
				}
			}
		}
		else
		{
			$bError = TRUE;
			$strError .= "There are no data in the database for the reqested time. ";
		}
	}
	else
	{
		$bError = TRUE;
		$strError .= "Not a valid query to the database. Check connection_id, mac, if_name and node_id. ";
	}
}

// Write error message if an error has occurred
if ($bError)
	ImageString($im, $font, $nLeft + 15, $nTop + ($nHeight/2) - $nFontHeight, $strError, $clrTitle);

// Write timeperiod and mode
$strPeriodStart = strftime("%y-%m-%d %R", $nStartTime);
$strPeriodStop = strftime("%y-%m-%d %R", $nStopTime);
$str = "$strPeriodStart - $strPeriodStop [$strMode]";
ImageString($im, $font, ($nSizeX/2) - (($nFontWidth*strlen($str))/2), $nSizeY-$nFontHeight-2, $str , $clrTitle);
ImageString($im, $font, 6, $nSizeY - 6 - $nFontHeight, "dBm", $clrTitle);

// Draw horizontal and vertical line
ImageLine($im, $nLeft, $nTop + $nHeight, $nLeft + $nWidth, $nTop + $nHeight, $clrBlack);
ImageLine($im, $nLeft, $nTop, $nLeft, $nTop + $nHeight, $clrBlack);

// Output Image
ImagePng($im);
ImageDestroy($im);
?>
Return current item: WaveWatcher