diff --git a/README.md b/README.md index 4054d7c..e09ff0d 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,10 @@ Sends a WOL packet to the specified host on website request and redirects to the ## `config.json` definition -| Key | Description | -|------------|----------------------------------------------------------------------------------| -| `host.mac` | The MAC address of the adapter that you want to wake (get it with `ip addr`) | -| `host.ip` | The IP address of the device you want to wake (only used for routing) | -| `timeout` | The timeout between refreshes in the browser (`0` disables automatic refreshing) | -| `details` | Show details to the client (e.g. try count, destination url, etc.) | +| Key | Description | +|------------|---------------------------------------------------------------------------------------------| +| `host.mac` | The MAC address of the adapter that you want to wake (get it with `ip addr`) | +| `host.ip` | The IP address of the device you want to wake (only used for ICMP ping) | +| `timeout` | The timeout between refreshes in the browser (`0` disables automatic refreshing) | +| `details` | Show details to the client (e.g. try count, destination url, etc.) | +| `log` | When `true` write log messages via the `error_log` function for debugging output (optional) | diff --git a/config.sample.json b/config.sample.json index cd53924..3dea0b1 100644 --- a/config.sample.json +++ b/config.sample.json @@ -4,5 +4,6 @@ "ip": "192.168.0.2" }, "timeout": 5, - "details": false + "details": false, + "log": false } \ No newline at end of file diff --git a/lib.php b/lib.php index 3edf984..68dbd05 100644 --- a/lib.php +++ b/lib.php @@ -7,13 +7,46 @@ const SESSION_PARAM_TRY_COUNT = "count"; if (!file_exists(CONFIG_PATH)) { - die("Configuration file doesn't exist. Please create JSON file at " . CONFIG_PATH . " and try again!"); + die("Configuration file doesn't exist. Please create JSON file at \"" . CONFIG_PATH . "\" and try again."); } $config = json_decode(file_get_contents(CONFIG_PATH)); +$log_enabled = $config->{"log"} ?? false; + +/// logging section + +function notice(string $message, string $scope = null): void { + global $log_enabled; + if (!$log_enabled) return; + $scope = isset($scope) ? " [" . $scope . "]" : ""; + error_log("notice" . $scope . ": " . $message . "\n"); +} + +function warn(string $message, string $scope = null): void { + global $log_enabled; + if (!$log_enabled) return; + $scope = isset($scope) ? " [" . $scope . "]" : ""; + error_log("warn" . $scope . ": " . $message . "\n"); +} + +function error(string $message, string $scope = null): void { + global $log_enabled; + if (!$log_enabled) return; + $scope = isset($scope) ? " [" . $scope . "]" : ""; + error_log("error" . $scope . ": " . $message . "\n"); +} /// command section +/** + * Same as {@link exec()} but additionally logs everything to the error log. + */ +function execute_command(string $command, array &$output, int &$result_code, string $scope = null) { + $result = exec($command, $output, $result_code); + notice("executed: " . $command . ", got: " . $output[0] . ", rc: " . $result_code, $scope); + return $result; +} + /** * Checks if a given command (UNIX binary) is in the PATH environment. * @@ -23,8 +56,7 @@ function command_exists(string $command): bool { $output = []; $result_code = 0; - $cmd = "command -v " . $command; - exec($cmd, $output, $result_code); + execute_command("command -v " . escapeshellarg($command), $output, $result_code, "command_exists"); return $result_code == 0; } @@ -38,13 +70,11 @@ function get_command_path(string $command): string { $output = []; $result_code = 0; - $cmd = "command -v " . $command; - exec($cmd, $output, $result_code); + execute_command("command -v " . escapeshellarg($command), $output, $result_code, "get_command_path"); if ($result_code > 0) { die("Requested command " . $command . " doesn't exist. Please install the required package and try again."); } - return $output[0]; } @@ -65,8 +95,7 @@ function is_up(): bool $output = []; $result_code = 0; $cmd = get_command_path("ping") . " -q -c 1 -w 1 " . $config->{"host"}->{"ip"}; - exec($cmd, $output, $result_code); - + execute_command($cmd, $output, $result_code, "is_up"); return $result_code == 0; } @@ -79,23 +108,19 @@ function wake(): bool { global $config; - // send one WOL packet to the host + // send one WoL packet to the host if (command_exists("wol")) { - $wol_command = sprintf(get_command_path("wol") . " -i \"%s\" \"%s\"", - $config->{"host"}->{"ip"}, - $config->{"host"}->{"mac"} - ); + notice("Found \"wol\" as WoL binary", "wake"); + $wol_command = get_command_path("wol") . " " . escapeshellarg($config->{"host"}->{"mac"}); } else if (command_exists("wakeonlan")) { - $wol_command = sprintf(get_command_path("wakeonlan") . " -i \"%s\" \"%s\"", - $config->{"host"}->{"ip"}, - $config->{"host"}->{"mac"} - ); + notice("Found \"wakeonlan\" as WoL binary", "wake"); + $wol_command = get_command_path("wakeonlan") . " " . escapeshellarg($config->{"host"}->{"mac"}); } else { - die("No wakeonlan binaries found. Please install \"wakeonlan\" or \"wol\" package."); + die("No WoL binaries found. Please install \"wakeonlan\" or \"wol\" package."); } $output = []; $result_code = 0; - exec($wol_command, $output, $result_code); + execute_command($wol_command, $output, $result_code, "wake"); return $result_code == 0; }