<?php

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

 adminlogin.php
 ---------------
 copyright : (C) 2002-2003 The ezContents Development Team

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

/***************************************************************************
 The ezContents Development Team offers no warranties on this script.
 The owner/licensee of the script is solely responsible for any problems
 caused by installation of the script or use of the script.

 All copyright notices regarding ezContents must remain intact on the
 scripts and in the HTML for the scripts.

 For more info on ezContents,
 visit http://www.ezcontents.org/

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

/***************************************************************************
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the License which can be found within the
 *	zipped package.
 *
 ***************************************************************************/

include_once ("rootdatapath.php");
require_once ($GLOBALS["rootdp"]."include/config.php");
require_once ($GLOBALS["rootdp"]."include/db.php");
require_once ($GLOBALS["rootdp"]."include/session.php");
include_once ($GLOBALS["rootdp"]."include/functions.php");

include_once ($GLOBALS["rootdp"]."include/settings.php");
includeLanguageFiles('admin');


if ($HTTP_POST_VARS["submitted"] == "yes") {
	if ((bCheckForm()) && (bVerifyAuthor())) {
		Header("Location: ".BuildLink('control2.php'));
	} else {
		force_page_refresh();
		frmLoginForm(false);
	}
} else {
	force_page_refresh();
	frmLoginForm(true);
}


function frmLoginForm($bAccess)
{
	global $EZ_SESSION_VARS, $HTTP_SERVER_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $admin_login;

	HTMLHeader('login');
	if ($GLOBALS["gsAdminStyle"] != '') { ?><LINK HREF="<?php echo $GLOBALS["rootdp"].$GLOBALS["style_home"].$GLOBALS["gsAdminStyle"]; ?>/vs.css" REL=STYLESHEET TYPE="text/css"><?php
	} else { StyleSheet(); }
	?>
	<script language="JavaScript" type="text/javascript">
		<!-- Begin
		function putFocus(formInst, elementInst) {
			var browserName = navigator.appName;
			var browserVersion = navigator.appVersion;
			var userPlatform = navigator.platform;

			if (browserName == "Microsoft Internet Explorer") {
				var brw_array;
				brw_array = browserVersion.split("MSIE ");
				brw_array = brw_array[1].split(";");
				browserVersion = brw_array[0];
			} else {
				if (navigator.product == "Gecko") {
					browserName = navigator.product;
					browserVersion = navigator.productSub;
				}
			}

			document.LoginAuthors.browser.value = browserName;
			document.LoginAuthors.version.value = browserVersion;
			document.LoginAuthors.platform.value = userPlatform;

			if (document.forms.length > 0) {
				document.forms[formInst].elements[elementInst].focus();
			}
		}
		//  End -->
	</script>
	</head>
	<?php
	if ($HTTP_COOKIE_VARS["UserIdCookie"] != '') {
		?>
		<body leftmargin=0 topmargin=0 marginwidth="0" marginheight="0" class="mainback" onLoad="putFocus(0,1);">
		<?php
	} else {
		?>
		<body leftmargin=0 topmargin=0 marginwidth="0" marginheight="0" class="mainback" onLoad="putFocus(0,0);">
		<?php
	}
	?>

	<table border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">
		<tr><td align="center" valign="middle">
			<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td align="center" width="100%">
					<form name="LoginAuthors" action="adminlogin.php" method="POST" enctype="multipart/form-data">
						<table border="0" cellspacing="3" cellpadding="3">
						<tr class="headercontent">
							<td colspan="2" align="center">
								<b><?php echo $GLOBALS["tLoginTitle"]; ?></b>
							</td>
						</tr>
						<?php
						if (!$bAccess) {
							?>
							<tr bgcolor="#900000">
								<td align="center" colspan="2">
									<b><?php echo $GLOBALS["tInvalidLogin"]; ?></b>
								</td>
							</tr>
							<?php
							if ($GLOBALS["strErrors"] != '') { echo $GLOBALS["strErrors"]; }
						}
						?>
						<tr class="tablecontent">
							<td valign=top><b><?php echo $GLOBALS["tUsernameText"]; ?>:</b></td>
							<td>
								<input type="text" name="login" size="20" value="<?php echo $HTTP_COOKIE_VARS["UserIdCookie"]; ?>" maxlength="20">
							</td>
						</tr>
						<tr class="tablecontent">
							<td valign=top><b><?php echo $GLOBALS["tPasswordText"]; ?>:</b></td>
							<td>
								<input type="password" name="password" size="20" value="<?php echo $GLOBALS["gsPassword"]; ?>" maxlength="20">
							</td>
						</tr>
						<?php
						if ($GLOBALS["gsMultiSite"] == 'Y') {
							echo '<tr class="tablecontent">';
							echo '<td valign=top><b>'.$GLOBALS["tCurrentSite"].'</b></td>';
							echo '<td>';
							$strQuery = "SELECT sitecode,sitename FROM ".$GLOBALS["eztbSites"]." WHERE siteenabled='1' ORDER BY sitename";
							$result = dbRetrieve($strQuery,true,0,0);
							echo '<select name="Site" size="1">';
							echo '<option selected ';
							echo 'value="">'.$GLOBALS["tMasterSite"];
							while ($rs = dbFetch($result)) {
								echo '<option ';
								if ($rs["sitecode"] == $EZ_SESSION_VARS["Site"]) { echo 'selected '; }
								echo 'value="'.$rs["sitecode"].'">'.$rs["sitename"];
							}
							dbFreeResult($result);
							echo '</select></td></tr>';
						}
						?>
						<tr class="topmenuback">
							<td colspan=2 align="center">
								<input type="submit" name="submit" value="<?php echo $GLOBALS["tLoginText"]; ?>">
							</td>
						</tr>
						</table>
						<input type="hidden" name="browser" id="browser">
						<input type="hidden" name="version" id="version">
						<input type="hidden" name="platform" id="platform">
						<input type="hidden" name="ezSID" value="<?php echo $GLOBALS["ezSID"]; ?>">
						<input type="hidden" name="submitted" value="yes">
					</form>
				</td>
			</tr>

			</table>
		</td></tr>
	</table>

	</body>
	</html>
	<?php
} // function frmLoginForm()


