Debian Codegraph @

classes/Report/ReportFetcher.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2021-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 the class ReportFetcher
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 use Liuch\DmarcSrg\ErrorHandler;
36 use Liuch\DmarcSrg\Report\Report;
37 use Liuch\DmarcSrg\Sources\Source;
38 use Liuch\DmarcSrg\ReportLog\ReportLogItem;
39 use Liuch\DmarcSrg\Exception\RuntimeException;
40
41 /**
42 * This class is designed to fetch report files from report sources and store them to the database.
43 */
44 class ReportFetcher
45 {
46 private $source = null;
47
48 /**
49 * It's the constructor of the class.
50 *
51 * @param Source $sou Source for fetching report files.
52 *
53 * @return void
54 */
55 public function __construct($sou)
56 {
57 $this->source = $sou;
58 }
59
60 /**
61 * Retrieves report files from the source and stores them in the database
62 * taking into account the limits from the configuration file.
63 *
64 * @return array Array of results.
65 */
66 public function fetch(): array
67 {
68 try {
69 $this->source->rewind();
70 } catch (RuntimeException $e) {
71 return [[ 'source_error' => $e->getMessage() ]];
72 }
73
74 $core = Core::instance();
75 $stype = $this->source->type();
76 switch ($stype) {
77 case Source::SOURCE_MAILBOX:
78 $s_act = $core->config('fetcher/mailboxes/when_done', '');
79 $f_act = $core->config('fetcher/mailboxes/when_failed', '');
80 $limit = $core->config('fetcher/mailboxes/messages_maximum', 0);
81 break;
82 case Source::SOURCE_DIRECTORY:
83 $s_act = $core->config('fetcher/directories/when_done', '');
84 $f_act = $core->config('fetcher/directories/when_failed', '');
85 $limit = $core->config('fetcher/directories/files_maximum', 0);
86 break;
87 case Source::SOURCE_REMOTE_FILESYSTEM:
88 $s_act = $core->config('fetcher/remote_filesystems/when_done', '');
89 $f_act = $core->config('fetcher/remote_filesystems/when_failed', '');
90 $limit = $core->config('fetcher/remote_filesystems/files_maximum', 0);
91 break;
92 case Source::SOURCE_UPLOADED_FILE:
93 $s_act = '';
94 $f_act = '';
95 $limit = 0;
96 break;
97 default:
98 throw new RuntimeException('Unknown source type');
99 }
100 $limit = intval($limit);
101 if ($stype === Source::SOURCE_MAILBOX ||
102 $stype === Source::SOURCE_DIRECTORY ||
103 $stype === Source::SOURCE_REMOTE_FILESYSTEM
104 ) {
105 $this->source->setParams([
106 'when_done' => $s_act,
107 'when_failed' => $f_act
108 ]);
109 }
110
111 $results = [];
112 while ($this->source->valid()) {
113 $result = null;
114 $fname = null;
115 $report = null;
116 $success = false;
117 $err_msg = null;
118
119 // Extracting and saving reports
120 try {
121 $rfile = $this->source->current();
122 $fname = $rfile->filename();
123 $report = Report::fromXmlFile($rfile->datastream());
124 $result = $report->save($fname);
125 $success = true;
126 } catch (RuntimeException $e) {
127 $err_msg = $e->getMessage();
128 $result = ErrorHandler::exceptionResult($e);
129 }
130 unset($rfile);
131
132 // Post processing
133 try {
134 if ($success) {
135 $this->source->accepted();
136 } else {
137 $this->source->rejected();
138 }
139 } catch (RuntimeException $e) {
140 $err_msg = $e->getMessage();
141 $result['post_processing_message'] = $err_msg;
142 }
143
144 // Adding a record to the log.
145 if (!$err_msg) {
146 $log = ReportLogItem::success($stype, $report, $fname, null)->save();
147 } else {
148 $log = ReportLogItem::failed($stype, $report, $fname, $err_msg)->save();
149 if ($this->source->type() === Source::SOURCE_MAILBOX) {
150 $msg = $this->source->mailMessage();
151 $ov = $msg->overview();
152 if (isset($ov['from'])) {
153 $result['emailed_from'] = $ov['from'];
154 }
155 if (isset($ov['date'])) {
156 $result['emailed_date'] = $ov['date']->format(\DateTime::RFC1123);
157 }
158 }
159 if ($report) {
160 if (isset($report->report_id)) {
161 $result['report_id'] = $report->report_id;
162 }
163 }
164 }
165 unset($report);
166
167 // Adding result to the results array.
168 $results[] = $result;
169
170 // Checking the fetcher limits
171 if ($limit > 0) {
172 if (--$limit === 0) {
173 break;
174 }
175 }
176
177 $this->source->next();
178 }
179 return $results;
180 }
181
182 /**
183 * Generates the final result based on the results of loading individual report files.
184 *
185 * @param array $results Array with results of loading report files.
186 *
187 * @return array Array of the final result to be sent to the client.
188 */
189 public static function makeSummaryResult(array $results): array
190 {
191 $reps = [];
192 $others = [];
193 $r_count = 0;
194 $loaded = 0;
195 foreach ($results as &$r) {
196 if (isset($r['source_error'])) {
197 $others[] = $r['source_error'];
198 } else {
199 $reps[] = $r;
200 ++$r_count;
201 if (!isset($r['error_code']) || $r['error_code'] === 0) {
202 ++$loaded;
203 }
204 if (isset($r['post_processing_message'])) {
205 $others[] = $r['post_processing_message'];
206 }
207 }
208 }
209 unset($r);
210
211 $result = null;
212 $o_count = count($others);
213 if ($r_count + $o_count === 1) {
214 if ($r_count === 1) {
215 $result = $reps[0];
216 } else {
217 $result = [
218 'error_code' => -1,
219 'message' => $others[0]
220 ];
221 }
222 } else {
223 $err_code = null;
224 $message = null;
225 if ($loaded === $r_count) {
226 $err_code = 0;
227 if ($r_count > 0) {
228 $message = strval($r_count) . ' report files have been loaded successfully';
229 } elseif ($o_count === 0) {
230 $message = 'There are no report files to load';
231 } else {
232 $err_code = -1;
233 }
234 } else {
235 $err_code = -1;
236 if ($loaded > 0) {
237 $message = "Only {$loaded} of the {$r_count} report files have been loaded";
238 } else {
239 $message = "None of the {$r_count} report files has been loaded";
240 }
241 }
242 $result['error_code'] = $err_code;
243 $result['message'] = $message;
244 if ($r_count > 0) {
245 $result['results'] = $reps;
246 }
247 if ($o_count > 0) {
248 $result['other_errors'] = $others;
249 }
250 }
251 return $result;
252 }
253 }