<?php
/*
    borrowed from services_captiveportal_saml_whitelist.php

    Copyright (C) 2008 Shrew Soft Inc.
    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.
*/
/*
	pfSense_MODULE:	captive_portal
*/

##|+PRIV
##|*IDENT=page-services-captiveportal-saml-whitelist
##|*NAME=Service: Captive portal SAML whitelist manager
##|*DESCR=Allow access to the 'Service: Captive portal SAML whitelist manager' page.
##|*MATCH=services_captiveportal_saml_whitelist.php*
##|-PRIV

require("guiconfig.inc");
require_once("certs.inc");
require_once("captiveportal_saml.inc");

$pgtitle = array(gettext("Service"), gettext("Captive portal"),"SAML 2.0 Whitelists");
$shortcut_section = "captiveportal";


if (is_numericint($_GET['id']))
	$id = $_GET['id'];
if (isset($_POST['id']) && is_numericint($_POST['id']))
	$id = $_POST['id'];

if (!is_array($config['ca']))
	$config['ca'] = array();

$a_ca =& $config['ca'];

if (!is_array($config['cert']))
	$config['cert'] = array();

$a_cert =& $config['cert'];

$act = $_GET['act'];
if ($_POST['act'])
	$act = $_POST['act'];

if (!is_array($config['univnautes']))
        $config['univnautes'] = array();
$a_univnautes =& $config['univnautes'];

if (!is_array($a_univnautes['whitelists']))
        $a_univnautes['whitelists'] = array();
$whitelists =& $a_univnautes['whitelists'];

if (!is_array($whitelists['whitelist']))
        $whitelists['whitelist'] = array();
$a_whitelist =& $whitelists['whitelist'];


if ($act == "del") {

	if (!isset($a_whitelist[$id])) {
		pfSenseHeader("services_captiveportal_saml_whitelist.php");
		exit;
	}

	$name = $a_whitelist[$id]['descr'];
	unset($a_whitelist[$id]);
	write_config();

        /* reload whitelists */
        mwexec_bg("/usr/local/univnautes/sp/rc.sh syncwl");

	$savemsg = sprintf(gettext("Whitelist %s successfully deleted"), $name) . "<br/>";
	pfSenseHeader("services_captiveportal_saml_whitelist.php");
	exit;
}

if ($act == "edit") {
	if (!$a_whitelist[$id]) {
		pfSenseHeader("services_captiveportal_saml_whitelist.php");
		exit;
	}
	$pconfig['refid'] = $a_whitelist[$id]['refid'];
	$pconfig['codename'] = $a_whitelist[$id]['codename'];
	$pconfig['descr'] = $a_whitelist[$id]['descr'];
	$pconfig['url'] = $a_whitelist[$id]['url'];
	$pconfig['caref'] = $a_whitelist[$id]['caref'];
}

if ($act == "new") {
	# $pconfig['descr'] = "New whitelist";
}

if ($_POST) {

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

	$reqdfields = explode(" ", "codename");
	$reqdfieldsn = array(gettext("Codename"));

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);

	/* if this is an AJAX caller then handle via JSON */
	if (isAjax() && is_array($input_errors)) {
		input_errors2Ajax($input_errors);
		exit;
	}

	if (preg_match('/[^A-Za-z0-9_]/', $_POST['codename'])) {
		$input_errors[] = gettext("The codename can only contain letters, digits, and underscores (_).");
	}

	/* save modifications */
	if (!$input_errors) {

		$whitelist = array();
		if (!isset($pconfig['refid']) || empty($pconfig['refid']))
			$whitelist['refid'] = uniqid('whitelist_');
		else
			$whitelist['refid'] = $pconfig['refid'];

		if (isset($id) && $a_whitelist[$id])
			$whitelist = $a_whitelist[$id];

		$whitelist['codename'] = $pconfig['codename'];
		$whitelist['descr'] = $pconfig['descr'];
		$whitelist['url'] = $pconfig['url'];
		$whitelist['caref'] = $pconfig['caref'];

		if (isset($id) && $a_whitelist[$id])
			$a_whitelist[$id] = $whitelist;
		else
			$a_whitelist[] = $whitelist;

		/*
		echo "<pre>";
                print_r($config);
                print_r($id);
                print_r($a_whitelist);
                print_r($whitelist);
		echo "</pre>";
		exit;
		*/

		write_config();

		/* reload whitelists */
		mwexec_bg("/usr/local/univnautes/sp/rc.sh syncwl");

		pfSenseHeader("services_captiveportal_saml_whitelist.php");
	}
}

include("head.inc");
?>

<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
<?php include("fbegin.inc"); ?>

<?php
	if ($input_errors)
		print_input_errors($input_errors);
	if ($savemsg)
		print_info_box($savemsg);
?>

