#!/usr/bin/php
<?php
#
# device-status - Called by web interface to get device status for display.
#
# (C) Copyright 2024-2026, Bogen Communications LLC. All rights reserved.
#
#
require '/usr/local/bin/db_credentials' ;
#
$debug = false;

$db = NULL;
$amp_id = "";
$extension = "";
$gateway_id = "";
$skip_ip_display = false;
$skip_forwarding_display = false;

$cmd_options = getopt("a:de:g:s");

foreach (array_keys($cmd_options) as $opt) {
	switch ($opt) {
		case 'a':
			$amp_id = $cmd_options['a'];
			break;
		case 'd':
			$debug = true;
			break;
		case 'e':
			$extension = $cmd_options['e'];
			break;
		case 'g':
			$gateway_id = $cmd_options['g'];
			break;
		case 's':
			$skip_ip_display = true;
			$skip_forwarding_display = true;
			break;
		default:
			exit(1);
	}
}

if ($debug == false) {
	error_reporting(0);
	mysqli_report(MYSQLI_REPORT_OFF);
}

$db = new mysqli($server, $user, $pass, $database);

if ($db->connect_errno > 0) {
	if ($debug) {
		die('Unable to connect to database ['.$db->connect_error.']');
	} else {
		doExit(1);
	}
}

#
# Display ASB Information
#
if (($gateway_id != "") && ($gateway_id != 0)) {
	$sql = "SELECT mac_address,ip_address,status,serial_number
		FROM gateway WHERE id='$gateway_id'";

	$got_asb_info = false;

	if ($result = $db->query("$sql")) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();

			$gw_mac = $row['mac_address'];
			$gw_ip = long2ip($row['ip_address']);
			$gw_status = $row['status'];
			$gw_sn = $row['serial_number'];

			printf("%-20s: IP Address: <a href=\"http://%s\" target=\"_blank\" rel=\"noopener noreferrer\">%s</a>, SN:%s, MAC:%s\n", "ASB", $gw_ip, $gw_ip, $gw_sn, $gw_mac);

			printf("%-20s: %s\n","ASB Status",$gw_status);

			$got_asb_info = true;
		}
		$result->free();
	}

	if ($got_asb_info == false) exit(0);

	$dcs_config_filename = "/srv/tftp/backup/$gw_mac"."-canconfig.xml";

	if (file_exists($dcs_config_filename)) {
		$dcs_config_str = file_get_contents($dcs_config_filename);
	} else {
		$dcs_config_str = '<SerializableClass Type="CANconfiguration" Version="0.1">
    				<Collection Name="PortsConfiguration"/>
    				<Collection Name="DCSentries"/>
				</SerializableClass>';
	}

	$dataArray = new SimpleXMLElement($dcs_config_str);

	#
	# Get assigned DCS serial numbers
	#
	$dcsArray = array();

	if($dataArray->Collection[0]){ // make sure the collection exists
		foreach($dataArray->Collection[0]->children() as $data){

			$port_number =  (string)$data->Member[0];

			$serial_number = '';

			foreach($data->Collection->Class as $data2){

				$serial_number = (string)$data2->Member[0];
				$device_number = (string)$data2->Member[1];

				if($serial_number!=''){
					array_push($dcsArray, array($serial_number,$device_number,$port_number));
				}
			}
		}
	}

	if ($debug) {
		echo "DCS List:\n";
		print_r($dcsArray);
	}

	$sql = "SELECT extension FROM station WHERE gateway_id='$gateway_id'
		ORDER BY gateway_port";

	if ($result = $db->query("$sql")) {
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {

				$extension = $row['extension'];

				exec("/usr/local/bin/device-status -s -e \"$extension\" 2>/dev/null",$out,$ret);

				if ($ret != 0) continue;

				foreach ($out as $line) {
					if (preg_match("/ASB Station         : (.*)\(Port:(.*), SN:(.*), MAC:(.*)\)/", $line, $parts)) {
						$station_name = getStationName($debug, $db, $extension);
						$port = $parts[2];
						printf("\n%-20s: Port %s: %s (%s)\n","Station",$port,$station_name,$extension);

						# Display list of DCS associated with this port.
						$dcs_sep = "";
						foreach ($dcsArray as $dcsSwitch) {
							if ($dcsSwitch[2] == $port) {
								if ($dcs_sep == "") {
									printf("%-20s: ","DCS list");
									echo "$dcs_sep$dcsSwitch[0]";
									$dcs_sep = ",";
								} else {
									echo "$dcs_sep$dcsSwitch[0]";
								}
							}
						}
						if ($dcs_sep != "") {
							echo "\n";
						}
						unset($parts);
					} else {
						echo "$line\n";
					}
				}
				unset($out);
			}
		}
		$result->free();
	}
	if ($gw_ip != "" && $gw_ip != "0.0.0.0") {
		$ping_output = shell_exec("/bin/ping -c 3 -W 15 $gw_ip");
		printf("\n%s",$ping_output);
		$firmware = file_get_contents('http://'.$gw_ip.'/services/firmware.php');
		if ($firmware != "") {
			printf("\nFirmware: %s\n",$firmware);
		}
	}

	exit(0);
}

