d92c76f0a7c5f8c55f3a5f3395cea9a0690cf429
[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 * @subpackage prefs
14 */
15
16 /** @ignore */
17 if (!defined('SM_PATH')) define('SM_PATH','../');
18
19 /** Include global.php */
20 require_once(SM_PATH . 'functions/global.php');
21 require_once(SM_PATH . 'functions/plugin.php');
22
23 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
24 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
25
26 if ( !sqsession_is_registered('prefs_are_cached') ||
27 !isset( $prefs_cache) ||
28 !is_array( $prefs_cache)
29 ) {
30 $prefs_are_cached = false;
31 $prefs_cache = array();
32 }
33
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');
39 } else {
40 require_once(SM_PATH . 'functions/file_prefs.php');
41 }
42
43 /* Hashing functions */
44
45 /**
46 * Given a username and datafilename, this will return the path to the
47 * hashed location of that datafile.
48 *
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
54 * @since 1.2.0
55 */
56 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
57
58 /* Remove trailing slash from $dir if found */
59 if (substr($dir, -1) == '/') {
60 $dir = substr($dir, 0, strlen($dir) - 1);
61 }
62
63 /* Compute the hash for this user and extract the hash directories. */
64 $hash_dirs = computeHashDirs($username);
65
66 /* First, get and make sure the full hash directory exists. */
67 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
68
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";
72
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);
78
79 /* Then check the full range of possible hash directories. */
80 } else {
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);
86 break;
87 }
88 }
89 }
90 }
91
92 /* Return the full hashed datafile path. */
93 return ($result);
94 }
95
96 /**
97 * Helper function for getHashedFile, given a username returns the hashed
98 * dir for that username.
99 *
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
104 * @since 1.2.0
105 */
106 function getHashedDir($username, $dir, $hash_dirs = '') {
107 global $dir_hash_level;
108
109 /* Remove trailing slash from $dir if found */
110 if (substr($dir, -1) == '/') {
111 $dir = substr($dir, 0, strlen($dir) - 1);
112 }
113
114 /* If necessary, populate the hash dir variable. */
115 if ($hash_dirs == '') {
116 $hash_dirs = computeHashDirs($username);
117 }
118
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";
128 exit;
129 }
130 }
131 }
132
133 /* And return that directory. */
134 return ($real_hash_dir);
135 }
136
137 /**
138 * Helper function for getHashDir which does the actual hash calculation.
139 *
140 * @param string username the username to calculate the hash dir for
141 * @return array a list of hash dirs for this username
142 * @since 1.2.0
143 */
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);
150 }
151
152 /* Return our array of hash directories. */
153 return ($hash_dirs);
154 }
155
156 /**
157 * Javascript support detection function
158 * @param boolean $reset recheck javascript support if set to true.
159 * @return integer SMPREF_JS_* constants
160 * @since 1.5.1
161 */
162 function checkForJavascript($reset = FALSE) {
163 global $data_dir, $username, $javascript_on, $javascript_setting;
164
165 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
166 return $javascript_on;
167
168 if ( $reset || !isset($javascript_setting) )
169 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
170
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;
174
175 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
176 $javascript_on = $js_autodetect_results;
177 else
178 $javascript_on = $javascript_setting;
179
180 sqsession_register($javascript_on, 'javascript_on');
181 return $javascript_on;
182 }
183
184 ?>