<?php
/************************************************************************/
/* ajax_logout.php                                                      */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// This script is stored out of the way in the ajax folder, but needs to
// act as if it were called from folder above (scripts) so that paths
// used in require_once files remain valid. Hence, chdir to the dir above.
chdir('..');

// Include mailserver module
require_once('inc/mailserver.php');

// Get $_SESSION array (not used but useful for LogAccess() message)
session_start();

// Record client's time for this request
$ajax_timestamp = 0;
if( isset( $_GET['timestamp'] ) )
{
  $l = strlen($_GET['timestamp']);
  if( $l > 3 )
  {
    $time_string = substr($_GET['timestamp'],0,$l-3);
    $ajax_timestamp = (int)$time_string;
  }
}

// Record page access
GetEventLog()->LogAccess();

// Connect to mail server
$mailserver = GetMailserver();
if( !$mailserver )
{
  $res = 'error+++ajax_logout+++' . gettext("Can't connect to mailserver.");
  GetEventLog()->WriteError( gettext("Can't connect to mailserver.") );
  header('Content-Type: text/plain; charset="utf-8"');
  header('Content-Length: ' . strlen($res));
  echo $res;
  exit;
}

$res = 'error+++ajax_logout+++' . gettext("No session to logout.");

// Tell mailserver that session has ended.
if( isset($_SESSION[ SessionHandler::USER_ID ]) )
{
  $userId = $_SESSION[ SessionHandler::USER_ID ];
  $res = $mailserver->LogoutUser(session_id(), $userId);
}

GetSessionHandler()->DestroySession();

// Output the resulting html to the stream.
header('Content-Type: text/plain; charset="utf-8"');
header('Content-Length: ' . strlen($res));
echo $res;

?>