Project

General

Profile

Download (6.55 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-2021 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
}
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 ($a_wol[$_POST['id']]) {
94
		unset($a_wol[$_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-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 (is_array($a_wol) && (count($a_wol) > 24)) {
165
?>
166
	<div class="panel-footer">
167
		<a class="btn btn-success" href="services_wol_edit.php">
168
			<i class="fa 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 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 ($a_wol 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 fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
205
								<a class="fa fa-trash"	title="<?=gettext('Delete Device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>" usepost></a>
206
								<a class="fa 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 fa-plus icon-embed-btn"></i>
217
			<?=gettext("Add");?>
218
		</a>
219

    
220
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary" usepost>
221
			<i class="fa fa-power-off icon-embed-btn"></i>
222
			<?=gettext("Wake All Devices")?>
223
		</a>
224
	</div>
225
</div>
226

    
227
<?php
228

    
229
include("foot.inc");
(146-146/227)