Debian Codegraph @

classes/Admin.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-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 Admin 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;
33
34 use Liuch\DmarcSrg\Mail\MailBoxes;
35 use Liuch\DmarcSrg\Directories\DirectoryList;
36 use Liuch\DmarcSrg\Exception\LogicException;
37 use Liuch\DmarcSrg\RemoteFilesystems\RemoteFilesystemList;
38
39 /**
40 * It's the main class for accessing administration functions.
41 */
42 class Admin
43 {
44 /** @var Core */
45 private $core = null;
46
47 /**
48 * The constructor of the class
49 *
50 * @param Core $core Instance of the Core class
51 */
52 public function __construct($core)
53 {
54 $this->core = $core;
55 }
56
57 /**
58 * Returns information about the database, directories, and mailboxes as an array.
59 *
60 * @return array Contains fields: `database`, `state`, `mailboxes`, `directories`.
61 */
62 public function state(): array
63 {
64 $res = [];
65 $res['database'] = $this->core->database()->state();
66 $res['mailboxes'] = (new MailBoxes())->list();
67 $res['directories'] = array_map(function ($dir) {
68 return $dir->toArray();
69 }, (new DirectoryList())->list());
70 $res['remotefs'] = array_map(function ($fs) {
71 return $fs->toArray();
72 }, (new RemoteFilesystemList(true))->list());
73
74 if ($res['database']['correct'] ?? false) {
75 $res['state'] = 'Ok';
76 } else {
77 $res['state'] = 'Err';
78 }
79
80 return $res;
81 }
82
83 /**
84 * Checks the availability of report sources.
85 *
86 * @param int $id Id of the checked source. If $id == 0 then all available sources with the passed type
87 * will be checked.
88 * @param string $type Type of the checked source.
89 *
90 * @return array Result array with `error_code` and `message` fields.
91 * For one resource and if there is no error,
92 * a field `status` will be added to the result.
93 */
94 public function checkSource(int $id, string $type): array
95 {
96 switch ($type) {
97 case 'mailbox':
98 $this->core->checkDependencies('imap,xml,zip');
99 return (new MailBoxes())->check($id);
100 case 'directory':
101 $this->core->checkDependencies('xml,zip');
102 return (new DirectoryList())->check($id);
103 case 'remotefs':
104 $this->core->checkDependencies('flyfs,xml,zip');
105 return (new RemoteFilesystemList(true))->check($id);
106 }
107 throw new LogicException('Unknown resource type');
108 }
109 }