interfaces_groups_edit.php 12 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	Copyright (C) 2009 Ermal Luçi
	Copyright (C) 2004 Scott Ullrich
	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.
*/

31
require_once("guiconfig.inc");
32
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
33

34
if (!isset($config['ifgroups']['ifgroupentry']) || !is_array($config['ifgroups']['ifgroupentry'])) {
Ad Schellevis's avatar
Ad Schellevis committed
35
	$config['ifgroups']['ifgroupentry'] = array();
36
}
Ad Schellevis's avatar
Ad Schellevis committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

$a_ifgroups = &$config['ifgroups']['ifgroupentry'];

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

if (isset($id) && $a_ifgroups[$id]) {
	$pconfig['ifname'] = $a_ifgroups[$id]['ifname'];
	$pconfig['members'] = $a_ifgroups[$id]['members'];
	$pconfig['descr'] = html_entity_decode($a_ifgroups[$id]['descr']);
}

$iflist = get_configured_interface_with_descr();
$iflist_disabled = get_configured_interface_with_descr(false, true);

if ($_POST) {

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

	if (!isset($id)) {
		foreach ($a_ifgroups as $groupentry)
			if ($groupentry['ifname'] == $_POST['ifname'])
				$input_errors[] = gettext("Group name already exists!");
	}
	if (preg_match("/([^a-zA-Z])+/", $_POST['ifname'], $match))
		$input_errors[] = gettext("Only letters A-Z are allowed as the group name.");

	foreach ($iflist as $gif => $gdescr) {
		if ($gdescr == $_POST['ifname'] || $gif == $_POST['ifname'])
69
			$input_errors[] = gettext("The specified group name is already used by an interface. Please choose another name.");
Ad Schellevis's avatar
Ad Schellevis committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	}
	$members = "";
	$isfirst = 0;
	/* item is a normal ifgroupentry type */
	for($x=0; $x<9999; $x++) {
		if($_POST["members{$x}"] <> "") {
			if ($isfirst > 0)
				$members .= " ";
			$members .= $_POST["members{$x}"];
			$isfirst++;
		}
	}

	if (!$input_errors) {
		$ifgroupentry = array();
		$ifgroupentry['members'] = $members;
		$ifgroupentry['descr'] = $_POST['descr'];

		if (isset($id) && $a_ifgroups[$id] && $_POST['ifname'] != $a_ifgroups[$id]['ifname']) {
			if (!empty($config['filter']) && is_array($config['filter']['rule'])) {
				foreach ($config['filter']['rule'] as $ridx => $rule) {
					if (isset($rule['floating'])) {
						$rule_ifs = explode(",", $rule['interface']);
						$rule_changed = false;
						foreach ($rule_ifs as $rule_if_id => $rule_if) {
							if ($rule_if == $a_ifgroups[$id]['ifname']) {
								$rule_ifs[$rule_if_id] = $_POST['ifname'];
								$rule_changed = true;
							}
						}
						if ($rule_changed)
							$config['filter']['rule'][$ridx]['interface'] = implode(",", $rule_ifs);
					} else {
						if ($rule['interface'] == $a_ifgroups[$id]['ifname'])
							$config['filter']['rule'][$ridx]['interface'] = $_POST['ifname'];
					}
				}
			}
			if (!empty($config['nat']) && is_array($config['nat']['rule'])) {
				foreach ($config['nat']['rule'] as $ridx => $rule) {
					if ($rule['interface'] == $a_ifgroups[$id]['ifname'])
						$config['nat']['rule'][$ridx]['interface'] = $_POST['ifname'];
				}
			}
			$omembers = explode(" ", $a_ifgroups[$id]['members']);
			if (count($omembers) > 0) {
				foreach ($omembers as $ifs) {
					$realif = get_real_interface($ifs);
					if ($realif)
						mwexec("/sbin/ifconfig {$realif} -group " . $a_ifgroups[$id]['ifname']);
				}
			}
			$ifgroupentry['ifname'] = $_POST['ifname'];
			$a_ifgroups[$id] = $ifgroupentry;
		} else if (isset($id) && $a_ifgroups[$id]) {
			$omembers = explode(" ", $a_ifgroups[$id]['members']);
			$nmembers = explode(" ", $members);
			$delmembers = array_diff($omembers, $nmembers);
			if (count($delmembers) > 0) {
				foreach ($delmembers as $ifs) {
					$realif = get_real_interface($ifs);
					if ($realif)
						mwexec("/sbin/ifconfig {$realif} -group " . $a_ifgroups[$id]['ifname']);
				}
			}
			$ifgroupentry['ifname'] = $_POST['ifname'];
			$a_ifgroups[$id] = $ifgroupentry;
		} else {
			$ifgroupentry['ifname'] = $_POST['ifname'];
			$a_ifgroups[] = $ifgroupentry;
		}

		write_config();

		interface_group_setup($ifgroupentry);

		header("Location: interfaces_groups.php");
		exit;
	} else {
		$pconfig['descr'] = $_POST['descr'];
		$pconfig['members'] = $members;
	}
}

