db_prefs.php 11 KB

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