1
+ <?php declare (strict_types=1 );
2
+
3
+ namespace FilipSedivy \EET \Utils \Debugger ;
4
+
5
+ use DOMDocument ;
6
+
7
+ class LastRequest
8
+ {
9
+ private const SENSITIVE_TAGS = [
10
+ 'DigestValue ' ,
11
+ 'SignatureValue ' ,
12
+ 'BinarySecurityToken '
13
+ ];
14
+
15
+ /** @var bool */
16
+ public $ highlight = true ;
17
+
18
+ /** @var bool */
19
+ public $ format = true ;
20
+
21
+ /** @var bool */
22
+ public $ hiddenSensitiveData = true ;
23
+
24
+ /** @var string */
25
+ public $ sensitiveValue = '**** CENSURE **** ' ;
26
+
27
+ /** @var string */
28
+ private $ lastRequest ;
29
+
30
+ public function __construct (string $ lastRequest )
31
+ {
32
+ $ this ->lastRequest = $ lastRequest ;
33
+ }
34
+
35
+ public function out (): void
36
+ {
37
+ if ($ this ->hiddenSensitiveData ) {
38
+ $ this ->doHiddenSensitiveData ();
39
+ }
40
+
41
+ if ($ this ->format ) {
42
+ $ this ->doFormat ();
43
+ }
44
+
45
+ if ($ this ->highlight ) {
46
+ $ this ->doHighlight ();
47
+ }
48
+
49
+ printf ('<pre>%s</pre> ' , $ this ->lastRequest );
50
+ }
51
+
52
+ private function doFormat (): void
53
+ {
54
+ $ dom = new DOMDocument ;
55
+
56
+ $ dom ->preserveWhiteSpace = false ;
57
+ $ dom ->formatOutput = true ;
58
+
59
+ $ dom ->loadXML ($ this ->lastRequest );
60
+
61
+ $ this ->lastRequest = $ dom ->saveXML ();
62
+ }
63
+
64
+ private function doHighlight (): void
65
+ {
66
+ $ s = htmlspecialchars ($ this ->lastRequest );
67
+
68
+ $ s = preg_replace ("#<([/]*?)(.*)([\s]*?)>#sU " ,
69
+ "<span style= \"color: #0000FF \">< \\1 \\2 \\3></span> " , $ s );
70
+
71
+ $ s = preg_replace ("#<([\?])(.*)([\?])>#sU " ,
72
+ "<span style= \"color: #800000 \">< \\1 \\2 \\3></span> " , $ s );
73
+
74
+ $ s = preg_replace ("#<([^\s\?/=])(.*)([\[\s/]|>)#iU " ,
75
+ "<<span style= \"color: #808000 \"> \\1 \\2</span> \\3 " , $ s );
76
+
77
+ $ s = preg_replace ("#<([/])([^\s]*?)([\s\]]*?)>#iU " ,
78
+ "< \\1<span style= \"color: #808000 \"> \\2</span> \\3> " , $ s );
79
+
80
+ $ s = preg_replace ("#([^\s]*?)\=("|')(.*)("|')#isU " ,
81
+ "<span style= \"color: #800080 \"> \\1</span>=<span style= \"color: #FF00FF \"> \\2 \\3 \\4</span> " , $ s );
82
+
83
+ $ s = preg_replace ("#<(.*)(\[)(.*)(\])>#isU " ,
84
+ "< \\1<span style= \"color: #800080 \"> \\2 \\3 \\4</span>> " , $ s );
85
+
86
+ $ this ->lastRequest = nl2br ($ s );
87
+ }
88
+
89
+ private function doHiddenSensitiveData (): void
90
+ {
91
+ $ dom = new DOMDocument ;
92
+ $ dom ->loadXML ($ this ->lastRequest );
93
+
94
+ foreach (self ::SENSITIVE_TAGS as $ tag ) {
95
+ $ nodeList = $ dom ->getElementsByTagName ($ tag );
96
+
97
+ for ($ i = 0 ; $ i < $ nodeList ->length ; $ i ++) {
98
+ $ node = $ nodeList ->item ($ i );
99
+ $ node ->nodeValue = $ this ->sensitiveValue ;
100
+ }
101
+ }
102
+
103
+ $ this ->lastRequest = $ dom ->saveXML ();
104
+ }
105
+ }
0 commit comments