<?php
/**
 * Klasse Fulltrace
 */

class Sms {
	function port(){
		$ba=new beroAri();
		$query=$ba->select("SELECT port FROM gsm order by port asc");
		$ret="";
		while ($entry=$ba->fetch_array($query)){
			$ret.="<option value='".$entry['port']."'>".$entry['port']."</option>";
		}
		return $ret;
	}
	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display() {

		$ba = new beroAri();

		include('./includes/header.php');
		require_once ('./includes/isgwtelnet.php');
		if (isset($_POST['number']) && isset($_POST['port'])){
			if (strlen($_POST['text'])<=670){
				if (!is_dir("/tmp/sms/out")) mkdir("/tmp/sms/out",0777,true);
				$file = '/tmp/sms/out/'.uniqid().'.sms';
				// Open the file to get existing content
				$content = "num=".$_POST['number']."\n".
							"port=".$_POST['port']."\n".
							"msg=".$_POST['text'];
				// Write the contents back to the file
				file_put_contents($file, $content);
				//SMS sofort senden
				$isgw = new isgwtelnet();
				$result=$isgw->isgw_login();
				if ($result!=false) $isgw->ssms();
			}else{
				$ret.="<br/><font color='red'>Error!</font>";
			}
		}

$ret.="<script type='text/javascript'>function contentDisp(filename)
{
$.ajax({
url : 'misc/fileContent.php?filename='+filename,
success : function (data) {
$('#dialog').html(data);

$('#dialog').dialog();
}
});
}</script>
<div id='dialog' title='SMS'></div>

";
	//Die Tabelle einkommende SMS

$dir = "/tmp/sms/in/";
$dirFail= "/tmp/sms/failout/";
$ret.="<script type='text/javascript'>
function del(filename,opt){
	conf = confirm('".SMS_DELETE_MESSAGE_DIS."?');
	if (conf==false)
		return false;
	else{
		window.location='./misc/deletedatei.php?option='+opt+'&filename='+filename;
	}
}
		</script>
<br/>
<form name='sendSMS' class='extensions_form' onsubmit='return(check_phone_number(number.value));' method='POST'>
<table><tr><td>".SMS_PORT_DIS."</td><td><select name='port'>"
.$this->port().
"</select></td></tr>
<tr><td>".SMS_NUMBER_DIS."</td><td><input name='number'/></td></tr>
<tr><td style='vertical-align:top;'>".SMS_TEXT_DIS."</td><td><textarea name='text' cols='40' rows='8' id='smsmessageId' onkeyup=\"countAndLimitChars('smscountline','smsmessageId','".SMS_REMAINING_CHARS_DIS."')\"
    onkeydown=\"countAndLimitChars('smscountline','smsmessageId','".SMS_REMAINING_CHARS_DIS."')\"></textarea>
<div id='smscountline'>".SMS_REMAINING_CHARS_DIS.": 670 </div>
</td></tr>
<tr><td></td><td><input type='submit' value='Send'/></td></tr>
</table>
</form>";
if (is_dir("/tmp/sms/in")){
$ret.="<br/><table class='contenttoc'>
		   <tr>
			   <th colspan='2'>".SMS_INBOX_DIS."</th>
		   </tr>";
foreach (glob($dir."*.sms") as $filename){

    $ret.= " <tr>
			   <td><a onclick='contentDisp(\"/tmp/sms/in/".basename($filename)."\")' onmouseover='$(this).css(\"cursor\", \"pointer\")'>".basename($filename)."</a></td>
			   <td style='width:30px;'><a class='popup_download' onclick='javascript:del(\"".basename($filename)."\",\"sms\")'>
            	    <img src='./includes/images/delete.png'></a></td>
		   </tr>";
}
$ret.="</table>";

}

if (is_dir("/tmp/sms/failout/")){
$ret.="<br/><table class='contenttoc'>
		   <tr>
			   <th colspan='5'>".SMS_FAIL_DIS."</th>
		   </tr>
		   <tr>
		   	   <th >".SMS_DATE_DIS."</th>
			   <th >".SMS_DESTINATION_DIS."</th>
			   <th >".SMS_PORT_DIS."</th>
			   <th >".SMS_LOG_DIS."</th>
			   <th ></th>
		   </tr>";

		   if ($handle = opendir($dirFail)) {
				while (false !== ($file = readdir($handle))) {
					if ($file != "." && $file != "..") {
						$content=file($dirFail.$file."/message");
						$ret.= " <tr>
									<td>".date("d M Y H:i",filectime($dirFail.$file."/message"))."</td>";
						foreach ($content as $line){
							if (strpos($line,"port")!==false){ $v=explode("=",$line); $ret.= " <td>".$v[1]."</td>"; }
							if (strpos($line,"num")!==false){ $v=explode("=",$line); $ret.= " <td>".$v[1]."</td>"; }
						}

						$ret.= "<td><a onclick='contentDisp(\"".$dirFail.$file."/message.log\")' onmouseover='$(this).css(\"cursor\", \"pointer\")'>Log</a></td>
						   <td style='width:30px;'><a class='popup_download' onclick='javascript:del(\"".$file."\",\"smsFail\")'>
								<img src='./includes/images/delete.png'></a></td>
					   </tr>";
					}
				}
				closedir($handle);
			}
$ret.="</table>";

}
$ret.="<br/>";

	//Rückgabe: Der Inhalt von der HTML-Seite
	return $ret;
}
}
?>
