@@ -12,6 +12,8 @@ class DompurifyCli implements CliInterface, HasSetupInterface
12
12
{
13
13
protected DompurifyCliConfig $ config ;
14
14
15
+ // TODO: better injection (fs and process)
16
+
15
17
/** @param DompurifyCliConfig $config */
16
18
public function configure (ConfigInterface $ config ): static
17
19
{
@@ -30,15 +32,7 @@ public function exec(string $html): string
30
32
{
31
33
$ htmlFile = $ this ->saveHtml ($ html );
32
34
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 ]);
42
36
$ process ->mustRun ();
43
37
44
38
$ output = $ process ->getOutput ();
@@ -57,18 +51,24 @@ public function exec(string $html): string
57
51
return $ clean ;
58
52
}
59
53
60
- private function saveHtml ( string $ value ): string
54
+ private function binPath ( ): string
61
55
{
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 ' ;
65
58
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 ' " );
70
63
}
71
64
65
+ return $ binAbsPath ;
66
+ }
67
+
68
+ private function saveHtml (string $ value ): string
69
+ {
70
+ $ dir = $ this ->tempDir ();
71
+
72
72
$ fileName = mt_rand ().'- ' .str_replace ([' ' , '. ' ], '' , microtime ()).'.xss ' ;
73
73
74
74
$ path = $ dir .DIRECTORY_SEPARATOR .$ fileName ;
@@ -79,4 +79,19 @@ private function saveHtml(string $value): string
79
79
80
80
return $ path ;
81
81
}
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
+ }
82
97
}
0 commit comments