Project

General

Profile

Download (7.67 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_wol.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Some or all of this file is based on the m0n0wall project which is
9
 *	Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *	Redistribution and use in source and binary forms, with or without modification,
12
 *	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
19
 *		the documentation and/or other materials provided with the
20
 *		distribution.
21
 *
22
 *	3. All advertising materials mentioning features or use of this software
23
 *		must display the following acknowledgment:
24
 *		"This product includes software developed by the pfSense Project
25
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
27
 *	4. The names "pfSense" and "pfSense Project" must not be used to
28
 *		 endorse or promote products derived from this software without
29
 *		 prior written permission. For written permission, please contact
30
 *		 coreteam@pfsense.org.
31
 *
32
 *	5. Products derived from this software may not be called "pfSense"
33
 *		nor may "pfSense" appear in their names without prior written
34
 *		permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *	6. Redistributions of any form whatsoever must retain the following
37
 *		acknowledgment:
38
 *
39
 *	"This product includes software developed by the pfSense Project
40
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
41
 *
42
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *	====================================================================
56
 *
57
 */
58
/*
59
	pfSense_BUILDER_BINARIES:	/usr/local/bin/wol
60
	pfSense_MODULE:	wol
61
*/
62

    
63
##|+PRIV
64
##|*IDENT=page-services-wakeonlan
65
##|*NAME=Services: Wake on LAN
66
##|*DESCR=Allow access to the 'Services: Wake on LAN' page.
67
##|*MATCH=services_wol.php*
68
##|-PRIV
69

    
70
require("guiconfig.inc");
71
if (!is_array($config['wol']['wolentry'])) {
72
	$config['wol']['wolentry'] = array();
73
}
74
$a_wol = &$config['wol']['wolentry'];
75

    
76
if($_GET['wakeall'] != "") {
77
	$i = 0;
78
	$savemsg = "";
79
	foreach ($a_wol as $wolent) {
80
		$mac = $wolent['mac'];
81
		$if = $wolent['interface'];
82
		$description = $wolent['descr'];
83
		$ipaddr = get_interface_ip($if);
84
		if (!is_ipaddr($ipaddr)) {
85
			continue;
86
		}
87
		$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
88
		/* Execute wol command and check return code. */
89
		if (!mwexec("/usr/local/bin/wol -i {$bcip} {$mac}")) {
90
			$savemsg .= sprintf(gettext('Sent magic packet to %1$s (%2$s)%3$s'), $mac, $description, ".<br />");
91
		} else {
92
			$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 />");
93
		}
94
	}
95
}
96

    
97
if ($_POST || $_GET['mac']) {
98
	unset($input_errors);
99

    
100
	if ($_GET['mac']) {
101
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
102
		$_GET['mac'] = strtolower(str_replace("-", ":", $_GET['mac']));
103
		$mac = $_GET['mac'];
104
		$if = $_GET['if'];
105
	} else {
106
		/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
107
		$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
108
		$mac = $_POST['mac'];
109
		$if = $_POST['interface'];
110
	}
111

    
112
	/* input validation */
113
	if (!$mac || !is_macaddr($mac)) {
114
		$input_errors[] = gettext("A valid MAC address must be specified.");
115
	}
116
	if (!$if) {
117
		$input_errors[] = gettext("A valid interface must be specified.");
118
	}
119

    
120
	if (!$input_errors) {
121
		/* determine broadcast address */
122
		$ipaddr = get_interface_ip($if);
123
		if (!is_ipaddr($ipaddr)) {
124
			$input_errors[] = gettext("A valid ip could not be found!");
125
		} else {
126
			$bcip = gen_subnet_max($ipaddr, get_interface_subnet($if));
127
			/* Execute wol command and check return code. */
128
			if (!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($mac))) {
129
				$savemsg .= sprintf(gettext("Sent magic packet to %s."), $mac);
130
			} else {
131
				$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 />");
132
			}
133
		}
134
	}
135
}
136

    
137
if ($_GET['act'] == "del") {
138
	if ($a_wol[$_GET['id']]) {
139
		unset($a_wol[$_GET['id']]);
140
		write_config();
141
		header("Location: services_wol.php");
142
		exit;
143
	}
144
}
145

    
146
$pgtitle = array(gettext("Services"), gettext("Wake on LAN"));
147
include("head.inc");
148

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

    
152
?>
153

    
154
<?php
155

    
156
if ($input_errors)
157
	print_input_errors($input_errors);
158

    
159
if ($savemsg)
160
	print_info_box($savemsg);
161

    
162
$form = new Form('Send');
163

    
164
$section = new Form_Section('Wake on LAN');
165

    
166
$section->addInput(new Form_Select(
167
	'interface',
168
	'Interface',
169
	(link_interface_to_bridge($if) ? null : $if),
170
	get_configured_interface_with_descr()
171
))->setHelp('Choose which interface the host to be woken up is connected to.');
172

    
173
$section->addInput(new Form_Input(
174
	'mac',
175
	'MAC address',
176
	'text',
177
	$mac
178
))->setHelp(gettext('Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx'));
179

    
180
$form->add($section);
181
print $form;
182
?>
183

    
184
<div class="panel panel-default">
185
	<div class="panel-heading">
186
		<h2 class="panel-title">Wake on LAN devices</h2>
187
	</div>
188

    
189
	<div class="panel-body">
190
		<p><?=gettext("Click the MAC address to wake up an individual device.")?></p>
191
		<div class="table-responsive">
192
			<table class="table table-striped table-hover">
193
				<thead>
194
					<tr>
195
						<th><?=gettext("Interface")?></th>
196
						<th><?=gettext("MAC address")?></th>
197
						<th><?=gettext("Description")?></th>
198
						<th></th>
199
					</tr>
200
				</thead>
201
				<tbody>
202
					<?php foreach ($a_wol as $i => $wolent): ?>
203
						<tr>
204
							<td>
205
								<?=convert_friendly_interface_to_friendly_descr($wolent['interface']);?>
206
							</td>
207
							<td>
208
								<a href="?mac=<?=$wolent['mac'];?>&amp;if=<?=$wolent['interface'];?>"><?=strtolower($wolent['mac']);?></a>
209
							</td>
210
							<td>
211
								<?=htmlspecialchars($wolent['descr']);?>
212
							</td>
213
							<td>
214
								<a class="fa fa-pencil"	title="<?=gettext('Edit device')?>"	href="services_wol_edit.php?id=<?=$i?>"></a>
215
								<a class="fa fa-trash"	title="<?=gettext('Delete device')?>" href="services_wol.php?act=del&amp;id=<?=$i?>"></a>
216
							</td>
217
						</tr>
218
					<?php endforeach?>
219
				</tbody>
220
			</table>
221
		</div>
222
	</div>
223
	<div class="panel-footer">
224
		<a class="btn btn-success" href="services_wol_edit.php">
225
			Add
226
		</a>
227

    
228
		<a href="services_wol.php?wakeall=true" role="button" class="btn btn-primary">
229
			<?=gettext("Wake all devices")?>
230
		</a>
231
	</div>
232
</div>
233

    
234
<?php
235

    
236
include("foot.inc");
(155-155/228)