<?php
/************************************************************************/
/* ajax_get_mailboxes.php                                               */
/*----------------------------------------------------------------------*/
/* Retrieves data for the list of extns/huntgroups                      */
/* for the Forward option.                                              */
// Ajax handler for $mailserver->GetMailboxes() which in turn calls     */
/* COM::GetMailboxes()                                                  */
/*----------------------------------------------------------------------*/
/* 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('..');

// For Disinfect()
require_once('inc/common.php');

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

// Recreate $_SESSION variable
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 (also writes session id)
GetEventLog()->LogAccess();

// Check that visitor should be here. Authenticate populates $user record.
$user = null;
$mailserver = null;
$reason = GetSessionHandler()->Authenticate( session_id(), $user );
if ( $reason != SessionHandler::AUTH_SUCCESS )
{
  $res = 'auth+++ajax_get_forward_data+++'. $reason;
  GetSessionHandler()->DestroySession();
  GetEventLog()->WriteError( gettext('Authentication failed:') . ' ' . GetSessionHandler()->ErrorText($reason) );
  header('Content-Type: text/plain; charset="utf-8"');
  header('Content-Length: ' . strlen($res));
  echo $res;
  exit;
}
else
{
  // Connect to mail server
  $mailserver = GetMailserver();
  if( !$mailserver )
  {
    $res = 'error+++ajax_get_forward_data+++' . 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;
  }

  // Keep session active
  $mailserver->UpdateLastAction( session_id(), $user->id, 0 );
}

// Initialise script output
$res = 'error+++ajax_get_forward_data+++' . gettext('Unspecified error.');

$res = $mailserver->GetMailboxes( session_id(), $user->id, Forward::EXTN );
$res .= ";";
$res .= $mailserver->GetMailboxes( session_id(), $user->id, Forward::HUNT_GROUP );
//Example output:
//$res = '{2293853B-4256-4678-A3B3-33CB37BB4450}|201|Extn201;'
//       '{D5DAD569-AEE9-49C3-89E2-D2D9B2D7EE2A}|202|Extn202;' ;
//       '{49CE9569-9CE9-29C1-D9E2-1D7EB2D79E02}|200|Main;'
//       '{9CDAD565-AE9C-1943-39BD-D2D9B2D7EE2A}|300|Sales;' ;

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


?>