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
17 if (!defined('SM_PATH')) define('SM_PATH','../');
19 /** Include global.php */
20 require_once(SM_PATH
. 'functions/global.php');
21 require_once(SM_PATH
. 'functions/plugin.php');
23 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
24 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
26 if ( !sqsession_is_registered('prefs_are_cached') ||
27 !isset( $prefs_cache) ||
28 !is_array( $prefs_cache)
30 $prefs_are_cached = false;
31 $prefs_cache = array();
34 $prefs_backend = do_hook_function('prefs_backend');
35 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
36 require_once(SM_PATH
. $prefs_backend);
37 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
38 require_once(SM_PATH
. 'functions/db_prefs.php');
40 require_once(SM_PATH
. 'functions/file_prefs.php');
43 /* Hashing functions */
46 * Given a username and datafilename, this will return the path to the
47 * hashed location of that datafile.
49 * @param string username the username of the current user
50 * @param string dir the SquirrelMail datadir
51 * @param string datafile the name of the file to open
52 * @param bool hash_seach default true
53 * @return string the hashed location of datafile
56 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
58 /* Remove trailing slash from $dir if found */
59 if (substr($dir, -1) == '/') {
60 $dir = substr($dir, 0, strlen($dir) - 1);
63 /* Compute the hash for this user and extract the hash directories. */
64 $hash_dirs = computeHashDirs($username);
66 /* First, get and make sure the full hash directory exists. */
67 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
69 /* Set the value of our real data file, after we've removed unwanted characters. */
70 $datafile = str_replace('/', '_', $datafile);
71 $result = "$real_hash_dir/$datafile";
73 /* Check for this file in the real hash directory. */
74 if ($hash_search && !@file_exists
($result)) {
75 /* First check the base directory, the most common location. */
76 if (@file_exists
("$dir/$datafile")) {
77 rename("$dir/$datafile", $result);
79 /* Then check the full range of possible hash directories. */
81 $check_hash_dir = $dir;
82 for ($h = 0; $h < 4; ++
$h) {
83 $check_hash_dir .= '/' . $hash_dirs[$h];
84 if (@is_readable
("$check_hash_dir/$datafile")) {
85 rename("$check_hash_dir/$datafile", $result);
92 /* Return the full hashed datafile path. */
97 * Helper function for getHashedFile, given a username returns the hashed
98 * dir for that username.
100 * @param string username the username of the current user
101 * @param string dir the SquirrelMail datadir
102 * @param string hash_dirs default ''
103 * @return the path to the hash dir for username
106 function getHashedDir($username, $dir, $hash_dirs = '') {
107 global $dir_hash_level;
109 /* Remove trailing slash from $dir if found */
110 if (substr($dir, -1) == '/') {
111 $dir = substr($dir, 0, strlen($dir) - 1);
114 /* If necessary, populate the hash dir variable. */
115 if ($hash_dirs == '') {
116 $hash_dirs = computeHashDirs($username);
119 /* Make sure the full hash directory exists. */
120 $real_hash_dir = $dir;
121 for ($h = 0; $h < $dir_hash_level; ++
$h) {
122 $real_hash_dir .= '/' . $hash_dirs[$h];
123 if (!@is_dir
($real_hash_dir)) {
124 if (!@mkdir
($real_hash_dir, 0770)) {
125 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
126 _("Could not create hashed directory structure!") . "<br />\n" .
127 _("Please contact your system administrator and report this error.") . "<br />\n";
133 /* And return that directory. */
134 return ($real_hash_dir);
138 * Helper function for getHashDir which does the actual hash calculation.
140 * @param string username the username to calculate the hash dir for
141 * @return array a list of hash dirs for this username
144 function computeHashDirs($username) {
145 /* Compute the hash for this user and extract the hash directories. */
146 $hash = base_convert(crc32($username), 10, 16);
147 $hash_dirs = array();
148 for ($h = 0; $h < 4; ++
$h) {
149 $hash_dirs[] = substr($hash, $h, 1);
152 /* Return our array of hash directories. */
157 * Javascript support detection function
158 * @param boolean $reset recheck javascript support if set to true.
159 * @return integer SMPREF_JS_* constants
162 function checkForJavascript($reset = FALSE) {
163 global $data_dir, $username, $javascript_on, $javascript_setting;
165 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION
) )
166 return $javascript_on;
168 if ( $reset ||
!isset($javascript_setting) )
169 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT
);
171 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
172 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
173 $js_autodetect_results = SMPREF_JS_OFF
;
175 if ( $javascript_setting == SMPREF_JS_AUTODETECT
)
176 $javascript_on = $js_autodetect_results;
178 $javascript_on = $javascript_setting;
180 sqsession_register($javascript_on, 'javascript_on');
181 return $javascript_on;