forked from hscheffknecht/cxml
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeader.php
More file actions
253 lines (218 loc) · 7.72 KB
/
Header.php
File metadata and controls
253 lines (218 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace CXml\Models\Messages;
use CXml\Models\Requests\RequestInterface;
class Header implements RequestInterface, MessageInterface
{
private $senderIdentity;
private $senderSharedSecret;
private $fromIdentity;
private $fromDomain;
/**
* @var \CXml\Models\Messages\Credential[]
*/
private $fromCredentials;
/**
* @var \CXml\Models\Messages\Credential[]
*/
private $toCredentials;
/**
* @var \CXml\Models\Messages\Credential[]
*/
private $senderCredentials;
/**
* @return mixed
*/
public function getFromIdentity()
{
return $this->fromIdentity;
}
/**
* @param mixed $fromIdentity
*
* @return Header
*/
public function setFromIdentity($fromIdentity): self
{
$this->fromIdentity = $fromIdentity;
return $this;
}
/**
* @return mixed
*/
public function getFromDomain()
{
return $this->fromDomain;
}
/**
* @param mixed $fromDomain
*
* @return Header
*/
public function setFromDomain($fromDomain): self
{
$this->fromDomain = $fromDomain;
return $this;
}
public function getSenderIdentity()
{
return $this->senderIdentity;
}
public function setSenderIdentity($senderIdentity): self
{
$this->senderIdentity = $senderIdentity;
return $this;
}
public function getSenderSharedSecret()
{
return $this->senderSharedSecret;
}
public function setSenderSharedSecret($senderSharedSecret): self
{
$this->senderSharedSecret = $senderSharedSecret;
return $this;
}
/**
* @return \CXml\Models\Messages\Credential[]
*/
public function getFromCredentials(): array
{
return $this->fromCredentials;
}
/**
* @param \CXml\Models\Messages\Credential[] $fromCredentials
*
* @return Header
*/
public function setFromCredentials(array $fromCredentials): Header
{
$this->fromCredentials = $fromCredentials;
return $this;
}
/**
* @return \CXml\Models\Messages\Credential[]
*/
public function getToCredentials(): array
{
return $this->toCredentials;
}
/**
* @param \CXml\Models\Messages\Credential[] $toCredentials
*
* @return Header
*/
public function setToCredentials(array $toCredentials): Header
{
$this->toCredentials = $toCredentials;
return $this;
}
/**
* @return \CXml\Models\Messages\Credential[]
*/
public function getSenderCredentials(): array
{
return $this->senderCredentials;
}
/**
* @param \CXml\Models\Messages\Credential[] $senderCredentials
*
* @return Header
*/
public function setSenderCredentials(array $senderCredentials): Header
{
$this->senderCredentials = $senderCredentials;
return $this;
}
public function parse(\SimpleXMLElement $headerXml, $validate = false): void
{
$identity = $headerXml->xpath('Sender/Credential/Identity');
$sharedSecret = $headerXml->xpath('Sender/Credential/SharedSecret');
if ($identity) {
$this->senderIdentity = (string)$identity[0];
}
if ($sharedSecret) {
$this->senderSharedSecret = (string)$sharedSecret[0];
}
// cXml Spec as of January 2021
// http://xml.cxml.org/current/cXMLGettingStarted.pdf
// 3.1.7.5 Credential
// Multiple Credentials
// The receiver should reject the document if there are multiple
// credentials in a To, From, or Sender section that use different
// values but use the same domain.
// @TODO Figure out how this is in the specification while the SAP
// catalog tester (https://service.ariba.com/CatalogTester.aw) happily
// sends payload that violates this....
if ($fromCredentials = $headerXml->xpath('From/Credential')) {
$this->fromIdentity = (string)$fromCredentials[0]->xpath('Identity')[0];
$this->fromDomain = (string)$fromCredentials[0]->attributes()->domain;
$domainValidation = [];
foreach ($fromCredentials as $fromCredential) {
$credential = new Credential();
$credential->parse($fromCredential);
if ($validate && isset($this->fromCredentials[$credential->getDomain()])) {
throw new \Exception('Duplicated "From" credential. Only one credential per domain is valid. See 3.1.7.5 Credential http://xml.cxml.org/current/cXMLGettingStarted.pdf');
}
$this->fromCredentials[] = $domainValidation[$credential->getDomain()] = $credential;
}
}
if ($toCredentials = $headerXml->xpath('To/Credential')) {
$domainValidation = [];
foreach ($toCredentials as $toCredential) {
$credential = new Credential();
$credential->parse($toCredential);
if ($validate && isset($this->toCredentials[$credential->getDomain()])) {
throw new \Exception('Duplicated "To" credential. Only one credential per domain is valid. See 3.1.7.5 Credential http://xml.cxml.org/current/cXMLGettingStarted.pdf');
}
$this->toCredentials[] = $domainValidation[$credential->getDomain()] = $credential;
}
}
if ($senderCredentials = $headerXml->xpath('Sender/Credential')) {
$domainValidation = [];
foreach ($senderCredentials as $senderCredential) {
$credential = new Credential();
$credential->parse($senderCredential);
if ($validate && isset($domainValidation[$credential->getDomain()])) {
throw new \Exception('Duplicated "Sender" credential. Only one credential per domain is valid. See 3.1.7.5 Credential http://xml.cxml.org/current/cXMLGettingStarted.pdf');
}
$this->senderCredentials[] = $domainValidation[$credential->getDomain()] = $credential;
}
}
}
public function render(\SimpleXMLElement $parentNode): void
{
$headerNode = $parentNode->addChild('Header');
if ($this->fromCredentials) {
$fromNode = $headerNode->addChild('From');
foreach ($this->fromCredentials as $fromCredential) {
$fromCredential->render($fromNode);
}
} else {
$this->addNode($headerNode, 'From', htmlspecialchars($this->fromIdentity ?? 'Unknown', ENT_XML1 | ENT_COMPAT, 'UTF-8'));
}
if ($this->toCredentials) {
$toNode = $headerNode->addChild('To');
foreach ($this->toCredentials as $toCredential) {
$toCredential->render($toNode);
}
} else {
$this->addNode($headerNode, 'To', 'Unknown');
}
if ($this->senderCredentials) {
$senderNode = $headerNode->addChild('Sender');
foreach ($this->senderCredentials as $senderCredential) {
$senderCredential->render($senderNode);
}
} else {
$this->addNode($headerNode, 'Sender', $this->getSenderIdentity() ?? 'Unknown')
->addChild('UserAgent', 'Unknown');
}
}
private function addNode(\SimpleXMLElement $parentNode, string $nodeName, string $identity): \SimpleXMLElement
{
$node = $parentNode->addChild($nodeName);
$credentialNode = $node->addChild('Credential');
$credentialNode->addAttribute('domain', '');
$credentialNode->addChild('Identity', htmlspecialchars($identity, ENT_XML1 | ENT_COMPAT, 'UTF-8'));
return $node;
}
}