<?php
/*
	services_captiveportal_saml_sp.php
	part of m0n0wall (http://m0n0.ch/wall)

	Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
	Copyright (C) 2014 Entr'ouvert <info@entrouvert.com>.
	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:	captiveportal
*/

##|+PRIV
##|*IDENT=page-services-captiveportal-saml-sp
##|*NAME=Services: Captive portal SAML SP page
##|*DESCR=Allow access to the 'Services: Captive portal SAML SP' page.
##|*MATCH=services_captiveportal_saml_sp.php*
##|-PRIV

require_once("guiconfig.inc");
require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");
require_once("captiveportal.inc");
require_once("captiveportal_saml.inc");

if (!is_array($config['captiveportal']))
	$config['captiveportal'] = array();
$a_cp =& $config['captiveportal'];

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

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

if (!is_array($a_un['sp']))
	$a_un['sp'] = array();
$a_sp =& $a_un['sp'];

$pconfig['certref'] = $a_sp['certref'];
$pconfig['defaultidps'] = base64_decode($a_sp['defaultidps']);
$pconfig['geolocations'] = base64_decode($a_sp['geolocations']);
$pconfig['geoinitialbounds'] = $a_sp['geoinitialbounds'];
$pconfig['tilesurl'] = $a_sp['tilesurl'];
$pconfig['redirect_url'] = $a_sp['redirect_url'];
$pconfig['redirect_delay'] = $a_sp['redirect_delay'];
if($pconfig['redirect_url']!='next_url')
    $pconfig['defined_redirect_url'] = $a_sp['redirect_url'];

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

if ($_POST) {

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

	/* input validation */
        $reqdfields = array("certref");
	$reqdfieldsn = array(gettext("SP SAML 2.0 Certificate"));

	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 (!$input_errors) {
		$a_sp['certref'] = $pconfig['certref'];
		$a_sp['defaultidps'] = base64_encode($pconfig['defaultidps']);
		$a_sp['geolocations'] = base64_encode($pconfig['geolocations']);
		$a_sp['geoinitialbounds'] = $pconfig['geoinitialbounds'];
		$a_sp['tilesurl'] = $pconfig['tilesurl'];
                $a_sp['redirect_url'] = $pconfig['redirect_url'];
                $a_sp['redirect_delay'] = $pconfig['redirect_delay'];
                if ($a_sp['redirect_url'] == 'defined_redirect_url')
                    $a_sp['redirect_url'] = $pconfig['defined_redirect_url'];
                else
                    $pconfig['defined_redirect_url'] = '';
		/* write config.xml */
		write_config();

		/* relaunch SP */
		mwexec_bg("/usr/local/univnautes/sp/rc.sh restart");

		/* back to the page */
		pfSenseHeader("services_captiveportal_saml_sp.php");
	}
}
include("head.inc");
?>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<?php if ($input_errors) print_input_errors($input_errors); ?>
<?php if ($savemsg) print_info_box($savemsg); ?>

