Project

General

Profile

« Previous | Next » 

Revision 4889ed28

Added by Peter Bouwdewijn about 10 years ago

Added fix in IpAddress to check if a mask is set to prevent a method call on a non object.
Migrated form.
Moved server input to an array form value.

View differences:

usr/local/www/classes/Form/IpAddress.class.php
35 35
	{
36 36
		parent::__construct($name, $title, 'text', $value);
37 37

  
38
		$this->_attributes['pattern'] = '[a-f0-9:.]';
38
		$this->_attributes['pattern'] = '[a-f0-9:.]*';
39 39
	}
40 40

  
41 41
	public function addMask($name, $value)
......
52 52

  
53 53
	public function setIsRepeated()
54 54
	{
55
		$this->_mask->setIsRepeated();
55
		if ($this->_mask)
56
			$this->_mask->setIsRepeated();
56 57

  
57 58
		return parent::setIsRepeated();
58 59
	}
usr/local/www/services_dhcp_relay.php
40 40
##|-PRIV
41 41

  
42 42
require("guiconfig.inc");
43
require('classes/Form.class.php');
44

  
45
function filterDestinationServers(array $destinationServers)
46
{
47
	return array_unique(
48
		array_filter(
49
			$destinationServers,
50
			function($val) {
51
				return !empty($val);
52
			}
53
		)
54
	);
55
}
43 56

  
44 57
$pconfig['enable'] = isset($config['dhcrelay']['enable']);
45 58
if (empty($config['dhcrelay']['interface']))
46 59
	$pconfig['interface'] = array();
47 60
else
48 61
	$pconfig['interface'] = explode(",", $config['dhcrelay']['interface']);
