-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.php
More file actions
447 lines (404 loc) · 14.8 KB
/
sync.php
File metadata and controls
447 lines (404 loc) · 14.8 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ImapSync - Processing</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.status {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.progress-section {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.stats-box {
background: #f8f9fa;
border: 1px solid #dee2e6;
padding: 15px;
border-radius: 5px;
}
.stats-box h4 {
margin: 0 0 10px 0;
color: #495057;
font-size: 14px;
font-weight: bold;
}
.stat-item {
display: flex;
justify-content: space-between;
margin: 5px 0;
font-size: 13px;
}
.stat-value {
font-weight: bold;
color: #007cba;
}
.progress-bar {
width: 100%;
height: 20px;
background-color: #e9ecef;
border-radius: 10px;
overflow: hidden;
margin: 10px 0;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #28a745 0%, #20c997 100%);
width: 0%;
transition: width 0.3s ease;
}
.log-container {
background: #1e1e1e;
color: #f8f8f2;
padding: 15px;
border-radius: 5px;
font-family: 'Courier New', monospace;
font-size: 12px;
line-height: 1.4;
max-height: 500px;
overflow-y: auto;
border: 1px solid #495057;
}
.log-line {
margin: 2px 0;
word-wrap: break-word;
}
.log-error { color: #ff6b6b; }
.log-warning { color: #feca57; }
.log-success { color: #48cab2; }
.log-info { color: #74b9ff; }
.command-display {
background: #2d3748;
color: #e2e8f0;
padding: 15px;
border-radius: 5px;
font-family: monospace;
font-size: 12px;
margin-bottom: 20px;
overflow-x: auto;
}
.back-btn {
background: #6c757d;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
display: inline-block;
margin-top: 20px;
}
.back-btn:hover {
background: #5a6268;
}
.status-indicator {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 8px;
}
.status-running { background: #feca57; animation: pulse 1.5s infinite; }
.status-success { background: #48cab2; }
.status-error { background: #ff6b6b; }
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.sync-summary {
background: #e3f2fd;
border: 1px solid #1976d2;
padding: 15px;
border-radius: 5px;
margin: 20px 0;
}
</style>
<script>
let stats = {
totalMessages: 0,
transferredMessages: 0,
skippedMessages: 0,
errorMessages: 0,
currentFolder: '',
startTime: new Date(),
bytesTransferred: 0
};
function updateProgress() {
const progressPercent = stats.totalMessages > 0 ?
Math.round((stats.transferredMessages / stats.totalMessages) * 100) : 0;
document.getElementById('progress-fill').style.width = progressPercent + '%';
document.getElementById('total-messages').textContent = stats.totalMessages;
document.getElementById('transferred-messages').textContent = stats.transferredMessages;
document.getElementById('skipped-messages').textContent = stats.skippedMessages;
document.getElementById('error-messages').textContent = stats.errorMessages;
document.getElementById('current-folder').textContent = stats.currentFolder || 'Initializing...';
document.getElementById('progress-percent').textContent = progressPercent + '%';
const elapsed = Math.floor((new Date() - stats.startTime) / 1000);
const minutes = Math.floor(elapsed / 60);
const seconds = elapsed % 60;
document.getElementById('elapsed-time').textContent =
`${minutes}:${seconds.toString().padStart(2, '0')}`;
}
function addLogLine(text, type = '') {
const logContainer = document.getElementById('log-output');
const line = document.createElement('div');
line.className = 'log-line ' + (type ? 'log-' + type : '');
line.textContent = text;
logContainer.appendChild(line);
logContainer.scrollTop = logContainer.scrollHeight;
// Parse statistics from log
parseLogForStats(text);
}
function parseLogForStats(text) {
// Extract folder information
if (text.includes('folder [')) {
const folderMatch = text.match(/folder \[([^\]]+)\]/);
if (folderMatch) {
stats.currentFolder = folderMatch[1];
}
}
// Extract message counts
if (text.includes('messages to sync')) {
const countMatch = text.match(/(\d+) messages to sync/);
if (countMatch) {
stats.totalMessages += parseInt(countMatch[1]);
}
}
// Extract transferred messages
if (text.includes('msg ')) {
stats.transferredMessages++;
}
// Extract skipped messages
if (text.includes('skipped')) {
stats.skippedMessages++;
}
// Extract errors
if (text.includes('ERROR') || text.includes('FAILED')) {
stats.errorMessages++;
}
updateProgress();
}
function setStatus(status, message) {
const indicator = document.getElementById('status-indicator');
const statusText = document.getElementById('status-text');
indicator.className = 'status-indicator status-' + status;
statusText.textContent = message;
}
</script>
</head>
<body>
<div class="container">
<h1>🔄 ImapSync Progress Monitor</h1>
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo '<div class="status">❌ Invalid request method.</div>';
echo '<a href="index.php" class="back-btn">← Back to Sync Form</a>';
exit;
}
// Sanitize inputs
function sanitize_input($data) {
return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}
$host1 = sanitize_input($_POST['host1'] ?? '');
$port1 = sanitize_input($_POST['port1'] ?? '');
$user1 = sanitize_input($_POST['user1'] ?? '');
$password1 = $_POST['password1'] ?? '';
$host2 = sanitize_input($_POST['host2'] ?? '');
$port2 = sanitize_input($_POST['port2'] ?? '');
$user2 = sanitize_input($_POST['user2'] ?? '');
$password2 = $_POST['password2'] ?? '';
$ssl1 = isset($_POST['ssl1']);
$ssl2 = isset($_POST['ssl2']);
$dry_run = isset($_POST['dry_run']);
$delete1 = isset($_POST['delete1']);
$automap = isset($_POST['automap']);
$gmail1 = isset($_POST['gmail1']);
$gmail2 = isset($_POST['gmail2']);
// Validate required fields
if (empty($host1) || empty($user1) || empty($password1) || empty($host2) || empty($user2) || empty($password2)) {
echo '<div class="status">❌ All required fields must be filled.</div>';
echo '<a href="index.php" class="back-btn">← Back to Sync Form</a>';
exit;
}
echo '<div class="status"><span id="status-indicator" class="status-indicator status-running"></span><span id="status-text">Initializing ImapSync...</span></div>';
// Build imapsync command
$cmd = '/usr/local/bin/imapsync';
$cmd .= ' --host1 ' . escapeshellarg($host1);
$cmd .= ' --user1 ' . escapeshellarg($user1);
$cmd .= ' --password1 ' . escapeshellarg($password1);
$cmd .= ' --host2 ' . escapeshellarg($host2);
$cmd .= ' --user2 ' . escapeshellarg($user2);
$cmd .= ' --password2 ' . escapeshellarg($password2);
if (!empty($port1)) {
$cmd .= ' --port1 ' . (int)$port1;
}
if (!empty($port2)) {
$cmd .= ' --port2 ' . (int)$port2;
}
if ($ssl1) {
$cmd .= ' --ssl1';
}
if ($ssl2) {
$cmd .= ' --ssl2';
}
if ($dry_run) {
$cmd .= ' --dry';
}
if ($delete1) {
$cmd .= ' --delete1';
}
if ($automap) {
$cmd .= ' --automap';
}
if ($gmail1) {
$cmd .= ' --gmail1';
}
if ($gmail2) {
$cmd .= ' --gmail2';
}
// Add security and logging options
$cmd .= ' --tmpdir /tmp/imapsync';
$cmd .= ' --logdir /var/log/imapsync';
$cmd .= ' --noreleasecheck';
echo '<h3>📝 Command</h3>';
// Show command but mask passwords
$display_cmd = str_replace(
[escapeshellarg($password1), escapeshellarg($password2)],
['[MASKED]', '[MASKED]'],
$cmd
);
echo '<div class="command-display">' . htmlspecialchars($display_cmd) . '</div>';
?>
<div class="progress-section">
<div class="stats-box">
<h4>📊 Sync Statistics</h4>
<div class="stat-item">
<span>Total Messages:</span>
<span class="stat-value" id="total-messages">0</span>
</div>
<div class="stat-item">
<span>Transferred:</span>
<span class="stat-value" id="transferred-messages">0</span>
</div>
<div class="stat-item">
<span>Skipped:</span>
<span class="stat-value" id="skipped-messages">0</span>
</div>
<div class="stat-item">
<span>Errors:</span>
<span class="stat-value" id="error-messages">0</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
<div style="text-align: center; font-weight: bold;" id="progress-percent">0%</div>
</div>
<div class="stats-box">
<h4>⏱️ Sync Progress</h4>
<div class="stat-item">
<span>Current Folder:</span>
<span class="stat-value" id="current-folder">Initializing...</span>
</div>
<div class="stat-item">
<span>Elapsed Time:</span>
<span class="stat-value" id="elapsed-time">0:00</span>
</div>
<div class="stat-item">
<span>Source:</span>
<span class="stat-value"><?php echo htmlspecialchars($user1 . '@' . $host1); ?></span>
</div>
<div class="stat-item">
<span>Destination:</span>
<span class="stat-value"><?php echo htmlspecialchars($user2 . '@' . $host2); ?></span>
</div>
</div>
</div>
<h3>📋 Live Log Output</h3>
<div class="log-container" id="log-output">
<!-- Log lines will be added here via JavaScript -->
</div>
<script>
setStatus('running', 'Sync in progress...');
// Start the sync process via server-sent events simulation
<?php
echo "addLogLine('Starting ImapSync process...', 'info');\n";
// Flush output buffer to show progress
if (ob_get_level()) {
ob_end_flush();
}
// Execute command and show live output
echo "setStatus('running', 'Connecting to servers...');\n";
$handle = popen($cmd . ' 2>&1', 'r');
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle);
if ($line !== false) {
$line = trim($line);
if (!empty($line)) {
$escaped_line = addslashes($line);
// Determine log type based on content
$log_type = '';
if (strpos($line, 'ERROR') !== false || strpos($line, 'FAILED') !== false) {
$log_type = 'error';
} elseif (strpos($line, 'WARNING') !== false) {
$log_type = 'warning';
} elseif (strpos($line, 'SUCCESS') !== false || strpos($line, 'OK') !== false) {
$log_type = 'success';
} elseif (strpos($line, 'folder') !== false || strpos($line, 'msg') !== false) {
$log_type = 'info';
}
echo "addLogLine('$escaped_line', '$log_type');\n";
flush();
}
}
}
$exit_code = pclose($handle);
if ($exit_code == 0) {
echo "setStatus('success', 'Sync completed successfully!');\n";
echo "addLogLine('✅ Sync completed successfully!', 'success');\n";
} else {
echo "setStatus('error', 'Sync completed with errors');\n";
echo "addLogLine('❌ Sync completed with errors. Exit code: $exit_code', 'error');\n";
}
} else {
echo "setStatus('error', 'Failed to start sync process');\n";
echo "addLogLine('❌ Failed to start imapsync process.', 'error');\n";
}
?>
</script>
<div class="sync-summary">
<h4>📋 Sync Summary</h4>
<p>The sync process has completed. Check the statistics and log output above for details.</p>
</div>
<a href="index.php" class="back-btn">← Start New Sync</a>
</div>
</body>
</html>