<?php
/************************************************************************/
/* ajax_media_play.php                                                  */
/*----------------------------------------------------------------------*/
/* Ajax handler for COM::MediaPlay()                                    */
/*----------------------------------------------------------------------*/
/* 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('..');

// Define Forward, Player, AppMode, Cookie, GetLocale, SessionHandler, Themes and AuthHandler, ErrorToHtml.
require_once('inc/common.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_media_play+++'. $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_media_play+++' . 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_media_play+++' . gettext('Unspecified error.');

// Get parameters from query string
if( !isset( $_POST['file'] ) )
{
  $res = 'error+++ajax_media_play+++Request is missing parameters.';
  GetEventLog()->WriteError( gettext("Request is missing parameters.")
  . " file=\"" . isset($_POST['file']) . "\"" );
}
else
{
  $fileName = Disinfect( $_POST['file'] );
  // Ensure filename has nothing naughty in it
  $fileName = str_ireplace(".WAV","",$fileName);
  $clean_filename="";
  $array = str_split($fileName);
  foreach($array as $char)
  {
    switch($char)
    {
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case 'A':
      case 'B':
      case 'C':
      case 'D':
      case 'E':
      case 'F':
      case 'a':
      case 'b':
      case 'c':
      case 'd':
      case 'e':
      case 'f':
        $clean_filename .= $char;
        break;
    }
  }
  $res = $mailserver->MediaPlay( session_id(), $user->name, $clean_filename );
}
// Output to the stream the resulting HTML
header('Content-Type: text/plain; charset="utf-8"');
header('Content-Length: ' . strlen($res));
echo $res;

?>