forked from mailwatch/MailWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.php
executable file
·981 lines (867 loc) · 35.3 KB
/
upgrade.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#!/usr/bin/php -q
<?php
/*
* MailWatch for MailScanner
* Copyright (C) 2003-2011 Steve Freegard ([email protected])
* Copyright (C) 2011 Garrod Alwood ([email protected])
* Copyright (C) 2014-2017 MailWatch Team (https://github.com/mailwatch/1.2.0/graphs/contributors)
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* In addition, as a special exception, the copyright holder gives permission to link the code of this program with
* those files in the PEAR library that are licensed under the PHP License (or with modified versions of those files
* that use the same license as those files), and distribute linked combinations including the two.
* You must obey the GNU General Public License in all respects for all of the code used other than those files in the
* PEAR library that are licensed under the PHP License. If you modify this program, you may extend this exception to
* your version of the program, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
if (PHP_SAPI !== 'cli') {
header('Content-type: text/plain');
}
// Edit if you changed webapp directory from default and not using command line argument to define it
$pathToFunctions = '/var/www/html/mailscanner/functions.php';
//$pathToFunctions = __DIR__ . '/mailscanner/functions.php';
$cli_options = getopt('', array('skip-user-confirm'));
if (isset($argv) && count($argv) > 1) {
if (empty($cli_options)) {
$pathToFunctions = $argv[1];
} else {
$args = array_search('--', $argv, true);
$args = array_splice($argv, $args ? ++$args : (count($argv) - count($cli_options)));
//get path from command line argument if set
$pathToFunctions = $args[0];
}
}
if (!@is_file($pathToFunctions)) {
die('Error: Cannot find functions.php file in "' . $pathToFunctions . '": edit ' . __FILE__ . ' and set the right path on line ' . (__LINE__ - 17) . PHP_EOL);
}
require_once $pathToFunctions;
$link = dbconn();
$mysql_utf8_variant = array(
'utf8' => array('charset' => 'utf8', 'collation' => 'utf8_unicode_ci'),
'utf8mb4' => array('charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci')
);
/*****************************************************************
* Start helper functions
*****************************************************************/
/**
* @param string $input
* @return string
*/
function pad($input)
{
return str_pad($input, 70, '.', STR_PAD_RIGHT);
}
/**
* @param string $sql
* @param bool $beSilent
*/
function executeQuery($sql, $beSilent = false)
{
global $link;
try {
if ($link->query($sql)) {
if (!$beSilent) {
echo color(' OK', 'green') . PHP_EOL;
}
} else {
echo color(' ERROR', 'red') . PHP_EOL;
die('Database error: ' . $link->error . " - SQL = '$sql'" . PHP_EOL);
}
} catch (Exception $e) {
echo color(' ERROR', 'red') . PHP_EOL;
die('Database error: ' . $e->getMessage() . " - SQL = '$sql'" . PHP_EOL);
}
}
/**
* @param string $table
* @return bool|mysqli_result
*/
function check_table_exists($table)
{
global $link;
$sql = 'SHOW TABLES LIKE "' . $table . '"';
return ($link->query($sql)->num_rows > 0);
}
/**
* @param string $table
* @param string $column
* @return bool|mysqli_result
*/
function check_column_exists($table, $column)
{
global $link;
$sql = 'SHOW COLUMNS FROM `' . $table . '` LIKE "' . $column . '"';
return ($link->query($sql)->num_rows > 0);
}
/**
* @return string|bool
*/
function get_database_charset()
{
global $link;
$sql = 'SELECT default_character_set_name
FROM information_schema.schemata
WHERE schema_name = "' . DB_NAME . '"';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$result = $link->query($sql);
$row = $result->fetch_array();
if (null !== $row && isset($row[0])) {
return $row[0];
}
return false;
}
/**
* @return string|bool
*/
function get_database_collation()
{
global $link;
$sql = 'SELECT default_collation_name
FROM information_schema.schemata
WHERE schema_name = "' . DB_NAME . '"';
$result = $link->query($sql);
$row = $result->fetch_array();
if (null !== $row && isset($row[0])) {
return $row[0];
}
return false;
}
/**
* @param string $db
* @param string $table
* @param string $utf8variant
* @return bool
*/
function check_utf8_table($db, $table, $utf8variant = 'utf8')
{
global $link;
global $mysql_utf8_variant;
$sql = 'SELECT c.character_set_name, c.collation_name
FROM information_schema.tables AS t, information_schema.collation_character_set_applicability AS c
WHERE c.collation_name = t.table_collation
AND t.table_schema = "' . $link->real_escape_string($db) . '"
AND t.table_name = "' . $link->real_escape_string($table) . '"';
$result = $link->query($sql);
$table_charset = database::mysqli_result($result, 0, 0);
$table_collation = database::mysqli_result($result, 0, 1);
return (
strtolower($table_charset) === $mysql_utf8_variant[$utf8variant]['charset'] &&
strtolower($table_collation) === $mysql_utf8_variant[$utf8variant]['collation']
);
}
/**
* @param string $db
* @param string $table
* @return bool
*/
function is_table_type_innodb($db, $table)
{
global $link;
$sql = 'SELECT t.engine
FROM information_schema.tables AS t
WHERE t.table_schema = "' . $link->real_escape_string($db) . '"
AND t.table_name = "' . $link->real_escape_string($table) . '"';
$result = $link->query($sql);
return 'innodb' === strtolower(database::mysqli_result($result, 0, 0));
}
/**
* @param string $db
* @param string $table
* @param string $index
* @return int|null
*/
function get_index_size($db, $table, $index)
{
global $link;
$sql = 'SHOW INDEX FROM ' . $db . '.' . $table . ' WHERE Key_name = "' . $index . '"';
$result = $link->query($sql);
$row = $result->fetch_assoc();
if (null === $row || null === $row['Sub_part']) {
return null;
}
return (int)$row['Sub_part'];
}
/**
* @param string $table
* @return array
*/
function getTableIndexes($table)
{
global $link;
$sql = 'SHOW INDEX FROM `' . $table . '`';
$result = $link->query($sql);
$indexes = array();
if (false === $result || $result->num_rows === 0) {
return $indexes;
}
while ($row = $result->fetch_assoc()) {
$indexes[] = $row['Key_name'];
}
return $indexes;
}
/**
* @return string
*/
function getSqlServer()
{
global $link;
//test if mysql or mariadb is used.
$sql = 'SELECT VERSION() as version';
$result = $link->query($sql);
$fetch = $result->fetch_array();
if (false === strpos($fetch['version'], 'MariaDB')) {
//mysql does not support aria storage engine
return 'mysql';
}
return 'mariadb';
}
function getColumnInfo($table, $column)
{
global $link;
$sql = 'SHOW COLUMNS FROM ' . $table . " LIKE '" . $column . "'";
$result = $link->query($sql);
return $result->fetch_array();
}
/**
* @param string $string
* @param string $color
* @return string
*/
function color($string, $color = '')
{
$after = "\033[0m";
switch ($color) {
case 'green':
$before = "\033[1;32m";
break;
case 'lightgreen':
$before = "\033[0;32m";
break;
case 'yellow':
$before = "\033[1;33;40m";
break;
case 'red':
$before = "\033[0;31m";
break;
default:
$before = '';
$after = '';
break;
}
return $before . $string . $after;
}
/**
* @param string $haystack
* @param string $needles
* @return bool
*/
function stringStartsWith($haystack, $needles)
{
foreach ((array)$needles as $needle) {
if ($needle !== '' && 0 === strpos($haystack, (string)$needle)) {
return true;
}
}
return false;
}
/**
* @param string $haystack
* @param string $needles
* @return bool
*/
function stringEndsWith($haystack, $needles)
{
foreach ((array)$needles as $needle) {
if (substr($haystack, -strlen($needle)) === (string)$needle) {
return true;
}
}
return false;
}
/*****************************************************************
* End helper functions
*****************************************************************/
$errors = false;
// Upgrade mailwatch database
echo PHP_EOL;
echo 'MailWatch for MailScanner Database Upgrade to ' . mailwatch_version() . PHP_EOL;
echo PHP_EOL;
if (!array_key_exists('skip-user-confirm', $cli_options)) {
echo "Have you done a full backup of your database? Type 'yes' to continue: ";
$handle = fopen('php://stdin', 'rb');
$line = fgets($handle);
if (strtolower(trim($line)) !== 'yes') {
echo 'ABORTING!' . PHP_EOL;
exit(1);
}
fclose($handle);
echo PHP_EOL;
}
// Test connectivity to the database
echo pad('Testing connectivity to the database ');
if ($link) {
echo color(' OK', 'green') . PHP_EOL;
// Update schema at this point
echo PHP_EOL;
echo 'Updating database schema: ' . PHP_EOL;
echo PHP_EOL;
/*
** Updates to the schema for 1.2.0
*/
$server_utf8_variant = 'utf8';
// Convert database to utf8 if not already utf8mb4 or if other charset
echo pad(' - Convert database to ' . $server_utf8_variant . '');
if (get_database_charset() === $mysql_utf8_variant['utf8mb4']['charset'] && get_database_collation() === $mysql_utf8_variant['utf8mb4']['collation']) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$server_utf8_variant = 'utf8';
$sql = 'ALTER DATABASE `' . DB_NAME .
'` CHARACTER SET = ' . $mysql_utf8_variant[$server_utf8_variant]['charset'] .
' COLLATE = ' . $mysql_utf8_variant[$server_utf8_variant]['collation'];
executeQuery($sql);
}
echo PHP_EOL;
// Drop geoip table
echo pad(' - Drop `geoip_country` table');
if (false === check_table_exists('geoip_country')) {
echo color(' ALREADY DROPPED', 'lightgreen') . PHP_EOL;
} else {
$sql = 'DROP TABLE IF EXISTS `geoip_country`';
executeQuery($sql);
}
// Drop spamscores table
echo pad(' - Drop `spamscores` table');
if (false === check_table_exists('spamscores')) {
echo color(' ALREADY DROPPED', 'lightgreen') . PHP_EOL;
} else {
$sql = 'DROP TABLE IF EXISTS `spamscores`';
executeQuery($sql);
}
// Add autorelease table if not exist (1.2RC2)
echo pad(' - Add autorelease table to `' . DB_NAME . '` database');
if (true === check_table_exists('autorelease')) {
echo color(' ALREADY EXIST', 'lightgreen') . PHP_EOL;
} else {
$sql = 'CREATE TABLE IF NOT EXISTS `autorelease` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`msg_id` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
`uid` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
executeQuery($sql);
}
// Add mtalog_ids table if not exist (if upgrade from < 1.2.0)
echo pad(' - Add mtalog_ids table to `' . DB_NAME . '` database');
if (true === check_table_exists('mtalog_ids')) {
echo color(' ALREADY EXIST', 'lightgreen') . PHP_EOL;
} else {
$sql = 'CREATE TABLE IF NOT EXISTS `mtalog_ids` (
`smtpd_id` VARCHAR(20) CHARACTER SET ascii DEFAULT NULL,
`smtp_id` VARCHAR(20) CHARACTER SET ascii DEFAULT NULL,
UNIQUE KEY `mtalog_ids_idx` (`smtpd_id`,`smtp_id`),
KEY `smtpd_id` (`smtpd_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
executeQuery($sql);
}
// Update users table schema for password-reset feature
echo pad(' - Add resetid, resetexpire and lastreset fields in `users` table');
if (false === check_column_exists('users', 'resetid')) {
$sql = 'ALTER TABLE `users` ADD COLUMN (
`resetid` VARCHAR(32),
`resetexpire` BIGINT(20),
`lastreset` BIGINT(20)
);';
executeQuery($sql);
} else {
echo color(' ALREADY EXIST', 'lightgreen') . PHP_EOL;
}
// Update users table schema for login_expiry, last_login and individual login_timeout feature
echo pad(' - Add login_expiry and login_timeout fields in `users` table');
if (false === check_column_exists('users', 'login_expiry')) {
$sql = "ALTER TABLE `users` ADD COLUMN (
`login_expiry` BIGINT(20) COLLATE utf8_unicode_ci DEFAULT '-1',
`last_login` BIGINT(20) COLLATE utf8_unicode_ci DEFAULT '-1',
`login_timeout` SMALLINT(5) COLLATE utf8_unicode_ci DEFAULT '-1'
);";
executeQuery($sql);
} else {
echo color(' ALREADY EXIST', 'lightgreen') . PHP_EOL;
}
echo PHP_EOL;
// Truncate needed for VARCHAR fields used as PRIMARY or FOREIGN KEY when using utf8mb4
// Table audit_log
echo pad(' - Fix schema for username field in `audit_log` table');
$audit_log_user_info = getColumnInfo('audit_log', 'user');
if ($audit_log_user_info['Type'] !== 'varchar(191)') {
$sql = "ALTER TABLE `audit_log` CHANGE `user` `user` VARCHAR(191) NOT NULL DEFAULT ''";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($audit_log_user_info);
// Table blacklist
echo pad(' - Fix schema for id field in `blacklist` table');
$blacklist_id_info = getColumnInfo('blacklist', 'id');
if (strtolower($blacklist_id_info['Type']) !== 'bigint(20) unsigned' || strtoupper($blacklist_id_info['Null']) !== 'NO' || strtolower($blacklist_id_info['Extra']) !== 'auto_increment') {
$sql = 'ALTER TABLE `blacklist` CHANGE `id` `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT';
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($blacklist_id_info);
// Table whitelist
echo pad(' - Fix schema for id field in `whitelist` table');
$whitelist_id_info = getColumnInfo('whitelist', 'id');
if (strtolower($whitelist_id_info['Type']) !== 'bigint(20) unsigned' || strtoupper($whitelist_id_info['Null']) !== 'NO' || strtolower($whitelist_id_info['Extra']) !== 'auto_increment') {
$sql = 'ALTER TABLE `whitelist` CHANGE `id` `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT';
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
// user name lenght to 191
echo pad(' - Fix schema for username field in `users` table');
$users_username_info = getColumnInfo('users', 'username');
if ($users_username_info['Type'] !== 'varchar(191)') {
$sql = "ALTER TABLE `users` CHANGE `username` `username` VARCHAR(191) NOT NULL DEFAULT ''";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
echo pad(' - Fix schema for username field in `user_filters` table');
$user_filters_username_info = getColumnInfo('users', 'username');
if ($user_filters_username_info['Type'] !== 'varchar(191)') {
$sql = "ALTER TABLE `user_filters` CHANGE `username` `username` VARCHAR( 191 ) NOT NULL DEFAULT ''";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
// Table user_filters spam score to float
echo pad(' - Fix schema for spamscore field in `users` table');
$users_spamscore_info = getColumnInfo('users', 'spamscore');
if ($users_spamscore_info['Type'] !== 'float') {
$sql = "ALTER TABLE `users` CHANGE `spamscore` `spamscore` FLOAT DEFAULT '0'";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
echo pad(' - Fix schema for highspamscore field in `users` table');
$users_highspamscore_info = getColumnInfo('users', 'highspamscore');
if ($users_highspamscore_info['Type'] !== 'float') {
$sql = "ALTER TABLE `users` CHANGE `highspamscore` `highspamscore` FLOAT DEFAULT '0'";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
// Change timestamp to only be updated on creation to fix messages not beeing deleted from maillog
// We don't need a default / on update value for the timestamp field in the maillog table because we only change it in mailwatch.pm
// where we use the current system time from perl (not mysql function) as value. So we can remove it. Initial remove because MySQL
// in version < 5.6 cannot handle two columns with CURRENT_TIMESTAMP in DEFAULT
echo pad(' - Fix schema for timestamp field in `maillog` table');
$maillog_timestamp_info = getColumnInfo('maillog', 'timestamp');
if (null !== $maillog_timestamp_info['Default'] || '' !== $maillog_timestamp_info['Extra']) {
//Set NULL default on timestamp column
$sql = 'ALTER TABLE `maillog` CHANGE `timestamp` `timestamp` TIMESTAMP NULL DEFAULT NULL;';
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($maillog_timestamp_info);
// Revert back some tables to the right values due to previous errors in upgrade.php
// Table users password to 255
echo pad(' - Fix schema for password field in `users` table');
$users_password_info = getColumnInfo('users', 'password');
if ($users_password_info['Type'] !== 'varchar(255)') {
$sql = 'ALTER TABLE `users` CHANGE `password` `password` VARCHAR( 255 ) DEFAULT NULL';
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($users_password_info);
// Table users fullname to 255
echo pad(' - Fix schema for fullname field in `users` table');
$users_fullname_info = getColumnInfo('users', 'fullname');
if ($users_fullname_info['Type'] !== 'varchar(255)') {
$sql = "ALTER TABLE `users` CHANGE `fullname` `fullname` VARCHAR( 255 ) NOT NULL DEFAULT ''";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($users_fullname_info);
// Table mcp_rules
echo pad(' - Fix schema for rule_desc field in `mcp_rules` table');
$mcp_rules_rule_desc_info = getColumnInfo('mcp_rules', 'rule_desc');
if ($mcp_rules_rule_desc_info['Type'] !== 'varchar(200)') {
$sql = "ALTER TABLE `mcp_rules` CHANGE `rule_desc` `rule_desc` VARCHAR( 200 ) NOT NULL DEFAULT ''";
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
unset($mcp_rules_rule_desc_info);
echo PHP_EOL;
// Cleanup orphaned user_filters
echo pad(' - Cleanup orphaned user_filters');
$sql = 'DELETE FROM `user_filters` WHERE `username` NOT IN (SELECT `username` FROM `users`)';
executeQuery($sql);
// Add new column and index to audit_log table
echo pad(' - Add id field and primary key to `audit_log` table');
if (true === check_column_exists('audit_log', 'id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `audit_log` ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`id`)';
executeQuery($sql);
}
// Add new column and index to inq table
echo pad(' - Add inq_id field and primary key to `inq` table');
if (true === check_column_exists('inq', 'inq_id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `inq` ADD `inq_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`inq_id`)';
executeQuery($sql);
}
// Add new column and index to maillog table
echo pad(' - Add maillog_id field and primary key to `maillog` table');
if (true === check_column_exists('maillog', 'maillog_id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `maillog` ADD `maillog_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`maillog_id`)';
executeQuery($sql);
}
// Add new column to maillog table
echo pad(' - Add rblspamreport field to `maillog` table');
if (true === check_column_exists('maillog', 'rblspamreport')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `maillog` ADD `rblspamreport` MEDIUMTEXT COLLATE utf8_unicode_ci DEFAULT NULL';
executeQuery($sql);
}
// Add new token column to maillog table
echo pad(' - Add token field to `maillog` table');
if (true === check_column_exists('maillog', 'token')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `maillog` ADD `token` CHAR(64) COLLATE utf8_unicode_ci DEFAULT NULL';
executeQuery($sql);
}
// Add new released column to maillog table
echo pad(' - Add released field to `maillog` table');
if (true === check_column_exists('maillog', 'released')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = "ALTER TABLE `maillog` ADD `released` TINYINT(1) DEFAULT '0'";
executeQuery($sql);
}
echo pad(' - Add last_update field to `maillog` table');
if (check_column_exists('maillog', 'last_update') === false) {
$sql = 'ALTER TABLE `maillog` ADD `last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
executeQuery($sql);
} else {
echo color(' ALREADY EXIST', 'lightgreen') . PHP_EOL;
}
// Add new salearn column to maillog table
echo pad(' - Add salearn field to `maillog` table');
if (true === check_column_exists('maillog', 'salearn')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = "ALTER TABLE `maillog` ADD `salearn` TINYINT(1) DEFAULT '0'";
executeQuery($sql);
}
// Check for missing tokens in maillog table and add them back QUARANTINE_REPORT_DAYS
echo pad(' - Check for missing tokens in `maillog` table');
if (defined('QUARANTINE_REPORT_DAYS')) {
$report_days = QUARANTINE_REPORT_DAYS;
} else {
// Missing, but let's keep going...
$report_days = 7;
}
$sql = 'SELECT `id`,`token` FROM `maillog` WHERE `date` >= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $report_days . ' DAY)';
$result = dbquery($sql);
$rows = $result->num_rows;
$countTokenGenerated = 0;
if ($rows > 0) {
while ($row = $result->fetch_object()) {
if ($row->token === null) {
$sql = 'UPDATE `maillog` SET `token`=\'' . generateToken() . '\' WHERE `id`=\'' . trim($row->id) . '\'';
executeQuery($sql, true);
$countTokenGenerated++;
}
}
echo color(' DONE', 'lightgreen') . PHP_EOL;
echo ' ' . $countTokenGenerated . ' token generated' . PHP_EOL;
} else {
echo color(' NOTHING FOUND', 'lightgreen') . PHP_EOL;
}
// Add new column and index to mtalog table
echo pad(' - Add mtalog_id field and primary key to `mtalog` table');
if (true === check_column_exists('mtalog', 'mtalog_id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `mtalog` ADD `mtalog_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`mtalog_id`)';
executeQuery($sql);
}
// Add new column and index to outq table
echo pad(' - Add mtalog_id field and primary key to `outq` table');
if (true === check_column_exists('outq', 'outq_id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `outq` ADD `outq_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`outq_id`)';
executeQuery($sql);
}
// Add new column and index to saved_filters table
echo pad(' - Add id field and primary key to `saved_filters` table');
if (true === check_column_exists('saved_filters', 'id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `saved_filters` ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`id`)';
executeQuery($sql);
}
// Add new column and index to user_filters table
echo pad(' - Add mtalog_id field and primary key to `user_filters` table');
if (true === check_column_exists('user_filters', 'id')) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER TABLE `user_filters` ADD `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`id`)';
executeQuery($sql);
}
echo PHP_EOL;
// Fix existing index size for utf8mb4 conversion
$too_big_indexes = array(
'maillog_from_idx',
'maillog_to_idx',
);
foreach ($too_big_indexes as $item) {
echo pad(' - Dropping too big index `' . $item . '` on table `maillog`');
if (get_index_size(DB_NAME, 'maillog', $item) > 191) {
$sql = 'ALTER TABLE `maillog` DROP INDEX `' . $item . '`';
executeQuery($sql);
} else {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
}
}
echo PHP_EOL;
// Convert database to utf8mb4 if MySQL ≥ 5.5.3
if ($link->server_version >= 50503) {
$server_utf8_variant = 'utf8mb4';
echo pad(' - Convert database to ' . $server_utf8_variant . '');
if (get_database_charset() === $mysql_utf8_variant[$server_utf8_variant]['charset'] && get_database_collation() === $mysql_utf8_variant[$server_utf8_variant]['collation']) {
echo color(' ALREADY DONE', 'lightgreen') . PHP_EOL;
} else {
$sql = 'ALTER DATABASE `' . DB_NAME .
'` CHARACTER SET = ' . $mysql_utf8_variant[$server_utf8_variant]['charset'] .
' COLLATE = ' . $mysql_utf8_variant[$server_utf8_variant]['collation'];
executeQuery($sql);
}
}
echo PHP_EOL;
$utf8_tables = array(
'audit_log',
'autorelease',
'blacklist',
'inq',
'maillog',
'mcp_rules',
'mtalog',
'mtalog_ids',
'outq',
'saved_filters',
'sa_rules',
'users',
'user_filters',
'whitelist',
);
// Convert tables to utf8 using $utf8_tables array
foreach ($utf8_tables as $table) {
echo pad(' - Convert table `' . $table . '` to ' . $server_utf8_variant . '');
if (false === check_table_exists($table)) {
echo ' DO NOT EXISTS' . PHP_EOL;
} else {
if (check_utf8_table(DB_NAME, $table, $server_utf8_variant) === false) {
$sql = 'ALTER TABLE `' . $table .
'` CONVERT TO CHARACTER SET ' . $mysql_utf8_variant[$server_utf8_variant]['charset'] .
' COLLATE ' . $mysql_utf8_variant[$server_utf8_variant]['collation'];
executeQuery($sql);
} else {
echo color(' ALREADY CONVERTED', 'lightgreen') . PHP_EOL;
}
}
}
echo PHP_EOL;
// Convert tables to InnoDB using $utf8_tables array
foreach ($utf8_tables as $table) {
echo pad(' - Convert table `' . $table . '` to InnoDB');
if (false === check_table_exists($table)) {
echo ' DO NOT EXISTS' . PHP_EOL;
} else {
if (is_table_type_innodb(DB_NAME, $table) === false) {
$sql = 'ALTER TABLE `' . $table . '` ENGINE = InnoDB';
executeQuery($sql);
} else {
echo color(' ALREADY CONVERTED', 'lightgreen') . PHP_EOL;
}
}
}
// check for missing indexes
$indexes = array(
'maillog' => array(
'maillog_datetime_idx' => array(
'fields' => '(`date`,`time`)',
'type' => 'KEY',
'minMysqlVersion' => '50100'
),
'maillog_id_idx' => array('fields' => '(`id`(20))', 'type' => 'KEY', 'minMysqlVersion' => '50100'),
'maillog_clientip_idx' => array(
'fields' => '(`clientip`(20))',
'type' => 'KEY',
'minMysqlVersion' => '50100'
),
'maillog_from_idx' => array(
'fields' => '(`from_address`(191))',
'type' => 'KEY',
'minMysqlVersion' => '50100'
),
'maillog_to_idx' => array('fields' => '(`to_address`(191))', 'type' => 'KEY', 'minMysqlVersion' => '50100'),
'maillog_host' => array('fields' => '(`hostname`(30))', 'type' => 'KEY', 'minMysqlVersion' => '50100'),
'from_domain_idx' => array(
'fields' => '(`from_domain`(50))',
'type' => 'KEY',
'minMysqlVersion' => '50100'
),
'to_domain_idx' => array('fields' => '(`to_domain`(50))', 'type' => 'KEY', 'minMysqlVersion' => '50100'),
'maillog_quarantined' => array(
'fields' => '(`quarantined`)',
'type' => 'KEY',
'minMysqlVersion' => '50100'
),
'timestamp_idx' => array('fields' => '(`timestamp`)', 'type' => 'KEY', 'minMysqlVersion' => '50100'),
// can't use FULLTEXT index on InnoDB table in MySQL < 5.6.4
'subject_idx' => array('fields' => '(`subject`)', 'type' => 'FULLTEXT', 'minMysqlVersion' => '50604'),
)
);
foreach ($indexes as $table => $indexlist) {
echo PHP_EOL . pad(' - Search for missing indexes on table `' . $table . '`') . color(' DONE',
'green') . PHP_EOL;
$existingIndexes = getTableIndexes($table);
foreach ($indexlist as $indexname => $indexValue) {
if (!in_array($indexname, $existingIndexes, true)) {
echo pad(' - Adding missing index `' . $indexname . '` on table `' . $table . '`');
if ($link->server_version >= $indexValue['minMysqlVersion']) {
$sql = 'ALTER TABLE `' . $table .
'` ADD ' . $indexValue['type'] . ' `' . $indexname . '` ' .
$indexValue['fields'] .
';';
executeQuery($sql);
} else {
echo ' ' . color('WARNING', 'yellow') . PHP_EOL;
$errors[] = 'Table Index: MySQL version unsupported for index `' . $indexname . '` on table `' . $table . '`, ' .
'upgrade to version >= ' . $indexValue['minMysqlVersion'] . ' (you have version ' . $link->server_version . ')';
}
}
}
}
dbclose();
} else {
echo color(' FAILED', 'red') . PHP_EOL;
$errors[] = 'Database connection failed: ' . $link->error;
}
echo PHP_EOL;
echo 'Checking for obsolete files: ' . PHP_EOL;
echo PHP_EOL;
if (file_exists(MAILWATCH_HOME . '/images/cache/')) {
$result = rmdir(MAILWATCH_HOME . '/images/cache/') . PHP_EOL;
if ($result === true) {
echo pad(' - Cache dir was still present. Removed it') . color(' INFO', 'lightgreen') . PHP_EOL;
} else {
echo pad(' - Cache dir was still present but removing it failed') . color(' ERROR', 'red') . PHP_EOL;
}
} else {
echo pad(' - Cache dir already removed') . color(' OK', 'green') . PHP_EOL;
}
echo PHP_EOL;
// Check MailScanner settings
echo 'Checking MailScanner.conf settings: ' . PHP_EOL;
echo PHP_EOL;
if (!is_file(MS_CONFIG_DIR . 'MailScanner.conf')) {
$err_msg = 'MailScanner.conf: cannot find file on path "' . MS_CONFIG_DIR . 'MailScanner.conf' . '"';
echo pad(' - ' . $err_msg) . color(' ERROR', 'red') . PHP_EOL;
$errors[] = $err_msg;
} else {
$check_settings = array(
'QuarantineWholeMessage' => 'yes',
'QuarantineWholeMessagesAsQueueFiles' => 'no',
'DetailedSpamReport' => 'yes',
'IncludeScoresInSpamAssassinReport' => 'yes',
'SpamActions' => 'store',
'HighScoringSpamActions' => 'store',
'AlwaysLookedUpLast' => '&MailWatchLogging'
);
foreach ($check_settings as $setting => $value) {
echo pad(" - $setting ");
if (preg_match('/' . $value . '/', get_conf_var($setting))) {
echo color(' OK', 'green') . PHP_EOL;
} else {
echo ' ' . color('WARNING', 'yellow') . PHP_EOL;
$errors[] = "MailScanner.conf: $setting != $value (=" . get_conf_var($setting) . ')';
}
}
}
echo PHP_EOL;
// Check configuration for missing entries
echo 'Checking conf.php configuration entry: ' . PHP_EOL;
echo PHP_EOL;
$checkConfigEntries = checkConfVariables();
if ($checkConfigEntries['needed']['count'] === 0) {
echo pad(' - All mandatory entries are present') . color(' OK', 'green') . PHP_EOL;
} else {
foreach ($checkConfigEntries['needed']['list'] as $missingConfigEntry) {
echo pad(" - $missingConfigEntry ") . ' ' . color('WARNING', 'yellow') . PHP_EOL;
$errors[] = 'conf.php: missing configuration entry "' . $missingConfigEntry . '"';
}
}
if ($checkConfigEntries['obsolete']['count'] === 0) {
echo pad(' - All obsolete entries are already removed') . color(' OK', 'green') . PHP_EOL;
} else {
foreach ($checkConfigEntries['obsolete']['list'] as $obsoleteConfigEntry) {
echo pad(" - $obsoleteConfigEntry ") . ' ' . color('WARNING', 'yellow') . PHP_EOL;
$errors[] = 'conf.php: obsolete configuration entry "' . $obsoleteConfigEntry . '" still present';
}
}
if ($checkConfigEntries['optional']['count'] === 0) {
echo pad(' - All optional entries are already present') . color(' OK', 'green') . PHP_EOL;
} else {
foreach ($checkConfigEntries['optional']['list'] as $optionalConfigEntry => $detail) {
echo pad(" - optional $optionalConfigEntry ") . ' ' . color('INFO', 'lightgreen') . PHP_EOL;
$errors[] = 'conf.php: optional configuration entry "' . $optionalConfigEntry . '" is missing, ' . $detail['description'];
}
}
echo PHP_EOL;
/* Check configuration for syntactically wrong entries */
// IMAGES_DIR need both leading and trailing slash
if (!stringStartsWith(IMAGES_DIR, '/') || !stringEndsWith(IMAGES_DIR, '/')) {
$err_msg = 'conf.php: IMAGES_DIR must start and end with a slash';
echo pad(' - ' . $err_msg) . color(' ERROR', 'red') . PHP_EOL;
$errors[] = $err_msg;
}
// MAILWATCH_HOSTURL don't need trailing slash
if (stringEndsWith(MAILWATCH_HOSTURL, '/')) {
$err_msg = 'conf.php: MAILWATCH_HOSTURL must not end with a slash';
echo pad(' - ' . $err_msg) . color(' ERROR', 'red') . PHP_EOL;
$errors[] = $err_msg;
}
echo PHP_EOL;
// Error messages
if (is_array($errors)) {
echo '*** ERROR/WARNING SUMMARY ***' . PHP_EOL;
foreach ($errors as $error) {
echo $error . PHP_EOL;
}
echo PHP_EOL;
}