You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upgrade.php 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. <?php
  2. if (!defined('POSTFIXADMIN')) {
  3. require_once('common.php');
  4. }
  5. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  6. # @version $Id$
  7. # Note: run with upgrade.php?debug=1 to see all SQL error messages
  8. /**
  9. * Use this to check whether an object (Table, index etc) exists within a
  10. * PostgreSQL database.
  11. * @param String the object name
  12. * @return boolean true if it exists
  13. */
  14. function _pgsql_object_exists($name) {
  15. $sql = "select relname from pg_class where relname = '$name'";
  16. $r = db_query($sql);
  17. if ($r['rows'] == 1) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. function _pgsql_field_exists($table, $field) {
  23. # $table = table_by_key($table); # _pgsql_field_exists is always called with the expanded table name - don't expand it twice
  24. $sql = '
  25. SELECT
  26. a.attname,
  27. pg_catalog.format_type(a.atttypid, a.atttypmod) AS "Datatype"
  28. FROM
  29. pg_catalog.pg_attribute a
  30. WHERE
  31. a.attnum > 0
  32. AND NOT a.attisdropped
  33. AND a.attrelid = (
  34. SELECT c.oid
  35. FROM pg_catalog.pg_class c
  36. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  37. WHERE c.relname ~ ' . "'^($table)\$'
  38. AND pg_catalog.pg_table_is_visible(c.oid)
  39. )
  40. AND a.attname = '$field' ";
  41. $r = db_query($sql);
  42. $row = db_row($r['result']);
  43. if ($row) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. function _mysql_field_exists($table, $field) {
  49. # $table = table_by_key($table); # _mysql_field_exists is always called with the expanded table name - don't expand it twice
  50. $sql = "SHOW COLUMNS FROM $table LIKE '$field'";
  51. $r = db_query($sql);
  52. $row = db_row($r['result']);
  53. if ($row) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. function _sqlite_field_exists($table, $field) {
  59. $sql = "PRAGMA table_info($table)";
  60. $r = db_query($sql);
  61. while ($row = db_row($r['result'])) {
  62. if ($row[1] == $field) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. function _db_field_exists($table, $field) {
  69. global $CONF;
  70. if ($CONF['database_type'] == 'pgsql') {
  71. return _pgsql_field_exists($table, $field);
  72. } elseif ($CONF['database_type'] == 'sqlite') {
  73. return _sqlite_field_exists($table, $field);
  74. } else {
  75. return _mysql_field_exists($table, $field);
  76. }
  77. }
  78. function _upgrade_filter_function($name) {
  79. return preg_match('/upgrade_[\d]+(_mysql|_pgsql|_sqlite|_mysql_pgsql)?$/', $name) == 1;
  80. }
  81. function _db_add_field($table, $field, $fieldtype, $after) {
  82. global $CONF;
  83. $query = "ALTER TABLE " . table_by_key($table) . " ADD COLUMN $field $fieldtype";
  84. if ($CONF['database_type'] == 'mysql') {
  85. $query .= " AFTER $after "; # PgSQL does not support to specify where to add the column, MySQL does
  86. }
  87. if (! _db_field_exists(table_by_key($table), $field)) {
  88. $result = db_query_parsed($query);
  89. } else {
  90. printdebug("field already exists: $table.$field");
  91. }
  92. }
  93. function printdebug($text) {
  94. if (safeget('debug') != "") {
  95. print "<p style='color:#999'>$text</p>";
  96. }
  97. }
  98. $table = table_by_key('config');
  99. if ($CONF['database_type'] == 'pgsql') {
  100. // check if table already exists, if so, don't recreate it
  101. $r = db_query("SELECT relname FROM pg_class WHERE relname = '$table'");
  102. if ($r['rows'] == 0) {
  103. $pgsql = "
  104. CREATE TABLE $table (
  105. id SERIAL,
  106. name VARCHAR(20) NOT NULL UNIQUE,
  107. value VARCHAR(20) NOT NULL,
  108. PRIMARY KEY(id)
  109. )";
  110. db_query_parsed($pgsql);
  111. }
  112. } elseif (db_sqlite()) {
  113. $enc = 'PRAGMA encoding = "UTF-8"';
  114. db_query_parsed($enc);
  115. $sql = "
  116. CREATE TABLE {IF_NOT_EXISTS} $table (
  117. `id` {AUTOINCREMENT},
  118. `name` TEXT NOT NULL UNIQUE DEFAULT '',
  119. `value` TEXT NOT NULL DEFAULT ''
  120. )
  121. ";
  122. db_query_parsed($sql);
  123. } else {
  124. $mysql = "
  125. CREATE TABLE {IF_NOT_EXISTS} $table (
  126. `id` {AUTOINCREMENT} {PRIMARY},
  127. `name` VARCHAR(20) {LATIN1} NOT NULL DEFAULT '',
  128. `value` VARCHAR(20) {LATIN1} NOT NULL DEFAULT '',
  129. UNIQUE name ( `name` )
  130. )
  131. ";
  132. db_query_parsed($mysql, 0, " COMMENT = 'PostfixAdmin settings'");
  133. }
  134. $version = check_db_version(false);
  135. _do_upgrade($version);
  136. function _do_upgrade($current_version) {
  137. global $CONF;
  138. $target_version = 0;
  139. // Rather than being bound to an svn revision number, just look for the largest function name that matches upgrade_\d+...
  140. // $target_version = preg_replace('/[^0-9]/', '', '$Revision$');
  141. $funclist = get_defined_functions();
  142. $our_upgrade_functions = array_filter($funclist['user'], '_upgrade_filter_function');
  143. foreach ($our_upgrade_functions as $function_name) {
  144. $bits = explode("_", $function_name);
  145. $function_number = $bits[1];
  146. if ($function_number > $current_version && $function_number > $target_version) {
  147. $target_version = $function_number;
  148. }
  149. }
  150. if ($current_version >= $target_version) {
  151. # already up to date
  152. echo "<p>Database is up to date: $current_version/$target_version </p>";
  153. return true;
  154. }
  155. echo "<p>Updating database:</p><p>- old version: $current_version; target version: $target_version</p>\n";
  156. echo "<div style='color:#999'>&nbsp;&nbsp;(If the update doesn't work, run setup.php?debug=1 to see the detailed error messages and SQL queries.)</div>";
  157. if (db_sqlite() && $current_version < 1824) {
  158. // Fast forward to the first revision supporting SQLite
  159. $current_version = 1823;
  160. }
  161. for ($i = $current_version +1; $i <= $target_version; $i++) {
  162. $function = "upgrade_$i";
  163. $function_mysql_pgsql = $function . "_mysql_pgsql";
  164. $function_mysql = $function . "_mysql";
  165. $function_pgsql = $function . "_pgsql";
  166. $function_sqlite = $function . "_sqlite";
  167. if (function_exists($function)) {
  168. echo "<p>updating to version $i (all databases)...";
  169. $function();
  170. echo " &nbsp; done";
  171. }
  172. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli' || $CONF['database_type'] == 'pgsql') {
  173. if (function_exists($function_mysql_pgsql)) {
  174. echo "<p>updating to version $i (MySQL and PgSQL)...";
  175. $function_mysql_pgsql();
  176. echo " &nbsp; done";
  177. }
  178. }
  179. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli') {
  180. if (function_exists($function_mysql)) {
  181. echo "<p>updating to version $i (MySQL)...";
  182. $function_mysql();
  183. echo " &nbsp; done";
  184. }
  185. } elseif (db_sqlite()) {
  186. if (function_exists($function_sqlite)) {
  187. echo "<p>updating to version $i (SQLite)...";
  188. $function_sqlite();
  189. echo " &nbsp; done";
  190. }
  191. } elseif ($CONF['database_type'] == 'pgsql') {
  192. if (function_exists($function_pgsql)) {
  193. echo "<p>updating to version $i (PgSQL)...";
  194. $function_pgsql();
  195. echo " &nbsp; done";
  196. }
  197. }
  198. // Update config table so we don't run the same query twice in the future.
  199. $i = (int) $i;
  200. $table = table_by_key('config');
  201. $sql = "UPDATE $table SET value = $i WHERE name = 'version'";
  202. db_query($sql);
  203. };
  204. }
  205. /**
  206. * Replaces database specific parts in a query
  207. * @param String sql query with placeholders
  208. * @param int (optional) whether errors should be ignored (0=false)
  209. * @param String (optional) MySQL specific code to attach, useful for COMMENT= on CREATE TABLE
  210. * @return String sql query
  211. */
  212. function db_query_parsed($sql, $ignore_errors = 0, $attach_mysql = "") {
  213. global $CONF;
  214. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli') {
  215. $replace = array(
  216. '{AUTOINCREMENT}' => 'int(11) not null auto_increment',
  217. '{PRIMARY}' => 'primary key',
  218. '{UNSIGNED}' => 'unsigned' ,
  219. '{FULLTEXT}' => 'FULLTEXT',
  220. '{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
  221. '{UTF-8}' => '/*!40100 CHARACTER SET utf8 */',
  222. '{LATIN1}' => '/*!40100 CHARACTER SET latin1 */',
  223. '{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
  224. '{RENAME_COLUMN}' => 'CHANGE COLUMN',
  225. '{MYISAM}' => '',
  226. '{INNODB}' => 'ENGINE=InnoDB',
  227. '{INT}' => 'integer NOT NULL DEFAULT 0',
  228. '{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
  229. '{DATETIME}' => "datetime NOT NULL default '2000-01-01 00:00:00'", # different from {DATE} only for MySQL
  230. '{DATE}' => "timestamp NOT NULL default '2000-01-01'", # MySQL needs a sane default (no default is interpreted as CURRENT_TIMESTAMP, which is ...
  231. '{DATEFUTURE}' => "timestamp NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
  232. '{DATECURRENT}' => 'timestamp NOT NULL default CURRENT_TIMESTAMP', # only allowed once per table in MySQL
  233. );
  234. $sql = "$sql $attach_mysql";
  235. } elseif (db_sqlite()) {
  236. $replace = array(
  237. '{AUTOINCREMENT}' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
  238. '{PRIMARY}' => 'PRIMARY KEY',
  239. '{UNSIGNED}' => 'unsigned',
  240. '{FULLTEXT}' => 'text',
  241. '{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
  242. '{BOOLEAN_TRUE}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
  243. '{UTF-8}' => '',
  244. '{LATIN1}' => '',
  245. '{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
  246. '{RENAME_COLUMN}' => 'CHANGE COLUMN',
  247. '{MYISAM}' => '',
  248. '{INNODB}' => '',
  249. '{INT}' => 'int(11) NOT NULL DEFAULT 0',
  250. '{BIGINT}' => 'bigint(20) NOT NULL DEFAULT 0',
  251. '{DATETIME}' => "datetime NOT NULL default '2000-01-01'",
  252. '{DATE}' => "datetime NOT NULL default '2000-01-01'",
  253. '{DATEFUTURE}' => "datetime NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
  254. '{DATECURRENT}' => 'datetime NOT NULL default CURRENT_TIMESTAMP',
  255. );
  256. } elseif ($CONF['database_type'] == 'pgsql') {
  257. $replace = array(
  258. '{AUTOINCREMENT}' => 'SERIAL',
  259. '{PRIMARY}' => 'primary key',
  260. '{UNSIGNED}' => '',
  261. '{FULLTEXT}' => '',
  262. '{BOOLEAN}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
  263. '{UTF-8}' => '', # UTF-8 is simply ignored.
  264. '{LATIN1}' => '', # same for latin1
  265. '{IF_NOT_EXISTS}' => '', # does not work with PgSQL
  266. '{RENAME_COLUMN}' => 'ALTER COLUMN', # PgSQL : ALTER TABLE x RENAME x TO y
  267. '{MYISAM}' => '',
  268. '{INNODB}' => '',
  269. '{INT}' => 'integer NOT NULL DEFAULT 0',
  270. '{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
  271. 'int(1)' => 'int',
  272. 'int(10)' => 'int',
  273. 'int(11)' => 'int',
  274. 'int(4)' => 'int',
  275. '{DATETIME}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
  276. '{DATE}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
  277. '{DATEFUTURE}' => "timestamp with time zone default '2038-01-18'", # stay in sync with MySQL
  278. '{DATECURRENT}' => 'timestamp with time zone default now()',
  279. );
  280. } else {
  281. echo "Sorry, unsupported database type " . $conf['database_type'];
  282. exit;
  283. }
  284. $replace['{BOOL_TRUE}'] = db_get_boolean(true);
  285. $replace['{BOOL_FALSE}'] = db_get_boolean(false);
  286. $query = trim(str_replace(array_keys($replace), $replace, $sql));
  287. if (safeget('debug') != "") {
  288. printdebug($query);
  289. }
  290. $result = db_query($query, $ignore_errors);
  291. if (safeget('debug') != "") {
  292. print "<div style='color:#f00'>" . $result['error'] . "</div>";
  293. }
  294. return $result;
  295. }
  296. function _drop_index($table, $index) {
  297. global $CONF;
  298. $table = table_by_key($table);
  299. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli') {
  300. return "ALTER TABLE $table DROP INDEX $index";
  301. } elseif ($CONF['database_type'] == 'pgsql' || db_sqlite()) {
  302. return "DROP INDEX $index"; # Index names are unique with a DB for PostgreSQL
  303. } else {
  304. echo "Sorry, unsupported database type " . $conf['database_type'];
  305. exit;
  306. }
  307. }
  308. function _add_index($table, $indexname, $fieldlist) {
  309. global $CONF;
  310. $table = table_by_key($table);
  311. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli') {
  312. $fieldlist = str_replace(',', '`,`', $fieldlist); # fix quoting if index contains multiple fields
  313. return "ALTER TABLE $table ADD INDEX `$indexname` ( `$fieldlist` )";
  314. } elseif ($CONF['database_type'] == 'pgsql') {
  315. $pgindexname = $table . "_" . $indexname . '_idx';
  316. return "CREATE INDEX $pgindexname ON $table($fieldlist);"; # Index names are unique with a DB for PostgreSQL
  317. } else {
  318. echo "Sorry, unsupported database type " . $conf['database_type'];
  319. exit;
  320. }
  321. }
  322. function upgrade_1_mysql() {
  323. #
  324. # creating the tables in this very old layout (pre 2.1) causes trouble if the MySQL charset is not latin1 (multibyte vs. index length)
  325. # therefore:
  326. return; # <-- skip running this function at all.
  327. # (remove the above "return" if you really want to start with a pre-2.1 database layout)
  328. // CREATE MYSQL DATABASE TABLES.
  329. $admin = table_by_key('admin');
  330. $alias = table_by_key('alias');
  331. $domain = table_by_key('domain');
  332. $domain_admins = table_by_key('domain_admins');
  333. $log = table_by_key('log');
  334. $mailbox = table_by_key('mailbox');
  335. $vacation = table_by_key('vacation');
  336. $sql = array();
  337. $sql[] = "
  338. CREATE TABLE {IF_NOT_EXISTS} $admin (
  339. `username` varchar(255) NOT NULL default '',
  340. `password` varchar(255) NOT NULL default '',
  341. `created` {DATETIME},
  342. `modified` {DATETIME},
  343. `active` tinyint(1) NOT NULL default '1',
  344. PRIMARY KEY (`username`)
  345. ) COMMENT='Postfix Admin - Virtual Admins';";
  346. $sql[] = "
  347. CREATE TABLE {IF_NOT_EXISTS} $alias (
  348. `address` varchar(255) NOT NULL default '',
  349. `goto` text NOT NULL,
  350. `domain` varchar(255) NOT NULL default '',
  351. `created` {DATETIME},
  352. `modified` {DATETIME},
  353. `active` tinyint(1) NOT NULL default '1',
  354. PRIMARY KEY (`address`)
  355. ) COMMENT='Postfix Admin - Virtual Aliases'; ";
  356. $sql[] = "
  357. CREATE TABLE {IF_NOT_EXISTS} $domain (
  358. `domain` varchar(255) NOT NULL default '',
  359. `description` varchar(255) NOT NULL default '',
  360. `aliases` int(10) NOT NULL default '0',
  361. `mailboxes` int(10) NOT NULL default '0',
  362. `maxquota` bigint(20) NOT NULL default '0',
  363. `quota` bigint(20) NOT NULL default '0',
  364. `transport` varchar(255) default NULL,
  365. `backupmx` tinyint(1) NOT NULL default '0',
  366. `created` {DATETIME},
  367. `modified` {DATETIME},
  368. `active` tinyint(1) NOT NULL default '1',
  369. PRIMARY KEY (`domain`)
  370. ) COMMENT='Postfix Admin - Virtual Domains'; ";
  371. $sql[] = "
  372. CREATE TABLE {IF_NOT_EXISTS} $domain_admins (
  373. `username` varchar(255) NOT NULL default '',
  374. `domain` varchar(255) NOT NULL default '',
  375. `created` {DATETIME},
  376. `active` tinyint(1) NOT NULL default '1',
  377. KEY username (`username`)
  378. ) COMMENT='Postfix Admin - Domain Admins';";
  379. $sql[] = "
  380. CREATE TABLE {IF_NOT_EXISTS} $log (
  381. `timestamp` {DATETIME},
  382. `username` varchar(255) NOT NULL default '',
  383. `domain` varchar(255) NOT NULL default '',
  384. `action` varchar(255) NOT NULL default '',
  385. `data` varchar(255) NOT NULL default '',
  386. KEY timestamp (`timestamp`)
  387. ) COMMENT='Postfix Admin - Log';";
  388. $sql[] = "
  389. CREATE TABLE {IF_NOT_EXISTS} $mailbox (
  390. `username` varchar(255) NOT NULL default '',
  391. `password` varchar(255) NOT NULL default '',
  392. `name` varchar(255) NOT NULL default '',
  393. `maildir` varchar(255) NOT NULL default '',
  394. `quota` bigint(20) NOT NULL default '0',
  395. `domain` varchar(255) NOT NULL default '',
  396. `created` {DATETIME},
  397. `modified` {DATETIME},
  398. `active` tinyint(1) NOT NULL default '1',
  399. PRIMARY KEY (`username`)
  400. ) COMMENT='Postfix Admin - Virtual Mailboxes';";
  401. $sql[] = "
  402. CREATE TABLE {IF_NOT_EXISTS} $vacation (
  403. email varchar(255) NOT NULL ,
  404. subject varchar(255) NOT NULL,
  405. body text NOT NULL,
  406. cache text NOT NULL,
  407. domain varchar(255) NOT NULL ,
  408. created {DATETIME},
  409. active tinyint(4) NOT NULL default '1',
  410. PRIMARY KEY (email),
  411. KEY email (email)
  412. ) {INNODB} DEFAULT CHARSET=latin1 COMMENT='Postfix Admin - Virtual Vacation' ;";
  413. foreach ($sql as $query) {
  414. db_query_parsed($query);
  415. }
  416. }
  417. function upgrade_2_mysql() {
  418. #
  419. # updating the tables in this very old layout (pre 2.1) causes trouble if the MySQL charset is not latin1 (multibyte vs. index length)
  420. # therefore:
  421. return; # <-- skip running this function at all.
  422. # (remove the above "return" if you really want to update a pre-2.1 database)
  423. # upgrade pre-2.1 database
  424. # from TABLE_BACKUP_MX.TXT
  425. $table_domain = table_by_key('domain');
  426. if (!_mysql_field_exists($table_domain, 'transport')) {
  427. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255) AFTER maxquota;", true);
  428. }
  429. if (!_mysql_field_exists($table_domain, 'backupmx')) {
  430. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx {BOOLEAN} AFTER transport;", true);
  431. }
  432. }
  433. function upgrade_2_pgsql() {
  434. if (!_pgsql_object_exists(table_by_key('domain'))) {
  435. db_query_parsed("
  436. CREATE TABLE " . table_by_key('domain') . " (
  437. domain character varying(255) NOT NULL,
  438. description character varying(255) NOT NULL default '',
  439. aliases integer NOT NULL default 0,
  440. mailboxes integer NOT NULL default 0,
  441. maxquota integer NOT NULL default 0,
  442. quota integer NOT NULL default 0,
  443. transport character varying(255) default NULL,
  444. backupmx boolean NOT NULL default false,
  445. created timestamp with time zone default now(),
  446. modified timestamp with time zone default now(),
  447. active boolean NOT NULL default true,
  448. Constraint \"domain_key\" Primary Key (\"domain\")
  449. );
  450. CREATE INDEX domain_domain_active ON " . table_by_key('domain') . "(domain,active);
  451. COMMENT ON TABLE " . table_by_key('domain') . " IS 'Postfix Admin - Virtual Domains';
  452. ");
  453. }
  454. if (!_pgsql_object_exists(table_by_key('admin'))) {
  455. db_query_parsed('
  456. CREATE TABLE ' . table_by_key("admin") . ' (
  457. "username" character varying(255) NOT NULL,
  458. "password" character varying(255) NOT NULL default \'\',
  459. "created" timestamp with time zone default now(),
  460. "modified" timestamp with time zone default now(),
  461. "active" boolean NOT NULL default true,
  462. Constraint "admin_key" Primary Key ("username")
  463. );' . "
  464. COMMENT ON TABLE " . table_by_key('admin') . " IS 'Postfix Admin - Virtual Admins';
  465. ");
  466. }
  467. if (!_pgsql_object_exists(table_by_key('alias'))) {
  468. db_query_parsed('
  469. CREATE TABLE ' . table_by_key("alias") . ' (
  470. address character varying(255) NOT NULL,
  471. goto text NOT NULL,
  472. domain character varying(255) NOT NULL REFERENCES "' . table_by_key("domain") . '",
  473. created timestamp with time zone default now(),
  474. modified timestamp with time zone default now(),
  475. active boolean NOT NULL default true,
  476. Constraint "alias_key" Primary Key ("address")
  477. );
  478. CREATE INDEX alias_address_active ON ' . table_by_key("alias") . '(address,active);
  479. COMMENT ON TABLE ' . table_by_key("alias") . ' IS \'Postfix Admin - Virtual Aliases\';
  480. ');
  481. }
  482. if (!_pgsql_object_exists(table_by_key('domain_admins'))) {
  483. db_query_parsed('
  484. CREATE TABLE ' . table_by_key('domain_admins') . ' (
  485. username character varying(255) NOT NULL,
  486. domain character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  487. created timestamp with time zone default now(),
  488. active boolean NOT NULL default true
  489. );
  490. COMMENT ON TABLE ' . table_by_key('domain_admins') . ' IS \'Postfix Admin - Domain Admins\';
  491. ');
  492. }
  493. if (!_pgsql_object_exists(table_by_key('log'))) {
  494. db_query_parsed('
  495. CREATE TABLE ' . table_by_key('log') . ' (
  496. timestamp timestamp with time zone default now(),
  497. username character varying(255) NOT NULL default \'\',
  498. domain character varying(255) NOT NULL default \'\',
  499. action character varying(255) NOT NULL default \'\',
  500. data text NOT NULL default \'\'
  501. );
  502. COMMENT ON TABLE ' . table_by_key('log') . ' IS \'Postfix Admin - Log\';
  503. ');
  504. }
  505. if (!_pgsql_object_exists(table_by_key('mailbox'))) {
  506. db_query_parsed('
  507. CREATE TABLE ' . table_by_key('mailbox') . ' (
  508. username character varying(255) NOT NULL,
  509. password character varying(255) NOT NULL default \'\',
  510. name character varying(255) NOT NULL default \'\',
  511. maildir character varying(255) NOT NULL default \'\',
  512. quota integer NOT NULL default 0,
  513. domain character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  514. created timestamp with time zone default now(),
  515. modified timestamp with time zone default now(),
  516. active boolean NOT NULL default true,
  517. Constraint "mailbox_key" Primary Key ("username")
  518. );
  519. CREATE INDEX mailbox_username_active ON ' . table_by_key('mailbox') . '(username,active);
  520. COMMENT ON TABLE ' . table_by_key('mailbox') . ' IS \'Postfix Admin - Virtual Mailboxes\';
  521. ');
  522. }
  523. if (!_pgsql_object_exists(table_by_key('vacation'))) {
  524. db_query_parsed('
  525. CREATE TABLE ' . table_by_key('vacation') . ' (
  526. email character varying(255) PRIMARY KEY,
  527. subject character varying(255) NOT NULL,
  528. body text NOT NULL ,
  529. cache text NOT NULL ,
  530. "domain" character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  531. created timestamp with time zone DEFAULT now(),
  532. active boolean DEFAULT true NOT NULL
  533. );
  534. CREATE INDEX vacation_email_active ON ' . table_by_key('vacation') . '(email,active);');
  535. }
  536. if (!_pgsql_object_exists(table_by_key('vacation_notification'))) {
  537. db_query_parsed('
  538. CREATE TABLE ' . table_by_key('vacation_notification') . ' (
  539. on_vacation character varying(255) NOT NULL REFERENCES ' . table_by_key('vacation') . '(email) ON DELETE CASCADE,
  540. notified character varying(255) NOT NULL,
  541. notified_at timestamp with time zone NOT NULL DEFAULT now(),
  542. CONSTRAINT vacation_notification_pkey primary key(on_vacation,notified)
  543. );
  544. ');
  545. }
  546. // this handles anyone who is upgrading... (and should have no impact on new installees)
  547. $table_domain = table_by_key('domain');
  548. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255)", true);
  549. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx BOOLEAN DEFAULT false", true);
  550. }
  551. function upgrade_3_mysql() {
  552. #
  553. # updating the tables in this very old layout (pre 2.1) causes trouble if the MySQL charset is not latin1 (multibyte vs. index length)
  554. # therefore:
  555. return; # <-- skip running this function at all.
  556. # (remove the above "return" if you really want to update a pre-2.1 database)
  557. # upgrade pre-2.1 database
  558. # from TABLE_CHANGES.TXT
  559. $table_admin = table_by_key('admin');
  560. $table_alias = table_by_key('alias');
  561. $table_domain = table_by_key('domain');
  562. $table_mailbox = table_by_key('mailbox');
  563. $table_vacation = table_by_key('vacation');
  564. if (!_mysql_field_exists($table_admin, 'created')) {
  565. db_query_parsed("ALTER TABLE $table_admin {RENAME_COLUMN} create_date created {DATETIME};");
  566. }
  567. if (!_mysql_field_exists($table_admin, 'modified')) {
  568. db_query_parsed("ALTER TABLE $table_admin {RENAME_COLUMN} change_date modified {DATETIME};");
  569. }
  570. if (!_mysql_field_exists($table_alias, 'created')) {
  571. db_query_parsed("ALTER TABLE $table_alias {RENAME_COLUMN} create_date created {DATETIME};");
  572. }
  573. if (!_mysql_field_exists($table_alias, 'modified')) {
  574. db_query_parsed("ALTER TABLE $table_alias {RENAME_COLUMN} change_date modified {DATETIME};");
  575. }
  576. if (!_mysql_field_exists($table_domain, 'created')) {
  577. db_query_parsed("ALTER TABLE $table_domain {RENAME_COLUMN} create_date created {DATETIME};");
  578. }
  579. if (!_mysql_field_exists($table_domain, 'modified')) {
  580. db_query_parsed("ALTER TABLE $table_domain {RENAME_COLUMN} change_date modified {DATETIME};");
  581. }
  582. if (!_mysql_field_exists($table_domain, 'aliases')) {
  583. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN aliases INT(10) DEFAULT '-1' NOT NULL AFTER description;");
  584. }
  585. if (!_mysql_field_exists($table_domain, 'mailboxes')) {
  586. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN mailboxes INT(10) DEFAULT '-1' NOT NULL AFTER aliases;");
  587. }
  588. if (!_mysql_field_exists($table_domain, 'maxquota')) {
  589. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN maxquota INT(10) DEFAULT '-1' NOT NULL AFTER mailboxes;");
  590. }
  591. if (!_mysql_field_exists($table_domain, 'transport')) {
  592. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255) AFTER maxquota;");
  593. }
  594. if (!_mysql_field_exists($table_domain, 'backupmx')) {
  595. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx TINYINT(1) DEFAULT '0' NOT NULL AFTER transport;");
  596. }
  597. if (!_mysql_field_exists($table_mailbox, 'created')) {
  598. db_query_parsed("ALTER TABLE $table_mailbox {RENAME_COLUMN} create_date created {DATETIME};");
  599. }
  600. if (!_mysql_field_exists($table_mailbox, 'modified')) {
  601. db_query_parsed("ALTER TABLE $table_mailbox {RENAME_COLUMN} change_date modified {DATETIME};");
  602. }
  603. if (!_mysql_field_exists($table_mailbox, 'quota')) {
  604. db_query_parsed("ALTER TABLE $table_mailbox ADD COLUMN quota INT(10) DEFAULT '-1' NOT NULL AFTER maildir;");
  605. }
  606. if (!_mysql_field_exists($table_vacation, 'domain')) {
  607. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN domain VARCHAR(255) DEFAULT '' NOT NULL AFTER cache;");
  608. }
  609. if (!_mysql_field_exists($table_vacation, 'created')) {
  610. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN created {DATETIME} AFTER domain;");
  611. }
  612. if (!_mysql_field_exists($table_vacation, 'active')) {
  613. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN active TINYINT(1) DEFAULT '1' NOT NULL AFTER created;");
  614. }
  615. db_query_parsed("ALTER TABLE $table_vacation DROP PRIMARY KEY");
  616. db_query_parsed("ALTER TABLE $table_vacation ADD PRIMARY KEY(email)");
  617. db_query_parsed("UPDATE $table_vacation SET domain=SUBSTRING_INDEX(email, '@', -1) WHERE email=email;");
  618. }
  619. function upgrade_4_mysql() { # MySQL only
  620. # changes between 2.1 and moving to sourceforge
  621. $table_domain = table_by_key('domain');
  622. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN quota int(10) NOT NULL default '0' AFTER maxquota", true);
  623. # Possible errors that can be ignored:
  624. # - Invalid query: Table 'postfix.domain' doesn't exist
  625. }
  626. /**
  627. * Changes between 2.1 and moving to sf.net
  628. */
  629. function upgrade_4_pgsql() {
  630. $table_domain = table_by_key('domain');
  631. $table_admin = table_by_key('admin');
  632. $table_alias = table_by_key('alias');
  633. $table_domain_admins = table_by_key('domain_admins');
  634. $table_log = table_by_key('log');
  635. $table_mailbox = table_by_key('mailbox');
  636. $table_vacation = table_by_key('vacation');
  637. $table_vacation_notification = table_by_key('vacation_notification');
  638. if (!_pgsql_field_exists($table_domain, 'quota')) {
  639. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN quota int NOT NULL default '0'");
  640. }
  641. $result = db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN domain DROP DEFAULT");
  642. if (!_pgsql_object_exists('domain_domain_active')) {
  643. $result = db_query_parsed("CREATE INDEX domain_domain_active ON $table_domain(domain,active)");
  644. }
  645. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN domain DROP DEFAULT");
  646. $result = db_query_parsed("ALTER TABLE $table_alias ALTER COLUMN address DROP DEFAULT");
  647. $result = db_query_parsed("ALTER TABLE $table_alias ALTER COLUMN domain DROP DEFAULT");
  648. if (!_pgsql_object_exists('alias_address_active')) {
  649. $result = db_query_parsed("CREATE INDEX alias_address_active ON $table_alias(address,active)");
  650. }
  651. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN username DROP DEFAULT");
  652. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN domain DROP DEFAULT");
  653. $result = db_query_parsed("
  654. BEGIN;
  655. ALTER TABLE $table_log RENAME COLUMN data TO data_old;
  656. ALTER TABLE $table_log ADD COLUMN data text NOT NULL default '';
  657. UPDATE $table_log SET data = CAST(data_old AS text);
  658. ALTER TABLE $table_log DROP COLUMN data_old;
  659. COMMIT;");
  660. $result = db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN username DROP DEFAULT");
  661. $result = db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN domain DROP DEFAULT");
  662. $result = db_query_parsed(
  663. "
  664. BEGIN;
  665. ALTER TABLE $table_mailbox RENAME COLUMN domain TO domain_old;
  666. ALTER TABLE $table_mailbox ADD COLUMN domain varchar(255) REFERENCES $table_domain (domain);
  667. UPDATE $table_mailbox SET domain = domain_old;
  668. ALTER TABLE $table_mailbox DROP COLUMN domain_old;
  669. COMMIT;"
  670. );
  671. if (!_pgsql_object_exists('mailbox_username_active')) {
  672. db_query_parsed("CREATE INDEX mailbox_username_active ON $table_mailbox(username,active)");
  673. }
  674. $result = db_query_parsed("ALTER TABLE $table_vacation ALTER COLUMN body SET DEFAULT ''");
  675. if (_pgsql_field_exists($table_vacation, 'cache')) {
  676. $result = db_query_parsed("ALTER TABLE $table_vacation DROP COLUMN cache");
  677. }
  678. $result = db_query_parsed("
  679. BEGIN;
  680. ALTER TABLE $table_vacation RENAME COLUMN domain to domain_old;
  681. ALTER TABLE $table_vacation ADD COLUMN domain varchar(255) REFERENCES $table_domain;
  682. UPDATE $table_vacation SET domain = domain_old;
  683. ALTER TABLE $table_vacation DROP COLUMN domain_old;
  684. COMMIT;
  685. ");
  686. if (!_pgsql_object_exists('vacation_email_active')) {
  687. $result = db_query_parsed("CREATE INDEX vacation_email_active ON $table_vacation(email,active)");
  688. }
  689. if (!_pgsql_object_exists($table_vacation_notification)) {
  690. $result = db_query_parsed("
  691. CREATE TABLE $table_vacation_notification (
  692. on_vacation character varying(255) NOT NULL REFERENCES $table_vacation(email) ON DELETE CASCADE,
  693. notified character varying(255) NOT NULL,
  694. notified_at timestamp with time zone NOT NULL DEFAULT now(),
  695. CONSTRAINT vacation_notification_pkey primary key(on_vacation,notified));");
  696. }
  697. }
  698. # Possible errors that can be ignored:
  699. #
  700. # NO MySQL errors should be ignored below this line!
  701. /**
  702. * create tables
  703. * version: Sourceforge SVN r1 of DATABASE_MYSQL.txt
  704. * changes compared to DATABASE_MYSQL.txt:
  705. * - removed MySQL user and database creation
  706. * - removed creation of default superadmin
  707. */
  708. function upgrade_5_mysql() {
  709. $result = db_query_parsed("
  710. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('admin') . " (
  711. `username` varchar(255) NOT NULL default '',
  712. `password` varchar(255) NOT NULL default '',
  713. `created` {DATETIME},
  714. `modified` {DATETIME},
  715. `active` tinyint(1) NOT NULL default '1',
  716. PRIMARY KEY (`username`),
  717. KEY username (`username`)
  718. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Admins'; ");
  719. $result = db_query_parsed("
  720. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('alias') . " (
  721. `address` varchar(255) NOT NULL default '',
  722. `goto` text NOT NULL,
  723. `domain` varchar(255) NOT NULL default '',
  724. `created` {DATETIME},
  725. `modified` {DATETIME},
  726. `active` tinyint(1) NOT NULL default '1',
  727. PRIMARY KEY (`address`),
  728. KEY address (`address`)
  729. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Aliases';
  730. ");
  731. $result = db_query_parsed("
  732. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('domain') . " (
  733. `domain` varchar(255) NOT NULL default '',
  734. `description` varchar(255) NOT NULL default '',
  735. `aliases` int(10) NOT NULL default '0',
  736. `mailboxes` int(10) NOT NULL default '0',
  737. `maxquota` int(10) NOT NULL default '0',
  738. `quota` int(10) NOT NULL default '0',
  739. `transport` varchar(255) default NULL,
  740. `backupmx` tinyint(1) NOT NULL default '0',
  741. `created` {DATETIME},
  742. `modified` {DATETIME},
  743. `active` tinyint(1) NOT NULL default '1',
  744. PRIMARY KEY (`domain`),
  745. KEY domain (`domain`)
  746. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Domains';
  747. ");
  748. $result = db_query_parsed("
  749. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('domain_admins') . " (
  750. `username` varchar(255) NOT NULL default '',
  751. `domain` varchar(255) NOT NULL default '',
  752. `created` {DATETIME},
  753. `active` tinyint(1) NOT NULL default '1',
  754. KEY username (`username`)
  755. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Domain Admins';
  756. ");
  757. $result = db_query_parsed("
  758. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('log') . " (
  759. `timestamp` {DATETIME},
  760. `username` varchar(255) NOT NULL default '',
  761. `domain` varchar(255) NOT NULL default '',
  762. `action` varchar(255) NOT NULL default '',
  763. `data` varchar(255) NOT NULL default '',
  764. KEY timestamp (`timestamp`)
  765. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Log';
  766. ");
  767. $result = db_query_parsed("
  768. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('mailbox') . " (
  769. `username` varchar(255) NOT NULL default '',
  770. `password` varchar(255) NOT NULL default '',
  771. `name` varchar(255) NOT NULL default '',
  772. `maildir` varchar(255) NOT NULL default '',
  773. `quota` int(10) NOT NULL default '0',
  774. `domain` varchar(255) NOT NULL default '',
  775. `created` {DATETIME},
  776. `modified` {DATETIME},
  777. `active` tinyint(1) NOT NULL default '1',
  778. PRIMARY KEY (`username`),
  779. KEY username (`username`)
  780. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Mailboxes';
  781. ");
  782. $result = db_query_parsed("
  783. CREATE TABLE {IF_NOT_EXISTS} " . table_by_key('vacation') . " (
  784. `email` varchar(255) NOT NULL ,
  785. `subject` varchar(255) NOT NULL,
  786. `body` text NOT NULL,
  787. `cache` text NOT NULL,
  788. `domain` varchar(255) NOT NULL,
  789. `created` {DATETIME},
  790. `active` tinyint(1) NOT NULL default '1',
  791. PRIMARY KEY (`email`),
  792. KEY email (`email`)
  793. ) DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Vacation';
  794. ");
  795. }
  796. /**
  797. * drop useless indicies (already available as primary key)
  798. */
  799. function upgrade_79_mysql() { # MySQL only
  800. $result = db_query_parsed(_drop_index('admin', 'username'), true);
  801. $result = db_query_parsed(_drop_index('alias', 'address'), true);
  802. $result = db_query_parsed(_drop_index('domain', 'domain'), true);
  803. $result = db_query_parsed(_drop_index('mailbox', 'username'), true);
  804. }
  805. function upgrade_81_mysql() { # MySQL only
  806. $table_vacation = table_by_key('vacation');
  807. $table_vacation_notification = table_by_key('vacation_notification');
  808. $all_sql = explode("\n", trim("
  809. ALTER TABLE `$table_vacation` CHANGE `email` `email` VARCHAR( 255 ) {LATIN1} NOT NULL
  810. ALTER TABLE `$table_vacation` CHANGE `subject` `subject` VARCHAR( 255 ) {UTF-8} NOT NULL
  811. ALTER TABLE `$table_vacation` CHANGE `body` `body` TEXT {UTF-8} NOT NULL
  812. ALTER TABLE `$table_vacation` CHANGE `cache` `cache` TEXT {LATIN1} NOT NULL
  813. ALTER TABLE `$table_vacation` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  814. ALTER TABLE `$table_vacation` CHANGE `active` `active` TINYINT( 1 ) NOT NULL DEFAULT '1'
  815. ALTER TABLE `$table_vacation` DEFAULT {LATIN1}
  816. ALTER TABLE `$table_vacation` {INNODB}
  817. "));
  818. foreach ($all_sql as $sql) {
  819. $result = db_query_parsed($sql, true);
  820. }
  821. }
  822. /**
  823. * Make logging translatable - i.e. create alias => create_alias
  824. */
  825. function upgrade_90_mysql_pgsql() {
  826. $result = db_query_parsed("UPDATE " . table_by_key('log') . " SET action = REPLACE(action,' ','_')", true);
  827. # change edit_alias_state to edit_alias_active
  828. $result = db_query_parsed("UPDATE " . table_by_key('log') . " SET action = 'edit_alias_state' WHERE action = 'edit_alias_active'", true);
  829. }
  830. /**
  831. * MySQL only allow quota > 2 GB
  832. */
  833. function upgrade_169_mysql() {
  834. $table_domain = table_by_key('domain');
  835. $table_mailbox = table_by_key('mailbox');
  836. $result = db_query_parsed("ALTER TABLE $table_domain MODIFY COLUMN `quota` bigint(20) NOT NULL default '0'", true);
  837. $result = db_query_parsed("ALTER TABLE $table_domain MODIFY COLUMN `maxquota` bigint(20) NOT NULL default '0'", true);
  838. $result = db_query_parsed("ALTER TABLE $table_mailbox MODIFY COLUMN `quota` bigint(20) NOT NULL default '0'", true);
  839. }
  840. /**
  841. * Create / modify vacation_notification table.
  842. * Note: This might not work if users used workarounds to create the table before.
  843. * In this case, dropping the table is the easiest solution.
  844. */
  845. function upgrade_318_mysql() {
  846. $table_vacation_notification = table_by_key('vacation_notification');
  847. $table_vacation = table_by_key('vacation');
  848. db_query_parsed("
  849. CREATE TABLE {IF_NOT_EXISTS} $table_vacation_notification (
  850. on_vacation varchar(255) NOT NULL,
  851. notified varchar(255) NOT NULL,
  852. notified_at timestamp NOT NULL default CURRENT_TIMESTAMP,
  853. PRIMARY KEY on_vacation (`on_vacation`, `notified`),
  854. CONSTRAINT `vacation_notification_pkey`
  855. FOREIGN KEY (`on_vacation`) REFERENCES $table_vacation(`email`) ON DELETE CASCADE
  856. )
  857. DEFAULT {LATIN1}
  858. COMMENT='Postfix Admin - Virtual Vacation Notifications'
  859. ");
  860. # in case someone has manually created the table with utf8 fields before:
  861. $all_sql = explode("\n", trim("
  862. ALTER TABLE $table_vacation_notification CHANGE `notified` `notified` VARCHAR( 255 ) {LATIN1} NOT NULL
  863. ALTER TABLE $table_vacation_notification DEFAULT CHARACTER SET utf8
  864. "));
  865. # Possible errors that can be ignored:
  866. # None.
  867. # If something goes wrong, the user should drop the vacation_notification table
  868. # (not a great loss) and re-create it using this function.
  869. foreach ($all_sql as $sql) {
  870. $result = db_query_parsed($sql);
  871. }
  872. }
  873. /**
  874. * Create fetchmail table
  875. */
  876. function upgrade_344_mysql() {
  877. $table_fetchmail = table_by_key('fetchmail');
  878. db_query_parsed("
  879. CREATE TABLE IF NOT EXISTS $table_fetchmail(
  880. id int(11) unsigned not null auto_increment,
  881. mailbox varchar(255) not null default '',
  882. src_server varchar(255) not null default '',
  883. src_auth enum('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any'),
  884. src_user varchar(255) not null default '',
  885. src_password varchar(255) not null default '',
  886. src_folder varchar(255) not null default '',
  887. poll_time int(11) unsigned not null default 10,
  888. fetchall tinyint(1) unsigned not null default 0,
  889. keep tinyint(1) unsigned not null default 0,
  890. protocol enum('POP3','IMAP','POP2','ETRN','AUTO'),
  891. extra_options text,
  892. returned_text text,
  893. mda varchar(255) not null default '',
  894. date timestamp,
  895. primary key(id)
  896. );
  897. ");
  898. }
  899. function upgrade_344_pgsql() {
  900. $fetchmail = table_by_key('fetchmail');
  901. // a field name called 'date' is probably a bad idea.
  902. if (!_pgsql_object_exists('fetchmail')) {
  903. db_query_parsed("
  904. create table $fetchmail(
  905. id serial,
  906. mailbox varchar(255) not null default '',
  907. src_server varchar(255) not null default '',
  908. src_auth varchar(15) NOT NULL,
  909. src_user varchar(255) not null default '',
  910. src_password varchar(255) not null default '',
  911. src_folder varchar(255) not null default '',
  912. poll_time integer not null default 10,
  913. fetchall boolean not null default false,
  914. keep boolean not null default false,
  915. protocol varchar(15) NOT NULL,
  916. extra_options text,
  917. returned_text text,
  918. mda varchar(255) not null default '',
  919. date timestamp with time zone default now(),
  920. primary key(id),
  921. CHECK (src_auth IN ('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any')),
  922. CHECK (protocol IN ('POP3', 'IMAP', 'POP2', 'ETRN', 'AUTO'))
  923. );
  924. ");
  925. }
  926. // MySQL expects sequences to start at 1. Stupid database.
  927. // fetchmail.php requires id parameters to be > 0, as it does if($id) like logic... hence if we don't
  928. // fudge the sequence starting point, you cannot delete/edit the first entry if using PostgreSQL.
  929. // I'm sure there's a more elegant way of fixing it properly.... but this should work for now.
  930. if (_pgsql_object_exists('fetchmail_id_seq')) {
  931. db_query_parsed("SELECT nextval('{$fetchmail}_id_seq')"); // I don't care about number waste.
  932. }
  933. }
  934. /**
  935. * Create alias_domain table - MySQL
  936. */
  937. # function upgrade_362_mysql() # renamed to _438 to make sure it runs after an upgrade from 2.2.x
  938. function upgrade_438_mysql() {
  939. # Table structure for table alias_domain
  940. #
  941. $table_alias_domain = table_by_key('alias_domain');
  942. db_query_parsed("
  943. CREATE TABLE IF NOT EXISTS $table_alias_domain (
  944. `alias_domain` varchar(255) {LATIN1} NOT NULL default '',
  945. `target_domain` varchar(255) {LATIN1} NOT NULL default '',
  946. `created` {DATETIME},
  947. `modified` {DATETIME},
  948. `active` tinyint(1) NOT NULL default '1',
  949. PRIMARY KEY (`alias_domain`),
  950. KEY `active` (`active`),
  951. KEY `target_domain` (`target_domain`)
  952. ) COMMENT='Postfix Admin - Domain Aliases'
  953. ");
  954. }
  955. /**
  956. * Create alias_domain table - PgSQL
  957. */
  958. # function upgrade_362_pgsql() # renamed to _438 to make sure it runs after an upgrade from 2.2.x
  959. function upgrade_438_pgsql() {
  960. # Table structure for table alias_domain
  961. $table_alias_domain = table_by_key('alias_domain');
  962. $table_domain = table_by_key('domain');
  963. if (_pgsql_object_exists($table_alias_domain)) {
  964. return;
  965. }
  966. db_query_parsed("
  967. CREATE TABLE $table_alias_domain (
  968. alias_domain character varying(255) NOT NULL REFERENCES $table_domain(domain) ON DELETE CASCADE,
  969. target_domain character varying(255) NOT NULL REFERENCES $table_domain(domain) ON DELETE CASCADE,
  970. created timestamp with time zone default now(),
  971. modified timestamp with time zone default now(),
  972. active boolean NOT NULL default true,
  973. PRIMARY KEY(alias_domain))");
  974. db_query_parsed("CREATE INDEX alias_domain_active ON $table_alias_domain(alias_domain,active)");
  975. db_query_parsed("COMMENT ON TABLE $table_alias_domain IS 'Postfix Admin - Domain Aliases'");
  976. }
  977. /**
  978. * Change description fields to UTF-8
  979. */
  980. function upgrade_373_mysql() { # MySQL only
  981. $table_domain = table_by_key('domain');
  982. $table_mailbox = table_by_key('mailbox');
  983. $all_sql = explode("\n", trim("
  984. ALTER TABLE $table_domain CHANGE `description` `description` VARCHAR( 255 ) {UTF-8} NOT NULL
  985. ALTER TABLE $table_mailbox CHANGE `name` `name` VARCHAR( 255 ) {UTF-8} NOT NULL
  986. "));
  987. foreach ($all_sql as $sql) {
  988. $result = db_query_parsed($sql);
  989. }
  990. }
  991. /**
  992. * add ssl option for fetchmail
  993. */
  994. function upgrade_439_mysql() {
  995. $table_fetchmail = table_by_key('fetchmail');
  996. if (!_mysql_field_exists($table_fetchmail, 'ssl')) {
  997. db_query_parsed("ALTER TABLE $table_fetchmail ADD `ssl` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `protocol` ; ");
  998. }
  999. }
  1000. function upgrade_439_pgsql() {
  1001. $table_fetchmail = table_by_key('fetchmail');
  1002. if (!_pgsql_field_exists($table_fetchmail, 'ssl')) {
  1003. db_query_parsed("ALTER TABLE $table_fetchmail ADD COLUMN ssl BOOLEAN NOT NULL DEFAULT false");
  1004. }
  1005. }
  1006. function upgrade_473_mysql() {
  1007. $table_admin = table_by_key('admin');
  1008. $table_alias = table_by_key('alias');
  1009. $table_al_dom = table_by_key('alias_domain');
  1010. $table_domain = table_by_key('domain');
  1011. $table_dom_adm = table_by_key('domain_admins');
  1012. $table_fmail = table_by_key('fetchmail');
  1013. $table_mailbox = table_by_key('mailbox');
  1014. $table_log = table_by_key('log');
  1015. # tables were created without explicit charset before :-(
  1016. $all_sql = explode("\n", trim("
  1017. ALTER TABLE $table_admin CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  1018. ALTER TABLE $table_admin CHANGE `password` `password` VARCHAR( 255 ) {LATIN1} NOT NULL
  1019. ALTER TABLE $table_admin DEFAULT {LATIN1}
  1020. ALTER TABLE $table_alias CHANGE `address` `address` VARCHAR( 255 ) {LATIN1} NOT NULL
  1021. ALTER TABLE $table_alias CHANGE `goto` `goto` TEXT {LATIN1} NOT NULL
  1022. ALTER TABLE $table_alias CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1023. ALTER TABLE $table_alias DEFAULT {LATIN1}
  1024. ALTER TABLE $table_al_dom CHANGE `alias_domain` `alias_domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1025. ALTER TABLE $table_al_dom CHANGE `target_domain` `target_domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1026. ALTER TABLE $table_al_dom DEFAULT {LATIN1}
  1027. ALTER TABLE $table_domain CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1028. ALTER TABLE $table_domain CHANGE `transport` `transport` VARCHAR( 255 ) {LATIN1} NOT NULL
  1029. ALTER TABLE $table_domain DEFAULT {LATIN1}
  1030. ALTER TABLE $table_dom_adm CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  1031. ALTER TABLE $table_dom_adm CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1032. ALTER TABLE $table_dom_adm DEFAULT {LATIN1}
  1033. ALTER TABLE $table_log CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  1034. ALTER TABLE $table_log CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1035. ALTER TABLE $table_log CHANGE `action` `action` VARCHAR( 255 ) {LATIN1} NOT NULL
  1036. ALTER TABLE $table_log CHANGE `data` `data` VARCHAR( 255 ) {LATIN1} NOT NULL
  1037. ALTER TABLE $table_log DEFAULT {LATIN1}
  1038. ALTER TABLE $table_mailbox CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  1039. ALTER TABLE $table_mailbox CHANGE `password` `password` VARCHAR( 255 ) {LATIN1} NOT NULL
  1040. ALTER TABLE $table_mailbox CHANGE `maildir` `maildir` VARCHAR( 255 ) {LATIN1} NOT NULL
  1041. ALTER TABLE $table_mailbox CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  1042. ALTER TABLE $table_mailbox DEFAULT {LATIN1}
  1043. ALTER TABLE $table_fmail CHANGE `mailbox` `mailbox` VARCHAR( 255 ) {LATIN1} NOT NULL
  1044. ALTER TABLE $table_fmail CHANGE `src_server` `src_server` VARCHAR( 255 ) {LATIN1} NOT NULL
  1045. ALTER TABLE $table_fmail CHANGE `src_user` `src_user` VARCHAR( 255 ) {LATIN1} NOT NULL
  1046. ALTER TABLE $table_fmail CHANGE `src_password` `src_password` VARCHAR( 255 ) {LATIN1} NOT NULL
  1047. ALTER TABLE $table_fmail CHANGE `src_folder` `src_folder` VARCHAR( 255 ) {LATIN1} NOT NULL
  1048. ALTER TABLE $table_fmail CHANGE `mda` `mda` VARCHAR( 255 ) {LATIN1} NOT NULL
  1049. ALTER TABLE $table_fmail CHANGE `mailbox` `mailbox` VARCHAR( 255 ) {LATIN1} NOT NULL
  1050. ALTER TABLE $table_fmail CHANGE `extra_options` `extra_options` TEXT {LATIN1} NULL DEFAULT NULL
  1051. ALTER TABLE $table_fmail CHANGE `returned_text` `returned_text` TEXT {LATIN1} NULL DEFAULT NULL
  1052. ALTER TABLE $table_fmail DEFAULT {LATIN1}
  1053. "));
  1054. foreach ($all_sql as $sql) {
  1055. $result = db_query_parsed($sql);
  1056. }
  1057. }
  1058. function upgrade_479_mysql() {
  1059. # ssl is a reserved word in MySQL and causes several problems. Renaming the field...
  1060. $table_fmail = table_by_key('fetchmail');
  1061. if (!_mysql_field_exists($table_fmail, 'usessl')) {
  1062. db_query_parsed("ALTER TABLE $table_fmail CHANGE `ssl` `usessl` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");
  1063. }
  1064. }
  1065. function upgrade_479_pgsql() {
  1066. $table_fmail = table_by_key('fetchmail');
  1067. if (!_pgsql_field_exists($table_fmail, 'usessl')) {
  1068. db_query_parsed("alter table $table_fmail rename column ssl to usessl");
  1069. }
  1070. }
  1071. function upgrade_483_mysql() {
  1072. $table_log = table_by_key('log');
  1073. db_query_parsed("ALTER TABLE $table_log CHANGE `data` `data` TEXT {LATIN1} NOT NULL");
  1074. }
  1075. # Add a local_part field to the mailbox table, and populate it with the local part of the user's address.
  1076. # This is to make it easier (hopefully) to change the filesystem location of a mailbox in the future
  1077. # See https://sourceforge.net/forum/message.php?msg_id=5394663
  1078. function upgrade_495_pgsql() {
  1079. $table_mailbox = table_by_key('mailbox');
  1080. if (!_pgsql_field_exists($table_mailbox, 'local_part')) {
  1081. db_query_parsed("ALTER TABLE $table_mailbox add column local_part varchar(255) ");
  1082. db_query_parsed("UPDATE $table_mailbox SET local_part = substring(username from '^(.*)@')");
  1083. db_query_parsed("ALTER TABLE $table_mailbox alter column local_part SET NOT NULL");
  1084. }
  1085. }
  1086. # See https://sourceforge.net/forum/message.php?msg_id=5394663
  1087. function upgrade_495_mysql() {
  1088. $table_mailbox = table_by_key('mailbox');
  1089. if (!_mysql_field_exists($table_mailbox, 'local_part')) {
  1090. db_query_parsed("ALTER TABLE $table_mailbox add local_part varchar(255) AFTER quota"); // allow to be null
  1091. db_query_parsed("UPDATE $table_mailbox SET local_part = substring_index(username, '@', 1)");
  1092. db_query_parsed("ALTER TABLE $table_mailbox change local_part local_part varchar(255) NOT NULL"); // remove null-ness...
  1093. }
  1094. }
  1095. function upgrade_504_mysql() {
  1096. $table_mailbox = table_by_key('mailbox');
  1097. db_query_parsed("ALTER TABLE $table_mailbox CHANGE `local_part` `local_part` VARCHAR( 255 ) {LATIN1} NOT NULL");
  1098. }
  1099. function upgrade_655_mysql_pgsql() {
  1100. db_query_parsed(_add_index('mailbox', 'domain', 'domain'));
  1101. db_query_parsed(_add_index('alias', 'domain', 'domain'));
  1102. }
  1103. /*
  1104. function number too small for upgrades from 2.3.x
  1105. -> adding activefrom and activeuntil to vacation table is now upgrade_964
  1106. -> the tables client_access, from_access, helo_access, rcpt_access, user_whitelist
  1107. are not used by PostfixAdmin - no replacement function needed
  1108. Note: Please never remove this function, even if it is disabled - it might be needed in case we have to debug a broken database upgrade etc.
  1109. Note: there never was a function upgrade_727_pgsql()
  1110. function upgrade_727_mysql() {
  1111. $table_vacation = table_by_key('vacation');
  1112. if(!_mysql_field_exists($table_vacation, 'activefrom')) {
  1113. db_query_parsed("ALTER TABLE $table_vacation add activefrom datetime default NULL");
  1114. }
  1115. if(!_mysql_field_exists($table_vacation, 'activeuntil')) {
  1116. db_query_parsed("ALTER TABLE $table_vacation add activeuntil datetime default NULL");
  1117. }
  1118. # the following tables are not used by postfixadmin
  1119. $table_client_access = table_by_key('client_access');
  1120. db_query_parsed("
  1121. CREATE TABLE IF NOT EXISTS $table_client_access (
  1122. `client` char(50) NOT NULL,
  1123. `action` char(50) NOT NULL default 'REJECT',
  1124. UNIQUE KEY `client` (`client`)
  1125. ) COMMENT='Postfix Admin - Client Access'
  1126. ");
  1127. $table_from_access = table_by_key('from_access');
  1128. db_query_parsed("
  1129. CREATE TABLE IF NOT EXISTS $table_from_access (
  1130. `from_access` char(50) NOT NULL,
  1131. `action` char(50) NOT NULL default 'REJECT',
  1132. UNIQUE KEY `from_access` (`from_access`)
  1133. ) COMMENT='Postfix Admin - From Access'
  1134. ");
  1135. $table_helo_access = table_by_key('helo_access');
  1136. db_query_parsed("
  1137. CREATE TABLE IF NOT EXISTS $table_helo_access (
  1138. `helo` char(50) NOT NULL,
  1139. `action` char(50) NOT NULL default 'REJECT',
  1140. UNIQUE KEY `helo` (`helo`)
  1141. ) COMMENT='Postfix Admin - Helo Access'
  1142. ");
  1143. $table_rcpt_access = table_by_key('rcpt_access');
  1144. db_query_parsed("
  1145. CREATE TABLE IF NOT EXISTS $table_rcpt_access (
  1146. `rcpt` char(50) NOT NULL,
  1147. `action` char(50) NOT NULL default 'REJECT',
  1148. UNIQUE KEY `rcpt` (`rcpt`)
  1149. ) COMMENT='Postfix Admin - Recipient Access'
  1150. ");
  1151. $table_user_whitelist = table_by_key('user_whitelist');
  1152. db_query_parsed("
  1153. CREATE TABLE IF NOT EXISTS $table_user_whitelist (
  1154. `recipient` char(50) NOT NULL,
  1155. `action` char(50) NOT NULL default 'REJECT',
  1156. UNIQUE KEY `recipient` (`recipient`)
  1157. ) COMMENT='Postfix Admin - User whitelist'
  1158. ");
  1159. }
  1160. */
  1161. function upgrade_729_mysql_pgsql() {
  1162. $table_quota = table_by_key('quota');
  1163. $table_quota2 = table_by_key('quota2');
  1164. # table for dovecot v1.0 & 1.1
  1165. # note: quota table created with old versions of upgrade.php (before r1605)
  1166. # will not have explicit "NOT NULL DEFAULT 0" for the "current" field
  1167. # (shouldn't hurt)
  1168. db_query_parsed("
  1169. CREATE TABLE {IF_NOT_EXISTS} $table_quota (
  1170. username VARCHAR(255) {LATIN1} NOT NULL,
  1171. path VARCHAR(100) {LATIN1} NOT NULL,
  1172. current {BIGINT},
  1173. PRIMARY KEY (username, path)
  1174. ) ;
  1175. ");
  1176. # table for dovecot >= 1.2
  1177. db_query_parsed("
  1178. CREATE TABLE {IF_NOT_EXISTS} $table_quota2 (
  1179. username VARCHAR(100) {LATIN1} NOT NULL,
  1180. bytes {BIGINT},
  1181. messages integer NOT NULL DEFAULT 0,
  1182. PRIMARY KEY (username)
  1183. ) ;
  1184. ");
  1185. }
  1186. function upgrade_730_pgsql() {
  1187. $table_quota = table_by_key('quota');
  1188. $table_quota2 = table_by_key('quota2');
  1189. db_query_parsed('CREATE LANGUAGE plpgsql', 1); /* will error if plpgsql is already installed */
  1190. # trigger for dovecot v1.0 & 1.1 quota table
  1191. # taken from http://wiki.dovecot.org/Quota/Dict
  1192. db_query_parsed("
  1193. CREATE OR REPLACE FUNCTION merge_quota() RETURNS TRIGGER AS \$merge_quota\$
  1194. BEGIN
  1195. UPDATE $table_quota SET current = NEW.current + current WHERE username = NEW.username AND path = NEW.path;
  1196. IF found THEN
  1197. RETURN NULL;
  1198. ELSE
  1199. RETURN NEW;
  1200. END IF;
  1201. END;
  1202. \$merge_quota\$ LANGUAGE plpgsql;
  1203. ");
  1204. db_query_parsed("
  1205. CREATE TRIGGER mergequota BEFORE INSERT ON $table_quota FOR EACH ROW EXECUTE PROCEDURE merge_quota();
  1206. ");
  1207. # trigger for dovecot >= 1.2 quota table
  1208. # taken from http://wiki.dovecot.org/Quota/Dict, table/trigger name changed to quota2 naming
  1209. db_query_parsed("
  1210. CREATE OR REPLACE FUNCTION merge_quota2() RETURNS TRIGGER AS \$\$
  1211. BEGIN
  1212. IF NEW.messages < 0 OR NEW.messages IS NULL THEN
  1213. -- ugly kludge: we came here from this function, really do try to insert
  1214. IF NEW.messages IS NULL THEN
  1215. NEW.messages = 0;
  1216. ELSE
  1217. NEW.messages = -NEW.messages;
  1218. END IF;
  1219. return NEW;
  1220. END IF;
  1221. LOOP
  1222. UPDATE $table_quota2 SET bytes = bytes + NEW.bytes,
  1223. messages = messages + NEW.messages
  1224. WHERE username = NEW.username;
  1225. IF found THEN
  1226. RETURN NULL;
  1227. END IF;
  1228. BEGIN
  1229. IF NEW.messages = 0 THEN
  1230. INSERT INTO $table_quota2 (bytes, messages, username) VALUES (NEW.bytes, NULL, NEW.username);
  1231. ELSE
  1232. INSERT INTO $table_quota2 (bytes, messages, username) VALUES (NEW.bytes, -NEW.messages, NEW.username);
  1233. END IF;
  1234. return NULL;
  1235. EXCEPTION WHEN unique_violation THEN
  1236. -- someone just inserted the record, update it
  1237. END;
  1238. END LOOP;
  1239. END;
  1240. \$\$ LANGUAGE plpgsql;
  1241. ");
  1242. db_query_parsed("
  1243. CREATE TRIGGER mergequota2 BEFORE INSERT ON $table_quota2
  1244. FOR EACH ROW EXECUTE PROCEDURE merge_quota2();
  1245. ");
  1246. }
  1247. function upgrade_945_mysql_pgsql() {
  1248. _db_add_field('vacation', 'modified', '{DATECURRENT}', 'created');
  1249. }
  1250. function upgrade_946_mysql_pgsql() {
  1251. # taken from upgrade_727_mysql, needs to be done for all databases
  1252. _db_add_field('vacation', 'activefrom', '{DATE}', 'body');
  1253. _db_add_field('vacation', 'activeuntil', '{DATEFUTURE}', 'activefrom');
  1254. }
  1255. function upgrade_968_pgsql() {
  1256. # pgsql counterpart for upgrade_169_mysql() - allow really big quota
  1257. $table_domain = table_by_key('domain');
  1258. $table_mailbox = table_by_key('mailbox');
  1259. db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN quota type bigint");
  1260. db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN maxquota type bigint");
  1261. db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN quota type bigint");
  1262. }
  1263. function upgrade_1050_mysql_pgsql() {
  1264. db_query_parsed(_add_index('log', 'domain_timestamp', 'domain,timestamp'));
  1265. }
  1266. function upgrade_1283_mysql_pgsql() {
  1267. _db_add_field('admin', 'superadmin', '{BOOLEAN}', 'password');
  1268. }
  1269. function upgrade_1284_mysql_pgsql() {
  1270. # migrate the ALL domain to the superadmin column
  1271. # Note: The ALL domain is not (yet) deleted to stay backwards-compatible for now (will be done in a later upgrade function)
  1272. $result = db_query("SELECT username FROM " . table_by_key('domain_admins') . " where domain='ALL'");
  1273. if ($result['rows'] > 0) {
  1274. while ($row = db_assoc($result['result'])) {
  1275. printdebug("Setting superadmin flag for " . $row['username']);
  1276. db_update('admin', 'username', $row['username'], array('superadmin' => db_get_boolean(true)));
  1277. }
  1278. }
  1279. }
  1280. function upgrade_1345_mysql() {
  1281. # $table_vacation = table_by_key('vacation');
  1282. # adding and usage of reply_type field removed in r1610
  1283. # db_query_parsed("ALTER TABLE `$table_vacation` ADD `reply_type` VARCHAR( 20 ) NOT NULL AFTER `domain` ");
  1284. # obsoleted by upgrade_1610()
  1285. # db_query_parsed("ALTER TABLE `$table_vacation` ADD `interval_time` INT NOT NULL DEFAULT '0' AFTER `reply_type` ");
  1286. }
  1287. function upgrade_1519_mysql_pgsql() {
  1288. _db_add_field('fetchmail', 'sslcertck', '{BOOLEAN}', 'usessl');
  1289. _db_add_field('fetchmail', 'sslcertpath', "VARCHAR(255) {UTF-8} DEFAULT ''", 'sslcertck');
  1290. _db_add_field('fetchmail', 'sslfingerprint', "VARCHAR(255) {LATIN1} DEFAULT ''", 'sslcertpath');
  1291. }
  1292. function upgrade_1610_mysql_pgsql() {
  1293. # obsoletes upgrade_1345_mysql() - which means debug mode could print "field already exists"
  1294. _db_add_field('vacation', 'interval_time', '{INT}', 'domain');
  1295. }
  1296. function upgrade_1685_mysql() {
  1297. # Fix existing log entries broken by https://sourceforge.net/p/postfixadmin/bugs/317/
  1298. $table = table_by_key('log');
  1299. db_query_parsed("UPDATE $table SET data = domain WHERE data = '' AND domain LIKE '%@%'");
  1300. db_query_parsed("UPDATE $table SET domain=SUBSTRING_INDEX(domain, '@', -1) WHERE domain=data;");
  1301. }
  1302. function upgrade_1685_pgsql() {
  1303. $table = table_by_key('log');
  1304. db_query_parsed("UPDATE $table SET data = domain WHERE data = '' AND domain LIKE '%@%'");
  1305. db_query_parsed("UPDATE $table SET domain=SPLIT_PART(domain, '@', 2) WHERE domain=data;");
  1306. }
  1307. function upgrade_1761_mysql() {
  1308. # upgrade_1762 adds the 'modified' column as {DATECURRENT}, therefore we first need to change
  1309. # 'date' to {DATE} (mysql only allows one {DATECURRENT} column per table)
  1310. $table_fetchmail = table_by_key('fetchmail');
  1311. db_query_parsed("ALTER TABLE $table_fetchmail CHANGE `date` `date` {DATE}");
  1312. }
  1313. function upgrade_1762_mysql_pgsql() {
  1314. _db_add_field('fetchmail', 'domain', "VARCHAR(255) {LATIN1} DEFAULT ''", 'id');
  1315. _db_add_field('fetchmail', 'active', '{BOOLEAN}', 'date');
  1316. _db_add_field('fetchmail', 'created', '{DATE}', 'date');
  1317. # If you followed SVN and got upgrade failures here, you might need to
  1318. # UPDATE config SET value=1760 WHERE name='version';
  1319. # and run setup.php again (upgrade_1761_mysql was added later).
  1320. _db_add_field('fetchmail', 'modified', '{DATECURRENT}', 'created');
  1321. }
  1322. function upgrade_1763_mysql() {
  1323. $table = table_by_key('fetchmail');
  1324. db_query_parsed("UPDATE $table SET domain=SUBSTRING_INDEX(mailbox, '@', -1) WHERE domain='';");
  1325. }
  1326. function upgrade_1763_pgsql() {
  1327. $table = table_by_key('fetchmail');
  1328. db_query_parsed("UPDATE $table SET domain=SPLIT_PART(mailbox, '@', 2) WHERE domain='';");
  1329. }
  1330. function upgrade_1767_mysql_pgsql() {
  1331. # 'active' was just added, so make sure all existing jobs stay active
  1332. $table = table_by_key('fetchmail');
  1333. db_query_parsed("UPDATE $table SET active='{BOOL_TRUE}'");
  1334. }
  1335. function upgrade_1795_mysql() {
  1336. # upgrade_1761_mysql() was added later (in r1795) - make sure it runs for everybody
  1337. # (running it twice doesn't hurt)
  1338. upgrade_1761_mysql();
  1339. }
  1340. function upgrade_1824_sqlite() {
  1341. $admin_table = table_by_key('admin');
  1342. $alias_table = table_by_key('alias');
  1343. $alias_domain_table = table_by_key('alias_domain');
  1344. $domain_table = table_by_key('domain');
  1345. $domain_admins_table = table_by_key('domain_admins');
  1346. $fetchmail_table = table_by_key('fetchmail');
  1347. $log_table = table_by_key('log');
  1348. $mailbox_table = table_by_key('mailbox');
  1349. $quota_table = table_by_key('quota');
  1350. $quota2_table = table_by_key('quota2');
  1351. $vacation_table = table_by_key('vacation');
  1352. $vacation_notification_table = table_by_key('vacation_notification');
  1353. db_query_parsed("
  1354. CREATE TABLE $admin_table (
  1355. `username` varchar(255) NOT NULL,
  1356. `password` varchar(255) NOT NULL,
  1357. `superadmin` {BOOLEAN},
  1358. `created` {DATE},
  1359. `modified` {DATE},
  1360. `active` {BOOLEAN_TRUE},
  1361. {PRIMARY} (`username`));
  1362. ");
  1363. db_query_parsed("
  1364. CREATE TABLE $alias_table (
  1365. `address` varchar(255) NOT NULL,
  1366. `goto` {FULLTEXT} NOT NULL,
  1367. `domain` varchar(255) NOT NULL,
  1368. `created` {DATE},
  1369. `modified` {DATE},
  1370. `active` {BOOLEAN_TRUE},
  1371. {PRIMARY} (`address`));
  1372. ");
  1373. db_query_parsed("
  1374. CREATE TABLE $alias_domain_table (
  1375. `alias_domain` varchar(255) NOT NULL,
  1376. `target_domain` varchar(255) NOT NULL,
  1377. `created` {DATE},
  1378. `modified` {DATE},
  1379. `active` {BOOLEAN_TRUE},
  1380. {PRIMARY} (`alias_domain`));
  1381. ");
  1382. db_query_parsed("
  1383. CREATE TABLE $domain_table (
  1384. `domain` varchar(255) NOT NULL,
  1385. `description` varchar(255) NOT NULL,
  1386. `aliases` {INT},
  1387. `mailboxes` {INT},
  1388. `maxquota` {BIGINT},
  1389. `quota` {BIGINT},
  1390. `transport` varchar(255) NOT NULL,
  1391. `backupmx` {BOOLEAN},
  1392. `created` {DATE},
  1393. `modified` {DATE},
  1394. `active` {BOOLEAN_TRUE},
  1395. {PRIMARY} (`domain`));
  1396. ");
  1397. db_query_parsed("
  1398. CREATE TABLE $domain_admins_table (
  1399. `username` varchar(255) NOT NULL,
  1400. `domain` varchar(255) NOT NULL,
  1401. `created` {DATE},
  1402. `active` {BOOLEAN_TRUE});
  1403. ");
  1404. db_query_parsed("
  1405. CREATE TABLE $fetchmail_table (
  1406. `id` {AUTOINCREMENT},
  1407. `domain` varchar(255) DEFAULT '',
  1408. `mailbox` varchar(255) NOT NULL,
  1409. `src_server` varchar(255) NOT NULL,
  1410. `src_auth` varchar(255) DEFAULT NULL,
  1411. `src_user` varchar(255) NOT NULL,
  1412. `src_password` varchar(255) NOT NULL,
  1413. `src_folder` varchar(255) NOT NULL,
  1414. `poll_time` int(11) NOT NULL DEFAULT '10',
  1415. `fetchall` {BOOLEAN},
  1416. `keep` {BOOLEAN},
  1417. `protocol` {FULLTEXT} DEFAULT NULL,
  1418. `usessl` {BOOLEAN},
  1419. `sslcertck` {BOOLEAN},
  1420. `sslcertpath` varchar(255) DEFAULT '',
  1421. `sslfingerprint` varchar(255) DEFAULT '',
  1422. `extra_options` {FULLTEXT},
  1423. `returned_text` {FULLTEXT},
  1424. `mda` varchar(255) NOT NULL,
  1425. `date` {DATE},
  1426. `created` {DATE},
  1427. `modified` {DATECURRENT},
  1428. `active` {BOOLEAN});
  1429. ");
  1430. db_query_parsed("
  1431. CREATE TABLE $log_table (
  1432. `timestamp` {DATE},
  1433. `username` varchar(255) NOT NULL,
  1434. `domain` varchar(255) NOT NULL,
  1435. `action` varchar(255) NOT NULL,
  1436. `data` {FULLTEXT} NOT NULL);
  1437. ");
  1438. db_query_parsed("
  1439. CREATE TABLE $mailbox_table (
  1440. `username` varchar(255) NOT NULL,
  1441. `password` varchar(255) NOT NULL,
  1442. `name` varchar(255) NOT NULL,
  1443. `maildir` varchar(255) NOT NULL,
  1444. `quota` {BIGINT},
  1445. `local_part` varchar(255) NOT NULL,
  1446. `domain` varchar(255) NOT NULL,
  1447. `created` {DATE},
  1448. `modified` {DATE},
  1449. `active` {BOOLEAN_TRUE},
  1450. {PRIMARY} (`username`));
  1451. ");
  1452. db_query_parsed("
  1453. CREATE TABLE $quota_table (
  1454. `username` varchar(255) NOT NULL,
  1455. `path` varchar(100) NOT NULL,
  1456. `current` {BIGINT},
  1457. {PRIMARY} (`username`,`path`));
  1458. ");
  1459. db_query_parsed("
  1460. CREATE TABLE $quota2_table (
  1461. `username` varchar(255) NOT NULL,
  1462. `bytes` {BIGINT},
  1463. `messages` {INT},
  1464. {PRIMARY} (`username`));
  1465. ");
  1466. db_query_parsed("
  1467. CREATE TABLE $vacation_table (
  1468. `email` varchar(255) NOT NULL,
  1469. `subject` varchar(255) NOT NULL,
  1470. `body` {FULLTEXT} NOT NULL,
  1471. `activefrom` {DATE},
  1472. `activeuntil` {DATEFUTURE},
  1473. `cache` {FULLTEXT} NOT NULL DEFAULT '',
  1474. `domain` varchar(255) NOT NULL,
  1475. `interval_time` {INT},
  1476. `created` {DATE},
  1477. `modified` {DATECURRENT},
  1478. `active` {BOOLEAN_TRUE},
  1479. {PRIMARY} (`email`));
  1480. ");
  1481. db_query_parsed("
  1482. CREATE TABLE $vacation_notification_table (
  1483. `on_vacation` varchar(255) NOT NULL,
  1484. `notified` varchar(255) NOT NULL,
  1485. `notified_at` {DATECURRENT},
  1486. {PRIMARY} (`on_vacation`,`notified`),
  1487. CONSTRAINT `vacation_notification_pkey` FOREIGN KEY (`on_vacation`) REFERENCES `vacation` (`email`) ON DELETE CASCADE);
  1488. ");
  1489. }
  1490. function upgrade_1835_mysql() {
  1491. # change default values for existing datetime fields with a 0000-00-00 default to {DATETIME}
  1492. foreach (array('domain_admins', 'vacation') as $table_to_change) {
  1493. $table = table_by_key($table_to_change);
  1494. db_query_parsed("ALTER TABLE $table CHANGE `created` `created` {DATETIME}");
  1495. }
  1496. foreach (array('admin', 'alias', 'alias_domain', 'domain', 'mailbox') as $table_to_change) {
  1497. $table = table_by_key($table_to_change);
  1498. db_query_parsed("ALTER TABLE $table CHANGE `created` `created` {DATETIME}, CHANGE `modified` `modified` {DATETIME}");
  1499. }
  1500. $table = table_by_key('log');
  1501. db_query_parsed("ALTER TABLE $table CHANGE `timestamp` `timestamp` {DATETIME}");
  1502. }
  1503. function upgrade_1836_mysql() {
  1504. $table_alias_domain = table_by_key('alias_domain');
  1505. $table_vacation_notification = table_by_key('vacation_notification');
  1506. $all_sql = explode("\n", trim("
  1507. ALTER TABLE $table_alias_domain CHANGE `alias_domain` `alias_domain` VARCHAR(255) {LATIN1} NOT NULL default ''
  1508. ALTER TABLE $table_alias_domain CHANGE `target_domain` `target_domain` VARCHAR(255) {LATIN1} NOT NULL default ''
  1509. ALTER TABLE $table_vacation_notification CHANGE `notified` `notified` VARCHAR(255) {LATIN1} NOT NULL default ''
  1510. "));
  1511. foreach ($all_sql as $sql) {
  1512. $result = db_query_parsed($sql, true);
  1513. }
  1514. }
  1515. function upgrade_1837() {
  1516. global $CONF;
  1517. if ($CONF['database_type'] == 'sqlite') {
  1518. return;
  1519. }
  1520. # alternative contact means to reset a forgotten password
  1521. foreach (array('admin', 'mailbox') as $table) {
  1522. _db_add_field($table, 'phone', "varchar(30) {UTF-8} NOT NULL DEFAULT ''", 'active');
  1523. _db_add_field($table, 'email_other', "varchar(255) {UTF-8} NOT NULL DEFAULT ''", 'phone');
  1524. _db_add_field($table, 'token', "varchar(255) {UTF-8} NOT NULL DEFAULT ''", 'email_other');
  1525. _db_add_field($table, 'token_validity', '{DATETIME}', 'token');
  1526. }
  1527. }
  1528. # TODO MySQL:
  1529. # - various varchar fields do not have a default value
  1530. # https://sourceforge.net/projects/postfixadmin/forums/forum/676076/topic/3419725
  1531. # - change default of all timestamp fields to {DATECURRENT} (CURRENT_TIMESTAMP} or {DATE}
  1532. # including vacation.activefrom/activeuntil (might have a different default as leftover from upgrade_727_mysql)
  1533. # including vacation.modified - should be {DATE}, not {DATECURRENT}
  1534. # https://sourceforge.net/tracker/?func=detail&aid=1699218&group_id=191583&atid=937964
  1535. # @todo vacation.email has 2 indizes
  1536. # Upgrading to v1835 & v1836 in sqlite have a couple of peculiarities:
  1537. # - DATE and DATETIME are the same type internally (NUMERIC)
  1538. # - SQLite does not support ALTER COLUMN. At all.
  1539. # TODO: Rename/create anew/migrate/drop tables for v1836... If it matters?
  1540. function upgrade_1837_sqlite() {
  1541. # Add columns for the alternative contact to reset a forgotten password.
  1542. foreach (array('admin', 'mailbox') as $table_to_change) {
  1543. $table = table_by_key($table_to_change);
  1544. if (!_sqlite_field_exists($table, 'phone')) {
  1545. db_query_parsed("ALTER TABLE `$table` ADD COLUMN `phone` varchar(30) NOT NULL DEFAULT ''");
  1546. }
  1547. if (!_sqlite_field_exists($table, 'email_other')) {
  1548. db_query_parsed("ALTER TABLE `$table` ADD COLUMN `email_other` varchar(255) NOT NULL DEFAULT ''");
  1549. }
  1550. }
  1551. }
  1552. /* https://github.com/postfixadmin/postfixadmin/issues/89 */
  1553. # upgrade_1838_mysql() renamed to upgrade_1839() to keep all databases in sync
  1554. function upgrade_1839() {
  1555. if(!db_sqlite()) {
  1556. return _db_add_field('log', 'id', '{AUTOINCREMENT} {PRIMARY}', 'data');
  1557. }
  1558. /* ONLY FOR Sqlite */
  1559. // probably didn't have a working log table, so drop it and recreate with an id field.
  1560. $log_table = table_by_key('log');
  1561. db_query_parsed("DROP TABLE IF EXISTS $log_table");
  1562. db_query_parsed("
  1563. CREATE TABLE $log_table (
  1564. `id` {AUTOINCREMENT},
  1565. `timestamp` {DATE},
  1566. `username` varchar(255) NOT NULL,
  1567. `domain` varchar(255) NOT NULL,
  1568. `action` varchar(255) NOT NULL,
  1569. `data` {FULLTEXT} NOT NULL);
  1570. ");
  1571. }
  1572. function upgrade_1840_mysql_pgsql() {
  1573. # sqlite doesn't support changing the default value
  1574. $vacation = table_by_key('vacation');
  1575. db_query_parsed("ALTER TABLE $vacation ALTER COLUMN activeuntil SET DEFAULT '2038-01-18'");
  1576. }
  1577. /* try and fix: https://github.com/postfixadmin/postfixadmin/issues/177 - sqlite missing columns */
  1578. function upgrade_1841_sqlite() {
  1579. foreach (array('admin', 'mailbox') as $table) {
  1580. _db_add_field($table, 'phone', "varchar(30) {UTF-8} NOT NULL DEFAULT ''", 'active');
  1581. _db_add_field($table, 'email_other', "varchar(255) {UTF-8} NOT NULL DEFAULT ''", 'phone');
  1582. _db_add_field($table, 'token', "varchar(255) {UTF-8} NOT NULL DEFAULT ''", 'email_other');
  1583. _db_add_field($table, 'token_validity', '{DATETIME}', 'token');
  1584. }
  1585. }