519f066ff2c1660fdab31704e872c6ebad60e1a7
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];
126 function deleteKey($user, $key) {
130 $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
132 $this->dbh
->quoteString($user),
133 $this->dbh
->quoteString($key));
135 $res = $this->dbh
->simpleQuery($query);
136 if(DB
::isError($res)) {
137 $this->failQuery($res);
140 unset($prefs_cache[$key]);
142 if(substr($key, 0, 9) == 'highlight') {
143 $this->renumberHighlightList($user);
149 function setKey($user, $key, $value) {
151 $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
152 "VALUES('%s','%s','%s')",
154 $this->dbh
->quoteString($user),
155 $this->dbh
->quoteString($key),
156 $this->dbh
->quoteString($value));
158 $res = $this->dbh
->simpleQuery($query);
159 if(DB
::isError($res)) {
160 $this->failQuery($res);
166 function fillPrefsCache($user) {
171 $prefs_cache = array();
172 $query = sprintf("SELECT prefkey, prefval FROM %s ".
175 $this->dbh
->quoteString($user));
176 $res = $this->dbh
->query($query);
177 if (DB
::isError($res)) {
178 $this->failQuery($res);
181 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
182 $prefs_cache[$row['prefkey']] = $row['prefval'];
187 * When a highlight option is deleted the preferences module
188 * must renumber the list. This should be done somewhere else,
189 * but it is not, so....
191 function renumberHighlightList($user) {
193 $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
194 "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
196 $this->dbh
->quoteString($user));
198 $res = $this->dbh
->query($query);
199 if(DB
::isError($res)) {
200 $this->failQuery($res);
203 /* Store old data in array */
205 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
209 /* Renumber keys of old data */
211 for($i = 0; $i < count($rows) ; $i++
) {
212 $oldkey = $rows[$i]['prefkey'];
213 $newkey = substr($oldkey, 0, 9) . $hilinum;
216 if($oldkey != $newkey) {
217 $query = sprintf("UPDATE %s SET prefkey='%s' ".
218 "WHERE user ='%s' AND prefkey='%s'",
220 $this->dbh
->quoteString($newkey),
221 $this->dbh
->quoteString($user),
222 $this->dbh
->quoteString($oldkey));
224 $res = $this->dbh
->simpleQuery($query);
225 if(DB
::isError($res)) {
226 $this->failQuery($res);
234 } /* end class dbPrefs */
237 /* returns the value for the pref $string */
238 function getPref($data_dir, $username, $string, $default = '') {
240 if(isset($db->error
)) {
241 printf( _("Preference database error (%s). Exiting abnormally"),
246 return $db->getKey($username, $string, $default);
249 /* Remove the pref $string */
250 function removePref($data_dir, $username, $string) {
252 if(isset($db->error
)) {
256 $db->deleteKey($username, $string);
260 /* sets the pref, $string, to $set_to */
261 function setPref($data_dir, $username, $string, $set_to) {
264 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
269 removePref($data_dir, $username, $string);
274 if(isset($db->error
)) {
278 $db->setKey($username, $string, $set_to);
279 $prefs_cache[$string] = $set_to;
280 assert_options(ASSERT_ACTIVE
, 1);
281 assert_options(ASSERT_BAIL
, 1);
282 assert ('$set_to == $prefs_cache[$string]');
287 /* This checks if the prefs are available */
288 function checkForPrefs($data_dir, $username) {
290 if(isset($db->error
)) {
295 /* Writes the Signature */
296 function setSig($data_dir, $username, $string) {
298 if(isset($db->error
)) {
302 $db->setKey($username, '___signature___', $string);
306 /* Gets the signature */
307 function getSig($data_dir, $username) {
309 if(isset($db->error
)) {
313 return $db->getKey($username, '___signature___');
316 /* Hashing functions */
318 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
319 global $dir_hash_level;
321 /* Remove trailing slash from $dir if found */
322 if (substr($dir, -1) == '/') {
323 $dir = substr($dir, 0, strlen($dir) - 1);
326 /* Compute the hash for this user and extract the hash directories. */
327 $hash_dirs = computeHashDirs($username);
329 /* First, get and make sure the full hash directory exists. */
330 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
332 /* Set the value of our real data file. */
333 $result = "$real_hash_dir/$datafile";
335 /* Check for this file in the real hash directory. */
336 if ($hash_search && !@file_exists
($result)) {
337 /* First check the base directory, the most common location. */
338 if (@file_exists
("$dir/$datafile")) {
339 rename("$dir/$datafile", $result);
341 /* Then check the full range of possible hash directories. */
343 $check_hash_dir = $dir;
344 for ($h = 0; $h < 4; ++
$h) {
345 $check_hash_dir .= '/' . $hash_dirs[$h];
346 if (@is_readable
("$check_hash_dir/$datafile")) {
347 rename("$check_hash_dir/$datafile", $result);
354 /* Return the full hashed datafile path. */
358 function getHashedDir($username, $dir, $hash_dirs = '') {
359 global $dir_hash_level;
361 /* Remove trailing slash from $dir if found */
362 if (substr($dir, -1) == '/') {
363 $dir = substr($dir, 0, strlen($dir) - 1);
366 /* If necessary, populate the hash dir variable. */
367 if ($hash_dirs == '') {
368 $hash_dirs = computeHashDirs($username);
371 /* Make sure the full hash directory exists. */
372 $real_hash_dir = $dir;
373 for ($h = 0; $h < $dir_hash_level; ++
$h) {
374 $real_hash_dir .= '/' . $hash_dirs[$h];
375 if (!@is_dir
($real_hash_dir)) {
376 if (!@mkdir
($real_hash_dir, 0770)) {
377 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>';
378 echo _("Could not create hashed directory structure!") . "<br>\n";
379 echo _("Please contact your system administrator and report this error.") . "<br>\n";
385 /* And return that directory. */
386 return ($real_hash_dir);
389 function computeHashDirs($username) {
390 /* Compute the hash for this user and extract the hash directories. */
391 $hash = base_convert(crc32($username), 10, 16);
392 $hash_dirs = array();
393 for ($h = 0; $h < 4; ++
$h) {
394 $hash_dirs[] = substr($hash, $h, 1);
397 /* Return our array of hash directories. */