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 | 13 | function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
|
76 | 13 | $this->parser = $parser; |
|
77 | 13 | } |
|
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 \Plasma\BinaryBuffer $buffer |
||
91 | * @return bool |
||
92 | * @throws \Plasma\Drivers\MySQL\Messages\ParseException |
||
93 | */ |
||
94 | 13 | function parseMessage(\Plasma\BinaryBuffer $buffer): bool { |
|
95 | 13 | $protocol = $buffer->readInt1(); |
|
96 | |||
97 | switch($protocol) { |
||
98 | 13 | case 0x0A: |
|
99 | 13 | $this->parseProtocol10($buffer); |
|
100 | 13 | break; |
|
1 ignored issue
–
show
Coding Style
introduced
by
![]() |
|||
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 | 13 | return true; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get the parser which created this message. |
||
115 | * @return \Plasma\Drivers\MySQL\ProtocolParser |
||
116 | */ |
||
117 | function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
||
118 | return $this->parser; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Sets the parser state, if necessary. If not, return `-1`. |
||
123 | * @return int |
||
124 | */ |
||
125 | 13 | function setParserState(): int { |
|
126 | 13 | return \Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Parses the message as Handshake V10. |
||
131 | * @return bool |
||
132 | * @throws \Plasma\Drivers\MySQL\Messages\ParseException |
||
133 | */ |
||
134 | 13 | protected function parseProtocol10(\Plasma\BinaryBuffer $buffer): bool { |
|
135 | 13 | $versionLength = \strpos($buffer->getContents(), "\x00"); |
|
136 | 13 | if($versionLength === false) { |
|
137 | return false; |
||
138 | } |
||
139 | |||
140 | 13 | $buffLength = $buffer->getSize(); |
|
141 | 13 | $minLength = $versionLength + 1 + 15; |
|
142 | 13 | $moreDataLength = $buffLength - $minLength; |
|
143 | |||
144 | 13 | if($buffLength < $minLength) { |
|
145 | return false; |
||
146 | 13 | } elseif($moreDataLength > 0 && $moreDataLength < 16) { |
|
147 | return false; |
||
148 | } |
||
149 | |||
150 | 13 | $this->protocolVersion = 10; |
|
151 | 13 | $this->serverVersion = $buffer->readStringNull(); |
|
152 | 13 | $this->connectionID = $buffer->readInt4(); |
|
153 | 13 | $this->scramble = $buffer->readStringLength(8); // Part 1 |
|
154 | |||
155 | 13 | $buffer->readStringLength(1); // Remove filler byte |
|
156 | |||
157 | 13 | $this->capability = $buffer->readInt2(); |
|
158 | |||
159 | 13 | if($buffer->getSize() > 0) { |
|
160 | 13 | $this->characterSet = $buffer->readInt1(); |
|
161 | 13 | $this->statusFlags = $buffer->readInt2(); |
|
162 | 13 | $this->capability += $buffer->readInt2() << 16; |
|
163 | |||
164 | 13 | if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PROTOCOL_41) === 0) { |
|
165 | $exception = new \Plasma\Drivers\MySQL\Messages\ParseException('The old MySQL protocol 320 is not supported'); |
||
166 | $exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
||
167 | $exception->setBuffer(''); |
||
168 | |||
169 | throw $exception; |
||
170 | } |
||
171 | |||
172 | 13 | if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
|
173 | 13 | $authDataLength = $buffer->readInt1(); |
|
174 | } else { |
||
175 | $authDataLength = 0; |
||
176 | $buffer->readStringLength(1); |
||
177 | } |
||
178 | |||
179 | 13 | $buffer->readStringLength(10); |
|
180 | |||
181 | 13 | if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SECURE_CONNECTION) !== 0) { |
|
182 | 13 | $len = \max(13, ($authDataLength - 8)); |
|
183 | 13 | $this->scramble .= \rtrim($buffer->readStringLength($len), "\x00"); |
|
184 | } |
||
185 | |||
186 | 13 | if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
|
187 | 13 | $this->authPluginName = $buffer->readStringNull(); |
|
188 | } |
||
189 | } |
||
190 | |||
191 | 13 | return true; |
|
192 | } |
||
193 | } |
||
194 |