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.
19 * @param string $data_dir
20 * @param string $username
23 function cachePrefValues($data_dir, $username) {
24 global $prefs_are_cached, $prefs_cache;
26 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
27 if ( isset($prefs_are_cached) && $prefs_are_cached) {
28 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
29 // sm_print_r($prefs_cache);
34 sqsession_unregister('prefs_cache');
35 sqsession_unregister('prefs_are_cached');
37 /* Calculate the filename for the user's preference file */
38 $filename = getHashedFile($username, $data_dir, "$username.pref");
40 /* A call to checkForPrefs here should take eliminate the need for */
41 /* this to be called throughout the rest of the SquirrelMail code. */
42 checkForPrefs($data_dir, $username, $filename);
44 /* Make sure that the preference file now DOES exist. */
45 if (!file_exists($filename)) {
46 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
50 /* Open the file, or else display an error to the user. */
51 if(!$file = @fopen
($filename, 'r'))
53 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
57 /* Read in the preferences. */
59 while (! feof($file)) {
61 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
62 while($read = fgets($file, 1024))
65 if(strpos($read,"\n") ||
strpos($read,"\r"))
69 $equalsAt = strpos($pref, '=');
71 $key = substr($pref, 0, $equalsAt);
72 $value = substr($pref, $equalsAt +
1);
73 /* this is to 'rescue' old-style highlighting rules. */
74 if (substr($key, 0, 9) == 'highlight') {
75 $key = 'highlight' . $highlight_num;
80 $prefs_cache[$key] = $value;
86 $prefs_are_cached = TRUE;
88 sqsession_register($prefs_cache, 'prefs_cache');
89 sqsession_register($prefs_are_cached, 'prefs_are_cached');
93 * Return the value for the preference given by $string.
94 * @param string $data_dir data directory
95 * @param string $username user name
96 * @param string $string preference name
97 * @param string $default (since 1.2.0) default preference value
100 function getPref($data_dir, $username, $string, $default = '') {
103 $result = do_hook('get_pref_override', $temp=array(&$username, &$string));
105 cachePrefValues($data_dir, $username);
106 if (isset($prefs_cache[$string])) {
107 $result = $prefs_cache[$string];
109 $result = do_hook('get_pref', $temp=array(&$username, &$string));
119 * Save the preferences for this user.
120 * @param string $data_dir data directory
121 * @param string $username user name
124 function savePrefValues($data_dir, $username) {
127 $filename = getHashedFile($username, $data_dir, "$username.pref");
129 /* Open the file for writing, or else display an error to the user. */
130 if(!$file = @fopen
($filename.'.tmp', 'w'))
132 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
135 foreach ($prefs_cache as $Key => $Value) {
137 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
138 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
144 if (! @copy
($filename . '.tmp',$filename) ) {
145 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') );
148 @unlink
($filename . '.tmp');
149 @chmod
($filename, 0600);
150 sqsession_register($prefs_cache , 'prefs_cache');
154 * Remove a preference for the current user.
155 * @param string $data_dir data directory
156 * @param string $username user name
157 * @param string $string preference name
159 function removePref($data_dir, $username, $string) {
162 cachePrefValues($data_dir, $username);
164 if (isset($prefs_cache[$string])) {
165 unset($prefs_cache[$string]);
168 savePrefValues($data_dir, $username);
172 * Set a there preference $string to $value.
173 * @param string $data_dir data directory
174 * @param string $username user name
175 * @param string $string preference name
176 * @param mixed $value preference value
178 function setPref($data_dir, $username, $string, $value) {
181 cachePrefValues($data_dir, $username);
182 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
187 removePref($data_dir, $username, $string);
191 $prefs_cache[$string] = $value;
192 savePrefValues($data_dir, $username);
196 * Check for a preferences file. If one can not be found, create it.
197 * @param string $data_dir data directory
198 * @param string $username user name
199 * @param string $filename (since 1.2.0) preference file name.
200 * detects file name, if set to empty string.
202 function checkForPrefs($data_dir, $username, $filename = '') {
203 /* First, make sure we have the filename. */
204 if ($filename == '') {
205 $filename = getHashedFile($username, $data_dir, "$username.pref");
208 /* Then, check if the file exists. */
209 if (!@file_exists
($filename) ) {
211 /* If it does not exist, check for default_prefs */
213 /* First, check legacy locations: data dir */
214 if(substr($data_dir,-1) != '/') {
217 $default_pref = $data_dir . 'default_pref';
219 /* or legacy location: internal data dir */
220 if (!@file_exists
($default_pref)) {
221 $default_pref = SM_PATH
. 'data/default_pref';
224 /* If no legacies, check where we'd expect it to be located:
226 if (!@file_exists
($default_pref)) {
227 $default_pref = SM_PATH
. 'config/default_pref';
230 /* If a default_pref file found, try to copy it, if none found,
231 * try to create an empty one. If that fails, report an error.
234 ( is_readable($default_pref) && !@copy
($default_pref, $filename) ) ||
238 if (function_exists('posix_getuid')){
239 $user_data = posix_getpwuid(posix_getuid());
240 $uid = $user_data['name'];
242 $errTitle = _("Could not create initial preference file!");
243 $errString = $errTitle . "<br />\n" .
244 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
245 _("Please contact your system administrator and report this error.") . "<br />\n";
246 logout_error( $errString, $errTitle );
253 * Write the User Signature.
254 * @param string $data_dir data directory
255 * @param string $username user name
256 * @param integer $number (since 1.2.5) identity number.
257 * parameter was used for signature text before 1.2.5.
258 * @param string $value (since 1.2.5) signature text
260 function setSig($data_dir, $username, $number, $value) {
261 // Limit signature size to 64KB (database BLOB limit)
262 if (strlen($value)>65536) {
263 error_option_save(_("Signature is too big."));
266 $filename = getHashedFile($username, $data_dir, "$username.si$number");
267 /* Open the file for writing, or else display an error to the user. */
268 if(!$file = @fopen
("$filename.tmp", 'w')) {
269 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
272 if ( sq_fwrite($file, $value) === FALSE ) {
273 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
277 if (! @copy
($filename . '.tmp',$filename) ) {
278 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') );
281 @unlink
($filename . '.tmp');
282 @chmod
($filename, 0600);
288 * @param string $data_dir data directory
289 * @param string $username user name
290 * @param integer $number (since 1.2.5) identity number
291 * @return string signature
293 function getSig($data_dir, $username, $number) {
294 $filename = getHashedFile($username, $data_dir, "$username.si$number");
296 if (file_exists($filename)) {
297 /* Open the file, or else display an error to the user. */
298 if(!$file = @fopen
($filename, 'r'))
300 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
303 while (!feof($file)) {
304 $sig .= fgets($file, 1024);