1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Lisachenko\Protocol\FCGI\Record; |
4
|
|
|
|
5
|
|
|
use Lisachenko\Protocol\FCGI; |
6
|
|
|
use Lisachenko\Protocol\FCGI\Record; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Params request record |
10
|
|
|
* |
11
|
|
|
* @author Alexander.Lisachenko |
12
|
|
|
*/ |
13
|
|
|
class Params extends Record |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* List of params |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $values = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructs a param request |
25
|
|
|
* |
26
|
|
|
* @param array $values |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $values = []) |
29
|
|
|
{ |
30
|
7 |
|
$this->type = FCGI::PARAMS; |
31
|
|
|
$this->values = $values; |
32
|
7 |
|
$this->setContentData($this->packPayload()); |
33
|
7 |
|
} |
34
|
7 |
|
|
35
|
7 |
|
/** |
36
|
|
|
* Returns an associative list of parameters |
37
|
|
|
*/ |
38
|
|
|
public function getValues(): array |
39
|
|
|
{ |
40
|
|
|
return $this->values; |
41
|
|
|
} |
42
|
6 |
|
|
43
|
|
|
/** |
44
|
6 |
|
* {@inheritdoc} |
45
|
|
|
* @param static $self |
46
|
|
|
*/ |
47
|
|
|
protected static function unpackPayload($self, string $data): void |
48
|
|
|
{ |
49
|
|
|
$currentOffset = 0; |
50
|
|
|
do { |
51
|
|
|
[$nameLengthHigh] = array_values(unpack('CnameLengthHigh', $data)); |
|
|
|
|
52
|
|
|
$isLongName = ($nameLengthHigh >> 7 == 1); |
53
|
|
|
$valueOffset = $isLongName ? 4 : 1; |
54
|
|
|
|
55
|
3 |
|
[$valueLengthHigh] = array_values(unpack('CvalueLengthHigh', substr($data, $valueOffset))); |
|
|
|
|
56
|
|
|
$isLongValue = ($valueLengthHigh >> 7 == 1); |
57
|
3 |
|
$dataOffset = $valueOffset + ($isLongValue ? 4 : 1); |
58
|
|
|
|
59
|
3 |
|
$formatParts = [ |
60
|
3 |
|
$isLongName ? 'NnameLength' : 'CnameLength', |
61
|
3 |
|
$isLongValue ? 'NvalueLength' : 'CvalueLength', |
62
|
|
|
]; |
63
|
3 |
|
$format = join('/', $formatParts); |
64
|
3 |
|
[$nameLength, $valueLength] = array_values(unpack($format, $data)); |
|
|
|
|
65
|
3 |
|
|
66
|
|
|
// Clear top bit for long record |
67
|
|
|
$nameLength &= ($isLongName ? 0x7fffffff : 0x7f); |
68
|
3 |
|
$valueLength &= ($isLongValue ? 0x7fffffff : 0x7f); |
69
|
3 |
|
|
70
|
3 |
|
[$nameData, $valueData] = array_values( |
|
|
|
|
71
|
3 |
|
unpack( |
72
|
|
|
"a{$nameLength}nameData/a{$valueLength}valueData", |
73
|
|
|
substr($data, $dataOffset) |
74
|
|
|
) |
75
|
3 |
|
); |
76
|
|
|
|
77
|
|
|
$self->values[$nameData] = $valueData; |
78
|
3 |
|
|
79
|
3 |
|
$keyValueLength = $dataOffset + $nameLength + $valueLength; |
80
|
|
|
$data = substr($data, $keyValueLength); |
81
|
3 |
|
$currentOffset += $keyValueLength; |
82
|
3 |
|
} while ($currentOffset < $self->getContentLength()); |
83
|
3 |
|
} |
84
|
3 |
|
|
85
|
|
|
/** {@inheritdoc} */ |
86
|
3 |
|
protected function packPayload(): string |
87
|
|
|
{ |
88
|
3 |
|
$payload = ''; |
89
|
3 |
|
foreach ($this->values as $nameData => $valueData) { |
90
|
3 |
|
$nameLength = strlen($nameData); |
91
|
3 |
|
$valueLength = strlen((string) $valueData); |
92
|
|
|
$isLongName = $nameLength > 127; |
93
|
3 |
|
$isLongValue = $valueLength > 127; |
94
|
|
|
$formatParts = [ |
95
|
|
|
$isLongName ? 'N' : 'C', |
96
|
|
|
$isLongValue ? 'N' : 'C', |
97
|
|
|
"a{$nameLength}", |
98
|
|
|
"a{$valueLength}", |
99
|
|
|
]; |
100
|
|
|
$format = join('', $formatParts); |
101
|
7 |
|
|
102
|
|
|
$payload .= pack( |
103
|
7 |
|
$format, |
104
|
7 |
|
$isLongName ? ($nameLength | 0x80000000) : $nameLength, |
105
|
3 |
|
$isLongValue ? ($valueLength | 0x80000000) : $valueLength, |
106
|
3 |
|
$nameData, |
107
|
3 |
|
$valueData |
108
|
3 |
|
); |
109
|
|
|
} |
110
|
3 |
|
|
111
|
3 |
|
return $payload; |
112
|
3 |
|
} |
113
|
3 |
|
|
114
|
|
|
} |
115
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.