function bVerifyAuthor()
{
	global $EZ_SESSION_VARS, $HTTP_POST_VARS;

	$sPassword = md5($HTTP_POST_VARS["password"]);

	if ($HTTP_POST_VARS["Site"] != '') {
		if ($GLOBALS["gsMultiSiteAuthors"] != 'Y') {
			$strQuery = "SELECT * FROM ".$HTTP_POST_VARS["Site"]."authors WHERE login='".$HTTP_POST_VARS["login"]."' AND userpassword='".$sPassword."'";
		} else {
			$strQuery = "SELECT * FROM ".$GLOBALS["eztbAuthors"]." WHERE login='".$HTTP_POST_VARS["login"]."' AND userpassword='".$sPassword."'";
		}
	} else {
		$strQuery = "SELECT * FROM ".$GLOBALS["eztbAuthors"]." WHERE login='".$HTTP_POST_VARS["login"]."' AND userpassword='".$sPassword."'";
	}
	$result = dbRetrieve($strQuery,true,0,0);
	$rs		= dbFetch($result);
	if (($rs["login"] == $HTTP_POST_VARS["login"]) && ($rs["disuser"] != '1')) {
		$EZ_SESSION_VARS["LoginCookie"]		= $HTTP_POST_VARS["login"];
		$EZ_SESSION_VARS["PasswordCookie"]	= $sPassword;
		$EZ_SESSION_VARS["UserID"]			= $rs["authorid"];
		$EZ_SESSION_VARS["UserName"]		= $rs["authorname"];
		$EZ_SESSION_VARS["UserGroup"]		= $rs["usergroup"];
		$EZ_SESSION_VARS["Language"]		= $rs["language"];
		$EZ_SESSION_VARS["Country"]			= $rs["countrycode"];
		$EZ_SESSION_VARS["Browser"]			= $HTTP_POST_VARS["browser"];
		$EZ_SESSION_VARS["BrowserVersion"]	= $HTTP_POST_VARS["version"];
		$EZ_SESSION_VARS["Platform"]		= $HTTP_POST_VARS["platform"];
		dbFreeResult($result);
		if ($GLOBALS["gsMultiSite"] == 'Y') {
			$EZ_SESSION_VARS["Site"]		= $HTTP_POST_VARS["Site"];
		}
		db_session_write();
		return true;
	}
	dbFreeResult($result);
	$EZ_SESSION_VARS["LoginCookie"]		= '';
	$EZ_SESSION_VARS["PasswordCookie"]	= '';
	$EZ_SESSION_VARS["UserID"]			= 0;
	$EZ_SESSION_VARS["UserName"]		= '';
	$EZ_SESSION_VARS["UserGroup"]		= '';
	$EZ_SESSION_VARS["Language"]		= '';
	$EZ_SESSION_VARS["Browser"]			= $HTTP_POST_VARS["browser"];
	$EZ_SESSION_VARS["BrowserVersion"]	= $HTTP_POST_VARS["version"];
	$EZ_SESSION_VARS["Platform"]		= $HTTP_POST_VARS["platform"];
	db_session_write();
	return false;
} // function bVerifyAuthor()


function bCheckForm()
{
	global $HTTP_POST_VARS;

	$bFormOK = true;
	$strMessage = '';
	$HTTP_POST_VARS["login"] = trim($HTTP_POST_VARS["login"]);
	$pos = strpos($HTTP_POST_VARS["login"], "%");
	if ($pos === false) {
		if ($HTTP_POST_VARS["login"] == "") {
			$strMessage .= '<br />'.$GLOBALS["eNoLogin"];
			$bFormOK = false;
		}
	} else {
		$strMessage .= '<br />'.$GLOBALS["eNoLogin"];
		$bFormOK = false;
	}

	if ($HTTP_POST_VARS["password"] == "") {
		$strMessage .= '<br />'.$GLOBALS["eNoPassword"];
		$bFormOK = false;
	}

	if (!$bFormOK) {
		$GLOBALS["strErrors"] = '<b>'.$strMessage.'</b>';
	}
	return $bFormOK;
} // function bCheckForm()

?>
