<?php
// Your pin here
$pin = '1234';
// Your network broadcast here
$broadcast = '10.255.255.255';
// Your computers here
$computers = array(
array('00:19:DB:B5:3B:53', 'PC-Test-11')
, array('00:1B:77:72:BA:AD', 'PC-Test-43')
, array('00:0B:7D:0F:93:75', 'PC-Test-54')
, array('E0:B9:A5:89:83:49', 'PC-Test-65')
);
function param($name) {
if (!isset($_GET[$name]))
return FALSE;
else
return $_GET[$name];
}
function new_int_val($str) {
if(preg_match("/^\\d+$/", $str)) {
return intval($str);
} else {
return FALSE;
}
}
// http://stackoverflow.com/questions/6055293/wake-on-lan-scipt-that-works
function wol($broadcast, $mac)
{
$mac_array = split(':', $mac);
$hwaddr = '';
foreach($mac_array AS $octet) { $hwaddr .= chr(hexdec($octet)); }
// Create Magic Packet
$packet = '';
for ($i = 1; $i <= 6; $i++) { $packet .= chr(255); }
for ($i = 1; $i <= 16; $i++) { $packet .= $hwaddr; }
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die('failed creating socket');
socket_set_option($sock, 1, 6, true) or die('failed setting option');
socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7) or die('failed sending packet');
socket_close($sock);
}
$pAction = param('action');
$pIndex = param('index');
$pPin = param('pin');
if($pAction !== FALSE && $pIndex !== FALSE && $pPin !== FALSE) {
$pIndex = new_int_val($pIndex);
if ($pIndex === FALSE) die('invalid index');
if ($pAction != 'wol') die('invalid action');
if ($pPin != $pin) die('invalid pin');
if ($pIndex >= count($computers)) die('invalid computer index');
$mac = $computers[$pIndex][0];
$label = $computers[$pIndex][1];
wol($broadcast, $mac);
die("magic packet sent to {$label} ({$mac})");
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WOL-PHP</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<style type="text/css">
body { font-family: sans-serif; }
.pin input[type=text] { font-size: 1.5em; text-align: center; width: 160px; }
#actions { width: 620px; }
#actions .item { white-space: pre; background-color: #CCC; width: 168px; padding: 5px 9px; border-radius: 0.65em; margin: 10px; cursor: pointer; float: left; }
#actions .item:hover { background-color: #999; }
#actions .item.in-progress { text-decoration: blink; background-color: #EEE; }
#log { clear: both; }
</style>
<script type="text/javascript">
data = [
<?php for($i = 0; $i < count($computers); $i++): ?>
<?php echo ($i == 0 ? ' ' : ','); ?> { index: <?php echo $i; ?>, label: "<?php echo addslashes($computers[$i][1]); ?>" }
<?php endfor; ?>
];
var log = function(text) {
var tsp = (new Date()).toISOString();
$('<div class="log-item" />').text(tsp + ' : ' + text).appendTo('#log');
};
$(function() {
for(var i =0 ; i < data.length; i++) {
$('<div />')
.addClass('item')
.data(data[i])
.text(data[i].label)
.appendTo('#actions')
.click(function(eventObject) {
if($('.in-progress').size() > 0) {
log('a request is in progress...');
return;
}
var $elm = $(this);
var data = $elm.data();
var pin = $('#pin').val();
$.extend(data, {pin: pin, 'action': 'wol'});
$elm.addClass('in-progress');
$.get('<?php echo $_SERVER["SCRIPT_NAME"]; ?>', data, function(data, textStatus, jqXHR) {
log(data);
$('.in-progress').removeClass('in-progress');
});
log('Request sent to start ' + data.label + '...');
});
}
});
</script>
</head>
<body>
<div class="pin">Pin : <input id="pin" name="pin" type="text" /></div>
<div id="actions"></div>
<div id="log"></div>
</body>
</html>