6 * This contains functions for manipulating user preferences in files
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
18 * Check the preferences into the session cache.
20 * @param string $data_dir
21 * @param string $username
26 function cachePrefValues($data_dir, $username) {
27 global $prefs_are_cached, $prefs_cache;
29 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
30 if ( isset($prefs_are_cached) && $prefs_are_cached) {
31 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
32 // sm_print_r($prefs_cache);
37 sqsession_unregister('prefs_cache');
38 sqsession_unregister('prefs_are_cached');
40 /* Calculate the filename for the user's preference file */
41 $filename = getHashedFile($username, $data_dir, "$username.pref");
43 /* A call to checkForPrefs here should take eliminate the need for */
44 /* this to be called throughout the rest of the SquirrelMail code. */
45 checkForPrefs($data_dir, $username, $filename);
47 /* Make sure that the preference file now DOES exist. */
48 if (!file_exists($filename)) {
49 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
53 /* Open the file, or else display an error to the user. */
54 if(!$file = @fopen
($filename, 'r'))
56 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
60 /* Read in the preferences. */
62 while (! feof($file)) {
64 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
65 while($read = fgets($file, 1024))
68 if(strpos($read,"\n") ||
strpos($read,"\r"))
72 $equalsAt = strpos($pref, '=');
74 $key = substr($pref, 0, $equalsAt);
75 $value = substr($pref, $equalsAt +
1);
77 //FIXME: this code is not in db_prefs.php that I can see
78 /* this is to 'rescue' old-style highlighting rules. */
79 if (substr($key, 0, 9) == 'highlight') {
80 $key = 'highlight' . $highlight_num;
84 //FIXME: this code is not in db_prefs.php that I can see
86 $prefs_cache[$key] = $value;
92 $prefs_are_cached = TRUE;
94 sqsession_register($prefs_cache, 'prefs_cache');
95 sqsession_register($prefs_are_cached, 'prefs_are_cached');
99 * Return the value for the desired preference.
101 * @param string $data_dir data directory
102 * @param string $username user name
103 * @param string $pref_name preference name
104 * @param string $default (since 1.2.0) default preference value
109 function getPref($data_dir, $username, $pref_name, $default = '') {
112 $temp = array(&$username, &$pref_name);
113 $result = do_hook('get_pref_override', $temp);
115 cachePrefValues($data_dir, $username);
116 if (isset($prefs_cache[$pref_name])) {
117 $result = $prefs_cache[$pref_name];
119 $temp = array(&$username, &$pref_name);
120 $result = do_hook('get_pref', $temp);
130 * Save the preferences for this user.
132 * @param string $data_dir data directory
133 * @param string $username user name
138 function savePrefValues($data_dir, $username) {
141 $filename = getHashedFile($username, $data_dir, "$username.pref");
143 /* Open the file for writing, or else display an error to the user. */
144 if(!$file = @fopen
($filename.'.tmp', 'w'))
146 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
149 foreach ($prefs_cache as $Key => $Value) {
151 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
152 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
158 if (! @copy
($filename . '.tmp',$filename) ) {
159 logout_error( sprintf( _("Preference file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
162 @unlink
($filename . '.tmp');
163 @chmod
($filename, 0600);
164 sqsession_register($prefs_cache , 'prefs_cache');
168 * Remove a preference for the current user.
170 * @param string $data_dir data directory
171 * @param string $username user name
172 * @param string $pref_name preference name
175 function removePref($data_dir, $username, $pref_name) {
178 cachePrefValues($data_dir, $username);
180 if (isset($prefs_cache[$pref_name])) {
181 unset($prefs_cache[$pref_name]);
184 savePrefValues($data_dir, $username);
188 * Set the desired preference setting ($pref_name)
189 * to whatever is in $value.
191 * @param string $data_dir data directory
192 * @param string $username user name
193 * @param string $pref_name preference name
194 * @param mixed $value preference value
197 function setPref($data_dir, $username, $pref_name, $value) {
200 cachePrefValues($data_dir, $username);
201 if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
206 removePref($data_dir, $username, $pref_name);
210 $prefs_cache[$pref_name] = $value;
211 savePrefValues($data_dir, $username);
215 * Check for a preferences file. If one can not be found, create it.
217 * @param string $data_dir data directory
218 * @param string $username user name
219 * @param string $filename (since 1.2.0) preference file name.
220 * (OPTIONAL; default is an empty string,
221 * in which case the file name is
222 * automatically detected)
225 function checkForPrefs($data_dir, $username, $filename = '') {
226 /* First, make sure we have the filename. */
227 if ($filename == '') {
228 $filename = getHashedFile($username, $data_dir, "$username.pref");
231 /* Then, check if the file exists. */
232 if (!@file_exists
($filename) ) {
234 /* If it does not exist, check for default_prefs */
236 /* First, check legacy locations: data dir */
237 if(substr($data_dir,-1) != '/') {
240 $default_pref = $data_dir . 'default_pref';
242 /* or legacy location: internal data dir */
243 if (!@file_exists
($default_pref)) {
244 $default_pref = SM_PATH
. 'data/default_pref';
247 /* If no legacies, check where we'd expect it to be located:
249 if (!@file_exists
($default_pref)) {
250 $default_pref = SM_PATH
. 'config/default_pref';
253 /* If a default_pref file found, try to copy it, if none found,
254 * try to create an empty one. If that fails, report an error.
257 ( is_readable($default_pref) && !@copy
($default_pref, $filename) ) ||
261 if (function_exists('posix_getuid')){
262 $user_data = posix_getpwuid(posix_getuid());
263 $uid = $user_data['name'];
265 $errTitle = _("Could not create initial preference file!");
266 $errString = $errTitle . "\n" .
267 sprintf( _("%s should be writable by user %s."), $data_dir, $uid ) . "\n" .
268 _("Please contact your system administrator and report this error.") ;
269 logout_error( $errString, $errTitle );
276 * Write the User Signature.
278 * @param string $data_dir data directory
279 * @param string $username user name
280 * @param integer $number (since 1.2.5) identity number.
281 * (before 1.2.5., this parameter
282 * was used for the signature value)
283 * @param string $value (since 1.2.5) signature value
286 function setSig($data_dir, $username, $number, $value) {
287 // Limit signature size to 64KB (database BLOB limit)
288 if (strlen($value)>65536) {
289 error_option_save(_("Signature is too big."));
292 $filename = getHashedFile($username, $data_dir, "$username.si$number");
293 /* Open the file for writing, or else display an error to the user. */
294 if(!$file = @fopen
("$filename.tmp", 'w')) {
295 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
298 if ( sq_fwrite($file, $value) === FALSE ) {
299 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
303 if (! @copy
($filename . '.tmp',$filename) ) {
304 logout_error( sprintf( _("Signature file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
307 @unlink
($filename . '.tmp');
308 @chmod
($filename, 0600);
315 * @param string $data_dir data directory
316 * @param string $username user name
317 * @param integer $number (since 1.2.5) identity number
319 * @return string signature
322 function getSig($data_dir, $username, $number) {
323 $filename = getHashedFile($username, $data_dir, "$username.si$number");
325 if (file_exists($filename)) {
326 /* Open the file, or else display an error to the user. */
327 if(!$file = @fopen
($filename, 'r'))
329 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
332 while (!feof($file)) {
333 $sig .= fgets($file, 1024);