encoding stuff
[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) {
21 return;
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');
3499f99f 82}
83
84/**
85 * Return the value for the prefernce given by $string.
86 */
87function getPref($data_dir, $username, $string, $default = '') {
88 global $prefs_cache;
397a9d7b 89
be6f5c46 90 $result = do_hook_function('get_pref_override',array($username,$string));
91 if (!$result) {
92 cachePrefValues($data_dir, $username);
93 if (isset($prefs_cache[$string])) {
94 $result = $prefs_cache[$string];
95 } else {
96 $result = do_hook_function('get_pref', array($username,$string));
97 if (!$result) {
98 $result = $default;
99 }
100 }
3499f99f 101 }
3499f99f 102 return ($result);
103}
104
105/**
106 * Save the preferences for this user.
107 */
108function savePrefValues($data_dir, $username) {
109 global $prefs_cache;
110
111 $filename = getHashedFile($username, $data_dir, "$username.pref");
112
35b5fa13 113 /* Open the file for writing, or else display an error to the user. */
be6f5c46 114 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 115 {
bd9c880b 116 include_once(SM_PATH . 'functions/display_messages.php');
be6f5c46 117 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 118 exit;
119 }
3499f99f 120 foreach ($prefs_cache as $Key => $Value) {
121 if (isset($Value)) {
dabef6fd 122 $tmpwrite = @fwrite($file, $Key . '=' . $Value . "\n");
123 if ($tmpwrite == -1) {
124 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
125 exit;
126 }
3499f99f 127 }
128 }
129 fclose($file);
dabef6fd 130 @copy($filename . '.tmp',$filename);
131 @unlink($filename . '.tmp');
3499f99f 132 chmod($filename, 0600);
133}
134
135/**
136 * Remove a preference for the current user.
137 */
138function removePref($data_dir, $username, $string) {
139 global $prefs_cache;
140
141 cachePrefValues($data_dir, $username);
142
143 if (isset($prefs_cache[$string])) {
144 unset($prefs_cache[$string]);
145 }
146
147 savePrefValues($data_dir, $username);
148}
149
150/**
151 * Set a there preference $string to $value.
152 */
153function setPref($data_dir, $username, $string, $value) {
154 global $prefs_cache;
155
156 cachePrefValues($data_dir, $username);
157 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
158 return;
159 }
160
161 if ($value === '') {
162 removePref($data_dir, $username, $string);
163 return;
164 }
165
166 $prefs_cache[$string] = $value;
167 savePrefValues($data_dir, $username);
168}
169
170/**
171 * Check for a preferences file. If one can not be found, create it.
172 */
173function checkForPrefs($data_dir, $username, $filename = '') {
174 /* First, make sure we have the filename. */
175 if ($filename == '') {
bd1180c1 176 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 177 }
178
179 /* Then, check if the file exists. */
180 if (!@file_exists($filename) ) {
181 /* First, check the $data_dir for the default preference file. */
a9e6867d 182 $default_pref = $data_dir . '/default_pref';
3499f99f 183
184 /* If it is not there, check the internal data directory. */
185 if (!@file_exists($default_pref)) {
bd9c880b 186 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 187 }
188
189 /* Otherwise, report an error. */
9be8198d 190 $errTitle = sprintf( _("Error opening %s"), $default_pref );
921bea1a 191 if (!is_readable($default_pref)) {
9be8198d 192 $errString = $errTitle . "<br>\n" .
921bea1a 193 _("Default preference file not found or not readable!") . "<br>\n" .
9be8198d 194 _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 195 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 196 logout_error( $errString, $errTitle );
3499f99f 197 exit;
198 } else if (!@copy($default_pref, $filename)) {
305a1012 199 $uid = 'httpd';
200 if (function_exists('posix_getuid')){
201 $user_data = posix_getpwuid(posix_getuid());
202 $uid = $user_data['name'];
203 }
9be8198d 204 $errString = $errTitle . '<br>' .
205 _("Could not create initial preference file!") . "<br>\n" .
206 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
207 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 208 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 209 logout_error( $errString, $errTitle );
3499f99f 210 exit;
211 }
212 }
213}
214
215/**
216 * Write the User Signature.
217 */
01265fba 218function setSig($data_dir, $username, $number, $value) {
219 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 220 /* Open the file for writing, or else display an error to the user. */
dabef6fd 221 if(!$file = @fopen("$filename.tmp", 'w')) {
222 include_once( '../functions/display_messages.php' );
223 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 224 exit;
225 }
dabef6fd 226 $tmpwrite = @fwrite($file, $value);
227 if ($tmpwrite == -1) {
228 include_once( '../functions/display_messages.php' );
229 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
230 exit;
231 }
3499f99f 232 fclose($file);
dabef6fd 233 @copy($filename . '.tmp',$filename);
234 @unlink($filename . '.tmp');
235 chmod($filename, 0600);
236
3499f99f 237}
238
239/**
240 * Get the signature.
241 */
01265fba 242function getSig($data_dir, $username, $number) {
01265fba 243 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 244 $sig = '';
245 if (file_exists($filename)) {
35b5fa13 246 /* Open the file, or else display an error to the user. */
247 if(!$file = @fopen($filename, 'r'))
248 {
bd9c880b 249 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 250 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
251 exit;
252 }
3499f99f 253 while (!feof($file)) {
254 $sig .= fgets($file, 1024);
255 }
256 fclose($file);
257 }
258 return $sig;
259}