#!/usr/bin/php
<?php
/* Copyright 2024-2025, Dan Landon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

/* Load the UD library file if it is not already loaded. */
require_once("plugins/unassigned.devices/include/lib.php");

/* This is a routine to collect some diagnostics from UD to help in diagnosing issues. */
$directory		= "/tmp/".UNASSIGNED_PLUGIN."/diagnostics";

/* Add a timestamp to the zip file name. */
$timestamp = date('Ymd-His');
$diag_output    = "/boot/logs/ud_diagnostics-".$timestamp.".zip";

$sourceFiles = [
	"/var/state/".UNASSIGNED_PLUGIN."/ping_status.json",
	"/var/state/".UNASSIGNED_PLUGIN."/run_status.json",
	"/var/state/".UNASSIGNED_PLUGIN."/device_hosts.json",
	"/var/state/".UNASSIGNED_PLUGIN."/unassigned.devices.ini",
	$paths['dev_state'],
	"/etc/hosts",
	"/var/local/emhttp/plugins/diskinfo/diskinfo.json",
	"/var/log/phplog",
	"/var/log/syslog"
];

/* Create the diagnostics directory and be sure it is empty if it already exists. */ 
exec("/usr/bin/mkdir -p ".$directory);
exec("/usr/bin/rm -f ".$directory."/*");

/* This is the routine that determines what disks on the system are not assigned to the array. */
$ud_disks		= get_unassigned_disks();
$output_file	= $directory."/ud_disks.txt";
$output			= print_r($ud_disks, true);
@file_put_contents($output_file, $output);

/* This is the routine that determines what disks on the system are not assigned to the array. */
$all_disks		= get_all_disks_info();
$output_file	= $directory."/all_disks.txt";
$output			= print_r($all_disks, true);
@file_put_contents($output_file, $output);

/* Get the current unassigned.cfg. */
$unassigned_cfg	= $ud_config;

/* Remove security keys. */
unset($unassigned_cfg['Config']['iv']);
unset($unassigned_cfg['Config']['key']);

/* Remove passwords. */
foreach ($unassigned_cfg as &$subArray) {
	if (isset($subArray['pass'])) {
		/* Anonymize the 'pass' value with a placeholder. */
		$subArray['pass'] = '**********';
	}
}
$output_file	= $directory."/unassigned.devices.cfg";
$output			= print_r($unassigned_cfg, true);
@file_put_contents($output_file, $output);

/* Get the current samba_mount.cfg. */
$samba_cfg	= $samba_config;

/* Remove passwords. */
foreach ($samba_cfg as &$subArray) {
	if (isset($subArray['user'])) {
		/* Anonymize the 'user' value with a placeholder. */
		$subArray['user'] = '**********';
	}

	if (isset($subArray['pass'])) {
		/* Anonymize the 'pass' value with a placeholder. */
		$subArray['pass'] = '**********';
	}
	if (isset($subArray['domain'])) {
		/* Anonymize the 'domain' value with a placeholder. */
		$subArray['domain'] = '**********';
	}
}
$output_file	= $directory."/samba_mount.cfg";
$output			= print_r($samba_cfg, true);
@file_put_contents($output_file, $output);

/* Get the current iso_mount.cfg. */
$iso_cfg		= $iso_config;

$output_file	= $directory."/iso_config.cfg";
$output			= print_r($iso_cfg, true);
@file_put_contents($output_file, $output);

/* Get the disk devices listing. */
$disk_by_id		= listFile("/dev/disk/by-id");

$output_file	= $directory."/disk_by-id.txt";
$output			= print_r($disk_by_id, true);
@file_put_contents($output_file, $output);

/* Copy files to the diagnostics directory with .txt extension */
foreach ($sourceFiles as $sourceFile) {
	/* Get the filename from the full path. */
	$filename = basename($sourceFile);

	if (file_exists($sourceFile)) {
		/* Change the file extension to .txt. */
		if (pathinfo($filename, PATHINFO_EXTENSION) === '') {
			/* Add .txt extension if the file doesn't have one. */
			$destinationFile = $directory."/".$filename.'.txt';
		} else {
			/* Change the file extension to .txt. */
			$destinationFile = $directory."/".str_replace(pathinfo($filename, PATHINFO_EXTENSION), 'txt', $filename);
		}

		/* Copy the file to the destination directory with .txt extension. */
		if (! copy($sourceFile, $destinationFile)) {
			echo "Failed to copy ".$sourceFile."\n";
		}
	}
}

/* Create a zip file from the diagnostics in /tmp/unassigned.devices/diagnostics/. */
/* Use a bash command to zip the directory. */
$zipCommand = "/usr/bin/zip -rj ".$diag_output." ".$directory;

/* Execute the zip command. */
exec($zipCommand, $output, $returnCode);

if ($returnCode === 0) {
	/* Output a success message. */
	echo "Diagnostics zip file created successfully!\n";
} else {
	/* Output an error message if the zip command failed. */
	echo "Failed to create zip file!\n";
}
?>
