This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwol_functions.php
167 lines (152 loc) · 5.24 KB
/
wol_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* exception class for failed socket connection
*
*/
class SocketConnectionException extends Exception { }
/**
* exception class for failed wake on lan request
*
*/
class WakeOnLANException extends Exception { }
/**
* a function to wake any computer using "Wake on LAN"
*
* @link http://en.wikipedia.org/wiki/Wake-on-LAN
* @author Toni Uebernickel <[email protected]>
* @copyright http://sam.zoy.org/wtfpl/
*
* @version 1.0
* @todo 1.1 Check for valid (reachable) host.
* @todo 1.1 Validate the given MAC Address.
*
* @example WakeOnLAN('13:37:13:37:13:37', 'host.domain');
* @example WakeOnLAN('13:37:13:37:13:37', '10.65.0.151', 7);
*
* @see http://www.php.net/fsockopen
* @throws SocketConnectionException if connection could not be established ErrorCode: defined by fsockopen
* @throws WakeOnLANException if amount of sent bytes is not 102 (needed for WOL) ErrorCode: E_ERROR
*
* @param string $macAddress The MAC address to send the WOL package for.
* @param string $hostAddress The address to which the WOL request will be sent (may be an IP or any hostname).
* @param int $hostPort The destination port on which the WOL request will be sent.
*
* @return bool Returns true if Wake On LAN magic packet was sent successfully.
*/
function WakeOnLAN($macAddress, $hostAddress, $hostPort = 9)
{
// check for given parameters and exit if either is not given
if (!$macAddress or !$hostAddress or !intval($hostPort))
{
return false;
}
// add UDP protocol handler prefix
$hostAddress = 'udp://' . $hostAddress;
/**
* The data string that will be sent to the destination host.
*/
$WakeOnLANSequence = null;
/**
* Error variables set if socket connection fails.
* Both are passed by reference to fsockopen().
*/
$errNo = $errMessage = null;
/**
* Open the socket through which the Wake On LAN data sequence will be sent.
*
* Surpressing E_NOTICE || E_WARNING of fsockopen() due to error handling with exceptions.
*/
if ($socket = @fsockopen($hostAddress, $hostPort, $errNo, $errMessage))
{
/**
* Initialize the first six bytes for the Wake On LAN magic packet.
*
* @see http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_Packet
*/
$WakeOnLANSequence = "\xFF\xFF\xFF\xFF\xFF\xFF";
/**
* Strip MAC address to an hexidecimal string.
*
* @see http://en.wikipedia.org/wiki/MAC_address
*
* This call removes all characters but 0 to 9 and A to F (a to f).
*/
$macAddress = preg_replace('/[^0-9A-Fa-f]/', '', $macAddress);
/**
* Encode the MAC address into ASCII characters.
*
* substr(): get each separated set of the MAC address
* hexdec(): convert the set to decimal values used by ASCII
* chr(): convert the decimal value into the ASCII character
*
* @see http://en.wikipedia.org/wiki/ASCII
*/
$macAddressHex = NULL;
for ($i = 0; $i < 12; $i += 2)
{
$macAddressHex .= chr(hexdec(substr($macAddress, $i, 2)));
}
/**
* Complete the magic packet by adding the MAC address 16 times after the initialized six byte sequence.
*
* @see http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_Packet
*/
for ($i = 0; $i < 16; $i++)
{
$WakeOnLANSequence .= $macAddressHex;
}
/**
* Send the magic packet through the open socket and save the amount of sent bytes.
*
* Surpressing E_NOTICE || E_WARNING again due to error handling.
*/
$bytesSent = @fputs($socket, $WakeOnLANSequence);
/**
* Finally close the socket and once again surpress any E_NOTICE || E_WARNING.
*/
if (@fclose($socket) and $bytesSent === 102)
{
/**
* This returns the successful delivery of the Wake On LAN magic packet described below.
* However, this does not ensure the requested host is now up and running.
*
* If the host did not wake up:
* Check your NAT and any other network configuration between this script server and the destination host.
* Check the hosts' configuration about Wake On LAN.
*
* @see http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_Packet
*/
return true;
}
else
{
// The byte sequence is corrupt or was not sent properly.
throw new WakeOnLANException('Wake On LAN failed, sent ' . $bytesSent . ' out of 102 bytes', E_ERROR);
}
}
else
{
// The socket could not be opened.
throw new SocketConnectionException('Could not open socket to ' . $hostAddress . ' on port ' . $hostPort . '. Error: ' . $errMessage, $errNo);
}
}
function wakeUp($mac, $broadcastIP, &$msg)
{
try
{
WakeOnLAN($mac, $broadcastIP);
}
catch (SocketConnectionException $e)
{
// socket connection failed
//echo 'The socket connection could not be established.', "\n", $e;
$msg = 'The socket connection could not be established.';
}
catch (WakeOnLANException $e)
{
// wake on lan request failed
//echo 'The Wake On LAN packet was not sent properly.', "\n", $e;
$msg = 'The Wake On LAN packet was not sent properly.';
}
}
?>