Sindbad~EG File Manager
<?php
namespace App\Service\Lsws;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class LswsCommandRunner
{
/**
* @var string
*/
private $lswsRoot;
/**
* @var string
*/
private $user;
/**
* @var string
*/
private $pass;
/**
* @var Filesystem
*/
private $fs;
public function __construct(string $lswsRoot, string $user, string $pass)
{
$this->lswsRoot = $lswsRoot;
$this->user = $user;
$this->pass = $pass;
$this->fs = new Filesystem();
}
/**
* Restart / reload the litespeed webserver.
*/
public function restart(): bool
{
return $this->sendCommand('restart');
}
/**
* Return the base directory for the $webserverType.
*/
private function getSocketDir(): string
{
return rtrim($this->lswsRoot, '/') . '/admin/tmp/';
}
/**
* Return true if a possible socket found under the $pack seems working and responding.
*/
private function checkSocketIsAlive(string $path): bool
{
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($sock === false) {
echo sprintf('Cant create a Socket. Error: %s', socket_strerror(socket_last_error()));
return false;
}
try {
$r = socket_connect($sock, $path) !== false;
} catch (\Exception $e) {
$r = false;
}
socket_close($sock);
return $r;
}
/**
* Return the right socket, found under the webserver /admin/tmp/ directory. Sometimes multiple sockets are presents
* but only one is really alive/working. Return this socket, or null in case of error.
*/
public function searchSocket(): ?string
{
if (!is_dir($this->getSocketDir())) {
echo sprintf('%s does not exists', $this->getSocketDir());
return null;
}
$finder = (new Finder())
->files()
->in($this->getSocketDir())
->filter(function (\SplFileInfo $file) {
return $file->getType() === 'socket';
})
;
foreach ($finder as $file) {
$path = $file->getRealPath();
if ($this->checkSocketIsAlive($path)) {
return $path;
}
try {
$this->fs->remove($path);
} catch (\Exception $e) {
echo sprintf('Error while trying to delete useless socket %s. Err: %s', $path, $e->getMessage());
}
}
echo sprintf("Can't find any valid socket in %s", $this->getSocketDir());
return null;
}
/**
* Send a command to the litespeed server and return true if the command was executed.
*/
private function sendCommand(string $command): bool
{
$path = $this->searchSocket();
if (is_null($path)) {
return false;
}
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($sock === false) {
echo sprintf('Cant create a Socket. Error: %s', socket_strerror(socket_last_error()));
return false;
}
try {
$r = socket_connect($sock, $path);
} catch (\Exception $e) {
$r = false;
}
if (!$r) {
echo sprintf('Cant connect to the %s socket. Error: %s', $path, socket_strerror(socket_last_error()));
return false;
}
$buf = 'auth:'.$this->user.':'.$this->pass."\n";
$buf .= $command."\n".'end of actions';
$i = socket_write($sock, $buf);
if ($i === false) {
echo sprintf('Cant write to the %s socket. Error: %s', $path, socket_strerror(socket_last_error()));
return false;
}
if ($i !== strlen($buf)) {
echo sprintf('socket_write returned an unexpected value. %s != %s.', strlen($buf), $i);
}
if (socket_shutdown($sock, 1) === false) {
echo 'socket_shutdown returned false';
}
socket_shutdown($sock, 1);
$res = socket_recv($sock, $buffer, 1024, 0);
if (($res > 0) && (strncasecmp($buffer, 'OK', 2) == 0)) {
return true;
} else {
echo sprintf('socket_recv returned an unexpected value. Socket:%s', $path);
return false;
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists