Wednesday, May 23, 2012

Generating default preferences for your Horde installation

While updating our demo server at I also wanted to get some kind of automatic reset for the demo user preferences. The aim was to allow each user testing our demo system to play around with the system in every way desired - while at the same time resetting to the defaults once the next user logs in. This way the second user wouldn't get an unpleasant surprise and turn away from Horde if the first user selected our beloved "Barbie" theme. By the way: this one will be dropped with the redesign - I hope nobody flames our mailing lists and starts a revolution to get this pink atrocity back :)

In order to achieve that I needed to move from SQL based preferences to Session based preferences. Using the latter would purge any preference changes at the end of the session. In order to provide the users with some sane and useful defaults though I also Wanted to copy the old values from the SQL database into the prefs.local.php files. This way I can set the preferences so that users are immediately greeted with a portal screen that demonstrates the twitter and facebook integration.

Converting SQL based preferences to prefs.local.php default settings is something you might be interested in for your installation as well. It allows you to set appropriate defaults for one initial test user and convert those into site-wide defaults for your installation.

How to do that without much hassle? horde-prefs to the rescue!

horde-prefs is a small tool that allows printing, exporting, and importing of preference values stored in a backend.

In order to use the tool you need to define a configuration file for the specific backend you want to access. For the demo server this has been a SQL database:

   $conf['driver'] = 'Sql';
   $conf['params']['db'] = new Horde_Db_Adapter_Mysql(
      'persistent' => false,
      'username' => 'root',
      'password' => 'PASSWORD',
      'socket' => '/var/run/mysqld/mysqld.sock',
      'protocol' => 'unix',
      'database' => 'horde',
      'charset' => 'utf-8',
      'ssl' => true,
      'splitread' => false,
      'phptype' => 'mysql',

Now you can access the stored preference values by calling the horde-prefs tool. As horde-prefs is not stored under /usr/bin on the home server but uses a non-standard location I prefix the command with an explicit PHP include path:

php -d include_path="/var/www/pear/php" /var/www/pear/horde-prefs config.php guest print horde

The previous command prints all preferences values stored for the guest user for the application horde:

$_prefs['twitter']['value'] = "a:2:{s:3:"key";s:50:"183748047-vr6RLMOiYhfbfTH3qI8Lc8E32jF4UGGFbIxdkZyt";s:6:"secret";s:42:"i2DGXInJBW4kk3r2bvBdrxzUKMxL6AYS4u97WAJDyQ";}"
$_prefs['upgrade_tasks']['value'] = "a:7:{s:5:"horde";s:6:"4.0.13";s:9:"kronolith";s:5:"3.0.5";s:8:"imp_auth";s:5:"5.0.8";s:3:"imp";s:5:"5.0.8";s:5:"turba";s:5:"3.0.7";s:5:"whups";s:10:"2.0-ALPHA1";s:6:"gollem";s:10:"2.0-ALPHA2";}"

To convert this into a format suitable for the prefs.local.php files I simply used sed.

php -d include_path="/var/www/pear/php" /var/www/pear/horde-prefs config.php guest print horde | sed -e "s/^\([^:]*\): \(.*\)/\$_prefs\['\1'\]\['value'\] = '\2';/" >> /var/www/config/prefs.local.php

No comments:

Post a Comment