Skip to content

Commit 19e9ce9

Browse files
committed
wip
1 parent 5e9d2c7 commit 19e9ce9

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

src/Dompurify/DompurifyCli.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class DompurifyCli implements CliInterface, HasSetupInterface
1212
{
1313
protected DompurifyCliConfig $config;
1414

15+
// TODO: better injection (fs and process)
16+
1517
/** @param DompurifyCliConfig $config */
1618
public function configure(ConfigInterface $config): static
1719
{
@@ -30,15 +32,7 @@ public function exec(string $html): string
3032
{
3133
$htmlFile = $this->saveHtml($html);
3234

33-
$binPath = __DIR__.DIRECTORY_SEPARATOR.'cli.js';
34-
35-
$binAbsPath = realpath($binPath);
36-
37-
if ($binAbsPath === false) {
38-
throw new XsslessException("Cannot locate '$binPath'");
39-
}
40-
41-
$process = new Process([$this->config->getNodePath(), $binAbsPath, $htmlFile]);
35+
$process = new Process([$this->config->getNodePath(), $this->binPath(), $htmlFile]);
4236
$process->mustRun();
4337

4438
$output = $process->getOutput();
@@ -57,18 +51,24 @@ public function exec(string $html): string
5751
return $clean;
5852
}
5953

60-
private function saveHtml(string $value): string
54+
private function binPath(): string
6155
{
62-
// TODO: take path from config
63-
$tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
64-
$dir = $tempDir.DIRECTORY_SEPARATOR.'xssless';
56+
// TODO: allow config to override
57+
$binPath = __DIR__.DIRECTORY_SEPARATOR.'cli.js';
6558

66-
if (! file_exists($dir)) {
67-
if (mkdir($dir, 0777, true) === false) {
68-
throw new XsslessException("Could not create temporary directory '{$dir}'");
69-
}
59+
$binAbsPath = realpath($binPath);
60+
61+
if ($binAbsPath === false) {
62+
throw new XsslessException("Cannot locate '$binPath'");
7063
}
7164

65+
return $binAbsPath;
66+
}
67+
68+
private function saveHtml(string $value): string
69+
{
70+
$dir = $this->tempDir();
71+
7272
$fileName = mt_rand().'-'.str_replace([' ', '.'], '', microtime()).'.xss';
7373

7474
$path = $dir.DIRECTORY_SEPARATOR.$fileName;
@@ -79,4 +79,19 @@ private function saveHtml(string $value): string
7979

8080
return $path;
8181
}
82+
83+
private function tempDir(): string
84+
{
85+
// TODO: take path from config
86+
$tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
87+
$dir = $tempDir.DIRECTORY_SEPARATOR.'xssless';
88+
89+
if (! file_exists($dir)) {
90+
if (mkdir($dir, 0777, true) === false) {
91+
throw new XsslessException("Could not create temporary directory '{$dir}'");
92+
}
93+
}
94+
95+
return $dir;
96+
}
8297
}

src/Dompurify/DompurifyService.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class DompurifyService implements HasSetupInterface, ServiceInterface
1515
{
1616
private DompurifyServiceConfig $config;
1717

18+
// TODO: better injection (fs and process http)
19+
1820
// TODO: private
1921
public Process $serviceProcess;
2022
// ? add static array for all processes
@@ -33,8 +35,6 @@ public function setup(): void
3335
$process->mustRun();
3436
}
3537

36-
// ========================================================================
37-
3838
public function send(string $html): string
3939
{
4040
$url = "http://{$this->config->getHost()}:{$this->config->getPort()}";
@@ -49,8 +49,6 @@ public function send(string $html): string
4949
return $res->getBody();
5050
}
5151

52-
// ========================================================================
53-
5452
public function start(): static
5553
{
5654
$this->serviceProcess = new Process([
@@ -107,6 +105,7 @@ public function throwIfFailedOnTerm(): void
107105
}
108106

109107
// TODO: fix windows check
108+
// https://github.com/medilies/xssless/actions/runs/10288495452/job/28474063301#step:7:26
110109
if ($this->isSigTerm() || $this->isWindows()) {
111110
return;
112111
}
@@ -142,8 +141,8 @@ private function isSigTerm(): bool
142141
return $this->serviceProcess->getTermSignal() === 15;
143142
}
144143

145-
private function isSigHup(): bool
146-
{
147-
return $this->serviceProcess->getTermSignal() === 1;
148-
}
144+
// private function isSigHup(): bool
145+
// {
146+
// return $this->serviceProcess->getTermSignal() === 1;
147+
// }
149148
}

0 commit comments

Comments
 (0)