Esempio n. 1
0
function Layout() {

	global $link,$locsep,$resmsg;
	global $xm,$ym,$xo,$yo,$len,$geo,$ina,$flt,$lev,$ipi;
	global $mapbg,$rloc,$cloc,$bloc;

	$net       = array();

# Gather Device Info
	if($ina == "vlan"){
		$query	= GenQuery('vlans','g','device','device','',array('vlanid'),array('regexp'),array($flt));
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($vl = @DbFetchRow($res)) ){
				$devs[] = preg_replace('/([\^\$+])/','\\\\\\\\$1',$vl[0]);		# \Q...\E Doesn't seem to work in MySQL?
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		if (! is_array ($devs) ){echo $resmsg;die;}
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('name'),array('regexp'),array(implode("|",$devs)));
	}elseif($ina == "network"){
		$query	= GenQuery('networks','g','device','device','',array('ip'),array('='),array($flt));
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($net = @DbFetchRow($res)) ){
				$devs[] = preg_replace('/([\^\$\*\+])/','\\\\\\\\$1',$net[0]);
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		if (! is_array ($devs) ){echo $resmsg;die;}
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('name'),array('regexp'),array(implode("|",$devs)));
	}elseif($ina == "type"){
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('type'),array('regexp'),array($flt));
	}else{
		$query	= GenQuery('devices','s','name,ip,location,icon','','',array('location'),array('regexp'),array($flt));
	}
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($d = @DbFetchRow($res)) ){
			$l = explode($locsep, $d[2]);
			if($lev == "c" or $lev == "C"){
				$locs[$l[0]][$l[1]]++;
			}elseif($lev == "b" or $lev == "B"){
				$locs[$l[0]][$l[1]][$l[2]]++;
			}else{
				$locs[$l[0]][$l[1]][$l[2]][$l[3]][] = $d[0];
			}
			$dev['ip'] [$d[0]] = long2ip($d[1]);
			$dev['ic'] [$d[0]] = $d[3];
			$dev['reg'][$d[0]] = $l[0];
			$dev['cty'][$d[0]] = $l[1];
			$dev['bld'][$d[0]] = $l[2];
			$dev['rom'][$d[0]] = $l[4];
		}
		@DbFreeResult($res);
	}else{
		echo @DbError($link);
	}
	if($ipi){
		$query	= GenQuery('networks');
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($n = @DbFetchRow($res)) ){
				$net[$n[0]][$n[1]] .= " ". long2ip($n[2]);
			}
		}else{
			echo @DbError($link);
		}
		@DbFreeResult($res);
	}

