<?php
class isgwtelnet{
	var $mysocket;
	var $myerror;

	function isgwtelnet()
	{

		$this->mysocket = FALSE;
		$this->myerror = "";
	}
	function isgw_login($host="localhost",$port="54322"){
		try{
			$this->mysocket = @fsockopen($host,$port,$errno, $errstr, 1);
		}catch(Exception $e){
			return FALSE;
		}

		if (!$this->mysocket) {
			$this->error =  "Could not connect - $errstr ($errno)";
			return false;
		}else{
			stream_set_timeout($this->mysocket, 1);
			$query="";
			$wrets = $this->Query($query);
			return "wrets" . $wrets;
			if (strpos($wrets, "ISGW") != FALSE){
				return "Yeah";
			}else{
				$this->error = "Could not connect - $errstr ($errno)";
				fclose($this->mysocket);
				$this->mysocket = FALSE;
				return false;
			}
		}
	}

	function isgw_transparent($command){
		$query=$command;
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_isdnstate(){
		$query="i";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_gsmstate(){
		$query="g";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_berocapistate(){
		$query="R";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_actualcalls(){
		$query="ss";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_bchannels(){
		$query="bc";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_callcompact(){
		$query="k";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_analogstate(){
		$query="a";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_uptime(){
		$query="u";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_sipstate(){
		$query="r";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_channelstate()
	{
		$query="k";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_pcmstate()
	{
		$query="pcm";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_portstatistic()
	{
		$query="ssp";
		$wrets=$this->Query($query);
		return $wrets;
	}


	function isgw_dialplanreload()
	{
		$query="rd";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_rcpt()
	{
		$query="rcpt";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function isgw_rgc()
	{
		$query="rgc";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function itpX($port)
	{
		$query="itp".$port;
		$wrets=$this->Query($query);
		return $wrets;
	}

	function lla()
	{
		$query="lla";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function lld()
	{
		$query="lld";
		$wrets=$this->Query($query);
		return $wrets;
	}


	function isgw_sipreload()
	{
		$query="rs";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function flush_cdr()
	{
		$query="fc";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function ssms()
	{
		$query="ssms";
		$wrets=$this->Query($query);
		return $wrets;
	}

	function port_restart($port)
	{
		$query="pr".$port;
		$wrets=$this->Query($query);
		return $wrets;
	}

	function port_restart_gsm($port,$lif)
	{
		$query="poogli".$lif;
		$wrets=$this->Query($query);
		if (strpos($wrets,'finished')!==false)
		foreach ($port as $value){
			$query="rgp".$value;
			$wrets=$this->Query($query);
		}
		return $wrets;
	}

	function loglevel($level=0){
		$query="v";
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
		$query=$level;
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
	}

	function loglevel_message($level=0){
		$query="m";
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
		$query=$level;
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
	}

	function acl($acl=""){
		$query="acl:".$acl;
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
	}

	function logactive($state){
		$query="ts";
		$wrets=$this->Query($query);
		if (strlen($wrets)==16 && $state==1){
			$query="t";
			if ($this->mysocket === FALSE)
				return FALSE;
			fputs($this->mysocket, $query);
		}elseif(strlen($wrets)==15 && $state==0){
			$query="t";
			if ($this->mysocket === FALSE)
				return FALSE;
			fputs($this->mysocket, $query);
		}
	}

	function logactive_message($state){
		if ($state==1){
			$query="lm1";
			if ($this->mysocket === FALSE)
				return FALSE;
			fputs($this->mysocket, $query);
		}elseif($state==0){
			$query="lm0";
			if ($this->mysocket === FALSE)
				return FALSE;
			fputs($this->mysocket, $query);
		}
	}

	function logserver($ip="127.0.0.1",$port=54323){
		$query="n";
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
		$query=$ip.":".$port;
		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
	}

	function isgw_dialplanconfig()
	{
		$query="dc";
		$wrets=$this->Query($query);
		return $wrets;
	}
	function Query($query)
	{
		$wrets = "";

		if ($this->mysocket === FALSE)
			return FALSE;

		fputs($this->mysocket, $query);
		$i=0;
		do
		{
			$i++;
			//Wenn ein GSM Port neugestartet wird, braucht die Anfage länger als eine Sekunde, deswegen wird die Timeout auf 30 erhöht
			(strpos($query,"poogli")===false && strpos($query,"rgp")===false && $query!="g") ? stream_set_timeout($this->mysocket,1) : stream_set_timeout($this->mysocket,30);
        	$info = stream_get_meta_data($this->mysocket);
			$line = fgets($this->mysocket, 4096);
			$wrets .= $line;
			$info = stream_get_meta_data($this->mysocket);
		}while ($line!="\r\n" && !$info['timed_out'] );

		return $wrets;
	}

	function isgw_login_dialplan($host="localhost",$port="54322"){
		try{
			$this->mysocket = @fsockopen($host,$port,$errno, $errstr, 1);
		}catch(Exception $e){
			return FALSE;
		}

		if (!$this->mysocket) {
			$this->error =  "Could not connect - $errstr ($errno)";
			return false;
		}else{
			stream_set_timeout($this->mysocket, 1);
			return true;
		}
	}
	function isgw_verbose()
	{
		$query="verbose";
		$this->Query_dialplan($query);
	}
	function Query_dialplan($query,$pid)
	{
		if ($this->mysocket === FALSE)
			return FALSE;
		$time=time();
		fputs($this->mysocket, $query);
		$wrets="";
		$show=FALSE;
		do
		{
			$fp= fopen("/tmp/dialplan.txt","a+");
			$write=fputs($fp,$pid);
			//$i++;
			$line = fgets($this->mysocket, 4096);
			if (strpos($line,"disconnect client")!== FALSE){
				 $show=TRUE;
				 $line="";
			 }
			 //if ($fp)
		 	stream_set_timeout($this->mysocket, 60);
			if (($line!="") && ($show==TRUE)) 	$write=fputs($fp,$line);
			fclose($fp);
		}while (filesize("/tmp/dialplan.txt")<512000 && time()<$time+1800);
		exec("/bin/mv /tmp/dialplan.txt /tmp/dialplan_d.txt");
		fclose($this->mysocket);

	}

}
