Project

General

Profile

Download (6.86 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * services_wol.php
4 191cb31d Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8 b9043cdc Stephen Beaver
 *
9 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12 b9043cdc Stephen Beaver
 *
13 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16 b9043cdc Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 b9043cdc Stephen Beaver
 *
19 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24 c9df279d Stephen Beaver
 */
25 5b237745 Scott Ullrich
26 6b07c15a Matthew Grooms
##|+PRIV
27
##|*IDENT=page-services-wakeonlan
28 7ca42d47 k-paulius
##|*NAME=Services: Wake-on-LAN
29
##|*DESCR=Allow access to the 'Services: Wake-on-LAN' page.
30 6b07c15a Matthew Grooms
##|*MATCH=services_wol.php*
31
##|-PRIV
32
33 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
34 3f01e023 Steve Beaver
35 ea0dd417 jim-p
init_config_arr(array('wol', 'wolentry'));
36 5b237745 Scott Ullrich
$a_wol = &$config['wol']['wolentry'];
37
38 13541a81 Steve Beaver
if ($_REQUEST['wakeall'] != "") {
39 4751361d Scott Ullrich
	$i = 0;
40
	$savemsg = "";
41
	foreach ($a_wol as $wolent) {
42
		$mac = $wolent['mac'];
43
		$if = $wolent['interface'];
44 1b197e55 stompro
		$description = $wolent['descr'];
45 2bf16ba2 Ermal
		$ipaddr = get_interface_ip($if);
46 56463a6c Phil Davis
		if (!is_ipaddr($ipaddr)) {
47 2bf16ba2 Ermal
			continue;
48 56463a6c Phil Davis
		}
49 2bf16ba2 Ermal
		$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
50 1b197e55 stompro
		/* Execute wol command and check return code. */
51 56463a6c Phil Davis
		if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
52 4bfc3f7d Phil Davis
			$savemsg .= sprintf(gettext('Sent magic packet to %1$s (%2$s).'), $mac, $description) . "<br />";
53 f78bbe16 Phil Davis
			$class = 'success';
54 56463a6c Phil Davis
		} else {
55 4bfc3f7d Phil Davis
			$savemsg .= sprintf(gettext('Please check the %1$ssystem log%2$s, the wol command for %3$s (%4$s) did not complete successfully.'), '<a href="/status_logs.php">', '</a>', $description, $mac) . "<br />";
56 f78bbe16 Phil Davis
			$class = 'warning';
57 56463a6c Phil Davis
		}
58 4751361d Scott Ullrich
	}
59
}
60
61 13541a81 Steve Beaver
if ($_POST['Submit'] || $_POST['mac']) {
62 5b237745 Scott Ullrich
	unset($input_errors);
63 f0fe3d30 Scott Ullrich
64 13541a81 Steve Beaver
	if ($_POST['mac']) {
65 56463a6c Phil Davis
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
66 13541a81 Steve Beaver
		$mac = strtolower(str_replace("-", ":", $_POST['mac']));
67
		$if = $_POST['if'];
68 5b237745 Scott Ullrich
	}
69
70
	/* input validation */
71 56463a6c Phil Davis
	if (!$mac || !is_macaddr($mac)) {
72 d58dbf67 Rafael Lucas
		$input_errors[] = gettext("A valid MAC address must be specified.");
73 56463a6c Phil Davis
	}
74 13541a81 Steve Beaver
75 56463a6c Phil Davis
	if (!$if) {
76 d58dbf67 Rafael Lucas
		$input_errors[] = gettext("A valid interface must be specified.");
77 56463a6c Phil Davis
	}
78 5b237745 Scott Ullrich
79 f0fe3d30 Scott Ullrich
	if (!$input_errors) {
80 5b237745 Scott Ullrich
		/* determine broadcast address */
81 2bf16ba2 Ermal
		$ipaddr = get_interface_ip($if);
82 56463a6c Phil Davis
		if (!is_ipaddr($ipaddr)) {
83 2bf16ba2 Ermal
			$input_errors[] = gettext("A valid ip could not be found!");
84 56463a6c Phil Davis
		} else {
85 2bf16ba2 Ermal
			$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
86
			/* Execute wol command and check return code. */
87 56463a6c Phil Davis
			if (!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($mac))) {
88
				$savemsg .= sprintf(gettext("Sent magic packet to %s."), $mac);
89 f78bbe16 Phil Davis
				$class = 'success';
90 56463a6c Phil Davis
			} else {
91 4bfc3f7d Phil Davis
				$savemsg .= sprintf(gettext('Please check the %1$ssystem log%2$s, the wol command for %3$s did not complete successfully.'), '<a href="/status_logs.php">', '</a>', $mac) . "<br />";
92 f78bbe16 Phil Davis
				$class = 'warning';
93 56463a6c Phil Davis
			}
94 1b197e55 stompro
		}
95 5b237745 Scott Ullrich
	}
