Debian Codegraph @

classes/Sources/UploadedFilesSource.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 UploadedFilesSource
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\ReportFile\ReportFile;
35 use Liuch\DmarcSrg\Exception\SoftException;
36
37 /**
38 * This class is designed to process report files from uploaded files.
39 */
40 class UploadedFilesSource extends Source
41 {
42 /** @var int */
43 private $index = 0;
44
45 /**
46 * Returns an instance of the ReportFile class for the current file.
47 *
48 * @return ReportFile
49 */
50 public function current(): object
51 {
52 if ($this->data['error'][$this->index] !== UPLOAD_ERR_OK) {
53 throw new SoftException('Failed to upload the report file');
54 }
55
56 $realfname = $this->data['name'][$this->index];
57 $tempfname = $this->data['tmp_name'][$this->index];
58 if (!is_uploaded_file($tempfname)) {
59 throw new SoftException('Possible file upload attack');
60 }
61
62 return ReportFile::fromFile($tempfname, $realfname, false);
63 }
64
65 /**
66 * Returns the index of the currect file.
67 *
68 * @return int
69 */
70 public function key(): int
71 {
72 return $this->index;
73 }
74
75 /**
76 * Moves forward to the next file.
77 *
78 * @return void
79 */
80 public function next(): void
81 {
82 ++$this->index;
83 }
84
85 /**
86 * Rewinds the position to the first file.
87 *
88 * @return void
89 */
90 public function rewind(): void
91 {
92 $this->index = 0;
93 }
94
95 /**
96 * Checks if the current postion is valid
97 *
98 * @return bool
99 */
100 public function valid(): bool
101 {
102 return isset($this->data['name'][$this->index]);
103 }
104
105 /**
106 * Returns type of the source.
107 *
108 * @return int
109 */
110 public function type(): int
111 {
112 return Source::SOURCE_UPLOADED_FILE;
113 }
114 }