db_prefs.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * db_prefs.php
  4. *
  5. * Copyright (c) 1999-2001 The Squirrelmail Development 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 tree columns:
  19. * username char \ primary
  20. * prefkey char / key
  21. * prefval blob
  22. *
  23. * CREATE TABLE userprefs (user CHAR(32) 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 changing $DSN below.
  30. *
  31. * $Id$
  32. */
  33. require_once('DB.php');
  34. class dbPrefs {
  35. var $DSN = 'mysql://user@host/database';
  36. var $table = 'userprefs';
  37. var $dbh = NULL;
  38. var $error = NULL;
  39. var $default = Array('chosen_theme' => '../themes/default_theme.php',
  40. 'show_html_default' => '0');
  41. function dbPrefs() {
  42. $this->open();
  43. }
  44. function open() {
  45. if(isset($this->dbh)) return true;
  46. $dbh = DB::connect($this->DSN, true);
  47. if(DB::isError($dbh) || DB::isWarning($dbh)) {
  48. $this->error = DB::errorMessage($dbh);
  49. return false;
  50. }
  51. $this->dbh = $dbh;
  52. return true;
  53. }
  54. function failQuery($res = NULL) {
  55. if($res == NULL) {
  56. printf(_("Preference database error (%s). Exiting abnormally"),
  57. $this->error);
  58. } else {
  59. printf(_("Preference database error (%s). Exiting abnormally"),
  60. DB::errorMessage($res));
  61. }
  62. exit;
  63. }
  64. function getKey($user, $key) {
  65. $this->open();
  66. $query = sprintf("SELECT prefval FROM %s ".
  67. "WHERE user='%s' AND prefkey='%s'",
  68. $this->table,
  69. $this->dbh->quoteString($user),
  70. $this->dbh->quoteString($key));
  71. $res = $this->dbh->query($query);
  72. if(DB::isError($res))
  73. $this->failQuery($res);
  74. if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  75. return $row['prefval'];
  76. } else {
  77. if(isset($this->default[$key])) {
  78. return $this->default[$key];
  79. } else {
  80. return '';
  81. }
  82. }
  83. return '';
  84. }
  85. function deleteKey($user, $key) {
  86. $this->open();
  87. $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
  88. $this->table,
  89. $this->dbh->quoteString($user),
  90. $this->dbh->quoteString($key));
  91. $res = $this->dbh->simpleQuery($query);
  92. if(DB::isError($res))
  93. $this->failQuery($res);
  94. if(substr($key, 0, 9) == 'highlight') {
  95. $this->renumberHighlightList($user);
  96. }
  97. return true;
  98. }
  99. function setKey($user, $key, $value) {
  100. $this->open();
  101. $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
  102. "VALUES('%s','%s','%s')",
  103. $this->table,
  104. $this->dbh->quoteString($user),
  105. $this->dbh->quoteString($key),
  106. $this->dbh->quoteString($value));
  107. $res = $this->dbh->simpleQuery($query);
  108. if(DB::isError($res))
  109. $this->failQuery($res);
  110. return true;
  111. }
  112. /**
  113. ** When a highlight option is deleted the preferences module
  114. ** must renumber the list. This should be done somewhere else,
  115. ** but it is not, so....
  116. **/
  117. function renumberHighlightList($user) {
  118. $this->open();
  119. $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
  120. "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
  121. $this->table,
  122. $this->dbh->quoteString($user));
  123. $res = $this->dbh->query($query);
  124. if(DB::isError($res))
  125. $this->failQuery($res);
  126. // Store old data in array
  127. $rows = Array();
  128. while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
  129. $rows[] = $row;
  130. // Renumber keys of old data
  131. $hilinum = 0;
  132. for($i = 0; $i < count($rows) ; $i++) {
  133. $oldkey = $rows[$i]['prefkey'];
  134. $newkey = substr($oldkey, 0, 9) . $hilinum;
  135. $hilinum++;
  136. if($oldkey != $newkey) {
  137. $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
  138. "AND prefkey='%s'",
  139. $this->table,
  140. $this->dbh->quoteString($newkey),
  141. $this->dbh->quoteString($user),
  142. $this->dbh->quoteString($oldkey));
  143. $res = $this->dbh->simpleQuery($query);
  144. if(DB::isError($res))
  145. $this->failQuery($res);
  146. }
  147. }
  148. return;
  149. }
  150. } // end class dbPrefs
  151. /** returns the value for the pref $string **/
  152. function getPref($data_dir, $username, $string, $default ) {
  153. $db = new dbPrefs;
  154. if(isset($db->error)) {
  155. printf( _("Preference database error (%s). Exiting abnormally"),
  156. $db->error);
  157. exit;
  158. }
  159. return $db->getKey($username, $string);
  160. }
  161. /** Remove the pref $string **/
  162. function removePref($data_dir, $username, $string) {
  163. $db = new dbPrefs;
  164. if(isset($db->error)) $db->failQuery();
  165. $db->deleteKey($username, $string);
  166. return;
  167. }
  168. /** sets the pref, $string, to $set_to **/
  169. function setPref($data_dir, $username, $string, $set_to) {
  170. $db = new dbPrefs;
  171. if(isset($db->error))
  172. $db->failQuery();
  173. $db->setKey($username, $string, $set_to);
  174. return;
  175. }
  176. /** This checks if the prefs are available **/
  177. function checkForPrefs($data_dir, $username) {
  178. $db = new dbPrefs;
  179. if(isset($db->error))
  180. $db->failQuery();
  181. }
  182. /** Writes the Signature **/
  183. function setSig($data_dir, $username, $string) {
  184. $db = new dbPrefs;
  185. if(isset($db->error))
  186. $db->failQuery();
  187. $db->setKey($username, "___signature___", $string);
  188. return;
  189. }
  190. /** Gets the signature **/
  191. function getSig($data_dir, $username) {
  192. $db = new dbPrefs;
  193. if(isset($db->error))
  194. $db->failQuery();
  195. return $db->getKey($username, "___signature___");
  196. }
  197. ?>