Debian Codegraph @

classes/Mail/MailBoxes.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 namespace Liuch\DmarcSrg\Mail;
24
25 use Liuch\DmarcSrg\Core;
26 use Liuch\DmarcSrg\Exception\LogicException;
27
28 class MailBoxes implements \Iterator
29 {
30 private $box_list;
31 private $index = 0;
32
33 public function __construct()
34 {
35 $mailboxes = Core::instance()->config('mailboxes');
36 $library = Core::instance()->config('fetcher/mailboxes/library', '');
37
38 $this->box_list = [];
39 if (is_array($mailboxes)) {
40 $cnt = count($mailboxes);
41 if ($cnt > 0) {
42 if (isset($mailboxes[0])) {
43 for ($i = 0; $i < $cnt; ++$i) {
44 $this->box_list[] = $this->getMailbox($mailboxes[$i], $library);
45 }
46 } else {
47 $this->box_list[] = $this->getMailbox($mailboxes, $library);
48 }
49 }
50 }
51 }
52
53 public function count()
54 {
55 return count($this->box_list);
56 }
57
58 public function list()
59 {
60 $id = 0;
61 $res = [];
62 foreach ($this->box_list as &$mbox) {
63 $id += 1;
64 $res[] = [
65 'id' => $id,
66 'name' => $mbox->name(),
67 'host' => $mbox->host(),
68 'mailbox' => $mbox->folder()
69 ];
70 }
71 unset($mbox);
72 return $res;
73 }
74
75 public function mailbox($id)
76 {
77 if (!is_int($id) || $id <= 0 || $id > count($this->box_list)) {
78 throw new LogicException("Incorrect mailbox Id: {$id}");
79 }
80 return $this->box_list[$id - 1];
81 }
82
83 public function check($id)
84 {
85 if ($id !== 0) {
86 return $this->mailbox($id)->check();
87 }
88
89 $results = [];
90 $err_cnt = 0;
91 $box_cnt = count($this->box_list);
92 for ($i = 0; $i < $box_cnt; ++$i) {
93 $r = $this->box_list[$i]->check();
94 if ($r['error_code'] !== 0) {
95 ++$err_cnt;
96 }
97 $results[] = $r;
98 }
99 $res = [];
100 if ($err_cnt == 0) {
101 $res['error_code'] = 0;
102 $res['message'] = 'Success';
103 } else {
104 $res['error_code'] = -1;
105 $res['message'] = sprintf('%d of the %d mailboxes failed the check', $err_cnt, $box_cnt);
106 }
107 $res['results'] = $results;
108 return $res;
109 }
110
111 public function current(): object
112 {
113 return $this->box_list[$this->index];
114 }
115
116 public function key(): int
117 {
118 return $this->index;
119 }
120
121 public function next(): void
122 {
123 ++$this->index;
124 }
125
126 public function rewind(): void
127 {
128 $this->index = 0;
129 }
130
131 public function valid(): bool
132 {
133 return isset($this->box_list[$this->index]);
134 }
135
136 private function getMailbox(array $params, string $library)
137 {
138 switch ($library) {
139 case 'php-extension':
140 $dir_name = 'PhpExtension';
141 break;
142 case 'imap-engine':
143 $dir_name = 'ImapEngine';
144 break;
145 case '':
146 case 'auto':
147 if (class_exists('\DirectoryTree\ImapEngine\Mailbox')) {
148 $dir_name = 'ImapEngine';
149 } else {
150 $dir_name = 'PhpExtension';
151 }
152 break;
153 default:
154 throw new LogicException("Mailbox: wrong library name '$library'");
155 }
156 $reflection = new \ReflectionClass($this);
157 $class_name = $reflection->getNamespaceName() . '\\' . $dir_name . '\\MailBox';
158 return new $class_name($params);
159 }
160 }