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