| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
} |