-
Notifications
You must be signed in to change notification settings - Fork 18
/
Imap.php
143 lines (126 loc) · 3.54 KB
/
Imap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace SecIT\ImapBundle\Service;
use PhpImap\Mailbox;
/**
* Class Imap.
*
* @author Tomasz Gemza
*/
class Imap
{
/**
* @var array
*/
protected $connections;
/**
* @var array|Mailbox[]
*/
protected $instances = [];
/**
* Imap constructor.
*
* @param array $connections
*/
public function __construct(array $connections)
{
$this->connections = $connections;
}
/**
* Get a connection to the specified mailbox.
*
* @param string $name
* @param bool $flush force to create a new Mailbox instance
*
* @return Mailbox
*
* @throws \Exception
*/
public function get($name, $flush = false)
{
if ($flush || !isset($this->instances[$name])) {
$this->instances[$name] = $this->getMailbox($name);
}
return $this->instances[$name];
}
/**
* Test mailbox connection.
*
* @param string $name
* @param bool $throwExceptions set to true if you'd like to get an exception on error instead of "return false"
*
* @return bool
*
* @throws \Exception
*/
public function testConnection($name, $throwExceptions = false)
{
try {
return $this->getMailbox($name)->getImapStream(true) !== null;
} catch (\Exception $exception) {
if ($throwExceptions) {
throw $exception;
}
}
return false;
}
/**
* Get new mailbox instance.
*
* @param string $name
*
* @return Mailbox
*
* @throws \Exception
*/
protected function getMailbox($name)
{
if (!isset($this->connections[$name])) {
throw new \Exception(sprintf('Imap connection %s is not configured.', $name));
}
$config = $this->connections[$name];
if (isset($config['attachments_dir'])) {
$this->checkAttachmentsDir(
$config['attachments_dir'],
$config['create_attachments_dir_if_not_exists'],
$config['created_attachments_dir_permissions']
);
}
return new Mailbox(
$config['mailbox'],
$config['username'],
$config['password'],
$config['attachments_dir'],
$config['server_encoding']
);
}
/**
* Check attachments directory.
*
* @param null|string $directoryPath
* @param bool $createIfNotExists
* @param int $directoryPermissions In decimal format! 775 instead of 0775
*
* @throws \Exception
*/
protected function checkAttachmentsDir($directoryPath, $createIfNotExists, $directoryPermissions)
{
if (!$directoryPath) {
return;
}
if (file_exists($directoryPath)) {
if (!is_dir($directoryPath)) {
throw new \Exception(sprintf('File "%s" exists but it is not a directory', $directoryPath));
}
if (!is_readable($directoryPath) || !is_writable($directoryPath)) {
throw new \Exception(sprintf('Directory "%s" does not have enough access permissions', $directoryPath));
}
} elseif($createIfNotExists) {
$umask = umask(0);
$created = mkdir($directoryPath, decoct($directoryPermissions), true);
umask($umask);
if (!$created) {
throw new \Exception(sprintf('Cannot create the attachments directory "%s"', $directoryPath));
}
}
}
}