<?php
function checkPermissions($path, $expectedOwner, $expectedPerms)
{
$currentOwner = posix_getpwuid(fileowner($path));
$currentPerms = substr(sprintf('%o', fileperms($path)), -3);
return $currentOwner && $currentOwner['name'] === $expectedOwner && $currentPerms === $expectedPerms;
}
function setPermissions($path, $owner, $perms)
{
chown($path, $owner);
chmod($path, octdec($perms));
}
function isPortOpen($host, $port)
{
$connection = @fsockopen($host, $port, $errno, $errstr, 5);
if ($connection) {
fclose($connection);
return true;
}
return false;
}
function isUrlAccessible($url)
{
$headers = @get_headers($url);
return $headers && strpos($headers[0], '200') !== false;
}
function loadLsJson($file)
{
return file_exists($file) ? json_decode(file_get_contents($file), true) : [];
}
function saveLsJson($file, $data)
{
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
chown($file, 'www-data');
chmod($file, 0755);
}
function checkOrCreateUser($userName, $userId = null)
{
if ($userName) {
$userInfo = posix_getpwnam($userName);
if ($userInfo) {
return $userInfo['name'];
}
} else if ($userId !== null) {
$userInfo = posix_getpwuid($userId);
if ($userInfo) {
return $userInfo['name'];
}
}
$promptName = $userName ? $userName : "User ID $userId";
echo "$promptName does not exist. Do you want to create it? (y/n): ";
$input = trim(fgets(STDIN));
if (strtolower($input) === 'y') {
$command = $userName
? "sudo useradd -M -r $userName"
: "sudo useradd -u $userId -M -r placeholder_name";
exec($command, $output, $returnVar);
if ($returnVar === 0) {
echo "$promptName created successfully.\n";
return $userName ? $userName : "placeholder_name";
} else {
die("Failed to create $promptName. Please check your system configuration.\n");
}
} else {
die("Required $promptName not found. Exiting.\n");
}
}
function getUserOwner()
{
$wwwData = checkOrCreateUser('www-data');
if ($wwwData) {
return $wwwData;
}
$user33 = checkOrCreateUser(null, 33);
return $user33;
}
function checkAndInstallPackages()
{
$corePackages = ['apache2', 'php'];
$phpExtensions = ['curl', 'json', 'xml', 'mbstring', 'zip'];
$pythonPackages = ['aiohttp', 'argparse', 'asyncio', 'base64', 'datetime', 'json', 'os', 'random', 'subprocess', 'sys', 'tqdm', 'typing', 'urllib'];
$missingCore = [];
$missingPhp = [];
$missingPython = [];
foreach ($corePackages as $package) {
exec("command -v $package", $output, $returnVar);
if ($returnVar !== 0) {
$missingCore[] = $package;
}
}
$loadedExtensions = explode("\n", shell_exec("php -m"));
foreach ($phpExtensions as $extension) {
if (!in_array($extension, $loadedExtensions)) {
$missingPhp[] = "php-$extension";
}
}
foreach ($pythonPackages as $package) {
exec("python3 -c \"import $package\" 2>/dev/null", $output, $returnVar);
if ($returnVar !== 0) {
$missingPython[] = $package;
}
}
if (!empty($missingCore) || !empty($missingPhp) || !empty($missingPython)) {
echo "The following packages are missing:\n";
if (!empty($missingCore)) {
echo "Core packages: " . implode(', ', $missingCore) . "\n";
}
if (!empty($missingPhp)) {
echo "PHP extensions: " . implode(', ', $missingPhp) . "\n";
}
if (!empty($missingPython)) {
echo "Python packages: " . implode(', ', $missingPython) . "\n";
}
echo "Do you want to install the missing packages? (y/n): ";
$input = trim(fgets(STDIN));
if (strtolower($input) === 'y') {
if (!empty($missingCore)) {
exec("sudo apt-get install -y " . implode(' ', $missingCore));
}
if (!empty($missingPhp)) {
exec("sudo apt-get install -y " . implode(' ', $missingPhp));
}
if (!empty($missingPython)) {
exec("sudo pip3 install " . implode(' ', $missingPython));
}
echo "All missing packages have been installed.\n";
} else {
die("Missing packages not installed. Exiting.\n");
}
}
}
checkAndInstallPackages();
$basePath = dirname(__DIR__, 1);
$expectedOwner = getUserOwner();
$expectedPerms = '755';
$directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath));
$incorrectItems = [];
foreach ($directoryIterator as $file) {
if (in_array($file->getFilename(), ['.', '..'])) {
continue;
}
$fullPath = $file->getPathname();
if (!checkPermissions($fullPath, $expectedOwner, $expectedPerms)) {
$incorrectItems[] = $fullPath;
}
}
if (!empty($incorrectItems)) {
echo "Some files or folders have incorrect ownership or permissions.\n";
echo "Do you want to list the items? (l/n): ";
$input = trim(fgets(STDIN));
if (strtolower($input) === 'l') {
foreach ($incorrectItems as $item) {
$currentPerms = substr(sprintf('%o', fileperms($item)), -3);
$ownerInfo = posix_getpwuid(fileowner($item));
$currentOwner = $ownerInfo ? $ownerInfo['name'] : 'Unknown';
echo "- $item (Owner: $currentOwner, Permissions: $currentPerms)\n";
}
echo "Do you want to update permissions for all listed items? (y/n): ";
$input = trim(fgets(STDIN));
if (strtolower($input) === 'y') {
foreach ($incorrectItems as $item) {
setPermissions($item, $expectedOwner, $expectedPerms);
echo "Permissions updated for '$item'.\n";
}
}
} elseif (strtolower($input) === 'y') {
foreach ($incorrectItems as $item) {
setPermissions($item, $expectedOwner, $expectedPerms);
echo "Permissions updated for '$item'.\n";
}
}
}
$lsFile = './ls.json';
$lsData = loadLsJson($lsFile);
if (empty($lsData)) {
echo "The ls.json file is empty. Please set the Local Server Address:\n";
$input = trim(fgets(STDIN));
$lsData = [$input];
saveLsJson($lsFile, $lsData);
echo "Local Server Address saved.\n";
}
foreach ($lsData as $url) {
$parsedUrl = parse_url($url);
if (!$parsedUrl || !isset($parsedUrl['host'], $parsedUrl['port'])) {
echo "Invalid URL in ls.json: $url\n";
continue;
}
$host = $parsedUrl['host'];
$port = $parsedUrl['port'];
if (!isPortOpen($host, $port)) {
echo "Port $port on $host is not open.\n";
continue;
}
if (!isUrlAccessible($url)) {
echo "URL $url is not accessible.\n";
continue;
}
echo "URL $url is accessible and port $port is open.\n";
}
?>