Add CVE-id's to ChangeLog.
[squirrelmail.git] / functions / prefs.php
1 <?php
2
3 /**
4 * prefs.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /** Include global.php */
16 require_once(SM_PATH . 'functions/global.php');
17 require_once(SM_PATH . 'functions/plugin.php');
18
19 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
20 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
21
22 if ( !sqsession_is_registered('prefs_are_cached') ||
23 !isset( $prefs_cache) ||
24 !is_array( $prefs_cache)
25 ) {
26 $prefs_are_cached = false;
27 $prefs_cache = array();
28 }
29
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');
35 } else {
36 require_once(SM_PATH . 'functions/file_prefs.php');
37 }
38
39 /* Hashing functions */
40
41 /**
42 * Given a username and datafilename, this will return the path to the
43 * hashed location of that datafile.
44 *
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
50 */
51 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
52
53 /* Remove trailing slash from $dir if found */
54 if (substr($dir, -1) == '/') {
55 $dir = substr($dir, 0, strlen($dir) - 1);
56 }
57
58 /* Compute the hash for this user and extract the hash directories. */
59 $hash_dirs = computeHashDirs($username);
60
61 /* First, get and make sure the full hash directory exists. */
62 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
63
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";
67
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);
73
74 /* Then check the full range of possible hash directories. */
75 } else {
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);
81 break;
82 }
83 }
84 }
85 }
86
87 /* Return the full hashed datafile path. */
88 return ($result);
89 }
90
91 /**
92 * Helper function for getHashedFile, given a username returns the hashed
93 * dir for that username.
94 *
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
99 */
100 function getHashedDir($username, $dir, $hash_dirs = '') {
101 global $dir_hash_level;
102
103 /* Remove trailing slash from $dir if found */
104 if (substr($dir, -1) == '/') {
105 $dir = substr($dir, 0, strlen($dir) - 1);
106 }
107
108 /* If necessary, populate the hash dir variable. */
109 if ($hash_dirs == '') {
110 $hash_dirs = computeHashDirs($username);
111 }
112
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";
122 exit;
123 }
124 }
125 }
126
127 /* And return that directory. */
128 return ($real_hash_dir);
129 }
130
131 /**
132 * Helper function for getHashDir which does the actual hash calculation.
133 *
134 * @param string username the username to calculate the hash dir for
135 * @return array a list of hash dirs for this username
136 */
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);
143 }
144
145 /* Return our array of hash directories. */
146 return ($hash_dirs);
147 }
148
149 /**
150 * FIXME: Undocumented function
151 */
152 function checkForJavascript($reset = FALSE)
153 {
154 global $data_dir, $username, $javascript_on, $javascript_setting;
155
156 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
157 return $javascript_on;
158
159 if ( $reset || !isset($javascript_setting) )
160 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
161
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;
165
166 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
167 $javascript_on = $js_autodetect_results;
168 else
169 $javascript_on = $javascript_setting;
170
171 sqsession_register($javascript_on, 'javascript_on');
172 return $javascript_on;
173 }
174
175 ?>