prefs.php 3.6 KB

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