From 44c80bfe3855cd23fcec098ce8c366b706238709 Mon Sep 17 00:00:00 2001 From: "CHIEFSOFT\\ameye" Date: Wed, 24 Sep 2025 08:03:34 -0400 Subject: [PATCH] clean up code --- app/Controllers/BaseController.php | 150 ++++++++++++++++------------- 1 file changed, 82 insertions(+), 68 deletions(-) diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 4a05446..42cb35b 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -56,18 +56,20 @@ abstract class BaseController extends Controller $this->db = \Config\Database::connect(); // E.g.: $this->session = service('session'); } - public function APIcall($method, $url, $data) { + + public function APIcall($method, $url, $data) + { // $curl = curl_init(); $curl = curl_init($url); switch ($method) { case "GET": $params2 = ''; - foreach($data as $key2=>$value2) - $params2 .= $key2.'='.$value2.'&'; + foreach ($data as $key2 => $value2) + $params2 .= $key2 . '=' . $value2 . '&'; $params2 = trim($params2, '&'); - $url = $url.'?'.$params2;// add param to URL - log_message('critical', "API URL FINAL =>".$url ); + $url = $url . '?' . $params2;// add param to URL + log_message('critical', "API URL FINAL =>" . $url); //curl_setopt($curl, CURLOPT_FRESH_CONNECT, true); //curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); @@ -96,72 +98,84 @@ abstract class BaseController extends Controller curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $result = curl_exec($curl); - if(!$result) { + if (!$result) { echo("Connection failure!"); } curl_close($curl); return json_decode($result, true); } - public function ansibleLogin():string { - - $result = "Unhandled exception"; - $base_url = $_ENV['ANSIBLE_AUTOMATION_SERVER_LOCATION']; - // $base_url = "http://172.16.4.90:3000"; - $cookies = tempnam('/tmp','cookie.txt'); - - # 1. Login - $url = $base_url . "/api/auth/login"; - $ch = curl_init( $url ); - # Setup request to send json via POST. - $payload = json_encode( array( "auth"=> "admin", "password" => "may12002" ) ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); - curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); - # Return response instead of printing. - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - # Fetch the headers, not the body content: - //curl_setopt($ch, CURLOPT_HEADER, true); // we want headers - //curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body - # Cookie jar - curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); - curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); - - # Send request. - $result = curl_exec($ch); - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $result = 'HTTP code: ' . $httpcode; - - curl_close($ch); - - if ($httpcode != "204") { - return "Login failed! $result"; - } - - # 2. Get token - $url = $base_url . "/api/user/tokens"; - $ch = curl_init( $url ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); - curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); - $result = curl_exec($ch); - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - + public function ansibleLogin(): string + { $token = null; - $arr = json_decode($result, true); - foreach ($arr as $item) { - if ($item["expired"]===false) { - $token = $item["id"]; - break; + try { + // Code that might throw an exception + $result = "Unhandled exception"; + $base_url = $_ENV['ANSIBLE_AUTOMATION_SERVER_LOCATION']; + // $base_url = "http://172.16.4.90:3000"; + $cookies = tempnam('/tmp', 'cookie.txt'); + + # 1. Login + $url = $base_url . "/api/auth/login"; + log_message('critical', "***** ***** Provision LOGIN URL:: " . $url); + + $ch = curl_init($url); + # Setup request to send json via POST. + $payload = json_encode(array("auth" => "admin", "password" => "may12002")); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); + # Return response instead of printing. + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + # Fetch the headers, not the body content: + //curl_setopt($ch, CURLOPT_HEADER, true); // we want headers + //curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body + # Cookie jar + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); + + # Send request. + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $result = 'HTTP code: ' . $httpcode; + + curl_close($ch); + + if ($httpcode != "204") { + return "Login failed! $result"; } + + # 2. Get token + $url = $base_url . "/api/user/tokens"; + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $token = null; + $arr = json_decode($result, true); + foreach ($arr as $item) { + if ($item["expired"] === false) { + $token = $item["id"]; + break; + } + } + $result = "Found token: $token"; + # Print response. + + } catch (\Exception $e) { + log_message('critical', "***** ***** Provision LOGIN FAILED :: " . $e->getMessage()); } - $result = "Found token: $token"; - # Print response. + + return $token; } - public function ansibleProvision($ansibleToken,$params){ + public function ansibleProvision($ansibleToken, $params) + { // log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ".$ansibleToken); // log_message('critical', "***** ***** Provision CALL:: ansibleProvision() Token ". serialize( $params)); @@ -172,19 +186,19 @@ abstract class BaseController extends Controller $PROJECT_ID = $_ENV['ANSIBLE_AUTOMATION_PROJECT_ID']; $url = $base_url . "/api/project/$PROJECT_ID/tasks"; - log_message('critical', "***** ***** Provision CALL URL:: ". $url); + log_message('critical', "***** ***** Provision CALL URL:: " . $url); $headers = [ 'Content-Type:application/json', 'Accept: application/json', 'Authorization: Bearer ' . $ansibleToken ]; - $ch = curl_init( $url ); - $payload = json_encode( $params ); - // $payload = json_encode( array( "template_id"=> 1, "debug" => false, "dry_run" => false, "playbook" => "first-playbook.yml", "environment" => "{}" ) ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); - curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + $ch = curl_init($url); + $payload = json_encode($params); + // $payload = json_encode( array( "template_id"=> 1, "debug" => false, "dry_run" => false, "playbook" => "first-playbook.yml", "environment" => "{}" ) ); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result .= "\n" . 'Launch task HTTP code: ' . $httpcode; @@ -193,9 +207,9 @@ abstract class BaseController extends Controller # 4. Get tasks $url = $base_url . "/api/project/$PROJECT_ID/tasks"; - $ch = curl_init( $url ); - curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result .= "\n" . 'Get tasks HTTP code: ' . $httpcode;