Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/Devices/Admin.php
<?php

require_once('./CS_Includes.inc.php');

function BuildAdminForm(&$newForm, $groupMenuList, $device)
{
	global $CSD_TYPES, $CSD_V3AUTHS, $CSD_V3PRIVS;

	if (!is_a($newForm, 'CSForm'))
		throw new Exception('Invalid CSForm context');

	$newForm->AddControl('<strong>Hostname</strong><br />Specifies the name of the device (e.g. hostname)',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'hostname', 'value' => ($device) ? $device->hostname : NULL, 'size' => '30')));

	$s = $newForm->AddControl('<strong>Type</strong><br />Select the device type',
		new CSFormControl(CSF_SELECT, array('name' => 'type_code')));
	foreach($CSD_TYPES as $deviceTypeCode => $deviceTypeName)
	{
		$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $deviceTypeCode, 'text' => $deviceTypeName,
								'selected' => ($device && $device->type_code == $deviceTypeCode))));
	}

	$newForm->AddControl('<strong>IP Address</strong><br />Enter the device IP address to connect to',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'ipaddress', 'value' => ($device) ? $device->ipaddress : NULL, 'size' => '20')));
	$newForm->AddControl('<strong>Description</strong><br />A brief description of the device (e.g. model)',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'descr', 'value' => ($device) ? $device->descr : NULL, 'size' => '45')));

	$s = $newForm->AddControl('<strong>Device Group</strong><br />Select where the device should appear',
		new CSFormControl(CSF_SELECT, array('name' => 'group_id')));
	foreach($groupMenuList as $groupId => $groupName)
	{
		$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $groupId, 'text' => $groupName,
							'selected' => ($device && $device->group_id == $groupId))));
	}

	$s = $newForm->AddControl('<strong>Alert Group</strong><br />Specifies alert settings for this device.',
		new CSFormControl(CSF_SELECT, array('name' => 'alert_id')));
	$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => '0', 'text' => '[No alert group]',
		'selected' => ($device && $device->alert_id == 0))));
	foreach(CSAlert::GetAlertGroupList() as $alert)
	{
		if (is_array($alert))
			$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $alert['alert_id'], 'text' => $alert['name'],
							'selected' => ($device && $device->alert_id == $alert['alert_id']))));
	}

	//
	// SNMP Version
	//

	$s = $newForm->AddControl('<strong>SNMP Version</strong><br />Specifies what version of SNMP to use for this device',
		new CSFormControl(CSF_RADIO, array('name' => 'snmp_v3', 'value' => '0', 'id' => 'snmp_v3_off',
			'checked' => (!$device || $device->snmp_v3 == 0))));
	$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'snmp_v3_off', 'text' => 'SNMP v1/v2 ')));
	$s->AddChild(new CSFormControl(CSF_RADIO, array('name' => 'snmp_v3', 'value' => '1', 'id' => 'snmp_v3_on',
			'checked' => ($device && $device->snmp_v3 == 1))));
	$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'snmp_v3_on', 'text' => 'SNMP v3')));

	//
	// SNMP Community
	//
	$newForm->AddControl('<strong>SNMP Community String</strong><br />SNMP v1/v2 only: specify the SNMP community string',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'snmp_community', 'value' => ($device) ? $device->snmp_community : NULL)));

	// Username
	$newForm->AddControl('<strong>SNMP Username</strong><br />SNMP v3 only: Specify the username',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'snmp_username', 'value' => ($device) ? $device->snmp_username : NULL)));

	// Auth Proto
	$s = $newForm->AddControl('<strong>SNMP Authentication Protocol</strong><br />SNMP v3 only: Specify authentication protocol',
		new CSFormControl(CSF_SELECT, array('name' => 'snmp_authproto')));
	foreach($CSD_V3AUTHS as $v3AuthCode => $v3AuthName)
	{
		$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $v3AuthCode, 'text' => $v3AuthName,
								'selected' => ($device && $device->snmp_authproto == $v3AuthCode))));
	}

	// Auth Pass
	$newForm->AddControl('<strong>SNMP Authentication Password</strong><br />SNMP v3 only: Authentication password',
		new CSFormControl(CSF_PASSWORD, array('name' => 'snmp_authpass', 'size' => '40')));

	// Priv Proto
	$s = $newForm->AddControl('<strong>SNMP Privacy Mode</strong><br />SNMP v3 only: specify your desired level of session privacy',
		new CSFormControl(CSF_SELECT, array('name' => 'snmp_privproto')));
	foreach($CSD_V3PRIVS as $v3PrivCode => $v3PrivName)
	{
		$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $v3PrivCode, 'text' => $v3PrivName,
								'selected' => ($device && $device->snmp_privproto == $v3PrivCode))));
	}

	// Priv Pass
	$newForm->AddControl('<strong>SNMP Privacy Password</strong><br />SNMP v3 only: privacy password',
		new CSFormControl(CSF_PASSWORD, array('name' => 'snmp_privpass', 'size' => '40')));
}

