Project

General

Profile

Download (6.52 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services_wol.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	notice, this list of conditions and the following disclaimer in the
19
	documentation and/or other materials provided with the distribution.
20

    
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
/*
33
	pfSense_BUILDER_BINARIES:	/usr/local/bin/wol
34
	pfSense_MODULE:	wol
35
*/
36

    
37
##|+PRIV
38
##|*IDENT=page-services-wakeonlan
39
##|*NAME=Services: Wake on LAN page
40
##|*DESCR=Allow access to the 'Services: Wake on LAN' page.
41
##|*MATCH=services_wol.php*
42
##|-PRIV
43

    
44
require("guiconfig.inc");
45
require_once('classes/Form.class.php');
46

    
47
if (!is_array($config['wol']['wolentry'])) {
48
	$config['wol']['wolentry'] = array();
49
}
50
$a_wol = &$config['wol']['wolentry'];
51

    
52
if($_GET['wakeall'] != "") {
53
	$i = 0;
54
	$savemsg = "";
55
	foreach ($a_wol as $wolent) {
56
		$mac = $wolent['mac'];
57
		$if = $wolent['interface'];
58
		$description = $wolent['descr'];
59
		$ipaddr = get_interface_ip($if);
60
		if (!is_ipaddr($ipaddr)) {
61
			continue;
62
		}
63
		$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
64
		/* Execute wol command and check return code. */
65
		if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
66
			$savemsg .= sprintf(gettext('Sent magic packet to %1$s (%2$s)%3$s'), $mac, $description, ".<br />");
67
		} else {
68
			$savemsg .= sprintf(gettext('Please check the %1$ssystem log%2$s, the wol command for %3$s (%4$s) did not complete successfully%5$s'), '<a href="/diag_logs.php">', '</a>', $description, $mac, ".<br />");
69
		}
70
	}
71
}
72

    
73
if ($_POST || $_GET['mac']) {
74
	unset($input_errors);
75

    
76
	if ($_GET['mac']) {
77
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
78
		$_GET['mac'] = strtolower(str_replace("-", ":", $_GET['mac']));
79
		$mac = $_GET['mac'];
80
		$if = $_GET['if'];
81
	} else {
82
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
83
		$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
84
		$mac = $_POST['mac'];
85
		$if = $_POST['interface'];
86
	}
87

    
88
	/* input validation */
89
	if (!$mac || !is_macaddr($mac)) {
90
		$input_errors[] = gettext("A valid MAC address must be specified.");
91
	}
92
	if (!$if) {
93
		$input_errors[] = gettext("A valid interface must be specified.");
94
	}
95

    
96
	if (!$input_errors) {
97
		/* determine broadcast address */
98
		$ipaddr = get_interface_ip($if);
99
		if (!is_ipaddr($ipaddr)) {
100
			$input_errors[] = gettext("A valid ip could not be found!");
101
		} else {
102
			$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
103
			/* Execute wol command and check return code. */
104
			if (!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($mac))) {
105
				$savemsg .= sprintf(gettext("Sent magic packet to %s."), $mac);
106
			} else {
107
				$savemsg .= sprintf(gettext('Please check the %1$ssystem log%2$s, the wol command for %3$s did not complete successfully%4$s'), '<a href="/diag_logs.php">', '</a>', $mac, ".<br />");
108
			}
109
		}
110
	}
111
}
112

    
113
if ($_GET['act'] == "del") {
114
	if ($a_wol[$_GET['id']]) {
115
		unset($a_wol[$_GET['id']]);
116
		write_config();
117
		header("Location: services_wol.php");
118
		exit;
119
	}
120
}
121

    
122
$pgtitle = array(gettext("Services"), gettext("Wake on LAN"));
123
include("head.inc");
124

    
125
print_info_box(gettext('This service can be used to wake up (power on) computers by sending special') . ' "' . gettext('Magic Packets') . '"<br />' . 
126
			   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).'));
127

    
128
?>
129

    
130
<?php
131

    
132
if ($input_errors)
133
	print_input_errors($input_errors);
134

    
135
if ($savemsg)
136
	print_info_box($savemsg);
137

    
138
$form = new Form('Send');
139

    
140
$section = new Form_Section('Wake on LAN');
141

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

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

    
156
$form->add($section);
157
print $form;
158
?>
159

    
160
<div class="panel panel-default">
161
	<div class="panel-heading">
162
		<h2 class="panel-title">Wake on LAN devices</h2>
163
	</div>
164

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

    
208
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
209
			<?=gettext("Wake all devices")?>
210
		</a>
211
	</div>
212
</div>
213

    
214
<?php
215

    
216
include("foot.inc");
(158-158/235)