Project

General

Profile

Download (6.86 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-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

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

    
33
require_once("guiconfig.inc");
34

    
35
init_config_arr(array('wol', 'wolentry'));
36
$a_wol = &$config['wol']['wolentry'];
37

    
38
if ($_REQUEST['wakeall'] != "") {
39
	$i = 0;
40
	$savemsg = "";
41
	foreach ($a_wol as $wolent) {
42
		$mac = $wolent['mac'];
43
		$if = $wolent['interface'];
44
		$description = $wolent['descr'];
45
		$ipaddr = get_interface_ip($if);
46
		if (!is_ipaddr($ipaddr)) {
47
			continue;
48
		}
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, $mac) . "<br />";
56
			$class = 'warning';
57
		}
58
	}
59
}
60

    
61
if ($_POST['Submit'] || $_POST['mac']) {
62
	unset($input_errors);
63

    
64
	if ($_POST['mac']) {
65
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
66
		$mac = strtolower(str_replace("-", ":", $_POST['mac']));
67
		$if = $_POST['if'];
68
	}
69

    
70
	/* input validation */
71
	if (!$mac || !is_macaddr($mac)) {
72
		$input_errors[] = gettext("A valid MAC address must be specified.");
73
	}
74

    
75
	if (!$if) {
76
		$input_errors[] = gettext("A valid interface must be specified.");
77
	}
78

    
79
	if (!$input_errors) {
80
		/* determine broadcast address */
81
		$ipaddr = get_interface_ip($if);
82
		if (!is_ipaddr($ipaddr)) {
83
			$input_errors[] = gettext("A valid ip could not be found!");
84
		} else {
85
			$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
86
			/* Execute wol command and check return code. */
87
			if (!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($mac))) {
88
				$savemsg .= sprintf(gettext("Sent magic packet to %s."), $mac);
89
				$class = 'success';
90
			} else {
91
				$savemsg .= sprintf(gettext('Please check the %1$ssystem log%2$s, the wol command for %3$s did not complete successfully.'), '<a href="/status_logs.php">', '</a>', $mac) . "<br />";
92
				$class = 'warning';
93
			}
94
		}
95
	}
96
}
97

    
98
if ($_POST['act'] == "del") {
99
	if ($a_wol[$_POST['id']]) {
100
		unset($a_wol[$_POST['id']]);
101
		write_config(gettext("Deleted a device from WOL configuration."));
102
		header("Location: services_wol.php");
103
		exit;
104
	}
105
}
106

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

    
116
?>
117
</div>
118
<?php
119

    
120
if ($input_errors) {
121
	print_input_errors($input_errors);
122
}
123

    
124
if ($savemsg) {
125
	print_info_box($savemsg, $class);
126
}
127

    
128
$selected_if = (empty($if) ? 'lan' : $if);
129
if (!isset(get_configured_interface_list(false)[$selected_if])) {
130
	$selected_if = null;
131
}
132

    
133
$form = new Form(false);
134

    
135
$section = new Form_Section('Wake-on-LAN');
136

    
137
$section->addInput(new Form_Select(
138
	'if',
139
	'*Interface',
140
	$selected_if,
141
	get_configured_interface_with_descr()
142
))->setHelp('Choose which interface the host to be woken up is connected to.');
143

    
144
$section->addInput(new Form_Input(
145
	'mac',
146
	'*MAC address',
147
	'text',
148
	$mac
149
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
150

    
151
$form->add($section);
152

    
153
$form->addGlobal(new Form_Button(
154
	'Submit',
155
	'Send',
156
	null,
157
	'fa-power-off'
158
))->addClass('btn-primary');
159

    
160
print $form;
161
?>
162

    
163
<div class="panel panel-default">
164
	<div class="panel-heading">
165
		<h2 class="panel-title"><?=gettext("Wake-on-LAN Devices");?></h2>
166
	</div>
167

    
168
<?php
169
	// Add top buttons if more than 24 entries in the table
170
	if (is_array($a_wol) && (count($a_wol) > 24)) {
171
?>
172
	<div class="panel-footer">
173
		<a class="btn btn-success" href="services_wol_edit.php">
174
			<i class="fa fa-plus icon-embed-btn"></i>
175
			<?=gettext("Add");?>
176
		</a>
177

    
178
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
179
			<i class="fa fa-power-off icon-embed-btn"></i>
180
			<?=gettext("Wake All Devices")?>
181
		</a>
182
	</div>
183
<?php } ?>
184

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

    
226
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
227
			<i class="fa fa-power-off icon-embed-btn"></i>
228
			<?=gettext("Wake All Devices")?>
229
		</a>
230
	</div>
231
</div>
232

    
233
<?php
234

    
235
include("foot.inc");
(152-152/234)