|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plasma Driver MySQL component |
|
4
|
|
|
* Copyright 2018-2019 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
|
63 |
|
function __construct( |
|
70
|
|
|
\Plasma\Drivers\MySQL\ProtocolParser $parser, \Plasma\Drivers\MySQL\Messages\HandshakeMessage $handshake, |
|
71
|
|
|
int $capability, ?string $plugin, string $user, string $password, string $database |
|
72
|
|
|
) { |
|
73
|
63 |
|
$this->parser = $parser; |
|
74
|
63 |
|
$this->handshake = $handshake; |
|
75
|
63 |
|
$this->capability = $capability; |
|
76
|
63 |
|
$this->plugin = $plugin; |
|
77
|
|
|
|
|
78
|
63 |
|
$this->user = $user; |
|
79
|
63 |
|
$this->password = $password; |
|
80
|
63 |
|
$this->database = $database; |
|
81
|
63 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the encoded message for writing to the database connection. |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
63 |
|
function getEncodedMessage(): string { |
|
88
|
63 |
|
$maxPacketSize = \Plasma\Drivers\MySQL\ProtocolParser::CLIENT_MAX_PACKET_SIZE; |
|
89
|
63 |
|
$charsetNumber = \Plasma\Drivers\MySQL\ProtocolParser::CLIENT_CHARSET_NUMBER; |
|
90
|
|
|
|
|
91
|
63 |
|
$packet = \pack('VVc', $this->capability, $maxPacketSize, $charsetNumber); |
|
92
|
63 |
|
$packet .= \str_repeat("\x00", 23); |
|
93
|
63 |
|
$packet .= $this->user."\x00"; |
|
94
|
|
|
|
|
95
|
63 |
|
if($this->plugin !== null) { |
|
96
|
63 |
|
$plug = $this->plugin; |
|
97
|
|
|
|
|
98
|
|
|
/** @var \Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface $auth */ |
|
99
|
63 |
|
$auth = new $plug($this->parser, $this->handshake); |
|
100
|
|
|
|
|
101
|
63 |
|
$packet .= $auth->getHandshakeAuth($this->password); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
63 |
|
if($this->database !== '') { |
|
105
|
47 |
|
$packet .= $this->database."\x00"; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
63 |
|
if($this->plugin !== null) { |
|
109
|
63 |
|
$packet .= $this->handshake->authPluginName."\x00"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
63 |
|
$this->finished = true; |
|
113
|
63 |
|
return $packet; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the parser state, if necessary. If not, return `-1`. |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
63 |
|
function setParserState(): int { |
|
121
|
63 |
|
return \Plasma\Drivers\MySQL\ProtocolParser::STATE_AUTH; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Sets the command as completed. This state gets reported back to the user. |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
62 |
|
function onComplete(): void { |
|
129
|
62 |
|
$this->finished = true; |
|
130
|
62 |
|
$this->emit('end'); |
|
131
|
62 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets the command as errored. This state gets reported back to the user. |
|
135
|
|
|
* @param \Throwable $throwable |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
1 |
|
function onError(\Throwable $throwable): void { |
|
139
|
1 |
|
$this->finished = true; |
|
140
|
1 |
|
$this->emit('error', array($throwable)); |
|
141
|
1 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Sends the next received value into the command. |
|
145
|
|
|
* @param mixed $value |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
function onNext($value): void { |
|
149
|
|
|
// Nothing to do |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Whether the command has finished. |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
62 |
|
function hasFinished(): bool { |
|
157
|
62 |
|
return $this->finished; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Whether this command sets the connection as busy. |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
63 |
|
function waitForCompletion(): bool { |
|
165
|
63 |
|
return true; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Whether the sequence ID should be resetted. |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
63 |
|
function resetSequence(): bool { |
|
173
|
63 |
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|