prefs.php 5.8 KB

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