<?php
/* ============================== Configuration settings ====================================== */
/* List of PCs that may be woken */
$config_network_data_array[] = array("name" => "KOMPUTER_1", "MAC" => "00:11:22:33:44:55", "IP" => "192.168.1.10", "WakeIP" =>
"192.168.1.255");
$config_network_data_array[] = array("name" => "KOMPUTER_2", "MAC" => "00:11:22:33:44:56", "IP" => "192.168.1.11", "WakeIP" =>
"192.168.1.255");
$config_network_data_array[] = array("name" => "KOMPUTER_3", "MAC" => "00:11:22:33:44:57", "IP" => "192.168.1.12", "WakeIP" =>
"192.168.1.255");
/* Optional list of URLs associated with the devices listed above */
$config_network_links_array['PC #1'] = array("webmin" => "https://192.168.1.14:10000");
$config_network_links_array['PC #2'] = array("website" => "http://192.168.1.15", "forum" => "http://192.168.1.15/forum");
// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "9";
$my_password = '123';
// If you don't want to password-protect:
$my_password = '';
$html_title = '<H2>My Wake On LAN devices</H2>';
$config_table_columns = array('name', 'IP', 'MAC', 'links');
# The following function is copied (with some edits, to suppress output and return TRUE or an error message) from:
# http://www.hackernotcracker.com/2006-04/wol-wake-on-lan-tutorial-with-bonus-php-script.html
# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
# Modified by Allan Barizo http://www.hackernotcracker.com
flush();
function WakeOnLan($addr, $mac,$socket_number) {
$separator = ':';
if (strstr ( $mac, '-' ) ) {
$separator = '-';
}
$addr_byte = explode($separator, $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
// echo "Error creating socket!\n";
// echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
return "Error creating socket!\nError code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_erro
($s));
// return FALSE;
}
else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret <0) {
// echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
// return FALSE;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
// echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
}
else {
// echo "Magic packet failed!";
return "Magic packet failed!";
// return FALSE;
}
}
}
/* ============================== some predefined texts ====================================== */
$display_sent = 'Magic Packet sent successfully!';
$button_text = 'Wake!';
$button_text2 = 'Wake all selected';
$password_element = "<P>password: <input type=\"text\" name=\"password\" /><input type=\"submit\" name=\"submit\" value = \"procee
\" />";
$table_html = "<TABLE border=\"2\">\n";
$logout_html = '';
/* ========================= Test for password protection ==================================== */
$wake_MAC_array = array();
if (!isset ($_POST['logout'])) {
$input_password = $_POST['password'];
}
if (($input_password === $my_password) or ($my_password === '')) {
$logged_in = TRUE;
$hidden_login = "<input type=\"hidden\" name=\"password\" value=\"$my_password\"/>";
if ($my_password !== '') {
$logout_html = "\n<P><input type=\"submit\" name=\"logout\" value=\"Log Out\"/>\n";
}
if ( (isset ($_POST['tickbox'])) and (is_array($_POST['tickbox']) ) ) {
$checkbox_array = $_POST['tickbox'];
foreach ($checkbox_array as $mac_address => $tickbox_setting) {
$wake_MAC_array[$mac_address] = $tickbox_setting;
}
}
} else {
$logged_in = FALSE;
$hidden_login = '';
$table_html = $password_element;
}
/* ================================ LOGGED-IN users only ===================================== */
/* ======================= construct table for listing of devices ============================ */
if ($logged_in == TRUE) {
$table_row = "\n<TR>";
foreach ($config_table_columns as $key => $column_heading) {
$table_row .= '<TD>' . $column_heading . '</TD>';
}
$table_row .= '<TD>Wake Up!</TD>';
$table_row .= '<TD>status</TD>';
$table_html .= $table_row . "</TR>\n";
foreach ($config_network_data_array as $device_key => $device_values) {
$table_row = "\n<TR>";
$mac = $device_values['MAC'];
$device_name = $device_values['name'];
$status_cell = '<TD> </TD>';
foreach ($config_table_columns as $key => $column_heading) {
if (isset ( $device_values[$column_heading])) {
$value = $device_values[$column_heading];
if ($column_heading == 'MAC') {
/* special coding for MAC address column; prepare clickable button */
$this_MAC = $value;
$value = "<input type=\"submit\" name=\"wake_MAC\" value = \"$value\" />";
if (( $_POST['wake_MAC'] === $this_MAC ) or (array_key_exists ($this_MAC,
$wake_MAC_array))) {
$status = WakeOnLan ($device_values['WakeIP'], $this_MAC, $socket_number) ;
if ( $status === TRUE ) {
$status = $display_sent;
}
$status_cell = "<TD>$status</TD>";
}
}
} elseif ($column_heading == 'links') {
/* special coding for links column; prepare clickable links from $config_network_links_array */
$value = '';
if (isset ( $config_network_links_array[$device_name])) {
foreach ($config_network_links_array[$device_name] as $link_title => $link_URL) {
if ( $value !== '') {
$value .= '<BR />';
}
$value .= '<A HREF="' . $link_URL . '">' . $link_title . '</A>';
}
}
} else {
$value = '';
}
if ($value === '') {
$value = ' ';
}
$table_row .= '<TD>' . $value . '</TD>';
}
/* now add a checkbox to wake up this device */
$table_row .= '<TD>' . "<input type=\"checkbox\" name=\"tickbox[$this_MAC]\" />" . '</TD>';
/* now add the status message (if applicable) for the attempt to send a packet to this device */
$table_row .= $status_cell;
$table_html .= $table_row . "</TR>\n";
}
$table_html .= "</TABLE>\n";
$table_html .= "<P><input type=\"submit\" name=\"wake all\" value = \"$button_text2\" />\n";
}
/* =========================================================================================== */
/* ======================= Now output the html that we've built ============================== */
echo $html_title;
echo "<FORM name=\"input\" action=\"" .$_SERVER['PHP_SELF'] . "\" method=\"post\">";
echo '<P>';
echo $table_html;
echo $hidden_login;
echo $logout_html;
echo "</FORM>\n";
?>