<?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;

/**
 * Persists each user's personal list of trusted sender addresses.
 * Under the "Spam Insecure" delivery posture, a message that
 * arrives without TLS is filed in Junk unless its envelope sender
 * is on the receiving user's trusted list, in which case it is
 * delivered to the INBOX. The list is per-user: a sender one user
 * trusts is not trusted for anyone else. The MAIL_SENDER_ALLOW
 * table's unique index on (USER_ID, SENDER) keeps one row per user
 * and sender. Sender comparison is case-insensitive because email
 * addresses are.
 */
class MailSenderAllowModel extends Model
{
    /**
     * Reports whether a user has trusted a sender address.
     *
     * @param int $user_id receiving user's id
     * @param string $sender envelope sender address to check
     * @return bool true when the user has trusted the sender
     */
    public function isAllowed($user_id, $sender)
    {
        $db = $this->db;
        $sender = trim((string) $sender);
        if ($sender === "") {
            return false;
        }
        $sql = "SELECT ID FROM MAIL_SENDER_ALLOW " .
            "WHERE USER_ID = ? AND LOWER(SENDER) = LOWER(?) " .
            $db->limitOffset(1);
        $result = $db->execute($sql, [(int) $user_id, $sender]);
        if ($result && $db->fetchArray($result)) {
            return true;
        }
        return false;
    }

    /**
     * Adds a trusted sender for a user. A no-op returning false
     * when the address is empty or the user already trusts it, so
     * a caller can branch on the result.
     *
     * @param int $user_id receiving user's id
     * @param string $sender envelope sender address to trust
     * @return bool true when a new row was added
     */
    public function addSender($user_id, $sender)
    {
        $db = $this->db;
        $sender = trim((string) $sender);
        if ($sender === "" || $this->isAllowed($user_id, $sender)) {
            return false;
        }
        $sql = "INSERT INTO MAIL_SENDER_ALLOW " .
            "(USER_ID, SENDER, CREATED_AT) VALUES (?, ?, ?)";
        $db->execute($sql, [(int) $user_id, $sender, time()]);
        return true;
    }

    /**
     * Removes a trusted sender for a user. Scoped to the owning
     * user so a request cannot change another user's list.
     * Matching is case-insensitive on the sender address.
     *
     * @param int $user_id receiving user's id
     * @param string $sender envelope sender address to untrust
     */
    public function removeSender($user_id, $sender)
    {
        $db = $this->db;
        $sql = "DELETE FROM MAIL_SENDER_ALLOW " .
            "WHERE USER_ID = ? AND LOWER(SENDER) = LOWER(?)";
        $db->execute($sql,
            [(int) $user_id, trim((string) $sender)]);
    }

    /**
     * Returns the sender addresses a user trusts, sorted
     * alphabetically for a stable display order.
     *
     * @param int $user_id receiving user's id
     * @return array list of sender address strings
     */
    public function sendersForUser($user_id)
    {
        $db = $this->db;
        $sql = "SELECT SENDER FROM MAIL_SENDER_ALLOW " .
            "WHERE USER_ID = ? ORDER BY SENDER ASC";
        $result = $db->execute($sql, [(int) $user_id]);
        $senders = [];
        if ($result) {
            while ($row = $db->fetchArray($result)) {
                $senders[] = $row['SENDER'];
            }
        }
        return $senders;
    }
}
