| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('WPRDBCallback')) : |
| 5 |
require_once dirname( __FILE__ ) . '/../streams.php'; |
| 6 |
|
| 7 |
class WPRDBCallback extends WPRCallbackBase { |
| 8 |
public $db; |
| 9 |
public $stream; |
| 10 |
public $account; |
| 11 |
public $siteinfo; |
| 12 |
|
| 13 |
public static $bvTables = array("fw_requests", "lp_requests", "ip_store"); |
| 14 |
|
| 15 |
const DB_WING_VERSION = 1.5; |
| 16 |
|
| 17 |
public function __construct($callback_handler) { |
| 18 |
$this->db = $callback_handler->db; |
| 19 |
$this->account = $callback_handler->account; |
| 20 |
$this->siteinfo = $callback_handler->siteinfo; |
| 21 |
} |
| 22 |
|
| 23 |
public function getLastID($pkeys, $end_row) { |
| 24 |
$last_ids = array(); |
| 25 |
foreach($pkeys as $pk) { |
| 26 |
$last_ids[$pk] = $end_row[$pk]; |
| 27 |
} |
| 28 |
return $last_ids; |
| 29 |
} |
| 30 |
|
| 31 |
public function getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, $include_rows = false) { |
| 32 |
$tinfo = array(); |
| 33 |
|
| 34 |
$rows_count = $this->db->rowsCount($table); |
| 35 |
$result = array('count' => $rows_count); |
| 36 |
if ($limit == 0) { |
| 37 |
$limit = $rows_count; |
| 38 |
} |
| 39 |
$srows = 1; |
| 40 |
while (($limit > 0) && ($srows > 0)) { |
| 41 |
if ($bsize > $limit) |
| 42 |
$bsize = $limit; |
| 43 |
$rows = $this->db->getTableContent($table, '*', $filter, $bsize, $offset); |
| 44 |
$srows = sizeof($rows); |
| 45 |
$data = array(); |
| 46 |
$data["table_name"] = $tname; |
| 47 |
$data["offset"] = $offset; |
| 48 |
$data["size"] = $srows; |
| 49 |
$serialized_rows = serialize($rows); |
| 50 |
$data['md5'] = md5($serialized_rows); |
| 51 |
$data['length'] = strlen($serialized_rows); |
| 52 |
array_push($tinfo, $data); |
| 53 |
if (!empty($pkeys) && $srows > 0) { |
| 54 |
$end_row = end($rows); |
| 55 |
$last_ids = $this->getLastID($pkeys, $end_row); |
| 56 |
$data['last_ids'] = $last_ids; |
| 57 |
$result['last_ids'] = $last_ids; |
| 58 |
} |
| 59 |
if ($include_rows) { |
| 60 |
$data["rows"] = $rows; |
| 61 |
$str = serialize($data); |
| 62 |
$this->stream->writeStream($str); |
| 63 |
} |
| 64 |
$offset += $srows; |
| 65 |
$limit -= $srows; |
| 66 |
} |
| 67 |
$result['size'] = $offset; |
| 68 |
$result['tinfo'] = $tinfo; |
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
public function buildPrimaryKeyCursorFilter($pkeys, $last_ids) { |
| 73 |
if (empty($pkeys)) return null; |
| 74 |
foreach($pkeys as $pk) { |
| 75 |
if (!isset($last_ids[$pk]) || $last_ids[$pk] === '') return null; |
| 76 |
} |
| 77 |
|
| 78 |
$conditions = array(); |
| 79 |
$order_by = array(); |
| 80 |
foreach($pkeys as $index => $pk) { |
| 81 |
$key = "`".$pk."`"; |
| 82 |
$order_by[] = $key; |
| 83 |
$parts = array(); |
| 84 |
for ($eq_index = 0; $eq_index < $index; $eq_index++) { |
| 85 |
$eq_pk = $pkeys[$eq_index]; |
| 86 |
$eq_key = "`".$eq_pk."`"; |
| 87 |
$eq_last_id = esc_sql((string) $last_ids[$eq_pk]); |
| 88 |
$parts[] = $eq_key." = '".$eq_last_id."'"; |
| 89 |
} |
| 90 |
$last_id = esc_sql((string) $last_ids[$pkeys[$index]]); |
| 91 |
$parts[] = $key." > '".$last_id."'"; |
| 92 |
$condition = implode(" AND ", $parts); |
| 93 |
$conditions[] = ($index == 0) ? $condition : "(".$condition.")"; |
| 94 |
} |
| 95 |
return " WHERE ".implode(" OR ", $conditions)." ORDER BY ".implode(",", $order_by); |
| 96 |
} |
| 97 |
|
| 98 |
public function getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, $include_rows = false, $cursor_mode = false) { |
| 99 |
$tinfo = array(); |
| 100 |
|
| 101 |
$rows_count = $this->db->rowsCount($table); |
| 102 |
$result = array('count' => $rows_count); |
| 103 |
if ($limit == 0) { |
| 104 |
$limit = $rows_count; |
| 105 |
} |
| 106 |
$srows = 1; |
| 107 |
$current_filter = $filter; |
| 108 |
$cursor_active = ($cursor_mode === true && !empty($pkeys)); |
| 109 |
$query_offset = $offset; |
| 110 |
while (($limit > 0) && ($srows > 0)) { |
| 111 |
if ($bsize > $limit) |
| 112 |
$bsize = $limit; |
| 113 |
$rows = $this->db->getTableContent($table, '*', $current_filter, $bsize, $query_offset); |
| 114 |
$srows = sizeof($rows); |
| 115 |
$data = array(); |
| 116 |
$data["table_name"] = $tname; |
| 117 |
$data["offset"] = $offset; |
| 118 |
$data["size"] = $srows; |
| 119 |
$serialized_rows = serialize($rows); |
| 120 |
$data['md5'] = md5($serialized_rows); |
| 121 |
$data['length'] = strlen($serialized_rows); |
| 122 |
array_push($tinfo, $data); |
| 123 |
if (!empty($pkeys) && $srows > 0) { |
| 124 |
$end_row = end($rows); |
| 125 |
$last_ids = $this->getLastID($pkeys, $end_row); |
| 126 |
$data['last_ids'] = $last_ids; |
| 127 |
$result['last_ids'] = $last_ids; |
| 128 |
if ($cursor_active) { |
| 129 |
$cursor_filter = $this->buildPrimaryKeyCursorFilter($pkeys, $last_ids); |
| 130 |
if ($cursor_filter !== null) { |
| 131 |
$current_filter = $cursor_filter; |
| 132 |
} else { |
| 133 |
$current_filter = $filter; |
| 134 |
$cursor_active = false; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
if ($include_rows) { |
| 139 |
$data["rows"] = $rows; |
| 140 |
$str = serialize($data); |
| 141 |
$this->stream->writeStream($str); |
| 142 |
} |
| 143 |
$offset += $srows; |
| 144 |
$limit -= $srows; |
| 145 |
$query_offset = $cursor_active ? 0 : $offset; |
| 146 |
} |
| 147 |
$result['size'] = $offset; |
| 148 |
$result['tinfo'] = $tinfo; |
| 149 |
return $result; |
| 150 |
} |
| 151 |
|
| 152 |
public function streamQueryResult($identifier, $query, $pkeys) { |
| 153 |
$data = array(); |
| 154 |
$data["identifier"] = $identifier; |
| 155 |
$data["query"] = $query; |
| 156 |
|
| 157 |
$data["query_start_time"] = time(); |
| 158 |
$rows = $this->db->getResult($query); |
| 159 |
$srows = sizeof($rows); |
| 160 |
$data["size"] = $srows; |
| 161 |
$data["query_end_time"] = time(); |
| 162 |
if (!empty($pkeys) && $srows > 0) { |
| 163 |
$end_row = end($rows); |
| 164 |
$last_ids = $this->getLastID($pkeys, $end_row); |
| 165 |
$data['last_ids'] = $last_ids; |
| 166 |
} |
| 167 |
$result = array_merge($data); |
| 168 |
$data["rows"] = $rows; |
| 169 |
$serialized_rows = serialize($data); |
| 170 |
$this->stream->writeStream($serialized_rows); |
| 171 |
$result['length'] = strlen($serialized_rows); |
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
function getRandomData($totalSize, $bsize) { |
| 176 |
if ($bsize == 0) { |
| 177 |
$bsize = $totalSize; |
| 178 |
} |
| 179 |
|
| 180 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 181 |
$charactersLength = strlen($characters); |
| 182 |
|
| 183 |
while ($totalSize > 0) { |
| 184 |
if ($bsize > $totalSize) { |
| 185 |
$bsize = $totalSize; |
| 186 |
} |
| 187 |
|
| 188 |
$randomString = ''; |
| 189 |
for ($i = 0; $i < $bsize; $i++) { |
| 190 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand |
| 191 |
} |
| 192 |
|
| 193 |
$this->stream->writeStream($randomString); |
| 194 |
$totalSize -= $bsize; |
| 195 |
} |
| 196 |
return array("status" => "true"); |
| 197 |
} |
| 198 |
|
| 199 |
public function getCreateTableQueries($tables) { |
| 200 |
$resp = array(); |
| 201 |
foreach($tables as $table) { |
| 202 |
$tname = $table; |
| 203 |
$resp[$tname] = array("create" => $this->db->showTableCreate($table)); |
| 204 |
} |
| 205 |
return $resp; |
| 206 |
} |
| 207 |
|
| 208 |
public function checkTables($tables, $type) { |
| 209 |
$resp = array(); |
| 210 |
foreach($tables as $table) { |
| 211 |
$tname = $table; |
| 212 |
$resp[$tname] = array("status" => $this->db->checkTable($table, $type)); |
| 213 |
} |
| 214 |
return $resp; |
| 215 |
} |
| 216 |
|
| 217 |
public function describeTables($tables) { |
| 218 |
$resp = array(); |
| 219 |
foreach($tables as $table) { |
| 220 |
$tname = $table; |
| 221 |
$resp[$tname] = array("description" => $this->db->describeTable($table)); |
| 222 |
$resp[$tname]["primary_keys_index"] = $this->db->showTableIndex($table); |
| 223 |
} |
| 224 |
return $resp; |
| 225 |
} |
| 226 |
|
| 227 |
public function checkTablesExist($tables) { |
| 228 |
$resp = array(); |
| 229 |
foreach($tables as $table) { |
| 230 |
$tname = $table; |
| 231 |
$resp[$tname] = array("tblexists" => $this->db->isTablePresent($table)); |
| 232 |
} |
| 233 |
return $resp; |
| 234 |
} |
| 235 |
|
| 236 |
public function getTablesRowCount($tables) { |
| 237 |
$resp = array(); |
| 238 |
foreach($tables as $table) { |
| 239 |
$tname = $table; |
| 240 |
$resp[$tname] = array("count" => $this->db->rowsCount($table)); |
| 241 |
} |
| 242 |
return $resp; |
| 243 |
} |
| 244 |
|
| 245 |
public function getTablesKeys($tables) { |
| 246 |
$resp = array(); |
| 247 |
foreach($tables as $table) { |
| 248 |
$tname = $table; |
| 249 |
$resp[$tname] = array("keys" => $this->db->tableKeys($table)); |
| 250 |
} |
| 251 |
return $resp; |
| 252 |
} |
| 253 |
|
| 254 |
public function multiGetResult($queries) { |
| 255 |
$resp = array(); |
| 256 |
foreach($queries as $query) { |
| 257 |
array_push($resp, $this->db->getResult($query)); |
| 258 |
} |
| 259 |
return $resp; |
| 260 |
} |
| 261 |
|
| 262 |
public function process($request) { |
| 263 |
$db = $this->db; |
| 264 |
$params = $request->params; |
| 265 |
$stream_init_info = WPRStream::startStream($this->account, $request); |
| 266 |
|
| 267 |
if (array_key_exists('stream', $stream_init_info)) { |
| 268 |
$this->stream = $stream_init_info['stream']; |
| 269 |
switch ($request->method) { |
| 270 |
case "gettbls": |
| 271 |
$resp = array("tables" => $db->showTables()); |
| 272 |
break; |
| 273 |
case "tblstatus": |
| 274 |
$resp = array("statuses" => $db->showTableStatus()); |
| 275 |
break; |
| 276 |
case "tablekeys": |
| 277 |
$table = $params['table']; |
| 278 |
$resp = array("table_keys" => $db->tableKeys($table)); |
| 279 |
break; |
| 280 |
case "describetable": |
| 281 |
$table = $params['table']; |
| 282 |
$resp = array("table_description" => $db->describeTable($table)); |
| 283 |
$resp["primary_keys_index"] = $db->showTableIndex($table); |
| 284 |
break; |
| 285 |
case "checktable": |
| 286 |
$table = $params['table']; |
| 287 |
$type = $params['type']; |
| 288 |
$resp = array("status" => $db->checkTable($table, $type)); |
| 289 |
break; |
| 290 |
case "repairtable": |
| 291 |
$table = $params['table']; |
| 292 |
$resp = array("status" => $db->repairTable($table)); |
| 293 |
break; |
| 294 |
case "gettcrt": |
| 295 |
$table = $params['table']; |
| 296 |
$resp = array("create" => $db->showTableCreate($table)); |
| 297 |
break; |
| 298 |
case "tblskys": |
| 299 |
$tables = $params['tables']; |
| 300 |
$resp = $this->getTablesKeys($tables); |
| 301 |
break; |
| 302 |
case "getmlticrt": |
| 303 |
$tables = $params['tables']; |
| 304 |
$resp = $this->getCreateTableQueries($tables); |
| 305 |
break; |
| 306 |
case "desctbls": |
| 307 |
$tables = $params['tables']; |
| 308 |
$resp = $this->describeTables($tables); |
| 309 |
break; |
| 310 |
case "mltirwscount": |
| 311 |
$tables = $params['tables']; |
| 312 |
$resp = $this->getTablesRowCount($tables); |
| 313 |
break; |
| 314 |
case "chktabls": |
| 315 |
$tables = $params['tables']; |
| 316 |
$type = $params['type']; |
| 317 |
$resp = $this->checkTables($tables, $type); |
| 318 |
break; |
| 319 |
case "chktablsxist": |
| 320 |
$tables = $params['tables']; |
| 321 |
$resp = $this->checkTablesExist($tables); |
| 322 |
break; |
| 323 |
case "getrowscount": |
| 324 |
$table = $params['table']; |
| 325 |
$resp = array("count" => $db->rowsCount($table)); |
| 326 |
break; |
| 327 |
case "gettablecontent": |
| 328 |
$result = array(); |
| 329 |
$table = $params['table']; |
| 330 |
$fields = $params['fields']; |
| 331 |
$filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 332 |
$limit = intval($params['limit']); |
| 333 |
$offset = intval($params['offset']); |
| 334 |
$pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 335 |
$result['timestamp'] = time(); |
| 336 |
$result['tablename'] = $table; |
| 337 |
$rows = $db->getTableContent($table, $fields, $filter, $limit, $offset); |
| 338 |
$srows = sizeof($rows); |
| 339 |
if (!empty($pkeys) && $srows > 0) { |
| 340 |
$end_row = end($rows); |
| 341 |
$result['last_ids'] = $this->getLastID($pkeys, $end_row); |
| 342 |
} |
| 343 |
$result["rows"] = $rows; |
| 344 |
$resp = $result; |
| 345 |
break; |
| 346 |
case "multitablecontent": |
| 347 |
$tableParams = $params['table_params']; |
| 348 |
$resp = array(); |
| 349 |
foreach($tableParams as $tableParam) { |
| 350 |
$result = array(); |
| 351 |
$identifier = $tableParam['identifier']; |
| 352 |
$table = $tableParam['table']; |
| 353 |
$tname = $tableParam['tname']; |
| 354 |
$fields = $tableParam['fields']; |
| 355 |
$filter = (array_key_exists('filter', $tableParam)) ? $tableParam['filter'] : ""; |
| 356 |
$limit = $tableParam['limit']; |
| 357 |
$offset = $tableParam['offset']; |
| 358 |
$pkeys = (array_key_exists('pkeys', $tableParam)) ? $tableParam['pkeys'] : array(); |
| 359 |
$result['timestamp'] = time(); |
| 360 |
$result['table_name'] = $tname; |
| 361 |
$rows = $db->getTableContent($table, $fields, $filter, $limit, $offset); |
| 362 |
$srows = sizeof($rows); |
| 363 |
if (!empty($pkeys) && $srows > 0) { |
| 364 |
$end_row = end($rows); |
| 365 |
$result['last_ids'] = $this->getLastID($pkeys, $end_row); |
| 366 |
} |
| 367 |
$result["rows"] = $rows; |
| 368 |
$result["size"] = $srows; |
| 369 |
$resp[$identifier] = $result; |
| 370 |
} |
| 371 |
break; |
| 372 |
case "tableinfo": |
| 373 |
$table = $params['table']; |
| 374 |
$offset = intval($params['offset']); |
| 375 |
$limit = intval($params['limit']); |
| 376 |
$bsize = intval($params['bsize']); |
| 377 |
$filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 378 |
$tname = $params['tname']; |
| 379 |
$pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 380 |
$cursor_mode = (array_key_exists('cursor_mode', $params)) ? $params['cursor_mode'] : false; |
| 381 |
if ($cursor_mode === true) { |
| 382 |
$resp = $this->getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false, $cursor_mode); |
| 383 |
} else { |
| 384 |
$resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false); |
| 385 |
} |
| 386 |
break; |
| 387 |
case "getmulttables": |
| 388 |
$result = array(); |
| 389 |
$tableParams = $params['table_params']; |
| 390 |
$resp = array(); |
| 391 |
foreach($tableParams as $tableParam) { |
| 392 |
$table = $tableParam['table']; |
| 393 |
$tname = $tableParam['tname']; |
| 394 |
$filter = (array_key_exists('filter', $tableParam)) ? $tableParam['filter'] : ""; |
| 395 |
$limit = intval($tableParam['limit']); |
| 396 |
$offset = intval($tableParam['offset']); |
| 397 |
$bsize = intval($tableParam['bsize']); |
| 398 |
$pkeys = (array_key_exists('pkeys', $tableParam)) ? $tableParam['pkeys'] : array(); |
| 399 |
$resp[$tname] = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true); |
| 400 |
} |
| 401 |
break; |
| 402 |
case "uploadrows": |
| 403 |
$table = $params['table']; |
| 404 |
$offset = intval($params['offset']); |
| 405 |
$limit = intval($params['limit']); |
| 406 |
$bsize = intval($params['bsize']); |
| 407 |
$filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 408 |
$tname = $params['tname']; |
| 409 |
$pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 410 |
$cursor_mode = (array_key_exists('cursor_mode', $params)) ? $params['cursor_mode'] : false; |
| 411 |
if ($cursor_mode === true) { |
| 412 |
$resp = $this->getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true, $cursor_mode); |
| 413 |
} else { |
| 414 |
$resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true); |
| 415 |
} |
| 416 |
break; |
| 417 |
case "tblexists": |
| 418 |
$resp = array("tblexists" => $db->isTablePresent($params['table'])); |
| 419 |
break; |
| 420 |
case "crttbl": |
| 421 |
$usedbdelta = array_key_exists('usedbdelta', $params); |
| 422 |
$resp = array("crttbl" => $db->createTable($params['query'], $params['table'], $usedbdelta)); |
| 423 |
break; |
| 424 |
case "drptbl": |
| 425 |
$resp = array("drptbl" => $db->dropBVTable($params['table'])); |
| 426 |
break; |
| 427 |
case "trttbl": |
| 428 |
$resp = array("trttbl" => $db->truncateBVTable($params['table'])); |
| 429 |
break; |
| 430 |
case "altrtbl": |
| 431 |
$resp = array("altrtbl" => $db->alterBVTable($params['query'], $params['query'])); |
| 432 |
break; |
| 433 |
case "mltigtrslt": |
| 434 |
$resp = array("mltigtrslt" => $this->multiGetResult($params['queries'])); |
| 435 |
break; |
| 436 |
case "mltiqrsstrm": |
| 437 |
$queries = $params['queries']; |
| 438 |
$result = array(); |
| 439 |
foreach ($queries as $qparams) { |
| 440 |
$identifier = $qparams['identifier']; |
| 441 |
$query = $qparams['query']; |
| 442 |
$pkeys = (array_key_exists('pkeys', $qparams)) ? $qparams['pkeys'] : array(); |
| 443 |
array_push($result, $this->streamQueryResult($identifier, $query, $pkeys)); |
| 444 |
} |
| 445 |
$resp = array('mltqrsstrm' => $result); |
| 446 |
break; |
| 447 |
case "getrndmdata": |
| 448 |
$resp = array("getrndmdata" => $this->getRandomData($params['size'], $params['batch_size'])); |
| 449 |
break; |
| 450 |
case "tbls": |
| 451 |
$resp = array(); |
| 452 |
|
| 453 |
if (array_key_exists('truncate', $params)) |
| 454 |
$resp['truncate'] = $db->truncateTables($params['truncate']); |
| 455 |
|
| 456 |
if (array_key_exists('drop', $params)) |
| 457 |
$resp['drop'] = $db->dropTables($params['drop']); |
| 458 |
|
| 459 |
if (array_key_exists('create', $params)) |
| 460 |
$resp['create'] = $db->createTables($params['create']); |
| 461 |
|
| 462 |
if (array_key_exists('alter', $params)) |
| 463 |
$resp['alter'] = $db->alterTables($params['alter']); |
| 464 |
|
| 465 |
break; |
| 466 |
default: |
| 467 |
$resp = false; |
| 468 |
} |
| 469 |
$end_stream_info = $this->stream->endStream(); |
| 470 |
if (!empty($end_stream_info) && is_array($resp)) { |
| 471 |
$resp = array_merge($resp, $end_stream_info); |
| 472 |
} |
| 473 |
} else { |
| 474 |
$resp = $stream_init_info; |
| 475 |
} |
| 476 |
return $resp; |
| 477 |
} |
| 478 |
} |
| 479 |
endif; |
| 480 |
|