functions.inc.php:

- new function db_pgsql() to replace lots of
  "if ($CONF[database_type] == 'pgsql')) checks
- delete unused function boolconf()

several files:
- use db_pgsql() instead of checking $CONF[database_type]



git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1582 a1433add-5e2c-0410-b055-b7f2511e0802
pull/2/head
Christian Boltz 11 years ago
parent b11ee57e0e
commit 20d1ffcafc

@ -27,7 +27,7 @@ authentication_require_role('global-admin');
(($CONF['backup'] == 'NO') ? header("Location: main.php") && exit : '1');
// TODO: make backup supported for postgres
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
flash_error('Sorry: Backup is currently not supported for your DBMS ('.$CONF['database_type'].').');
$smarty->assign ('smarty_template', 'message');
$smarty->display ('index.tpl');

@ -166,7 +166,7 @@ if ($cancel) { # cancel $new or $edit
}
}
$formvars['id'] = $edit; # results in 0 on $new
if($CONF['database_type'] == 'pgsql' && $new) {
if(db_pgsql() && $new) {
// skip - shouldn't need to specify this as it will default to the next available value anyway.
unset($formvars['id']);
}
@ -213,7 +213,7 @@ if ($cancel) { # cancel $new or $edit
} elseif ($edit) { # edit entry form
$formvars = $edit_row;
$formvars['src_password'] = '';
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
$formvars['fetchall']=('t'==$formvars['fetchall']) ? 1 : 0;
$formvars['keep']=('t'==$formvars['keep']) ? 1 : 0;
$formvars['usessl']=('t'==$formvars['usessl']) ? 1 : 0;
@ -235,7 +235,7 @@ if ($edit + $new == 0) { # display list
$res = db_query ("SELECT ".implode(",",escape_string(array_keys($fm_struct)))." FROM $table_fetchmail WHERE mailbox IN ($user_mailboxes_sql) ORDER BY mailbox,src_server,src_user");
if ($res['rows'] > 0) {
while ($row = db_array ($res['result'])) {
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
//. at least in my database, $row['modified'] already looks like : 2009-04-11 21:38:10.75586+01,
// while gmstrftime expects an integer value. strtotime seems happy though.
//$row['date']=gmstrftime('%c %Z',$row['date']);

@ -289,7 +289,7 @@ function escape_string ($string) {
if ($CONF['database_type'] == "mysqli") {
$escaped_string = mysqli_real_escape_string($link, $string);
}
if ($CONF['database_type'] == "pgsql") {
if (db_pgsql()) {
// php 5.2+ allows for $link to be specified.
if (version_compare(phpversion(), "5.2.0", ">=")) {
$escaped_string = pg_escape_string($link, $string);
@ -449,7 +449,7 @@ function create_page_browser($idxfield, $querypart) {
# init row counter
$initcount = "SET @row=-1";
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
$initcount = "CREATE TEMPORARY SEQUENCE rowcount MINVALUE 0";
}
$result = db_query($initcount);
@ -462,7 +462,7 @@ function create_page_browser($idxfield, $querypart) {
) idx WHERE MOD(idx.row, $page_size) IN (0,$page_size_zerobase) OR idx.row = $count_results
";
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
$query = "
SELECT * FROM (
SELECT $idxfield AS label, nextval('rowcount') AS row $querypart
@ -493,7 +493,7 @@ function create_page_browser($idxfield, $querypart) {
}
}
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
db_query ("DROP SEQUENCE rowcount");
}
@ -1233,7 +1233,7 @@ function db_connect ($ignore_errors = 0) {
} else {
$error_text .= "<p />DEBUG INFORMATION:<br />MySQL 4.1 functions not available! (php5-mysqli installed?)<br />database_type = 'mysqli' in config.inc.php, are you using a different database? $DEBUG_TEXT";
}
} elseif ($CONF['database_type'] == "pgsql") {
} elseif (db_pgsql()) {
if (function_exists ("pg_pconnect")) {
if(!isset($CONF['database_port'])) {
$CONF['database_port'] = '5432';
@ -1276,14 +1276,13 @@ function db_get_boolean($bool) {
die("Invalid usage of 'db_get_boolean($bool)'");
}
global $CONF;
if($CONF['database_type']=='pgsql') {
if(db_pgsql()) {
// return either true or false (unquoted strings)
if($bool) {
return 't';
}
return 'f';
} elseif($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli') {
} elseif(Config::Read('database_type') == 'mysql' || Config::Read('database_type') == 'mysqli') {
if($bool) {
return 1;
}
@ -1293,6 +1292,17 @@ function db_get_boolean($bool) {
}
}
/**
* returns true if PostgreSQL is used, false otherwise
*/
function db_pgsql() {
if(Config::Read('database_type')=='pgsql') {
return true;
} else {
return false;
}
}
//
// db_query
// Action: Sends a query to the database and returns query result and number of rows
@ -1315,7 +1325,7 @@ function db_query ($query, $ignore_errors = 0) {
or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query ($query) : " . mysql_error($link) . "$DEBUG_TEXT";
if ($CONF['database_type'] == "mysqli") $result = @mysqli_query ($link, $query)
or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query ($query) : " . mysqli_error($link) . "$DEBUG_TEXT";
if ($CONF['database_type'] == "pgsql") {
if (db_pgsql()) {
$result = @pg_query ($link, $query)
or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query ($query): " . pg_last_error() . "$DEBUG_TEXT";
}
@ -1326,13 +1336,13 @@ function db_query ($query, $ignore_errors = 0) {
// if $query was a SELECT statement check the number of rows with [database_type]_num_rows ().
if ($CONF['database_type'] == "mysql") $number_rows = mysql_num_rows ($result);
if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_num_rows ($result);
if ($CONF['database_type'] == "pgsql") $number_rows = pg_num_rows ($result);
if (db_pgsql() ) $number_rows = pg_num_rows ($result);
} else {
// if $query was something else, UPDATE, DELETE or INSERT check the number of rows with
// [database_type]_affected_rows ().
if ($CONF['database_type'] == "mysql") $number_rows = mysql_affected_rows ($link);
if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_affected_rows ($link);
if ($CONF['database_type'] == "pgsql") $number_rows = pg_affected_rows ($result);
if (db_pgsql() ) $number_rows = pg_affected_rows ($result);
}
}
@ -1355,7 +1365,7 @@ function db_row ($result) {
$row = "";
if ($CONF['database_type'] == "mysql") $row = mysql_fetch_row ($result);
if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_row ($result);
if ($CONF['database_type'] == "pgsql") $row = pg_fetch_row ($result);
if (db_pgsql() ) $row = pg_fetch_row ($result);
return $row;
}
@ -1370,7 +1380,7 @@ function db_array ($result) {
$row = "";
if ($CONF['database_type'] == "mysql") $row = mysql_fetch_array ($result);
if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_array ($result);
if ($CONF['database_type'] == "pgsql") $row = pg_fetch_array ($result);
if (db_pgsql() ) $row = pg_fetch_array ($result);
return $row;
}
@ -1385,7 +1395,7 @@ function db_assoc ($result) {
$row = "";
if ($CONF['database_type'] == "mysql") $row = mysql_fetch_assoc ($result);
if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_assoc ($result);
if ($CONF['database_type'] == "pgsql") $row = pg_fetch_assoc ($result);
if (db_pgsql() ) $row = pg_fetch_assoc ($result);
return $row;
}
@ -1485,25 +1495,19 @@ function db_update_q ($table, $where, $values, $timestamp = array('modified') )
* Call: db_begin()
*/
function db_begin () {
global $CONF;
# if ('pgsql'== Config::read('database_type')) {
if ('pgsql'== $CONF['database_type']) {
if (db_pgsql()) { # TODO: also enable for mysql? (not supported by MyISAM, which is used for most tables)
db_query('BEGIN');
}
}
function db_commit () {
global $CONF;
# if ('pgsql'== Config::read('database_type')) {
if ('pgsql'== $CONF['database_type']) {
if (db_pgsql()) {
db_query('COMMIT');
}
}
function db_rollback () {
global $CONF;
# if ('pgsql'== Config::read('database_type')) {
if ('pgsql'== $CONF['database_type']) {
if (db_pgsql()) {
db_query('ROLLBACK');
}
}
@ -1739,19 +1743,6 @@ function getRemoteAddr() {
/*
Convert $CONF['whatever'] to boolean
(obviously only useful for settings that can be YES or NO)
Returns: TRUE (on YES/yes) or FALSE (on NO/no/not set/unknown value)
Note: boolconf() is deprecated - please use Config::bool() instead
*/
function boolconf($setting) {
return Config::bool($setting);
}
#$table_admin = table_by_key ('admin');
$table_alias = table_by_key ('alias');
#$table_alias_domain = table_by_key ('alias_domain');

@ -223,7 +223,7 @@ if ($result['rows'] > 0) {
}
}
}
if ('pgsql'==$CONF['database_type']) {
if (db_pgsql()) {
// XXX
$row['modified'] = date('Y-m-d H:i', strtotime($row['modified']));
$row['created'] = date('Y-m-d H:i', strtotime($row['created']));

@ -29,7 +29,7 @@ class AdminHandler extends PFAHandler {
# NOTE: (Disabling both shouldn't be a problem.)
# TODO: move to a db_group_concat() function?
if (Config::read('database_type') == 'pgsql') {
if (db_pgsql()) {
$domains_grouped = "array_to_string(array_agg(domain), ',')";
} else { # mysql
$domains_grouped = 'group_concat(domain)';

@ -413,7 +413,7 @@ abstract class PFAHandler {
$yes = escape_string(Config::lang('YES'));
$no = escape_string(Config::lang('NO'));
if (Config::read('database_type') == 'pgsql') {
if (db_pgsql()) {
$formatted_date = "TO_DATE(text(###KEY###), '" . escape_string(Config::Lang('dateformat_pgsql')) . "')";
} else {
$formatted_date = "DATE_FORMAT(###KEY###, '" . escape_string(Config::Lang('dateformat_mysql')) . "')";

@ -57,8 +57,7 @@ $tLog = array();
if ($error != 1)
{
$query = "SELECT timestamp,username,domain,action,data FROM $table_log WHERE domain='$fDomain' ORDER BY timestamp DESC LIMIT 10";
if ('pgsql'==$CONF['database_type'])
{
if (db_pgsql()) {
$query = "SELECT extract(epoch from timestamp) as timestamp,username,domain,action,data FROM $table_log WHERE domain='$fDomain' ORDER BY timestamp DESC LIMIT 10";
}
$result=db_query($query);
@ -66,8 +65,7 @@ if ($error != 1)
{
while ($row = db_array ($result['result']))
{
if ('pgsql'==$CONF['database_type'])
{
if (db_pgsql()) {
$row['timestamp']=gmstrftime('%c %Z',$row['timestamp']);
}
$tLog[] = $row;

Loading…
Cancel
Save