should be false by default (sorry, again) ;-P
[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
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 echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
39 exit;
40 }
41
42 $file = fopen($filename, 'r');
43
44 /* Read in the preferences. */
45 $highlight_num = 0;
46 while (! feof($file)) {
47 $pref = trim(fgets($file, 1024));
48 $equalsAt = strpos($pref, '=');
49 if ($equalsAt > 0) {
50 $key = substr($pref, 0, $equalsAt);
51 $value = substr($pref, $equalsAt + 1);
52 if (substr($key, 0, 9) == 'highlight') {
53 $key = 'highlight' . $highlight_num;
54 $highlight_num ++;
55 }
56
57 if ($value != '') {
58 $prefs_cache[$key] = $value;
59 }
60 }
61 }
62 fclose($file);
63
64 $prefs_are_cached = TRUE;
65
66 session_register('prefs_cache');
67 session_register('prefs_are_cached');
68}
69
70/**
71 * Return the value for the prefernce given by $string.
72 */
73function getPref($data_dir, $username, $string, $default = '') {
74 global $prefs_cache;
75 $result = '';
76
77 cachePrefValues($data_dir, $username);
78
79 if (isset($prefs_cache[$string])) {
80 $result = $prefs_cache[$string];
81 } else {
82 $result = $default;
83 }
84
85 return ($result);
86}
87
88/**
89 * Save the preferences for this user.
90 */
91function savePrefValues($data_dir, $username) {
92 global $prefs_cache;
93
94 $filename = getHashedFile($username, $data_dir, "$username.pref");
95
96 $file = fopen($filename, 'w');
97 foreach ($prefs_cache as $Key => $Value) {
98 if (isset($Value)) {
99 fwrite($file, $Key . '=' . $Value . "\n");
100 }
101 }
102 fclose($file);
103 chmod($filename, 0600);
104}
105
106/**
107 * Remove a preference for the current user.
108 */
109function removePref($data_dir, $username, $string) {
110 global $prefs_cache;
111
112 cachePrefValues($data_dir, $username);
113
114 if (isset($prefs_cache[$string])) {
115 unset($prefs_cache[$string]);
116 }
117
118 savePrefValues($data_dir, $username);
119}
120
121/**
122 * Set a there preference $string to $value.
123 */
124function setPref($data_dir, $username, $string, $value) {
125 global $prefs_cache;
126
127 cachePrefValues($data_dir, $username);
128 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
129 return;
130 }
131
132 if ($value === '') {
133 removePref($data_dir, $username, $string);
134 return;
135 }
136
137 $prefs_cache[$string] = $value;
138 savePrefValues($data_dir, $username);
139}
140
141/**
142 * Check for a preferences file. If one can not be found, create it.
143 */
144function checkForPrefs($data_dir, $username, $filename = '') {
145 /* First, make sure we have the filename. */
146 if ($filename == '') {
147 $filename = getHashedFile($username, $data_dir, '$username.pref');
148 }
149
150 /* Then, check if the file exists. */
151 if (!@file_exists($filename) ) {
152 /* First, check the $data_dir for the default preference file. */
153 $default_pref = $data_dir . 'default_pref';
154
155 /* If it is not there, check the internal data directory. */
156 if (!@file_exists($default_pref)) {
157 $default_pref = '../data/default_pref';
158 }
159
160 /* Otherwise, report an error. */
161 if (!file_exists($default_pref)) {
162 echo _("Error opening ") . $default_pref . "<br>\n";
163 echo _("Default preference file not found!") . "<br>\n";
164 echo _("Please contact your system administrator and report this error.") . "<br>\n";
165 exit;
166 } else if (!@copy($default_pref, $filename)) {
167 echo _("Error opening ") . $default_pref . '<br>';
168 echo _("Could not create initial preference file!") . "<br>\n";
169 echo _("Please contact your system administrator and report this error.") . "<br>\n";
170 exit;
171 }
172 }
173}
174
175/**
176 * Write the User Signature.
177 */
178function setSig($data_dir, $username, $value) {
179 $filename = getHashedFile($username, $data_dir, "$username.sig");
180 $file = fopen($filename, 'w');
181 fwrite($file, $value);
182 fclose($file);
183}
184
185/**
186 * Get the signature.
187 */
188function getSig($data_dir, $username) {
189 #$filename = $data_dir . $username . '.sig';
190 $filename = getHashedFile($username, $data_dir, "$username.sig");
191 $sig = '';
192 if (file_exists($filename)) {
193 $file = fopen($filename, 'r');
194 while (!feof($file)) {
195 $sig .= fgets($file, 1024);
196 }
197 fclose($file);
198 }
199 return $sig;
200}