Make the filters plugin more efficient and remove two E_ALL warnings
[squirrelmail.git] / functions / file_prefs.php
CommitLineData
3499f99f 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
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)) {
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
9eb0fbd4 71 sqsession_register($prefs_cache, 'prefs_cache');
72 sqsession_register($prefs_are_cached, 'prefs_are_cached');
3499f99f 73}
74
75/**
76 * Return the value for the prefernce given by $string.
77 */
78function getPref($data_dir, $username, $string, $default = '') {
79 global $prefs_cache;
397a9d7b 80
be6f5c46 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 }
3499f99f 92 }
3499f99f 93 return ($result);
94}
95
96/**
97 * Save the preferences for this user.
98 */
99function savePrefValues($data_dir, $username) {
100 global $prefs_cache;
101
102 $filename = getHashedFile($username, $data_dir, "$username.pref");
103
35b5fa13 104 /* Open the file for writing, or else display an error to the user. */
be6f5c46 105 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 106 {
bd9c880b 107 include_once(SM_PATH . 'functions/display_messages.php');
be6f5c46 108 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 109 exit;
110 }
3499f99f 111 foreach ($prefs_cache as $Key => $Value) {
112 if (isset($Value)) {
dabef6fd 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 }
3499f99f 118 }
119 }
120 fclose($file);
dabef6fd 121 @copy($filename . '.tmp',$filename);
122 @unlink($filename . '.tmp');
3499f99f 123 chmod($filename, 0600);
124}
125
126/**
127 * Remove a preference for the current user.
128 */
129function 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 */
144function 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 */
164function checkForPrefs($data_dir, $username, $filename = '') {
165 /* First, make sure we have the filename. */
166 if ($filename == '') {
bd1180c1 167 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 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)) {
bd9c880b 177 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 178 }
179
180 /* Otherwise, report an error. */
9be8198d 181 $errTitle = sprintf( _("Error opening %s"), $default_pref );
3499f99f 182 if (!file_exists($default_pref)) {
9be8198d 183 $errString = $errTitle . "<br>\n" .
184 _("Default preference file not found!") . "<br>\n" .
185 _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 186 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 187 logout_error( $errString, $errTitle );
3499f99f 188 exit;
189 } else if (!@copy($default_pref, $filename)) {
305a1012 190 $uid = 'httpd';
191 if (function_exists('posix_getuid')){
192 $user_data = posix_getpwuid(posix_getuid());
193 $uid = $user_data['name'];
194 }
9be8198d 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";
bd9c880b 199 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 200 logout_error( $errString, $errTitle );
3499f99f 201 exit;
202 }
203 }
204}
205
206/**
207 * Write the User Signature.
208 */
01265fba 209function setSig($data_dir, $username, $number, $value) {
210 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 211 /* Open the file for writing, or else display an error to the user. */
dabef6fd 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') );
35b5fa13 215 exit;
216 }
dabef6fd 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 }
3499f99f 223 fclose($file);
dabef6fd 224 @copy($filename . '.tmp',$filename);
225 @unlink($filename . '.tmp');
226 chmod($filename, 0600);
227
3499f99f 228}
229
230/**
231 * Get the signature.
232 */
01265fba 233function getSig($data_dir, $username, $number) {
01265fba 234 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 235 $sig = '';
236 if (file_exists($filename)) {
35b5fa13 237 /* Open the file, or else display an error to the user. */
238 if(!$file = @fopen($filename, 'r'))
239 {
bd9c880b 240 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 241 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
242 exit;
243 }
3499f99f 244 while (!feof($file)) {
245 $sig .= fgets($file, 1024);
246 }
247 fclose($file);
248 }
249 return $sig;
250}