m3 Security Fixes

This commit is contained in:
Mark Gregory
2026-09-17 10:31:34 +01:00
parent c9856d58c1
commit a094477840
6 changed files with 251 additions and 379 deletions
@@ -25,124 +25,165 @@ class M3InventoryReport extends Controller
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'));
// dd($request->json()->all());
// Get the Calling User
$SHUSER = m3_user() !== '' ? Str::upper(m3_user()) : 'WEBUSER';
// Set SHSOURCE
$SHSOURCE = 'M3InventoryReport';
$M3Errors = '';
$DELIVERYNO = isset($DELIVERYNO) ? $DELIVERYNO : self::ZERO_PAD;
$user = Str::upper(trim((string) m3_user())) ?: 'WEBUSER';
$source = 'M3InventoryReport';
$warehouse = '';
$payload = [];
try {
// Get Json Contents
$jsonFile = count($request->json()->all()) > 0 ? $request->json()->all() : '';
if ($jsonFile != '') {
$WHS = isset($jsonFile['WHS']) ? $jsonFile['WHS'] : '';
$PART = isset($jsonFile['PART']) ? $jsonFile['PART'] : '';
$LOT = isset($jsonFile['LOT']) ? $jsonFile['LOT'] : '';
} else {
// code...
}
$input = $request->json()->all();
$warehouse = trim((string) ($input['WHS'] ?? ''));
$payload = [
'WHS' => $warehouse,
'PART' => trim((string) ($input['PART'] ?? '')),
'LOT' => trim((string) ($input['LOT'] ?? '')),
];
// Get Token
$tokenResponse = Helper::checkReceiveTokenV2();
$tokenData = $tokenResponse->getData();
if ($tokenData->code == '200') {
$IonToken = isset($tokenData->token) ? $tokenData->token : '';
} else {
$SHDATA = isset($tokenData->message) ? $tokenData->message : '';
// Write to Logs : Error
$data = ['SHMACPKR' => self::ZERO_PAD, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => $SHDATA, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
if ((string) ($tokenData->code ?? '') !== '200') {
self::logInventoryResponse(
self::ZERO_PAD,
(string) ($tokenData->message ?? ''),
$user,
$source,
$warehouse
);
return $tokenResponse;
}
// Call ionapi with Params
$endPointURL = env('ENDPOINTURL') . 'APIFLOWS/Sentinel/M3InventoryReport?ionapiRespStyle=sync';
$token = $IonToken;
$JSON = [
'WHS' => $WHS,
'PART' => $PART,
'LOT' => $LOT,
];
$response = Http::withHeaders([
'accept' => self::JSON_CONTENT_TYPE,
'Content-Type' => self::JSON_CONTENT_TYPE,
])
->withToken($token)
->timeout(Config::get('arrays.curlTimeOut'))
->post($endPointURL, $JSON);
$responseStatusCode = $response->getStatusCode();
$M3SessionId = $response->hasHeader('x-transaction-id') ? '-sessionID:' . $response->header('x-transaction-id') : '-sessionID';
if ($responseStatusCode == '200') {
// Check M3 Response for Errors
$M3Errors = '';
$responseJSON = $response->getBody()->getContents();
$JsonSanytized = Helper::sanityzeString($responseJSON);
} else {
$M3Errors = self::NO_M3_RESPONSE;
}
if ($M3Errors != '') {
// Write to Logs : Error
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => $responseStatusCode . $M3Errors . $M3SessionId, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
// Send Response
return response()->json([
'code' => '405',
'status' => 'error',
'message' => $M3Errors,
]);
} else {
// Write to Logs Out : Success
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => '', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => $responseStatusCode . $JsonSanytized . '-' . json_encode($JSON) . $M3SessionId, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE,
'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
return response()->json([
'code' => $responseStatusCode,
'status' => 'Success',
'message' => json_decode($JsonSanytized),
]);
}
} catch (\Throwable $th) {
// Log::error($th);
$getCurlErrors = str_contains($th->getMessage(), 'cURL error');
if ($getCurlErrors) {
// Write to Logs Out : Failed
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'Curl Error: Communication with M3 Timed Out' . ' M3-JSN-out' . $JSON, 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE, 'SHCO' => '', 'SHWHS' => $WHS];
Helper::logToICM699M($data);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => 'Curl Error: Communication with M3 Timed Out.',
]);
} else {
// Write to Logs Out : Error
$data = ['SHMACPKR' => $DELIVERYNO, 'SHDT' => date('Y-m-d'), 'SHTIME' => date('H:i:s'), 'SHERROR' => 'X', 'SHINTBOX' => '', 'SHTYPE' => 'M3-Res-out',
'SHDATA' => 'An Error Occured - ' . substr($th, 0, 300), 'SHUSER' => $SHUSER, 'SHSOURCE' => $SHSOURCE];
Helper::logToICM699M($data);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => 'Catched Error: ' . $th,
]);
}
return self::sendInventoryRequest(
(string) ($tokenData->token ?? ''),
$payload,
$user,
$source,
$warehouse
);
} catch (\Throwable $exception) {
return self::inventoryExceptionResponse($exception, $payload, $user, $source, $warehouse);
}
}
private static function sendInventoryRequest(
string $token,
array $payload,
string $user,
string $source,
string $warehouse
) {
if ($token === '') {
return response()->json([
'code' => '401',
'status' => 'error',
'message' => 'M3 authentication token was not returned.',
], 401);
}
$response = Http::withHeaders([
'accept' => self::JSON_CONTENT_TYPE,
'Content-Type' => self::JSON_CONTENT_TYPE,
])
->withToken($token)
->timeout(Config::get('arrays.curlTimeOut'))
->post(env('ENDPOINTURL') . 'APIFLOWS/Sentinel/M3InventoryReport?ionapiRespStyle=sync', $payload);
$statusCode = $response->getStatusCode();
$sessionId = $response->hasHeader('x-transaction-id')
? '-sessionID:' . $response->header('x-transaction-id')
: '-sessionID';
if ($statusCode !== Response::HTTP_OK) {
self::logInventoryResponse(
self::ZERO_PAD,
$statusCode . self::NO_M3_RESPONSE . $sessionId,
$user,
$source,
$warehouse,
true
);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => self::NO_M3_RESPONSE,
], 502);
}
$sanitized = Helper::sanityzeString($response->body());
self::logInventoryResponse(
self::ZERO_PAD,
$statusCode . $sanitized . '-' . json_encode($payload) . $sessionId,
$user,
$source,
$warehouse
);
return response()->json([
'code' => $statusCode,
'status' => 'Success',
'message' => json_decode($sanitized),
]);
}
private static function inventoryExceptionResponse(
\Throwable $exception,
array $payload,
string $user,
string $source,
string $warehouse
) {
$isCurlError = str_contains($exception->getMessage(), 'cURL error');
$message = $isCurlError
? 'Curl Error: Communication with M3 Timed Out.'
: 'M3 inventory request failed.';
self::logInventoryResponse(
self::ZERO_PAD,
$isCurlError ? $message : mb_substr($exception->getMessage(), 0, 300),
$user,
$source,
$warehouse,
true
);
Log::error('M3 inventory request failed.', [
'exception' => $exception,
'payload' => $payload,
]);
return response()->json([
'code' => '405',
'status' => 'error',
'message' => $message,
], 502);
}
private static function logInventoryResponse(
string $deliveryNumber,
string $message,
string $user,
string $source,
string $warehouse,
bool $isError = false
): void {
Helper::logToICM699M([
'SHMACPKR' => $deliveryNumber,
'SHDT' => date('Y-m-d'),
'SHTIME' => date('H:i:s'),
'SHERROR' => $isError ? 'X' : '',
'SHINTBOX' => '',
'SHTYPE' => 'M3-Res-out',
'SHDATA' => $message,
'SHUSER' => $user,
'SHSOURCE' => $source,
'SHCO' => '',
'SHWHS' => $warehouse,
]);
}
// End M3InventoryReport
// M3InventoryReportList with URL Params