1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Mail basic class. |
7
|
|
|
* |
8
|
|
|
* @package App |
9
|
|
|
* |
10
|
|
|
* @copyright YetiForce S.A. |
11
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
12
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
13
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Mail |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Get smtp server by id. |
19
|
|
|
* |
20
|
|
|
* @param int $smtpId |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public static function getSmtpById(int $smtpId): array |
25
|
|
|
{ |
26
|
|
|
if (Cache::has('SmtpServer', $smtpId)) { |
27
|
|
|
return Cache::get('SmtpServer', $smtpId); |
28
|
|
|
} |
29
|
|
|
$servers = static::getAll(); |
30
|
|
|
$smtp = []; |
31
|
|
|
if (isset($servers[$smtpId])) { |
32
|
|
|
$smtp = $servers[$smtpId]; |
33
|
|
|
} |
34
|
|
|
Cache::save('SmtpServer', $smtpId, $smtp, Cache::LONG); |
35
|
|
|
|
36
|
|
|
return $smtp; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get a list of all smtp servers. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public static function getAll() |
45
|
|
|
{ |
46
|
|
|
if (Cache::has('SmtpServers', 'all')) { |
47
|
|
|
return Cache::get('SmtpServers', 'all'); |
48
|
|
|
} |
49
|
|
|
$all = (new Db\Query())->from('s_#__mail_smtp')->indexBy('id')->all(Db::getInstance('admin')); |
50
|
|
|
Cache::save('SmtpServers', 'all', $all, Cache::LONG); |
51
|
|
|
|
52
|
|
|
return $all; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get default smtp Id. |
57
|
1 |
|
* |
58
|
|
|
* @return int |
59
|
1 |
|
*/ |
60
|
|
|
public static function getDefaultSmtp() |
61
|
|
|
{ |
62
|
1 |
|
if (Cache::has('DefaultSmtp', '')) { |
63
|
1 |
|
return Cache::get('DefaultSmtp', ''); |
64
|
1 |
|
} |
65
|
|
|
$id = (new Db\Query())->select(['id'])->from('s_#__mail_smtp')->where(['default' => 1])->scalar(Db::getInstance('admin')); |
66
|
1 |
|
if (!$id) { |
67
|
|
|
$id = (new Db\Query())->select(['id'])->from('s_#__mail_smtp')->limit(1)->scalar(Db::getInstance('admin')); |
68
|
1 |
|
} |
69
|
|
|
Cache::save('DefaultSmtp', '', $id, Cache::LONG); |
70
|
|
|
|
71
|
|
|
return $id; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get template list for module. |
76
|
|
|
* |
77
|
|
|
* @param string $moduleName |
78
|
|
|
* @param string $type |
79
|
|
|
* @param bool $hideSystem |
80
|
|
|
* @param int|null $userId |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function getTemplateList(string $moduleName = '', string $type = '', bool $hideSystem = true, ?int $userId = null) |
85
|
|
|
{ |
86
|
|
|
$queryGenerator = new \App\QueryGenerator('EmailTemplates', $userId ?? \App\User::getCurrentUserId()); |
87
|
|
|
$queryGenerator->setFields(['id', 'name', 'module_name']); |
88
|
|
|
if ($moduleName) { |
89
|
|
|
$queryGenerator->addCondition('module_name', $moduleName, 'e'); |
90
|
|
|
} |
91
|
|
|
if ($type) { |
92
|
|
|
$queryGenerator->addCondition('email_template_type', $type, 'e'); |
93
|
|
|
} |
94
|
|
|
if ($hideSystem) { |
95
|
|
|
$queryGenerator->addNativeCondition(['u_#__emailtemplates.sys_name' => [null, '']]); |
96
|
|
|
} |
97
|
|
|
return $queryGenerator->createQuery()->all(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get mail template. |
102
|
|
|
* |
103
|
|
|
* @param int|string $id |
104
|
|
|
* @param bool $attachments |
105
|
1 |
|
* |
106
|
|
|
* @return array |
107
|
1 |
|
*/ |
108
|
1 |
|
public static function getTemplate($id, bool $attachments = true): array |
109
|
|
|
{ |
110
|
1 |
|
if (!is_numeric($id)) { |
111
|
1 |
|
$id = self::getTemplateIdFromSysName($id); |
112
|
1 |
|
} |
113
|
|
|
if (!$id || !\App\Record::isExists($id, 'EmailTemplates')) { |
114
|
|
|
return []; |
115
|
|
|
} |
116
|
|
|
$template = \Vtiger_Record_Model::getInstanceById($id, 'EmailTemplates'); |
117
|
|
|
if (!$attachments) { |
118
|
|
|
return $template->getData(); |
119
|
|
|
} |
120
|
|
|
return array_merge( |
121
|
|
|
$template->getData(), static::getAttachmentsFromTemplate($template->getId()) |
122
|
|
|
); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
1 |
|
/** |
126
|
1 |
|
* Get template ID. |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
1 |
|
* |
130
|
1 |
|
* @return int|null |
131
|
1 |
|
*/ |
132
|
1 |
|
public static function getTemplateIdFromSysName(string $name): ?int |
133
|
1 |
|
{ |
134
|
1 |
|
$cacheName = 'TemplateIdFromSysName'; |
135
|
|
|
if (Cache::has($cacheName, '')) { |
136
|
1 |
|
$templates = Cache::get($cacheName, ''); |
137
|
|
|
} else { |
138
|
|
|
$queryGenerator = new \App\QueryGenerator('EmailTemplates'); |
139
|
|
|
$queryGenerator->setFields(['id']); |
140
|
|
|
$queryGenerator->permissions = false; |
141
|
|
|
$queryGenerator->addNativeCondition(['not', ['sys_name' => null]]); |
142
|
|
|
$templates = $queryGenerator->createQuery()->select(['sys_name', 'emailtemplatesid'])->createCommand()->queryAllByGroup(); |
143
|
|
|
Cache::save($cacheName, '', $templates, Cache::LONG); |
144
|
|
|
} |
145
|
|
|
return $templates[$name] ?? null; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
/** |
149
|
|
|
* Get attachments email template. |
150
|
|
|
* |
151
|
1 |
|
* @param int|string $id |
152
|
1 |
|
* |
153
|
1 |
|
* @return array |
154
|
1 |
|
*/ |
155
|
1 |
|
public static function getAttachmentsFromTemplate($id) |
156
|
|
|
{ |
157
|
|
|
if (Cache::has('MailAttachmentsFromTemplete', $id)) { |
158
|
1 |
|
return Cache::get('MailAttachmentsFromTemplete', $id); |
159
|
|
|
} |
160
|
1 |
|
$ids = (new \App\Db\Query())->select(['u_#__documents_emailtemplates.crmid'])->from('u_#__documents_emailtemplates') |
161
|
|
|
->innerJoin('vtiger_crmentity', 'u_#__documents_emailtemplates.relcrmid = vtiger_crmentity.crmid') |
162
|
|
|
->where(['vtiger_crmentity.deleted' => 0, 'u_#__documents_emailtemplates.relcrmid' => $id])->column(); |
163
|
|
|
$attachments = []; |
164
|
|
|
if ($ids) { |
165
|
|
|
$attachments['attachments'] = ['ids' => $ids]; |
166
|
|
|
} |
167
|
|
|
Cache::save('MailAttachmentsFromTemplete', $id, $attachments, Cache::LONG); |
168
|
|
|
return $attachments; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get attachments from document. |
173
|
|
|
* |
174
|
|
|
* @param int|int[] $ids |
175
|
|
|
* @param mixed $returnOnlyName |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public static function getAttachmentsFromDocument($ids, $returnOnlyName = true) |
180
|
|
|
{ |
181
|
|
|
$cacheId = "$returnOnlyName|" . \is_array($ids) ? implode(',', $ids) : $ids; |
182
|
|
|
if (Cache::has('MailAttachmentsFromDocument', $cacheId)) { |
183
|
|
|
return Cache::get('MailAttachmentsFromDocument', $cacheId); |
184
|
|
|
} |
185
|
|
|
$query = (new \App\Db\Query())->select(['vtiger_attachments.*'])->from('vtiger_attachments') |
186
|
|
|
->innerJoin('vtiger_seattachmentsrel', 'vtiger_attachments.attachmentsid = vtiger_seattachmentsrel.attachmentsid') |
187
|
|
|
->where(['vtiger_seattachmentsrel.crmid' => $ids]); |
188
|
|
|
$attachments = []; |
189
|
|
|
$dataReader = $query->createCommand()->query(); |
190
|
|
|
while ($row = $dataReader->read()) { |
191
|
|
|
$filePath = realpath(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . $row['path'] . $row['attachmentsid']); |
192
|
|
|
if (is_file($filePath)) { |
193
|
|
|
$attachments[$filePath] = $returnOnlyName ? Purifier::decodeHtml($row['name']) : $row; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
Cache::save('MailAttachmentsFromDocument', $cacheId, $attachments, Cache::LONG); |
197
|
|
|
return $attachments; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Check if the user has access to the mail client. |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public static function checkMailClient(): bool |
206
|
|
|
{ |
207
|
|
|
if (Cache::staticHas('MailCheckMailClient')) { |
208
|
|
|
return Cache::staticGet('MailCheckMailClient'); |
209
|
|
|
} |
210
|
|
|
$return = \Config\Main::$isActiveSendingMails && \App\Privilege::isPermitted('OSSMail'); |
|
|
|
|
211
|
|
|
Cache::staticSave('MailCheckMailClient', '', $return); |
212
|
|
|
return $return; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check if the user has access to the internal mail client. |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
public static function checkInternalMailClient(): bool |
221
|
|
|
{ |
222
|
|
|
if (Cache::staticHas('MailCheckInternalMailClient')) { |
223
|
|
|
return Cache::staticGet('MailCheckInternalMailClient'); |
224
|
|
|
} |
225
|
|
|
$return = self::checkMailClient() && 1 === (int) \App\User::getCurrentUserModel()->getDetail('internal_mailer') && file_exists(ROOT_DIRECTORY . '/public_html/modules/OSSMail/roundcube/'); |
226
|
|
|
Cache::staticSave('MailCheckInternalMailClient', '', $return); |
227
|
|
|
return $return; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|