# Gather Link Info
	$query	= GenQuery('links');
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($l = @DbFetchRow($res)) ){
			if($dev[$l[1]]['ic'] and $dev[$l[3]]['ic']){					# both ends are ok, if an icon exists
				if($lev == "f"){
					if( isset($devlink[$l[3]][$l[1]]) ){				# opposite link doesn't exist?
						$devlink[$l[3]][$l[1]]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{
						$devlink[$l[1]][$l[3]]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
				if($dev[$l[1]]['bld'] != $dev[$l[3]]['bld'])			{	# is it same bld?
					$nbldlink[$dev[$l[1]]['bld']] ++;
					$abldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]++;		# needed for Arranging.
					if(isset($bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]) ){	# link defined already?
						$bldlink[$dev[$l[3]]['bld']][$dev[$l[1]]['bld']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{
						$bldlink[$dev[$l[1]]['bld']][$dev[$l[3]]['bld']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
				if($dev[$l[1]]['cty'] != $dev[$l[3]]['cty']){				# is it same cty?
					$nctylink[$dev[$l[1]]['cty']]++;
					$actylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]++;		# needed for Arranging.
					if(isset($ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]) ){	# link defined already?
						$ctylink[$dev[$l[3]]['cty']][$dev[$l[1]]['cty']]['nbw'][$l[3]][$l[4]][$l[1]][$l[2]] = $l[5];
					}else{

						$ctylink[$dev[$l[1]]['cty']][$dev[$l[3]]['cty']]['bw'][$l[1]][$l[2]][$l[3]][$l[4]] = $l[5];
					}
				}
			}
		}
		@DbFreeResult($res);
	}else{
		echo @DbError($link);
	}

# Prepare geographic stuff
	$mapinfo   = "";
	$mapframes = "";
	$maplinks  = "";
	$mapitems  = "";

	if ($geo) {
		if(count($cdev) == 1){
			$rk   = array_keys($cdev);
			if(count($cdev[$rk[0]]) == 1){
				$ck = array_keys($cdev[$rk[0]]);
				$mapbg = TopoMap($rk[0],$ck[0]);
			}else{
				$mapbg = TopoMap($rk[0]);
			}
		}else{
			$mapbg = TopoMap();
		}
		$bg = Imagecreatefrompng("log/$mapbg");
		$xm = Imagesx($bg);
		$ym = Imagesy($bg);
		Imagedestroy($bg);

		$query	= GenQuery('locations');
		$res	= @DbQuery($query,$link);
		if($res){
			while( ($l = @DbFetchRow($res)) ){
				if($l[3]){
					if( $mapbg == TopoMap($l[1],$l[2]) ){
						$bloc[$l[3]] = $l[4];
						$bloc[$l[3]] = $l[5];
					}
				}elseif($l[2]){
					if($mapbg == TopoMap($l[1]) ){
						$cloc[$l[2]] = $l[4];
						$cloc[$l[2]] = $l[5];
					}
				}else{
					$rloc[$l[1]] = $l[4];
					$rloc[$l[1]] = $l[5];
				}
			}
			@DbFreeResult($res);
		}else{
			echo @DbError($link);
		}
		
	}
# Build Layout
	Drawlevel($locs,(intval($xm/2) + $xo), (intval($ym/2) + $yo),$len );
}
function Map()
{
    global $link, $lev, $fco, $xm, $ym, $xo, $yo, $csi, $bsi, $fsi, $cro, $bro, $cwt, $loi, $bwt, $dev, $ndev, $bdev, $cdev;
    global $devlink, $ctylink, $bldlink, $rdevlink, $rctylink, $rbldlink, $nctylink, $nbldlink, $imgmap;
    global $geo, $xbl, $ybl, $xct, $yct, $mapbg, $mapinfo, $mapframes, $maplinks, $mapitems, $locsep;
    $mapinfo = "";
    $mapframes = "";
    $maplinks = "";
    $mapitems = "";
    $ctyscalx = 1.3;
    $ctyscaly = 1;
    $nreg = count($rdev);
    $ncty = count($cdev);
    if ($nreg == 1) {
        $rk = array_keys($rdev);
        if ($ncty == 1) {
            $ctyscalx = 0;
            $ctyscaly = 0;
            $ck = array_keys($rdev[$rk[0]]);
            $mapbg = TopoMap($rk[0], $ck[0]);
        } else {
            $mapbg = TopoMap($rk[0]);
        }
    } else {
        $mapbg = TopoMap();
    }
    if ($geo) {
        $bg = Imagecreatefrompng("log/{$mapbg}");
        $xm = Imagesx($bg);
        $ym = Imagesy($bg);
        Imagedestroy($bg);
        $query = GenQuery('locations');
        $res = @DbQuery($query, $link);
        if ($res) {
            while ($l = @DbFetchRow($res)) {
                if ($l[3]) {
                    if ($mapbg == TopoMap($l[1], $l[2])) {
                        $xbl["{$l['1']}{$locsep}{$l['2']}"][$l[3]] = $l[4];
                        $ybl["{$l['1']}{$locsep}{$l['2']}"][$l[3]] = $l[5];
                    }
                } elseif ($l[2]) {
                    if ($mapbg == TopoMap($l[1])) {
                        $xct["{$l['1']}{$locsep}{$l['2']}"] = $l[4];
                        $yct["{$l['1']}{$locsep}{$l['2']}"] = $l[5];
                    }
                } else {
                    $xrg[$l[1]] = $l[4];
                    $yrg[$l[1]] = $l[5];
                }
            }
            @DbFreeResult($res);
        } else {
            echo @DbError($link);
        }
    }
    $ctynum = 0;
    ksort($cdev);
    foreach (Arrange($cdev, "c") as $cty) {
        $ctynum++;
        $nbld = count($ndev[$cty]);
        $ncl = $nctylink[$cty] ? $nctylink[$cty] : 1;
        $l = explode($locsep, $cty);
        if (!(isset($xct[$cty]) and isset($yct[$cty]))) {
            $phi = $cro * M_PI / 180 + 2 * $ctynum * M_PI / $ncty;
            $ctywght = pow($ncl, $cwt / 10);
            $xct[$cty] = intval(intval($xm / 2) + $xo + $csi * cos($phi) * $ctyscalx / $ctywght);
            $yct[$cty] = intval(intval($ym / 2) + $yo + $csi * sin($phi) * $ctyscaly / $ctywght);
        }
        if ($lev == "c" or $lev == "C") {
            $mapitems .= Drawitem($xct[$cty], $yct[$cty], $cdev[$cty], $l[1], $lev);
            $area = $xct[$cty] - 20 . "," . ($yct[$cty] - 20) . "," . ($xct[$cty] + 20) . "," . ($yct[$cty] + 20);
            $imgmap .= "<area href=?flt=" . rawurlencode($cty) . "&lev=b&loi=1&draw=1 coords=\"{$area}\" shape=rect title=\"Show {$nbld} buildings\">\n";
        } else {
            if ($nbld == 1) {
                $bldscalx = 0;
                $bldscaly = 0;
            } else {
                $bldscalx = 1.3;
                $bldscaly = 1;
                if ($loi) {
                    $mapinfo .= Drawitem($xct[$cty], $yct[$cty], '0', "{$l['1']} {$l['0']}", 'ci');
                }
            }
            $bldnum = 0;
            foreach (Arrange($ndev[$cty], "b") as $bld) {
                $bldnum++;
                $nbl = $nbldlink[$bld] ? $nbldlink[$bld] : 1;
                if (!(isset($xbl[$bld]) and isset($ybl[$bld]))) {
                    $eps = $bro * M_PI / 180 + 2 * $bldnum * M_PI / $nbld;
                    $bldwght = pow($nbl, $bwt / 10);
                    $xbl[$bld] = intval($xct[$cty] + $bsi * cos($eps) * $bldscalx / $bldwght);
                    $ybl[$bld] = intval($yct[$cty] + $bsi * sin($eps) * $bldscaly / $bldwght);
                }
                if ($lev == "b" or $lev == "B") {
                    $mapitems .= Drawitem($xbl[$bld], $ybl[$bld], $bdev[$cty][$bld], $bld, $lev);
                    $area = $xbl[$bld] - 20 . "," . ($ybl[$bld] - 20) . "," . ($xbl[$bld] + 20) . "," . ($ybl[$bld] + 20);
                    $imgmap .= "<area href=?flt=" . rawurlencode($bld) . "&lev=f&loi=1&ipi=1&draw=1 coords=\"{$area}\" shape=rect title=\"Show " . $bdev[$cty][$bld] . " devices\">\n";
                } else {
                    $cury = $rows = 0;
                    $cols = 1;
                    foreach (array_keys($ndev[$cty][$bld]) as $flr) {
                        # Determine building size
                        $cols = max(count($ndev[$cty][$bld][$flr]), $cols);
                        # find max cols
                        if ($cols > $fco) {
                            # Break row, if > Floor columns
                            $rrow = ceil($cols / $fco);
                            # How many rows result?
                            $rows += $rrow;
                            $cols = $fco;
                        } else {
                            $rows++;
                        }
                    }
                    $woff = intval($fsi * ($cols - 1) / 2);
                    $hoff = intval($fsi * ($rows - 1) / 2);
                    $mapframes .= Drawbox($xbl[$bld] - $woff - intval($fsi / 2) - 10, $ybl[$bld] - $hoff - intval($fsi / 2), $xbl[$bld] + $woff + intval($fsi / 2), $ybl[$bld] + $hoff + intval($fsi / 2), $bld);
                    uksort($ndev[$cty][$bld], "Floorsort");
                    foreach (array_keys($ndev[$cty][$bld]) as $flr) {
                        $mapitems .= Drawitem($xbl[$bld] - $woff - intval($fsi / 2), $ybl[$bld] - $hoff + $cury * $fsi, 0, $flr, 'fl');
                        usort($ndev[$cty][$bld][$flr], "Roomsort");
                        $curx = 0;
                        foreach ($ndev[$cty][$bld][$flr] as $dv) {
                            $xd[$dv] = $xbl[$bld] - $woff + $curx * $fsi;
                            $yd[$dv] = $ybl[$bld] - $hoff + $cury * $fsi;
                            $mapitems .= Drawitem($xd[$dv], $yd[$dv], '0', $dv, 'd');
                            $area = $xd[$dv] - 20 . "," . ($yd[$dv] - 20) . "," . ($xd[$dv] + 20) . "," . ($yd[$dv] + 20);
                            $imgmap .= "<area href=Devices-Status.php?dev=" . rawurlencode($dv) . " coords=\"{$area}\" shape=rect title=\"Show {$dv} Status\">\n";
                            $curx++;
                            if ($curx == $fco) {
                                $curx = 0;
                                $cury++;
                            }
                        }
                        $cury++;
                    }
                }
            }
        }
    }
    if ($lev == "c" or $lev == "C") {
        # Draw links after we set all coordinates
        foreach (array_keys($ctylink) as $ctyl) {
            foreach (array_keys($ctylink[$ctyl]) as $ctyn) {
                $mylink = Drawlink($xct[$ctyl], $yct[$ctyl], $xct[$ctyn], $yct[$ctyn], $ctylink[$ctyl][$ctyn]);
                $maplinks .= $mylink[0];
                $mapitems .= $mylink[1];
            }
        }
    } elseif ($lev == "b" or $lev == "B") {
        foreach (array_keys($bldlink) as $bldl) {
            foreach (array_keys($bldlink[$bldl]) as $bldn) {
                $mylink = Drawlink($xbl[$bldl], $ybl[$bldl], $xbl[$bldn], $ybl[$bldn], $bldlink[$bldl][$bldn]);
                $maplinks .= $mylink[0];
                $mapitems .= $mylink[1];
            }
        }
    } elseif ($lev == "f") {
        foreach (array_keys($devlink) as $devl) {
            foreach (array_keys($devlink[$devl]) as $devn) {
                $mylink = Drawlink($xd[$devl] - 8, $yd[$devl] - 4, $xd[$devn] - 8, $yd[$devn] - 4, $devlink[$devl][$devn]);
                $maplinks .= $mylink[0];
                $mapitems .= $mylink[1];
            }
        }
    }
}