prefs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * 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. *
  10. * $Id$
  11. */
  12. global $prefs_are_cached, $prefs_cache;
  13. if ( !session_is_registered('prefs_are_cached') ||
  14. !isset( $prefs_cache) ||
  15. !is_array( $prefs_cache) ||
  16. substr( phpversion(), 0, 3 ) == '4.1' ||
  17. substr( phpversion(), 0, 3 ) == '4.2' ) {
  18. $prefs_are_cached = false;
  19. $prefs_cache = array();
  20. }
  21. if (isset($prefs_dsn) && !empty($prefs_dsn)) {
  22. require_once('../functions/db_prefs.php');
  23. } else {
  24. require_once('../functions/file_prefs.php');
  25. }
  26. /* Hashing functions */
  27. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  28. global $dir_hash_level;
  29. /* Remove trailing slash from $dir if found */
  30. if (substr($dir, -1) == '/') {
  31. $dir = substr($dir, 0, strlen($dir) - 1);
  32. }
  33. /* Compute the hash for this user and extract the hash directories. */
  34. $hash_dirs = computeHashDirs($username);
  35. /* First, get and make sure the full hash directory exists. */
  36. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  37. /* Set the value of our real data file. */
  38. $result = "$real_hash_dir/$datafile";
  39. /* Check for this file in the real hash directory. */
  40. if ($hash_search && !@file_exists($result)) {
  41. /* First check the base directory, the most common location. */
  42. if (@file_exists("$dir/$datafile")) {
  43. rename("$dir/$datafile", $result);
  44. /* Then check the full range of possible hash directories. */
  45. } else {
  46. $check_hash_dir = $dir;
  47. for ($h = 0; $h < 4; ++$h) {
  48. $check_hash_dir .= '/' . $hash_dirs[$h];
  49. if (@is_readable("$check_hash_dir/$datafile")) {
  50. rename("$check_hash_dir/$datafile", $result);
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. /* Return the full hashed datafile path. */
  57. return ($result);
  58. }
  59. function getHashedDir($username, $dir, $hash_dirs = '') {
  60. global $dir_hash_level;
  61. /* Remove trailing slash from $dir if found */
  62. if (substr($dir, -1) == '/') {
  63. $dir = substr($dir, 0, strlen($dir) - 1);
  64. }
  65. /* If necessary, populate the hash dir variable. */
  66. if ($hash_dirs == '') {
  67. $hash_dirs = computeHashDirs($username);
  68. }
  69. /* Make sure the full hash directory exists. */
  70. $real_hash_dir = $dir;
  71. for ($h = 0; $h < $dir_hash_level; ++$h) {
  72. $real_hash_dir .= '/' . $hash_dirs[$h];
  73. if (!@is_dir($real_hash_dir)) {
  74. if (!@mkdir($real_hash_dir, 0770)) {
  75. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
  76. _("Could not create hashed directory structure!") . "<br>\n" .
  77. _("Please contact your system administrator and report this error.") . "<br>\n";
  78. exit;
  79. }
  80. }
  81. }
  82. /* And return that directory. */
  83. return ($real_hash_dir);
  84. }
  85. function computeHashDirs($username) {
  86. /* Compute the hash for this user and extract the hash directories. */
  87. $hash = base_convert(crc32($username), 10, 16);
  88. $hash_dirs = array();
  89. for ($h = 0; $h < 4; ++ $h) {
  90. $hash_dirs[] = substr($hash, $h, 1);
  91. }
  92. /* Return our array of hash directories. */
  93. return ($hash_dirs);
  94. }
  95. ?>