prefs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * prefs.php
  4. *
  5. * This contains functions for filebased user prefs locations
  6. *
  7. * @copyright &copy; 1999-2007 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. if (!@mkdir($real_hash_dir, 0770)) {
  83. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
  84. _("Could not create hashed directory structure!") . "<br />\n" .
  85. _("Please contact your system administrator and report this error.") . "<br />\n";
  86. exit;
  87. }
  88. }
  89. }
  90. /* And return that directory. */
  91. return ($real_hash_dir);
  92. }
  93. /**
  94. * Helper function for getHashDir which does the actual hash calculation.
  95. *
  96. * @param string username the username to calculate the hash dir for
  97. * @return array a list of hash dirs for this username
  98. * @since 1.2.0
  99. */
  100. function computeHashDirs($username) {
  101. /* Compute the hash for this user and extract the hash directories. */
  102. $hash = base_convert(crc32($username), 10, 16);
  103. $hash_dirs = array();
  104. for ($h = 0; $h < 4; ++ $h) {
  105. $hash_dirs[] = substr($hash, $h, 1);
  106. }
  107. /* Return our array of hash directories. */
  108. return ($hash_dirs);
  109. }