<?
/**
 * VoiceOne Webservices Interface
 *
 * @package voiceone_webservices
 * @subpackage  db_asterisk
 */

//******************************************************************************
//****************              ASTERISK DATABASE              *****************
//******************************************************************************

// ---- putInDB(family:string,key:string,value:string):int ---------------------
function putInDB($family, $key, $value){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database put ".$family." ".$key." ".$value;
  $ret=sendCommand($cmd);
  return 1;
}

// ---- rmFromDB(family:string,key:string):int ---------------------------------
function rmFromDB($family, $key){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database del ".$family." ".$key;
  $ret=sendCommand($cmd);
  return 1;
}

// ---- getFromDB(family:string,key:string):commandReturn ----------------------
function getFromDB($family, $key){
  if(($family!="DND")&&($family!="CFU")&&($family!="CFB")&&($family!="CFNR")) return 0;
  $cmd="database get ".$family." ".$key;
  $ret=sendCommand($cmd);
  $retArr=explode("\n",$ret);
  $present=-1;
  $value="error";
  if(is_array($retArr)){
    foreach($retArr as $line){
      if(strstr($line, "Value: ")){
        $present=1;
        $value=substr($line, 7);
        break;
      }else if(strstr($line, "entry not found")){
        $present=0;
        $value="";
        break;
      }
    }
  }
  $res=Array( "present" => $present, "value" => $value );
  return $res;
}

// ---- getClientsDBEntry():clientsDBEntryArray --------------------------------
function getClientsDBEntry(){
  $cmd="database show";
  $ret=sendCommand($cmd);
  $retArr=explode("\n",$ret);
  if(is_array($retArr)){
    foreach($retArr as $line){
      list($empty,$key,$extension_value)=explode("/",$line,3);
      if(($key=="DND")||($key=="CFU")||($key=="CFB")||($key=="CFNR")){
        list($extension,$value)=explode(":",$extension_value);
        $extension=trim($extension);
        $matrix[$extension][$key]=$value;    
      }  
    }
  }
  return $matrix;
}