include("head.inc");

?>

158
<body>
Ad Schellevis's avatar
Ad Schellevis committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
<?php include("fbegin.inc"); ?>

<script type="text/javascript">
//<![CDATA[
// Global Variables
var rowname = new Array(9999);
var rowtype = new Array(9999);
var newrow  = new Array(9999);
var rowsize = new Array(9999);

for (i = 0; i < 9999; i++) {
        rowname[i] = '';
        rowtype[i] = 'select';
        newrow[i] = '';
        rowsize[i] = '30';
}

var field_counter_js = 0;
var loaded = 0;
var is_streaming_progress_bar = 0;
var temp_streaming_text = "";

var addRowTo = (function() {
    return (function (tableId) {
        var d, tbody, tr, td, bgc, i, ii, j;
        d = document;
        tbody = d.getElementById(tableId).getElementsByTagName("tbody").item(0);
        tr = d.createElement("tr");
        for (i = 0; i < field_counter_js; i++) {
                td = d.createElement("td");
		<?php
                        $innerHTML="\"<input type='hidden' value='\" + totalrows +\"' name='\" + rowname[i] + \"_row-\" + totalrows + \"' /><select size='1' name='\" + rowname[i] + totalrows + \"'>\" +\"";

                        foreach ($iflist as $ifnam => $ifdescr)
                                $innerHTML .= "<option value='{$ifnam}'>{$ifdescr}<\/option>";
			$innerHTML .= "<\/select>\";";
                ?>
			td.innerHTML=<?=$innerHTML;?>
                tr.appendChild(td);
        }
        td = d.createElement("td");
        td.rowSpan = "1";

202
        td.innerHTML = '<a onclick="removeRow(this);return false;" href="#"><span class="glyphicon glyphicon-remove" alt="remove"><\/span><\/a>';
Ad Schellevis's avatar
Ad Schellevis committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        tr.appendChild(td);
        tbody.appendChild(tr);
        totalrows++;
    });
})();

function removeRow(el) {
    var cel;
    while (el && el.nodeName.toLowerCase() != "tr")
            el = el.parentNode;

    if (el && el.parentNode) {
        cel = el.getElementsByTagName("td").item(0);
        el.parentNode.removeChild(el);
    }
}

	rowname[0] = "members";
	rowtype[0] = "textbox";
	rowsize[0] = "30";

	rowname[2] = "detail";
	rowtype[2] = "textbox";
	rowsize[2] = "50";
//]]>
</script>
Ad Schellevis's avatar
Ad Schellevis committed
229 230 231
<input type='hidden' name='members_type' value='textbox' class="form-control unknown" />

	<section class="page-content-main">
232
		<div class="container-fluid">
Ad Schellevis's avatar
Ad Schellevis committed
233
			<div class="row">
234

235
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
Ad Schellevis's avatar
Ad Schellevis committed
236 237
				<div id="inputerrors"></div>

238

Ad Schellevis's avatar
Ad Schellevis committed
239
			    <section class="col-xs-12">
240 241 242 243 244 245 246 247 248 249

				<div class="content-box">

					 <header class="content-box-head container-fluid">
				        <h3><?=gettext("Interface Groups Edit");?></h3>
				    </header>

				    <div class="content-box-main">

						<form action="interfaces_groups_edit.php" method="post" name="iform" id="iform">
