<?php

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

 login.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.
 *
 ***************************************************************************/

$GLOBALS["rootdp"] = './';
require_once ($GLOBALS["rootdp"]."include/config.php");
require_once ($GLOBALS["rootdp"]."include/db.php");
require_once ($GLOBALS["rootdp"]."include/session.php");
if ((!isset($HTTP_GET_VARS["ezSID"])) && (isset($HTTP_POST_VARS["ezSID"]))) $HTTP_GET_VARS["ezSID"] = $HTTP_POST_VARS["ezSID"];

include_once ($GLOBALS["rootdp"]."include/settings.php");
include_once ($GLOBALS["rootdp"]."include/functions.php");
include_once ($GLOBALS["rootdp"].$GLOBALS["admin_home"]."adminfunctions.php");

includeLanguageFiles('admin','main');


if (!isset($HTTP_POST_VARS["ezSID"])) $HTTP_POST_VARS["ezSID"] = $HTTP_GET_VARS["ezSID"];
if (!isset($HTTP_POST_VARS["topgroupname"])) $HTTP_POST_VARS["topgroupname"] = $HTTP_GET_VARS["topgroupname"];
if (!isset($HTTP_POST_VARS["groupname"])) $HTTP_POST_VARS["groupname"] = $HTTP_GET_VARS["groupname"];
if (!isset($HTTP_POST_VARS["subgroupname"])) $HTTP_POST_VARS["subgroupname"] = $HTTP_GET_VARS["subgroupname"];
if (!isset($HTTP_POST_VARS["link"])) $HTTP_POST_VARS["link"] = $HTTP_GET_VARS["link"];


if ($HTTP_POST_VARS["submitted"] == "yes") {
	if ((bCheckForm()) && (bVerifyAuthor())) {
		$valid = frmLoginHeader(true);
		if ($valid) { echo $GLOBALS["tLoginSuccessful"].' '; }
		else {
			$GLOBALS["strErrors"] = $GLOBALS["tMustRegister"].' '.$GLOBALS["tLogin"].' '.$GLOBALS["tHigherPriv"].' '.$GLOBALS["tToAccessPrivate"];
			frmLoginForm(false);
		}
	} else {
		$valid = frmLoginHeader(false);
		frmLoginForm(false);
	}
} else {
	$HTTP_POST_VARS["topgroupname"] = $HTTP_GET_VARS["topgroupname"];
	$HTTP_POST_VARS["groupname"]	= $HTTP_GET_VARS["groupname"];
	$HTTP_POST_VARS["subgroupname"] = $HTTP_GET_VARS["subgroupname"];
	$HTTP_POST_VARS["contentname"]  = $HTTP_GET_VARS["contentname"];
	$HTTP_POST_VARS["link"]			= $HTTP_GET_VARS["link"];
	$HTTP_POST_VARS["ref"]			= $HTTP_GET_VARS["ref"];
	frmLoginHeader(false);
	frmLoginForm(true);
}


?>
</body>
</html>
<?php
End_Gzip();



function frmLoginHeader($refresh)
{
	global $HTTP_POST_VARS, $HTTP_COOKIE_VARS;

	Start_Gzip();
	force_page_refresh();
	HTMLHeader('module');
	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
	$sqlQuery = '';
	if ($HTTP_POST_VARS["subgroupname"] > '') {
		$sqlQuery="SELECT loginreq,usergroups,subgrouplink AS link FROM ".$GLOBALS["eztbSubgroups"]." WHERE subgroupname='".$HTTP_POST_VARS["subgroupname"]."' AND language='".$GLOBALS["gsDefault_language"]."'";
	} elseif ($HTTP_POST_VARS["groupname"] > '') {
		$sqlQuery="SELECT loginreq,usergroups,grouplink AS link FROM ".$GLOBALS["eztbGroups"]." WHERE groupname='".$HTTP_POST_VARS["groupname"]."' AND language='".$GLOBALS["gsDefault_language"]."'";
	} elseif ($HTTP_POST_VARS["topgroupname"] > '') {
		$sqlQuery="SELECT loginreq,usergroups,topgrouplink AS link FROM ".$GLOBALS["eztbTopgroups"]." WHERE topgroupname='".$HTTP_POST_VARS["topgroupname"]."' AND language='".$GLOBALS["gsDefault_language"]."'";
	}
	if ($sqlQuery > '') {
		$result = dbRetrieve($sqlQuery,true,0,0);
		$rs	= dbFetch($result);
		$link = privatemenu($rs["loginreq"],$rs["usergroups"],$rs["link"]);
		dbFreeResult($result);
		if (($link == 'loginreq.php') || ($link == 'loginreq2.php')) {
			$refresh = false;
		}
	}

	if ($refresh) {
		$onload = 'opener.parent.location.href=\''.BuildLink('control.php').'&topgroupname='.$HTTP_POST_VARS["topgroupname"].'&groupname='.$HTTP_POST_VARS["groupname"].'&subgroupname='.$HTTP_POST_VARS["subgroupname"].'\'; self.close();';
	} else {
		if ($HTTP_COOKIE_VARS["UserIdCookie"] != '') {
			$onload = 'putFocus(\'LoginAuthors\',\'password\');';
		} else {
			$onload = 'putFocus(\'LoginAuthors\',\'login\');';
		}
	}
	?>
	<body marginwidth="0" marginheight="0" leftmargin="5" rightmargin="5" topmargin="10" class="mainback" onload="<?php echo $onload; ?>">
	<?php
	return $refresh;
}

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

	?>
	<form name="LoginAuthors" action="login.php" method="POST" enctype="multipart/form-data">
		<table border="0" width="100%" 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>
						<?php if ($GLOBALS["strErrors"] != '') { echo $GLOBALS["strErrors"]; } ?>
					</td>
				</tr>
				<?php
			}
			?>
			<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>
			<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="ezSID" value="<?php echo $GLOBALS["ezSID"]; ?>">
		<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="noframesbrowser" value="<?php echo $EZ_SESSION_VARS["noframesbrowser"]; ?>">
		<input type="hidden" name="topgroupname" value="<?php echo $HTTP_POST_VARS["topgroupname"]; ?>">
		<input type="hidden" name="groupname" value="<?php echo $HTTP_POST_VARS["groupname"]; ?>">
		<input type="hidden" name="subgroupname" value="<?php echo $HTTP_POST_VARS["subgroupname"]; ?>">
		<input type="hidden" name="contentname" value="<?php echo $HTTP_POST_VARS["contentname"]; ?>">
		<input type="hidden" name="link" value="<?php echo $HTTP_POST_VARS["link"]; ?>">
		<input type="hidden" name="submitted" value="yes">
	</form>
	<?php
} // function frmLoginForm()


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

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

	$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);
		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["Country"]			= '';
	$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()

?>
