diff --git a/functions.inc.php b/functions.inc.php index 3f88915f..b116951c 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -16,6 +16,7 @@ */ $version = '2.93'; +$min_db_version = 1835; # update (at least) before a release with the latest function numbrer in upgrade.php /** * check_session @@ -1754,7 +1755,34 @@ function table_by_key ($table_key) { return $CONF['database_prefix'].$table; } +/* + * check if the database layout is up to date + * returns the current 'version' value from the config table + * if $error_out is True (default), die() with a message that recommends to run setup.php. + */ +function check_db_version($error_out = True) { + global $min_db_version; + $table = table_by_key('config'); + + $sql = "SELECT value FROM $table WHERE name = 'version'"; + $r = db_query($sql); + + if($r['rows'] == 1) { + $row = db_assoc($r['result']); + $dbversion = $row['value']; + } else { + $dbversion = 0; + db_query("INSERT INTO $table (name, value) VALUES ('version', '0')", 0, ''); + } + + if ( ($dbversion < $min_db_version) && $error_out == True) { + echo "ERROR: The PostfixAdmin database layout is outdated (you have r$dbversion, but r$min_db_version is expected).\nPlease run setup.php to upgrade the database.\n"; + exit(1); + } + + return $dbversion; +} /* Called after an alias_domain has been deleted in the DBMS. diff --git a/login.php b/login.php index 4d83ae0d..42acda0c 100644 --- a/login.php +++ b/login.php @@ -34,6 +34,7 @@ if($CONF['configured'] !== true) { exit; } +check_db_version(); # check if the database layout is up to date (and error out if not) if ($_SERVER['REQUEST_METHOD'] == "POST") { diff --git a/scripts/postfixadmin-cli.php b/scripts/postfixadmin-cli.php index b105e85d..904956bc 100644 --- a/scripts/postfixadmin-cli.php +++ b/scripts/postfixadmin-cli.php @@ -177,6 +177,9 @@ class PostfixAdmin { return false; } + # make sure global variables fron functions.inc.php end up in the global namespace, instead of being local to this function + global $version, $min_db_version; + if (!require_once(PATH . '/common.php')) { $this->stderr("Failed to load " . PATH . '/common.php'); return false; @@ -190,6 +193,8 @@ class PostfixAdmin { public function dispatch() { $CONF = Config::read('all'); + check_db_version(); # ensure the database layout is up to date + if (!isset($this->args[0])) { $this->help(); return; diff --git a/upgrade.php b/upgrade.php index ddaefa80..95e52eee 100644 --- a/upgrade.php +++ b/upgrade.php @@ -131,21 +131,7 @@ if($CONF['database_type'] == 'pgsql') { db_query_parsed($mysql, 0, " ENGINE = MYISAM COMMENT = 'PostfixAdmin settings'"); } -$sql = "SELECT * FROM $table WHERE name = 'version'"; - -// insert into config('version', '01'); - -$r = db_query($sql); - -if($r['rows'] == 1) { - $rs = $r['result']; - $row = db_array($rs); - $version = $row['value']; -} else { - db_query_parsed("INSERT INTO $table (name, value) VALUES ('version', '0')", 0, ''); - $version = 0; -} - +$version = check_db_version(False); _do_upgrade($version); function _do_upgrade($current_version) { diff --git a/users/login.php b/users/login.php index e697c95f..41fbd214 100644 --- a/users/login.php +++ b/users/login.php @@ -30,6 +30,7 @@ $rel_path = '../'; define('POSTFIXADMIN_LOGOUT', 1); require_once("../common.php"); +check_db_version(); # check if the database layout is up to date (and error out if not) if ($_SERVER['REQUEST_METHOD'] == "POST") {