<?php
/*
Copyright Intermesh 2003
Author: Merijn Schering <mschering@intermesh.nl>
Version: 1.0 Release date: 08 July 2003

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
*/
require_once("Group-Office.php");
print "step1";
$config_file = $GO_CONFIG->get_config_file();
prit $config_file;

/*Uncomment with release!
if(file_exists('install'))
{
	echo 'Install directory exists. Click <a href="install/install.php">here</a> to install or '.
			'upgrade your installation or remove the install directory for security reasons. ';
	exit();
}elseif(is_writable($config_file))
{
	echo '<font color="red"><b>\''.$config_file.'\' is writable please chmod 755
    '.$config_file.' and change the ownership to any other user then the
    webserver user.</b></font>';
    
	exit();
}*/


$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
require_once($GO_LANGUAGE->get_base_language_file('login'));

if ($task == "logout")
{

/* zed -- for bookmarks */
# reset cookies
setcookie("BM4U_ROOT", "", 0, "/");
setcookie("BM4U_USER", "", 0, "/");
setcookie("BM4U_UID", "", 0, "/");
setcookie("BM4U_TIME", "", 0, "/");
setcookie("BM4U_TOKEN", "", 0, "/");
setcookie("BM4U_GUSER", "", 0, "/");
setcookie("BM4U_GUID", "", 0, "/");
setcookie("BM4U_GTIME", "", 0, "/");
setcookie("BM4U_GTOKEN", "", 0, "/");
setcookie("BM4U_GUEST_MODE", "", 0, "/");
setcookie("BM4U_CLIPBRD", "", 0, "/");
setcookie("BM4U_SEARCHES", "", 0, "/");
#    echo "<script type='text/javascript' src='javascript/avw_common.js'></script>";
#    echo "<script>open_win('modules/bookmarks/index.php','extra');</script>";
	SetCookie("GO_UN","",time()-3600,"/","",0);
	SetCookie("GO_PW","",time()-3600,"/","",0);
	unset($_SESSION);
	unset($_COOKIE);

/* zed - delete _webChat.html file */
$f_path = $HTTP_SESSION_VARS['f_path'];
$if_path = $HTTP_SESSION_VARS['if_path'];

if(file_exists($if_path)) {
//echo "here".$_SERVER['HTTP_REFERER'].$if_path; die();
//	fclose($if_path);
	if(!unlink($if_path)) die();
}

if(file_exists($f_path)) {
//	fclose($f_path);
	if(!unlink($f_path)) die();
}

	$GO_SECURITY->logout();
}

//when the user is logged in redirect him.
if ($GO_SECURITY->logged_in == true)
{
	$start_module = $GO_MODULES->get_module(
	$_SESSION['GO_SESSION']['start_module']);
        if (isset($_REQUEST['return_to']))
	{
		$link = $_REQUEST['return_to'];
	}elseif ( $start_module && ( $GO_SECURITY->has_permission(
	$GO_SECURITY->user_id, $start_module['acl_read']) ||
	$GO_SECURITY->has_permission($GO_SECURITY->user_id,
	$start_module['acl_write']) ) )
	{
		$link = $start_module['url'];
	}else
	{
		$link = $GO_CONFIG->host.'configuration/';
	}

	require_once($GO_THEME->theme_path."frames.inc");

	exit();
}

