1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fhp\Response; |
4
|
|
|
|
5
|
|
|
use Fhp\Message\AbstractMessage; |
6
|
|
|
use Fhp\Segment\AbstractSegment; |
7
|
|
|
use Fhp\Segment\NameMapping; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Response |
11
|
|
|
* |
12
|
|
|
* @package Fhp\Response |
13
|
|
|
*/ |
14
|
|
|
class Response |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
public $rawResponse; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $response; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $segments = array(); |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $dialogId; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $systemId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Response constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $rawResponse |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct($rawResponse) |
38
|
|
|
{ |
39
|
1 |
|
if ($rawResponse instanceof Initialization) { |
40
|
|
|
$rawResponse = $rawResponse->rawResponse; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
$this->rawResponse = $rawResponse; |
44
|
1 |
|
$this->response = $this->unwrapEncryptedMsg($rawResponse); |
45
|
1 |
|
$this->segments = preg_split("#'(?=[A-Z]{4,}:\d|')#", $rawResponse); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Extracts dialog ID from response. |
50
|
|
|
* |
51
|
|
|
* @return string|null |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
|
|
public function getDialogId() |
55
|
|
|
{ |
56
|
|
|
$segment = $this->findSegment('HNHBK'); |
57
|
|
|
|
58
|
|
|
if (null == $segment) { |
|
|
|
|
59
|
|
|
throw new \Exception('Could not find element HNHBK. Invalid response?'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->getSegmentIndex(4, $segment); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Extracts bank name from response. |
67
|
|
|
* |
68
|
|
|
* @return string|null |
69
|
|
|
*/ |
70
|
|
|
public function getBankName() |
71
|
|
|
{ |
72
|
|
|
$bankName = null; |
73
|
|
|
$segment = $this->findSegment('HIBPA'); |
74
|
|
|
if (null != $segment) { |
|
|
|
|
75
|
|
|
$split = $this->splitSegment($segment); |
76
|
|
|
if (isset($split[3])) { |
77
|
|
|
$bankName = $split[3]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $bankName; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Some kind of HBCI pagination. |
86
|
|
|
* |
87
|
|
|
* @param AbstractMessage $message |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getTouchDowns(AbstractMessage $message) |
92
|
|
|
{ |
93
|
|
|
$touchdown = array(); |
94
|
|
|
$messageSegments = $message->getEncryptedSegments(); |
|
|
|
|
95
|
|
|
/** @var AbstractSegment $msgSeg */ |
96
|
|
|
foreach ($messageSegments as $msgSeg) { |
97
|
|
|
$segment = $this->findSegmentForReference('HIRMS', $msgSeg); |
98
|
|
|
if (null != $segment) { |
|
|
|
|
99
|
|
|
$parts = $this->splitSegment($segment); |
100
|
|
|
// remove header |
101
|
|
|
array_shift($parts); |
102
|
|
|
foreach ($parts as $p) { |
103
|
|
|
$pSplit = $this->splitDeg($p); |
104
|
|
|
if ($pSplit[0] == 3040) { |
105
|
|
|
$td = $pSplit[3]; |
106
|
|
|
$touchdown[$msgSeg->getName()] = $td; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $touchdown; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Extracts supported TAN mechanisms from response. |
117
|
|
|
* |
118
|
|
|
* @return array|bool |
119
|
|
|
*/ |
120
|
|
|
public function getSupportedTanMechanisms() |
121
|
|
|
{ |
122
|
|
|
$segments = $this->findSegments('HIRMS'); |
123
|
|
|
// @todo create method to get reference element from request |
124
|
|
|
foreach ($segments as $segment) { |
|
|
|
|
125
|
|
|
$segment = $this->splitSegment($segment); |
126
|
|
|
array_shift($segment); |
127
|
|
|
foreach ($segment as $seg) { |
128
|
|
|
list($id, $msg) = explode('::', $seg, 2); |
129
|
|
|
if ("3920" == $id) { |
130
|
|
|
if (preg_match_all('/\d{3}/', $msg, $matches)) { |
131
|
|
|
return $matches[0]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getHksalMaxVersion() |
144
|
|
|
{ |
145
|
|
|
return $this->getSegmentMaxVersion('HISALS'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getHkkazMaxVersion() |
152
|
|
|
{ |
153
|
|
|
return $this->getSegmentMaxVersion('HIKAZS'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Checks if request / response was successful. |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isSuccess() |
162
|
|
|
{ |
163
|
|
|
$summary = $this->getMessageSummary(); |
164
|
|
|
|
165
|
|
|
foreach ($summary as $code => $message) { |
166
|
|
|
if ("9" == substr($code, 0, 1)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return array |
176
|
|
|
* @throws \Exception |
177
|
|
|
*/ |
178
|
|
|
public function getMessageSummary() |
179
|
|
|
{ |
180
|
|
|
return $this->getSummaryBySegment('HIRMG'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array |
185
|
|
|
* @throws \Exception |
186
|
|
|
*/ |
187
|
|
|
public function getSegmentSummary() |
188
|
|
|
{ |
189
|
|
|
return $this->getSummaryBySegment('HIRMS'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $name |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
* @throws \Exception |
197
|
|
|
*/ |
198
|
|
|
protected function getSummaryBySegment($name) |
199
|
|
|
{ |
200
|
|
|
if (!in_array($name, array('HIRMS', 'HIRMG'))) { |
201
|
|
|
throw new \Exception('Invalid segment for message summary. Only HIRMS and HIRMG supported'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$result = array(); |
205
|
|
|
$segment = $this->findSegment($name); |
206
|
|
|
$segment = $this->splitSegment($segment); |
207
|
|
|
array_shift($segment); |
208
|
|
|
foreach ($segment as $de) { |
209
|
|
|
$de = $this->splitDeg($de); |
210
|
|
|
$result[$de[0]] = $de[2]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $segmentName |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function getSegmentMaxVersion($segmentName) |
222
|
|
|
{ |
223
|
|
|
$version = "3"; |
224
|
|
|
$segments = $this->findSegments($segmentName); |
225
|
|
|
foreach ($segments as $s) { |
|
|
|
|
226
|
|
|
$parts = $this->splitSegment($s); |
227
|
|
|
$segmentHeader = $this->splitDeg($parts[0]); |
228
|
|
|
$curVersion = $segmentHeader[2]; |
229
|
|
|
if ($curVersion > $version) { |
230
|
|
|
$version = $curVersion; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $version; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string |
239
|
|
|
* @throws \Exception |
240
|
|
|
*/ |
241
|
|
|
public function getSystemId() |
242
|
|
|
{ |
243
|
|
|
$segment = $this->findSegment('HISYN'); |
244
|
|
|
|
245
|
|
|
if (!preg_match('/HISYN:\d+:\d+:\d+\+(.+)/', $segment, $matches)) { |
246
|
|
|
throw new \Exception('Could not determine system id.'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $matches[1]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param bool $translateCodes |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public function humanReadable($translateCodes = false) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
return str_replace( |
260
|
|
|
array("'", '+'), |
261
|
|
|
array(PHP_EOL, PHP_EOL . " "), |
262
|
|
|
$translateCodes |
263
|
|
|
? NameMapping::translateResponse($this->rawResponse) |
264
|
|
|
: $this->rawResponse |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $name |
270
|
|
|
* @param AbstractSegment $reference |
271
|
|
|
* |
272
|
|
|
* @return string|null |
273
|
|
|
*/ |
274
|
|
|
protected function findSegmentForReference($name, AbstractSegment $reference) |
275
|
|
|
{ |
276
|
|
|
$result = null; |
|
|
|
|
277
|
|
|
$segments = $this->findSegments($name); |
278
|
|
|
foreach ($segments as $seg) { |
|
|
|
|
279
|
|
|
$segSplit = $this->splitSegment($seg); |
280
|
|
|
$segSplit = array_shift($segSplit); |
281
|
|
|
$segSplit = $this->splitDeg($segSplit); |
282
|
|
|
if ($segSplit[3] == $reference->getSegmentNumber()) { |
283
|
|
|
return $seg; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return null; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $name |
292
|
|
|
* |
293
|
|
|
* @return array|null|string |
294
|
|
|
*/ |
295
|
|
|
protected function findSegment($name) |
296
|
|
|
{ |
297
|
|
|
return $this->findSegments($name, true); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $name |
302
|
|
|
* @param bool $one |
303
|
|
|
* |
304
|
|
|
* @return array|null|string |
305
|
|
|
*/ |
306
|
|
|
protected function findSegments($name, $one = false) |
307
|
|
|
{ |
308
|
|
|
$found = $one ? null : array(); |
309
|
|
|
|
310
|
|
|
foreach ($this->segments as $segment) { |
311
|
|
|
$split = explode(':', $segment, 2); |
312
|
|
|
|
313
|
|
|
if ($split[0] == $name) { |
314
|
|
|
if ($one) { |
315
|
|
|
return $segment; |
316
|
|
|
} |
317
|
|
|
$found[] = $segment; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $found; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param $segment |
326
|
|
|
* |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
1 |
|
protected function splitSegment($segment) |
330
|
|
|
{ |
331
|
1 |
|
$parts = preg_split('/\+(?<!\?\+)/', $segment); |
332
|
|
|
|
333
|
1 |
|
foreach ($parts as &$part) { |
334
|
1 |
|
$part = str_replace('?+', '+', $part); |
335
|
1 |
|
} |
336
|
|
|
|
337
|
1 |
|
return $parts; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param $deg |
342
|
|
|
* |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
|
|
protected function splitDeg($deg) |
346
|
|
|
{ |
347
|
|
|
return explode(':', $deg); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param int $idx |
352
|
|
|
* @param $segment |
353
|
|
|
* |
354
|
|
|
* @return string|null |
355
|
|
|
*/ |
356
|
|
|
protected function getSegmentIndex($idx, $segment) |
357
|
|
|
{ |
358
|
|
|
$segment = $this->splitSegment($segment); |
359
|
|
|
if (isset($segment[$idx - 1])) { |
360
|
|
|
return $segment[$idx - 1]; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return null; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param string $response |
368
|
|
|
* |
369
|
|
|
* @return string |
370
|
|
|
*/ |
371
|
1 |
|
protected function unwrapEncryptedMsg($response) |
372
|
|
|
{ |
373
|
1 |
|
if (preg_match('/HNVSD:\d+:\d+\+@\d+@(.+)\'\'/', $response, $matches)) { |
374
|
|
|
return $matches[1]; |
375
|
|
|
} |
376
|
|
|
|
377
|
1 |
|
return $response; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|