<?php
/* $Id$ */
/*
	v_settings.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");


$a_settings = &$config['installedpackages']['freeswitchsettings']['config'];

//$id = $_GET['id'];
//if (isset($_POST['id'])) {
//	$id = $_POST['id'];
//}
$id = 0;

if (isset($id) && $a_settings[$id]) {

	$pconfig['numbering_plan'] = $a_settings[$id]['numbering_plan'];
	$pconfig['default_gateway'] = $a_settings[$id]['default_gateway'];
	$pconfig['default_area_code'] = $a_settings[$id]['default_area_code'];
	$pconfig['event_socket_port'] = $a_settings[$id]['event_socket_port'];
	$pconfig['event_socket_password'] = $a_settings[$id]['event_socket_password'];
	$pconfig['xml_rpc_http_port'] = $a_settings[$id]['xml_rpc_http_port'];
	$pconfig['xml_rpc_auth_realm'] = $a_settings[$id]['xml_rpc_auth_realm'];
	$pconfig['xml_rpc_auth_user'] = $a_settings[$id]['xml_rpc_auth_user'];
	$pconfig['xml_rpc_auth_pass'] = $a_settings[$id]['xml_rpc_auth_pass'];
	$pconfig['admin_pin'] = $a_settings[$id]['admin_pin'];
	$pconfig['smtphost'] = $a_settings[$id]['smtphost'];
	$pconfig['smtpsecure'] = $a_settings[$id]['smtpsecure'];
	$pconfig['smtpauth'] = $a_settings[$id]['smtpauth'];	
	$pconfig['smtpusername'] = $a_settings[$id]['smtpusername'];
	$pconfig['smtppassword'] = $a_settings[$id]['smtppassword'];
	$pconfig['smtpfrom'] = $a_settings[$id]['smtpfrom'];
	$pconfig['smtpfromname'] = $a_settings[$id]['smtpfromname'];
	$pconfig['mod_shout_decoder'] = $a_settings[$id]['mod_shout_decoder'];
	$pconfig['mod_shout_volume'] = $a_settings[$id]['mod_shout_volume'];
	
}

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;


	if (!$input_errors) {
	
		$ent = array();
		$ent['numbering_plan'] = $_POST['numbering_plan'];
		$ent['default_gateway'] = $_POST['default_gateway'];
		$ent['default_area_code'] = $_POST['default_area_code'];
		$ent['event_socket_port'] = $_POST['event_socket_port'];
		$ent['event_socket_password'] = $_POST['event_socket_password'];
		$ent['xml_rpc_http_port'] = $_POST['xml_rpc_http_port'];
		$ent['xml_rpc_auth_realm'] = $_POST['xml_rpc_auth_realm'];
		$ent['xml_rpc_auth_user'] = $_POST['xml_rpc_auth_user'];
		$ent['xml_rpc_auth_pass'] = $_POST['xml_rpc_auth_pass'];
		$ent['admin_pin'] = $_POST['admin_pin'];
		$ent['smtphost'] = $_POST['smtphost'];
		$ent['smtpsecure'] = $_POST['smtpsecure'];
		$ent['smtpauth'] = $_POST['smtpauth'];
		$ent['smtpusername'] = $_POST['smtpusername'];
		$ent['smtppassword'] = $_POST['smtppassword'];
		$ent['smtpfrom'] = $_POST['smtpfrom'];
		$ent['smtpfromname'] = $_POST['smtpfromname'];
		$ent['mod_shout_decoder'] = $_POST['mod_shout_decoder'];
		$ent['mod_shout_volume'] = $_POST['mod_shout_volume'];

		
		if (isset($id) && $a_settings[$id]) {
		  	//update
      		$a_settings[$id] = $ent;
		}
		else {
		  	//add	
			$a_settings[] = $ent;
		}
		v_settings(); //add the global variables
		write_config();
		sync_package_v_settings();
		
		//header("Location: v_extensions.php");
		//exit;
	}
}

include("head.inc");

?>

<script type="text/javascript" language="JavaScript">

function show_advanced_config() {
	document.getElementById("showadvancedbox").innerHTML='';
	aodiv = document.getElementById('showadvanced');
	aodiv.style.display = "block";
}

</script>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php
include("fbegin.inc");
if ($v_label_show) {
	echo "<p class=\"pgtitle\">$v_label: Settings</p>\n";
}
?>
<?php if ($input_errors) print_input_errors($input_errors); ?>


<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" >

	<!--
  	<table width="100%" border="0" cellpadding="6" cellspacing="0">
      <tr>
        <td><p><span class="vexpl"><span class="red"><strong>Settings<br />
            </strong></span>
			<?php
			//if ($v_path_show) {
			//	echo $v_conf_dir."/directory/default/\n";
			//}
			?>
            </p></td>
      </tr>
    </table>
	-->
    <br />

        <form action="v_settings.php" method="post" name="iform" id="iform">
            <table width="100%" border="0" cellpadding="6" cellspacing="0">

            <tr>
              <td width="25%" valign="top" class="vncell">Numbering Plan</td>
              <td width="75%" class="vtable">
                <input name="numbering_plan" type="text" class="formfld" id="numbering_plan" size="40" value="<?=htmlspecialchars($pconfig['numbering_plan']);?>">
                <br /><span class="vexpl">Enter the numbering plan here. example: US<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Default Gateway</td>
              <td width="75%" class="vtable">
                <input name="default_gateway" type="text" class="formfld" id="default_gateway" size="40" value="<?=htmlspecialchars($pconfig['default_gateway']);?>">
                <br /><span class="vexpl">Enter the default gateway name here.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Default Area Code</td>
              <td width="75%" class="vtable">
                <input name="default_area_code" type="text" class="formfld" id="default_area_code" size="40" value="<?=htmlspecialchars($pconfig['default_area_code']);?>">
                <br /><span class="vexpl">Enter the area code here. example: 208<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Event Socket Port</td>
              <td width="75%" class="vtable">
                <input name="event_socket_port" type="text" class="formfld" id="event_socket_port" size="40" value="<?=htmlspecialchars($pconfig['event_socket_port']);?>">
                <br /><span class="vexpl">Enter the event socket port here. default: 8021<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Event Socket Password</td>
              <td width="75%" class="vtable">
                <input name="event_socket_password" type="password" class="formfld" id="event_socket_password" size="40" value="<?=htmlspecialchars($pconfig['event_socket_password']);?>">
                <br /><span class="vexpl">Enter the event socket password here. default: 7e4d3i<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">XML RPC HTTP Port</td>
              <td width="75%" class="vtable">
                <input name="xml_rpc_http_port" type="text" class="formfld" id="xml_rpc_http_port" size="40" value="<?=htmlspecialchars($pconfig['xml_rpc_http_port']);?>">
                <br /><span class="vexpl">Enter the XML RPC HTTP Port here. default: 8787<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">XML RPC Auth Realm</td>
              <td width="75%" class="vtable">
                <input name="xml_rpc_auth_realm" type="text" class="formfld" id="xml_rpc_auth_realm" size="40" value="<?=htmlspecialchars($pconfig['xml_rpc_auth_realm']);?>">
                <br /><span class="vexpl">Enter the XML RPC Auth Realm here. default: <?php echo $v_name; ?><br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">XML RPC Auth User</td>
              <td width="75%" class="vtable">
                <input name="xml_rpc_auth_user" type="text" class="formfld" id="xml_rpc_auth_user" size="40" value="<?=htmlspecialchars($pconfig['xml_rpc_auth_user']);?>">
                <br /><span class="vexpl">Enter the XML RPC Auth User here. default: xmlrpc<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">XML RPC Auth Password</td>
              <td width="75%" class="vtable">
                <input name="xml_rpc_auth_pass" type="password" class="formfld" id="xml_rpc_auth_pass" size="40" value="<?=htmlspecialchars($pconfig['xml_rpc_auth_pass']);?>">
                <br /><span class="vexpl">Enter the XML RPC Auth Password here. default: 7e4d3i<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Admin PIN Number</td>
              <td width="75%" class="vtable">
                <input name="admin_pin" type="password" class="formfld" id="admin_pin" size="40" value="<?=htmlspecialchars($pconfig['admin_pin']);?>">
                <br /><span class="vexpl">Enter a admin pin number. Used to authenticate the admin from the phone.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">SMTP Host</td>
              <td width="75%" class="vtable">
                <input name="smtphost" type="text" class="formfld2" id="smtphost" size="40" value="<?=htmlspecialchars($pconfig['smtphost']);?>">
                <br /><span class="vexpl">Enter the SMTP host address. example: smtp.gmail.com:465<br /></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">SMTP Secure</td>
              <td width="78%" class="vtable">
                <?php
                echo "              <select name='smtpsecure' class='formfld'>\n";
                echo "                <option></option>\n";
                switch (htmlspecialchars($pconfig['smtpsecure'])) {
                case "none":
                    echo "              <option value='none' selected='yes'>none</option>\n";
                    echo "              <option value='tls'>tls</option>\n";
                    echo "              <option value='ssl'>ssl</option>\n";
                    break;
                case "tls":
					echo "              <option value='none'>none</option>\n";
					echo "              <option value='tls' selected='yes'>tls</option>\n";
                    echo "              <option value='ssl'>ssl</option>\n";
                    break;
                case "ssl":
					echo "              <option value='none'>none</option>\n";
					echo "              <option value='tls'>tls</option>\n";
                    echo "              <option value='ssl' selected='yes'>ssl</option>\n";
                    break;
                default:
					echo "              <option value='' selected='yes'>none</option>\n";
                    echo "              <option value='tls'>tls</option>\n";
                    echo "              <option value='ssl'>ssl</option>\n";
                }
                echo "              </select><br />\n";
				echo "Select the SMTP security. None, TLS, SSL<br />";
                ?>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">SMTP Auth</td>
              <td width="78%" class="vtable">
                <?php
                echo "              <select name='smtpauth' class='formfld'>\n";
                echo "                <option></option>\n";
                switch (htmlspecialchars($pconfig['smtpauth'])) {
                case "true":
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                    break;
                case "false":
                    echo "              <option value='true'>true</option>\n";
                    echo "              <option value='false' selected='yes'>false</option>\n";

                    break;
                default:
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                }
                echo "              </select><br />\n";
				echo "Use SMTP Authentication true or false.<br />";
                ?>
              </td>
            </tr>
			
            <tr>
              <td width="25%" valign="top" class="vncell">SMTP Username</td>
              <td width="75%" class="vtable">
                <input name="smtpusername" type="text" class="formfld" id="smtpusername" size="40" value="<?=htmlspecialchars($pconfig['smtpusername']);?>">
                <br /><span class="vexpl">Enter the SMTP authentication username.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">SMTP Password</td>
              <td width="75%" class="vtable">
                <input name="smtppassword" type="password" class="formfld" id="smtppassword" size="40" value="<?=htmlspecialchars($pconfig['smtppassword']);?>">
                <br /><span class="vexpl">Enter the SMTP authentication password.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">SMTP From</td>
              <td width="75%" class="vtable">
                <input name="smtpfrom" type="text" class="formfld" id="smtpfrom" size="40" value="<?=htmlspecialchars($pconfig['smtpfrom']);?>">
                <br /><span class="vexpl">Enter the SMTP From email address.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">SMTP From Name</td>
              <td width="75%" class="vtable">
                <input name="smtpfromname" type="text" class="formfld" id="smtpfromname" size="40" value="<?=htmlspecialchars($pconfig['smtpfromname']);?>">
                <br /><span class="vexpl">Enter the SMTP From Name.<br /></span>
              </td>
            </tr>
            <tr>
              <td width="25%" valign="top" class="vncell">Mod Shout Decoder</td>
              <td width="75%" class="vtable">
                <input name="mod_shout_decoder" type="text" class="formfld" id="mod_shout_decoder" size="40" value="<?=htmlspecialchars($pconfig['mod_shout_decoder']);?>">
                <br /><span class="vexpl">Enter the Decoder. default: i386<br /></span>
              </td>
            </tr>
            </table>
			<!--
  			<div id="showadvancedbox">
				<table width="100%" border="0" cellpadding="6" cellspacing="0">
				<tr>
					<td width="25%" valign="top" class="vncell">Show Advanced</td>
					<td width="75%" class="vtable">
						<input type="button" onClick="show_advanced_config()" value="Advanced"></input></a>
					</td>
				</tr>
				</table>				
  			</div>
  			<div id="showadvanced" style="display:none">
				<table width="100%" border="0" cellpadding="6" cellspacing="0">
				<tr>
					<td width="25%" valign="top" class="vncell">zzz</td>
					<td width="75%" class="vtable">
						<input name="zzz" type="text" class="formfld" id="zzz" size="40" value="<?=htmlspecialchars($pconfig['zzz']);?>">
						<br /> <span class="vexpl">zzz<br /></span>
					</td>
				</tr>
				</table>
  			</div>
			-->
			<br /><br />
            <table width="100%" border="0" cellpadding="6" cellspacing="0">
            <tr>
              <td width="25%" valign="top">&nbsp;</td>
              <td>
                <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
                <?php
				$id = 0;
				if (isset($id) && $a_settings[$id]) {
                  echo "<input name=\"id\" type=\"hidden\" value=\"".$id."\">\n";
                }
				?>
              </td>
            </tr>
            </table>
        </form>

      <br />
      <br />
      <br />
      <br />
      <br />
      <br />

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

</div>

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