prefs.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * prefs.php
  4. *
  5. * This contains functions for filebased user prefs locations
  6. *
  7. * @copyright 1999-2025 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage prefs
  12. */
  13. /* Hashing functions */
  14. /**
  15. * Given a username and datafilename, this will return the path to the
  16. * hashed location of that datafile.
  17. *
  18. * @param string username the username of the current user
  19. * @param string dir the SquirrelMail datadir
  20. * @param string datafile the name of the file to open
  21. * @param bool hash_seach default true
  22. * @return string the hashed location of datafile
  23. * @since 1.2.0
  24. */
  25. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  26. /* Remove trailing slash from $dir if found */
  27. if (substr($dir, -1) == '/') {
  28. $dir = substr($dir, 0, strlen($dir) - 1);
  29. }
  30. /* Compute the hash for this user and extract the hash directories. */
  31. $hash_dirs = computeHashDirs($username);
  32. /* First, get and make sure the full hash directory exists. */
  33. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  34. /* Set the value of our real data file, after we've removed unwanted characters. */
  35. $datafile = str_replace('/', '_', $datafile);
  36. $result = "$real_hash_dir/$datafile";
  37. /* Check for this file in the real hash directory. */
  38. if ($hash_search && !@file_exists($result)) {
  39. /* First check the base directory, the most common location. */
  40. if (@file_exists("$dir/$datafile")) {
  41. rename("$dir/$datafile", $result);
  42. /* Then check the full range of possible hash directories. */
  43. } else {
  44. $check_hash_dir = $dir;
  45. for ($h = 0; $h < 4; ++$h) {
  46. $check_hash_dir .= '/' . $hash_dirs[$h];
  47. if (@is_readable("$check_hash_dir/$datafile")) {
  48. rename("$check_hash_dir/$datafile", $result);
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. /* Return the full hashed datafile path. */
  55. return ($result);
  56. }
  57. /**
  58. * Helper function for getHashedFile, given a username returns the hashed
  59. * dir for that username.
  60. *
  61. * @param string username the username of the current user
  62. * @param string dir the SquirrelMail datadir
  63. * @param string hash_dirs default ''
  64. * @return the path to the hash dir for username
  65. * @since 1.2.0
  66. */
  67. function getHashedDir($username, $dir, $hash_dirs = '') {
  68. global $dir_hash_level;
  69. /* Remove trailing slash from $dir if found */
  70. if (substr($dir, -1) == '/') {
  71. $dir = substr($dir, 0, strlen($dir) - 1);
  72. }
  73. /* If necessary, populate the hash dir variable. */
  74. if ($hash_dirs == '') {
  75. $hash_dirs = computeHashDirs($username);
  76. }
  77. /* Make sure the full hash directory exists. */
  78. $real_hash_dir = $dir;
  79. for ($h = 0; $h < $dir_hash_level; ++$h) {
  80. $real_hash_dir .= '/' . $hash_dirs[$h];
  81. if (!@is_dir($real_hash_dir)) {
  82. //FIXME: When safe_mode is turned on, the error suppression below makes debugging safe_mode UID/GID restrictions tricky... for now, I will add a check in configtest
  83. if (!@mkdir($real_hash_dir, 0770)) {
  84. error_box ( sprintf(_("Error creating directory %s."), $real_hash_dir) . "\n" .
  85. _("Could not create hashed directory structure!") . "\n" .
  86. _("Please contact your system administrator and report this error.") );
  87. exit;
  88. }
  89. }
  90. }
  91. /* And return that directory. */
  92. return ($real_hash_dir);
  93. }
  94. /**
  95. * Helper function for getHashDir which does the actual hash calculation.
  96. *
  97. * Uses a crc32 algorithm by default, but if you put
  98. * the following in config/config_local.php, you can
  99. * force md5 instead:
  100. * $hash_dirs_use_md5 = TRUE;
  101. *
  102. * You may also specify that if usernames are in full
  103. * email address format, the domain part (beginning
  104. * with "@") be stripped before calculating the crc
  105. * or md5. Do that by putting the following in
  106. * config/config_local.php:
  107. * $hash_dirs_strip_domain = TRUE;
  108. *
  109. * @param string username the username to calculate the hash dir for
  110. *
  111. * @return array a list of hash dirs for this username
  112. *
  113. * @since 1.2.0
  114. *
  115. */
  116. function computeHashDirs($username) {
  117. global $hash_dirs_use_md5, $hash_dirs_strip_domain;
  118. static $hash_dirs = array();
  119. // strip domain from username
  120. if ($hash_dirs_strip_domain)
  121. $user = substr($username, 0, strpos($username, '@'));
  122. else
  123. $user = $username;
  124. // have we already calculated it?
  125. if (!empty($hash_dirs[$user]))
  126. return $hash_dirs[$user];
  127. if ($hash_dirs_use_md5) {
  128. $hash = md5($user);
  129. //$hash = md5bin($user);
  130. } else {
  131. /* Compute the hash for this user and extract the hash directories. */
  132. /* Note that the crc32() function result will be different on 32 and */
  133. /* 64 bit systems, thus the hack below. */
  134. $crc = crc32($user);
  135. if ($crc & 0x80000000) {
  136. $crc ^= 0xffffffff;
  137. $crc += 1;
  138. }
  139. $hash = base_convert($crc, 10, 16);
  140. }
  141. $my_hash_dirs = array();
  142. for ($h = 0; $h < 4; ++ $h) {
  143. $my_hash_dirs[] = substr($hash, $h, 1);
  144. }
  145. // Return our array of hash directories
  146. $hash_dirs[$user] = $my_hash_dirs;
  147. return ($my_hash_dirs);
  148. }