<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
********************************************************************************/



if( !isset( $install_script ) || !$install_script ){
	die($mod_strings['ERR_NO_DIRECT_SCRIPT']);
}

$web_root = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$web_root = str_replace("/install.php", "", $web_root);
$web_root = "http://$web_root";
$current_dir = str_replace('\install', "", dirname(__FILE__));
$current_dir = str_replace('/install', "", $current_dir);
$current_dir = trim($current_dir);
$setup_db_conn_test_result = -1;

if(!isset($_SESSION['dbConfig_submitted']) || !$_SESSION['dbConfig_submitted']){
	if( is_readable('config.php') ){
		 require_once("config.php");
	}

	// set the form's php var to the loaded config's var else default to sane settings
	$_SESSION['setup_db_host_name']					= empty($sugar_config['dbconfig']['db_host_name'])		? 'localhost'	: $sugar_config['dbconfig']['db_host_name'];
	$_SESSION['setup_db_host_instance']					= empty($sugar_config['dbconfig']['db_host_instance'])		? 'sqlexpress'	: $sugar_config['dbconfig']['db_host_instance'];
	$_SESSION['setup_db_database_name']				= empty($sugar_config['dbconfig']['db_name'])			? 'sugarcrm'	: $sugar_config['dbconfig']['db_name'];
	$_SESSION['setup_db_sugarsales_user']				= empty($sugar_config['dbconfig']['db_user_name'])		? 'sugarcrm'	: $sugar_config['dbconfig']['db_user_name'];
	$_SESSION['setup_db_sugarsales_password']			= empty($sugar_config['dbconfig']['db_password'])		? ''			: $sugar_config['dbconfig']['db_password'];
	$_SESSION['setup_db_sugarsales_password_retype']	= empty($sugar_config['dbconfig']['db_password'])		? ''			: $sugar_config['dbconfig']['db_password'];
	$_SESSION['setup_db_use_mb_demo_data']				= false;
	$_SESSION['setup_db_create_database']				= false;
	$_SESSION['setup_db_drop_tables']					= false;
	$_SESSION['setup_db_pop_demo_data']				= false;
	$_SESSION['setup_db_username_is_privileged']		= true;
	$_SESSION['setup_db_admin_user_name']				= 'root';
	$_SESSION['setup_db_admin_password']				= '';
	$_SESSION['setup_db_create_sugarsales_user']		= false;
}

$validationErr = '';
if( isset($validation_errors) ){
	if( count($validation_errors) > 0 ){
		 $validationErr  = '<div id="errorMsgs">';
		 $validationErr .= "<p>{$mod_strings['ERR_DBCONF_VALIDATION']}</p>";
		 $validationErr .= '<ul>';

		 foreach( $validation_errors as $error ){
				$validationErr .= '<li>' . $error . '</li>';
		 }
		 $validationErr .= '</ul>';
		 $validationErr .= '</div>';
	}
}


// DB split 
$oci8sid = '';
$oci8name = '/>';
$createDbCheckbox = '';
$createDb = (isset($_SESSION['setup_db_create_database']) && !empty($_SESSION['setup_db_create_database'])) ? 'checked="checked"' : '';
$dropCreate = (isset($_SESSION['setup_db_drop_tables']) && !empty($_SESSION['setup_db_drop_tables'])) ? 'checked="checked"' : ''; 
$demoData = (isset($_SESSION['setup_db_pop_demo_data']) && !empty($_SESSION['setup_db_pop_demo_data'])) ? 'checked="checked"' : '';
$mbDemoData = (isset($_SESSION['setup_db_use_mb_demo_data']) && $_SESSION['setup_db_use_mb_demo_data'] == 'yes') ? 'checked="checked"' : '';
$instanceName = '';
if (isset($_SESSION['setup_db_host_instance']) && !empty($_SESSION['setup_db_host_instance'])){
	$instanceName = $_SESSION['setup_db_host_instance'];
}

