From 6ff1c69061784933fba5cf76c1bbd9e14e659b0f Mon Sep 17 00:00:00 2001 From: pallo Date: Mon, 7 May 2001 20:29:14 +0000 Subject: [PATCH] Made it easier to configure personal addressbooks in a database. Also included some rough documentation on how to install it and on the tables you need in the database. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@1343 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- config/config_default.php | 8 ++++ doc/db-backend.txt | 84 +++++++++++++++++++++++++++++++++++++++ functions/addressbook.php | 51 ++++++++++++------------ 3 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 doc/db-backend.txt diff --git a/config/config_default.php b/config/config_default.php index c8fecd83..e540b917 100644 --- a/config/config_default.php +++ b/config/config_default.php @@ -285,6 +285,14 @@ global $ldap_server; // "name" => "Netcenter Member Directory", // "base" => "ou=member_directory,o=netcenter.com"); +// Database-driven private addressbooks +// +// DSN (Data Source Name) for a database where the private +// addressbooks are stored. See doc/db-backend.txt for more info. +// If it is not defined, the addressbooks are stored in files +// in the data dir. +// +// $addrbook_dsn = 'mysql://user:pass@hostname/dbname'; // you have an option to chose between javascript or html version of diff --git a/doc/db-backend.txt b/doc/db-backend.txt new file mode 100644 index 00000000..4b3a6d94 --- /dev/null +++ b/doc/db-backend.txt @@ -0,0 +1,84 @@ +$Id$ + + +Storing private addressbooks and preferences in a database +========================================================== + + +On sites with many users you might want to store your user data in a +database instead of in files. This document describe how to configure +SquirrelMail to do this. + +Methods for storing both personal addressbooks and user preferences in +a database is as a part of the distribution. + + + +Configuring PEAR DB +------------------- + +To work you must install the PEAR classes that is a part of PHP. Make +sure the directory where the PEAR files are located is a part of your +include path. See the PHP documentation for info on how to do that. + + + +Configuring addressbooks in database +------------------------------------ + +First you need to create a database and a table to store the data in. +Create a database user with access to read and write in that table. + +For MySQL you would normally do something like: + + (from the command line) + # mysqladmin create squirrelmail + + (from the mysql client) + mysql> GRANT select,insert,update,delete ON squirrelmail.* + TO squrreluser@localhost IDENTIFIED BY 'sqpassword'; + +The table structure should be similar to this (for MySQL): + + CREATE TABLE address ( + owner varchar(16) DEFAULT '' NOT NULL, + nickname varchar(16) DEFAULT '' NOT NULL, + firstname varchar(128) DEFAULT '' NOT NULL, + lastname varchar(128) DEFAULT '' NOT NULL, + email varchar(128) DEFAULT '' NOT NULL, + label varchar(255), + PRIMARY KEY (owner,nickname), + KEY firstname (firstname,lastname) + ); + + +Next, edit config/config.php and add a DSN (Data Source Name) for the +database. It should look something like: + + $addrbook_dsn = 'mysql://squirreluser:sqpassword@localhost/squirrelmail'; + +From now on all users' personal addressbooks will be stored in a +database. + + + +Configuring preferences in database +----------------------------------- + +There is no easy way to do this yet. You will have to remove +functions/prefs.php and replace it with functions/db_prefs.php. Then +edit the new functions/prefs.php (db_prefs.php) and change the $DSN to +point to a database you create (can be the same you use for +addressbooks). Create a table similar to this (for MySQL): + + CREATE TABLE userprefs ( + user varchar(32) DEFAULT '' NOT NULL, + prefkey varchar(64) DEFAULT '' NOT NULL, + prefval blob DEFAULT '' NOT NULL, + PRIMARY KEY (user,prefkey) + ); + + +Default preferences can be set by altering the $default array in +prefs.php (db_prefs.php). + diff --git a/functions/addressbook.php b/functions/addressbook.php index 26b6aed3..0fd0451b 100644 --- a/functions/addressbook.php +++ b/functions/addressbook.php @@ -16,43 +16,42 @@ include('../functions/abook_local_file.php'); include('../functions/abook_ldap_server.php'); - // Un-comment if you're using database backend - // include('../functions/abook_database.php'); + // Only load database backend if database is configured + if(isset($addrbook_dsn)) + include('../functions/abook_database.php'); // Create and initialize an addressbook object. // Returns the created object function addressbook_init($showerr = true, $onlylocal = false) { global $data_dir, $username, $ldap_server; + global $addrbook_dsn; // Create a new addressbook object $abook = new AddressBook; - // Always add a local backend - - // Use *either* file-based *or* database addressbook. Remove - // and insert comments to enable the one you want. - - // ------ BEGIN Initialize file-based personal addressbook ------ - $filename = sprintf('%s%s.abook', $data_dir, $username); - $r = $abook->add_backend('local_file', Array('filename' => $filename, - 'create' => true)); - - if(!$r && $showerr) { - printf(_("Error opening file %s"), $filename); - exit; + // Always add a local backend. We use *either* file-based *or* a + // database addressbook. If $addrbook_dsn is set, the database + // backend is used. If not, addressbooks are stores in files. + if(isset($addrbook_dsn) && !empty($addrbook_dsn)) { + // Database + $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, + 'owner' => $username, + 'table' => 'address')); + if(!$r && $showerr) { + printf(_("Error initializing addressbook database.")); + exit; + } + } else { + // File + $filename = sprintf('%s%s.abook', $data_dir, $username); + $r = $abook->add_backend('local_file', Array('filename' => $filename, + 'create' => true)); + if(!$r && $showerr) { + printf(_("Error opening file %s"), $filename); + exit; + } } - // ------ END Initialize file-based personal addressbook ------ - - // ------ BEGIN Initialize database-based personal addressbook ------ - // $r = $abook->add_backend('database', Array('dsn' => 'mysql://dbuser@host/dbname', - // 'owner' => $username, - // 'table' => 'address')); - // if(!$r && $showerr) { - // printf(_("Error initializing addressbook: %s"), $filename); - // exit; - // } - // ------ END Initialize database-based personal addressbook ------ if($onlylocal) return $abook; -- 2.25.1