feat: refactor into php for mrbs page
This commit is contained in:
218
php/functions.php
Normal file
218
php/functions.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
/**
|
||||
* Validate email address
|
||||
*/
|
||||
function validateEmail($email) {
|
||||
return preg_match(EMAIL_REGEX, $email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize input string
|
||||
*/
|
||||
function sanitizeInput($input) {
|
||||
return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate XML from form data
|
||||
*/
|
||||
function generateXML($data) {
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->formatOutput = true;
|
||||
|
||||
$root = $xml->createElement('form_submission');
|
||||
$xml->appendChild($root);
|
||||
|
||||
// Static data
|
||||
$static = $xml->createElement('static');
|
||||
$root->appendChild($static);
|
||||
|
||||
foreach (['name', 'lastname', 'title', 'telno', 'mail', 'apparatsname', 'subject', 'semester', 'dauerapparat'] as $field) {
|
||||
if (isset($data[$field])) {
|
||||
$element = $xml->createElement($field, htmlspecialchars($data[$field]));
|
||||
$static->appendChild($element);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['message'])) {
|
||||
$messageEl = $xml->createElement('message', htmlspecialchars($data['message']));
|
||||
$static->appendChild($messageEl);
|
||||
}
|
||||
|
||||
// Books
|
||||
if (isset($data['books']) && is_array($data['books'])) {
|
||||
$booksNode = $xml->createElement('books');
|
||||
$root->appendChild($booksNode);
|
||||
|
||||
foreach ($data['books'] as $book) {
|
||||
$bookNode = $xml->createElement('book');
|
||||
$booksNode->appendChild($bookNode);
|
||||
|
||||
foreach (['authorname', 'year', 'title', 'signature'] as $field) {
|
||||
$value = isset($book[$field]) ? htmlspecialchars($book[$field]) : '';
|
||||
$fieldNode = $xml->createElement($field, $value);
|
||||
$bookNode->appendChild($fieldNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $xml->saveXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate XML for ELSA form
|
||||
*/
|
||||
function generateELSAXML($data) {
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->formatOutput = true;
|
||||
|
||||
$root = $xml->createElement('elsa_submission');
|
||||
$xml->appendChild($root);
|
||||
|
||||
// General info
|
||||
$generalInfo = $xml->createElement('general_info');
|
||||
$root->appendChild($generalInfo);
|
||||
|
||||
$generalFields = ['name', 'lastname', 'title', 'mail', 'subject', 'classname',
|
||||
'usage_date_from', 'usage_date_to', 'availability_date'];
|
||||
|
||||
foreach ($generalFields as $field) {
|
||||
if (isset($data[$field])) {
|
||||
$element = $xml->createElement($field, htmlspecialchars($data[$field]));
|
||||
$generalInfo->appendChild($element);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['message'])) {
|
||||
$messageEl = $xml->createElement('message', htmlspecialchars($data['message']));
|
||||
$generalInfo->appendChild($messageEl);
|
||||
}
|
||||
|
||||
// Media sections
|
||||
$mediaRoot = $xml->createElement('media');
|
||||
$root->appendChild($mediaRoot);
|
||||
|
||||
// Add different media types (monografie, zeitschrift, herausgeber)
|
||||
$mediaTypes = [
|
||||
'monografien' => $data['monografien'] ?? [],
|
||||
'zeitschriftenartikel' => $data['zeitschriftenartikel'] ?? [],
|
||||
'herausgeberwerke' => $data['herausgeberwerke'] ?? []
|
||||
];
|
||||
|
||||
foreach ($mediaTypes as $type => $entries) {
|
||||
if (!empty($entries)) {
|
||||
$section = $xml->createElement($type);
|
||||
$mediaRoot->appendChild($section);
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$entryNode = $xml->createElement('entry');
|
||||
$section->appendChild($entryNode);
|
||||
|
||||
foreach ($entry as $key => $value) {
|
||||
$fieldNode = $xml->createElement($key, htmlspecialchars($value));
|
||||
$entryNode->appendChild($fieldNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $xml->saveXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email with XML attachment
|
||||
* Uses PHP's mail() function or SMTP if configured
|
||||
*/
|
||||
function sendEmail($subject, $xmlContent, $toEmail = null) {
|
||||
$to = $toEmail ?? MAIL_TO;
|
||||
$from = MAIL_FROM;
|
||||
|
||||
if (!MAIL_ENABLED) {
|
||||
// Log instead of sending
|
||||
error_log("==========================================================");
|
||||
error_log("MAIL SENDING DISABLED - Would have sent:");
|
||||
error_log("From: " . $from);
|
||||
error_log("To: " . $to);
|
||||
error_log("Subject: " . $subject);
|
||||
error_log("----------------------------------------------------------");
|
||||
error_log($xmlContent);
|
||||
error_log("==========================================================");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try using SMTP if credentials are configured
|
||||
if (SMTP_USERNAME && SMTP_PASSWORD && class_exists('PHPMailer\PHPMailer\PHPMailer')) {
|
||||
return sendEmailSMTP($subject, $xmlContent, $to, $from);
|
||||
}
|
||||
|
||||
// Fallback to PHP mail()
|
||||
$headers = "From: " . $from . "\r\n";
|
||||
$headers .= "Content-Type: application/xml; charset=UTF-8\r\n";
|
||||
$headers .= "X-Mailer: PHP/" . phpversion();
|
||||
|
||||
return mail($to, $subject, $xmlContent, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email via SMTP using PHPMailer (if available)
|
||||
*/
|
||||
function sendEmailSMTP($subject, $xmlContent, $to, $from) {
|
||||
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
|
||||
error_log("PHPMailer not available, falling back to mail()");
|
||||
return false;
|
||||
}
|
||||
|
||||
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
|
||||
|
||||
try {
|
||||
// Server settings
|
||||
$mail->isSMTP();
|
||||
$mail->Host = SMTP_HOST;
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = SMTP_USERNAME;
|
||||
$mail->Password = SMTP_PASSWORD;
|
||||
$mail->SMTPSecure = SMTP_ENCRYPTION;
|
||||
$mail->Port = SMTP_PORT;
|
||||
$mail->CharSet = 'UTF-8';
|
||||
|
||||
// Recipients
|
||||
$mail->setFrom($from);
|
||||
$mail->addAddress($to);
|
||||
|
||||
// Content
|
||||
$mail->Subject = $subject;
|
||||
$mail->Body = $xmlContent;
|
||||
$mail->ContentType = 'application/xml';
|
||||
|
||||
$mail->send();
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
error_log("Email sending failed: " . $mail->ErrorInfo);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to URL
|
||||
*/
|
||||
function redirect($url) {
|
||||
header("Location: " . $url);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get POST value with default
|
||||
*/
|
||||
function post($key, $default = '') {
|
||||
return isset($_POST[$key]) ? sanitizeInput($_POST[$key]) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all POST values matching a pattern (for arrays)
|
||||
*/
|
||||
function postArray($key) {
|
||||
return isset($_POST[$key]) && is_array($_POST[$key]) ?
|
||||
array_map('sanitizeInput', $_POST[$key]) : [];
|
||||
}
|
||||
Reference in New Issue
Block a user