Ad Schellevis's avatar
Ad Schellevis committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
			                        <table class="table table-striped table-sort">
									  <tr>
									    <td valign="top" class="vncellreq"><?=gettext("Group Name");?></td>
									    <td class="vtable">
										<input type="text" class="form-control unknown" name="ifname" id="ifname" maxlength="15" value="<?=htmlspecialchars($pconfig['ifname']);?>" />
										<br />
										<?=gettext("No numbers or spaces are allowed. Only characters in a-zA-Z");?>
									    </td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top" class="vncell"><?=gettext("Description");?></td>
									    <td width="78%" class="vtable">
									      <input name="descr" type="text" class="form-control unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
									      <br />
									      <span class="vexpl">
									        <?=gettext("You may enter a description here for your reference (not parsed).");?>
									      </span>
									    </td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top" class="vncellreq"><div id="membersnetworkport"><?=gettext("Member (s)");?></div></td>
									    <td width="78%" class="vtable">
									      <table id="maintable" summary="main table">
									        <tbody>
									          <tr>
									            <td><div id="onecolumn"><?=gettext("Interface");?></div></td>
									          </tr>
277

Ad Schellevis's avatar
Ad Schellevis committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
										<?php
										$counter = 0;
										$members = $pconfig['members'];
										if ($members <> "") {
											$item = explode(" ", $members);
											foreach($item as $ww) {
												$members = $item[$counter];
												$tracker = $counter;
										?>
									        <tr>
										<td class="vtable">
										        <select name="members<?php echo $tracker; ?>" class="formselect" id="members<?php echo $tracker; ?>">
												<?php
													$found = false;
													foreach ($iflist as $ifnam => $ifdescr) {
														echo "<option value=\"{$ifnam}\"";
														if ($ifnam == $members) {
															$found = true;
															echo " selected=\"selected\"";
														}
														echo ">{$ifdescr}</option>";
													}
300

Ad Schellevis's avatar
Ad Schellevis committed
301 302 303 304 305 306 307 308
													if ($found === false)
														foreach ($iflist_disabled as $ifnam => $ifdescr)
															if ($ifnam == $members)
																echo "<option value=\"{$ifnam}\" selected=\"selected\">{$ifdescr}</option>";
												?>
									                        </select>
										</td>
									        <td>
309
										<a onclick="removeRow(this); return false;" href="#"><span class="glyphicon glyphicon-remove" alt="remove"></span></a>
Ad Schellevis's avatar
Ad Schellevis committed
310 311 312 313
										      </td>
									          </tr>
									<?php
											$counter++;
314

Ad Schellevis's avatar
Ad Schellevis committed
315 316 317 318 319
											} // end foreach
										} // end if
									?>
									        </tbody>
											  </table>
Ad Schellevis's avatar
Ad Schellevis committed
320 321 322
												<a onclick="javascript:addRowTo('maintable'); return false;" class="btn btn-default btn-xs" href="#">
												    <span class="glyphicon glyphicon-plus"></span>
												</a>
Ad Schellevis's avatar
Ad Schellevis committed
323 324
									      </a>
											<br /><br />
325 326
											<strong><?= gettext('NOTE:') ?></strong>
											<?= gettext('Rules for WAN type interfaces in groups do not contain the reply-to mechanism upon which Multi-WAN typically relies.') ?>
Ad Schellevis's avatar
Ad Schellevis committed
327 328 329 330 331 332 333 334 335 336 337 338 339
											</td>
									  </tr>
									  <tr>
									    <td width="22%" valign="top">&nbsp;</td>
									    <td width="78%">
									      <input id="submit" name="submit" type="submit" class="btn btn-primary" value="<?=gettext("Save");?>" />
									      <a href="interfaces_groups.php"><input id="cancelbutton" name="cancelbutton" type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" /></a>
									      <?php if (isset($id) && $a_ifgroups[$id]): ?>
									      <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
									      <?php endif; ?>
									    </td>
									  </tr>
									</table>
340 341 342
						</form>
				    </div>
				</div>
Ad Schellevis's avatar
Ad Schellevis committed
343 344 345 346
			    </section>
			</div>
		</div>
	</section>
347

Ad Schellevis's avatar
Ad Schellevis committed
348 349 350 351 352 353 354 355 356 357 358 359 360

<script type="text/javascript">
//<![CDATA[
	field_counter_js = 1;
	rows = 1;
	totalrows = <?php echo $counter; ?>;
	loaded = <?php echo $counter; ?>;
//]]>
</script>

<?php
	unset($iflist);
	unset($iflist_disabled);
Ad Schellevis's avatar
Ad Schellevis committed
361
	include("foot.inc");
362
?>