Project

General

Profile

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

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

    
71
require("guiconfig.inc");
72
require_once('classes/Form.class.php');
73

    
74
if (!is_array($config['wol']['wolentry'])) {
75
	$config['wol']['wolentry'] = array();
76
}
77
$a_wol = &$config['wol']['wolentry'];
78

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

    
100
if ($_POST || $_GET['mac']) {
101
	unset($input_errors);
102

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

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

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

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

    
149
$pgtitle = array(gettext("Services"), gettext("Wake on LAN"));
150
include("head.inc");
151

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

    
155
?>
156

    
157
<?php
158

    
159
if ($input_errors)
160
	print_input_errors($input_errors);
161

    
162
if ($savemsg)
163
	print_info_box($savemsg);
164

    
165
$form = new Form('Send');
166

    
167
$section = new Form_Section('Wake on LAN');
168

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

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

    
183
$form->add($section);
184
print $form;
185
?>
186

    
187
<div class="panel panel-default">
188
	<div class="panel-heading">
189
		<h2 class="panel-title">Wake on LAN devices</h2>
190
	</div>
191

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

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

    
237
<?php
238

    
239
include("foot.inc");
(157-157/234)