prefs.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, after we've removed unwanted characters. */
  43. $datafile = str_replace('/', '_', $datafile);
  44. $result = "$real_hash_dir/$datafile";
  45. /* Check for this file in the real hash directory. */
  46. if ($hash_search && !@file_exists($result)) {
  47. /* First check the base directory, the most common location. */
  48. if (@file_exists("$dir/$datafile")) {
  49. rename("$dir/$datafile", $result);
  50. /* Then check the full range of possible hash directories. */
  51. } else {
  52. $check_hash_dir = $dir;
  53. for ($h = 0; $h < 4; ++$h) {
  54. $check_hash_dir .= '/' . $hash_dirs[$h];
  55. if (@is_readable("$check_hash_dir/$datafile")) {
  56. rename("$check_hash_dir/$datafile", $result);
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. /* Return the full hashed datafile path. */
  63. return ($result);
  64. }
  65. function getHashedDir($username, $dir, $hash_dirs = '') {
  66. global $dir_hash_level;
  67. /* Remove trailing slash from $dir if found */
  68. if (substr($dir, -1) == '/') {
  69. $dir = substr($dir, 0, strlen($dir) - 1);
  70. }
  71. /* If necessary, populate the hash dir variable. */
  72. if ($hash_dirs == '') {
  73. $hash_dirs = computeHashDirs($username);
  74. }
  75. /* Make sure the full hash directory exists. */
  76. $real_hash_dir = $dir;
  77. for ($h = 0; $h < $dir_hash_level; ++$h) {
  78. $real_hash_dir .= '/' . $hash_dirs[$h];
  79. if (!@is_dir($real_hash_dir)) {
  80. if (!@mkdir($real_hash_dir, 0770)) {
  81. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
  82. _("Could not create hashed directory structure!") . "<br>\n" .
  83. _("Please contact your system administrator and report this error.") . "<br>\n";
  84. exit;
  85. }
  86. }
  87. }
  88. /* And return that directory. */
  89. return ($real_hash_dir);
  90. }
  91. function computeHashDirs($username) {
  92. /* Compute the hash for this user and extract the hash directories. */
  93. $hash = base_convert(crc32($username), 10, 16);
  94. $hash_dirs = array();
  95. for ($h = 0; $h < 4; ++ $h) {
  96. $hash_dirs[] = substr($hash, $h, 1);
  97. }
  98. /* Return our array of hash directories. */
  99. return ($hash_dirs);
  100. }
  101. ?>