Project

General

Profile

Download (6.45 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-2016 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 ($_GET['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 || $_GET['mac']) {
63
	unset($input_errors);
64

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

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

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

    
104
if ($_GET['act'] == "del") {
105
	if ($a_wol[$_GET['id']]) {
106
		unset($a_wol[$_GET['id']]);
107
		write_config();
108
		header("Location: services_wol.php");
109
		exit;
110
	}
111
}
112

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

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

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

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

    
134
$form = new Form(false);
135

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

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

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

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

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

    
161
print $form;
162
?>
163

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

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

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

    
217
<?php
218

    
219
include("foot.inc");
(145-145/225)