Files
WrenchBoradWeb/www-api/app/Controllers/Bko.php
T
CHIEFSOFT\ameye 21d339daaf flatten
2023-11-26 18:33:00 -05:00

127 lines
4.0 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\ResultFormatter;
use CodeIgniter\API\ResponseTrait;
class Bko extends BaseController
{
use ResponseTrait;
protected $request;
public function __construct()
{
$this->request = $request = \Config\Services::request();
}
public function index()
{
return [];
}
public function apigate()
{
$endpoints = array(
'generics' => array('POST')
);
$call_backend = true; // sometimes we need to overwite the call to the extenstion API
$local_out = []; // use local out to send output when the result is not from the extenstion
$ret = -1;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
header('Content-type: application/json');
if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
exit();
}
$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 );
// $endpoint = strtolower(str_replace('/svs/bko/', '', strtok($_SERVER['REQUEST_URI'], '?')));
log_message('critical', "BkoApi Path GATE 001 endpoint = $endpoint");
$id = 0; // update, get & delete actions require ID
if (substr($endpoint, 0, 19) == 'gettransportrequest' || substr($endpoint, 0, 13) == 'updateprofile') {
$endpoint = strtok($endpoint, '/');
$id = strtok('/');
}
if (!isset($endpoints[$endpoint])) {
header('HTTP/1.1 400 Bad Request');
header('Status: 400 Bad Request');
echo "{\"status\":\"Invalid endpoint url WRB\"}";
exit();
}
$methods = $endpoints[$endpoint];
if (array_search($_SERVER['REQUEST_METHOD'], $methods) === false) {
header('HTTP/1.1 405 Method Not Allowed');
header('Status: 405 Method Not Allowed');
echo "{\"status\":\"Invalid request method\"}";
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$in = $this->flatten(json_decode(file_get_contents('php://input'), true));
}
if ($_SERVER["REQUEST_METHOD"] == "PUT") {
parse_str(file_get_contents('php://input'), $in);
}
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$in = $_GET;
}
$in["loc"] = $_SERVER["REMOTE_ADDR"];
switch ($endpoint) {
case 'generics':
break;
}
$in["pid"] = 100;
$out = array();
if ( $call_backend == true){
$wrenchboard = new \App\Models\BackendModel();
$ret = $wrenchboard->wrenchboard_api($in, $out);
$out['internal_return'] = $ret; // this is reserved array parameter - to be captured and received before you use the out array()
}
else
{
$out = $local_out;
}
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
echo json_encode(( new ResultFormatter() )->processOutJson($in, $out));
exit();
}
function flatten($data, $parentkey = "") {
$result = array();
foreach ($data as $key => $val) {
if (is_array($val)) {
$result = array_merge($result, flatten($val, $parentkey . $key . "_"));
} else {
$result[$parentkey . $key] = $val;
}
}
return $result;
}
}