#
# Display Amplifier Information
#
if (($amp_id != "") && ($amp_id != 0)) {

	$sql = "SELECT device_type,ip_address,mac_address,serial_number,
		channels,status
		FROM amplifier WHERE id='$amp_id'";
	if ($result = $db->query("$sql")) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();

			$amp_type = $row['device_type'];
			$amp_ip = long2ip($row['ip_address']);
			$amp_mac = $row['mac_address'];
			$amp_sn = $row['serial_number'];
			$amp_channels = $row['channels'];
			$amp_status = $row['status'];

			if ($amp_ip == 0) $amp_ip = "Unknown";

			printf("%-20s: IP Address: <a href=\"http://%s\" target=\"_blank\" rel=\"noopener noreferrer\">%s</a>, Channels:%s, SN:%s, MAC:%s\n", "Amplifier", $amp_ip, $amp_ip, $amp_channels, $amp_sn, $amp_mac);

			printf("%-20s: %s\n","Amplifier Status",$amp_status);
		}
	}

	#
	# Display Amplifier Stations
	#
	$sep = "";

	$sql = "SELECT extension FROM station WHERE amplifier_id='$amp_id'";

	if ($result = $db->query("$sql")) {
		while ($row = $result->fetch_assoc()) {

			$extension = $row['extension'];

			if ($extension == 0) {
				displaySoundMaskingAmp($debug, $db, $amp_id, $amp_ip);
				exit(0);
			}

			unset($out);

			exec("/usr/local/bin/device-status -e \"$extension\" 2>/dev/null",$out,$ret);

			foreach ($out as $line) {
				if (preg_match("/Station Amp: (.*)\(Port:(.*), SN:(.*), MAC:(.*)\)/", $line, $parts)) {

					$name = "Unknown";
					$sql = "SELECT name FROM station WHERE extension='$extension'";
					if ($result2 = $db->query("$sql")) {
						if ($result2->num_rows > 0) {
							$row = $result2->fetch_assoc();
							$name = $row['name'];
						}
					}

					switch ($amp_type) {
						case '27':
						case '28':
						case '29':
							switch ($parts[2]) {
								case '1':
									$port_label = "Line Out";
									break;
								case '2':
									$port_label = "Speaker Out";
									break;
								default:
									$port_label = "Unknown";
									break;
							}
							break;
						case '67':
							$port_label = "Output:";
							$port_num = $parts[2];
							break;
						default:
							$port_label = "Channel:";
							{ switch ($parts[2]) {
								case '1':
									$port_num = "A";
									break;
								case '2':
									$port_num = "B";
									break;
								case '3':
									$port_num = "C";
									break;
								case '4':
									$port_num = "D";
									break;
							}
							}
							break;
					}

					printf("%s%-20s: %s - %s (%s %s)\n",$sep,"Station",$extension,$name,$port_label,$port_num);
					$sep = "\n";
				} else {
					echo "$line\n";
				}
			}
		}
	}
	if ($amp_ip != "" && $amp_ip != "0.0.0.0") {
		$ping_output = shell_exec("/bin/ping -c 3 -W 15 $amp_ip");
		printf("\n%s",$ping_output);
		$firmware = file_get_contents('http://'.$amp_ip.'/services/firmware.php');
		if ($firmware != "") {
			printf("\nFirmware: %s\n",$firmware);
		}
	}

	exit(0);
}

