Debian Codegraph @

public/files.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) 2020-2025 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 namespace Liuch\DmarcSrg;
24
25 use Liuch\DmarcSrg\Users\User;
26 use Liuch\DmarcSrg\Mail\MailBoxes;
27 use Liuch\DmarcSrg\Report\ReportFetcher;
28 use Liuch\DmarcSrg\Sources\MailboxSource;
29 use Liuch\DmarcSrg\Sources\DirectorySource;
30 use Liuch\DmarcSrg\Sources\UploadedFilesSource;
31 use Liuch\DmarcSrg\Sources\RemoteFilesystemSource;
32 use Liuch\DmarcSrg\Directories\DirectoryList;
33 use Liuch\DmarcSrg\Exception\AuthException;
34 use Liuch\DmarcSrg\Exception\RuntimeException;
35 use Liuch\DmarcSrg\Exception\ForbiddenException;
36 use Liuch\DmarcSrg\RemoteFilesystems\RemoteFilesystemList;
37
38 require realpath(__DIR__ . '/..') . '/init.php';
39
40 if (Core::requestMethod() == 'GET') {
41 if (!Core::isJson()) {
42 Core::instance()->sendHtml();
43 return;
44 }
45
46 try {
47 $auth = Core::instance()->auth();
48
49 $auth->isAllowed(User::LEVEL_MANAGER);
50 $res = [];
51 $up_max = ini_get('max_file_uploads');
52 if ($up_max) {
53 $res['upload_max_file_count'] = intval($up_max);
54 }
55 $up_size = ini_get('upload_max_filesize');
56 if ($up_size) {
57 $ch = strtolower($up_size[strlen($up_size) - 1]);
58 $up_size = intval($up_size);
59 switch ($ch) {
60 case 'g':
61 $up_size *= 1024;
62 // no break
63 case 'm':
64 $up_size *= 1024;
65 // no break
66 case 'k':
67 $up_size *= 1024;
68 // no break
69 }
70 $res['upload_max_file_size'] = $up_size;
71 }
72
73 try {
74 $auth->isAllowed(User::LEVEL_ADMIN);
75 $res['mailboxes'] = array_map(function ($mb) {
76 return [
77 'id' => $mb['id'],
78 'name' => $mb['name'],
79 'host' => $mb['host'],
80 'mailbox' => $mb['mailbox']
81 ];
82 }, (new MailBoxes())->list());
83 $dmap = [
84 [ 'directories', (new DirectoryList())->list() ]
85 ];
86 try {
87 Core::instance()->checkDependencies('flyfs');
88 $dmap[] = [ 'remotefs', (new RemoteFilesystemList(true))->list() ];
89 } catch (RuntimeException $e) {
90 }
91 foreach ($dmap as $it) {
92 $dirs = [];
93 foreach ($it[1] as $dir) {
94 $da = $dir->toArray();
95 try {
96 $da['files'] = $dir->count();
97 } catch (RuntimeException $e) {
98 $da['error'] = true;
99 }
100 $dirs[] = $da;
101 }
102 $res[$it[0]] = $dirs;
103 }
104 } catch (ForbiddenException $e) {
105 }
106
107 Core::sendJson($res);
108 } catch (RuntimeException $e) {
109 Core::sendJson(ErrorHandler::exceptionResult($e));
110 }
111 return;
112 }
113
114 if (Core::requestMethod() == 'POST') {
115 try {
116 $core = Core::instance();
117 $data = Core::getJsonData();
118 if ($data) {
119 $core->auth()->isAllowed(User::LEVEL_ADMIN);
120
121 if (isset($data['cmd'])) {
122 $cmd_id = array_search($data['cmd'], [ 'load-mailbox', 'load-directory', 'load-remotefs' ]);
123 if ($cmd_id !== false) {
124 if (isset($data['ids']) && gettype($data['ids']) === 'array' && count($data['ids']) > 0) {
125 $done = [];
126 $slst = [];
127 switch ($cmd_id) {
128 case 0:
129 $core->checkDependencies('imap,xml,zip');
130 $list = new MailBoxes();
131 break;
132 case 1:
133 $core->checkDependencies('xml,zip');
134 $list = new DirectoryList();
135 break;
136 case 2:
137 $core->checkDependencies('flyfs,xml,zip');
138 $list = new RemoteFilesystemList(true);
139 break;
140 default:
141 $list = [];
142 }
143 foreach ($data['ids'] as $id) {
144 $dir_id = gettype($id) === 'integer' ? $id : -1;
145 if (!in_array($id, $done, true)) {
146 $done[] = $id;
147 if ($cmd_id === 0) {
148 $slst[] = new MailboxSource($list->mailbox($dir_id));
149 } elseif ($cmd_id === 1) {
150 $slst[] = new DirectorySource($list->directory($dir_id));
151 } elseif ($cmd_id === 2) {
152 $slst[] = new RemoteFilesystemSource($list->filesystem($dir_id));
153 }
154 }
155 }
156 if (count($slst) > 0) {
157 $results = [];
158 foreach ($slst as $sou) {
159 $sres = (new ReportFetcher($sou))->fetch();
160 foreach ($sres as &$r) {
161 $results[] = $r;
162 }
163 unset($r);
164 }
165 Core::sendJson(ReportFetcher::makeSummaryResult($results));
166 return;
167 }
168 }
169 }
170 }
171 } elseif (isset($_FILES['report_file']) && isset($_POST['cmd']) && $_POST['cmd'] === 'upload-report') {
172 $core->auth()->isAllowed(User::LEVEL_MANAGER);
173 $core->checkDependencies('xml,zip');
174
175 $results = (new ReportFetcher(new UploadedFilesSource($_FILES['report_file'])))->fetch();
176 Core::sendJson(ReportFetcher::makeSummaryResult($results));
177 return;
178 }
179 } catch (RuntimeException $e) {
180 Core::sendJson(ErrorHandler::exceptionResult($e));
181 return;
182 }
183 }
184
185 Core::sendBad();