New icon theming. Also added basic template for left_main.php
[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 *
47ccfad4 8 * @copyright &copy; 1999-2006 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
ace4c62c 16/** @ignore */
17if (! defined('SM_PATH')) define('SM_PATH','../');
18
d6c32258 19/** include this for error messages */
b94bfe13 20include_once(SM_PATH . 'functions/display_messages.php');
21
3499f99f 22/**
23 * Check the preferences into the session cache.
ace4c62c 24 * @param string $data_dir
25 * @param string $username
26 * @since 1.1.3
3499f99f 27 */
28function cachePrefValues($data_dir, $username) {
29 global $prefs_are_cached, $prefs_cache;
324ac3c5 30
37d5278d 31 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
3499f99f 32 if ( isset($prefs_are_cached) && $prefs_are_cached) {
0d8a462c 33 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
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);
1c159927 76 /* this is to 'rescue' old-style highlighting rules. */
3499f99f 77 if (substr($key, 0, 9) == 'highlight') {
78 $key = 'highlight' . $highlight_num;
79 $highlight_num ++;
80 }
81
82 if ($value != '') {
83 $prefs_cache[$key] = $value;
84 }
85 }
491696a8 86 }
87 fclose($file);
3499f99f 88
89 $prefs_are_cached = TRUE;
90
9eb0fbd4 91 sqsession_register($prefs_cache, 'prefs_cache');
92 sqsession_register($prefs_are_cached, 'prefs_are_cached');
3499f99f 93}
324ac3c5 94
3499f99f 95/**
b94bfe13 96 * Return the value for the preference given by $string.
ace4c62c 97 * @param string $data_dir data directory
98 * @param string $username user name
99 * @param string $string preference name
100 * @param string $default (since 1.2.0) default preference value
101 * @return mixed
3499f99f 102 */
103function getPref($data_dir, $username, $string, $default = '') {
104 global $prefs_cache;
397a9d7b 105
be6f5c46 106 $result = do_hook_function('get_pref_override',array($username,$string));
107 if (!$result) {
324ac3c5 108 cachePrefValues($data_dir, $username);
109 if (isset($prefs_cache[$string])) {
0d8a462c 110 $result = $prefs_cache[$string];
324ac3c5 111 } else {
112 $result = do_hook_function('get_pref', array($username,$string));
113 if (!$result) {
114 $result = $default;
115 }
116 }
3499f99f 117 }
3499f99f 118 return ($result);
119}
120
121/**
122 * Save the preferences for this user.
ace4c62c 123 * @param string $data_dir data directory
124 * @param string $username user name
125 * @since 1.1.3
3499f99f 126 */
2b2967d4 127function savePrefValues($data_dir, $username) {
128 global $prefs_cache;
324ac3c5 129
3499f99f 130 $filename = getHashedFile($username, $data_dir, "$username.pref");
131
35b5fa13 132 /* Open the file for writing, or else display an error to the user. */
be6f5c46 133 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 134 {
be6f5c46 135 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 136 exit;
137 }
3499f99f 138 foreach ($prefs_cache as $Key => $Value) {
139 if (isset($Value)) {
3ecad5e6 140 if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
dabef6fd 141 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
142 exit;
143 }
3499f99f 144 }
145 }
146 fclose($file);
1d44a937 147 if (! @copy($filename . '.tmp',$filename) ) {
b94bfe13 148 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') );
149 exit;
150 }
dabef6fd 151 @unlink($filename . '.tmp');
4623cba9 152 @chmod($filename, 0600);
c4f32485 153 sqsession_register($prefs_cache , 'prefs_cache');
3499f99f 154}
155
156/**
157 * Remove a preference for the current user.
ace4c62c 158 * @param string $data_dir data directory
159 * @param string $username user name
160 * @param string $string preference name
3499f99f 161 */
162function removePref($data_dir, $username, $string) {
163 global $prefs_cache;
164
2b2967d4 165 cachePrefValues($data_dir, $username);
324ac3c5 166
3499f99f 167 if (isset($prefs_cache[$string])) {
168 unset($prefs_cache[$string]);
169 }
324ac3c5 170
2b2967d4 171 savePrefValues($data_dir, $username);
3499f99f 172}
173
174/**
175 * Set a there preference $string to $value.
ace4c62c 176 * @param string $data_dir data directory
177 * @param string $username user name
178 * @param string $string preference name
179 * @param mixed $value preference value
3499f99f 180 */
181function setPref($data_dir, $username, $string, $value) {
182 global $prefs_cache;
183
2b2967d4 184 cachePrefValues($data_dir, $username);
3499f99f 185 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
186 return;
187 }
188
189 if ($value === '') {
2b2967d4 190 removePref($data_dir, $username, $string);
191 return;
3499f99f 192 }
2b2967d4 193
194 $prefs_cache[$string] = $value;
195 savePrefValues($data_dir, $username);
3499f99f 196}
197
198/**
199 * Check for a preferences file. If one can not be found, create it.
ace4c62c 200 * @param string $data_dir data directory
201 * @param string $username user name
202 * @param string $filename (since 1.2.0) preference file name.
203 * detects file name, if set to empty string.
3499f99f 204 */
205function checkForPrefs($data_dir, $username, $filename = '') {
206 /* First, make sure we have the filename. */
207 if ($filename == '') {
bd1180c1 208 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 209 }
210
211 /* Then, check if the file exists. */
212 if (!@file_exists($filename) ) {
a04a3c20 213
214 /* If it does not exist, check for default_prefs */
f8a1ed5a 215
a04a3c20 216 /* First, check legacy locations: data dir */
e8c533f6 217 if(substr($data_dir,-1) != '/') {
218 $data_dir .= '/';
219 }
220 $default_pref = $data_dir . 'default_pref';
3499f99f 221
a04a3c20 222 /* or legacy location: internal data dir */
3499f99f 223 if (!@file_exists($default_pref)) {
bd9c880b 224 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 225 }
226
a04a3c20 227 /* If no legacies, check where we'd expect it to be located:
228 * under config/ */
229 if (!@file_exists($default_pref)) {
230 $default_pref = SM_PATH . 'config/default_pref';
231 }
f8a1ed5a 232
a04a3c20 233 /* If a default_pref file found, try to copy it, if none found,
234 * try to create an empty one. If that fails, report an error.
235 */
236 if (
237 ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
238 !@touch($filename)
239 ) {
305a1012 240 $uid = 'httpd';
241 if (function_exists('posix_getuid')){
242 $user_data = posix_getpwuid(posix_getuid());
243 $uid = $user_data['name'];
244 }
a04a3c20 245 $errTitle = _("Could not create initial preference file!");
246 $errString = $errTitle . "<br />\n" .
247 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
248 _("Please contact your system administrator and report this error.") . "<br />\n";
9be8198d 249 logout_error( $errString, $errTitle );
3499f99f 250 exit;
251 }
252 }
253}
254
255/**
256 * Write the User Signature.
ace4c62c 257 * @param string $data_dir data directory
258 * @param string $username user name
259 * @param integer $number (since 1.2.5) identity number.
260 * parameter was used for signature text before 1.2.5.
261 * @param string $value (since 1.2.5) signature text
3499f99f 262 */
01265fba 263function setSig($data_dir, $username, $number, $value) {
288df1a0 264 // Limit signature size to 64KB (database BLOB limit)
265 if (strlen($value)>65536) {
266 error_option_save(_("Signature is too big."));
267 return;
268 }
01265fba 269 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 270 /* Open the file for writing, or else display an error to the user. */
dabef6fd 271 if(!$file = @fopen("$filename.tmp", 'w')) {
dabef6fd 272 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 273 exit;
274 }
3ecad5e6 275 if ( sq_fwrite($file, $value) === FALSE ) {
dabef6fd 276 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
277 exit;
278 }
3499f99f 279 fclose($file);
1d44a937 280 if (! @copy($filename . '.tmp',$filename) ) {
281 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') );
282 exit;
283 }
dabef6fd 284 @unlink($filename . '.tmp');
4623cba9 285 @chmod($filename, 0600);
dabef6fd 286
3499f99f 287}
288
289/**
290 * Get the signature.
ace4c62c 291 * @param string $data_dir data directory
292 * @param string $username user name
293 * @param integer $number (since 1.2.5) identity number
294 * @return string signature
3499f99f 295 */
01265fba 296function getSig($data_dir, $username, $number) {
01265fba 297 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 298 $sig = '';
299 if (file_exists($filename)) {
35b5fa13 300 /* Open the file, or else display an error to the user. */
301 if(!$file = @fopen($filename, 'r'))
302 {
35b5fa13 303 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
304 exit;
305 }
3499f99f 306 while (!feof($file)) {
307 $sig .= fgets($file, 1024);
308 }
309 fclose($file);
310 }
311 return $sig;
312}
37d5278d 313
314// vim: et ts=4
f8a1ed5a 315?>