<?php
# $Header: /cvsroot/phptsmadmin/phpTSMadmin/lib/functions.tsm.php,v 1.39 2009/04/19 04:03:05 wurley Exp $

/**
 * Classes and functions for TSM server configuration and capability
 *
 * @author The phpTSMadmin development team
 * @package phpTSMadmin
 */

/**
 * Prune the .decimals from the TSM dates.
 * @param string Date
 * @param string Prune key
 * @return string Date in required format
 */
function tsmDate($string,$prune='') {
	switch ($prune) {
		case 'notime' :
			$regexp = '/ [0-9]+:.*$/';
			break;
		case 'nosec' :
			$regexp = '/:[0-9]+\.[0-9]*$/';
			break;
		case 'nomsec' :
			$regexp = '/^([0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+)\..*/';
			break;
		case 'daytime' :
			$regexp = '/^[0-9]+-[0-9]+-([0-9]+ [0-9]+:[0-9]+).*/';
			break;
		case 'monthdaytime' :
			$regexp = '/^[0-9]+-([0-9]+-[0-9]+ [0-9]+:[0-9]+).*/';
			break;
		default :
			$regexp = '/\.[0-9]*$/';
	}
	return (preg_replace($regexp,'$1',$string));
}

/**
 * Translate backup types into meaningful strings.
 * @param string TSM backup string
 * @return string
 */
function tsmBackupType($string) {
	switch ($string) {
		case 'BACKUPINCR' : return 'INCR';
			break;
		case 'BACKUPFULL' : return 'FULL';
			break;
		case 'DBSNAPSHOT' : return 'SNAPSHOT';
			break;
		default : return $string;
			break;
	}
}

function isPrimaryPool($pool) {
	global $app;

	objectCache('stgps');

	return $_SESSION['cache'][$app['server']->getIndex()]['stgps']->isPrimaryPool($pool);
}

function isCopyPool($pool) {
	global $app;

	objectCache('stgps');

	return $_SESSION['cache'][$app['server']->getIndex()]['stgps']->isCopyPool($pool);
}

function getReclaim($pool) {
	global $app;

	objectCache('stgps');

	return $_SESSION['cache'][$app['server']->getIndex()]['stgps']->getReclaim($pool);
}

function getReUse($pool) {
	global $app;

	objectCache('stgps');

	return $_SESSION['cache'][$app['server']->getIndex()]['stgps']->getReUse($pool);
}

function getVolume($vol) {
	global $app;

	objectCache('volumes');

	return $_SESSION['cache'][$app['server']->getIndex()]['volumes']->getVolume($vol);
}

function isLibraryDevClass($devclass) {
	global $app;

	objectCache('devclasses');

	return $_SESSION['cache'][$app['server']->getIndex()]['devclasses']->isLibraryDevClass($devclass);
}

function LibraryDevClass($devclass) {
	global $app;

	objectCache('devclasses');

	return $_SESSION['cache'][$app['server']->getIndex()]['devclasses']->LibraryDevClass($devclass);
}

/**
 * Return the name that a cookie will be stored by the browser.
 * @param int Server ID.
 * @param string Name of Cookie.
 * @return string Fully qualified cookie name.
 */
function cookie_name($index,$name) {
	global $app;

	return $app['server']->getValue('cookie','prefix').$index.$name;
}

/**
 * Returns the value of cookie
 * @param int Server ID
 * @param string Name
 * @return string|false Value of cookie (null if not set/no value)
 */
function get_cookie($index,$name) {
	global $_COOKIE;

	$cookie = cookie_name($index,$name);

	if (isset($_COOKIE[$cookie]))
		return $_COOKIE[$cookie];
	else
		return false;
}

/**
 * Store a cookie in the user browser.
 * @param int Server ID
 * @param string Name of Cookie
 * @param string Value for Cookie
 * @param int Time for cookie to live in seconds
 * @param string dir Cookie Path
 * @result boolean
 */
function store_cookie($index,$name,$val,$expire=null,$dir=null) {
	global $app;

	$cookie_name = cookie_name($index,$name);
	$cookie_time = $app['server']->getValue('cookie','time');

	if ($expire == null)
		$expire = ($cookie_time == 0 ? null : time() + $cookie_time);

	if ($dir == null)
		$dir = dirname($app['server']->getValue('server','path') ? $app['server']->getValue('server','path') : '/');

	if (setcookie($cookie_name,$val,$expire,$dir))
		return true;
	else
		return false;
}

/**
 * Unset all cookies on logout.
 * @param int Server ID
 * @result boolean True if successful, false if not
 * @todo: No routine to call this.
 */
function unset_cookie($index) {
	global $app;
	global $_COOKIE;

	$expire = time()-3600;

	foreach ($_COOKIE as $cookie => $cookie_value) {
		$cookie = preg_replace("/^".$app['server']->getValue('server','path').$index.'/','',$cookie);
		if ($cookie == 'PHPSESSID') continue;
		$result = store_cookie($index,$cookie,'',$expire) ;

		if (! $result) return false;
	}
	return true;
}

