prefs.php 3.4 KB

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