<?php
function get_ports_status($ip,$eq_type) {
	echo test;


}

function cr_table_ports($ports) {
	$ip_sw = $_GET['ip'];
	$ports_arr = explode(" ",$ports);
	$mac_file = "db/".$ip_sw.".db";
	// echo $mac_file;
//	if (file_exists($mac_file)) echo '';
//	else shell_exec('./dgs1100.sh '.$ip_sw);
	echo "<a href=/monitor/get_info_eq.php?ip=".$ip_sw."&id=".$_GET['id']."&upd=1>Update mac</a>";
        if ( $_GET['upd'] == '1' ) {
		shell_exec('./dgs1100.sh '.$ip_sw);
	}
//	shell_exec('./dgs1100.sh '.$ip_sw);
	echo("<table id='example' border='1' cellpadding='5' width='100%'>");	
	$i = 0;
	echo "<tr>";
	echo "<td>Номер порта</td><td>Статус порта</td><td>mac</td><td>ip</td><td>vlan</td><td>dogovor</td></tr>";
	$count_arr = count ($ports_arr);
// echo "count=".$count_arr;
// print_r ($ports_arr);
	while ($i<$count_arr-1) {
			echo "<tr>";
			$y = $i + 1;
			$port_num = $ports_arr[$i];
			if ( $ports_arr[$y] == '1' ) {
				$ports_status = 'Port UP';
                                $mac_port = get_mac_by_port($ip_sw,$port_num);
				$ip_mac_port = find_ip_by_mac($mac_port)[0];
				$uid = get_id_by_mac($mac_port);
				$vlan = get_vlan_by_port($ip_sw,$port_num);
				echo "<td>".$port_num."</td><td>".$ports_status."</td><td>".$mac_port."</td><td>".$ip_mac_port."</td><td>".$vlan."</td><td><a href = 'https://bill.tiger-telecom.ru/?mod=usr&act=viewinfo&uid=".$uid."'>".$uid."</td>";
			} else {
				$ports_status = 'Port Down';
				echo "<td>".$port_num."</td><td>".$ports_status."</td><td>Port Down</td><td>Port Down</td><td>Проверка</td><td>Port Down</td>";
			}
			// $port_num = $ports_arr[$i];
			// echo "<td>".$port_num."</td><td>".$ports_status."</td><td>".get_mac_by_port($ip_sw,$port_num)."</td>";
			echo "</tr>";
			$i++;
			$i++;
	}
	echo "</table>";
}

function get_mac_ports($port,$ip) {
	$test = './dgs1100.sh '.$ip.' "show fdb po '.$port.'" | grep Dynamic | awk \'{print $3}\'';
	$test2 = escapeshellcmd($test);
	$output = shell_exec($test2);
	return $output;
}

function find_ip_by_mac ($mac) {
	$test = 'cat /var/www/html/monitor/db/arp.db | grep '.$mac.' | awk \'{print $1}\'';
	//$test2 = escapeshellcmd($test);
	$ipmac = shell_exec($test);
	$ip_mac_arr = explode(' ',$ipmac);
	return $ip_mac_arr;
}

function get_mac_by_port($ip,$port) {
	// $test = 'cat /var/www/html/monitor/db/'.$ip.'.db | grep  \'\s'.$port.'\s\' | awk \'{print $3}\'';
	$test = '/var/www/html/monitor/get_mac_port.sh '.$ip.' '.$port;
//echo $test;
	$test2 = escapeshellcmd($test);
	$mac2 = shell_exec($test2);
	$len_mac = strlen($mac2);
	if ( $len_mac > '18') {
		$mac2 = substr ($mac2,0,18);
		$mac2 = $mac2.' Вероятно магистральный';
		//echo $mac2;
	}
//	echo 'len='.$len_mac;
	// $mac2 = substr($mac2,0,-1);
	// echo '$mac='.$mac;
	// echo $test2;
	// $mac = shell_exec ($test);
	return $mac2;
}
	
function get_vlan_by_port($ip,$port) {
        $test = '/var/www/html/monitor/get_vlan_port.sh '.$ip.' '.$port;
        $test2 = escapeshellcmd($test);
        $vlan2 = shell_exec($test2);
        $len_mac = strlen($vlan2);
        if ( $len_mac > '4') {
                $vlan2 = substr ($vlan2,0,4);
                $vlan2 = $vlan2.' Вероятно магистральный';
        }
        return $vlan2;
}


function get_id_by_mac($mac) {
	$link = msconnect_tt();
	$mac_bill = strtoupper ($mac);
	$mac_bill2 = substr($mac_bill,0,17);
	$sql = 'select userid from users_ip where macaddr = \''.$mac_bill2.'\' limit 1';
	//$sql = escapeshellcmd($sql);
	// echo $sql;
	$result = mysqli_query($link, $sql);
	if ($result->num_rows) {
		$row = mysqli_fetch_assoc($result);
		return $row['userid'];
		mysqli_free_result($result);
		// return $result;
	} 
	else {
		return 'Не найдено';
	}
	msclose($link);
}

?>