#
# Display Station Information
#
if ($extension != "") {

	$sql = "SELECT name,type,device_type,mac_address,day_cos,night_cos,
		day_admin,night_admin,spkr_ext,has_paging,has_multicast_audio,
		serial_number,911_route,admin_group,gateway_id,gateway_port,
		amplifier_id,amplifier_port
		FROM station WHERE extension='$extension'";

	if ($result = $db->query("$sql")) {
		if ($result->num_rows == 0) {
			echo "Station $extension not found\n";
			exit(1);
		}
		$row = $result->fetch_assoc();
		$name = $row['name'];
		$type = $row['type'];
		$device_type = $row['device_type'];
		$mac_address = $row['mac_address'];
		$day_admin = $row['day_admin'];
		$night_admin = $row['night_admin'];
		$day_cos = $row['day_cos'];
		$night_cos = $row['night_cos'];
		$spkr_ext = $row['spkr_ext'];
		$has_paging = $row['has_paging'];
		$has_multicast_audio = $row['has_multicast_audio'];
		$serial_number = $row['serial_number'];
		$route_911 = $row['911_route'];
		$admin_group = $row['admin_group'];
		$gateway_id = $row['gateway_id'];
		$gateway_port = $row['gateway_port'];
		$amplifier_id = $row['amplifier_id'];
		$amplifier_port = $row['amplifier_port'];

		if ($debug) {
			echo "Device Name: $name\n";
		}

		if (($gateway_id != 0) && ($gateway_id != "")) {
			$sql = "SELECT name,mac_address,serial_number
				FROM gateway WHERE id='$gateway_id'";
			if ($result = $db->query("$sql")) {
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();

					$gw_name = $row['name'];
					$gw_mac = $row['mac_address'];
					$gw_sn = $row['serial_number'];

					printf("%-20s: %s (Port:%s, SN:%s, MAC:%s)\n","ASB Station",$gw_name,$gateway_port,$gw_sn,$gw_mac);
				}
			}
		}

		if (($amplifier_id != 0) && ($amplifier_id != "")) {
			$sql = "SELECT name,mac_address,serial_number
				FROM amplifier WHERE id='$amplifier_id'";
			if ($result = $db->query("$sql")) {
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();

					$amp_name = $row['name'];
					$amp_mac = $row['mac_address'];
					$amp_sn = $row['serial_number'];

					#TODO: Change "Port" to "Channel" terminology.
					printf("\n%-20s: %s (Port:%s, SN:%s, MAC:%s)\n","Amplifier Station",$amp_name,$amplifier_port,$amp_sn,$amp_mac);
				}
			}
		}

		if (($serial_number != "") && ($serial_number != "Unknown")) {
			printf("%-20s: %s\n","Serial Number",$serial_number);
		}

		if ($mac_address != "") {
			printf("%-20s: %s\n","MAC Address","$mac_address");
		}

		#
		# Display DSC01/WBC settings
		#
		if (($device_type == 71) || ($device_type == 73)) {
			$sql = "SELECT enable_clock_display,enable_flasher_use
				FROM wbc_display WHERE extension='$extension'";

			if ($result = $db->query("$sql")) {
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();

					printf("%-20s: %s\n","Enable Clock Display",($row['enable_clock_display']) ? "Yes" : "No");
					printf("%-20s: %s\n","Enable Flasher Use",($row['enable_flasher_use']) ? "Yes" : "No");
				}
				$result->free();
			}
		}


		#
		# Display Paging Zone configuration
		#
		$sql = "SELECT id FROM paging_exclusion WHERE extension='$extension'";
		if ($result = $db->query("$sql")) {
			if ($result->num_rows == 0) {
				printf("%-20s: %s\n","Paging",($has_paging) ? "to station enabled" : "to station disabled");
				if ($has_paging) {
					$pnames = getPagingZoneNames($debug, $db, $extension);
					printf("%-20s: %s\n","Paging Zones",$pnames);
				}
			} else {
				printf("%-20s: %s\n","Paging", "excluded from paging (exclusion enabled)");
			}
		}


		printf("%-20s: %s\n","Audio Distribution",($has_multicast_audio) ? "to station enabled" : "to station disabled");

		if (($spkr_ext != "0") && ($spkr_ext != "")) {
			printf("%-20s: %s\n","Speaker Extension",$spkr_ext);
		}
		if (($day_admin != $extension) && ($day_admin != "") && ($type != 28) && ($type != 29)) {
			printf("%-20s: %s\n","Day Admin",($day_admin != 0) ? $day_admin : "Admin Group");

			#
			# Display Day Callin Level
			#
			$sql = "SELECT callin_level_enum.name FROM callin_level_enum
				INNER JOIN cos ON cos.callin_level=callin_level_enum.id
				WHERE cos.id='$day_cos'";
			$result = $db->query("$sql");

			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
			}
			$callin_level_name = $row['name'];
			printf("%-20s: %s\n","Day Call in level", $callin_level_name);
		}

		if (($night_admin != $extension) && ($night_admin != "") && ($type != 28) && ($type != 29)) {
			printf("%-20s: %s\n","Night Admin",($night_admin != 0) ? $night_admin : "Admin Group");

			#
			# Display Night Callin Level
			#
			$sql = "SELECT callin_level_enum.name FROM callin_level_enum
				INNER JOIN cos ON cos.callin_level=callin_level_enum.id
				WHERE cos.id='$night_cos'";
			$result = $db->query("$sql");

			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();
			}
			$callin_level_name = $row['name'];
			printf("%-20s: %s\n","Night Call in level", $callin_level_name);
		}

		#
		# Display Admin Group
		#
		if (($day_admin != $extension) && ($night_admin != $extension) && ($type != 28) && ($type != 29)) {
			if ($admin_group > 0) {
				$admin_group_name = getAdminGroupName($debug, $db, $admin_group);
				if ($admin_group_name != "") {
					printf("%-20s: %s\n","Admin Group",$admin_group_name);
				}
			}
		}

		#
		# Display Admin Groups
		#
		$sql = "SELECT GROUP_CONCAT(queue_name SEPARATOR ',') AS qm
			FROM queue_members WHERE membername='$extension'";

		$result = $db->query("$sql");

		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$admin_groups = $row['qm'];
			if ($admin_groups != "") {
				printf("%-20s: %s\n","Admin Groups",$admin_groups);
			}
		}

		#
		# Dispaly 911 route
		#
		if (($type < 7) || ($type == 17)) {
			printf("%-20s: %s\n","911 Route",$route_911);
		}

		#
		# Display outside access
		#
		$sql = "SELECT outside_access_types.name
			FROM outside_access_types
			INNER JOIN station
			ON outside_access_types.id=station.outside_access
			WHERE station.extension='$extension'";

		$result = $db->query("$sql");

		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$outside_access = $row['name'];

			if (($outside_access != "No Access") && ($type != 23) &&
			    ($type != 28) && ($type != 29) && ($type != 30))  {
				printf("%-20s: %s\n","Outside Access",$outside_access);
			}
		}

		#
		# Display Mixer PTT config
		#
		$sql = "SELECT ptt_type, ptt_zones, ptt_src_to_out, ptt_output
			FROM mixer_config WHERE extension='$extension'";

		$result = $db->query("$sql");

		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();

			printf("%-20s: %s\n","Push To Talk Type",$row['ptt_type']);

			if ($row['ptt_type'] != "Disabled") {
				if ($row['ptt_type'] == "Analog-Output") {
					printf("%-20s: %s\n","PTT Output",$row['ptt_output']);
				}
				else
				if ($row['ptt_zones'] != "") {
					$ptt_zone_name = getZoneName($debug, $db, $row['ptt_zones']);
					printf("%-20s: %s\n","Push To Talk Zone",$ptt_zone_name);
					printf("%-20s: %s\n","PTT Source forOutput",$row['ptt_src_to_out']);
					if ($row['ptt_src_to_out'] == "Analog") {
						printf("%-20s: %s\n","PTT Output",$row['ptt_output']);
					}
				}
				else {
					printf("%-20s: %s\n","PTT Source forOutput",$row['ptt_src_to_out']);
					if ($row['ptt_src_to_out'] == "Analog") {
						printf("%-20s: %s\n","PTT Output",$row['ptt_output']);
					}
				}
			}
		}

		#
		# Display PTT config for GA10PV
		#
		if ($type == 28) {
			$sql = "SELECT ptt_type,ptt_zones
				FROM intercom_config WHERE extension='$extension'";
			$result = $db->query("$sql");

			if ($result->num_rows > 0) {
				$row = $result->fetch_assoc();

				$ptt_zones = $row['ptt_zones'];

				printf("%-20s: %s\n","Push To Talk Type",$row['ptt_type']);

				if ($row['ptt_zones'] != "") {
					$ptt_zone_name = getZoneName($debug, $db, $row['ptt_zones']);
					printf("%-20s: %s\n","Push To Talk Zone",$ptt_zone_name);
				}
			}
		}

		#
		# Display Phantom Power settings from device config
		#
		if ($type == "22") {
			$json_fname = "/srv/tftp/backup/$mac_address"."-mixer.json";
			if (file_exists($json_fname)) {
				$json_data = file_get_contents($json_fname);
				$json = json_decode($json_data,true);
				$inputs = array('In1', 'In2', 'In3', 'In4');

				foreach ($inputs as $input) {
					$phantom_power = $json['Mixer'][$input]['Phantom'];
					if ($phantom_power == true) {
						printf("%-20s: Enabled\n","Phantom Power on $input");
					}
				}
			}

		}


		#
		# Display Call Forwarding
		#
		if ($skip_forwarding_display) {
			$ret = 1;
		} else {
			exec("/usr/sbin/asterisk -rx \"database show CF%/$extension\" 2>/dev/null", $out, $ret);
		}

		if (($ret == 0) && (count($out) > 1)) {
			preg_match("/\/CF(.*)\/([0-9]*)\s*: (.*)/", $out[0], $cfparts);

			if ($cfparts[2] == $extension) {
				switch ($cfparts[1]) {
					case 'ALL':
						$cftype = "All Calls";
						break;
					case 'BS':
						$cftype = "Busy";
						break;
					case 'NA':
						$cftype = "No Answer";
						break;
					case 'BN':
						$cftype = "Busy/No-Answer";
						break;
					default:
						$cftype = "Unknown";
				}

				$fw_name = getStationName($debug, $db, $cfparts[3]);

				printf("%-20s: %s -> %s (%s)\n","Call Forwarding",$cftype,$fw_name,$cfparts[3]);
			}
		}
	} else {
		echo "Station $extension not found\n";
		exit;
	}

	if ($type < 6) {
		exec("ls -l /var/spool/asterisk/voicemail/default/$extension/INBOX/*.g722 2>/dev/null|wc -l", $inbox, $ret);

		if ($ret == 0) {
			if ($inbox[0] > 0) {
				if ($inbox[0] > 1) {
					$plural = "s";
				} else {
					$plural = "";
				}
				printf("%-20s: %s new message%s\n","Voicemail",$inbox[0],$plural);
			}
		}
	}

	#
	# Display IP Address, Registration, Call Status
	#
	unset($out);

	exec("/usr/sbin/asterisk -rx \"pjsip show endpoint $extension\"", $out, $ret);

	$got_contact = false;

	$non_nyquist_devices = [1,2,3,4,5,6,7,8,32,33,34,35,40,45,46,47,48,50];

	if ($ret == 0) {
		foreach ($out as $line) {

			unset($parts);

			$search_str = "Contact:\s*$extension\/sips?:$extension";

			if (preg_match("/^$search_str/", trim($line))) {

				$contact_parts = explode(' ',$line);
				$line = $contact_parts[8];

				#
				# Contact:  750/sip(s):750@10.10.5.31:45625;ob            019c46a8b6 NonQual         nan
				#
				preg_match("/(.*)\/sips?:(.*)@(.*):(.*)/", $line, $parts);

				if (!$skip_ip_display) {
					$appliance_ip = $parts[3];
					#
					# If device is NQ-T2100 (32), use https
					#
					if ($device_type == 32) {
						$secure="s";
					} else {
						$secure="";
					}
					printf("%-20s: <a href=\"http$secure://%s\" target=\"_blank\" rel=\"noopener noreferrer\">%s</a>\n","IP Address",$parts[3],$parts[3]);
				}

				if (str_ends_with($line, 'transport=TL')) {
					$line.= 'S';
				}
				printf("%-20s: %s ","Registered",$line);

				$got_contact = true;

			} elseif (preg_match("~Endpoint:\\s*([0-9]*)\\s*([A-Za-z\\s+]*)\\s*[0-9]*~", $line, $parts)) {
					if ($parts[1] != "") {
						$status = $parts[2];
						printf("%-20s: %s\n","Call Status",$status);
					}
			}

			if (preg_match("/ Transport:\\s*transport-([a-z]*)\\s*/", $line, $parts)) {
				if ($got_contact) {
					if ($parts[1] == "tls") {
							printf("(Secure SIP enabled)\n");
					} else {
						printf("(Secure SIP disabled)\n");
					}
				} else {
					if ($parts[1] == "tls") {
						printf("Configured for Secure SIP with TLS\n");
					}
				}
			}
		}
		if (!$skip_ip_display && $got_contact && !$did_ping) {
			$ping_output = shell_exec("/bin/ping -c 3 -W 15 $appliance_ip");
			printf("\n%s",$ping_output);
			$did_ping = true;
			if (!in_array($device_type, $non_nyquist_devices)) {
				$firmware = file_get_contents('http://'.$appliance_ip.'/services/firmware.php');
				if ($firmware != "") {
					printf("\nFirmware: %s\n",$firmware);
				}
			}
		}

	}
}

