namespace MO_CAW\PRO\Functionality;
use MO_CAW\PRO\Utils;
use MO_CAW\Common\DB_Utils;
use MO_CAW\PRO\Constants;
use MO_CAW\Common\Functionality\SQL_API_Creation as Common_SQL_API_Creation;
class SQL_API_Creation extends Common_SQL_API_Creation
{
public function __construct()
{
$yO = current_action();
if (!(Constants::REST_API_INIT_HOOK === $yO)) {
goto sk;
}
$this->pro_rest_init_functionalities();
sk:
}
private function pro_rest_init_functionalities()
{
$Pe = DB_Utils::get_configuration(array("type" => Constants::SQL_ENDPOINT));
foreach ($Pe as $RJ) {
parent::register_rest_routes($RJ, $this);
Ts:
}
CX:
}
public function authenticate_request($uV)
{
$nm = array("status" => Constants::ERROR, "code" => 403, "error" => Constants::FORBIDDEN, "error_description" => Constants::FEATURE_NOT_SUPPORTED);
if (Utils::check_plan_capabilities(Constants::ADVANCE_API_CREATION_PLAN_NAME)) {
goto JM;
}
wp_send_json_error($nm);
JM:
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"];
$tN = !empty($Xi["response"]["response_content"]["success"]) ? json_decode($Xi["response"]["response_content"]["success"], true) : false;
if ($A6["is_enabled"]) {
goto UZ;
}
$nm = array("status" => Constants::ERROR, "code" => 403, "error" => Constants::ENDPOINT_DEACTIVATED, "error_description" => Constants::API_DISABLED);
wp_send_json($nm, 403);
UZ:
$lF = $Xi["sql_queries"];
if (\strtoupper(Constants::HTTP_GET) === $we || \strtoupper(Constants::HTTP_DELETE) === $we) {
goto tV;
}
if (\strtoupper(Constants::HTTP_POST) === $we || \strtoupper(Constants::HTTP_PUT) === $we) {
goto Cq;
}
$n9 = array("error" => "invalid_method", "error_description" => "Requested method is not registered using CUSTOM API for WP plugin.");
wp_send_json($n9, 400);
goto AX;
tV:
$n9 = array("error" => Constants::INVALID_FORMAT, "error_description" => "Required arguments are missing or passed in the correct format.");
$T3 = $this->run_sql_query($cQ, $lF, $n9, $we);
if (!(Constants::ERROR === $T3["status"])) {
goto Xp;
}
$tN = !empty($Xi["response"]["response_content"]["error"]) ? json_decode($Xi["response"]["response_content"]["error"], true) : false;
Xp:
Utils::send_custom_api_response($T3, $tN);
goto AX;
Cq:
$n9 = array("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 pr;
}
$nm = array("status" => Constants::ERROR, "code" => 400, "error" => Constants::INVALID_FORMAT, "error_description" => "Request body cannot be empty for " . $we . " method");
wp_send_json($nm, 400);
pr:
$T3 = $this->run_sql_query($q8, $lF, $n9, $we);
if (!(Constants::ERROR === $T3["status"])) {
goto uL;
}
$tN = !empty($Xi["response"]["response_content"]["error"]) ? json_decode($Xi["response"]["response_content"]["error"], true) : false;
uL:
Utils::send_custom_api_response($T3, $tN);
AX:
}
private function run_sql_query($iY, $SB, $n9, $we)
{
$am = implode("|-|-|", $SB);
$am = parent::replace_dynamic_values($am, $iY, $n9);
$SB = explode("|-|-|", $am);
$vs = '';
$p0 = array();
$n9 = array("error" => "query_fail", "error_description" => "following query's execution failed", "query" => array());
foreach ($SB as $kl) {
$vs = parent::execute_query($kl, $we);
if (!(Constants::ERROR === $vs["status"])) {
goto Mz;
}
$p0 = array();
$p0["error_message"] = $vs["data"];
$p0["query"] = $kl;
array_push($n9["query"], $p0);
Mz:
Se:
}
vh:
if (empty($n9["query"])) {
goto rS;
}
wp_send_json($n9, 400);
goto wB;
rS:
$T3["status"] = Constants::SUCCESS;
$T3["status_code"] = 200;
return $vs;
wB:
}
}
© 2023 Quttera Ltd. All rights reserved.