6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
12 * @package squirrelmail
15 /** Include global.php */
16 require_once(SM_PATH
. 'functions/global.php');
17 require_once(SM_PATH
. 'functions/plugin.php');
19 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
20 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
22 if ( !sqsession_is_registered('prefs_are_cached') ||
23 !isset( $prefs_cache) ||
24 !is_array( $prefs_cache)
26 $prefs_are_cached = false;
27 $prefs_cache = array();
30 $prefs_backend = do_hook_function('prefs_backend');
31 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
32 require_once(SM_PATH
. $prefs_backend);
33 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
34 require_once(SM_PATH
. 'functions/db_prefs.php');
36 require_once(SM_PATH
. 'functions/file_prefs.php');
39 /* Hashing functions */
42 * Given a username and datafilename, this will return the path to the
43 * hashed location of that datafile.
45 * @param string username the username of the current user
46 * @param string dir the SquirrelMail datadir
47 * @param string datafile the name of the file to open
48 * @param bool hash_seach default true
49 * @return string the hashed location of datafile
51 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
53 /* Remove trailing slash from $dir if found */
54 if (substr($dir, -1) == '/') {
55 $dir = substr($dir, 0, strlen($dir) - 1);
58 /* Compute the hash for this user and extract the hash directories. */
59 $hash_dirs = computeHashDirs($username);
61 /* First, get and make sure the full hash directory exists. */
62 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
64 /* Set the value of our real data file, after we've removed unwanted characters. */
65 $datafile = str_replace('/', '_', $datafile);
66 $result = "$real_hash_dir/$datafile";
68 /* Check for this file in the real hash directory. */
69 if ($hash_search && !@file_exists
($result)) {
70 /* First check the base directory, the most common location. */
71 if (@file_exists
("$dir/$datafile")) {
72 rename("$dir/$datafile", $result);
74 /* Then check the full range of possible hash directories. */
76 $check_hash_dir = $dir;
77 for ($h = 0; $h < 4; ++
$h) {
78 $check_hash_dir .= '/' . $hash_dirs[$h];
79 if (@is_readable
("$check_hash_dir/$datafile")) {
80 rename("$check_hash_dir/$datafile", $result);
87 /* Return the full hashed datafile path. */
92 * Helper function for getHashedFile, given a username returns the hashed
93 * dir for that username.
95 * @param string username the username of the current user
96 * @param string dir the SquirrelMail datadir
97 * @param string hash_dirs default ''
98 * @return the path to the hash dir for username
100 function getHashedDir($username, $dir, $hash_dirs = '') {
101 global $dir_hash_level;
103 /* Remove trailing slash from $dir if found */
104 if (substr($dir, -1) == '/') {
105 $dir = substr($dir, 0, strlen($dir) - 1);
108 /* If necessary, populate the hash dir variable. */
109 if ($hash_dirs == '') {
110 $hash_dirs = computeHashDirs($username);
113 /* Make sure the full hash directory exists. */
114 $real_hash_dir = $dir;
115 for ($h = 0; $h < $dir_hash_level; ++
$h) {
116 $real_hash_dir .= '/' . $hash_dirs[$h];
117 if (!@is_dir
($real_hash_dir)) {
118 if (!@mkdir
($real_hash_dir, 0770)) {
119 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
120 _("Could not create hashed directory structure!") . "<br />\n" .
121 _("Please contact your system administrator and report this error.") . "<br />\n";
127 /* And return that directory. */
128 return ($real_hash_dir);
132 * Helper function for getHashDir which does the actual hash calculation.
134 * @param string username the username to calculate the hash dir for
135 * @return array a list of hash dirs for this username
137 function computeHashDirs($username) {
138 /* Compute the hash for this user and extract the hash directories. */
139 $hash = base_convert(crc32($username), 10, 16);
140 $hash_dirs = array();
141 for ($h = 0; $h < 4; ++
$h) {
142 $hash_dirs[] = substr($hash, $h, 1);
145 /* Return our array of hash directories. */
150 * FIXME: Undocumented function
152 function checkForJavascript($reset = FALSE)
154 global $data_dir, $username, $javascript_on, $javascript_setting;
156 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION
) )
157 return $javascript_on;
159 if ( $reset ||
!isset($javascript_setting) )
160 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT
);
162 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
163 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
164 $js_autodetect_results = SMPREF_JS_OFF
;
166 if ( $javascript_setting == SMPREF_JS_AUTODETECT
)
167 $javascript_on = $js_autodetect_results;
169 $javascript_on = $javascript_setting;
171 sqsession_register($javascript_on, 'javascript_on');
172 return $javascript_on;