blob: c43afbf9cfa089478bf2746af7e34e8339e2092f [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2function init_db_schema() {
3 try {
4 global $pdo;
5
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02006 $db_version = "01072021_0630";
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01007
8 $stmt = $pdo->query("SHOW TABLES LIKE 'versions'");
9 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
10 if ($num_results != 0) {
11 $stmt = $pdo->query("SELECT `version` FROM `versions` WHERE `application` = 'db_schema'");
12 if ($stmt->fetch(PDO::FETCH_ASSOC)['version'] == $db_version) {
13 return true;
14 }
15 if (!preg_match('/y|yes/i', getenv('MASTER'))) {
16 $_SESSION['return'][] = array(
17 'type' => 'warning',
18 'log' => array(__FUNCTION__),
19 'msg' => 'Database not initialized: not running db_init on slave.'
20 );
21 return true;
22 }
23 }
24
25 $views = array(
26 "grouped_mail_aliases" => "CREATE VIEW grouped_mail_aliases (username, aliases) AS
27 SELECT goto, IFNULL(GROUP_CONCAT(address ORDER BY address SEPARATOR ' '), '') AS address FROM alias
28 WHERE address!=goto
29 AND active = '1'
30 AND sogo_visible = '1'
31 AND address NOT LIKE '@%'
32 GROUP BY goto;",
33 // START
34 // Unused at the moment - we cannot allow to show a foreign mailbox as sender address in SOGo, as SOGo does not like this
35 // We need to create delegation in SOGo AND set a sender_acl in mailcow to allow to send as user X
36 "grouped_sender_acl" => "CREATE VIEW grouped_sender_acl (username, send_as_acl) AS
37 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
38 WHERE send_as NOT LIKE '@%'
39 GROUP BY logged_in_as;",
40 // END
41 "grouped_sender_acl_external" => "CREATE VIEW grouped_sender_acl_external (username, send_as_acl) AS
42 SELECT logged_in_as, IFNULL(GROUP_CONCAT(send_as SEPARATOR ' '), '') AS send_as_acl FROM sender_acl
43 WHERE send_as NOT LIKE '@%' AND external = '1'
44 GROUP BY logged_in_as;",
45 "grouped_domain_alias_address" => "CREATE VIEW grouped_domain_alias_address (username, ad_alias) AS
46 SELECT username, IFNULL(GROUP_CONCAT(local_part, '@', alias_domain SEPARATOR ' '), '') AS ad_alias FROM mailbox
47 LEFT OUTER JOIN alias_domain ON target_domain=domain
48 GROUP BY username;",
49 "sieve_before" => "CREATE VIEW sieve_before (id, username, script_name, script_data) AS
50 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
51 WHERE filter_type = 'prefilter';",
52 "sieve_after" => "CREATE VIEW sieve_after (id, username, script_name, script_data) AS
53 SELECT md5(script_data), username, script_name, script_data FROM sieve_filters
54 WHERE filter_type = 'postfilter';"
55 );
56
57 $tables = array(
58 "versions" => array(
59 "cols" => array(
60 "application" => "VARCHAR(255) NOT NULL",
61 "version" => "VARCHAR(100) NOT NULL",
62 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
63 ),
64 "keys" => array(
65 "primary" => array(
66 "" => array("application")
67 )
68 ),
69 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
70 ),
71 "admin" => array(
72 "cols" => array(
73 "username" => "VARCHAR(255) NOT NULL",
74 "password" => "VARCHAR(255) NOT NULL",
75 "superadmin" => "TINYINT(1) NOT NULL DEFAULT '0'",
76 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
77 "modified" => "DATETIME ON UPDATE NOW(0)",
78 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
79 ),
80 "keys" => array(
81 "primary" => array(
82 "" => array("username")
83 )
84 ),
85 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
86 ),
87 "fido2" => array(
88 "cols" => array(
89 "username" => "VARCHAR(255) NOT NULL",
90 "friendlyName" => "VARCHAR(255)",
91 "rpId" => "VARCHAR(255) NOT NULL",
92 "credentialPublicKey" => "TEXT NOT NULL",
93 "certificateChain" => "TEXT",
94 // Can be null for format "none"
95 "certificate" => "TEXT",
96 "certificateIssuer" => "VARCHAR(255)",
97 "certificateSubject" => "VARCHAR(255)",
98 "signatureCounter" => "INT",
99 "AAGUID" => "BLOB",
100 "credentialId" => "BLOB NOT NULL",
101 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
102 "modified" => "DATETIME ON UPDATE NOW(0)",
103 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
104 ),
105 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
106 ),
107 "_sogo_static_view" => array(
108 "cols" => array(
109 "c_uid" => "VARCHAR(255) NOT NULL",
110 "domain" => "VARCHAR(255) NOT NULL",
111 "c_name" => "VARCHAR(255) NOT NULL",
112 "c_password" => "VARCHAR(255) NOT NULL DEFAULT ''",
113 "c_cn" => "VARCHAR(255)",
114 "mail" => "VARCHAR(255) NOT NULL",
115 // TODO -> use TEXT and check if SOGo login breaks on empty aliases
116 "aliases" => "TEXT NOT NULL",
117 "ad_aliases" => "VARCHAR(6144) NOT NULL DEFAULT ''",
118 "ext_acl" => "VARCHAR(6144) NOT NULL DEFAULT ''",
119 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
120 "multiple_bookings" => "INT NOT NULL DEFAULT -1"
121 ),
122 "keys" => array(
123 "primary" => array(
124 "" => array("c_uid")
125 ),
126 "key" => array(
127 "domain" => array("domain")
128 )
129 ),
130 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
131 ),
132 "relayhosts" => array(
133 "cols" => array(
134 "id" => "INT NOT NULL AUTO_INCREMENT",
135 "hostname" => "VARCHAR(255) NOT NULL",
136 "username" => "VARCHAR(255) NOT NULL",
137 "password" => "VARCHAR(255) NOT NULL",
138 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
139 ),
140 "keys" => array(
141 "primary" => array(
142 "" => array("id")
143 ),
144 "key" => array(
145 "hostname" => array("hostname")
146 )
147 ),
148 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
149 ),
150 "transports" => array(
151 "cols" => array(
152 "id" => "INT NOT NULL AUTO_INCREMENT",
153 "destination" => "VARCHAR(255) NOT NULL",
154 "nexthop" => "VARCHAR(255) NOT NULL",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200155 "username" => "VARCHAR(255) NOT NULL DEFAULT ''",
156 "password" => "VARCHAR(255) NOT NULL DEFAULT ''",
157 "is_mx_based" => "TINYINT(1) NOT NULL DEFAULT '0'",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100158 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
159 ),
160 "keys" => array(
161 "primary" => array(
162 "" => array("id")
163 ),
164 "key" => array(
165 "destination" => array("destination"),
166 "nexthop" => array("nexthop"),
167 )
168 ),
169 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
170 ),
171 "alias" => array(
172 "cols" => array(
173 "id" => "INT NOT NULL AUTO_INCREMENT",
174 "address" => "VARCHAR(255) NOT NULL",
175 "goto" => "TEXT NOT NULL",
176 "domain" => "VARCHAR(255) NOT NULL",
177 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
178 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
179 "private_comment" => "TEXT",
180 "public_comment" => "TEXT",
181 "sogo_visible" => "TINYINT(1) NOT NULL DEFAULT '1'",
182 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
183 ),
184 "keys" => array(
185 "primary" => array(
186 "" => array("id")
187 ),
188 "unique" => array(
189 "address" => array("address")
190 ),
191 "key" => array(
192 "domain" => array("domain")
193 )
194 ),
195 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
196 ),
197 "api" => array(
198 "cols" => array(
199 "api_key" => "VARCHAR(255) NOT NULL",
200 "allow_from" => "VARCHAR(512) NOT NULL",
201 "skip_ip_check" => "TINYINT(1) NOT NULL DEFAULT '0'",
202 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
203 "modified" => "DATETIME ON UPDATE NOW(0)",
204 "access" => "ENUM('ro', 'rw') NOT NULL DEFAULT 'rw'",
205 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
206 ),
207 "keys" => array(
208 "primary" => array(
209 "" => array("api_key")
210 ),
211 ),
212 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
213 ),
214 "sender_acl" => array(
215 "cols" => array(
216 "id" => "INT NOT NULL AUTO_INCREMENT",
217 "logged_in_as" => "VARCHAR(255) NOT NULL",
218 "send_as" => "VARCHAR(255) NOT NULL",
219 "external" => "TINYINT(1) NOT NULL DEFAULT '0'"
220 ),
221 "keys" => array(
222 "primary" => array(
223 "" => array("id")
224 )
225 ),
226 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
227 ),
228 "domain" => array(
229 // Todo: Move some attributes to json
230 "cols" => array(
231 "domain" => "VARCHAR(255) NOT NULL",
232 "description" => "VARCHAR(255)",
233 "aliases" => "INT(10) NOT NULL DEFAULT '0'",
234 "mailboxes" => "INT(10) NOT NULL DEFAULT '0'",
235 "defquota" => "BIGINT(20) NOT NULL DEFAULT '3072'",
236 "maxquota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
237 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
238 "relayhost" => "VARCHAR(255) NOT NULL DEFAULT '0'",
239 "backupmx" => "TINYINT(1) NOT NULL DEFAULT '0'",
240 "gal" => "TINYINT(1) NOT NULL DEFAULT '1'",
241 "relay_all_recipients" => "TINYINT(1) NOT NULL DEFAULT '0'",
242 "relay_unknown_only" => "TINYINT(1) NOT NULL DEFAULT '0'",
243 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
244 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
245 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
246 ),
247 "keys" => array(
248 "primary" => array(
249 "" => array("domain")
250 )
251 ),
252 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
253 ),
254 "tls_policy_override" => array(
255 "cols" => array(
256 "id" => "INT NOT NULL AUTO_INCREMENT",
257 "dest" => "VARCHAR(255) NOT NULL",
258 "policy" => "ENUM('none', 'may', 'encrypt', 'dane', 'dane-only', 'fingerprint', 'verify', 'secure') NOT NULL",
259 "parameters" => "VARCHAR(255) DEFAULT ''",
260 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
261 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
262 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
263 ),
264 "keys" => array(
265 "primary" => array(
266 "" => array("id")
267 ),
268 "unique" => array(
269 "dest" => array("dest")
270 ),
271 ),
272 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
273 ),
274 "quarantine" => array(
275 "cols" => array(
276 "id" => "INT NOT NULL AUTO_INCREMENT",
277 "qid" => "VARCHAR(30) NOT NULL",
278 "subject" => "VARCHAR(500)",
279 "score" => "FLOAT(8,2)",
280 "ip" => "VARCHAR(50)",
281 "action" => "CHAR(20) NOT NULL DEFAULT 'unknown'",
282 "symbols" => "JSON",
283 "fuzzy_hashes" => "JSON",
284 "sender" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
285 "rcpt" => "VARCHAR(255)",
286 "msg" => "LONGTEXT",
287 "domain" => "VARCHAR(255)",
288 "notified" => "TINYINT(1) NOT NULL DEFAULT '0'",
289 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
290 "user" => "VARCHAR(255) NOT NULL DEFAULT 'unknown'",
291 ),
292 "keys" => array(
293 "primary" => array(
294 "" => array("id")
295 )
296 ),
297 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
298 ),
299 "mailbox" => array(
300 "cols" => array(
301 "username" => "VARCHAR(255) NOT NULL",
302 "password" => "VARCHAR(255) NOT NULL",
303 "name" => "VARCHAR(255)",
304 "description" => "VARCHAR(255)",
305 // mailbox_path_prefix is followed by domain/local_part/
306 "mailbox_path_prefix" => "VARCHAR(150) DEFAULT '/var/vmail/'",
307 "quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
308 "local_part" => "VARCHAR(255) NOT NULL",
309 "domain" => "VARCHAR(255) NOT NULL",
310 "attributes" => "JSON",
311 "kind" => "VARCHAR(100) NOT NULL DEFAULT ''",
312 "multiple_bookings" => "INT NOT NULL DEFAULT -1",
313 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
314 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
315 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
316 ),
317 "keys" => array(
318 "primary" => array(
319 "" => array("username")
320 ),
321 "key" => array(
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200322 "domain" => array("domain"),
323 "kind" => array("kind")
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100324 )
325 ),
326 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
327 ),
328 "sieve_filters" => array(
329 "cols" => array(
330 "id" => "INT NOT NULL AUTO_INCREMENT",
331 "username" => "VARCHAR(255) NOT NULL",
332 "script_desc" => "VARCHAR(255) NOT NULL",
333 "script_name" => "ENUM('active','inactive')",
334 "script_data" => "TEXT NOT NULL",
335 "filter_type" => "ENUM('postfilter','prefilter')",
336 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
337 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
338 ),
339 "keys" => array(
340 "primary" => array(
341 "" => array("id")
342 ),
343 "key" => array(
344 "username" => array("username"),
345 "script_desc" => array("script_desc")
346 ),
347 "fkey" => array(
348 "fk_username_sieve_global_before" => array(
349 "col" => "username",
350 "ref" => "mailbox.username",
351 "delete" => "CASCADE",
352 "update" => "NO ACTION"
353 )
354 )
355 ),
356 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
357 ),
358 "app_passwd" => array(
359 "cols" => array(
360 "id" => "INT NOT NULL AUTO_INCREMENT",
361 "name" => "VARCHAR(255) NOT NULL",
362 "mailbox" => "VARCHAR(255) NOT NULL",
363 "domain" => "VARCHAR(255) NOT NULL",
364 "password" => "VARCHAR(255) NOT NULL",
365 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
366 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
367 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
368 ),
369 "keys" => array(
370 "primary" => array(
371 "" => array("id")
372 ),
373 "key" => array(
374 "mailbox" => array("mailbox"),
375 "password" => array("password"),
376 "domain" => array("domain"),
377 ),
378 "fkey" => array(
379 "fk_username_app_passwd" => array(
380 "col" => "mailbox",
381 "ref" => "mailbox.username",
382 "delete" => "CASCADE",
383 "update" => "NO ACTION"
384 )
385 )
386 ),
387 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
388 ),
389 "user_acl" => array(
390 "cols" => array(
391 "username" => "VARCHAR(255) NOT NULL",
392 "spam_alias" => "TINYINT(1) NOT NULL DEFAULT '1'",
393 "tls_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
394 "spam_score" => "TINYINT(1) NOT NULL DEFAULT '1'",
395 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
396 "delimiter_action" => "TINYINT(1) NOT NULL DEFAULT '1'",
397 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
398 "eas_reset" => "TINYINT(1) NOT NULL DEFAULT '1'",
399 "sogo_profile_reset" => "TINYINT(1) NOT NULL DEFAULT '0'",
400 "pushover" => "TINYINT(1) NOT NULL DEFAULT '1'",
401 // quarantine is for quarantine actions, todo: rename
402 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
403 "quarantine_attachments" => "TINYINT(1) NOT NULL DEFAULT '1'",
404 "quarantine_notification" => "TINYINT(1) NOT NULL DEFAULT '1'",
405 "quarantine_category" => "TINYINT(1) NOT NULL DEFAULT '1'",
406 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
407 ),
408 "keys" => array(
409 "primary" => array(
410 "" => array("username")
411 ),
412 "fkey" => array(
413 "fk_username" => array(
414 "col" => "username",
415 "ref" => "mailbox.username",
416 "delete" => "CASCADE",
417 "update" => "NO ACTION"
418 )
419 )
420 ),
421 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
422 ),
423 "alias_domain" => array(
424 "cols" => array(
425 "alias_domain" => "VARCHAR(255) NOT NULL",
426 "target_domain" => "VARCHAR(255) NOT NULL",
427 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
428 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
429 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
430 ),
431 "keys" => array(
432 "primary" => array(
433 "" => array("alias_domain")
434 ),
435 "key" => array(
436 "active" => array("active"),
437 "target_domain" => array("target_domain")
438 )
439 ),
440 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
441 ),
442 "spamalias" => array(
443 "cols" => array(
444 "address" => "VARCHAR(255) NOT NULL",
445 "goto" => "TEXT NOT NULL",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200446 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
447 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
448 "validity" => "INT(11)"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100449 ),
450 "keys" => array(
451 "primary" => array(
452 "" => array("address")
453 ),
454 ),
455 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
456 ),
457 "filterconf" => array(
458 "cols" => array(
459 "object" => "VARCHAR(255) NOT NULL DEFAULT ''",
460 "option" => "VARCHAR(50) NOT NULL DEFAULT ''",
461 "value" => "VARCHAR(100) NOT NULL DEFAULT ''",
462 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
463 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
464 "prefid" => "INT(11) NOT NULL AUTO_INCREMENT"
465 ),
466 "keys" => array(
467 "primary" => array(
468 "" => array("prefid")
469 ),
470 "key" => array(
471 "object" => array("object")
472 )
473 ),
474 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
475 ),
476 "settingsmap" => array(
477 "cols" => array(
478 "id" => "INT NOT NULL AUTO_INCREMENT",
479 "desc" => "VARCHAR(255) NOT NULL",
480 "content" => "LONGTEXT NOT NULL",
481 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
482 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
483 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
484 ),
485 "keys" => array(
486 "primary" => array(
487 "" => array("id")
488 )
489 ),
490 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
491 ),
492 "logs" => array(
493 "cols" => array(
494 "id" => "INT NOT NULL AUTO_INCREMENT",
495 "task" => "CHAR(32) NOT NULL DEFAULT '000000'",
496 "type" => "VARCHAR(32) DEFAULT ''",
497 "msg" => "TEXT",
498 "call" => "TEXT",
499 "user" => "VARCHAR(64) NOT NULL",
500 "role" => "VARCHAR(32) NOT NULL",
501 "remote" => "VARCHAR(39) NOT NULL",
502 "time" => "INT(11) NOT NULL"
503 ),
504 "keys" => array(
505 "primary" => array(
506 "" => array("id")
507 )
508 ),
509 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
510 ),
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200511 "sasl_log" => array(
512 "cols" => array(
513 "service" => "VARCHAR(32) NOT NULL DEFAULT ''",
514 "app_password" => "INT",
515 "username" => "VARCHAR(255) NOT NULL",
516 "real_rip" => "VARCHAR(64) NOT NULL",
517 "datetime" => "DATETIME(0) NOT NULL DEFAULT NOW(0)"
518 ),
519 "keys" => array(
520 "primary" => array(
521 "" => array("service", "real_rip", "username")
522 ),
523 "key" => array(
524 "username" => array("username"),
525 "service" => array("service"),
526 "datetime" => array("datetime"),
527 "real_rip" => array("real_rip")
528 )
529 ),
530 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
531 ),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100532 "quota2" => array(
533 "cols" => array(
534 "username" => "VARCHAR(255) NOT NULL",
535 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
536 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
537 ),
538 "keys" => array(
539 "primary" => array(
540 "" => array("username")
541 )
542 ),
543 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
544 ),
545 "quota2replica" => array(
546 "cols" => array(
547 "username" => "VARCHAR(255) NOT NULL",
548 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
549 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
550 ),
551 "keys" => array(
552 "primary" => array(
553 "" => array("username")
554 )
555 ),
556 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
557 ),
558 "domain_admins" => array(
559 "cols" => array(
560 "id" => "INT NOT NULL AUTO_INCREMENT",
561 "username" => "VARCHAR(255) NOT NULL",
562 "domain" => "VARCHAR(255) NOT NULL",
563 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
564 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
565 ),
566 "keys" => array(
567 "primary" => array(
568 "" => array("id")
569 ),
570 "key" => array(
571 "username" => array("username")
572 )
573 ),
574 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
575 ),
576 "da_acl" => array(
577 "cols" => array(
578 "username" => "VARCHAR(255) NOT NULL",
579 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
580 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
581 "login_as" => "TINYINT(1) NOT NULL DEFAULT '1'",
582 "sogo_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
583 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
584 "bcc_maps" => "TINYINT(1) NOT NULL DEFAULT '1'",
585 "pushover" => "TINYINT(1) NOT NULL DEFAULT '0'",
586 "filters" => "TINYINT(1) NOT NULL DEFAULT '1'",
587 "ratelimit" => "TINYINT(1) NOT NULL DEFAULT '1'",
588 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
589 "extend_sender_acl" => "TINYINT(1) NOT NULL DEFAULT '0'",
590 "unlimited_quota" => "TINYINT(1) NOT NULL DEFAULT '0'",
591 "protocol_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
592 "smtp_ip_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
593 "alias_domains" => "TINYINT(1) NOT NULL DEFAULT '0'",
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200594 "mailbox_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
595 "domain_relayhost" => "TINYINT(1) NOT NULL DEFAULT '1'",
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100596 "domain_desc" => "TINYINT(1) NOT NULL DEFAULT '0'"
597 ),
598 "keys" => array(
599 "primary" => array(
600 "" => array("username")
601 )
602 ),
603 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
604 ),
605 "imapsync" => array(
606 "cols" => array(
607 "id" => "INT NOT NULL AUTO_INCREMENT",
608 "user2" => "VARCHAR(255) NOT NULL",
609 "host1" => "VARCHAR(255) NOT NULL",
610 "authmech1" => "ENUM('PLAIN','LOGIN','CRAM-MD5') DEFAULT 'PLAIN'",
611 "regextrans2" => "VARCHAR(255) DEFAULT ''",
612 "authmd51" => "TINYINT(1) NOT NULL DEFAULT 0",
613 "domain2" => "VARCHAR(255) NOT NULL DEFAULT ''",
614 "subfolder2" => "VARCHAR(255) NOT NULL DEFAULT ''",
615 "user1" => "VARCHAR(255) NOT NULL",
616 "password1" => "VARCHAR(255) NOT NULL",
617 "exclude" => "VARCHAR(500) NOT NULL DEFAULT ''",
618 "maxage" => "SMALLINT NOT NULL DEFAULT '0'",
619 "mins_interval" => "SMALLINT UNSIGNED NOT NULL DEFAULT '0'",
620 "maxbytespersecond" => "VARCHAR(50) NOT NULL DEFAULT '0'",
621 "port1" => "SMALLINT UNSIGNED NOT NULL",
622 "enc1" => "ENUM('TLS','SSL','PLAIN') DEFAULT 'TLS'",
623 "delete2duplicates" => "TINYINT(1) NOT NULL DEFAULT '1'",
624 "delete1" => "TINYINT(1) NOT NULL DEFAULT '0'",
625 "delete2" => "TINYINT(1) NOT NULL DEFAULT '0'",
626 "automap" => "TINYINT(1) NOT NULL DEFAULT '0'",
627 "skipcrossduplicates" => "TINYINT(1) NOT NULL DEFAULT '0'",
628 "custom_params" => "VARCHAR(512) NOT NULL DEFAULT ''",
629 "timeout1" => "SMALLINT NOT NULL DEFAULT '600'",
630 "timeout2" => "SMALLINT NOT NULL DEFAULT '600'",
631 "subscribeall" => "TINYINT(1) NOT NULL DEFAULT '1'",
632 "is_running" => "TINYINT(1) NOT NULL DEFAULT '0'",
633 "returned_text" => "LONGTEXT",
634 "last_run" => "TIMESTAMP NULL DEFAULT NULL",
635 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
636 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
637 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
638 ),
639 "keys" => array(
640 "primary" => array(
641 "" => array("id")
642 )
643 ),
644 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
645 ),
646 "bcc_maps" => array(
647 "cols" => array(
648 "id" => "INT NOT NULL AUTO_INCREMENT",
649 "local_dest" => "VARCHAR(255) NOT NULL",
650 "bcc_dest" => "VARCHAR(255) NOT NULL",
651 "domain" => "VARCHAR(255) NOT NULL",
652 "type" => "ENUM('sender','rcpt')",
653 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
654 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
655 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
656 ),
657 "keys" => array(
658 "primary" => array(
659 "" => array("id")
660 ),
661 "key" => array(
662 "local_dest" => array("local_dest"),
663 )
664 ),
665 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
666 ),
667 "recipient_maps" => array(
668 "cols" => array(
669 "id" => "INT NOT NULL AUTO_INCREMENT",
670 "old_dest" => "VARCHAR(255) NOT NULL",
671 "new_dest" => "VARCHAR(255) NOT NULL",
672 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
673 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
674 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
675 ),
676 "keys" => array(
677 "primary" => array(
678 "" => array("id")
679 ),
680 "key" => array(
681 "local_dest" => array("old_dest"),
682 )
683 ),
684 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
685 ),
686 "tfa" => array(
687 "cols" => array(
688 "id" => "INT NOT NULL AUTO_INCREMENT",
689 "key_id" => "VARCHAR(255) NOT NULL",
690 "username" => "VARCHAR(255) NOT NULL",
691 "authmech" => "ENUM('yubi_otp', 'u2f', 'hotp', 'totp')",
692 "secret" => "VARCHAR(255) DEFAULT NULL",
693 "keyHandle" => "VARCHAR(255) DEFAULT NULL",
694 "publicKey" => "VARCHAR(255) DEFAULT NULL",
695 "counter" => "INT NOT NULL DEFAULT '0'",
696 "certificate" => "TEXT",
697 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
698 ),
699 "keys" => array(
700 "primary" => array(
701 "" => array("id")
702 )
703 ),
704 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
705 ),
706 "forwarding_hosts" => array(
707 "cols" => array(
708 "host" => "VARCHAR(255) NOT NULL",
709 "source" => "VARCHAR(255) NOT NULL",
710 "filter_spam" => "TINYINT(1) NOT NULL DEFAULT '0'"
711 ),
712 "keys" => array(
713 "primary" => array(
714 "" => array("host")
715 ),
716 ),
717 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
718 ),
719 "sogo_acl" => array(
720 "cols" => array(
721 "id" => "INT NOT NULL AUTO_INCREMENT",
722 "c_folder_id" => "INT NOT NULL",
723 "c_object" => "VARCHAR(255) NOT NULL",
724 "c_uid" => "VARCHAR(255) NOT NULL",
725 "c_role" => "VARCHAR(80) NOT NULL"
726 ),
727 "keys" => array(
728 "primary" => array(
729 "" => array("id")
730 ),
731 "key" => array(
732 "sogo_acl_c_folder_id_idx" => array("c_folder_id"),
733 "sogo_acl_c_uid_idx" => array("c_uid")
734 )
735 ),
736 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
737 ),
738 "sogo_alarms_folder" => array(
739 "cols" => array(
740 "id" => "INT NOT NULL AUTO_INCREMENT",
741 "c_path" => "VARCHAR(255) NOT NULL",
742 "c_name" => "VARCHAR(255) NOT NULL",
743 "c_uid" => "VARCHAR(255) NOT NULL",
744 "c_recurrence_id" => "INT(11) DEFAULT NULL",
745 "c_alarm_number" => "INT(11) NOT NULL",
746 "c_alarm_date" => "INT(11) NOT NULL"
747 ),
748 "keys" => array(
749 "primary" => array(
750 "" => array("id")
751 )
752 ),
753 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
754 ),
755 "sogo_cache_folder" => array(
756 "cols" => array(
757 "c_uid" => "VARCHAR(255) NOT NULL",
758 "c_path" => "VARCHAR(255) NOT NULL",
759 "c_parent_path" => "VARCHAR(255) DEFAULT NULL",
760 "c_type" => "TINYINT(3) unsigned NOT NULL",
761 "c_creationdate" => "INT(11) NOT NULL",
762 "c_lastmodified" => "INT(11) NOT NULL",
763 "c_version" => "INT(11) NOT NULL DEFAULT '0'",
764 "c_deleted" => "TINYINT(4) NOT NULL DEFAULT '0'",
765 "c_content" => "LONGTEXT"
766 ),
767 "keys" => array(
768 "primary" => array(
769 "" => array("c_uid", "c_path")
770 ),
771 ),
772 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
773 ),
774 "sogo_folder_info" => array(
775 "cols" => array(
776 "c_folder_id" => "BIGINT(20) unsigned NOT NULL AUTO_INCREMENT",
777 "c_path" => "VARCHAR(255) NOT NULL",
778 "c_path1" => "VARCHAR(255) NOT NULL",
779 "c_path2" => "VARCHAR(255) DEFAULT NULL",
780 "c_path3" => "VARCHAR(255) DEFAULT NULL",
781 "c_path4" => "VARCHAR(255) DEFAULT NULL",
782 "c_foldername" => "VARCHAR(255) NOT NULL",
783 "c_location" => "VARCHAR(2048) DEFAULT NULL",
784 "c_quick_location" => "VARCHAR(2048) DEFAULT NULL",
785 "c_acl_location" => "VARCHAR(2048) DEFAULT NULL",
786 "c_folder_type" => "VARCHAR(255) NOT NULL"
787 ),
788 "keys" => array(
789 "primary" => array(
790 "" => array("c_path")
791 ),
792 "unique" => array(
793 "c_folder_id" => array("c_folder_id")
794 )
795 ),
796 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
797 ),
798 "sogo_quick_appointment" => array(
799 "cols" => array(
800 "c_folder_id" => "INT NOT NULL",
801 "c_name" => "VARCHAR(255) NOT NULL",
802 "c_uid" => "VARCHAR(1000) NOT NULL",
803 "c_startdate" => "INT",
804 "c_enddate" => "INT",
805 "c_cycleenddate" => "INT",
806 "c_title" => "VARCHAR(1000) NOT NULL",
807 "c_participants" => "TEXT",
808 "c_isallday" => "INT",
809 "c_iscycle" => "INT",
810 "c_cycleinfo" => "TEXT",
811 "c_classification" => "INT NOT NULL",
812 "c_isopaque" => "INT NOT NULL",
813 "c_status" => "INT NOT NULL",
814 "c_priority" => "INT",
815 "c_location" => "VARCHAR(255)",
816 "c_orgmail" => "VARCHAR(255)",
817 "c_partmails" => "TEXT",
818 "c_partstates" => "TEXT",
819 "c_category" => "VARCHAR(255)",
820 "c_sequence" => "INT",
821 "c_component" => "VARCHAR(10) NOT NULL",
822 "c_nextalarm" => "INT",
823 "c_description" => "TEXT"
824 ),
825 "keys" => array(
826 "primary" => array(
827 "" => array("c_folder_id", "c_name")
828 )
829 ),
830 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
831 ),
832 "sogo_quick_contact" => array(
833 "cols" => array(
834 "c_folder_id" => "INT NOT NULL",
835 "c_name" => "VARCHAR(255) NOT NULL",
836 "c_givenname" => "VARCHAR(255)",
837 "c_cn" => "VARCHAR(255)",
838 "c_sn" => "VARCHAR(255)",
839 "c_screenname" => "VARCHAR(255)",
840 "c_l" => "VARCHAR(255)",
841 "c_mail" => "TEXT",
842 "c_o" => "VARCHAR(500)",
843 "c_ou" => "VARCHAR(255)",
844 "c_telephonenumber" => "VARCHAR(255)",
845 "c_categories" => "VARCHAR(255)",
846 "c_component" => "VARCHAR(10) NOT NULL",
847 "c_hascertificate" => "INT4 DEFAULT 0"
848 ),
849 "keys" => array(
850 "primary" => array(
851 "" => array("c_folder_id", "c_name")
852 )
853 ),
854 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
855 ),
856 "sogo_sessions_folder" => array(
857 "cols" => array(
858 "c_id" => "VARCHAR(255) NOT NULL",
859 "c_value" => "VARCHAR(255) NOT NULL",
860 "c_creationdate" => "INT(11) NOT NULL",
861 "c_lastseen" => "INT(11) NOT NULL"
862 ),
863 "keys" => array(
864 "primary" => array(
865 "" => array("c_id")
866 )
867 ),
868 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
869 ),
870 "sogo_store" => array(
871 "cols" => array(
872 "c_folder_id" => "INT NOT NULL",
873 "c_name" => "VARCHAR(255) NOT NULL",
874 "c_content" => "MEDIUMTEXT NOT NULL",
875 "c_creationdate" => "INT NOT NULL",
876 "c_lastmodified" => "INT NOT NULL",
877 "c_version" => "INT NOT NULL",
878 "c_deleted" => "INT"
879 ),
880 "keys" => array(
881 "primary" => array(
882 "" => array("c_folder_id", "c_name")
883 )
884 ),
885 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
886 ),
887 "pushover" => array(
888 "cols" => array(
889 "username" => "VARCHAR(255) NOT NULL",
890 "key" => "VARCHAR(255) NOT NULL",
891 "token" => "VARCHAR(255) NOT NULL",
892 "attributes" => "JSON",
893 "title" => "TEXT",
894 "text" => "TEXT",
895 "senders" => "TEXT",
896 "senders_regex" => "TEXT",
897 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
898 ),
899 "keys" => array(
900 "primary" => array(
901 "" => array("username")
902 )
903 ),
904 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
905 ),
906 "sogo_user_profile" => array(
907 "cols" => array(
908 "c_uid" => "VARCHAR(255) NOT NULL",
909 "c_defaults" => "LONGTEXT",
910 "c_settings" => "LONGTEXT"
911 ),
912 "keys" => array(
913 "primary" => array(
914 "" => array("c_uid")
915 )
916 ),
917 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
918 ),
919 "oauth_clients" => array(
920 "cols" => array(
921 "id" => "INT NOT NULL AUTO_INCREMENT",
922 "client_id" => "VARCHAR(80) NOT NULL",
923 "client_secret" => "VARCHAR(80)",
924 "redirect_uri" => "VARCHAR(2000)",
925 "grant_types" => "VARCHAR(80)",
926 "scope" => "VARCHAR(4000)",
927 "user_id" => "VARCHAR(80)"
928 ),
929 "keys" => array(
930 "primary" => array(
931 "" => array("client_id")
932 ),
933 "unique" => array(
934 "id" => array("id")
935 )
936 ),
937 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
938 ),
939 "oauth_access_tokens" => array(
940 "cols" => array(
941 "access_token" => "VARCHAR(40) NOT NULL",
942 "client_id" => "VARCHAR(80) NOT NULL",
943 "user_id" => "VARCHAR(80)",
944 "expires" => "TIMESTAMP NOT NULL",
945 "scope" => "VARCHAR(4000)"
946 ),
947 "keys" => array(
948 "primary" => array(
949 "" => array("access_token")
950 )
951 ),
952 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
953 ),
954 "oauth_authorization_codes" => array(
955 "cols" => array(
956 "authorization_code" => "VARCHAR(40) NOT NULL",
957 "client_id" => "VARCHAR(80) NOT NULL",
958 "user_id" => "VARCHAR(80)",
959 "redirect_uri" => "VARCHAR(2000)",
960 "expires" => "TIMESTAMP NOT NULL",
961 "scope" => "VARCHAR(4000)",
962 "id_token" => "VARCHAR(1000)"
963 ),
964 "keys" => array(
965 "primary" => array(
966 "" => array("authorization_code")
967 )
968 ),
969 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
970 ),
971 "oauth_refresh_tokens" => array(
972 "cols" => array(
973 "refresh_token" => "VARCHAR(40) NOT NULL",
974 "client_id" => "VARCHAR(80) NOT NULL",
975 "user_id" => "VARCHAR(80)",
976 "expires" => "TIMESTAMP NOT NULL",
977 "scope" => "VARCHAR(4000)"
978 ),
979 "keys" => array(
980 "primary" => array(
981 "" => array("refresh_token")
982 )
983 ),
984 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
985 )
986 );
987
988 foreach ($tables as $table => $properties) {
989 // Migrate to quarantine
990 if ($table == 'quarantine') {
991 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantaine'");
992 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
993 if ($num_results != 0) {
994 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantine'");
995 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
996 if ($num_results == 0) {
997 $pdo->query("RENAME TABLE `quarantaine` TO `quarantine`");
998 }
999 }
1000 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001001
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001002 // Migrate tls_enforce_* options
1003 if ($table == 'mailbox') {
1004 $stmt = $pdo->query("SHOW TABLES LIKE 'mailbox'");
1005 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1006 if ($num_results != 0) {
1007 $stmt = $pdo->query("SHOW COLUMNS FROM `mailbox` LIKE '%tls_enforce%'");
1008 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1009 if ($num_results != 0) {
1010 $stmt = $pdo->query("SELECT `username`, `tls_enforce_in`, `tls_enforce_out` FROM `mailbox`");
1011 $tls_options_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1012 while ($row = array_shift($tls_options_rows)) {
1013 $tls_options[$row['username']] = array('tls_enforce_in' => $row['tls_enforce_in'], 'tls_enforce_out' => $row['tls_enforce_out']);
1014 }
1015 }
1016 }
1017 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001018
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001019 $stmt = $pdo->query("SHOW TABLES LIKE '" . $table . "'");
1020 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1021 if ($num_results != 0) {
1022 $stmt = $pdo->prepare("SELECT CONCAT('ALTER TABLE ', `table_schema`, '.', `table_name`, ' DROP FOREIGN KEY ', `constraint_name`, ';') AS `FKEY_DROP` FROM `information_schema`.`table_constraints`
1023 WHERE `constraint_type` = 'FOREIGN KEY' AND `table_name` = :table;");
1024 $stmt->execute(array(':table' => $table));
1025 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
1026 while ($row = array_shift($rows)) {
1027 $pdo->query($row['FKEY_DROP']);
1028 }
1029 foreach($properties['cols'] as $column => $type) {
1030 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
1031 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1032 if ($num_results == 0) {
1033 if (strpos($type, 'AUTO_INCREMENT') !== false) {
1034 $type = $type . ' PRIMARY KEY ';
1035 // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists
1036 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1037 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1038 if ($num_results != 0) {
1039 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1040 }
1041 }
1042 $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type);
1043 }
1044 else {
1045 $pdo->query("ALTER TABLE `" . $table . "` MODIFY COLUMN `" . $column . "` " . $type);
1046 }
1047 }
1048 foreach($properties['keys'] as $key_type => $key_content) {
1049 if (strtolower($key_type) == 'primary') {
1050 foreach ($key_content as $key_values) {
1051 $fields = "`" . implode("`, `", $key_values) . "`";
1052 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1053 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1054 $is_drop = ($num_results != 0) ? "DROP PRIMARY KEY, " : "";
1055 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD PRIMARY KEY (" . $fields . ")");
1056 }
1057 }
1058 if (strtolower($key_type) == 'key') {
1059 foreach ($key_content as $key_name => $key_values) {
1060 $fields = "`" . implode("`, `", $key_values) . "`";
1061 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1062 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1063 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1064 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD KEY `" . $key_name . "` (" . $fields . ")");
1065 }
1066 }
1067 if (strtolower($key_type) == 'unique') {
1068 foreach ($key_content as $key_name => $key_values) {
1069 $fields = "`" . implode("`, `", $key_values) . "`";
1070 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1071 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1072 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1073 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD UNIQUE KEY `" . $key_name . "` (" . $fields . ")");
1074 }
1075 }
1076 if (strtolower($key_type) == 'fkey') {
1077 foreach ($key_content as $key_name => $key_values) {
1078 $fields = "`" . implode("`, `", $key_values) . "`";
1079 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1080 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1081 if ($num_results != 0) {
1082 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $key_name . "`");
1083 }
1084 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1085 $pdo->query("ALTER TABLE `" . $table . "` ADD FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1086 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update']);
1087 }
1088 }
1089 }
1090 // Drop all vanished columns
1091 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "`");
1092 $cols_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1093 while ($row = array_shift($cols_in_table)) {
1094 if (!array_key_exists($row['Field'], $properties['cols'])) {
1095 $pdo->query("ALTER TABLE `" . $table . "` DROP COLUMN `" . $row['Field'] . "`;");
1096 }
1097 }
1098
1099 // Step 1: Get all non-primary keys, that currently exist and those that should exist
1100 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE `Key_name` != 'PRIMARY'");
1101 $keys_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1102 $keys_to_exist = array();
1103 if (isset($properties['keys']['unique']) && is_array($properties['keys']['unique'])) {
1104 foreach ($properties['keys']['unique'] as $key_name => $key_values) {
1105 $keys_to_exist[] = $key_name;
1106 }
1107 }
1108 if (isset($properties['keys']['key']) && is_array($properties['keys']['key'])) {
1109 foreach ($properties['keys']['key'] as $key_name => $key_values) {
1110 $keys_to_exist[] = $key_name;
1111 }
1112 }
1113 // Index for foreign key must exist
1114 if (isset($properties['keys']['fkey']) && is_array($properties['keys']['fkey'])) {
1115 foreach ($properties['keys']['fkey'] as $key_name => $key_values) {
1116 $keys_to_exist[] = $key_name;
1117 }
1118 }
1119 // Step 2: Drop all vanished indexes
1120 while ($row = array_shift($keys_in_table)) {
1121 if (!in_array($row['Key_name'], $keys_to_exist)) {
1122 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $row['Key_name'] . "`");
1123 }
1124 }
1125 // Step 3: Drop all vanished primary keys
1126 if (!isset($properties['keys']['primary'])) {
1127 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1128 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1129 if ($num_results != 0) {
1130 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1131 }
1132 }
1133 }
1134 else {
1135 // Create table if it is missing
1136 $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (";
1137 foreach($properties['cols'] as $column => $type) {
1138 $sql .= "`" . $column . "` " . $type . ",";
1139 }
1140 foreach($properties['keys'] as $key_type => $key_content) {
1141 if (strtolower($key_type) == 'primary') {
1142 foreach ($key_content as $key_values) {
1143 $fields = "`" . implode("`, `", $key_values) . "`";
1144 $sql .= "PRIMARY KEY (" . $fields . ")" . ",";
1145 }
1146 }
1147 elseif (strtolower($key_type) == 'key') {
1148 foreach ($key_content as $key_name => $key_values) {
1149 $fields = "`" . implode("`, `", $key_values) . "`";
1150 $sql .= "KEY `" . $key_name . "` (" . $fields . ")" . ",";
1151 }
1152 }
1153 elseif (strtolower($key_type) == 'unique') {
1154 foreach ($key_content as $key_name => $key_values) {
1155 $fields = "`" . implode("`, `", $key_values) . "`";
1156 $sql .= "UNIQUE KEY `" . $key_name . "` (" . $fields . ")" . ",";
1157 }
1158 }
1159 elseif (strtolower($key_type) == 'fkey') {
1160 foreach ($key_content as $key_name => $key_values) {
1161 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1162 $sql .= "FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1163 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update'] . ",";
1164 }
1165 }
1166 }
1167 $sql = rtrim($sql, ",");
1168 $sql .= ") " . $properties['attr'];
1169 $pdo->query($sql);
1170 }
1171 // Reset table attributes
1172 $pdo->query("ALTER TABLE `" . $table . "` " . $properties['attr'] . ";");
1173
1174 }
1175
1176 // Recreate SQL views
1177 foreach ($views as $view => $create) {
1178 $pdo->query("DROP VIEW IF EXISTS `" . $view . "`;");
1179 $pdo->query($create);
1180 }
1181
1182 // Mitigate imapsync pipemess issue
1183 $pdo->query("UPDATE `imapsync` SET `custom_params` = '' WHERE `custom_params` LIKE '%pipemess%';");
1184
1185 // Inject admin if not exists
1186 $stmt = $pdo->query("SELECT NULL FROM `admin`");
1187 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1188 if ($num_results == 0) {
1189 $pdo->query("INSERT INTO `admin` (`username`, `password`, `superadmin`, `created`, `modified`, `active`)
1190 VALUES ('admin', '{SSHA256}K8eVJ6YsZbQCfuJvSUbaQRLr0HPLz5rC9IAp0PAFl0tmNDBkMDc0NDAyOTAxN2Rk', 1, NOW(), NOW(), 1)");
1191 $pdo->query("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
1192 SELECT `username`, 'ALL', NOW(), 1 FROM `admin`
1193 WHERE superadmin='1' AND `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1194 $pdo->query("DELETE FROM `admin` WHERE `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1195 }
1196 // Insert new DB schema version
1197 $pdo->query("REPLACE INTO `versions` (`application`, `version`) VALUES ('db_schema', '" . $db_version . "');");
1198
1199 // Fix dangling domain admins
1200 $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1201 $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1202
1203 // Migrate attributes
1204 // pushover
1205 $pdo->query("UPDATE `pushover` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1206 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.evaluate_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.evaluate_x_prio') IS NULL;");
1207 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.only_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.only_x_prio') IS NULL;");
1208 // mailbox
1209 $pdo->query("UPDATE `mailbox` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001210 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.passwd_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.passwd_update') IS NULL;");
1211 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.relayhost', \"0\") WHERE JSON_VALUE(`attributes`, '$.relayhost') IS NULL;");
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001212 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.force_pw_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_pw_update') IS NULL;");
1213 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sogo_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sogo_access') IS NULL;");
1214 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.imap_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.imap_access') IS NULL;");
1215 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.pop3_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.pop3_access') IS NULL;");
1216 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.smtp_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.smtp_access') IS NULL;");
1217 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.mailbox_format', \"maildir:\") WHERE JSON_VALUE(`attributes`, '$.mailbox_format') IS NULL;");
1218 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', \"never\") WHERE JSON_VALUE(`attributes`, '$.quarantine_notification') IS NULL;");
1219 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', \"reject\") WHERE JSON_VALUE(`attributes`, '$.quarantine_category') IS NULL;");
1220 foreach($tls_options as $tls_user => $tls_options) {
1221 $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_enforce_in),
1222 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_enforce_out)
1223 WHERE `username` = :username");
1224 $stmt->execute(array(':tls_enforce_in' => $tls_options['tls_enforce_in'], ':tls_enforce_out' => $tls_options['tls_enforce_out'], ':username' => $tls_user));
1225 }
1226 // Set tls_enforce_* if still missing (due to deleted attrs for example)
1227 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_out') IS NULL;");
1228 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_in') IS NULL;");
1229 // Fix ACL
1230 $pdo->query("INSERT INTO `user_acl` (`username`) SELECT `username` FROM `mailbox` WHERE `kind` = '' AND NOT EXISTS (SELECT `username` FROM `user_acl`);");
1231 $pdo->query("INSERT INTO `da_acl` (`username`) SELECT DISTINCT `username` FROM `domain_admins` WHERE `username` != 'admin' AND NOT EXISTS (SELECT `username` FROM `da_acl`);");
1232 // Fix domain_admins
1233 $pdo->query("DELETE FROM `domain_admins` WHERE `domain` = 'ALL';");
1234
1235 if (php_sapi_name() == "cli") {
1236 echo "DB initialization completed" . PHP_EOL;
1237 } else {
1238 $_SESSION['return'][] = array(
1239 'type' => 'success',
1240 'log' => array(__FUNCTION__),
1241 'msg' => 'db_init_complete'
1242 );
1243 }
1244 }
1245 catch (PDOException $e) {
1246 if (php_sapi_name() == "cli") {
1247 echo "DB initialization failed: " . print_r($e, true) . PHP_EOL;
1248 } else {
1249 $_SESSION['return'][] = array(
1250 'type' => 'danger',
1251 'log' => array(__FUNCTION__),
1252 'msg' => array('mysql_error', $e)
1253 );
1254 }
1255 }
1256}
1257if (php_sapi_name() == "cli") {
1258 include '/web/inc/vars.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001259 include '/web/inc/functions.docker.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001260 // $now = new DateTime();
1261 // $mins = $now->getOffset() / 60;
1262 // $sgn = ($mins < 0 ? -1 : 1);
1263 // $mins = abs($mins);
1264 // $hrs = floor($mins / 60);
1265 // $mins -= $hrs * 60;
1266 // $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
1267 $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
1268 $opt = [
1269 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
1270 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
1271 PDO::ATTR_EMULATE_PREPARES => false,
1272 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
1273 ];
1274 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
1275 $stmt = $pdo->query("SELECT COUNT('OK') AS OK_C FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sogo_view' OR TABLE_NAME = '_sogo_static_view';");
1276 $res = $stmt->fetch(PDO::FETCH_ASSOC);
1277 if (intval($res['OK_C']) === 2) {
1278 // Be more precise when replacing into _sogo_static_view, col orders may change
1279 try {
1280 $stmt = $pdo->query("REPLACE INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`)
1281 SELECT `c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings` from sogo_view");
1282 $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');");
1283 echo "Fixed _sogo_static_view" . PHP_EOL;
1284 }
1285 catch ( Exception $e ) {
1286 // Dunno
1287 }
1288 }
1289 try {
1290 $m = new Memcached();
1291 $m->addServer('memcached', 11211);
1292 $m->flush();
1293 echo "Cleaned up memcached". PHP_EOL;
1294 }
1295 catch ( Exception $e ) {
1296 // Dunno
1297 }
1298 init_db_schema();
1299}