prefs.php 5.5 KB

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