<?php
/************************************************************************
	UebiMiau is a GPL'ed software developed by 
	 - Aldoir Ventura - aldoir@users.sourceforge.net
	 - http://uebimiau.sourceforge.net São Paulo - Brasil
   		
	***********************************************************************
	Version 3.2.0 Upgrades and templates developed by
	 - Todd Henderson - Lead Developer - http://tdah.us 
	 - Dave Rodgers - Developer - http://www.manvel.net 
	 - Laurent (AdNovea) - Developer - http://adnovea.free.fr
	 
	***********************************************************************
	 - Special thanks to the developers at 
	codeworxtech.com, tinymce.moxiecode.com
	telaen.org, phptoys.com, winged.info, ngcoders.com
	without help this project wouldn't be possible 
	
	***********************************************************************
	- File:			install5.php
	- Developer: 	Todd Henderson / Mods by Laurent (AdNovea)
	- Date:			November 4, 2008
	- version:		(3.2.0) 1.0
	- Description:  Second stage in the installation

*************************************************************************/

	// This just converts global variables into local variables
	foreach($HTTP_POST_VARS as $postvar => $postval){ ${$postvar} = $postval; }
	foreach($HTTP_GET_VARS as $getvar => $getval){ ${$getvar} = $getval; }

?>     

	<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0">
		<tr>
			<td colspan="2"><div align="left" class="f8"><font color="#CCCCCC"><font color="#999999"><?php echo _STG5_CONFIG_EXP;?></font></font></div></td>
		</tr>
<?php

$out1 = sprintf("<?php
// This file has been automatically created during the installation

#_Default_Server_Settings_#

  \$company = '%s';
	\$allow_user_change_language = %s;
	\$default_language = '%s';
	\$allow_user_change_theme = %s;
	\$default_theme = '%s';
	\$check_first_login = %s;
	\$mail_use_top = %s;
	\$add_signature = %s;
	\$default_sortby = '%s';
	\$default_sortorder = '%s';
	\$enable_debug = %s;
	\$default_char_set = '%s';

#_Mail_Settings_#

	\$mail_server_type = '%s';
	
	#_One-for-each see config.mail.php for extra servers_#
	
	#_One-for-All_#
	\$default_mail_server = '%s';
	\$default_protocol= '%s';
	\$default_port = %s;
	
#_Smtp_Settings_#

	\$mailer_type = '%s';
	\$smtp_server = '%s';
	\$smtp_port = %s;
	\$use_password_for_smtp = %s;
	\$smtp_static_auth = %s;
	\$smtp_static_user = '%s';
	\$smtp_static_password ='%s';
	\$smtp_secure = '%s';

#_Security_Settings_#

	\$quota_limit = %s;
	\$allow_html = %s;
	\$allow_modified_from = %s;
	\$allow_scripts = %s;
	\$allow_filters = %s;
	\$block_external_images = %s;
	\$require_same_ip = yes;
	\$idle_timeout = %s;

#_Default_User_Preferences_#

	\$default_preferences = Array(
	'send_to_trash_default'=> yes,		
	'st_only_ready_default'=> no,	
	'save_to_sent_default'=> yes,		
	'empty_trash_default'=> no,		
	'rpp_default'=> 22,		
	'add_signature_default'=> no,		
	'signature_default'=> '',		
	'timezone_default'=> '%s',	
	'display_images_default'=> yes,		
	'editor_mode_default'=> 'html',	
	'refresh_time_default'=> 1
	);



?>" 
,$ccompany,$callow_user_change_language,
$cdefault_language,$callow_user_change_theme,$cdefault_theme,$ccheck_first_login,
$cmail_use_top,$cadd_signature,$cdefault_sortby,$cdefault_sortorder,$cenable_debug,
$cdefault_char_set,$cmail_server_type,$cdefault_mail_server,$cdefault_protocol,$cdefault_port,$cmailer_type,$csmtp_server,$csmtp_port,$cuse_password_for_smtp,
$csmtp_static_auth,$csmtp_static_user,$csmtp_static_password,$csmtp_secure,$cquota,$callow_html,
$callow_modified_from,$callow_scripts,$callow_filters,$cblock_external_images,$cidle_timeout,$ctimezone);

	//create database files and folders		
	
	{if (!file_exists($absolute_path.$temporary_directory.'chat')) 
	mkdir($absolute_path.$temporary_directory.'chat');
		else ("bad permissions");}
		
		{if (!file_exists($absolute_path.$temporary_directory.'chat/upload/')) 
	mkdir($absolute_path.$temporary_directory.'chat/upload/');
		else ("bad permissions");}
		
		{if (!file_exists($absolute_path.$temporary_directory.'chat/online.ucf'))
	fopen($absolute_path.$temporary_directory.'chat/online.ucf',"a+");
		else ("bad permissions");}
		
		{	if (!file_exists($absolute_path.$temporary_directory.'system_admin/chatdata.ucf'))
	fopen($absolute_path.$temporary_directory.'chat/chatdata.ucf',"a+");
		else ("bad permissions");}
			
			{if (!file_exists($absolute_path.$temporary_directory.'system_admin/log.ucf'))
	fopen($absolute_path.$temporary_directory.'system_admin/log.ucf',"a+");
	else ("bad permissions");}
		
	// Backup previous config 
	$file = $cwd.CFG_CONFIG;
	if (file_exists ("$file.bak")) unlink("$file.bak");
	copy($file,"$file.bak");
	
	$file = $cwd.CFG_MAIL;
	if (file_exists ("$file.bak")) unlink("$file.bak");
	copy($file,"$file.bak");
	
		$file = $cwd.CFG_PATHS;
	if (file_exists ("$file.bak")) unlink("$file.bak");
	copy($file,"$file.bak");
	
	// Save config.php file
	$write_error1 = false;
	$fp = fopen($absolute_path.CFG_CONFIG,"w");
	if ($fp == null)  {
		$write_error1 = true;
	} else {
		fwrite($fp,$out1,strlen($out1));
		fclose($fp);
	}
	
