Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/inc/CSConfig.class.php
<?php

class CSConfig
{
	private $rec;

	private $parent;
	public function getParent() { return $this->parent; }

	public function __construct($record, &$parent=null)
	{
		global $CS;

		//
		// If the provided record is an array, load that. Otherwise,
		// query the revisions table for a matching record.
		//

		if (is_array($record))
		{
			// We don't force the actual config data to be present in the record. If
			// it is missing in action, GetFile() will re-query the database for it.
			//
			if (!array_key_exists('revision_id', $record) || !array_key_exists('device_id', $record)
			  || !array_key_exists('revision', $record) || !array_key_exists('created', $record))
			{
				throw new Exception('CSConfig: provided recordset is invalid');
			}
			$this->rec = $record;

			if (!is_a($parent, 'CSDevice'))
				throw new Exception('CSConfig: provided parent is not a device object');
			$this->parent =& $parent;
		}
		elseif (empty($record))
		{
			throw new Exception('Invalid revision identifier');
		}
		else
		{
			$this->rec = $CS->getDb()->GetOneRecord('SELECT * FROM `revisions` WHERE `revision_id` = ?', $record);
			if (!$this->rec)
				throw new Exception('The specified revision does not exist');

			// Load the parent object too, as it won't have been passed to us.
			$this->parent = new CSDevice($this->rec['device_id']);
		}
	}
	public function __destruct() { }

	public function __get($field)
	{
		if (array_key_exists($field, $this->rec) && isset($this->rec[$field]))
		{
			switch ($field)
			{
				case 'approx_size':
					return sprintf("%0.2f kb", ($this->rec[$field] / 1024));
				default:
					return $this->rec[$field];
			}
		}

		return null;
	}

	// GetFileName()
	//	Return a pretty file name for this context
	//
	public function GetFileName()
	{
		$fileName = '';

		if (is_a($this->parent, 'CSDevice'))
			$fileName = str_replace(' ', '_', $this->parent->hostname).'-';

		$fileName .= 'config-R'.$this->revision.'.txt';

		return $fileName;
	}

	// GetHeader()
	//	Returns an array of header lines for a config file, appropriately
	//	commented out for the type of device.
	//
	public function GetHeader()
	{
		$headerBuf = array();

		// The standard Cisco comment start is exclamation, but Alteon
		// config files support /* style comments
		//
		$sp = '! ';
		if (is_a($this->parent, 'CSDevice'))
			if ($this->parent->type_code != CSD_TYPE_CISCO)
				$sp = '/* ';

		$headerBuf[] = $sp.str_repeat('-', strlen(CS_TITLE) + 25);
		$headerBuf[] = $sp.'Generated by '.CS_TITLE;
		$headerBuf[] = $sp.str_repeat('-', strlen(CS_TITLE) + 25);

		if (is_a($this->parent, 'CSDevice'))
			$headerBuf[] = $sp.'Device:   '.$this->parent->hostname.' ('.$this->parent->ipaddress.')';

		$headerBuf[] = $sp.'Revision: '.$this->revision;
		$headerBuf[] = $sp.'Created:  '.$this->created;
		$headerBuf[] = $sp;

		return $headerBuf;
	}

	// GetConfig()
	//	Returns the configuration lines in an array with no CRLFs
	//
	public function GetData()
	{
		// If the data isn't here, load it!
		if (is_null($this->data))
		{
			global $CS;

			$rec = $CS->getDb()->GetOneRecord('SELECT `data` FROM `revisions` WHERE `revision_id` = ?', $this->revision_id);
			if (!$rec)
				throw new Exception('CSConfig::GetFileContents() cannot read data for preallocated object');

			$this->rec['data'] = $rec['data'];
		}

		return $this->data;
	}

	// GetConfig()
	//	Returns file header and configuration in a buffer with CRLF line termination.
	//
	public function GetConfig()
	{
		$fileBuf = implode("\r\n", $this->GetHeader());
		$fileBuf .= "\r\n";
		$fileBuf .= str_replace("\n", "\r\n", $this->GetData());

		return $fileBuf;
	}
}

?>
Return current item: Network Configuration Store