Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pop3
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 18
992
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 __destruct
n/a
0 / 0
n/a
0 / 0
1
 connectInbox
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getBoxes
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 renameBox
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 deleteBox
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 createBox
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 countMail
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getMailboxInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 countRecent
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 countUnseen
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 search
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 copyMail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 moveMail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 deleteMail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaders
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getHeaderInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 closeInbox
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Message\Mail
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Message\Mail;
16
17/**
18 * Imap mail class.
19 *
20 * @package phpOMS\Message\Mail
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25class Pop3 implements MailBoxInterface
26{
27    /**
28     * Connection flags
29     *
30     * @var string
31     * @since 1.0.0
32     */
33    public string $flags = '/pop3';
34
35    /**
36     * Current inbox
37     *
38     * Boxes can be in parent boxes. The path must be delimitted with . e.g. INBOX.Subdir1.Subdir2
39     *
40     * @var string
41     * @since 1.0.0
42     */
43    private $box = null;
44
45    /**
46     * Host.
47     *
48     * @var string
49     * @since 1.0.0
50     */
51    public string $host = 'localhost';
52
53    /**
54     * The default port.
55     *
56     * @var int
57     * @since 1.0.0
58     */
59    public int $port = 110;
60
61    /**
62     * Encryption
63     *
64     * @var string
65     * @since 1.0.0
66     */
67    public string $encryption = EncryptionType::NONE;
68
69    /**
70     * Username.
71     *
72     * @var string
73     * @since 1.0.0
74     */
75    public string $username = '';
76
77    /**
78     * Password.
79     *
80     * @var string
81     * @since 1.0.0
82     */
83    public string $password = '';
84
85    /**
86     * {@inheritdoc}
87     */
88    public function __construct(string $user = '', string $pass = '', int $port = 110, string $encryption = EncryptionType::NONE)
89    {
90        $this->username   = $user;
91        $this->password   = $pass;
92        $this->port       = $port;
93        $this->encryption = $encryption;
94        $this->flags .= $this->encryption !== EncryptionType::NONE ? '/ssl' : '';
95    }
96
97    /**
98     * Destructor.
99     *
100     * @since 1.0.0
101     * @codeCoverageIgnore
102     */
103    public function __destruct()
104    {
105        $this->closeInbox();
106    }
107
108    /**
109     * {@inheritdoc}
110     */
111    public function connectInbox() : bool
112    {
113        $this->mailbox = ($tmp = @\imap_open(
114            '{' . $this->host . ':' . $this->port . $this->flags . '}',
115            $this->username, $this->password
116        )) === false ? null : $tmp;
117
118        return \is_resource($this->mailbox);
119    }
120
121    /**
122     * Get mailboxes
123     *
124     * @return array
125     *
126     * @since 1.0.0
127     */
128    public function getBoxes() : array
129    {
130        $list = \imap_list($this->mailbox, $reference = '{' . $this->host . ':' . $this->port . $this->flags . '}', '*');
131        if (!\is_array($list)) {
132            return []; // @codeCoverageIgnore
133        }
134
135        foreach ($list as $key => $value) {
136            $list[$key] = \str_replace($reference, '', \imap_utf7_decode($value));
137        }
138
139        return $list;
140    }
141
142    /**
143     * Rename mailbox
144     *
145     * @param string $old Old name
146     * @param string $new New name
147     *
148     * @return bool
149     *
150     * @since 1.0.0
151     */
152    public function renameBox(string $old, string $new) : bool
153    {
154        return \imap_renamemailbox(
155            $this->mailbox,
156            \imap_utf7_encode('{' . $this->host . ':' . $this->port . $this->flags . '}' . $old),
157            \imap_utf7_encode('{' . $this->host . ':' . $this->port . $this->flags . '}' . $new)
158        );
159    }
160
161    /**
162     * Delete mailbox
163     *
164     * @param string $box Box to delete
165     *
166     * @return bool
167     *
168     * @since 1.0.0
169     */
170    public function deleteBox(string $box) : bool
171    {
172        return \imap_deletemailbox(
173            $this->mailbox,
174            \imap_utf7_encode('{' . $this->host . ':' . $this->port . $this->flags . '}' . $box)
175        );
176    }
177
178    /**
179     * Create mailbox
180     *
181     * @param string $box Box to create
182     *
183     * @return bool
184     *
185     * @since 1.0.0
186     */
187    public function createBox(string $box) : bool
188    {
189        return \imap_createmailbox(
190            $this->mailbox,
191            \imap_utf7_encode('{' . $this->host . ':' . $this->port . $this->flags . '}' . $box)
192        );
193    }
194
195    /**
196     * {@inheritdoc}
197     */
198    public function countMail(string $box) : int
199    {
200        if ($this->box !== $box) {
201            \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
202            $this->box = $box;
203        }
204
205        return \imap_num_msg($this->mailbox);
206    }
207
208    /**
209     * {@inheritdoc}
210     */
211    public function getMailboxInfo(string $box) : object
212    {
213        return \imap_status($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box,  \SA_ALL);
214    }
215
216    /**
217     * {@inheritdoc}
218     */
219    public function countRecent(string $box) : int
220    {
221        if ($this->box !== $box) {
222            \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
223            $this->box = $box;
224        }
225
226        return \imap_num_recent($this->mailbox);
227    }
228
229    /**
230     * {@inheritdoc}
231     */
232    public function countUnseen(string $box) : int
233    {
234        if ($this->box !== $box) {
235            \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
236            $this->box = $box;
237        }
238
239        return \count(\imap_search($this->mailbox, 'UNSEEN'));
240    }
241
242    /**
243     * {@inheritdoc}
244     */
245    public function search(
246        string $box,
247        string $subject = '',
248        string $body = '',
249        string $to = '',
250        string $cc = '',
251        string $from = '',
252        string $bcc = '',
253        \DateTime $before = null,
254        \DateTime $since = null,
255        \DateTime $on = null,
256        bool $deleted = false,
257        bool $flagged = false
258    ) : array
259    {
260        if ($this->box !== $box) {
261            \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
262            $this->box = $box;
263        }
264
265        return [];
266    }
267
268    /**
269     * Copy message to another mailbox
270     *
271     * @param string|array $messages Messages to copy
272     * @param string       $box      Box to copy messages to
273     *
274     * @return bool
275     *
276     * @since 1.0.0
277     */
278    public function copyMail(string | array $messages, string $box) : bool
279    {
280        return \imap_mail_copy($this->mailbox, \is_string($messages) ? $messages : \implode(',', $messages), '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
281    }
282
283    /**
284     * Move message to another mailbox
285     *
286     * @param string|array $messages Messages to copy
287     * @param string       $box      Box to copy messages to
288     *
289     * @return bool
290     *
291     * @since 1.0.0
292     */
293    public function moveMail(string | array $messages, string $box) : bool
294    {
295        return \imap_mail_copy($this->mailbox, \is_string($messages) ? $messages : \implode(',', $messages), '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
296    }
297
298    /**
299     * Delete message
300     *
301     * @param int $msg Message number (not uid)
302     *
303     * @return bool
304     *
305     * @since 1.0.0
306     */
307    public function deleteMail(int $msg) : bool
308    {
309        return \imap_delete($this->mailbox, $msg);
310    }
311
312    /**
313     * {@inheritdoc}
314     */
315    public function getHeaders(string $box) : array
316    {
317        if ($this->box !== $box) {
318            \imap_reopen($this->mailbox, '{' . $this->host . ':' . $this->port . $this->flags . '}' . $box);
319            $this->box = $box;
320        }
321
322        return \imap_headers($this->mailbox);
323    }
324
325    /**
326     * Get message header information
327     *
328     * @param int $msg Message number (not uid)
329     *
330     * @return object
331     *
332     * @since 1.0.0
333     */
334    public function getHeaderInfo(int $msg) : object
335    {
336        return \imap_headerinfo($this->mailbox, $msg);
337    }
338
339    /**
340     * {@inheritdoc}
341     */
342    public function getMail(int $msg) : Email
343    {
344        return new Email();
345    }
346
347    /**
348     * {@inheritdoc}
349     */
350    public function closeInbox() : void
351    {
352        if (\is_resource($this->mailbox)) {
353            \imap_close($this->mailbox);
354        }
355    }
356}