6 * This contains functions for manipulating user preferences in files
8 * @copyright © 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
17 if (! defined('SM_PATH')) define('SM_PATH','../');
19 /** include this for error messages */
20 include_once(SM_PATH
. 'functions/display_messages.php');
23 * Check the preferences into the session cache.
24 * @param string $data_dir
25 * @param string $username
28 function cachePrefValues($data_dir, $username) {
29 global $prefs_are_cached, $prefs_cache;
31 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
32 if ( isset($prefs_are_cached) && $prefs_are_cached) {
33 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
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);
76 /* this is to 'rescue' old-style highlighting rules. */
77 if (substr($key, 0, 9) == 'highlight') {
78 $key = 'highlight' . $highlight_num;
83 $prefs_cache[$key] = $value;
89 $prefs_are_cached = TRUE;
91 sqsession_register($prefs_cache, 'prefs_cache');
92 sqsession_register($prefs_are_cached, 'prefs_are_cached');
96 * Return the value for the preference given by $string.
97 * @param string $data_dir data directory
98 * @param string $username user name
99 * @param string $string preference name
100 * @param string $default (since 1.2.0) default preference value
103 function getPref($data_dir, $username, $string, $default = '') {
106 $result = do_hook_function('get_pref_override',array($username,$string));
108 cachePrefValues($data_dir, $username);
109 if (isset($prefs_cache[$string])) {
110 $result = $prefs_cache[$string];
112 $result = do_hook_function('get_pref', array($username,$string));
122 * Save the preferences for this user.
123 * @param string $data_dir data directory
124 * @param string $username user name
127 function savePrefValues($data_dir, $username) {
130 $filename = getHashedFile($username, $data_dir, "$username.pref");
132 /* Open the file for writing, or else display an error to the user. */
133 if(!$file = @fopen
($filename.'.tmp', 'w'))
135 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
138 foreach ($prefs_cache as $Key => $Value) {
140 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
141 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
147 if (! @copy
($filename . '.tmp',$filename) ) {
148 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') );
151 @unlink
($filename . '.tmp');
152 @chmod
($filename, 0600);
153 sqsession_register($prefs_cache , 'prefs_cache');
157 * Remove a preference for the current user.
158 * @param string $data_dir data directory
159 * @param string $username user name
160 * @param string $string preference name
162 function removePref($data_dir, $username, $string) {
165 cachePrefValues($data_dir, $username);
167 if (isset($prefs_cache[$string])) {
168 unset($prefs_cache[$string]);
171 savePrefValues($data_dir, $username);
175 * Set a there preference $string to $value.
176 * @param string $data_dir data directory
177 * @param string $username user name
178 * @param string $string preference name
179 * @param mixed $value preference value
181 function setPref($data_dir, $username, $string, $value) {
184 cachePrefValues($data_dir, $username);
185 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
190 removePref($data_dir, $username, $string);
194 $prefs_cache[$string] = $value;
195 savePrefValues($data_dir, $username);
199 * Check for a preferences file. If one can not be found, create it.
200 * @param string $data_dir data directory
201 * @param string $username user name
202 * @param string $filename (since 1.2.0) preference file name.
203 * detects file name, if set to empty string.
205 function checkForPrefs($data_dir, $username, $filename = '') {
206 /* First, make sure we have the filename. */
207 if ($filename == '') {
208 $filename = getHashedFile($username, $data_dir, "$username.pref");
211 /* Then, check if the file exists. */
212 if (!@file_exists
($filename) ) {
214 /* If it does not exist, check for default_prefs */
216 /* First, check legacy locations: data dir */
217 if(substr($data_dir,-1) != '/') {
220 $default_pref = $data_dir . 'default_pref';
222 /* or legacy location: internal data dir */
223 if (!@file_exists
($default_pref)) {
224 $default_pref = SM_PATH
. 'data/default_pref';
227 /* If no legacies, check where we'd expect it to be located:
229 if (!@file_exists
($default_pref)) {
230 $default_pref = SM_PATH
. 'config/default_pref';
233 /* If a default_pref file found, try to copy it, if none found,
234 * try to create an empty one. If that fails, report an error.
237 ( is_readable($default_pref) && !@copy
($default_pref, $filename) ) ||
241 if (function_exists('posix_getuid')){
242 $user_data = posix_getpwuid(posix_getuid());
243 $uid = $user_data['name'];
245 $errTitle = _("Could not create initial preference file!");
246 $errString = $errTitle . "<br />\n" .
247 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
248 _("Please contact your system administrator and report this error.") . "<br />\n";
249 logout_error( $errString, $errTitle );
256 * Write the User Signature.
257 * @param string $data_dir data directory
258 * @param string $username user name
259 * @param integer $number (since 1.2.5) identity number.
260 * parameter was used for signature text before 1.2.5.
261 * @param string $value (since 1.2.5) signature text
263 function setSig($data_dir, $username, $number, $value) {
264 // Limit signature size to 64KB (database BLOB limit)
265 if (strlen($value)>65536) {
266 error_option_save(_("Signature is too big."));
269 $filename = getHashedFile($username, $data_dir, "$username.si$number");
270 /* Open the file for writing, or else display an error to the user. */
271 if(!$file = @fopen
("$filename.tmp", 'w')) {
272 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
275 if ( sq_fwrite($file, $value) === FALSE ) {
276 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
280 if (! @copy
($filename . '.tmp',$filename) ) {
281 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') );
284 @unlink
($filename . '.tmp');
285 @chmod
($filename, 0600);
291 * @param string $data_dir data directory
292 * @param string $username user name
293 * @param integer $number (since 1.2.5) identity number
294 * @return string signature
296 function getSig($data_dir, $username, $number) {
297 $filename = getHashedFile($username, $data_dir, "$username.si$number");
299 if (file_exists($filename)) {
300 /* Open the file, or else display an error to the user. */
301 if(!$file = @fopen
($filename, 'r'))
303 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
306 while (!feof($file)) {
307 $sig .= fgets($file, 1024);