doExit(0);

function getStationName($debug, $db, $extension)
{
	$stationName = "Unknown";

	if ($extension == "") return $stationName;

	$sql = "SELECT name FROM station WHERE extension='$extension'";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$stationName = $row['name'];
		}
	}

	return $stationName;
}

function getZoneName($debug, $db, $zone_number)
{
	$name = "";

	if ($zone_number == "") return $name;

	$sql = "SELECT name FROM pagingzones WHERE zone_number='$zone_number'";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$name = $row['name'];
		}
	}

	return $name;
}

function getPagingZoneNames($debug, $db, $extension)
{
	$names = "";

	if ($extension == "") return $names;

	$sql = "SELECT GROUP_CONCAT(pagingzones.name SEPARATOR ',') AS pnames
		FROM pagingzones INNER JOIN zone_sublist
		ON zone_sublist.zone_number=pagingzones.zone_number
		WHERE zone_sublist.station='$extension'";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$names = $row['pnames'];
		}
	}

	return $names;
}

function getAdminGroupName($debug, $db, $admin_group)
{
	$adminGroupName = "";

	if (($admin_group == 0) || ($admin_group == "")) return $adminGroupName;

	$sql = "SELECT name FROM admin_group WHERE id='$admin_group'";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$adminGroupName = $row['name'];
		}
	}

	return $adminGroupName;
}

