Project

General

Profile

Download (6.94 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * services_wol.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * 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
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * 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
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-services-wakeonlan
30
##|*NAME=Services: Wake-on-LAN
31
##|*DESCR=Allow access to the 'Services: Wake-on-LAN' page.
32
##|*MATCH=services_wol.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36

    
37
function send_wol($if, $mac, $description, & $savemsg, & $class) {
38
	$ipaddr = get_interface_ip($if);
39
	if (!is_ipaddr($ipaddr) || !is_macaddr($mac)) {
40
		return array();
41
	}
42
	if (!empty($description)) {
43
		$description = ' (' . htmlspecialchars($description) . ')';
44
	}
45
	/* determine broadcast address */
46
	$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
47
	/* Execute wol command and check return code. */
48
	if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
49
		$savemsg .= sprintf(gettext('Sent magic packet to %1$s%2$s.'), $mac, $description) . "<br />";
50
		$class = 'success';
51
	} else {
52
		$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 />";
53
		$class = 'warning';
54
	}
55
}
56

    
57
$savemsg = "";
58
$class = "";
59

    
60
if ($_REQUEST['wakeall'] != "") {
61
	foreach (config_get_path('wol/wolentry', []) as $wolent) {
62
		send_wol($wolent['interface'], $wolent['mac'], $wolent['descr'], $savemsg, $class);
63
	}
64
	$savemsg .= gettext('Sent magic packet to all devices.') . "<br />";
65
}
66

    
67
if ($_POST['Submit'] || $_POST['mac']) {
68
	unset($input_errors);
69

    
70
	if ($_POST['mac']) {
71
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
72
		$mac = strtolower(str_replace("-", ":", $_POST['mac']));
73
		$if = $_POST['if'];
74
	}
75

    
76
	/* input validation */
77
	if (!$mac || !is_macaddr($mac)) {
78
		$input_errors[] = gettext("A valid MAC address must be specified.");
79
	}
80

    
81
	if (!$if) {
82
		$input_errors[] = gettext("A valid interface must be specified.");
83
	}
84

    
85
	if (!$input_errors) {
86
		send_wol($if, $mac, '', $savemsg, $class);
87
	}
88
}
89

    
90
if (is_numericint($_POST['id']) && $_POST['act'] == "del") {
91
	if (config_get_path("wol/wolentry/{$_POST['id']}")) {
92
		config_del_path("wol/wolentry/{$_POST['id']}");
93
		write_config(gettext("Deleted a device from WOL configuration."));
94
		header("Location: services_wol.php");
95
		exit;
96
	}
97
}
98

    
99
$pgtitle = array(gettext("Services"), gettext("Wake-on-LAN"));
100
include("head.inc");
101
?>
102
<div class="infoblock blockopen">
103
<?php
104
print_info_box(gettext('This service can be used to wake up (power on) computers by sending special "Magic Packets".') . '<br />' .
105
			   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).'),
106
			   'info', false);
107

    
108
?>
109
</div>
110
<?php
111

    
112
if ($input_errors) {
113
	print_input_errors($input_errors);
114
}
115

    
116
if ($savemsg) {
117
	print_info_box($savemsg, $class);
118
}
119

    
120
$selected_if = (empty($if) ? 'lan' : $if);
121
if (!isset(get_configured_interface_list(false)[$selected_if])) {
122
	$selected_if = null;
123
}
124

    
125
$form = new Form(false);
126

    
127
$section = new Form_Section('Wake-on-LAN');
128

    
129
$section->addInput(new Form_Select(
130
	'if',
131
	'*Interface',
132
	$selected_if,
133
	get_configured_interface_with_descr()
134
))->setHelp('Choose which interface the host to be woken up is connected to.');
135

    
136
$section->addInput(new Form_Input(
137
	'mac',
138
	'*MAC address',
139
	'text',
140
	$mac
141
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
142

    
143
$form->add($section);
144

    
145
$form->addGlobal(new Form_Button(
146
	'Submit',
147
	'Send',
148
	null,
149
	'fa-solid fa-power-off'
150
))->addClass('btn-primary');
151

    
152
print $form;
153
?>
154

    
155
<div class="panel panel-default">
156
	<div class="panel-heading">
157
		<h2 class="panel-title"><?=gettext("Wake-on-LAN Devices");?></h2>
158
	</div>
159

    
160
<?php
161
	// Add top buttons if more than 24 entries in the table
162
	if (count(config_get_path('wol/wolentry', [])) > 24) {
163
?>
164
	<div class="panel-footer">
165
		<a class="btn btn-success" href="services_wol_edit.php">
166
			<i class="fa-solid fa-plus icon-embed-btn"></i>
167
			<?=gettext("Add");?>
168
		</a>
169

    
170
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
171
			<i class="fa-solid fa-power-off icon-embed-btn"></i>
172
			<?=gettext("Wake All Devices")?>
173
		</a>
174
	</div>
175
<?php } ?>
176

    
177
	<div class="panel-body">
178
		<p class="text-danger" style="margin-left: 8px;margin-bottom:0px;"><?=gettext("Click the MAC address to wake up an individual device.")?></p>
179
		<div class="table-responsive">
180
			<table class="table table-striped table-hover table-rowdblclickedit">
181
				<thead>
182
					<tr>
183
						<th><?=gettext("Interface")?></th>
184
						<th><?=gettext("MAC address")?></th>
185
						<th><?=gettext("Description")?></th>
186
						<th><?=gettext("Actions")?></th>
187
					</tr>
188
				</thead>
189
				<tbody>
190
					<?php foreach (config_get_path('wol/wolentry', []) as $i => $wolent): ?>
191
						<tr>
192
							<td>
193
								<?=convert_friendly_interface_to_friendly_descr($wolent['interface']);?>
194
							</td>
195
							<td>
196
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost><?=strtolower($wolent['mac']);?></a>
197
							</td>
198
							<td>
199
								<?=htmlspecialchars($wolent['descr']);?>
200
							</td>
201
							<td>
202
								<a class="fa-solid fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
203
								<a class="fa-solid fa-trash-can"	title="<?=gettext('Delete Device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>" usepost></a>
204
								<a class="fa-solid fa-power-off" title="<?=gettext('Wake Device')?>" href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost></a>
205
							</td>
206
						</tr>
207
					<?php endforeach?>
208
				</tbody>
209
			</table>
210
		</div>
211
	</div>
212
	<div class="panel-footer">
213
		<a class="btn btn-success" href="services_wol_edit.php">
214
			<i class="fa-solid fa-plus icon-embed-btn"></i>
215
			<?=gettext("Add");?>
216
		</a>
217

    
218
		<button id="wakeall" class="btn btn-primary">
219
			<i class="fa-solid fa-power-off icon-embed-btn"></i>
220
			<?=gettext("Wake All Devices")?>
221
		</button>
222
	</div>
223
</div>
224

    
225
<script type="text/javascript">
226
//<![CDATA[
227
events.push(function() {
228

    
229
	$('#wakeall').click(function() {
230
		if (confirm("Are you sure you wish to Wake All Devices?")) {
231
			postSubmit({wakeall: 'true'}, 'services_wol.php');
232
		}
233
	});
234

    
235
});
236
//]]>
237
</script>
238

    
239
<?php
240

    
241
include("foot.inc");
(150-150/232)