Include original configuration array in Option object; allows custom save handlers...
[squirrelmail.git] / functions / file_prefs.php
CommitLineData
3499f99f 1<?php
2
3/**
4 * file_prefs.php
5 *
3499f99f 6 * This contains functions for manipulating user preferences in files
7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
ace4c62c 12 * @subpackage prefs
13 * @since 1.2.5
3499f99f 14 */
15
b94bfe13 16
3499f99f 17/**
18 * Check the preferences into the session cache.
37b11ab0 19 *
ace4c62c 20 * @param string $data_dir
21 * @param string $username
37b11ab0 22 *
ace4c62c 23 * @since 1.1.3
37b11ab0 24 *
3499f99f 25 */
26function cachePrefValues($data_dir, $username) {
27 global $prefs_are_cached, $prefs_cache;
324ac3c5 28
37d5278d 29 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
3499f99f 30 if ( isset($prefs_are_cached) && $prefs_are_cached) {
0d8a462c 31 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
202bcbcc 32// sm_print_r($prefs_cache);
33// exit;
2b2967d4 34 return;
3499f99f 35 }
324ac3c5 36
9eb0fbd4 37 sqsession_unregister('prefs_cache');
38 sqsession_unregister('prefs_are_cached');
324ac3c5 39
3499f99f 40 /* Calculate the filename for the user's preference file */
41 $filename = getHashedFile($username, $data_dir, "$username.pref");
42
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);
46
47 /* Make sure that the preference file now DOES exist. */
48 if (!file_exists($filename)) {
9be8198d 49 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
3499f99f 50 exit;
51 }
52
35b5fa13 53 /* Open the file, or else display an error to the user. */
54 if(!$file = @fopen($filename, 'r'))
55 {
35b5fa13 56 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
57 exit;
58 }
3499f99f 59
60 /* Read in the preferences. */
61 $highlight_num = 0;
62 while (! feof($file)) {
710a8254 63 $pref = '';
64 /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
65 while($read = fgets($file, 1024))
66 {
67 $pref .= $read;
68 if(strpos($read,"\n") || strpos($read,"\r"))
69 break;
70 }
71 $pref = trim($pref);
3499f99f 72 $equalsAt = strpos($pref, '=');
73 if ($equalsAt > 0) {
74 $key = substr($pref, 0, $equalsAt);
75 $value = substr($pref, $equalsAt + 1);
37b11ab0 76
77//FIXME: this code is not in db_prefs.php that I can see
1c159927 78 /* this is to 'rescue' old-style highlighting rules. */
3499f99f 79 if (substr($key, 0, 9) == 'highlight') {
80 $key = 'highlight' . $highlight_num;
81 $highlight_num ++;
82 }
83
37b11ab0 84//FIXME: this code is not in db_prefs.php that I can see
3499f99f 85 if ($value != '') {
86 $prefs_cache[$key] = $value;
87 }
88 }
491696a8 89 }
90 fclose($file);
3499f99f 91
92 $prefs_are_cached = TRUE;
93
9eb0fbd4 94 sqsession_register($prefs_cache, 'prefs_cache');
95 sqsession_register($prefs_are_cached, 'prefs_are_cached');
3499f99f 96}
324ac3c5 97
3499f99f 98/**
37b11ab0 99 * Return the value for the desired preference.
100 *
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
105 *
ace4c62c 106 * @return mixed
37b11ab0 107 *
3499f99f 108 */
37b11ab0 109function getPref($data_dir, $username, $pref_name, $default = '') {
3499f99f 110 global $prefs_cache;
397a9d7b 111
81cc6a19 112 $temp = array(&$username, &$pref_name);
113 $result = do_hook('get_pref_override', $temp);
be6f5c46 114 if (!$result) {
324ac3c5 115 cachePrefValues($data_dir, $username);
37b11ab0 116 if (isset($prefs_cache[$pref_name])) {
117 $result = $prefs_cache[$pref_name];
324ac3c5 118 } else {
81cc6a19 119 $temp = array(&$username, &$pref_name);
120 $result = do_hook('get_pref', $temp);
324ac3c5 121 if (!$result) {
122 $result = $default;
123 }
124 }
3499f99f 125 }
3499f99f 126 return ($result);
127}
128
129/**
130 * Save the preferences for this user.
37b11ab0 131 *
ace4c62c 132 * @param string $data_dir data directory
133 * @param string $username user name
37b11ab0 134 *
ace4c62c 135 * @since 1.1.3
37b11ab0 136 *
3499f99f 137 */
2b2967d4 138function savePrefValues($data_dir, $username) {
139 global $prefs_cache;
324ac3c5 140
3499f99f 141 $filename = getHashedFile($username, $data_dir, "$username.pref");
142
35b5fa13 143 /* Open the file for writing, or else display an error to the user. */
be6f5c46 144 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 145 {
be6f5c46 146 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 147 exit;
148 }
3499f99f 149 foreach ($prefs_cache as $Key => $Value) {
150 if (isset($Value)) {
3ecad5e6 151 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
dabef6fd 152 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
153 exit;
154 }
3499f99f 155 }
156 }
157 fclose($file);
1d44a937 158 if (! @copy($filename . '.tmp',$filename) ) {
b94bfe13 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') );
160 exit;
161 }
dabef6fd 162 @unlink($filename . '.tmp');
4623cba9 163 @chmod($filename, 0600);
c4f32485 164 sqsession_register($prefs_cache , 'prefs_cache');
3499f99f 165}
166
167/**
168 * Remove a preference for the current user.
37b11ab0 169 *
170 * @param string $data_dir data directory
171 * @param string $username user name
172 * @param string $pref_name preference name
173 *
3499f99f 174 */
37b11ab0 175function removePref($data_dir, $username, $pref_name) {
3499f99f 176 global $prefs_cache;
177
2b2967d4 178 cachePrefValues($data_dir, $username);
324ac3c5 179
37b11ab0 180 if (isset($prefs_cache[$pref_name])) {
181 unset($prefs_cache[$pref_name]);
3499f99f 182 }
324ac3c5 183
2b2967d4 184 savePrefValues($data_dir, $username);
3499f99f 185}
186
187/**
37b11ab0 188 * Set the desired preference setting ($pref_name)
189 * to whatever is in $value.
190 *
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
195 *
3499f99f 196 */
37b11ab0 197function setPref($data_dir, $username, $pref_name, $value) {
3499f99f 198 global $prefs_cache;
199
2b2967d4 200 cachePrefValues($data_dir, $username);
37b11ab0 201 if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
3499f99f 202 return;
203 }
204
205 if ($value === '') {
37b11ab0 206 removePref($data_dir, $username, $pref_name);
2b2967d4 207 return;
3499f99f 208 }
2b2967d4 209
37b11ab0 210 $prefs_cache[$pref_name] = $value;
2b2967d4 211 savePrefValues($data_dir, $username);
3499f99f 212}
213
214/**
215 * Check for a preferences file. If one can not be found, create it.
37b11ab0 216 *
ace4c62c 217 * @param string $data_dir data directory
218 * @param string $username user name
219 * @param string $filename (since 1.2.0) preference file name.
37b11ab0 220 * (OPTIONAL; default is an empty string,
221 * in which case the file name is
222 * automatically detected)
223 *
3499f99f 224 */
225function checkForPrefs($data_dir, $username, $filename = '') {
226 /* First, make sure we have the filename. */
227 if ($filename == '') {
bd1180c1 228 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 229 }
230
231 /* Then, check if the file exists. */
232 if (!@file_exists($filename) ) {
a04a3c20 233
234 /* If it does not exist, check for default_prefs */
f8a1ed5a 235
a04a3c20 236 /* First, check legacy locations: data dir */
e8c533f6 237 if(substr($data_dir,-1) != '/') {
238 $data_dir .= '/';
239 }
240 $default_pref = $data_dir . 'default_pref';
3499f99f 241
a04a3c20 242 /* or legacy location: internal data dir */
3499f99f 243 if (!@file_exists($default_pref)) {
bd9c880b 244 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 245 }
246
a04a3c20 247 /* If no legacies, check where we'd expect it to be located:
248 * under config/ */
249 if (!@file_exists($default_pref)) {
250 $default_pref = SM_PATH . 'config/default_pref';
251 }
f8a1ed5a 252
a04a3c20 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.
255 */
256 if (
257 ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
258 !@touch($filename)
259 ) {
305a1012 260 $uid = 'httpd';
261 if (function_exists('posix_getuid')){
262 $user_data = posix_getpwuid(posix_getuid());
263 $uid = $user_data['name'];
264 }
a04a3c20 265 $errTitle = _("Could not create initial preference file!");
ae13f72f 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.") ;
9be8198d 269 logout_error( $errString, $errTitle );
3499f99f 270 exit;
271 }
272 }
273}
274
275/**
276 * Write the User Signature.
37b11ab0 277 *
ace4c62c 278 * @param string $data_dir data directory
279 * @param string $username user name
37b11ab0 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
284 *
3499f99f 285 */
01265fba 286function setSig($data_dir, $username, $number, $value) {
288df1a0 287 // Limit signature size to 64KB (database BLOB limit)
288 if (strlen($value)>65536) {
289 error_option_save(_("Signature is too big."));
290 return;
291 }
01265fba 292 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 293 /* Open the file for writing, or else display an error to the user. */
dabef6fd 294 if(!$file = @fopen("$filename.tmp", 'w')) {
dabef6fd 295 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 296 exit;
297 }
3ecad5e6 298 if ( sq_fwrite($file, $value) === FALSE ) {
dabef6fd 299 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
300 exit;
301 }
3499f99f 302 fclose($file);
1d44a937 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') );
305 exit;
306 }
dabef6fd 307 @unlink($filename . '.tmp');
4623cba9 308 @chmod($filename, 0600);
dabef6fd 309
3499f99f 310}
311
312/**
313 * Get the signature.
37b11ab0 314 *
315 * @param string $data_dir data directory
316 * @param string $username user name
317 * @param integer $number (since 1.2.5) identity number
318 *
ace4c62c 319 * @return string signature
37b11ab0 320 *
3499f99f 321 */
01265fba 322function getSig($data_dir, $username, $number) {
01265fba 323 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 324 $sig = '';
325 if (file_exists($filename)) {
35b5fa13 326 /* Open the file, or else display an error to the user. */
327 if(!$file = @fopen($filename, 'r'))
328 {
35b5fa13 329 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
330 exit;
331 }
3499f99f 332 while (!feof($file)) {
333 $sig .= fgets($file, 1024);
334 }
335 fclose($file);
336 }
337 return $sig;
338}
37d5278d 339
340// vim: et ts=4