function displaySoundMaskingAmp($debug, $db, $amp_id, $amp_ip)
{
	if (($amp_id == 0) || ($amp_id == "")) return "";

	$sql = "SELECT zone_number
		FROM sm_zone_sublist
		WHERE amplifier_id='$amp_id' LIMIT 1";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			$row = $result->fetch_assoc();
			$sm_zone_number = $row['zone_number'];

			$sql = "SELECT name
				FROM sm_zones
				WHERE zone_number='$sm_zone_number'";
			if ($result = $db->query($sql)) {
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();

					$sm_zone_name = $row['name'];

					printf("%-20s: %s (%s)\n","Sound Masking Zone",$sm_zone_name,$sm_zone_number);
				}
			}
		}
	}

	$sql = "SELECT extension,name,amplifier_port,in_phase
		FROM station
		WHERE amplifier_id='$amp_id'";

	if ($result = $db->query($sql)) {
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$name = $row['name'];
				$amp_port = $row['amplifier_port'];
				$in_phase = $row['in_phase'];

				switch ($amp_port) {
					case '1':
						$port_label = "A";
						break;
					case '2':
						$port_label = "B";
						break;
					case '3':
						$port_label = "C";
						break;
					case '4':
						$port_label = "D";
						break;
					default:
						$port_label = "?";
						break;
				}
				printf("%-20s: %s (Channel: %s)\n","SoundMasking Station",$name,$port_label);
				printf("%-20s: %s\n","Add Phase Shift",($in_phase) ? "Yes" : "No");
			}
		}
	}

	if ($amp_ip != "" && $amp_ip != "0.0.0.0") {
		$ping_output = shell_exec("/bin/ping -c 3 -W 15 $amp_ip");
		printf("\n%s",$ping_output);
		$firmware = file_get_contents('http://'.$amp_ip.'/services/firmware.php');
		if ($firmware != "") {
			printf("\nFirmware: %s\n",$firmware);
		}
	}
}

function doExit($signo)
{
	global  $debug,
		$db;

	if ($db != NULL) $db->close();

	exit($signo);
}

?>
