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
config_init_path('wol/wolentry');
38

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

    
59
$savemsg = "";
60
$class = "";
61

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

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

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

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

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

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

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

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

    
110
?>
111
</div>
112
<?php
113

    
114
if ($input_errors) {
115
	print_input_errors($input_errors);
116
}
117

    
118
if ($savemsg) {
119
	print_info_box($savemsg, $class);
120
}
121

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

    
127
$form = new Form(false);
128

    
129
$section = new Form_Section('Wake-on-LAN');
130

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

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

    
145
$form->add($section);
146

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

    
154
print $form;
155
?>
156

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

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

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

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

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

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

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

    
237
});
238
//]]>
239
</script>
240

    
241
<?php
242

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