<?php
/* $Id$ */
/*
	freeswitch_status.php
	Copyright (C) 2008 Mark J Crane
	All rights reserved.

	FreeSWITCH (TM)
	http://www.freeswitch.org/


	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require("guiconfig.inc");
require("/usr/local/pkg/freeswitch.inc");


if ($_GET['a'] == "download") {
  if ($_GET['t'] == "logs") {
    $tmp = '/usr/local/freeswitch/log/';
    $filename = 'freeswitch.log';
  }
  if ($_GET['t'] == "cdrcsv") {
    $tmp = '/usr/local/freeswitch/log/cdr-csv/';
    $filename = 'Master.csv';
  }
  if ($_GET['t'] == "backup") {
    $tmp = '/root/backup/';
    $filename = 'freeswitch.bak.tgz';
	if (!is_dir('/root/backup/')) {
	   exec("mkdir /root/backup/");
	}
    system('cd /usr/local/;tar cvzf /root/backup/freeswitch.bak.tgz freeswitch');
  }
	session_cache_limiter('public');
	$fd = fopen($tmp.$filename, "rb");
	header("Content-Type: binary/octet-stream");
	header("Content-Length: " . filesize($tmp.$filename));
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	fpassthru($fd);
	exit;
}

if ($_GET['a'] == "other") {
  if ($_GET['t'] == "restore") {
    $tmp = '/root/backup/';
    $filename = 'freeswitch.bak.tgz';

    //extract a specific directory to /usr/local/freeswitch
    if (file_exists('/root/backup/'.$filename)) {
      //echo "The file $filename exists";

	  //Recommended
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/db/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/log/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/recordings/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/scripts/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/storage/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/sounds/custom/8000/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/sounds/music/8000/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/ssl');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/sip_profiles/');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/vars.xml');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/dialplan/default.xml');
		system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/dialplan/public.xml');		

	  //Optional
		//system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/conf/');
		//system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/grammar/');
		//system('cd /usr/local; tar xvpfz /root/backup/'.$filename.' freeswitch/htdocs/');

		header( 'Location: freeswitch_status.php?savemsg=Backup+has+been+restored.' ) ;
    }
    else {
        header( 'Location: freeswitch_status.php?savemsg=Restore+failed.+Backup+file+not+found.' ) ;
    }

	exit;
  }
}

include("head.inc");

$password = $config['installedpackages']['freeswitchsettings']['config'][0]['event_socket_password'];
$port = $config['installedpackages']['freeswitchsettings']['config'][0]['event_socket_port'];
$host = $config['interfaces']['lan']['ipaddr'];

$savemsg = $_GET["savemsg"];

//if freeswitch is not running then start it
if (!pkg_is_service_running('freeswitch')) {
	$handle = popen("/usr/local/etc/rc.d/freeswitch.sh start", "r");
	pclose($handle);
	//give freeswitch time to load
	sleep(7);
}
?>

<script language="Javascript" type="text/javascript" src="/edit_area/edit_area_full.js"></script>
<script language="Javascript" type="text/javascript">
	// initialisation
	editAreaLoader.init({
		id: "log"	// id of the textarea to transform		
		,start_highlight: false
		,allow_toggle: true
		,display: "later"
		,language: "en"
		,syntax: "html"	
		,toolbar: "search, go_to_line,|, fullscreen, |, undo, redo, |, select_font, |, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help"
		,syntax_selection_allow: "css,html,js,php,xml,c,cpp,sql"
		,show_line_colors: true
	});	
</script>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>

<p class="pgtitle">FreeSWITCH: Status</font></p>

<?php
if ($savemsg) {
  print_info_box($savemsg);
}
?>

<div id="mainlevel">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td class="tabnavtbl">
<?php

display_top_tabs(build_menu());
 	
?>
</td></tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
   <tr>
     <td class="tabcont" >

<?php

echo "<br /><br />\n\n";


$fp = event_socket_create($host, $port, $password);
$cmd = "api sofia status";
$response = event_socket_request($fp, $cmd);
echo "<table width='690' cellpadding='0' cellspacing='0' border='0'>\n";
echo "<tr>\n";
echo "<td width='50%'>\n";
echo "  <b>sofia status</b> \n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='reloadxml' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+reloadxml';\" />\n";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<pre style=\"font-size: 9pt;\">\n";
echo $response;
echo "</pre>\n";
fclose($fp);
echo "<br /><br />\n\n";

foreach (ListFiles('/usr/local/freeswitch/conf/sip_profiles') as $key=>$sip_profile_file){
	
	$sip_profile_name = str_replace(".xml", "", $sip_profile_file);
	$fp = event_socket_create($host, $port, $password);
	$cmd = "api sofia status profile ".$sip_profile_name;
	$response = event_socket_request($fp, $cmd);
	echo "<table width='690' cellpadding='0' cellspacing='0' border='0'>\n";
	echo "<tr>\n";
	echo "<td width='50%'>\n";
	echo "  <b>sofia status profile $sip_profile_name</b> \n";
	echo "</td>\n";
	echo "<td width='50%' align='right'>\n";
	echo "  <input type='button' value='start' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+start';\" />\n";
	echo "  <input type='button' value='stop' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+stop';\" />\n";
	echo "  <input type='button' value='restart' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+restart';\" />\n";
	if ($sip_profile_name == "external") {
		echo "  <input type='button' value='rescan' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+rescan';\" />\n";
	}
	else {
		echo "  <input type='button' value='flush_inbound_reg' onclick=\"document.location.href='/packages/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+flush_inbound_reg';\" />\n";
	}
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "<pre style=\"font-size: 9pt;\">\n";
	echo $response;
	echo "</pre>\n";
	fclose($fp);
	echo "<br /><br />\n\n";

}


$fp = event_socket_create($host, $port, $password);
$cmd = "api status";
$response = event_socket_request($fp, $cmd);
echo "<b>status</b><br />\n";
echo "<pre style=\"font-size: 9pt;\">\n";
echo $response;
echo "</pre>\n";
fclose($fp);
echo "<br /><br />\n\n";


$fp = event_socket_create($host, $port, $password);
$cmd = "api show channels";
$response = event_socket_request($fp, $cmd);
echo "<b>show channels</b><br />\n";
if (strlen($response) > 40) {
  echo "<textarea cols='85' rows='10' wrap='off'>\n";
  echo $response;
  echo "</textarea>\n";
}
else {
  echo "<pre style=\"font-size: 9pt;\">\n";
  echo $response;
  echo "</pre>\n";
}
fclose($fp);
echo "<br /><br />\n\n";
echo "<br /><br />\n\n";


$fp = event_socket_create($host, $port, $password);
$cmd = "api show calls";
$response = event_socket_request($fp, $cmd);
echo "<b>show calls</b><br />\n";
if (strlen($response) > 40) {
  echo "<textarea cols='85' rows='10' wrap='off'>\n";
  echo $response;
  echo "</textarea>\n";
}
else {
  echo "<pre style=\"font-size: 9pt;\">\n";
  echo $response;
  echo "</pre>\n";
}
fclose($fp);
echo "<br /><br />\n\n";
echo "<br /><br />\n\n";


echo "<table width='690' cellpadding='0' cellspacing='0' border='0'>\n";
echo "<tr>\n";
echo "<td width='80%'>\n";
echo "<b>Backup / Restore</b><br />\n";
echo "The 'backup' button will tar gzip /usr/local/freeswitch/ to /root/backup/freeswitch.bak.tgz it then presents a file to download. \n";
echo "If the backup file does not exist in /root/backup/freeswitch.bak.tgz then the 'restore' button will be hidden. \n";
echo "Use Diagnostics->Command->File to upload: to browse to the file and then click on upload it now ready to be restored. \n";
echo "<br /><br />\n";
echo "</td>\n";
echo "<td width='20%' valign='middle' align='right'>\n";
echo "  <input type='button' value='backup' onclick=\"document.location.href='/packages/freeswitch/freeswitch_status.php?a=download&t=backup';\" />\n";
if (file_exists('/root/backup/freeswitch.bak.tgz')) {
  echo "  <input type='button' value='restore' onclick=\"document.location.href='/packages/freeswitch/freeswitch_status.php?a=other&t=restore';\" />\n";
}
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";


echo "<table width='690' cellpadding='0' cellspacing='0' border='0'>\n";
echo "<tr>\n";
echo "<td width='50%'>\n";
echo "<b>Call Detail Records</b><br />\n";
echo "/usr/local/freeswitch/log/cdr-csv/Master.csv<br /><br />\n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='download cdr csv' onclick=\"document.location.href='/packages/freeswitch/freeswitch_status.php?a=download&t=cdrcsv';\" />\n";echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";


echo "<table width='690' cellpadding='0' cellspacing='0' border='0'>\n";
echo "<tr>\n";
echo "<td width='50%'>\n";
echo "<b>Logs</b><br />\n";
echo "/usr/local/freeswitch/log/cdr-csv/freeswitch.log<br /><br />\n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='download logs' onclick=\"document.location.href='/packages/freeswitch/freeswitch_status.php?a=download&t=logs';\" />\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";


echo "<b>tail -n 500 /usr/local/freeswitch/log/freeswitch.log</b><br />\n";
echo "<textarea id='log' name='log' cols='93' rows='30' wrap='off'>\n";
echo system("tail -n 500 /usr/local/freeswitch/log/freeswitch.log");
echo "</textarea>\n";
echo "<br /><br />\n\n";


//$fp = event_socket_create($host, $port, $password);
//$cmd = "api sofia ";
//$response = event_socket_request($fp, $cmd);
//echo "<b>api sofia</b><br />\n";
//echo "<pre style=\"font-size: 9pt;\">\n";
//echo $response;
//echo "</pre>\n";
//fclose($fp);
//echo "<br /><br />\n\n";

?>

     </td>
    </tr>
</table>

</div>
<?php include("fend.inc"); ?>

</body>
</html>