Debian Codegraph @

classes/Sources/DirectorySource.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-2023 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 DirectorySource
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\Sources;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\ReportFile\ReportFile;
36 use Liuch\DmarcSrg\Exception\SoftException;
37 use Liuch\DmarcSrg\Exception\RuntimeException;
38
39 /**
40 * This class is designed to process report files from local server directories.
41 */
42 class DirectorySource extends Source
43 {
44 private $path = null;
45 private $list = null;
46 private $index = 0;
47 private $params = null;
48
49 /**
50 * Sets parameters that difine the behavior of the source
51 *
52 * @param $params Key-value array
53 * 'when_done' => one or more rules to be executed after successful report processing
54 * (array|string)
55 * 'when_failed' => one or more rules to be executed after report processing fails
56 * (array|string)
57 *
58 * @return void
59 */
60 public function setParams(array $params): void
61 {
62 $this->params = [];
63 $this->params['when_done'] = SourceAction::fromSetting(
64 $params['when_done'] ?? [],
65 SourceAction::FLAG_BASENAME,
66 'delete'
67 );
68 $this->params['when_failed'] = SourceAction::fromSetting(
69 $params['when_failed'] ?? [],
70 SourceAction::FLAG_BASENAME,
71 'move_to:failed'
72 );
73 }
74
75 /**
76 * Returns an instance of the ReportFile class for the current file.
77 *
78 * @return ReportFile
79 */
80 public function current(): object
81 {
82 return ReportFile::fromFile($this->list[$this->index]);
83 }
84
85 /**
86 * Returns the index of the current file.
87 *
88 * @return int
89 */
90 public function key(): int
91 {
92 return $this->index;
93 }
94
95 /**
96 * Moves forward to the next file.
97 *
98 * @return void
99 */
100 public function next(): void
101 {
102 ++$this->index;
103 }
104
105 /**
106 * Rewinds the position to the first file.
107 *
108 * @return void
109 */
110 public function rewind(): void
111 {
112 if (is_null($this->list)) {
113 $this->path = $this->data->toArray()['location'];
114 if (!is_dir($this->path)) {
115 throw new SoftException("The {$this->path} directory does not exist!");
116 }
117 try {
118 $fs = new \FilesystemIterator($this->path);
119 } catch (\Exception $e) {
120 throw new RuntimeException("Error accessing directory {$this->path}", -1, $e);
121 }
122 $this->list = [];
123 foreach ($fs as $entry) {
124 if ($entry->isFile()) {
125 $this->list[] = $entry->getPathname();
126 }
127 }
128 }
129 if (is_null($this->params)) {
130 $this->setParams([]);
131 }
132 $this->index = 0;
133 }
134
135 /**
136 * Checks if the current postion is valid
137 *
138 * @return bool
139 */
140 public function valid(): bool
141 {
142 return isset($this->list[$this->index]);
143 }
144
145 /**
146 * Processes the accepted report file according to the settings
147 *
148 * @return void
149 */
150 public function accepted(): void
151 {
152 $this->processReportFileActions($this->params['when_done']);
153 }
154
155 /**
156 * Processes the rejected report file according to the settings
157 *
158 * @return void
159 */
160 public function rejected(): void
161 {
162 $this->processReportFileActions($this->params['when_failed']);
163 }
164
165 /**
166 * Returns type of the source.
167 *
168 * @return int
169 */
170 public function type(): int
171 {
172 return Source::SOURCE_DIRECTORY;
173 }
174
175 /**
176 * Logs an error message
177 *
178 * @param string $message
179 */
180 private function logError(string $message): void
181 {
182 Core::instance()->logger()->error($message);
183 }
184
185 /**
186 * Processes the current report file according to settings
187 *
188 * @param array $actions List of actions to apply to the file
189 *
190 * @return void
191 */
192 private function processReportFileActions(array &$actions): void
193 {
194 foreach ($actions as $sa) {
195 switch ($sa->type) {
196 case SourceAction::ACTION_DELETE:
197 $this->deleteReportFile();
198 break;
199 case SourceAction::ACTION_MOVE:
200 $this->moveReportFile($sa->param);
201 break;
202 }
203 }
204 }
205
206 /**
207 * Deletes the current report file
208 *
209 * @return void
210 */
211 private function deleteReportFile(): void
212 {
213 try {
214 unlink($this->list[$this->index]);
215 } catch (\ErrorException $e) {
216 $error_message = "Error deleting file from directory {$this->path}";
217 $this->logError($error_message);
218 throw new RuntimeException($error_message, -1, $e);
219 }
220 }
221
222 /**
223 * Moves the current report file
224 *
225 * @param string $dir_name Directory name where to move the report file to
226 *
227 * @return void
228 */
229 private function moveReportFile(string $dir_name): void
230 {
231 $fdir = $this->path . $dir_name;
232 if (!is_dir($fdir)) {
233 try {
234 mkdir($fdir);
235 } catch (\ErrorException $e) {
236 $e = new RuntimeException("Error creating directory {$fdir}/", -1, $e);
237 $this->logError(strval($e));
238 throw $e;
239 }
240 try {
241 chmod($fdir, 0700);
242 } catch (\ErrorException $e) {
243 $this->logError(strval($e));
244 }
245 }
246 $file = $this->list[$this->index];
247 try {
248 rename($file, $fdir . '/' . basename($file));
249 } catch (\ErrorException $e) {
250 $e = new RuntimeException("Error moving file to directory {$fdir}/", -1, $e);
251 $this->logError(strval($e));
252 throw $e;
253 }
254 }
255 }