<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="CA manager">
	<tr>
		<td>
		<?php display_saml_tabs(basename(__FILE__)); ?>
		</td>
	</tr>
	<tr>
		<td id="mainarea">
			<div class="tabcont">

				<?php if ($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors): ?>

				<form action="" method="post" name="iform" id="iform">
					<?php if ($act == "edit"): ?>
					<input type="hidden" name="edit" value="edit" id="edit" />
					<input type="hidden" name="id" value="<?php echo htmlspecialchars($id); ?>" id="id" />
					<input type="hidden" name="refid" value="<?php echo $pconfig['refid']; ?>" id="refid" />
					<?php endif; ?>
					<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Code name");?></td>
							<td width="78%" class="vtable">
							<?php
							if ($act == 'new') {
							?>
							<input name="codename" type="text" class="formfld unknown" id="codename" size="20" value="<?=htmlspecialchars($pconfig['codename']);?>" placeholder="codename" />
							<br/>Can not be changed after. The codename can only contain letters, digits, and underscores (_).
							<?php } else { ?>
							<input name="codename" type="hidden" value="<?=htmlspecialchars($pconfig['codename']);?>" /><?=$pconfig['codename'];?>
							<?php }; ?>
							</td>
						</tr>
						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Description");?></td>
							<td width="78%" class="vtable">
								<input name="descr" type="text" class="formfld unknown" id="descr" size="65" value="<?=htmlspecialchars($pconfig['descr']);?>" />
							</td>
						</tr>
					</table>

					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing" summary="existing">
						<tr>
							<td colspan="2" class="list" height="12"></td>
						</tr>
						<tr>
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Whitelist Download");?></td>
						</tr>

						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Metadata URL");?></td>
							<td width="78%" class="vtable">
								<input name="url" type="text" class="formfld url" id="url" size="65" value="<?=htmlspecialchars($pconfig['url']);?>" placeholder="https://..." />
								<br/><?=gettext("");?>
							</td>
						</tr>

						<tr>
							<td width="22%" valign="top" class="vncellreq"><?=gettext("HTTPS CA certificate"); ?></td>
							<td width="78%" class="vtable">
								<?php if (count($a_ca)): ?>
								<select name="caref" id="caref" class="formselect">
									<option value="__none__">None</option>
									<?php
										foreach($a_ca as $ca):
											$selected = "";
											if ($pconfig['caref'] == $ca['refid'])
												$selected = "selected";
									?>
									<option value="<?=$ca['refid'];?>" <?=$selected;?>><?=$ca['descr'];?></option>
									<?php endforeach; ?>
								</select>
                                                                <br/><?=gettext("If None, the HTTPS server certificate will not be checked."); ?>
								<?php else: ?>
									<b><?=gettext("No CA defined."); ?></b> <br/>Create one under <a href="system_certmanager.php">System &gt; Cert Manager</a>.
								<?php endif; ?>
							</td>
						</tr>

					</table>

					<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="save">
						<tr>
							<td width="22%" valign="top">&nbsp;</td>
							<td width="78%">
								<input id="submit" name="save" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
								<?php if (isset($id) && $a_ca[$id]): ?>
								<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
								<?php endif;?>
							</td>
						</tr>


					</table>
				</form>

				<?php else: ?>

				<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
					<tr>
						<td width="15%" class="listhdrr"><?=gettext("Name");?></td>
						<td width="20%" class="listhdrr"><?=gettext("Description");?></td>
						<td width="60%" class="listhdrr"><?=gettext("URL");?></td>
					</tr>
					<?php
						$i = 0;
						foreach($a_whitelist as $whitelist):
							$name = htmlspecialchars($whitelist['codename']);
							$descr = htmlspecialchars($whitelist['descr']);
							$url = htmlspecialchars($whitelist['url']);

					?>
					<tr>
						<td class="listlr">
							<?=$name;?><br />
						</td>
						<td class="listr">
							<em><?=$descr;?></em>
						</td>
						<td class="listr"><?=$url;?>&nbsp;</td>
						<td valign="middle" nowrap class="list">
							<a href="services_captiveportal_saml_whitelist.php?act=edit&amp;id=<?=$i;?>">
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_e.gif" title="<?=gettext("edit");?>" alt="<?=gettext("edit");?>" width="17" height="17" border="0" />
							</a>
							<a href="services_captiveportal_saml_whitelist.php?act=del&amp;id=<?=$i;?>" onclick="return confirm('<?=gettext("Do you really want to delete this Whitelist?");?>')">
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="<?=gettext("delete");?>" alt="<?=gettext("delete"); ?>" width="17" height="17" border="0" />
							</a>
						</td>
					</tr>
					<?php
							$i++;
						endforeach;
					?>
					<tr>
						<td class="list" colspan="5"></td>
						<td class="list">
							<a href="services_captiveportal_saml_whitelist.php?act=new">
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="<?=gettext("add or import ca");?>" alt="<?=gettext("add ca");?>" width="17" height="17" border="0" />
							</a>
						</td>
					</tr>
					<tr>
						<td colspan="5">
							<p>
								<?=gettext("Additional IP Whitelists can be added here.");?>
							</p>
						</td>
					</tr>
				</table>

				<?php endif; ?>

			</div>
		</td>
	</tr>
</table>
<?php include("fend.inc");?>
<script type="text/javascript">
//<![CDATA[

method_change();

//]]>
</script>

</body>
</html>
