Confirm Subscription
You are subscribing to my email newsletter. Click the link below to confirm:
{CONFIRMLINK}";
}
if(!isset($GLOBALS['MAILSUB_REGULAR'])){
    $GLOBALS['MAILSUB_REGULAR']="
This is my email newsletter. Edit your subscription
";
}
if(!isset($GLOBALS['MAILSUB_WELCOME'])){
    $GLOBALS['MAILSUB_WELCOME']="Welcome
You have subscribed to my email newsletter. Edit your subscription below:
{EDITSUBSCRIPTION}";
}
if(!isset($GLOBALS['MAILSUB_EDIT'])){
    $GLOBALS['MAILSUB_EDIT']="Edit Subscription
Unsubscribe my newsletter: CONFIRM
";
}
if(!isset($GLOBALS['MY_HOST'])){
    $GLOBALS['MY_HOST']="https://www.example.com";
}
$PENDING=[];
$ACCEPTED=[];
$f = fopen("pending_confirm.php",'r'); $tries=0;
while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); fclose($f); $tries++;} $f=0;
$content = file_get_contents("pending_confirm.php");
$f = fopen("all_subscribers.php",'r'); $tries=0;
while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); fclose($f); $tries++;} $f=0;
$subscribers = file_get_contents("all_subscribers.php");
if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$content,$matches,PREG_SET_ORDER)){
    foreach($matches as $m){ $PENDING[]=[$m[1],$m[2],$m[3]]; }
}
if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$subscribers,$matches,PREG_SET_ORDER)){
    foreach($matches as $m){ $ACCEPTED[]=[$m[1],$m[2],$m[3]]; }
}
if(isset($_GET['subscribe']) && isset($_POST['email_subscribe_name'])){
    if(preg_match("/^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/",$_POST['email_subscribe_name'])){
        $item=[uniqid(),$_POST['email_subscribe_name'],'en'];
        $PENDING[]=$item;
        send_verification($item[0],$_POST['email_subscribe_name']);
    }else{
        echo "Not a valid email.";
    }
    $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
    fwrite($f,"Couldn't confirm your subscription."; exit; }
    $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
    fwrite($f,"Successfully subscribed";
}
if(isset($_GET['unsubscribe'])&&isset($_GET['id'])){
    if(!isset($_GET['doit'])){
        $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe={$_GET['unsubscribe']}&doit=1&id={$_GET['id']}";
        $body=preg_replace("/\{UNSUBSCRIBE\}/",$link,$GLOBALS['MAILSUB_EDIT']);
        echo $body; exit;
    }
    $any=0;
    foreach(array_values($ACCEPTED) as $key=>$p){
        if($p[0]==$_GET['id'] && $_GET['unsubscribe']==$p[1]){
            unset($ACCEPTED[$key]); $any=1;
        }
    }
    if(!$any){ echo "Unknown error
"; exit; }
    $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
    fwrite($f,"Successfully unsubscribed";
}
function send_email($to,$subject,$body){
    try {
        global $mail;
        global $mailconf;
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = $mailconf['host'];                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = $mailconf['username'];                     //SMTP username
        $mail->Password   = $mailconf['password'];                            //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            //Enable implicit TLS encryption
        $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    
        //Recipients
        $mail->setFrom($mailconf['from'], $mailconf['name']);
        $mail->addAddress($to);
    
        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $body;
    
        $mail->send();
        
        return null;
    } catch (Exception $e) {
        echo "Message could not be sent."; #Mailer Error: {$mail->ErrorInfo}";
        return -1;
    }
}
function send_verification($id,$address){
    $addressencode=urlencode($address);
    $link="{$GLOBALS['MY_HOST']}/mailsub.php?confirm=$addressencode&id=$id";
    $body=preg_replace("/\{CONFIRMLINK\}/",$link,$GLOBALS['MAILSUB_CONFIRM']);
    if(!send_email($address,"Confirm Subscription",$body)){
        echo "Please check your inbox for confirmation e-mail.";
    }else{
        echo "Error sending email.";
    }
}
function send_welcome($id,$address){
    $addressencode=urlencode($address);
    $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe=$addressencode&id=$id";
    $body=preg_replace("/\{EDITSUBSCRIPTION\}/",$link,$GLOBALS['MAILSUB_WELCOME']);
    send_email($address,"Welcome!",$body);
}