prefs.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. require_once(SM_PATH . 'functions/global.php');
  13. if (isset($_SESSION['prefs_cache'])) {
  14. $prefs_cache = $_SESSION['prefs_cache'];
  15. }
  16. if (isset($_SESSION['prefs_are_cached'])) {
  17. $prefs_are_cached = $_SESSION['prefs_are_cached'];
  18. }
  19. $rg = ini_get('register_globals');
  20. if ( !sqsession_is_registered('prefs_are_cached') ||
  21. !isset( $prefs_cache) ||
  22. !is_array( $prefs_cache) ||
  23. substr( phpversion(), 0, 3 ) == '4.1' ||
  24. substr( phpversion(), 0, 3 ) == '4.2' ||
  25. (substr( phpversion(), 0, 3 ) == '4.0' && empty($rg))) {
  26. $prefs_are_cached = false;
  27. $prefs_cache = array();
  28. }
  29. if (isset($prefs_dsn) && !empty($prefs_dsn)) {
  30. require_once(SM_PATH . 'functions/db_prefs.php');
  31. } else {
  32. require_once(SM_PATH . 'functions/file_prefs.php');
  33. }
  34. /* Hashing functions */
  35. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  36. global $dir_hash_level;
  37. /* Remove trailing slash from $dir if found */
  38. if (substr($dir, -1) == '/') {
  39. $dir = substr($dir, 0, strlen($dir) - 1);
  40. }
  41. /* Compute the hash for this user and extract the hash directories. */
  42. $hash_dirs = computeHashDirs($username);
  43. /* First, get and make sure the full hash directory exists. */
  44. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  45. /* Set the value of our real data file. */
  46. $result = "$real_hash_dir/$datafile";
  47. /* Check for this file in the real hash directory. */
  48. if ($hash_search && !@file_exists($result)) {
  49. /* First check the base directory, the most common location. */
  50. if (@file_exists("$dir/$datafile")) {
  51. rename("$dir/$datafile", $result);
  52. /* Then check the full range of possible hash directories. */
  53. } else {
  54. $check_hash_dir = $dir;
  55. for ($h = 0; $h < 4; ++$h) {
  56. $check_hash_dir .= '/' . $hash_dirs[$h];
  57. if (@is_readable("$check_hash_dir/$datafile")) {
  58. rename("$check_hash_dir/$datafile", $result);
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. /* Return the full hashed datafile path. */
  65. return ($result);
  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. function computeHashDirs($username) {
  94. /* Compute the hash for this user and extract the hash directories. */
  95. $hash = base_convert(crc32($username), 10, 16);
  96. $hash_dirs = array();
  97. for ($h = 0; $h < 4; ++ $h) {
  98. $hash_dirs[] = substr($hash, $h, 1);
  99. }
  100. /* Return our array of hash directories. */
  101. return ($hash_dirs);
  102. }
  103. ?>