if($_SESSION['setup_db_type'] == 'oci8') {







} else {
	$dbSplit1 = '
	<tr>
		 <td><span class="required">*</span></td>
		 <td nowrap><b>'.$mod_strings['LBL_DBCONF_HOST_NAME'].'</b></td>
		 <td align="left">
			<input type="text" name="setup_db_host_name" id="defaultFocus" value="'.$_SESSION['setup_db_host_name'].'" />';
			if (isset($_SESSION['setup_db_type']) && $_SESSION['setup_db_type'] =='mssql'){
				$dbSplit1 .= '&nbsp;\&nbsp;<input type="text" name="setup_db_host_instance" id="defaultFocus" value="'.$instanceName.'" />';
			}
		$dbSplit1 .= '</td>
	</tr>';
	
	$dbSplit2 = '<tr><td></td>
		<td nowrap><b>'.$mod_strings['LBL_DBCONF_PRIV_USER_2'].'</b></td>
		<td align="left"><input type="checkbox" class="checkbox" name="setup_db_username_is_privileged" value="yes" onclick="toggleUsernameIsPrivileged();"';
	if(isset($_SESSION['setup_db_username_is_privileged']) && !empty($_SESSION['setup_db_username_is_privileged'])) {
		$dbSplit2 .= 'checked="checked"';
	}
	$dbSplit2 .=	' /></td></tr><tbody id="privileged_user_info"><tr><td><span class="required">*</span></td><td width="50%">
					<b>'.$mod_strings['LBL_DBCONF_PRIV_USER'].'</b><br><em>'.$mod_strings['LBL_DBCONF_PRIV_USER_DIRECTIONS'].'</em></td>
						<td align="left"><input type="text" name="setup_db_admin_user_name" value="'.$_SESSION['setup_db_admin_user_name'].'" /></td></tr>
						<tr><td></td><td nowrap><b>'.$mod_strings['LBL_DBCONF_PRIV_PASS'].'</b></td><td align="left"><input type="password" name="setup_db_admin_password" value="'.$_SESSION['setup_db_admin_password'].'" /></td></tr></tbody>';
	
	$dbUser = '<input type="checkbox" class="checkbox" name="setup_db_create_sugarsales_user" onclick="togglePasswordRetypeRequired();" value="yes"';
	if(isset($_SESSION['setup_db_create_sugarsales_user']) && !empty($_SESSION['setup_db_create_sugarsales_user'])) {
		$dbUser .= 'checked="checked"';
	}
	$dbUser .= " /> <b>{$mod_strings['LBL_DBCONF_CREATE_USER']}</b>";
	
	$createDbCheckbox = "<input type='checkbox' class='checkbox' name='setup_db_create_database' onclick='toggleDropTables();' value='yes' {$createDb} /> <b>{$mod_strings['LBL_DBCONF_CREATE_DB']}</b>";
}




///////////////////////////////////////////////////////////////////////////////
////	BEGIN PAGE OUTPUT

$out =<<<EOQ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta http-equiv="Content-Script-Type" content="text/javascript">
	<meta http-equiv="Content-Style-Type" content="text/css">
	<title>{$mod_strings['LBL_WIZARD_TITLE']} {$next_step}</title>
	<link rel="stylesheet" href="install/install.css" type="text/css" />
	<script type="text/javascript" src="install/installCommon.js"></script>
	<script type="text/javascript" src="install/dbConfig.js"></script>
	<script type="text/javascript">
		function showMb() {
			var mb1 = document.getElementById("mbTd1");
			var mb2 = document.getElementById("mbTd2");
			
			if(document.getElementById("demoData").checked == true) {
				mb1.style.display = "";
				mb2.style.display = "";
			} else {
				mb1.style.display = "none";
				mb2.style.display = "none";
			}
		}
	</script>
</head>
EOQ;
$out .= '<body onload="';
if(!isset($_SESSION['oc_install']) || $_SESSION['oc_install'] == false){
    $out .= 'showMb();';   
}
$out .= 'toggleDropTables();togglePasswordRetypeRequired();toggleUsernameIsPrivileged();document.getElementById(\'defaultFocus\').focus();">';

