Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. // Your pin here
  4. $pin = '1234';
  5. // Your network broadcast here
  6. $broadcast = '10.255.255.255';
  7. // Your computers here
  8. $computers = array(
  9.       array('00:19:DB:B5:3B:53', 'PC-Test-11')
  10.     , array('00:1B:77:72:BA:AD', 'PC-Test-43')
  11.     , array('00:0B:7D:0F:93:75', 'PC-Test-54')
  12.     , array('E0:B9:A5:89:83:49', 'PC-Test-65')
  13. );
  14.  
  15. function param($name) {
  16.     if (!isset($_GET[$name]))
  17.         return FALSE;
  18.     else
  19.         return $_GET[$name];
  20. }
  21.  
  22. function new_int_val($str) {
  23.     if(preg_match("/^\\d+$/", $str)) {
  24.         return intval($str);
  25.     } else {
  26.         return FALSE;
  27.     }
  28. }
  29.  
  30. // http://stackoverflow.com/questions/6055293/wake-on-lan-scipt-that-works
  31. function wol($broadcast, $mac)
  32. {
  33.     $mac_array = split(':', $mac);
  34.     $hwaddr = '';
  35.     foreach($mac_array AS $octet) { $hwaddr .= chr(hexdec($octet)); }
  36.  
  37.     // Create Magic Packet
  38.     $packet = '';
  39.     for ($i = 1; $i <= 6; $i++) { $packet .= chr(255); }
  40.     for ($i = 1; $i <= 16; $i++) { $packet .= $hwaddr; }
  41.  
  42.     $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die('failed creating socket');
  43.     socket_set_option($sock, 1, 6, true) or die('failed setting option');
  44.     socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7) or die('failed sending packet');
  45.     socket_close($sock);
  46. }
  47.  
  48. $pAction = param('action');
  49. $pIndex = param('index');
  50. $pPin = param('pin');
  51.  
  52. if($pAction !== FALSE && $pIndex !== FALSE && $pPin !== FALSE) {
  53.     $pIndex = new_int_val($pIndex);
  54.     if ($pIndex === FALSE) die('invalid index');
  55.     if ($pAction != 'wol') die('invalid action');
  56.     if ($pPin != $pin) die('invalid pin');
  57.     if ($pIndex >= count($computers)) die('invalid computer index');
  58.    
  59.     $mac = $computers[$pIndex][0];
  60.     $label = $computers[$pIndex][1];
  61.    
  62.     wol($broadcast, $mac);
  63.    
  64.     die("magic packet sent to {$label} ({$mac})");
  65. }
  66.  
  67.  
  68. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  69. <html xmlns="http://www.w3.org/1999/xhtml">
  70.   <head>
  71.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  72.     <title>WOL-PHP</title>
  73.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  74. <style type="text/css">
  75. body { font-family: sans-serif; }
  76. .pin input[type=text] { font-size: 1.5em; text-align: center; width: 160px; }
  77. #actions { width: 620px; }
  78. #actions .item { white-space: pre; background-color: #CCC; width: 168px; padding: 5px 9px; border-radius: 0.65em; margin: 10px; cursor: pointer; float: left; }
  79. #actions .item:hover { background-color: #999; }
  80. #actions .item.in-progress { text-decoration: blink; background-color: #EEE; }
  81. #log { clear: both; }
  82. </style>
  83. <script type="text/javascript">
  84. data = [
  85. <?php for($i = 0; $i < count($computers); $i++): ?>
  86. <?php echo ($i == 0 ? ' ' : ','); ?> { index: <?php echo $i; ?>, label: "<?php echo addslashes($computers[$i][1]); ?>" }
  87. <?php endfor; ?>
  88. ];
  89.  
  90. var log = function(text) {
  91.     var tsp = (new Date()).toISOString();
  92.     $('<div class="log-item" />').text(tsp + ' : ' + text).appendTo('#log');
  93. };
  94.  
  95. $(function() {
  96.     for(var i =0 ; i < data.length; i++) {
  97.         $('<div />')
  98.             .addClass('item')
  99.             .data(data[i])
  100.             .text(data[i].label)
  101.             .appendTo('#actions')
  102.             .click(function(eventObject) {
  103.                 if($('.in-progress').size() > 0) {
  104.                     log('a request is in progress...');
  105.                     return;
  106.                 }
  107.                
  108.                 var $elm = $(this);
  109.                 var data = $elm.data();
  110.                 var pin = $('#pin').val();
  111.                
  112.                 $.extend(data, {pin: pin, 'action': 'wol'});
  113.                
  114.                 $elm.addClass('in-progress');
  115.                 $.get('<?php echo $_SERVER["SCRIPT_NAME"]; ?>', data, function(data, textStatus, jqXHR) {
  116.                     log(data);
  117.                     $('.in-progress').removeClass('in-progress');
  118.                 });
  119.                 log('Request sent to start ' + data.label + '...');
  120.             });
  121.     }
  122. });
  123. </script>
  124.     </head>
  125.     <body>
  126.         <div class="pin">Pin : <input id="pin" name="pin" type="text" /></div>
  127.         <div id="actions"></div>
  128.         <div id="log"></div>
  129.     </body>
  130. </html>