<?php

/*
 *	W-AGORA 4.0
 *	-----------
 *	$Id: login.php,v 1.14 2005/06/04 10:14:43 mdruilhe Exp $
 *	Usage:		present login form to user
 *	Author:		Marc Druilhe <mdruilhe@w-agora.com>
 *
 */

	$bn_action="login";
	define("no_auth_required", 1);		// Don't authenticate the user, he want to login !!
	require ("init.inc");

# Execute before_access program
# -----------------------------
	if (!empty($before_access)) include $before_access;

# redirect to the LOGIN page if defined
# -------------------------------------
	$newURL = $auth->getLoginURL();
	if (is_string($newURL)) {
		if (empty($newURL)) {
			display_header ('', $header_footer);
			msgForm (ERROR_FEATURE_DISABLED, $WA_SELF, 'back');
			display_footer ($header_footer);
		} else {
			header("Location: $newURL");
		}
		exit;
	}

# set the url where to redirect if login is successful, 
# can be set by the following methods (by decreasing priority) :
#
#	1. Using the 'redirect' parameter in the URL => login.php?site=mysite&redirect=mypage.php
#	2. In the before access file:
#		if ($bn_action=="login") {$redirect_url = "mypage.php";}
#	3. Using the 'redirect_url'  hidden field in the form template : 
#		eg. <input type="hidden" name="redirect_url" value="mypage.php">
# ---------------------------------------------------------------------
	if (!empty($_GET['redirect'])) {
		$redirect_url = $_GET['redirect'];
	} elseif (!empty($_POST['redirect'])) {
		$redirect_url = $_POST['redirect'];
	} elseif (empty($redirect_url) && !empty($_POST['redirect_url'])) {
		$redirect_url = $_POST['redirect_url'];
	}

	if (isset($loginform)) {
# if login form has been submitted then authenticate him
# ------------------------------------------------------
		if ($is_authenticated) {
			if (empty($redirect_url)  ) {
				display_header (TITLE_WELCOME, $header_footer);
				printf(MSG_WELCOME, $auth->user["username"], anchor($main_page, ($bn_frames==1) ? '_top': '', ALT_WELCOME) );
			} elseif ($bn_frames) {
				display_header (TITLE_WELCOME, $header_footer);
				printf(MSG_WELCOME, $auth->user["username"], anchor($redirect_url, '_top', ALT_WELCOME, '') );
			} else {
				header ("Location: ". expandUrl($redirect_url) );
			}

		} else {
			display_header (ERROR_INVALID_LOGIN, $header_footer);
			$auth->login ($site);
		}
	} else {
# else we present the login form
# ------------------------------
		// if ($is_authenticated) {
		//   $auth->logout($site);
		// }
		display_header (LABEL_PLEASE_LOGIN, $header_footer);
		$auth->login ($site);
	}
	display_footer ($header_footer);
?>
