forked from mp107/Roundcube-SMTP-per-Identity-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity_smtp.php
249 lines (222 loc) · 8.23 KB
/
identity_smtp.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
*
* Per identity smtp settings
*
* Description
*
* @version 0.1
* @url skweez.net
*
* MIT License
*
**/
class identity_smtp extends rcube_plugin
{
public $task = 'mail|settings';
private $from_identity;
function init()
{
$this->include_script('identity_smtp.js');
$this->add_texts('localization/', true);
$this->add_hook('message_before_send', array(
$this,
'messageBeforeSend'
));
$this->add_hook('smtp_connect', array(
$this,
'smtpWillConnect'
));
$this->add_hook('identity_form', array(
$this,
'identityFormWillBeDisplayed'
));
$this->add_hook('identity_create', array(
$this,
'identityWasCreated'
));
$this->add_hook('identity_update', array(
$this,
'identityWasUpdated'
));
$this->add_hook('identity_delete', array(
$this,
'identityWasDeleted'
));
}
function smtpLog($message)
{
rcube::write_log("identity_smtp_plugin", $message);
}
function saveSmtpSettings($args)
{
$identities = rcmail::get_instance()->config->get('identity_smtp');
$id = intval($args['id']);
if (!isset($identities)) {
$identities = array();
}
$smtp_standard = rcube_utils::get_input_value('_smtp_standard', rcube_utils::INPUT_POST);
$password = rcube_utils::get_input_value('_smtp_pass', rcube_utils::INPUT_POST, true);
$password2 = rcube_utils::get_input_value('_smtp_pass2', rcube_utils::INPUT_POST, true);
if ($password != $password2) {
$args['abort'] = true;
$args['result'] = false;
$args['message'] = $this->gettext('smtp_passwords_mismatch');
return $args;
}
if ($password != $identities[$id]['smtp_pass']) {
$password = rcmail::get_instance()->encrypt($password);
}
$smtpSettingsRecord = array(
'smtp_standard' => isset($smtp_standard),
'smtp_host' => rcube_utils::get_input_value('_smtp_host', rcube_utils::INPUT_POST),
'smtp_user' => rcube_utils::get_input_value('_smtp_user', rcube_utils::INPUT_POST),
'smtp_pass' => $password
);
unset($identities[$id]);
$identities += array(
$id => $smtpSettingsRecord
);
rcmail::get_instance()->user->save_prefs(array(
'identity_smtp' => $identities
));
return $args;
}
function loadSmtpSettings($args)
{
$smtpSettings = rcmail::get_instance()->config->get('identity_smtp');
$id = intval($args['identity_id']);
$smtpSettingsRecord = array(
'smtp_standard' => $smtpSettings[$id]['smtp_standard'],
'smtp_host' => $smtpSettings[$id]['smtp_host'],
'smtp_user' => $smtpSettings[$id]['smtp_user'],
'smtp_pass' => $smtpSettings[$id]['smtp_pass'],
'smtp_pass2' => $smtpSettings[$id]['smtp_pass']
);
if (is_null($smtpSettingsRecord['smtp_standard'])) {
$smtpSettingsRecord['smtp_standard'] = true;
}
return $smtpSettingsRecord;
}
function identityFormWillBeDisplayed($args)
{
$form = $args['form'];
$record = isset($args['record']) ? $args['record'] : [];
// Load the stored smtp settings
$smtpSettingsRecord = $this->loadSmtpSettings($record);
if (!isset($record['identity_id'])) {
// FIX ME
$smtpSettingsForm = array(
'smtpSettings' => array(
'name' => $this->gettext('smtp_settings_header'),
'content' => array(
'text' => array(
'label' => $this->gettext('smtp_settings_not_available'),
'value' => ' '
)
)
)
);
} else {
$smtpSettingsForm = array(
'smtpSettings' => array(
'name' => $this->gettext('smtp_settings_header'),
'content' => array(
'smtp_standard' => array(
'type' => 'checkbox',
'label' => $this->gettext('use_default_smtp_server'),
'onclick' => 'identity_smtp_toggle_standard_server()'
),
'smtp_host' => array(
'type' => 'text',
'label' => $this->gettext('smtp_host'),
'class' => 'identity_smtp_form',
'size' => 40
),
'smtp_user' => array(
'type' => 'text',
'label' => $this->gettext('smtp_user'),
'class' => 'identity_smtp_form',
'size' => 40
),
'smtp_pass' => array(
'type' => 'password',
'label' => $this->gettext('smtp_pass'),
'class' => 'identity_smtp_form',
'size' => 40
),
'smtp_pass2' => array(
'type' => 'password',
'label' => $this->gettext('smtp_pass2'),
'class' => 'identity_smtp_form',
'placeholder' => 'test',
'size' => 40
)
)
)
);
if ($smtpSettingsRecord['smtp_standard'] || is_null($smtpSettingsRecord['smtp_standard'])) {
foreach ($smtpSettingsForm['smtpSettings']['content'] as &$input) {
if ($input['type'] != 'checkbox') {
$input['disabled'] = 'disabled';
}
}
}
}
$form = $form + $smtpSettingsForm;
$record = $record + $smtpSettingsRecord;
$OUTPUT = array(
'form' => $form,
'record' => $record
);
return $OUTPUT;
}
// This function is called when a new identity is created. We want to use the default smtp server here
function identityWasCreated($args)
{
$args = $this->saveSmtpSettings($args);
return $args;
}
// This function is called when the users saves a changed identity. It is responsible for saving the smtp settings
function identityWasUpdated($args)
{
$args = $this->saveSmtpSettings($args);
return $args;
}
function identityWasDeleted($args)
{
$smtpSettings = rcmail::get_instance()->config->get('identity_smtp');
$id = $args['id'];
unset($smtpSettings[$id]);
rcmail::get_instance()->user->save_prefs(array(
'identity_smtp' => $smtpSettings
));
// Return false to not abort the deletion of the identity
return false;
}
function messageBeforeSend($args)
{
$identities = rcmail::get_instance()->user->list_identities();
foreach ($identities as $idx => $ident) {
if ($identities[$idx]['email'] == $args['from']) {
$this->from_identity = $identities[$idx]['identity_id'];
}
}
return $args;
}
// This function is called when an email is sent and it should pull the correct smtp settings for the used identity and insert them
function smtpWillConnect($args)
{
$smtpSettings = $this->loadSmtpSettings(array(
'identity_id' => $this->from_identity
));
if (!$smtpSettings['smtp_standard'] && !is_null($smtpSettings['smtp_standard'])) {
$args['smtp_host'] = $smtpSettings['smtp_host'];
$args['smtp_user'] = $smtpSettings['smtp_user'];
$args['smtp_pass'] = rcmail::get_instance()->decrypt($smtpSettings['smtp_pass']);
}
return $args;
}
}
?>