<form action="services_captiveportal_saml_sp.php" method="post" enctype="multipart/form-data" name="iform" id="iform">

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr><td class="tabnavtbl"><?php display_saml_tabs(basename(__FILE__)); ?></td></tr>
  <tr>
  <td class="tabcont">
  <table width="100%" border="0" cellpadding="6" cellspacing="0">

	<tr>
		<td colspan="2" class="list" height="12"></td>
	</tr>
	<tr>
		<td colspan="2" valign="top" class="listtopic"><?=gettext("Service Provider SAML 2.0");?></td>
	</tr>


	<tr>
		<td width="22%" valign="top" class="vncell"><?=gettext("SP SAML 2.0 Certificate"); ?></td>
		<td width="78%" class="vtable">
			<?php if (count($a_cert)): ?>
			<select name="certref" id="certref" class="formselect">
                        <option value="__none__">None</option>
				<?php
					foreach($a_cert as $cert):
						$selected = "";
						if ($pconfig['certref'] == $cert['refid'])
							$selected = "selected";
				?>
				<option value="<?=$cert['refid'];?>"<?=$selected;?>><?=$cert['descr'];?></option>
			<?php endforeach; ?>
			</select>
			<?php else: ?>
				<b><?=gettext("No Certificates defined."); ?></b> <br/>Create one under <a href="system_certmanager.php">System &gt; Cert Manager</a>.
			<?php endif; ?>
		</td>
	</tr>
	<tr>
		<td colspan="2" class="list" height="12"></td>
	</tr>
	<tr>
		<td colspan="2" valign="top" class="listtopic"><?=gettext("User interface : default Identity Providers");?></td>
	</tr>
	<tr>
		<td valign="top" class="vncellreq"><?=gettext("Default IdPs"); ?></td>
		<td class="vtable">
			<textarea name="defaultidps" cols="65" rows="4" id="defaultidps" class="formpre"><?=htmlspecialchars($pconfig['defaultidps']);?></textarea>
			<br>
			<?=gettext("List of EntityIDs, one per line. These IdP will appear at the top of the list."); ?>
		</td>
	</tr>

	<tr>
		<td colspan="2" class="list" height="12"></td>
	</tr>
	<tr>
		<td colspan="2" valign="top" class="listtopic"><?=gettext("User interface : Map");?></td>
	</tr>

	<tr>
		<td valign="top" class="vncellreq"><?=gettext("Geo locations"); ?></td>
		<td class="vtable">
			<textarea name="geolocations" cols="65" rows="4" id="geolocations" class="formpre" placeholder="https://..."><?=htmlspecialchars($pconfig['geolocations']);?></textarea>
			<br>
			<?=gettext("List of URLs, one per line. Example: https://static.discojuice.org/feeds/renater"); ?>
		</td>
	</tr>
	<tr>
		<td valign="top" class="vncellreq"><?=gettext("Initial bounds"); ?></td>
		<td class="vtable">
			<input name="geoinitialbounds" type="text" class="formfld unknown" id="geoinitialbounds" size="50" value="<?=htmlspecialchars($pconfig['geoinitialbounds']);?>" placeholder="[[left,top],[right,bottom]]" />
			<br>
			<?=gettext("Format : [[left,top],[right,bottom]]. Example: [[41.1,-5.53],[51.5,9.91]]"); ?>
		</td>
	</tr>
        <tr>
                <td valign="top" class="vncell"><?=gettext("Tiles URL"); ?> </td>
                <td class="vtable">
                <input name="tilesurl" type="text" class="formfld url" id="tilesurl" size="60" value="<?=htmlspecialchars($pconfig['tilesurl']);?>"><br>
                <?php printf(gettext("URL like "));?>: http://tiles.example.net/%(z)d/%(x)d/%(y)d.png
               </td>
        </tr>
	<tr>
		<td colspan="2" class="list" height="12"></td>
	</tr>
	<tr>
		<td colspan="2" valign="top" class="listtopic"><?=gettext("User interface : Redirect after login");?></td>
	</tr>
	<tr>
		<td width="22%" valign="top" class="vncell"><?=gettext("After login, redirect to"); ?></td>
		<td width="78%" class="vtable">
                  <table>
                    <tr>
                      <td><input type="radio" name="redirect_url" value="next_url" <?php if($pconfig['redirect_url']=='next_url') echo "checked";?> />User requested URL</td>
                    </tr>
                    <tr>
                      <td>
                        <input type="radio" name="redirect_url" value="defined_redirect_url" <?php if ($pconfig['redirect_url'] && $pconfig['redirect_url']!='next_url') echo "checked";?> />
                        <input type="url" name="defined_redirect_url" value="<?=htmlspecialchars($pconfig['defined_redirect_url']);?>"  placeholder="https://www.example.net/" size="50" class="formfld url" /> <br />
                        <?=gettext("Accepts variables, Django templates format {{ varname }}"); ?>
                      </td>
                    </tr>
                  </table>
		</td>
	</tr>
        <tr>
		<td width="22%" valign="top" class="vncell"><?=gettext("Redirect delay"); ?></td>
		<td width="78%" class="vtable">
                  <input type="number" name="redirect_delay" min="0" max="60" value="<?=htmlspecialchars($pconfig['redirect_delay']);?>" /> seconds
                </td>
        </tr>

	<tr>
	  <td width="22%" valign="top">&nbsp;</td>
	  <td width="78%">
		<?php echo "<input name='zone' id='zone' type='hidden' value='" . htmlspecialchars($cpzone) . "'/>"; ?>
		<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" onClick="enable_change(true)">
		<a href="services_captiveportal_zones.php"><input name="Cancel" type="button" class="formbtn" value="<?=gettext("Cancel"); ?>" onClick="enable_change(true)"></a>
	  </td>
	</tr>
  </table>
  </td>
  </tr>
  </table>
</form>
<script language="JavaScript">
<!--
enable_change(false);
//-->
</script>
<?php include("fend.inc"); ?>
</body>
</html>