49
$pconfig['server'] = $config['dhcrelay']['server'];
62

  
63
$pconfig['server'] = filterDestinationServers(
64
	explode(',', $config['dhcrelay']['server'])
65
);
50 66
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
51 67

  
52 68
$iflist = get_configured_interface_with_descr();
......
68 84
if ($_POST) {
69 85

  
70 86
	unset($input_errors);
87

  
88
	if ($_POST['server'])
89
		$_POST['server'] = filterDestinationServers($_POST['server']);
90

  
71 91
	$pconfig = $_POST;
72 92

  
73 93
	/* input validation */
......
78 98
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
79 99

  
80 100
		if ($_POST['server']) {
81
			$checksrv = explode(",", $_POST['server']);
82
			foreach ($checksrv as $srv) {
101
			foreach ($_POST['server'] as $srv) {
83 102
				if (!is_ipaddr($srv))
84 103
					$input_errors[] = gettext("A valid Destination Server IP address must be specified.");
85 104
			}
......
90 109
		$config['dhcrelay']['enable'] = $_POST['enable'] ? true : false;
91 110
		$config['dhcrelay']['interface'] = implode(",", $_POST['interface']);
92 111
		$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
93
		$config['dhcrelay']['server'] = $_POST['server'];
112
		$config['dhcrelay']['server'] = implode(',', $_POST['server']);
94 113

  
95 114
		write_config();
96 115

  
......
106 125
$shortcut_section = "dhcp";
107 126
include("head.inc");
108 127

  
109
?>
110

  
111
<script type="text/javascript">
112
//<![CDATA[
113
function enable_change(enable_over) {
114
	if (document.iform.enable.checked || enable_over) {
115
		document.iform.server.disabled = 0;
116
		document.iform.interface.disabled = 0;
117
		document.iform.agentoption.disabled = 0;
118
	} else {
119
		document.iform.server.disabled = 1;
120
		document.iform.interface.disabled = 1;
121
		document.iform.agentoption.disabled = 1;
122
	}
128
if ($dhcpd_enabled)
129
{
130
	echo "DHCP Server is currently enabled. Cannot enable the DHCP Relay service while the DHCP Server is enabled on any interface.";
131
	include("foot.inc");
132
	exit;
123 133
}
124
//]]>
125
</script>
126
</head>
127 134

  
135
if ($input_errors)
136
	print_input_errors($input_errors);
137

  
138
if ($savemsg)
139
	print_info_box($savemsg);
140

  
141
$form = new Form;
142

  
143
$section = new Form_Section('DHCP Relay configuration');
144
$form->add($section);
145

  
146
$section->addInput(new Form_Checkbox(
147
	'enable',
148
	'Enable',
149
	'Enable DHCP relay on interface',
150
	$pconfig['enable']
151
))->toggles('.form-group:not(:first-child)');
152

  
153
$section->addInput(new Form_Select(
154
	'interface',
155
	'Interface(s)',
156
	$pconfig['interface'],
157
	$iflist,
158
	true
159
));
160

  
161
$section->addInput(new Form_Checkbox(
162
	'agentoption',
163
	'',
164
	'Append circuit ID and agent ID to requests',
165
	'yes',
166
	$pconfig['agentoption']
167
))->setHelp(
168
	'If this is checked, the DHCP relay will append the circuit ID (%s interface number) and the agent ID to the DHCP request.',
169
	[$g['product_name']]
170
);
171

  
172
//Small function to prevent duplicate code
173
function createDestinationServerInputGroup($value = null)
174
{
175
	$group = new Form_Group('Destination server');
176
	$group->enableDuplication();
177

  
178
	$group->add(new Form_IpAddress(
179
		'server',
180
		'Destination server',
181
		$value
182
	))->setHelp(
183
		'This is the IP address of the server to which DHCP requests are relayed.'
184
	)->setIsRepeated();
185

  
186
	return $group;
187
}
128 188

  
189
if (!isset($pconfig['server']) || count($pconfig['server']) < 1)
190
	$section->add(createDestinationServerInputGroup());
191
else
192
	foreach ($pconfig['server'] as $idx => $server)
193
		$section->add(createDestinationServerInputGroup($server));
129 194

  
130
<form action="services_dhcp_relay.php" method="post" name="iform" id="iform">
131
<?php if ($input_errors) print_input_errors($input_errors)?>
132
<?php if ($savemsg) print_info_box($savemsg)?>
195
print $form;
133 196

  
134
<table>
135
  <tr>
136
	<td>
137
	<div>
138
			  <table>
139
		<tr>
140
<?php
141
	if ($dhcpd_enabled) {
142
		echo "<td>DHCP Server is currently enabled. Cannot enable the DHCP Relay service while the DHCP Server is enabled on any interface.";
143
			echo "</td></tr></table></div></td></tr></table></form>";
144
			include("fend.inc"); 
145
			echo "</body></html>";
146
			exit;
147
		}
148
?>
149

  
150
			<td><?=gettext("DHCP Relay configuration")?></td>
151
		</tr>
152
		<tr>
153
						<td>Enable</td>
154
						<td>
155
			<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked=\"checked\""?> onclick="enable_change(false)" />
156
						  <strong><?php printf(gettext("Enable DHCP relay on interface"))?></strong>
157
			</td>
158
		</tr>
159
		<tr>
160
						<td>Interface(s)</td>
161
						<td>
162
				<select id="interface" name="interface[]" multiple="multiple" class="formselect" size="3">
163
			<?php
164
								foreach ($iflist as $ifent => $ifdesc) {
165
					if (!is_ipaddr(get_interface_ip($ifent)))
166
						continue;
167
					echo "<option value=\"{$ifent}\"";
168
					if (in_array($ifent, $pconfig['interface']))
169
						echo " selected=\"selected\"";
170
					echo ">{$ifdesc}</option>\n";
171
				}
172
			?>
173
								</select>
174
				<br />Interfaces without an IP address will not be shown.
175
			</td>
176
		</tr>
177
		<tr>
178
				  <td>&nbsp;</td>
179
					  <td>
180
<input name="agentoption" type="checkbox" value="yes" <?php if ($pconfig['agentoption']) echo "checked=\"checked\""?> />
181
					  <strong><?=gettext("Append circuit ID and agent ID to requests")?></strong><br />
182
					  <?php printf(gettext("If this is checked, the DHCP relay will append the circuit ID (%s interface number) and the agent ID to the DHCP request."), $g['product_name'])?></td>
183
		</tr>
184
		<tr>
185
						<td><?=gettext("Destination server")?></td>
186
						<td>
187
						  <input name="server" type="text" class="formfld unknown" id="server" size="20" value="<?=htmlspecialchars($pconfig['server'])?>" />
188
						  <br />
189
			  <?=gettext("This is the IP address of the server to which DHCP requests are relayed. You can enter multiple server IP addresses, separated by commas.")?>
190
						</td>
191
		</tr>
192
		<tr>
193
						<td>&nbsp;</td>
194
						<td>
195
						  <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save")?>" onclick="enable_change(true)" />
196
						</td>
197
		</tr>
198
	</table>
199
	</div>
200
	</td>
201
  </tr>
202
</table>
203
</form>
204
<script type="text/javascript">
205
//<![CDATA[
206
enable_change(false);
207
//]]>
208
</script>
209
<?php include("foot.inc")?>
210
</body>
211
</html>
197
include("foot.inc");

Also available in: Unified diff