-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSecure.php
More file actions
669 lines (585 loc) · 24.5 KB
/
Copy pathSecure.php
File metadata and controls
669 lines (585 loc) · 24.5 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
/**
* This file is part of PHPDevsr/PHPSpreadsheet-Secure.
*
* (c) 2024 Denny Septian Panggabean <xamidimura@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace PHPDevsr\Spreadsheet;
use Closure;
use Exception;
use OLE;
use OLE_PPS_File;
use OLE_PPS_Root;
use SimpleXMLElement;
use ValueError;
class Secure
{
/**
* Data Binary
*/
public Closure $data;
/**
* Password
*/
public string $password = '';
/**
* Buffer Offset
*/
public int $_offset = 8;
/**
* Buffer Chunk Size
*/
public int $_chunk_size = 4096;
/**
* Block Keys
*
* @var array{
* dataIntegrity: array{
* hmacKey: list<int>,
* hmacValue: list<int>
* },
* key: list<int>,
* verifierHash: array{
* input: list<int>,
* value: list<int>
* }
* }
*/
public array $_block_keys = [
'dataIntegrity' => [
'hmacKey' => [0x5F, 0xB2, 0xAD, 0x01, 0x0C, 0xB9, 0xE1, 0xF6],
'hmacValue' => [0xA0, 0x67, 0x7F, 0x02, 0xB2, 0x2C, 0x84, 0x33],
],
'key' => [0x14, 0x6E, 0x0B, 0xE7, 0xAB, 0xAC, 0xD0, 0xD6],
'verifierHash' => [
'input' => [0xFE, 0xA7, 0xD2, 0x76, 0x3B, 0x4B, 0x9E, 0x79],
'value' => [0xD7, 0xAA, 0x0F, 0x6D, 0x30, 0x61, 0x34, 0x4E],
],
];
public function __construct(
/**
* Check using binary?
*/
public bool $NOFILE = false,
) {
}
/**
* Files Input
*
* @param string $data Filename
*
* @return $this
*/
public function setFile($data = '')
{
if ($this->NOFILE) {
$this->data = (static function () use ($data) {
for ($i = 0; $i < strlen($data) / 4096; $i++) {
yield (array) unpack('C*', substr($data, $i * 4096, 4096));
}
});
return $this;
}
$this->data = (static function () use ($data) {
$fp = fopen($data, 'rb');
while (! feof($fp)) {
yield (array) unpack('C*', (string) fread($fp, 4096));
}
fclose($fp);
unset($fp);
});
return $this;
}
/**
* Passwords
*
* @param string $password Password
*
* @return $this
*/
public function setPassword(string $password = '')
{
$this->password = $password;
return $this;
}
/**
* Output
*
* @param string|null $filePath Filename / Binary
*
* @return string
*/
public function output(?string $filePath = null)
{
if (! $this->NOFILE && null === $filePath) {
throw new Exception('Output filepath cannot be NULL when NOFILE is false');
}
$packageKey = (array) unpack('C*', random_bytes(32));
$encryptionInfo = [
'package' => [
'cipherAlgorithm' => 'AES', // Cipher algorithm to use. Excel uses AES.
'cipherChaining' => 'ChainingModeCBC', // Cipher chaining mode to use. Excel uses CBC.
'saltValue' => (array) unpack('C*', random_bytes(16)), // Random value to use as encryption salt. Excel uses 16 bytes.
'hashAlgorithm' => 'SHA512', // Hash algorithm to use. Excel uses SHA512.
'hashSize' => 64, // The size of the hash in bytes. SHA512 results in 64-byte hashes
'blockSize' => 16, // The number of bytes used to encrypt one block of data. It MUST be at least 2, no greater than 4096, and a multiple of 2. Excel uses 16
'keyBits' => count($packageKey) * 8,
],
'key' => [ // Info on the encryption of the package key.
'cipherAlgorithm' => 'AES', // Cipher algorithm to use. Excel uses AES.
'cipherChaining' => 'ChainingModeCBC', // Cipher chaining mode to use. Excel uses CBC.
'saltValue' => (array) unpack('C*', random_bytes(16)), // Random value to use as encryption salt. Excel uses 16 bytes.
'hashAlgorithm' => 'SHA512', // Hash algorithm to use. Excel uses SHA512.
'hashSize' => 64, // The size of the hash in bytes. SHA512 results in 64-byte hashes
'blockSize' => 16, // The number of bytes used to encrypt one block of data. It MUST be at least 2, no greater than 4096, and a multiple of 2. Excel uses 16
'spinCount' => 100000, // The number of times to iterate on a hash of a password. It MUST NOT be greater than 10,000,000. Excel uses 100,000.
'keyBits' => 256, // The length of the key to generate from the password. Must be a multiple of 8. Excel uses 256.
],
];
$lowerKeyHashAlgo = \strtolower($encryptionInfo['key']['hashAlgorithm']);
$lowerPackageHashAlgo = $encryptionInfo['key']['hashAlgorithm'] === $encryptionInfo['package']['hashAlgorithm'] ? $lowerKeyHashAlgo : \strtolower($encryptionInfo['package']['hashAlgorithm']);
$lowerKeyCipherAlgo = \strtolower($encryptionInfo['key']['cipherAlgorithm']);
$lowerPackageCipherAlgo = $encryptionInfo['key']['cipherAlgorithm'] === $encryptionInfo['package']['cipherAlgorithm'] ? $lowerKeyCipherAlgo : \strtolower($encryptionInfo['package']['cipherAlgorithm']);
// Package Encryption
$encryptedPackage = $this->_cryptPackage(
true,
$lowerPackageCipherAlgo,
$encryptionInfo['package']['cipherChaining'],
$lowerPackageHashAlgo,
$encryptionInfo['package']['blockSize'],
$encryptionInfo['package']['saltValue'],
$this->data,
$packageKey,
);
/**
* Create the data integrity fields used by clients for integrity checks.
*
* First generate a random array of bytes to use in HMAC.
* The docs say to use the same length as the key salt, but Excel seems to use 64.
*
* @var list<int|string> $hmacKey */
$hmacKey = unpack('C*', random_bytes(64));
// Then create an initialization vector using the package encryption info and the appropriate block key.
$hmacKeyIV = $this->_createIV(
$lowerPackageHashAlgo,
$encryptionInfo['package']['saltValue'],
$encryptionInfo['package']['blockSize'],
$this->_block_keys['dataIntegrity']['hmacKey'],
);
// Use the package key and the IV to encrypt the HMAC key
$encryptedHmacKey = $this->_crypt(
true,
$lowerPackageCipherAlgo,
$encryptionInfo['package']['cipherChaining'],
$packageKey,
$hmacKeyIV,
$hmacKey,
);
// Now create the HMAC
$hmacValue = $this->_hmac($lowerPackageHashAlgo, $hmacKey, $encryptedPackage['tmpFile']);
// Next generate an initialization vector for encrypting the resulting HMAC value.
$hmacValueIV = $this->_createIV(
$lowerPackageHashAlgo,
$encryptionInfo['package']['saltValue'],
$encryptionInfo['package']['blockSize'],
$this->_block_keys['dataIntegrity']['hmacValue'],
);
// Now encrypt the value
$encryptedHmacValue = $this->_crypt(
true,
$lowerPackageCipherAlgo,
$encryptionInfo['package']['cipherChaining'],
$packageKey,
$hmacValueIV,
$hmacValue,
);
// Put the encrypted key and value on the encryption info
$encryptionInfo['dataIntegrity'] = [
'encryptedHmacKey' => $encryptedHmacKey,
'encryptedHmacValue' => $encryptedHmacValue,
];
// Key Encryption
$password = $this->password;
// Convert the password to an encryption key
$key = $this->_convertPasswordToKey(
$password,
$lowerKeyHashAlgo,
$encryptionInfo['key']['saltValue'],
$encryptionInfo['key']['spinCount'],
$encryptionInfo['key']['keyBits'],
$this->_block_keys['key'],
);
// // Encrypt the package key with the
$encryptionInfo['key']['encryptedKeyValue'] = $this->_crypt(
true,
$lowerKeyCipherAlgo,
$encryptionInfo['key']['cipherChaining'],
$key,
$encryptionInfo['key']['saltValue'],
$packageKey,
);
// Verifier hash with random byte array for hashing
$verifierHashInput = (array) unpack('C*', random_bytes(16));
// Create an encryption key from the password for the input
$verifierHashInputKey = $this->_convertPasswordToKey(
$password,
$lowerKeyHashAlgo,
$encryptionInfo['key']['saltValue'],
$encryptionInfo['key']['spinCount'],
$encryptionInfo['key']['keyBits'],
$this->_block_keys['verifierHash']['input'],
);
// Use the key to encrypt the verifier input
$encryptionInfo['key']['encryptedVerifierHashInput'] = $this->_crypt(
true,
$lowerKeyCipherAlgo,
$encryptionInfo['key']['cipherChaining'],
$verifierHashInputKey,
$encryptionInfo['key']['saltValue'],
$verifierHashInput,
);
// Create a hash of the input
$verifierHashValue = $this->_hash($lowerKeyHashAlgo, $verifierHashInput);
// Create an encryption key from the password for the hash
$verifierHashValueKey = $this->_convertPasswordToKey(
$password,
$lowerKeyHashAlgo,
$encryptionInfo['key']['saltValue'],
$encryptionInfo['key']['spinCount'],
$encryptionInfo['key']['keyBits'],
$this->_block_keys['verifierHash']['value'],
);
// Use the key to encrypt the hash value
$encryptionInfo['key']['encryptedVerifierHashValue'] = $this->_crypt(
true,
$lowerKeyCipherAlgo,
$encryptionInfo['key']['cipherChaining'],
$verifierHashValueKey,
$encryptionInfo['key']['saltValue'],
$verifierHashValue,
);
// Build the encryption info buffer
$encryptionInfoBuffer = $this->_buildEncryptionInfo($encryptionInfo);
$OLE = new OLE_PPS_File(OLE::Asc2Ucs('EncryptionInfo'));
$OLE->init();
$OLE->append(pack('C*', ...$encryptionInfoBuffer));
$OLE2 = new OLE_PPS_File(OLE::Asc2Ucs('EncryptedPackage'));
$OLE2->init();
$filesize = (int) filesize($encryptedPackage['tmpFile']);
for ($i = 0; $i < ($filesize / 4096); $i++) {
$unpackEncryptedPackage = unpack('C*', (string) file_get_contents($encryptedPackage['tmpFile'], false, null, $i * 4096, 4096));
$OLE2->append(pack('C*', ...$unpackEncryptedPackage));
}
unlink($encryptedPackage['tmpFile']);
$root = new OLE_PPS_Root(1000000000, 1000000000, [$OLE, $OLE2]);
if ($this->NOFILE) {
$filePath = tempnam(sys_get_temp_dir(), 'NOFILE');
}
$filePath = (string) $filePath;
$root->save($filePath);
return file_get_contents($filePath);
}
/**
* Encryption Info
*
* @param array<string, array<string, array<string, int|string>>> $encryptionInfo Data
*
* @return array<string, mixed>
*/
private function _buildEncryptionInfo(array $encryptionInfo = [])
{
$ENCRYPTION_INFO_PREFIX = [0x04, 0x00, 0x04, 0x00, 0x40, 0x00, 0x00, 0x00];
/**
* Map the object into the appropriate XML structure. Buffers are encoded in base 64.
*
* @var array<string, array<int|string, array<int|string, mixed>|string>|string> $encryptionInfoNode
*/
$encryptionInfoNode = [
'name' => 'encryption',
'attributes' => [
'xmlns' => 'http://schemas.microsoft.com/office/2006/encryption',
'xmlns:p' => 'http://schemas.microsoft.com/office/2006/keyEncryptor/password',
'xmlns:c' => 'http://schemas.microsoft.com/office/2006/keyEncryptor/certificate',
],
'children' => [
[
'name' => 'keyData',
'attributes' => [
'saltSize' => count($encryptionInfo['package']['saltValue']),
'blockSize' => $encryptionInfo['package']['blockSize'],
'keyBits' => $encryptionInfo['package']['keyBits'],
'hashSize' => $encryptionInfo['package']['hashSize'],
'cipherAlgorithm' => $encryptionInfo['package']['cipherAlgorithm'],
'cipherChaining' => $encryptionInfo['package']['cipherChaining'],
'hashAlgorithm' => $encryptionInfo['package']['hashAlgorithm'],
'saltValue' => base64_encode(pack('C*', ...$encryptionInfo['package']['saltValue'])),
],
],
[
'name' => 'dataIntegrity',
'attributes' => [
'encryptedHmacKey' => base64_encode(pack('C*', ...$encryptionInfo['dataIntegrity']['encryptedHmacKey'])),
'encryptedHmacValue' => base64_encode(pack('C*', ...$encryptionInfo['dataIntegrity']['encryptedHmacValue'])),
],
],
[
'name' => 'keyEncryptors',
'children' => [
[
'name' => 'keyEncryptor',
'attributes' => [
'uri' => 'http://schemas.microsoft.com/office/2006/keyEncryptor/password',
],
'children' => [
[
'name' => 'p:encryptedKey',
'attributes' => [
'spinCount' => $encryptionInfo['key']['spinCount'],
'saltSize' => count($encryptionInfo['key']['saltValue']),
'blockSize' => $encryptionInfo['key']['blockSize'],
'keyBits' => $encryptionInfo['key']['keyBits'],
'hashSize' => $encryptionInfo['key']['hashSize'],
'cipherAlgorithm' => $encryptionInfo['key']['cipherAlgorithm'],
'cipherChaining' => $encryptionInfo['key']['cipherChaining'],
'hashAlgorithm' => $encryptionInfo['key']['hashAlgorithm'],
'saltValue' => base64_encode(pack('C*', ...$encryptionInfo['key']['saltValue'])),
'encryptedVerifierHashInput' => base64_encode(pack('C*', ...$encryptionInfo['key']['encryptedVerifierHashInput'])),
'encryptedVerifierHashValue' => base64_encode(pack('C*', ...$encryptionInfo['key']['encryptedVerifierHashValue'])),
'encryptedKeyValue' => base64_encode(pack('C*', ...$encryptionInfo['key']['encryptedKeyValue'])),
],
],
],
],
],
],
],
];
$byte_array = (array) unpack('C*', $this->arrayToXml($encryptionInfoNode));
array_unshift($byte_array, ...$ENCRYPTION_INFO_PREFIX);
return $byte_array;
}
/**
* Define a function that converts array to xml
*
* @param array<string, mixed> $array Array
*
* @return string
*/
private function arrayToXml(array $array = [])
{
$this->build($rootNode = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><encryption/>'), $array);
return str_replace(['\r', '\n', '\r\n', '\n\r'], '', (string) $rootNode->asXML());
}
/**
* Crypt method
*
* @param string $cipherAlgorithm
* @param string $cipherChaining
* @param array $key
* @param bool $encrypt
* @param array<int|string, int|string> $iv
* @param array<int|string, int|string> $input
*/
private function _crypt($encrypt, $cipherAlgorithm, $cipherChaining, $key, $iv, $input)
{
$algorithm = $cipherAlgorithm . '-' . (count($key) * 8);
if ($cipherChaining === 'ChainingModeCBC') {
$algorithm .= '-cbc';
} else {
throw new Exception("Unknown cipher chaining: {$cipherChaining}");
}
$cipher = [];
if ($encrypt) {
$ciphertext = openssl_encrypt(
pack('C*', ...$input),
$algorithm,
pack('C*', ...$key),
OPENSSL_NO_PADDING,
pack('C*', ...$iv),
);
$cipher = (array) unpack('C*', (string) $ciphertext);
}
return $cipher;
}
/**
* Hashing
*
* @param string $algorithm
*
* @return array<int|string, int|string>
*/
private function _hash($algorithm, ...$buffers)
{
try {
$ctx = hash_init($algorithm);
} catch (ValueError) {
throw new Exception(sprintf("Hash algorithm '%s' not supported!", $algorithm));
}
hash_update($ctx, pack('C*', ...$buffers));
return (array) unpack('C*', hash_final($ctx, true));
}
/**
* Hmac
*
* @param string $algorithm Algorithm
* @param list<int|string> $key Key
* @param string $fileName Filename
*
* @return array<int|string, int|string>
*/
private function _hmac($algorithm, $key, $fileName)
{
return (array) unpack('C*', hash_hmac_file(
$algorithm,
$fileName,
pack('C*', ...$key),
true,
));
}
/**
* Create Unsigned Integer 32-bit Buffer
*
* @param int $value
* @param int $bufferSize
*/
private function _createUInt32LEBuffer($value, $bufferSize = 4)
{
return array_pad(array_values((array) unpack('C*', pack('V', $value))), $bufferSize, 0);
}
private function _convertPasswordToKey($password, $hashAlgorithm, $saltValue, $spinCount, $keyBits, $blockKey)
{
// Password must be in unicode buffer
$passwordBuffer = array_map('hexdec', str_split(bin2hex(mb_convert_encoding($password, 'UTF-16LE', 'utf-8')), 2));
// Generate the initial hash
$key = $this->_hash($hashAlgorithm, $saltValue, $passwordBuffer);
// Get back to a binary string
$bKey = pack('C*', ...$key);
// Now regenerate until spin count
for ($i = 0; $i < $spinCount; $i++) {
$bKey = hash($hashAlgorithm, pack('V', $i) . $bKey, true);
}
// Convert binary string back to unpacked C* form
$key = (array) unpack('C*', $bKey);
// Now generate the final hash
$key = $this->_hash($hashAlgorithm, $key, $blockKey);
// Truncate or pad as needed to get to length of keyBits
$keyBytes = $keyBits / 8;
$keyCounter = count($key);
if ($keyCounter < $keyBytes) {
$key = array_pad($key, $keyBytes, 0x36);
} elseif ($keyCounter > $keyBytes) {
$key = array_slice($key, 0, $keyBytes);
}
return $key;
}
/**
* Create initialization vector
*
* @param string $hashAlgorithm
* @param array<int|string, int|string> $saltValue
* @param int $blockSize
* @param mixed $blockKey
*/
private function _createIV($hashAlgorithm, $saltValue, $blockSize, $blockKey)
{
// Create the block key from the current index
if (is_int($blockKey)) {
$blockKey = $this->_createUInt32LEBuffer($blockKey);
}
// Create the initialization vector by hashing the salt with the block key.
// Truncate or pad as needed to meet the block size.
$iv = $this->_hash($hashAlgorithm, $saltValue, $blockKey);
if (count($iv) < $blockSize) {
$iv = array_pad($iv, $blockSize, 0x36);
} elseif (count($iv) > $blockSize) {
$iv = array_slice($iv, 0, $blockSize);
}
return $iv;
}
/**
* Package Crypt
*
* @param bool $encrypt
* @param string $cipherAlgorithm
* @param string $cipherChaining
* @param string $hashAlgorithm
* @param int $blockSize
* @param array<int|string, int|string> $saltValue
* @param mixed $input
* @param array<int|string, int|string> $key
*/
private function _cryptPackage(
$encrypt,
$cipherAlgorithm,
$cipherChaining,
$hashAlgorithm,
$blockSize,
$saltValue,
$input,
$key = [],
) {
$tmpOutputChunk = (string) tempnam(sys_get_temp_dir(), 'outputChunk');
$tmpFileHeaderLength = (string) tempnam(sys_get_temp_dir(), 'fileHeaderLength');
$tmpFile = (string) tempnam(sys_get_temp_dir(), 'file');
if (is_callable($input) && is_a($in = $input(), 'Generator')) {
$inputCount = 0;
foreach ($in as $i => $inputChunk) {
$lengthInputChunk = count($inputChunk);
// Grab the next chunk
$inputCount += $lengthInputChunk;
$remainder = $lengthInputChunk % $blockSize;
if ($remainder !== 0) {
$inputChunk = array_pad($inputChunk, $lengthInputChunk + (16 - $remainder), 0);
}
// Create the initialization vector
$iv = $this->_createIV($hashAlgorithm, $saltValue, $blockSize, $i);
// Encrypt/decrypt the chunk and add it to the array
$outputChunk = $this->_crypt($encrypt, $cipherAlgorithm, $cipherChaining, $key, $iv, $inputChunk);
file_put_contents($tmpOutputChunk, pack('C*', ...$outputChunk), FILE_APPEND);
unset($inputChunk, $outputChunk, $iv);
}
unset($this->data);
file_put_contents($tmpFileHeaderLength, pack('C*', ...$this->_createUInt32LEBuffer($inputCount, $this->_offset)));
file_put_contents($tmpFile, file_get_contents($tmpFileHeaderLength) . file_get_contents($tmpOutputChunk));
unlink($tmpOutputChunk);
unlink($tmpFileHeaderLength);
return [
'tmpFile' => $tmpFile,
];
}
}
/**
* Build XML
*
* @param array<string, array<string, array<string, string>|string>|int|string> $data Data
* @param SimpleXMLElement $rootNode Node
*
* @return void
*/
private function build(SimpleXMLElement $rootNode, array $data = [])
{
// https://stackoverflow.com/questions/7717227/unable-to-add-attribute-with-namespace-prefix-using-php-simplexml
foreach ($data as $k => $v) {
if (is_countable($v)) {
foreach ($v as $kk => $vv) {
if ($k === 'attributes') {
$isNamespace = count(explode(':', $kk)) === 2;
if ($isNamespace) {
$rootNode->addAttribute('xmlns:xmlns:' . explode(':', $kk)[1], $vv);
} else {
$rootNode->addAttribute($kk, $vv);
}
}
if ($k === 'children') {
$isNamespace = count(explode(':', $vv['name'])) === 2;
$r = $isNamespace ? $rootNode->addChild('xmlns:' . $vv['name'], '') : $rootNode->addChild($vv['name'], '');
$this->build($r, $vv);
}
}
}
}
}
}