6 * Copyright (c) 1999-2002 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 in files
14 global $prefs_are_cached, $prefs_cache;
17 * Check the preferences into the session cache.
19 function cachePrefValues($data_dir, $username) {
20 global $prefs_are_cached, $prefs_cache;
22 if ( isset($prefs_are_cached) && $prefs_are_cached) {
26 session_unregister('prefs_cache');
27 session_unregister('prefs_are_cached');
29 /* Calculate the filename for the user's preference file */
30 $filename = getHashedFile($username, $data_dir, "$username.pref");
32 /* A call to checkForPrefs here should take eliminate the need for */
33 /* this to be called throughout the rest of the SquirrelMail code. */
34 checkForPrefs($data_dir, $username, $filename);
36 /* Make sure that the preference file now DOES exist. */
37 if (!file_exists($filename)) {
38 include_once( '../functions/display_messages.php' );
39 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
43 $file = fopen($filename, 'r');
45 /* Read in the preferences. */
47 while (! feof($file)) {
48 $pref = trim(fgets($file, 1024));
49 $equalsAt = strpos($pref, '=');
51 $key = substr($pref, 0, $equalsAt);
52 $value = substr($pref, $equalsAt +
1);
53 if (substr($key, 0, 9) == 'highlight') {
54 $key = 'highlight' . $highlight_num;
59 $prefs_cache[$key] = $value;
65 $prefs_are_cached = TRUE;
67 session_register('prefs_cache');
68 session_register('prefs_are_cached');
72 * Return the value for the prefernce given by $string.
74 function getPref($data_dir, $username, $string, $default = '') {
78 cachePrefValues($data_dir, $username);
80 if (isset($prefs_cache[$string])) {
81 $result = $prefs_cache[$string];
90 * Save the preferences for this user.
92 function savePrefValues($data_dir, $username) {
95 $filename = getHashedFile($username, $data_dir, "$username.pref");
97 $file = fopen($filename, 'w');
98 foreach ($prefs_cache as $Key => $Value) {
100 fwrite($file, $Key . '=' . $Value . "\n");
104 chmod($filename, 0600);
108 * Remove a preference for the current user.
110 function removePref($data_dir, $username, $string) {
113 cachePrefValues($data_dir, $username);
115 if (isset($prefs_cache[$string])) {
116 unset($prefs_cache[$string]);
119 savePrefValues($data_dir, $username);
123 * Set a there preference $string to $value.
125 function setPref($data_dir, $username, $string, $value) {
128 cachePrefValues($data_dir, $username);
129 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
134 removePref($data_dir, $username, $string);
138 $prefs_cache[$string] = $value;
139 savePrefValues($data_dir, $username);
143 * Check for a preferences file. If one can not be found, create it.
145 function checkForPrefs($data_dir, $username, $filename = '') {
146 /* First, make sure we have the filename. */
147 if ($filename == '') {
148 $filename = getHashedFile($username, $data_dir, "$username.pref");
151 /* Then, check if the file exists. */
152 if (!@file_exists
($filename) ) {
153 /* First, check the $data_dir for the default preference file. */
154 $default_pref = $data_dir . 'default_pref';
156 /* If it is not there, check the internal data directory. */
157 if (!@file_exists
($default_pref)) {
158 $default_pref = '../data/default_pref';
161 /* Otherwise, report an error. */
162 $errTitle = sprintf( _("Error opening %s"), $default_pref );
163 if (!file_exists($default_pref)) {
164 $errString = $errTitle . "<br>\n" .
165 _("Default preference file not found!") . "<br>\n" .
166 _("Please contact your system administrator and report this error.") . "<br>\n";
167 include_once( '../functions/display_messages.php' );
168 logout_error( $errString, $errTitle );
170 } else if (!@copy
($default_pref, $filename)) {
172 if (function_exists('posix_getuid')){
173 $user_data = posix_getpwuid(posix_getuid());
174 $uid = $user_data['name'];
176 $errString = $errTitle . '<br>' .
177 _("Could not create initial preference file!") . "<br>\n" .
178 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
179 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
180 include_once( '../functions/display_messages.php' );
181 logout_error( $errString, $errTitle );
188 * Write the User Signature.
190 function setSig($data_dir, $username, $number, $value) {
191 $filename = getHashedFile($username, $data_dir, "$username.si$number");
192 $file = fopen($filename, 'w');
193 fwrite($file, $value);
200 function getSig($data_dir, $username, $number) {
201 #$filename = $data_dir . $username . '.si$number';
202 $filename = getHashedFile($username, $data_dir, "$username.si$number");
204 if (file_exists($filename)) {
205 $file = fopen($filename, 'r');
206 while (!feof($file)) {
207 $sig .= fgets($file, 1024);