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

	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/v_config.inc");


if ($_GET['a'] == "download") {
  if ($_GET['t'] == "logs") {
    $tmp = $v_log_dir.'/';
    $filename = $v_name.'.log';
  }
  if ($_GET['t'] == "cdrcsv") {
    $tmp = $v_log_dir.'/cdr-csv/';
    $filename = 'Master.csv';
  }
  if ($_GET['t'] == "backup") {
    $tmp = $v_backup_dir.'/';
    $filename = $v_name.'.bak.tgz';
	if (!is_dir($v_backup_dir.'/')) {
	   exec("mkdir ".$v_backup_dir."/");
	}
	chdir($v_parent_dir);
    system('tar cvzf '.$v_backup_dir.'/'.$v_name.'.bak.tgz '.$v_name);
  }
	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'] == "update") {
	if ($_GET['t'] == "gui_phase_1") {
	
		//chdir('/tmp/');
		chdir($v_parent_dir.'/pkg/');
		exec("fetch ".$v_download_path."v_config.inc");
		//exec("cp ".$tmp_dir."/v_config.tmp ".$v_parent_dir."/pkg/v_config.php");
		//unlink_if_exists($tmp_dir."/v_config.tmp");
		
		header( 'Location: v_status.php?a=update&t=gui_phase_2' ) ;
		exit;
	}
}

if ($_GET['a'] == "update") {
	if ($_GET['t'] == "gui_phase_2") {		
		v_install_phase_2(); //needs to run on the new page so that it uses the new v_config.inc file
		header( 'Location: v_status.php?savemsg=Update+Completed.' ) ;
		exit;
	}
}

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

    //extract a specific directory
    if (file_exists($v_backup_dir.'/'.$filename)) {
		//echo "The file $filename exists";
		
		//Clear the following directories to prepare for the Restore
			exec("rm -R ".$v_conf_dir."/sip_profiles/");
			exec("rm -R ".$v_dir."/sounds/music/");

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

		//Optional
			//system('cd /usr/local; tar xvpfz '.$v_backup_dir.'/'.$filename.' '.$v_name.'/conf/');
			//system('cd /usr/local; tar xvpfz '.$v_backup_dir.'/'.$filename.' '.$v_name.'/grammar/');
			//system('cd /usr/local; tar xvpfz '.$v_backup_dir.'/'.$filename.' '.$v_name.'/htdocs/');

		//Synchronize Package
			sync_package_freeswitch();

		header( 'Location: v_status.php?savemsg=Backup+has+been+restored.' ) ;
    }
    else {
        header( 'Location: v_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 service is not running then start it
if (!pkg_is_service_running($v_name)) {
	$handle = popen($v_startup_script_dir."/".$v_name.".sh start", "r");
	pclose($handle);
	//give time for the service 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");
if ($v_label_show) {
	echo "<p class=\"pgtitle\">$v_label: Status</p>\n";
}

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

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

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='v_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($v_conf_dir.'/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='v_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+start';\" />\n";
	echo "  <input type='button' value='stop' onclick=\"document.location.href='v_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+stop';\" />\n";
	echo "  <input type='button' value='restart' onclick=\"document.location.href='v_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='v_cmd.php?cmd=api+sofia+profile+".$sip_profile_name."+rescan';\" />\n";
	}
	else {
		echo "  <input type='button' value='flush_inbound_reg' onclick=\"document.location.href='v_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 ".$v_dir." to ".$v_backup_dir."/".$v_name.".bak.tgz it then presents a file to download. \n";
echo "If the backup file does not exist in ".$v_backup_dir."/".$v_name.".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='v_status.php?a=download&t=backup';\" />\n";
if (file_exists($v_backup_dir.'/'.$v_name.'.bak.tgz')) {
  echo "  <input type='button' value='restore' onclick=\"document.location.href='v_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 $v_log_dir."/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='v_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>Web Interface</b><br />\n";
echo "Use the following button to update the web interface.<br /><br />\n";
echo "</td>\n";
echo "<td width='50%' align='right'>\n";
echo "  <input type='button' value='update' onclick=\"document.location.href='v_status.php?a=update&t=gui_phase_1';\" />\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 $v_log_dir."/cdr-csv/".$v_name.".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='v_status.php?a=download&t=logs';\" />\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";


echo "<b>tail -n 500 ".$v_log_dir."/".$v_name.".log</b><br />\n";
echo "<textarea id='log' name='log' style='width:99%'; rows='30' wrap='off'>\n";
echo system("tail -n 500 ".$v_log_dir."/".$v_name.".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>