Project

General

Profile

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