-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpCrate.class.php
More file actions
226 lines (176 loc) · 6.03 KB
/
PhpCrate.class.php
File metadata and controls
226 lines (176 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Licensed to Level 7 Systems Ltd. under one or more contributor
* license agreements. Level 7 Systems Ltd. licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
class PhpCrate
{
private $read_timeout = 10000; // ms
private $port = 4200;
private $servers = array();
private $_socket = null;
private $server;
/**
* Sets array of servers to use
*
* @param array $servers array of server IP address to use
*/
public function setServers($servers)
{
foreach ($servers as $server) {
$this->servers[$server] = true;
}
}
/**
* Sets port number to use
*
* @param int $port
*/
public function setPort($port)
{
$this->port = $port;
}
/**
* Executes INSERT, UPDATE, DELETE statement
*
* @param string $sql SQL query to execute
* @param array $args array of parameters to pass to the query
*
* @return int number of rows affected
*/
public function exec($sql, $args = array())
{
$response = null;
while (!$response) {
$response = $this->send($sql, $args);
}
if (!isset($response['rowcount'])) {
throw new Exception("rowcount parameter missing in server response");
}
return $response['rowcount'];
}
/**
* Executes SELECT statement
*
* @param string $sql SQL query to execute
* @param array $args array of parameters to pass to the query
*
* @return array results as an associative array
*/
public function query($sql, $args = array())
{
$response = null;
while (!$response) {
$response = $this->send($sql, $args);
}
if (!isset($response['rows'])) {
throw new Exception("rows parameter missing in server response");
}
if (!isset($response['cols'])) {
throw new Exception("cols parameter missing in server response");
}
$output = array();
foreach ($response['rows'] as $index => $row) {
foreach ($row as $key => $value) {
$output[$index][$response['cols'][$key]] = $value;
}
}
return $output;
}
private function connect()
{
if (!$this->servers) {
throw new Exception("No servers to connect left");
}
$servers = array_keys($this->servers);
$this->server = $servers[rand(0, count($servers) - 1)];
if (!$this->_socket = @fsockopen($this->server, $this->port, $err_no, $err_str, 2)) {
$this->_socket = null;
unset($this->servers[$this->server]);
return false;
}
stream_set_timeout($this->_socket, 3);
stream_set_blocking($this->_socket, 0);
return true;
}
private function send($sql, $args)
{
if (!$this->_socket) {
while (!$this->_socket) {
$this->connect();
}
}
$json = json_encode(array(
"stmt" => $sql,
"args" => $args,
));
$data = "POST /_sql HTTP/1.1\r\n"
."Accept: */*\r\n"
."Host: ".$this->server.":".$this->port."\r\n"
."Content-Length: ".strlen($json)."\r\n\r\n"
.$json."\r\n";
if (!fwrite($this->_socket, $data, strlen($data))) {
unset($this->servers[$this->server]);
$this->_socket = null;
return false;
}
$response_length = 0;
$i = 0;
$response = '';
$body = false;
while (1) {
$line = trim(fgets($this->_socket));
$m = array();
if (!$response_length && preg_match('/^Content-Length: ([0-9]+)$/', $line, $m)) {
$response_length = $m[1];
}
// if we have Content-Length and there is an ampty line, what is below must be response body
if ($response_length && !$body && !$line) {
$body = true;
}
if ($body) {
if (!$response_length) {
throw new Exception("Failed to parse Content-Length header from server response");
}
$response.= $line;
if (strlen($response) == $response_length) {
break;
}
}
usleep(5);
$i++;
if ($i > ($this->read_timeout * 20)) {
unset($this->servers[$this->server]);
$this->_socket = null;
return false;
}
}
if ($response == '') {
throw new Exception("Failed to read response to '$sql'");
}
if (!$json = json_decode($response, true)) {
throw new Exception("Failed to json_decode '$response'");
}
if (isset($json['error'])) {
throw new Exception("(".$json['error']['code'].") ".$json['error']['message']);
}
return $json;
}
public function __destruct()
{
if ($this->_socket) {
return fclose($this->_socket);
}
}
}