db_prefs.php 12 KB

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