/**
 * Initialise JPgraph
 * @param boolean Must initialise or fail with an error if unable to do so
 */
function initJPGraph($need=false) {
	$jpgraph = $_SESSION[APPCONFIG]->getValue('lib','jpgraph'); 

	if (! $jpgraph)
		if ($need)
			die(_('Your JPGRAPH setting in config.php is pointing to a directory that does not exist. Please fix your config.php'));
		else
			return false;

	if ((! file_exists($jpgraph.'/src/jpgraph.php')) || (! file_exists($jpgraph.'/src/jpgraph_gantt.php')))
		if ($need)
			die(sprintf(_('PTA was not able to find the JPGRAPH utility in the "%s" directory. Either your config.php is pointing to the wrong directory, or you havent installed JPGRAPH'),$jpgraph));
		else
			return false;

	if (! function_exists('imagetypes') && ! function_exists('imagecreatefromstring'))
		if ($need)
			die(_('It seems that GD support is not available. Please add GD support to PTA.'));
		else
			return false;

	if (! is_dir($_SESSION[APPCONFIG]->getValue('image','path')) || ! is_writable($_SESSION[APPCONFIG]->getValue('image','path')))
			die(sprintf(_('The temporary path for JPGRAPH either doesnt exist or is not writable - you have it configured for %s.'),$_SESSION[APPCONFIG]->getValue('image','path')));

	# When we get here, we have all the pre-reqs.
	error_reporting(0);
	require_once $jpgraph.'/src/jpgraph.php';
	require_once $jpgraph.'/src/jpgraph_gantt.php';
	require_once $jpgraph.'/src/jpgraph_line.php';
	require_once $jpgraph.'/src/jpgraph_bar.php';
	require_once $jpgraph.'/src/jpgraph_pie.php';
	require_once $jpgraph.'/src/jpgraph_date.php';
	error_reporting(E_ALL);
	return true;
}

/**
 * Put some HTML classes around strings containing values.
 * @param string String to parse
 * @param string Class to substitute
 * @return string String with <span class=$class> around %s
 */
function classValue($string,$class) {
	return preg_replace('/(\%[0-9.]*[sf])/',"<span class=\"$class\">$1</span>$2",$string);
}

function objectCache($object,$force=false) {
	global $app;

	$index = $app['server']->getIndex();

	if (DEBUG_ENABLED)
		debug_log('objectCache(): Entered with (%s,%s)',1,$object,isset($_SESSION['cache'][$index][$object]));

	if (isset($_SESSION['cache'][$index][$object])) {
		if ($_SESSION['cache'][$index][$object]->expired() || $force) {
			if (DEBUG_ENABLED)
				debug_log('objectCache(): Object expired (%s,%s)',1,$_SESSION['cache'][$index][$object]->cache,time());

			$_SESSION['cache'][$index][$object]->load();
			$_SESSION['cache'][$index][$object]->cache = time();
		}

	} else {
		switch($object) {	
			case 'devclasses' :
				$_SESSION['cache'][$index][$object] = new deviceClasses($index);
				break;

			case 'drives' :
				$_SESSION['cache'][$index][$object] = new drives($index);
				break;

			case 'help' :
				$_SESSION['cache'][$index][$object] = new help($index);
				break;

			case 'libraries' :
				$_SESSION['cache'][$index][$object] = new libraries($index);
				break;

			case 'mgmtclasses' :
				$_SESSION['cache'][$index][$object] = new mgmtClasses($index);
				break;

			case 'nodes' :
				$_SESSION['cache'][$index][$object] = new nodes($index);
				break;

			case 'occupancy' :
				$_SESSION['cache'][$index][$object] = new occupancy($index);
				break;

			case 'stgps' :
				$_SESSION['cache'][$index][$object] = new storagePools($index);
				break;

			case 'summaryinfo' :
				$_SESSION['cache'][$index][$object] = new summaryInfo($index);
				break;

			case 'volumes' :
				$_SESSION['cache'][$index][$object] = new volumes($index);
				break;

			default:
				error(sprintf('Unknown object %s',$object));
		}

		$_SESSION['cache'][$index][$object]->cache = time();
	}

	return $_SESSION['cache'][$index][$object];
}

function render_page($title,$body) {
	$www = new page();

	if (is_array($title)) {
		foreach ($title as $key => $title) {
			$block = new block();
			$block->SetTitle($title);
			$block->SetBody($body[$key]);
			$www->block_add('body',$block);
		}

	} else {
		$block = new block();
		$block->SetTitle($title);
		$block->SetBody($body);
		$www->block_add('body',$block);
	}
	$www->body();
}
?>
