Commit b3169d7b authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) refactor load_balancer_monitor_edit.php

(cherry picked from commit b13fe7de)
parent f328fe58
<?php <?php
/* /*
Copyright (C) 2014-2015 Deciso B.V. Copyright (C) 2014-2016 Deciso B.V.
Copyright (C) 2008 Bill Marquette <bill.marquette@gmail.com>. Copyright (C) 2008 Bill Marquette <bill.marquette@gmail.com>.
All rights reserved. All rights reserved.
...@@ -75,160 +75,102 @@ $rfc2616 = array( ...@@ -75,160 +75,102 @@ $rfc2616 = array(
505 => "505 HTTP Version Not Supported" 505 => "505 HTTP Version Not Supported"
); );
function is_rfc2616_code($code) { if (empty($config['load_balancer']['monitor_type']) || !is_array($config['load_balancer']['monitor_type'])) {
global $rfc2616;
if (isset($rfc2616[$code]))
return true;
else
return false;
}
function print_rfc2616_select($tag, $current){
global $rfc2616;
/* Default to 200 OK if not set */
if ($current == "")
$current = 200;
echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
foreach($rfc2616 as $code => $message) {
if ($code == $current) {
$sel = " selected=\"selected\"";
} else {
$sel = "";
}
echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
}
echo "</select>\n";
}
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/load_balancer_monitor.php');
if (!is_array($config['load_balancer']['monitor_type'])) {
$config['load_balancer']['monitor_type'] = array(); $config['load_balancer']['monitor_type'] = array();
} }
$a_monitor = &$config['load_balancer']['monitor_type']; $a_monitor = &$config['load_balancer']['monitor_type'];
if (is_numericint($_GET['id']))
$id = $_GET['id'];
if (isset($_POST['id']) && is_numericint($_POST['id']))
$id = $_POST['id'];
if (isset($id) && $a_monitor[$id]) {
$pconfig['name'] = $a_monitor[$id]['name'];
$pconfig['type'] = $a_monitor[$id]['type'];
$pconfig['descr'] = $a_monitor[$id]['descr'];
$pconfig['options'] = array();
$pconfig['options'] = $a_monitor[$id]['options'];
} else {
/* Some sane page defaults */
$pconfig['options']['path'] = '/';
$pconfig['options']['code'] = 200;
}
if ($_POST) {
unset($input_errors);
$pconfig = $_POST;
/* turn $_POST['http_options_*'] into $pconfig['options'][*] */ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
foreach($_POST as $key => $val) { if (isset($_GET['id']) && !empty($a_monitor[$_GET['id']])) {
if (stristr($key, 'options') !== false) { $id = $_GET['id'];
if (stristr($key, $pconfig['type'].'_') !== false) {
$opt = explode('_',$key);
$pconfig['options'][$opt[2]] = $val;
} }
unset($pconfig[$key]); $pconfig = array();
foreach (array('name', 'type', 'descr') as $fieldname) {
if (isset($id) && isset($a_monitor[$id][$fieldname])) {
$pconfig[$fieldname] = $a_monitor[$id][$fieldname];
} else {
$pconfig[$fieldname] = null;
} }
} }
if (isset($id)) {
$pconfig['options_send'] = isset($a_monitor[$id]['options']['send']) ? $a_monitor[$id]['options']['send'] : null;
$pconfig['options_expect'] = isset($a_monitor[$id]['options']['expect']) ? $a_monitor[$id]['options']['expect'] : null;
$pconfig['options_path'] = isset($a_monitor[$id]['options']['path']) ? $a_monitor[$id]['options']['path'] : null;
$pconfig['options_host'] = isset($a_monitor[$id]['options']['host']) ? $a_monitor[$id]['options']['host'] : null;
$pconfig['options_code'] = isset($a_monitor[$id]['options']['code']) ? $a_monitor[$id]['options']['code'] : null;
} else {
/* option defaults */
$pconfig['options_send'] = null;
$pconfig['options_expect'] = null;
$pconfig['options_path'] = '/';
$pconfig['options_code'] = 200;
$pconfig['options_host'] = null;
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['id']) && !empty($a_monitor[$_POST['id']])) {
$id = $_POST['id'];
}
$pconfig = $_POST;
$input_errors = array();
/* input validation */ /* input validation */
$reqdfields = explode(" ", "name type descr"); $reqdfields = explode(" ", "name type descr");
$reqdfieldsn = array(gettext("Name"),gettext("Type"),gettext("Description")); $reqdfieldsn = array(gettext("Name"),gettext("Type"),gettext("Description"));
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors); do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);
/* Ensure that our monitor names are unique */ /* Ensure that our monitor names are unique */
for ($i=0; isset($config['load_balancer']['monitor_type'][$i]); $i++) for ($i=0; isset($config['load_balancer']['monitor_type'][$i]); $i++) {
if (($_POST['name'] == $config['load_balancer']['monitor_type'][$i]['name']) && ($i != $id)) if ($pconfig['name'] == $config['load_balancer']['monitor_type'][$i]['name'] && $i != $id) {
$input_errors[] = gettext("This monitor name has already been used. Monitor names must be unique."); $input_errors[] = gettext("This monitor name has already been used. Monitor names must be unique.");
}
}
if (strpos($_POST['name'], " ") !== false) if (strpos($pconfig['name'], " ") !== false) {
$input_errors[] = gettext("You cannot use spaces in the 'name' field."); $input_errors[] = gettext("You cannot use spaces in the 'name' field.");
switch($_POST['type']) {
case 'icmp': {
break;
} }
case 'tcp': { switch($pconfig['type']) {
case 'icmp':
case 'tcp':
break; break;
}
case 'http': case 'http':
case 'https': { case 'https':
if (is_array($pconfig['options'])) { if (!empty($pconfig['options_host']) && !is_hostname($pconfig['options_host'])) {
if (isset($pconfig['options']['host']) && $pconfig['options']['host'] != "") {
if (!is_hostname($pconfig['options']['host'])) {
$input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'."); $input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'.");
} }
} if (!empty($pconfig['options_code']) && !isset($rfc2616[$pconfig['options_code']])) {
if (isset($pconfig['options']['code']) && $pconfig['options']['code'] != "") {
// Check code
if(!is_rfc2616_code($pconfig['options']['code'])) {
$input_errors[] = gettext("HTTP(s) codes must be from RFC2616."); $input_errors[] = gettext("HTTP(s) codes must be from RFC2616.");
} }
} if (empty($pconfig['options_path'])) {
if (!isset($pconfig['options']['path']) || $pconfig['options']['path'] == "") {
$input_errors[] = gettext("The path to monitor must be set."); $input_errors[] = gettext("The path to monitor must be set.");
} }
}
break; break;
} case 'send':
case 'send': {
if (is_array($pconfig['options'])) {
if (isset($pconfig['options']['send']) && $pconfig['options']['send'] != "") {
// Check send
}
if (isset($pconfig['options']['expect']) && $pconfig['options']['expect'] != "") {
// Check expect
}
}
break; break;
} }
}
if (!$input_errors) { if (count($input_errors) == 0) {
$monent = array(); $monent = array();
if (isset($id) && $a_monitor[$id]) {
$monent = $a_monitor[$id];
}
$monent['name'] = $pconfig['name']; $monent['name'] = $pconfig['name'];
$monent['type'] = $pconfig['type']; $monent['type'] = $pconfig['type'];
$monent['descr'] = $pconfig['descr']; $monent['descr'] = $pconfig['descr'];
if($pconfig['type'] == "http" || $pconfig['type'] == "https" ) {
/* log updates, then clear array and reassign - dumb, but easiest way to have a clear array */
$monent['options'] = array();
$monent['options']['path'] = $pconfig['options']['path'];
$monent['options']['host'] = $pconfig['options']['host'];
$monent['options']['code'] = $pconfig['options']['code'];
}
if($pconfig['type'] == "send" ) {
/* log updates, then clear array and reassign - dumb, but easiest way to have a clear array */
$monent['options'] = array();
$monent['options']['send'] = $pconfig['options']['send'];
$monent['options']['expect'] = $pconfig['options']['expect'];
}
if($pconfig['type'] == "tcp" || $pconfig['type'] == "icmp") {
$monent['options'] = array(); $monent['options'] = array();
if($pconfig['type'] == "http" || $pconfig['type'] == "https") {
$monent['options']['path'] = $pconfig['options_path'];
$monent['options']['host'] = $pconfig['options_host'];
$monent['options']['code'] = $pconfig['options_code'];
} elseif ($pconfig['type'] == "send") {
$monent['options']['send'] = $pconfig['options_send'];
$monent['options']['expect'] = $pconfig['options_expect'];
} }
if (isset($id) && $a_monitor[$id]) { if (isset($id)) {
/* modify all pools with this name */ /* modify all pools with this name */
for ($i = 0; isset($config['load_balancer']['lbpool'][$i]); $i++) { for ($i = 0; isset($config['load_balancer']['lbpool'][$i]); $i++) {
if ($config['load_balancer']['lbpool'][$i]['monitor'] == $a_monitor[$id]['name']) if ($config['load_balancer']['lbpool'][$i]['monitor'] == $a_monitor[$id]['name']) {
$config['load_balancer']['lbpool'][$i]['monitor'] = $monent['name']; $config['load_balancer']['lbpool'][$i]['monitor'] = $monent['name'];
} }
}
$a_monitor[$id] = $monent; $a_monitor[$id] = $monent;
} else { } else {
$a_monitor[] = $monent; $a_monitor[] = $monent;
...@@ -244,182 +186,144 @@ if ($_POST) { ...@@ -244,182 +186,144 @@ if ($_POST) {
$service_hook = 'relayd'; $service_hook = 'relayd';
include("head.inc"); include("head.inc");
legacy_html_escape_form_data($pconfig);
$types = array("icmp" => gettext("ICMP"), "tcp" => gettext("TCP"), "http" => gettext("HTTP"), "https" => gettext("HTTPS"), "send" => gettext("Send/Expect")); $types = array("icmp" => gettext("ICMP"), "tcp" => gettext("TCP"), "http" => gettext("HTTP"), "https" => gettext("HTTPS"), "send" => gettext("Send/Expect"));
?> ?>
<body> <body>
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ $( document ).ready(function() {
function updateType(t){ $("#monitor_type").change(function(){
switch(t) { switch ($(this).val()) {
<?php case 'http':
/* OK, so this is sick using php to generate javascript, but it needed to be done */ case 'https':
foreach ($types as $key => $val) { $("#type_details_send").hide();
echo " case \"{$key}\": {\n"; $("#type_details_http").show();
$t = $types; $("#type_details").show();
foreach ($t as $k => $v) { break;
if ($k != $key) { case 'send':
echo " jQuery('#{$k}').hide();\n"; $("#type_details_send").show();
} $("#type_details_http").hide();
} $("#type_details").show();
echo " }\n"; break;
} default:
?> $("#type_details").hide();
} break;
jQuery('#' + t).show();
} }
//]]> });
$("#monitor_type").change();
});
</script> </script>
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?> <?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
<section class="col-xs-12"> <section class="col-xs-12">
<div class="content-box"> <div class="content-box">
<form name="iform" method="post" id="iform">
<form action="load_balancer_monitor_edit.php" method="post" name="iform" id="iform">
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped table-sort"> <table class="table table-striped">
<tr> <tr>
<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit Load Balancer - Monitor entry"); ?></td> <td width="22%"><strong><?=gettext("Edit Monitor entry"); ?></strong></td>
</tr> <td width="78%" align="right">
<tr align="left"> <small><?=gettext("full help"); ?> </small>
<td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td> <i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
<td width="78%" class="vtable" colspan="2">
<input name="name" type="text" <?php if(isset($pconfig['name'])) echo "value=\"" . htmlspecialchars($pconfig['name']) . "\"";?> size="16" maxlength="16" />
</td>
</tr>
<tr align="left">
<td width="22%" valign="top" class="vncellreq"><?=gettext("Description"); ?></td>
<td width="78%" class="vtable" colspan="2">
<input name="descr" type="text" <?php if(isset($pconfig['descr'])) echo "value=\"" . htmlspecialchars($pconfig['descr']) . "\"";?> size="64" />
</td>
</tr>
<tr align="left">
<td width="22%" valign="top" class="vncellreq"><?=gettext("Type"); ?></td>
<td width="78%" class="vtable" colspan="2">
<select id="type" name="type">
<?
foreach ($types as $key => $val) {
if(isset($pconfig['type']) && $pconfig['type'] == $key) {
$selected = " selected=\"selected\"";
} else {
$selected = "";
}
echo "<option value=\"{$key}\" onclick=\"updateType('{$key}');\"{$selected}>{$val}</option>\n";
}
?>
</select>
</td>
</tr>
<tr align="left" id="icmp"<?= $pconfig['type'] == "icmp" ? "" : " style=\"display:none;\""?>><td></td>
</tr>
<tr align="left" id="tcp"<?= $pconfig['type'] == "tcp" ? "" : " style=\"display:none;\""?>><td></td>
</tr>
<tr align="left" id="http"<?= $pconfig['type'] == "http" ? "" : " style=\"display:none;\""?>>
<td width="22%" valign="top" class="vncellreq"><?=gettext("HTTP"); ?></td>
<td width="78%" class="vtable" colspan="2">
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="http">
<tr align="left">
<td valign="top" align="right" class="vtable"><?=gettext("Path"); ?></td>
<td class="vtable" colspan="2">
<input name="http_options_path" type="text" <?php if(isset($pconfig['options']['path'])) echo "value=\"" . htmlspecialchars($pconfig['options']['path']) . "\"";?> size="64" />
</td>
</tr>
<tr align="left">
<td valign="top" align="right" class="vtable"><?=gettext("Host"); ?></td>
<td class="vtable" colspan="2">
<input name="http_options_host" type="text" <?php if(isset($pconfig['options']['host'])) echo "value=\"" . htmlspecialchars($pconfig['options']['host']) . "\"";?> size="64" /><br /><?=gettext("Hostname for Host: header if needed."); ?>
</td>
</tr>
<tr align="left">
<td valign="top" align="right" class="vtable"><?=gettext("HTTP Code"); ?></td>
<td class="vtable" colspan="2">
<?= print_rfc2616_select("http_options_code", isset($pconfig['options']['code'])?$pconfig['options']['code']:""); ?>
</td>
</tr>
<!-- BILLM: XXX not supported digest checking just yet
<tr align="left">
<td width="22%" valign="top" class="vncell">MD5 Page Digest</td>
<td width="78%" class="vtable" colspan="2">
<input name="digest" type="text" <?php if(isset($pconfig['digest'])) echo "value=\"" . htmlspecialchars($pconfig['digest']) . "\"";?>size="32"><br /><b>TODO: add fetch functionality here</b>
</td> </td>
</tr> </tr>
--> <tr>
</table> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Name"); ?></td>
<td>
<input name="name" type="text" value="<?=$pconfig['name'];?>" />
</td> </td>
</tr> </tr>
<tr align="left" id="https"<?= $pconfig['type'] == "https" ? "" : " style=\"display:none;\""?>> <tr>
<td width="22%" valign="top" class="vncellreq"><?=gettext("HTTPS"); ?></td> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Description"); ?></td>
<td width="78%" class="vtable" colspan="2"> <td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="https"> <input name="descr" type="text" value="<?=$pconfig['descr'];?>"/>
<tr align="left">
<td valign="top" align="right" class="vtable"><?=gettext("Path"); ?></td>
<td class="vtable" colspan="2">
<input name="https_options_path" type="text" <?php if(isset($pconfig['options']['path'])) echo "value=\"" . htmlspecialchars($pconfig['options']['path']) ."\"";?> size="64" />
</td> </td>
</tr> </tr>
<tr align="left"> <tr>
<td valign="top" align="right" class="vtable"><?=gettext("Host"); ?></td> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Type"); ?></td>
<td class="vtable" colspan="2"> <td>
<input name="https_options_host" type="text" <?php if(isset($pconfig['options']['host'])) echo "value=\"" . htmlspecialchars($pconfig['options']['host']) . "\"";?> size="64" /><br /><?=gettext("Hostname for Host: header if needed."); ?> <select id="monitor_type" name="type" class="selectpicker">
<option value="icmp" <?=$pconfig['type'] == "icmp" ? " selected=\"selected\"" : "";?> >
<?=gettext("ICMP");?>
</option>
<option value="tcp" <?=$pconfig['type'] == "tcp" ? " selected=\"selected\"" : "";?> >
<?=gettext("TCP");?>
</option>
<option value="http" <?=$pconfig['type'] == "http" ? " selected=\"selected\"" : "";?> >
<?=gettext("HTTP");?>
</option>
<option value="https" <?=$pconfig['type'] == "https" ? " selected=\"selected\"" : "";?> >
<?=gettext("HTTPS");?>
</option>
<option value="send" <?=$pconfig['type'] == "send" ? " selected=\"selected\"" : "";?> >
<?=gettext("Send/Expect");?>
</option>
</select>
</td> </td>
</tr> </tr>
<tr align="left"> <tr id="type_details" style="display:none;">
<td valign="top" align="right" class="vtable"><?=gettext("HTTP Code"); ?></td> <td></td>
<td class="vtable" colspan="2"> <td>
<?= print_rfc2616_select("https_options_code", isset($pconfig['options']['code'])?$pconfig['options']['code']:""); ?> <div id="type_details_send">
<table class="table table-condensed">
<tr>
<td><?=gettext("Send string"); ?></td>
<td>
<input name="options_send" type="text" value="<?=$pconfig['options_send'];?>"/>
</td> </td>
</tr> </tr>
<!-- BILLM: XXX not supported digest checking just yet <tr>
<td><?=gettext("Expect string"); ?></td>
<tr align="left"> <td>
<td width="22%" valign="top" class="vncellreq">MD5 Page Digest</td> <input name="options_expect" type="text" value="<?=$pconfig['options_expect'];?>"/>
<td width="78%" class="vtable" colspan="2">
<input name="digest" type="text" <?php if(isset($pconfig['digest'])) echo "value=\"" . htmlspecialchars($pconfig['digest']) . "\"";?>size="32"><br /><b>TODO: add fetch functionality here</b>
</td> </td>
</tr> </tr>
-->
</table> </table>
</div>
<div id="type_details_http">
<table class="table table-condensed">
<tr>
<td><?=gettext("Path"); ?></td>
<td>
<input name="options_path" type="text" value="<?=$pconfig['options_path'];?>"/>
</td> </td>
</tr> </tr>
<tr align="left" id="send"<?= $pconfig['type'] == "send" ? "" : " style=\"display:none;\""?>> <tr>
<td width="22%" valign="top" class="vncellreq"><?=gettext("Send/Expect"); ?></td> <td><?=gettext("Host"); ?></td>
<td width="78%" class="vtable" colspan="2"> <td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="send expect"> <input name="options_host" type="text" value="<?=$pconfig['options_host'];?>" />
<tr align="left"> <small><?=gettext("Hostname for Host: header if needed."); ?></small>
<td valign="top" align="right" class="vtable"><?=gettext("Send string"); ?></td>
<td class="vtable" colspan="2">
<input name="send_options_send" type="text" <?php if(isset($pconfig['options']['send'])) echo "value=\"" . htmlspecialchars($pconfig['options']['send']) . "\"";?> size="64" />
</td> </td>
</tr> </tr>
<tr align="left"> <tr>
<td valign="top" align="right" class="vtable"><?=gettext("Expect string"); ?></td> <td><?=gettext("HTTP Code"); ?></td>
<td class="vtable" colspan="2"> <td>
<input name="send_options_expect" type="text" <?php if(isset($pconfig['options']['expect'])) echo "value=\"" . htmlspecialchars($pconfig['options']['expect']) . "\"";?> size="64" /> <select name="options_code">
<?php
foreach($rfc2616 as $code => $message):?>
<option value="<?=$code;?>" <?=$pconfig['options_code'] == $code ? " selected=\"selected\"" :"" ;?>>
<?=$message;?>
</option>
<?php
endforeach;?>
</select>
</td> </td>
</tr> </tr>
</table> </table>
</div>
</td> </td>
</tr> </tr>
<tr align="left"> <tr>
<td width="22%" valign="top">&nbsp;</td> <td valign="top">&nbsp;</td>
<td width="78%"> <td width="78%">
<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" /> <input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
<input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" /> <input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/load_balancer_monitor.php');?>'" />
<?php if (isset($id) && $a_monitor[$id]): ?> <?php if (isset($id)): ?>
<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" /> <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
<?php endif; ?> <?php endif; ?>
</td> </td>
...@@ -432,5 +336,4 @@ $types = array("icmp" => gettext("ICMP"), "tcp" => gettext("TCP"), "http" => get ...@@ -432,5 +336,4 @@ $types = array("icmp" => gettext("ICMP"), "tcp" => gettext("TCP"), "http" => get
</div> </div>
</div> </div>
</section> </section>
<?php include("foot.inc"); ?> <?php include("foot.inc"); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment