prefs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * prefs.php
  4. *
  5. * Copyright (c) 1999-2004 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. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** Include global.php */
  14. require_once(SM_PATH . 'functions/global.php');
  15. sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
  16. sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
  17. if ( !sqsession_is_registered('prefs_are_cached') ||
  18. !isset( $prefs_cache) ||
  19. !is_array( $prefs_cache)
  20. ) {
  21. $prefs_are_cached = false;
  22. $prefs_cache = array();
  23. }
  24. if (isset($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
  25. require_once(SM_PATH . $prefs_backend);
  26. } elseif (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. /**
  33. * Given a username and datafilename, this will return the path to the
  34. * hashed location of that datafile.
  35. *
  36. * @param string username the username of the current user
  37. * @param string dir the squirrelmail datadir
  38. * @param string datafile the name of the file to open
  39. * @param bool hash_seach default true
  40. * @return string the hashed location of datafile
  41. */
  42. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  43. global $dir_hash_level;
  44. /* Remove trailing slash from $dir if found */
  45. if (substr($dir, -1) == '/') {
  46. $dir = substr($dir, 0, strlen($dir) - 1);
  47. }
  48. /* Compute the hash for this user and extract the hash directories. */
  49. $hash_dirs = computeHashDirs($username);
  50. /* First, get and make sure the full hash directory exists. */
  51. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  52. /* Set the value of our real data file, after we've removed unwanted characters. */
  53. $datafile = str_replace('/', '_', $datafile);
  54. $result = "$real_hash_dir/$datafile";
  55. /* Check for this file in the real hash directory. */
  56. if ($hash_search && !@file_exists($result)) {
  57. /* First check the base directory, the most common location. */
  58. if (@file_exists("$dir/$datafile")) {
  59. rename("$dir/$datafile", $result);
  60. /* Then check the full range of possible hash directories. */
  61. } else {
  62. $check_hash_dir = $dir;
  63. for ($h = 0; $h < 4; ++$h) {
  64. $check_hash_dir .= '/' . $hash_dirs[$h];
  65. if (@is_readable("$check_hash_dir/$datafile")) {
  66. rename("$check_hash_dir/$datafile", $result);
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. /* Return the full hashed datafile path. */
  73. return ($result);
  74. }
  75. /**
  76. * Helper function for getHashedFile, given a username returns the hashed
  77. * dir for that username.
  78. *
  79. * @param string username the username of the current user
  80. * @param string dir the squirrelmail datadir
  81. * @param string hash_dirs default ''
  82. * @return the path to the hash dir for username
  83. */
  84. function getHashedDir($username, $dir, $hash_dirs = '') {
  85. global $dir_hash_level;
  86. /* Remove trailing slash from $dir if found */
  87. if (substr($dir, -1) == '/') {
  88. $dir = substr($dir, 0, strlen($dir) - 1);
  89. }
  90. /* If necessary, populate the hash dir variable. */
  91. if ($hash_dirs == '') {
  92. $hash_dirs = computeHashDirs($username);
  93. }
  94. /* Make sure the full hash directory exists. */
  95. $real_hash_dir = $dir;
  96. for ($h = 0; $h < $dir_hash_level; ++$h) {
  97. $real_hash_dir .= '/' . $hash_dirs[$h];
  98. if (!@is_dir($real_hash_dir)) {
  99. if (!@mkdir($real_hash_dir, 0770)) {
  100. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
  101. _("Could not create hashed directory structure!") . "<br>\n" .
  102. _("Please contact your system administrator and report this error.") . "<br>\n";
  103. exit;
  104. }
  105. }
  106. }
  107. /* And return that directory. */
  108. return ($real_hash_dir);
  109. }
  110. /**
  111. * Helper function for getHashDir which does the actual hash calculation.
  112. *
  113. * @param string username the username to calculate the hash dir for
  114. * @return array a list of hash dirs for this username
  115. */
  116. function computeHashDirs($username) {
  117. /* Compute the hash for this user and extract the hash directories. */
  118. $hash = base_convert(crc32($username), 10, 16);
  119. $hash_dirs = array();
  120. for ($h = 0; $h < 4; ++ $h) {
  121. $hash_dirs[] = substr($hash, $h, 1);
  122. }
  123. /* Return our array of hash directories. */
  124. return ($hash_dirs);
  125. }
  126. function checkForJavascript($reset = FALSE)
  127. {
  128. global $data_dir, $username, $javascript_on, $javascript_setting;
  129. if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
  130. return $javascript_on;
  131. if ( $reset || !isset($javascript_setting) )
  132. $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
  133. if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
  134. !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
  135. $js_autodetect_results = SMPREF_JS_OFF;
  136. if ( $javascript_setting == SMPREF_JS_AUTODETECT )
  137. $javascript_on = $js_autodetect_results;
  138. else
  139. $javascript_on = $javascript_setting;
  140. sqsession_register($javascript_on, 'javascript_on');
  141. return $javascript_on;
  142. }
  143. ?>