mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
Merge pull request #922 from postfixadmin/fixes-4.0-beta
Some fixes for 4.0 beta (thanks @cboltz)
This commit is contained in:
3 files changed
+337
-279
No files matched your search
+19
-9
@@ -69,7 +69,8 @@ class TotpPf
|
||||
return false;
|
||||
}
|
||||
|
||||
$sql = "SELECT totp_secret FROM {$this->table} WHERE username = :username AND active = :active";
|
||||
$table_name = table_by_key($this->table);
|
||||
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
|
||||
|
||||
$active = db_get_boolean(true);
|
||||
|
||||
@@ -93,7 +94,8 @@ class TotpPf
|
||||
*/
|
||||
public function checkUserTOTP(string $username, string $code): bool
|
||||
{
|
||||
$sql = "SELECT totp_secret FROM {$this->table} WHERE username = :username AND active = :active";
|
||||
$table_name = table_by_key($this->table);
|
||||
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
|
||||
|
||||
$active = db_get_boolean(true);
|
||||
|
||||
@@ -129,7 +131,8 @@ class TotpPf
|
||||
|
||||
public function removeTotpFromUser(string $username): void
|
||||
{
|
||||
$sql = "UPDATE {$this->table} SET totp_secret = NULL WHERE username = :username";
|
||||
$table_name = table_by_key($this->table);
|
||||
$sql = "UPDATE $table_name SET totp_secret = NULL WHERE username = :username";
|
||||
db_execute($sql, ['username' => $username]);
|
||||
}
|
||||
|
||||
@@ -146,7 +149,8 @@ class TotpPf
|
||||
throw new \Exception(Config::Lang('pPassword_password_current_text_error'));
|
||||
}
|
||||
|
||||
$sql = "SELECT totp_secret FROM {$this->table} WHERE username = :username AND active = :active";
|
||||
$table_name = table_by_key($this->table);
|
||||
$sql = "SELECT totp_secret FROM $table_name WHERE username = :username AND active = :active";
|
||||
|
||||
$active = db_get_boolean(true);
|
||||
|
||||
@@ -309,8 +313,10 @@ class TotpPf
|
||||
|
||||
if ($error == 0) {
|
||||
// OK to insert/replace.
|
||||
|
||||
$totp_exception_address = table_by_key('totp_exception_address');
|
||||
// As PostgeSQL lacks REPLACE we first check and delete any previous rows matching this ip and user
|
||||
$exists = db_query_all('SELECT id FROM totp_exception_address WHERE ip = :ip AND username = :username',
|
||||
$exists = db_query_all("SELECT id FROM $totp_exception_address WHERE ip = :ip AND username = :username",
|
||||
['ip' => $ip_address, 'username' => $exception_username]);
|
||||
if (isset($exists[0])) {
|
||||
foreach ($exists as $x) {
|
||||
@@ -405,7 +411,8 @@ class TotpPf
|
||||
}
|
||||
|
||||
|
||||
$result = db_execute('DELETE FROM totp_exception_address WHERE id = :id', ['id' => $id]);
|
||||
$totp_exception_address = table_by_key('totp_exception_address');
|
||||
$result = db_execute("DELETE FROM $totp_exception_address WHERE id = :id", ['id' => $id]);
|
||||
|
||||
if ($result != 1) {
|
||||
db_log($Exception_domain, 'pViewlog_action_delete_totp_exception', "FAILURE: " . $username);
|
||||
@@ -449,7 +456,8 @@ class TotpPf
|
||||
*/
|
||||
public function getAllExceptions(): array
|
||||
{
|
||||
return db_query_all("SELECT * FROM totp_exception_address");
|
||||
$totp_exception_address = table_by_key('totp_exception_address');
|
||||
return db_query_all("SELECT * FROM $totp_exception_address");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -460,7 +468,8 @@ class TotpPf
|
||||
public function getExceptionsFor(string $username): array
|
||||
{
|
||||
list($local_part, $domain) = explode('@', $username);
|
||||
return db_query_all("SELECT * FROM totp_exception_address WHERE username = :username OR username = :domain OR username IS NULL", ['username' => $username, 'domain' => $domain]);
|
||||
$totp_exception_address = table_by_key('totp_exception_address');
|
||||
return db_query_all("SELECT * FROM $totp_exception_address WHERE username = :username OR username = :domain OR username IS NULL", ['username' => $username, 'domain' => $domain]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -470,6 +479,7 @@ class TotpPf
|
||||
*/
|
||||
public function getException(int $id): ?array
|
||||
{
|
||||
return db_query_one("SELECT * FROM totp_exception_address WHERE id = :id", ['id' => $id]);
|
||||
$totp_exception_address = table_by_key('totp_exception_address');
|
||||
return db_query_one("SELECT * FROM $totp_exception_address WHERE id = :id", ['id' => $id]);
|
||||
}
|
||||
}
|
||||
+176
-169
@@ -301,11 +301,15 @@ if ($authenticated) {
|
||||
$php_error_log = ini_get('error_log');
|
||||
} else {
|
||||
if (!empty($check['error'])) {
|
||||
echo '<h3 class="text-danger">Hosting Environment errors found. Login to see details.</h3>';
|
||||
echo '<p class="text-danger">Hosting Environment errors found. Login to see details.</p>';
|
||||
}
|
||||
|
||||
if (!empty($check['warn'])) {
|
||||
echo '<h3 class="text-warning">Hosting Environment warnings found. Login to see details.</h3>';
|
||||
echo '<p class="text-warning">Hosting Environment warnings found. Login to see details.</p>';
|
||||
}
|
||||
|
||||
if (empty($check['warn']) && empty($check['error'])) {
|
||||
echo "<p> $tick No problems detected.</p>";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,188 +320,195 @@ if ($authenticated) {
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h2 class="h2">Database Update</h2>
|
||||
<h2 class="h2">Database</h2>
|
||||
|
||||
<?php
|
||||
$db = false;
|
||||
<ul>
|
||||
<?php
|
||||
$db = false;
|
||||
try {
|
||||
$db = db_connect();
|
||||
} catch (\Exception $e) {
|
||||
echo "<p class='h3 text-danger'>Something went wrong while trying to connect to the database. A message should be logged - check PHP's error_log (" . ini_get('error_log') . ')</p>';
|
||||
echo "<li class='h3 text-danger'>Something went wrong while trying to connect to the database. A message should be logged - check PHP's error_log (" . ini_get('error_log') . ')</li>';
|
||||
error_log("Couldn't perform PostfixAdmin database update - failed to connect to db? " . $e->getMessage() . " Trace: " . $e->getTraceAsString());
|
||||
}
|
||||
|
||||
if ($db) {
|
||||
echo "<p>Everything seems fine... attempting to create/update database structure</p>\n";
|
||||
echo "<li>$tick Database - connection works</li>";
|
||||
try {
|
||||
require_once(dirname(__FILE__) . '/upgrade.php');
|
||||
ob_start();
|
||||
$ret = require_once(dirname(__FILE__) . '/upgrade.php');
|
||||
$output = ob_get_clean();
|
||||
if ($ret) {
|
||||
echo "<li>$tick Database update check - $output </li>";
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
if ($authenticated) {
|
||||
echo "<p class='h3 text-danger'>Exception message: {$e->getMessage()} - check logs!</p>";
|
||||
echo "<li class='h3 text-danger'>Exception message: {$e->getMessage()} - check logs!</li>";
|
||||
}
|
||||
echo "<p class='h3 text-danger'>Something went wrong while trying to apply database updates, a message should be logged - check PHP's error_log (" . ini_get('error_log') . ')</p>';
|
||||
echo "<li class='h3 text-danger'>Something went wrong while trying to apply database updates, a message should be logged - check PHP's error_log (" . ini_get('error_log') . ')</li>';
|
||||
error_log("Couldn't perform PostfixAdmin database update via upgrade.php - " . $e->getMessage() . " Trace: " . $e->getTraceAsString());
|
||||
}
|
||||
} else {
|
||||
echo "<h3 class='h3 text-danger'>Could not connect to database to perform updates; check PHP error log.</h3>";
|
||||
echo "<li class='text-danger'>Could not connect to database to perform updates; check PHP error log.</li>";
|
||||
}
|
||||
?>
|
||||
|
||||
</div>
|
||||
</ul>
|
||||
</div
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
if ($authenticated) {
|
||||
$setupMessage = '';
|
||||
<?php
|
||||
if ($authenticated) {
|
||||
$setupMessage = '';
|
||||
|
||||
if (safepost("submit") === "createadmin") {
|
||||
?>
|
||||
<div class='row'>
|
||||
<div class='col-12'>
|
||||
<?php
|
||||
# "create admin" form submitted, make sure the correct setup password was specified.
|
||||
if (safepost("submit") === "createadmin") {
|
||||
?>
|
||||
<div class='row'>
|
||||
<div class='col-12'>
|
||||
<?php
|
||||
# "create admin" form submitted, make sure the correct setup password was specified.
|
||||
|
||||
// XXX need to ensure domains table includes an 'ALL' entry.
|
||||
$table_domain = table_by_key('domain');
|
||||
$rows = db_query_all("SELECT * FROM $table_domain WHERE domain = 'ALL'");
|
||||
if (empty($rows)) {
|
||||
// all other fields should default through the schema.
|
||||
db_insert('domain', array('domain' => 'ALL', 'description' => '', 'transport' => ''));
|
||||
}
|
||||
|
||||
$values = array(
|
||||
'username' => safepost('username'),
|
||||
'password' => safepost('password'),
|
||||
'password2' => safepost('password2'),
|
||||
'superadmin' => 1,
|
||||
'domains' => array(),
|
||||
'active' => 1,
|
||||
);
|
||||
|
||||
list($error, $setupMessage, $errors) = create_admin($values);
|
||||
|
||||
if ($error == 1) {
|
||||
$tUsername = htmlentities($values['username']);
|
||||
error_log("failed to add admin - " . json_encode([$error, $setupMessage, $errors]));
|
||||
echo "<p class='text-danger'>Admin addition failed; check field error messages or server logs.</p>";
|
||||
} else {
|
||||
// all good!.
|
||||
$setupMessage .= "<p>You are done with your basic setup. <b>You can now <a href='login.php'>login to PostfixAdmin</a> using the account you just created.</b></p>";
|
||||
}
|
||||
|
||||
echo "</div></div>";
|
||||
// XXX need to ensure domains table includes an 'ALL' entry.
|
||||
$table_domain = table_by_key('domain');
|
||||
$rows = db_query_all("SELECT * FROM $table_domain WHERE domain = 'ALL'");
|
||||
if (empty($rows)) {
|
||||
// all other fields should default through the schema.
|
||||
db_insert('domain', array('domain' => 'ALL', 'description' => '', 'transport' => ''));
|
||||
}
|
||||
|
||||
$table_admin = table_by_key('admin');
|
||||
$bool = db_get_boolean(true);
|
||||
$admins = db_query_all("SELECT * FROM $table_admin WHERE superadmin = '$bool' AND active = '$bool'");
|
||||
$values = array(
|
||||
'username' => safepost('username'),
|
||||
'password' => safepost('password'),
|
||||
'password2' => safepost('password2'),
|
||||
'superadmin' => 1,
|
||||
'domains' => array(),
|
||||
'active' => 1,
|
||||
);
|
||||
|
||||
if (!empty($admins)) { ?>
|
||||
list($error, $setupMessage, $errors) = create_admin($values);
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
if ($error == 1) {
|
||||
$tUsername = htmlentities($values['username']);
|
||||
error_log("failed to add admin - " . json_encode([$error, $setupMessage, $errors]));
|
||||
echo "<p class='text-danger'>Admin addition failed; check field error messages or server logs.</p>";
|
||||
} else {
|
||||
// all good!.
|
||||
$setupMessage .= "<p>You are done with your basic setup. <b>You can now <a href='login.php'>login to PostfixAdmin</a> using the account you just created.</b></p>";
|
||||
}
|
||||
|
||||
<h2 class="h2">Super admins</h2>
|
||||
<p>The following 'super-admin' accounts have already been added to the database.</p>
|
||||
<ul>
|
||||
<?php
|
||||
foreach ($admins as $row) {
|
||||
echo "<li>{$row['username']}</li>";
|
||||
}
|
||||
?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h2>Add Superadmin Account</h2>
|
||||
|
||||
<form name="create_admin" class="form-horizontal" method="post">
|
||||
<div class="form-group">
|
||||
<label for="setup_password" class="col-sm-4 control-label">Setup password</label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required="required"
|
||||
name="setup_password"
|
||||
minlength=5
|
||||
value=""/>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<label for="username" class="col-sm-4 control-label"><?= $PALANG['admin'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="text" required="required" name="username"
|
||||
minlength=5
|
||||
id="username"
|
||||
value=""/>
|
||||
|
||||
<?= _error_field($errors, 'username'); ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password" class="col-sm-4 control-label"><?= $PALANG['password'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required=required
|
||||
name="password" minlength=5
|
||||
id="password" autocomplete="new-password"
|
||||
value=""/>
|
||||
<?= _error_field($errors, 'password'); ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password2"
|
||||
class="col-sm-4 control-label"><?= $PALANG['password_again'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required=required
|
||||
name="password2" minlength=5
|
||||
id="password2" autocomplete="new-password"
|
||||
value=""/>
|
||||
|
||||
<?= _error_field($errors, 'password2'); ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-4 col-sm-4">
|
||||
<button class="btn btn-primary" type="submit" name="submit"
|
||||
value="createadmin"><?= $PALANG['pAdminCreate_admin_button'] ?>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<p class="text-success"><?= $setupMessage ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
echo "</div></div>";
|
||||
}
|
||||
|
||||
?>
|
||||
</div>
|
||||
<footer class="footer mt-5 bg-dark">
|
||||
<div class="container text-center">
|
||||
<a target="_blank" rel="noopener"
|
||||
href="https://github.com/postfixadmin/postfixadmin/blob/master/DOCUMENTS/">Documentation</a>
|
||||
//
|
||||
<a target="_blank" rel="noopener"
|
||||
href="https://github.com/postfixadmin/postfixadmin/">Postfix Admin</a>
|
||||
$table_admin = table_by_key('admin');
|
||||
$bool = db_get_boolean(true);
|
||||
$admins = db_query_all("SELECT * FROM $table_admin WHERE superadmin = '$bool' AND active = '$bool'");
|
||||
|
||||
if (!empty($admins)) { ?>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
|
||||
<h2 class="h2">Super admins</h2>
|
||||
<p>The following 'super-admin' accounts have already been added to the database.</p>
|
||||
<ul>
|
||||
<?php
|
||||
foreach ($admins as $row) {
|
||||
echo "<li>{$row['username']}</li>";
|
||||
}
|
||||
?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
<?php } ?>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h2>Add Superadmin Account</h2>
|
||||
|
||||
<form name="create_admin" class="form-horizontal" method="post">
|
||||
<div class="form-group">
|
||||
<label for="setup_password" class="col-sm-4 control-label">Setup password</label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required="required"
|
||||
name="setup_password"
|
||||
minlength=5
|
||||
value=""/>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<label for="username" class="col-sm-4 control-label"><?= $PALANG['admin'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="text" required="required" name="username"
|
||||
minlength=5
|
||||
id="username"
|
||||
value=""/>
|
||||
|
||||
<?= _error_field($errors, 'username'); ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password" class="col-sm-4 control-label"><?= $PALANG['password'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required=required
|
||||
name="password" minlength=5
|
||||
id="password" autocomplete="new-password"
|
||||
value=""/>
|
||||
<?= _error_field($errors, 'password'); ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password2"
|
||||
class="col-sm-4 control-label"><?= $PALANG['password_again'] ?></label>
|
||||
<div class="col-sm-4">
|
||||
<input class="form-control" type="password" required=required
|
||||
name="password2" minlength=5
|
||||
id="password2" autocomplete="new-password"
|
||||
value=""/>
|
||||
|
||||
<?= _error_field($errors, 'password2'); ?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-4 col-sm-4">
|
||||
<button class="btn btn-primary" type="submit" name="submit"
|
||||
value="createadmin"><?= $PALANG['pAdminCreate_admin_button'] ?>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<p class="text-success"><?= $setupMessage ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
?>
|
||||
</div>
|
||||
<footer class="footer mt-5 bg-dark">
|
||||
<div class="container text-center">
|
||||
<a target="_blank" rel="noopener"
|
||||
href="https://github.com/postfixadmin/postfixadmin/blob/master/DOCUMENTS/">Documentation</a>
|
||||
//
|
||||
<a target="_blank" rel="noopener"
|
||||
href="https://github.com/postfixadmin/postfixadmin/">Postfix Admin</a>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -620,24 +631,20 @@ function do_software_environment_check()
|
||||
$error[] = "There is no database (PDO) support in your PHP setup, you MUST install a suitable PHP PDO extension (e.g. pdo_pgsql, pdo_mysql or pdo_sqlite).";
|
||||
}
|
||||
|
||||
if ($m_pdo_mysql) {
|
||||
$info[] = "Database - MySQL support available";
|
||||
} else {
|
||||
$info[] = "Database - MySQL (pdo_mysql) extension not found";
|
||||
if (!$m_pdo_mysql && $CONF['database_type'] == 'mysql') {
|
||||
$error[] = "Database support - MySQL compatible extension (pdo_mysql) not found";
|
||||
}
|
||||
|
||||
|
||||
// PostgreSQL functions
|
||||
if ($m_pdo_pgsql) {
|
||||
$info[] = "Database - PostgreSQL support available ";
|
||||
} else {
|
||||
$warn[] = "Database - PostgreSQL (pdo_pgsql) extension not found";
|
||||
if (!$m_pdo_pgsql && $CONF['database_type'] == 'pgsql') {
|
||||
$error[] = "Database support - PostgreSQL (pdo_pgsql) extension not found";
|
||||
}
|
||||
|
||||
if ($m_pdo_sqlite) {
|
||||
$info[] = "Database - SQLite support available";
|
||||
} else {
|
||||
$warn[] = "Database support - SQLite (pdo_sqlite) extension not found";
|
||||
if (!$m_pdo_sqlite && $CONF['database_type'] == 'sqlite') {
|
||||
$error[] = "Database support - SQLite (pdo_sqlite) PHP extension not found";
|
||||
}
|
||||
|
||||
if (!$m_pdo_mysql && !$m_pdo_sqlite && !$m_pdo_pgsql) {
|
||||
$error[] = "No database support available (mysql, sqlite or postgresql)";
|
||||
}
|
||||
|
||||
if (empty($CONF['encrypt'])) {
|
||||
|
||||
+142
-101
@@ -57,8 +57,7 @@ function _mysql_field_exists($table, $field)
|
||||
{
|
||||
# $table = table_by_key($table); # _mysql_field_exists is always called with the expanded table name - don't expand it twice
|
||||
$sql = "SHOW COLUMNS FROM $table LIKE ?";
|
||||
$r = db_query_all($sql, array( $field));
|
||||
|
||||
$r = db_query_all($sql, [$field]);
|
||||
return !empty($r);
|
||||
}
|
||||
|
||||
@@ -86,10 +85,6 @@ function _db_field_exists($table, $field)
|
||||
return _mysql_field_exists($table, $field);
|
||||
}
|
||||
}
|
||||
function _upgrade_filter_function($name)
|
||||
{
|
||||
return preg_match('/upgrade_[\d]+(_mysql|_pgsql|_sqlite|_mysql_pgsql)?$/', $name) == 1;
|
||||
}
|
||||
|
||||
function _db_add_field($table, $field, $fieldtype, $after = '')
|
||||
{
|
||||
@@ -100,7 +95,7 @@ function _db_add_field($table, $field, $fieldtype, $after = '')
|
||||
$query .= " AFTER $after "; # PgSQL does not support to specify where to add the column, MySQL does
|
||||
}
|
||||
|
||||
if (! _db_field_exists(table_by_key($table), $field)) {
|
||||
if (!_db_field_exists(table_by_key($table), $field)) {
|
||||
db_query_parsed($query);
|
||||
} else {
|
||||
printdebug("field already exists: $table.$field");
|
||||
@@ -160,31 +155,52 @@ if ($CONF['database_type'] == 'pgsql') {
|
||||
}
|
||||
|
||||
$version = check_db_version(false);
|
||||
_do_upgrade($version);
|
||||
return _do_upgrade($version);
|
||||
|
||||
function _do_upgrade($current_version)
|
||||
function _do_upgrade(int $current_version): bool
|
||||
{
|
||||
global $CONF;
|
||||
|
||||
$target_version = 0;
|
||||
$target_version = $current_version;
|
||||
// Rather than being bound to an svn revision number, just look for the largest function name that matches upgrade_\d+...
|
||||
// $target_version = preg_replace('/[^0-9]/', '', '$Revision$');
|
||||
$funclist = get_defined_functions();
|
||||
$our_upgrade_functions = array_filter($funclist['user'], '_upgrade_filter_function');
|
||||
|
||||
$filter = function (string $name): bool {
|
||||
return preg_match('/upgrade_[\d]+(_mysql|_pgsql|_sqlite|_mysql_pgsql)?$/', $name) == 1;
|
||||
};
|
||||
|
||||
$our_upgrade_functions = array_filter(
|
||||
$funclist['user'],
|
||||
$filter
|
||||
);
|
||||
|
||||
|
||||
foreach ($our_upgrade_functions as $function_name) {
|
||||
$bits = explode("_", $function_name);
|
||||
$function_number = $bits[1];
|
||||
// just find the highest number we need to go to, later on we go through numerically ascending.
|
||||
if ($function_number > $current_version && $function_number > $target_version) {
|
||||
$target_version = $function_number;
|
||||
}
|
||||
}
|
||||
|
||||
if ($current_version >= $target_version) {
|
||||
# already up to date
|
||||
echo_out("<p>Database is up to date: $current_version/$target_version </p>");
|
||||
if ($target_version == 0) {
|
||||
$target_version = $current_version;
|
||||
}
|
||||
|
||||
|
||||
if ($current_version == $target_version) {
|
||||
echo_out("all database updates are applied (version $current_version)");
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($current_version > $target_version) {
|
||||
// surely impossible?
|
||||
echo_out("Database - our current version $current_version is more recent than the $target_version - this shouldn't be possible?");
|
||||
return false;
|
||||
}
|
||||
|
||||
echo_out("<p>Updating database:</p><p>- old version: $current_version; target version: $target_version</p>\n");
|
||||
echo_out("<div style='color:#999'> (If the update doesn't work, run setup.php?debug=1 to see the detailed error messages and SQL queries.)</div>");
|
||||
|
||||
@@ -234,8 +250,9 @@ function _do_upgrade($current_version)
|
||||
// Update config table so we don't run the same query twice in the future.
|
||||
$table = table_by_key('config');
|
||||
$sql = "UPDATE $table SET value = :value WHERE name = 'version'";
|
||||
db_execute($sql, array('value' => $i));
|
||||
};
|
||||
db_execute($sql, ['value' => $i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,75 +269,75 @@ function db_query_parsed($sql, $ignore_errors = 0, $attach_mysql = "")
|
||||
|
||||
if (db_mysql()) {
|
||||
$replace = array(
|
||||
'{AUTOINCREMENT}' => 'int(11) not null auto_increment',
|
||||
'{PRIMARY}' => 'primary key',
|
||||
'{UNSIGNED}' => 'unsigned' ,
|
||||
'{FULLTEXT}' => 'FULLTEXT',
|
||||
'{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '/*!40100 CHARACTER SET utf8mb4 */',
|
||||
'{LATIN1}' => '/*!40100 CHARACTER SET latin1 COLLATE latin1_general_ci */',
|
||||
'{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
|
||||
'{RENAME_COLUMN}' => 'CHANGE COLUMN',
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => 'ENGINE=InnoDB',
|
||||
'{INT}' => 'integer NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
|
||||
'{DATETIME}' => "datetime NOT NULL default '2000-01-01 00:00:00'", # different from {DATE} only for MySQL
|
||||
'{DATE}' => "timestamp NOT NULL default '2000-01-01'", # MySQL needs a sane default (no default is interpreted as CURRENT_TIMESTAMP, which is ...
|
||||
'{DATEFUTURE}' => "timestamp NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
|
||||
'{DATECURRENT}' => 'timestamp NOT NULL default CURRENT_TIMESTAMP', # only allowed once per table in MySQL
|
||||
'{COLLATE}' => "CHARACTER SET latin1 COLLATE latin1_general_ci", # just incase someone has a unicode collation set.
|
||||
'{AUTOINCREMENT}' => 'int(11) not null auto_increment',
|
||||
'{PRIMARY}' => 'primary key',
|
||||
'{UNSIGNED}' => 'unsigned',
|
||||
'{FULLTEXT}' => 'FULLTEXT',
|
||||
'{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '/*!40100 CHARACTER SET utf8mb4 */',
|
||||
'{LATIN1}' => '/*!40100 CHARACTER SET latin1 COLLATE latin1_general_ci */',
|
||||
'{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
|
||||
'{RENAME_COLUMN}' => 'CHANGE COLUMN',
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => 'ENGINE=InnoDB',
|
||||
'{INT}' => 'integer NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
|
||||
'{DATETIME}' => "datetime NOT NULL default '2000-01-01 00:00:00'", # different from {DATE} only for MySQL
|
||||
'{DATE}' => "timestamp NOT NULL default '2000-01-01'", # MySQL needs a sane default (no default is interpreted as CURRENT_TIMESTAMP, which is ...
|
||||
'{DATEFUTURE}' => "timestamp NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
|
||||
'{DATECURRENT}' => 'timestamp NOT NULL default CURRENT_TIMESTAMP', # only allowed once per table in MySQL
|
||||
'{COLLATE}' => "CHARACTER SET latin1 COLLATE latin1_general_ci", # just incase someone has a unicode collation set.
|
||||
|
||||
);
|
||||
$sql = "$sql $attach_mysql";
|
||||
} elseif (db_sqlite()) {
|
||||
$replace = array(
|
||||
'{AUTOINCREMENT}' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
|
||||
'{PRIMARY}' => 'PRIMARY KEY',
|
||||
'{UNSIGNED}' => 'unsigned',
|
||||
'{FULLTEXT}' => 'text',
|
||||
'{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '',
|
||||
'{LATIN1}' => '',
|
||||
'{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
|
||||
'{RENAME_COLUMN}' => 'CHANGE COLUMN',
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => '',
|
||||
'{INT}' => 'int(11) NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint(20) NOT NULL DEFAULT 0',
|
||||
'{DATETIME}' => "datetime NOT NULL default '2000-01-01'",
|
||||
'{DATE}' => "datetime NOT NULL default '2000-01-01'",
|
||||
'{DATEFUTURE}' => "datetime NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
|
||||
'{DATECURRENT}' => 'datetime NOT NULL default CURRENT_TIMESTAMP',
|
||||
'{COLLATE}' => ''
|
||||
'{AUTOINCREMENT}' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
|
||||
'{PRIMARY}' => 'PRIMARY KEY',
|
||||
'{UNSIGNED}' => 'unsigned',
|
||||
'{FULLTEXT}' => 'text',
|
||||
'{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '',
|
||||
'{LATIN1}' => '',
|
||||
'{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
|
||||
'{RENAME_COLUMN}' => 'CHANGE COLUMN',
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => '',
|
||||
'{INT}' => 'int(11) NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint(20) NOT NULL DEFAULT 0',
|
||||
'{DATETIME}' => "datetime NOT NULL default '2000-01-01'",
|
||||
'{DATE}' => "datetime NOT NULL default '2000-01-01'",
|
||||
'{DATEFUTURE}' => "datetime NOT NULL default '2038-01-18'", # different default timestamp for vacation.activeuntil
|
||||
'{DATECURRENT}' => 'datetime NOT NULL default CURRENT_TIMESTAMP',
|
||||
'{COLLATE}' => ''
|
||||
);
|
||||
} elseif ($CONF['database_type'] == 'pgsql') {
|
||||
$replace = array(
|
||||
'{AUTOINCREMENT}' => 'SERIAL',
|
||||
'{PRIMARY}' => 'primary key',
|
||||
'{UNSIGNED}' => '',
|
||||
'{FULLTEXT}' => '',
|
||||
'{BOOLEAN}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '', # UTF-8 is simply ignored.
|
||||
'{LATIN1}' => '', # same for latin1
|
||||
'{IF_NOT_EXISTS}' => '', # does not work with PgSQL
|
||||
'{RENAME_COLUMN}' => 'ALTER COLUMN', # PgSQL : ALTER TABLE x RENAME x TO y
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => '',
|
||||
'{INT}' => 'integer NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
|
||||
'int(1)' => 'int',
|
||||
'int(10)' => 'int',
|
||||
'int(11)' => 'int',
|
||||
'int(4)' => 'int',
|
||||
'{DATETIME}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
|
||||
'{DATE}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
|
||||
'{DATEFUTURE}' => "timestamp with time zone default '2038-01-18'", # stay in sync with MySQL
|
||||
'{DATECURRENT}' => 'timestamp with time zone default now()',
|
||||
'{COLLATE}' => '',
|
||||
'{AUTOINCREMENT}' => 'SERIAL',
|
||||
'{PRIMARY}' => 'primary key',
|
||||
'{UNSIGNED}' => '',
|
||||
'{FULLTEXT}' => '',
|
||||
'{BOOLEAN}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(false) . "'",
|
||||
'{BOOLEAN_TRUE}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(true) . "'",
|
||||
'{UTF-8}' => '', # UTF-8 is simply ignored.
|
||||
'{LATIN1}' => '', # same for latin1
|
||||
'{IF_NOT_EXISTS}' => '', # does not work with PgSQL
|
||||
'{RENAME_COLUMN}' => 'ALTER COLUMN', # PgSQL : ALTER TABLE x RENAME x TO y
|
||||
'{MYISAM}' => '',
|
||||
'{INNODB}' => '',
|
||||
'{INT}' => 'integer NOT NULL DEFAULT 0',
|
||||
'{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
|
||||
'int(1)' => 'int',
|
||||
'int(10)' => 'int',
|
||||
'int(11)' => 'int',
|
||||
'int(4)' => 'int',
|
||||
'{DATETIME}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
|
||||
'{DATE}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
|
||||
'{DATEFUTURE}' => "timestamp with time zone default '2038-01-18'", # stay in sync with MySQL
|
||||
'{DATECURRENT}' => 'timestamp with time zone default now()',
|
||||
'{COLLATE}' => '',
|
||||
);
|
||||
} else {
|
||||
echo_out("Sorry, unsupported database type " . $CONF['database_type']);
|
||||
@@ -349,6 +366,7 @@ function db_query_parsed($sql, $ignore_errors = 0, $attach_mysql = "")
|
||||
throw new \Exception("Postfixadmin DB update failed. Please check your PHP error_log");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param string $index
|
||||
@@ -370,10 +388,10 @@ function _drop_index($table, $index)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @param string $table
|
||||
* @param string $indexname
|
||||
* @param string $fieldlist
|
||||
* @return string
|
||||
*/
|
||||
function _add_index($table, $indexname, $fieldlist)
|
||||
{
|
||||
@@ -825,7 +843,6 @@ function upgrade_4_pgsql()
|
||||
# NO MySQL errors should be ignored below this line!
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* create tables
|
||||
* version: Sourceforge SVN r1 of DATABASE_MYSQL.txt
|
||||
@@ -1197,14 +1214,14 @@ function upgrade_439_pgsql()
|
||||
*/
|
||||
function upgrade_473_mysql()
|
||||
{
|
||||
$table_admin = table_by_key('admin');
|
||||
$table_alias = table_by_key('alias');
|
||||
$table_al_dom = table_by_key('alias_domain');
|
||||
$table_domain = table_by_key('domain');
|
||||
$table_admin = table_by_key('admin');
|
||||
$table_alias = table_by_key('alias');
|
||||
$table_al_dom = table_by_key('alias_domain');
|
||||
$table_domain = table_by_key('domain');
|
||||
$table_dom_adm = table_by_key('domain_admins');
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
$table_mailbox = table_by_key('mailbox');
|
||||
$table_log = table_by_key('log');
|
||||
$table_log = table_by_key('log');
|
||||
|
||||
# tables were created without explicit charset before :-(
|
||||
$all_sql = explode("\n", trim("
|
||||
@@ -1257,7 +1274,7 @@ function upgrade_473_mysql()
|
||||
function upgrade_479_mysql()
|
||||
{
|
||||
# ssl is a reserved word in MySQL and causes several problems. Renaming the field...
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
if (!_mysql_field_exists($table_fmail, 'usessl')) {
|
||||
db_query_parsed("ALTER TABLE $table_fmail CHANGE `ssl` `usessl` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");
|
||||
}
|
||||
@@ -1269,7 +1286,7 @@ function upgrade_479_mysql()
|
||||
*/
|
||||
function upgrade_479_pgsql()
|
||||
{
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
$table_fmail = table_by_key('fetchmail');
|
||||
if (!_pgsql_field_exists($table_fmail, 'usessl')) {
|
||||
db_query_parsed("alter table $table_fmail rename column ssl to usessl");
|
||||
}
|
||||
@@ -1280,7 +1297,7 @@ function upgrade_479_pgsql()
|
||||
*/
|
||||
function upgrade_483_mysql()
|
||||
{
|
||||
$table_log = table_by_key('log');
|
||||
$table_log = table_by_key('log');
|
||||
db_query_parsed("ALTER TABLE $table_log CHANGE `data` `data` TEXT {LATIN1} NOT NULL");
|
||||
}
|
||||
|
||||
@@ -1299,6 +1316,7 @@ function upgrade_495_pgsql()
|
||||
db_query_parsed("ALTER TABLE $table_mailbox alter column local_part SET NOT NULL");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* See https://sourceforge.net/forum/message.php?msg_id=5394663
|
||||
* @return void
|
||||
@@ -2004,7 +2022,7 @@ function upgrade_1842()
|
||||
db_execute("UPDATE $domain SET created='2000-01-01 00:00:00', modified='2000-01-01 00:00:00' WHERE domain='ALL'", [], true);
|
||||
}
|
||||
_db_add_field('mailbox', 'password_expiry', "{DATETIME}"); // when a specific mailbox password expires
|
||||
_db_add_field('domain', 'password_expiry', 'int DEFAULT 0'); // expiry applied to mailboxes within that domain
|
||||
_db_add_field('domain', 'password_expiry', 'int DEFAULT 0'); // expiry applied to mailboxes within that domain
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2140,12 +2158,25 @@ function upgrade_1847()
|
||||
// no op, function exists in the postfixadmin_3.3 branch doing some collation fixes.
|
||||
}
|
||||
|
||||
function move_core_tables_to_innodb()
|
||||
{
|
||||
$tables = ['admin', 'alias_domain', 'alias', 'config', 'domain_admins', 'domain', 'fetchmail', 'log', 'mailbox', 'quota', 'quota2'];
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$table_to_modify = table_by_key($table);
|
||||
db_query("ALTER TABLE $table_to_modify Engine=InnoDB");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add DKIM tables
|
||||
* @return void
|
||||
*/
|
||||
function upgrade_1848_mysql()
|
||||
{
|
||||
|
||||
move_core_tables_to_innodb();
|
||||
|
||||
$dkim_key_table = table_by_key('dkim');
|
||||
$dkim_signing_table = table_by_key('dkim_signing');
|
||||
$domain_table = table_by_key('domain');
|
||||
@@ -2270,7 +2301,7 @@ function upgrade_1848_sqlite()
|
||||
function upgrade_1849_mysql()
|
||||
{
|
||||
_db_add_field('mailbox', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'password_expiry');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
|
||||
$totp_exception_table = table_by_key('totp_exception_address');
|
||||
db_query_parsed("
|
||||
@@ -2293,6 +2324,7 @@ function upgrade_1849_mysql()
|
||||
)
|
||||
");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add TOTP fields
|
||||
* @return void
|
||||
@@ -2300,11 +2332,12 @@ function upgrade_1849_mysql()
|
||||
function upgrade_1849_pgsql()
|
||||
{
|
||||
_db_add_field('mailbox', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'password_expiry');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
|
||||
if (!_pgsql_object_exists('totp_exception_address')) {
|
||||
$totp_exception_address = table_by_key("totp_exception_address");
|
||||
if (!_pgsql_object_exists($totp_exception_address)) {
|
||||
db_query_parsed("
|
||||
CREATE TABLE {IF_NOT_EXISTS} totp_exception_address (
|
||||
CREATE TABLE {IF_NOT_EXISTS} $totp_exception_address (
|
||||
id {AUTOINCREMENT} {PRIMARY},
|
||||
ip varchar(46) NOT NULL,
|
||||
username varchar(255) DEFAULT NULL,
|
||||
@@ -2312,12 +2345,14 @@ function upgrade_1849_pgsql()
|
||||
);
|
||||
");
|
||||
db_query_parsed("
|
||||
CREATE UNIQUE INDEX ip_user ON totp_exception_address (ip,username)
|
||||
CREATE UNIQUE INDEX ip_user ON $totp_exception_address (ip,username)
|
||||
");
|
||||
}
|
||||
if (!_pgsql_object_exists('mailbox_app_password')) {
|
||||
|
||||
$mailbox_app_password = table_by_key("mailbox_app_password");
|
||||
if (!_pgsql_object_exists($mailbox_app_password)) {
|
||||
db_query_parsed("
|
||||
CREATE TABLE {IF_NOT_EXISTS} mailbox_app_password(
|
||||
CREATE TABLE {IF_NOT_EXISTS} $mailbox_app_password (
|
||||
id {AUTOINCREMENT} {PRIMARY},
|
||||
username varchar(255) DEFAULT NULL,
|
||||
description varchar(255) DEFAULT NULL,
|
||||
@@ -2330,20 +2365,26 @@ function upgrade_1849_pgsql()
|
||||
function upgrade_1849_sqlite()
|
||||
{
|
||||
_db_add_field('mailbox', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'password_expiry');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
_db_add_field('admin', 'totp_secret', "VARCHAR(255) {UTF-8} DEFAULT NULL", 'token_validity');
|
||||
|
||||
$totp_exception_address = table_by_key("totp_exception_address");
|
||||
|
||||
db_query_parsed("
|
||||
CREATE TABLE {IF_NOT_EXISTS} totp_exception_address (
|
||||
CREATE TABLE {IF_NOT_EXISTS} $totp_exception_address (
|
||||
id {AUTOINCREMENT},
|
||||
ip varchar(46) NOT NULL,
|
||||
username varchar(255) DEFAULT NULL,
|
||||
description varchar(255) DEFAULT NULL
|
||||
);
|
||||
");
|
||||
|
||||
db_query_parsed("
|
||||
CREATE UNIQUE INDEX ip_user ON totp_exception_address (ip,username)
|
||||
CREATE UNIQUE INDEX ip_user ON $totp_exception_address (ip,username)
|
||||
");
|
||||
|
||||
$mailbox_app_password = table_by_key("mailbox_app_password");
|
||||
db_query_parsed("
|
||||
CREATE TABLE {IF_NOT_EXISTS} mailbox_app_password(
|
||||
CREATE TABLE {IF_NOT_EXISTS} $mailbox_app_password (
|
||||
id {AUTOINCREMENT},
|
||||
username varchar(255) DEFAULT NULL,
|
||||
description varchar(255) DEFAULT NULL,
|
||||
@@ -2355,5 +2396,5 @@ function upgrade_1849_sqlite()
|
||||
function upgrade_1850()
|
||||
{
|
||||
// see also: https://github.com/postfixadmin/postfixadmin/issues/891
|
||||
_db_add_field('mailbox', 'smtp_active', '{BOOLEAN_TRUE}');
|
||||
_db_add_field('mailbox', 'smtp_active', '{BOOLEAN_TRUE}');
|
||||
}
|
||||
Reference in new issue
Block a user