<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\models;

/**
 * Keeps the site-wide list of email addresses that have asked to receive
 * no further mail from this site. Unlike a per-group subscription, an
 * address on this list is skipped by the send path no matter which group
 * or activity the mail came from, which is what a recipient who is not a
 * group member needs when they unsubscribe from a registration message.
 * The MAIL_SUPPRESSION table's unique index on EMAIL keeps one row per
 * address. Address comparison is case-insensitive because email
 * addresses are.
 */
class MailSuppressionModel extends Model
{
    /**
     * Reports whether an address has asked to receive no mail.
     *
     * @param string $email address to check
     * @return bool true when the address is on the suppression list
     */
    public function isSuppressed($email)
    {
        $db = $this->db;
        $email = trim((string) $email);
        if ($email === "") {
            return false;
        }
        $sql = "SELECT ID FROM MAIL_SUPPRESSION " .
            "WHERE LOWER(EMAIL) = LOWER(?) " . $db->limitOffset(1);
        $result = $db->execute($sql, [$email]);
        if ($result && $db->fetchArray($result)) {
            return true;
        }
        return false;
    }
    /**
     * Adds an address to the suppression list. A no-op returning false
     * when the address is empty or already on the list, so a caller can
     * branch on the result.
     *
     * @param string $email address to stop mailing
     * @return bool true when a new row was added
     */
    public function suppress($email)
    {
        $db = $this->db;
        $email = trim((string) $email);
        if ($email === "" || $this->isSuppressed($email)) {
            return false;
        }
        $sql = "INSERT INTO MAIL_SUPPRESSION (EMAIL, CREATED_AT) " .
            "VALUES (?, ?)";
        $db->execute($sql, [$email, time()]);
        return true;
    }
    /**
     * Removes an address from the suppression list so the site may mail
     * it again. Matching is case-insensitive on the address.
     *
     * @param string $email address to allow mail to once more
     */
    public function unsuppress($email)
    {
        $db = $this->db;
        $sql = "DELETE FROM MAIL_SUPPRESSION WHERE LOWER(EMAIL) = LOWER(?)";
        $db->execute($sql, [trim((string) $email)]);
    }
}
