<?php
    $json = @file_get_contents('/etc/usm/config.json');
    if (!$json) {
        # use local config for development
        $json = file_get_contents('../../etc/config.json');
    }
    $config = json_decode($json, true);

    $database = $config['database'];
    
	// localhost
	$mySQLServer	= $database['host'];
	$mySQLDBName	= $database['database'];
	$mySQLLogin     = $database['user'];
	$mySQLPassword	= $database['password'];
	$myDBConnStr	= "{$database['driver']}:host=$mySQLServer;dbname=$mySQLDBName";
	
	//$dbType		= 'mysql';
	$pmView		= 1		 ; // can view this thing
	$pmReset	= 2		 ; // can reset this property
	$pmEdit		= 4		 ; // can edit this col
	$pmDelete	= 8		 ; // can delete this object
	$pmLinks	= 16	 ; // TELNET and LINK show up as hyperlinks
	$pmControl	= 32	 ; // can perform system control
	$pmAdmin	= 64	 ; // administer the system
	
	function dbConnect(){
		global $myDBConnStr, $mySQLServer, $mySQLDBName,  $mySQLLogin,  $mySQLPassword; 
		//$db = null;
		//echo "sqlsrv:server=$mySQLServer;Database=$mySQLDBName;uid=$mySQLLogin;PWD=$mySQLPassword";
				
		try { // try to connect to the db
			$db = new PDO($myDBConnStr, $mySQLLogin, $mySQLPassword);
			$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch(PDOException $e) {
			echo 'ERROR: '.$e->getMessage();
			echo '<br>';
			echo $myDBConnStr;
			echo phpinfo();
			echo '<br>';
			echo '<br>';
			die();
		}
		
		return $db;
	}
	
	function dbClose($db){
		$db = null;
	}
	
	function getPermissions($db, $user){
		dbConnect($db);
		$sql = "SELECT PERMISSIONS FROM tblUsers WHERE LOGIN ='".$user."';";
		$sth = $db->prepare($sql);
		$sth->execute();
		$result = $sth->fetch(PDO::FETCH_OBJ);
		return intval($result->PERMISSIONS);
	}
?>
