db_prefs.php 11 KB

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