b0f38df7cdb169143c6e738f67c1ee5743df3c7e
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
10 * stored in a database, accessed though the Pear DB layer.
12 * To use this instead of the regular prefs.php, create a
13 * database as described below, and replace prefs.php
19 * The preferences table should have tree columns:
20 * username char \ primary
24 * CREATE TABLE userprefs (user CHAR(32) NOT NULL DEFAULT '',
25 * prefkey CHAR(64) NOT NULL DEFAULT '',
26 * prefval BLOB NOT NULL DEFAULT '',
27 * primary key (user,prefkey));
29 * Configuration of databasename, username and password is done
30 * by changing $DSN below.
35 require_once('DB.php');
37 global $prefs_are_cached, $prefs_cache;
39 if ( !session_is_registered('prefs_are_cached') ||
40 !isset( $prefs_cache) ||
41 !is_array( $prefs_cache) ||
42 substr( phpversion(), 0, 3 ) == '4.1' ) {
43 $prefs_are_cached = false;
44 $prefs_cache = array();
47 function cachePrefValues($username) {
48 global $prefs_are_cached, $prefs_cache;
50 if ($prefs_are_cached) {
54 session_unregister('prefs_cache');
55 session_unregister('prefs_are_cached');
58 if(isset($db->error
)) {
59 printf( _("Preference database error (%s). Exiting abnormally"),
64 $db->fillPrefsCache($username);
65 if (isset($db->error
)) {
66 printf( _("Preference database error (%s). Exiting abnormally"),
71 $prefs_are_cached = true;
73 session_register('prefs_cache');
74 session_register('prefs_are_cached');
78 var $DSN = 'mysql://user:passwd@host/database';
79 var $table = 'userprefs';
84 var $default = Array('chosen_theme' => '../themes/default_theme.php',
85 'show_html_default' => '0');
88 if(isset($this->dbh
)) {
91 $dbh = DB
::connect($this->DSN
, true);
93 if(DB
::isError($dbh) || DB
::isWarning($dbh)) {
94 $this->error
= DB
::errorMessage($dbh);
102 function failQuery($res = NULL) {
104 printf(_("Preference database error (%s). Exiting abnormally"),
107 printf(_("Preference database error (%s). Exiting abnormally"),
108 DB
::errorMessage($res));
114 function getKey($user, $key, $default = '') {
117 cachePrefValues($user);
119 if (isset($prefs_cache[$key])) {
120 return $prefs_cache[$key];
122 if (isset($this->default[$key])) {
123 return $this->default[$key];
130 function deleteKey($user, $key) {
134 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
136 $this->dbh
->quoteString($user),
137 $this->dbh
->quoteString($key));
139 $res = $this->dbh
->simpleQuery($query);
140 if(DB
::isError($res)) {
141 $this->failQuery($res);
144 unset($prefs_cache[$key]);
146 if(substr($key, 0, 9) == 'highlight') {
147 $this->renumberHighlightList($user);
153 function setKey($user, $key, $value) {
155 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
156 "VALUES('%s','%s','%s')",
158 $this->dbh
->quoteString($user),
159 $this->dbh
->quoteString($key),
160 $this->dbh
->quoteString($value));
162 $res = $this->dbh
->simpleQuery($query);
163 if(DB
::isError($res)) {
164 $this->failQuery($res);
170 function fillPrefsCache($user) {
175 $prefs_cache = array();
176 $query = sprintf("SELECT prefkey, prefval FROM %s ".
179 $this->dbh
->quoteString($user));
180 $res = $this->dbh
->query($query);
181 if (DB
::isError($res)) {
182 $this->failQuery($res);
185 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
186 $prefs_cache[$row['prefkey']] = $row['prefval'];
191 * When a highlight option is deleted the preferences module
192 * must renumber the list. This should be done somewhere else,
193 * but it is not, so....
195 function renumberHighlightList($user) {
197 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
198 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
200 $this->dbh
->quoteString($user));
202 $res = $this->dbh
->query($query);
203 if(DB
::isError($res)) {
204 $this->failQuery($res);
207 /* Store old data in array */
209 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
213 /* Renumber keys of old data */
215 for($i = 0; $i < count($rows) ; $i++
) {
216 $oldkey = $rows[$i]['prefkey'];
217 $newkey = substr($oldkey, 0, 9) . $hilinum;
220 if($oldkey != $newkey) {
221 $query = sprintf("UPDATE %s SET prefkey='%s' ".
222 "WHERE user ='%s' AND prefkey='%s'",
224 $this->dbh
->quoteString($newkey),
225 $this->dbh
->quoteString($user),
226 $this->dbh
->quoteString($oldkey));
228 $res = $this->dbh
->simpleQuery($query);
229 if(DB
::isError($res)) {
230 $this->failQuery($res);
238 } /* end class dbPrefs */
241 /* returns the value for the pref $string */
242 function getPref($data_dir, $username, $string, $default = '') {
244 if(isset($db->error
)) {
245 printf( _("Preference database error (%s). Exiting abnormally"),
250 return $db->getKey($username, $string, $default);
253 /* Remove the pref $string */
254 function removePref($data_dir, $username, $string) {
256 if(isset($db->error
)) {
260 $db->deleteKey($username, $string);
264 /* sets the pref, $string, to $set_to */
265 function setPref($data_dir, $username, $string, $set_to) {
268 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
273 removePref($data_dir, $username, $string);
278 if(isset($db->error
)) {
282 $db->setKey($username, $string, $set_to);
283 $prefs_cache[$string] = $set_to;
284 assert_options(ASSERT_ACTIVE
, 1);
285 assert_options(ASSERT_BAIL
, 1);
286 assert ('$set_to == $prefs_cache[$string]');
291 /* This checks if the prefs are available */
292 function checkForPrefs($data_dir, $username) {
294 if(isset($db->error
)) {
299 /* Writes the Signature */
300 function setSig($data_dir, $username, $string) {
302 if(isset($db->error
)) {
306 $db->setKey($username, '___signature___', $string);
310 /* Gets the signature */
311 function getSig($data_dir, $username) {
313 if(isset($db->error
)) {
317 return $db->getKey($username, '___signature___');
320 /* Hashing functions */
322 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
323 global $dir_hash_level;
325 /* Remove trailing slash from $dir if found */
326 if (substr($dir, -1) == '/') {
327 $dir = substr($dir, 0, strlen($dir) - 1);
330 /* Compute the hash for this user and extract the hash directories. */
331 $hash_dirs = computeHashDirs($username);
333 /* First, get and make sure the full hash directory exists. */
334 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
336 /* Set the value of our real data file. */
337 $result = "$real_hash_dir/$datafile";
339 /* Check for this file in the real hash directory. */
340 if ($hash_search && !@file_exists
($result)) {
341 /* First check the base directory, the most common location. */
342 if (@file_exists
("$dir/$datafile")) {
343 rename("$dir/$datafile", $result);
345 /* Then check the full range of possible hash directories. */
347 $check_hash_dir = $dir;
348 for ($h = 0; $h < 4; ++
$h) {
349 $check_hash_dir .= '/' . $hash_dirs[$h];
350 if (@is_readable
("$check_hash_dir/$datafile")) {
351 rename("$check_hash_dir/$datafile", $result);
358 /* Return the full hashed datafile path. */
362 function getHashedDir($username, $dir, $hash_dirs = '') {
363 global $dir_hash_level;
365 /* Remove trailing slash from $dir if found */
366 if (substr($dir, -1) == '/') {
367 $dir = substr($dir, 0, strlen($dir) - 1);
370 /* If necessary, populate the hash dir variable. */
371 if ($hash_dirs == '') {
372 $hash_dirs = computeHashDirs($username);
375 /* Make sure the full hash directory exists. */
376 $real_hash_dir = $dir;
377 for ($h = 0; $h < $dir_hash_level; ++
$h) {
378 $real_hash_dir .= '/' . $hash_dirs[$h];
379 if (!@is_dir
($real_hash_dir)) {
380 if (!@mkdir
($real_hash_dir, 0770)) {
381 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>';
382 echo _("Could not create hashed directory structure!") . "<br>\n";
383 echo _("Please contact your system administrator and report this error.") . "<br>\n";
389 /* And return that directory. */
390 return ($real_hash_dir);
393 function computeHashDirs($username) {
394 /* Compute the hash for this user and extract the hash directories. */
395 $hash = base_convert(crc32($username), 10, 16);
396 $hash_dirs = array();
397 for ($h = 0; $h < 4; ++
$h) {
398 $hash_dirs[] = substr($hash, $h, 1);
401 /* Return our array of hash directories. */