//if form was posted user wants to login
//set cookies to remember login before headers are sent
if ( $_SERVER['REQUEST_METHOD'] == "POST" || (isset($_COOKIE['GO_UN'])
&& isset($_COOKIE['GO_PW'])) )
{
	if ($_SERVER['REQUEST_METHOD'] != "POST")
	{
		$remind = true;
		$password = smart_addslashes($_COOKIE['GO_PW']);
		$username = smart_addslashes($_COOKIE['GO_UN']);
	} else {
		$remind = isset($_POST['remind']) ? true : false;
		$username = smart_addslashes($_POST['username']);
		$password = smart_addslashes($_POST['password']);
	}

	//check if both fields were filled
	if (!$username)// || !$password)
	{
		$feedback = "<p class=\"Error\">".$login_missing_field."</p>";
	} else {
		SetCookie("GO_AUTH_SOURCE_KEY",
		$_REQUEST['auth_source_key'], time()+3600*24*30,"/",'',0);
		$_COOKIE['GO_AUTH_SOURCE_KEY'] = $_REQUEST['auth_source_key'];

		//attempt login using security class inherited from index.php
		//$params = isset( $auth_sources[$auth_source]) ?  $auth_sources[$auth_source] : false;
		if ($GO_AUTH->login($username, $password, $_SESSION['auth_source']))
		{
			//login is correct final check if login registration was ok
			if ($GO_SECURITY->logged_in == true)
			{
				if ($remind)
				{
					SetCookie("GO_UN",$username,time()+3600*24*30,"/",'',0);
					SetCookie("GO_PW",$password,time()+3600*24*30,"/",'',0);
				}

				//update language
				if(isset($_POST['SET_LANGUAGE']) &&
				$_POST['SET_LANGUAGE'] != $_SESSION['GO_SESSION']['language']['id'])
				{
					$GO_USERS->set_language($GO_SECURITY->user_id, $_POST['SET_LANGUAGE']);
				}

				if ($_SESSION['GO_SESSION']['first_name'] == '' ||
				$_SESSION['GO_SESSION']['last_name'] == '' ||
				$_SESSION['GO_SESSION']['email'] == '')
				{
					header("Location: ".$GO_CONFIG->host.
					"configuration/index.php");
					exit();
				}else
				{
				//	echo "value is ".$username;
					//exit;
			
					$_SESSION["uname"]= $username;
					$_SESSION['pw']= $password;
					$start_module = $GO_MODULES->get_module($_SESSION['GO_SESSION']['start_module']);
#echo("there");exit();
/*( zed - create chat file here */

$uname = $_SESSION['uname'];
$pw = $_SESSION['pw'];

$f_path = "../webChat/".$uname."_webChat.html";
$if_path = "../webChat/if_".$uname."_webChat.html";
$if_url = "if_".$uname."_webChat.html";
umask(0177);

if(!($handle = fopen($if_path, "w"))) {
	echo "cannot open file ($if_path)";
	exit();
} else {

$htmlcontent=  "
<HTML>
<HEAD>
   <TITLE>AVW webChat</TITLE>
</HEAD>
<BODY BGCOLOR='white'>
<CENTER>
<TABLE WIDTH='100%' border='0'>
<TR>
<TD>
<!--
<APPLET archive='s_e4applet.jar' code='e4Applet.class' width=225 height=400>
<PARAM NAME='SERVER' VALUE='61.17.250.24'>
<PARAM NAME='muc'	 VALUE='conference.pc100.local' >
<PARAM NAME='bgcolor' VALUE='0xFFFFFF'>
<PARAM NAME='chat_color' VALUE='0x0AF00A'>
<PARAM NAME='USER' VALUE='".$uname."'>
<PARAM NAME='PASSWORD' VALUE='".$pw."'>
<PARAM NAME='resource' value='AVW Chat'>
</APPLET>
-->
	<APPLET name=\"avw_chat\" 
	archive=\"sapplet.jar,plugins/alertwindow.jar,plugins/filetransfer.jar,plugins/vcard.jar,plugins/titlescroller.jar,plugins/emoticons.jar,plugins/groupchat.jar,plugins/sound.jar,plugins/xhtml.jar\"  
	CODE=\"nu.fw.jeti.applet.Jeti.class\" width=\"100%\" height=\"325px\" NAME=\"jeti\">

	<param name=\"USER\" value=\"".$uname."\">
	<param name=\"PASSWORD\" value=\"".$pw."\">
	<param name=\"RESOURCE\" value=\"AVW WebChat\">
	<param name=\"SERVER\" value=\"61.17.250.23\">
	</APPLET>
</TD>
</TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
";
	if(fwrite($handle , $htmlcontent) == FALSE){
		echo "cannot write to file ($f_path)";
		exit;
	}
	fclose($handle);
}

if(!($handle = fopen($f_path, "w"))) {
	echo "cannot open file ($f_path)";
	exit();
} else {
$htmlcontent=  "
<html>
<head>
<title>AVW webChat</title>
<link rel='stylesheet' href='../mvw/avw_coll.css' type='text/css'>
<link rel='stylesheet' href='../wfx/css/avw.css' type='text/css'>
</head>
<body class='body_l' onContextMenu='return false;'>
<table border='1' width='100%'>
  <tbody>
    <tr class='sec_row'>
      <td align='center' valign='top'>
      <iframe id='if_webChat_id' name='if_webChat' src='".$if_url."' scrolling='auto' width='100%' height='100%'></iframe>
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>
";
	if(fwrite($handle , $htmlcontent) == FALSE){
		echo "cannot write to file ($f_path)";
		exit;
	}
	fclose($handle);
}

session_register('f_path');
session_register('if_path');

/* ) */
					if(!$start_module ||
					(!$GO_SECURITY->has_permission($GO_SECURITY->user_id,$start_module['acl_read']) &&
					!$GO_SECURITY->has_permission($GO_SECURITY->user_id,$start_module['acl_wrtite'])))
					{
						if($modules = $GO_MODULES->get_modules_with_locations())
						{
							while($module = array_shift($modules))
							{
								if($GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_read']) ||
								$GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_write']))
								{
									$start_module = $module;
									$GO_USERS->set_start_module($GO_SECURITY->user_id, $module['id']);
									break;
								}
							}
						}
					}

					if (isset($_REQUEST['return_to']))
					{
						$link = $_REQUEST['return_to'];
					} elseif ($start_module)
					{
						$link = $start_module['url'];
					} else
					{
						$link = $GO_CONFIG->host.'configuration/index.php?account=look.inc';
					}
					//redefine theme
					$GO_THEME = new GO_THEME();
					require_once($GO_THEME->theme_path."frames.inc");
					exit();
				}
			}else
			{
				$feedback = "<p class=\"Error\">".$login_registration_fail."</p>";
			}
		}else
		{
			$feedback = "<p class=\"Error\">".$login_bad_login."</p>";
		}
	}
}
require_once('login_screens/'.$GO_CONFIG->login_screen.'/login.inc');
//print 'login_screens/'.$GO_CONFIG->login_screen.'/login.inc';
?>