Project

General

Profile

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

    
39
if (!is_array($config['wol']['wolentry'])) {
40
	$config['wol']['wolentry'] = array();
41
}
42

    
43
$a_wol = &$config['wol']['wolentry'];
44

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

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

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

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

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

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

    
105
if ($_POST['act'] == "del") {
106
	if ($a_wol[$_POST['id']]) {
107
		unset($a_wol[$_POST['id']]);
108
		write_config(gettext("Deleted a device from WOL configuration."));
109
		header("Location: services_wol.php");
110
		exit;
111
	}
112
}
113

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

    
123
?>
124
</div>
125
<?php
126

    
127
if ($input_errors) {
128
	print_input_errors($input_errors);
129
}
130

    
131
if ($savemsg) {
132
	print_info_box($savemsg, $class);
133
}
134

    
135
$form = new Form(false);
136

    
137
$section = new Form_Section('Wake-on-LAN');
138

    
139
$section->addInput(new Form_Select(
140
	'if',
141
	'*Interface',
142
	(link_interface_to_bridge($if) ? null : $if),
143
	get_configured_interface_with_descr()
144
))->setHelp('Choose which interface the host to be woken up is connected to.');
145

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

    
153
$form->add($section);
154

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

    
162
print $form;
163
?>
164

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

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

    
211
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
212
			<i class="fa fa-power-off icon-embed-btn"></i>
213
			<?=gettext("Wake All Devices")?>
214
		</a>
215
	</div>
216
</div>
217

    
218
<?php
219

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