blob: 8010bbddd2b103b6bfcdfde0726a94f6b04981c6 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2function init_db_schema() {
3 try {
4 global $pdo;
5
6 $db_version = "28112020_1210";
7
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",
155 "username" => "VARCHAR(255) NOT NULL",
156 "password" => "VARCHAR(255) NOT NULL",
157 "lookup_mx" => "TINYINT(1) NOT NULL DEFAULT '1'",
158 "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(
322 "domain" => array("domain")
323 )
324 ),
325 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
326 ),
327 "sieve_filters" => array(
328 "cols" => array(
329 "id" => "INT NOT NULL AUTO_INCREMENT",
330 "username" => "VARCHAR(255) NOT NULL",
331 "script_desc" => "VARCHAR(255) NOT NULL",
332 "script_name" => "ENUM('active','inactive')",
333 "script_data" => "TEXT NOT NULL",
334 "filter_type" => "ENUM('postfilter','prefilter')",
335 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
336 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP"
337 ),
338 "keys" => array(
339 "primary" => array(
340 "" => array("id")
341 ),
342 "key" => array(
343 "username" => array("username"),
344 "script_desc" => array("script_desc")
345 ),
346 "fkey" => array(
347 "fk_username_sieve_global_before" => array(
348 "col" => "username",
349 "ref" => "mailbox.username",
350 "delete" => "CASCADE",
351 "update" => "NO ACTION"
352 )
353 )
354 ),
355 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
356 ),
357 "app_passwd" => array(
358 "cols" => array(
359 "id" => "INT NOT NULL AUTO_INCREMENT",
360 "name" => "VARCHAR(255) NOT NULL",
361 "mailbox" => "VARCHAR(255) NOT NULL",
362 "domain" => "VARCHAR(255) NOT NULL",
363 "password" => "VARCHAR(255) NOT NULL",
364 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
365 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
366 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
367 ),
368 "keys" => array(
369 "primary" => array(
370 "" => array("id")
371 ),
372 "key" => array(
373 "mailbox" => array("mailbox"),
374 "password" => array("password"),
375 "domain" => array("domain"),
376 ),
377 "fkey" => array(
378 "fk_username_app_passwd" => array(
379 "col" => "mailbox",
380 "ref" => "mailbox.username",
381 "delete" => "CASCADE",
382 "update" => "NO ACTION"
383 )
384 )
385 ),
386 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
387 ),
388 "user_acl" => array(
389 "cols" => array(
390 "username" => "VARCHAR(255) NOT NULL",
391 "spam_alias" => "TINYINT(1) NOT NULL DEFAULT '1'",
392 "tls_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
393 "spam_score" => "TINYINT(1) NOT NULL DEFAULT '1'",
394 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
395 "delimiter_action" => "TINYINT(1) NOT NULL DEFAULT '1'",
396 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
397 "eas_reset" => "TINYINT(1) NOT NULL DEFAULT '1'",
398 "sogo_profile_reset" => "TINYINT(1) NOT NULL DEFAULT '0'",
399 "pushover" => "TINYINT(1) NOT NULL DEFAULT '1'",
400 // quarantine is for quarantine actions, todo: rename
401 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
402 "quarantine_attachments" => "TINYINT(1) NOT NULL DEFAULT '1'",
403 "quarantine_notification" => "TINYINT(1) NOT NULL DEFAULT '1'",
404 "quarantine_category" => "TINYINT(1) NOT NULL DEFAULT '1'",
405 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
406 ),
407 "keys" => array(
408 "primary" => array(
409 "" => array("username")
410 ),
411 "fkey" => array(
412 "fk_username" => array(
413 "col" => "username",
414 "ref" => "mailbox.username",
415 "delete" => "CASCADE",
416 "update" => "NO ACTION"
417 )
418 )
419 ),
420 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
421 ),
422 "alias_domain" => array(
423 "cols" => array(
424 "alias_domain" => "VARCHAR(255) NOT NULL",
425 "target_domain" => "VARCHAR(255) NOT NULL",
426 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
427 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
428 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
429 ),
430 "keys" => array(
431 "primary" => array(
432 "" => array("alias_domain")
433 ),
434 "key" => array(
435 "active" => array("active"),
436 "target_domain" => array("target_domain")
437 )
438 ),
439 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
440 ),
441 "spamalias" => array(
442 "cols" => array(
443 "address" => "VARCHAR(255) NOT NULL",
444 "goto" => "TEXT NOT NULL",
445 "validity" => "INT(11) NOT NULL"
446 ),
447 "keys" => array(
448 "primary" => array(
449 "" => array("address")
450 ),
451 ),
452 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
453 ),
454 "filterconf" => array(
455 "cols" => array(
456 "object" => "VARCHAR(255) NOT NULL DEFAULT ''",
457 "option" => "VARCHAR(50) NOT NULL DEFAULT ''",
458 "value" => "VARCHAR(100) NOT NULL DEFAULT ''",
459 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
460 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
461 "prefid" => "INT(11) NOT NULL AUTO_INCREMENT"
462 ),
463 "keys" => array(
464 "primary" => array(
465 "" => array("prefid")
466 ),
467 "key" => array(
468 "object" => array("object")
469 )
470 ),
471 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
472 ),
473 "settingsmap" => array(
474 "cols" => array(
475 "id" => "INT NOT NULL AUTO_INCREMENT",
476 "desc" => "VARCHAR(255) NOT NULL",
477 "content" => "LONGTEXT NOT NULL",
478 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
479 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
480 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
481 ),
482 "keys" => array(
483 "primary" => array(
484 "" => array("id")
485 )
486 ),
487 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
488 ),
489 "logs" => array(
490 "cols" => array(
491 "id" => "INT NOT NULL AUTO_INCREMENT",
492 "task" => "CHAR(32) NOT NULL DEFAULT '000000'",
493 "type" => "VARCHAR(32) DEFAULT ''",
494 "msg" => "TEXT",
495 "call" => "TEXT",
496 "user" => "VARCHAR(64) NOT NULL",
497 "role" => "VARCHAR(32) NOT NULL",
498 "remote" => "VARCHAR(39) NOT NULL",
499 "time" => "INT(11) NOT NULL"
500 ),
501 "keys" => array(
502 "primary" => array(
503 "" => array("id")
504 )
505 ),
506 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
507 ),
508 "quota2" => array(
509 "cols" => array(
510 "username" => "VARCHAR(255) NOT NULL",
511 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
512 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
513 ),
514 "keys" => array(
515 "primary" => array(
516 "" => array("username")
517 )
518 ),
519 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
520 ),
521 "quota2replica" => array(
522 "cols" => array(
523 "username" => "VARCHAR(255) NOT NULL",
524 "bytes" => "BIGINT(20) NOT NULL DEFAULT '0'",
525 "messages" => "BIGINT(20) NOT NULL DEFAULT '0'"
526 ),
527 "keys" => array(
528 "primary" => array(
529 "" => array("username")
530 )
531 ),
532 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
533 ),
534 "domain_admins" => array(
535 "cols" => array(
536 "id" => "INT NOT NULL AUTO_INCREMENT",
537 "username" => "VARCHAR(255) NOT NULL",
538 "domain" => "VARCHAR(255) NOT NULL",
539 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
540 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
541 ),
542 "keys" => array(
543 "primary" => array(
544 "" => array("id")
545 ),
546 "key" => array(
547 "username" => array("username")
548 )
549 ),
550 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
551 ),
552 "da_acl" => array(
553 "cols" => array(
554 "username" => "VARCHAR(255) NOT NULL",
555 "syncjobs" => "TINYINT(1) NOT NULL DEFAULT '1'",
556 "quarantine" => "TINYINT(1) NOT NULL DEFAULT '1'",
557 "login_as" => "TINYINT(1) NOT NULL DEFAULT '1'",
558 "sogo_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
559 "app_passwds" => "TINYINT(1) NOT NULL DEFAULT '1'",
560 "bcc_maps" => "TINYINT(1) NOT NULL DEFAULT '1'",
561 "pushover" => "TINYINT(1) NOT NULL DEFAULT '0'",
562 "filters" => "TINYINT(1) NOT NULL DEFAULT '1'",
563 "ratelimit" => "TINYINT(1) NOT NULL DEFAULT '1'",
564 "spam_policy" => "TINYINT(1) NOT NULL DEFAULT '1'",
565 "extend_sender_acl" => "TINYINT(1) NOT NULL DEFAULT '0'",
566 "unlimited_quota" => "TINYINT(1) NOT NULL DEFAULT '0'",
567 "protocol_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
568 "smtp_ip_access" => "TINYINT(1) NOT NULL DEFAULT '1'",
569 "alias_domains" => "TINYINT(1) NOT NULL DEFAULT '0'",
570 "domain_desc" => "TINYINT(1) NOT NULL DEFAULT '0'"
571 ),
572 "keys" => array(
573 "primary" => array(
574 "" => array("username")
575 )
576 ),
577 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
578 ),
579 "imapsync" => array(
580 "cols" => array(
581 "id" => "INT NOT NULL AUTO_INCREMENT",
582 "user2" => "VARCHAR(255) NOT NULL",
583 "host1" => "VARCHAR(255) NOT NULL",
584 "authmech1" => "ENUM('PLAIN','LOGIN','CRAM-MD5') DEFAULT 'PLAIN'",
585 "regextrans2" => "VARCHAR(255) DEFAULT ''",
586 "authmd51" => "TINYINT(1) NOT NULL DEFAULT 0",
587 "domain2" => "VARCHAR(255) NOT NULL DEFAULT ''",
588 "subfolder2" => "VARCHAR(255) NOT NULL DEFAULT ''",
589 "user1" => "VARCHAR(255) NOT NULL",
590 "password1" => "VARCHAR(255) NOT NULL",
591 "exclude" => "VARCHAR(500) NOT NULL DEFAULT ''",
592 "maxage" => "SMALLINT NOT NULL DEFAULT '0'",
593 "mins_interval" => "SMALLINT UNSIGNED NOT NULL DEFAULT '0'",
594 "maxbytespersecond" => "VARCHAR(50) NOT NULL DEFAULT '0'",
595 "port1" => "SMALLINT UNSIGNED NOT NULL",
596 "enc1" => "ENUM('TLS','SSL','PLAIN') DEFAULT 'TLS'",
597 "delete2duplicates" => "TINYINT(1) NOT NULL DEFAULT '1'",
598 "delete1" => "TINYINT(1) NOT NULL DEFAULT '0'",
599 "delete2" => "TINYINT(1) NOT NULL DEFAULT '0'",
600 "automap" => "TINYINT(1) NOT NULL DEFAULT '0'",
601 "skipcrossduplicates" => "TINYINT(1) NOT NULL DEFAULT '0'",
602 "custom_params" => "VARCHAR(512) NOT NULL DEFAULT ''",
603 "timeout1" => "SMALLINT NOT NULL DEFAULT '600'",
604 "timeout2" => "SMALLINT NOT NULL DEFAULT '600'",
605 "subscribeall" => "TINYINT(1) NOT NULL DEFAULT '1'",
606 "is_running" => "TINYINT(1) NOT NULL DEFAULT '0'",
607 "returned_text" => "LONGTEXT",
608 "last_run" => "TIMESTAMP NULL DEFAULT NULL",
609 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
610 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
611 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
612 ),
613 "keys" => array(
614 "primary" => array(
615 "" => array("id")
616 )
617 ),
618 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
619 ),
620 "bcc_maps" => array(
621 "cols" => array(
622 "id" => "INT NOT NULL AUTO_INCREMENT",
623 "local_dest" => "VARCHAR(255) NOT NULL",
624 "bcc_dest" => "VARCHAR(255) NOT NULL",
625 "domain" => "VARCHAR(255) NOT NULL",
626 "type" => "ENUM('sender','rcpt')",
627 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
628 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
629 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
630 ),
631 "keys" => array(
632 "primary" => array(
633 "" => array("id")
634 ),
635 "key" => array(
636 "local_dest" => array("local_dest"),
637 )
638 ),
639 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
640 ),
641 "recipient_maps" => array(
642 "cols" => array(
643 "id" => "INT NOT NULL AUTO_INCREMENT",
644 "old_dest" => "VARCHAR(255) NOT NULL",
645 "new_dest" => "VARCHAR(255) NOT NULL",
646 "created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
647 "modified" => "DATETIME ON UPDATE CURRENT_TIMESTAMP",
648 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
649 ),
650 "keys" => array(
651 "primary" => array(
652 "" => array("id")
653 ),
654 "key" => array(
655 "local_dest" => array("old_dest"),
656 )
657 ),
658 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
659 ),
660 "tfa" => array(
661 "cols" => array(
662 "id" => "INT NOT NULL AUTO_INCREMENT",
663 "key_id" => "VARCHAR(255) NOT NULL",
664 "username" => "VARCHAR(255) NOT NULL",
665 "authmech" => "ENUM('yubi_otp', 'u2f', 'hotp', 'totp')",
666 "secret" => "VARCHAR(255) DEFAULT NULL",
667 "keyHandle" => "VARCHAR(255) DEFAULT NULL",
668 "publicKey" => "VARCHAR(255) DEFAULT NULL",
669 "counter" => "INT NOT NULL DEFAULT '0'",
670 "certificate" => "TEXT",
671 "active" => "TINYINT(1) NOT NULL DEFAULT '0'"
672 ),
673 "keys" => array(
674 "primary" => array(
675 "" => array("id")
676 )
677 ),
678 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
679 ),
680 "forwarding_hosts" => array(
681 "cols" => array(
682 "host" => "VARCHAR(255) NOT NULL",
683 "source" => "VARCHAR(255) NOT NULL",
684 "filter_spam" => "TINYINT(1) NOT NULL DEFAULT '0'"
685 ),
686 "keys" => array(
687 "primary" => array(
688 "" => array("host")
689 ),
690 ),
691 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
692 ),
693 "sogo_acl" => array(
694 "cols" => array(
695 "id" => "INT NOT NULL AUTO_INCREMENT",
696 "c_folder_id" => "INT NOT NULL",
697 "c_object" => "VARCHAR(255) NOT NULL",
698 "c_uid" => "VARCHAR(255) NOT NULL",
699 "c_role" => "VARCHAR(80) NOT NULL"
700 ),
701 "keys" => array(
702 "primary" => array(
703 "" => array("id")
704 ),
705 "key" => array(
706 "sogo_acl_c_folder_id_idx" => array("c_folder_id"),
707 "sogo_acl_c_uid_idx" => array("c_uid")
708 )
709 ),
710 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
711 ),
712 "sogo_alarms_folder" => array(
713 "cols" => array(
714 "id" => "INT NOT NULL AUTO_INCREMENT",
715 "c_path" => "VARCHAR(255) NOT NULL",
716 "c_name" => "VARCHAR(255) NOT NULL",
717 "c_uid" => "VARCHAR(255) NOT NULL",
718 "c_recurrence_id" => "INT(11) DEFAULT NULL",
719 "c_alarm_number" => "INT(11) NOT NULL",
720 "c_alarm_date" => "INT(11) NOT NULL"
721 ),
722 "keys" => array(
723 "primary" => array(
724 "" => array("id")
725 )
726 ),
727 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
728 ),
729 "sogo_cache_folder" => array(
730 "cols" => array(
731 "c_uid" => "VARCHAR(255) NOT NULL",
732 "c_path" => "VARCHAR(255) NOT NULL",
733 "c_parent_path" => "VARCHAR(255) DEFAULT NULL",
734 "c_type" => "TINYINT(3) unsigned NOT NULL",
735 "c_creationdate" => "INT(11) NOT NULL",
736 "c_lastmodified" => "INT(11) NOT NULL",
737 "c_version" => "INT(11) NOT NULL DEFAULT '0'",
738 "c_deleted" => "TINYINT(4) NOT NULL DEFAULT '0'",
739 "c_content" => "LONGTEXT"
740 ),
741 "keys" => array(
742 "primary" => array(
743 "" => array("c_uid", "c_path")
744 ),
745 ),
746 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
747 ),
748 "sogo_folder_info" => array(
749 "cols" => array(
750 "c_folder_id" => "BIGINT(20) unsigned NOT NULL AUTO_INCREMENT",
751 "c_path" => "VARCHAR(255) NOT NULL",
752 "c_path1" => "VARCHAR(255) NOT NULL",
753 "c_path2" => "VARCHAR(255) DEFAULT NULL",
754 "c_path3" => "VARCHAR(255) DEFAULT NULL",
755 "c_path4" => "VARCHAR(255) DEFAULT NULL",
756 "c_foldername" => "VARCHAR(255) NOT NULL",
757 "c_location" => "VARCHAR(2048) DEFAULT NULL",
758 "c_quick_location" => "VARCHAR(2048) DEFAULT NULL",
759 "c_acl_location" => "VARCHAR(2048) DEFAULT NULL",
760 "c_folder_type" => "VARCHAR(255) NOT NULL"
761 ),
762 "keys" => array(
763 "primary" => array(
764 "" => array("c_path")
765 ),
766 "unique" => array(
767 "c_folder_id" => array("c_folder_id")
768 )
769 ),
770 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
771 ),
772 "sogo_quick_appointment" => array(
773 "cols" => array(
774 "c_folder_id" => "INT NOT NULL",
775 "c_name" => "VARCHAR(255) NOT NULL",
776 "c_uid" => "VARCHAR(1000) NOT NULL",
777 "c_startdate" => "INT",
778 "c_enddate" => "INT",
779 "c_cycleenddate" => "INT",
780 "c_title" => "VARCHAR(1000) NOT NULL",
781 "c_participants" => "TEXT",
782 "c_isallday" => "INT",
783 "c_iscycle" => "INT",
784 "c_cycleinfo" => "TEXT",
785 "c_classification" => "INT NOT NULL",
786 "c_isopaque" => "INT NOT NULL",
787 "c_status" => "INT NOT NULL",
788 "c_priority" => "INT",
789 "c_location" => "VARCHAR(255)",
790 "c_orgmail" => "VARCHAR(255)",
791 "c_partmails" => "TEXT",
792 "c_partstates" => "TEXT",
793 "c_category" => "VARCHAR(255)",
794 "c_sequence" => "INT",
795 "c_component" => "VARCHAR(10) NOT NULL",
796 "c_nextalarm" => "INT",
797 "c_description" => "TEXT"
798 ),
799 "keys" => array(
800 "primary" => array(
801 "" => array("c_folder_id", "c_name")
802 )
803 ),
804 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
805 ),
806 "sogo_quick_contact" => array(
807 "cols" => array(
808 "c_folder_id" => "INT NOT NULL",
809 "c_name" => "VARCHAR(255) NOT NULL",
810 "c_givenname" => "VARCHAR(255)",
811 "c_cn" => "VARCHAR(255)",
812 "c_sn" => "VARCHAR(255)",
813 "c_screenname" => "VARCHAR(255)",
814 "c_l" => "VARCHAR(255)",
815 "c_mail" => "TEXT",
816 "c_o" => "VARCHAR(500)",
817 "c_ou" => "VARCHAR(255)",
818 "c_telephonenumber" => "VARCHAR(255)",
819 "c_categories" => "VARCHAR(255)",
820 "c_component" => "VARCHAR(10) NOT NULL",
821 "c_hascertificate" => "INT4 DEFAULT 0"
822 ),
823 "keys" => array(
824 "primary" => array(
825 "" => array("c_folder_id", "c_name")
826 )
827 ),
828 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
829 ),
830 "sogo_sessions_folder" => array(
831 "cols" => array(
832 "c_id" => "VARCHAR(255) NOT NULL",
833 "c_value" => "VARCHAR(255) NOT NULL",
834 "c_creationdate" => "INT(11) NOT NULL",
835 "c_lastseen" => "INT(11) NOT NULL"
836 ),
837 "keys" => array(
838 "primary" => array(
839 "" => array("c_id")
840 )
841 ),
842 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
843 ),
844 "sogo_store" => array(
845 "cols" => array(
846 "c_folder_id" => "INT NOT NULL",
847 "c_name" => "VARCHAR(255) NOT NULL",
848 "c_content" => "MEDIUMTEXT NOT NULL",
849 "c_creationdate" => "INT NOT NULL",
850 "c_lastmodified" => "INT NOT NULL",
851 "c_version" => "INT NOT NULL",
852 "c_deleted" => "INT"
853 ),
854 "keys" => array(
855 "primary" => array(
856 "" => array("c_folder_id", "c_name")
857 )
858 ),
859 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
860 ),
861 "pushover" => array(
862 "cols" => array(
863 "username" => "VARCHAR(255) NOT NULL",
864 "key" => "VARCHAR(255) NOT NULL",
865 "token" => "VARCHAR(255) NOT NULL",
866 "attributes" => "JSON",
867 "title" => "TEXT",
868 "text" => "TEXT",
869 "senders" => "TEXT",
870 "senders_regex" => "TEXT",
871 "active" => "TINYINT(1) NOT NULL DEFAULT '1'"
872 ),
873 "keys" => array(
874 "primary" => array(
875 "" => array("username")
876 )
877 ),
878 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
879 ),
880 "sogo_user_profile" => array(
881 "cols" => array(
882 "c_uid" => "VARCHAR(255) NOT NULL",
883 "c_defaults" => "LONGTEXT",
884 "c_settings" => "LONGTEXT"
885 ),
886 "keys" => array(
887 "primary" => array(
888 "" => array("c_uid")
889 )
890 ),
891 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
892 ),
893 "oauth_clients" => array(
894 "cols" => array(
895 "id" => "INT NOT NULL AUTO_INCREMENT",
896 "client_id" => "VARCHAR(80) NOT NULL",
897 "client_secret" => "VARCHAR(80)",
898 "redirect_uri" => "VARCHAR(2000)",
899 "grant_types" => "VARCHAR(80)",
900 "scope" => "VARCHAR(4000)",
901 "user_id" => "VARCHAR(80)"
902 ),
903 "keys" => array(
904 "primary" => array(
905 "" => array("client_id")
906 ),
907 "unique" => array(
908 "id" => array("id")
909 )
910 ),
911 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
912 ),
913 "oauth_access_tokens" => array(
914 "cols" => array(
915 "access_token" => "VARCHAR(40) NOT NULL",
916 "client_id" => "VARCHAR(80) NOT NULL",
917 "user_id" => "VARCHAR(80)",
918 "expires" => "TIMESTAMP NOT NULL",
919 "scope" => "VARCHAR(4000)"
920 ),
921 "keys" => array(
922 "primary" => array(
923 "" => array("access_token")
924 )
925 ),
926 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
927 ),
928 "oauth_authorization_codes" => array(
929 "cols" => array(
930 "authorization_code" => "VARCHAR(40) NOT NULL",
931 "client_id" => "VARCHAR(80) NOT NULL",
932 "user_id" => "VARCHAR(80)",
933 "redirect_uri" => "VARCHAR(2000)",
934 "expires" => "TIMESTAMP NOT NULL",
935 "scope" => "VARCHAR(4000)",
936 "id_token" => "VARCHAR(1000)"
937 ),
938 "keys" => array(
939 "primary" => array(
940 "" => array("authorization_code")
941 )
942 ),
943 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
944 ),
945 "oauth_refresh_tokens" => array(
946 "cols" => array(
947 "refresh_token" => "VARCHAR(40) NOT NULL",
948 "client_id" => "VARCHAR(80) NOT NULL",
949 "user_id" => "VARCHAR(80)",
950 "expires" => "TIMESTAMP NOT NULL",
951 "scope" => "VARCHAR(4000)"
952 ),
953 "keys" => array(
954 "primary" => array(
955 "" => array("refresh_token")
956 )
957 ),
958 "attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
959 )
960 );
961
962 foreach ($tables as $table => $properties) {
963 // Migrate to quarantine
964 if ($table == 'quarantine') {
965 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantaine'");
966 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
967 if ($num_results != 0) {
968 $stmt = $pdo->query("SHOW TABLES LIKE 'quarantine'");
969 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
970 if ($num_results == 0) {
971 $pdo->query("RENAME TABLE `quarantaine` TO `quarantine`");
972 }
973 }
974 }
975 // Migrate tls_enforce_* options
976 if ($table == 'mailbox') {
977 $stmt = $pdo->query("SHOW TABLES LIKE 'mailbox'");
978 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
979 if ($num_results != 0) {
980 $stmt = $pdo->query("SHOW COLUMNS FROM `mailbox` LIKE '%tls_enforce%'");
981 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
982 if ($num_results != 0) {
983 $stmt = $pdo->query("SELECT `username`, `tls_enforce_in`, `tls_enforce_out` FROM `mailbox`");
984 $tls_options_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
985 while ($row = array_shift($tls_options_rows)) {
986 $tls_options[$row['username']] = array('tls_enforce_in' => $row['tls_enforce_in'], 'tls_enforce_out' => $row['tls_enforce_out']);
987 }
988 }
989 }
990 }
991 $stmt = $pdo->query("SHOW TABLES LIKE '" . $table . "'");
992 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
993 if ($num_results != 0) {
994 $stmt = $pdo->prepare("SELECT CONCAT('ALTER TABLE ', `table_schema`, '.', `table_name`, ' DROP FOREIGN KEY ', `constraint_name`, ';') AS `FKEY_DROP` FROM `information_schema`.`table_constraints`
995 WHERE `constraint_type` = 'FOREIGN KEY' AND `table_name` = :table;");
996 $stmt->execute(array(':table' => $table));
997 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
998 while ($row = array_shift($rows)) {
999 $pdo->query($row['FKEY_DROP']);
1000 }
1001 foreach($properties['cols'] as $column => $type) {
1002 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
1003 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1004 if ($num_results == 0) {
1005 if (strpos($type, 'AUTO_INCREMENT') !== false) {
1006 $type = $type . ' PRIMARY KEY ';
1007 // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists
1008 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1009 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1010 if ($num_results != 0) {
1011 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1012 }
1013 }
1014 $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type);
1015 }
1016 else {
1017 $pdo->query("ALTER TABLE `" . $table . "` MODIFY COLUMN `" . $column . "` " . $type);
1018 }
1019 }
1020 foreach($properties['keys'] as $key_type => $key_content) {
1021 if (strtolower($key_type) == 'primary') {
1022 foreach ($key_content as $key_values) {
1023 $fields = "`" . implode("`, `", $key_values) . "`";
1024 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1025 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1026 $is_drop = ($num_results != 0) ? "DROP PRIMARY KEY, " : "";
1027 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD PRIMARY KEY (" . $fields . ")");
1028 }
1029 }
1030 if (strtolower($key_type) == 'key') {
1031 foreach ($key_content as $key_name => $key_values) {
1032 $fields = "`" . implode("`, `", $key_values) . "`";
1033 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1034 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1035 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1036 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD KEY `" . $key_name . "` (" . $fields . ")");
1037 }
1038 }
1039 if (strtolower($key_type) == 'unique') {
1040 foreach ($key_content as $key_name => $key_values) {
1041 $fields = "`" . implode("`, `", $key_values) . "`";
1042 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1043 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1044 $is_drop = ($num_results != 0) ? "DROP INDEX `" . $key_name . "`, " : "";
1045 $pdo->query("ALTER TABLE `" . $table . "` " . $is_drop . "ADD UNIQUE KEY `" . $key_name . "` (" . $fields . ")");
1046 }
1047 }
1048 if (strtolower($key_type) == 'fkey') {
1049 foreach ($key_content as $key_name => $key_values) {
1050 $fields = "`" . implode("`, `", $key_values) . "`";
1051 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = '" . $key_name . "'");
1052 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1053 if ($num_results != 0) {
1054 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $key_name . "`");
1055 }
1056 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1057 $pdo->query("ALTER TABLE `" . $table . "` ADD FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1058 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update']);
1059 }
1060 }
1061 }
1062 // Drop all vanished columns
1063 $stmt = $pdo->query("SHOW COLUMNS FROM `" . $table . "`");
1064 $cols_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1065 while ($row = array_shift($cols_in_table)) {
1066 if (!array_key_exists($row['Field'], $properties['cols'])) {
1067 $pdo->query("ALTER TABLE `" . $table . "` DROP COLUMN `" . $row['Field'] . "`;");
1068 }
1069 }
1070
1071 // Step 1: Get all non-primary keys, that currently exist and those that should exist
1072 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE `Key_name` != 'PRIMARY'");
1073 $keys_in_table = $stmt->fetchAll(PDO::FETCH_ASSOC);
1074 $keys_to_exist = array();
1075 if (isset($properties['keys']['unique']) && is_array($properties['keys']['unique'])) {
1076 foreach ($properties['keys']['unique'] as $key_name => $key_values) {
1077 $keys_to_exist[] = $key_name;
1078 }
1079 }
1080 if (isset($properties['keys']['key']) && is_array($properties['keys']['key'])) {
1081 foreach ($properties['keys']['key'] as $key_name => $key_values) {
1082 $keys_to_exist[] = $key_name;
1083 }
1084 }
1085 // Index for foreign key must exist
1086 if (isset($properties['keys']['fkey']) && is_array($properties['keys']['fkey'])) {
1087 foreach ($properties['keys']['fkey'] as $key_name => $key_values) {
1088 $keys_to_exist[] = $key_name;
1089 }
1090 }
1091 // Step 2: Drop all vanished indexes
1092 while ($row = array_shift($keys_in_table)) {
1093 if (!in_array($row['Key_name'], $keys_to_exist)) {
1094 $pdo->query("ALTER TABLE `" . $table . "` DROP INDEX `" . $row['Key_name'] . "`");
1095 }
1096 }
1097 // Step 3: Drop all vanished primary keys
1098 if (!isset($properties['keys']['primary'])) {
1099 $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'");
1100 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1101 if ($num_results != 0) {
1102 $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY");
1103 }
1104 }
1105 }
1106 else {
1107 // Create table if it is missing
1108 $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (";
1109 foreach($properties['cols'] as $column => $type) {
1110 $sql .= "`" . $column . "` " . $type . ",";
1111 }
1112 foreach($properties['keys'] as $key_type => $key_content) {
1113 if (strtolower($key_type) == 'primary') {
1114 foreach ($key_content as $key_values) {
1115 $fields = "`" . implode("`, `", $key_values) . "`";
1116 $sql .= "PRIMARY KEY (" . $fields . ")" . ",";
1117 }
1118 }
1119 elseif (strtolower($key_type) == 'key') {
1120 foreach ($key_content as $key_name => $key_values) {
1121 $fields = "`" . implode("`, `", $key_values) . "`";
1122 $sql .= "KEY `" . $key_name . "` (" . $fields . ")" . ",";
1123 }
1124 }
1125 elseif (strtolower($key_type) == 'unique') {
1126 foreach ($key_content as $key_name => $key_values) {
1127 $fields = "`" . implode("`, `", $key_values) . "`";
1128 $sql .= "UNIQUE KEY `" . $key_name . "` (" . $fields . ")" . ",";
1129 }
1130 }
1131 elseif (strtolower($key_type) == 'fkey') {
1132 foreach ($key_content as $key_name => $key_values) {
1133 @list($table_ref, $field_ref) = explode('.', $key_values['ref']);
1134 $sql .= "FOREIGN KEY `" . $key_name . "` (" . $key_values['col'] . ") REFERENCES `" . $table_ref . "` (`" . $field_ref . "`)
1135 ON DELETE " . $key_values['delete'] . " ON UPDATE " . $key_values['update'] . ",";
1136 }
1137 }
1138 }
1139 $sql = rtrim($sql, ",");
1140 $sql .= ") " . $properties['attr'];
1141 $pdo->query($sql);
1142 }
1143 // Reset table attributes
1144 $pdo->query("ALTER TABLE `" . $table . "` " . $properties['attr'] . ";");
1145
1146 }
1147
1148 // Recreate SQL views
1149 foreach ($views as $view => $create) {
1150 $pdo->query("DROP VIEW IF EXISTS `" . $view . "`;");
1151 $pdo->query($create);
1152 }
1153
1154 // Mitigate imapsync pipemess issue
1155 $pdo->query("UPDATE `imapsync` SET `custom_params` = '' WHERE `custom_params` LIKE '%pipemess%';");
1156
1157 // Inject admin if not exists
1158 $stmt = $pdo->query("SELECT NULL FROM `admin`");
1159 $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
1160 if ($num_results == 0) {
1161 $pdo->query("INSERT INTO `admin` (`username`, `password`, `superadmin`, `created`, `modified`, `active`)
1162 VALUES ('admin', '{SSHA256}K8eVJ6YsZbQCfuJvSUbaQRLr0HPLz5rC9IAp0PAFl0tmNDBkMDc0NDAyOTAxN2Rk', 1, NOW(), NOW(), 1)");
1163 $pdo->query("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
1164 SELECT `username`, 'ALL', NOW(), 1 FROM `admin`
1165 WHERE superadmin='1' AND `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1166 $pdo->query("DELETE FROM `admin` WHERE `username` NOT IN (SELECT `username` FROM `domain_admins`);");
1167 }
1168 // Insert new DB schema version
1169 $pdo->query("REPLACE INTO `versions` (`application`, `version`) VALUES ('db_schema', '" . $db_version . "');");
1170
1171 // Fix dangling domain admins
1172 $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1173 $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
1174
1175 // Migrate attributes
1176 // pushover
1177 $pdo->query("UPDATE `pushover` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1178 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.evaluate_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.evaluate_x_prio') IS NULL;");
1179 $pdo->query("UPDATE `pushover` SET `attributes` = JSON_SET(`attributes`, '$.only_x_prio', \"0\") WHERE JSON_VALUE(`attributes`, '$.only_x_prio') IS NULL;");
1180 // mailbox
1181 $pdo->query("UPDATE `mailbox` SET `attributes` = '{}' WHERE `attributes` = '' OR `attributes` IS NULL;");
1182 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.force_pw_update', \"0\") WHERE JSON_VALUE(`attributes`, '$.force_pw_update') IS NULL;");
1183 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.sogo_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.sogo_access') IS NULL;");
1184 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.imap_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.imap_access') IS NULL;");
1185 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.pop3_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.pop3_access') IS NULL;");
1186 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.smtp_access', \"1\") WHERE JSON_VALUE(`attributes`, '$.smtp_access') IS NULL;");
1187 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.mailbox_format', \"maildir:\") WHERE JSON_VALUE(`attributes`, '$.mailbox_format') IS NULL;");
1188 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', \"never\") WHERE JSON_VALUE(`attributes`, '$.quarantine_notification') IS NULL;");
1189 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', \"reject\") WHERE JSON_VALUE(`attributes`, '$.quarantine_category') IS NULL;");
1190 foreach($tls_options as $tls_user => $tls_options) {
1191 $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_enforce_in),
1192 `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_enforce_out)
1193 WHERE `username` = :username");
1194 $stmt->execute(array(':tls_enforce_in' => $tls_options['tls_enforce_in'], ':tls_enforce_out' => $tls_options['tls_enforce_out'], ':username' => $tls_user));
1195 }
1196 // Set tls_enforce_* if still missing (due to deleted attrs for example)
1197 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_out') IS NULL;");
1198 $pdo->query("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', \"1\") WHERE JSON_VALUE(`attributes`, '$.tls_enforce_in') IS NULL;");
1199 // Fix ACL
1200 $pdo->query("INSERT INTO `user_acl` (`username`) SELECT `username` FROM `mailbox` WHERE `kind` = '' AND NOT EXISTS (SELECT `username` FROM `user_acl`);");
1201 $pdo->query("INSERT INTO `da_acl` (`username`) SELECT DISTINCT `username` FROM `domain_admins` WHERE `username` != 'admin' AND NOT EXISTS (SELECT `username` FROM `da_acl`);");
1202 // Fix domain_admins
1203 $pdo->query("DELETE FROM `domain_admins` WHERE `domain` = 'ALL';");
1204
1205 if (php_sapi_name() == "cli") {
1206 echo "DB initialization completed" . PHP_EOL;
1207 } else {
1208 $_SESSION['return'][] = array(
1209 'type' => 'success',
1210 'log' => array(__FUNCTION__),
1211 'msg' => 'db_init_complete'
1212 );
1213 }
1214 }
1215 catch (PDOException $e) {
1216 if (php_sapi_name() == "cli") {
1217 echo "DB initialization failed: " . print_r($e, true) . PHP_EOL;
1218 } else {
1219 $_SESSION['return'][] = array(
1220 'type' => 'danger',
1221 'log' => array(__FUNCTION__),
1222 'msg' => array('mysql_error', $e)
1223 );
1224 }
1225 }
1226}
1227if (php_sapi_name() == "cli") {
1228 include '/web/inc/vars.inc.php';
1229 // $now = new DateTime();
1230 // $mins = $now->getOffset() / 60;
1231 // $sgn = ($mins < 0 ? -1 : 1);
1232 // $mins = abs($mins);
1233 // $hrs = floor($mins / 60);
1234 // $mins -= $hrs * 60;
1235 // $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
1236 $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
1237 $opt = [
1238 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
1239 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
1240 PDO::ATTR_EMULATE_PREPARES => false,
1241 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
1242 ];
1243 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
1244 $stmt = $pdo->query("SELECT COUNT('OK') AS OK_C FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sogo_view' OR TABLE_NAME = '_sogo_static_view';");
1245 $res = $stmt->fetch(PDO::FETCH_ASSOC);
1246 if (intval($res['OK_C']) === 2) {
1247 // Be more precise when replacing into _sogo_static_view, col orders may change
1248 try {
1249 $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`)
1250 SELECT `c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings` from sogo_view");
1251 $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');");
1252 echo "Fixed _sogo_static_view" . PHP_EOL;
1253 }
1254 catch ( Exception $e ) {
1255 // Dunno
1256 }
1257 }
1258 try {
1259 $m = new Memcached();
1260 $m->addServer('memcached', 11211);
1261 $m->flush();
1262 echo "Cleaned up memcached". PHP_EOL;
1263 }
1264 catch ( Exception $e ) {
1265 // Dunno
1266 }
1267 init_db_schema();
1268}