<?
function get_user_input_option ($options, &$ret,$min,$max) { 

$loop = TRUE;

while ( $loop ) { 

$i = 1;

while ( list($k,$e) = each ( $options ) ) {

$agi->text2wav('Press '. $i.' for '. $e.' and press the pound sign');

$ret[$i] = $e;

}

$dtmf = implode('',get_dtmf());

if (($dtmf != NULL) && ($dtmf >= $min ) && ($dtmf <= $max) ) {

return $ret = $ret[$dtmf];

$loop = FALSE;

} else {

$agi->text2wav(_INVALID_OPTION);

$loop = TRUE;

}

}

}


function get_dtmf ( $len=1, $timeout=20000,$terminator='#',$prompt=FALSE) {

return $agi->agi_getdtmf($len,$timeout,$terminator='#',$prompt=FALSE);


}


function send_mail ( $carrier, $message, $phone, &$count ) {

$count++;

return @mail ( $phone.'@'.$carrier,$message.$count);


}






function execute_old($command) { 
   GLOBAL $in, $out; 
   fputs($out, $command . " \n"); 
   fflush($out); 
   $data = fgets($in, 4096); 
   if (preg_match(" ^([0-9]{1,3}) (.*) ", $data, $matches)) { 
       if (preg_match(' ^result=([0-9a-zA-Z]*)( ?\((.*)\))?$ ', $matches[2], $match)) { 
           $arr['code'] = $matches[1]; 
           $arr['result'] = $match[1]; 
           if (isset($match[3]) && $match[3]) 
               $arr['data'] = $match[3]; 
           return $arr; 
       } else return 0; 
   } else return -1; 
} 



function fetch_vars ( &$stdin  ) { 
while (!feof($stdin)) { 
  $temp = fgets($stdin); 
  $temp = str_replace("\n","",$temp); 
  $s = explode(":",$temp); 
  $agivar[$s[0]] = trim($s[1]); 
  if (($temp == "") || ($temp == "\n")) { 
     break; 
     } 
  }
return $agivar;
}


function myErrorHandler($errno, $errstr, $errfile, $errline) 
 {
   switch ($errno) {
   case FATAL:
    $ret .= "<b>FATAL</b> [$errno] $errstr<br />\n";
    $ret .= "  Fatal error in line $errline of file $errfile";
    $ret .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
    $ret .= "Aborting...<br />\n";
    write_to_file(_LOG, $ret);
    exit(1);
    break;
   case ERROR:
    $ret .= "<b>ERROR</b> [$errno] $errstr<br />\n";
    write_to_file(_LOG, $ret);
    break;
   case WARNING:
    $ret .= "<b>WARNING</b> [$errno] $errstr<br />\n";
    write_to_file(_LOG, $ret);
    break;
   default:
    $ret .= "Unkown error type: [$errno] $errstr<br />\n";
    write_to_file(_LOG, $ret);
    break;
   }
 }


function write_to_file ($file,$write ) { 

$fp = fopen($file,'a');

// on error could loop
@fwrite($fp,$write);

return TRUE;

}





