-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimapsync.php
More file actions
291 lines (254 loc) · 11.4 KB
/
imapsync.php
File metadata and controls
291 lines (254 loc) · 11.4 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
require_once __DIR__ . '/lib/RoundcubeImapSyncException.php';
require_once __DIR__ . '/lib/RoundcubeImapSyncClient.php';
require_once __DIR__ . '/lib/RoundcubeImapSyncJob.php';
require_once __DIR__ . '/lib/RoundcubeImapSyncResult.php';
require_once __DIR__ . '/lib/RoundcubeImapSyncEngine.php';
class imapsync extends rcube_plugin
{
public $task = 'settings';
private rcmail $rc;
public function init(): void
{
$this->rc = rcmail::get_instance();
$this->load_config();
$this->add_texts('localization/', true);
$this->add_hook('settings_actions', [$this, 'settings_actions']);
$this->register_action('plugin.imapsync', [$this, 'action_form']);
$this->register_action('plugin.imapsync.start', [$this, 'action_start']);
$this->register_action('plugin.imapsync.status', [$this, 'action_status']);
}
public function settings_actions(array $args): array
{
$args['actions'][] = [
'action' => 'plugin.imapsync',
'class' => 'imapsync',
'label' => 'pagetitle',
'domain' => 'imapsync',
'title' => 'pagetitle',
];
return $args;
}
public function action_form(): void
{
$this->include_stylesheet($this->local_skin_path() . '/imapsync.css');
$this->include_script('imapsync.js');
$this->rc->output->add_handler('plugin.imapsyncform', [$this, 'render_form']);
$this->rc->output->add_label(
'imapsync.confirmstart',
'imapsync.progresstitle',
'imapsync.summarytitle',
'imapsync.folderssynced',
'imapsync.messagescopied',
'imapsync.messagesskipped',
'imapsync.errors',
'imapsync.donesuccess',
'imapsync.donewitherrors',
'imapsync.errorvalidation',
);
$this->rc->output->set_env('imapsync_allow_insecure', $this->allowInsecure());
$this->rc->output->set_pagetitle($this->gettext('pagetitle'));
$this->rc->output->send('imapsync.imapsync');
}
public function render_form(array $attrib): string
{
$host = new html_inputfield([
'name' => '_host',
'id' => 'imapsync-host',
'class' => 'form-control',
'required' => true,
]);
$port = new html_inputfield([
'name' => '_port',
'id' => 'imapsync-port',
'class' => 'form-control',
'type' => 'number',
'min' => 1,
'max' => 65535,
'required' => true,
]);
$user = new html_inputfield([
'name' => '_source_user',
'id' => 'imapsync-user',
'class' => 'form-control',
'required' => true,
'autocomplete' => 'username',
]);
$password = new html_passwordfield([
'name' => '_source_password',
'id' => 'imapsync-password',
'class' => 'form-control',
'required' => true,
'autocomplete' => 'current-password',
]);
$encryption = new html_select([
'name' => '_encryption',
'id' => 'imapsync-encryption',
'class' => 'custom-select',
]);
$encryption->add($this->gettext('encryptionssl'), 'ssl');
$encryption->add($this->gettext('encryptiontls'), 'tls');
if ($this->allowInsecure()) {
$encryption->add($this->gettext('encryptionnone'), 'none');
} else {
$encryption->add($this->gettext('encryptionnone'), 'none', ['disabled' => 'disabled']);
}
$table = new html_table(['class' => 'propform imapsync-form-table', 'cols' => 2]);
$this->addFormRow($table, 'imapsync-host', 'sourcehost', $host->show());
$this->addFormRow($table, 'imapsync-port', 'sourceport', $port->show('993'));
$this->addFormRow($table, 'imapsync-encryption', 'encryption', $encryption->show('ssl'));
$this->addFormRow($table, 'imapsync-user', 'sourceuser', $user->show());
$this->addFormRow($table, 'imapsync-password', 'sourcepassword', $password->show());
$button = html::tag('button', [
'type' => 'submit',
'id' => 'imapsync-submit',
'class' => 'button mainaction submit',
], rcube::Q($this->gettext('startsync')));
$cancelButton = html::tag('button', [
'type' => 'button',
'id' => 'imapsync-cancel',
'class' => 'button cancel',
'hidden' => 'hidden',
], rcube::Q($this->gettext('cancelsync')));
$form = $this->rc->output->form_tag([
'id' => 'imapsync-form',
'name' => 'imapsync-form',
'method' => 'post',
'action' => './?_task=settings&_action=plugin.imapsync.start',
], $table->show() . html::p(['class' => 'formbuttons footerleft'], $button . ' ' . $cancelButton));
$notice = html::div(['class' => 'boxinformation imapsync-notice'],
html::tag('strong', ['class' => 'imapsync-notice-title'], rcube::Q($this->gettext('noticetitle')))
. html::tag('ul', ['class' => 'imapsync-notice-list'],
html::tag('li', [], rcube::Q($this->gettext('noticepreserves')))
. html::tag('li', [], rcube::Q($this->gettext('noticesynchronous')))
. html::tag('li', [], rcube::Q($this->gettext('noticeretry')))
. html::tag('li', [], rcube::Q($this->gettext('noticeduration')))
)
);
return html::div(['id' => 'prefs-title', 'class' => 'boxtitle'], rcube::Q($this->gettext('pagetitle')))
. html::div(['class' => 'box formcontainer scroller'],
html::div(['class' => 'boxcontent formcontent'],
html::p(['class' => 'imapsync-intro'], rcube::Q($this->gettext('intro')))
. $notice
. $form
)
);
}
public function action_start(): void
{
// Release the session write-lock immediately: the sync may run for many
// seconds and we don't want it to block every other AJAX request the
// browser fires during that time. From here on $_SESSION is read-only
// in-memory; nothing we set will be persisted.
if (session_status() === PHP_SESSION_ACTIVE) {
session_write_close();
}
try {
$job = $this->createJobFromRequest();
$source = new RoundcubeImapSyncGenericClient(new rcube_imap_generic());
$destination = $this->createDestinationClient();
$engine = new RoundcubeImapSyncEngine($source, $destination);
$result = $engine->run($job, static function (string $folder, int $current, int $total): void {
// Live progress is intentionally not exposed in the synchronous
// MVP — see AGENTS.md "Open work / known limits". The callback
// stays so the engine signature is stable for the planned
// worker mode.
});
} catch (InvalidArgumentException $validationException) {
$message = $validationException->getMessage() !== ''
? $validationException->getMessage()
: $this->gettext('errorvalidation');
$this->rc->output->command('plugin.imapsync_error', $message);
$this->rc->output->send();
return;
} catch (RoundcubeImapSyncException $syncException) {
$result = new RoundcubeImapSyncResult();
$result->fatalError = $syncException->getMessage();
$result->finishedAt = microtime(true);
}
$this->rc->output->command('plugin.imapsync_status', [
'running' => false,
'progress' => [],
'result' => $result->toArray(),
]);
$this->rc->output->send();
}
public function action_status(): void
{
// Retained for the planned worker-mode iteration. In the synchronous
// MVP the start action returns the final result directly, so this
// endpoint always reports "nothing running, no result".
$this->rc->output->command('plugin.imapsync_status', [
'running' => false,
'progress' => [],
'result' => null,
]);
$this->rc->output->send();
}
private function addFormRow(html_table $table, string $fieldId, string $labelKey, string $field): void
{
$table->add('title', html::label($fieldId, rcube::Q($this->gettext($labelKey))));
$table->add(null, $field);
}
private function createJobFromRequest(): RoundcubeImapSyncJob
{
$host = trim(rcube_utils::get_input_string('_host', rcube_utils::INPUT_POST));
$port = (int) rcube_utils::get_input_value('_port', rcube_utils::INPUT_POST);
$encryption = trim(rcube_utils::get_input_string('_encryption', rcube_utils::INPUT_POST));
$sourceUser = trim(rcube_utils::get_input_string('_source_user', rcube_utils::INPUT_POST));
$sourcePassword = (string) rcube_utils::get_input_value('_source_password', rcube_utils::INPUT_POST);
if ($host === '' || $sourceUser === '' || $sourcePassword === '') {
throw new InvalidArgumentException($this->gettext('errorvalidation'));
}
if ($port < 1 || $port > 65535 || !in_array($encryption, ['ssl', 'tls', 'none'], true)) {
throw new InvalidArgumentException($this->gettext('errorvalidation'));
}
if ($encryption === 'none' && !$this->allowInsecure()) {
throw new InvalidArgumentException($this->gettext('errorinsecure'));
}
if (!$this->hostAllowed($host)) {
throw new InvalidArgumentException($this->gettext('errorhostblocked'));
}
return new RoundcubeImapSyncJob($host, $port, $encryption, $sourceUser, $sourcePassword, [
'skip_folders' => $this->rc->config->get('imapsync_skip_folders', []),
'folder_prefix' => $this->rc->config->get('imapsync_folder_prefix', ''),
]);
}
private function createDestinationClient(): RoundcubeImapSyncClient
{
if (!$this->rc->storage_connect()) {
throw new RoundcubeImapSyncException($this->gettext('errorconnect'));
}
$storage = $this->rc->get_storage();
if (!isset($storage->conn) || !$storage->conn instanceof rcube_imap_generic) {
throw new RoundcubeImapSyncException($this->gettext('errorconnect'));
}
return new RoundcubeImapSyncGenericClient($storage->conn);
}
private function hostAllowed(string $host): bool
{
$allowlist = (array) $this->rc->config->get('imapsync_host_allowlist', []);
$denylist = (array) $this->rc->config->get('imapsync_host_denylist', []);
if ($this->matchesAnyPattern($host, $denylist)) {
return false;
}
if ($allowlist === []) {
return true;
}
return $this->matchesAnyPattern($host, $allowlist);
}
private function matchesAnyPattern(string $host, array $patterns): bool
{
foreach ($patterns as $pattern) {
$pattern = (string) $pattern;
if ($pattern !== '' && @preg_match($pattern, $host) === 1) {
return true;
}
}
return false;
}
private function allowInsecure(): bool
{
return (bool) $this->rc->config->get('imapsync_allow_insecure', false);
}
}