<?
/**
 * VoiceOne Webservices Methods
 *  
 * @package voiceone_webservices
 * @subpackage recording
 */
//******************************************************************************
//******************           PUBLIC FUNCTIONS            *********************
//******************************************************************************
// ---- Function getRecordedCalls ----------------------------------------------
/**
* Parse monitor folder and return the list of the recorded calls
*
* @return:RecordedCallArray -> array of recorded calls
*/
function getRecordedCalls(){
  global $db, $voTools, $voiceoneWSDocumentRoot;
  $ret=array();
  // read monitor dir and create recording list file
  $topath = $voiceoneWSDocumentRoot."/sandbox";
  $file_list = $topath."/recordingList.txt";
  exec("sudo $voTools readMonitorDir $file_list",$output,$retScript);
	// extract file list
  if(is_file($file_list)) {
  	$lines = file($file_list);
   	// process files list
   	foreach($lines as $line){
   		$line=str_replace(array("\n","\r"),"",trim($line));
  		$k=explode(".", $line);
      $l=explode("-", $k[0]);
      if(trim($l[0])=="auto"){
      	$recorded_on_demand=1;
      }else{
      	$recorded_on_demand=0;
      }
      //$call_date=date('F j, Y \a\t g:i a', $l[1]);
      $call_date=$l[1];
      $caller=$l[2];
      $callee=$l[3];
      switch($l[4]){
      	case "INC":
      		$call_type=1;
      		break;
       	case "OUT":
       		$call_type=2;
       		break;
       	case "INT":
       		$call_type=3;
       		break;
       	default:
       		$call_type=0;
       		break;
      }
      $entry=array(
      		"call_date" 	=> $call_date,
      		"caller" 			=> $caller,
      		"callee" 			=> $callee,
      		"call_type" 	=> $call_type,
      		"recorded_on_demand" => $recorded_on_demand,
      		"file_name" 	=> $line
      	);
      $ret[]=$entry;
    }
  }
  if(is_array($ret)) $ret=array_reverse($ret,false);
  return $ret;
}
// ---- Function delRecordedCall ----------------------------------------------
/**
* Delete a recorded call
*
* @param: filename -> name of the audio file
* @return:int -> 1 on success, 0 on error
*/
function delRecordedCall($filename){
  global $db, $voTools;
  if(trim($filename)=='') return 0;
  // read monitor dir and create recording list file
  exec("sudo $voTools delMonitorfile $filename",$output,$retScript);
  return 1;
}
// ---- Function mvMonitorFileToSandbox ----------------------------------------------
/**
 * Check if a monitor file is in voiceone_ws's sandbox. If not, it copies it in sandbox 
 *
 * @param string $filename name to check
 * @return int $ret 1 if message is present, 0 if not
 */
function mvMonitorFileToSandbox($filename) {
  global $db, $voTools, $voiceoneWSDocumentRoot;
  // clean sandbox/sounds
  $topath=$voiceoneWSDocumentRoot."/sandbox/monitor";
  $audioList=glob($topath."/*");
  foreach($audioList as $file){
    exec("sudo $voTools monitorClear $file",$output,$retScript);
  } 
  // move file
  exec("sudo $voTools monitorMoveToSandbox $filename $topath",$output,$retScript); 
  // verify and return
  if(file_exists($topath."/".$filename))  return 1;
  else return 0;
}
 
?>