db_prefs.php 11 KB

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