diff --git a/functions.inc.php b/functions.inc.php index dc69c727..9bc11881 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -571,17 +571,14 @@ function divide_quota($quota) /** * Checks if the admin is the owner of the domain (or global-admin) - * @param string $username - * @param string $domain - * @return bool */ -function check_owner($username, $domain) +function check_owner(string $username, string $domain): bool { $table_domain_admins = table_by_key('domain_admins'); $result = db_query_all( "SELECT 1 FROM $table_domain_admins WHERE username= ? AND (domain = ? OR domain = 'ALL') AND active = ?", - array($username, $domain, db_get_boolean(true)) + array($username, $domain, true) ); if (sizeof($result) == 1 || sizeof($result) == 2) { # "ALL" + specific domain permissions is possible @@ -613,23 +610,22 @@ function list_domains_for_admin(string $username): array $query = "SELECT $table_domain.domain FROM $table_domain "; $condition[] = "$table_domain.domain != 'ALL'"; - $result = db_query_one("SELECT username FROM $table_domain_admins WHERE username= :username AND domain='ALL'", ['username' => $username]); + $result = db_query_one("SELECT username FROM $table_domain_admins WHERE username = :username AND domain = 'ALL'", ['username' => $username]); if (empty($result)) { # not a superadmin $pvalues['username'] = $username; - $pvalues['active'] = db_get_boolean(true); - $pvalues['backupmx'] = db_get_boolean(false); + $pvalues['active'] = true; + $pvalues['backupmx'] = false; $query .= " LEFT JOIN $table_domain_admins ON $table_domain.domain=$table_domain_admins.domain "; $condition[] = "$table_domain_admins.username = :username "; $condition[] = "$table_domain.active = :active "; # TODO: does it really make sense to exclude inactive... - $condition[] = "$table_domain.backupmx = :backupmx"; # TODO: ... and backupmx domains for non-superadmins? + $condition[] = "$table_domain.backupmx = :backupmx "; # TODO: ... and backupmx domains for non-superadmins? } $query .= " WHERE " . join(' AND ', $condition); $query .= " ORDER BY $table_domain.domain"; $result = db_query_all($query, $pvalues); - return array_column($result, 'domain'); } @@ -1721,6 +1717,15 @@ function db_execute(string $sql, array $values = [], bool $throw_exceptions = fa try { $stmt = $link->prepare($sql); + + // PDO and PostgreSQL either require us to explicitly bind values as bools, or it seems to work + // if we pass in 0/1 .... 0/1 is easier? + foreach ($values as $key => $value) { + if (is_bool($value)) { + $values[$key] = (int)$value; + } + } + $stmt->execute($values); } catch (PDOException $e) { $error_text = "Invalid query: " . $e->getMessage() . " caused by " . $sql . ' ' . json_encode($values); @@ -1749,8 +1754,17 @@ function db_query(string $sql, array $values = array(), bool $ignore_errors = fa $stmt = null; try { $stmt = $link->prepare($sql); + + // PDO and PostgreSQL either require us to explicitly bind values as bools, or it seems to work + // if we pass in 0/1 .... 0/1 is easier? + foreach ($values as $key => $value) { + if (is_bool($value)) { + $values[$key] = (int)$value; + } + } $stmt->execute($values); } catch (PDOException $e) { + $error_text = "Invalid query: " . $e->getMessage() . " caused by " . $sql; error_log($error_text); if (defined('PHPUNIT_TEST')) { @@ -2149,7 +2163,7 @@ function gen_show_status($show_alias) // Vacation CHECK if (array_key_exists('show_vacation', $CONF) && $CONF['show_vacation'] == 'YES') { - $stat_result = db_query_one("SELECT * FROM " . table_by_key('vacation') . " WHERE email = ? AND active = ? ", array($show_alias, db_get_boolean(true))); + $stat_result = db_query_one("SELECT * FROM " . table_by_key('vacation') . " WHERE email = ? AND active = ? ", array($show_alias, true)); if (!empty($stat_result)) { $stat_string .= "" . $CONF['show_status_text'] . " "; } else { @@ -2161,7 +2175,7 @@ function gen_show_status($show_alias) if (array_key_exists('show_disabled', $CONF) && $CONF['show_disabled'] == 'YES') { $stat_result = db_query_one( "SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND active = ?", - array($show_alias, db_get_boolean(false)) + array($show_alias, false) ); if (!empty($stat_result)) { $stat_string .= "" . $CONF['show_status_text'] . " "; @@ -2177,7 +2191,7 @@ function gen_show_status($show_alias) $now = "datetime('now')"; } - $stat_result = db_query_one("SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND password_expiry <= $now AND active = ?", array($show_alias, db_get_boolean(true))); + $stat_result = db_query_one("SELECT * FROM " . table_by_key('mailbox') . " WHERE username = ? AND password_expiry <= $now AND active = ?", array($show_alias, true)); if (!empty($stat_result)) { $stat_string .= "" . $CONF['show_status_text'] . " "; diff --git a/model/Login.php b/model/Login.php index 4c868dd4..e75d324e 100644 --- a/model/Login.php +++ b/model/Login.php @@ -24,7 +24,7 @@ class Login { $query = "SELECT password FROM {$this->key_table} WHERE username = :username AND active = :active"; - $values = ['username' => $username, 'active' => db_get_boolean(true)]; + $values = ['username' => $username, 'active' => true]; $result = db_query_all($query, $values); @@ -62,7 +62,7 @@ class Login { $sql = "SELECT count(1) FROM {$this->key_table} WHERE username = :username AND active = :active"; - $active = db_get_boolean(true); + $active = true; $values = [ 'username' => $username, @@ -97,7 +97,7 @@ class Login $values = [ 'username' => $username, - 'active' => db_get_boolean(true), + 'active' => true, ]; // Fetch the domain diff --git a/model/PFAHandler.php b/model/PFAHandler.php index be3ebded..9129bc85 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -565,8 +565,7 @@ abstract class PFAHandler foreach ($db_values as $key => $val) { switch ($this->struct[$key]['type']) { # modify field content for some types case 'bool': - $val = (string)$val; - $db_values[$key] = db_get_boolean($val); + $db_values[$key] = (bool) $val; break; case 'pass': $val = (string)$val; @@ -876,7 +875,7 @@ abstract class PFAHandler public function checkPasswordRecoveryCode($username, $token) { $table = table_by_key($this->db_table); - $active = db_get_boolean(true); + $active = true; $now = date('Y-m-d H:i:s'); diff --git a/model/TotpPf.php b/model/TotpPf.php index 286f5a3c..278bd9f1 100644 --- a/model/TotpPf.php +++ b/model/TotpPf.php @@ -121,7 +121,7 @@ class TotpPf $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); + $active = true; $values = [ 'username' => $username, @@ -141,7 +141,7 @@ class TotpPf $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); + $active = true; $values = [ 'username' => $username, @@ -193,7 +193,7 @@ class TotpPf $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); + $active = true; $values = [ 'username' => $username, diff --git a/model/VacationHandler.php b/model/VacationHandler.php index 8a8a8ef1..8b3361a1 100644 --- a/model/VacationHandler.php +++ b/model/VacationHandler.php @@ -134,7 +134,7 @@ class VacationHandler extends PFAHandler // tidy up vacation table. $vacation_data = array( - 'active' => db_get_boolean(false), + 'active' => false, ); db_update('vacation', 'email', $this->username, $vacation_data); @@ -206,12 +206,12 @@ class VacationHandler extends PFAHandler if (!is_array($row)) { return false; } - $boolean = ($row['active'] == db_get_boolean(true)); + # TODO: only return true and store the db result array in $this->whatever for consistency with the other classes return array( 'subject' => isset($row['subject']) ? $row['subject'] : null, 'body' => isset($row['body']) ? $row['body'] : null, - 'active' => $boolean, + 'active' => (bool) $row['active'], 'interval_time' => isset($row['interval_time']) ? $row['interval_time'] : null, 'activeFrom' => isset($row['activefrom']) ? $row['activefrom'] : null, 'activeUntil' => isset($row['activeuntil']) ? $row['activeuntil'] : null @@ -235,7 +235,7 @@ class VacationHandler extends PFAHandler 'subject' => $subject, 'body' => $body, 'interval_time' => $interval_time, - 'active' => db_get_boolean(true), + 'active' => true, // MySQL 8.x supports a timezone (Y-m-d H:i:sP), but MariaDB doesn't seem to? I'm sure PostgreSQL will. 'activefrom' => $activeFrom->format('Y-m-d H:i:s'), 'activeuntil' => $activeUntil->format('Y-m-d H:i:s'), diff --git a/public/broadcast-message.php b/public/broadcast-message.php index 3a031157..5e90a011 100644 --- a/public/broadcast-message.php +++ b/public/broadcast-message.php @@ -60,14 +60,14 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $table_mailbox = table_by_key('mailbox'); $table_alias = table_by_key('alias'); - $params = []; - $q = "SELECT username from $table_mailbox WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params); + $params = ['active' => true]; + $q = "SELECT username from $table_mailbox WHERE active = :active AND " . db_in_clause("domain", $wanted_domains, $params); $result = db_query_all($q, $params); $recipients = array_column($result, 'username'); if (intval(safepost('mailboxes_only')) == 0) { - $params2 = []; - $q2 = "SELECT goto FROM $table_alias WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params2); + $params2 = ['active' => true]; + $q2 = "SELECT goto FROM $table_alias WHERE active= :active AND " . db_in_clause("domain", $wanted_domains, $params2); $result2 = db_query_all($q2, $params2); $recipients = array_merge($recipients, array_column($result2, 'goto')); } diff --git a/public/setup.php b/public/setup.php index dbcf82a7..465c9a2b 100644 --- a/public/setup.php +++ b/public/setup.php @@ -397,8 +397,8 @@ if ($db) { } $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'"); + + $admins = db_query_all("SELECT * FROM $table_admin WHERE superadmin = :admin AND active = :active", ['admin' => true, 'active' => true]); if (!empty($admins)) { ?> diff --git a/public/upgrade.php b/public/upgrade.php index ae59460a..a65bb092 100644 --- a/public/upgrade.php +++ b/public/upgrade.php @@ -1350,7 +1350,7 @@ function upgrade_1284_mysql_pgsql() foreach ($result as $row) { printdebug("Setting superadmin flag for " . $row['username']); - db_update('admin', 'username', $row['username'], array('superadmin' => db_get_boolean(true))); + db_update('admin', 'username', $row['username'], array('superadmin' => true)); } } diff --git a/scripts/examples/vacation-cron.php b/scripts/examples/vacation-cron.php index 5a63825a..db297475 100644 --- a/scripts/examples/vacation-cron.php +++ b/scripts/examples/vacation-cron.php @@ -22,7 +22,7 @@ define('POSTFIXADMIN_CLI', 1); $table_vacation = table_by_key('vacation'); -$active = db_get_boolean(1); +$active = true; $vacations_that_need_deactivating = db_query_all("SELECT * FROM $table_vacation WHERE activeuntil <= NOW() AND active = :active ", ['active' => $active]); diff --git a/tests/AliasHandlerTest.php b/tests/AliasHandlerTest.php index 71f304d3..3dd2bcf3 100644 --- a/tests/AliasHandlerTest.php +++ b/tests/AliasHandlerTest.php @@ -48,14 +48,6 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase $x = new AliasHandler(1, 'admin', true); - $values = [ - 'localpart' => 'david.test', - 'domain' => 'example.com', - 'active' => 1, - 'address' => 'david.test@example.com', - 'goto' => ['dest@example.com'] - ]; - try { $r = $x->init('david.test@example.com'); $this->fail("Should not see this - example.com is not present"); @@ -101,6 +93,7 @@ class AliasHandlerTest extends \PHPUnit\Framework\TestCase $ret = $dh->save(); + $this->assertTrue($ret); // Need to add 'admin' as a domain_admin diff --git a/tests/TotpPfTest.php b/tests/TotpPfTest.php index 38fbab90..0f639e6c 100644 --- a/tests/TotpPfTest.php +++ b/tests/TotpPfTest.php @@ -22,7 +22,7 @@ class TotpPfTest extends TestCase 'maildir' => '/foo/bar', 'local_part' => 'test', 'domain' => 'example.com', - 'active' => db_get_boolean(true), + 'active' => true, ]); parent::setUp(); // TODO: Change the autogenerated stub @@ -97,8 +97,8 @@ class TotpPfTest extends TestCase [ 'username' => 'admin@example.com', 'password' => pacrypt('adminpass'), - 'superadmin' => db_get_boolean(true), - 'active' => db_get_boolean(true), + 'superadmin' => true, + 'active' => true, ] ); db_execute( @@ -106,7 +106,7 @@ class TotpPfTest extends TestCase [ 'username' => 'admin@example.com', 'domain' => 'example.com', - 'active' => db_get_boolean(true), + 'active' => true, ] ); @@ -136,8 +136,8 @@ class TotpPfTest extends TestCase [ 'username' => 'admin@example.com', 'password' => pacrypt('adminpass'), - 'superadmin' => db_get_boolean(false), - 'active' => db_get_boolean(true), + 'superadmin' => false, + 'active' => true, ] ); db_execute( @@ -145,7 +145,7 @@ class TotpPfTest extends TestCase [ 'username' => 'admin@example.com', 'domain' => 'example.com', - 'active' => db_get_boolean(true), + 'active' => true, ] ); @@ -173,7 +173,7 @@ class TotpPfTest extends TestCase 'maildir' => '/foo/bar2', 'local_part' => 'test2', 'domain' => 'example.com', - 'active' => db_get_boolean(true), + 'active' => true, ]); db_execute('INSERT INTO mailbox_app_password (username, description, password_hash) VALUES (:username, :description, :password_hash)', ['username' => 'test@example.com', 'description' => 'test foobar', 'password_hash' => pacrypt('foobar-app')]); @@ -218,7 +218,7 @@ EOF; 'username' => $username, 'password_hash' => pacrypt($plain_password), 'client_ip' => $ip, - 'active' => db_get_boolean(true), + 'active' => true, ]); if (count($rows) != 1) {