// Define our possible CSForm objects, which are looked for later
$modifyForm = null;
$deleteForm = null;

try {
	if (isset($_GET['submit']))
	{
		CS::Check($_POST['action'], CSA_EMPTY|CSA_THROW, 'DeviceAdmin: submit action not specified');

		if ($_POST['action'] == 'create')
		{
			if (!CS::Check($_POST['hostname'], CSA_EMPTY))
				throw new CSHostnameException();

			if (!CS::Check($_POST['type_code'], CSA_EMPTY))
				throw new CSDeviceTypeException();

			if (!CS::Check($_POST['ipaddress'], CSA_IP4))
				throw new CSIPAddressException();

			if (!CS::Check($_POST['descr'], CSA_EMPTY))
				throw new CSArgumentMissingException('Description');

			if (!CS::Check($_POST['group_id'], CSA_EMPTY))
				throw new CSArgumentMissingException('Device Group');

			if (!CS::Check($_POST['snmp_v3'], CSA_ISSET))
				throw new CSArgumentMissingException('Alert Group');

			if ($_POST['snmp_v3'] == 1)
			{
				if (!CS::Check($_POST['snmp_username'], CSA_EMPTY))
					throw new CSArgumentMissingException('SNMP Username');
			}
			else {
				if (!CS::Check($_POST['snmp_community'], CSA_EMPTY))
					throw new CSArgumentMissingException('SNMP Community');
			}

			$new_id = $CS->getDb()->InsertRecord(
					'INSERT INTO `devices` (`group_id`,`alert_id`,`hostname`,`type_code`,`ipaddress`,`descr`,'.
							'`state_code`,`snmp_community`,`snmp_v3`,`snmp_username`,`snmp_authproto`,'.
							'`snmp_authpass`,`snmp_privproto`,`snmp_privpass`) '.
							'VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
					array($_POST['group_id'], $_POST['alert_id'], $_POST['hostname'], $_POST['type_code'], $_POST['ipaddress'],
						$_POST['descr'], CSD_STATE_UNCHK, $_POST['snmp_community'], $_POST['snmp_v3'],
						$_POST['snmp_username'], $_POST['snmp_authproto'], $_POST['snmp_authpass'],
						$_POST['snmp_privproto'], $_POST['snmp_privpass'])
				);
			header('Location: View.php?device_id='.$new_id);
			exit();
		}
		if ($_POST['action'] == 'modify')
		{
			if (!CS::Check($_POST['device_id'], CSA_EMPTY))
				throw new Exception('DeviceAdmin: modify action with no device_id');

			if (!CS::Check($_POST['hostname'], CSA_EMPTY))
				throw new CSHostnameException();

			if (!CS::Check($_POST['type_code'], CSA_EMPTY))
				throw new CSDeviceTypeException();

			if (!CS::Check($_POST['ipaddress'], CSA_IP4))
				throw new CSIPAddressException();

			if (!CS::Check($_POST['descr'], CSA_EMPTY))
				throw new CSArgumentMissingException('Description');

			if (!CS::Check($_POST['group_id'], CSA_EMPTY))
				throw new CSArgumentMissingException('Device Group');

			if (!CS::Check($_POST['snmp_v3'], CSA_ISSET))
				throw new CSArgumentMissingException('Alert Group');

			if ($_POST['snmp_v3'] == 1)
			{
				if (!CS::Check($_POST['snmp_username'], CSA_EMPTY))
					throw new CSArgumentMissingException('SNMP Username');
			}
			else {
				if (!CS::Check($_POST['snmp_community'], CSA_EMPTY))
					throw new CSArgumentMissingException('SNMP Community');
			}

			$CS->getDb()->UpdateRecord(
						'UPDATE `devices` SET `group_id` = ?, `alert_id` = ?, `hostname` = ?, `type_code` = ?, '.
								'`ipaddress` = ?, `descr` = ?, `snmp_community` = ?, `snmp_v3` = ?, `snmp_username` = ?, '.
								'`snmp_authproto` = ?, `snmp_authpass` = ?, '.
								'`snmp_privproto` = ?, `snmp_privpass` = ? WHERE `device_id` = ?',
						array($_POST['group_id'], $_POST['alert_id'], $_POST['hostname'], $_POST['type_code'], $_POST['ipaddress'],
							$_POST['descr'], $_POST['snmp_community'], $_POST['snmp_v3'], $_POST['snmp_username'],
							$_POST['snmp_authproto'], $_POST['snmp_authpass'],
							$_POST['snmp_privproto'], $_POST['snmp_privpass'], $_POST['device_id'])
					);

			header('Location: View.php?device_id='.$_POST['device_id'].'&updated');
			exit();
		}
		if ($_POST['action'] == 'delete')
		{
			if (!CS::Check($_POST['device_id'], CSA_EMPTY))
				throw new Exception('DeviceAdmin: delete action with no device_id');
			if (!CS::Check($_POST['delete_op'], CSA_EMPTY))
				throw new Exception('DeviceAdmin: delete action with no delete_op');

			if ($_POST['delete_op'] == 'archive')
			{
				$CS->getDb()->UpdateRecord('UPDATE `devices` SET `state_code` = ? WHERE `device_id` = ?',
						array(CSD_STATE_ARCHIVED, $_POST['device_id']));
				header('Location: View.php?device_id='.$_POST['device_id'].'&updated');
				exit();
			}
			elseif ($_POST['delete_op'] == 'delete')
			{
				$CS->getDb()->UpdateRecord('DELETE FROM `revisions` WHERE `device_id` = ?', $_POST['device_id']);
				$CS->getDb()->UpdateRecord('DELETE FROM `devices` WHERE `device_id` = ?', $_POST['device_id']);
				header('Location: CS_Main.php');
				exit();
			}
		}

		throw new Exception('DeviceAdmin: submit action not supported');
	}

	if (!empty($_GET['device_id']))
	{
		$device = new CSDevice($_GET['device_id']);

		if (isset($_GET['disable']))
		{
			$CS->getDb()->UpdateRecord('UPDATE `devices` SET `state_code` = ? WHERE `device_id` = ?',
					array(CSD_STATE_DISABLED, $_GET['device_id']));
			header('Location: View.php?device_id='.$_GET['device_id']);
			exit();
		}
		elseif (isset($_GET['enable']))
		{
			$CS->getDb()->UpdateRecord('UPDATE `devices` SET `state_code` = ? WHERE `device_id` = ?',
					array(CSD_STATE_UNCHK, $_GET['device_id']));
			header('Location: View.php?device_id='.$_GET['device_id']);
			exit();
		}
		elseif (isset($_GET['delete']))
		{
			$deleteForm = new CSForm('Admin.php?submit');
			$deleteForm->AddHidden('action', 'delete');
			$deleteForm->AddHidden('device_id', $device->device_id);
			$deleteForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Submit')));
			$deleteForm->AddButton(new CSFormControl(CSF_BUTTON, array('value' => 'Cancel', 'onclick' => 'history.back(1);')));

			$deleteForm->AddControl('<strong>Selected Device</strong><br />The device you have chosen to perform an action upon',
					new CSFormControl(CSF_TEXT, array('text' => $device->hostname.' ('.$device->ipaddress.')')));
			$s = $deleteForm->AddControl('<strong>Action to Perform</strong><br />Select the action to perform to this device',
					new CSFormControl(CSF_SELECT, array('name' => 'delete_op')));
			$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => 'archive', 'text' => 'Move to Archive')));
			$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => 'delete', 'text' => 'Delete (irreversible!)')));
		}
		else
		{
			// Get our group drop-down options
			$groupMenuList = CSDevice::GetGroupMenuList(CSDevice::GetGroupById(0, FALSE), 0, TRUE);

			$modifyForm = new CSForm('Admin.php?submit');
			$modifyForm->AddHidden('action', 'modify');
			$modifyForm->AddHidden('device_id', $_GET['device_id']);
			$modifyForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Save')));
			$modifyForm->AddButton(new CSFormControl(CSF_RESET, array('value' => 'Reset')));
			$modifyForm->AddButton(new CSFormControl(CSF_BUTTON, array('value' => 'Cancel', 'onclick' => 'history.back(1);')));

			// Build modifyForm with values from $device
			BuildAdminForm($modifyForm, $groupMenuList, $device);
		}
	}
	else
	{
		// Get our group drop-down options
		$groupMenuList = CSDevice::GetGroupMenuList(CSDevice::GetGroupById(0, FALSE), 0, TRUE);

		if (!is_array($groupMenuList) || count($groupMenuList) == 0)
			throw new CSNoDeviceGroupsException();

		$createForm = new CSForm('Admin.php?submit');
		$createForm->AddHidden('action', 'create');
		$createForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Create')));

		// Create an empty form
		BuildAdminForm($createForm, $groupMenuList, NULL);
	}
}
catch (Exception $e) {
	CS::Abort($e);
}

CS::PrintHtmlHeader('Device Admin');

?>

<body id="Main">

<h1>Device Admin</h1>

<?php if (!is_null($modifyForm)): ?>

	<h2>Modify</h2>
	<?php $modifyForm->InsertHtml(); ?>

<?php elseif (!is_null($deleteForm)): ?>

	<h2>Delete</h2>

	<p>You have chosen to delete the device specified below, however it is recommended you archive the device instead.</p>
	<p>An archived device will not be displayed in the Device Menu and will not be polled for configuration changes. However,
	the device will remain in the database, meaning all configuration revisions are available for future reference. The
	device archive is accessible from the Device Menu.</p>

	<p class="InfoMsg">Warning! Deleting a device will also delete any saved configuration revisions. The operation is irreversible.</p>

	<?php $deleteForm->InsertHtml(); ?>

<?php else: ?>

	<h2>Create</h2>
	<?php $createForm->InsertHtml(); ?>

<?php endif; ?>

</body>
</html>
Return current item: Network Configuration Store