6 * This contains functions for manipulating user preferences
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
16 if (!defined('SM_PATH')) define('SM_PATH','../');
18 /** Include global.php */
19 require_once(SM_PATH
. 'functions/global.php');
20 require_once(SM_PATH
. 'functions/plugin.php');
22 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
23 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
25 if ( !sqsession_is_registered('prefs_are_cached') ||
26 !isset( $prefs_cache) ||
27 !is_array( $prefs_cache)
29 $prefs_are_cached = false;
30 $prefs_cache = array();
33 $prefs_backend = do_hook_function('prefs_backend');
34 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
35 require_once(SM_PATH
. $prefs_backend);
36 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
37 require_once(SM_PATH
. 'functions/db_prefs.php');
39 require_once(SM_PATH
. 'functions/file_prefs.php');
42 /* Hashing functions */
45 * Given a username and datafilename, this will return the path to the
46 * hashed location of that datafile.
48 * @param string username the username of the current user
49 * @param string dir the SquirrelMail datadir
50 * @param string datafile the name of the file to open
51 * @param bool hash_seach default true
52 * @return string the hashed location of datafile
55 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
57 /* Remove trailing slash from $dir if found */
58 if (substr($dir, -1) == '/') {
59 $dir = substr($dir, 0, strlen($dir) - 1);
62 /* Compute the hash for this user and extract the hash directories. */
63 $hash_dirs = computeHashDirs($username);
65 /* First, get and make sure the full hash directory exists. */
66 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
68 /* Set the value of our real data file, after we've removed unwanted characters. */
69 $datafile = str_replace('/', '_', $datafile);
70 $result = "$real_hash_dir/$datafile";
72 /* Check for this file in the real hash directory. */
73 if ($hash_search && !@file_exists
($result)) {
74 /* First check the base directory, the most common location. */
75 if (@file_exists
("$dir/$datafile")) {
76 rename("$dir/$datafile", $result);
78 /* Then check the full range of possible hash directories. */
80 $check_hash_dir = $dir;
81 for ($h = 0; $h < 4; ++
$h) {
82 $check_hash_dir .= '/' . $hash_dirs[$h];
83 if (@is_readable
("$check_hash_dir/$datafile")) {
84 rename("$check_hash_dir/$datafile", $result);
91 /* Return the full hashed datafile path. */
96 * Helper function for getHashedFile, given a username returns the hashed
97 * dir for that username.
99 * @param string username the username of the current user
100 * @param string dir the SquirrelMail datadir
101 * @param string hash_dirs default ''
102 * @return the path to the hash dir for username
105 function getHashedDir($username, $dir, $hash_dirs = '') {
106 global $dir_hash_level;
108 /* Remove trailing slash from $dir if found */
109 if (substr($dir, -1) == '/') {
110 $dir = substr($dir, 0, strlen($dir) - 1);
113 /* If necessary, populate the hash dir variable. */
114 if ($hash_dirs == '') {
115 $hash_dirs = computeHashDirs($username);
118 /* Make sure the full hash directory exists. */
119 $real_hash_dir = $dir;
120 for ($h = 0; $h < $dir_hash_level; ++
$h) {
121 $real_hash_dir .= '/' . $hash_dirs[$h];
122 if (!@is_dir
($real_hash_dir)) {
123 if (!@mkdir
($real_hash_dir, 0770)) {
124 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
125 _("Could not create hashed directory structure!") . "<br />\n" .
126 _("Please contact your system administrator and report this error.") . "<br />\n";
132 /* And return that directory. */
133 return ($real_hash_dir);
137 * Helper function for getHashDir which does the actual hash calculation.
139 * @param string username the username to calculate the hash dir for
140 * @return array a list of hash dirs for this username
143 function computeHashDirs($username) {
144 /* Compute the hash for this user and extract the hash directories. */
145 $hash = base_convert(crc32($username), 10, 16);
146 $hash_dirs = array();
147 for ($h = 0; $h < 4; ++
$h) {
148 $hash_dirs[] = substr($hash, $h, 1);
151 /* Return our array of hash directories. */
156 * Javascript support detection function
157 * @param boolean $reset recheck javascript support if set to true.
158 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php})
161 function checkForJavascript($reset = FALSE) {
162 global $data_dir, $username, $javascript_on, $javascript_setting;
164 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION
) )
165 return $javascript_on;
167 if ( $reset ||
!isset($javascript_setting) )
168 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT
);
170 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
171 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
172 $js_autodetect_results = SMPREF_JS_OFF
;
174 if ( $javascript_setting == SMPREF_JS_AUTODETECT
)
175 $javascript_on = $js_autodetect_results;
177 $javascript_on = $javascript_setting;
179 sqsession_register($javascript_on, 'javascript_on');
180 return $javascript_on;