db_prefs.php 11 KB

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