238 lines
7.0 KiB
Plaintext
238 lines
7.0 KiB
Plaintext
TEXT TO lcReceiver NOSHOW
|
|
// receiver.php
|
|
|
|
<?php
|
|
// Încarcare configura?ie
|
|
$config = json_decode(file_get_contents(dirname(__FILE__) . '/config.json'), true);
|
|
if (!$config) {
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
die('Eroare la încarcarea configura?iei');
|
|
}
|
|
|
|
// Func?ie de validare XML
|
|
function validateXML($xmlContent) {
|
|
// Dezactiveaza raportarea erorilor standard ?i folose?te erori interne libxml
|
|
libxml_use_internal_errors(true);
|
|
|
|
// Elimina BOM (Byte Order Mark) daca exista
|
|
$xmlContent = preg_replace('/^\xEF\xBB\xBF/', '', $xmlContent);
|
|
|
|
// Cura?a spa?iile de la început ?i final
|
|
$xmlContent = trim($xmlContent);
|
|
|
|
// Încearca sa încarce XML-ul
|
|
$xml = simplexml_load_string($xmlContent);
|
|
|
|
if ($xml === false) {
|
|
$errors = libxml_get_errors();
|
|
$errorMessages = [];
|
|
|
|
foreach ($errors as $error) {
|
|
$errorMessages[] = [
|
|
'level' => $error->level,
|
|
'code' => $error->code,
|
|
'column' => $error->column,
|
|
'message' => $error->message,
|
|
'line' => $error->line
|
|
];
|
|
}
|
|
|
|
libxml_clear_errors();
|
|
|
|
return [
|
|
'valid' => false,
|
|
'errors' => $errorMessages
|
|
];
|
|
}
|
|
|
|
// Verifica namespace-urile necesare
|
|
$namespaces = $xml->getNamespaces(true);
|
|
$requiredNamespaces = [
|
|
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
|
|
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2',
|
|
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
|
|
];
|
|
|
|
foreach ($requiredNamespaces as $ns) {
|
|
$found = false;
|
|
foreach ($namespaces as $namespace) {
|
|
if ($namespace === $ns) {
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$found) {
|
|
return [
|
|
'valid' => false,
|
|
'errors' => [
|
|
['message' => "Namespace lipsa: $ns"]
|
|
]
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'valid' => true,
|
|
'errors' => []
|
|
];
|
|
}
|
|
|
|
// Verificare IP
|
|
function checkIP() {
|
|
global $config;
|
|
$clientIP = $_SERVER['REMOTE_ADDR'];
|
|
return in_array($clientIP, $config['allowed_ips']);
|
|
}
|
|
|
|
// Verificare token
|
|
function validateToken() {
|
|
global $config;
|
|
$headers = getallheaders();
|
|
$token = isset($headers['X-Api-Key']) ? $headers['X-Api-Key'] : '';
|
|
return hash_equals($config['api_key'], $token);
|
|
}
|
|
|
|
// Verificare origine request
|
|
if (!checkIP()) {
|
|
header('HTTP/1.1 403 Forbidden');
|
|
error_log("Acces interzis pentru IP: " . $_SERVER['REMOTE_ADDR']);
|
|
die(json_encode([
|
|
'success' => false,
|
|
'error' => 'Acces interzis',
|
|
'details' => 'IP-ul nu este autorizat'
|
|
]));
|
|
}
|
|
|
|
// Verificare token
|
|
if (!validateToken()) {
|
|
header('HTTP/1.1 401 Unauthorized');
|
|
error_log("Token invalid de la IP: " . $_SERVER['REMOTE_ADDR']);
|
|
die(json_encode([
|
|
'success' => false,
|
|
'error' => 'Token invalid',
|
|
'details' => 'Autentificare e?uata'
|
|
]));
|
|
}
|
|
|
|
// Configurare director pentru fi?iere temporare
|
|
$uploadDir = dirname(__FILE__) . '/temp/';
|
|
if (!file_exists($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
|
|
// Procesare request POST (primire XML)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
try {
|
|
// Cite?te con?inutul XML din request
|
|
$xmlContent = file_get_contents('php://input');
|
|
|
|
// Validare XML
|
|
$validationResult = validateXML($xmlContent);
|
|
|
|
if (!$validationResult['valid']) {
|
|
header('Content-Type: application/json');
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'XML invalid',
|
|
'details' => $validationResult['errors']
|
|
]);
|
|
error_log("Validare XML e?uata: " . json_encode($validationResult['errors']));
|
|
exit;
|
|
}
|
|
|
|
// Genereaza nume unic pentru fi?ier
|
|
$fileName = uniqid('xml_') . '.xml';
|
|
$filePath = $uploadDir . $fileName;
|
|
|
|
// Salveaza fi?ierul
|
|
if (file_put_contents($filePath, $xmlContent)) {
|
|
// Raspuns succes
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => true,
|
|
'fileName' => $fileName
|
|
]);
|
|
} else {
|
|
throw new Exception('Eroare la salvarea fi?ierului');
|
|
}
|
|
} catch (Exception $e) {
|
|
header('Content-Type: application/json');
|
|
http_response_code(500);
|
|
error_log("Eroare procesare XML: " . $e->getMessage());
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Procesare request GET (cura?are fi?iere temporare)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['cleanup'])) {
|
|
$fileName = basename($_GET['cleanup']); // Sanitizare nume fi?ier
|
|
if (preg_match('/^xml_[a-f0-9]+\.xml$/', $fileName)) { // Verifica formatul numelui
|
|
$filePath = $uploadDir . $fileName;
|
|
|
|
if (file_exists($filePath)) {
|
|
if (unlink($filePath)) {
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
header('Content-Type: application/json');
|
|
http_response_code(500);
|
|
error_log("Nu s-a putut ?terge fi?ierul: " . $filePath);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Nu s-a putut ?terge fi?ierul'
|
|
]);
|
|
}
|
|
} else {
|
|
header('Content-Type: application/json');
|
|
http_response_code(404);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Fi?ierul nu exista'
|
|
]);
|
|
}
|
|
} else {
|
|
header('Content-Type: application/json');
|
|
http_response_code(400);
|
|
error_log("Nume fi?ier invalid solicitat: " . $fileName);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Nume fi?ier invalid'
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Cura?are automata a fi?ierelor vechi
|
|
$files = glob($uploadDir . 'xml_*.xml');
|
|
$now = time();
|
|
$maxAge = $config['temp_file_lifetime'] * 3600; // Conversie ore în secunde
|
|
|
|
foreach ($files as $file) {
|
|
if ($now - filemtime($file) > $maxAge) {
|
|
@unlink($file);
|
|
error_log("Fi?ier vechi ?ters: " . basename($file));
|
|
}
|
|
}
|
|
?>
|
|
ENDTEXT
|
|
|
|
TEXT TO lcJson noshow
|
|
//config.json
|
|
{
|
|
"api_key": "d3a46a47f844dcb5c34cafac19d678c4",
|
|
"allowed_ips": [
|
|
"127.0.0.1",
|
|
"::1",
|
|
"localhost",
|
|
"5.14.199.44",
|
|
"83.103.197.79"
|
|
],
|
|
"temp_file_lifetime": 1
|
|
}
|
|
ENDTEXT
|
|
|