96
}
97
98 13541a81 Steve Beaver
if ($_POST['act'] == "del") {
99
	if ($a_wol[$_POST['id']]) {
100
		unset($a_wol[$_POST['id']]);
101 b525d581 doktornotor
		write_config(gettext("Deleted a device from WOL configuration."));
102 5b237745 Scott Ullrich
		header("Location: services_wol.php");
103
		exit;
104
	}
105
}
106 4df96eff Scott Ullrich
107 7ca42d47 k-paulius
$pgtitle = array(gettext("Services"), gettext("Wake-on-LAN"));
108 4df96eff Scott Ullrich
include("head.inc");
109 f78bbe16 Phil Davis
?>
110 c95dabdd Stephen Beaver
<div class="infoblock blockopen">
111 f78bbe16 Phil Davis
<?php
112 4bfc3f7d Phil Davis
print_info_box(gettext('This service can be used to wake up (power on) computers by sending special "Magic Packets".') . '<br />' .
113 7ca42d47 k-paulius
			   gettext('The NIC in the computer that is to be woken up must support Wake-on-LAN and must be properly configured (WOL cable, BIOS settings).'),
114 4bfc3f7d Phil Davis
			   'info', false);
115 5b237745 Scott Ullrich
116 6658142a Stephen Beaver
?>
117 f78bbe16 Phil Davis
</div>
118 bc3fa9f1 Peter Bouwdewijn
<?php
119
120 20db3e1a Phil Davis
if ($input_errors) {
121 bc3fa9f1 Peter Bouwdewijn
	print_input_errors($input_errors);
122 20db3e1a Phil Davis
}
123 bc3fa9f1 Peter Bouwdewijn
124 20db3e1a Phil Davis
if ($savemsg) {
125 f78bbe16 Phil Davis
	print_info_box($savemsg, $class);
126 20db3e1a Phil Davis
}
127 bc3fa9f1 Peter Bouwdewijn
128 fa4734fc luckman212
$selected_if = (empty($if) ? 'lan' : $if);
129
if (!isset(get_configured_interface_list(false)[$selected_if])) {
130
	$selected_if = null;
131
}
132
133 37676f4e jim-p
$form = new Form(false);
134 bc3fa9f1 Peter Bouwdewijn
135 7ca42d47 k-paulius
$section = new Form_Section('Wake-on-LAN');
136 bc3fa9f1 Peter Bouwdewijn
137
$section->addInput(new Form_Select(
138 9cc7e37b PiBa-NL
	'if',
139 24b82516 Phil Davis
	'*Interface',
140 fa4734fc luckman212
	$selected_if,
141 bc3fa9f1 Peter Bouwdewijn
	get_configured_interface_with_descr()
142
))->setHelp('Choose which interface the host to be woken up is connected to.');
143
144
$section->addInput(new Form_Input(
145
	'mac',
146 24b82516 Phil Davis
	'*MAC address',
147 ba8749ed Peter Bouwdewijn
	'text',
148
	$mac
149 314a088a Phil Davis
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
150 bc3fa9f1 Peter Bouwdewijn
151 e8a37c87 jim-p
$form->add($section);
152
153
$form->addGlobal(new Form_Button(
154 37676f4e jim-p
	'Submit',
155 faab522f Renato Botelho
	'Send',
156 37676f4e jim-p
	null,
157
	'fa-power-off'
158
))->addClass('btn-primary');
159
160 bc3fa9f1 Peter Bouwdewijn
print $form;
161
?>
162
163
<div class="panel panel-default">
164
	<div class="panel-heading">
165 7ca42d47 k-paulius
		<h2 class="panel-title"><?=gettext("Wake-on-LAN Devices");?></h2>
166 bc3fa9f1 Peter Bouwdewijn
	</div>
167
168 613fa52d Steve Beaver
<?php
169
	// Add top buttons if more than 24 entries in the table
170
	if (is_array($a_wol) && (count($a_wol) > 24)) {
171
?>
172
	<div class="panel-footer">
173
		<a class="btn btn-success" href="services_wol_edit.php">
174
			<i class="fa fa-plus icon-embed-btn"></i>
175
			<?=gettext("Add");?>
176
		</a>
177
178
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
179
			<i class="fa fa-power-off icon-embed-btn"></i>
180
			<?=gettext("Wake All Devices")?>
181
		</a>
182
	</div>
183
<?php } ?>
184
185 bc3fa9f1 Peter Bouwdewijn
	<div class="panel-body">
186 613fa52d Steve Beaver
		<p class="text-danger" style="margin-left: 8px;margin-bottom:0px;"><?=gettext("Click the MAC address to wake up an individual device.")?></p>
187 f0b20c3f Peter Bouwdewijn
		<div class="table-responsive">
188 1c10ce97 PiBa-NL
			<table class="table table-striped table-hover table-rowdblclickedit">
189 f0b20c3f Peter Bouwdewijn
				<thead>
190 bc3fa9f1 Peter Bouwdewijn
					<tr>
191 f0b20c3f Peter Bouwdewijn
						<th><?=gettext("Interface")?></th>
192
						<th><?=gettext("MAC address")?></th>
193
						<th><?=gettext("Description")?></th>
194 70dc5cd6 Phil Davis
						<th><?=gettext("Actions")?></th>
195 bc3fa9f1 Peter Bouwdewijn
					</tr>
196 f0b20c3f Peter Bouwdewijn
				</thead>
197
				<tbody>
198
					<?php foreach ($a_wol as $i => $wolent): ?>
199
						<tr>
200
							<td>
201
								<?=convert_friendly_interface_to_friendly_descr($wolent['interface']);?>
202
							</td>
203
							<td>
204 9cc7e37b PiBa-NL
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost><?=strtolower($wolent['mac']);?></a>
205 f0b20c3f Peter Bouwdewijn
							</td>
206
							<td>
207
								<?=htmlspecialchars($wolent['descr']);?>
208
							</td>
209
							<td>
210 e8a37c87 jim-p
								<a class="fa fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
211 13541a81 Steve Beaver
								<a class="fa fa-trash"	title="<?=gettext('Delete Device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>" usepost></a>
212
								<a class="fa fa-power-off" title="<?=gettext('Wake Device')?>" href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost></a>
213 f0b20c3f Peter Bouwdewijn
							</td>
214
						</tr>
215
					<?php endforeach?>
216
				</tbody>
217
			</table>
218
		</div>
219 bc3fa9f1 Peter Bouwdewijn
	</div>
220
	<div class="panel-footer">
221
		<a class="btn btn-success" href="services_wol_edit.php">
222 37676f4e jim-p
			<i class="fa fa-plus icon-embed-btn"></i>
223 4bb7c0d1 bruno
			<?=gettext("Add");?>
224 bc3fa9f1 Peter Bouwdewijn
		</a>
225
226
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
227 37676f4e jim-p
			<i class="fa fa-power-off icon-embed-btn"></i>
228 e8a37c87 jim-p
			<?=gettext("Wake All Devices")?>
229 bc3fa9f1 Peter Bouwdewijn
		</a>
230
	</div>
231
</div>
232
233
<?php
234
235 370358b8 heper
include("foot.inc");