Project

General

Profile

Download (6.44 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
$selected_if = (empty($if) ? 'lan' : $if);
136
if (!isset(get_configured_interface_list(false)[$selected_if])) {
137
	$selected_if = null;
138
}
139

    
140
$form = new Form(false);
141

    
142
$section = new Form_Section('Wake-on-LAN');
143

    
144
$section->addInput(new Form_Select(
145
	'if',
146
	'*Interface',
147
	$selected_if,
148
	get_configured_interface_with_descr()
149
))->setHelp('Choose which interface the host to be woken up is connected to.');
150

    
151
$section->addInput(new Form_Input(
152
	'mac',
153
	'*MAC address',
154
	'text',
155
	$mac
156
))->setHelp('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx');
157

    
158
$form->add($section);
159

    
160
$form->addGlobal(new Form_Button(
161
	'Submit',
162
	'Send',
163
	null,
164
	'fa-power-off'
165
))->addClass('btn-primary');
166

    
167
print $form;
168
?>
169

    
170
<div class="panel panel-default">
171
	<div class="panel-heading">
172
		<h2 class="panel-title"><?=gettext("Wake-on-LAN Devices");?></h2>
173
	</div>
174

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

    
216
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
217
			<i class="fa fa-power-off icon-embed-btn"></i>
218
			<?=gettext("Wake All Devices")?>
219
		</a>
220
	</div>
221
</div>
222

    
223
<?php
224

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