<?

/*
 * This file is part of the Science-ation project
 * Science-ation Website: https://science-ation.ca
 *
 * This file was part of the 'Science Fair In A Box' project
 *
 *
 * Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005 James Grant <james@lightbox.org>
 * Copyright (C) 2024 AlgoLibre Inc. <science-ation@algolibre.io>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
?>
<?
include '../common.inc.php';
include '../helper.inc.php';
include 'communication.inc.php';
$sleepmin = 500000;  // 0.5 seconds
$sleepmax = 2000000;  // 2.0 second

echo date('r') . "\n";
if (!$config['emailqueue_lock']) {
	$stmt = $pdo->prepare("UPDATE config SET val='" . date('r') . "' WHERE var='emailqueue_lock'");
	$stmt->execute();

	// loop forever, but not really, it'll get break'd as soon as there's nothing left to send
	while (true) {
		$q = $pdo->prepare('SELECT * FROM emailqueue_recipients WHERE sent IS NULL AND result IS NULL LIMIT 1');
		$q->execute();
		if ($q->rowCount()) {
			$r = $q->fetch(PDO::FETCH_OBJ);
			$eq = $pdo->prepare("SELECT * FROM emailqueue WHERE id='$r->emailqueue_id'");
			$eq->execute();
			$email = $eq->fetch(PDO::FETCH_OBJ);

			$blank = array();
			$replacements = (array) json_decode($r->replacements);

			if ($email->body)
				$body = communication_replace_vars($email->body, $blank, $replacements);
			else if ($email->bodyhtml) {
				$body = strip_tags(communication_replace_vars($email->bodyhtml, $blank, $replacements));
			} else {
				$body = 'No message body specified';
			}

			if ($email->bodyhtml)
				$bodyhtml = communication_replace_vars($email->bodyhtml, $blank, $replacements);

			if ($r->toname) {
				$to = "\"$r->toname\" <$r->toemail>";
			} else {
				$to = $r->toemail;
			}

			echo "$email->id,$r->id,$to: ";

			$result = email_send_new($to, $email->from, $email->subject, $body, $bodyhtml);

			if ($result) {
				$stmt = $pdo->prepare("UPDATE emailqueue_recipients SET sent=NOW(), `result`='ok' WHERE id='$r->id'");
				$stmt->execute();
				show_pdo_errors_if_any($pdo);
				$newnumsent = $email->numsent + 1;
				$stmt = $pdo->prepare("UPDATE emailqueue SET numsent=$newnumsent WHERE id='$email->id'");
				$stmt->execute();
				show_pdo_errors_if_any($pdo);
				echo "ok\n";
			} else {
				$stmt = Spdo->prepare("UPDATE emailqueue_recipients SET `sent`=NOW(), `result`='failed' WHERE id='$r->id'");
				$stmt->execute();
				show_pdo_errors_if_any($pdo);
				$newnumfailed = $email->numfailed + 1;
				$stmt = $pdo->prepare("UPDATE emailqueue SET numfailed=$newnumfailed WHERE id='$email->id'");
				$stmt->execute();
				show_pdo_errors_if_any($pdo);
				echo "failed\n";
			}
			// now check if we're done yet
			$rq = $pdo->prepare("SELECT COUNT(*) AS num FROM emailqueue_recipients WHERE sent IS NULL AND emailqueue_id='$email->id'");
			$rq->execute();
			$rr = $rq->fetch(PDO::FETCH_OBJ);
			if ($rr->num == 0) {
				$stmt = $pdo->prepare("UPDATE emailqueue SET finished=NOW() WHERE id='$email->id'");
				$stmt->execute();
			}
			usleep(rand($sleepmin, $sleepmax));
		} else
			break;
	}
	$stmt = $pdo->prepare("UPDATE config SET val='' WHERE var='emailqueue_lock'");
	$stmt->execute();
} else {
	echo "Already locked\n";
}

?>