");
#$_POST['f_matricola'] = "1234";
#$_POST['f_nome']="marco";
#$_POST['f_cognome']="surna";
#$_POST['f_password']="123";
#$_POST['f_password1']="123";
##$_POST['f_mail']="marco.procaccini@me.com";
#$_POST['f_mail']="giorgi@acm.org";
#$_POST['MAX_MATRLEN']=30;
#$_POST['MAX_PASSLEN']=30;
#$_POST['MAX_NOMELEN']=30;
#$_POST['MAX_SURLEN']=30;
#$_POST['mailproto']="http";
#$_POST['mailsrv']="x.dii.unisi.it";
#$_POST['mailsrvport']=":8088";
#$_POST['mailuser']="giorgi";
#$_POST['coursedida']="didattica";
#$_POST['courseacro']="arcal1";
#$_POST['savemsg']="dai";
$locmatr=(isset($_POST['f_matricola'])?$_POST['f_matricola']:"");
$locnome=(isset($_POST['f_nome'])?$_POST['f_nome']:"");
$loccogn=(isset($_POST['f_cognome'])?$_POST['f_cognome']:"");
$locpass=(isset($_POST['f_password'])?$_POST['f_password']:"");
$locpas1=(isset($_POST['f_password1'])?$_POST['f_password1']:"");
$locmail=(isset($_POST['f_mail'])?$_POST['f_mail']:"");
$loclchk=(isset($_POST['f_mylcheck'])?$_POST['f_mylcheck']:"");
$locsvat=(isset($_POST['saveat'])?$_POST['saveat']:"");
$locsmsg=(isset($_POST['savemsg'])?$_POST['savemsg']:"");
$locserr=(isset($_POST['saveerr'])?$_POST['saveerr']:"");
$_POST['saveat'] = $locsvat;
#$a=$_POST['f_matricola'];
#$b=$_POST['f_password'];
#$c=$_POST['f_password1'];
#echo "$a $b $c";
#$_POST['f_mail'] = trim($_POST['f_mail']);
$_POST['f_mail'] = trim($locmail);
$_POST['f_mail'] = PREG_REPLACE("/ /", "//", $_POST['f_mail']);
#$loccogn = trim($_POST['f_cognome']);
$loccogn = trim($loccogn);
$loccogn = PREG_REPLACE("/ /", "/_/", $loccogn);
$loccogn = PREG_REPLACE("/[^0-9a-zA-Z_]/i", '', $loccogn);
$_POST['f_cognome'] = strtoupper(substr($loccogn,0,1)).strtolower(substr($loccogn,1));
$locnome = trim($locnome);
$locnome = PREG_REPLACE("/ /", "/_/", $locnome);
$locnome = PREG_REPLACE("/[^0-9a-zA-Z_ ]/", "", $locnome);
$_POST['f_nome'] = strtoupper(substr($locnome,0,1)).strtolower(substr($locnome,1));
#PRINTDEBUG
#
#print ("MATRICOLA =".$_POST['f_matricola']."
");
#print ("NOME =".$_POST['f_nome']."
");
#print ("COGNOME =".$_POST['f_cognome']."
");
#print ("PASSWORD =".$_POST['f_password']."
");
#print ("PASSWORD1 =".$_POST['f_password1']."
");
#print ("EMAIL =".$_POST['f_mail']."
");
#print ("MAX_MATRLEN =".$_POST['MAX_MATRLEN']."
");
#print ("MAX_PASSLEN =".$_POST['MAX_PASSLEN']."
");
#print ("MAX_NOMELEN =".$_POST['MAX_NOMELEN']."
");
#print ("MAX_SURLEN =".$_POST['MAX_SURLEN']."
");
$ptmp=PREG_REPLACE("/[<>\$\\\\ \+()#]/", "", $locpass);
$ptmp2=str_replace("\$", "", $ptmp);
#$ptmp=PREG_REPLACE("/ /", "//", $locpass);
#$ptmp2=PREG_REPLACE("/ /", "//", $locpass);
#print("CIAO2 ptmp2=".$ptmp2."
");
#if (strpos($_POST['f_mail'], '@')>0) {
#print("CIAO3 ".$_POST['f_mail']."
");
if(filter_var($_POST['f_mail'], FILTER_VALIDATE_EMAIL)) {
#print("CIAO4
");
if ($ptmp2<>"") {
#print("CIAO5
");
if ($locpass==$ptmp2) {
#print("CIAO6
");
$_POST['f_password'] = $locpass;
$_POST['f_password1'] = $locpas1;
$_POST['f_mylcheck'] = $loclchk;
if ($_POST['f_matricola']<>"" and $_POST['f_cognome']<>"" and $_POST['f_nome']<>"" and $_POST['f_mail']<>"" and $_POST['f_password']<>"" and $_POST['f_password1']<>"" ) {
#print("CIAO7
");
if (strlen($_POST['f_matricola']) < $_POST['MAX_MATRLEN'] and strlen($_POST['f_password']) < $_POST['MAX_PASSLEN'] and strlen($_POST['f_nome']) < $_POST['MAX_NOMELEN'] and strlen($_POST['f_cognome']) < $_POST['MAX_SURLEN']) {
#print("CIAO8
");
if ($_POST['f_password']==$_POST['f_password1']) {
#print("CIAO9
");
#------------------------------------------ NEW MAIL MANAGEMENT START
$maildata = [
'f_matricola' => $_POST['f_matricola'] ?? '',
'f_nome' => $_POST['f_nome'] ?? '',
'f_cognome' => $_POST['f_cognome'] ?? '',
'f_mail' => $_POST['f_mail'] ?? '',
'f_password' => $_POST['f_password'] ?? '',
'f_mylcheck' => $_POST['f_mylcheck'] ?? '',
];
// Build URL as you do now
if (!empty($_POST['mailuser'])) {
$myurl = $_POST['mailsrv'].$_POST['mailsrvport']."/~".$_POST['mailuser']."/".$_POST['coursedida']."/".$_POST['courseacro'];
} else {
$myurl = $_POST['courseacrsh'].".".$_POST['mailsrv'].$_POST['mailsrvport'];
}
$pathemail = $_POST['mailproto']."://".$myurl."/email.php";
// --- Robust request with retry ---
$attempts = 0;
$maxAttempts = 3;
$lastErr = null;
$responseBody = null;
$httpCode = 0;
while ($attempts < $maxAttempts) {
$attempts++;
$ch = curl_init();
$verbose = fopen('php://temp', 'w+');
curl_setopt_array($ch, [
CURLOPT_URL => $pathemail,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_TIMEOUT => 30,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_FAILONERROR => false, // we’ll inspect HTTP code ourselves
CURLOPT_POSTFIELDS => $maildata,
CURLOPT_HTTPHEADER => ['Accept: application/json'],
CURLOPT_STDERR => $verbose,
]);
$responseBody = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$curlErrNo = curl_errno($ch);
$curlErr = curl_error($ch);
if ($responseBody !== false && $httpCode >= 200 && $httpCode < 500) {
// success or client error — no further retries
curl_close($ch);
break;
}
// transient errors worth retrying: timeouts, connect, 5xx
$transient = in_array($curlErrNo, [
CURLE_COULDNT_CONNECT,
CURLE_COULDNT_RESOLVE_HOST,
CURLE_OPERATION_TIMEDOUT,
CURLE_GOT_NOTHING,
], true) || ($httpCode >= 500 && $httpCode <= 599);
rewind($verbose);
$vlog = stream_get_contents($verbose);
curl_close($ch);
// log each attempt
error_log('[REGMAIL] attempt='.$attempts.' http='.$httpCode.' curl='.$curlErrNo.' err='.$curlErr.' verbose='.str_replace("\n","\\n",$vlog));
if (!$transient) break; // don’t bother retrying non-transient errors
// backoff: 0.5s, 1s
usleep($attempts * 500000);
}
// --- Persist an audit record regardless of outcome ---
try {
$auditDir = __DIR__.'/logs/registrations/'.date('Y/m/d');
@mkdir($auditDir, 0770, true);
$rec = [
'ts' => date('c'),
'client_ip' => $_SERVER['REMOTE_ADDR'] ?? '',
'request' => $maildata,
'target' => $pathemail,
'http' => $httpCode,
'resp' => $responseBody,
];
# Write-ahead log (WAL) on the caller side (already in the snippet): every submission is saved under logs/registrations/YYYY/MM/DD/*.json. If the network or mail backend is down, you still have the payload.
# file_put_contents($auditDir.'/'.bin2hex(random_bytes(8)).'.json', json_encode($rec, JSON_PRETTY_PRINT), LOCK_EX);
# --> CAUSES PERMISSION DENIED
} catch (Throwable $e) {
error_log('[REGMAIL] audit write failed: '.$e->getMessage());
}
// --- Build user feedback ---
$ok = false;
$userMsg = 'We received your request.';
if ($responseBody !== false && $httpCode >= 200 && $httpCode < 300) {
$json = json_decode($responseBody, true);
if (is_array($json)) {
if (!empty($json['ok']) && !empty($json['accepted'])) {
$ok = true;
$userMsg = '✅ '.$locsmsg;
} else {
$userMsg = '⚠️ '.$locserr . ($json['details'] ?? 'unknown error');
}
} else {
// non-JSON but 2xx — assume ok-ish
$ok = true;
$userMsg = '✅ '.$locsmsg;
}
} else {
$userMsg = '❌ Temporarily unable to queue your registration email. Please try again in a minute.';
}
// Show a clean message on the feedback screen
echo '