<?php

// always come first!
session_start();

include_once(dirname(__FILE__) . '/Config/qidportal.cfg.php');
include_once(dirname(__FILE__) . '/UbiLib/dbfuncs.' . UC_LIB_DBAPI . '.php');
include_once(dirname(__FILE__) . '/UbiLib/funcs_email.php');
include_once(dirname(__FILE__) . '/UbiLib/funcs_varia.php');
include_once(dirname(__FILE__) . '/UbiLib/funcs_strings.php');
include_once(dirname(__FILE__) . '/Lib/funcs_data_enterprise.php');
include_once(dirname(__FILE__) . '/Lib/funcs_data_log.php');
include_once(dirname(__FILE__) . '/UbiLib/countries-data.utf8.php');
include_once(dirname(__FILE__) . '/UbiLib/funcs_countries.php');
include_once(dirname(__FILE__) . '/Styles/corporate_styles_stuff.php');
include_once(dirname(__FILE__) . '/Lib/uc_corporate_htm_parts.php');
include_once(dirname(__FILE__) . '/Lib/funcs_forms.php');

if (isset($_SESSION) && isset($_SESSION['username']) && ($_SESSION['username'] != ''))
	{ $logged_in = true; }
else
	{ $logged_in = false; }

if (!isset($_REQUEST['oper'])) { $oper = 'noop'; }
else { $oper = $_REQUEST['oper']; }

openlog('login-enterprise', QID_PORTAL_SYSLOG_OPTION, QID_PORTAL_SYSLOG_FACILITY);

if ($oper == 'logout')
{
	// actually, I don't need to destroy the session, just to drop all its vars
	$skeys = array_keys($_SESSION);
	foreach($skeys as $ckey) { unset($_SESSION[$ckey]); } // that includes key 'username'
	$logged_in = false;

	// the corporate login page is not really intended for use. Redirect to main site
	header('Location: ../');
	ob_flush();
	sleep(2); // of any use?
	die('<!DOCTYPE html>
			<html>
				<head>
				</head>
				<body style="background-color:#eeeeee;">
					<center><br /><br />
					YOU SHOULD BE REDIRECTED TO THE PAGE IN A FEW SECONDS. IF NOT SO, PLEASE CLICK ON THE LINK BELOW:<br /><br />
					<a href="../">HOMEPAGE</a><br /></br>
					</center>
				</body>
			</html>
	');
}


echo '<!DOCTYPE html>
<html lang="en">
';

// produce standard HTML head tag and content

uc_qidportal_corporate_htm_head('Q-ID : Corporate : Welcome / Login', '');

echo '
<body class="corp_reg_body">
	<div id="divMain" class="nxd_divmain">
';
qidportal_show_main_menu(!$logged_in);

//
//  **** DO NOT DISPLAY ANYTHING (YET). WAIT FOR THE MENU TO BE IN PLACE, IF IT IS THE CASE ***
//
//  use variables logged_in and dispmsg to control the page look
//

$dispmsg = '';

switch($oper)
{
	case 'logout':

		$logged_in = false;
		$dispmsg = '<br /><br /><font class="msgok">OK, LOGGED OUT.</font><br /><br />' . "\n\n";
		break;

	case 'login_enterprise':

		if ($logged_in)
		{
			$dispmsg = '<br /><br /><font class="warning">
				WARNING: no need to access this page, you already did log in!</font><br /><br />';
			break;
		}

		if
		(
			!isset($_REQUEST['username']) || ($_REQUEST['username'] == '') ||
			!isset($_REQUEST['pwd']) || ($_REQUEST['pwd'] == '')
		)
		{
			$dispmsg = '<br /><br /><font class="problem">
				ERROR: enter you user name and password, please!</font><br /><br />';
			break;
		}

		$dbpwd = uc_qid_pass2dbv($_REQUEST['pwd']);

		// syslog(LOG_NOTICE, 'DEBUG: **REMOVE** >login for ' . $_REQUEST['username'] . ': ' . $dbpwd);
		$isok = uc_qidportal_enterprise_checklogin(false, $_REQUEST['username'], $dbpwd, true);

		if ($isok)
		{
			$logged_in = true;

			if (strpos($_REQUEST['username'], '@') !== false) // the user logged in with the e-mail
			{
				$eid = uc_qidportal_enterprisedata_field2eid($dbc, 'email_main', $_REQUEST['username']);
			}
			else
			{
				$eid = $_REQUEST['username'];
			}

			$_SESSION['username'] = $eid;
			syslog(LOG_NOTICE, 'INFO: logged in enterprise user ' . $_SESSION['username']);

			$dispmsg = '
				<br /><br /><font class="msgok">Welcome, user ' . $_SESSION['username'] . '</font><br /><br />
			';
		}
		else
		{
			syslog(LOG_NOTICE, 'ERROR: login failed -bad credentials, form-passed username: ' . $_REQUEST['username']);

			/* it is displayed anyway, do not repeat:
				<font class="nxd_frm_explain">
				If you do not possess any credentials to enter the corporate site,
				please <a href="corporate_registration.php">click here to register</a></font>
				<br /><br />
			*/

			$dispmsg = '<br /><br /><font class="problem">ERROR: wrong user and/or password.</font><br /><br />
			';
		}

		break;

	default:

		if ($logged_in)
		{
			$dispmsg = '<br /><br /><font class="warning">
				WARNING: no need to access this page, you already did log in!</font><br /><br />';
			break;
		}
}


// display the menu and the central box for the workarea (knows what to do if not logged in)

uc_qidportal_corporate_central_box_open($logged_in, ($logged_in ? $_SESSION['username'] : false),
	'corporate_login.php', 'Login / welcome page');

// was a lot troublesome!...: padding: 10px 10px 10px 10px;
echo "\n\n" . '<br /><br /><div style="width:100%; background-color:#eeeeee; margin:auto;">' . "\n\n";
// display login facility, notice that I am not re-testing the user being already in (1st parm is false)

echo $dispmsg;

if (!$logged_in)
{
	$loginhtm = uc_qidportal_form_login_htm(false, 'corporate_login.php',
				'v', '', 'class="infoask"', false,
				'style="width:100%; font-weight:bold; background-color: red; color: white; border: 1px solid #222222; outline: white none 0px;"',
				'username', 'pwd', 'login_enterprise');

	if (is_string($loginhtm) && $loginhtm != '')
	{
		// echo '<div class="infoask" style="width:80%;">enter your username and password to log in</div>' . "\n";
		echo $loginhtm . '<br />' . "\n";
	}

	echo '
		<br /><br />
		<div class="smallnote_r">
			If you do not possess an account and you are interested in our Q-ID solution,
			please <a href="corporate_registration.php">click here to register</a><br /><br />
			INFORMATION: after login, this site will use cookies for the purpose of keeping the system
			informed of the fact that you are logged in.<br />
			To learn more about cookies, <a href="cookie_info.php" target="cookie_info">click here</a>
		</div>
		<br />
	';
}

echo "\n\n" . '</div><br /><br />' . "\n\n";

// close the workarea along with my central enclosing table
// WARNING: for some unclear reason, if the user is logged in the bar comes out with a bad placement. Do not show it, then!

uc_qidportal_corporate_central_box_close($logged_in, !$logged_in);

echo '
	</div>  <!-- end of divMain, unless HTML is messed up -->

	<!-- to test search engine policies -->
	<span style="font-size:2px; align:right;"><a href="https://www.rustichelli.net/g-artu/" target="gartu" lang="it">here is art</a></span>
</body>
</html>
';

closelog();
