db_prefs.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /*****************************************************************/
  34. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  35. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  36. /*** + Base level indent should begin at left margin, as ***/
  37. /*** the require_once below. ***/
  38. /*** + All identation should consist of four space blocks ***/
  39. /*** + Tab characters are evil. ***/
  40. /*** + all comments should use "slash-star ... star-slash" ***/
  41. /*** style -- no pound characters, no slash-slash style ***/
  42. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  43. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  44. /*** + Please use ' instead of ", when possible. Note " ***/
  45. /*** should always be used in _( ) function calls. ***/
  46. /*** Thank you for your help making the SM code more readable. ***/
  47. /*****************************************************************/
  48. require_once('DB.php');
  49. class dbPrefs {
  50. var $DSN = 'mysql://user@host/database';
  51. var $table = 'userprefs';
  52. var $dbh = NULL;
  53. var $error = NULL;
  54. var $default = Array('chosen_theme' => '../themes/default_theme.php',
  55. 'show_html_default' => '0');
  56. function dbPrefs() {
  57. $this->open();
  58. }
  59. function open() {
  60. if(isset($this->dbh)) return true;
  61. $dbh = DB::connect($this->DSN, true);
  62. if(DB::isError($dbh) || DB::isWarning($dbh)) {
  63. $this->error = DB::errorMessage($dbh);
  64. return false;
  65. }
  66. $this->dbh = $dbh;
  67. return true;
  68. }
  69. function failQuery($res = NULL) {
  70. if($res == NULL) {
  71. printf(_("Preference database error (%s). Exiting abnormally"),
  72. $this->error);
  73. } else {
  74. printf(_("Preference database error (%s). Exiting abnormally"),
  75. DB::errorMessage($res));
  76. }
  77. exit;
  78. }
  79. function getKey($user, $key) {
  80. $this->open();
  81. $query = sprintf("SELECT prefval FROM %s ".
  82. "WHERE user='%s' AND prefkey='%s'",
  83. $this->table,
  84. $this->dbh->quoteString($user),
  85. $this->dbh->quoteString($key));
  86. $res = $this->dbh->query($query);
  87. if(DB::isError($res))
  88. $this->failQuery($res);
  89. if($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  90. return $row['prefval'];
  91. } else {
  92. if(isset($this->default[$key])) {
  93. return $this->default[$key];
  94. } else {
  95. return '';
  96. }
  97. }
  98. return '';
  99. }
  100. function deleteKey($user, $key) {
  101. $this->open();
  102. $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
  103. $this->table,
  104. $this->dbh->quoteString($user),
  105. $this->dbh->quoteString($key));
  106. $res = $this->dbh->simpleQuery($query);
  107. if(DB::isError($res))
  108. $this->failQuery($res);
  109. if(substr($key, 0, 9) == 'highlight') {
  110. $this->renumberHighlightList($user);
  111. }
  112. return true;
  113. }
  114. function setKey($user, $key, $value) {
  115. $this->open();
  116. $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
  117. "VALUES('%s','%s','%s')",
  118. $this->table,
  119. $this->dbh->quoteString($user),
  120. $this->dbh->quoteString($key),
  121. $this->dbh->quoteString($value));
  122. $res = $this->dbh->simpleQuery($query);
  123. if(DB::isError($res))
  124. $this->failQuery($res);
  125. return true;
  126. }
  127. /**
  128. ** When a highlight option is deleted the preferences module
  129. ** must renumber the list. This should be done somewhere else,
  130. ** but it is not, so....
  131. **/
  132. function renumberHighlightList($user) {
  133. $this->open();
  134. $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
  135. "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
  136. $this->table,
  137. $this->dbh->quoteString($user));
  138. $res = $this->dbh->query($query);
  139. if(DB::isError($res))
  140. $this->failQuery($res);
  141. // Store old data in array
  142. $rows = Array();
  143. while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
  144. $rows[] = $row;
  145. // Renumber keys of old data
  146. $hilinum = 0;
  147. for($i = 0; $i < count($rows) ; $i++) {
  148. $oldkey = $rows[$i]['prefkey'];
  149. $newkey = substr($oldkey, 0, 9) . $hilinum;
  150. $hilinum++;
  151. if($oldkey != $newkey) {
  152. $query = sprintf("UPDATE %s SET prefkey='%s' WHERE user='%s' ".
  153. "AND prefkey='%s'",
  154. $this->table,
  155. $this->dbh->quoteString($newkey),
  156. $this->dbh->quoteString($user),
  157. $this->dbh->quoteString($oldkey));
  158. $res = $this->dbh->simpleQuery($query);
  159. if(DB::isError($res))
  160. $this->failQuery($res);
  161. }
  162. }
  163. return;
  164. }
  165. } // end class dbPrefs
  166. /** returns the value for the pref $string **/
  167. function getPref($data_dir, $username, $string, $default ) {
  168. $db = new dbPrefs;
  169. if(isset($db->error)) {
  170. printf( _("Preference database error (%s). Exiting abnormally"),
  171. $db->error);
  172. exit;
  173. }
  174. return $db->getKey($username, $string);
  175. }
  176. /** Remove the pref $string **/
  177. function removePref($data_dir, $username, $string) {
  178. $db = new dbPrefs;
  179. if(isset($db->error)) $db->failQuery();
  180. $db->deleteKey($username, $string);
  181. return;
  182. }
  183. /** sets the pref, $string, to $set_to **/
  184. function setPref($data_dir, $username, $string, $set_to) {
  185. $db = new dbPrefs;
  186. if(isset($db->error))
  187. $db->failQuery();
  188. $db->setKey($username, $string, $set_to);
  189. return;
  190. }
  191. /** This checks if the prefs are available **/
  192. function checkForPrefs($data_dir, $username) {
  193. $db = new dbPrefs;
  194. if(isset($db->error))
  195. $db->failQuery();
  196. }
  197. /** Writes the Signature **/
  198. function setSig($data_dir, $username, $string) {
  199. $db = new dbPrefs;
  200. if(isset($db->error))
  201. $db->failQuery();
  202. $db->setKey($username, "___signature___", $string);
  203. return;
  204. }
  205. /** Gets the signature **/
  206. function getSig($data_dir, $username) {
  207. $db = new dbPrefs;
  208. if(isset($db->error))
  209. $db->failQuery();
  210. return $db->getKey($username, "___signature___");
  211. }
  212. ?>