namespace MO_CAW\Common\Functionality;
use MO_CAW\Common\Utils;
use MO_CAW\Common\DB_Utils;
use MO_CAW\Common\Constants;
class SQL_API_Creation
{
public function __construct()
{
$yO = current_action();
if (!(Constants::REST_API_INIT_HOOK === $yO)) {
goto Z6;
}
$this->rest_init_functionalities();
Z6:
}
protected function rest_init_functionalities()
{
$Pe = DB_Utils::get_configuration(array("type" => Constants::SQL_ENDPOINT));
if (!isset($Pe[0])) {
goto ci;
}
$this->register_rest_routes($Pe[0], $this);
ci:
}
protected function register_rest_routes($RJ, $wF)
{
$Qi = $RJ["namespace"];
$PG["endpoint_configuration"] = $RJ;
register_rest_route($Qi, $RJ["connection_name"], array("methods" => \strtoupper($RJ["method"]), "callback" => array($wF, "sql_endpoint_callback"), "args" => $PG, "user" => wp_get_current_user(), "permission_callback" => array($wF, "authenticate_request")));
}
public function authenticate_request($uV)
{
return API_Security::authorize_custom_api_request($uV);
}
public function sql_endpoint_callback($uV)
{
$xz = $uV->get_attributes();
$cQ = $uV->get_params();
$q8 = $uV->get_body();
$Ug = $uV->get_headers();
$we = $uV->get_method();
$A6 = $xz["args"]["endpoint_configuration"];
$Xi = $A6["configuration"];
$lF = $Xi["sql_queries"][0];
$tN = !empty($Xi["response"]["response_content"]["success"]) ? json_decode($Xi["response"]["response_content"]["success"], true) : false;
if ($A6["is_enabled"]) {
goto kW;
}
$nm = array("status" => Constants::ERROR, "code" => 403, "error" => Constants::ENDPOINT_DEACTIVATED, "error_description" => Constants::API_DISABLED);
wp_send_json($nm, 403);
kW:
if (\strtoupper(Constants::HTTP_GET) === $we || \strtoupper(Constants::HTTP_DELETE) === $we) {
goto ZG;
}
if (\strtoupper(Constants::HTTP_POST) === $we || \strtoupper(Constants::HTTP_PUT) === $we) {
goto o8;
}
$n9 = array("status" => Constants::ERROR, "code" => 400, "error" => Constants::INVALID_FORMAT, "error_description" => "Requested method is not registered using CUSTOM API for WP plugin.");
wp_send_json($n9, 400);
goto YE;
ZG:
$n9 = array("status" => Constants::ERROR, "code" => 400, "error" => Constants::INVALID_FORMAT, "error_description" => "Required arguments are missing or not passed in the correct format.");
$T3 = $this->run_sql_query($cQ, $lF, $n9, $we);
if (!(Constants::ERROR === $T3["status"])) {
goto EF;
}
$tN = !empty($Xi["response"]["response_content"]["error"]) ? json_decode($Xi["response"]["response_content"]["error"], true) : false;
EF:
Utils::send_custom_api_response($T3, $tN);
goto YE;
o8:
$n9 = array("status" => Constants::ERROR, "code" => 400, "error" => Constants::INVALID_FORMAT, "error_description" => "Required body parameters are missing or not passed in the correct format.");
$ll = $Ug["content_type"][0] ?? '';
$q8 = Utils::get_custom_api_curated_body($ll, $q8);
if (!empty($q8)) {
goto Dh;
}
wp_send_json($n9, 400);
Dh:
$T3 = $this->run_sql_query($q8, $lF, $n9, $we);
if (!(Constants::ERROR === $T3["status"])) {
goto Fg;
}
$tN = !empty($Xi["response"]["response_content"]["error"]) ? json_decode($Xi["response"]["response_content"]["error"], true) : false;
Fg:
Utils::send_custom_api_response($T3, $tN);
YE:
}
private function run_sql_query($iY, $lF, $n9, $we)
{
$lF = $this->replace_dynamic_values($lF, $iY, $n9);
return $this->execute_query($lF, $we);
}
protected function replace_dynamic_values($lF, $iY, $n9)
{
$Mn = "/{{[A-Za-z0-9-_]+}}/";
preg_match_all($Mn, $lF, $uE);
$Ql = array_unique($uE[0]);
$Oa = count($iY);
if (count($Ql) === $Oa) {
goto pM;
}
wp_send_json($n9, 400);
goto L6;
pM:
$Nm = 0;
oz:
if (!($Nm < $Oa)) {
goto hX;
}
$mx = ltrim($Ql[$Nm], "{{");
$mx = rtrim($mx, "}}");
if (isset($iY[$mx]) && null !== $iY[$mx]) {
goto ZT;
}
wp_send_json($n9, 400);
goto Sg;
ZT:
$ng = is_int($iY[$mx]) ? $iY[$mx] : (string) $iY[$mx];
$lF = str_replace($Ql[$Nm], $ng, $lF);
Sg:
qk:
$Nm++;
goto oz;
hX:
L6:
return $lF;
}
protected function execute_query($lF, $we)
{
global $wpdb;
$T3 = array();
if (empty($lF)) {
goto v9;
}
if (\strtoupper(Constants::HTTP_GET) === $we) {
goto na;
}
$T3["data"] = $wpdb->query($lF);
goto V0;
na:
$T3["data"] = $wpdb->get_results($lF);
V0:
v9:
if ($wpdb->last_error) {
goto gX;
}
$T3["status"] = Constants::SUCCESS;
$T3["status_code"] = 200;
goto IZ;
gX:
$T3["status"] = Constants::ERROR;
$T3["status_code"] = 400;
$T3["data"] = $wpdb->last_error;
IZ:
return $T3;
}
}
© 2023 Quttera Ltd. All rights reserved.