| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * db_prefs.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains functions for manipulating user preferences |
| 10 | * stored in a database, accessed though the Pear DB layer. |
| 11 | * |
| 12 | * Database: |
| 13 | * --------- |
| 14 | * |
| 15 | * The preferences table should have three columns: |
| 16 | * user char \ primary |
| 17 | * prefkey char / key |
| 18 | * prefval blob |
| 19 | * |
| 20 | * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '', |
| 21 | * prefkey CHAR(64) NOT NULL DEFAULT '', |
| 22 | * prefval BLOB NOT NULL DEFAULT '', |
| 23 | * primary key (user,prefkey)); |
| 24 | * |
| 25 | * Configuration of databasename, username and password is done |
| 26 | * by using conf.pl or the administrator plugin |
| 27 | * |
| 28 | * $Id$ |
| 29 | * @package squirrelmail |
| 30 | */ |
| 31 | |
| 32 | /** Unknown database */ |
| 33 | define('SMDB_UNKNOWN', 0); |
| 34 | /** MySQL */ |
| 35 | define('SMDB_MYSQL', 1); |
| 36 | /** PostgreSQL */ |
| 37 | define('SMDB_PGSQL', 2); |
| 38 | |
| 39 | require_once('DB.php'); |
| 40 | require_once(SM_PATH . 'config/config.php'); |
| 41 | |
| 42 | global $prefs_are_cached, $prefs_cache; |
| 43 | |
| 44 | function cachePrefValues($username) { |
| 45 | global $prefs_are_cached, $prefs_cache; |
| 46 | |
| 47 | if ($prefs_are_cached) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | sqsession_unregister('prefs_cache'); |
| 52 | sqsession_unregister('prefs_are_cached'); |
| 53 | |
| 54 | $db = new dbPrefs; |
| 55 | if(isset($db->error)) { |
| 56 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 57 | $db->error); |
| 58 | exit; |
| 59 | } |
| 60 | |
| 61 | $db->fillPrefsCache($username); |
| 62 | if (isset($db->error)) { |
| 63 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 64 | $db->error); |
| 65 | exit; |
| 66 | } |
| 67 | |
| 68 | $prefs_are_cached = true; |
| 69 | |
| 70 | sqsession_register($prefs_cache, 'prefs_cache'); |
| 71 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Completely undocumented class - someone document it! |
| 76 | * @package squirrelmail |
| 77 | */ |
| 78 | class dbPrefs { |
| 79 | var $table = 'userprefs'; |
| 80 | var $user_field = 'user'; |
| 81 | var $key_field = 'prefkey'; |
| 82 | var $val_field = 'prefval'; |
| 83 | |
| 84 | var $dbh = NULL; |
| 85 | var $error = NULL; |
| 86 | var $db_type = SMDB_UNKNOWN; |
| 87 | |
| 88 | var $default = Array('theme_default' => 0, |
| 89 | 'show_html_default' => '0'); |
| 90 | |
| 91 | function open() { |
| 92 | global $prefs_dsn, $prefs_table; |
| 93 | global $prefs_user_field, $prefs_key_field, $prefs_val_field; |
| 94 | |
| 95 | if(isset($this->dbh)) { |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | if (preg_match('/^mysql/', $prefs_dsn)) { |
| 100 | $this->db_type = SMDB_MYSQL; |
| 101 | } elseif (preg_match('/^pgsql/', $prefs_dsn)) { |
| 102 | $this->db_type = SMDB_PGSQL; |
| 103 | } |
| 104 | |
| 105 | if (!empty($prefs_table)) { |
| 106 | $this->table = $prefs_table; |
| 107 | } |
| 108 | if (!empty($prefs_user_field)) { |
| 109 | $this->user_field = $prefs_user_field; |
| 110 | } |
| 111 | if (!empty($prefs_key_field)) { |
| 112 | $this->key_field = $prefs_key_field; |
| 113 | } |
| 114 | if (!empty($prefs_val_field)) { |
| 115 | $this->val_field = $prefs_val_field; |
| 116 | } |
| 117 | $dbh = DB::connect($prefs_dsn, true); |
| 118 | |
| 119 | if(DB::isError($dbh)) { |
| 120 | $this->error = DB::errorMessage($dbh); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | $this->dbh = $dbh; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | function failQuery($res = NULL) { |
| 129 | if($res == NULL) { |
| 130 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 131 | $this->error); |
| 132 | } else { |
| 133 | printf(_("Preference database error (%s). Exiting abnormally"), |
| 134 | DB::errorMessage($res)); |
| 135 | } |
| 136 | exit; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | function getKey($user, $key, $default = '') { |
| 141 | global $prefs_cache; |
| 142 | |
| 143 | cachePrefValues($user); |
| 144 | |
| 145 | if (isset($prefs_cache[$key])) { |
| 146 | return $prefs_cache[$key]; |
| 147 | } else { |
| 148 | if (isset($this->default[$key])) { |
| 149 | return $this->default[$key]; |
| 150 | } else { |
| 151 | return $default; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | function deleteKey($user, $key) { |
| 157 | global $prefs_cache; |
| 158 | |
| 159 | if (!$this->open()) { |
| 160 | return false; |
| 161 | } |
| 162 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 163 | $this->table, |
| 164 | $this->user_field, |
| 165 | $this->dbh->quoteString($user), |
| 166 | $this->key_field, |
| 167 | $this->dbh->quoteString($key)); |
| 168 | |
| 169 | $res = $this->dbh->simpleQuery($query); |
| 170 | if(DB::isError($res)) { |
| 171 | $this->failQuery($res); |
| 172 | } |
| 173 | |
| 174 | unset($prefs_cache[$key]); |
| 175 | |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | function setKey($user, $key, $value) { |
| 180 | if (!$this->open()) { |
| 181 | return false; |
| 182 | } |
| 183 | if ($this->db_type == SMDB_MYSQL) { |
| 184 | $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". |
| 185 | "VALUES('%s','%s','%s')", |
| 186 | $this->table, |
| 187 | $this->user_field, |
| 188 | $this->key_field, |
| 189 | $this->val_field, |
| 190 | $this->dbh->quoteString($user), |
| 191 | $this->dbh->quoteString($key), |
| 192 | $this->dbh->quoteString($value)); |
| 193 | |
| 194 | $res = $this->dbh->simpleQuery($query); |
| 195 | if(DB::isError($res)) { |
| 196 | $this->failQuery($res); |
| 197 | } |
| 198 | } elseif ($this->db_type == SMDB_PGSQL) { |
| 199 | $this->dbh->simpleQuery("BEGIN TRANSACTION"); |
| 200 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 201 | $this->table, |
| 202 | $this->user_field, |
| 203 | $this->dbh->quoteString($user), |
| 204 | $this->key_field, |
| 205 | $this->dbh->quoteString($key)); |
| 206 | $res = $this->dbh->simpleQuery($query); |
| 207 | if (DB::isError($res)) { |
| 208 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 209 | $this->failQuery($res); |
| 210 | } |
| 211 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 212 | $this->table, |
| 213 | $this->user_field, |
| 214 | $this->key_field, |
| 215 | $this->val_field, |
| 216 | $this->dbh->quoteString($user), |
| 217 | $this->dbh->quoteString($key), |
| 218 | $this->dbh->quoteString($value)); |
| 219 | $res = $this->dbh->simpleQuery($query); |
| 220 | if (DB::isError($res)) { |
| 221 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
| 222 | $this->failQuery($res); |
| 223 | } |
| 224 | $this->dbh->simpleQuery("COMMIT TRANSACTION"); |
| 225 | } else { |
| 226 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
| 227 | $this->table, |
| 228 | $this->user_field, |
| 229 | $this->dbh->quoteString($user), |
| 230 | $this->key_field, |
| 231 | $this->dbh->quoteString($key)); |
| 232 | $res = $this->dbh->simpleQuery($query); |
| 233 | if (DB::isError($res)) { |
| 234 | $this->failQuery($res); |
| 235 | } |
| 236 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
| 237 | $this->table, |
| 238 | $this->user_field, |
| 239 | $this->key_field, |
| 240 | $this->val_field, |
| 241 | $this->dbh->quoteString($user), |
| 242 | $this->dbh->quoteString($key), |
| 243 | $this->dbh->quoteString($value)); |
| 244 | $res = $this->dbh->simpleQuery($query); |
| 245 | if (DB::isError($res)) { |
| 246 | $this->failQuery($res); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | function fillPrefsCache($user) { |
| 254 | global $prefs_cache; |
| 255 | |
| 256 | if (!$this->open()) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $prefs_cache = array(); |
| 261 | $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". |
| 262 | "WHERE %s = '%s'", |
| 263 | $this->key_field, |
| 264 | $this->val_field, |
| 265 | $this->table, |
| 266 | $this->user_field, |
| 267 | $this->dbh->quoteString($user)); |
| 268 | $res = $this->dbh->query($query); |
| 269 | if (DB::isError($res)) { |
| 270 | $this->failQuery($res); |
| 271 | } |
| 272 | |
| 273 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 274 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | } /* end class dbPrefs */ |
| 279 | |
| 280 | |
| 281 | /* returns the value for the pref $string */ |
| 282 | function getPref($data_dir, $username, $string, $default = '') { |
| 283 | $db = new dbPrefs; |
| 284 | if(isset($db->error)) { |
| 285 | printf( _("Preference database error (%s). Exiting abnormally"), |
| 286 | $db->error); |
| 287 | exit; |
| 288 | } |
| 289 | |
| 290 | return $db->getKey($username, $string, $default); |
| 291 | } |
| 292 | |
| 293 | /* Remove the pref $string */ |
| 294 | function removePref($data_dir, $username, $string) { |
| 295 | global $prefs_cache; |
| 296 | $db = new dbPrefs; |
| 297 | if(isset($db->error)) { |
| 298 | $db->failQuery(); |
| 299 | } |
| 300 | |
| 301 | $db->deleteKey($username, $string); |
| 302 | |
| 303 | if (isset($prefs_cache[$string])) { |
| 304 | unset($prefs_cache[$string]); |
| 305 | } |
| 306 | |
| 307 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | /* sets the pref, $string, to $set_to */ |
| 312 | function setPref($data_dir, $username, $string, $set_to) { |
| 313 | global $prefs_cache; |
| 314 | |
| 315 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | if ($set_to === '') { |
| 320 | removePref($data_dir, $username, $string); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | $db = new dbPrefs; |
| 325 | if(isset($db->error)) { |
| 326 | $db->failQuery(); |
| 327 | } |
| 328 | |
| 329 | $db->setKey($username, $string, $set_to); |
| 330 | $prefs_cache[$string] = $set_to; |
| 331 | assert_options(ASSERT_ACTIVE, 1); |
| 332 | assert_options(ASSERT_BAIL, 1); |
| 333 | assert ('$set_to == $prefs_cache[$string]'); |
| 334 | sqsession_register($prefs_cache , 'prefs_cache'); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | /* This checks if the prefs are available */ |
| 339 | function checkForPrefs($data_dir, $username) { |
| 340 | $db = new dbPrefs; |
| 341 | if(isset($db->error)) { |
| 342 | $db->failQuery(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* Writes the Signature */ |
| 347 | function setSig($data_dir, $username, $number, $string) { |
| 348 | if ($number == "g") { |
| 349 | $key = '___signature___'; |
| 350 | } else { |
| 351 | $key = sprintf('___sig%s___', $number); |
| 352 | } |
| 353 | setPref($data_dir, $username, $key, $string); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | /* Gets the signature */ |
| 358 | function getSig($data_dir, $username, $number) { |
| 359 | if ($number == "g") { |
| 360 | $key = '___signature___'; |
| 361 | } else { |
| 362 | $key = sprintf('___sig%d___', $number); |
| 363 | } |
| 364 | return getPref($data_dir, $username, $key); |
| 365 | } |
| 366 | |
| 367 | ?> |