if ($cmail_server_type =='ONE-FOR-EACH') {
	$out2 =  "<?php
// This file has been automatically created during the installation
\$one_for_all_login_type	= '%user%@%domain%';\n\n";

	$tmp = explode(',', $cmail_servers);
	$nb_item_profil = 6;
	for ($i=0; $i < count($tmp)/$nb_item_profil; $i++) {
		$out2 .= "\$mail_servers[] = Array(\n";
		$out2 .= "	'profile' => '".$tmp[$i*$nb_item_profil+1]."',\n";
		$out2 .= "	'domain' => '".$tmp[$i*$nb_item_profil+1]."',\n";
		$out2 .= "	'server' => '".$tmp[$i*$nb_item_profil+2]."',\n";
		$out2 .= "	'login_type' => '".$tmp[$i*$nb_item_profil+3]."',\n";
		$out2 .= "	'protocol' => '".$tmp[$i*$nb_item_profil+4]."',\n";
		$out2 .= "	'port' => '".$tmp[$i*$nb_item_profil+5]."');\n\n";
	}
	$out2 .=  "?>\n";
		// Save config.mail.php file
		$write_error2 = false;
		$fp = fopen($absolute_path.CFG_MAIL,"w");
		if ($fp == null)  {
			$write_error2 = true;
		} else {
			fwrite($fp,$out2,strlen($out2));
			fclose($fp);
		}
	}

		// Save Admin password file (admin.ucf)
	if (!$reinstall) {
		$write_error3 = false;
		$fp = fopen($absolute_path.$temporary_directory.DB_ADMIN,"w");
		if ($fp == null)  {
			$write_error3 = true;
		} else {
			
			$out3 = ($cusername.":".(md5($cpassword))."\n");
			
			if ( fwrite($fp,$out3,strlen($out3)) == 0 ) $write_error3 = true;
			fclose($fp);
		}
	}

	// Logout the session
	unset($_SESSION['validUser']);
	unset($_SESSION['userName']);
	unset($_SESSION['installMode']);
	session_destroy();

	?>

	<!-- config.php writing result -->
		<tr>
			<td style="background-image: url('images/bar.gif'); width: 60%;"><?php echo _STG5_CONFIG_WRITE." <b>config.php</b>";?></td>
			<td style="background-image: url('images/bar.gif'); width: 40%;">
	
	<?php
		if(!$write_error1) { echo '<b><font color="green">'._STG5_SUCCESS.'</font></b>'; }
		else { echo  '<b><font color="red">'._STG5_CONFIG_ERROR.'</font></b>'; }
		
		
	?>
			</td>
		</tr>
		<tr>

			  <td><?php echo _STG5_CONFIG_CONTENT.CFG_CONFIG;?>&nbsp;</td>
			  <td width="60%">
			  
			  <form>
				<textarea name="textarea" cols="55" rows="18" style="height: 150px;"><?php echo $out1;?></textarea></td>
		   </form>
		
		</tr>

	<!-- config.mail.php writing result -->
	<?php if ($cmail_server_type =='ONE-FOR-EACH') { ?>
		<tr>
			<td style="background-image: url('images/bar.gif'); width: 60%;"><?php echo _STG5_CONFIG_WRITE." <b>config.mail.php</b>";?></td>
			<td style="background-image: url('images/bar.gif'); width: 40%;">
	<?php
		if(!$write_error2) { echo '<b><font color="green">'._STG5_SUCCESS.'</font></b>'; }
		else { echo  '<b><font color="red">'._STG5_CONFIG_ERROR2.'</font></b>'; }
		
	?>
			</td>
		</tr>
		<tr>
			  <td><?php echo _STG5_CONFIG_CONTENT.CFG_MAIL;?>&nbsp;</td>
			  <td width="60%">
			  <form>
				<textarea name="textarea" cols="55" rows="18" style="height: 150px;"><?php echo $out2;?></textarea></td>
		</form>
		</tr>
	<?php } ?>

	<!-- Admin password writing result -->
	<?php 	if (!$reinstall) { ?>
		<tr>
			<td style="background-image: url('images/bar.gif'); width; 60%;"><?php echo _STG5_CONFIG_WRITE." <b>admin.ucf</b>";?></td>
			<td style="background-image: url('images/bar.gif'); width: 40%;">
	<?php
		if(!$write_error3) { echo '<b><font color="green">'._STG5_SUCCESS.'</font></b>'; }
		else { echo  '<b><font color="red">'._STG5_CONFIG_ERROR3.'</font></b>'; }
		
	?>
			</td>
		</tr>
	<?php } ?>
	
		<tr><td align="left" colspan="2">&nbsp;<br></td>
		</tr>
		<tr>
			<td align="left" colspan="2">
				<div align="right">
					<a href="<?php echo "./index.php?lid=$lid"; ?>" class="menulink"><?php echo _STG5_FRONT1;?> &nbsp; &nbsp; &nbsp;
					<a href="<?php echo RPATH_ROOT."index.php?lid=$lid"; ?>" class="menulink"><?php echo _STG5_FRONT;?></a> &nbsp; &nbsp; &nbsp;
					<a href="<?php echo RPATH_ROOT.PATH_ADMIN."index.php?lid=$lid"; ?>" class="menulink"><?php echo _STG5_ADMIN;?></a> &nbsp; &nbsp;
				</div>
			</td>
		</tr>
	</table>

