More rg=0
[squirrelmail.git] / functions / file_prefs.php
1 <?php
2
3 /**
4 * file_prefs.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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
14 /**
15 * Check the preferences into the session cache.
16 */
17 function 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
24 sqsession_unregister('prefs_cache');
25 sqsession_unregister('prefs_are_cached');
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)) {
36 include_once(SM_PATH . 'functions/display_messages.php');
37 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
38 exit;
39 }
40
41 /* Open the file, or else display an error to the user. */
42 if(!$file = @fopen($filename, 'r'))
43 {
44 include_once(SM_PATH . 'functions/display_messages.php');
45 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
46 exit;
47 }
48
49 /* Read in the preferences. */
50 $highlight_num = 0;
51 while (! feof($file)) {
52 $pref = trim(fgets($file, 1024));
53 $equalsAt = strpos($pref, '=');
54 if ($equalsAt > 0) {
55 $key = substr($pref, 0, $equalsAt);
56 $value = substr($pref, $equalsAt + 1);
57 if (substr($key, 0, 9) == 'highlight') {
58 $key = 'highlight' . $highlight_num;
59 $highlight_num ++;
60 }
61
62 if ($value != '') {
63 $prefs_cache[$key] = $value;
64 }
65 }
66 }
67 fclose($file);
68
69 $prefs_are_cached = TRUE;
70
71 sqsession_register($prefs_cache, 'prefs_cache');
72 sqsession_register($prefs_are_cached, 'prefs_are_cached');
73 }
74
75 /**
76 * Return the value for the prefernce given by $string.
77 */
78 function getPref($data_dir, $username, $string, $default = '') {
79 global $prefs_cache;
80 $result = '';
81
82 cachePrefValues($data_dir, $username);
83
84 if (isset($prefs_cache[$string])) {
85 $result = $prefs_cache[$string];
86 } else {
87 $result = $default;
88 }
89
90 return ($result);
91 }
92
93 /**
94 * Save the preferences for this user.
95 */
96 function savePrefValues($data_dir, $username) {
97 global $prefs_cache;
98
99 $filename = getHashedFile($username, $data_dir, "$username.pref");
100
101 /* Open the file for writing, or else display an error to the user. */
102 if(!$file = @fopen($filename, 'w'))
103 {
104 include_once(SM_PATH . 'functions/display_messages.php');
105 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
106 exit;
107 }
108
109 foreach ($prefs_cache as $Key => $Value) {
110 if (isset($Value)) {
111 fwrite($file, $Key . '=' . $Value . "\n");
112 }
113 }
114 fclose($file);
115 chmod($filename, 0600);
116 }
117
118 /**
119 * Remove a preference for the current user.
120 */
121 function removePref($data_dir, $username, $string) {
122 global $prefs_cache;
123
124 cachePrefValues($data_dir, $username);
125
126 if (isset($prefs_cache[$string])) {
127 unset($prefs_cache[$string]);
128 }
129
130 savePrefValues($data_dir, $username);
131 }
132
133 /**
134 * Set a there preference $string to $value.
135 */
136 function setPref($data_dir, $username, $string, $value) {
137 global $prefs_cache;
138
139 cachePrefValues($data_dir, $username);
140 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
141 return;
142 }
143
144 if ($value === '') {
145 removePref($data_dir, $username, $string);
146 return;
147 }
148
149 $prefs_cache[$string] = $value;
150 savePrefValues($data_dir, $username);
151 }
152
153 /**
154 * Check for a preferences file. If one can not be found, create it.
155 */
156 function checkForPrefs($data_dir, $username, $filename = '') {
157 /* First, make sure we have the filename. */
158 if ($filename == '') {
159 $filename = getHashedFile($username, $data_dir, "$username.pref");
160 }
161
162 /* Then, check if the file exists. */
163 if (!@file_exists($filename) ) {
164 /* First, check the $data_dir for the default preference file. */
165 $default_pref = $data_dir . 'default_pref';
166
167 /* If it is not there, check the internal data directory. */
168 if (!@file_exists($default_pref)) {
169 $default_pref = SM_PATH . 'data/default_pref';
170 }
171
172 /* Otherwise, report an error. */
173 $errTitle = sprintf( _("Error opening %s"), $default_pref );
174 if (!file_exists($default_pref)) {
175 $errString = $errTitle . "<br>\n" .
176 _("Default preference file not found!") . "<br>\n" .
177 _("Please contact your system administrator and report this error.") . "<br>\n";
178 include_once(SM_PATH . 'functions/display_messages.php' );
179 logout_error( $errString, $errTitle );
180 exit;
181 } else if (!@copy($default_pref, $filename)) {
182 $uid = 'httpd';
183 if (function_exists('posix_getuid')){
184 $user_data = posix_getpwuid(posix_getuid());
185 $uid = $user_data['name'];
186 }
187 $errString = $errTitle . '<br>' .
188 _("Could not create initial preference file!") . "<br>\n" .
189 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
190 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
191 include_once(SM_PATH . 'functions/display_messages.php' );
192 logout_error( $errString, $errTitle );
193 exit;
194 }
195 }
196 }
197
198 /**
199 * Write the User Signature.
200 */
201 function setSig($data_dir, $username, $number, $value) {
202 $filename = getHashedFile($username, $data_dir, "$username.si$number");
203 /* Open the file for writing, or else display an error to the user. */
204 if(!$file = @fopen($filename, 'w'))
205 {
206 include_once(SM_PATH . '/functions/display_messages.php' );
207 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
208 exit;
209 }
210 fwrite($file, $value);
211 fclose($file);
212 }
213
214 /**
215 * Get the signature.
216 */
217 function getSig($data_dir, $username, $number) {
218 $filename = getHashedFile($username, $data_dir, "$username.si$number");
219 $sig = '';
220 if (file_exists($filename)) {
221 /* Open the file, or else display an error to the user. */
222 if(!$file = @fopen($filename, 'r'))
223 {
224 include_once(SM_PATH . 'functions/display_messages.php');
225 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
226 exit;
227 }
228 while (!feof($file)) {
229 $sig .= fgets($file, 1024);
230 }
231 fclose($file);
232 }
233 return $sig;
234 }