<?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 = '/tmp/';
    $filename = 'freeswitch.bak.tgz';
    system('cd /usr/local/;tar cvzf /tmp/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 = '/tmp/';
    $filename = 'freeswitch.bak.tgz';
   
    //extract a specific directory to /usr/local/freeswitch
    if (file_exists('/tmp/'.$filename)) {
      //echo "The file $filename exists";

      //Recommended
        system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/db/');
        system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/log/');
        system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/recordings/');
        system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/scripts/');
        system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/storage/');
        
      //Optional  
        //system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/conf/');
        //system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/grammar/');  
        //system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/htdocs/'); 
        //system('cd /usr/local; tar xvpfz /tmp/'.$filename.' freeswitch/sounds/'); 
    
        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"];
?>
<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
			
	$tab_array = array();
	$tab_array[] = array(gettext("Settings"), false, "/pkg_edit.php?xml=freeswitch.xml&amp;id=0");
	$tab_array[] = array(gettext("Dialplan"), false, "/freeswitch/freeswitch_dialplan_includes.php");
	$tab_array[] = array(gettext("Extensions"), false, "/pkg.php?xml=freeswitch_extensions.xml");
	$tab_array[] = array(gettext("External"), false, "/pkg_edit.php?xml=freeswitch_external.xml&amp;id=0");	
	$tab_array[] = array(gettext("Gateways"), false, "/freeswitch/freeswitch_gateways.php");
	$tab_array[] = array(gettext("Internal"), false, "/pkg_edit.php?xml=freeswitch_internal.xml&amp;id=0");
	$tab_array[] = array(gettext("IVR"), false, "/freeswitch/freeswitch_ivr.php");
	$tab_array[] = array(gettext("Modules"), false, "/pkg_edit.php?xml=freeswitch_modules.xml&amp;id=0");
	$tab_array[] = array(gettext("Public"), false, "/freeswitch/freeswitch_public_includes.php");
	$tab_array[] = array(gettext("Rec"), false, "/freeswitch/freeswitch_recordings.php");
	$tab_array[] = array(gettext("Status"), true, "/freeswitch/freeswitch_status.php");
	$tab_array[] = array(gettext("Vars"), false, "/pkg_edit.php?xml=freeswitch_vars.xml&amp;id=0");
 	display_top_tabs($tab_array);
 	
?>
</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='/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";


$fp = event_socket_create($host, $port, $password);
$cmd = "api sofia status profile internal";
$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 internal</b> \n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='start' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+internal+start';\" />\n";
echo "  <input type='button' value='stop' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+internal+stop';\" />\n";
echo "  <input type='button' value='flush_inbound_reg' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+internal+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 sofia status profile external";
$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 external</b> \n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='start' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+external+start+reloadxml';\" />\n";
echo "  <input type='button' value='stop' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+external+stop';\" />\n";
echo "  <input type='button' value='restart' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+external+restart+reloadxml';\" />\n";
echo "  <input type='button' value='rescan' onclick=\"document.location.href='/freeswitch/freeswitch_cmd.php?cmd=api+sofia+profile+external+rescan+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";


$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>\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>\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 /tmp/freeswitch.bak.tgz it then presents a file to download. \n";
echo "If the backup file does not exist in /tmp/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='/freeswitch/freeswitch_status.php?a=download&t=backup';\" />\n";
if (file_exists('/tmp/freeswitch.bak.tgz')) {
  echo "  <input type='button' value='restore' onclick=\"document.location.href='/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='/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='/freeswitch/freeswitch_status.php?a=download&t=logs';\" />\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";


echo "<b>tail -n 100 /usr/local/freeswitch/log/freeswitch.log</b><br />\n";
echo "<textarea cols='85' rows='30' wrap='off'>\n";
echo system("tail -n 100 /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>