db_prefs.php 13 KB

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