<?php  
/**
* Defining database connection
*
* Script is used for "First Steps" and package "Configuration"
* @author Gabriele Droege, DNA Bank Network <contact@dnabank-network.org>
* @version 2.0
* @package Configuration
* @copyright Copyright © 2011 DNA Bank Network http://www.dnabank-network.org<br>The contents of this file are subject to the Mozilla Public License Version 1.1
* @filesource
* @license http://www.mozilla.org/MPL/ MPL
*/

    /********************************************************************************************
    * check, if user is logged in as administrator, if not permission denied                    *
    *********************************************************************************************/
if (!isset($_SESSION['admin']) || !$_SESSION['admin']) {

$sql= "SELECT ID_User FROM user";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
if(mysqli_num_rows($result) > 0) {                        
$TestAdmin = false;
echo "<tr><td colspan='3'><span class='error'>Sorry, you don't have permission for editing connection parameters! Please login as administrator or contact an admin.</span></td></tr>"; }

else
{ $Test = true; }
}

if($Test == "true" or (isset($_SESSION['admin']) || $_SESSION['admin'])) {
?>
	<tr>
		<td colspan="3" valign="top">
		<table border="0 id="table2">
    <?php
    if($_POST['formSubmitSave']) {
    
if($_POST['DBMS'] == "") { echo "<tr><td colspan='2'><span class='error'>Please select a DBMS!</span></td></tr>"; 
$test = true; }
if($_POST['host'] == "") { echo "<tr><td colspan='2'><span class='error'>Please enter the host of the database!</span></td></tr>";
$test = true; }    
if($_POST['database'] == "") { echo "<tr><td colspan='2'><span class='error'>Please select the database!</span></td></tr>"; 
$test = true; }    
if($_POST['user'] == "") { echo "<tr><td colspan='2'><span class='error'>Please enter the user!</span></td></tr>";
$test = true; }    

if($test != true) {

    /********************************************************************************************
    * saving database connection parameters into an xml file (config/provider.xml)                *
    * START                                                                                     *
    *********************************************************************************************/
    
if(file_exists("config/provider.xml"))
{ $file = "config/provider.xml"; } else { $file = "../config/provider.xml"; }

$newfile = fopen($file,"w+");

rewind($newfile);

    $newxml = '<?xml version="1.0" encoding="UTF-8"?>
    <provider>
    <dbms>'.$_REQUEST['DBMS'].'</dbms>
    <host>'.$_REQUEST['host'].'</host>
    <db>'.$_REQUEST['database'].'</db>
    <user>'.$_REQUEST['user'].'</user>
    <pw>'.$_REQUEST['password'].'</pw>
    </provider>'; 

fwrite($newfile, $newxml);
fclose($newfile); }


    }
    /********************************************************************************************
    * saving database connection parameters into an xml file (config/provider.xml)                *
    * END                                                                                       *
    *********************************************************************************************/
    
    if(file_exists("config/loaddnaprovider.php"))
    { 
    /**
    * Start page, try to load exiting xml parser, just in case connection has been set up already, (config/loaddnaprovider.php -> config/provider.xml)
    */
    include ("config/loaddnaprovider.php"); } else { 
    /**
    * Start page, alternative path for xml parser
    */
    include("../config/loaddnaprovider.php");} 
    
    echo "<tr><td id='DescriptionBlack'><b>Connection to database: </b></td><td>"; 
    
    if($DBMS == "mysql") {
    $connID = @($GLOBALS["___mysqli_ston"] = mysqli_connect($host,  $user,  $passwd));
    if ($connID) {    $connection = ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE $dbname"));  
    if($connection) { echo "<span class='fine'>OK</span>"; }
    if(!$connection) { echo "<span class='error'>Not OK</span>"; }}
    
    if(!$connID) { echo "<span class='error'>Not OK</span>"; } }
    echo "</td></tr>";
    ?>
 		<tr>
				<td id="DescriptionBlack">
				DBMS</td>
				<td>
        <select name="DBMS" id="SelectLong">
          <option selected value="mysql">mysql</option>
        </select></td>
        <td>Currently only MySQL can be used as database management system for DNA Module.</td>
    </tr>
    <tr>
				<td id="DescriptionBlack">
				Host</td>
				<td>
        <input name="host" id="SelectLong" type="text" value="<?php echo $host; ?>"/></td>
        <td>The host of the database.</td>
    </tr>
    <tr>
				<td id="DescriptionBlack">
				Database</td>
				<td>
        <input name="database" id="SelectLong" type="text" value="<?php echo $dbname; ?>"/></td>
        <td>The name of the database.</td>
    </tr>
    <tr>
				<td id="DescriptionBlack">
				User</td>
				<td>
        <input name="user" id="SelectLong" type="text" value="<?php echo $user; ?>"/></td>
        <td>User name.</td>
    </tr>
        <tr>
				<td id="DescriptionBlack">
				Password</td>
				<td>
        <input name="password" id="SelectLong" type="password" value="<?php echo $passwd; ?>"/></td>
        <td>Password, can be left empty if not assigned.</td>
    </tr>
	  <tr>
	    <td id="DescriptionBlack"></td>
	    <td colspan="2">
          <input type="submit" value="Save" name="formSubmitSave" id="SelectLong"></td>
    </tr>
</table></td></tr>
<?php
 } // if($Test == "true" or (isset($_SESSION['admin']) || $_SESSION['admin']))
 ?>