Project

General

Profile

Download (6.27 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
if (!is_array($config['wol']['wolentry'])) {
35
	$config['wol']['wolentry'] = array();
36
}
37
$a_wol = &$config['wol']['wolentry'];
38

    
39
if ($_REQUEST['wakeall'] != "") {
40
	$i = 0;
41
	$savemsg = "";
42
	foreach ($a_wol as $wolent) {
43
		$mac = $wolent['mac'];
44
		$if = $wolent['interface'];
45
		$description = $wolent['descr'];
46
		$ipaddr = get_interface_ip($if);
47
		if (!is_ipaddr($ipaddr)) {
48
			continue;
49
		}
50
		$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
51
		/* Execute wol command and check return code. */
52
		if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
53
			$savemsg .= sprintf(gettext('Sent magic packet to %1$s (%2$s).'), $mac, $description) . "<br />";
54
			$class = 'success';
55
		} else {
56
			$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 />";
57
			$class = 'warning';
58
		}
59
	}
60
}
61

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

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

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

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

    
80
	if (!$input_errors) {
81
		/* determine broadcast address */
82
		$ipaddr = get_interface_ip($if);
83
		if (!is_ipaddr($ipaddr)) {
84
			$input_errors[] = gettext("A valid ip could not be found!");
85
		} else {
86
			$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
87
			/* Execute wol command and check return code. */
88
			if (!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($mac))) {
89
				$savemsg .= sprintf(gettext("Sent magic packet to %s."), $mac);
90
				$class = 'success';
91
			} else {
92
				$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 />";
93
				$class = 'warning';
94
			}
95
		}
96
	}
97
}
98

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

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

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

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

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

    
129
$form = new Form(false);
130

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

    
133
$section->addInput(new Form_Select(
134
	'if',
135
	'*Interface',
136
	(link_interface_to_bridge($if) ? null : $if),
137
	get_configured_interface_with_descr()
138
))->setHelp('Choose which interface the host to be woken up is connected to.');
139

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

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

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

    
156
print $form;
157
?>
158

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

    
164
	<div class="panel-body">
165
		<p><?=gettext("Click the MAC address to wake up an individual device.")?></p>
166
		<div class="table-responsive">
167
			<table class="table table-striped table-hover table-rowdblclickedit">
168
				<thead>
169
					<tr>
170
						<th><?=gettext("Interface")?></th>
171
						<th><?=gettext("MAC address")?></th>
172
						<th><?=gettext("Description")?></th>
173
						<th><?=gettext("Actions")?></th>
174
					</tr>
175
				</thead>
176
				<tbody>
177
					<?php foreach ($a_wol as $i => $wolent): ?>
178
						<tr>
179
							<td>
180
								<?=convert_friendly_interface_to_friendly_descr($wolent['interface']);?>
181
							</td>
182
							<td>
183
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost><?=strtolower($wolent['mac']);?></a>
184
							</td>
185
							<td>
186
								<?=htmlspecialchars($wolent['descr']);?>
187
							</td>
188
							<td>
189
								<a class="fa fa-pencil"	title="<?=gettext('Edit Device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
190
								<a class="fa fa-trash"	title="<?=gettext('Delete Device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>" usepost></a>
191
								<a class="fa fa-power-off" title="<?=gettext('Wake Device')?>" href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>" usepost></a>
192
							</td>
193
						</tr>
194
					<?php endforeach?>
195
				</tbody>
196
			</table>
197
		</div>
198
	</div>
199
	<div class="panel-footer">
200
		<a class="btn btn-success" href="services_wol_edit.php">
201
			<i class="fa fa-plus icon-embed-btn"></i>
202
			<?=gettext("Add");?>
203
		</a>
204

    
205
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
206
			<i class="fa fa-power-off icon-embed-btn"></i>
207
			<?=gettext("Wake All Devices")?>
208
		</a>
209
	</div>
210
</div>
211

    
212
<?php
213

    
214
include("foot.inc");
(151-151/232)