<?php
	Require("../Includes/i_Includes.php");
	Require("SQL/index.php");
	
	$FILE_DOMAINS	= "i_OpenDomain.php";
	$sNewDirName	= "PHPJK";

	OpenPage();
	Main();
	ClosePage();

	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function Main()
	{
		Global $sSQLServerName;
		Global $sPHP_JKLogin;
		Global $sPHP_JKPassword;
		Global $sPHP_JKName;
		Global $DBConnection;
		Global $sThumbnailType;
		Global $sEmailType;
		Global $sEMAIL_REMOTEHOST;
		
		$sAction = Request("sAction");

		If ( $sAction == "Begin" ) {
			$sError = CheckFormFields();
			If ( $sError == "" )
			{
				// Change the i_OpenDomain.php file
				ChangeASP();
				
				$DBConnection = DB_DBConnect($sSQLServerName, $sPHP_JKLogin, $sPHP_JKPassword);
				
				// Create all the databases, users and set the rights
				CreateTables($DBConnection);

				// Create the new entry in the DomainInfo table
				// Create the new entries in the Configurations table
				$sPHP_JKName = str_replace ("HTTP://", "", strtoupper($sPHP_JKName));
				DB_Insert ("INSERT INTO DomainInfo (Description,Domain,Type) VALUES ('The first domain.', '" . SQLEncode($sPHP_JKName) . "','')", $DBConnection);
				Defaults($DBConnection);
				?>
				<b>Setup was successful!</b>
				<br><br>
				Please now delete the installation script directory: \PHPJK\PHP_JK_Installation\ to avoid
				accidental or malicious overwriting of your PHPJK installation.
				<br><br>
				You may also remove WRITE permissions for the IUSR on the \PHPJK\Includes\ directory.
				<br><br>
				<a href='/PHPJK/'>Enter the gallery</a>
				<?php 

			}Else{
				Echo "<hr><BR><Font color='660000'><b>" . $sError . "</b></font><br><hr><br>";
				WriteForm();
			}
		}Else{
			WriteForm();
		}
	}
	//************************************************************************************
	

	//************************************************************************************
	//*																					*
	//*	This is the function to write out the form.										*
	//*																					*
	//************************************************************************************
	Function WriteForm()
	{
		Global $sLocASP;
		Global $sAdminPassword;
		Global $sAdminLogin;
		Global $sPHP_JKPassword;
		Global $sPHP_JKLogin;
		Global $sPHP_JKName;
		Global $sNewDirName;
		Global $sNewDirLocation;
		Global $sSQLServerName;
		Global $sUseDB;
		Global $sThumbnailType;
		Global $sEmailType;
		Global $sEMAIL_REMOTEHOST;
		
		If ( $sPHP_JKLogin == "" )
			$sPHP_JKLogin = "PHPJKLogin";
		?>
		<SCRIPT LANGUAGE="JavaScript">

			function disableForm(theform) {
				if (document.all || document.getElementById) {
					for (i = 0; i < theform.length; i++) {
						var tempobj = theform.elements[i];
						if (tempobj.type.toLowerCase() == "submit")
							tempobj.disabled = true;
					}
					return true;
				}else{
					return false;
				}
			}
			
		</script>
		<center>
		<table><tr><td>
		<b>Please delete these installation files or move them someplace not accessable via the web
		once you are finished here.</b>
		<br><Br>
		<center>
		<form action='index.php?sAction=Begin' method='post' onSubmit="return disableForm(this);">
			<table cellpadding = 0 cellspacing = 0 border = 0><tr><td bgcolor = 000066>
			<table cellpadding = 5>
				<tr>
					<td bgcolor = 000066 colspan=2 align=center>
						<b><font size=+1 color=FFFFFF><?=$sUseDB?> Database Setup
					</td>
				</tr>
				<tr>
					<td bgcolor = AAAADD colspan=2>
						You must create the PHP_JK database before running this script.
						<br><br>
						In addition to creating the database, you must also create a database user as the owner of the PHP_JK database.
					</td>
				</tr>
				<tr>
					<td bgcolor = EEEEFF align = right>
						<b><?=$sUseDB?> Server Name</b>
					</td>
					<td bgcolor = EEEEFF>
						<input type="text" name="sSQLServerName" value="<?=$sSQLServerName?>" maxlength=250 size=60>
					</td>
				</tr>
				<tr>
					<td bgcolor = EEEEFF align = right>
						<b>PHP_JK DB Login</b>
					</td>
					<td bgcolor = EEEEFF>
						<input type="text" name="sPHP_JKLogin" value="<?=$sPHP_JKLogin?>" maxlength=250 size=60>
					</td>
				</tr>
				<tr>
					<td bgcolor = EEEEFF align = right>
						<b>PHP_JK DB Password</b>
					</td>
					<td bgcolor = EEEEFF>
						<input type="text" name="sPHP_JKPassword" value="<?=$sPHP_JKPassword?>" maxlength=250 size=60>
					</td>
				</tr>
			</table>
			</td></tr></table>
			<br><br>
			<table cellpadding = 0 cellspacing = 0 border = 0><tr><td bgcolor = 666600>
			<table cellpadding = 5>
				<tr>
					<td bgcolor = 666600 colspan=2 align=center>
						<b><font size=+1 color=FFFFFF>Domain Setup
					</td>
				</tr>
				<tr>
					<td bgcolor = DDDDAA colspan=2>
						Enter your first PHPJK website's information.
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE align = right width=100%>
						<b>The domain name of your first PHPJK website as entered into IIS (eg: www.PHPJK.com)</b>
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sPHP_JKName" value="<?=$sPHP_JKName?>" maxlength=250 size=60>
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE colspan=2>
						&nbsp;
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>Your first PHPJK website's directory location (eg: e:\Inetpub\wwwroot\mywebsite\):</b>
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sNewDirLocation" value="<?=$sNewDirLocation?>" maxlength=250 size=60>
					</td>
				</tr>
				
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>First administrator account login</b>
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sAdminLogin" value="<?=$sAdminLogin?>" maxlength=250 size=60>
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>First administrator account password</b>
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sAdminPassword" value="<?=$sAdminPassword?>" maxlength=250 size=60>
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>Location of the i_OpenDomain.php file<br>(eg: e:\Inetpub\wwwroot\Includes\)</b>
						<br>
						Make sure this location has WRITE access for the IUSR account. This script will try and write the
						database connection information into the /Includes/i_OpenDomain.php file.
						You can revoke this WRITE access after the script has finished.
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sLocASP" value="<?=$sLocASP?>" maxlength=250 size=60>
					</td>
				</tr>
				<?php If ( $GLOBALS["sOS"] != "UNIX" ) { ?>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>Thumbnail system to use</b>
					</td>
					<td bgcolor = FFFFEE>
						<select name='sThumbnailType'>
							<option value='' <?php If ( $sThumbnailType == "" ) Echo "selected"; ?>>No thumbnail creation</option>
							<option value='PHP' <?php If ( $sThumbnailType == "PHP" ) Echo "selected"; ?>>PHP Native (no component)</option>
							<option value='GFL' <?php If ( $sThumbnailType == "GFL" ) Echo "selected"; ?>>GlfAX (www.xnview.com)</option>
						</select>
					</td>
				</tr>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>Email system to use</b>
					</td>
					<td bgcolor = FFFFEE>
						<select name='sEmailType'>
							<option value='' <?php If ( $sEmailType == "" ) Echo "selected"; ?>>Disable email functionality</option>
							<option value='ASPEMAIL' <?php If ( $sEmailType == "ASPEMAIL" ) Echo "selected"; ?>>ASPEmail (www.serverobjects.com)</option>
							<option value='CDONTS' <?php If ( $sEmailType == "CDONTS" ) Echo "selected"; ?>>CDONTS</option>
							<option value='SENDMAIL' <?php If ( $sEmailType == "SENDMAIL" ) Echo "selected"; ?>>sendmail</option>
						</select>
					</td>
				</tr>
				<?php }Else{ ?>
				<input type='hidden' name='sEmailType' value='SENDMAIL'>
				<input type='hidden' name='sThumbnailType' value='PHP'>
				<?php } ?>
				<tr>
					<td bgcolor = FFFFEE align = right>
						<b>Email remote host (usually something like mail.yourwebsite.com).</b><br>If you don't know, you can enter this later on the Configuration Variables administration screen.</b>
					</td>
					<td bgcolor = FFFFEE>
						<input type="text" name="sEMAIL_REMOTEHOST" value="<?=$sEMAIL_REMOTEHOST?>" maxlength=250 size=60>
					</td>
				</tr>
			</table>
			</td></tr></table>
			<br><br>
			<table cellpadding = 0 cellspacing = 0 border = 0><tr><td bgcolor = 000000>
			<table cellpadding = 5>
				<tr>
					<td bgcolor = AAAAAA colspan=2 align=center>
						<input type="submit" value="                         Install                         ">
						<br>
						Please be patient, this may take a minute or two.
					</td>
				</tr>
			</table>
			</td></tr></table>
		</form>
		</td></tr></table>
		<?php 
	}
	//************************************************************************************
	
	

	//************************************************************************************
	//*																					*
	//*	Make sure that all the form fields have been filled out properly.				*
	//*																					*
	//************************************************************************************
	Function CheckFormFields()
	{
		Global $sLocASP;
		Global $sAdminPassword;
		Global $sAdminLogin;
		Global $sPHP_JKPassword;
		Global $sPHP_JKLogin;
		Global $sPHP_JKName;
		Global $sNewDirLocation;
		Global $sSQLServerName;
		Global $sThumbnailType;
		Global $sEmailType;
		Global $sEMAIL_REMOTEHOST;
	
		$CheckFormFields = "";
		
		$sLocASP				 = Trim(Request("sLocASP"));
		$sAdminPassword			 = Trim(Request("sAdminPassword"));
		$sAdminLogin			 = Trim(Request("sAdminLogin"));
		$sPHP_JKPassword		 = Trim(Request("sPHP_JKPassword"));
		$sPHP_JKLogin			 = Trim(Request("sPHP_JKLogin"));
		$sPHP_JKName			 = Trim(Request("sPHP_JKName"));
		$sNewDirLocation		 = Trim(Request("sNewDirLocation"));
		$sSQLServerName			 = Trim(Request("sSQLServerName"));
		$sThumbnailType			 = Trim(Request("sThumbnailType"));
		$sEmailType				 = Trim(Request("sEmailType"));
		$sEMAIL_REMOTEHOST		 = Trim(Request("sEMAIL_REMOTEHOST"));
		
		If ( $sPHP_JKPassword == "" )
			$CheckFormFields = $CheckFormFields . "Please enter the password for the PHP_JK_Domains database login.<br>";

		If ( $sPHP_JKLogin == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the login for the PHP_JK_Domains database.<br>";

		If ( $sPHP_JKName == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the first domain's name.<br>";

		If ( $sNewDirLocation == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the first domain's directory location.<br>";

		If ( $sAdminLogin == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the first administrators login.<br>";

		If ( $sAdminPassword == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the first administrators password.<br>";

		If ( $sLocASP == "" ) 
			$CheckFormFields = $CheckFormFields . "Please enter the location of the i_OpenDomain.php file.<br>";
		
		If ( ! file_exists($sLocASP))
			$CheckFormFields = $CheckFormFields . "Database file location does not exist. Please create the directory (" . $sLocASP . ") or choose another one.<br>";

		If ( $sSQLServerName == "" )
			$CheckFormFields = $CheckFormFields . "Please enter the SQL Server name.<br>";
			
		Return $CheckFormFields;
	}
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*	This changes the /Includes/i_OpenDomain.php file.					*
	//*																					*
	//************************************************************************************
	Function ChangeASP()
	{
		Global $sLocASP;
		Global $FILE_DOMAINS;
		Global $sSQLServerName;
		Global $sPHP_JKLogin;
		Global $sPHP_JKPassword;
		
		$sError = "";
		
		$sTempString = "<?php\n";
		$sTempString .= "//************************************************************************************\n";
		$sTempString .= "//*																					*\n";
		$sTempString .= "//*	Open the connection and command objects for the PHP_JK database.	 			*\n";
		$sTempString .= "//*	This MUST be seperate from i_Domains.asp because it gets changed in the 		*\n";
		$sTempString .= "//*		installation script.														*\n";
		$sTempString .= "//*																					*\n";
		$sTempString .= "//*	Changes to this file must be reproduced in the ChangeASP() function in the 		*\n";
		$sTempString .= "//*		/PHP_JK_Installation/Default.asp file.										*\n";
		$sTempString .= "//************************************************************************************\n";
		$sTempString .= "\n";
		$sTempString .= "Function DB_OpenDomains()\n";
		$sTempString .= "{\n";
		$sTempString .= "\n";
		$sTempString .= "	Global " . "$" . "PHPJKConnection;";
		$sTempString .= "\n";
		$sTempString .= "	" . "$" . "PHPJKConnection = DB_DBConnect (\"" . $sSQLServerName . "\", \"" . $sPHP_JKLogin . "\", \"" . $sPHP_JKPassword . "\");\n";
		$sTempString .= "\n";
		$sTempString .= "	Return " . "$" . "PHPJKConnection;";
		$sTempString .= "\n";
		$sTempString .= "}\n";
		$sTempString .= "?>\n";
		
		$sASPLocation = str_replace("/", "\\", $sLocASP);
		If ( strpos($sASPLocation, "\\" . $FILE_DOMAINS) === false ) {
			$sASPLocation = $sASPLocation . "\\" . $FILE_DOMAINS;
		}Else{
			$sASPLocation = $sASPLocation . $FILE_DOMAINS;
		}
		
		$sASPLocation	= str_replace("\\\\", "\\", $sASPLocation);
		
		If ( $GLOBALS["sOS"] = "UNIX" )
			$sASPLocation	= str_replace("\\", "/", $sASPLocation);
			
		If ( ! file_exists($sASPLocation))
		{
			$sError = "<br>The Include file i_OpenDomains.php does not exist there.";
		}Else{
			$handle			= fopen($sASPLocation, "w");
			If (fwrite($handle, $sTempString) === FALSE)
				$sError = "Cannot write to the file: " . $sASPLocation . ". Installation aborted.<br>";
		}

		Return $sError;
	}
	//************************************************************************************
	

	//************************************************************************************
	//*																					*
	//*	Opening HTML.																	*
	//*																					*
	//************************************************************************************
	Function OpenPage()
	{
		?>
		<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
		<HTML>
		<HEAD>
			<TITLE>PHPJK Installation</TITLE>
			<http-equiv='pragma' content='NO-CACHE'>
		</HEAD>
		<BODY BGCOLOR='#FFFFFF' TEXT='#000000' TOPMARGIN=10 LEFTMARGIN=10 MARGINWIDTH=0 MARGINHEIGHT=0>
			<center>
			<table width=100% border=0 cellpadding=0 cellspacing=0>
				<tr>
					<td valign=bottom>
						<img src = 'Images/Navigation/PHPJKLogo.gif' alt = 'PHPJK Installation'>
					</td>
				</tr>
			</table>
			<table width=100% border=0 cellpadding=0 cellspacing=0>
				<tr>
					<td width=35><img src = 'Images/Navigation/Border_Top_Left.gif' width = 35 height = 36 border=0></td>
					<td background='Images/Navigation/Border_Top_Middle.gif' width=100%><img src = 'Images/Navigation/Blank.gif' width=2 height=36></td>
					<td width=37><img src = 'Images/Navigation/Border_Top_Right.gif' width = 37 height = 36 border=0></td>
				</tr>
			</table>
			<table width=100% border=0 cellpadding=0 cellspacing=0>
				<tr>
					<td width=35 background='Images/Navigation/Border_Left_Middle.gif'><img src = 'Images/Navigation/Blank.gif' width=35 height=36></td>
					<td valign=top width=100%>
		<?php 
	}
	//************************************************************************************


	//************************************************************************************
	//*																					*
	//*	Opening HTML.																	*
	//*																					*
	//************************************************************************************
	Function ClosePage()
	{
		?>
						</td>
						<td width=37 background='Images/Navigation/Border_Right_Middle.gif'><img src = 'Images/Navigation/Blank.gif' width=37 height=36></td>
					</tr>
				</table>
				<table width=100% border=0 cellpadding=0 cellspacing=0>
					<tr>
						<td width=35><img src = 'Images/Navigation/Border_Bottom_Left.gif' width = 35 height = 36 border=0></td>
						<td background='Images/Navigation/Border_Bottom_Middle.gif' width=100%><img src = 'Images/Navigation/Blank.gif' width=2 height=36></td>
						<td width=37><img src = 'Images/Navigation/Border_Bottom_Right.gif' width = 37 height = 36 border=0></td>
					</tr>
				</table>
			</td></tr></table>
			</center>
		</BODY></HTML>
		<?php 
	}
	//************************************************************************************
?>