Project

General

Profile

Download (6.83 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 8f2f85c3 Luiz Otavio O Souza
 * Copyright (c) 2014-2022 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 2e94828c jim-p
function send_wol($if, $mac, $description, & $savemsg, & $class) {
41
	$ipaddr = get_interface_ip($if);
42
	if (!is_ipaddr($ipaddr) || !is_macaddr($mac)) {
43
		return array();
44
	}
45
	if (!empty($description)) {
46
		$description = ' (' . htmlspecialchars($description) . ')';
47
	}
48
	/* determine broadcast address */
49
	$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
50
	/* Execute wol command and check return code. */
51
	if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
52
		$savemsg .= sprintf(gettext('Sent magic packet to %1$s%2$s.'), $mac, $description) . "<br />";
53
		$class = 'success';
54
	} else {
55
		$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, htmlspecialchars($mac)) . "<br />";
56
		$class = 'warning';
57
	}
58
}
59
60
$savemsg = "";
61
$class = "";
62
63 13541a81 Steve Beaver
if ($_REQUEST['wakeall'] != "") {
64 4751361d Scott Ullrich
	foreach ($a_wol as $wolent) {
65 2e94828c jim-p
		send_wol($wolent['interface'], $wolent['mac'], $wolent['descr'], $savemsg, $class);
66 4751361d Scott Ullrich
	}
67 f645fb5f Viktor G
	$savemsg .= gettext('Sent magic packet to all devices.') . "<br />";
68 4751361d Scott Ullrich
}
69
70 13541a81 Steve Beaver
if ($_POST['Submit'] || $_POST['mac']) {
71 5b237745 Scott Ullrich
	unset($input_errors);
72 f0fe3d30 Scott Ullrich
73 13541a81 Steve Beaver
	if ($_POST['mac']) {
74 56463a6c Phil Davis
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
75 13541a81 Steve Beaver
		$mac = strtolower(str_replace("-", ":", $_POST['mac']));
76
		$if = $_POST['if'];
77 5b237745 Scott Ullrich
	}
78
79
	/* input validation */
80 56463a6c Phil Davis
	if (!$mac || !is_macaddr($mac)) {
81 d58dbf67 Rafael Lucas
		$input_errors[] = gettext("A valid MAC address must be specified.");
82 56463a6c Phil Davis
	}
83 13541a81 Steve Beaver
84 56463a6c Phil Davis
	if (!$if) {
85 d58dbf67 Rafael Lucas
		$input_errors[] = gettext("A valid interface must be specified.");
86 56463a6c Phil Davis
	}
87 5b237745 Scott Ullrich
88 f0fe3d30 Scott Ullrich
	if (!$input_errors) {
89 2e94828c jim-p
		send_wol($if, $mac, '', $savemsg, $class);
90 5b237745 Scott Ullrich
	}
91
}
92
93 13541a81 Steve Beaver
if ($_POST['act'] == "del") {
94
	if ($a_wol[$_POST['id']]) {
95
		unset($a_wol[$_POST['id']]);
96 b525d581 doktornotor
		write_config(gettext("Deleted a device from WOL configuration."));
97 5b237745 Scott Ullrich
		header("Location: services_wol.php");
98
		exit;
99
	}
100
}
101 4df96eff Scott Ullrich
102 7ca42d47 k-paulius
$pgtitle = array(gettext("Services"), gettext("Wake-on-LAN"));
103 4df96eff Scott Ullrich
include("head.inc");
104 f78bbe16 Phil Davis
?>
105 c95dabdd Stephen Beaver
<div class="infoblock blockopen">
106 f78bbe16 Phil Davis
<?php
107 4bfc3f7d Phil Davis
print_info_box(gettext('This service can be used to wake up (power on) computers by sending special "Magic Packets".') . '<br />' .
108 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).'),
109 4bfc3f7d Phil Davis
			   'info', false);
