| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports. |
| 5 | * Copyright (C) 2020-2024 Aleksey Andreev (liuch) |
| 6 | * |
| 7 | * Available at: |
| 8 | * https://github.com/liuch/dmarc-srg |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation, either version 3 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along with |
| 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * ========================= |
| 23 | * |
| 24 | * This file contains ReportList class |
| 25 | * |
| 26 | * @category API |
| 27 | * @package DmarcSrg |
| 28 | * @author Aleksey Andreev (liuch) |
| 29 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3 |
| 30 | */ |
| 31 | |
| 32 | namespace Liuch\DmarcSrg\Report; |
| 33 | |
| 34 | use Liuch\DmarcSrg\Core; |
| 35 | |
| 36 | /** |
| 37 | * It's the main class for working with the incoming reports, such as: |
| 38 | * - getting a list of reports |
| 39 | * - deleting several reports at once |
| 40 | * - getting the number of reports stored in the database |
| 41 | */ |
| 42 | class ReportList |
| 43 | { |
| 44 | public const ORDER_NONE = 0; |
| 45 | public const ORDER_BEGIN_TIME = 1; |
| 46 | public const ORDER_ASCENT = 2; |
| 47 | public const ORDER_DESCENT = 3; |
| 48 | |
| 49 | private $db = null; |
| 50 | private $user = null; |
| 51 | private $limit = 0; |
| 52 | private $filter = []; |
| 53 | private $order = null; |
| 54 | |
| 55 | /** |
| 56 | * The constructor |
| 57 | * |
| 58 | * @param \Liuch\DmarcSrg\Users\User $user User to get list |
| 59 | * @param \Liuch\DmarcSrg\Database\DatabaseController $db The database controller |
| 60 | */ |
| 61 | public function __construct($user, $db = null) |
| 62 | { |
| 63 | $this->user = $user; |
| 64 | $this->db = $db ?? Core::instance()->database(); |
| 65 | $this->resetOrderParams(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a list of reports with specified parameters from position $pos |
| 70 | * |
| 71 | * This method returns a list of reports that depends on the filter and order. |
| 72 | * The filter, order, and limit for the list can be set using the setFilter, setOrder and setMaxCount methods. |
| 73 | * |
| 74 | * @param int $pos The starting position from which the list will be returned |
| 75 | * |
| 76 | * @return array An array with keys `reports` and `more`. |
| 77 | * `reports` is an array of incoming reports which contains maximum 25 records by default. |
| 78 | * Another value of the number of records can be specified by calling |
| 79 | * the method setMaxCount. |
| 80 | * `more` is true if there are more records in the database, false otherwise. |
| 81 | */ |
| 82 | public function getList(int $pos): array |
| 83 | { |
| 84 | $max_rec = $this->limit > 0 ? $this->limit : 25; |
| 85 | $limit = [ |
| 86 | 'offset' => $pos, |
| 87 | 'count' => $max_rec + 1 |
| 88 | ]; |
| 89 | |
| 90 | $list = $this->db->getMapper('report')->list($this->filter, $this->order, $limit, $this->user->id()); |
| 91 | if (count($list) > $max_rec) { |
| 92 | $more = true; |
| 93 | unset($list[$max_rec]); |
| 94 | } else { |
| 95 | $more = false; |
| 96 | } |
| 97 | return [ |
| 98 | 'reports' => $list, |
| 99 | 'more' => $more |
| 100 | ]; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets the sort order for the list and for deleting several reports at once |
| 105 | * |
| 106 | * @param int $field The field to sort by. Currently only ORDER_BEGIN_TIME is available. |
| 107 | * @param int $direction The sorting direction. ORDER_ASCENT or ORDER_DESCENT must be used here. |
| 108 | * |
| 109 | * @return self |
| 110 | */ |
| 111 | public function setOrder(int $field, int $direction) |
| 112 | { |
| 113 | $this->order = null; |
| 114 | if ($field > self::ORDER_NONE && $field < self::ORDER_ASCENT) { |
| 115 | if ($direction !== self::ORDER_ASCENT) { |
| 116 | $direction = self::ORDER_DESCENT; |
| 117 | } |
| 118 | $this->order = [ |
| 119 | 'field' => 'begin_time', |
| 120 | 'direction' => $direction === self::ORDER_ASCENT ? 'ascent' : 'descent' |
| 121 | ]; |
| 122 | } else { |
| 123 | $this->resetOrderParams(); |
| 124 | } |
| 125 | return $this; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets maximum numbers of records in the list and for deleting reports |
| 130 | * |
| 131 | * @param int $num Maximum number of records in the list |
| 132 | * |
| 133 | * @return self |
| 134 | */ |
| 135 | public function setMaxCount(int $num) |
| 136 | { |
| 137 | if ($num > 0) { |
| 138 | $this->limit = $num; |
| 139 | } else { |
| 140 | $this->limit = 0; |
| 141 | } |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Sets filter values for the list and for deleting reports |
| 147 | * |
| 148 | * @param array $filter Key-value array: |
| 149 | * 'before_time' => DateTime, timestamp |
| 150 | * 'dkim' => string, 'fail' or 'pass' |
| 151 | * 'domain' => string or instance of Domain class |
| 152 | * 'month' => string, yyyy-mm format |
| 153 | * 'organization' => string |
| 154 | * 'spf' => string, 'fail' or 'pass' |
| 155 | * 'status' => string, 'read' or 'unread' |
| 156 | * Note! 'dkim', 'spf' and 'disposition' do not affect the delete method |
| 157 | * |
| 158 | * @return self |
| 159 | */ |
| 160 | public function setFilter(array $filter) |
| 161 | { |
| 162 | $this->filter = $filter; |
| 163 | return $this; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the number of reports in the database |
| 168 | * |
| 169 | * It returns the number of reports in the database. |
| 170 | * The limit and some filter items `dkim`, `spf` and `disposition` do not affect this. |
| 171 | * |
| 172 | * @return int The number of reports in the database |
| 173 | */ |
| 174 | public function count(): int |
| 175 | { |
| 176 | $limit = [ 'offset' => 0, 'count' => $this->limit ]; |
| 177 | return $this->db->getMapper('report')->count($this->filter, $limit, $this->user->id()); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Deletes reports from the database |
| 182 | * |
| 183 | * It deletes repors form the database. The filter items `dkim`, `spf` and `disposition` do not affect this. |
| 184 | * |
| 185 | * @return void |
| 186 | */ |
| 187 | public function delete(): void |
| 188 | { |
| 189 | $limit = [ 'offset' => 0, 'count' => $this->limit ]; |
| 190 | $this->db->getMapper('report')->delete($this->filter, $this->order, $limit); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns a list of values for each filter item except for `before_time` |
| 195 | * |
| 196 | * @return array An key-value array, where the key is the filter item name |
| 197 | * and the value is an array of possible values for the item |
| 198 | */ |
| 199 | public function getFilterList(): array |
| 200 | { |
| 201 | $domainMapper = $this->db->getMapper('domain'); |
| 202 | $reportMapper = $this->db->getMapper('report'); |
| 203 | $user_id = $this->user->id(); |
| 204 | return [ |
| 205 | 'domain' => $domainMapper->names($user_id), |
| 206 | 'month' => $reportMapper->months($user_id), |
| 207 | 'organization' => $reportMapper->organizations($user_id), |
| 208 | 'dkim' => [ 'pass', 'fail' ], |
| 209 | 'spf' => [ 'pass', 'fail' ], |
| 210 | 'disposition' => [ 'none', 'reject', 'quarantine' ], |
| 211 | 'status' => [ 'read', 'unread' ] |
| 212 | ]; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Resets the sort params |
| 217 | * |
| 218 | * @return void |
| 219 | */ |
| 220 | private function resetOrderParams(): void |
| 221 | { |
| 222 | $this->order = [ |
| 223 | 'field' => 'begin_time', |
| 224 | 'direction' => 'descent' |
| 225 | ]; |
| 226 | } |
| 227 | } |