db_prefs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 tree columns:
  19. * username 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 changing $DSN below.
  30. *
  31. * $Id$
  32. */
  33. require_once('DB.php');
  34. global $prefs_are_cached, $prefs_cache;
  35. if ( !session_is_registered('prefs_are_cached') ||
  36. !isset( $prefs_cache) ||
  37. !is_array( $prefs_cache) ||
  38. substr( phpversion(), 0, 3 ) == '4.1' ) {
  39. $prefs_are_cached = false;
  40. $prefs_cache = array();
  41. }
  42. function cachePrefValues($username) {
  43. global $prefs_are_cached, $prefs_cache;
  44. if ($prefs_are_cached) {
  45. return;
  46. }
  47. session_unregister('prefs_cache');
  48. session_unregister('prefs_are_cached');
  49. $db = new dbPrefs;
  50. if(isset($db->error)) {
  51. printf( _("Preference database error (%s). Exiting abnormally"),
  52. $db->error);
  53. exit;
  54. }
  55. $db->fillPrefsCache($username);
  56. if (isset($db->error)) {
  57. printf( _("Preference database error (%s). Exiting abnormally"),
  58. $db->error);
  59. exit;
  60. }
  61. $prefs_are_cached = true;
  62. session_register('prefs_cache');
  63. session_register('prefs_are_cached');
  64. }
  65. class dbPrefs {
  66. var $DSN = 'mysql://user:passwd@host/database';
  67. var $table = 'userprefs';
  68. var $dbh = NULL;
  69. var $error = NULL;
  70. var $default = Array('chosen_theme' => '../themes/default_theme.php',
  71. 'show_html_default' => '0');
  72. function open() {
  73. if(isset($this->dbh)) {
  74. return true;
  75. }
  76. $dbh = DB::connect($this->DSN, true);
  77. if(DB::isError($dbh) || DB::isWarning($dbh)) {
  78. $this->error = DB::errorMessage($dbh);
  79. return false;
  80. }
  81. $this->dbh = $dbh;
  82. return true;
  83. }
  84. function failQuery($res = NULL) {
  85. if($res == NULL) {
  86. printf(_("Preference database error (%s). Exiting abnormally"),
  87. $this->error);
  88. } else {
  89. printf(_("Preference database error (%s). Exiting abnormally"),
  90. DB::errorMessage($res));
  91. }
  92. exit;
  93. }
  94. function getKey($user, $key, $default = '') {
  95. global $prefs_cache;
  96. cachePrefValues($user);
  97. if (isset($prefs_cache[$key])) {
  98. return $prefs_cache[$key];
  99. } else {
  100. if (isset($this->default[$key])) {
  101. return $this->default[$key];
  102. } else {
  103. return $default;
  104. }
  105. }
  106. }
  107. function deleteKey($user, $key) {
  108. global $prefs_cache;
  109. $this->open();
  110. $query = sprintf("DELETE FROM %s WHERE user='%s' AND prefkey='%s'",
  111. $this->table,
  112. $this->dbh->quoteString($user),
  113. $this->dbh->quoteString($key));
  114. $res = $this->dbh->simpleQuery($query);
  115. if(DB::isError($res)) {
  116. $this->failQuery($res);
  117. }
  118. unset($prefs_cache[$key]);
  119. if(substr($key, 0, 9) == 'highlight') {
  120. $this->renumberHighlightList($user);
  121. }
  122. return true;
  123. }
  124. function setKey($user, $key, $value) {
  125. $this->open();
  126. $query = sprintf("REPLACE INTO %s (user,prefkey,prefval) ".
  127. "VALUES('%s','%s','%s')",
  128. $this->table,
  129. $this->dbh->quoteString($user),
  130. $this->dbh->quoteString($key),
  131. $this->dbh->quoteString($value));
  132. $res = $this->dbh->simpleQuery($query);
  133. if(DB::isError($res)) {
  134. $this->failQuery($res);
  135. }
  136. return true;
  137. }
  138. function fillPrefsCache($user) {
  139. global $prefs_cache;
  140. $this->open();
  141. $prefs_cache = array();
  142. $query = sprintf("SELECT prefkey, prefval FROM %s ".
  143. "WHERE user = '%s'",
  144. $this->table,
  145. $this->dbh->quoteString($user));
  146. $res = $this->dbh->query($query);
  147. if (DB::isError($res)) {
  148. $this->failQuery($res);
  149. }
  150. while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  151. $prefs_cache[$row['prefkey']] = $row['prefval'];
  152. }
  153. }
  154. /*
  155. * When a highlight option is deleted the preferences module
  156. * must renumber the list. This should be done somewhere else,
  157. * but it is not, so....
  158. */
  159. function renumberHighlightList($user) {
  160. $this->open();
  161. $query = sprintf("SELECT * FROM %s WHERE user='%s' ".
  162. "AND prefkey LIKE 'highlight%%' ORDER BY prefkey",
  163. $this->table,
  164. $this->dbh->quoteString($user));
  165. $res = $this->dbh->query($query);
  166. if(DB::isError($res)) {
  167. $this->failQuery($res);
  168. }
  169. /* Store old data in array */
  170. $rows = Array();
  171. while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  172. $rows[] = $row;
  173. }
  174. /* Renumber keys of old data */
  175. $hilinum = 0;
  176. for($i = 0; $i < count($rows) ; $i++) {
  177. $oldkey = $rows[$i]['prefkey'];
  178. $newkey = substr($oldkey, 0, 9) . $hilinum;
  179. $hilinum++;
  180. if($oldkey != $newkey) {
  181. $query = sprintf("UPDATE %s SET prefkey='%s' ".
  182. "WHERE user ='%s' AND prefkey='%s'",
  183. $this->table,
  184. $this->dbh->quoteString($newkey),
  185. $this->dbh->quoteString($user),
  186. $this->dbh->quoteString($oldkey));
  187. $res = $this->dbh->simpleQuery($query);
  188. if(DB::isError($res)) {
  189. $this->failQuery($res);
  190. }
  191. }
  192. }
  193. return;
  194. }
  195. } /* end class dbPrefs */
  196. /* returns the value for the pref $string */
  197. function getPref($data_dir, $username, $string, $default = '') {
  198. $db = new dbPrefs;
  199. if(isset($db->error)) {
  200. printf( _("Preference database error (%s). Exiting abnormally"),
  201. $db->error);
  202. exit;
  203. }
  204. return $db->getKey($username, $string, $default);
  205. }
  206. /* Remove the pref $string */
  207. function removePref($data_dir, $username, $string) {
  208. $db = new dbPrefs;
  209. if(isset($db->error)) {
  210. $db->failQuery();
  211. }
  212. $db->deleteKey($username, $string);
  213. return;
  214. }
  215. /* sets the pref, $string, to $set_to */
  216. function setPref($data_dir, $username, $string, $set_to) {
  217. global $prefs_cache;
  218. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $set_to)) {
  219. return;
  220. }
  221. if ($set_to == '') {
  222. removePref($data_dir, $username, $string);
  223. return;
  224. }
  225. $db = new dbPrefs;
  226. if(isset($db->error)) {
  227. $db->failQuery();
  228. }
  229. $db->setKey($username, $string, $set_to);
  230. $prefs_cache[$string] = $set_to;
  231. assert_options(ASSERT_ACTIVE, 1);
  232. assert_options(ASSERT_BAIL, 1);
  233. assert ('$set_to == $prefs_cache[$string]');
  234. return;
  235. }
  236. /* This checks if the prefs are available */
  237. function checkForPrefs($data_dir, $username) {
  238. $db = new dbPrefs;
  239. if(isset($db->error)) {
  240. $db->failQuery();
  241. }
  242. }
  243. /* Writes the Signature */
  244. function setSig($data_dir, $username, $string) {
  245. $db = new dbPrefs;
  246. if(isset($db->error)) {
  247. $db->failQuery();
  248. }
  249. $db->setKey($username, '___signature___', $string);
  250. return;
  251. }
  252. /* Gets the signature */
  253. function getSig($data_dir, $username) {
  254. $db = new dbPrefs;
  255. if(isset($db->error)) {
  256. $db->failQuery();
  257. }
  258. return $db->getKey($username, '___signature___');
  259. }
  260. /* Hashing functions */
  261. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  262. global $dir_hash_level;
  263. /* Remove trailing slash from $dir if found */
  264. if (substr($dir, -1) == '/') {
  265. $dir = substr($dir, 0, strlen($dir) - 1);
  266. }
  267. /* Compute the hash for this user and extract the hash directories. */
  268. $hash_dirs = computeHashDirs($username);
  269. /* First, get and make sure the full hash directory exists. */
  270. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  271. /* Set the value of our real data file. */
  272. $result = "$real_hash_dir/$datafile";
  273. /* Check for this file in the real hash directory. */
  274. if ($hash_search && !@file_exists($result)) {
  275. /* First check the base directory, the most common location. */
  276. if (@file_exists("$dir/$datafile")) {
  277. rename("$dir/$datafile", $result);
  278. /* Then check the full range of possible hash directories. */
  279. } else {
  280. $check_hash_dir = $dir;
  281. for ($h = 0; $h < 4; ++$h) {
  282. $check_hash_dir .= '/' . $hash_dirs[$h];
  283. if (@is_readable("$check_hash_dir/$datafile")) {
  284. rename("$check_hash_dir/$datafile", $result);
  285. break;
  286. }
  287. }
  288. }
  289. }
  290. /* Return the full hashed datafile path. */
  291. return ($result);
  292. }
  293. function getHashedDir($username, $dir, $hash_dirs = '') {
  294. global $dir_hash_level;
  295. /* Remove trailing slash from $dir if found */
  296. if (substr($dir, -1) == '/') {
  297. $dir = substr($dir, 0, strlen($dir) - 1);
  298. }
  299. /* If necessary, populate the hash dir variable. */
  300. if ($hash_dirs == '') {
  301. $hash_dirs = computeHashDirs($username);
  302. }
  303. /* Make sure the full hash directory exists. */
  304. $real_hash_dir = $dir;
  305. for ($h = 0; $h < $dir_hash_level; ++$h) {
  306. $real_hash_dir .= '/' . $hash_dirs[$h];
  307. if (!@is_dir($real_hash_dir)) {
  308. if (!@mkdir($real_hash_dir, 0770)) {
  309. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>';
  310. echo _("Could not create hashed directory structure!") . "<br>\n";
  311. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  312. exit;
  313. }
  314. }
  315. }
  316. /* And return that directory. */
  317. return ($real_hash_dir);
  318. }
  319. function computeHashDirs($username) {
  320. /* Compute the hash for this user and extract the hash directories. */
  321. $hash = base_convert(crc32($username), 10, 16);
  322. $hash_dirs = array();
  323. for ($h = 0; $h < 4; ++ $h) {
  324. $hash_dirs[] = substr($hash, $h, 1);
  325. }
  326. /* Return our array of hash directories. */
  327. return ($hash_dirs);
  328. }
  329. ?>