<?php
class State_calls {
	function display($args) {

		require_once "./includes/isgwtelnet.php";
		include('./includes/header.php');
		$isgw = new isgwtelnet();
		$result=$isgw->isgw_login();
		if ($result==false) {
			$ret.="<font size='3' color='red'>".STATE_ISWG_ERROR_DIS."</font>";
			return $ret;
		}
		$channelresult=$isgw->isgw_channelstate();

		//LISTING CALL OPERATIONS
		$rows="";
		$rows=explode("\n",$channelresult);
		$ret.= "<br><br>";
		$ret.= "<table class='contenttoc active' id='table'>";
		$ret.= "<th colspan=6>".ACTIVE_CHANNELS_TABLE_HEADER_DIS."</th>";
		$ret.= "<tr><td><b>".ACTIVE_CHANNELS_DIRECTION_DIS."</b></td><td><b>".ACTIVE_CHANNELS_PORT_DIS."</b></td>";
		$ret.= "<td><b>".ACTIVE_CHANNELS_CHANNEL_DIS."</b></td><td><b>".ACTIVE_CHANNELS_DAD_DIS."</b></td><td><b>".ACTIVE_CHANNELS_OAD_DIS."</b></td><td><b>".ACTIVE_CHANNELS_CALL_TYPE_DIS."</b></td></tr>";
		$i=0;

		$zeilen=array();
		foreach ($rows as $row){
			if (trim($row)!="" && strpos($row,"LISTING CALL OPERATIONS COMPACT")===false){
			$values=explode(" ",$row);
			$zeilen[intval($values[0])]=$row;
			}
		}
		$dontUse=array();
		foreach ($rows as $row){
			$values=explode(" ",$row);
			if (trim($row)!="" && strpos($row,"LISTING CALL OPERATIONS COMPACT")===false && (!in_array(intval($values[0]),$dontUse))){

			if (strpos($values[13],"MSP_BRIDGED")!==false){
			/**
			 * PSTN zu PSTN Einträgen
			 */
				$tmp=explode("=",$values[10]);
				array_push($dontUse,intval($tmp[1]));
				//Werte des zweiten Eintrags
				$values1=explode(" ",$zeilen[intval($tmp[1])]);
				unset($tmp);

				//Call direction
				$CALL_DIRECTION=$values[2];
				$CALL_DIRECTION1=$values1[2];
				$tmp=explode("2",$CALL_DIRECTION);
				$tmp1=explode("2",$CALL_DIRECTION1);
		 		if(trim($tmp[0])=="A"){
					$CALL_DIRECTION="ANALOG";
				}else if(trim($tmp[0])=="I"){
					$CALL_DIRECTION="ISDN";
				}else if(trim($tmp[0])=="G"){
					$CALL_DIRECTION="GSM";
				}

				if(trim($tmp1[1])=="A"){
					$CALL_DIRECTION1="ANALOG";
				}else if(trim($tmp1[1])=="I"){
					$CALL_DIRECTION1="ISDN";
				}else if(trim($tmp1[1])=="G"){
					$CALL_DIRECTION1="GSM";
				}
				$CALL_DIRECTION=$CALL_DIRECTION . "  --> " . $CALL_DIRECTION1;
		 		unset($tmp);
		 		unset($tmp1);

				//DAD
		 		$tmp=explode("=",$values[4]);
		 		$tmp1=explode("=",$values1[4]);
		 		$DAD=$tmp[1];
		 		$DAD1=$tmp1[1];
		 		$DAD=$DAD . " --> " . $DAD1;
		 		unset($tmp);
		 		unset($tmp1);


		 		//OAD
		 		$tmp=explode("=",$values[3]);
		 		$tmp1=explode("=",$values1[3]);
		 		$OAD=$tmp[1];
		 		$OAD1=$tmp1[1];
		 		$OAD=$OAD . " --> " . $OAD1;
		 		unset($tmp);
		 		unset($tmp1);

		 		//Port/Channel
		 		if(strpos($values[5],"P/C")!==false){
		 			$tmp=explode("=",$values[5]);
		 			$portChannel=explode(":",$tmp[1]);
		 			$PORT=$portChannel[0];
		 			$CHANNEL_ISDN=$portChannel[1];
		 			unset($tmp);
		 			unset($portChannel);
		 			//echo "PORT_ISDN=" . $PORT_ISDN. "<br>";
		 		}else{
					$tmp=explode("=",$values[5]);
	 				$PORT=$tmp[1];
		 			$CHANNEL_ISDN="1";
		 			unset($tmp);
				}

		 		if(strpos($values1[5],"P/C")!==false){
		 			$tmp1=explode("=",$values1[5]);
		 			$portChannel1=explode(":",$tmp1[1]);
		 			$PORT1=$portChannel1[0];
		 			$CHANNEL_ISDN1=$portChannel1[1];
		 			unset($tmp1);
		 			unset($portChannel1);
		 			//echo "PORT_ISDN=" . $PORT_ISDN. "<br>";
		 		}else{
					$tmp1=explode("=",$values1[5]);
	 				$PORT1=$tmp1[1];
		 			$CHANNEL_ISDN1="1";
		 			unset($tmp1);
				}
				$PORT=$PORT . "  --> " . $PORT1;
				$CHANNEL_ISDN=$CHANNEL_ISDN . "  --> " . $CHANNEL_ISDN1;

		 		//CALL TYPE
		 		$tmp=explode("=",$values[13]);
		 		$CALL_TYPE=$tmp[1];
		 		if ($CALL_TYPE=="MSP_VOICE_ACTIVE")
					$CALL_TYPE="Voice";
				else if ($CALL_TYPE=="MSP_T38_ACTIVE")
					$CALL_TYPE="T38";
				else if ($CALL_TYPE=="MSP_TONEGEN_ACTIVE")
					$CALL_TYPE="Tonegen";
				else if ($CALL_TYPE=="MSP_BRIDGED")
					$CALL_TYPE="Bridged";
				else if ($CALL_TYPE=="MSP_REMOTE_BRIDGED")
					$CALL_TYPE="Remote Bridged";
		 		unset($tmp);

				$ret.="<tr>";
				$ret.="<td>".$CALL_DIRECTION."</td>";
				$ret.="<td>".$PORT."</td>";
				$ret.="<td>".$CHANNEL_ISDN."</td>";
				$ret.="<td>".$DAD."</td>";
				$ret.="<td>".$OAD."</td>";
				$ret.="<td>".$CALL_TYPE."</td>";
				$ret.="</tr>";
				$i++;
			}else{
				//Call direction
				$CALL_DIRECTION=$values[2];
		 		if(trim($CALL_DIRECTION)=="S2I")	{
		 			$CALL_DIRECTION="SIP   --> ISDN";
				}else if(trim($CALL_DIRECTION)=="S2A"){
					$CALL_DIRECTION="SIP   --> ANALOG";
				}else if(trim($CALL_DIRECTION)=="A2S"){
					$CALL_DIRECTION="ANALOG   --> SIP";
				}else if(trim($CALL_DIRECTION)=="G2S"){
					$CALL_DIRECTION="GSM   --> SIP";
				}else if(trim($CALL_DIRECTION)=="S2G"){
					$CALL_DIRECTION="SIP   --> GSM";
				}else{
					$CALL_DIRECTION="ISDN  --> SIP";
				}
		 		unset($tmp);

		 		//DAD
		 		$tmp=explode("=",$values[4]);
		 		$DAD=$tmp[1];
		 		unset($tmp);

		 		//OAD
		 		$tmp=explode("=",$values[3]);
		 		$OAD=$tmp[1];
		 		unset($tmp);

		 		//Port/Channel
		 		if(strpos($values[5],"P/C")!==false){
		 			$tmp=explode("=",$values[5]);
		 			$portChannel=explode(":",$tmp[1]);
		 			$PORT=$portChannel[0];
		 			$CHANNEL_ISDN=$portChannel[1];
		 			unset($tmp);
		 			unset($portChannel);
		 			//echo "PORT_ISDN=" . $PORT_ISDN. "<br>";
		 		}else{
					$tmp=explode("=",$values[5]);
	 				$PORT=$tmp[1];
		 			$CHANNEL_ISDN="1";
		 			unset($tmp);
				}

		 		//CALL TYPE
		 		$tmp=explode("=",$values[13]);
		 		$CALL_TYPE=$tmp[1];
		 		if ($CALL_TYPE=="MSP_VOICE_ACTIVE")
					$CALL_TYPE="Voice";
				else if ($CALL_TYPE=="MSP_T38_ACTIVE")
					$CALL_TYPE="T38";
				else if ($CALL_TYPE=="MSP_TONEGEN_ACTIVE")
					$CALL_TYPE="Tonegen";
				else if ($CALL_TYPE=="MSP_BRIDGED")
					$CALL_TYPE="Bridged";
				else if ($CALL_TYPE=="MSP_REMOTE_BRIDGED")
					$CALL_TYPE="Remote Bridged";
		 		unset($tmp);

		 		$ret.="<tr>";
				$ret.="<td>".$CALL_DIRECTION."</td>";
				$ret.="<td>".$PORT."</td>";
				$ret.="<td>".$CHANNEL_ISDN."</td>";
				$ret.="<td>".$DAD."</td>";
				$ret.="<td>".$OAD."</td>";
				$ret.="<td>".$CALL_TYPE."</td>";
				$ret.="</tr>";
				$i++;
			}


			}


			}//end foreach
			 $ret.="<tr><td style='text-align:left;' colspan='6'><b>".ACTIVE_CHANNELS_TOTAL_CALLS_DIS.":".$i."</b></td></tr>";
		$ret.="</table>";
		return $ret;

	}
}

?>