110 5b237745 Scott Ullrich
111 6658142a Stephen Beaver
?>
112 f78bbe16 Phil Davis
</div>
113 bc3fa9f1 Peter Bouwdewijn
<?php
114
115 20db3e1a Phil Davis
if ($input_errors) {
116 bc3fa9f1 Peter Bouwdewijn
	print_input_errors($input_errors);
117 20db3e1a Phil Davis
}
118 bc3fa9f1 Peter Bouwdewijn
119 20db3e1a Phil Davis
if ($savemsg) {
120 f78bbe16 Phil Davis
	print_info_box($savemsg, $class);
121 20db3e1a Phil Davis
}
122 bc3fa9f1 Peter Bouwdewijn
123 c818076d luckman212
$selected_if = (empty($if) ? 'lan' : $if);
124
if (!isset(get_configured_interface_list(false)[$selected_if])) {
125
	$selected_if = null;
126
}
127
128 37676f4e jim-p
$form = new Form(false);
129 bc3fa9f1 Peter Bouwdewijn
130 7ca42d47 k-paulius
$section = new Form_Section('Wake-on-LAN');
131 bc3fa9f1 Peter Bouwdewijn
132
$section->addInput(new Form_Select(
133 9cc7e37b PiBa-NL
	'if',
134 24b82516 Phil Davis
	'*Interface',
135 c818076d luckman212
	$selected_if,
136 bc3fa9f1 Peter Bouwdewijn
	get_configured_interface_with_descr()
137
))->setHelp('Choose which interface the host to be woken up is connected to.');
138
139
$section->addInput(new Form_Input(
140
	'mac',
141 24b82516 Phil Davis
	'*MAC address',
142 ba8749ed Peter Bouwdewijn
	'text',
143
	$mac
144 314a088a Phil Davis
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
145 bc3fa9f1 Peter Bouwdewijn
146 e8a37c87 jim-p
$form->add($section);
147
148
$form->addGlobal(new Form_Button(
149 37676f4e jim-p
	'Submit',
150 faab522f Renato Botelho
	'Send',
151 37676f4e jim-p
	null,
152
	'fa-power-off'
153
))->addClass('btn-primary');
154
155 bc3fa9f1 Peter Bouwdewijn
print $form;
156
?>
157
158
<div class="panel panel-default">
159
	<div class="panel-heading">
160 7ca42d47 k-paulius
		<h2 class="panel-title"><?=gettext("Wake-on-LAN Devices");?></h2>
161 bc3fa9f1 Peter Bouwdewijn
	</div>
162
163 e7299fd8 Steve Beaver
<?php
164
	// Add top buttons if more than 24 entries in the table
165
	if (is_array($a_wol) && (count($a_wol) > 24)) {
166
?>
167
	<div class="panel-footer">
168
		<a class="btn btn-success" href="services_wol_edit.php">
169
			<i class="fa fa-plus icon-embed-btn"></i>
170
			<?=gettext("Add");?>
171
		</a>
172
173
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
174
			<i class="fa fa-power-off icon-embed-btn"></i>
175
			<?=gettext("Wake All Devices")?>
176
		</a>
177
	</div>
178
<?php } ?>
179
180 bc3fa9f1 Peter Bouwdewijn
	<div class="panel-body">
181 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>
182 f0b20c3f Peter Bouwdewijn
		<div class="table-responsive">
183 1c10ce97 PiBa-NL
			<table class="table table-striped table-hover table-rowdblclickedit">
184 f0b20c3f Peter Bouwdewijn
				<thead>
185 bc3fa9f1 Peter Bouwdewijn
					<tr>
186 f0b20c3f Peter Bouwdewijn
						<th><?=gettext("Interface")?></th>
187
						<th><?=gettext("MAC address")?></th>
188
						<th><?=gettext("Description")?></th>
189 70dc5cd6 Phil Davis
						<th><?=gettext("Actions")?></th>
190 bc3fa9f1 Peter Bouwdewijn
					</tr>
191 f0b20c3f Peter Bouwdewijn
				</thead>
192
				<tbody>
193
					<?php foreach ($a_wol as $i => $wolent): ?>
194
						<tr>
195
							<td>
196
								<?=convert_friendly_interface_to_friendly_descr($wolent['interface']);?>
197
							</td>
198
							<td>
199 9cc7e37b PiBa-NL
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost><?=strtolower($wolent['mac']);?></a>
200 f0b20c3f Peter Bouwdewijn
							</td>
201
							<td>
202
								<?=htmlspecialchars($wolent['descr']);?>
203
							</td>
204
							<td>
205 e8a37c87 jim-p
								<a class="fa fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
206 13541a81 Steve Beaver
								<a class="fa fa-trash"	title="<?=gettext('Delete Device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>" usepost></a>
207
								<a class="fa fa-power-off" title="<?=gettext('Wake Device')?>" href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost></a>
208 f0b20c3f Peter Bouwdewijn
							</td>
209
						</tr>
210
					<?php endforeach?>
211
				</tbody>
212
			</table>
213
		</div>
214 bc3fa9f1 Peter Bouwdewijn
	</div>
215
	<div class="panel-footer">
216
		<a class="btn btn-success" href="services_wol_edit.php">
217 37676f4e jim-p
			<i class="fa fa-plus icon-embed-btn"></i>
218 4bb7c0d1 bruno
			<?=gettext("Add");?>
219 bc3fa9f1 Peter Bouwdewijn
		</a>
220
221 f645fb5f Viktor G
		<button id="wakeall" class="btn btn-primary">
222 37676f4e jim-p
			<i class="fa fa-power-off icon-embed-btn"></i>
223 e8a37c87 jim-p
			<?=gettext("Wake All Devices")?>
224 f645fb5f Viktor G
		</button>
225 bc3fa9f1 Peter Bouwdewijn
	</div>
226
</div>
227
228 f645fb5f Viktor G
<script type="text/javascript">
229
//<![CDATA[
230
events.push(function() {
231
232
	$('#wakeall').click(function() {
233
		if (confirm("Are you sure you wish to Wake All Devices?")) {
234
			postSubmit({wakeall: 'true'}, 'services_wol.php');
235
		}
236
	});
237
238
});
239
//]]>
240
</script>
241
242 bc3fa9f1 Peter Bouwdewijn
<?php
243
244 370358b8 heper
include("foot.inc");