From 99a6c2228990756c0985a3e877cbef5c569fda27 Mon Sep 17 00:00:00 2001 From: simond Date: Fri, 1 Mar 2002 09:01:35 +0000 Subject: [PATCH] Add support for renaming db prefs fieldnames (this doesn't add postgres support, but that's on it's way next git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@2525 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- config/conf.pl | 76 ++++++++++++++++++++++++++++--- functions/db_prefs.php | 51 ++++++++++++++++----- plugins/administrator/defines.php | 12 +++++ 3 files changed, 122 insertions(+), 17 deletions(-) diff --git a/config/conf.pl b/config/conf.pl index e4348494..223a0bb4 100755 --- a/config/conf.pl +++ b/config/conf.pl @@ -284,6 +284,15 @@ if ( !$edit_identity ) { if ( !$edit_name ) { $edit_name = "true"; } +if ( !$prefs_user_field ) { + $prefs_user_field = 'user'; +} +if ( !$prefs_key_field ) { + $prefs_key_field = 'prefkey'; +} +if ( !$prefs_val_field ) { + $prefs_val_field = 'prefval'; +} if ( $ARGV[0] eq '--install-plugin' ) { print "Activating plugin " . $ARGV[1] . "\n"; @@ -475,8 +484,12 @@ while ( ( $command ne "q" ) && ( $command ne "Q" ) ) { print $WHT. "Database\n" . $NRM; print "1. DSN for Address Book : $WHT$addrbook_dsn$NRM\n"; print "2. Table for Address Book : $WHT$addrbook_table$NRM\n"; + print "\n"; print "3. DSN for Preferences : $WHT$prefs_dsn$NRM\n"; print "4. Table for Preferences : $WHT$prefs_table$NRM\n"; + print "5. Field for username : $WHT$prefs_user_field$NRM\n"; + print "6. Field for prefs key : $WHT$prefs_key_field$NRM\n"; + print "7. Field for prefs value : $WHT$prefs_val_field$NRM\n"; print "\n"; print "S Save data\n"; print "R Return to Main Menu\n"; @@ -590,10 +603,13 @@ while ( ( $command ne "q" ) && ( $command ne "Q" ) ) { if ( $command =~ /^[0-9]+/ ) { @plugins = command81(); } elsif ( $command eq "a" ) { command8s(); } } elsif ( $menu == 9 ) { - if ( $command == 1 ) { $addrbook_dsn = command91(); } - elsif ( $command == 2 ) { $addrbook_table = command92(); } - elsif ( $command == 3 ) { $prefs_dsn = command93(); } - elsif ( $command == 4 ) { $prefs_table = command94(); } + if ( $command == 1 ) { $addrbook_dsn = command91(); } + elsif ( $command == 2 ) { $addrbook_table = command92(); } + elsif ( $command == 3 ) { $prefs_dsn = command93(); } + elsif ( $command == 4 ) { $prefs_table = command94(); } + elsif ( $command == 5 ) { $prefs_user_field = command95(); } + elsif ( $command == 6 ) { $prefs_key_field = command96(); } + elsif ( $command == 7 ) { $prefs_val_field = command97(); } } } } @@ -2002,7 +2018,7 @@ sub command93 { sub command94 { print "This is the name of the table you want to store the preferences\n"; - print "data in, it defaults to 'address'\n"; + print "data in, it defaults to 'userprefs'\n"; print "\n"; print "[$WHT$prefs_table$NRM]: $WHT"; $new_table = ; @@ -2014,6 +2030,50 @@ sub command94 { return $new_table; } +sub command95 { + print "This is the name of the field in which you want to store the\n"; + print "username of the person the prefs are for. It default to 'user'\n"; + print "which clashes with a reserved keyword in PostgreSQL so this\n"; + print "will need to be changed for that database at least\n"; + print "\n"; + print "[$WHT$prefs_user_field$NRM]: $WHT"; + $new_field = ; + if ( $new_field eq "\n" ) { + $new_field = $prefs_user_field; + } else { + $new_field =~ s/[\r|\n]//g; + } + return $new_field; +} + +sub command96 { + print "This is the name of the field in which you want to store the\n"; + print "preferences keyword. It defaults to 'prefkey'\n"; + print "\n"; + print "[$WHT$prefs_key_field$NRM]: $WHT"; + $new_field = ; + if ( $new_field eq "\n" ) { + $new_field = $prefs_key_field; + } else { + $new_field =~ s/[\r|\n]//g; + } + return $new_field; +} + +sub command97 { + print "This is the name of the field in which you want to store the\n"; + print "preferences value. It defaults to 'prefval'\n"; + print "\n"; + print "[$WHT$prefs_val_field$NRM]: $WHT"; + $new_field = ; + if ( $new_field eq "\n" ) { + $new_field = $prefs_val_field; + } else { + $new_field =~ s/[\r|\n]//g; + } + return $new_field; +} + sub save_data { $tab = " "; if ( open( CF, ">config.php" ) ) { @@ -2134,7 +2194,11 @@ sub save_data { print CF "\$addrbook_dsn = '$addrbook_dsn';\n"; print CF "\$addrbook_table = '$addrbook_table';\n\n"; print CF "\$prefs_dsn = '$prefs_dsn';\n"; - print CF "\$prefs_table = '$prefs_table';\n\n"; + print CF "\$prefs_table = '$prefs_table';\n"; + print CF "\$prefs_user_field = '$prefs_user_field';\n"; + print CF "\$prefs_key_field = '$prefs_key_field';\n"; + print CF "\$prefs_val_field = '$prefs_val_field';\n"; + print CF "\n"; print CF "/**\n"; print CF " * Make sure there are no characters after the PHP closing\n"; diff --git a/functions/db_prefs.php b/functions/db_prefs.php index 1aab90ba..c11b2c59 100644 --- a/functions/db_prefs.php +++ b/functions/db_prefs.php @@ -16,8 +16,8 @@ * Database: * --------- * - * The preferences table should have tree columns: - * username char \ primary + * The preferences table should have three columns: + * user char \ primary * prefkey char / key * prefval blob * @@ -69,6 +69,9 @@ function cachePrefValues($username) { class dbPrefs { var $table = 'userprefs'; + var $user_field = 'user'; + var $key_field = 'prefkey'; + var $val_field = 'prefval'; var $dbh = NULL; var $error = NULL; @@ -86,6 +89,15 @@ class dbPrefs { if (!empty($prefs_table)) { $this->table = $prefs_table; } + if (!empty($prefs_user_field)) { + $this->user_field = $prefs_user_field; + } + if (!empty($prefs_key_field)) { + $this->key_field = $prefs_key_field; + } + if (!empty($prefs_val_field)) { + $this->val_field = $prefs_val_field; + } $dbh = DB::connect($prefs_dsn, true); if(DB::isError($dbh) || DB::isWarning($dbh)) { @@ -131,9 +143,11 @@ class dbPrefs { if (!$this->open()) { return false; } - $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'", + $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", $this->table, + $this->user_field, $this->dbh->quoteString($user), + $this->key_field, $this->dbh->quoteString($key)); $res = $this->dbh->simpleQuery($query); @@ -154,9 +168,12 @@ class dbPrefs { if (!$this->open()) { return false; } - $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ". + $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". "VALUES('%s','%s','%s')", $this->table, + $this->user_field, + $this->key_field, + $this->val_field, $this->dbh->quoteString($user), $this->dbh->quoteString($key), $this->dbh->quoteString($value)); @@ -177,9 +194,12 @@ class dbPrefs { } $prefs_cache = array(); - $query = sprintf("SELECT prefkey, prefval FROM %s ". - "WHERE user = '%s'", + $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". + "WHERE %s = '%s'", + $this->key_field, + $this->val_field, $this->table, + $this->user_field, $this->dbh->quoteString($user)); $res = $this->dbh->query($query); if (DB::isError($res)) { @@ -200,10 +220,16 @@ class dbPrefs { if (!$this->open()) { return; } - $query = sprintf("SELECT * FROM %s WHERE user='%s' ". - "AND prefkey LIKE 'highlight%%' ORDER BY prefkey", + $query = sprintf("SELECT %s, %s as prefkey, %s as prefval FROM %s WHERE %s='%s' ". + "AND %s LIKE 'highlight%%' ORDER BY %s", + $this->user_field, + $this->key_field, + $this->val_field, $this->table, - $this->dbh->quoteString($user)); + $this->user_field, + $this->dbh->quoteString($user), + $this->key_field, + $this->key_field); $res = $this->dbh->query($query); if(DB::isError($res)) { @@ -224,11 +250,14 @@ class dbPrefs { $hilinum++; if($oldkey != $newkey) { - $query = sprintf("UPDATE %s SET prefkey='%s' ". - "WHERE user ='%s' AND prefkey='%s'", + $query = sprintf("UPDATE %s SET %s='%s' ". + "WHERE %s ='%s' AND %s='%s'", $this->table, + $this->key_field, $this->dbh->quoteString($newkey), + $this->user_field, $this->dbh->quoteString($user), + $this->key_field, $this->dbh->quoteString($oldkey)); $res = $this->dbh->simpleQuery($query); diff --git a/plugins/administrator/defines.php b/plugins/administrator/defines.php index 6b3abf7b..5d41a60f 100644 --- a/plugins/administrator/defines.php +++ b/plugins/administrator/defines.php @@ -227,6 +227,18 @@ $defcfg = array( '$config_version' => array( 'name' => _("Config File Version"), 'type' => SMOPT_TYPE_STRING, 'size' => 40, 'default' => 'userprefs' ), + '$prefs_user_field' => array('name' => _("Preferences username field"), + 'type' => SMOPT_TYPE_STRING, + 'size' => 40, + 'default' => 'user' ), + '$prefs_key_field' => array('name' => _("Preferences key field"), + 'type' => SMOPT_TYPE_STRING, + 'size' => 40, + 'default' => 'prefkey' ), + '$prefs_val_field' => array('name' => _("Preferences value field"), + 'type' => SMOPT_TYPE_STRING, + 'size' => 40, + 'default' => 'prefval' ), /* --------------------------------------------------------*/ 'Group7' => array( 'name' => _("Themes"), 'type' => SMOPT_TYPE_TITLE ), -- 2.25.1