131 lines
3.7 KiB
PHP
131 lines
3.7 KiB
PHP
<?php
|
|
// import PHPMailer classes into the global namespace
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
$dname = dirname(__FILE__);
|
|
require $dname . '/Exception.php';
|
|
require $dname . '/PHPMailer.php';
|
|
require $dname . '/SMTP.php';
|
|
|
|
$webroot = $_SERVER['DOCUMENT_ROOT'];
|
|
|
|
// read ini file
|
|
$ini_file = $webroot . '/../config.ini';
|
|
$ini = parse_ini_file($ini_file, TRUE);
|
|
|
|
// general constants
|
|
$general = $ini['general'];
|
|
$general['sql'] = [
|
|
'create_table' => 'CREATE TABLE IF NOT EXISTS subscribers (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE, name TEXT);',
|
|
'create_record' => 'INSERT INTO subscribers(email, name) VALUES(:e, :n);',
|
|
'read_record' => 'SELECT 1 FROM subscribers WHERE email = :e;',
|
|
'update_record' => '',
|
|
'delete_record' => 'DELETE FROM subscribers WHERE email = :e;'
|
|
];
|
|
|
|
$smtp = $ini['smtp'];
|
|
$db = $ini['db'];
|
|
|
|
function GetConfirmationHash($confEmail) {
|
|
global $general;
|
|
return md5($confEmail . $general['uniqueKey']);
|
|
}
|
|
|
|
// connects to database and returns PDO object
|
|
function getPDO($dbType = 'sqlite') {
|
|
global $db;
|
|
if ( $dbType == 'sqlite' ) {
|
|
global $webroot;
|
|
error_log('my dsn is ' . 'sqlite:' . $webroot . '/../' . $db['sqlite_file']);
|
|
|
|
return new \PDO('sqlite:' . $webroot . '/../' . $db['sqlite_file']);
|
|
}
|
|
// error
|
|
return NULL;
|
|
}
|
|
|
|
// Sends an email to single recipient with subject, body and sender info specified in an array
|
|
function SendEmail($toAddress, $mailContents) {
|
|
global $general, $smtp;
|
|
$mail = new PHPMailer(true);
|
|
|
|
//Server settings
|
|
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
|
|
$mail->isSMTP();
|
|
$mail->Host = $smtp['host'];
|
|
$mail->Port = $smtp['port']; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
|
|
$mail->SMTPAuth = $smtp['auth'];
|
|
$mail->Username = $smtp['username'];
|
|
$mail->Password = $smtp['password'];
|
|
//$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
|
|
|
|
// recipient
|
|
$mail->addAddress($toAddress);
|
|
|
|
// content
|
|
$mail->CharSet = 'UTF-8';
|
|
if ( isset($mailContents['bodyHTML']) and $mailContents['bodyHTML'] != '' ) {
|
|
$mail->isHTML(true);
|
|
$mail->Body = $mailContents['bodyHTML'];
|
|
$mail->AltBody = $mailContents['bodyText'];
|
|
} else {
|
|
$mail->isHTML(false);
|
|
$mail->Body = $mailContents['bodyText'];
|
|
}
|
|
$mail->Subject = $mailContents['subject'];
|
|
$mail->setFrom($mailContents['fromAddress'], $mailContents['fromName']);
|
|
|
|
// send
|
|
$mail->send();
|
|
}
|
|
|
|
|
|
function MakeSureTableExists($pdo) {
|
|
global $general;
|
|
$query = $pdo->prepare($general['sql']['create_table']);
|
|
if ( ! $query->execute() ) {
|
|
return 'Unbekannter Datenbankfehler';
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
// returns true if record does not yet exist in database; error string otherwise
|
|
function NotYetSubscribed($email, $pdo = NULL) {
|
|
if (!isset($pdo)) {
|
|
$pdo = getPDO();
|
|
}
|
|
|
|
$result = MakeSureTableExists($pdo);
|
|
if ( gettype($result) == 'string' ) {
|
|
return $result;
|
|
}
|
|
|
|
global $general;
|
|
$query = $pdo->prepare($general['sql']['read_record']);
|
|
if ( ! $query->execute([':e' => $email]) ) {
|
|
return 'Fehler beim Zugriff auf Datenbank';
|
|
}
|
|
|
|
if ( $query->fetch() ) {
|
|
return "Emailadresse {$email} ist bereits eingetragen";
|
|
}
|
|
|
|
// success
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
// redirects to specified URL via GET request and conveys an optional message; then exits
|
|
function GracefulExit($location, $message = NULL) {
|
|
global $general;
|
|
header($general['statusCode']);
|
|
if (isset($message)) {
|
|
$location .= '?' . http_build_query(['msg' => $message]);
|
|
error_log("Redirecting to {$location} now …");
|
|
}
|
|
header("Location: {$location}");
|
|
exit;
|
|
}
|
|
?>
|