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