<?php
class amifunc{
	 var $mysocket;
	 var $myerror;
	   
function amifunc()
{
   
   $this->mysocket = FALSE;
   $this->myerror = "";
} 

function Login($host="localhost",$port="5038", $username="migue", $password="migue"){
  
 $this->mysocket = fsockopen($host,$port,$errno, $errstr, 1); 
 if (!$this->mysocket) {
     $this->myerror =  "Could not connect - $errstr ($errno)";
     return FALSE;
 }else{
     stream_set_timeout($this->mysocket, 1); 
     $query="Action: Login\r\nUserName: $username\r\nSecret: $password\r\n\r\n"; 
     $wrets = $this->Query($query); 

     if (strpos($wrets, "Message: Authentication accepted") != FALSE){
         return true;
     }else{
        $this->error = "Could not login - Authentication failed";
        fclose($this->mysocket); 
        $this->mysocket = FALSE;
	    return false;
     }
 }	
}
function Originate($sChannel,$sNumber,$CallerID){
    
	$sQuery = "Action: Originate\r\n";
	$sQuery.= "Channel: ". $sChannel . $sNumber . "\r\n";
	$sQuery.= "Callerid: " . $CallerID . "\r\n";
	$sQuery.= "Application: Playback\r\n";
	$sQuery.= "Data: tt-monkeys\r\n\r\n";
	
	$wrets=$this->Query($sQuery);
	return $wrets;
	
	
}

function DBPut($family,$key,$value){
    
	$sQuery = "Action: DBPut\r\n";
	$sQuery.= "Family: ". $family . "\r\n";
	$sQuery.= "Key: " . $key . "\r\n";
	$sQuery.= "Val: " . $value . "\r\n\r\n";
	
	$wrets=$this->Query($sQuery);
	return $wrets;
	
	
}

function deltree($family){
    
	$sQuery = "Action: command\r\n";
	$sQuery.= "command: database deltree ". $family . "\r\n\r\n";
	
	$wrets=$this->Query($sQuery);
	return $wrets;
	
	
}

function del($family,$key){
    
	$sQuery = "Action: command\r\n";
	$sQuery.= "command: database del ". $family . " " . $key . "\r\n\r\n";
	$wrets=$this->Query($sQuery);
	return $wrets;
}

function SipShowRegistry(){
	$wrets=$this->Query("Action: Command\r\ncommand: Sip Show Registry\r\n\r\n");
	return $wrets;
}

function ShowChannels(){
	$wrets=$this->Query("Action: Command\r\ncommand: Core Show Channels\r\n\r\n");
	return $wrets;
}

function DatabaseShow(){
	$wrets=$this->Query("Action: Command\r\ncommand: Database Show\r\n\r\n");
	return $wrets;
}

function GsmShowStatus(){
	$wrets=$this->Query("Action: Command\r\ncommand: gsm show status\r\n\r\n");
	return $wrets;
}

function MisdnShowStacks(){
	$wrets=$this->Query("Action: Command\r\ncommand: misdn show stacks\r\n\r\n");
	return $wrets;
}

function SnomReboot($name){
	$wrets=$this->Query("Action: Command\r\ncommand: sip notify reboot-snom $name\r\n\r\n");
	return $wrets;
}

function Logout(){
 if ($this->mysocket){
    fputs($this->mysocket, "Action: Logoff\r\n\r\n"); 
    while (!feof($this->mysocket)) { 
         $wrets .= fread($this->mysocket, 8192); 
    } 
    fclose($this->mysocket); 
    $this->mysocket = "FALSE";
 }
return;  
}

function SendSMS($port,$number,$text){
    
    $wrets=$this->Query("Action: GsmSendSms\r\nport: $port\r\nnumber: $number\r\ntext: $text\r\n\r\n");
    return $wrets;
}
function ExtReload(){
    $wrets=$this->Query("Action: Command\r\ncommand: Extensions Reload\r\n\r\n");
    return $wrets;
}	
function Reload(){
    $wrets=$this->Query("Action: Command\r\ncommand: Reload\r\n\r\n");
    return $wrets;
}	
function Restart(){
    $wrets=$this->Query("Action: Command\r\ncommand: restart now\r\n\r\n");
    return $wrets;
}
function Query($query)
{
   $wrets = "";
    
   if ($this->mysocket === FALSE)
       return FALSE;
		       
   fputs($this->mysocket, $query); 
   $i;
   do
   {
     $i++;
     $line = fgets($this->mysocket, 4096);
     $wrets .= $line;
     $info = stream_get_meta_data($this->mysocket);
   }while ($line != "\r\n" && $infotimed_out>'timed_out' == false );	
   return $wrets;
} 
}
