Project

General

Profile

Download (6.83 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-2023 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
init_config_arr(array('wol', 'wolentry'));
38
$a_wol = &$config['wol']['wolentry'];
39

    
40
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
if ($_REQUEST['wakeall'] != "") {
64
	foreach ($a_wol as $wolent) {
65
		send_wol($wolent['interface'], $wolent['mac'], $wolent['descr'], $savemsg, $class);
66
	}
67
	$savemsg .= gettext('Sent magic packet to all devices.') . "<br />";
68
}
69

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

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

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

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

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

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

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

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

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

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

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

    
128
$form = new Form(false);
129

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

    
132
$section->addInput(new Form_Select(
133
	'if',
134
	'*Interface',
135
	$selected_if,
136
	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
	'*MAC address',
142
	'text',
143
	$mac
144
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
145

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

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

    
155
print $form;
156
?>
157

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

    
163
<?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
	<div class="panel-body">
181
		<p class="text-danger" style="margin-left: 8px;margin-bottom:0px;"><?=gettext("Click the MAC address to wake up an individual device.")?></p>
182
		<div class="table-responsive">
183
			<table class="table table-striped table-hover table-rowdblclickedit">
184
				<thead>
185
					<tr>
186
						<th><?=gettext("Interface")?></th>
187
						<th><?=gettext("MAC address")?></th>
188
						<th><?=gettext("Description")?></th>
189
						<th><?=gettext("Actions")?></th>
190
					</tr>
191
				</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
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost><?=strtolower($wolent['mac']);?></a>
200
							</td>
201
							<td>
202
								<?=htmlspecialchars($wolent['descr']);?>
203
							</td>
204
							<td>
205
								<a class="fa fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
206
								<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
							</td>
209
						</tr>
210
					<?php endforeach?>
211
				</tbody>
212
			</table>
213
		</div>
214
	</div>
215
	<div class="panel-footer">
216
		<a class="btn btn-success" href="services_wol_edit.php">
217
			<i class="fa fa-plus icon-embed-btn"></i>
218
			<?=gettext("Add");?>
219
		</a>
220

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

    
228
<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
<?php
243

    
244
include("foot.inc");
(146-146/228)