m3 Security Fixes

This commit is contained in:
Mark Gregory
2026-09-17 10:37:56 +01:00
parent a094477840
commit 1644efed9c
5 changed files with 356 additions and 821 deletions
@@ -5,101 +5,108 @@ namespace Modules\M3\Http\Controllers;
use Artisan;
use Config;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Log;
class M3CallCloseAPIController extends Controller
{
public static function index(Request $request)
public static function index(Request $request): JsonResponse
{
ini_set('max_execution_time', Config::get('arrays.PHPTimeOut'));
ini_set('max_input_time', Config::get('arrays.PHPTimeOut'));
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOut'));
self::configureTimeouts();
$params = self::validatedParams($request);
if ($params === null) {
Log::channel('dailyCron')->error('Manifest CloseAPI: Manifest, company and warehouse must be defined.');
return self::errorResponse('Manifest CloseAPI: Manifest, company and warehouse must be defined.');
}
try {
$records = self::findOpenManifestRecords($params);
if ($records->isEmpty()) {
Log::channel('dailyCron')->info('Manifest CloseAPI: No records found in ICM705M1.');
// Get the Calling User
// Get URL PARAMS
$urlParams = count($request->all()) > 0 ? $request->all() : '';
// dd($urlParams);
$SHUSER = isset($urlParams['SHUSER']) ? $urlParams['SHUSER'] : '';
$MANIFEST = isset($urlParams['MANIFEST']) ? $urlParams['MANIFEST'] : '';
$APCO = isset($urlParams['APCO']) ? $urlParams['APCO'] : '';
$APWHS = isset($urlParams['APWHS']) ? $urlParams['APWHS'] : '';
if ($MANIFEST != '' && $APCO != '' && $APWHS != '') { // $SHUSER!='' &&
// Log::channel('dailyCron')->info("Cron...Start CallCloseAPI...For user:".$SHUSER." Manifest#: ".$MANIFEST);
// Get Logged User CO & WH
// Get users from ICM428M
// $ICM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
// ->select('R428US','R428CO', 'R428WH')
// ->where('R428US','=',$SHUSER)
// ->first();
// $R428US = isset($ICM428M->R428US) ? $ICM428M->R428US : '';
// $USERCO = isset($ICM428M->R428CO) ? $ICM428M->R428CO : '';
// $USERWH = isset($ICM428M->R428WH) ? $ICM428M->R428WH : '';
// dump($R428US.':'.$USERCO.':'.$USERWH);
// Get Macpickers from ICM705M1
// SELECT APMANIFEST FROM ICM705M1 WHERE APSPARE2='' AND APCO='110' AND APWHS='321'
$ICM705M1 = DB::connection(config('m3.connection', 'pgsql'))->table('icm705m1')
->select('APMANIFEST', 'APCO', 'APWHS')
->where('APSPARE2', '')
->where('APCO', $APCO)
->where('APWHS', $APWHS)
->where('APMANIFEST', '=', $MANIFEST)
->whereRaw('EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - APLDCLOZ)) / 60 > 15')
->orderBy('APMANIFEST')
->get();
// dump($ICM705M1);
if (count($ICM705M1) > 0) {
// Loop the Results from $ICM705M1
foreach ($ICM705M1 as $key => $ICM705M1R) {
// Send CO and WHS to CallCloseAPI
$USERCO = isset($ICM705M1R->APCO) ? $ICM705M1R->APCO : '';
$USERWH = isset($ICM705M1R->APWHS) ? $ICM705M1R->APWHS : '';
$callCloseShipment = Artisan::call('command:CallCloseAPI', ['user' => $SHUSER, 'manifest' => $ICM705M1R->APMANIFEST,
'co' => $USERCO, 'whs' => $USERWH]);
// dump($callCloseShipment);
if ($callCloseShipment == 1) {
return response()->json([
'code' => '200',
'status' => 'Success',
'message' => 'Manifest Closing submitted',
]);
} else {
return response()->json([
'code' => '206',
'status' => 'Failure',
'message' => 'Manifest Closing Failed',
]);
}
}
} else {
Log::channel('dailyCron')->info('Manifest CloseAPI: No records found in ICM705M1...');
return response()->json([
'code' => '206',
'status' => 'Error',
'message' => 'Manifest CloseAPI: No records found in ICM705M1...',
]);
}
} else {
Log::channel('dailyCron')->error('Manifest CloseAPI: A User and Manifest must be defined...');
return response()->json([
'code' => '206',
'status' => 'Error',
'message' => 'Manifest CloseAPI: A User and Manifest must be defined...',
]);
return self::errorResponse('Manifest CloseAPI: No records found in ICM705M1.');
}
} catch (\Throwable $th) {
Log::error($th);
return self::submitManifest($records->first(), $params['SHUSER']);
} catch (\Throwable $exception) {
Log::error($exception);
return response()->json([
'code' => '500',
'status' => 'Error',
'message' => 'Manifest Closing failed unexpectedly.',
], 500);
}
}
private static function configureTimeouts(): void
{
$timeout = (string) Config::get('arrays.PHPTimeOut');
ini_set('max_execution_time', $timeout);
ini_set('max_input_time', $timeout);
ini_set('default_socket_timeout', $timeout);
}
private static function validatedParams(Request $request): ?array
{
$params = [
'SHUSER' => trim((string) $request->input('SHUSER', '')),
'MANIFEST' => trim((string) $request->input('MANIFEST', '')),
'APCO' => trim((string) $request->input('APCO', '')),
'APWHS' => trim((string) $request->input('APWHS', '')),
];
if ($params['MANIFEST'] === '' || $params['APCO'] === '' || $params['APWHS'] === '') {
return null;
}
return $params;
}
private static function findOpenManifestRecords(array $params)
{
$connection = config('m3.connection', 'pgsql');
if (! Schema::connection($connection)->hasTable('icm705m1')) {
return collect();
}
return DB::connection($connection)->table('icm705m1')
->select('APMANIFEST', 'APCO', 'APWHS')
->where('APSPARE2', '')
->where('APCO', $params['APCO'])
->where('APWHS', $params['APWHS'])
->where('APMANIFEST', $params['MANIFEST'])
->whereRaw('EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - APLDCLOZ)) / 60 > 15')
->orderBy('APMANIFEST')
->get();
}
private static function submitManifest(object $record, string $user): JsonResponse
{
$exitCode = Artisan::call('command:CallCloseAPI', [
'user' => $user,
'manifest' => $record->APMANIFEST ?? '',
'co' => $record->APCO ?? '',
'whs' => $record->APWHS ?? '',
]);
if ($exitCode === 1) {
return response()->json([
'code' => '200',
'status' => 'Success',
'message' => 'Manifest Closing submitted',
]);
}
return self::errorResponse('Manifest Closing Failed');
}
private static function errorResponse(string $message): JsonResponse
{
return response()->json(['code' => '206', 'status' => 'Error', 'message' => $message]);
}
}