$out2 =<<<EOQ2
<form action="install.php" method="post" name="setConfig" id="form">
<table cellspacing="0" cellpadding="0" border="0" align="center" class="shell">
      <tr><td colspan="2" id="help"><a href="{$help_url}" target='_blank'>{$mod_strings['LBL_HELP']} </a></td></tr>
    <tr>
      <th width="500">
		<p><img src="include/images/sugar_md.png" alt="SugarCRM" border="0"></p>
		{$mod_strings['LBL_STEP']} {$next_step}: {$mod_strings['LBL_DBCONF_TITLE']}</th>
	<th width="200" style="text-align: right;"><a href="http://www.sugarcrm.com" target="_blank"><IMG src="include/images/sugarcrm_login.png" width="145" height="30" alt="SugarCRM" border="0"></a></th>
</tr>
<tr>
	<td colspan="2">
		 <p>{$mod_strings['LBL_DBCONF_INSTRUCTIONS']}</p>
		 {$validationErr}
<div class="required">{$mod_strings['LBL_REQUIRED']}</div>
<table width="100%" cellpadding="0" cellpadding="0" border="0" class="StyleDottedHr">
<tr><th colspan="3" align="left">{$mod_strings['LBL_DBCONF_TITLE']}</td></tr>

{$dbSplit1}

<tr><td><span class="required">*</span></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_NAME']} {$oci8sid}</b></td>
	<td nowrap align="left">
		 <input type="text" name="setup_db_database_name"  value="{$_SESSION['setup_db_database_name']}"
		 {$oci8name}
		 {$createDbCheckbox}
	</td>
</tr>
<tr>
	<td><span class="required">*</span></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_USER']}</b></td>
	<td nowrap align="left">
		 <input type="text" name="setup_db_sugarsales_user" maxlength="16" value="{$_SESSION['setup_db_sugarsales_user']}" />
		 {$dbUser}
	</td>
</tr>
<tr>
	<td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_PASSWORD']}</b></td>
	<td nowrap align="left"><input type="password" name="setup_db_sugarsales_password" value="{$_SESSION['setup_db_sugarsales_password']}" /></td></tr>
<tbody id="password_retype_required">
<tr><td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_PASSWORD2']}</b></td>
	<td nowrap align="left">
		<input type="password" name="setup_db_sugarsales_password_retype" value="{$_SESSION['setup_db_sugarsales_password_retype']}" /></td></tr>
</tbody>
<tr>
	<td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_DROP_CREATE']}</b><br>
	<i>{$mod_strings['LBL_DBCONF_DB_DROP_CREATE_WARN']}</i></td>
	<td nowrap align="left"><input type="checkbox" class="checkbox" name="setup_db_drop_tables" value="yes" {$dropCreate} /></td></tr>
EOQ2;
$out3 =<<<EOQ3
<tr>
	<td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DEMO_DATA']}</b></td>
	<td nowrap align="left">
		<input type="checkbox" class="checkbox" id="demoData" name="setup_db_pop_demo_data" value="yes" {$demoData} onClick="showMb();" />
	</td>
</tr>
<tr>
	<td></td>
	<td nowrap id="mbTd1" style="display:none;">
		<b>{$mod_strings['LBL_DBCONF_MB_DEMO_DATA']}</b><br>
	</td>
	<td nowrap id="mbTd2" align="left">
		<input type="checkbox" class="checkbox" id="mbCheckbox" name="setup_db_use_mb_demo_data" value="yes" {$mbDemoData} />
	</td>
</tr>
EOQ3;
$out4 =<<<EOQ4
	{$dbSplit2}
</table>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<hr>
	 <input type="hidden" name="current_step" value="{$next_step}">
	 <table cellspacing="0" cellpadding="0" border="0" class="stdTable">
		<tr>
			<td><input class="button" type="button" onclick="window.open('http://www.sugarcrm.com/forums/');" value="{$mod_strings['LBL_HELP']}" /></td>
			<td>
				<input class="button" type="button" name="goto" value="{$mod_strings['LBL_BACK']}" onclick="document.getElementById('form').submit();" />
	            <input type="hidden" name="goto" value="{$mod_strings['LBL_BACK']}" />
			</td>
			<td>
				<input class="button" type="submit" name="goto" id="defaultFocus" value="{$mod_strings['LBL_NEXT']}" />
			</td>
		</tr>
	 </table>
</td>
</tr>
</table>
</form>
<br>
</body>
</html>
EOQ4;

echo $out.$out2;



    echo $out3;



echo $out4;


////	END PAGE OUTPUT
///////////////////////////////////////////////////////////////////////////////

?>
