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\Commands; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handshake Response command. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class HandshakeResponseCommand implements CommandInterface { |
17
|
|
|
use \Evenement\EventEmitterTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Plasma\Drivers\MySQL\ProtocolParser |
21
|
|
|
*/ |
22
|
|
|
protected $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Plasma\Drivers\MySQL\Messages\HandshakeMessage |
26
|
|
|
*/ |
27
|
|
|
protected $handshake; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $capability; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
protected $plugin; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $user; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $password; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $database; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $finished = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
* @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
62
|
|
|
* @param \Plasma\Drivers\MySQL\Messages\HandshakeMessage $handshake |
63
|
|
|
* @param int $capability |
64
|
|
|
* @param string|null $plugin |
65
|
|
|
* @param string $user |
66
|
|
|
* @param string $password |
67
|
|
|
* @param string $database |
68
|
|
|
*/ |
69
|
10 |
|
function __construct( |
70
|
|
|
\Plasma\Drivers\MySQL\ProtocolParser $parser, \Plasma\Drivers\MySQL\Messages\HandshakeMessage $handshake, int $capability, ?string $plugin, string $user, string $password, string $database |
71
|
|
|
) { |
72
|
10 |
|
$this->parser = $parser; |
73
|
10 |
|
$this->handshake = $handshake; |
74
|
10 |
|
$this->capability = $capability; |
75
|
10 |
|
$this->plugin = $plugin; |
76
|
|
|
|
77
|
10 |
|
$this->user = $user; |
78
|
10 |
|
$this->password = $password; |
79
|
10 |
|
$this->database = $database; |
80
|
10 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the encoded message for writing to the database connection. |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
10 |
|
function getEncodedMessage(): string { |
87
|
10 |
|
$maxPacketSize = \Plasma\Drivers\MySQL\ProtocolParser::CLIENT_MAX_PACKET_SIZE; |
88
|
10 |
|
$charsetNumber = \Plasma\Drivers\MySQL\ProtocolParser::CLIENT_CHARSET_NUMBER; |
89
|
|
|
|
90
|
10 |
|
$packet = \pack('VVc', $this->capability, $maxPacketSize, $charsetNumber); |
91
|
10 |
|
$packet .= \str_repeat("\x00", 23); |
92
|
10 |
|
$packet .= $this->user."\x00"; |
93
|
|
|
|
94
|
10 |
|
if($this->plugin !== null) { |
95
|
10 |
|
$plug = $this->plugin; |
96
|
|
|
|
97
|
|
|
/** @var \Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface $auth */ |
98
|
10 |
|
$auth = new $plug($this->parser, $this->handshake); |
99
|
|
|
|
100
|
10 |
|
$packet .= $auth->getHandshakeAuth($this->password); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
if($this->database !== '') { |
104
|
|
|
$packet .= $this->database."\x00"; |
105
|
|
|
} |
106
|
|
|
|
107
|
10 |
|
if($this->plugin !== null) { |
108
|
10 |
|
$packet .= $this->handshake->authPluginName."\x00"; |
109
|
|
|
} |
110
|
|
|
|
111
|
10 |
|
$this->finished = true; |
112
|
10 |
|
return $packet; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the parser state, if necessary. If not, return `-1`. |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
10 |
|
function setParserState(): int { |
120
|
10 |
|
return \Plasma\Drivers\MySQL\ProtocolParser::STATE_AUTH; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets the command as completed. This state gets reported back to the user. |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
9 |
|
function onComplete(): void { |
128
|
9 |
|
$this->finished = true; |
129
|
9 |
|
$this->emit('end', array()); |
130
|
9 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the command as errored. This state gets reported back to the user. |
134
|
|
|
* @param \Throwable $throwable |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
1 |
|
function onError(\Throwable $throwable): void { |
138
|
1 |
|
$this->finished = true; |
139
|
1 |
|
$this->emit('error', array($throwable)); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sends the next received value into the command. |
144
|
|
|
* @param mixed $value |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
function onNext($value): void { |
148
|
|
|
// Nothing to do |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Whether the command has finished. |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
9 |
|
function hasFinished(): bool { |
156
|
9 |
|
return $this->finished; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Whether this command sets the connection as busy. |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
10 |
|
function waitForCompletion(): bool { |
164
|
10 |
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Whether the sequence ID should be resetted. |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
10 |
|
function resetSequence(): bool { |
172
|
10 |
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|