<?php
/* 
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare 
	
	Released under the GNU General Public License 
*/
  $www_location=isset($HTTP_POST_VARS['HTTP_WWW_ADDRESS'])?$HTTP_POST_VARS['HTTP_WWW_ADDRESS']:'';
  $web_service=isset($HTTP_POST_VARS['webservice'])?$HTTP_POST_VARS['webservice']:'0';
  $search_engine=isset($HTTP_POST_VARS['search_engine'])?$HTTP_POST_VARS['search_engine']:'N';
  $sample_data=isset($HTTP_POST_VARS['sample_data'])?$HTTP_POST_VARS['sample_data']:'N';
  $db_error="";
  if ($www_location==""){
	  $www_location = 'http://' .  $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
	  $www_location = substr($www_location, 0, strpos($www_location, 'install'));
  }
  if (osc_in_array('database', $HTTP_POST_VARS['install']) && isset($HTTP_POST_VARS["sell"])) {
    $db = array();
    $db['DB_SERVER'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER']));
    $db['DB_SERVER_USERNAME'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_USERNAME']));
    $db['DB_SERVER_PASSWORD'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_PASSWORD']));
    $db['DB_DATABASE'] = trim(stripslashes($HTTP_POST_VARS['DB_DATABASE']));
      
    osc_db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);
    
	if (!osc_db_select_db($db['DB_DATABASE'])){
		$db_error=mysql_error();
	}
	if($search_engine=='Y' && !$db_error){ 
	  	$current_dir=$HTTP_POST_VARS['DIR_FS_DOCUMENT_ROOT'];
		$root_dir = substr(dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME']),0,strlen(dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME']))-7);
		if($current_dir=='') $current_dir=$root_dir;
		$error_text .="Search Engine Friendly URLs will only work if this directory:<br>".$root_dir."tmp <br>is created and writeable by the webserver.";
		$slashes="/";
		if(strpos("\\",$current_dir)) $slashes="\\";	  		  	
	  	$current_dir_urlcache=$current_dir."tmp".$slashes; 
	  	$update_query=array('SEARCH_ENGINE_FRIENDLY_URLS'=>'true','USE_CACHE'=>'true','DIR_FS_CACHE'=>$current_dir_urlcache,'SESSION_FORCE_COOKIE_USE'=>'true');
	  	if(file_exists($current_dir_urlcache) && !is_writable($current_dir_urlcache)) $db_error=$error_text;
	   	else if(!file_exists($current_dir_urlcache)) {
	   	   if(!is_writable($current_dir)) $db_error=$error_text;
	   	   else mkdir($current_dir_urlcache); 
	   	}
	   	if(!$db_error)
	   		while(list($key,$val)=each($update_query))
	  	 		osc_db_query("UPDATE configuration set configuration_value='".  $update_query[$key] ."' where configuration_key='".$key."';");
    }  
	// update the hide menu details in the configuaration
	$check_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='ENABLE_WEB_SERVICE'");
	if (osc_db_num_rows($check_query)>0){
		osc_db_query("UPDATE configuration set configuration_value='".  $web_service ."' where configuration_key='ENABLE_WEB_SERVICE'");
	} else {
		osc_db_query("INSERT INTO configuration(configuration_title,configuration_key,configuration_description,configuration_group_id,set_function,configuration_value) VALUES('Activate the webservice','ENABLE_WEB_SERVICE','To Enable or Disable the webservice','901','tep_cfg_email_option(','" . $web_service . "')");
	}
	if (!$db_error){
		$sell=$HTTP_POST_VARS["sell"];
		$encrypt_value=isset($HTTP_POST_VARS["encrypt_value"])?$HTTP_POST_VARS["encrypt_value"]:'';
		$sell_items="";
		for ($icnt=0,$n=count($sell);$icnt<$n;$icnt++){
			$sell_items.=$sell[$icnt];
		}
		if ($sell_items=="") $sell_items="P";
		if (!osc_db_query("UPDATE configuration set configuration_value='" . $sell_items . "' where configuration_key='SHOP_SELL_ITEMS'")){
			$db_error=mysql_error();
		}
		///==============alert message-------------
		$email_address='';
		if($HTTP_POST_VARS["email_alert"]=="1")
		{
			$email_alert='true';
			$email_address=$HTTP_POST_VARS["email_address"];
		}
		else
			$email_alert='false';
		$check_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='EMAIL_SECURITY_ALERTS'");
		if (osc_db_num_rows($check_query)>0){
			osc_db_query("UPDATE configuration set configuration_value='".  $email_alert ."' where configuration_key='EMAIL_SECURITY_ALERTS'");
		} else {
			osc_db_query("INSERT INTO configuration(configuration_title,configuration_key,configuration_description,configuration_group_id,set_function,configuration_value) VALUES ('Email me when there are security alerts.','EMAIL_SECURITY_ALERTS','Email me when there are security alerts.',917,'tep_cfg_email_alerts(','".$email_alert. "')");
		}
		//==email address
		$check_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='SECURITY_ALERT_EMAIL_ADDRESS'");
		if (osc_db_num_rows($check_query)>0)
			osc_db_query("UPDATE configuration set configuration_value='" . $email_address . "' where configuration_key='SECURITY_ALERT_EMAIL_ADDRESS'");
		else
			osc_db_query("INSERT INTO configuration(configuration_title,configuration_key,configuration_description,configuration_group_id,configuration_value) VALUES ('security alert email address','SECURITY_ALERT_EMAIL_ADDRESS','alert email address',0,'" . $email_address. "')");
		//===upgrade type
		$check_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='UPGRADE_NOTICE_TYPE'");
		if (osc_db_num_rows($check_query)>0)
			osc_db_query("UPDATE configuration set configuration_value='" . $HTTP_POST_VARS["upgrade_type"] . "' where configuration_key='UPGRADE_NOTICE_TYPE'");
		else
			osc_db_query("INSERT INTO configuration(configuration_title,configuration_key,configuration_description,configuration_group_id,use_function,set_function,configuration_value) VALUES ('What type of upgrade notices do you want to receive?','UPGRADE_NOTICE_TYPE','freeway upgrade version alert type',917,'tep_get_upgrade_alert_text','tep_cfg_upgrade_alerts(','". $HTTP_POST_VARS["upgrade_type"]."')");
		//=================================
		$config_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='ENCRYPTION_HASH_VALUE'");
		if ($encrypt_value!='' && osc_db_num_rows($config_query)<=0){
		  osc_db_query("INSERT INTO configuration VALUES (NULL,'Encryption Hash Value','ENCRYPTION_HASH_VALUE','" . mysql_escape_string(stripslashes($encrypt_value)) ."','Encryption Hash Value applied in cookies for storage of password',1,100,'','','','');");
		}
	}
	if (!$db_error && $sample_data=='Y'){
		osc_db_select_db($db['DB_DATABASE']);
		$installed_query=osc_db_query("SELECT file_name from sql_queries where file_name='freeway_sample_data.sql'");
		$categories_query=osc_db_query("SELECT * from categories limit 1");
		if (osc_db_num_rows($installed_query)<=0 && osc_db_num_rows($categories_query)<=0){			  $script_filename = getenv('PATH_TRANSLATED');
		
			  if (empty($script_filename)) {
				$script_filename = getenv('SCRIPT_FILENAME');
			  }
			  if (empty($script_filename)) {
				$script_filename = $_SERVER["SCRIPT_FILENAME"];
			  }
		
			  $script_filename = str_replace('\\', '/', $script_filename);
			  $script_filename = str_replace('//', '/', $script_filename);
		
			  $dir_fs_www_root_array = explode('/', dirname($script_filename));
			  $dir_fs_www_root = array();
			  for ($i=0, $n=sizeof($dir_fs_www_root_array)-1; $i<$n; $i++) {
				$dir_fs_www_root[] = $dir_fs_www_root_array[$i];
			  }
			  $dir_fs_www_root = implode('/', $dir_fs_www_root) . '/';
				
			  if (file_exists($dir_fs_www_root . "install/freeway_sample_data.sql")){
				osc_db_install($db['DB_DATABASE'], $dir_fs_www_root . "install/freeway_sample_data.sql");
			  } else {
				$db_error="freeway_sample_data.sql not found in install folder";
			  }
		}
	}
    if ($db_error != false) {
	  	$current_step="config";
?>

<table width="100%" cellpadding="0" cellspacing="0">
<form name="install" action="install.php?step=8" method="post">
	<tr>
		<td>
		<?php echo osc_draw_box_start(300);?>
		<table width="95%" border="0" cellpadding="2" class="formPage">
		  <tr>
			<td>
			  <p>The following error has occurred:</p>
			   <p class="boxme"><?php echo $db_error; ?></p>
			</td>
		  </tr>
		</table>
		<?php echo osc_draw_box_end();?>
		</td>
	</tr>
	<tr>
		<td>
		<?php
			  reset($HTTP_POST_VARS);
			  while (list($key, $value) = each($HTTP_POST_VARS)) {
				if (($key != 'x') && ($key != 'y') && ($key != 'DB_TEST_CONNECTION')) {
				  if (is_array($value)) {
					for ($i=0; $i<sizeof($value); $i++) {
					  echo osc_draw_hidden_field($key . '[]', $value[$i]);
					}
				  } else {
					echo osc_draw_hidden_field($key, $value);
				  }
				}
			  }
		?>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
			<td><a href="index.php"><img src="images/button_cancel.gif" border="0" alt="Cancel"></a></td>
			<td align="right"><input type="image" src="images/button_retry.gif" border="0" alt="Retry"></td>
		  </tr>
		</table>
		</td>
	</tr>
</form>
</table>
<?php
    } else {
		echo '<html><body>';
		echo '<form action="install.php?step=9" method="post" name="frmConfig">';
		  reset($HTTP_POST_VARS);
		  while (list($key, $value) = each($HTTP_POST_VARS)) {
			if (($key != 'x') && ($key != 'y') && ($key != 'DB_TEST_CONNECTION')) {
			  if (is_array($value)) {
				for ($i=0; $i<sizeof($value); $i++) {
				  echo osc_draw_hidden_field($key . '[]', $value[$i]);
				}
			  } else {
				echo osc_draw_hidden_field($key, $value);
			  }
			}
		  }
		echo '</form>';
		echo '</body></html>';
		echo '<script language="javascript">document.frmConfig.submit();</script>';
		exit;
?>
		<p><b>Configure Freeway</b></p>
		<table width="95%" border="0" cellpadding="2" class="formPage">
		  <tr>
			<td>
			  <p>The Configuration was <b>successful!</b></p>
			  <p></p>
			  <p>The Login Settings for Administration tool are <b>Username</b>: admin@freeway.com&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Password</b>:admin </p>
			</td>
		  </tr>
		</table><p>
		<table border="0" cellspacing="5" cellpadding="0" align="center">
		  <tr>
			<td align="center"><a href="<?php echo $www_location . 'index.php'; ?>" target="_blank"><img src="images/button_catalog.gif" border="0" alt="Catalog"></a></td>
			<td align="center"><a href="<?php echo $www_location . 'admin/index.php'; ?>" target="_blank"><img src="images/button_administration_tool.gif" border="0" alt="Administration Tool"></a></td>
		  </tr>
		</table>
<?php
		} // db_error
		return;
	} // update
	if (osc_in_array('database', $HTTP_POST_VARS['install'])){
		// fetch ENCRYPTION_VALUE from configuration table if empty
		$show_encrypt=false;
		
	    $db = array();
		$db['DB_SERVER'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER']));
		$db['DB_SERVER_USERNAME'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_USERNAME']));
		$db['DB_SERVER_PASSWORD'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_PASSWORD']));
		$db['DB_DATABASE'] = trim(stripslashes($HTTP_POST_VARS['DB_DATABASE']));
	  
		osc_db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);
	
		if (!osc_db_select_db($db['DB_DATABASE'])){
			$db_error=mysql_error();
		}
		if (!$db_error){
			$config_query=osc_db_query("SELECT configuration_value from configuration where configuration_key='ENCRYPTION_HASH_VALUE'");
			if (osc_db_num_rows($config_query)<=0){
				$show_encrypt=true;
			}
		}
	  	$current_step="config";
?>
		<script language="javascript">
			function checkSell(item){
			
				if (item.checked==false){
					var selected=false;
					var sell=document.install.elements["sell[]"];
					for (icnt=0;icnt<sell.length;icnt++){	
						if (sell[icnt].checked) {
							selected=true;
							break;
						}
					}
					if (!selected) item.checked=true;
				}
			}

			function doAction(){
				error="";
				<?php if ($show_encrypt) {?>
				if (str_trim(document.install.encrypt_value.value)=="") error="Encryption Hash Value cannot be empty\n";
				else if (str_trim(document.install.encrypt_value.value).length<6) error="Encryption Hash value must have atleast 6 chars\n";
				<?php } ?>
				if(document.install.email_alert.checked)
				{
					if(document.install.email_address.value=="")
						error+="Email Address is required.\n";
					else
					{
						if(document.install.email_address.value!=document.install.cemail_address.value)
							error+="Email Address does not match with confirm Email.\n";
					}
				}
				if (error!=""){
					alert(error);
					return;
				}
				document.install.submit();
			}
			 function str_trim(str){  
				if(!str || typeof str != 'string')  
					return " ";  
				return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
			 } 
		</script>
<table width="100%" border="0" cellpadding="0">
<form name="install" action="install.php?step=8" method="post">
	<tr>
		<td>
		<?php echo osc_draw_box_start(480);?>
		<table width="100%" border="0" cellpadding="5" class="formPage">
			<tr>
				<td>
					<?php echo osc_title_bar('What do you want to sell with Freeway?');?>
				</td>
			</tr>
			<tr>
				<td style="padding-left:20px;"><b>
					<input type="checkbox" name="sell[]" value="P" checked onclick="javascript:checkSell(this)" class="checkbox">&nbsp;Products<br>
					<input type="checkbox" name="sell[]" value="E" checked onclick="javascript:checkSell(this)" class="checkbox">&nbsp;Events<br>
					<input type="checkbox" name="sell[]" value="S" checked onclick="javascript:checkSell(this)" class="checkbox">&nbsp;Subscriptions<br>
					<input type="checkbox" name="sell[]" value="V" checked onclick="javascript:checkSell(this)" class="checkbox">&nbsp;Services<br></b>
				</td>
			</tr>
			<tr>
			<td  style="line-height:25px;padding-left:20px;">
			<div class="smallText" style="text-indent:-21px;padding-left:20px;">
			<img src="images/setting_warning.gif" align="absmiddle"/>&nbsp;For example if you are only going to sell products, just tick that. You will only see products features in the Admin area. You can easily change your selection later and add new sales features.
			</div>
			</td>
			</tr>
			<tr>
				<td style="padding-left:20px;">
					<input type="checkbox" name="sample_data" value="Y" class="checkbox" checked="checked" class="checkbox"><b>&nbsp;Install Sample Sale Items </b>
				</td>
			</tr>
			<tr>
				<td  style="line-height:25px;padding-left:20px;">
				<div class="smallText" style="text-indent:-21px;padding-left:20px;">
					<img src="images/setting_warning.gif" align="absmiddle"/>&nbsp;You can delete sale items en masse in Shop Settings > Advanced > Delete all items
				</div>
				</td>
			</tr>	
			<tr>
				<td><?php echo osc_title_bar('Alert Messaging');?></td>
			</tr>	
						<Tr><td style="line-height:25px;padding-left:20px;"><b>What type of upgrade notices do you want to receive?</b><br>
			<input type="radio" name="upgrade_type" value="n" checked>I am new to Freeway, just tell me about major releases.<br>
			<input type="radio" name="upgrade_type" value="d">I am a developer, alert me when there are new Subversion revisions.
			</td></Tr>
			<tr>
				<td style="line-height:25px;padding-left:20px;"><input type="checkbox" name="email_alert" value="1" id="email_alert" onclick="javascript:if(this.checked && document.getElementById('email_alert_div')) document.getElementById('email_alert_div').style.display=''; else document.getElementById('email_alert_div').style.display='none';"><b>Email me when there are security alerts.</b></td>
			</tr>
			<tr id="email_alert_div" style="display:none">
				<td style="line-height:25px;padding-left:20px;">
					<b>&nbsp;&nbsp;&nbsp;&nbsp;Email Address : </b><input type="text" name="email_address"><br>
					<b>Confirm Address : </b><input type="text" name="cemail_address"><br>
				</td>
			</tr>
	
			<tr>
				<td><?php echo osc_title_bar('Advanced');?></td>
			</tr>
			<tr>
				<td style="line-height:25px;padding-left:20px;">
					<b>Do you want to install the Freeway Webservice to allow 3rd party transactions?</b><br />
					<input type="radio" name="webservice" value="1" class="radio">Yes&nbsp;<input type="radio" name="webservice" value="0" class="radio" checked="checked">No
					<br><br>
					<div class="smallText" style="text-indent:-21px;padding-left:20px;">
					<img src="images/setting_warning.gif" align="absmiddle"/>&nbsp;This webservice must be installed securely to prevent possible hacking of your installation. Please read the installation manual thoroughly before use.
					</div>
				</td>
			</tr>
			<?php if ($show_encrypt) { ?>
			<tr>
				<td>
					Encryption Hash Value
					<input type="text" name="encrypt_value" value="<?php echo time();?>" size="20">
					<br>
					<div class="smallText" style="padding-left:10px">
					<font color="red">
						Encryption Hash Value applied in cookies for storage of password
					</font>
					</div>
				</td>
			</tr>
	<?php 
			}
			  reset($HTTP_POST_VARS);
			  while (list($key, $value) = each($HTTP_POST_VARS)) {
				if (($key != 'x') && ($key != 'y') && ($key != 'DB_TEST_CONNECTION')) {
				  if (is_array($value)) {
					for ($i=0; $i<sizeof($value); $i++) {
					  echo osc_draw_hidden_field($key . '[]', $value[$i]);
					}
				  } else {
					echo osc_draw_hidden_field($key, $value);
				  }
				}
			  }
?>
		<tr>
				<td style="line-height:25px;padding-left:20px;">
					<b><input type="checkbox" name="search_engine" value='Y' id="search_engine" onclick="javascript:if(this.checked && document.getElementById('sengine_div')) document.getElementById('sengine_div').style.display=''; else document.getElementById('sengine_div').style.display='none';">Do you want to use Search Engine Friendly URLs?</b>									
				</td>
			</tr>
			<tr id="sengine_div" style="display:none">
			  <td style="line-height:25px;padding-left:20px;"><div class="smallText" style="text-indent:-21px;padding-left:20px;"><img src="images/setting_warning.gif" align="absmiddle"/>&nbsp;"For Search Engine Friendly URLs to work, please ensure your urlcache directory is writeable and add the following to your .htaccess file or Apache config:<br>       
      			&nbsp;&nbsp;&nbsp;&nbsp;RewriteEngine on<br> 
      			&nbsp;&nbsp;&nbsp;&nbsp;RewriteCond %{REQUEST_FILENAME} !-f <br> 
      			&nbsp;&nbsp;&nbsp;&nbsp;RewriteCond %{REQUEST_FILENAME} !-d <br>
      			&nbsp;&nbsp;&nbsp;&nbsp;RewriteRule ^(.*) index.php" </div></td>
			</tr>
		</table>
		<?php echo osc_draw_box_end();?>
		</td>
	</tr>
	<tr>
		<td>
		<table border="0" cellspacing="5" cellpadding="0" width="100%">
		  <tr>
			<td>
				<a href="index.php"><img src="images/button_cancel.gif" border=0 alt="Cancel"></a>&nbsp;
			</td>
			<td align="right">
				<a href="javascript:doAction()"><img src="images/button_continue.gif" border=0 alt="Continue"></a>
			</td>
		  </tr>
		</table>
		</td>
	</tr>
  <form>
</table>
<?php
	} 
?>
<script language="javascript">
	if(document.getElementById("search_engine") && document.getElementById("search_engine").checked && document.getElementById('sengine_div')) 
		document.getElementById('sengine_div').style.display=''; 
	else 
		document.getElementById('sengine_div').style.display='none'		
	if(document.getElementById("email_alert") && document.getElementById("email_alert").checked && document.getElementById('email_alert_div')) 
		document.getElementById('email_alert_div').style.display=''; 
	else 
		document.getElementById('email_alert_div').style.display='none'		
</script>