1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Driver MySQL component |
4
|
|
|
* Copyright 2018 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma\Drivers\MySQL\Messages; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents a Handshake Message. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class HandshakeMessage implements \Plasma\Drivers\MySQL\Messages\MessageInterface { |
17
|
|
|
/** |
18
|
|
|
* The Handshake Protocol version. |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $protocolVersion; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A human readable server version. |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $serverVersion; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The connection ID. |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public $connectionID; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Authentication data. |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $scramble; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The server's capability flags. |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
public $capability; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The character set. |
49
|
|
|
* @var int|null |
50
|
|
|
*/ |
51
|
|
|
public $characterSet; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The status flags. |
55
|
|
|
* @var int|null |
56
|
|
|
* @see \Plasma\Drivers\MySQL\StatusFlags |
57
|
|
|
*/ |
58
|
|
|
public $statusFlags; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The authentication plugin name. |
62
|
|
|
* @var string|null |
63
|
|
|
*/ |
64
|
|
|
public $authPluginName; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \Plasma\Drivers\MySQL\ProtocolParser |
68
|
|
|
*/ |
69
|
|
|
protected $parser; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Constructor. |
73
|
|
|
* @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
74
|
|
|
*/ |
75
|
12 |
|
function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
76
|
12 |
|
$this->parser = $parser; |
77
|
12 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the identifier for the packet. |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
static function getID(): string { |
84
|
|
|
return ""; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parses the message, once the complete string has been received. |
89
|
|
|
* Returns false if not enough data has been received, or the remaining buffer. |
90
|
|
|
* @param string $buffer |
91
|
|
|
* @return string|bool |
92
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
93
|
|
|
*/ |
94
|
12 |
|
function parseMessage(string $buffer) { |
95
|
12 |
|
$protocol = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
96
|
|
|
|
97
|
|
|
switch($protocol) { |
98
|
12 |
|
case 0x0A: |
|
|
|
|
99
|
12 |
|
return $this->parseProtocol10($buffer); |
100
|
|
|
break; |
|
|
|
|
101
|
|
|
default: |
|
|
|
|
102
|
|
|
$exception = new \Plasma\Drivers\MySQL\Messages\ParseException('Unsupported protocol version'); |
103
|
|
|
$exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
104
|
|
|
$exception->setBuffer(''); |
105
|
|
|
|
106
|
|
|
throw $exception; |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the parser which created this message. |
113
|
|
|
* @return \Plasma\Drivers\MySQL\ProtocolParser |
114
|
|
|
*/ |
115
|
|
|
function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
116
|
|
|
return $this->parser; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the parser state, if necessary. If not, return `-1`. |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
12 |
|
function setParserState(): int { |
124
|
12 |
|
return \Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
|
|
|
|
128
|
|
|
* Parses the message as Handshake V10. |
129
|
|
|
* @return string|bool |
130
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
131
|
|
|
*/ |
132
|
12 |
|
protected function parseProtocol10(string $buffer) { |
133
|
12 |
|
$versionLength = \strpos($buffer, "\x00"); |
134
|
12 |
|
if($versionLength === false) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
12 |
|
$buffLength = \strlen($buffer); |
139
|
12 |
|
$minLength = $versionLength + 1 + 15; |
140
|
12 |
|
$moreDataLength = $buffLength - $minLength; |
141
|
|
|
|
142
|
12 |
|
if($buffLength < $minLength) { |
143
|
|
|
return false; |
144
|
12 |
|
} elseif($moreDataLength > 0 && $moreDataLength < 16) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
12 |
|
$serverVersion = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringNull($buffer); |
149
|
12 |
|
$connectionID = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt4($buffer); |
150
|
12 |
|
$scramble = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer, 8); // Part 1 |
151
|
|
|
|
152
|
12 |
|
$buffer = \substr($buffer, 1); // Remove filler byte |
153
|
|
|
|
154
|
12 |
|
$capability = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
155
|
|
|
|
156
|
12 |
|
$this->protocolVersion = 10; |
157
|
12 |
|
$this->serverVersion = $serverVersion; |
158
|
12 |
|
$this->connectionID = $connectionID; |
159
|
12 |
|
$this->scramble = $scramble; |
160
|
|
|
|
161
|
12 |
|
if(\strlen($buffer) > 0) { |
162
|
12 |
|
$characterSet = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
163
|
12 |
|
$statusFlags = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer); |
164
|
12 |
|
$capability += \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer) << 16; |
165
|
|
|
|
166
|
12 |
|
if(($capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PROTOCOL_41) === 0) { |
167
|
|
|
$exception = new \Plasma\Drivers\MySQL\Messages\ParseException('The old MySQL protocol 320 is not supported'); |
168
|
|
|
$exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
169
|
|
|
$exception->setBuffer(''); |
170
|
|
|
|
171
|
|
|
throw $exception; |
172
|
|
|
} |
173
|
|
|
|
174
|
12 |
|
if(($capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
175
|
12 |
|
$authDataLength = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer); |
176
|
|
|
} else { |
177
|
|
|
$authDataLength = 0; |
178
|
|
|
$buffer = \substr($buffer, 1); |
179
|
|
|
} |
180
|
|
|
|
181
|
12 |
|
$buffer = \substr($buffer, 10); |
182
|
|
|
|
183
|
12 |
|
if(($capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SECURE_CONNECTION) !== 0) { |
184
|
12 |
|
$len = \max(13, ($authDataLength - 8)); |
185
|
12 |
|
$this->scramble .= \rtrim(\Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer, $len), "\x00"); |
186
|
|
|
} |
187
|
|
|
|
188
|
12 |
|
if(($capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
189
|
12 |
|
$authPluginName = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringNull($buffer); |
190
|
|
|
} |
191
|
|
|
|
192
|
12 |
|
$this->characterSet = $characterSet; |
193
|
12 |
|
$this->statusFlags = $statusFlags; |
194
|
12 |
|
$this->authPluginName = $authPluginName ?? null; |
195
|
|
|
} |
196
|
|
|
|
197
|
12 |
|
return $buffer; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|