<?php
needRole('vhost');

class DbControl extends Control
{
	public function index()
	{
		$vhost = getRole('vhost');
		$user = $_SESSION['user'][$vhost];

		if (!$user) {
			$user = daocall('vhost', 'getVhost', array($vhost));
			$_SESSION['user'][$vhost] = $user;
		}

		$db_ok = ($user['db_quota'] > 0 && $user['db_type'] != 'sqlsrv');
		$used = 0;

		if ($db_ok) {
			$db = apicall('nodes', 'makeDbProduct', array('localhost', $user['db_type']));

			if ($db) {
				$u = $db->used($user['db_name']);

				if ($u !== false && $u !== null) {
					$used = round($u, 2);
				}
			}
		}

		$node = $GLOBALS['node_cfg']['localhost'];
		$db_host = $node['db_host'] ? $node['db_host'] : 'localhost';
		$db_port = $node['db_port'] ? $node['db_port'] : '3306';

		$dbadmin_url = 'http://' . $_SERVER['SERVER_NAME'] . ':3313/mysql/?db=' . $user['db_name'];

		if (is_https()) {
			$dbadmin_url = 'https://' . $_SERVER['SERVER_NAME'] . ':4413/mysql/?db=' . $user['db_name'];
		}

		$this->assign('user', $user);
		$this->assign('db_ok', $db_ok);
		$this->assign('db_used', $used);
		$this->assign('db_host', $db_host);
		$this->assign('db_port', $db_port);
		$this->assign('dbadmin_url', $dbadmin_url);

		return $this->fetch('db/index.html');
	}
}
?>
