db_prefs.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /*
  3. * db_prefs.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $Id$
  28. */
  29. define('SMDB_UNKNOWN', 0);
  30. define('SMDB_MYSQL', 1);
  31. define('SMDB_PGSQL', 2);
  32. require_once('DB.php');
  33. require_once(SM_PATH . 'config/config.php');
  34. global $prefs_are_cached, $prefs_cache;
  35. function cachePrefValues($username) {
  36. global $prefs_are_cached, $prefs_cache;
  37. if ($prefs_are_cached) {
  38. return;
  39. }
  40. sqsession_unregister('prefs_cache');
  41. sqsession_unregister('prefs_are_cached');
  42. $db = new dbPrefs;
  43. if(isset($db->error)) {
  44. printf( _("Preference database error (%s). Exiting abnormally"),
  45. $db->error);
  46. exit;
  47. }
  48. $db->fillPrefsCache($username);
  49. if (isset($db->error)) {
  50. printf( _("Preference database error (%s). Exiting abnormally"),
  51. $db->error);
  52. exit;
  53. }
  54. $prefs_are_cached = true;
  55. sqsession_register($prefs_cache, 'prefs_cache');
  56. sqsession_register($prefs_are_cached, 'prefs_are_cached');
  57. }
  58. class dbPrefs {
  59. var $table = 'userprefs';
  60. var $user_field = 'user';
  61. var $key_field = 'prefkey';
  62. var $val_field = 'prefval';
  63. var $dbh = NULL;
  64. var $error = NULL;
  65. var $db_type = SMDB_UNKNOWN;
  66. var $default = Array('theme_default' => 0,
  67. 'show_html_default' => '0');
  68. function open() {
  69. global $prefs_dsn, $prefs_table;
  70. global $prefs_user_field, $prefs_key_field, $prefs_val_field;
  71. if(isset($this->dbh)) {
  72. return true;
  73. }
  74. if (preg_match('/^mysql/', $prefs_dsn)) {
  75. $this->db_type = SMDB_MYSQL;
  76. } elseif (preg_match('/^pgsql/', $prefs_dsn)) {
  77. $this->db_type = SMDB_PGSQL;
  78. }
  79. if (!empty($prefs_table)) {
  80. $this->table = $prefs_table;
  81. }
  82. if (!empty($prefs_user_field)) {
  83. $this->user_field = $prefs_user_field;
  84. }
  85. if (!empty($prefs_key_field)) {
  86. $this->key_field = $prefs_key_field;
  87. }
  88. if (!empty($prefs_val_field)) {
  89. $this->val_field = $prefs_val_field;
  90. }
  91. $dbh = DB::connect($prefs_dsn, true);
  92. if(DB::isError($dbh)) {
  93. $this->error = DB::errorMessage($dbh);
  94. return false;
  95. }
  96. $this->dbh = $dbh;
  97. return true;
  98. }
  99. function failQuery($res = NULL) {
  100. if($res == NULL) {
  101. printf(_("Preference database error (%s). Exiting abnormally"),
  102. $this->error);
  103. } else {
  104. printf(_("Preference database error (%s). Exiting abnormally"),
  105. DB::errorMessage($res));
  106. }
  107. exit;
  108. }
  109. function getKey($user, $key, $default = '') {
  110. global $prefs_cache;
  111. cachePrefValues($user);
  112. if (isset($prefs_cache[$key])) {
  113. return $prefs_cache[$key];
  114. } else {
  115. if (isset($this->default[$key])) {
  116. return $this->default[$key];
  117. } else {
  118. return $default;
  119. }
  120. }
  121. }
  122. function deleteKey($user, $key) {
  123. global $prefs_cache;
  124. if (!$this->open()) {
  125. return false;
  126. }
  127. $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  128. $this->table,
  129. $this->user_field,
  130. $this->dbh->quoteString($user),
  131. $this->key_field,
  132. $this->dbh->quoteString($key));
  133. $res = $this->dbh->simpleQuery($query);
  134. if(DB::isError($res)) {
  135. $this->failQuery($res);
  136. }
  137. unset($prefs_cache[$key]);
  138. return true;
  139. }
  140. function setKey($user, $key, $value) {
  141. if (!$this->open()) {
  142. return false;
  143. }
  144. if ($this->db_type == SMDB_MYSQL) {
  145. $query = sprintf("REPLACE INTO %s (%s, %s, %s) ".
  146. "VALUES('%s','%s','%s')",
  147. $this->table,
  148. $this->user_field,
  149. $this->key_field,
  150. $this->val_field,
  151. $this->dbh->quoteString($user),
  152. $this->dbh->quoteString($key),
  153. $this->dbh->quoteString($value));
  154. $res = $this->dbh->simpleQuery($query);
  155. if(DB::isError($res)) {
  156. $this->failQuery($res);
  157. }
  158. } elseif ($this->db_type == SMDB_PGSQL) {
  159. $this->dbh->simpleQuery("BEGIN TRANSACTION");
  160. $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  161. $this->table,
  162. $this->user_field,
  163. $this->dbh->quoteString($user),
  164. $this->key_field,
  165. $this->dbh->quoteString($key));
  166. $res = $this->dbh->simpleQuery($query);
  167. if (DB::isError($res)) {
  168. $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
  169. $this->failQuery($res);
  170. }
  171. $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
  172. $this->table,
  173. $this->user_field,
  174. $this->key_field,
  175. $this->val_field,
  176. $this->dbh->quoteString($user),
  177. $this->dbh->quoteString($key),
  178. $this->dbh->quoteString($value));
  179. $res = $this->dbh->simpleQuery($query);
  180. if (DB::isError($res)) {
  181. $this->dbh->simpleQuery("ROLLBACK TRANSACTION");
  182. $this->failQuery($res);
  183. }
  184. $this->dbh->simpleQuery("COMMIT TRANSACTION");
  185. } else {
  186. $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'",
  187. $this->table,
  188. $this->user_field,
  189. $this->dbh->quoteString($user),
  190. $this->key_field,
  191. $this->dbh->quoteString($key));
  192. $res = $this->dbh->simpleQuery($query);
  193. if (DB::isError($res)) {
  194. $this->failQuery($res);
  195. }
  196. $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
  197. $this->table,
  198. $this->user_field,
  199. $this->key_field,
  200. $this->val_field,
  201. $this->dbh->quoteString($user),
  202. $this->dbh->quoteString($key),
  203. $this->dbh->quoteString($value));
  204. $res = $this->dbh->simpleQuery($query);
  205. if (DB::isError($res)) {
  206. $this->failQuery($res);
  207. }
  208. }
  209. return true;
  210. }
  211. function fillPrefsCache($user) {
  212. global $prefs_cache;
  213. if (!$this->open()) {
  214. return;
  215. }
  216. $prefs_cache = array();
  217. $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ".
  218. "WHERE %s = '%s'",
  219. $this->key_field,
  220. $this->val_field,
  221. $this->table,
  222. $this->user_field,
  223. $this->dbh->quoteString($user));
  224. $res = $this->dbh->query($query);
  225. if (DB::isError($res)) {
  226. $this->failQuery($res);
  227. }
  228. while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  229. $prefs_cache[$row['prefkey']] = $row['prefval'];
  230. }
  231. }
  232. } /* end class dbPrefs */
  233. /* returns the value for the pref $string */
  234. function getPref($data_dir, $username, $string, $default = '') {
  235. $db = new dbPrefs;
  236. if(isset($db->error)) {
  237. printf( _("Preference database error (%s). Exiting abnormally"),
  238. $db->error);
  239. exit;
  240. }
  241. return $db->getKey($username, $string, $default);
  242. }
  243. /* Remove the pref $string */
  244. function removePref($data_dir, $username, $string) {
  245. $db = new dbPrefs;
  246. if(isset($db->error)) {
  247. $db->failQuery();
  248. }
  249. $db->deleteKey($username, $string);
  250. return;
  251. }
  252. /* sets the pref, $string, to $set_to */
  253. function setPref($data_dir, $username, $string, $set_to) {
  254. global $prefs_cache;
  255. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
  256. return;
  257. }
  258. if ($set_to === '') {
  259. removePref($data_dir, $username, $string);
  260. return;
  261. }
  262. $db = new dbPrefs;
  263. if(isset($db->error)) {
  264. $db->failQuery();
  265. }
  266. $db->setKey($username, $string, $set_to);
  267. $prefs_cache[$string] = $set_to;
  268. assert_options(ASSERT_ACTIVE, 1);
  269. assert_options(ASSERT_BAIL, 1);
  270. assert ('$set_to == $prefs_cache[$string]');
  271. return;
  272. }
  273. /* This checks if the prefs are available */
  274. function checkForPrefs($data_dir, $username) {
  275. $db = new dbPrefs;
  276. if(isset($db->error)) {
  277. $db->failQuery();
  278. }
  279. }
  280. /* Writes the Signature */
  281. function setSig($data_dir, $username, $number, $string) {
  282. if ($number == "g") {
  283. $key = '___signature___';
  284. } else {
  285. $key = sprintf('___sig%s___', $number);
  286. }
  287. setPref($data_dir, $username, $key, $string);
  288. return;
  289. }
  290. /* Gets the signature */
  291. function getSig($data_dir, $username, $number) {
  292. if ($number == "g") {
  293. $key = '___signature___';
  294. } else {
  295. $key = sprintf('___sig%d___', $number);
  296. }
  297. return getPref($data_dir, $username, $key);
  298. }
  299. ?>