79 lines
1.7 KiB
PHP
79 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types = 1);
|
|
namespace Kimendisch\Sbgg_Jetzt;
|
|
use Flake\Dat;
|
|
use Flake\Id64;
|
|
|
|
class Newsletter {
|
|
/**
|
|
* Add a new mail address to the mailing list.
|
|
*
|
|
* @param string $mail_address Recipient mail address.
|
|
* @param string $language Desired newsletter language.
|
|
*/
|
|
public static function subscribe(string $mail_address, string $language): void {
|
|
// OPEN DATABASE //
|
|
// acquire runlock
|
|
$old_ignore_user_abort = (bool)ignore_user_abort(true);
|
|
|
|
// obtain handle
|
|
$newsletter_db = new Dat("./.dat/newsletter", Dat::MODE_READ_WRITE);
|
|
|
|
|
|
// MAYBE INITIALIZE DATABASE //
|
|
if(!is_array($newsletter_db->get("list"))){
|
|
// initialize as empty array
|
|
$newsletter_db->set("list", []);
|
|
}
|
|
|
|
|
|
// UPDATE DATABASE //
|
|
$entry_key = ["list", $mail_address];
|
|
|
|
// generate unsubscribe key
|
|
$unsubscribe_key = Id64::new(length: 16);
|
|
|
|
// only allow new entries
|
|
$is_new = ($newsletter_db->get($entry_key) === null);
|
|
|
|
// maybe add to database
|
|
if($is_new){
|
|
$newsletter_db->set($entry_key, [
|
|
"unsubscribe_key" => $unsubscribe_key,
|
|
"language" => $language
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
// FINALIZE //
|
|
// close database
|
|
$newsletter_db->write_close();
|
|
|
|
if($is_new){
|
|
// send welcome mail
|
|
self::send_welcome_mail(mail_address: $mail_address);
|
|
|
|
} else {
|
|
// fake delay
|
|
//*/ TODO
|
|
}
|
|
|
|
// release runlock
|
|
ignore_user_abort($old_ignore_user_abort);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* HELPER: Send welcome mail to new recipient.
|
|
*
|
|
* @param string $mail_address Recipient mail address.
|
|
*/
|
|
private static function send_welcome_mail(string $mail_address): void {
|
|
/**/print_r(["send_welcome_mail" => $mail_address]);
|
|
//*/ TODO
|
|
}
|
|
}
|
|
?>
|