Changelog update
[squirrelmail.git] / functions / file_prefs.php
CommitLineData
3499f99f 1<?php
2
3/**
4 * file_prefs.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
3499f99f 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences in files
10 *
11 * $Id$
12 */
13
3499f99f 14/**
15 * Check the preferences into the session cache.
16 */
17function cachePrefValues($data_dir, $username) {
18 global $prefs_are_cached, $prefs_cache;
19
20 if ( isset($prefs_are_cached) && $prefs_are_cached) {
c96c32f4 21 return $prefs_cache;
3499f99f 22 }
23
9eb0fbd4 24 sqsession_unregister('prefs_cache');
25 sqsession_unregister('prefs_are_cached');
3499f99f 26
27 /* Calculate the filename for the user's preference file */
28 $filename = getHashedFile($username, $data_dir, "$username.pref");
29
30 /* A call to checkForPrefs here should take eliminate the need for */
31 /* this to be called throughout the rest of the SquirrelMail code. */
32 checkForPrefs($data_dir, $username, $filename);
33
34 /* Make sure that the preference file now DOES exist. */
35 if (!file_exists($filename)) {
bd9c880b 36 include_once(SM_PATH . 'functions/display_messages.php');
9be8198d 37 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
3499f99f 38 exit;
39 }
40
35b5fa13 41 /* Open the file, or else display an error to the user. */
42 if(!$file = @fopen($filename, 'r'))
43 {
bd9c880b 44 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 45 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
46 exit;
47 }
3499f99f 48
49 /* Read in the preferences. */
50 $highlight_num = 0;
51 while (! feof($file)) {
710a8254 52 $pref = '';
53 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
54 while($read = fgets($file, 1024))
55 {
56 $pref .= $read;
57 if(strpos($read,"\n") || strpos($read,"\r"))
58 break;
59 }
60 $pref = trim($pref);
3499f99f 61 $equalsAt = strpos($pref, '=');
62 if ($equalsAt > 0) {
63 $key = substr($pref, 0, $equalsAt);
64 $value = substr($pref, $equalsAt + 1);
1c159927 65 /* this is to 'rescue' old-style highlighting rules. */
3499f99f 66 if (substr($key, 0, 9) == 'highlight') {
67 $key = 'highlight' . $highlight_num;
68 $highlight_num ++;
69 }
70
71 if ($value != '') {
72 $prefs_cache[$key] = $value;
73 }
74 }
75 }
76 fclose($file);
77
78 $prefs_are_cached = TRUE;
79
9eb0fbd4 80 sqsession_register($prefs_cache, 'prefs_cache');
81 sqsession_register($prefs_are_cached, 'prefs_are_cached');
c96c32f4 82 return $prefs_cache;
3499f99f 83}
84
85/**
86 * Return the value for the prefernce given by $string.
87 */
88function getPref($data_dir, $username, $string, $default = '') {
89 global $prefs_cache;
397a9d7b 90
be6f5c46 91 $result = do_hook_function('get_pref_override',array($username,$string));
92 if (!$result) {
c96c32f4 93 $prefs_cache = cachePrefValues($data_dir, $username);
be6f5c46 94 if (isset($prefs_cache[$string])) {
95 $result = $prefs_cache[$string];
96 } else {
97 $result = do_hook_function('get_pref', array($username,$string));
98 if (!$result) {
99 $result = $default;
100 }
101 }
3499f99f 102 }
3499f99f 103 return ($result);
104}
105
106/**
107 * Save the preferences for this user.
108 */
c96c32f4 109function savePrefValues($data_dir, $username, $prefs_cache) {
3499f99f 110
111 $filename = getHashedFile($username, $data_dir, "$username.pref");
112
c96c32f4 113 if (!is_array($prefs_cache) || count($prefs_cache) == 0) {
114 include_once(SM_PATH . 'functions/display_messages.php');
115 logout_error( _("Houston we have a problem, the preference cache is lost!!!."));
116 exit;
117 }
118
35b5fa13 119 /* Open the file for writing, or else display an error to the user. */
be6f5c46 120 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 121 {
bd9c880b 122 include_once(SM_PATH . 'functions/display_messages.php');
be6f5c46 123 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 124 exit;
125 }
3499f99f 126 foreach ($prefs_cache as $Key => $Value) {
127 if (isset($Value)) {
dabef6fd 128 $tmpwrite = @fwrite($file, $Key . '=' . $Value . "\n");
129 if ($tmpwrite == -1) {
130 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
131 exit;
132 }
3499f99f 133 }
134 }
135 fclose($file);
dabef6fd 136 @copy($filename . '.tmp',$filename);
137 @unlink($filename . '.tmp');
3499f99f 138 chmod($filename, 0600);
139}
140
141/**
142 * Remove a preference for the current user.
143 */
144function removePref($data_dir, $username, $string) {
145 global $prefs_cache;
146
3499f99f 147 if (isset($prefs_cache[$string])) {
148 unset($prefs_cache[$string]);
149 }
150
c96c32f4 151 savePrefValues($data_dir, $username,$prefs_cache);
3499f99f 152}
153
154/**
155 * Set a there preference $string to $value.
156 */
157function setPref($data_dir, $username, $string, $value) {
158 global $prefs_cache;
159
3499f99f 160 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
161 return;
162 }
163
164 if ($value === '') {
c96c32f4 165 unset($prefs_cache[$string]);
166 } else {
167 $prefs_cache[$string] = $value;
3499f99f 168 }
c96c32f4 169 savePrefValues($data_dir, $username, $prefs_cache);
3499f99f 170}
171
172/**
173 * Check for a preferences file. If one can not be found, create it.
174 */
175function checkForPrefs($data_dir, $username, $filename = '') {
176 /* First, make sure we have the filename. */
177 if ($filename == '') {
bd1180c1 178 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 179 }
180
181 /* Then, check if the file exists. */
182 if (!@file_exists($filename) ) {
183 /* First, check the $data_dir for the default preference file. */
a9e6867d 184 $default_pref = $data_dir . '/default_pref';
3499f99f 185
186 /* If it is not there, check the internal data directory. */
187 if (!@file_exists($default_pref)) {
bd9c880b 188 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 189 }
190
191 /* Otherwise, report an error. */
9be8198d 192 $errTitle = sprintf( _("Error opening %s"), $default_pref );
921bea1a 193 if (!is_readable($default_pref)) {
9be8198d 194 $errString = $errTitle . "<br>\n" .
921bea1a 195 _("Default preference file not found or not readable!") . "<br>\n" .
9be8198d 196 _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 197 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 198 logout_error( $errString, $errTitle );
3499f99f 199 exit;
200 } else if (!@copy($default_pref, $filename)) {
305a1012 201 $uid = 'httpd';
202 if (function_exists('posix_getuid')){
203 $user_data = posix_getpwuid(posix_getuid());
204 $uid = $user_data['name'];
205 }
9be8198d 206 $errString = $errTitle . '<br>' .
207 _("Could not create initial preference file!") . "<br>\n" .
208 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
209 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 210 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 211 logout_error( $errString, $errTitle );
3499f99f 212 exit;
213 }
214 }
215}
216
217/**
218 * Write the User Signature.
219 */
01265fba 220function setSig($data_dir, $username, $number, $value) {
221 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 222 /* Open the file for writing, or else display an error to the user. */
dabef6fd 223 if(!$file = @fopen("$filename.tmp", 'w')) {
224 include_once( '../functions/display_messages.php' );
225 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 226 exit;
227 }
dabef6fd 228 $tmpwrite = @fwrite($file, $value);
229 if ($tmpwrite == -1) {
230 include_once( '../functions/display_messages.php' );
231 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
232 exit;
233 }
3499f99f 234 fclose($file);
dabef6fd 235 @copy($filename . '.tmp',$filename);
236 @unlink($filename . '.tmp');
237 chmod($filename, 0600);
238
3499f99f 239}
240
241/**
242 * Get the signature.
243 */
01265fba 244function getSig($data_dir, $username, $number) {
01265fba 245 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 246 $sig = '';
247 if (file_exists($filename)) {
35b5fa13 248 /* Open the file, or else display an error to the user. */
249 if(!$file = @fopen($filename, 'r'))
250 {
bd9c880b 251 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 252 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
253 exit;
254 }
3499f99f 255 while (!feof($file)) {
256 $sig .= fgets($file, 1024);
257 }
258 fclose($file);
259 }
260 return $sig;
261}