From 9651fd0a4042da73edbe560ad4cbaa183180dad2 Mon Sep 17 00:00:00 2001 From: "CHIEFSOFT\\ameye" Date: Sat, 24 Feb 2024 14:17:02 -0500 Subject: [PATCH] Wrench Jobs --- www-api/app/Controllers/BaseController.php | 11 ++ www-api/app/Controllers/WrenchApi.php | 15 +-- www-api/app/Controllers/WrenchJobs.php | 114 +++++++++++++++++++++ 3 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 www-api/app/Controllers/WrenchJobs.php diff --git a/www-api/app/Controllers/BaseController.php b/www-api/app/Controllers/BaseController.php index 882ba5db..e08e30b5 100644 --- a/www-api/app/Controllers/BaseController.php +++ b/www-api/app/Controllers/BaseController.php @@ -52,6 +52,17 @@ abstract class BaseController extends Controller $this->cache_tag = $this->getSiteConfigurations("system.cache_tag"); } + public function getIpData(){ + //$ip_loc="0.0.0.0"; + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + $ip_loc = $_SERVER['HTTP_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $ip_loc = $_SERVER['HTTP_X_FORWARDED_FOR']; + } else { + $ip_loc = $_SERVER['REMOTE_ADDR']; + } + return $ip_loc; + } public function saveCache($cacheKey,$data,$timeLine=5000){ $cacheKey = $this->cache_tag."-".$cacheKey; // $cache = \Config\Services::cache(); diff --git a/www-api/app/Controllers/WrenchApi.php b/www-api/app/Controllers/WrenchApi.php index 448f7b27..64482623 100644 --- a/www-api/app/Controllers/WrenchApi.php +++ b/www-api/app/Controllers/WrenchApi.php @@ -532,14 +532,15 @@ class WrenchApi extends BaseController header('content-type: application/json; charset=utf-8'); $method = $_SERVER['REQUEST_METHOD']; - if (!empty($_SERVER['HTTP_CLIENT_IP'])) { - $ip_loc = $_SERVER['HTTP_CLIENT_IP']; - } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $ip_loc = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else { - $ip_loc = $_SERVER['REMOTE_ADDR']; - } +// if (!empty($_SERVER['HTTP_CLIENT_IP'])) { +// $ip_loc = $_SERVER['HTTP_CLIENT_IP']; +// } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { +// $ip_loc = $_SERVER['HTTP_X_FORWARDED_FOR']; +// } else { +// $ip_loc = $_SERVER['REMOTE_ADDR']; +// } + $ip_loc = $this->getIpData(); if ($method == "OPTIONS") { header("HTTP/1.1 200 OK CORS"); diff --git a/www-api/app/Controllers/WrenchJobs.php b/www-api/app/Controllers/WrenchJobs.php new file mode 100644 index 00000000..399e2975 --- /dev/null +++ b/www-api/app/Controllers/WrenchJobs.php @@ -0,0 +1,114 @@ +request = $request = \Config\Services::request(); + } + public function index() + { + return []; + } + + public function apigate(){ + log_message('critical', "0001"); + header('Access-Control-Allow-Origin: *'); + log_message('critical', "MainApi Path GATE 001"); + $call_backend = true; + + header("Access-Control-Allow-Headers: Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token"); + log_message('critical', "0003"); + header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS"); + header("Access-Control-Allow-Credentials: true"); + header("Access-Control-Max-Age: 3600"); + header('content-type: application/json; charset=utf-8'); + $method = $_SERVER['REQUEST_METHOD']; + + $ip_loc = $this->getIpData(); + + + + if ($method == "OPTIONS") { + header("HTTP/1.1 200 OK CORS"); + log_message('critical', " WrenchJobs()-> OPTIONS DIE*****" ); + die(); + } + + //$request = service('request'); + // what is the endpoint + $uri = urldecode(current_url(true)); + $findme = '?'; + $pos = strpos($uri, $findme); + if ($pos > 5) { + $uri = substr($uri, 0, $pos); + } + log_message('critical', "API-GATE URI -> ".$uri ); + $pieces = explode('/', $uri); + $psc = count($pieces); + + $endpoint = $psc > 0 ? $pieces[$psc - 1] : ''; + log_message('critical', "Enpoint-> ".$endpoint ); + + $endpoints = $this->endPointList(); + $out = array(); + $res1 = []; + if (array_key_exists($endpoint, $endpoints)) { + } else { + http_response_code(404); + // tell the user product does not exist + echo json_encode([ + 'message' => 'Endpoint not found.', + 'URI' => $uri, + ]); + } + + $current_env = $this->getSiteConfigurations("system.live"); + $primary_image_sever = $this->getSiteConfigurations("system.primary_image_sever"); + $server_tag = $this->getSiteConfigurations("system.server_tag"); + + // echo "EXYTACT INPUT DATA HERE"; + $raw_json = file_get_contents('php://input'); + $raw_array = json_decode($raw_json, true); + $local_out =[]; + if ($_SERVER['REQUEST_METHOD'] == 'GET') { + log_message('critical', "Enpoint LOC2 HERE -> ".$endpoint ); + $get_param = $_GET['reqData'] ?? null; + $raw_array = ($get_param!=null) ? json_decode($get_param, true):[]; + } + + + $in["loc"] = $_SERVER["REMOTE_ADDR"]; + $out = array(); + + + $res1 = []; + if (!array_key_exists($endpoint, $this->sessionExcludedList())) { + // TOKEN VERIFICATION WILL GAPPEN + } + + if ( $call_backend == true && $in["action"] !='' ){ + $wrenchboard = new \App\Models\BackendModel(); + $ret = $wrenchboard->wrenchboard_api($in, $out); + $out['internal_return'] = $ret; + } + else + { + $out = $local_out; + } + + $this->doCacheStep($in, $out); + $final_out = ( new \App\Models\ResultFormatter() )->processOutJson($in, $out); + $final_out["environment"] = $current_env + 0; // force convert to interger = $this->getSiteConfigurations("system.live"); + $final_out["session_image_server"] = $primary_image_sever; // ( $final_out["environment"] > 0 )? 'https://apigate.nebula.g1.wrenchboard.com/en/wrench/api/v1/getmedia/' : "https://apigate.lotus.g1.wrenchboard.com/en/wrench/api/v1/getmedia/"; + $final_out["server_tag"] = $server_tag; + $final_out["language"] = "en"; + $final_out["ip_loc"] = $ip_loc; + return json_encode( $final_out ); + } + + + +} \ No newline at end of file