prefs.php 3.6 KB

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